server.c 134 KB

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