client.c 155 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. ((func_args*)args)->return_code = -1; /* error state */
  1930. #ifndef NO_RSA
  1931. verifyCert = caCertFile;
  1932. ourCert = cliCertFile;
  1933. ourKey = cliKeyFile;
  1934. #else
  1935. #ifdef HAVE_ECC
  1936. verifyCert = caEccCertFile;
  1937. ourCert = cliEccCertFile;
  1938. ourKey = cliEccKeyFile;
  1939. #elif defined(HAVE_ED25519)
  1940. verifyCert = caEdCertFile;
  1941. ourCert = cliEdCertFile;
  1942. ourKey = cliEdKeyFile;
  1943. #elif defined(HAVE_ED448)
  1944. verifyCert = caEd448CertFile;
  1945. ourCert = cliEd448CertFile;
  1946. ourKey = cliEd448KeyFile;
  1947. #else
  1948. verifyCert = NULL;
  1949. ourCert = NULL;
  1950. ourKey = NULL;
  1951. #endif
  1952. #endif
  1953. (void)session;
  1954. (void)sslResume;
  1955. (void)atomicUser;
  1956. (void)scr;
  1957. (void)forceScr;
  1958. (void)scrAppData;
  1959. (void)resumeScr;
  1960. (void)ourKey;
  1961. (void)ourCert;
  1962. (void)verifyCert;
  1963. (void)useClientCert;
  1964. (void)disableCRL;
  1965. (void)minDhKeyBits;
  1966. (void)alpnList;
  1967. (void)alpn_opt;
  1968. (void)updateKeysIVs;
  1969. (void)earlyData;
  1970. (void)useX25519;
  1971. (void)useX448;
  1972. (void)helloRetry;
  1973. (void)onlyKeyShare;
  1974. (void)useSupCurve;
  1975. (void)loadCertKeyIntoSSLObj;
  1976. (void)usePqc;
  1977. (void)pqcAlg;
  1978. (void)opensslPsk;
  1979. StackTrap();
  1980. /* Reinitialize the global myVerifyAction. */
  1981. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  1982. #ifndef WOLFSSL_VXWORKS
  1983. /* Not used: All used */
  1984. while ((ch = mygetopt_long(argc, argv, "?:"
  1985. "ab:c:defgh:i;jk:l:mnop:q:rstu;v:wxyz"
  1986. "A:B:CDE:F:GH:IJKL:M:NO:PQRS:TUVW:XYZ:"
  1987. "01:23:4567:89"
  1988. "@#", long_options, 0)) != -1) {
  1989. switch (ch) {
  1990. case '?' :
  1991. if(myoptarg!=NULL) {
  1992. lng_index = atoi(myoptarg);
  1993. if(lng_index<0||lng_index>1){
  1994. lng_index = 0;
  1995. }
  1996. }
  1997. Usage();
  1998. XEXIT_T(EXIT_SUCCESS);
  1999. case 257 :
  2000. lng_index = 0;
  2001. Usage();
  2002. XEXIT_T(EXIT_SUCCESS);
  2003. case 258 :
  2004. lng_index = 1;
  2005. Usage();
  2006. XEXIT_T(EXIT_SUCCESS);
  2007. case 'g' :
  2008. sendGET = 1;
  2009. break;
  2010. case 'd' :
  2011. doPeerCheck = 0;
  2012. break;
  2013. case 'e' :
  2014. ShowCiphers();
  2015. XEXIT_T(EXIT_SUCCESS);
  2016. case 'D' :
  2017. myVerifyAction = VERIFY_OVERRIDE_DATE_ERR;
  2018. break;
  2019. case 'C' :
  2020. #ifdef HAVE_CRL
  2021. disableCRL = 1;
  2022. #endif
  2023. break;
  2024. case 'u' :
  2025. doDTLS = 1;
  2026. dtlsUDP = 1;
  2027. #if (defined(WOLFSSL_SCTP) || defined(WOLFSSL_DTLS_MTU)) && \
  2028. defined(WOLFSSL_DTLS)
  2029. dtlsMTU = atoi(myoptarg);
  2030. #endif
  2031. break;
  2032. #ifdef WOLFSSL_SRTP
  2033. case 260:
  2034. doDTLS = 1;
  2035. dtlsUDP = 1;
  2036. dtlsSrtpProfiles = myoptarg != NULL ? myoptarg :
  2037. "SRTP_AES128_CM_SHA1_80";
  2038. printf("Using SRTP Profile(s): %s\n", dtlsSrtpProfiles);
  2039. break;
  2040. #endif
  2041. #ifdef WOLFSSL_DTLS13
  2042. case 261:
  2043. #ifdef HAVE_SESSION_TICKET
  2044. waitTicket = 1;
  2045. #endif /* HAVE_SESSION_TICKET */
  2046. break;
  2047. #endif /* WOLFSSL_DTLS13 */
  2048. #ifdef WOLFSSL_DTLS_CID
  2049. case 262:
  2050. useDtlsCID = 1;
  2051. if (myoptarg != NULL) {
  2052. if (XSTRLEN(myoptarg) >= DTLS_CID_BUFFER_SIZE) {
  2053. err_sys("provided connection ID is too big");
  2054. }
  2055. else {
  2056. strcpy(dtlsCID, myoptarg);
  2057. }
  2058. }
  2059. break;
  2060. #endif /* WOLFSSL_CID */
  2061. case 'G' :
  2062. #ifdef WOLFSSL_SCTP
  2063. doDTLS = 1;
  2064. dtlsUDP = 1;
  2065. dtlsSCTP = 1;
  2066. #endif
  2067. break;
  2068. case 's' :
  2069. usePsk = 1;
  2070. break;
  2071. #ifdef WOLFSSL_TRUST_PEER_CERT
  2072. case 'E' :
  2073. trustCert = myoptarg;
  2074. break;
  2075. #endif
  2076. case 'm' :
  2077. matchName = 1;
  2078. break;
  2079. case 'x' :
  2080. useClientCert = 0;
  2081. break;
  2082. case 'X' :
  2083. externalTest = 1;
  2084. break;
  2085. case 'f' :
  2086. fewerPackets = 1;
  2087. break;
  2088. case 'U' :
  2089. #if defined(ATOMIC_USER) && !defined(WOLFSSL_AEAD_ONLY)
  2090. atomicUser = 1;
  2091. #endif
  2092. break;
  2093. case 'P' :
  2094. #ifdef HAVE_PK_CALLBACKS
  2095. pkCallbacks = 1;
  2096. #endif
  2097. break;
  2098. case 'h' :
  2099. host = myoptarg;
  2100. domain = myoptarg;
  2101. break;
  2102. case 'p' :
  2103. port = (word16)atoi(myoptarg);
  2104. #if !defined(NO_MAIN_DRIVER) || defined(USE_WINDOWS_API)
  2105. if (port == 0)
  2106. err_sys("port number cannot be 0");
  2107. #endif
  2108. break;
  2109. case 'v' :
  2110. if (myoptarg[0] == 'd') {
  2111. version = CLIENT_DOWNGRADE_VERSION;
  2112. break;
  2113. }
  2114. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2115. else if (myoptarg[0] == 'e') {
  2116. version = EITHER_DOWNGRADE_VERSION;
  2117. #ifndef NO_CERTS
  2118. loadCertKeyIntoSSLObj = 1;
  2119. #endif
  2120. break;
  2121. }
  2122. #endif
  2123. version = atoi(myoptarg);
  2124. if (version < 0 || version > 4) {
  2125. Usage();
  2126. XEXIT_T(MY_EX_USAGE);
  2127. }
  2128. break;
  2129. case 'V' :
  2130. ShowVersions();
  2131. XEXIT_T(EXIT_SUCCESS);
  2132. case 'l' :
  2133. cipherList = myoptarg;
  2134. break;
  2135. case 'H' :
  2136. if (XSTRCMP(myoptarg, "defCipherList") == 0) {
  2137. printf("Using default cipher list for testing\n");
  2138. useDefCipherList = 1;
  2139. }
  2140. else if (XSTRCMP(myoptarg, "exitWithRet") == 0) {
  2141. printf("Skip exit() for testing\n");
  2142. exitWithRet = 1;
  2143. }
  2144. else if (XSTRCMP(myoptarg, "verifyFail") == 0) {
  2145. printf("Verify should fail\n");
  2146. myVerifyAction = VERIFY_FORCE_FAIL;
  2147. }
  2148. else if (XSTRCMP(myoptarg, "verifyInfo") == 0) {
  2149. printf("Verify should not override error\n");
  2150. myVerifyAction = VERIFY_USE_PREVERFIY;
  2151. }
  2152. else if (XSTRCMP(myoptarg, "useSupCurve") == 0) {
  2153. printf("Attempting to test use supported curve\n");
  2154. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  2155. useSupCurve = 1;
  2156. #else
  2157. printf("Supported curves not compiled in!\n");
  2158. #endif
  2159. }
  2160. else if (XSTRCMP(myoptarg, "loadSSL") == 0) {
  2161. printf("Load cert/key into wolfSSL object\n");
  2162. #ifndef NO_CERTS
  2163. loadCertKeyIntoSSLObj = 1;
  2164. #else
  2165. printf("Certs turned off with NO_CERTS!\n");
  2166. #endif
  2167. }
  2168. else if (XSTRCMP(myoptarg, "disallowETM") == 0) {
  2169. printf("Disallow Encrypt-Then-MAC\n");
  2170. #ifdef HAVE_ENCRYPT_THEN_MAC
  2171. disallowETM = 1;
  2172. #endif
  2173. }
  2174. else {
  2175. Usage();
  2176. XEXIT_T(MY_EX_USAGE);
  2177. }
  2178. break;
  2179. case 'A' :
  2180. customVerifyCert = 1;
  2181. verifyCert = myoptarg;
  2182. break;
  2183. case 'c' :
  2184. ourCert = myoptarg;
  2185. break;
  2186. case 'k' :
  2187. ourKey = myoptarg;
  2188. break;
  2189. case 'Z' :
  2190. #ifndef NO_DH
  2191. minDhKeyBits = atoi(myoptarg);
  2192. if (minDhKeyBits <= 0 || minDhKeyBits > 16000) {
  2193. Usage();
  2194. XEXIT_T(MY_EX_USAGE);
  2195. }
  2196. #endif
  2197. break;
  2198. case 'b' :
  2199. benchmark = atoi(myoptarg);
  2200. if (benchmark < 0 || benchmark > 1000000) {
  2201. Usage();
  2202. XEXIT_T(MY_EX_USAGE);
  2203. }
  2204. break;
  2205. case 'B' :
  2206. throughput = atol(myoptarg);
  2207. for (; *myoptarg != '\0'; myoptarg++) {
  2208. if (*myoptarg == ',') {
  2209. block = atoi(myoptarg + 1);
  2210. break;
  2211. }
  2212. }
  2213. if (throughput == 0 || block <= 0) {
  2214. Usage();
  2215. XEXIT_T(MY_EX_USAGE);
  2216. }
  2217. break;
  2218. case 'N' :
  2219. nonBlocking = 1;
  2220. break;
  2221. case 'r' :
  2222. resumeSession = 1;
  2223. break;
  2224. case 'w' :
  2225. wc_shutdown = 1;
  2226. break;
  2227. case 'R' :
  2228. #ifdef HAVE_SECURE_RENEGOTIATION
  2229. scr = 1;
  2230. #endif
  2231. break;
  2232. case 'i' :
  2233. #ifdef HAVE_SECURE_RENEGOTIATION
  2234. scr = 1;
  2235. forceScr = 1;
  2236. if (XSTRCMP(myoptarg, "scr-app-data") == 0) {
  2237. scrAppData = 1;
  2238. }
  2239. #endif
  2240. break;
  2241. case 'z' :
  2242. #ifndef WOLFSSL_LEANPSK
  2243. wolfSSL_GetObjectSize();
  2244. #endif
  2245. break;
  2246. case 'S' :
  2247. if (XSTRCMP(myoptarg, "check") == 0) {
  2248. #ifdef HAVE_SNI
  2249. printf("SNI is: ON\n");
  2250. #else
  2251. printf("SNI is: OFF\n");
  2252. #endif
  2253. XEXIT_T(EXIT_SUCCESS);
  2254. }
  2255. #ifdef HAVE_SNI
  2256. sniHostName = myoptarg;
  2257. #endif
  2258. break;
  2259. case 'F' :
  2260. #ifdef HAVE_MAX_FRAGMENT
  2261. maxFragment = atoi(myoptarg);
  2262. if (maxFragment < WOLFSSL_MFL_MIN ||
  2263. maxFragment > WOLFSSL_MFL_MAX) {
  2264. Usage();
  2265. XEXIT_T(MY_EX_USAGE);
  2266. }
  2267. #endif
  2268. break;
  2269. case 'T' :
  2270. #ifdef HAVE_TRUNCATED_HMAC
  2271. truncatedHMAC = 1;
  2272. #endif
  2273. break;
  2274. case 'n' :
  2275. #ifdef HAVE_EXTENDED_MASTER
  2276. disableExtMasterSecret = 1;
  2277. #endif
  2278. break;
  2279. case 'W' :
  2280. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  2281. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2282. {
  2283. word32 myoptargSz;
  2284. statusRequest = atoi(myoptarg);
  2285. if (statusRequest > OCSP_STAPLING_OPT_MAX) {
  2286. Usage();
  2287. XEXIT_T(MY_EX_USAGE);
  2288. }
  2289. myoptargSz = (word32)XSTRLEN(myoptarg);
  2290. if (myoptargSz > 0 &&
  2291. XTOUPPER((unsigned char)myoptarg[myoptargSz-1]) == 'M') {
  2292. mustStaple = 1;
  2293. }
  2294. }
  2295. #endif
  2296. break;
  2297. case 'o' :
  2298. #ifdef HAVE_OCSP
  2299. useOcsp = 1;
  2300. #endif
  2301. break;
  2302. case 'O' :
  2303. #ifdef HAVE_OCSP
  2304. useOcsp = 1;
  2305. ocspUrl = myoptarg;
  2306. #endif
  2307. break;
  2308. case 'a' :
  2309. #ifdef HAVE_ANON
  2310. useAnon = 1;
  2311. #endif
  2312. break;
  2313. case 'L' :
  2314. #ifdef HAVE_ALPN
  2315. alpnList = myoptarg;
  2316. if (alpnList[0] == 'C' && alpnList[1] == ':')
  2317. alpn_opt = WOLFSSL_ALPN_CONTINUE_ON_MISMATCH;
  2318. else if (alpnList[0] == 'F' && alpnList[1] == ':')
  2319. alpn_opt = WOLFSSL_ALPN_FAILED_ON_MISMATCH;
  2320. else {
  2321. Usage();
  2322. XEXIT_T(MY_EX_USAGE);
  2323. }
  2324. alpnList += 2;
  2325. #endif
  2326. break;
  2327. case 'M' :
  2328. doSTARTTLS = 1;
  2329. starttlsProt = myoptarg;
  2330. if (XSTRCMP(starttlsProt, "smtp") != 0) {
  2331. Usage();
  2332. XEXIT_T(MY_EX_USAGE);
  2333. }
  2334. break;
  2335. case 'q' :
  2336. #ifdef HAVE_WNR
  2337. wnrConfigFile = myoptarg;
  2338. #endif
  2339. break;
  2340. case 'J' :
  2341. #ifdef WOLFSSL_TLS13
  2342. helloRetry = 1;
  2343. #endif
  2344. break;
  2345. case 'K' :
  2346. #ifdef WOLFSSL_TLS13
  2347. noPskDheKe = 1;
  2348. #endif
  2349. break;
  2350. case 'I' :
  2351. #ifdef WOLFSSL_TLS13
  2352. updateKeysIVs = 1;
  2353. #endif
  2354. break;
  2355. case 'y' :
  2356. #if defined(WOLFSSL_TLS13) && \
  2357. defined(HAVE_SUPPORTED_CURVES) && !defined(NO_DH)
  2358. onlyKeyShare = 1;
  2359. #endif
  2360. break;
  2361. case 'Y' :
  2362. #if defined(WOLFSSL_TLS13) && \
  2363. defined(HAVE_SUPPORTED_CURVES) && defined(HAVE_ECC)
  2364. onlyKeyShare = 2;
  2365. #endif
  2366. break;
  2367. case 'j' :
  2368. useVerifyCb = 1;
  2369. break;
  2370. case 't' :
  2371. #ifdef HAVE_CURVE25519
  2372. useX25519 = 1;
  2373. #ifdef HAVE_ECC
  2374. useSupCurve = 1;
  2375. #if defined(WOLFSSL_TLS13) && \
  2376. defined(HAVE_SUPPORTED_CURVES)
  2377. onlyKeyShare = 2;
  2378. #endif
  2379. #endif
  2380. #endif
  2381. break;
  2382. case 'Q' :
  2383. #if defined(WOLFSSL_TLS13) && \
  2384. defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  2385. postHandAuth = 1;
  2386. #endif
  2387. break;
  2388. case '0' :
  2389. #ifdef WOLFSSL_EARLY_DATA
  2390. earlyData = 1;
  2391. #endif
  2392. break;
  2393. case '1' :
  2394. lng_index = atoi(myoptarg);
  2395. if(lng_index<0||lng_index>1){
  2396. lng_index = 0;
  2397. }
  2398. break;
  2399. case '2' :
  2400. #if !defined(NO_DH) && !defined(HAVE_FIPS) && \
  2401. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_OLD_PRIME_CHECK)
  2402. doDhKeyCheck = 0;
  2403. #endif
  2404. break;
  2405. case '3' :
  2406. #ifdef WOLFSSL_MULTICAST
  2407. doMcast = 1;
  2408. mcastID = (byte)(atoi(myoptarg) & 0xFF);
  2409. #endif
  2410. break;
  2411. case '4' :
  2412. #ifdef HAVE_SECURE_RENEGOTIATION
  2413. scr = 1;
  2414. forceScr = 1;
  2415. resumeScr = 1;
  2416. #endif
  2417. break;
  2418. case '5' :
  2419. #ifdef HAVE_TRUSTED_CA
  2420. trustedCaKeyId = 1;
  2421. #endif /* HAVE_TRUSTED_CA */
  2422. break;
  2423. case '6' :
  2424. #ifdef WOLFSSL_ASYNC_IO
  2425. nonBlocking = 1;
  2426. simulateWantWrite = 1;
  2427. #else
  2428. LOG_ERROR("Ignoring -6 since async I/O support not "
  2429. "compiled in.\n");
  2430. #endif
  2431. break;
  2432. case '7' :
  2433. minVersion = atoi(myoptarg);
  2434. if (minVersion < 0 || minVersion > 4) {
  2435. Usage();
  2436. XEXIT_T(MY_EX_USAGE);
  2437. }
  2438. break;
  2439. case '8' :
  2440. #ifdef HAVE_CURVE448
  2441. useX448 = 1;
  2442. #ifdef HAVE_ECC
  2443. useSupCurve = 1;
  2444. #if defined(WOLFSSL_TLS13) && \
  2445. defined(HAVE_SUPPORTED_CURVES)
  2446. onlyKeyShare = 2;
  2447. #endif
  2448. #endif
  2449. #endif
  2450. break;
  2451. case '9' :
  2452. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  2453. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  2454. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  2455. useCertFolder = 1;
  2456. #endif
  2457. break;
  2458. case '@' :
  2459. {
  2460. #ifdef HAVE_WC_INTROSPECTION
  2461. const char *conf_args = wolfSSL_configure_args();
  2462. if (conf_args) {
  2463. puts(conf_args);
  2464. XEXIT_T(EXIT_SUCCESS);
  2465. } else {
  2466. fputs("configure args not compiled in.\n",stderr);
  2467. XEXIT_T(MY_EX_USAGE);
  2468. }
  2469. #else
  2470. fputs("compiled without BUILD_INTROSPECTION.\n",stderr);
  2471. XEXIT_T(MY_EX_USAGE);
  2472. #endif
  2473. }
  2474. case '#' :
  2475. {
  2476. #ifdef HAVE_WC_INTROSPECTION
  2477. const char *cflags = wolfSSL_global_cflags();
  2478. if (cflags) {
  2479. puts(cflags);
  2480. XEXIT_T(EXIT_SUCCESS);
  2481. } else {
  2482. fputs("CFLAGS not compiled in.\n",stderr);
  2483. XEXIT_T(MY_EX_USAGE);
  2484. }
  2485. #else
  2486. fputs("compiled without BUILD_INTROSPECTION.\n",stderr);
  2487. XEXIT_T(MY_EX_USAGE);
  2488. #endif
  2489. }
  2490. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  2491. case 256:
  2492. #if !defined(NO_FILESYSTEM) && !defined(WOLFSENTRY_NO_JSON)
  2493. wolfsentry_config_path = myoptarg;
  2494. #endif
  2495. break;
  2496. #endif
  2497. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES) && \
  2498. defined(HAVE_PQC)
  2499. case 259:
  2500. usePqc = 1;
  2501. onlyKeyShare = 3;
  2502. pqcAlg = myoptarg;
  2503. break;
  2504. #endif
  2505. #ifdef WOLFSSL_SYS_CA_CERTS
  2506. case 263:
  2507. loadSysCaCerts = 1;
  2508. break;
  2509. #endif
  2510. case 264:
  2511. #ifdef HAVE_SUPPORTED_CURVES
  2512. #ifdef WOLFSSL_TLS13
  2513. onlyPskDheKe = 1;
  2514. #endif
  2515. #endif
  2516. break;
  2517. case 265:
  2518. #ifndef NO_PSK
  2519. opensslPsk = 1;
  2520. #endif
  2521. break;
  2522. case 266:
  2523. quieter = 1;
  2524. break;
  2525. default:
  2526. Usage();
  2527. XEXIT_T(MY_EX_USAGE);
  2528. }
  2529. }
  2530. myoptind = 0; /* reset for test cases */
  2531. #endif /* !WOLFSSL_VXWORKS */
  2532. if (externalTest) {
  2533. /* detect build cases that wouldn't allow test against wolfssl.com */
  2534. int done = 0;
  2535. #ifdef NO_RSA
  2536. done += 1; /* require RSA for external tests */
  2537. #endif
  2538. if (!XSTRCMP(domain, "www.globalsign.com")) {
  2539. /* www.globalsign.com does not respond to ipv6 ocsp requests */
  2540. #if defined(TEST_IPV6) && defined(HAVE_OCSP)
  2541. done += 1;
  2542. #endif
  2543. /* www.globalsign.com has limited supported cipher suites */
  2544. #if defined(NO_AES) && defined(HAVE_OCSP)
  2545. done += 1;
  2546. #endif
  2547. /* www.globalsign.com only supports static RSA or ECDHE with AES */
  2548. /* We cannot expect users to have on static RSA so test for ECC only
  2549. * as some users will most likely be on 32-bit systems where ECC
  2550. * is not enabled by default */
  2551. #if defined(HAVE_OCSP) && !defined(HAVE_ECC)
  2552. done += 1;
  2553. #endif
  2554. }
  2555. #ifndef NO_PSK
  2556. if (usePsk) {
  2557. done += 1; /* don't perform external tests if PSK is enabled */
  2558. }
  2559. #endif
  2560. #ifdef NO_SHA
  2561. done += 1; /* external cert chain most likely has SHA */
  2562. #endif
  2563. #if !defined(HAVE_ECC) && !defined(WOLFSSL_STATIC_RSA) \
  2564. || ( defined(HAVE_ECC) && !defined(HAVE_SUPPORTED_CURVES) \
  2565. && !defined(WOLFSSL_STATIC_RSA) )
  2566. /* google needs ECDHE+Supported Curves or static RSA */
  2567. if (!XSTRCASECMP(domain, "www.google.com"))
  2568. done += 1;
  2569. #endif
  2570. #if !defined(HAVE_ECC) && !defined(WOLFSSL_STATIC_RSA)
  2571. /* wolfssl needs ECDHE or static RSA */
  2572. if (!XSTRCASECMP(domain, "www.wolfssl.com"))
  2573. done += 1;
  2574. #endif
  2575. #if !defined(WOLFSSL_SHA384)
  2576. if (!XSTRCASECMP(domain, "www.wolfssl.com")) {
  2577. /* wolfssl need sha384 for cert chain verify */
  2578. done += 1;
  2579. }
  2580. #endif
  2581. #if !defined(HAVE_AESGCM) && defined(NO_AES) && \
  2582. !(defined(HAVE_CHACHA) && defined(HAVE_POLY1305))
  2583. /* need at least one of these for external tests */
  2584. done += 1;
  2585. #endif
  2586. /* For the external test, if we disable AES, GoDaddy will reject the
  2587. * connection. They only currently support AES suites, RC4 and 3DES
  2588. * suites. With AES disabled we only offer PolyChacha suites. */
  2589. #if defined(NO_AES) && !defined(HAVE_AESGCM)
  2590. if (!XSTRCASECMP(domain, "www.wolfssl.com")) {
  2591. done += 1;
  2592. }
  2593. #endif
  2594. if (done) {
  2595. LOG_ERROR("external test can't be run in this mode\n");
  2596. ((func_args*)args)->return_code = 0;
  2597. XEXIT_T(EXIT_SUCCESS);
  2598. }
  2599. }
  2600. /* sort out DTLS versus TLS versions */
  2601. if (version == CLIENT_INVALID_VERSION) {
  2602. if (doDTLS)
  2603. version = CLIENT_DTLS_DEFAULT_VERSION;
  2604. else
  2605. version = CLIENT_DEFAULT_VERSION;
  2606. }
  2607. else {
  2608. if (doDTLS) {
  2609. if (version == 3) {
  2610. version = -2;
  2611. }
  2612. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2613. else if (version == EITHER_DOWNGRADE_VERSION) {
  2614. version = -3;
  2615. }
  2616. #endif
  2617. else if (version == 4) {
  2618. #ifdef WOLFSSL_DTLS13
  2619. version = -4;
  2620. #else
  2621. err_sys("Bad DTLS version");
  2622. #endif /* WOLFSSL_DTLS13 */
  2623. }
  2624. else if (version == 2)
  2625. version = -1;
  2626. }
  2627. }
  2628. #ifndef HAVE_SESSION_TICKET
  2629. if ((version >= 4) && resumeSession) {
  2630. LOG_ERROR("Can't do TLS 1.3 resumption; need session tickets!\n");
  2631. }
  2632. #endif
  2633. #ifdef HAVE_WNR
  2634. if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0)
  2635. err_sys("can't load whitewood net random config file");
  2636. #endif
  2637. #ifdef HAVE_PQC
  2638. if (usePqc) {
  2639. if (version == CLIENT_DOWNGRADE_VERSION ||
  2640. version == EITHER_DOWNGRADE_VERSION)
  2641. LOG_ERROR(
  2642. "WARNING: If a TLS 1.3 connection is not negotiated, you "
  2643. "will not be using a post-quantum group.\n");
  2644. else if (version != 4 && version != -4)
  2645. err_sys("can only use post-quantum groups with TLS 1.3 or DTLS 1.3");
  2646. }
  2647. #endif
  2648. switch (version) {
  2649. #ifndef NO_OLD_TLS
  2650. #ifdef WOLFSSL_ALLOW_SSLV3
  2651. case 0:
  2652. method = wolfSSLv3_client_method_ex;
  2653. break;
  2654. #endif
  2655. #ifndef NO_TLS
  2656. #ifdef WOLFSSL_ALLOW_TLSV10
  2657. case 1:
  2658. method = wolfTLSv1_client_method_ex;
  2659. break;
  2660. #endif
  2661. case 2:
  2662. method = wolfTLSv1_1_client_method_ex;
  2663. break;
  2664. #endif /* !NO_TLS */
  2665. #endif /* !NO_OLD_TLS */
  2666. #ifndef NO_TLS
  2667. #ifndef WOLFSSL_NO_TLS12
  2668. case 3:
  2669. method = wolfTLSv1_2_client_method_ex;
  2670. break;
  2671. #endif
  2672. #ifdef WOLFSSL_TLS13
  2673. case 4:
  2674. method = wolfTLSv1_3_client_method_ex;
  2675. break;
  2676. #endif
  2677. case CLIENT_DOWNGRADE_VERSION:
  2678. if (!doDTLS) {
  2679. method = wolfSSLv23_client_method_ex;
  2680. }
  2681. else {
  2682. #ifdef WOLFSSL_DTLS
  2683. method = wolfDTLS_client_method_ex;
  2684. #else
  2685. err_sys("version not supported");
  2686. #endif /* WOLFSSL_DTLS */
  2687. }
  2688. break;
  2689. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2690. case EITHER_DOWNGRADE_VERSION:
  2691. method = wolfSSLv23_method_ex;
  2692. break;
  2693. #endif
  2694. #endif /* NO_TLS */
  2695. #ifdef WOLFSSL_DTLS
  2696. #ifndef NO_OLD_TLS
  2697. case -1:
  2698. method = wolfDTLSv1_client_method_ex;
  2699. break;
  2700. #endif
  2701. #ifndef WOLFSSL_NO_TLS12
  2702. case -2:
  2703. method = wolfDTLSv1_2_client_method_ex;
  2704. break;
  2705. #endif
  2706. #ifdef WOLFSSL_DTLS13
  2707. case -4:
  2708. method = wolfDTLSv1_3_client_method_ex;
  2709. break;
  2710. #endif /* WOLFSSL_DTLS13 */
  2711. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2712. case -3:
  2713. method = wolfDTLSv1_2_method_ex;
  2714. break;
  2715. #endif
  2716. #endif
  2717. default:
  2718. err_sys("Bad SSL version");
  2719. }
  2720. if (method == NULL)
  2721. err_sys("unable to get method");
  2722. #ifdef WOLFSSL_STATIC_MEMORY
  2723. #ifdef DEBUG_WOLFSSL
  2724. /* print off helper buffer sizes for use with static memory
  2725. * printing to stderr in case of debug mode turned on */
  2726. LOG_ERROR("static memory management size = %d\n",
  2727. wolfSSL_MemoryPaddingSz());
  2728. LOG_ERROR("calculated optimum general buffer size = %d\n",
  2729. wolfSSL_StaticBufferSz(memory, sizeof(memory), 0));
  2730. LOG_ERROR("calculated optimum IO buffer size = %d\n",
  2731. wolfSSL_StaticBufferSz(memoryIO, sizeof(memoryIO),
  2732. WOLFMEM_IO_POOL_FIXED));
  2733. #endif /* DEBUG_WOLFSSL */
  2734. if (wc_LoadStaticMemory(&heap, memory, sizeof(memory), WOLFMEM_GENERAL, 1)
  2735. != 0) {
  2736. err_sys("unable to load static memory");
  2737. }
  2738. ctx = wolfSSL_CTX_new_ex(method(heap), heap);
  2739. if (ctx == NULL)
  2740. err_sys("unable to get ctx");
  2741. #ifdef WOLFSSL_CALLBACKS
  2742. wolfSSL_CTX_set_msg_callback(ctx, msgDebugCb);
  2743. #endif
  2744. if (wolfSSL_CTX_load_static_memory(&ctx, NULL, memoryIO, sizeof(memoryIO),
  2745. WOLFMEM_IO_POOL_FIXED | WOLFMEM_TRACK_STATS, 1) != WOLFSSL_SUCCESS) {
  2746. err_sys("unable to load static memory");
  2747. }
  2748. #else
  2749. if (method != NULL) {
  2750. ctx = wolfSSL_CTX_new(method(NULL));
  2751. if (ctx == NULL)
  2752. err_sys("unable to get ctx");
  2753. }
  2754. #endif
  2755. #ifdef WOLFSSL_SYS_CA_CERTS
  2756. if (loadSysCaCerts &&
  2757. wolfSSL_CTX_load_system_CA_certs(ctx) != WOLFSSL_SUCCESS) {
  2758. err_sys("wolfSSL_CTX_load_system_CA_certs failed");
  2759. }
  2760. #endif /* WOLFSSL_SYS_CA_CERTS */
  2761. if (minVersion != CLIENT_INVALID_VERSION) {
  2762. #ifdef WOLFSSL_DTLS
  2763. if (doDTLS) {
  2764. switch (minVersion) {
  2765. case 4:
  2766. #ifdef WOLFSSL_DTLS13
  2767. minVersion = WOLFSSL_DTLSV1_3;
  2768. break;
  2769. #else
  2770. err_sys("invalid minimum downgrade version");
  2771. #endif /* WOLFSSL_DTLS13 */
  2772. case 3:
  2773. minVersion = WOLFSSL_DTLSV1_2;
  2774. break;
  2775. case 2:
  2776. minVersion = WOLFSSL_DTLSV1;
  2777. break;
  2778. }
  2779. }
  2780. #endif /* WOLFSSL_DTLS */
  2781. if (wolfSSL_CTX_SetMinVersion(ctx, minVersion) != WOLFSSL_SUCCESS)
  2782. err_sys("can't set minimum downgrade version");
  2783. }
  2784. if (simulateWantWrite) {
  2785. #ifdef USE_WOLFSSL_IO
  2786. wolfSSL_CTX_SetIOSend(ctx, SimulateWantWriteIOSendCb);
  2787. #endif
  2788. }
  2789. #ifdef SINGLE_THREADED
  2790. if (wolfSSL_CTX_new_rng(ctx) != WOLFSSL_SUCCESS) {
  2791. wolfSSL_CTX_free(ctx); ctx = NULL;
  2792. err_sys("Single Threaded new rng at CTX failed");
  2793. }
  2794. #endif
  2795. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  2796. /* Restore wolfSSL verify defaults */
  2797. if (ctx) {
  2798. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_DEFAULT, NULL);
  2799. }
  2800. #endif
  2801. #ifdef WOLFSSL_SRTP
  2802. if (dtlsSrtpProfiles != NULL) {
  2803. if (wolfSSL_CTX_set_tlsext_use_srtp(ctx, dtlsSrtpProfiles)
  2804. != WOLFSSL_SUCCESS) {
  2805. err_sys("unable to set DTLS SRTP profile");
  2806. }
  2807. }
  2808. #endif
  2809. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  2810. if (wolfsentry_setup(&wolfsentry, wolfsentry_config_path,
  2811. WOLFSENTRY_ROUTE_FLAG_DIRECTION_OUT) < 0) {
  2812. err_sys("unable to initialize wolfSentry");
  2813. }
  2814. if (wolfSSL_CTX_set_ConnectFilter(
  2815. ctx,
  2816. (NetworkFilterCallback_t)wolfSentry_NetworkFilterCallback,
  2817. wolfsentry) < 0) {
  2818. err_sys("unable to install wolfSentry_NetworkFilterCallback");
  2819. }
  2820. #endif
  2821. if (cipherList && !useDefCipherList) {
  2822. if (wolfSSL_CTX_set_cipher_list(ctx, cipherList) != WOLFSSL_SUCCESS) {
  2823. wolfSSL_CTX_free(ctx); ctx = NULL;
  2824. err_sys("client can't set cipher list 1");
  2825. }
  2826. }
  2827. #ifdef WOLFSSL_LEANPSK
  2828. if (!usePsk) {
  2829. usePsk = 1;
  2830. }
  2831. #endif
  2832. #if defined(NO_RSA) && !defined(HAVE_ECC) && !defined(HAVE_ED25519) && \
  2833. !defined(HAVE_ED448)
  2834. if (!usePsk) {
  2835. usePsk = 1;
  2836. }
  2837. #endif
  2838. if (fewerPackets)
  2839. wolfSSL_CTX_set_group_messages(ctx);
  2840. #if (defined(WOLFSSL_SCTP) || defined(WOLFSSL_DTLS_MTU)) && \
  2841. defined(WOLFSSL_DTLS)
  2842. if (dtlsMTU)
  2843. wolfSSL_CTX_dtls_set_mtu(ctx, dtlsMTU);
  2844. #endif
  2845. #ifndef NO_DH
  2846. if (wolfSSL_CTX_SetMinDhKey_Sz(ctx, (word16)minDhKeyBits)
  2847. != WOLFSSL_SUCCESS) {
  2848. err_sys("Error setting minimum DH key size");
  2849. }
  2850. #endif
  2851. if (usePsk) {
  2852. #ifndef NO_PSK
  2853. const char *defaultCipherList = cipherList;
  2854. wolfSSL_CTX_set_psk_client_callback(ctx, my_psk_client_cb);
  2855. #ifdef WOLFSSL_TLS13
  2856. #if !defined(WOLFSSL_PSK_TLS13_CB) && !defined(WOLFSSL_PSK_ONE_ID)
  2857. if (!opensslPsk) {
  2858. wolfSSL_CTX_set_psk_client_cs_callback(ctx, my_psk_client_cs_cb);
  2859. }
  2860. else
  2861. #endif
  2862. {
  2863. wolfSSL_CTX_set_psk_client_tls13_callback(ctx,
  2864. my_psk_client_tls13_cb);
  2865. }
  2866. #endif
  2867. if (defaultCipherList == NULL) {
  2868. #if defined(HAVE_AESGCM) && !defined(NO_DH)
  2869. #ifdef WOLFSSL_TLS13
  2870. defaultCipherList = "TLS13-AES128-GCM-SHA256"
  2871. #ifndef WOLFSSL_NO_TLS12
  2872. ":DHE-PSK-AES128-GCM-SHA256"
  2873. #endif
  2874. ;
  2875. #else
  2876. defaultCipherList = "DHE-PSK-AES128-GCM-SHA256";
  2877. #endif
  2878. #elif defined(HAVE_AESGCM) && defined(WOLFSSL_TLS13)
  2879. defaultCipherList = "TLS13-AES128-GCM-SHA256"
  2880. #ifndef WOLFSSL_NO_TLS12
  2881. ":PSK-AES128-GCM-SHA256"
  2882. #endif
  2883. ;
  2884. #elif defined(HAVE_NULL_CIPHER)
  2885. defaultCipherList = "PSK-NULL-SHA256";
  2886. #elif !defined(NO_AES_CBC)
  2887. defaultCipherList = "PSK-AES128-CBC-SHA256";
  2888. #else
  2889. defaultCipherList = "PSK-AES128-GCM-SHA256";
  2890. #endif
  2891. if (wolfSSL_CTX_set_cipher_list(ctx, defaultCipherList)
  2892. !=WOLFSSL_SUCCESS) {
  2893. wolfSSL_CTX_free(ctx); ctx = NULL;
  2894. err_sys("client can't set cipher list 2");
  2895. }
  2896. }
  2897. wolfSSL_CTX_set_psk_callback_ctx(ctx, (void*)defaultCipherList);
  2898. #endif
  2899. if (useClientCert) {
  2900. useClientCert = 0;
  2901. }
  2902. }
  2903. if (useAnon) {
  2904. #ifdef HAVE_ANON
  2905. if (cipherList == NULL || (cipherList && useDefCipherList)) {
  2906. const char* defaultCipherList;
  2907. wolfSSL_CTX_allow_anon_cipher(ctx);
  2908. defaultCipherList = "ADH-AES256-GCM-SHA384:"
  2909. "ADH-AES128-SHA";
  2910. if (wolfSSL_CTX_set_cipher_list(ctx,defaultCipherList)
  2911. != WOLFSSL_SUCCESS) {
  2912. wolfSSL_CTX_free(ctx); ctx = NULL;
  2913. err_sys("client can't set cipher list 4");
  2914. }
  2915. }
  2916. #endif
  2917. if (useClientCert) {
  2918. useClientCert = 0;
  2919. }
  2920. }
  2921. #ifdef WOLFSSL_SCTP
  2922. if (dtlsSCTP)
  2923. wolfSSL_CTX_dtls_set_sctp(ctx);
  2924. #endif
  2925. #ifdef WOLFSSL_ENCRYPTED_KEYS
  2926. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  2927. #endif
  2928. #ifdef WOLFSSL_SNIFFER
  2929. if (cipherList == NULL && version < 4) {
  2930. /* static RSA or ECC cipher suites */
  2931. const char* staticCipherList = "AES128-SHA:ECDH-ECDSA-AES128-SHA";
  2932. if (wolfSSL_CTX_set_cipher_list(ctx, staticCipherList) != WOLFSSL_SUCCESS) {
  2933. wolfSSL_CTX_free(ctx); ctx = NULL;
  2934. err_sys("client can't set cipher list 3");
  2935. }
  2936. }
  2937. #endif
  2938. #ifdef HAVE_OCSP
  2939. if (useOcsp) {
  2940. #if defined(HAVE_IO_TIMEOUT) && defined(HAVE_HTTP_CLIENT)
  2941. wolfIO_SetTimeout(DEFAULT_TIMEOUT_SEC);
  2942. #endif
  2943. if (ocspUrl != NULL) {
  2944. wolfSSL_CTX_SetOCSP_OverrideURL(ctx, ocspUrl);
  2945. wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE
  2946. | WOLFSSL_OCSP_URL_OVERRIDE);
  2947. }
  2948. else {
  2949. wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL);
  2950. }
  2951. #ifdef WOLFSSL_NONBLOCK_OCSP
  2952. wolfSSL_CTX_SetOCSP_Cb(ctx, OCSPIOCb, OCSPRespFreeCb, NULL);
  2953. #endif
  2954. }
  2955. #endif
  2956. #ifdef USER_CA_CB
  2957. wolfSSL_CTX_SetCACb(ctx, CaCb);
  2958. #endif
  2959. #if defined(HAVE_EXT_CACHE) && !defined(NO_SESSION_CACHE)
  2960. wolfSSL_CTX_sess_set_get_cb(ctx, mySessGetCb);
  2961. wolfSSL_CTX_sess_set_new_cb(ctx, mySessNewCb);
  2962. wolfSSL_CTX_sess_set_remove_cb(ctx, mySessRemCb);
  2963. #endif
  2964. #ifndef NO_CERTS
  2965. if (useClientCert && !loadCertKeyIntoSSLObj){
  2966. #if defined(NO_FILESYSTEM) && defined(USE_CERT_BUFFERS_2048)
  2967. if (wolfSSL_CTX_use_certificate_chain_buffer_format(ctx,
  2968. client_cert_der_2048, sizeof_client_cert_der_2048,
  2969. WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS)
  2970. err_sys("can't load client cert buffer");
  2971. #elif !defined(TEST_LOAD_BUFFER)
  2972. if (wolfSSL_CTX_use_certificate_chain_file(ctx, ourCert)
  2973. != WOLFSSL_SUCCESS) {
  2974. wolfSSL_CTX_free(ctx); ctx = NULL;
  2975. err_sys("can't load client cert file, check file and run from"
  2976. " wolfSSL home dir");
  2977. }
  2978. #else
  2979. load_buffer(ctx, ourCert, WOLFSSL_CERT_CHAIN);
  2980. #endif
  2981. }
  2982. #ifdef HAVE_PK_CALLBACKS
  2983. pkCbInfo.ourKey = ourKey;
  2984. #endif
  2985. if (useClientCert && !loadCertKeyIntoSSLObj
  2986. #if defined(HAVE_PK_CALLBACKS) && defined(TEST_PK_PRIVKEY)
  2987. && !pkCallbacks
  2988. #endif
  2989. ) {
  2990. #ifdef NO_FILESYSTEM
  2991. if (wolfSSL_CTX_use_PrivateKey_buffer(ctx, client_key_der_2048,
  2992. sizeof_client_key_der_2048, SSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS)
  2993. err_sys("can't load client private key buffer");
  2994. #elif !defined(TEST_LOAD_BUFFER)
  2995. if (wolfSSL_CTX_use_PrivateKey_file(ctx, ourKey, WOLFSSL_FILETYPE_PEM)
  2996. != WOLFSSL_SUCCESS) {
  2997. wolfSSL_CTX_free(ctx); ctx = NULL;
  2998. err_sys("can't load client private key file, check file and run "
  2999. "from wolfSSL home dir");
  3000. }
  3001. #else
  3002. load_buffer(ctx, ourKey, WOLFSSL_KEY);
  3003. #endif
  3004. }
  3005. if (!usePsk && !useAnon && !useVerifyCb && myVerifyAction != VERIFY_FORCE_FAIL) {
  3006. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  3007. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  3008. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  3009. if (useCertFolder) {
  3010. WOLFSSL_X509_STORE *store;
  3011. WOLFSSL_X509_LOOKUP *lookup;
  3012. store = wolfSSL_CTX_get_cert_store(ctx);
  3013. if (store == NULL) {
  3014. wolfSSL_CTX_free(ctx); ctx = NULL;
  3015. err_sys("can't get WOLFSSL_X509_STORE");
  3016. }
  3017. lookup = wolfSSL_X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
  3018. if (lookup == NULL) {
  3019. wolfSSL_CTX_free(ctx); ctx = NULL;
  3020. err_sys("can't add lookup");
  3021. }
  3022. if (wolfSSL_X509_LOOKUP_ctrl(lookup, WOLFSSL_X509_L_ADD_DIR, caCertFolder,
  3023. X509_FILETYPE_PEM, NULL) != WOLFSSL_SUCCESS) {
  3024. err_sys("X509_LOOKUP_ctrl w/ L_ADD_DIR failed");
  3025. }
  3026. } else {
  3027. #endif
  3028. #ifdef NO_FILESYSTEM
  3029. if (wolfSSL_CTX_load_verify_buffer(ctx, ca_cert_der_2048,
  3030. sizeof_ca_cert_der_2048, SSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS) {
  3031. wolfSSL_CTX_free(ctx); ctx = NULL;
  3032. err_sys("can't load ca buffer, Please run from wolfSSL home dir");
  3033. }
  3034. #elif !defined(TEST_LOAD_BUFFER)
  3035. unsigned int verify_flags = 0;
  3036. #ifdef TEST_BEFORE_DATE
  3037. verify_flags |= WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY;
  3038. #endif
  3039. if (doPeerCheck != 0 &&
  3040. wolfSSL_CTX_load_verify_locations_ex(ctx, verifyCert, 0, verify_flags)
  3041. != WOLFSSL_SUCCESS) {
  3042. wolfSSL_CTX_free(ctx); ctx = NULL;
  3043. err_sys("can't load ca file, Please run from wolfSSL home dir");
  3044. }
  3045. #else
  3046. load_buffer(ctx, verifyCert, WOLFSSL_CA);
  3047. #endif /* !NO_FILESYSTEM */
  3048. #ifdef HAVE_ECC
  3049. /* load ecc verify too, echoserver uses it by default w/ ecc */
  3050. #ifdef NO_FILESYSTEM
  3051. if (doPeerCheck != 0 &&
  3052. wolfSSL_CTX_load_verify_buffer(ctx, ca_ecc_cert_der_256,
  3053. sizeof_ca_ecc_cert_der_256, SSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS) {
  3054. wolfSSL_CTX_free(ctx); ctx = NULL;
  3055. err_sys("can't load ecc ca buffer");
  3056. }
  3057. #elif !defined(TEST_LOAD_BUFFER)
  3058. if (doPeerCheck != 0 && !customVerifyCert &&
  3059. wolfSSL_CTX_load_verify_locations_ex(ctx, eccCertFile, 0, verify_flags)
  3060. != WOLFSSL_SUCCESS) {
  3061. wolfSSL_CTX_free(ctx); ctx = NULL;
  3062. err_sys("can't load ecc ca file, Please run from wolfSSL home dir");
  3063. }
  3064. #else
  3065. load_buffer(ctx, eccCertFile, WOLFSSL_CA);
  3066. #endif /* !TEST_LOAD_BUFFER */
  3067. #endif /* HAVE_ECC */
  3068. #if defined(WOLFSSL_TRUST_PEER_CERT) && !defined(NO_FILESYSTEM)
  3069. if (trustCert) {
  3070. if (wolfSSL_CTX_trust_peer_cert(ctx, trustCert,
  3071. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3072. wolfSSL_CTX_free(ctx); ctx = NULL;
  3073. err_sys("can't load trusted peer cert file");
  3074. }
  3075. }
  3076. #endif /* WOLFSSL_TRUST_PEER_CERT && !NO_FILESYSTEM */
  3077. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  3078. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  3079. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  3080. }
  3081. #endif
  3082. }
  3083. if (useVerifyCb || myVerifyAction == VERIFY_FORCE_FAIL ||
  3084. myVerifyAction == VERIFY_USE_PREVERFIY) {
  3085. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  3086. }
  3087. else if (!usePsk && !useAnon && doPeerCheck == 0) {
  3088. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, NULL);
  3089. }
  3090. else if (!usePsk && !useAnon && myVerifyAction == VERIFY_OVERRIDE_DATE_ERR) {
  3091. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  3092. }
  3093. #endif /* !NO_CERTS */
  3094. #ifdef WOLFSSL_ASYNC_CRYPT
  3095. ret = wolfAsync_DevOpen(&devId);
  3096. if (ret < 0) {
  3097. LOG_ERROR("Async device open failed\nRunning without async\n");
  3098. }
  3099. wolfSSL_CTX_SetDevId(ctx, devId);
  3100. #endif /* WOLFSSL_ASYNC_CRYPT */
  3101. #ifdef HAVE_SNI
  3102. if (sniHostName) {
  3103. if (wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, sniHostName,
  3104. (word16) XSTRLEN(sniHostName)) != WOLFSSL_SUCCESS) {
  3105. wolfSSL_CTX_free(ctx); ctx = NULL;
  3106. err_sys("UseSNI failed");
  3107. }
  3108. }
  3109. #endif
  3110. #ifdef HAVE_MAX_FRAGMENT
  3111. if (maxFragment)
  3112. if (wolfSSL_CTX_UseMaxFragment(ctx, maxFragment) != WOLFSSL_SUCCESS) {
  3113. wolfSSL_CTX_free(ctx); ctx = NULL;
  3114. err_sys("UseMaxFragment failed");
  3115. }
  3116. #endif
  3117. #ifdef HAVE_TRUNCATED_HMAC
  3118. if (truncatedHMAC)
  3119. if (wolfSSL_CTX_UseTruncatedHMAC(ctx) != WOLFSSL_SUCCESS) {
  3120. wolfSSL_CTX_free(ctx); ctx = NULL;
  3121. err_sys("UseTruncatedHMAC failed");
  3122. }
  3123. #endif
  3124. #ifdef HAVE_SESSION_TICKET
  3125. if (wolfSSL_CTX_UseSessionTicket(ctx) != WOLFSSL_SUCCESS) {
  3126. wolfSSL_CTX_free(ctx); ctx = NULL;
  3127. err_sys("UseSessionTicket failed");
  3128. }
  3129. #endif
  3130. #ifdef HAVE_EXTENDED_MASTER
  3131. if (disableExtMasterSecret)
  3132. if (wolfSSL_CTX_DisableExtendedMasterSecret(ctx) != WOLFSSL_SUCCESS) {
  3133. wolfSSL_CTX_free(ctx); ctx = NULL;
  3134. err_sys("DisableExtendedMasterSecret failed");
  3135. }
  3136. #endif
  3137. #if defined(HAVE_SUPPORTED_CURVES)
  3138. #if defined(HAVE_CURVE25519)
  3139. if (useX25519) {
  3140. if (wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_X25519)
  3141. != WOLFSSL_SUCCESS) {
  3142. err_sys("unable to support X25519");
  3143. }
  3144. }
  3145. #endif /* HAVE_CURVE25519 */
  3146. #if defined(HAVE_CURVE448)
  3147. if (useX448) {
  3148. if (wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_X448)
  3149. != WOLFSSL_SUCCESS) {
  3150. err_sys("unable to support X448");
  3151. }
  3152. }
  3153. #endif /* HAVE_CURVE448 */
  3154. #ifdef HAVE_ECC
  3155. if (useSupCurve) {
  3156. #if !defined(NO_ECC_SECP) && \
  3157. (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  3158. if (wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP384R1)
  3159. != WOLFSSL_SUCCESS) {
  3160. err_sys("unable to support secp384r1");
  3161. }
  3162. #endif
  3163. #if !defined(NO_ECC_SECP) && \
  3164. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES))
  3165. if (wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1)
  3166. != WOLFSSL_SUCCESS) {
  3167. err_sys("unable to support secp256r1");
  3168. }
  3169. #endif
  3170. }
  3171. #endif /* HAVE_ECC */
  3172. #ifdef HAVE_FFDHE_2048
  3173. if (useSupCurve) {
  3174. if (wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_FFDHE_2048)
  3175. != WOLFSSL_SUCCESS) {
  3176. err_sys("unable to support FFDHE 2048");
  3177. }
  3178. }
  3179. #endif
  3180. #endif /* HAVE_SUPPORTED_CURVES */
  3181. #ifdef WOLFSSL_TLS13
  3182. if (noPskDheKe)
  3183. wolfSSL_CTX_no_dhe_psk(ctx);
  3184. #ifdef HAVE_SUPPORTED_CURVES
  3185. if (onlyPskDheKe)
  3186. wolfSSL_CTX_only_dhe_psk(ctx);
  3187. #endif
  3188. #endif
  3189. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  3190. if (postHandAuth)
  3191. wolfSSL_CTX_allow_post_handshake_auth(ctx);
  3192. #endif
  3193. if (benchmark) {
  3194. ((func_args*)args)->return_code =
  3195. ClientBenchmarkConnections(ctx, host, port, dtlsUDP, dtlsSCTP,
  3196. benchmark, resumeSession, useX25519,
  3197. useX448, usePqc, pqcAlg, helloRetry,
  3198. onlyKeyShare, version, earlyData);
  3199. wolfSSL_CTX_free(ctx); ctx = NULL;
  3200. XEXIT_T(EXIT_SUCCESS);
  3201. }
  3202. if (throughput) {
  3203. ((func_args*)args)->return_code =
  3204. ClientBenchmarkThroughput(ctx, host, port, dtlsUDP, dtlsSCTP,
  3205. block, throughput, useX25519, useX448,
  3206. usePqc, pqcAlg, exitWithRet, version,
  3207. onlyKeyShare);
  3208. wolfSSL_CTX_free(ctx); ctx = NULL;
  3209. if (((func_args*)args)->return_code != EXIT_SUCCESS && !exitWithRet)
  3210. XEXIT_T(EXIT_SUCCESS);
  3211. else
  3212. goto exit;
  3213. }
  3214. #if defined(WOLFSSL_MDK_ARM)
  3215. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, NULL);
  3216. #endif
  3217. #if defined(OPENSSL_EXTRA)
  3218. if (wolfSSL_CTX_get_read_ahead(ctx) != 0) {
  3219. wolfSSL_CTX_free(ctx); ctx = NULL;
  3220. err_sys("bad read ahead default value");
  3221. }
  3222. if (wolfSSL_CTX_set_read_ahead(ctx, 1) != WOLFSSL_SUCCESS) {
  3223. wolfSSL_CTX_free(ctx); ctx = NULL;
  3224. err_sys("error setting read ahead value");
  3225. }
  3226. #endif
  3227. #if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL)
  3228. LOG_ERROR("Before creating SSL\n");
  3229. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
  3230. err_sys("ctx not using static memory");
  3231. if (wolfSSL_PrintStats(&mem_stats) != 1) /* function in test.h */
  3232. err_sys("error printing out memory stats");
  3233. #endif
  3234. if (doMcast) {
  3235. #ifdef WOLFSSL_MULTICAST
  3236. wolfSSL_CTX_mcast_set_member_id(ctx, mcastID);
  3237. if (wolfSSL_CTX_set_cipher_list(ctx, "WDM-NULL-SHA256")
  3238. != WOLFSSL_SUCCESS) {
  3239. wolfSSL_CTX_free(ctx); ctx = NULL;
  3240. err_sys("Couldn't set multicast cipher list.");
  3241. }
  3242. #endif
  3243. }
  3244. #ifdef HAVE_PK_CALLBACKS
  3245. if (pkCallbacks)
  3246. SetupPkCallbacks(ctx);
  3247. #endif
  3248. ssl = wolfSSL_new(ctx);
  3249. if (ssl == NULL) {
  3250. wolfSSL_CTX_free(ctx); ctx = NULL;
  3251. err_sys("unable to get SSL object");
  3252. }
  3253. #ifdef WOLFSSL_DUAL_ALG_CERTS
  3254. /* Set our preference for verfication to be for both the native and
  3255. * alternative chains. Ultimately, its the server's choice.
  3256. */
  3257. {
  3258. byte cks_order[3] = {
  3259. WOLFSSL_CKS_SIGSPEC_BOTH,
  3260. WOLFSSL_CKS_SIGSPEC_ALTERNATIVE,
  3261. WOLFSSL_CKS_SIGSPEC_NATIVE,
  3262. };
  3263. if (!wolfSSL_UseCKS(ssl, cks_order, sizeof(cks_order))) {
  3264. wolfSSL_CTX_free(ctx); ctx = NULL;
  3265. err_sys("unable to set the CKS order.");
  3266. }
  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 */