server.c 134 KB

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