server.c 130 KB

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