client.c 132 KB

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