client.c 151 KB

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