client.c 144 KB

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