client.c 153 KB

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