server.c 133 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864
  1. /* server.c
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* For simpler wolfSSL TLS server examples, visit
  22. * https://github.com/wolfSSL/wolfssl-examples/tree/master/tls
  23. */
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #ifndef WOLFSSL_USER_SETTINGS
  28. #include <wolfssl/options.h>
  29. #endif
  30. #include <wolfssl/wolfcrypt/settings.h>
  31. #undef TEST_OPENSSL_COEXIST /* can't use this option with this example */
  32. #include <wolfssl/ssl.h> /* name change portability layer */
  33. #ifdef HAVE_ECC
  34. #include <wolfssl/wolfcrypt/ecc.h> /* wc_ecc_fp_free */
  35. #endif
  36. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  37. #include <wolfsentry/wolfsentry.h>
  38. #if !defined(NO_FILESYSTEM) && !defined(WOLFSENTRY_NO_JSON)
  39. static const char *wolfsentry_config_path = NULL;
  40. #endif
  41. #endif /* WOLFSSL_WOLFSENTRY_HOOKS */
  42. #if defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include "rl_fs.h"
  46. #include "rl_net.h"
  47. #endif
  48. #ifdef NO_FILESYSTEM
  49. #ifdef NO_RSA
  50. #error currently the example only tries to load in a RSA buffer
  51. #endif
  52. #undef USE_CERT_BUFFERS_2048
  53. #define USE_CERT_BUFFERS_2048
  54. #include <wolfssl/certs_test.h>
  55. #endif
  56. #include <wolfssl/test.h>
  57. #include <wolfssl/error-ssl.h>
  58. /* Force enable the compatibility macros for this example */
  59. #ifndef OPENSSL_EXTRA_X509_SMALL
  60. #define OPENSSL_EXTRA_X509_SMALL
  61. #endif
  62. #include <wolfssl/openssl/ssl.h>
  63. #include "examples/server/server.h"
  64. #ifndef NO_WOLFSSL_SERVER
  65. #if defined(WOLFSSL_TLS13) && ( \
  66. defined(HAVE_ECC) \
  67. || defined(HAVE_CURVE25519) \
  68. || defined(HAVE_CURVE448) \
  69. || defined(HAVE_FFDHE_2048))
  70. #define CAN_FORCE_CURVE
  71. #endif
  72. #if defined(CAN_FORCE_CURVE) && defined(HAVE_ECC)
  73. struct group_info {
  74. word16 group;
  75. const char *name;
  76. };
  77. static struct group_info group_id_to_text[] = {
  78. { WOLFSSL_ECC_SECP160K1, "SECP160K1" },
  79. { WOLFSSL_ECC_SECP160R1, "SECP160R1" },
  80. { WOLFSSL_ECC_SECP160R2, "SECP160R2" },
  81. { WOLFSSL_ECC_SECP192K1, "SECP192K1" },
  82. { WOLFSSL_ECC_SECP192R1, "SECP192R1" },
  83. { WOLFSSL_ECC_SECP224K1, "SECP224K1" },
  84. { WOLFSSL_ECC_SECP224R1, "SECP224R1" },
  85. { WOLFSSL_ECC_SECP256K1, "SECP256K1" },
  86. { WOLFSSL_ECC_SECP256R1, "SECP256R1" },
  87. { WOLFSSL_ECC_SECP384R1, "SECP384R1" },
  88. { WOLFSSL_ECC_SECP521R1, "SECP521R1" },
  89. { WOLFSSL_ECC_BRAINPOOLP256R1, "BRAINPOOLP256R1" },
  90. { WOLFSSL_ECC_BRAINPOOLP384R1, "BRAINPOOLP384R1" },
  91. { WOLFSSL_ECC_BRAINPOOLP512R1, "BRAINPOOLP512R1" },
  92. { 0, NULL }
  93. };
  94. #endif /* CAN_FORCE_CURVE && HAVE_ECC */
  95. #ifdef WOLFSSL_ASYNC_CRYPT
  96. static int devId = INVALID_DEVID;
  97. #endif
  98. #define DEFAULT_TIMEOUT_SEC 2
  99. /* Note on using port 0: if the server uses port 0 to bind an ephemeral port
  100. * number and is using the ready file for scripted testing, the code in
  101. * test.h will write the actual port number into the ready file for use
  102. * by the client. */
  103. #ifndef WOLFSSL_ALT_TEST_STRINGS
  104. static const char kReplyMsg[] = "I hear you fa shizzle!";
  105. #else
  106. static const char kReplyMsg[] = "I hear you fa shizzle!\n";
  107. #endif
  108. static const char kHttpServerMsg[] =
  109. "HTTP/1.1 200 OK\r\n"
  110. "Content-Type: text/html\r\n"
  111. "Connection: close\r\n"
  112. "Content-Length: 141\r\n"
  113. "\r\n"
  114. "<html>\r\n"
  115. "<head>\r\n"
  116. "<title>Welcome to wolfSSL!</title>\r\n"
  117. "</head>\r\n"
  118. "<body>\r\n"
  119. "<p>wolfSSL has successfully performed handshake!</p>\r\n"
  120. "</body>\r\n"
  121. "</html>\r\n";
  122. /* Read needs to be largest of the client.c message strings (29) */
  123. #define SRV_READ_SZ 32
  124. int runWithErrors = 0; /* Used with -x flag to run err_sys vs. print errors */
  125. int catastrophic = 0; /* Use with -x flag to still exit when an error is
  126. * considered catastrophic EG the servers own cert failing
  127. * to load would be catastrophic since there would be no
  128. * cert to send to clients attempting to connect. The
  129. * server should error out completely in that case
  130. */
  131. static int lng_index = 0;
  132. #ifdef WOLFSSL_CALLBACKS
  133. #if !defined(NO_OLD_TIMEVAL_NAME)
  134. Timeval srvTo;
  135. #else
  136. WOLFSSL_TIMEVAL srvTo;
  137. #endif
  138. static int srvHandShakeCB(HandShakeInfo* info)
  139. {
  140. (void)info;
  141. return 0;
  142. }
  143. static int srvTimeoutCB(TimeoutInfo* info)
  144. {
  145. (void)info;
  146. return 0;
  147. }
  148. #endif
  149. #ifndef NO_HANDSHAKE_DONE_CB
  150. static int myHsDoneCb(WOLFSSL* ssl, void* user_ctx)
  151. {
  152. (void)user_ctx;
  153. (void)ssl;
  154. /* printf("Notified HandShake done\n"); */
  155. /* return negative number to end TLS connection now */
  156. return 0;
  157. }
  158. #endif
  159. static void err_sys_ex(int out, const char* msg)
  160. {
  161. if (out == 1) { /* if server is running w/ -x flag, print error w/o exit */
  162. fprintf(stderr, "wolfSSL error: %s\n", msg);
  163. fprintf(stderr, "Continuing server execution...\n\n");
  164. } else {
  165. err_sys(msg);
  166. }
  167. }
  168. #if defined(WOLFSSL_DTLS) && defined(USE_WOLFSSL_IO)
  169. /* Translates return codes returned from
  170. * send() and recv() if need be.
  171. */
  172. static WC_INLINE int TranslateReturnCode(int old, int sd)
  173. {
  174. (void)sd;
  175. #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  176. if (old == 0) {
  177. errno = SOCKET_EWOULDBLOCK;
  178. return -1; /* convert to BSD style wouldblock as error */
  179. }
  180. if (old < 0) {
  181. errno = RTCS_geterror(sd);
  182. if (errno == RTCSERR_TCP_CONN_CLOSING)
  183. return 0; /* convert to BSD style closing */
  184. if (errno == RTCSERR_TCP_CONN_RLSD)
  185. errno = SOCKET_ECONNRESET;
  186. if (errno == RTCSERR_TCP_TIMED_OUT)
  187. errno = SOCKET_EAGAIN;
  188. }
  189. #endif
  190. return old;
  191. }
  192. static WC_INLINE int wolfSSL_LastError(void)
  193. {
  194. #ifdef USE_WINDOWS_API
  195. return WSAGetLastError();
  196. #elif defined(EBSNET)
  197. return xn_getlasterror();
  198. #else
  199. return errno;
  200. #endif
  201. }
  202. /* wolfSSL Sock Addr */
  203. struct WOLFSSL_TEST_SOCKADDR {
  204. unsigned int sz; /* sockaddr size */
  205. SOCKADDR_IN_T sa; /* pointer to the sockaddr_in or sockaddr_in6 */
  206. };
  207. typedef struct WOLFSSL_TEST_DTLS_CTX {
  208. struct WOLFSSL_TEST_SOCKADDR peer;
  209. int rfd;
  210. int wfd;
  211. int failOnce;
  212. word32 blockSeq;
  213. } WOLFSSL_TEST_DTLS_CTX;
  214. static WC_INLINE int PeekSeq(const char* buf, word32* seq)
  215. {
  216. const char* c = buf + 3;
  217. if ((c[0] | c[1] | c[2] | c[3]) == 0) {
  218. *seq = (c[4] << 24) | (c[5] << 16) | (c[6] << 8) | c[7];
  219. return 1;
  220. }
  221. return 0;
  222. }
  223. /* The send embedded callback
  224. * return : nb bytes sent, or error
  225. */
  226. static int TestEmbedSendTo(WOLFSSL* ssl, char *buf, int sz, void *ctx)
  227. {
  228. WOLFSSL_TEST_DTLS_CTX* dtlsCtx = (WOLFSSL_TEST_DTLS_CTX*)ctx;
  229. int sd = dtlsCtx->wfd;
  230. int sent;
  231. int err;
  232. (void)ssl;
  233. WOLFSSL_ENTER("TestEmbedSendTo()");
  234. if (dtlsCtx->failOnce) {
  235. word32 seq = 0;
  236. if (PeekSeq(buf, &seq) && seq == dtlsCtx->blockSeq) {
  237. dtlsCtx->failOnce = 0;
  238. WOLFSSL_MSG("Forcing WANT_WRITE");
  239. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  240. }
  241. }
  242. sent = (int)sendto(sd, buf, sz, 0, (const SOCKADDR*)&dtlsCtx->peer.sa,
  243. dtlsCtx->peer.sz);
  244. sent = TranslateReturnCode(sent, sd);
  245. if (sent < 0) {
  246. err = wolfSSL_LastError();
  247. WOLFSSL_MSG("Embed Send To error");
  248. if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) {
  249. WOLFSSL_MSG("\tWould Block");
  250. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  251. }
  252. else if (err == SOCKET_ECONNRESET) {
  253. WOLFSSL_MSG("\tConnection reset");
  254. return WOLFSSL_CBIO_ERR_CONN_RST;
  255. }
  256. else if (err == SOCKET_EINTR) {
  257. WOLFSSL_MSG("\tSocket interrupted");
  258. return WOLFSSL_CBIO_ERR_ISR;
  259. }
  260. else if (err == SOCKET_EPIPE) {
  261. WOLFSSL_MSG("\tSocket EPIPE");
  262. return WOLFSSL_CBIO_ERR_CONN_CLOSE;
  263. }
  264. else {
  265. WOLFSSL_MSG("\tGeneral error");
  266. return WOLFSSL_CBIO_ERR_GENERAL;
  267. }
  268. }
  269. return sent;
  270. }
  271. #endif /* WOLFSSL_DTLS && USE_WOLFSSL_IO */
  272. static int NonBlockingSSL_Accept(SSL* ssl)
  273. {
  274. #ifndef WOLFSSL_CALLBACKS
  275. int ret = SSL_accept(ssl);
  276. #else
  277. int ret = wolfSSL_accept_ex(ssl, srvHandShakeCB, srvTimeoutCB, srvTo);
  278. #endif
  279. int error = SSL_get_error(ssl, 0);
  280. SOCKET_T sockfd = (SOCKET_T)SSL_get_fd(ssl);
  281. int select_ret = 0;
  282. while (ret != WOLFSSL_SUCCESS &&
  283. (error == WOLFSSL_ERROR_WANT_READ || error == WOLFSSL_ERROR_WANT_WRITE
  284. #ifdef WOLFSSL_ASYNC_CRYPT
  285. || error == WC_PENDING_E
  286. #endif
  287. )) {
  288. int currTimeout = 1;
  289. if (error == WOLFSSL_ERROR_WANT_READ) {
  290. /* printf("... server would read block\n"); */
  291. }
  292. else if (error == WOLFSSL_ERROR_WANT_WRITE) {
  293. /* printf("... server would write block\n"); */
  294. }
  295. #ifdef WOLFSSL_ASYNC_CRYPT
  296. if (error == WC_PENDING_E) {
  297. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  298. if (ret < 0) break;
  299. }
  300. else
  301. #endif
  302. {
  303. if (error == WOLFSSL_ERROR_WANT_WRITE)
  304. {
  305. select_ret = tcp_select_tx(sockfd, currTimeout);
  306. }
  307. else {
  308. #ifdef WOLFSSL_DTLS
  309. if (wolfSSL_dtls(ssl))
  310. currTimeout = wolfSSL_dtls_get_current_timeout(ssl);
  311. #endif
  312. select_ret = tcp_select(sockfd, currTimeout);
  313. }
  314. }
  315. if ((select_ret == TEST_RECV_READY) || (select_ret == TEST_SEND_READY)
  316. || (select_ret == TEST_ERROR_READY)
  317. #ifdef WOLFSSL_ASYNC_CRYPT
  318. || error == WC_PENDING_E
  319. #endif
  320. ) {
  321. #ifndef WOLFSSL_CALLBACKS
  322. ret = SSL_accept(ssl);
  323. #else
  324. ret = wolfSSL_accept_ex(ssl,
  325. srvHandShakeCB, srvTimeoutCB, srvTo);
  326. #endif
  327. error = SSL_get_error(ssl, 0);
  328. }
  329. else if (select_ret == TEST_TIMEOUT && !wolfSSL_dtls(ssl)) {
  330. error = WOLFSSL_ERROR_WANT_READ;
  331. }
  332. #ifdef WOLFSSL_DTLS
  333. else if (select_ret == TEST_TIMEOUT && wolfSSL_dtls(ssl)) {
  334. ret = wolfSSL_dtls_got_timeout(ssl);
  335. if (ret != WOLFSSL_SUCCESS)
  336. error = wolfSSL_get_error(ssl, ret);
  337. else
  338. error = WOLFSSL_ERROR_WANT_READ;
  339. ret = WOLFSSL_FAILURE; /* Reset error so we loop */
  340. }
  341. #endif
  342. else {
  343. error = WOLFSSL_FATAL_ERROR;
  344. }
  345. }
  346. return ret;
  347. }
  348. /* Echo number of bytes specified by -B arg */
  349. int ServerEchoData(SSL* ssl, int clientfd, int echoData, int block,
  350. size_t throughput)
  351. {
  352. int ret = 0, err;
  353. double start = 0, rx_time = 0, tx_time = 0;
  354. int select_ret, len, rx_pos;
  355. size_t xfer_bytes = 0;
  356. char* buffer;
  357. buffer = (char*)malloc(block);
  358. if (!buffer) {
  359. err_sys_ex(runWithErrors, "Server buffer malloc failed");
  360. }
  361. while ((echoData && throughput == 0) ||
  362. (!echoData && xfer_bytes < throughput))
  363. {
  364. select_ret = tcp_select(clientfd, 1); /* Timeout=1 second */
  365. if (select_ret == TEST_RECV_READY) {
  366. if (throughput)
  367. len = min(block, (int)(throughput - xfer_bytes));
  368. else
  369. len = block;
  370. rx_pos = 0;
  371. if (throughput) {
  372. start = current_time(1);
  373. }
  374. /* Read data */
  375. while (rx_pos < len) {
  376. ret = SSL_read(ssl, &buffer[rx_pos], len - rx_pos);
  377. if (ret <= 0) {
  378. err = SSL_get_error(ssl, 0);
  379. #ifdef WOLFSSL_ASYNC_CRYPT
  380. if (err == WC_PENDING_E) {
  381. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  382. if (ret < 0) break;
  383. }
  384. else
  385. #endif
  386. if (err != WOLFSSL_ERROR_WANT_READ &&
  387. err != WOLFSSL_ERROR_WANT_WRITE &&
  388. err != WOLFSSL_ERROR_ZERO_RETURN &&
  389. err != APP_DATA_READY) {
  390. fprintf(stderr, "SSL_read echo error %d\n", err);
  391. err_sys_ex(runWithErrors, "SSL_read failed");
  392. break;
  393. }
  394. if (err == WOLFSSL_ERROR_ZERO_RETURN) {
  395. free(buffer);
  396. return WOLFSSL_ERROR_ZERO_RETURN;
  397. }
  398. }
  399. else {
  400. rx_pos += ret;
  401. if (!throughput)
  402. break;
  403. }
  404. }
  405. if (throughput) {
  406. rx_time += current_time(0) - start;
  407. start = current_time(1);
  408. }
  409. /* Write data */
  410. do {
  411. err = 0; /* reset error */
  412. ret = SSL_write(ssl, buffer, min(len, rx_pos));
  413. if (ret <= 0) {
  414. err = SSL_get_error(ssl, 0);
  415. #ifdef WOLFSSL_ASYNC_CRYPT
  416. if (err == WC_PENDING_E) {
  417. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  418. if (ret < 0) break;
  419. }
  420. #endif
  421. }
  422. } while (err == WC_PENDING_E);
  423. if (ret != (int)min(len, rx_pos)) {
  424. fprintf(stderr, "SSL_write echo error %d\n", err);
  425. err_sys_ex(runWithErrors, "SSL_write failed");
  426. }
  427. if (throughput) {
  428. tx_time += current_time(0) - start;
  429. }
  430. xfer_bytes += len;
  431. }
  432. }
  433. free(buffer);
  434. if (throughput) {
  435. printf(
  436. #if !defined(__MINGW32__)
  437. "wolfSSL Server Benchmark %zu bytes\n"
  438. #else
  439. "wolfSSL Server Benchmark %d bytes\n"
  440. #endif
  441. "\tRX %8.3f ms (%8.3f MBps)\n"
  442. "\tTX %8.3f ms (%8.3f MBps)\n",
  443. #if !defined(__MINGW32__)
  444. throughput,
  445. #else
  446. (int)throughput,
  447. #endif
  448. rx_time * 1000, throughput / rx_time / 1024 / 1024,
  449. tx_time * 1000, throughput / tx_time / 1024 / 1024
  450. );
  451. }
  452. return 0;
  453. }
  454. static void ServerRead(WOLFSSL* ssl, char* input, int inputLen)
  455. {
  456. int ret, err;
  457. char buffer[WOLFSSL_MAX_ERROR_SZ];
  458. /* Read data */
  459. do {
  460. err = 0; /* reset error */
  461. ret = SSL_read(ssl, input, inputLen);
  462. if (ret < 0) {
  463. err = SSL_get_error(ssl, ret);
  464. #ifdef HAVE_SECURE_RENEGOTIATION
  465. if (err == APP_DATA_READY) {
  466. /* If we receive a message during renegotiation
  467. * then just print it. We return the message sent
  468. * after the renegotiation. */
  469. ret = SSL_read(ssl, input, inputLen);
  470. if (ret >= 0) {
  471. /* null terminate message */
  472. input[ret] = '\0';
  473. printf("Client message received during "
  474. "secure renegotiation: %s\n", input);
  475. err = WOLFSSL_ERROR_WANT_READ;
  476. }
  477. else {
  478. err = SSL_get_error(ssl, ret);
  479. }
  480. }
  481. #endif
  482. #ifdef WOLFSSL_ASYNC_CRYPT
  483. if (err == WC_PENDING_E) {
  484. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  485. if (ret < 0) break;
  486. }
  487. else
  488. #endif
  489. #ifdef WOLFSSL_DTLS
  490. if (wolfSSL_dtls(ssl) && err == DECRYPT_ERROR) {
  491. fprintf(stderr, "Dropped client's message due to a bad MAC\n");
  492. }
  493. else
  494. #endif
  495. if (err != WOLFSSL_ERROR_WANT_READ
  496. && err != WOLFSSL_ERROR_WANT_WRITE /* Can happen during
  497. * handshake */
  498. #ifdef HAVE_SECURE_RENEGOTIATION
  499. && err != APP_DATA_READY
  500. #endif
  501. ) {
  502. fprintf(stderr, "SSL_read input error %d, %s\n", err,
  503. ERR_error_string(err, buffer));
  504. err_sys_ex(runWithErrors, "SSL_read failed");
  505. }
  506. }
  507. else if (SSL_get_error(ssl, 0) == 0 &&
  508. tcp_select(SSL_get_fd(ssl), 0) == TEST_RECV_READY) {
  509. err = wolfSSL_peek(ssl, buffer, 0);
  510. if(err < 0) {
  511. err_sys_ex(runWithErrors, "wolfSSL_peek failed");
  512. }
  513. if (wolfSSL_pending(ssl))
  514. err = WOLFSSL_ERROR_WANT_READ;
  515. }
  516. } while (err == WC_PENDING_E
  517. || err == WOLFSSL_ERROR_WANT_READ
  518. || err == WOLFSSL_ERROR_WANT_WRITE);
  519. if (ret > 0) {
  520. /* null terminate message */
  521. input[ret] = '\0';
  522. printf("Client message: %s\n", input);
  523. }
  524. }
  525. static void ServerWrite(WOLFSSL* ssl, const char* output, int outputLen)
  526. {
  527. int ret, err;
  528. char buffer[WOLFSSL_MAX_ERROR_SZ];
  529. int len;
  530. #ifdef OPENSSL_ALL
  531. /* Fuzz testing expects reply split over two msgs when TLSv1.0 or below */
  532. if (wolfSSL_GetVersion(ssl) <= WOLFSSL_TLSV1)
  533. len = outputLen / 2;
  534. else
  535. #endif
  536. len = outputLen;
  537. do {
  538. err = 0; /* reset error */
  539. ret = SSL_write(ssl, output, len);
  540. if (ret <= 0) {
  541. err = SSL_get_error(ssl, 0);
  542. #ifdef WOLFSSL_ASYNC_CRYPT
  543. if (err == WC_PENDING_E) {
  544. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  545. if (ret < 0) break;
  546. }
  547. #endif
  548. }
  549. else if (ret != outputLen) {
  550. output += ret;
  551. len = (outputLen -= ret);
  552. err = WOLFSSL_ERROR_WANT_WRITE;
  553. }
  554. } while (err == WC_PENDING_E || err == WOLFSSL_ERROR_WANT_WRITE);
  555. if (ret != outputLen) {
  556. fprintf(stderr, "SSL_write msg error %d, %s\n", err,
  557. ERR_error_string(err, buffer));
  558. err_sys_ex(runWithErrors, "SSL_write failed");
  559. }
  560. }
  561. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  562. #define MAX_GROUP_NUMBER 4
  563. static void SetKeyShare(WOLFSSL* ssl, int onlyKeyShare, int useX25519,
  564. int useX448, int usePqc, char* pqcAlg)
  565. {
  566. int ret;
  567. int groups[MAX_GROUP_NUMBER] = {0};
  568. int count = 0;
  569. (void)useX25519;
  570. (void)useX448;
  571. (void)usePqc;
  572. (void)pqcAlg;
  573. WOLFSSL_START(WC_FUNC_CLIENT_KEY_EXCHANGE_SEND);
  574. if (onlyKeyShare == 2) {
  575. if (useX25519) {
  576. #ifdef HAVE_CURVE25519
  577. do {
  578. ret = wolfSSL_UseKeyShare(ssl, WOLFSSL_ECC_X25519);
  579. if (ret == WOLFSSL_SUCCESS)
  580. groups[count++] = WOLFSSL_ECC_X25519;
  581. #ifdef WOLFSSL_ASYNC_CRYPT
  582. else if (ret == WC_PENDING_E)
  583. wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  584. #endif
  585. else
  586. err_sys("unable to use curve x25519");
  587. } while (ret == WC_PENDING_E);
  588. #endif
  589. }
  590. else if (useX448) {
  591. #ifdef HAVE_CURVE448
  592. do {
  593. ret = wolfSSL_UseKeyShare(ssl, WOLFSSL_ECC_X448);
  594. if (ret == WOLFSSL_SUCCESS)
  595. groups[count++] = WOLFSSL_ECC_X448;
  596. #ifdef WOLFSSL_ASYNC_CRYPT
  597. else if (ret == WC_PENDING_E)
  598. wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  599. #endif
  600. else
  601. err_sys("unable to use curve x448");
  602. } while (ret == WC_PENDING_E);
  603. #endif
  604. }
  605. else if (usePqc == 1) {
  606. #ifdef HAVE_PQC
  607. groups[count] = 0;
  608. if (XSTRCMP(pqcAlg, "KYBER_LEVEL1") == 0) {
  609. groups[count] = WOLFSSL_KYBER_LEVEL1;
  610. }
  611. else if (XSTRCMP(pqcAlg, "KYBER_LEVEL3") == 0) {
  612. groups[count] = WOLFSSL_KYBER_LEVEL3;
  613. }
  614. else if (XSTRCMP(pqcAlg, "KYBER_LEVEL5") == 0) {
  615. groups[count] = WOLFSSL_KYBER_LEVEL5;
  616. }
  617. else if (XSTRCMP(pqcAlg, "NTRU_HPS_LEVEL1") == 0) {
  618. groups[count] = WOLFSSL_NTRU_HPS_LEVEL1;
  619. }
  620. else if (XSTRCMP(pqcAlg, "NTRU_HPS_LEVEL3") == 0) {
  621. groups[count] = WOLFSSL_NTRU_HPS_LEVEL3;
  622. }
  623. else if (XSTRCMP(pqcAlg, "NTRU_HPS_LEVEL5") == 0) {
  624. groups[count] = WOLFSSL_NTRU_HPS_LEVEL5;
  625. }
  626. else if (XSTRCMP(pqcAlg, "NTRU_HRSS_LEVEL3") == 0) {
  627. groups[count] = WOLFSSL_NTRU_HRSS_LEVEL3;
  628. }
  629. else if (XSTRCMP(pqcAlg, "SABER_LEVEL1") == 0) {
  630. groups[count] = WOLFSSL_SABER_LEVEL1;
  631. }
  632. else if (XSTRCMP(pqcAlg, "SABER_LEVEL3") == 0) {
  633. groups[count] = WOLFSSL_SABER_LEVEL3;
  634. }
  635. else if (XSTRCMP(pqcAlg, "SABER_LEVEL5") == 0) {
  636. groups[count] = WOLFSSL_SABER_LEVEL5;
  637. }
  638. else if (XSTRCMP(pqcAlg, "KYBER_90S_LEVEL1") == 0) {
  639. groups[count] = WOLFSSL_KYBER_90S_LEVEL1;
  640. }
  641. else if (XSTRCMP(pqcAlg, "KYBER_90S_LEVEL3") == 0) {
  642. groups[count] = WOLFSSL_KYBER_90S_LEVEL3;
  643. }
  644. else if (XSTRCMP(pqcAlg, "KYBER_90S_LEVEL5") == 0) {
  645. groups[count] = WOLFSSL_KYBER_90S_LEVEL5;
  646. }
  647. else if (XSTRCMP(pqcAlg, "P256_NTRU_HPS_LEVEL1") == 0) {
  648. groups[count] = WOLFSSL_P256_NTRU_HPS_LEVEL1;
  649. }
  650. else if (XSTRCMP(pqcAlg, "P384_NTRU_HPS_LEVEL3") == 0) {
  651. groups[count] = WOLFSSL_P384_NTRU_HPS_LEVEL3;
  652. }
  653. else if (XSTRCMP(pqcAlg, "P521_NTRU_HPS_LEVEL5") == 0) {
  654. groups[count] = WOLFSSL_P521_NTRU_HPS_LEVEL5;
  655. }
  656. else if (XSTRCMP(pqcAlg, "P384_NTRU_HRSS_LEVEL3") == 0) {
  657. groups[count] = WOLFSSL_P384_NTRU_HRSS_LEVEL3;
  658. }
  659. else if (XSTRCMP(pqcAlg, "P256_SABER_LEVEL1") == 0) {
  660. groups[count] = WOLFSSL_P256_SABER_LEVEL1;
  661. }
  662. else if (XSTRCMP(pqcAlg, "P384_SABER_LEVEL3") == 0) {
  663. groups[count] = WOLFSSL_P384_SABER_LEVEL3;
  664. }
  665. else if (XSTRCMP(pqcAlg, "P521_SABER_LEVEL5") == 0) {
  666. groups[count] = WOLFSSL_P521_SABER_LEVEL5;
  667. }
  668. else if (XSTRCMP(pqcAlg, "P256_KYBER_LEVEL1") == 0) {
  669. groups[count] = WOLFSSL_P256_KYBER_LEVEL1;
  670. }
  671. else if (XSTRCMP(pqcAlg, "P384_KYBER_LEVEL3") == 0) {
  672. groups[count] = WOLFSSL_P384_KYBER_LEVEL3;
  673. }
  674. else if (XSTRCMP(pqcAlg, "P521_KYBER_LEVEL5") == 0) {
  675. groups[count] = WOLFSSL_P521_KYBER_LEVEL5;
  676. }
  677. else if (XSTRCMP(pqcAlg, "P256_KYBER_90S_LEVEL1") == 0) {
  678. groups[count] = WOLFSSL_P256_KYBER_90S_LEVEL1;
  679. }
  680. else if (XSTRCMP(pqcAlg, "P384_KYBER_90S_LEVEL3") == 0) {
  681. groups[count] = WOLFSSL_P384_KYBER_90S_LEVEL3;
  682. }
  683. else if (XSTRCMP(pqcAlg, "P521_KYBER_90S_LEVEL5") == 0) {
  684. groups[count] = WOLFSSL_P521_KYBER_90S_LEVEL5;
  685. }
  686. if (groups[count] == 0) {
  687. err_sys("invalid post-quantum KEM specified");
  688. }
  689. else {
  690. if (wolfSSL_UseKeyShare(ssl, groups[count]) == WOLFSSL_SUCCESS) {
  691. printf("Using Post-Quantum KEM: %s\n", pqcAlg);
  692. count++;
  693. }
  694. else {
  695. groups[count] = 0;
  696. err_sys("unable to use post-quantum algorithm");
  697. }
  698. }
  699. #endif
  700. }
  701. else {
  702. #ifdef HAVE_ECC
  703. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  704. do {
  705. ret = wolfSSL_UseKeyShare(ssl, WOLFSSL_ECC_SECP256R1);
  706. if (ret == WOLFSSL_SUCCESS)
  707. groups[count++] = WOLFSSL_ECC_SECP256R1;
  708. #ifdef WOLFSSL_ASYNC_CRYPT
  709. else if (ret == WC_PENDING_E)
  710. wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  711. #endif
  712. else
  713. err_sys("unable to use curve secp256r1");
  714. } while (ret == WC_PENDING_E);
  715. #endif
  716. #endif
  717. }
  718. }
  719. if (onlyKeyShare == 1) {
  720. #ifdef HAVE_FFDHE_2048
  721. do {
  722. ret = wolfSSL_UseKeyShare(ssl, WOLFSSL_FFDHE_2048);
  723. if (ret == WOLFSSL_SUCCESS)
  724. groups[count++] = WOLFSSL_FFDHE_2048;
  725. #ifdef WOLFSSL_ASYNC_CRYPT
  726. else if (ret == WC_PENDING_E)
  727. wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  728. #endif
  729. else
  730. err_sys("unable to use DH 2048-bit parameters");
  731. } while (ret == WC_PENDING_E);
  732. #endif
  733. }
  734. if (count >= MAX_GROUP_NUMBER)
  735. err_sys("example group array size error");
  736. if (count > 0) {
  737. if (wolfSSL_set_groups(ssl, groups, count) != WOLFSSL_SUCCESS)
  738. err_sys("unable to set groups");
  739. }
  740. WOLFSSL_END(WC_FUNC_CLIENT_KEY_EXCHANGE_SEND);
  741. }
  742. #endif /* WOLFSSL_TLS13 && HAVE_SUPPORTED_CURVES */
  743. /* when adding new option, please follow the steps below: */
  744. /* 1. add new option message in English section */
  745. /* 2. increase the number of the second column */
  746. /* 3. increase the array dimension */
  747. /* 4. add the same message into Japanese section */
  748. /* (will be translated later) */
  749. /* 5. add printf() into suitable position of Usage() */
  750. static const char* server_usage_msg[][65] = {
  751. /* English */
  752. {
  753. " NOTE: All files relative to wolfSSL home dir\n", /* 0 */
  754. "-? <num> Help, print this usage\n"
  755. " 0: English, 1: Japanese\n"
  756. "--help Help, in English\n", /* 1 */
  757. "-p <num> Port to listen on, not 0, default", /* 2 */
  758. #ifndef WOLFSSL_TLS13
  759. "-v <num> SSL version [0-3], SSLv3(0) - TLS1.2(3)), default", /* 3 */
  760. #else
  761. "-v <num> SSL version [0-4], SSLv3(0) - TLS1.3(4)), default", /* 3 */
  762. #endif
  763. "-l <str> Cipher suite list (: delimited)\n", /* 4 */
  764. "-c <file> Certificate file, default", /* 5 */
  765. "-k <file> Key file, default", /* 6 */
  766. "-A <file> Certificate Authority file, default", /* 7 */
  767. "-R <file> Create Ready file for external monitor"
  768. " default none\n", /* 8 */
  769. #ifndef NO_DH
  770. "-D <file> Diffie-Hellman Params file, default", /* 9 */
  771. "-Z <num> Minimum DH key bits, default", /* 10 */
  772. #endif
  773. #ifdef HAVE_ALPN
  774. "-L <str> Application-Layer Protocol Negotiation"
  775. " ({C,F}:<list>)\n", /* 11 */
  776. #endif
  777. "-d Disable client cert check\n", /* 12 */
  778. "-b Bind to any interface instead of localhost only\n",/* 13 */
  779. "-s Use pre Shared keys\n", /* 14 */
  780. #ifndef WOLFSSL_DTLS13
  781. "-u Use UDP DTLS, add -v 2 for DTLSv1, -v 3 for DTLSv1.2"
  782. " (default)\n", /* 15 */
  783. #else
  784. "-u Use UDP DTLS, add -v 2 for DTLSv1, -v 3 for DTLSv1.2"
  785. " (default), -v 4 for DTLSv1.3\n", /* 15 */
  786. #endif /* !WOLFSSL_DTLS13 */
  787. #ifdef WOLFSSL_SCTP
  788. "-G Use SCTP DTLS,"
  789. " add -v 2 for DTLSv1, -v 3 for DTLSv1.2 (default)\n", /* 16 */
  790. #endif
  791. "-f Fewer packets/group messages\n", /* 17 */
  792. "-r Allow one client Resumption\n", /* 18 */
  793. "-N Use Non-blocking sockets\n", /* 19 */
  794. "-S <str> Use Host Name Indication\n", /* 20 */
  795. "-w Wait for bidirectional shutdown\n", /* 21 */
  796. #ifdef HAVE_OCSP
  797. "-o Perform OCSP lookup on peer certificate\n", /* 22 */
  798. "-O <url> Perform OCSP lookup using <url> as responder\n", /* 23 */
  799. #endif
  800. #ifdef HAVE_PK_CALLBACKS
  801. "-P Public Key Callbacks\n", /* 24 */
  802. #endif
  803. #ifdef HAVE_ANON
  804. "-a Anonymous server\n", /* 25 */
  805. #endif
  806. #ifndef NO_PSK
  807. "-I Do not send PSK identity hint\n", /* 26 */
  808. #endif
  809. "-x Print server errors but do not close connection\n",/* 27 */
  810. "-i Loop indefinitely (allow repeated connections)\n", /* 28 */
  811. "-e Echo data mode (return raw bytes received)\n", /* 29 */
  812. "-B <num> Benchmark throughput"
  813. " using <num> bytes and print stats\n", /* 31 */
  814. #ifdef HAVE_CRL
  815. "-V Disable CRL\n", /* 32 */
  816. #endif
  817. #ifdef WOLFSSL_TRUST_PEER_CERT
  818. "-E <file> Path to load trusted peer cert\n", /* 33 */
  819. #endif
  820. #ifdef HAVE_WNR
  821. "-q <file> Whitewood config file, default", /* 34 */
  822. #endif
  823. "-g Return basic HTML web page\n", /* 35 */
  824. "-C <num> The number of connections to accept, default: 1\n",/* 36 */
  825. "-H <arg> Internal tests"
  826. " [defCipherList, exitWithRet, verifyFail, useSupCurve,\n", /* 37 */
  827. " loadSSL, disallowETM]\n", /* 38 */
  828. #ifdef WOLFSSL_TLS13
  829. "-U Update keys and IVs before sending\n", /* 39 */
  830. "-K Key Exchange for PSK not using (EC)DHE\n", /* 40 */
  831. #ifndef NO_DH
  832. "-y Pre-generate Key Share using FFDHE_2048 only\n", /* 41 */
  833. #endif
  834. #ifdef HAVE_ECC
  835. "-Y Pre-generate Key Share using P-256 only \n", /* 42 */
  836. #endif
  837. #ifdef HAVE_CURVE25519
  838. "-t Pre-generate Key share using Curve25519 only\n", /* 43 */
  839. #endif
  840. #endif /* WOLFSSL_TLS13 */
  841. #ifdef HAVE_SESSION_TICKET
  842. #if defined(WOLFSSL_NO_TLS12) && defined(NO_OLD_TLS)
  843. "-T Do not generate session ticket\n", /* 44 */
  844. #else
  845. "-T [aon] Do not generate session ticket\n", /* 44 */
  846. " No option affects TLS 1.3 only, 'a' affects all"
  847. " protocol versions,\n", /* 45 */
  848. " 'o' affects TLS 1.2 and below only\n", /* 46 */
  849. " 'n' affects TLS 1.3 only\n", /* 47 */
  850. #endif
  851. #endif
  852. #ifdef WOLFSSL_TLS13
  853. "-F Send alert if no mutual authentication\n", /* 48 */
  854. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  855. "-Q Request certificate from client post-handshake\n", /* 49 */
  856. #endif
  857. #ifdef WOLFSSL_SEND_HRR_COOKIE
  858. "-J [n] Server sends Cookie Extension containing state (n to "
  859. "disable)\n", /* 50 */
  860. #endif
  861. #endif /* WOLFSSL_TLS13 */
  862. #ifdef WOLFSSL_EARLY_DATA
  863. "-0 Early data read from client (0-RTT handshake)\n", /* 51 */
  864. #endif
  865. #ifdef WOLFSSL_MULTICAST
  866. "-3 <grpid> Multicast, grpid < 256\n", /* 52 */
  867. #endif
  868. "-1 <num> Display a result by specified language."
  869. "\n 0: English, 1: Japanese\n", /* 53 */
  870. #ifdef HAVE_TRUSTED_CA
  871. "-5 Use Trusted CA Key Indication\n", /* 54 */
  872. #endif
  873. "-6 Simulate WANT_WRITE errors on every other IO send\n",
  874. /* 55 */
  875. #ifdef HAVE_CURVE448
  876. "-8 Pre-generate Key share using Curve448 only\n", /* 56 */
  877. #endif
  878. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  879. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  880. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  881. "-9 Use hash dir look up for certificate loading\n"
  882. " loading from <wolfSSL home>/certs folder\n"
  883. " files in the folder would have the form \"hash.N\" file name\n"
  884. " e.g symbolic link to the file at certs folder\n"
  885. " ln -s client-ca.pem `openssl x509 -in client-ca.pem -hash -noout`.0\n",
  886. /* 57 */
  887. #endif
  888. #if defined(WOLFSSL_WOLFSENTRY_HOOKS) && !defined(NO_FILESYSTEM) && !defined(WOLFSENTRY_NO_JSON)
  889. "--wolfsentry-config <file> Path for JSON wolfSentry config\n",
  890. /* 58 */
  891. #endif
  892. #ifndef WOLFSSL_TLS13
  893. "-7 Set minimum downgrade protocol version [0-3] "
  894. " SSLv3(0) - TLS1.2(3)\n",
  895. #else
  896. "-7 Set minimum downgrade protocol version [0-4] "
  897. " SSLv3(0) - TLS1.3(4)\n", /* 59 */
  898. #endif
  899. #ifdef HAVE_PQC
  900. "--pqc <alg> Key Share with specified post-quantum algorithm only [KYBER_LEVEL1, KYBER_LEVEL3,\n"
  901. " KYBER_LEVEL5, KYBER_90S_LEVEL1, KYBER_90S_LEVEL3, KYBER_90S_LEVEL5,\n"
  902. " NTRU_HPS_LEVEL1, NTRU_HPS_LEVEL3, NTRU_HPS_LEVEL5, NTRU_HRSS_LEVEL3,\n"
  903. " SABER_LEVEL1, SABER_LEVEL3, SABER_LEVEL5, P256_NTRU_HPS_LEVEL1,\n"
  904. " P384_NTRU_HPS_LEVEL3, P521_NTRU_HPS_LEVEL5, P384_NTRU_HRSS_LEVEL3,\n"
  905. " P256_SABER_LEVEL1, P384_SABER_LEVEL3, P521_SABER_LEVEL5, P256_KYBER_LEVEL1,\n"
  906. " P384_KYBER_LEVEL3, P521_KYBER_LEVEL5, P256_KYBER_90S_LEVEL1, P384_KYBER_90S_LEVEL3,\n"
  907. " P521_KYBER_90S_LEVEL5]\n", /* 60 */
  908. #endif
  909. #ifdef WOLFSSL_SRTP
  910. "--srtp <profile> (default is SRTP_AES128_CM_SHA1_80)\n", /* 61 */
  911. #endif
  912. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)
  913. "--send-ticket Send a new session ticket during application data\n",
  914. /* 62 */
  915. #endif
  916. #ifdef CAN_FORCE_CURVE
  917. "--force-curve [<curve>] Pre-generate a Key Share using <curve>.\n"
  918. " Leave <curve> blank to list all curves.\n"
  919. " Note: requires TLS1.3\n",
  920. /* 63 */
  921. #endif
  922. "\n"
  923. "For simpler wolfSSL TLS server examples, visit\n"
  924. "https://github.com/wolfSSL/wolfssl-examples/tree/master/tls\n",
  925. /* 64 */
  926. NULL,
  927. },
  928. #ifndef NO_MULTIBYTE_PRINT
  929. /* Japanese */
  930. {
  931. " 注意 : 全てのファイルは"
  932. " wolfSSL ホーム・ディレクトリからの相対です。\n", /* 0 */
  933. "-? <num> ヘルプ, 使い方を表示\n"
  934. " 0: 英語、 1: 日本語\n"
  935. "--ヘルプ 日本語で使い方を表示\n", /* 1 */
  936. "-p <num> 接続先ポート, 0は無効, 既定値", /* 2 */
  937. #ifndef WOLFSSL_TLS13
  938. "-v <num> SSL バージョン [0-3], SSLv3(0) - TLS1.2(3)),"
  939. " 既定値", /* 3 */
  940. #else
  941. "-v <num> SSL バージョン [0-4], SSLv3(0) - TLS1.3(4)),"
  942. " 既定値", /* 3 */
  943. #endif
  944. "-l <str> 暗号スイートリスト (区切り文字 :)\n", /* 4 */
  945. "-c <file> 証明書ファイル, 既定値", /* 5 */
  946. "-k <file> 鍵ファイル, 既定値", /* 6 */
  947. "-A <file> 認証局ファイル, 既定値", /* 7 */
  948. "-R <file> 外部モニタ用の準備完了ファイルを作成する。"
  949. "既定値 なし\n", /* 8 */
  950. #ifndef NO_DH
  951. "-D <file> ディフィー・ヘルマンのパラメータファイル,"
  952. " 既定値", /* 9 */
  953. "-Z <num> 最小 DH 鍵 ビット, 既定値", /* 10 */
  954. #endif
  955. #ifdef HAVE_ALPN
  956. "-L <str> アプリケーション層プロトコルネゴシエーションを行う"
  957. " ({C,F}:<list>)\n", /* 11 */
  958. #endif
  959. "-d クライアント認証を無効とする\n", /* 12 */
  960. "-b ローカルホスト以外のインターフェースへも"
  961. "バインドする\n", /* 13 */
  962. "-s 事前共有鍵を使用する\n", /* 14 */
  963. "-u UDP DTLSを使用する。\n"
  964. #ifndef WOLFSSL_DTLS13
  965. " -v 2 を追加指定するとDTLSv1, "
  966. "-v 3 を追加指定すると DTLSv1.2 (既定値)\n", /* 15 */
  967. #else
  968. " -v 2 を追加指定するとDTLSv1, "
  969. "-v 3 を追加指定すると DTLSv1.2 (既定値),\n"
  970. " -v 4 を追加指定すると DTLSv1.3\n", /* 15 */
  971. #endif /* !WOLFSSL_DTLS13 */
  972. #ifdef WOLFSSL_SCTP
  973. "-G SCTP DTLSを使用する。-v 2 を追加指定すると"
  974. " DTLSv1, -v 3 を追加指定すると DTLSv1.2 (既定値)\n", /* 16 */
  975. #endif
  976. "-f より少ないパケット/グループメッセージを使用する\n",/* 17 */
  977. "-r クライアントの再開を許可する\n", /* 18 */
  978. "-N ノンブロッキング・ソケットを使用する\n", /* 19 */
  979. "-S <str> ホスト名表示を使用する\n", /* 20 */
  980. "-w 双方向シャットダウンを待つ\n", /* 21 */
  981. #ifdef HAVE_OCSP
  982. "-o OCSPルックアップをピア証明書で実施する\n", /* 22 */
  983. "-O <url> OCSPルックアップを、"
  984. "<url>を使用し応答者として実施する\n", /* 23 */
  985. #endif
  986. #ifdef HAVE_PK_CALLBACKS
  987. "-P 公開鍵コールバック\n", /* 24 */
  988. #endif
  989. #ifdef HAVE_ANON
  990. "-a 匿名サーバー\n", /* 25 */
  991. #endif
  992. #ifndef NO_PSK
  993. "-I PSKアイデンティティのヒントを送信しない\n", /* 26 */
  994. #endif
  995. "-x サーバーエラーを出力するが接続を切断しない\n", /* 27 */
  996. "-i 無期限にループする(繰り返し接続を許可)\n", /* 28 */
  997. "-e エコー・データモード"
  998. "(受け取ったバイトデータを返す)\n", /* 29 */
  999. "-B <num> <num> バイトを用いてのベンチマーク・スループット"
  1000. "測定と結果を出力する\n", /* 31 */
  1001. #ifdef HAVE_CRL
  1002. "-V CRLを無効とする\n", /* 32 */
  1003. #endif
  1004. #ifdef WOLFSSL_TRUST_PEER_CERT
  1005. "-E <file> 信頼出来るピアの証明書ロードの為のパス\n\n", /* 33 */
  1006. #endif
  1007. #ifdef HAVE_WNR
  1008. "-q <file> Whitewood コンフィグファイル, 既定値", /* 34 */
  1009. #endif
  1010. "-g 基本的な Web ページを返す\n", /* 35 */
  1011. "-C <num> アクセプト可能な接続数を指定する。既定値: 1\n", /* 36 */
  1012. "-H <arg> 内部テスト"
  1013. " [defCipherList, exitWithRet, verifyFail, useSupCurve,\n", /* 37 */
  1014. " loadSSL, disallowETM]\n", /* 38 */
  1015. #ifdef WOLFSSL_TLS13
  1016. "-U データ送信前に、鍵とIVを更新する\n", /* 39 */
  1017. "-K 鍵交換にPSKを使用、(EC)DHEは使用しない\n", /* 40 */
  1018. #ifndef NO_DH
  1019. "-y FFDHE_2048のみを使用して鍵共有を事前生成する\n", /* 41 */
  1020. #endif
  1021. #ifdef HAVE_ECC
  1022. "-Y P-256のみを使用したキー共有の事前生成\n", /* 42 */
  1023. #endif
  1024. #ifdef HAVE_CURVE25519
  1025. "-t Curve25519のみを使用して鍵共有を事前生成する\n", /* 43 */
  1026. #endif
  1027. #endif /* WOLFSSL_TLS13 */
  1028. #if defined(WOLFSSL_NO_TLS12) && defined(NO_OLD_TLS)
  1029. "-T セッションチケットを生成しない\n", /* 44 */
  1030. #else
  1031. "-T [aon] セッションチケットを生成しない\n", /* 44 */
  1032. " オプション指定なしの場合、TLS 1.3 にだけ有効\n"
  1033. " 'a' を指定した場合、"
  1034. "全てのプロトコルバージョンに有効\n" /* 45 */
  1035. " 'o' を指定した場合、TLS 1.2 及び"
  1036. "それ以下のプロトコルバージョンに有効\n" /* 46 */
  1037. " 'n' を指定した場合、TLS 1.3 にのみ有効\n", /* 47 */
  1038. #endif
  1039. #ifdef WOLFSSL_TLS13
  1040. "-F 相互認証が無い場合にalert を送信\n", /* 48 */
  1041. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  1042. "-Q クライアントのポストハンドシェイクから"
  1043. "証明書を要求する\n", /* 49 */
  1044. #endif
  1045. #ifdef WOLFSSL_SEND_HRR_COOKIE
  1046. "-J サーバーの状態を含むTLS Cookie 拡張を送信する\n", /* 50 */
  1047. #endif
  1048. #endif /* WOLFSSL_TLS13 */
  1049. #ifdef WOLFSSL_EARLY_DATA
  1050. "-0 クライアントからの Early Data 読み取り"
  1051. "(0-RTTハンドシェイク)\n", /* 51 */
  1052. #endif
  1053. #ifdef WOLFSSL_MULTICAST
  1054. "-3 <grpid> マルチキャスト, grpid < 256\n", /* 52 */
  1055. #endif
  1056. "-1 <num> 指定された言語で結果を表示します。"
  1057. "\n 0: 英語、 1: 日本語\n", /* 53 */
  1058. #ifdef HAVE_TRUSTED_CA
  1059. "-5 信頼できる認証局の鍵表示を使用する\n", /* 54 */
  1060. #endif
  1061. "-6 交互の IO 送信で WANT_WRITE エラー"
  1062. "をシュミレート\n",
  1063. /* 55 */
  1064. #ifdef HAVE_CURVE448
  1065. "-8 Curve448のみを使用して鍵共有を事前生成する\n", /* 56 */
  1066. #endif
  1067. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  1068. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  1069. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  1070. "-9 証明書の読み込みに hash dir 機能を使用する\n"
  1071. " <wolfSSL home>/certs フォルダーからロードします\n"
  1072. " フォルダー中のファイルは、\"hash.N\"[N:0-9]名である必要があります\n"
  1073. " 以下の例ではca-cert.pemにシンボリックリンクを設定します\n"
  1074. " ln -s client-ca.pem `openssl x509 -in client-ca.pem -hash -noout`.0\n",
  1075. /* 57 */
  1076. #endif
  1077. #if defined(WOLFSSL_WOLFSENTRY_HOOKS) && !defined(NO_FILESYSTEM) && !defined(WOLFSENTRY_NO_JSON)
  1078. "--wolfsentry-config <file> wolfSentry コンフィグファイル\n",
  1079. /* 58 */
  1080. #endif
  1081. #ifndef WOLFSSL_TLS13
  1082. "-7 最小ダウングレード可能なプロトコルバージョンを設定します [0-3] "
  1083. " SSLv3(0) - TLS1.2(3)\n",
  1084. #else
  1085. "-7 最小ダウングレード可能なプロトコルバージョンを設定します [0-4] "
  1086. " SSLv3(0) - TLS1.3(4)\n", /* 59 */
  1087. #endif
  1088. #ifdef HAVE_PQC
  1089. "--pqc <alg> post-quantum 名前付きグループとの鍵共有のみ [KYBER_LEVEL1, KYBER_LEVEL3,\n"
  1090. " KYBER_LEVEL5, KYBER_90S_LEVEL1, KYBER_90S_LEVEL3, KYBER_90S_LEVEL5,\n"
  1091. " NTRU_HPS_LEVEL1, NTRU_HPS_LEVEL3, NTRU_HPS_LEVEL5, NTRU_HRSS_LEVEL3,\n"
  1092. " SABER_LEVEL1, SABER_LEVEL3, SABER_LEVEL5, P256_NTRU_HPS_LEVEL1,\n"
  1093. " P384_NTRU_HPS_LEVEL3, P521_NTRU_HPS_LEVEL5, P384_NTRU_HRSS_LEVEL3,\n"
  1094. " P256_SABER_LEVEL1, P384_SABER_LEVEL3, P521_SABER_LEVEL5, P256_KYBER_LEVEL1,\n"
  1095. " P384_KYBER_LEVEL3, P521_KYBER_LEVEL5, P256_KYBER_90S_LEVEL1, P384_KYBER_90S_LEVEL3,\n"
  1096. " P521_KYBER_90S_LEVEL5]\n", /* 60 */
  1097. #endif
  1098. #ifdef WOLFSSL_SRTP
  1099. "--srtp <profile> (デフォルトはSRTP_AES128_CM_SHA1_80)\n", /* 61 */
  1100. #endif
  1101. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)
  1102. "--send-ticket Application data 中に新しい"
  1103. "セッションチケットを送信します\n",
  1104. /* 62 */
  1105. #endif
  1106. #ifdef CAN_FORCE_CURVE
  1107. /* TODO: Need Japanese translation */
  1108. "--force-curve [<curve>] Pre-generate a Key Share using <curve>.\n"
  1109. " Leave <curve> blank to list all curves.\n"
  1110. " Note: requires TLS1.3\n",
  1111. /* 63 */
  1112. #endif
  1113. "\n"
  1114. "より簡単なwolfSSL TSL クライアントの例については"
  1115. "下記にアクセスしてください\n"
  1116. "https://github.com/wolfSSL/wolfssl-examples/tree/master/tls\n", /* 64 */
  1117. NULL,
  1118. },
  1119. #endif
  1120. };
  1121. static void Usage(void)
  1122. {
  1123. int msgId = 0;
  1124. const char** msg = server_usage_msg[lng_index];
  1125. printf("%s%s%s", "server ", LIBWOLFSSL_VERSION_STRING,
  1126. msg[msgId]);
  1127. printf("%s", msg[++msgId]); /* ? */
  1128. printf("%s %d\n", msg[++msgId], wolfSSLPort); /* -p */
  1129. #ifndef WOLFSSL_TLS13
  1130. printf("%s %d\n", msg[++msgId], SERVER_DEFAULT_VERSION); /* -v */
  1131. #else
  1132. printf("%s %d\n", msg[++msgId], SERVER_DEFAULT_VERSION); /* -v */
  1133. #endif
  1134. printf("%s", msg[++msgId]); /* -l */
  1135. printf("%s %s\n", msg[++msgId], svrCertFile); /* -c */
  1136. printf("%s %s\n", msg[++msgId], svrKeyFile); /* -k */
  1137. printf("%s %s\n", msg[++msgId], cliCertFile); /* -A */
  1138. printf("%s", msg[++msgId]); /* -R */
  1139. #ifndef NO_DH
  1140. printf("%s %s\n", msg[++msgId], dhParamFile); /* -D */
  1141. printf("%s %d\n", msg[++msgId], DEFAULT_MIN_DHKEY_BITS);/* -Z */
  1142. #endif
  1143. #ifdef HAVE_ALPN
  1144. printf("%s", msg[++msgId]); /* -L */
  1145. #endif
  1146. printf("%s", msg[++msgId]); /* -d */
  1147. printf("%s", msg[++msgId]); /* -b */
  1148. printf("%s", msg[++msgId]); /* -s */
  1149. printf("%s", msg[++msgId]); /* -u */
  1150. #ifdef WOLFSSL_SCTP
  1151. printf("%s", msg[++msgId]); /* -G */
  1152. #endif
  1153. printf("%s", msg[++msgId]); /* -f */
  1154. printf("%s", msg[++msgId]); /* -r */
  1155. printf("%s", msg[++msgId]); /* -N */
  1156. printf("%s", msg[++msgId]); /* -S */
  1157. printf("%s", msg[++msgId]); /* -w */
  1158. #ifdef HAVE_SECURE_RENEGOTIATION
  1159. printf("-M Allow Secure Renegotiation\n");
  1160. printf("-m Force Server Initiated Secure Renegotiation\n");
  1161. #endif /* HAVE_SECURE_RENEGOTIATION */
  1162. #ifdef HAVE_OCSP
  1163. printf("%s", msg[++msgId]); /* -o */
  1164. printf("%s", msg[++msgId]); /* -O */
  1165. #endif
  1166. #ifdef HAVE_PK_CALLBACKS
  1167. printf("%s", msg[++msgId]); /* -P */
  1168. #endif
  1169. #ifdef HAVE_ANON
  1170. printf("%s", msg[++msgId]); /* -a */
  1171. #endif
  1172. #ifndef NO_PSK
  1173. printf("%s", msg[++msgId]); /* -I */
  1174. #endif
  1175. printf("%s", msg[++msgId]); /* -x */
  1176. printf("%s", msg[++msgId]); /* -i */
  1177. printf("%s", msg[++msgId]); /* -e */
  1178. printf("%s", msg[++msgId]); /* -B */
  1179. #ifdef HAVE_CRL
  1180. printf("%s", msg[++msgId]); /* -V */
  1181. #endif
  1182. #ifdef WOLFSSL_TRUST_PEER_CERT
  1183. printf("%s", msg[++msgId]); /* -E */
  1184. #endif
  1185. #ifdef HAVE_WNR
  1186. printf("%s %s\n", msg[++msgId], wnrConfig); /* -q */
  1187. #endif
  1188. printf("%s", msg[++msgId]); /* -g */
  1189. printf("%s", msg[++msgId]); /* -C */
  1190. printf("%s", msg[++msgId]); /* -H */
  1191. printf("%s", msg[++msgId]); /* more -H options */
  1192. #ifdef WOLFSSL_TLS13
  1193. printf("%s", msg[++msgId]); /* -U */
  1194. printf("%s", msg[++msgId]); /* -K */
  1195. #ifndef NO_DH
  1196. printf("%s", msg[++msgId]); /* -y */
  1197. #endif
  1198. #ifdef HAVE_ECC
  1199. printf("%s", msg[++msgId]); /* -Y */
  1200. #endif
  1201. #ifdef HAVE_CURVE25519
  1202. printf("%s", msg[++msgId]); /* -t */
  1203. #endif
  1204. #endif /* WOLFSSL_TLS13 */
  1205. #ifdef HAVE_SESSION_TICKET
  1206. printf("%s", msg[++msgId]); /* -T */
  1207. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  1208. printf("%s", msg[++msgId]); /* -T */
  1209. printf("%s", msg[++msgId]); /* -T */
  1210. printf("%s", msg[++msgId]); /* -T */
  1211. #endif
  1212. #endif
  1213. #ifdef WOLFSSL_TLS13
  1214. printf("%s", msg[++msgId]); /* -F */
  1215. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  1216. printf("%s", msg[++msgId]); /* -Q */
  1217. #endif
  1218. #ifdef WOLFSSL_SEND_HRR_COOKIE
  1219. printf("%s", msg[++msgId]); /* -J */
  1220. #endif
  1221. #endif /* WOLFSSL_TLS13 */
  1222. #ifdef WOLFSSL_EARLY_DATA
  1223. printf("%s", msg[++msgId]); /* -0 */
  1224. #endif
  1225. #if !defined(NO_DH) && !defined(HAVE_FIPS) && \
  1226. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_OLD_PRIME_CHECK)
  1227. printf("-2 Disable DH Prime check\n");
  1228. #endif
  1229. #ifdef WOLFSSL_DTLS
  1230. printf("-4 <seq> DTLS fake would-block for message seq\n");
  1231. #endif
  1232. #ifdef WOLFSSL_MULTICAST
  1233. printf("%s", msg[++msgId]); /* -3 */
  1234. #endif
  1235. printf("%s", msg[++msgId]); /* -1 */
  1236. #ifdef HAVE_TRUSTED_CA
  1237. printf("%s", msg[++msgId]); /* -5 */
  1238. #endif /* HAVE_TRUSTED_CA */
  1239. printf("%s", msg[++msgId]); /* -6 */
  1240. #ifdef HAVE_CURVE448
  1241. printf("%s", msg[++msgId]); /* -8 */
  1242. #endif
  1243. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  1244. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  1245. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  1246. printf("%s", msg[++msgId]); /* -9 */
  1247. #endif
  1248. #if defined(WOLFSSL_WOLFSENTRY_HOOKS) && !defined(NO_FILESYSTEM) && \
  1249. !defined(WOLFSENTRY_NO_JSON)
  1250. printf("%s", msg[++msgId]); /* --wolfsentry-config */
  1251. #endif
  1252. printf("%s", msg[++msgId]); /* -7 */
  1253. #ifdef HAVE_PQC
  1254. printf("%s", msg[++msgId]); /* --pqc */
  1255. printf("%s", msg[++msgId]); /* --pqc options */
  1256. printf("%s", msg[++msgId]); /* more --pqc options */
  1257. printf("%s", msg[++msgId]); /* more --pqc options */
  1258. #endif
  1259. #ifdef WOLFSSL_SRTP
  1260. printf("%s", msg[++msgId]); /* dtls-srtp */
  1261. #endif
  1262. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)
  1263. printf("%s", msg[++msgId]); /* send-ticket */
  1264. #endif
  1265. #ifdef CAN_FORCE_CURVE
  1266. printf("%s", msg[++msgId]); /* force-curve */
  1267. #endif
  1268. printf("%s", msg[++msgId]); /* Examples repo link */
  1269. }
  1270. #ifdef WOLFSSL_SRTP
  1271. /**
  1272. * server_srtp_test() - print the ekm and share it with the client
  1273. * @ssl: ssl context
  1274. * @srtp_helper: srtp_test_helper shared struct with the client
  1275. *
  1276. * if @srtp_helper is NULL the ekm isn't shared, but it is still printed.
  1277. *
  1278. * calls srtp_helper_set_ekm() to wake the client and share the ekm with
  1279. * him. The client will check that the ekm matches the one computed by itself.
  1280. */
  1281. static int server_srtp_test(WOLFSSL *ssl, func_args *args)
  1282. {
  1283. size_t srtp_secret_length;
  1284. byte *srtp_secret, *p;
  1285. int ret;
  1286. #if !defined(SINGLE_THREADED) && defined(_POSIX_THREADS)
  1287. srtp_test_helper *srtp_helper = args->srtp_helper;
  1288. #else
  1289. (void)args;
  1290. #endif
  1291. ret = wolfSSL_export_dtls_srtp_keying_material(ssl, NULL,
  1292. &srtp_secret_length);
  1293. if (ret != LENGTH_ONLY_E) {
  1294. fprintf(stderr, "DTLS SRTP: Error getting key material length\n");
  1295. return ret;
  1296. }
  1297. srtp_secret = (byte*)XMALLOC(srtp_secret_length,
  1298. NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1299. if (srtp_secret == NULL) {
  1300. err_sys("DTLS SRTP: Memory error");
  1301. }
  1302. ret = wolfSSL_export_dtls_srtp_keying_material(ssl, srtp_secret,
  1303. &srtp_secret_length);
  1304. if (ret != WOLFSSL_SUCCESS) {
  1305. XFREE(srtp_secret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1306. fprintf(stderr, "DTLS SRTP: Error getting key material\n");
  1307. return ret;
  1308. }
  1309. printf("DTLS SRTP: Exported key material: ");
  1310. for (p = srtp_secret; p < srtp_secret + srtp_secret_length; p++)
  1311. printf("%02X", *p);
  1312. printf("\n");
  1313. #if !defined(SINGLE_THREADED) && defined(_POSIX_THREADS)
  1314. if (srtp_helper != NULL) {
  1315. srtp_helper_set_ekm(srtp_helper, srtp_secret, srtp_secret_length);
  1316. /* client code will free srtp_secret buffer after checking for
  1317. correctness */
  1318. return 0;
  1319. }
  1320. #endif /* _POSIX_THREADS */
  1321. XFREE(srtp_secret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1322. return 0;
  1323. }
  1324. #endif
  1325. THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
  1326. {
  1327. SOCKET_T sockfd = WOLFSSL_SOCKET_INVALID;
  1328. SOCKET_T clientfd = WOLFSSL_SOCKET_INVALID;
  1329. SOCKADDR_IN_T client_addr;
  1330. socklen_t client_len;
  1331. wolfSSL_method_func method = NULL;
  1332. SSL_CTX* ctx = 0;
  1333. SSL* ssl = 0;
  1334. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  1335. wolfsentry_errcode_t wolfsentry_ret;
  1336. #endif
  1337. int minVersion = SERVER_INVALID_VERSION;
  1338. int useWebServerMsg = 0;
  1339. char input[SRV_READ_SZ];
  1340. #ifndef WOLFSSL_VXWORKS
  1341. int ch;
  1342. static const struct mygetopt_long_config long_options[] = {
  1343. #if defined(WOLFSSL_WOLFSENTRY_HOOKS) && !defined(NO_FILESYSTEM) && \
  1344. !defined(WOLFSENTRY_NO_JSON)
  1345. { "wolfsentry-config", 1, 256 },
  1346. #endif
  1347. { "help", 0, 257 },
  1348. { "ヘルプ", 0, 258 },
  1349. #if defined(HAVE_PQC)
  1350. { "pqc", 1, 259 },
  1351. #endif
  1352. #ifdef WOLFSSL_SRTP
  1353. { "srtp", 2, 260 }, /* optional argument */
  1354. #endif
  1355. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)
  1356. { "send-ticket", 0, 261 },
  1357. #endif
  1358. #ifdef CAN_FORCE_CURVE
  1359. { "force-curve", 2, 262},
  1360. #endif
  1361. #ifdef WOLFSSL_DTLS_CID
  1362. {"cid", 2, 263},
  1363. #endif /* WOLFSSL_DTLS_CID */
  1364. { 0, 0, 0 }
  1365. };
  1366. #endif
  1367. int version = SERVER_DEFAULT_VERSION;
  1368. #ifndef WOLFSSL_NO_CLIENT_AUTH
  1369. int doCliCertCheck = 1;
  1370. #else
  1371. int doCliCertCheck = 0;
  1372. #endif
  1373. #ifdef HAVE_CRL
  1374. int disableCRL = 0;
  1375. #endif
  1376. int useAnyAddr = 0;
  1377. word16 port = wolfSSLPort;
  1378. int usePsk = 0;
  1379. int usePskPlus = 0;
  1380. int useAnon = 0;
  1381. int doDTLS = 0;
  1382. int dtlsUDP = 0;
  1383. #if (defined(WOLFSSL_SCTP) || defined(WOLFSSL_DTLS_MTU)) && \
  1384. defined(WOLFSSL_DTLS)
  1385. int dtlsMTU = 0;
  1386. #endif
  1387. int dtlsSCTP = 0;
  1388. int doMcast = 0;
  1389. #if defined(WOLFSSL_DTLS) && defined(USE_WOLFSSL_IO)
  1390. int doBlockSeq = 0;
  1391. WOLFSSL_TEST_DTLS_CTX dtlsCtx;
  1392. #endif
  1393. int needDH = 0;
  1394. int nonBlocking = 0;
  1395. int simulateWantWrite = 0;
  1396. int fewerPackets = 0;
  1397. #ifdef HAVE_PK_CALLBACKS
  1398. int pkCallbacks = 0;
  1399. PkCbInfo pkCbInfo;
  1400. #endif
  1401. int wc_shutdown = 0;
  1402. int resume = 0;
  1403. int resumeCount = 0;
  1404. int loops = 1;
  1405. int cnt = 0;
  1406. int echoData = 0;
  1407. int block = TEST_BUFFER_SIZE;
  1408. size_t throughput = 0;
  1409. int minDhKeyBits = DEFAULT_MIN_DHKEY_BITS;
  1410. short minRsaKeyBits = DEFAULT_MIN_RSAKEY_BITS;
  1411. short minEccKeyBits = DEFAULT_MIN_ECCKEY_BITS;
  1412. int doListen = 1;
  1413. int crlFlags = 0;
  1414. int ret;
  1415. int err = 0;
  1416. char* serverReadyFile = NULL;
  1417. char* alpnList = NULL;
  1418. unsigned char alpn_opt = 0;
  1419. char* cipherList = NULL;
  1420. int useDefCipherList = 0;
  1421. const char* verifyCert;
  1422. const char* ourCert;
  1423. const char* ourKey;
  1424. const char* ourDhParam = dhParamFile;
  1425. tcp_ready* readySignal = NULL;
  1426. int argc = ((func_args*)args)->argc;
  1427. char** argv = ((func_args*)args)->argv;
  1428. #ifdef WOLFSSL_TRUST_PEER_CERT
  1429. const char* trustCert = NULL;
  1430. #endif
  1431. #ifndef NO_PSK
  1432. int sendPskIdentityHint = 1;
  1433. #endif
  1434. #ifdef HAVE_SNI
  1435. char* sniHostName = NULL;
  1436. #endif
  1437. #ifdef HAVE_TRUSTED_CA
  1438. int trustedCaKeyId = 0;
  1439. #endif /* HAVE_TRUSTED_CA */
  1440. #ifdef HAVE_OCSP
  1441. int useOcsp = 0;
  1442. char* ocspUrl = NULL;
  1443. #endif
  1444. #ifdef HAVE_WNR
  1445. const char* wnrConfigFile = wnrConfig;
  1446. #endif
  1447. char buffer[WOLFSSL_MAX_ERROR_SZ];
  1448. #ifdef WOLFSSL_TLS13
  1449. int noPskDheKe = 0;
  1450. #endif
  1451. int updateKeysIVs = 0;
  1452. #ifndef NO_CERTS
  1453. int mutualAuth = 0;
  1454. #endif
  1455. int postHandAuth = 0;
  1456. int sendTicket = 0;
  1457. #ifdef WOLFSSL_EARLY_DATA
  1458. int earlyData = 0;
  1459. #endif
  1460. #ifdef HAVE_SECURE_RENEGOTIATION
  1461. int scr = 0;
  1462. int forceScr = 0;
  1463. #endif /* HAVE_SECURE_RENEGOTIATION */
  1464. #ifdef WOLFSSL_SEND_HRR_COOKIE
  1465. int hrrCookie = 0;
  1466. #endif
  1467. byte mcastID = 0;
  1468. #if !defined(NO_DH) && !defined(HAVE_FIPS) && \
  1469. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_OLD_PRIME_CHECK)
  1470. int doDhKeyCheck = 1;
  1471. #endif
  1472. #ifdef WOLFSSL_DTLS_CID
  1473. int useDtlsCID = 0;
  1474. char dtlsCID[DTLS_CID_BUFFER_SIZE] = { 0 };
  1475. #endif /* WOLFSSL_DTLS_CID */
  1476. #ifdef WOLFSSL_STATIC_MEMORY
  1477. /* Note: Actual memory used is much less, this is the entire buffer buckets,
  1478. * which is partitioned into pools of common sizes. To adjust the buckets
  1479. * sizes see WOLFMEM_BUCKETS in memory.h */
  1480. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) \
  1481. || defined(SESSION_CERTS)
  1482. /* big enough to handle most cases including session certs */
  1483. #if !defined(WOLFSSL_NO_CLIENT_AUTH) && \
  1484. ((defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \
  1485. (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH)))
  1486. /* increase is due to EdDSA_Update */
  1487. byte memory[440000];
  1488. #else
  1489. byte memory[320000];
  1490. #endif
  1491. #else
  1492. byte memory[80000];
  1493. #endif
  1494. byte memoryIO[34500]; /* max for IO buffer (TLS packet can be 16k) */
  1495. WOLFSSL_MEM_CONN_STATS ssl_stats;
  1496. #ifdef DEBUG_WOLFSSL
  1497. WOLFSSL_MEM_STATS mem_stats;
  1498. #endif
  1499. #endif
  1500. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1501. int onlyKeyShare = 0;
  1502. #endif
  1503. #if defined(HAVE_SESSION_TICKET)
  1504. #ifdef WOLFSSL_TLS13
  1505. int noTicketTls13 = 0;
  1506. #endif
  1507. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  1508. int noTicketTls12 = 0;
  1509. #endif
  1510. #endif
  1511. int useX25519 = 0;
  1512. int useX448 = 0;
  1513. int usePqc = 0;
  1514. char* pqcAlg = NULL;
  1515. int exitWithRet = 0;
  1516. int loadCertKeyIntoSSLObj = 0;
  1517. #ifdef HAVE_ENCRYPT_THEN_MAC
  1518. int disallowETM = 0;
  1519. #endif
  1520. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  1521. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  1522. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  1523. int useCertFolder = 0;
  1524. #endif
  1525. #ifdef WOLFSSL_SRTP
  1526. const char* dtlsSrtpProfiles = NULL;
  1527. #endif
  1528. #ifdef HAVE_TEST_SESSION_TICKET
  1529. MyTicketCtx myTicketCtx;
  1530. #endif
  1531. #ifdef CAN_FORCE_CURVE
  1532. int force_curve_group_id = 0;
  1533. #endif
  1534. ((func_args*)args)->return_code = -1; /* error state */
  1535. #ifndef NO_RSA
  1536. verifyCert = cliCertFile;
  1537. ourCert = svrCertFile;
  1538. ourKey = svrKeyFile;
  1539. #else
  1540. #ifdef HAVE_ECC
  1541. verifyCert = cliEccCertFile;
  1542. ourCert = eccCertFile;
  1543. ourKey = eccKeyFile;
  1544. #elif defined(HAVE_ED25519)
  1545. verifyCert = cliEdCertFile;
  1546. ourCert = edCertFile;
  1547. ourKey = edKeyFile;
  1548. #elif defined(HAVE_ED448)
  1549. verifyCert = cliEd448CertFile;
  1550. ourCert = ed448CertFile;
  1551. ourKey = ed448KeyFile;
  1552. #else
  1553. verifyCert = NULL;
  1554. ourCert = NULL;
  1555. ourKey = NULL;
  1556. #endif
  1557. #endif
  1558. (void)needDH;
  1559. (void)ourKey;
  1560. (void)ourCert;
  1561. (void)ourDhParam;
  1562. (void)verifyCert;
  1563. (void)doCliCertCheck;
  1564. (void)minDhKeyBits;
  1565. (void)minRsaKeyBits;
  1566. (void)minEccKeyBits;
  1567. (void)alpnList;
  1568. (void)alpn_opt;
  1569. (void)crlFlags;
  1570. (void)readySignal;
  1571. (void)updateKeysIVs;
  1572. #ifndef NO_CERTS
  1573. (void)mutualAuth;
  1574. #endif
  1575. (void)postHandAuth;
  1576. (void)sendTicket;
  1577. (void)mcastID;
  1578. (void)loadCertKeyIntoSSLObj;
  1579. (void)nonBlocking;
  1580. (void)pqcAlg;
  1581. (void)usePqc;
  1582. #ifdef WOLFSSL_TIRTOS
  1583. fdOpenSession(Task_self());
  1584. #endif
  1585. #ifdef WOLFSSL_VXWORKS
  1586. useAnyAddr = 1;
  1587. #else
  1588. /* Reinitialize the global myVerifyAction. */
  1589. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  1590. /* Not Used: h, z, W, X */
  1591. while ((ch = mygetopt_long(argc, argv, "?:"
  1592. "abc:defgijk:l:mop:q:rstu;v:wxy"
  1593. "A:B:C:D:E:FGH:IJ;KL:MNO:PQR:S:T;UVYZ:"
  1594. "01:23:4:567:89"
  1595. "@#", long_options, 0)) != -1) {
  1596. switch (ch) {
  1597. case '?' :
  1598. if(myoptarg!=NULL) {
  1599. lng_index = atoi(myoptarg);
  1600. if(lng_index<0||lng_index>1){
  1601. lng_index = 0;
  1602. }
  1603. }
  1604. Usage();
  1605. XEXIT_T(EXIT_SUCCESS);
  1606. case 257 :
  1607. lng_index = 0;
  1608. Usage();
  1609. XEXIT_T(EXIT_SUCCESS);
  1610. case 258 :
  1611. lng_index = 1;
  1612. Usage();
  1613. XEXIT_T(EXIT_SUCCESS);
  1614. case 'x' :
  1615. runWithErrors = 1;
  1616. break;
  1617. case 'd' :
  1618. doCliCertCheck = 0;
  1619. break;
  1620. case 'V' :
  1621. #ifdef HAVE_CRL
  1622. disableCRL = 1;
  1623. #endif
  1624. break;
  1625. case 'b' :
  1626. useAnyAddr = 1;
  1627. break;
  1628. case 's' :
  1629. usePsk = 1;
  1630. break;
  1631. case 'j' :
  1632. usePskPlus = 1;
  1633. break;
  1634. case 'u' :
  1635. doDTLS = 1;
  1636. dtlsUDP = 1;
  1637. #if (defined(WOLFSSL_SCTP) || defined(WOLFSSL_DTLS_MTU)) && \
  1638. defined(WOLFSSL_DTLS)
  1639. dtlsMTU = atoi(myoptarg);
  1640. #endif
  1641. break;
  1642. #ifdef WOLFSSL_SRTP
  1643. case 260:
  1644. doDTLS = 1;
  1645. dtlsUDP = 1;
  1646. dtlsSrtpProfiles = myoptarg != NULL ? myoptarg :
  1647. "SRTP_AES128_CM_SHA1_80";
  1648. printf("Using SRTP Profile(s): %s\n", dtlsSrtpProfiles);
  1649. break;
  1650. #endif
  1651. case 'G' :
  1652. #ifdef WOLFSSL_SCTP
  1653. doDTLS = 1;
  1654. dtlsUDP = 1;
  1655. dtlsSCTP = 1;
  1656. #endif
  1657. break;
  1658. case 'f' :
  1659. fewerPackets = 1;
  1660. break;
  1661. case 'R' :
  1662. serverReadyFile = myoptarg;
  1663. break;
  1664. case 'r' :
  1665. #ifndef NO_SESSION_CACHE
  1666. resume = 1;
  1667. #endif
  1668. break;
  1669. case 'P' :
  1670. #ifdef HAVE_PK_CALLBACKS
  1671. pkCallbacks = 1;
  1672. #endif
  1673. break;
  1674. case 'p' :
  1675. port = (word16)atoi(myoptarg);
  1676. break;
  1677. case 'w' :
  1678. wc_shutdown = 1;
  1679. break;
  1680. case 'v' :
  1681. if (myoptarg[0] == 'd') {
  1682. version = SERVER_DOWNGRADE_VERSION;
  1683. break;
  1684. }
  1685. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  1686. else if (myoptarg[0] == 'e') {
  1687. version = EITHER_DOWNGRADE_VERSION;
  1688. #ifndef NO_CERTS
  1689. loadCertKeyIntoSSLObj = 1;
  1690. #endif
  1691. break;
  1692. }
  1693. #endif
  1694. version = atoi(myoptarg);
  1695. if (version < 0 || version > 4) {
  1696. Usage();
  1697. XEXIT_T(MY_EX_USAGE);
  1698. }
  1699. break;
  1700. case 'l' :
  1701. cipherList = myoptarg;
  1702. break;
  1703. case 'H' :
  1704. if (XSTRCMP(myoptarg, "defCipherList") == 0) {
  1705. printf("Using default cipher list for testing\n");
  1706. useDefCipherList = 1;
  1707. }
  1708. else if (XSTRCMP(myoptarg, "exitWithRet") == 0) {
  1709. printf("Skip exit() for testing\n");
  1710. exitWithRet = 1;
  1711. }
  1712. else if (XSTRCMP(myoptarg, "verifyFail") == 0) {
  1713. printf("Verify should fail\n");
  1714. myVerifyAction = VERIFY_FORCE_FAIL;
  1715. }
  1716. else if (XSTRCMP(myoptarg, "verifyInfo") == 0) {
  1717. printf("Verify should use preverify (just show info)\n");
  1718. myVerifyAction = VERIFY_USE_PREVERFIY;
  1719. }
  1720. else if (XSTRCMP(myoptarg, "loadSSL") == 0) {
  1721. printf("Also load cert/key into wolfSSL object\n");
  1722. #ifndef NO_CERTS
  1723. loadCertKeyIntoSSLObj = 2;
  1724. #endif
  1725. }
  1726. else if (XSTRCMP(myoptarg, "loadSSLOnly") == 0) {
  1727. printf("Only load cert/key into wolfSSL object\n");
  1728. #ifndef NO_CERTS
  1729. loadCertKeyIntoSSLObj = 1;
  1730. #endif
  1731. }
  1732. else if (XSTRCMP(myoptarg, "disallowETM") == 0) {
  1733. printf("Disallow Encrypt-Then-MAC\n");
  1734. #ifdef HAVE_ENCRYPT_THEN_MAC
  1735. disallowETM = 1;
  1736. #endif
  1737. }
  1738. else if (XSTRCMP(myoptarg, "overrideDateErr") == 0) {
  1739. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1740. myVerifyAction = VERIFY_OVERRIDE_DATE_ERR;
  1741. #endif
  1742. }
  1743. else {
  1744. Usage();
  1745. XEXIT_T(MY_EX_USAGE);
  1746. }
  1747. break;
  1748. case 'A' :
  1749. verifyCert = myoptarg;
  1750. break;
  1751. case 'c' :
  1752. ourCert = myoptarg;
  1753. break;
  1754. case 'k' :
  1755. ourKey = myoptarg;
  1756. break;
  1757. case 'D' :
  1758. #ifndef NO_DH
  1759. ourDhParam = myoptarg;
  1760. #endif
  1761. break;
  1762. case 'Z' :
  1763. #ifndef NO_DH
  1764. minDhKeyBits = atoi(myoptarg);
  1765. if (minDhKeyBits <= 0 || minDhKeyBits > 16000) {
  1766. Usage();
  1767. XEXIT_T(MY_EX_USAGE);
  1768. }
  1769. #endif
  1770. break;
  1771. case 'N':
  1772. nonBlocking = 1;
  1773. break;
  1774. case 'S' :
  1775. #ifdef HAVE_SNI
  1776. sniHostName = myoptarg;
  1777. #endif
  1778. break;
  1779. case 'o' :
  1780. #ifdef HAVE_OCSP
  1781. useOcsp = 1;
  1782. #endif
  1783. break;
  1784. case 'O' :
  1785. #ifdef HAVE_OCSP
  1786. useOcsp = 1;
  1787. ocspUrl = myoptarg;
  1788. #endif
  1789. break;
  1790. case 'a' :
  1791. #ifdef HAVE_ANON
  1792. useAnon = 1;
  1793. #endif
  1794. break;
  1795. case 'I':
  1796. #ifndef NO_PSK
  1797. sendPskIdentityHint = 0;
  1798. #endif
  1799. break;
  1800. case 'L' :
  1801. #ifdef HAVE_ALPN
  1802. alpnList = myoptarg;
  1803. if (alpnList[0] == 'C' && alpnList[1] == ':')
  1804. alpn_opt = WOLFSSL_ALPN_CONTINUE_ON_MISMATCH;
  1805. else if (alpnList[0] == 'F' && alpnList[1] == ':')
  1806. alpn_opt = WOLFSSL_ALPN_FAILED_ON_MISMATCH;
  1807. else {
  1808. Usage();
  1809. XEXIT_T(MY_EX_USAGE);
  1810. }
  1811. alpnList += 2;
  1812. #endif
  1813. break;
  1814. case 'i' :
  1815. loops = -1;
  1816. break;
  1817. case 'C' :
  1818. loops = atoi(myoptarg);
  1819. if (loops <= 0) {
  1820. Usage();
  1821. XEXIT_T(MY_EX_USAGE);
  1822. }
  1823. break;
  1824. case 'e' :
  1825. echoData = 1;
  1826. break;
  1827. case 'B':
  1828. throughput = atol(myoptarg);
  1829. for (; *myoptarg != '\0'; myoptarg++) {
  1830. if (*myoptarg == ',') {
  1831. block = atoi(myoptarg + 1);
  1832. break;
  1833. }
  1834. }
  1835. if (throughput == 0 || block <= 0) {
  1836. Usage();
  1837. XEXIT_T(MY_EX_USAGE);
  1838. }
  1839. break;
  1840. #ifdef WOLFSSL_TRUST_PEER_CERT
  1841. case 'E' :
  1842. trustCert = myoptarg;
  1843. break;
  1844. #endif
  1845. case 'q' :
  1846. #ifdef HAVE_WNR
  1847. wnrConfigFile = myoptarg;
  1848. #endif
  1849. break;
  1850. case 'g' :
  1851. useWebServerMsg = 1;
  1852. break;
  1853. case 'y' :
  1854. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES) \
  1855. && !defined(NO_DH)
  1856. onlyKeyShare = 1;
  1857. #endif
  1858. break;
  1859. case 'Y' :
  1860. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES) \
  1861. && defined(HAVE_ECC)
  1862. onlyKeyShare = 2;
  1863. #endif
  1864. break;
  1865. case 't' :
  1866. #ifdef HAVE_CURVE25519
  1867. useX25519 = 1;
  1868. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1869. onlyKeyShare = 2;
  1870. #endif
  1871. #endif
  1872. break;
  1873. case 'K' :
  1874. #ifdef WOLFSSL_TLS13
  1875. noPskDheKe = 1;
  1876. #endif
  1877. break;
  1878. case 'T' :
  1879. #if defined(HAVE_SESSION_TICKET)
  1880. if (XSTRLEN(myoptarg) == 0) {
  1881. #if defined(WOLFSSL_TLS13)
  1882. noTicketTls13 = 1;
  1883. #endif
  1884. }
  1885. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  1886. else if (XSTRCMP(myoptarg, "a") == 0) {
  1887. noTicketTls12 = 1;
  1888. #if defined(WOLFSSL_TLS13)
  1889. noTicketTls13 = 1;
  1890. #endif
  1891. }
  1892. else if (XSTRCMP(myoptarg, "o") == 0) {
  1893. noTicketTls12 = 1;
  1894. }
  1895. else if (XSTRCMP(myoptarg, "n") == 0) {
  1896. #if defined(WOLFSSL_TLS13)
  1897. noTicketTls13 = 1;
  1898. #endif
  1899. }
  1900. #endif
  1901. else {
  1902. Usage();
  1903. XEXIT_T(MY_EX_USAGE);
  1904. }
  1905. #endif
  1906. break;
  1907. case 'U' :
  1908. #ifdef WOLFSSL_TLS13
  1909. updateKeysIVs = 1;
  1910. #endif
  1911. break;
  1912. #ifndef NO_CERTS
  1913. case 'F' :
  1914. mutualAuth = 1;
  1915. break;
  1916. #endif
  1917. case 'Q' :
  1918. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  1919. postHandAuth = 1;
  1920. doCliCertCheck = 0;
  1921. #endif
  1922. break;
  1923. case 'J' :
  1924. #ifdef WOLFSSL_SEND_HRR_COOKIE
  1925. hrrCookie = 1;
  1926. if (XSTRCMP(myoptarg, "n") == 0)
  1927. hrrCookie = -1;
  1928. #endif
  1929. break;
  1930. case 'M' :
  1931. #ifdef HAVE_SECURE_RENEGOTIATION
  1932. scr = 1;
  1933. #endif /* HAVE_SECURE_RENEGOTIATION */
  1934. break;
  1935. case 'm' :
  1936. #ifdef HAVE_SECURE_RENEGOTIATION
  1937. scr = 1;
  1938. forceScr = 1;
  1939. #endif /* HAVE_SECURE_RENEGOTIATION */
  1940. break;
  1941. case '0' :
  1942. #ifdef WOLFSSL_EARLY_DATA
  1943. earlyData = 1;
  1944. #endif
  1945. break;
  1946. case '1' :
  1947. lng_index = atoi(myoptarg);
  1948. if(lng_index<0||lng_index>1){
  1949. lng_index = 0;
  1950. }
  1951. break;
  1952. case '2' :
  1953. #if !defined(NO_DH) && !defined(HAVE_FIPS) && \
  1954. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_OLD_PRIME_CHECK)
  1955. doDhKeyCheck = 0;
  1956. #endif
  1957. break;
  1958. case '3' :
  1959. #ifdef WOLFSSL_MULTICAST
  1960. doMcast = 1;
  1961. mcastID = (byte)(atoi(myoptarg) & 0xFF);
  1962. #endif
  1963. break;
  1964. case '4' :
  1965. #if defined(WOLFSSL_DTLS) && defined(USE_WOLFSSL_IO)
  1966. XMEMSET(&dtlsCtx, 0, sizeof(dtlsCtx));
  1967. doBlockSeq = 1;
  1968. dtlsCtx.blockSeq = atoi(myoptarg);
  1969. #endif
  1970. break;
  1971. case '5' :
  1972. #ifdef HAVE_TRUSTED_CA
  1973. trustedCaKeyId = 1;
  1974. #endif /* HAVE_TRUSTED_CA */
  1975. break;
  1976. case '6' :
  1977. #ifdef WOLFSSL_ASYNC_IO
  1978. nonBlocking = 1;
  1979. simulateWantWrite = 1;
  1980. #else
  1981. fprintf(stderr, "Ignoring -6 since async I/O support not "
  1982. "compiled in.\n");
  1983. #endif
  1984. break;
  1985. case '7' :
  1986. minVersion = atoi(myoptarg);
  1987. if (minVersion < 0 || minVersion > 4) {
  1988. Usage();
  1989. XEXIT_T(MY_EX_USAGE);
  1990. }
  1991. break;
  1992. case '8' :
  1993. #ifdef HAVE_CURVE448
  1994. useX448 = 1;
  1995. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1996. onlyKeyShare = 2;
  1997. #endif
  1998. #endif
  1999. break;
  2000. case '9' :
  2001. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  2002. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  2003. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  2004. useCertFolder = 1;
  2005. break;
  2006. #endif
  2007. case '@' :
  2008. {
  2009. #ifdef HAVE_WC_INTROSPECTION
  2010. const char *conf_args = wolfSSL_configure_args();
  2011. if (conf_args) {
  2012. puts(conf_args);
  2013. XEXIT_T(EXIT_SUCCESS);
  2014. } else {
  2015. fputs("configure args not compiled in.\n",stderr);
  2016. XEXIT_T(MY_EX_USAGE);
  2017. }
  2018. #else
  2019. fputs("compiled without BUILD_INTROSPECTION.\n",stderr);
  2020. XEXIT_T(MY_EX_USAGE);
  2021. #endif
  2022. }
  2023. case '#' :
  2024. {
  2025. #ifdef HAVE_WC_INTROSPECTION
  2026. const char *cflags = wolfSSL_global_cflags();
  2027. if (cflags) {
  2028. puts(cflags);
  2029. XEXIT_T(EXIT_SUCCESS);
  2030. } else {
  2031. fputs("CFLAGS not compiled in.\n",stderr);
  2032. XEXIT_T(MY_EX_USAGE);
  2033. }
  2034. #else
  2035. fputs("compiled without BUILD_INTROSPECTION.\n",stderr);
  2036. XEXIT_T(MY_EX_USAGE);
  2037. #endif
  2038. }
  2039. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  2040. case 256:
  2041. #if !defined(NO_FILESYSTEM) && !defined(WOLFSENTRY_NO_JSON)
  2042. wolfsentry_config_path = myoptarg;
  2043. #endif
  2044. break;
  2045. #endif
  2046. #ifdef HAVE_PQC
  2047. case 259:
  2048. usePqc = 1;
  2049. onlyKeyShare = 2;
  2050. pqcAlg = myoptarg;
  2051. break;
  2052. #endif
  2053. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)
  2054. case 261:
  2055. sendTicket = 1;
  2056. break;
  2057. #endif
  2058. #ifdef CAN_FORCE_CURVE
  2059. case 262: {
  2060. /* Note: this requires TSL1.3 (version >= 4) */
  2061. #ifdef HAVE_ECC
  2062. int idx = 0; /* ecc curve index */
  2063. int j = 0; /* our group index */
  2064. #endif
  2065. if (NULL == myoptarg) {
  2066. Usage();
  2067. if (lng_index == 1) {
  2068. /* TODO: Need Japanese translation */
  2069. printf("\nAvailable choices for --force-curve:\n");
  2070. } else {
  2071. printf("\nAvailable choices for --force-curve:\n");
  2072. }
  2073. #ifdef HAVE_ECC
  2074. for (idx=0; ; ++idx) {
  2075. int id = wc_ecc_get_curve_id(idx);
  2076. if (ECC_CURVE_INVALID == id) {
  2077. break;
  2078. }
  2079. for (j=0; group_id_to_text[j].group != 0; ++j) {
  2080. if (XSTRCMP(group_id_to_text[j].name,
  2081. wc_ecc_get_curve_name_from_id(id)) == 0) {
  2082. printf("\t%s\n", group_id_to_text[j].name);
  2083. }
  2084. }
  2085. }
  2086. #endif
  2087. #ifdef HAVE_CURVE25519
  2088. printf("\tCURVE25519\n");
  2089. #endif
  2090. #ifdef HAVE_CURVE448
  2091. printf("\tCURVE448\n");
  2092. #endif
  2093. printf("\n");
  2094. XEXIT_T(EXIT_SUCCESS);
  2095. }
  2096. #ifdef HAVE_ECC
  2097. for (j=0; group_id_to_text[j].group != 0; ++j) {
  2098. if (XSTRCMP(group_id_to_text[j].name, myoptarg) == 0) {
  2099. force_curve_group_id = group_id_to_text[j].group;
  2100. }
  2101. }
  2102. #endif
  2103. #ifdef HAVE_CURVE25519
  2104. if (force_curve_group_id <= 0) {
  2105. if (XSTRCMP(myoptarg, "CURVE25519") == 0) {
  2106. force_curve_group_id = WOLFSSL_ECC_X25519;
  2107. }
  2108. }
  2109. #endif
  2110. #ifdef HAVE_CURVE448
  2111. if (force_curve_group_id <= 0) {
  2112. if (XSTRCMP(myoptarg, "CURVE448") == 0) {
  2113. force_curve_group_id = WOLFSSL_ECC_X448;
  2114. }
  2115. }
  2116. #endif
  2117. if (force_curve_group_id <= 0) {
  2118. if (lng_index == 1) {
  2119. /* TODO: Need Japanese translation */
  2120. fprintf(stderr, "Invalid curve '%s'\n", myoptarg);
  2121. } else {
  2122. fprintf(stderr, "Invalid curve '%s'\n", myoptarg);
  2123. }
  2124. XEXIT_T(EXIT_FAILURE);
  2125. }
  2126. }
  2127. break;
  2128. #endif /* CAN_FORCE_CURVE */
  2129. #ifdef WOLFSSL_DTLS_CID
  2130. case 263:
  2131. useDtlsCID = 1;
  2132. if (myoptarg != NULL) {
  2133. if (XSTRLEN(myoptarg) >= DTLS_CID_BUFFER_SIZE) {
  2134. err_sys("provided connection ID is too big");
  2135. }
  2136. else {
  2137. strcpy(dtlsCID, myoptarg);
  2138. }
  2139. }
  2140. break;
  2141. #endif /* WOLFSSL_CID */
  2142. default:
  2143. Usage();
  2144. XEXIT_T(MY_EX_USAGE);
  2145. }
  2146. }
  2147. myoptind = 0; /* reset for test cases */
  2148. #endif /* !WOLFSSL_VXWORKS */
  2149. /* Can only use DTLS over UDP or SCTP, can't do both. */
  2150. if (dtlsUDP && dtlsSCTP) {
  2151. err_sys_ex(runWithErrors, "Cannot use DTLS with both UDP and SCTP.");
  2152. }
  2153. /* sort out DTLS versus TLS versions */
  2154. if (version == CLIENT_INVALID_VERSION) {
  2155. if (doDTLS)
  2156. version = CLIENT_DTLS_DEFAULT_VERSION;
  2157. else
  2158. version = CLIENT_DEFAULT_VERSION;
  2159. }
  2160. else {
  2161. if (doDTLS) {
  2162. if (version == 3) {
  2163. version = -2;
  2164. }
  2165. else if (version == 4) {
  2166. #ifdef WOLFSSL_DTLS13
  2167. version = -4;
  2168. #else
  2169. err_sys_ex(runWithErrors, "Bad DTLS version");
  2170. #endif /* WOLFSSL_DTLS13 */
  2171. }
  2172. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2173. else if (version == EITHER_DOWNGRADE_VERSION) {
  2174. version = -3;
  2175. }
  2176. #endif
  2177. else if (version == 2)
  2178. version = -1;
  2179. }
  2180. }
  2181. #ifdef HAVE_WNR
  2182. if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0)
  2183. err_sys_ex(runWithErrors, "can't load whitewood net random config "
  2184. "file");
  2185. #endif
  2186. #ifdef HAVE_PQC
  2187. if (usePqc) {
  2188. if (version == SERVER_DOWNGRADE_VERSION ||
  2189. version == EITHER_DOWNGRADE_VERSION) {
  2190. fprintf(stderr,
  2191. "WARNING: If a TLS 1.3 connection is not negotiated, you "
  2192. "will not be using a post-quantum group.\n");
  2193. } else if (version != 4 && version != -4) {
  2194. err_sys("can only use post-quantum groups with TLS 1.3 or DTLS 1.3");
  2195. }
  2196. }
  2197. #endif
  2198. switch (version) {
  2199. #ifndef NO_OLD_TLS
  2200. #ifdef WOLFSSL_ALLOW_SSLV3
  2201. case 0:
  2202. method = wolfSSLv3_server_method_ex;
  2203. break;
  2204. #endif
  2205. #ifndef NO_TLS
  2206. #ifdef WOLFSSL_ALLOW_TLSV10
  2207. case 1:
  2208. method = wolfTLSv1_server_method_ex;
  2209. break;
  2210. #endif
  2211. case 2:
  2212. method = wolfTLSv1_1_server_method_ex;
  2213. break;
  2214. #endif /* !NO_TLS */
  2215. #endif /* !NO_OLD_TLS */
  2216. #ifndef NO_TLS
  2217. #ifndef WOLFSSL_NO_TLS12
  2218. case 3:
  2219. method = wolfTLSv1_2_server_method_ex;
  2220. break;
  2221. #endif
  2222. #ifdef WOLFSSL_TLS13
  2223. case 4:
  2224. method = wolfTLSv1_3_server_method_ex;
  2225. break;
  2226. #endif
  2227. case SERVER_DOWNGRADE_VERSION:
  2228. if (!doDTLS) {
  2229. method = wolfSSLv23_server_method_ex;
  2230. }
  2231. else {
  2232. #ifdef WOLFSSL_DTLS
  2233. method = wolfDTLS_server_method_ex;
  2234. #else
  2235. err_sys_ex(runWithErrors, "version not supported");
  2236. #endif /* WOLFSSL_DTLS */
  2237. }
  2238. break;
  2239. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2240. case EITHER_DOWNGRADE_VERSION:
  2241. method = wolfSSLv23_method_ex;
  2242. break;
  2243. #endif
  2244. #endif /* NO_TLS */
  2245. #ifdef WOLFSSL_DTLS
  2246. #ifndef NO_OLD_TLS
  2247. case -1:
  2248. method = wolfDTLSv1_server_method_ex;
  2249. break;
  2250. #endif
  2251. #ifndef WOLFSSL_NO_TLS12
  2252. case -2:
  2253. method = wolfDTLSv1_2_server_method_ex;
  2254. break;
  2255. #endif
  2256. #ifdef WOLFSSL_DTLS13
  2257. case -4:
  2258. method = wolfDTLSv1_3_server_method_ex;
  2259. break;
  2260. #endif
  2261. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  2262. case -3:
  2263. method = wolfDTLSv1_2_method_ex;
  2264. break;
  2265. #endif
  2266. #endif
  2267. default:
  2268. err_sys_ex(runWithErrors, "Bad SSL version");
  2269. }
  2270. if (method == NULL)
  2271. err_sys_ex(runWithErrors, "unable to get method");
  2272. #ifdef WOLFSSL_STATIC_MEMORY
  2273. #ifdef DEBUG_WOLFSSL
  2274. /* print off helper buffer sizes for use with static memory
  2275. * printing to stderr in case of debug mode turned on */
  2276. fprintf(stderr, "static memory management size = %d\n",
  2277. wolfSSL_MemoryPaddingSz());
  2278. fprintf(stderr, "calculated optimum general buffer size = %d\n",
  2279. wolfSSL_StaticBufferSz(memory, sizeof(memory), 0));
  2280. fprintf(stderr, "calculated optimum IO buffer size = %d\n",
  2281. wolfSSL_StaticBufferSz(memoryIO, sizeof(memoryIO),
  2282. WOLFMEM_IO_POOL_FIXED));
  2283. #endif /* DEBUG_WOLFSSL */
  2284. if (wolfSSL_CTX_load_static_memory(&ctx, method, memory, sizeof(memory),0,1)
  2285. != WOLFSSL_SUCCESS)
  2286. err_sys_ex(catastrophic, "unable to load static memory and create ctx");
  2287. /* load in a buffer for IO */
  2288. if (wolfSSL_CTX_load_static_memory(&ctx, NULL, memoryIO, sizeof(memoryIO),
  2289. WOLFMEM_IO_POOL_FIXED | WOLFMEM_TRACK_STATS, 1)
  2290. != WOLFSSL_SUCCESS)
  2291. err_sys_ex(catastrophic, "unable to load static memory and create ctx");
  2292. #else
  2293. if (method != NULL) {
  2294. ctx = SSL_CTX_new(method(NULL));
  2295. }
  2296. #endif /* WOLFSSL_STATIC_MEMORY */
  2297. if (ctx == NULL)
  2298. err_sys_ex(catastrophic, "unable to get ctx");
  2299. if (minVersion != SERVER_INVALID_VERSION) {
  2300. #ifdef WOLFSSL_DTLS
  2301. if (doDTLS) {
  2302. switch (minVersion) {
  2303. #ifdef WOLFSSL_DTLS13
  2304. case 4:
  2305. minVersion = WOLFSSL_DTLSV1_3;
  2306. break;
  2307. #endif /* WOLFSSL_DTLS13 */
  2308. case 3:
  2309. minVersion = WOLFSSL_DTLSV1_2;
  2310. break;
  2311. case 2:
  2312. minVersion = WOLFSSL_DTLSV1;
  2313. break;
  2314. }
  2315. }
  2316. #endif /* WOLFSSL_DTLS13 */
  2317. if (wolfSSL_CTX_SetMinVersion(ctx, minVersion) != WOLFSSL_SUCCESS)
  2318. err_sys_ex(catastrophic, "can't set minimum downgrade version");
  2319. }
  2320. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  2321. /* Restore wolfSSL verify defaults */
  2322. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_DEFAULT, NULL);
  2323. #endif
  2324. #ifdef WOLFSSL_SRTP
  2325. if (dtlsSrtpProfiles != NULL) {
  2326. if (wolfSSL_CTX_set_tlsext_use_srtp(ctx, dtlsSrtpProfiles)
  2327. != WOLFSSL_SUCCESS) {
  2328. err_sys_ex(catastrophic, "unable to set DTLS SRTP profile");
  2329. }
  2330. }
  2331. #endif
  2332. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  2333. if (wolfsentry_setup(&wolfsentry, wolfsentry_config_path,
  2334. WOLFSENTRY_ROUTE_FLAG_DIRECTION_IN) < 0) {
  2335. err_sys("unable to initialize wolfSentry");
  2336. }
  2337. if (wolfSSL_CTX_set_AcceptFilter(
  2338. ctx,
  2339. (NetworkFilterCallback_t)wolfSentry_NetworkFilterCallback,
  2340. wolfsentry) < 0) {
  2341. err_sys_ex(catastrophic,
  2342. "unable to install wolfSentry_NetworkFilterCallback");
  2343. }
  2344. #endif
  2345. if (simulateWantWrite)
  2346. {
  2347. #ifdef USE_WOLFSSL_IO
  2348. wolfSSL_CTX_SetIOSend(ctx, SimulateWantWriteIOSendCb);
  2349. #endif
  2350. }
  2351. #ifdef HAVE_TEST_SESSION_TICKET
  2352. if (TicketInit() != 0)
  2353. err_sys_ex(catastrophic, "unable to setup Session Ticket Key context");
  2354. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  2355. XMEMSET(&myTicketCtx, 0, sizeof(myTicketCtx));
  2356. wolfSSL_CTX_set_TicketEncCtx(ctx, &myTicketCtx);
  2357. #endif
  2358. #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_STATIC_EPHEMERAL)
  2359. /* used for testing only to set a static/fixed ephemeral key
  2360. for use with the sniffer */
  2361. #if defined(HAVE_ECC) && !defined(NO_ECC_SECP) && \
  2362. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES))
  2363. ret = wolfSSL_CTX_set_ephemeral_key(ctx, WC_PK_TYPE_ECDH,
  2364. "./certs/statickeys/ecc-secp256r1.pem", 0, WOLFSSL_FILETYPE_PEM);
  2365. if (ret != 0) {
  2366. err_sys_ex(runWithErrors, "error loading static ECDH key");
  2367. }
  2368. {
  2369. const byte* key = NULL;
  2370. word32 keySz = 0;
  2371. /* example for getting pointer to loaded static ephemeral key */
  2372. wolfSSL_CTX_get_ephemeral_key(ctx, WC_PK_TYPE_ECDH, &key, &keySz);
  2373. (void)key;
  2374. (void)keySz;
  2375. }
  2376. #endif
  2377. #ifndef NO_DH
  2378. ret = wolfSSL_CTX_set_ephemeral_key(ctx, WC_PK_TYPE_DH,
  2379. "./certs/statickeys/dh-ffdhe2048.pem", 0, WOLFSSL_FILETYPE_PEM);
  2380. if (ret != 0) {
  2381. err_sys_ex(runWithErrors, "error loading static DH key");
  2382. }
  2383. #endif
  2384. #ifdef HAVE_CURVE25519
  2385. ret = wolfSSL_CTX_set_ephemeral_key(ctx, WC_PK_TYPE_CURVE25519,
  2386. "./certs/statickeys/x25519.pem", 0, WOLFSSL_FILETYPE_PEM);
  2387. if (ret != 0) {
  2388. err_sys_ex(runWithErrors, "error loading static X25519 key");
  2389. }
  2390. #endif
  2391. #endif /* WOLFSSL_SNIFFER && WOLFSSL_STATIC_EPHEMERAL */
  2392. if (cipherList && !useDefCipherList) {
  2393. if (SSL_CTX_set_cipher_list(ctx, cipherList) != WOLFSSL_SUCCESS)
  2394. err_sys_ex(runWithErrors, "server can't set custom cipher list");
  2395. }
  2396. #ifdef WOLFSSL_LEANPSK
  2397. if (!usePsk) {
  2398. usePsk = 1;
  2399. }
  2400. #endif
  2401. #if defined(NO_RSA) && !defined(HAVE_ECC) && !defined(HAVE_ED25519) && \
  2402. !defined(HAVE_ED448)
  2403. if (!usePsk) {
  2404. usePsk = 1;
  2405. }
  2406. #endif
  2407. if (fewerPackets)
  2408. wolfSSL_CTX_set_group_messages(ctx);
  2409. #if (defined(WOLFSSL_SCTP) || defined(WOLFSSL_DTLS_MTU)) && \
  2410. defined(WOLFSSL_DTLS)
  2411. if (dtlsMTU)
  2412. wolfSSL_CTX_dtls_set_mtu(ctx, dtlsMTU);
  2413. #endif
  2414. #ifdef WOLFSSL_SCTP
  2415. if (dtlsSCTP)
  2416. wolfSSL_CTX_dtls_set_sctp(ctx);
  2417. #endif
  2418. #ifdef WOLFSSL_ENCRYPTED_KEYS
  2419. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  2420. #endif
  2421. #if !defined(NO_CERTS)
  2422. if ((!usePsk || usePskPlus) && !useAnon && !(loadCertKeyIntoSSLObj == 1)) {
  2423. #if defined(NO_FILESYSTEM) && defined(USE_CERT_BUFFERS_2048)
  2424. if (wolfSSL_CTX_use_certificate_chain_buffer_format(ctx,
  2425. server_cert_der_2048, sizeof_server_cert_der_2048,
  2426. WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS)
  2427. err_sys_ex(catastrophic, "can't load server cert buffer");
  2428. #elif !defined(TEST_LOAD_BUFFER)
  2429. if (SSL_CTX_use_certificate_chain_file(ctx, ourCert)
  2430. != WOLFSSL_SUCCESS)
  2431. err_sys_ex(catastrophic, "can't load server cert file, check file "
  2432. "and run from wolfSSL home dir");
  2433. #else
  2434. /* loads cert chain file using buffer API */
  2435. load_buffer(ctx, ourCert, WOLFSSL_CERT_CHAIN);
  2436. #endif
  2437. }
  2438. #endif
  2439. #ifndef NO_DH
  2440. if (wolfSSL_CTX_SetMinDhKey_Sz(ctx, (word16)minDhKeyBits)
  2441. != WOLFSSL_SUCCESS) {
  2442. err_sys_ex(runWithErrors, "Error setting minimum DH key size");
  2443. }
  2444. #endif
  2445. #ifndef NO_RSA
  2446. if (wolfSSL_CTX_SetMinRsaKey_Sz(ctx, minRsaKeyBits) != WOLFSSL_SUCCESS){
  2447. err_sys_ex(runWithErrors, "Error setting minimum RSA key size");
  2448. }
  2449. #endif
  2450. #ifdef HAVE_ECC
  2451. if (wolfSSL_CTX_SetMinEccKey_Sz(ctx, minEccKeyBits) != WOLFSSL_SUCCESS){
  2452. err_sys_ex(runWithErrors, "Error setting minimum ECC key size");
  2453. }
  2454. #endif
  2455. #if !defined(NO_CERTS)
  2456. #ifdef HAVE_PK_CALLBACKS
  2457. pkCbInfo.ourKey = ourKey;
  2458. #endif
  2459. if ((!usePsk || usePskPlus) && !useAnon
  2460. && !(loadCertKeyIntoSSLObj == 1)
  2461. #if defined(HAVE_PK_CALLBACKS) && defined(TEST_PK_PRIVKEY)
  2462. && !pkCallbacks
  2463. #endif /* HAVE_PK_CALLBACKS && TEST_PK_PRIVKEY */
  2464. ) {
  2465. #ifdef NO_FILESYSTEM
  2466. if (wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  2467. sizeof_server_key_der_2048, SSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS)
  2468. err_sys_ex(catastrophic, "can't load server private key buffer");
  2469. #elif !defined(TEST_LOAD_BUFFER)
  2470. if (SSL_CTX_use_PrivateKey_file(ctx, ourKey, WOLFSSL_FILETYPE_PEM)
  2471. != WOLFSSL_SUCCESS)
  2472. err_sys_ex(catastrophic, "can't load server private key file, "
  2473. "check file and run from wolfSSL home dir");
  2474. #else
  2475. /* loads private key file using buffer API */
  2476. load_buffer(ctx, ourKey, WOLFSSL_KEY);
  2477. #endif
  2478. }
  2479. #endif
  2480. if (usePsk || usePskPlus) {
  2481. #ifndef NO_PSK
  2482. const char *defaultCipherList = cipherList;
  2483. SSL_CTX_set_psk_server_callback(ctx, my_psk_server_cb);
  2484. #ifdef WOLFSSL_TLS13
  2485. wolfSSL_CTX_set_psk_server_tls13_callback(ctx, my_psk_server_tls13_cb);
  2486. #endif
  2487. if (sendPskIdentityHint == 1)
  2488. SSL_CTX_use_psk_identity_hint(ctx, "cyassl server");
  2489. if (defaultCipherList == NULL && !usePskPlus) {
  2490. #if defined(HAVE_AESGCM) && !defined(NO_DH)
  2491. #ifdef WOLFSSL_TLS13
  2492. defaultCipherList = "TLS13-AES128-GCM-SHA256"
  2493. #ifndef WOLFSSL_NO_TLS12
  2494. ":DHE-PSK-AES128-GCM-SHA256"
  2495. #endif
  2496. ;
  2497. #else
  2498. defaultCipherList = "DHE-PSK-AES128-GCM-SHA256";
  2499. #endif
  2500. needDH = 1;
  2501. #elif defined(HAVE_AESGCM) && defined(WOLFSSL_TLS13)
  2502. defaultCipherList = "TLS13-AES128-GCM-SHA256"
  2503. #ifndef WOLFSSL_NO_TLS12
  2504. ":PSK-AES128-GCM-SHA256"
  2505. #endif
  2506. ;
  2507. #elif defined(HAVE_NULL_CIPHER)
  2508. defaultCipherList = "PSK-NULL-SHA256";
  2509. #elif !defined(NO_AES_CBC)
  2510. defaultCipherList = "PSK-AES128-CBC-SHA256";
  2511. #else
  2512. defaultCipherList = "PSK-AES128-GCM-SHA256";
  2513. #endif
  2514. if (SSL_CTX_set_cipher_list(ctx, defaultCipherList)
  2515. != WOLFSSL_SUCCESS)
  2516. err_sys_ex(runWithErrors, "server can't set cipher list 2");
  2517. }
  2518. wolfSSL_CTX_set_psk_callback_ctx(ctx, (void*)defaultCipherList);
  2519. #endif /* !NO_PSK */
  2520. }
  2521. #ifndef NO_CERTS
  2522. if (mutualAuth)
  2523. wolfSSL_CTX_mutual_auth(ctx, 1);
  2524. #endif
  2525. #ifdef HAVE_ECC
  2526. /* Use ECDHE key size that matches long term key.
  2527. * Zero means use ctx->privateKeySz.
  2528. * Default ECDHE_SIZE is 32 bytes
  2529. */
  2530. if (wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 0) != WOLFSSL_SUCCESS){
  2531. err_sys_ex(runWithErrors, "Error setting ECDHE size");
  2532. }
  2533. #endif
  2534. if (useAnon) {
  2535. #ifdef HAVE_ANON
  2536. wolfSSL_CTX_allow_anon_cipher(ctx);
  2537. if (cipherList == NULL || (cipherList && useDefCipherList)) {
  2538. const char* defaultCipherList;
  2539. defaultCipherList = "ADH-AES256-GCM-SHA384:"
  2540. "ADH-AES128-SHA";
  2541. if (SSL_CTX_set_cipher_list(ctx, defaultCipherList)
  2542. != WOLFSSL_SUCCESS)
  2543. err_sys_ex(runWithErrors, "server can't set cipher list 4");
  2544. }
  2545. #endif
  2546. }
  2547. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2548. /* if not using PSK, verify peer with certs
  2549. if using PSK Plus then verify peer certs except PSK suites */
  2550. if (doCliCertCheck && (usePsk == 0 || usePskPlus) && useAnon == 0) {
  2551. unsigned int verify_flags = 0;
  2552. SSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER |
  2553. (usePskPlus ? WOLFSSL_VERIFY_FAIL_EXCEPT_PSK :
  2554. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT),
  2555. (myVerifyAction == VERIFY_OVERRIDE_DATE_ERR ||
  2556. myVerifyAction == VERIFY_FORCE_FAIL) ? myVerify : NULL);
  2557. #ifdef TEST_BEFORE_DATE
  2558. verify_flags |= WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY;
  2559. #endif
  2560. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  2561. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  2562. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  2563. if (useCertFolder) {
  2564. WOLFSSL_X509_STORE *store;
  2565. WOLFSSL_X509_LOOKUP *lookup;
  2566. store = wolfSSL_CTX_get_cert_store(ctx);
  2567. if (store == NULL) {
  2568. wolfSSL_CTX_free(ctx); ctx = NULL;
  2569. err_sys("can't get WOLFSSL_X509_STORE");
  2570. }
  2571. lookup = wolfSSL_X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
  2572. if (lookup == NULL) {
  2573. wolfSSL_CTX_free(ctx); ctx = NULL;
  2574. err_sys("can't add lookup");
  2575. }
  2576. if (wolfSSL_X509_LOOKUP_ctrl(lookup, WOLFSSL_X509_L_ADD_DIR, caCertFolder,
  2577. X509_FILETYPE_PEM, NULL) != WOLFSSL_SUCCESS) {
  2578. err_sys("X509_LOOKUP_ctrl w/ L_ADD_DIR failed");
  2579. }
  2580. } else {
  2581. #endif
  2582. if (wolfSSL_CTX_load_verify_locations_ex(ctx, verifyCert, 0,
  2583. verify_flags) != WOLFSSL_SUCCESS) {
  2584. err_sys_ex(catastrophic,
  2585. "can't load ca file, Please run from wolfSSL home dir");
  2586. }
  2587. #ifdef WOLFSSL_TRUST_PEER_CERT
  2588. if (trustCert) {
  2589. if (wolfSSL_CTX_trust_peer_cert(ctx, trustCert,
  2590. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2591. err_sys_ex(runWithErrors, "can't load trusted peer cert file");
  2592. }
  2593. }
  2594. #endif /* WOLFSSL_TRUST_PEER_CERT */
  2595. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  2596. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  2597. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  2598. }
  2599. #endif
  2600. }
  2601. #endif
  2602. #ifdef WOLFSSL_SNIFFER
  2603. if (cipherList == NULL && version < 4) {
  2604. /* static RSA or static ECC cipher suites */
  2605. const char* staticCipherList = "AES128-SHA:ECDH-ECDSA-AES128-SHA";
  2606. if (SSL_CTX_set_cipher_list(ctx, staticCipherList) != WOLFSSL_SUCCESS) {
  2607. err_sys_ex(runWithErrors, "server can't set cipher list 3");
  2608. }
  2609. }
  2610. #endif
  2611. #ifdef HAVE_SNI
  2612. if (sniHostName)
  2613. if (wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, sniHostName,
  2614. (word16) XSTRLEN(sniHostName)) != WOLFSSL_SUCCESS)
  2615. err_sys_ex(runWithErrors, "UseSNI failed");
  2616. #endif
  2617. #ifdef USE_WINDOWS_API
  2618. if (port == 0) {
  2619. /* Generate random port for testing */
  2620. port = GetRandomPort();
  2621. }
  2622. #endif /* USE_WINDOWS_API */
  2623. #ifdef WOLFSSL_ASYNC_CRYPT
  2624. ret = wolfAsync_DevOpen(&devId);
  2625. if (ret < 0) {
  2626. fprintf(stderr, "Async device open failed\nRunning without async\n");
  2627. }
  2628. wolfSSL_CTX_SetDevId(ctx, devId);
  2629. #endif /* WOLFSSL_ASYNC_CRYPT */
  2630. #ifdef WOLFSSL_TLS13
  2631. if (noPskDheKe)
  2632. wolfSSL_CTX_no_dhe_psk(ctx);
  2633. #endif
  2634. #ifdef HAVE_SESSION_TICKET
  2635. #ifdef WOLFSSL_TLS13
  2636. if (noTicketTls13)
  2637. wolfSSL_CTX_no_ticket_TLSv13(ctx);
  2638. #endif
  2639. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  2640. if (noTicketTls12)
  2641. wolfSSL_CTX_NoTicketTLSv12(ctx);
  2642. #endif
  2643. #endif
  2644. while (1) {
  2645. /* allow resume option */
  2646. if (resumeCount > 1) {
  2647. if (dtlsUDP == 0) {
  2648. client_len = sizeof client_addr;
  2649. clientfd = accept(sockfd, (struct sockaddr*)&client_addr,
  2650. (ACCEPT_THIRD_T)&client_len);
  2651. }
  2652. else {
  2653. tcp_listen(&sockfd, &port, useAnyAddr, dtlsUDP, dtlsSCTP);
  2654. clientfd = sockfd;
  2655. }
  2656. if (WOLFSSL_SOCKET_IS_INVALID(clientfd)) {
  2657. err_sys_ex(runWithErrors, "tcp accept failed");
  2658. }
  2659. }
  2660. #if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL)
  2661. fprintf(stderr, "Before creating SSL\n");
  2662. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
  2663. err_sys_ex(runWithErrors, "ctx not using static memory");
  2664. if (wolfSSL_PrintStats(&mem_stats) != 1) /* function in test.h */
  2665. err_sys_ex(runWithErrors, "error printing out memory stats");
  2666. #endif
  2667. if (doMcast) {
  2668. #ifdef WOLFSSL_MULTICAST
  2669. wolfSSL_CTX_mcast_set_member_id(ctx, mcastID);
  2670. if (wolfSSL_CTX_set_cipher_list(ctx, "WDM-NULL-SHA256")
  2671. != WOLFSSL_SUCCESS)
  2672. err_sys("Couldn't set multicast cipher list.");
  2673. #endif
  2674. }
  2675. if (doDTLS && dtlsUDP) {
  2676. #if defined(WOLFSSL_DTLS) && defined(USE_WOLFSSL_IO)
  2677. if (doBlockSeq) {
  2678. wolfSSL_CTX_SetIOSend(ctx, TestEmbedSendTo);
  2679. }
  2680. #endif
  2681. }
  2682. #ifdef HAVE_PK_CALLBACKS
  2683. if (pkCallbacks)
  2684. SetupPkCallbacks(ctx);
  2685. #endif
  2686. ssl = SSL_new(ctx);
  2687. if (ssl == NULL)
  2688. err_sys_ex(catastrophic, "unable to create an SSL object");
  2689. #if defined(OPENSSL_EXTRA) || defined(HAVE_SECRET_CALLBACK)
  2690. wolfSSL_KeepArrays(ssl);
  2691. #endif
  2692. /* Support for loading private key and cert using WOLFSSL object */
  2693. #if !defined(NO_CERTS)
  2694. if ((!usePsk || usePskPlus) && !useAnon && loadCertKeyIntoSSLObj) {
  2695. #if defined(NO_FILESYSTEM) && defined(USE_CERT_BUFFERS_2048)
  2696. if (wolfSSL_use_certificate_chain_buffer_format(ssl,
  2697. server_cert_der_2048, sizeof_server_cert_der_2048,
  2698. WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS)
  2699. err_sys_ex(catastrophic, "can't load server cert buffer");
  2700. #elif !defined(TEST_LOAD_BUFFER)
  2701. if (SSL_use_certificate_chain_file(ssl, ourCert)
  2702. != WOLFSSL_SUCCESS)
  2703. err_sys_ex(catastrophic, "can't load server cert file, check file "
  2704. "and run from wolfSSL home dir");
  2705. #else
  2706. /* loads cert chain file using buffer API */
  2707. load_ssl_buffer(ssl, ourCert, WOLFSSL_CERT_CHAIN);
  2708. #endif
  2709. }
  2710. if ((!usePsk || usePskPlus) && !useAnon &&
  2711. loadCertKeyIntoSSLObj
  2712. #if defined(HAVE_PK_CALLBACKS) && defined(TEST_PK_PRIVKEY)
  2713. && !pkCallbacks
  2714. #endif /* HAVE_PK_CALLBACKS && TEST_PK_PRIVKEY */
  2715. ) {
  2716. #if defined(NO_FILESYSTEM)
  2717. if (wolfSSL_use_PrivateKey_buffer(ssl, server_key_der_2048,
  2718. sizeof_server_key_der_2048, SSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS)
  2719. err_sys_ex(catastrophic, "can't load server private key buffer");
  2720. #elif !defined(TEST_LOAD_BUFFER)
  2721. if (SSL_use_PrivateKey_file(ssl, ourKey, WOLFSSL_FILETYPE_PEM)
  2722. != WOLFSSL_SUCCESS)
  2723. err_sys_ex(catastrophic, "can't load server private key file, check"
  2724. "file and run from wolfSSL home dir");
  2725. #else
  2726. /* loads private key file using buffer API */
  2727. load_ssl_buffer(ssl, ourKey, WOLFSSL_KEY);
  2728. #endif
  2729. }
  2730. #endif /* !NO_CERTS */
  2731. #ifdef WOLFSSL_SEND_HRR_COOKIE
  2732. if (hrrCookie == 1 && wolfSSL_send_hrr_cookie(ssl, NULL, 0)
  2733. != WOLFSSL_SUCCESS) {
  2734. err_sys("unable to set use of cookie with HRR msg");
  2735. }
  2736. else if (hrrCookie == -1) {
  2737. wolfSSL_disable_hrr_cookie(ssl);
  2738. }
  2739. #endif
  2740. #if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL)
  2741. fprintf(stderr, "After creating SSL\n");
  2742. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
  2743. err_sys_ex(runWithErrors, "ctx not using static memory");
  2744. if (wolfSSL_PrintStats(&mem_stats) != 1) /* function in test.h */
  2745. err_sys_ex(runWithErrors, "error printing out memory stats");
  2746. #endif
  2747. if (doMcast) {
  2748. #ifdef WOLFSSL_MULTICAST
  2749. /* DTLS multicast secret for testing only */
  2750. #define CLI_SRV_RANDOM_SZ 32 /* RAN_LEN (see internal.h) */
  2751. #define PMS_SZ 512 /* ENCRYPT_LEN (see internal.h) */
  2752. byte pms[PMS_SZ]; /* pre master secret */
  2753. byte cr[CLI_SRV_RANDOM_SZ]; /* client random */
  2754. byte sr[CLI_SRV_RANDOM_SZ]; /* server random */
  2755. const byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  2756. XMEMSET(pms, 0x23, sizeof(pms));
  2757. XMEMSET(cr, 0xA5, sizeof(cr));
  2758. XMEMSET(sr, 0x5A, sizeof(sr));
  2759. if (wolfSSL_set_secret(ssl, 1, pms, sizeof(pms), cr, sr, suite)
  2760. != WOLFSSL_SUCCESS) {
  2761. err_sys("unable to set mcast secret");
  2762. }
  2763. #endif
  2764. }
  2765. #ifdef HAVE_SECURE_RENEGOTIATION
  2766. if (scr) {
  2767. if (wolfSSL_UseSecureRenegotiation(ssl) != WOLFSSL_SUCCESS) {
  2768. err_sys_ex(runWithErrors, "can't enable secure renegotiation");
  2769. }
  2770. }
  2771. #endif /* HAVE_SECURE_RENEGOTIATION */
  2772. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2773. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  2774. if (postHandAuth) {
  2775. unsigned int verify_flags = 0;
  2776. #ifdef TEST_BEFORE_DATE
  2777. verify_flags |= WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY;
  2778. #endif
  2779. if (wolfSSL_CTX_load_verify_locations_ex(ctx, verifyCert, 0,
  2780. verify_flags)
  2781. != WOLFSSL_SUCCESS) {
  2782. err_sys_ex(runWithErrors, "can't load ca file, Please run from "
  2783. "wolfSSL home dir");
  2784. }
  2785. #ifdef WOLFSSL_TRUST_PEER_CERT
  2786. if (trustCert) {
  2787. if (wolfSSL_trust_peer_cert(ssl, trustCert,
  2788. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  2789. err_sys_ex(runWithErrors, "can't load trusted peer cert "
  2790. "file");
  2791. }
  2792. }
  2793. #endif /* WOLFSSL_TRUST_PEER_CERT */
  2794. }
  2795. #endif
  2796. #endif
  2797. #ifndef NO_HANDSHAKE_DONE_CB
  2798. wolfSSL_SetHsDoneCb(ssl, myHsDoneCb, NULL);
  2799. #endif
  2800. #ifdef HAVE_CRL
  2801. if (!disableCRL) {
  2802. #ifdef HAVE_CRL_MONITOR
  2803. crlFlags = WOLFSSL_CRL_MONITOR | WOLFSSL_CRL_START_MON;
  2804. #endif
  2805. if (wolfSSL_EnableCRL(ssl, 0) != WOLFSSL_SUCCESS)
  2806. err_sys_ex(runWithErrors, "unable to enable CRL");
  2807. if (wolfSSL_LoadCRL(ssl, crlPemDir, WOLFSSL_FILETYPE_PEM, crlFlags)
  2808. != WOLFSSL_SUCCESS)
  2809. err_sys_ex(runWithErrors, "unable to load CRL");
  2810. if (wolfSSL_SetCRL_Cb(ssl, CRL_CallBack) != WOLFSSL_SUCCESS)
  2811. err_sys_ex(runWithErrors, "unable to set CRL callback url");
  2812. }
  2813. #endif
  2814. #ifdef HAVE_OCSP
  2815. if (useOcsp) {
  2816. if (ocspUrl != NULL) {
  2817. wolfSSL_CTX_SetOCSP_OverrideURL(ctx, ocspUrl);
  2818. wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE
  2819. | WOLFSSL_OCSP_URL_OVERRIDE);
  2820. }
  2821. else
  2822. wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE);
  2823. }
  2824. #ifndef NO_RSA
  2825. /* All the OCSP Stapling test certs are RSA. */
  2826. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  2827. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2828. { /* scope start */
  2829. const char* ca1 = "certs/ocsp/intermediate1-ca-cert.pem";
  2830. const char* ca2 = "certs/ocsp/intermediate2-ca-cert.pem";
  2831. const char* ca3 = "certs/ocsp/intermediate3-ca-cert.pem";
  2832. int fails = 0;
  2833. if (wolfSSL_CTX_EnableOCSPStapling(ctx) != WOLFSSL_SUCCESS) {
  2834. err_sys_ex(catastrophic, "can't enable OCSP Stapling "
  2835. "Certificate Manager");
  2836. }
  2837. if (SSL_CTX_load_verify_locations(ctx, ca1, 0) != WOLFSSL_SUCCESS) {
  2838. fails++;
  2839. err_sys_ex(runWithErrors, "can't load ca file, Please run from "
  2840. "wolfSSL home dir");
  2841. }
  2842. if (SSL_CTX_load_verify_locations(ctx, ca2, 0) != WOLFSSL_SUCCESS) {
  2843. fails++;
  2844. err_sys_ex(runWithErrors, "can't load ca file, Please run from "
  2845. "wolfSSL home dir");
  2846. }
  2847. if (SSL_CTX_load_verify_locations(ctx, ca3, 0) != WOLFSSL_SUCCESS) {
  2848. fails++;
  2849. err_sys_ex(runWithErrors, "can't load ca file, Please run from "
  2850. "wolfSSL home dir");
  2851. }
  2852. if (fails > 2) {
  2853. err_sys_ex(catastrophic, "Failed to load any intermediates for "
  2854. "OCSP stapling test");
  2855. }
  2856. } /* scope end */
  2857. #endif /* HAVE_CERTIFICATE_STATUS_REQUEST HAVE_CERTIFICATE_STATUS_REQUEST_V2 */
  2858. #endif /* NO_RSA */
  2859. #endif /* HAVE_OCSP */
  2860. #ifdef HAVE_PK_CALLBACKS
  2861. /* This must be before SetKeyShare */
  2862. if (pkCallbacks) {
  2863. SetupPkCallbackContexts(ssl, &pkCbInfo);
  2864. }
  2865. #endif
  2866. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  2867. if (version >= 4 || version == -4) {
  2868. #ifdef CAN_FORCE_CURVE
  2869. if (force_curve_group_id > 0) {
  2870. do {
  2871. ret = wolfSSL_UseKeyShare(ssl, force_curve_group_id);
  2872. if (ret == WOLFSSL_SUCCESS) {
  2873. }
  2874. #ifdef WOLFSSL_ASYNC_CRYPT
  2875. else if (ret == WC_PENDING_E) {
  2876. wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  2877. }
  2878. #endif
  2879. else {
  2880. err_sys("Failed wolfSSL_UseKeyShare in force-curve");
  2881. }
  2882. } while (ret == WC_PENDING_E);
  2883. ret = wolfSSL_set_groups(ssl, &force_curve_group_id, 1);
  2884. if (WOLFSSL_SUCCESS != ret) {
  2885. err_sys("Failed wolfSSL_set_groups in force-curve");
  2886. }
  2887. }
  2888. else
  2889. #endif
  2890. SetKeyShare(ssl, onlyKeyShare, useX25519, useX448, usePqc,
  2891. pqcAlg);
  2892. }
  2893. #endif
  2894. #ifdef HAVE_ENCRYPT_THEN_MAC
  2895. if (disallowETM)
  2896. wolfSSL_AllowEncryptThenMac(ssl, 0);
  2897. #endif
  2898. /* do accept */
  2899. readySignal = ((func_args*)args)->signal;
  2900. if (readySignal) {
  2901. readySignal->srfName = serverReadyFile;
  2902. }
  2903. client_len = sizeof client_addr;
  2904. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, useAnyAddr,
  2905. dtlsUDP, dtlsSCTP, serverReadyFile ? 1 : 0, doListen,
  2906. &client_addr, &client_len);
  2907. doListen = 0; /* Don't listen next time */
  2908. if (port == 0) {
  2909. port = readySignal->port;
  2910. }
  2911. if (SSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  2912. err_sys_ex(catastrophic, "error in setting fd");
  2913. }
  2914. #ifdef HAVE_TRUSTED_CA
  2915. if (trustedCaKeyId) {
  2916. if (wolfSSL_UseTrustedCA(ssl, WOLFSSL_TRUSTED_CA_PRE_AGREED,
  2917. NULL, 0) != WOLFSSL_SUCCESS) {
  2918. err_sys_ex(runWithErrors, "UseTrustedCA failed");
  2919. }
  2920. }
  2921. #endif /* HAVE_TRUSTED_CA */
  2922. #ifdef HAVE_ALPN
  2923. if (alpnList != NULL) {
  2924. printf("ALPN accepted protocols list : %s\n", alpnList);
  2925. wolfSSL_UseALPN(ssl, alpnList, (word32)XSTRLEN(alpnList), alpn_opt);
  2926. }
  2927. #endif
  2928. #if defined(WOLFSSL_DTLS) && defined(USE_WOLFSSL_IO)
  2929. if (doDTLS && dtlsUDP) {
  2930. byte b[1500];
  2931. int n;
  2932. int isClientHello = 0;
  2933. while (!isClientHello) {
  2934. client_len = sizeof client_addr;
  2935. /* For DTLS, peek at the next datagram so we can get the
  2936. * client's address and set it into the ssl object later to
  2937. * generate the cookie. */
  2938. n = (int)recvfrom(clientfd, (char*)b, sizeof(b), MSG_PEEK,
  2939. (struct sockaddr*)&client_addr, &client_len);
  2940. if (n <= 0)
  2941. err_sys_ex(runWithErrors, "recvfrom failed");
  2942. /* when doing resumption, it may happen that we receive the
  2943. alert used to shutdown the first connection as the first
  2944. packet of the second accept:
  2945. Client | Server
  2946. | WolfSSL_Shutdown()
  2947. | <- Alert
  2948. | recvfrom(peek)
  2949. WolfSSL_Shutdown() |
  2950. Alert-> |
  2951. | wolfSSL_set_dtls_peer()
  2952. but this will set the wrong src port, making the test fail.
  2953. Discard not-handshake message to avoid this.
  2954. */
  2955. if (b[0] != 0x16) {
  2956. /* discard the packet */
  2957. n = (int)recvfrom(clientfd, (char *)b, sizeof(b), 0,
  2958. (struct sockaddr *)&client_addr, &client_len);
  2959. if (n <= 0)
  2960. err_sys_ex(runWithErrors, "recvfrom failed");
  2961. }
  2962. else {
  2963. isClientHello = 1;
  2964. }
  2965. }
  2966. if (doBlockSeq) {
  2967. XMEMCPY(&dtlsCtx.peer.sa, &client_addr, client_len);
  2968. dtlsCtx.peer.sz = client_len;
  2969. dtlsCtx.wfd = clientfd;
  2970. dtlsCtx.failOnce = 1;
  2971. wolfSSL_SetIOWriteCtx(ssl, &dtlsCtx);
  2972. }
  2973. else {
  2974. wolfSSL_dtls_set_peer(ssl, &client_addr, client_len);
  2975. }
  2976. if (simulateWantWrite) {
  2977. #ifdef USE_WOLFSSL_IO
  2978. /* connect on a udp to associate peer with this fd to make it
  2979. * simpler for SimulateWantWriteIOSendCb */
  2980. if (connect(clientfd, (struct sockaddr*)&client_addr,
  2981. client_len) != 0) {
  2982. err_sys_ex(catastrophic, "error in connecting to peer");
  2983. }
  2984. wolfSSL_SetIOWriteCtx(ssl, (void*)&sockfd);
  2985. #endif
  2986. }
  2987. }
  2988. #endif
  2989. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  2990. {
  2991. SOCKADDR_IN_T local_addr;
  2992. socklen_t local_len = sizeof(local_addr);
  2993. getsockname(clientfd, (struct sockaddr *)&local_addr,
  2994. (socklen_t *)&local_len);
  2995. if (((struct sockaddr *)&client_addr)->sa_family !=
  2996. ((struct sockaddr *)&local_addr)->sa_family)
  2997. err_sys_ex(catastrophic,
  2998. "client_addr.sa_family != local_addr.sa_family");
  2999. if (wolfsentry_store_endpoints(
  3000. ssl, &client_addr, &local_addr,
  3001. dtlsUDP ? IPPROTO_UDP : IPPROTO_TCP,
  3002. WOLFSENTRY_ROUTE_FLAG_DIRECTION_IN, NULL) != WOLFSSL_SUCCESS)
  3003. err_sys_ex(catastrophic,
  3004. "error in wolfsentry_store_endpoints()");
  3005. }
  3006. #endif /* WOLFSSL_WOLFSENTRY_HOOKS */
  3007. if ((usePsk == 0 || usePskPlus) || useAnon == 1 || cipherList != NULL
  3008. || needDH == 1) {
  3009. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && !defined(NO_ASN)
  3010. wolfSSL_SetTmpDH_file(ssl, ourDhParam, WOLFSSL_FILETYPE_PEM);
  3011. #elif !defined(NO_DH)
  3012. SetDH(ssl); /* repick suites with DHE, higher priority than
  3013. * PSK */
  3014. #endif
  3015. #if !defined(NO_DH) && !defined(WOLFSSL_OLD_PRIME_CHECK) && \
  3016. !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  3017. if (!doDhKeyCheck)
  3018. wolfSSL_SetEnableDhKeyTest(ssl, 0);
  3019. #endif
  3020. }
  3021. #ifdef WOLFSSL_DTLS_CID
  3022. if (useDtlsCID) {
  3023. ret = wolfSSL_dtls_cid_use(ssl);
  3024. if (ret != WOLFSSL_SUCCESS)
  3025. err_sys("Can't enable DTLS ConnectionID");
  3026. ret = wolfSSL_dtls_cid_set(ssl, (byte*)dtlsCID,
  3027. (word32)XSTRLEN(dtlsCID));
  3028. if (ret != WOLFSSL_SUCCESS)
  3029. err_sys("Can't set DTLS ConnectionID");
  3030. }
  3031. #endif /* WOLFSSL_DTLS_CID */
  3032. #ifndef WOLFSSL_CALLBACKS
  3033. if (nonBlocking) {
  3034. #ifdef WOLFSSL_DTLS
  3035. if (doDTLS) {
  3036. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  3037. }
  3038. #endif
  3039. tcp_set_nonblocking(&clientfd);
  3040. ret = NonBlockingSSL_Accept(ssl);
  3041. }
  3042. else {
  3043. #ifdef WOLFSSL_EARLY_DATA
  3044. if (earlyData) {
  3045. do {
  3046. int len;
  3047. err = 0; /* reset error */
  3048. ret = wolfSSL_read_early_data(ssl, input, sizeof(input)-1,
  3049. &len);
  3050. if (ret != WOLFSSL_SUCCESS) {
  3051. err = SSL_get_error(ssl, 0);
  3052. #ifdef WOLFSSL_ASYNC_CRYPT
  3053. if (err == WC_PENDING_E) {
  3054. /* returns the number of polled items or <0 for
  3055. * error */
  3056. ret = wolfSSL_AsyncPoll(ssl,
  3057. WOLF_POLL_FLAG_CHECK_HW);
  3058. if (ret < 0) break;
  3059. }
  3060. #endif
  3061. }
  3062. else if (ret > 0) {
  3063. input[ret] = 0; /* null terminate message */
  3064. printf("Early Data Client message: %s\n", input);
  3065. }
  3066. } while (err == WC_PENDING_E || ret > 0);
  3067. }
  3068. #endif
  3069. do {
  3070. err = 0; /* reset error */
  3071. ret = SSL_accept(ssl);
  3072. if (ret != WOLFSSL_SUCCESS) {
  3073. err = SSL_get_error(ssl, 0);
  3074. #ifdef WOLFSSL_ASYNC_CRYPT
  3075. if (err == WC_PENDING_E) {
  3076. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3077. if (ret < 0) break;
  3078. }
  3079. #endif
  3080. }
  3081. } while (err == WC_PENDING_E);
  3082. }
  3083. #else
  3084. ret = NonBlockingSSL_Accept(ssl);
  3085. #endif
  3086. #ifdef WOLFSSL_EARLY_DATA
  3087. EarlyDataStatus(ssl);
  3088. #endif
  3089. if (ret != WOLFSSL_SUCCESS) {
  3090. err = SSL_get_error(ssl, 0);
  3091. fprintf(stderr, "SSL_accept error %d, %s\n", err,
  3092. ERR_error_string(err, buffer));
  3093. if (!exitWithRet) {
  3094. err_sys_ex(runWithErrors, "SSL_accept failed");
  3095. } else {
  3096. /* cleanup */
  3097. SSL_free(ssl); ssl = NULL;
  3098. SSL_CTX_free(ctx); ctx = NULL;
  3099. CloseSocket(clientfd);
  3100. CloseSocket(sockfd);
  3101. ((func_args*)args)->return_code = err;
  3102. goto exit;
  3103. }
  3104. }
  3105. showPeerEx(ssl, lng_index);
  3106. if (SSL_state(ssl) != 0) {
  3107. err_sys_ex(runWithErrors, "SSL in error state");
  3108. }
  3109. /* if the caller requested a particular cipher, check here that either
  3110. * a canonical name of the established cipher matches the requested
  3111. * cipher name, or the requested cipher name is marked as an alias
  3112. * that matches the established cipher.
  3113. */
  3114. if (cipherList && !useDefCipherList && (! XSTRSTR(cipherList, ":"))) {
  3115. WOLFSSL_CIPHER* established_cipher = wolfSSL_get_current_cipher(ssl);
  3116. byte requested_cipherSuite0, requested_cipherSuite;
  3117. int requested_cipherFlags;
  3118. if (established_cipher &&
  3119. /* don't test for pseudo-ciphers like "ALL" and "DEFAULT". */
  3120. (wolfSSL_get_cipher_suite_from_name(cipherList,
  3121. &requested_cipherSuite0,
  3122. &requested_cipherSuite,
  3123. &requested_cipherFlags) == 0)) {
  3124. word32 established_cipher_id = wolfSSL_CIPHER_get_id(established_cipher);
  3125. byte established_cipherSuite0 = (established_cipher_id >> 8) & 0xff;
  3126. byte established_cipherSuite = established_cipher_id & 0xff;
  3127. const char *established_cipher_name =
  3128. wolfSSL_get_cipher_name_from_suite(established_cipherSuite0,
  3129. established_cipherSuite);
  3130. const char *established_cipher_name_iana =
  3131. wolfSSL_get_cipher_name_iana_from_suite(established_cipherSuite0,
  3132. established_cipherSuite);
  3133. if (established_cipher_name == NULL)
  3134. err_sys_ex(catastrophic, "error looking up name of established cipher");
  3135. if (strcmp(cipherList, established_cipher_name) &&
  3136. ((established_cipher_name_iana == NULL) ||
  3137. strcmp(cipherList, established_cipher_name_iana))) {
  3138. if (! (requested_cipherFlags & WOLFSSL_CIPHER_SUITE_FLAG_NAMEALIAS))
  3139. err_sys_ex(
  3140. catastrophic,
  3141. "Unexpected mismatch between names of requested and established ciphers.");
  3142. else if ((requested_cipherSuite0 != established_cipherSuite0) ||
  3143. (requested_cipherSuite != established_cipherSuite))
  3144. err_sys_ex(
  3145. catastrophic,
  3146. "Mismatch between IDs of requested and established ciphers.");
  3147. }
  3148. }
  3149. }
  3150. #if defined(OPENSSL_EXTRA) || defined(HAVE_SECRET_CALLBACK)
  3151. {
  3152. byte* rnd = NULL;
  3153. byte* pt;
  3154. size_t size;
  3155. /* get size of buffer then print */
  3156. size = wolfSSL_get_server_random(NULL, NULL, 0);
  3157. if (size == 0) {
  3158. err_sys_ex(runWithErrors, "error getting server random buffer "
  3159. "size");
  3160. }
  3161. else {
  3162. rnd = (byte*)XMALLOC(size, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3163. }
  3164. if (rnd == NULL) {
  3165. err_sys_ex(runWithErrors, "error creating server random buffer");
  3166. }
  3167. size = wolfSSL_get_server_random(ssl, rnd, size);
  3168. if (size == 0) {
  3169. if (rnd) {
  3170. XFREE(rnd, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3171. rnd = NULL;
  3172. }
  3173. err_sys_ex(runWithErrors, "error getting server random buffer");
  3174. }
  3175. if (rnd) {
  3176. printf("Server Random : ");
  3177. for (pt = rnd; pt < rnd + size; pt++) printf("%02X", *pt);
  3178. printf("\n");
  3179. XFREE(rnd, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3180. rnd = NULL;
  3181. }
  3182. }
  3183. #endif
  3184. #ifdef WOLFSSL_SRTP
  3185. if (dtlsSrtpProfiles != NULL) {
  3186. err = server_srtp_test(ssl, (func_args*)args);
  3187. if (err != 0) {
  3188. if (exitWithRet) {
  3189. ((func_args*)args)->return_code = err;
  3190. wolfSSL_free(ssl); ssl = NULL;
  3191. wolfSSL_CTX_free(ctx); ctx = NULL;
  3192. goto exit;
  3193. }
  3194. /* else */
  3195. err_sys("SRTP check failed");
  3196. }
  3197. }
  3198. #endif /* WOLFSSL_SRTP */
  3199. #ifdef WOLFSSL_DTLS_CID
  3200. if (useDtlsCID && wolfSSL_dtls_cid_is_enabled(ssl)) {
  3201. byte receivedCID[DTLS_CID_BUFFER_SIZE];
  3202. unsigned int receivedCIDSz;
  3203. printf("CID extension was negotiated\n");
  3204. ret = wolfSSL_dtls_cid_get_tx_size(ssl, &receivedCIDSz);
  3205. if (ret != WOLFSSL_SUCCESS)
  3206. err_sys("Can't get negotiated DTLS CID size\n");
  3207. if (receivedCIDSz > 0) {
  3208. ret = wolfSSL_dtls_cid_get_tx(ssl, receivedCID,
  3209. DTLS_CID_BUFFER_SIZE - 1);
  3210. if (ret != WOLFSSL_SUCCESS)
  3211. err_sys("Can't get negotiated DTLS CID\n");
  3212. printf("Sending CID is ");
  3213. printBuffer(receivedCID, receivedCIDSz);
  3214. printf("\n");
  3215. }
  3216. else {
  3217. printf("other peer provided empty CID\n");
  3218. }
  3219. }
  3220. #endif
  3221. #ifdef HAVE_ALPN
  3222. if (alpnList != NULL) {
  3223. char *protocol_name = NULL, *list = NULL;
  3224. word16 protocol_nameSz = 0, listSz = 0;
  3225. err = wolfSSL_ALPN_GetProtocol(ssl, &protocol_name,
  3226. &protocol_nameSz);
  3227. if (err == WOLFSSL_SUCCESS)
  3228. printf("Sent ALPN protocol : %s (%d)\n",
  3229. protocol_name, protocol_nameSz);
  3230. else if (err == WOLFSSL_ALPN_NOT_FOUND)
  3231. printf("No ALPN response sent (no match)\n");
  3232. else
  3233. printf("Getting ALPN protocol name failed\n");
  3234. err = wolfSSL_ALPN_GetPeerProtocol(ssl, &list, &listSz);
  3235. if (err == WOLFSSL_SUCCESS)
  3236. printf("List of protocol names sent by Client: %s (%d)\n",
  3237. list, listSz);
  3238. else
  3239. printf("Get list of client's protocol name failed\n");
  3240. free(list);
  3241. }
  3242. #endif
  3243. if (echoData == 0 && throughput == 0) {
  3244. ServerRead(ssl, input, sizeof(input)-1);
  3245. err = SSL_get_error(ssl, 0);
  3246. }
  3247. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  3248. defined(HAVE_SERVER_RENEGOTIATION_INFO)
  3249. if (scr && forceScr) {
  3250. if (nonBlocking) {
  3251. if (wolfSSL_Rehandshake(ssl) != WOLFSSL_SUCCESS) {
  3252. err = wolfSSL_get_error(ssl, 0);
  3253. if (err == WOLFSSL_ERROR_WANT_READ ||
  3254. err == WOLFSSL_ERROR_WANT_WRITE) {
  3255. do {
  3256. #ifdef WOLFSSL_ASYNC_CRYPT
  3257. if (err == WC_PENDING_E) {
  3258. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3259. if (ret < 0) break;
  3260. }
  3261. #endif
  3262. if (err == APP_DATA_READY) {
  3263. if (wolfSSL_read(ssl, input, sizeof(input)-1) < 0) {
  3264. err_sys("APP DATA should be present but error returned");
  3265. }
  3266. printf("Received message: %s\n", input);
  3267. }
  3268. err = 0;
  3269. if ((ret = wolfSSL_accept(ssl)) != WOLFSSL_SUCCESS) {
  3270. err = wolfSSL_get_error(ssl, ret);
  3271. }
  3272. } while (ret != WOLFSSL_SUCCESS &&
  3273. (err == WOLFSSL_ERROR_WANT_READ ||
  3274. err == WOLFSSL_ERROR_WANT_WRITE ||
  3275. err == APP_DATA_READY ||
  3276. err == WC_PENDING_E));
  3277. if (ret == WOLFSSL_SUCCESS) {
  3278. printf("NON-BLOCKING RENEGOTIATION SUCCESSFUL\n");
  3279. err = 0;
  3280. }
  3281. }
  3282. if (ret != WOLFSSL_SUCCESS) {
  3283. err = wolfSSL_get_error(ssl, 0);
  3284. fprintf(stderr,
  3285. "wolfSSL_Rehandshake error %d, %s\n", err,
  3286. wolfSSL_ERR_error_string(err, buffer));
  3287. wolfSSL_free(ssl); ssl = NULL;
  3288. wolfSSL_CTX_free(ctx); ctx = NULL;
  3289. err_sys("non-blocking wolfSSL_Rehandshake failed");
  3290. }
  3291. }
  3292. } else {
  3293. if (wolfSSL_Rehandshake(ssl) != WOLFSSL_SUCCESS) {
  3294. #ifdef WOLFSSL_ASYNC_CRYPT
  3295. err = wolfSSL_get_error(ssl, 0);
  3296. while (err == WC_PENDING_E) {
  3297. err = 0;
  3298. ret = wolfSSL_negotiate(ssl);
  3299. if (ret != WOLFSSL_SUCCESS) {
  3300. err = wolfSSL_get_error(ssl, 0);
  3301. if (err == WC_PENDING_E) {
  3302. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3303. if (ret < 0) break;
  3304. }
  3305. }
  3306. }
  3307. if (ret != WOLFSSL_SUCCESS)
  3308. #endif
  3309. printf("not doing secure renegotiation\n");
  3310. }
  3311. else {
  3312. printf("RENEGOTIATION SUCCESSFUL\n");
  3313. }
  3314. }
  3315. }
  3316. #endif /* HAVE_SECURE_RENEGOTIATION */
  3317. if (err == 0 && echoData == 0 && throughput == 0) {
  3318. const char* write_msg;
  3319. int write_msg_sz;
  3320. #ifdef WOLFSSL_TLS13
  3321. if (updateKeysIVs)
  3322. wolfSSL_update_keys(ssl);
  3323. #endif
  3324. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  3325. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  3326. if (postHandAuth) {
  3327. SSL_set_verify(ssl, WOLFSSL_VERIFY_PEER |
  3328. ((usePskPlus) ? WOLFSSL_VERIFY_FAIL_EXCEPT_PSK :
  3329. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT), 0);
  3330. wolfSSL_request_certificate(ssl);
  3331. }
  3332. #endif
  3333. #endif
  3334. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)
  3335. if (sendTicket) {
  3336. if (wolfSSL_send_SessionTicket(ssl) != WOLFSSL_SUCCESS) {
  3337. fprintf(stderr, "Sending new session ticket failed\n");
  3338. }
  3339. else {
  3340. fprintf(stderr, "New session ticket sent\n");
  3341. }
  3342. }
  3343. #endif
  3344. /* Write data */
  3345. if (!useWebServerMsg) {
  3346. write_msg = kReplyMsg;
  3347. write_msg_sz = (int)XSTRLEN(kReplyMsg);
  3348. }
  3349. else {
  3350. write_msg = kHttpServerMsg;
  3351. write_msg_sz = (int)XSTRLEN(kHttpServerMsg);
  3352. }
  3353. ServerWrite(ssl, write_msg, write_msg_sz);
  3354. #ifdef WOLFSSL_TLS13
  3355. if (updateKeysIVs || postHandAuth)
  3356. ServerRead(ssl, input, sizeof(input)-1);
  3357. #endif
  3358. }
  3359. else if (err == 0 || err == WOLFSSL_ERROR_ZERO_RETURN) {
  3360. err = ServerEchoData(ssl, clientfd, echoData, block, throughput);
  3361. /* Got close notify. Ignore it if not expecting a failure. */
  3362. if (err == WOLFSSL_ERROR_ZERO_RETURN && exitWithRet == 0)
  3363. err = 0;
  3364. if (err != 0) {
  3365. SSL_free(ssl); ssl = NULL;
  3366. SSL_CTX_free(ctx); ctx = NULL;
  3367. CloseSocket(clientfd);
  3368. CloseSocket(sockfd);
  3369. ((func_args*)args)->return_code = err;
  3370. goto exit;
  3371. }
  3372. }
  3373. #if defined(WOLFSSL_MDK_SHELL) && defined(HAVE_MDK_RTX)
  3374. os_dly_wait(500) ;
  3375. #elif defined (WOLFSSL_TIRTOS)
  3376. Task_yield();
  3377. #endif
  3378. #if defined(WOLFSSL_DTLS13)
  3379. if (wolfSSL_dtls(ssl) && version == -4) {
  3380. int zero_return = 0;
  3381. while (wolfSSL_dtls13_has_pending_msg(ssl)) {
  3382. err =
  3383. process_handshake_messages(ssl, !nonBlocking, &zero_return);
  3384. if (err < 0) {
  3385. /* other peer closes the connection, non fatal */
  3386. if (zero_return)
  3387. break;
  3388. err_sys("Error while processing pending DTLSv1.3 messages");
  3389. }
  3390. }
  3391. }
  3392. #endif /* WOLFSSL_DTLS13 */
  3393. ret = SSL_shutdown(ssl);
  3394. if (wc_shutdown && ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  3395. while (tcp_select(wolfSSL_get_fd(ssl), DEFAULT_TIMEOUT_SEC) ==
  3396. TEST_RECV_READY) {
  3397. ret = wolfSSL_shutdown(ssl); /* bidirectional shutdown */
  3398. if (ret == WOLFSSL_SUCCESS) {
  3399. printf("Bidirectional shutdown complete\n");
  3400. break;
  3401. }
  3402. else if (ret != WOLFSSL_SHUTDOWN_NOT_DONE) {
  3403. fprintf(stderr, "Bidirectional shutdown failed\n");
  3404. break;
  3405. }
  3406. }
  3407. if (ret != WOLFSSL_SUCCESS)
  3408. fprintf(stderr, "Bidirectional shutdown failed\n");
  3409. }
  3410. /* display collected statistics */
  3411. #ifdef WOLFSSL_STATIC_MEMORY
  3412. if (wolfSSL_is_static_memory(ssl, &ssl_stats) != 1)
  3413. err_sys_ex(runWithErrors, "static memory was not used with ssl");
  3414. fprintf(stderr, "\nprint off SSL memory stats\n");
  3415. fprintf(stderr, "*** This is memory state before wolfSSL_free is "
  3416. "called\n");
  3417. wolfSSL_PrintStatsConn(&ssl_stats);
  3418. #endif
  3419. SSL_free(ssl); ssl = NULL;
  3420. CloseSocket(clientfd);
  3421. if (resume == 1 && resumeCount == 0) {
  3422. resumeCount++; /* only do one resume for testing */
  3423. continue;
  3424. }
  3425. resumeCount = 0;
  3426. cnt++;
  3427. if (loops > 0 && --loops == 0) {
  3428. break; /* out of while loop, done with normal and resume option */
  3429. }
  3430. } /* while(1) */
  3431. WOLFSSL_TIME(cnt);
  3432. (void)cnt;
  3433. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  3434. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  3435. wolfSSL_CTX_DisableOCSPStapling(ctx);
  3436. #endif
  3437. CloseSocket(sockfd);
  3438. SSL_CTX_free(ctx); ctx = NULL;
  3439. ((func_args*)args)->return_code = 0;
  3440. exit:
  3441. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  3442. wolfsentry_ret = wolfsentry_shutdown(&wolfsentry);
  3443. if (wolfsentry_ret < 0) {
  3444. fprintf(stderr,
  3445. "wolfsentry_shutdown() returned " WOLFSENTRY_ERROR_FMT "\n",
  3446. WOLFSENTRY_ERROR_FMT_ARGS(wolfsentry_ret));
  3447. }
  3448. #endif
  3449. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  3450. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  3451. wc_ecc_fp_free(); /* free per thread cache */
  3452. #endif
  3453. #ifdef WOLFSSL_TIRTOS
  3454. fdCloseSession(Task_self());
  3455. #endif
  3456. #ifdef HAVE_TEST_SESSION_TICKET
  3457. TicketCleanup();
  3458. #endif
  3459. #ifdef WOLFSSL_ASYNC_CRYPT
  3460. wolfAsync_DevClose(&devId);
  3461. #endif
  3462. /* There are use cases when these assignments are not read. To avoid
  3463. * potential confusion those warnings have been handled here.
  3464. */
  3465. (void) ourKey;
  3466. (void) verifyCert;
  3467. (void) doCliCertCheck;
  3468. (void) ourDhParam;
  3469. (void) ourCert;
  3470. (void) useX25519;
  3471. (void) useX448;
  3472. #ifdef HAVE_SECURE_RENEGOTIATION
  3473. (void) forceScr;
  3474. #endif
  3475. #ifdef WOLFSSL_CALLBACKS
  3476. (void) earlyData;
  3477. #endif
  3478. #ifndef WOLFSSL_TIRTOS
  3479. return 0;
  3480. #endif
  3481. }
  3482. #endif /* !NO_WOLFSSL_SERVER */
  3483. /* so overall tests can pull in test function */
  3484. #ifndef NO_MAIN_DRIVER
  3485. int main(int argc, char** argv)
  3486. {
  3487. func_args args;
  3488. tcp_ready ready;
  3489. StartTCP();
  3490. args.argc = argc;
  3491. args.argv = argv;
  3492. args.signal = &ready;
  3493. args.return_code = 0;
  3494. #if defined(WOLFSSL_SRTP) && !defined(SINGLE_THREADED) && defined(_POSIX_THREADS)
  3495. args.srtp_helper = NULL;
  3496. #endif
  3497. InitTcpReady(&ready);
  3498. #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_MDK_SHELL)
  3499. wolfSSL_Debugging_ON();
  3500. #endif
  3501. wolfSSL_Init();
  3502. #ifdef WC_RNG_SEED_CB
  3503. wc_SetSeed_Cb(wc_GenerateSeed);
  3504. #endif
  3505. ChangeToWolfRoot();
  3506. #ifndef NO_WOLFSSL_SERVER
  3507. #ifdef HAVE_STACK_SIZE
  3508. StackSizeCheck(&args, server_test);
  3509. #else
  3510. server_test(&args);
  3511. #endif
  3512. #else
  3513. fprintf(stderr, "Server not compiled in!\n");
  3514. #endif
  3515. wolfSSL_Cleanup();
  3516. FreeTcpReady(&ready);
  3517. #ifdef HAVE_WNR
  3518. if (wc_FreeNetRandom() < 0)
  3519. err_sys_ex(runWithErrors, "Failed to free netRandom context");
  3520. #endif /* HAVE_WNR */
  3521. return args.return_code;
  3522. }
  3523. int myoptind = 0;
  3524. char* myoptarg = NULL;
  3525. #endif /* NO_MAIN_DRIVER */