1
0

server.c 138 KB

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