2
0

client.c 154 KB

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