client.c 151 KB

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