client.c 122 KB

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