2
0

client.c 160 KB

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