test.h 163 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505
  1. /* test.h
  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. /*!
  22. \file ../wolfssl/test.h
  23. \brief Header file containing test inline functions
  24. */
  25. /* Testing functions */
  26. #ifndef wolfSSL_TEST_H
  27. #define wolfSSL_TEST_H
  28. #include <wolfssl/wolfcrypt/settings.h>
  29. #include <wolfssl/wolfcrypt/wc_port.h>
  30. #ifdef FUSION_RTOS
  31. #include <fclstdio.h>
  32. #include <fclstdlib.h>
  33. #else
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #endif
  37. #include <assert.h>
  38. #include <ctype.h>
  39. #ifdef HAVE_ERRNO_H
  40. #include <errno.h>
  41. #endif
  42. #include <wolfssl/wolfcrypt/types.h>
  43. #include <wolfssl/error-ssl.h>
  44. #include <wolfssl/wolfcrypt/random.h>
  45. #include <wolfssl/wolfcrypt/mem_track.h>
  46. #include <wolfssl/wolfio.h>
  47. #include <wolfssl/wolfcrypt/asn.h>
  48. #ifdef ATOMIC_USER
  49. #include <wolfssl/wolfcrypt/aes.h>
  50. #include <wolfssl/wolfcrypt/arc4.h>
  51. #include <wolfssl/wolfcrypt/hmac.h>
  52. #endif
  53. #ifdef HAVE_PK_CALLBACKS
  54. #ifndef NO_RSA
  55. #include <wolfssl/wolfcrypt/rsa.h>
  56. #endif
  57. #ifdef HAVE_ECC
  58. #include <wolfssl/wolfcrypt/ecc.h>
  59. #endif /* HAVE_ECC */
  60. #ifndef NO_DH
  61. #include <wolfssl/wolfcrypt/dh.h>
  62. #endif /* !NO_DH */
  63. #ifdef HAVE_ED25519
  64. #include <wolfssl/wolfcrypt/ed25519.h>
  65. #endif /* HAVE_ED25519 */
  66. #ifdef HAVE_CURVE25519
  67. #include <wolfssl/wolfcrypt/curve25519.h>
  68. #endif /* HAVE_ECC */
  69. #ifdef HAVE_ED448
  70. #include <wolfssl/wolfcrypt/ed448.h>
  71. #endif /* HAVE_ED448 */
  72. #ifdef HAVE_CURVE448
  73. #include <wolfssl/wolfcrypt/curve448.h>
  74. #endif /* HAVE_ECC */
  75. #endif /*HAVE_PK_CALLBACKS */
  76. #ifdef USE_WINDOWS_API
  77. #include <winsock2.h>
  78. #include <process.h>
  79. #ifdef TEST_IPV6 /* don't require newer SDK for IPV4 */
  80. #include <ws2tcpip.h>
  81. #include <wspiapi.h>
  82. #endif
  83. #define SOCKET_T SOCKET
  84. #define SNPRINTF _snprintf
  85. #define XSLEEP_MS(t) Sleep(t)
  86. #elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
  87. #include <string.h>
  88. #include "rl_net.h"
  89. #define SOCKET_T int
  90. typedef int socklen_t ;
  91. #define inet_addr wolfSSL_inet_addr
  92. static unsigned long wolfSSL_inet_addr(const char *cp)
  93. {
  94. unsigned int a[4] ; unsigned long ret ;
  95. sscanf(cp, "%u.%u.%u.%u", &a[0], &a[1], &a[2], &a[3]) ;
  96. ret = ((a[3]<<24) + (a[2]<<16) + (a[1]<<8) + a[0]) ;
  97. return(ret) ;
  98. }
  99. #if defined(HAVE_KEIL_RTX)
  100. #define XSLEEP_MS(t) os_dly_wait(t)
  101. #elif defined(WOLFSSL_CMSIS_RTOS) || defined(WOLFSSL_CMSIS_RTOSv2)
  102. #define XSLEEP_MS(t) osDelay(t)
  103. #endif
  104. #elif defined(WOLFSSL_TIRTOS)
  105. #include <string.h>
  106. #include <netdb.h>
  107. #include <sys/types.h>
  108. #include <arpa/inet.h>
  109. #include <sys/socket.h>
  110. #include <ti/sysbios/knl/Task.h>
  111. struct hostent {
  112. char *h_name; /* official name of host */
  113. char **h_aliases; /* alias list */
  114. int h_addrtype; /* host address type */
  115. int h_length; /* length of address */
  116. char **h_addr_list; /* list of addresses from name server */
  117. };
  118. #define SOCKET_T int
  119. #define XSLEEP_MS(t) Task_sleep(t/1000)
  120. #elif defined(WOLFSSL_VXWORKS)
  121. #include <hostLib.h>
  122. #include <sockLib.h>
  123. #include <arpa/inet.h>
  124. #include <string.h>
  125. #include <selectLib.h>
  126. #include <sys/types.h>
  127. #include <netinet/in.h>
  128. #include <fcntl.h>
  129. #ifdef WOLFSSL_VXWORKS_6_x
  130. #include <time.h>
  131. #else
  132. #include <sys/time.h>
  133. #endif
  134. #include <netdb.h>
  135. #include <pthread.h>
  136. #define SOCKET_T int
  137. #elif defined(WOLFSSL_ZEPHYR)
  138. #include <string.h>
  139. #include <sys/types.h>
  140. #include <zephyr/net/socket.h>
  141. #define SOCKET_T int
  142. #define SOL_SOCKET 1
  143. #define WOLFSSL_USE_GETADDRINFO
  144. static unsigned long inet_addr(const char *cp)
  145. {
  146. unsigned int a[4]; unsigned long ret;
  147. int i, j;
  148. for (i=0, j=0; i<4; i++) {
  149. a[i] = 0;
  150. while (cp[j] != '.' && cp[j] != '\0') {
  151. a[i] *= 10;
  152. a[i] += cp[j] - '0';
  153. j++;
  154. }
  155. }
  156. ret = ((a[3]<<24) + (a[2]<<16) + (a[1]<<8) + a[0]) ;
  157. return(ret) ;
  158. }
  159. #elif defined(NETOS)
  160. #include <string.h>
  161. #include <sys/types.h>
  162. struct hostent {
  163. char* h_name; /* official name of host */
  164. char** h_aliases; /* alias list */
  165. int h_addrtype; /* host address type */
  166. int h_length; /* length of address */
  167. char** h_addr_list; /* list of addresses from the name server */
  168. };
  169. #else
  170. #include <string.h>
  171. #include <sys/types.h>
  172. #ifndef WOLFSSL_LEANPSK
  173. #include <unistd.h>
  174. #include <netdb.h>
  175. #include <netinet/in.h>
  176. #include <netinet/tcp.h>
  177. #include <arpa/inet.h>
  178. #include <sys/ioctl.h>
  179. #include <sys/time.h>
  180. #include <sys/socket.h>
  181. #ifdef HAVE_PTHREAD
  182. #include <pthread.h>
  183. #endif
  184. #include <fcntl.h>
  185. #ifdef TEST_IPV6
  186. #include <netdb.h>
  187. #endif
  188. #endif
  189. #ifdef FREESCALE_MQX
  190. typedef int socklen_t ;
  191. #endif
  192. #define SOCKET_T int
  193. #ifndef SO_NOSIGPIPE
  194. #include <signal.h> /* ignore SIGPIPE */
  195. #endif
  196. #define SNPRINTF snprintf
  197. #define XSELECT_WAIT(x,y) do { \
  198. struct timeval tv = {((x) + ((y) / 1000000)),((y) % 1000000)}; \
  199. if ((select(0, NULL, NULL, NULL, &tv) < 0) && (errno != EINTR)) \
  200. err_sys("select for XSELECT_WAIT failed."); \
  201. } while (0)
  202. #define XSLEEP_US(u) XSELECT_WAIT(0,u)
  203. #define XSLEEP_MS(m) XSELECT_WAIT(0,(m)*1000)
  204. #endif /* USE_WINDOWS_API */
  205. #ifndef XSLEEP_MS
  206. #define XSLEEP_MS(t) sleep(t/1000)
  207. #endif
  208. #ifdef WOLFSSL_ASYNC_CRYPT
  209. #include <wolfssl/wolfcrypt/async.h>
  210. #endif
  211. #ifdef HAVE_CAVIUM
  212. #include <wolfssl/wolfcrypt/port/cavium/cavium_nitrox.h>
  213. #endif
  214. #ifdef _MSC_VER
  215. /* disable conversion warning */
  216. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  217. #pragma warning(disable:4244 4996)
  218. #endif
  219. #ifndef WOLFSSL_CIPHER_LIST_MAX_SIZE
  220. #define WOLFSSL_CIPHER_LIST_MAX_SIZE 4096
  221. #endif
  222. /* Buffer for benchmark tests */
  223. #ifndef TEST_BUFFER_SIZE
  224. #define TEST_BUFFER_SIZE 16384
  225. #endif
  226. #ifndef WOLFSSL_HAVE_MIN
  227. #define WOLFSSL_HAVE_MIN
  228. #ifdef NO_INLINE
  229. #define min no_inline_min
  230. #endif
  231. static WC_INLINE word32 min(word32 a, word32 b)
  232. {
  233. return a > b ? b : a;
  234. }
  235. #endif /* WOLFSSL_HAVE_MIN */
  236. /* Socket Handling */
  237. #ifndef WOLFSSL_SOCKET_INVALID
  238. #ifdef USE_WINDOWS_API
  239. #define WOLFSSL_SOCKET_INVALID ((SOCKET_T)INVALID_SOCKET)
  240. #elif defined(WOLFSSL_TIRTOS)
  241. #define WOLFSSL_SOCKET_INVALID ((SOCKET_T)-1)
  242. #else
  243. #define WOLFSSL_SOCKET_INVALID (SOCKET_T)(0)
  244. #endif
  245. #endif /* WOLFSSL_SOCKET_INVALID */
  246. #ifndef WOLFSSL_SOCKET_IS_INVALID
  247. #if defined(USE_WINDOWS_API) || defined(WOLFSSL_TIRTOS)
  248. #define WOLFSSL_SOCKET_IS_INVALID(s) ((SOCKET_T)(s) == WOLFSSL_SOCKET_INVALID)
  249. #else
  250. #define WOLFSSL_SOCKET_IS_INVALID(s) ((SOCKET_T)(s) < WOLFSSL_SOCKET_INVALID)
  251. #endif
  252. #endif /* WOLFSSL_SOCKET_IS_INVALID */
  253. #if defined(__MACH__) || defined(USE_WINDOWS_API)
  254. #ifndef _SOCKLEN_T
  255. typedef int socklen_t;
  256. #endif
  257. #endif
  258. /* HPUX doesn't use socklent_t for third parameter to accept, unless
  259. _XOPEN_SOURCE_EXTENDED is defined */
  260. #if !defined(__hpux__) && !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_IAR_ARM)\
  261. && !defined(WOLFSSL_ROWLEY_ARM) && !defined(WOLFSSL_KEIL_TCP_NET)
  262. typedef socklen_t* ACCEPT_THIRD_T;
  263. #else
  264. #if defined _XOPEN_SOURCE_EXTENDED
  265. typedef socklen_t* ACCEPT_THIRD_T;
  266. #else
  267. typedef int* ACCEPT_THIRD_T;
  268. #endif
  269. #endif
  270. #ifndef MY_EX_USAGE
  271. #define MY_EX_USAGE 2
  272. #endif
  273. #ifndef EXIT_FAILURE
  274. #define EXIT_FAILURE 1
  275. #endif
  276. #if defined(WOLFSSL_FORCE_MALLOC_FAIL_TEST) || defined(WOLFSSL_ZEPHYR)
  277. #ifndef EXIT_SUCCESS
  278. #define EXIT_SUCCESS 0
  279. #endif
  280. #define XEXIT(rc) return rc
  281. #define XEXIT_T(rc) return (THREAD_RETURN)rc
  282. #else
  283. #define XEXIT(rc) exit((int)(rc))
  284. #define XEXIT_T(rc) exit((int)(rc))
  285. #endif
  286. static WC_INLINE
  287. #if defined(WOLFSSL_FORCE_MALLOC_FAIL_TEST) || defined(WOLFSSL_ZEPHYR)
  288. THREAD_RETURN
  289. #else
  290. WC_NORETURN void
  291. #endif
  292. err_sys(const char* msg)
  293. {
  294. #if !defined(__GNUC__)
  295. /* scan-build (which pretends to be gnuc) can get confused and think the
  296. * msg pointer can be null even when hardcoded and then it won't exit,
  297. * making null pointer checks above the err_sys() call useless.
  298. * We could just always exit() but some compilers will complain about no
  299. * possible return, with gcc we know the attribute to handle that with
  300. * WC_NORETURN. */
  301. if (msg)
  302. #endif
  303. {
  304. fprintf(stderr, "wolfSSL error: %s\n", msg);
  305. }
  306. XEXIT_T(EXIT_FAILURE);
  307. }
  308. static WC_INLINE
  309. #if defined(WOLFSSL_FORCE_MALLOC_FAIL_TEST) || defined(WOLFSSL_ZEPHYR)
  310. THREAD_RETURN
  311. #else
  312. WC_NORETURN void
  313. #endif
  314. err_sys_with_errno(const char* msg)
  315. {
  316. #if !defined(__GNUC__)
  317. /* scan-build (which pretends to be gnuc) can get confused and think the
  318. * msg pointer can be null even when hardcoded and then it won't exit,
  319. * making null pointer checks above the err_sys() call useless.
  320. * We could just always exit() but some compilers will complain about no
  321. * possible return, with gcc we know the attribute to handle that with
  322. * WC_NORETURN. */
  323. if (msg)
  324. #endif
  325. {
  326. #if defined(HAVE_STRING_H) && defined(HAVE_ERRNO_H)
  327. fprintf(stderr, "wolfSSL error: %s: %s\n", msg, strerror(errno));
  328. #else
  329. fprintf(stderr, "wolfSSL error: %s\n", msg);
  330. #endif
  331. }
  332. XEXIT_T(EXIT_FAILURE);
  333. }
  334. #define LIBCALL_CHECK_RET(...) do { \
  335. int _libcall_ret = (__VA_ARGS__); \
  336. if (_libcall_ret < 0) { \
  337. fprintf(stderr, "%s L%d error %d for \"%s\"\n", \
  338. __FILE__, __LINE__, errno, #__VA_ARGS__); \
  339. err_sys("library/system call failed"); \
  340. } \
  341. } while(0)
  342. #define PTHREAD_CHECK_RET(...) do { \
  343. int _pthread_ret = (__VA_ARGS__); \
  344. if (_pthread_ret != 0) { \
  345. errno = _pthread_ret; \
  346. fprintf(stderr, "%s L%d error %d for \"%s\"\n", \
  347. __FILE__, __LINE__, _pthread_ret, #__VA_ARGS__); \
  348. err_sys("pthread call failed"); \
  349. } \
  350. } while(0)
  351. #ifndef WOLFSSL_NO_TLS12
  352. #define SERVER_DEFAULT_VERSION 3
  353. #else
  354. #define SERVER_DEFAULT_VERSION 4
  355. #endif
  356. #define SERVER_DTLS_DEFAULT_VERSION (-2)
  357. #define SERVER_INVALID_VERSION (-99)
  358. #define SERVER_DOWNGRADE_VERSION (-98)
  359. #ifndef WOLFSSL_NO_TLS12
  360. #define CLIENT_DEFAULT_VERSION 3
  361. #else
  362. #define CLIENT_DEFAULT_VERSION 4
  363. #endif
  364. #define CLIENT_DTLS_DEFAULT_VERSION (-2)
  365. #define CLIENT_INVALID_VERSION (-99)
  366. #define CLIENT_DOWNGRADE_VERSION (-98)
  367. #define EITHER_DOWNGRADE_VERSION (-97)
  368. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_MAX_STRENGTH)
  369. #define DEFAULT_MIN_DHKEY_BITS 2048
  370. #define DEFAULT_MAX_DHKEY_BITS 3072
  371. #else
  372. #define DEFAULT_MIN_DHKEY_BITS 1024
  373. #define DEFAULT_MAX_DHKEY_BITS 2048
  374. #endif
  375. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_MAX_STRENGTH)
  376. #define DEFAULT_MIN_RSAKEY_BITS 2048
  377. #else
  378. #ifndef DEFAULT_MIN_RSAKEY_BITS
  379. #define DEFAULT_MIN_RSAKEY_BITS 1024
  380. #endif
  381. #endif
  382. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_MAX_STRENGTH)
  383. #define DEFAULT_MIN_ECCKEY_BITS 256
  384. #else
  385. #ifndef DEFAULT_MIN_ECCKEY_BITS
  386. #define DEFAULT_MIN_ECCKEY_BITS 224
  387. #endif
  388. #endif
  389. #ifndef DEFAULT_TIMEOUT_SEC
  390. #define DEFAULT_TIMEOUT_SEC 2
  391. #endif
  392. /* all certs relative to wolfSSL home directory now */
  393. #if defined(WOLFSSL_NO_CURRDIR) || defined(WOLFSSL_MDK_SHELL)
  394. #define caCertFile "certs/ca-cert.pem"
  395. #define eccCertFile "certs/server-ecc.pem"
  396. #define eccKeyFile "certs/ecc-key.pem"
  397. #define eccKeyPubFile "certs/ecc-keyPub.pem"
  398. #define eccRsaCertFile "certs/server-ecc-rsa.pem"
  399. #define svrCertFile "certs/server-cert.pem"
  400. #define svrKeyFile "certs/server-key.pem"
  401. #define svrKeyPubFile "certs/server-keyPub.pem"
  402. #define cliCertFile "certs/client-cert.pem"
  403. #define cliCertDerFile "certs/client-cert.der"
  404. #define cliCertFileExt "certs/client-cert-ext.pem"
  405. #define cliCertDerFileExt "certs/client-cert-ext.der"
  406. #define cliKeyFile "certs/client-key.pem"
  407. #define cliKeyPubFile "certs/client-keyPub.pem"
  408. #define dhParamFile "certs/dh2048.pem"
  409. #define cliEccKeyFile "certs/ecc-client-key.pem"
  410. #define cliEccKeyPubFile "certs/ecc-client-keyPub.pem"
  411. #define cliEccCertFile "certs/client-ecc-cert.pem"
  412. #define caEccCertFile "certs/ca-ecc-cert.pem"
  413. #define crlPemDir "certs/crl"
  414. #define edCertFile "certs/ed25519/server-ed25519-cert.pem"
  415. #define edKeyFile "certs/ed25519/server-ed25519-priv.pem"
  416. #define edKeyPubFile "certs/ed25519/server-ed25519-key.pem"
  417. #define cliEdCertFile "certs/ed25519/client-ed25519.pem"
  418. #define cliEdKeyFile "certs/ed25519/client-ed25519-priv.pem"
  419. #define cliEdKeyPubFile "certs/ed25519/client-ed25519-key.pem"
  420. #define caEdCertFile "certs/ed25519/ca-ed25519.pem"
  421. #define ed448CertFile "certs/ed448/server-ed448-cert.pem"
  422. #define ed448KeyFile "certs/ed448/server-ed448-priv.pem"
  423. #define cliEd448CertFile "certs/ed448/client-ed448.pem"
  424. #define cliEd448KeyFile "certs/ed448/client-ed448-priv.pem"
  425. #define caEd448CertFile "certs/ed448/ca-ed448.pem"
  426. #define caCertFolder "certs/"
  427. #ifdef HAVE_WNR
  428. /* Whitewood netRandom default config file */
  429. #define wnrConfig "wnr-example.conf"
  430. #endif
  431. #elif defined(NETOS) && defined(HAVE_FIPS)
  432. /* These defines specify the file system volume and root directory used by
  433. * the FTP server used in the only supported NETOS FIPS solution (at this
  434. * time), these can be tailored in the event a future FIPS solution is added
  435. * for an alternate NETOS use-case */
  436. #define FS_VOLUME1 "FLASH0"
  437. #define FS_VOLUME1_DIR FS_VOLUME1 "/"
  438. #define caCertFile FS_VOLUME1_DIR "certs/ca-cert.pem"
  439. #define eccCertFile FS_VOLUME1_DIR "certs/server-ecc.pem"
  440. #define eccKeyFile FS_VOLUME1_DIR "certs/ecc-key.pem"
  441. #define svrCertFile FS_VOLUME1_DIR "certs/server-cert.pem"
  442. #define svrKeyFile FS_VOLUME1_DIR "certs/server-key.pem"
  443. #define cliCertFile FS_VOLUME1_DIR "certs/client-cert.pem"
  444. #define cliKeyFile FS_VOLUME1_DIR "certs/client-key.pem"
  445. #define ntruCertFile FS_VOLUME1_DIR "certs/ntru-cert.pem"
  446. #define ntruKeyFile FS_VOLUME1_DIR "certs/ntru-key.raw"
  447. #define dhParamFile FS_VOLUME1_DIR "certs/dh2048.pem"
  448. #define cliEccKeyFile FS_VOLUME1_DIR "certs/ecc-client-key.pem"
  449. #define cliEccCertFile FS_VOLUME1_DIR "certs/client-ecc-cert.pem"
  450. #define caEccCertFile FS_VOLUME1_DIR "certs/ca-ecc-cert/pem"
  451. #define crlPemDir FS_VOLUME1_DIR "certs/crl"
  452. #ifdef HAVE_WNR
  453. /* Whitewood netRandom default config file */
  454. #define wnrConfig "wnr-example.conf"
  455. #endif
  456. #else
  457. #define caCertFile "./certs/ca-cert.pem"
  458. #define eccCertFile "./certs/server-ecc.pem"
  459. #define eccKeyFile "./certs/ecc-key.pem"
  460. #define eccKeyPubFile "./certs/ecc-keyPub.pem"
  461. #define eccRsaCertFile "./certs/server-ecc-rsa.pem"
  462. #define svrCertFile "./certs/server-cert.pem"
  463. #define svrKeyFile "./certs/server-key.pem"
  464. #define svrKeyPubFile "./certs/server-keyPub.pem"
  465. #define cliCertFile "./certs/client-cert.pem"
  466. #define cliCertDerFile "./certs/client-cert.der"
  467. #define cliCertFileExt "./certs/client-cert-ext.pem"
  468. #define cliCertDerFileExt "./certs/client-cert-ext.der"
  469. #define cliKeyFile "./certs/client-key.pem"
  470. #define cliKeyPubFile "./certs/client-keyPub.pem"
  471. #define dhParamFile "./certs/dh2048.pem"
  472. #define cliEccKeyFile "./certs/ecc-client-key.pem"
  473. #define cliEccKeyPubFile "./certs/ecc-client-keyPub.pem"
  474. #define cliEccCertFile "./certs/client-ecc-cert.pem"
  475. #define caEccCertFile "./certs/ca-ecc-cert.pem"
  476. #define crlPemDir "./certs/crl"
  477. #define edCertFile "./certs/ed25519/server-ed25519-cert.pem"
  478. #define edKeyFile "./certs/ed25519/server-ed25519-priv.pem"
  479. #define edKeyPubFile "./certs/ed25519/server-ed25519-key.pem"
  480. #define cliEdCertFile "./certs/ed25519/client-ed25519.pem"
  481. #define cliEdKeyFile "./certs/ed25519/client-ed25519-priv.pem"
  482. #define cliEdKeyPubFile "./certs/ed25519/client-ed25519-key.pem"
  483. #define caEdCertFile "./certs/ed25519/ca-ed25519.pem"
  484. #define ed448CertFile "./certs/ed448/server-ed448-cert.pem"
  485. #define ed448KeyFile "./certs/ed448/server-ed448-priv.pem"
  486. #define cliEd448CertFile "./certs/ed448/client-ed448.pem"
  487. #define cliEd448KeyFile "./certs/ed448/client-ed448-priv.pem"
  488. #define caEd448CertFile "./certs/ed448/ca-ed448.pem"
  489. #define caCertFolder "./certs/"
  490. #ifdef HAVE_WNR
  491. /* Whitewood netRandom default config file */
  492. #define wnrConfig "./wnr-example.conf"
  493. #endif
  494. #endif
  495. #ifdef TEST_IPV6
  496. typedef struct sockaddr_in6 SOCKADDR_IN_T;
  497. #define AF_INET_V AF_INET6
  498. #else
  499. typedef struct sockaddr_in SOCKADDR_IN_T;
  500. #define AF_INET_V AF_INET
  501. #endif
  502. typedef struct tcp_ready {
  503. word16 ready; /* predicate */
  504. word16 port;
  505. char* srfName; /* server ready file name */
  506. #ifdef HAVE_PTHREAD
  507. pthread_mutex_t mutex;
  508. pthread_cond_t cond;
  509. #endif
  510. #ifdef NETOS
  511. TX_MUTEX mutex;
  512. #endif
  513. } tcp_ready;
  514. static WC_INLINE void InitTcpReady(tcp_ready* ready)
  515. {
  516. ready->ready = 0;
  517. ready->port = 0;
  518. ready->srfName = NULL;
  519. #if defined(HAVE_PTHREAD)
  520. PTHREAD_CHECK_RET(pthread_mutex_init(&ready->mutex, 0));
  521. PTHREAD_CHECK_RET(pthread_cond_init(&ready->cond, 0));
  522. #elif defined(NETOS)
  523. tx_mutex_create(&ready->mutex, "wolfSSL Lock", TX_INHERIT);
  524. #else
  525. /* no threading init or single threaded */
  526. #endif
  527. }
  528. #ifdef NETOS
  529. struct hostent* gethostbyname(const char* name);
  530. #endif
  531. static WC_INLINE void FreeTcpReady(tcp_ready* ready)
  532. {
  533. #if defined(HAVE_PTHREAD)
  534. PTHREAD_CHECK_RET(pthread_mutex_destroy(&ready->mutex));
  535. PTHREAD_CHECK_RET(pthread_cond_destroy(&ready->cond));
  536. #elif defined(NETOS)
  537. tx_mutex_delete(&ready->mutex);
  538. #else
  539. (void)ready;
  540. #endif
  541. }
  542. typedef WOLFSSL_METHOD* (*method_provider)(void);
  543. typedef void (*ctx_callback)(WOLFSSL_CTX* ctx);
  544. typedef void (*ssl_callback)(WOLFSSL* ssl);
  545. typedef struct callback_functions {
  546. method_provider method;
  547. ctx_callback ctx_ready;
  548. ssl_callback ssl_ready;
  549. ssl_callback on_result;
  550. ssl_callback on_cleanup;
  551. WOLFSSL_CTX* ctx;
  552. const char* caPemFile;
  553. const char* certPemFile;
  554. const char* keyPemFile;
  555. const char* crlPemFile;
  556. #ifdef WOLFSSL_STATIC_MEMORY
  557. byte* mem;
  558. word32 memSz;
  559. wolfSSL_method_func method_ex;
  560. #endif
  561. int devId;
  562. int return_code;
  563. int last_err;
  564. unsigned char isSharedCtx:1;
  565. unsigned char loadToSSL:1;
  566. unsigned char ticNoInit:1;
  567. unsigned char doUdp:1;
  568. } callback_functions;
  569. #if defined(WOLFSSL_SRTP) && defined(HAVE_PTHREAD)
  570. typedef struct srtp_test_helper {
  571. pthread_mutex_t mutex;
  572. pthread_cond_t cond;
  573. uint8_t* server_srtp_ekm;
  574. size_t server_srtp_ekm_size;
  575. } srtp_test_helper;
  576. #endif /* WOLFSSL_SRTP HAVE_PTHREAD */
  577. typedef struct func_args {
  578. int argc;
  579. char** argv;
  580. int return_code;
  581. tcp_ready* signal;
  582. callback_functions *callbacks;
  583. #if defined(WOLFSSL_SRTP) && defined(HAVE_PTHREAD)
  584. srtp_test_helper* srtp_helper;
  585. #endif
  586. } func_args;
  587. #ifdef NETOS
  588. int dc_log_printf(char* format, ...);
  589. #undef printf
  590. #define printf dc_log_printf
  591. #endif
  592. void wait_tcp_ready(func_args* args);
  593. #ifndef SINGLE_THREADED
  594. void start_thread(THREAD_CB fun, func_args* args, THREAD_TYPE* thread);
  595. void join_thread(THREAD_TYPE thread);
  596. #endif
  597. typedef int (*cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  598. void test_wolfSSL_client_server_nofail_ex(callback_functions* client_cb,
  599. callback_functions* server_cb, cbType client_on_handshake);
  600. void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
  601. callback_functions* server_cb);
  602. /* Return
  603. * tmpDir on success
  604. * NULL on failure */
  605. char* create_tmp_dir(char* tmpDir, int len);
  606. /* Remaining functions return
  607. * 0 on success
  608. * -1 on failure */
  609. int rem_dir(const char* dirName);
  610. int rem_file(const char* fileName);
  611. int copy_file(const char* in, const char* out);
  612. /* wolfSSL */
  613. #ifndef TEST_IPV6
  614. static const char* const wolfSSLIP = "127.0.0.1";
  615. #else
  616. static const char* const wolfSSLIP = "::1";
  617. #endif
  618. static const word16 wolfSSLPort = 11111;
  619. extern int myoptind;
  620. extern char* myoptarg;
  621. #if defined(WOLFSSL_SRTP) && defined(HAVE_PTHREAD)
  622. static WC_INLINE void srtp_helper_init(srtp_test_helper *srtp)
  623. {
  624. srtp->server_srtp_ekm_size = 0;
  625. srtp->server_srtp_ekm = NULL;
  626. PTHREAD_CHECK_RET(pthread_mutex_init(&srtp->mutex, 0));
  627. PTHREAD_CHECK_RET(pthread_cond_init(&srtp->cond, 0));
  628. }
  629. /**
  630. * strp_helper_get_ekm() - get exported key material of other peer
  631. * @srtp: srtp_test_helper struct shared with other peer [in]
  632. * @ekm: where to store the shared buffer pointer [out]
  633. * @size: size of the shared buffer returned [out]
  634. *
  635. * This function wait that the other peer calls strp_helper_set_ekm() and then
  636. * store the buffer pointer/size in @ekm and @size.
  637. */
  638. static WC_INLINE void srtp_helper_get_ekm(srtp_test_helper *srtp,
  639. uint8_t **ekm, size_t *size)
  640. {
  641. PTHREAD_CHECK_RET(pthread_mutex_lock(&srtp->mutex));
  642. if (srtp->server_srtp_ekm == NULL)
  643. PTHREAD_CHECK_RET(pthread_cond_wait(&srtp->cond, &srtp->mutex));
  644. *ekm = srtp->server_srtp_ekm;
  645. *size = srtp->server_srtp_ekm_size;
  646. /* reset */
  647. srtp->server_srtp_ekm = NULL;
  648. srtp->server_srtp_ekm_size = 0;
  649. PTHREAD_CHECK_RET(pthread_mutex_unlock(&srtp->mutex));
  650. }
  651. /**
  652. * strp_helper_set_ekm() - set exported key material of other peer
  653. * @srtp: srtp_test_helper struct shared with other peer [in]
  654. * @ekm: pointer to the shared buffer [in]
  655. * @size: size of the shared buffer [in]
  656. *
  657. * This function set the @ekm and wakes up a peer waiting in
  658. * srtp_helper_get_ekm().
  659. *
  660. * used in client_srtp_test()/server_srtp_test()
  661. */
  662. static WC_INLINE void srtp_helper_set_ekm(srtp_test_helper *srtp,
  663. uint8_t *ekm, size_t size)
  664. {
  665. PTHREAD_CHECK_RET(pthread_mutex_lock(&srtp->mutex));
  666. srtp->server_srtp_ekm_size = size;
  667. srtp->server_srtp_ekm = ekm;
  668. PTHREAD_CHECK_RET(pthread_cond_signal(&srtp->cond));
  669. PTHREAD_CHECK_RET(pthread_mutex_unlock(&srtp->mutex));
  670. }
  671. static WC_INLINE void srtp_helper_free(srtp_test_helper *srtp)
  672. {
  673. PTHREAD_CHECK_RET(pthread_mutex_destroy(&srtp->mutex));
  674. PTHREAD_CHECK_RET(pthread_cond_destroy(&srtp->cond));
  675. }
  676. #endif /* WOLFSSL_SRTP && !SINGLE_THREADED && POSIX_THREADS */
  677. /**
  678. *
  679. * @param argc Number of argv strings
  680. * @param argv Array of string arguments
  681. * @param optstring String containing the supported alphanumeric arguments.
  682. * A ':' following a character means that it requires a
  683. * value in myoptarg to be set. A ';' means that the
  684. * myoptarg is optional. myoptarg is set to "" if not
  685. * present.
  686. * @return Option letter in argument
  687. */
  688. static WC_INLINE int mygetopt(int argc, char** argv, const char* optstring)
  689. {
  690. static char* next = NULL;
  691. char c;
  692. char* cp;
  693. /* Added sanity check because scan-build complains argv[myoptind] access
  694. * results in a null pointer dereference. */
  695. if (argv == NULL) {
  696. myoptarg = NULL;
  697. return -1;
  698. }
  699. if (myoptind == 0)
  700. next = NULL; /* we're starting new/over */
  701. if (next == NULL || *next == '\0') {
  702. if (myoptind == 0)
  703. myoptind++;
  704. if (myoptind >= argc || argv[myoptind] == NULL ||
  705. argv[myoptind][0] != '-' || argv[myoptind][1] == '\0') {
  706. myoptarg = NULL;
  707. if (myoptind < argc)
  708. myoptarg = argv[myoptind];
  709. return -1;
  710. }
  711. if (strcmp(argv[myoptind], "--") == 0) {
  712. myoptind++;
  713. myoptarg = NULL;
  714. if (myoptind < argc)
  715. myoptarg = argv[myoptind];
  716. return -1;
  717. }
  718. next = argv[myoptind];
  719. next++; /* skip - */
  720. myoptind++;
  721. }
  722. c = *next++;
  723. /* The C++ strchr can return a different value */
  724. cp = (char*)strchr(optstring, c);
  725. if (cp == NULL || c == ':' || c == ';')
  726. return '?';
  727. cp++;
  728. if (*cp == ':') {
  729. if (*next != '\0') {
  730. myoptarg = next;
  731. next = NULL;
  732. }
  733. else if (myoptind < argc) {
  734. myoptarg = argv[myoptind];
  735. myoptind++;
  736. }
  737. else
  738. return '?';
  739. }
  740. else if (*cp == ';') {
  741. myoptarg = (char*)"";
  742. if (*next != '\0') {
  743. myoptarg = next;
  744. next = NULL;
  745. }
  746. else if (myoptind < argc) {
  747. /* Check if next argument is not a parameter argument */
  748. if (argv[myoptind] && argv[myoptind][0] != '-') {
  749. myoptarg = argv[myoptind];
  750. myoptind++;
  751. }
  752. }
  753. }
  754. return c;
  755. }
  756. struct mygetopt_long_config {
  757. const char *name;
  758. int takes_arg; /* 0=no arg, 1=required arg, 2=optional arg */
  759. int value;
  760. };
  761. /**
  762. *
  763. * @param argc Number of argv strings
  764. * @param argv Array of string arguments
  765. * @param optstring String containing the supported alphanumeric arguments.
  766. * A ':' following a character means that it requires a
  767. * value in myoptarg to be set. A ';' means that the
  768. * myoptarg is optional. myoptarg is set to "" if not
  769. * present.
  770. * @return Option letter in argument
  771. */
  772. static WC_INLINE int mygetopt_long(int argc, char** argv, const char* optstring,
  773. const struct mygetopt_long_config *longopts, int *longindex)
  774. {
  775. static char* next = NULL;
  776. int c;
  777. char* cp;
  778. /* Added sanity check because scan-build complains argv[myoptind] access
  779. * results in a null pointer dereference. */
  780. if (argv == NULL) {
  781. myoptarg = NULL;
  782. return -1;
  783. }
  784. if (myoptind == 0)
  785. next = NULL; /* we're starting new/over */
  786. if (next == NULL || *next == '\0') {
  787. if (myoptind == 0)
  788. myoptind++;
  789. if (myoptind >= argc || argv[myoptind] == NULL ||
  790. argv[myoptind][0] != '-' || argv[myoptind][1] == '\0') {
  791. myoptarg = NULL;
  792. if (myoptind < argc)
  793. myoptarg = argv[myoptind];
  794. return -1;
  795. }
  796. if (strcmp(argv[myoptind], "--") == 0) {
  797. myoptind++;
  798. myoptarg = NULL;
  799. if (myoptind < argc)
  800. myoptarg = argv[myoptind];
  801. return -1;
  802. }
  803. if (strncmp(argv[myoptind], "--", 2) == 0) {
  804. const struct mygetopt_long_config *i;
  805. c = -1;
  806. myoptarg = NULL;
  807. for (i = longopts; i->name; ++i) {
  808. if (! strcmp(argv[myoptind] + 2, i->name)) {
  809. c = i->value;
  810. myoptind++;
  811. if (longindex)
  812. *longindex = (int)((size_t)(i - longopts) / sizeof i[0]);
  813. if (i->takes_arg) {
  814. if (myoptind < argc) {
  815. if (i->takes_arg == 1 || argv[myoptind][0] != '-') {
  816. myoptarg = argv[myoptind];
  817. myoptind++;
  818. }
  819. } else if (i->takes_arg != 2) {
  820. return -1;
  821. }
  822. }
  823. break;
  824. }
  825. }
  826. return c;
  827. }
  828. next = argv[myoptind];
  829. next++; /* skip - */
  830. myoptind++;
  831. }
  832. c = (int)(unsigned char)*next++;
  833. /* The C++ strchr can return a different value */
  834. cp = (char*)strchr(optstring, c);
  835. if (cp == NULL || c == ':' || c == ';')
  836. return '?';
  837. cp++;
  838. if (*cp == ':') {
  839. if (*next != '\0') {
  840. myoptarg = next;
  841. next = NULL;
  842. }
  843. else if (myoptind < argc) {
  844. myoptarg = argv[myoptind];
  845. myoptind++;
  846. }
  847. else
  848. return '?';
  849. }
  850. else if (*cp == ';') {
  851. myoptarg = (char*)"";
  852. if (*next != '\0') {
  853. myoptarg = next;
  854. next = NULL;
  855. }
  856. else if (myoptind < argc) {
  857. /* Check if next argument is not a parameter argument */
  858. if (argv[myoptind] && argv[myoptind][0] != '-') {
  859. myoptarg = argv[myoptind];
  860. myoptind++;
  861. }
  862. }
  863. }
  864. return c;
  865. }
  866. #ifdef WOLFSSL_ENCRYPTED_KEYS
  867. static WC_INLINE int PasswordCallBack(char* passwd, int sz, int rw, void* userdata)
  868. {
  869. (void)rw;
  870. (void)userdata;
  871. if (userdata != NULL) {
  872. strncpy(passwd, (char*)userdata, sz);
  873. return (int)XSTRLEN((char*)userdata);
  874. }
  875. else {
  876. strncpy(passwd, "yassl123", sz);
  877. return 8;
  878. }
  879. }
  880. #endif
  881. static const char* client_showpeer_msg[][9] = {
  882. /* English */
  883. {
  884. "SSL version is",
  885. "SSL cipher suite is",
  886. "SSL signature algorithm is",
  887. "SSL curve name is",
  888. "SSL DH size is",
  889. "SSL reused session",
  890. "Alternate cert chain used",
  891. "peer's cert info:",
  892. NULL
  893. },
  894. #ifndef NO_MULTIBYTE_PRINT
  895. /* Japanese */
  896. {
  897. "SSL バージョンは",
  898. "SSL 暗号スイートは",
  899. "SSL signature algorithm is",
  900. "SSL 曲線名は",
  901. "SSL DH サイズは",
  902. "SSL 再利用セッション",
  903. "代替証明チェーンを使用",
  904. "相手方証明書情報",
  905. NULL
  906. },
  907. #endif
  908. };
  909. #if defined(KEEP_PEER_CERT) || defined(KEEP_OUR_CERT) || defined(SESSION_CERTS)
  910. static const char* client_showx509_msg[][5] = {
  911. /* English */
  912. {
  913. "issuer",
  914. "subject",
  915. "altname",
  916. "serial number",
  917. NULL
  918. },
  919. #ifndef NO_MULTIBYTE_PRINT
  920. /* Japanese */
  921. {
  922. "発行者",
  923. "サブジェクト",
  924. "代替名",
  925. "シリアル番号",
  926. NULL
  927. },
  928. #endif
  929. };
  930. /* lng_index is to specify the language for displaying message. */
  931. /* 0:English, 1:Japanese */
  932. static WC_INLINE void ShowX509Ex(WOLFSSL_X509* x509, const char* hdr,
  933. int lng_index)
  934. {
  935. char* altName;
  936. char* issuer;
  937. char* subject;
  938. byte serial[32];
  939. int ret;
  940. int sz = sizeof(serial);
  941. const char** words = client_showx509_msg[lng_index];
  942. if (x509 == NULL) {
  943. fprintf(stderr, "%s No Cert\n", hdr);
  944. return;
  945. }
  946. issuer = wolfSSL_X509_NAME_oneline(
  947. wolfSSL_X509_get_issuer_name(x509), 0, 0);
  948. subject = wolfSSL_X509_NAME_oneline(
  949. wolfSSL_X509_get_subject_name(x509), 0, 0);
  950. printf("%s\n %s : %s\n %s: %s\n", hdr, words[0], issuer, words[1], subject);
  951. while ( (altName = wolfSSL_X509_get_next_altname(x509)) != NULL)
  952. printf(" %s = %s\n", words[2], altName);
  953. ret = wolfSSL_X509_get_serial_number(x509, serial, &sz);
  954. if (ret == WOLFSSL_SUCCESS) {
  955. int i;
  956. int strLen;
  957. char serialMsg[80];
  958. /* testsuite has multiple threads writing to stdout, get output
  959. message ready to write once */
  960. strLen = sprintf(serialMsg, " %s", words[3]);
  961. for (i = 0; i < sz; i++)
  962. sprintf(serialMsg + strLen + (i*3), ":%02x ", serial[i]);
  963. printf("%s\n", serialMsg);
  964. }
  965. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  966. XFREE(issuer, 0, DYNAMIC_TYPE_OPENSSL);
  967. #if defined(SHOW_CERTS) && defined(OPENSSL_EXTRA)
  968. {
  969. WOLFSSL_BIO* bio;
  970. char buf[WC_ASN_NAME_MAX];
  971. int textSz;
  972. /* print out domain component if certificate has it */
  973. textSz = wolfSSL_X509_NAME_get_text_by_NID(
  974. wolfSSL_X509_get_subject_name(x509), NID_domainComponent,
  975. buf, sizeof(buf));
  976. if (textSz > 0) {
  977. printf("Domain Component = %s\n", buf);
  978. }
  979. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  980. if (bio != NULL) {
  981. wolfSSL_BIO_set_fp(bio, stdout, BIO_NOCLOSE);
  982. wolfSSL_X509_print(bio, x509);
  983. wolfSSL_BIO_free(bio);
  984. }
  985. }
  986. #endif /* SHOW_CERTS && OPENSSL_EXTRA */
  987. }
  988. /* original ShowX509 to maintain compatibility */
  989. static WC_INLINE void ShowX509(WOLFSSL_X509* x509, const char* hdr)
  990. {
  991. ShowX509Ex(x509, hdr, 0);
  992. }
  993. #endif /* KEEP_PEER_CERT || KEEP_OUR_CERT || SESSION_CERTS */
  994. #if defined(SHOW_CERTS) && defined(SESSION_CERTS) && \
  995. (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL))
  996. static WC_INLINE void ShowX509Chain(WOLFSSL_X509_CHAIN* chain, int count,
  997. const char* hdr)
  998. {
  999. int i;
  1000. int length;
  1001. unsigned char buffer[3072];
  1002. WOLFSSL_X509* chainX509;
  1003. for (i = 0; i < count; i++) {
  1004. wolfSSL_get_chain_cert_pem(chain, i, buffer, sizeof(buffer), &length);
  1005. buffer[length] = 0;
  1006. printf("\n%s: %d has length %d data = \n%s\n", hdr, i, length, buffer);
  1007. chainX509 = wolfSSL_get_chain_X509(chain, i);
  1008. if (chainX509)
  1009. ShowX509(chainX509, hdr);
  1010. else
  1011. fprintf(stderr, "get_chain_X509 failed\n");
  1012. wolfSSL_FreeX509(chainX509);
  1013. }
  1014. }
  1015. #endif /* SHOW_CERTS && SESSION_CERTS */
  1016. /* lng_index is to specify the language for displaying message. */
  1017. /* 0:English, 1:Japanese */
  1018. static WC_INLINE void showPeerEx(WOLFSSL* ssl, int lng_index)
  1019. {
  1020. WOLFSSL_CIPHER* cipher;
  1021. const char** words = client_showpeer_msg[lng_index];
  1022. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448) || \
  1023. !defined(NO_DH)
  1024. const char *name;
  1025. #endif
  1026. #ifndef NO_DH
  1027. int bits;
  1028. #endif
  1029. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  1030. int nid;
  1031. #endif
  1032. #ifdef KEEP_PEER_CERT
  1033. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  1034. if (peer)
  1035. ShowX509Ex(peer, words[6], lng_index);
  1036. else
  1037. fprintf(stderr, "peer has no cert!\n");
  1038. wolfSSL_FreeX509(peer);
  1039. #endif
  1040. #if defined(SHOW_CERTS) && defined(KEEP_OUR_CERT) && \
  1041. (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL))
  1042. ShowX509(wolfSSL_get_certificate(ssl), "our cert info:");
  1043. printf("Peer verify result = %lu\n", wolfSSL_get_verify_result(ssl));
  1044. #endif /* SHOW_CERTS && KEEP_OUR_CERT */
  1045. printf("%s %s\n", words[0], wolfSSL_get_version(ssl));
  1046. cipher = wolfSSL_get_current_cipher(ssl);
  1047. printf("%s %s\n", words[1], wolfSSL_CIPHER_get_name(cipher));
  1048. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  1049. if (wolfSSL_get_signature_nid(ssl, &nid) == WOLFSSL_SUCCESS) {
  1050. printf("%s %s\n", words[2], OBJ_nid2sn(nid));
  1051. }
  1052. #endif
  1053. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448) || \
  1054. !defined(NO_DH)
  1055. if ((name = wolfSSL_get_curve_name(ssl)) != NULL)
  1056. printf("%s %s\n", words[3], name);
  1057. #endif
  1058. #ifndef NO_DH
  1059. else if ((bits = wolfSSL_GetDhKey_Sz(ssl)) > 0)
  1060. printf("%s %d bits\n", words[4], bits);
  1061. #endif
  1062. if (wolfSSL_session_reused(ssl))
  1063. printf("%s\n", words[5]);
  1064. #ifdef WOLFSSL_ALT_CERT_CHAINS
  1065. if (wolfSSL_is_peer_alt_cert_chain(ssl))
  1066. printf("%s\n", words[6]);
  1067. #endif
  1068. #if defined(SHOW_CERTS) && defined(SESSION_CERTS) && \
  1069. (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL))
  1070. {
  1071. WOLFSSL_X509_CHAIN* chain;
  1072. chain = wolfSSL_get_peer_chain(ssl);
  1073. ShowX509Chain(chain, wolfSSL_get_chain_count(chain), "session cert");
  1074. #ifdef WOLFSSL_ALT_CERT_CHAINS
  1075. if (wolfSSL_is_peer_alt_cert_chain(ssl)) {
  1076. chain = wolfSSL_get_peer_alt_chain(ssl);
  1077. ShowX509Chain(chain, wolfSSL_get_chain_count(chain), "alt cert");
  1078. }
  1079. #endif
  1080. }
  1081. #endif /* SHOW_CERTS && SESSION_CERTS */
  1082. (void)ssl;
  1083. }
  1084. /* original showPeer to maintain compatibility */
  1085. static WC_INLINE void showPeer(WOLFSSL* ssl)
  1086. {
  1087. showPeerEx(ssl, 0);
  1088. }
  1089. static WC_INLINE void build_addr(SOCKADDR_IN_T* addr, const char* peer,
  1090. word16 port, int udp, int sctp)
  1091. {
  1092. int useLookup = 0;
  1093. (void)useLookup;
  1094. (void)udp;
  1095. (void)sctp;
  1096. if (addr == NULL) {
  1097. err_sys("invalid argument to build_addr, addr is NULL");
  1098. return;
  1099. }
  1100. XMEMSET(addr, 0, sizeof(SOCKADDR_IN_T));
  1101. #ifndef TEST_IPV6
  1102. /* peer could be in human readable form */
  1103. if ( ((size_t)peer != INADDR_ANY) && isalpha((int)peer[0])) {
  1104. #ifdef WOLFSSL_USE_POPEN_HOST
  1105. char host_ipaddr[4] = { 127, 0, 0, 1 };
  1106. int found = 1;
  1107. if ((XSTRCMP(peer, "localhost") != 0) &&
  1108. (XSTRCMP(peer, "127.0.0.1") != 0)) {
  1109. FILE* fp;
  1110. char cmd[100];
  1111. XSTRNCPY(cmd, "host ", 6);
  1112. XSTRNCAT(cmd, peer, 99 - XSTRLEN(cmd));
  1113. found = 0;
  1114. fp = popen(cmd, "r");
  1115. if (fp != NULL) {
  1116. char host_out[100];
  1117. while (fgets(host_out, sizeof(host_out), fp) != NULL) {
  1118. int i;
  1119. int j = 0;
  1120. for (j = 0; host_out[j] != '\0'; j++) {
  1121. if ((host_out[j] >= '0') && (host_out[j] <= '9')) {
  1122. break;
  1123. }
  1124. }
  1125. found = (host_out[j] >= '0') && (host_out[j] <= '9');
  1126. if (!found) {
  1127. continue;
  1128. }
  1129. for (i = 0; i < 4; i++) {
  1130. host_ipaddr[i] = atoi(host_out + j);
  1131. while ((host_out[j] >= '0') && (host_out[j] <= '9')) {
  1132. j++;
  1133. }
  1134. if (host_out[j] == '.') {
  1135. j++;
  1136. found &= (i != 3);
  1137. }
  1138. else {
  1139. found &= (i == 3);
  1140. break;
  1141. }
  1142. }
  1143. if (found) {
  1144. break;
  1145. }
  1146. }
  1147. pclose(fp);
  1148. }
  1149. }
  1150. if (found) {
  1151. XMEMCPY(&addr->sin_addr.s_addr, host_ipaddr, sizeof(host_ipaddr));
  1152. useLookup = 1;
  1153. }
  1154. #elif !defined(WOLFSSL_USE_GETADDRINFO)
  1155. #if defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
  1156. int err;
  1157. struct hostent* entry = gethostbyname(peer, &err);
  1158. #elif defined(WOLFSSL_TIRTOS)
  1159. struct hostent* entry = DNSGetHostByName(peer);
  1160. #elif defined(WOLFSSL_VXWORKS)
  1161. struct hostent* entry = (struct hostent*)hostGetByName((char*)peer);
  1162. #else
  1163. struct hostent* entry = gethostbyname(peer);
  1164. #endif
  1165. if (entry) {
  1166. XMEMCPY(&addr->sin_addr.s_addr, entry->h_addr_list[0],
  1167. entry->h_length);
  1168. useLookup = 1;
  1169. }
  1170. #else
  1171. struct zsock_addrinfo hints, *addrInfo;
  1172. char portStr[6];
  1173. XSNPRINTF(portStr, sizeof(portStr), "%d", port);
  1174. XMEMSET(&hints, 0, sizeof(hints));
  1175. hints.ai_family = AF_UNSPEC;
  1176. hints.ai_socktype = udp ? SOCK_DGRAM : SOCK_STREAM;
  1177. hints.ai_protocol = udp ? IPPROTO_UDP : IPPROTO_TCP;
  1178. if (getaddrinfo((char*)peer, portStr, &hints, &addrInfo) == 0) {
  1179. XMEMCPY(addr, addrInfo->ai_addr, sizeof(*addr));
  1180. useLookup = 1;
  1181. }
  1182. #endif
  1183. else
  1184. err_sys("no entry for host");
  1185. }
  1186. #endif
  1187. #ifndef TEST_IPV6
  1188. #if defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
  1189. addr->sin_family = PF_INET;
  1190. #else
  1191. addr->sin_family = AF_INET_V;
  1192. #endif
  1193. addr->sin_port = XHTONS(port);
  1194. if ((size_t)peer == INADDR_ANY)
  1195. addr->sin_addr.s_addr = INADDR_ANY;
  1196. else {
  1197. if (!useLookup)
  1198. addr->sin_addr.s_addr = inet_addr(peer);
  1199. }
  1200. #else
  1201. addr->sin6_family = AF_INET_V;
  1202. addr->sin6_port = XHTONS(port);
  1203. if ((size_t)peer == INADDR_ANY) {
  1204. addr->sin6_addr = in6addr_any;
  1205. }
  1206. else {
  1207. #if defined(HAVE_GETADDRINFO)
  1208. struct addrinfo hints;
  1209. struct addrinfo* answer = NULL;
  1210. int ret;
  1211. char strPort[80];
  1212. XMEMSET(&hints, 0, sizeof(hints));
  1213. hints.ai_family = AF_INET_V;
  1214. if (udp) {
  1215. hints.ai_socktype = SOCK_DGRAM;
  1216. hints.ai_protocol = IPPROTO_UDP;
  1217. }
  1218. #ifdef WOLFSSL_SCTP
  1219. else if (sctp) {
  1220. hints.ai_socktype = SOCK_STREAM;
  1221. hints.ai_protocol = IPPROTO_SCTP;
  1222. }
  1223. #endif
  1224. else {
  1225. hints.ai_socktype = SOCK_STREAM;
  1226. hints.ai_protocol = IPPROTO_TCP;
  1227. }
  1228. (void)SNPRINTF(strPort, sizeof(strPort), "%d", port);
  1229. strPort[79] = '\0';
  1230. ret = getaddrinfo(peer, strPort, &hints, &answer);
  1231. if (ret < 0 || answer == NULL)
  1232. err_sys("getaddrinfo failed");
  1233. XMEMCPY(addr, answer->ai_addr, answer->ai_addrlen);
  1234. freeaddrinfo(answer);
  1235. #else
  1236. printf("no ipv6 getaddrinfo, loopback only tests/examples\n");
  1237. addr->sin6_addr = in6addr_loopback;
  1238. #endif
  1239. }
  1240. #endif
  1241. }
  1242. static WC_INLINE void tcp_socket(SOCKET_T* sockfd, int udp, int sctp)
  1243. {
  1244. (void)sctp;
  1245. if (udp)
  1246. *sockfd = socket(AF_INET_V, SOCK_DGRAM, IPPROTO_UDP);
  1247. #ifdef WOLFSSL_SCTP
  1248. else if (sctp)
  1249. *sockfd = socket(AF_INET_V, SOCK_STREAM, IPPROTO_SCTP);
  1250. #endif
  1251. else
  1252. *sockfd = socket(AF_INET_V, SOCK_STREAM, IPPROTO_TCP);
  1253. if(WOLFSSL_SOCKET_IS_INVALID(*sockfd)) {
  1254. err_sys_with_errno("socket failed\n");
  1255. }
  1256. #ifndef USE_WINDOWS_API
  1257. #ifdef SO_NOSIGPIPE
  1258. {
  1259. int on = 1;
  1260. socklen_t len = sizeof(on);
  1261. int res = setsockopt(*sockfd, SOL_SOCKET, SO_NOSIGPIPE, &on, len);
  1262. if (res < 0)
  1263. err_sys_with_errno("setsockopt SO_NOSIGPIPE failed\n");
  1264. }
  1265. #elif defined(WOLFSSL_MDK_ARM) || defined (WOLFSSL_TIRTOS) ||\
  1266. defined(WOLFSSL_KEIL_TCP_NET) || defined(WOLFSSL_ZEPHYR)
  1267. /* nothing to define */
  1268. #elif defined(NETOS)
  1269. /* TODO: signal(SIGPIPE, SIG_IGN); */
  1270. #else /* no S_NOSIGPIPE */
  1271. signal(SIGPIPE, SIG_IGN);
  1272. #endif /* S_NOSIGPIPE */
  1273. #if defined(TCP_NODELAY)
  1274. if (!udp && !sctp)
  1275. {
  1276. int on = 1;
  1277. socklen_t len = sizeof(on);
  1278. int res = setsockopt(*sockfd, IPPROTO_TCP, TCP_NODELAY, &on, len);
  1279. if (res < 0)
  1280. err_sys_with_errno("setsockopt TCP_NODELAY failed\n");
  1281. }
  1282. #endif
  1283. #endif /* USE_WINDOWS_API */
  1284. }
  1285. #if defined(WOLFSSL_WOLFSENTRY_HOOKS) && defined(WOLFSENTRY_H)
  1286. #include <wolfsentry/wolfsentry_util.h>
  1287. #if !defined(NO_FILESYSTEM) && !defined(WOLFSENTRY_NO_JSON)
  1288. #include <wolfsentry/wolfsentry_json.h>
  1289. #endif
  1290. #if defined(WOLFSENTRY_VERSION_GE)
  1291. #if WOLFSENTRY_VERSION_GE(0, 8, 0)
  1292. #define HAVE_WOLFSENTRY_API_0v8
  1293. #endif
  1294. #endif
  1295. #ifndef HAVE_WOLFSENTRY_API_0v8
  1296. #define WOLFSENTRY_CONTEXT_ARGS_OUT_EX(x) (x)
  1297. #define WOLFSENTRY_CONTEXT_ARGS_OUT_EX4(x, y) (x)
  1298. #endif
  1299. struct wolfsentry_data {
  1300. WOLFSENTRY_SOCKADDR(128) remote;
  1301. WOLFSENTRY_SOCKADDR(128) local;
  1302. wolfsentry_route_flags_t flags;
  1303. void *heap;
  1304. int alloctype;
  1305. };
  1306. static void free_wolfsentry_data(struct wolfsentry_data *data) {
  1307. XFREE(data, data->heap, data->alloctype);
  1308. }
  1309. static struct wolfsentry_context *wolfsentry = NULL;
  1310. static int wolfsentry_data_index = -1;
  1311. static WC_INLINE int wolfsentry_store_endpoints(
  1312. WOLFSSL *ssl,
  1313. SOCKADDR_IN_T *remote,
  1314. SOCKADDR_IN_T *local,
  1315. int proto,
  1316. wolfsentry_route_flags_t flags,
  1317. struct wolfsentry_data **wolfsentry_data_out)
  1318. {
  1319. struct wolfsentry_data *wolfsentry_data = (struct wolfsentry_data *)XMALLOC(
  1320. sizeof *wolfsentry_data, NULL, DYNAMIC_TYPE_SOCKADDR);
  1321. if (wolfsentry_data == NULL)
  1322. return WOLFSSL_FAILURE;
  1323. wolfsentry_data->heap = NULL;
  1324. wolfsentry_data->alloctype = DYNAMIC_TYPE_SOCKADDR;
  1325. #ifdef TEST_IPV6
  1326. if ((sizeof wolfsentry_data->remote.addr < sizeof remote->sin6_addr) ||
  1327. (sizeof wolfsentry_data->local.addr < sizeof local->sin6_addr))
  1328. return WOLFSSL_FAILURE;
  1329. wolfsentry_data->remote.sa_family = wolfsentry_data->local.sa_family = remote->sin6_family;
  1330. wolfsentry_data->remote.sa_port = ntohs(remote->sin6_port);
  1331. wolfsentry_data->local.sa_port = ntohs(local->sin6_port);
  1332. if (WOLFSENTRY_MASKIN_BITS(flags, WOLFSENTRY_ROUTE_FLAG_SA_REMOTE_ADDR_WILDCARD)) {
  1333. wolfsentry_data->remote.addr_len = 0;
  1334. XMEMSET(wolfsentry_data->remote.addr, 0, sizeof remote->sin6_addr);
  1335. } else {
  1336. wolfsentry_data->remote.addr_len = sizeof remote->sin6_addr * BITS_PER_BYTE;
  1337. XMEMCPY(wolfsentry_data->remote.addr, &remote->sin6_addr, sizeof remote->sin6_addr);
  1338. }
  1339. if (WOLFSENTRY_MASKIN_BITS(flags, WOLFSENTRY_ROUTE_FLAG_SA_LOCAL_ADDR_WILDCARD)) {
  1340. wolfsentry_data->local.addr_len = 0;
  1341. XMEMSET(wolfsentry_data->local.addr, 0, sizeof local->sin6_addr);
  1342. } else {
  1343. wolfsentry_data->local.addr_len = sizeof local->sin6_addr * BITS_PER_BYTE;
  1344. XMEMCPY(wolfsentry_data->local.addr, &local->sin6_addr, sizeof local->sin6_addr);
  1345. }
  1346. #else
  1347. if ((sizeof wolfsentry_data->remote.addr < sizeof remote->sin_addr) ||
  1348. (sizeof wolfsentry_data->local.addr < sizeof local->sin_addr))
  1349. return WOLFSSL_FAILURE;
  1350. wolfsentry_data->remote.sa_family = wolfsentry_data->local.sa_family = remote->sin_family;
  1351. wolfsentry_data->remote.sa_port = ntohs(remote->sin_port);
  1352. wolfsentry_data->local.sa_port = ntohs(local->sin_port);
  1353. if (WOLFSENTRY_MASKIN_BITS(flags, WOLFSENTRY_ROUTE_FLAG_SA_REMOTE_ADDR_WILDCARD)) {
  1354. wolfsentry_data->remote.addr_len = 0;
  1355. XMEMSET(wolfsentry_data->remote.addr, 0, sizeof remote->sin_addr);
  1356. } else {
  1357. wolfsentry_data->remote.addr_len = sizeof remote->sin_addr * BITS_PER_BYTE;
  1358. XMEMCPY(wolfsentry_data->remote.addr, &remote->sin_addr, sizeof remote->sin_addr);
  1359. }
  1360. if (WOLFSENTRY_MASKIN_BITS(flags, WOLFSENTRY_ROUTE_FLAG_SA_LOCAL_ADDR_WILDCARD)) {
  1361. wolfsentry_data->local.addr_len = 0;
  1362. XMEMSET(wolfsentry_data->local.addr, 0, sizeof local->sin_addr);
  1363. } else {
  1364. wolfsentry_data->local.addr_len = sizeof local->sin_addr * BITS_PER_BYTE;
  1365. XMEMCPY(wolfsentry_data->local.addr, &local->sin_addr, sizeof local->sin_addr);
  1366. }
  1367. #endif
  1368. wolfsentry_data->remote.sa_proto = wolfsentry_data->local.sa_proto = proto;
  1369. wolfsentry_data->remote.interface = wolfsentry_data->local.interface = 0;
  1370. wolfsentry_data->flags = flags;
  1371. if (wolfSSL_set_ex_data_with_cleanup(
  1372. ssl, wolfsentry_data_index, wolfsentry_data,
  1373. (wolfSSL_ex_data_cleanup_routine_t)free_wolfsentry_data) !=
  1374. WOLFSSL_SUCCESS) {
  1375. free_wolfsentry_data(wolfsentry_data);
  1376. return WOLFSSL_FAILURE;
  1377. }
  1378. if (wolfsentry_data_out != NULL)
  1379. *wolfsentry_data_out = wolfsentry_data;
  1380. return WOLFSSL_SUCCESS;
  1381. }
  1382. static int wolfSentry_NetworkFilterCallback(
  1383. WOLFSSL *ssl,
  1384. struct wolfsentry_context *_wolfsentry,
  1385. wolfSSL_netfilter_decision_t *decision)
  1386. {
  1387. struct wolfsentry_data *data;
  1388. char inet_ntop_buf[INET6_ADDRSTRLEN], inet_ntop_buf2[INET6_ADDRSTRLEN];
  1389. wolfsentry_errcode_t ret;
  1390. wolfsentry_action_res_t action_results;
  1391. #if defined(WOLFSENTRY_THREADSAFE) && defined(HAVE_WOLFSENTRY_API_0v8)
  1392. WOLFSENTRY_THREAD_HEADER(WOLFSENTRY_THREAD_FLAG_NONE);
  1393. if (WOLFSENTRY_THREAD_GET_ERROR < 0) {
  1394. fprintf(stderr, "wolfsentry thread init error: "
  1395. WOLFSENTRY_ERROR_FMT "\n",
  1396. WOLFSENTRY_ERROR_FMT_ARGS(WOLFSENTRY_THREAD_GET_ERROR));
  1397. return WOLFSSL_FAILURE;
  1398. }
  1399. #endif /* WOLFSENTRY_THREADSAFE && HAVE_WOLFSENTRY_API_0v8 */
  1400. if ((data = wolfSSL_get_ex_data(ssl, wolfsentry_data_index)) == NULL)
  1401. return WOLFSSL_FAILURE;
  1402. ret = wolfsentry_route_event_dispatch(
  1403. WOLFSENTRY_CONTEXT_ARGS_OUT_EX(_wolfsentry),
  1404. (const struct wolfsentry_sockaddr *)&data->remote,
  1405. (const struct wolfsentry_sockaddr *)&data->local,
  1406. data->flags,
  1407. NULL /* event_label */,
  1408. 0 /* event_label_len */,
  1409. NULL /* caller_context */,
  1410. NULL /* id */,
  1411. NULL /* inexact_matches */,
  1412. &action_results);
  1413. if (ret >= 0) {
  1414. if (WOLFSENTRY_MASKIN_BITS(action_results, WOLFSENTRY_ACTION_RES_REJECT))
  1415. *decision = WOLFSSL_NETFILTER_REJECT;
  1416. else if (WOLFSENTRY_MASKIN_BITS(action_results, WOLFSENTRY_ACTION_RES_ACCEPT))
  1417. *decision = WOLFSSL_NETFILTER_ACCEPT;
  1418. else
  1419. *decision = WOLFSSL_NETFILTER_PASS;
  1420. } else {
  1421. fprintf(stderr, "wolfsentry_route_event_dispatch error "
  1422. WOLFSENTRY_ERROR_FMT "\n", WOLFSENTRY_ERROR_FMT_ARGS(ret));
  1423. *decision = WOLFSSL_NETFILTER_PASS;
  1424. }
  1425. printf("wolfSentry got network filter callback: family=%d proto=%d rport=%d"
  1426. " lport=%d raddr=%s laddr=%s interface=%d; decision=%d (%s)\n",
  1427. data->remote.sa_family,
  1428. data->remote.sa_proto,
  1429. data->remote.sa_port,
  1430. data->local.sa_port,
  1431. inet_ntop(data->remote.sa_family, data->remote.addr, inet_ntop_buf,
  1432. sizeof inet_ntop_buf),
  1433. inet_ntop(data->local.sa_family, data->local.addr, inet_ntop_buf2,
  1434. sizeof inet_ntop_buf2),
  1435. data->remote.interface,
  1436. *decision,
  1437. *decision == WOLFSSL_NETFILTER_REJECT ? "REJECT" :
  1438. *decision == WOLFSSL_NETFILTER_ACCEPT ? "ACCEPT" :
  1439. *decision == WOLFSSL_NETFILTER_PASS ? "PASS" :
  1440. "???");
  1441. #if defined(WOLFSENTRY_THREADSAFE) && defined(HAVE_WOLFSENTRY_API_0v8)
  1442. ret = WOLFSENTRY_THREAD_TAILER(WOLFSENTRY_THREAD_FLAG_NONE);
  1443. if (ret < 0) {
  1444. fprintf(stderr, "wolfsentry thread exit error: "
  1445. WOLFSENTRY_ERROR_FMT "\n", WOLFSENTRY_ERROR_FMT_ARGS(ret));
  1446. }
  1447. #endif
  1448. return WOLFSSL_SUCCESS;
  1449. }
  1450. static int wolfsentry_setup(
  1451. struct wolfsentry_context **_wolfsentry,
  1452. const char *_wolfsentry_config_path,
  1453. wolfsentry_route_flags_t route_flags)
  1454. {
  1455. wolfsentry_errcode_t ret;
  1456. #ifdef HAVE_WOLFSENTRY_API_0v8
  1457. #ifdef WOLFSENTRY_THREADSAFE
  1458. WOLFSENTRY_THREAD_HEADER(WOLFSENTRY_THREAD_FLAG_NONE);
  1459. if (WOLFSENTRY_THREAD_GET_ERROR < 0) {
  1460. fprintf(stderr, "wolfsentry thread init error: "
  1461. WOLFSENTRY_ERROR_FMT "\n",
  1462. WOLFSENTRY_ERROR_FMT_ARGS(WOLFSENTRY_THREAD_GET_ERROR));
  1463. err_sys("unable to initialize wolfSentry thread context");
  1464. }
  1465. #endif
  1466. ret = wolfsentry_init(wolfsentry_build_settings,
  1467. WOLFSENTRY_CONTEXT_ARGS_OUT_EX(NULL /* hpi */),
  1468. NULL /* default config */,
  1469. _wolfsentry);
  1470. #else
  1471. ret = wolfsentry_init(NULL /* hpi */, NULL /* default config */,
  1472. _wolfsentry);
  1473. #endif
  1474. if (ret < 0) {
  1475. fprintf(stderr, "wolfsentry_init() returned " WOLFSENTRY_ERROR_FMT "\n",
  1476. WOLFSENTRY_ERROR_FMT_ARGS(ret));
  1477. err_sys("unable to initialize wolfSentry");
  1478. }
  1479. if (wolfsentry_data_index < 0)
  1480. wolfsentry_data_index = wolfSSL_get_ex_new_index(0, NULL, NULL, NULL,
  1481. NULL);
  1482. #if !defined(NO_FILESYSTEM) && !defined(WOLFSENTRY_NO_JSON)
  1483. if (_wolfsentry_config_path != NULL) {
  1484. unsigned char buf[512];
  1485. char err_buf[512];
  1486. struct wolfsentry_json_process_state *jps;
  1487. FILE *f = fopen(_wolfsentry_config_path, "r");
  1488. if (f == NULL) {
  1489. fprintf(stderr, "fopen(%s): %s\n",_wolfsentry_config_path,strerror(errno));
  1490. err_sys("unable to open wolfSentry config file");
  1491. }
  1492. if ((ret = wolfsentry_config_json_init(
  1493. WOLFSENTRY_CONTEXT_ARGS_OUT_EX(*_wolfsentry),
  1494. WOLFSENTRY_CONFIG_LOAD_FLAG_NONE,
  1495. &jps)) < 0) {
  1496. fprintf(stderr, "wolfsentry_config_json_init() returned "
  1497. WOLFSENTRY_ERROR_FMT "\n",
  1498. WOLFSENTRY_ERROR_FMT_ARGS(ret));
  1499. err_sys("error while initializing wolfSentry config parser");
  1500. }
  1501. for (;;) {
  1502. size_t n = fread(buf, 1, sizeof buf, f);
  1503. if ((n < sizeof buf) && ferror(f)) {
  1504. fprintf(stderr,"fread(%s): %s\n",_wolfsentry_config_path, strerror(errno));
  1505. err_sys("error while reading wolfSentry config file");
  1506. }
  1507. ret = wolfsentry_config_json_feed(jps, buf, n, err_buf, sizeof err_buf);
  1508. if (ret < 0) {
  1509. fprintf(stderr, "%.*s\n", (int)sizeof err_buf, err_buf);
  1510. err_sys("error while loading wolfSentry config file");
  1511. }
  1512. if ((n < sizeof buf) && feof(f))
  1513. break;
  1514. }
  1515. fclose(f);
  1516. if ((ret = wolfsentry_config_json_fini(&jps, err_buf, sizeof err_buf)) < 0) {
  1517. fprintf(stderr, "%.*s\n", (int)sizeof err_buf, err_buf);
  1518. err_sys("error while loading wolfSentry config file");
  1519. }
  1520. } else
  1521. #endif /* !NO_FILESYSTEM && !WOLFSENTRY_NO_JSON */
  1522. {
  1523. struct wolfsentry_route_table *table;
  1524. #ifdef WOLFSENTRY_THREADSAFE
  1525. ret = WOLFSENTRY_SHARED_EX(*_wolfsentry);
  1526. if (ret < 0) {
  1527. fprintf(stderr, "wolfsentry shared lock op failed: "
  1528. WOLFSENTRY_ERROR_FMT ".\n",
  1529. WOLFSENTRY_ERROR_FMT_ARGS(ret));
  1530. return ret;
  1531. }
  1532. #endif
  1533. if ((ret = wolfsentry_route_get_main_table(
  1534. WOLFSENTRY_CONTEXT_ARGS_OUT_EX(*_wolfsentry),
  1535. &table)) < 0)
  1536. {
  1537. fprintf(stderr, "wolfsentry_route_get_main_table() returned "
  1538. WOLFSENTRY_ERROR_FMT "\n",
  1539. WOLFSENTRY_ERROR_FMT_ARGS(ret));
  1540. #ifdef WOLFSENTRY_THREADSAFE
  1541. WOLFSENTRY_WARN_ON_FAILURE(
  1542. wolfsentry_context_unlock(
  1543. WOLFSENTRY_CONTEXT_ARGS_OUT_EX(*_wolfsentry)));
  1544. #endif
  1545. return ret;
  1546. }
  1547. if (WOLFSENTRY_MASKIN_BITS(route_flags, WOLFSENTRY_ROUTE_FLAG_DIRECTION_OUT)) {
  1548. WOLFSENTRY_SOCKADDR(128) remote, local;
  1549. wolfsentry_ent_id_t id;
  1550. wolfsentry_action_res_t action_results;
  1551. if ((ret = wolfsentry_route_table_default_policy_set(
  1552. WOLFSENTRY_CONTEXT_ARGS_OUT_EX(*_wolfsentry),
  1553. table,
  1554. WOLFSENTRY_ACTION_RES_ACCEPT))
  1555. < 0) {
  1556. fprintf(stderr,
  1557. "wolfsentry_route_table_default_policy_set() returned "
  1558. WOLFSENTRY_ERROR_FMT "\n",
  1559. WOLFSENTRY_ERROR_FMT_ARGS(ret));
  1560. #ifdef WOLFSENTRY_THREADSAFE
  1561. WOLFSENTRY_WARN_ON_FAILURE(
  1562. wolfsentry_context_unlock(
  1563. WOLFSENTRY_CONTEXT_ARGS_OUT_EX(*_wolfsentry)));
  1564. #endif
  1565. return ret;
  1566. }
  1567. XMEMSET(&remote, 0, sizeof remote);
  1568. XMEMSET(&local, 0, sizeof local);
  1569. #ifdef TEST_IPV6
  1570. remote.sa_family = local.sa_family = AF_INET6;
  1571. remote.addr_len = 128;
  1572. XMEMCPY(remote.addr, "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\001", 16);
  1573. #else
  1574. remote.sa_family = local.sa_family = AF_INET;
  1575. remote.addr_len = 32;
  1576. XMEMCPY(remote.addr, "\177\000\000\001", 4);
  1577. #endif
  1578. if ((ret = wolfsentry_route_insert
  1579. (WOLFSENTRY_CONTEXT_ARGS_OUT_EX(*_wolfsentry),
  1580. NULL /* caller_context */,
  1581. (const struct wolfsentry_sockaddr *)&remote,
  1582. (const struct wolfsentry_sockaddr *)&local,
  1583. route_flags |
  1584. WOLFSENTRY_ROUTE_FLAG_GREENLISTED |
  1585. WOLFSENTRY_ROUTE_FLAG_PARENT_EVENT_WILDCARD |
  1586. WOLFSENTRY_ROUTE_FLAG_REMOTE_INTERFACE_WILDCARD|
  1587. WOLFSENTRY_ROUTE_FLAG_LOCAL_INTERFACE_WILDCARD |
  1588. WOLFSENTRY_ROUTE_FLAG_SA_LOCAL_ADDR_WILDCARD |
  1589. WOLFSENTRY_ROUTE_FLAG_SA_PROTO_WILDCARD |
  1590. WOLFSENTRY_ROUTE_FLAG_SA_REMOTE_PORT_WILDCARD |
  1591. WOLFSENTRY_ROUTE_FLAG_SA_LOCAL_PORT_WILDCARD,
  1592. 0 /* event_label_len */, 0 /* event_label */, &id,
  1593. &action_results)) < 0) {
  1594. fprintf(stderr, "wolfsentry_route_insert() returned "
  1595. WOLFSENTRY_ERROR_FMT "\n",
  1596. WOLFSENTRY_ERROR_FMT_ARGS(ret));
  1597. #ifdef WOLFSENTRY_THREADSAFE
  1598. WOLFSENTRY_WARN_ON_FAILURE(
  1599. wolfsentry_context_unlock(
  1600. WOLFSENTRY_CONTEXT_ARGS_OUT_EX(*_wolfsentry)));
  1601. #endif
  1602. return ret;
  1603. }
  1604. } else if (WOLFSENTRY_MASKIN_BITS(route_flags, WOLFSENTRY_ROUTE_FLAG_DIRECTION_IN)) {
  1605. WOLFSENTRY_SOCKADDR(128) remote, local;
  1606. wolfsentry_ent_id_t id;
  1607. wolfsentry_action_res_t action_results;
  1608. if ((ret = wolfsentry_route_table_default_policy_set(
  1609. WOLFSENTRY_CONTEXT_ARGS_OUT_EX(*_wolfsentry), table,
  1610. WOLFSENTRY_ACTION_RES_REJECT|WOLFSENTRY_ACTION_RES_STOP))
  1611. < 0) {
  1612. fprintf(stderr,
  1613. "wolfsentry_route_table_default_policy_set() returned "
  1614. WOLFSENTRY_ERROR_FMT "\n",
  1615. WOLFSENTRY_ERROR_FMT_ARGS(ret));
  1616. #ifdef WOLFSENTRY_THREADSAFE
  1617. WOLFSENTRY_WARN_ON_FAILURE(
  1618. wolfsentry_context_unlock(
  1619. WOLFSENTRY_CONTEXT_ARGS_OUT_EX(*_wolfsentry)));
  1620. #endif
  1621. return ret;
  1622. }
  1623. XMEMSET(&remote, 0, sizeof remote);
  1624. XMEMSET(&local, 0, sizeof local);
  1625. #ifdef TEST_IPV6
  1626. remote.sa_family = local.sa_family = AF_INET6;
  1627. remote.addr_len = 128;
  1628. XMEMCPY(remote.addr, "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\001", 16);
  1629. #else
  1630. remote.sa_family = local.sa_family = AF_INET;
  1631. remote.addr_len = 32;
  1632. XMEMCPY(remote.addr, "\177\000\000\001", 4);
  1633. #endif
  1634. if ((ret = wolfsentry_route_insert
  1635. (WOLFSENTRY_CONTEXT_ARGS_OUT_EX(*_wolfsentry),
  1636. NULL /* caller_context */,
  1637. (const struct wolfsentry_sockaddr *)&remote,
  1638. (const struct wolfsentry_sockaddr *)&local,
  1639. route_flags |
  1640. WOLFSENTRY_ROUTE_FLAG_GREENLISTED |
  1641. WOLFSENTRY_ROUTE_FLAG_PARENT_EVENT_WILDCARD |
  1642. WOLFSENTRY_ROUTE_FLAG_REMOTE_INTERFACE_WILDCARD|
  1643. WOLFSENTRY_ROUTE_FLAG_LOCAL_INTERFACE_WILDCARD |
  1644. WOLFSENTRY_ROUTE_FLAG_SA_LOCAL_ADDR_WILDCARD |
  1645. WOLFSENTRY_ROUTE_FLAG_SA_PROTO_WILDCARD |
  1646. WOLFSENTRY_ROUTE_FLAG_SA_REMOTE_PORT_WILDCARD |
  1647. WOLFSENTRY_ROUTE_FLAG_SA_LOCAL_PORT_WILDCARD,
  1648. 0 /* event_label_len */, 0 /* event_label */, &id,
  1649. &action_results)) < 0) {
  1650. fprintf(stderr, "wolfsentry_route_insert() returned "
  1651. WOLFSENTRY_ERROR_FMT "\n",
  1652. WOLFSENTRY_ERROR_FMT_ARGS(ret));
  1653. #ifdef WOLFSENTRY_THREADSAFE
  1654. WOLFSENTRY_WARN_ON_FAILURE(
  1655. wolfsentry_context_unlock(
  1656. WOLFSENTRY_CONTEXT_ARGS_OUT_EX(*_wolfsentry)));
  1657. #endif
  1658. return ret;
  1659. }
  1660. }
  1661. #ifdef WOLFSENTRY_THREADSAFE
  1662. WOLFSENTRY_WARN_ON_FAILURE(
  1663. wolfsentry_context_unlock(
  1664. WOLFSENTRY_CONTEXT_ARGS_OUT_EX(*_wolfsentry)));
  1665. #endif
  1666. }
  1667. #if defined(WOLFSENTRY_THREADSAFE) && defined(HAVE_WOLFSENTRY_API_0v8)
  1668. ret = WOLFSENTRY_THREAD_TAILER(WOLFSENTRY_THREAD_FLAG_NONE);
  1669. if (ret < 0) {
  1670. fprintf(stderr, "wolfsentry thread exit error: "
  1671. WOLFSENTRY_ERROR_FMT "\n", WOLFSENTRY_ERROR_FMT_ARGS(ret));
  1672. }
  1673. #endif
  1674. return 0;
  1675. }
  1676. static WC_INLINE int tcp_connect_with_wolfSentry(
  1677. SOCKET_T* sockfd,
  1678. const char* ip,
  1679. word16 port,
  1680. int udp,
  1681. int sctp,
  1682. WOLFSSL* ssl,
  1683. struct wolfsentry_context *_wolfsentry)
  1684. {
  1685. SOCKADDR_IN_T remote_addr;
  1686. struct wolfsentry_data *wolfsentry_data;
  1687. char inet_ntop_buf[INET6_ADDRSTRLEN], inet_ntop_buf2[INET6_ADDRSTRLEN];
  1688. wolfsentry_errcode_t ret;
  1689. wolfsentry_action_res_t action_results;
  1690. wolfSSL_netfilter_decision_t decision;
  1691. #if defined(WOLFSENTRY_THREADSAFE) && defined(HAVE_WOLFSENTRY_API_0v8)
  1692. WOLFSENTRY_THREAD_HEADER(WOLFSENTRY_THREAD_FLAG_NONE);
  1693. if (WOLFSENTRY_THREAD_GET_ERROR < 0) {
  1694. fprintf(stderr, "wolfsentry thread init error: "
  1695. WOLFSENTRY_ERROR_FMT "\n",
  1696. WOLFSENTRY_ERROR_FMT_ARGS(WOLFSENTRY_THREAD_GET_ERROR));
  1697. err_sys("unable to initialize wolfSentry thread context");
  1698. }
  1699. #endif
  1700. build_addr(&remote_addr, ip, port, udp, sctp);
  1701. {
  1702. SOCKADDR_IN_T local_addr;
  1703. #ifdef TEST_IPV6
  1704. local_addr.sin6_port = 0;
  1705. #else
  1706. local_addr.sin_port = 0;
  1707. #endif
  1708. ((struct sockaddr *)&local_addr)->sa_family = ((struct sockaddr *)&remote_addr)->sa_family;
  1709. if (wolfsentry_store_endpoints(
  1710. ssl, &remote_addr, &local_addr,
  1711. udp ? IPPROTO_UDP : IPPROTO_TCP,
  1712. WOLFSENTRY_ROUTE_FLAG_DIRECTION_OUT|
  1713. WOLFSENTRY_ROUTE_FLAG_SA_LOCAL_ADDR_WILDCARD|
  1714. WOLFSENTRY_ROUTE_FLAG_SA_LOCAL_PORT_WILDCARD, &wolfsentry_data) != WOLFSSL_SUCCESS)
  1715. return WOLFSSL_FAILURE;
  1716. }
  1717. ret = wolfsentry_route_event_dispatch(
  1718. WOLFSENTRY_CONTEXT_ARGS_OUT_EX(_wolfsentry),
  1719. (const struct wolfsentry_sockaddr *)&wolfsentry_data->remote,
  1720. (const struct wolfsentry_sockaddr *)&wolfsentry_data->local,
  1721. wolfsentry_data->flags,
  1722. NULL /* event_label */,
  1723. 0 /* event_label_len */,
  1724. NULL /* caller_context */,
  1725. NULL /* id */,
  1726. NULL /* inexact_matches */,
  1727. &action_results);
  1728. if (ret < 0) {
  1729. fprintf(stderr, "wolfsentry_route_event_dispatch error "
  1730. WOLFSENTRY_ERROR_FMT "\n", WOLFSENTRY_ERROR_FMT_ARGS(ret));
  1731. decision = WOLFSSL_NETFILTER_PASS;
  1732. } else {
  1733. if (WOLFSENTRY_MASKIN_BITS(action_results, WOLFSENTRY_ACTION_RES_REJECT))
  1734. decision = WOLFSSL_NETFILTER_REJECT;
  1735. else if (WOLFSENTRY_MASKIN_BITS(action_results, WOLFSENTRY_ACTION_RES_ACCEPT))
  1736. decision = WOLFSSL_NETFILTER_ACCEPT;
  1737. else
  1738. decision = WOLFSSL_NETFILTER_PASS;
  1739. }
  1740. printf("wolfSentry callin from tcp_connect_with_wolfSentry: family=%d proto=%d rport=%d"
  1741. " lport=%d raddr=%s laddr=%s interface=%d; decision=%d (%s)\n",
  1742. wolfsentry_data->remote.sa_family,
  1743. wolfsentry_data->remote.sa_proto,
  1744. wolfsentry_data->remote.sa_port,
  1745. wolfsentry_data->local.sa_port,
  1746. inet_ntop(wolfsentry_data->remote.sa_family, wolfsentry_data->remote.addr, inet_ntop_buf,
  1747. sizeof inet_ntop_buf),
  1748. inet_ntop(wolfsentry_data->local.sa_family, wolfsentry_data->local.addr, inet_ntop_buf2,
  1749. sizeof inet_ntop_buf2),
  1750. wolfsentry_data->remote.interface,
  1751. decision,
  1752. decision == WOLFSSL_NETFILTER_REJECT ? "REJECT" :
  1753. decision == WOLFSSL_NETFILTER_ACCEPT ? "ACCEPT" :
  1754. decision == WOLFSSL_NETFILTER_PASS ? "PASS" :
  1755. "???");
  1756. if (decision == WOLFSSL_NETFILTER_REJECT)
  1757. return SOCKET_FILTERED_E;
  1758. if (udp) {
  1759. wolfSSL_dtls_set_peer(ssl, &remote_addr, sizeof(remote_addr));
  1760. }
  1761. tcp_socket(sockfd, udp, sctp);
  1762. if (!udp) {
  1763. if (connect(*sockfd, (const struct sockaddr*)&remote_addr, sizeof(remote_addr)) != 0)
  1764. err_sys_with_errno("tcp connect failed");
  1765. }
  1766. #if defined(WOLFSENTRY_THREADSAFE) && defined(HAVE_WOLFSENTRY_API_0v8)
  1767. ret = WOLFSENTRY_THREAD_TAILER(WOLFSENTRY_THREAD_FLAG_NONE);
  1768. if (ret < 0) {
  1769. fprintf(stderr, "wolfsentry thread exit error: "
  1770. WOLFSENTRY_ERROR_FMT "\n", WOLFSENTRY_ERROR_FMT_ARGS(ret));
  1771. }
  1772. #endif
  1773. return WOLFSSL_SUCCESS;
  1774. }
  1775. #define tcp_connect(sockfd, ip, port, udp, sctp, ssl) \
  1776. tcp_connect_with_wolfSentry(sockfd, ip, port, udp, sctp, ssl, wolfsentry)
  1777. #else /* !WOLFSSL_WOLFSENTRY_HOOKS */
  1778. static WC_INLINE void tcp_connect(SOCKET_T* sockfd, const char* ip, word16 port,
  1779. int udp, int sctp, WOLFSSL* ssl)
  1780. {
  1781. SOCKADDR_IN_T addr;
  1782. build_addr(&addr, ip, port, udp, sctp);
  1783. if (udp) {
  1784. wolfSSL_dtls_set_peer(ssl, &addr, sizeof(addr));
  1785. }
  1786. tcp_socket(sockfd, udp, sctp);
  1787. if (!udp) {
  1788. if (connect(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
  1789. err_sys_with_errno("tcp connect failed");
  1790. }
  1791. }
  1792. #endif /* WOLFSSL_WOLFSENTRY_HOOKS */
  1793. static WC_INLINE void udp_connect(SOCKET_T* sockfd, const char* ip, word16 port)
  1794. {
  1795. SOCKADDR_IN_T addr;
  1796. build_addr(&addr, ip, port, 1, 0);
  1797. if (connect(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
  1798. err_sys_with_errno("tcp connect failed");
  1799. }
  1800. enum {
  1801. TEST_SELECT_FAIL,
  1802. TEST_TIMEOUT,
  1803. TEST_RECV_READY,
  1804. TEST_SEND_READY,
  1805. TEST_ERROR_READY
  1806. };
  1807. #if !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_TCP_NET) && \
  1808. !defined(WOLFSSL_TIRTOS)
  1809. static WC_INLINE int tcp_select_ex(SOCKET_T socketfd, int to_sec, int rx)
  1810. {
  1811. fd_set fds, errfds;
  1812. fd_set* recvfds = NULL;
  1813. fd_set* sendfds = NULL;
  1814. SOCKET_T nfds = socketfd + 1;
  1815. #if !defined(__INTEGRITY)
  1816. struct timeval timeout = {(to_sec > 0) ? to_sec : 0, 0};
  1817. #else
  1818. struct timeval timeout;
  1819. #endif
  1820. int result;
  1821. FD_ZERO(&fds);
  1822. FD_SET(socketfd, &fds);
  1823. FD_ZERO(&errfds);
  1824. FD_SET(socketfd, &errfds);
  1825. if (rx)
  1826. recvfds = &fds;
  1827. else
  1828. sendfds = &fds;
  1829. #if defined(__INTEGRITY)
  1830. timeout.tv_sec = (long long)(to_sec > 0) ? to_sec : 0, 0;
  1831. #endif
  1832. result = select(nfds, recvfds, sendfds, &errfds, &timeout);
  1833. if (result == 0)
  1834. return TEST_TIMEOUT;
  1835. else if (result > 0) {
  1836. if (FD_ISSET(socketfd, &fds)) {
  1837. if (rx)
  1838. return TEST_RECV_READY;
  1839. else
  1840. return TEST_SEND_READY;
  1841. }
  1842. else if(FD_ISSET(socketfd, &errfds))
  1843. return TEST_ERROR_READY;
  1844. }
  1845. return TEST_SELECT_FAIL;
  1846. }
  1847. static WC_INLINE int tcp_select(SOCKET_T socketfd, int to_sec)
  1848. {
  1849. return tcp_select_ex(socketfd, to_sec, 1);
  1850. }
  1851. static WC_INLINE int tcp_select_tx(SOCKET_T socketfd, int to_sec)
  1852. {
  1853. return tcp_select_ex(socketfd, to_sec, 0);
  1854. }
  1855. #elif defined(WOLFSSL_TIRTOS) || defined(WOLFSSL_KEIL_TCP_NET)
  1856. static WC_INLINE int tcp_select(SOCKET_T socketfd, int to_sec)
  1857. {
  1858. return TEST_RECV_READY;
  1859. }
  1860. static WC_INLINE int tcp_select_tx(SOCKET_T socketfd, int to_sec)
  1861. {
  1862. return TEST_SEND_READY;
  1863. }
  1864. #endif /* !WOLFSSL_MDK_ARM */
  1865. static WC_INLINE void tcp_listen(SOCKET_T* sockfd, word16* port, int useAnyAddr,
  1866. int udp, int sctp)
  1867. {
  1868. SOCKADDR_IN_T addr;
  1869. /* don't use INADDR_ANY by default, firewall may block, make user switch
  1870. on */
  1871. build_addr(&addr, (useAnyAddr ? (const char*)INADDR_ANY : wolfSSLIP),
  1872. *port, udp, sctp);
  1873. tcp_socket(sockfd, udp, sctp);
  1874. #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_MDK_ARM)\
  1875. && !defined(WOLFSSL_KEIL_TCP_NET) && !defined(WOLFSSL_ZEPHYR)
  1876. {
  1877. int res, on = 1;
  1878. socklen_t len = sizeof(on);
  1879. res = setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &on, len);
  1880. if (res < 0)
  1881. err_sys_with_errno("setsockopt SO_REUSEADDR failed\n");
  1882. }
  1883. #ifdef SO_REUSEPORT
  1884. {
  1885. int res, on = 1;
  1886. socklen_t len = sizeof(on);
  1887. res = setsockopt(*sockfd, SOL_SOCKET, SO_REUSEPORT, &on, len);
  1888. if (res < 0)
  1889. err_sys_with_errno("setsockopt SO_REUSEPORT failed\n");
  1890. }
  1891. #endif
  1892. #endif
  1893. if (bind(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
  1894. err_sys_with_errno("tcp bind failed");
  1895. if (!udp) {
  1896. #ifdef WOLFSSL_KEIL_TCP_NET
  1897. #define SOCK_LISTEN_MAX_QUEUE 1
  1898. #else
  1899. #define SOCK_LISTEN_MAX_QUEUE 5
  1900. #endif
  1901. if (listen(*sockfd, SOCK_LISTEN_MAX_QUEUE) != 0)
  1902. err_sys_with_errno("tcp listen failed");
  1903. }
  1904. #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_TIRTOS) \
  1905. && !defined(WOLFSSL_ZEPHYR)
  1906. if (*port == 0) {
  1907. socklen_t len = sizeof(addr);
  1908. if (getsockname(*sockfd, (struct sockaddr*)&addr, &len) == 0) {
  1909. #ifndef TEST_IPV6
  1910. *port = XNTOHS(addr.sin_port);
  1911. #else
  1912. *port = XNTOHS(addr.sin6_port);
  1913. #endif
  1914. }
  1915. }
  1916. #endif
  1917. }
  1918. #if 0
  1919. static WC_INLINE int udp_read_connect(SOCKET_T sockfd)
  1920. {
  1921. SOCKADDR_IN_T cliaddr;
  1922. byte b[1500];
  1923. int n;
  1924. socklen_t len = sizeof(cliaddr);
  1925. n = (int)recvfrom(sockfd, (char*)b, sizeof(b), MSG_PEEK,
  1926. (struct sockaddr*)&cliaddr, &len);
  1927. if (n > 0) {
  1928. if (connect(sockfd, (const struct sockaddr*)&cliaddr,
  1929. sizeof(cliaddr)) != 0)
  1930. err_sys("udp connect failed");
  1931. }
  1932. else
  1933. err_sys("recvfrom failed");
  1934. return sockfd;
  1935. }
  1936. #endif
  1937. static WC_INLINE void udp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd,
  1938. int useAnyAddr, word16 port, func_args* args)
  1939. {
  1940. SOCKADDR_IN_T addr;
  1941. (void)args;
  1942. build_addr(&addr, (useAnyAddr ? (const char*)INADDR_ANY : wolfSSLIP),
  1943. port, 1, 0);
  1944. tcp_socket(sockfd, 1, 0);
  1945. #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_MDK_ARM) \
  1946. && !defined(WOLFSSL_KEIL_TCP_NET) && !defined(WOLFSSL_ZEPHYR)
  1947. {
  1948. int res, on = 1;
  1949. socklen_t len = sizeof(on);
  1950. res = setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &on, len);
  1951. if (res < 0)
  1952. err_sys_with_errno("setsockopt SO_REUSEADDR failed\n");
  1953. }
  1954. #ifdef SO_REUSEPORT
  1955. {
  1956. int res, on = 1;
  1957. socklen_t len = sizeof(on);
  1958. res = setsockopt(*sockfd, SOL_SOCKET, SO_REUSEPORT, &on, len);
  1959. if (res < 0)
  1960. err_sys_with_errno("setsockopt SO_REUSEPORT failed\n");
  1961. }
  1962. #endif
  1963. #endif
  1964. if (bind(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
  1965. err_sys_with_errno("tcp bind failed");
  1966. #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_TIRTOS)
  1967. if (port == 0) {
  1968. socklen_t len = sizeof(addr);
  1969. if (getsockname(*sockfd, (struct sockaddr*)&addr, &len) == 0) {
  1970. #ifndef TEST_IPV6
  1971. port = XNTOHS(addr.sin_port);
  1972. #else
  1973. port = XNTOHS(addr.sin6_port);
  1974. #endif
  1975. }
  1976. }
  1977. #endif
  1978. if (args != NULL && args->signal != NULL) {
  1979. #if defined(HAVE_PTHREAD)
  1980. /* signal ready to accept data */
  1981. tcp_ready* ready = args->signal;
  1982. PTHREAD_CHECK_RET(pthread_mutex_lock(&ready->mutex));
  1983. ready->ready = 1;
  1984. ready->port = port;
  1985. PTHREAD_CHECK_RET(pthread_cond_signal(&ready->cond));
  1986. PTHREAD_CHECK_RET(pthread_mutex_unlock(&ready->mutex));
  1987. #elif defined (WOLFSSL_TIRTOS)
  1988. /* Need mutex? */
  1989. tcp_ready* ready = args->signal;
  1990. ready->ready = 1;
  1991. ready->port = port;
  1992. #elif defined(NETOS)
  1993. tcp_ready* ready = args->signal;
  1994. (void)tx_mutex_get(&ready->mutex, TX_WAIT_FOREVER);
  1995. ready->ready = 1;
  1996. ready->port = port;
  1997. (void)tx_mutex_put(&ready->mutex);
  1998. #else
  1999. (void)port;
  2000. #endif
  2001. }
  2002. else {
  2003. fprintf(stderr, "args or args->signal was NULL. Not setting ready info.");
  2004. }
  2005. *clientfd = *sockfd;
  2006. }
  2007. static WC_INLINE void tcp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd,
  2008. func_args* args, word16 port, int useAnyAddr,
  2009. int udp, int sctp, int ready_file, int do_listen,
  2010. SOCKADDR_IN_T *client_addr, socklen_t *client_len)
  2011. {
  2012. tcp_ready* ready = NULL;
  2013. (void) ready; /* Account for case when "ready" is not used */
  2014. if (udp) {
  2015. udp_accept(sockfd, clientfd, useAnyAddr, port, args);
  2016. return;
  2017. }
  2018. if(do_listen) {
  2019. tcp_listen(sockfd, &port, useAnyAddr, udp, sctp);
  2020. #if defined(NO_MAIN_DRIVER) && defined(HAVE_PTHREAD)
  2021. /* signal ready to tcp_accept */
  2022. if (args)
  2023. ready = args->signal;
  2024. if (ready) {
  2025. PTHREAD_CHECK_RET(pthread_mutex_lock(&ready->mutex));
  2026. ready->ready = 1;
  2027. ready->port = port;
  2028. PTHREAD_CHECK_RET(pthread_cond_signal(&ready->cond));
  2029. PTHREAD_CHECK_RET(pthread_mutex_unlock(&ready->mutex));
  2030. }
  2031. #elif defined (WOLFSSL_TIRTOS)
  2032. /* Need mutex? */
  2033. if (args)
  2034. ready = args->signal;
  2035. if (ready) {
  2036. ready->ready = 1;
  2037. ready->port = port;
  2038. }
  2039. #elif defined(NETOS)
  2040. /* signal ready to tcp_accept */
  2041. if (args)
  2042. ready = args->signal;
  2043. if (ready) {
  2044. (void)tx_mutex_get(&ready->mutex, TX_WAIT_FOREVER);
  2045. ready->ready = 1;
  2046. ready->port = port;
  2047. (void)tx_mutex_put(&ready->mutex);
  2048. }
  2049. #endif
  2050. if (ready_file) {
  2051. #if !defined(NO_FILESYSTEM) || defined(FORCE_BUFFER_TEST) && \
  2052. !defined(NETOS)
  2053. XFILE srf = (XFILE)NULL;
  2054. if (args)
  2055. ready = args->signal;
  2056. if (ready) {
  2057. srf = XFOPEN(ready->srfName, "w");
  2058. if (srf) {
  2059. /* let's write port sever is listening on to ready file
  2060. external monitor can then do ephemeral ports by passing
  2061. -p 0 to server on supported platforms with -R ready_file
  2062. client can then wait for existence of ready_file and see
  2063. which port the server is listening on. */
  2064. LIBCALL_CHECK_RET(fprintf(srf, "%d\n", (int)port));
  2065. fclose(srf);
  2066. }
  2067. }
  2068. #endif
  2069. }
  2070. }
  2071. *clientfd = accept(*sockfd, (struct sockaddr*)client_addr,
  2072. (ACCEPT_THIRD_T)client_len);
  2073. if(WOLFSSL_SOCKET_IS_INVALID(*clientfd)) {
  2074. err_sys_with_errno("tcp accept failed");
  2075. }
  2076. }
  2077. static WC_INLINE void tcp_set_nonblocking(SOCKET_T* sockfd)
  2078. {
  2079. #if defined(USE_WINDOWS_API) || defined(EBSNET)
  2080. unsigned long blocking = 1;
  2081. int ret = ioctlsocket(*sockfd, FIONBIO, &blocking);
  2082. if (ret == SOCKET_ERROR)
  2083. err_sys_with_errno("ioctlsocket failed");
  2084. #elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET) \
  2085. || defined (WOLFSSL_TIRTOS)|| defined(WOLFSSL_VXWORKS) \
  2086. || defined(WOLFSSL_ZEPHYR)
  2087. /* non blocking not supported, for now */
  2088. #else
  2089. int flags = fcntl(*sockfd, F_GETFL, 0);
  2090. if (flags < 0)
  2091. err_sys_with_errno("fcntl get failed");
  2092. flags = fcntl(*sockfd, F_SETFL, flags | O_NONBLOCK);
  2093. if (flags < 0)
  2094. err_sys_with_errno("fcntl set failed");
  2095. #endif
  2096. }
  2097. static WC_INLINE void tcp_set_blocking(SOCKET_T* sockfd)
  2098. {
  2099. #ifdef USE_WINDOWS_API
  2100. unsigned long blocking = 0;
  2101. int ret = ioctlsocket(*sockfd, FIONBIO, &blocking);
  2102. if (ret == SOCKET_ERROR)
  2103. err_sys_with_errno("ioctlsocket failed");
  2104. #elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET) \
  2105. || defined (WOLFSSL_TIRTOS)|| defined(WOLFSSL_VXWORKS) \
  2106. || defined(WOLFSSL_ZEPHYR)
  2107. /* non blocking not supported, for now */
  2108. #else
  2109. int flags = fcntl(*sockfd, F_GETFL, 0);
  2110. if (flags < 0)
  2111. err_sys_with_errno("fcntl get failed");
  2112. flags = fcntl(*sockfd, F_SETFL, flags & (~O_NONBLOCK));
  2113. if (flags < 0)
  2114. err_sys_with_errno("fcntl set failed");
  2115. #endif
  2116. }
  2117. #ifndef NO_PSK
  2118. /* identity is OpenSSL testing default for openssl s_client, keep same */
  2119. static const char* kIdentityStr = "Client_identity";
  2120. static WC_INLINE unsigned int my_psk_client_cb(WOLFSSL* ssl, const char* hint,
  2121. char* identity, unsigned int id_max_len, unsigned char* key,
  2122. unsigned int key_max_len)
  2123. {
  2124. (void)ssl;
  2125. (void)hint;
  2126. (void)key_max_len;
  2127. /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */
  2128. XSTRNCPY(identity, kIdentityStr, id_max_len);
  2129. if (wolfSSL_GetVersion(ssl) < WOLFSSL_TLSV1_3) {
  2130. /* test key in hex is 0x1a2b3c4d , in decimal 439,041,101 , we're using
  2131. unsigned binary */
  2132. key[0] = 0x1a;
  2133. key[1] = 0x2b;
  2134. key[2] = 0x3c;
  2135. key[3] = 0x4d;
  2136. return 4; /* length of key in octets or 0 for error */
  2137. }
  2138. else {
  2139. int i;
  2140. int b = 0x01;
  2141. for (i = 0; i < 32; i++, b += 0x22) {
  2142. if (b >= 0x100)
  2143. b = 0x01;
  2144. key[i] = b;
  2145. }
  2146. return 32; /* length of key in octets or 0 for error */
  2147. }
  2148. }
  2149. static WC_INLINE unsigned int my_psk_server_cb(WOLFSSL* ssl, const char* identity,
  2150. unsigned char* key, unsigned int key_max_len)
  2151. {
  2152. (void)ssl;
  2153. (void)key_max_len;
  2154. /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */
  2155. if (XSTRCMP(identity, kIdentityStr) != 0)
  2156. return 0;
  2157. if (wolfSSL_GetVersion(ssl) < WOLFSSL_TLSV1_3) {
  2158. /* test key in hex is 0x1a2b3c4d , in decimal 439,041,101 , we're using
  2159. unsigned binary */
  2160. key[0] = 0x1a;
  2161. key[1] = 0x2b;
  2162. key[2] = 0x3c;
  2163. key[3] = 0x4d;
  2164. return 4; /* length of key in octets or 0 for error */
  2165. }
  2166. else {
  2167. int i;
  2168. int b = 0x01;
  2169. for (i = 0; i < 32; i++, b += 0x22) {
  2170. if (b >= 0x100)
  2171. b = 0x01;
  2172. key[i] = b;
  2173. }
  2174. return 32; /* length of key in octets or 0 for error */
  2175. }
  2176. }
  2177. #ifdef WOLFSSL_TLS13
  2178. static WC_INLINE unsigned int my_psk_client_tls13_cb(WOLFSSL* ssl,
  2179. const char* hint, char* identity, unsigned int id_max_len,
  2180. unsigned char* key, unsigned int key_max_len, const char** ciphersuite)
  2181. {
  2182. int i;
  2183. int b = 0x01;
  2184. const char* userCipher = (const char*)wolfSSL_get_psk_callback_ctx(ssl);
  2185. (void)ssl;
  2186. (void)hint;
  2187. (void)key_max_len;
  2188. /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */
  2189. XSTRNCPY(identity, kIdentityStr, id_max_len);
  2190. for (i = 0; i < 32; i++, b += 0x22) {
  2191. if (b >= 0x100)
  2192. b = 0x01;
  2193. key[i] = b;
  2194. }
  2195. *ciphersuite = userCipher ? userCipher : "TLS13-AES128-GCM-SHA256";
  2196. return 32; /* length of key in octets or 0 for error */
  2197. }
  2198. static WC_INLINE unsigned int my_psk_server_tls13_cb(WOLFSSL* ssl,
  2199. const char* identity, unsigned char* key, unsigned int key_max_len,
  2200. const char** ciphersuite)
  2201. {
  2202. int i;
  2203. int b = 0x01;
  2204. int kIdLen = (int)XSTRLEN(kIdentityStr);
  2205. const char* userCipher = (const char*)wolfSSL_get_psk_callback_ctx(ssl);
  2206. (void)ssl;
  2207. (void)key_max_len;
  2208. /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */
  2209. if (XSTRNCMP(identity, kIdentityStr, kIdLen) != 0)
  2210. return 0;
  2211. if (identity[kIdLen] != '\0') {
  2212. userCipher = wolfSSL_get_cipher_name_by_hash(ssl, identity + kIdLen);
  2213. }
  2214. for (i = 0; i < 32; i++, b += 0x22) {
  2215. if (b >= 0x100)
  2216. b = 0x01;
  2217. key[i] = b;
  2218. }
  2219. *ciphersuite = userCipher ? userCipher : "TLS13-AES128-GCM-SHA256";
  2220. return 32; /* length of key in octets or 0 for error */
  2221. }
  2222. #endif
  2223. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  2224. !defined(NO_FILESYSTEM)
  2225. static unsigned char local_psk[32];
  2226. #endif
  2227. static WC_INLINE int my_psk_use_session_cb(WOLFSSL* ssl,
  2228. const WOLFSSL_EVP_MD* md, const unsigned char **id,
  2229. size_t* idlen, WOLFSSL_SESSION **sess)
  2230. {
  2231. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  2232. !defined(NO_FILESYSTEM)
  2233. int i;
  2234. WOLFSSL_SESSION* lsess;
  2235. char buf[256];
  2236. const char* cipher_id = "TLS13-AES128-GCM-SHA256";
  2237. const SSL_CIPHER* cipher = NULL;
  2238. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  2239. int numCiphers = 0;
  2240. (void)ssl;
  2241. (void)md;
  2242. printf("use psk session callback \n");
  2243. lsess = SSL_SESSION_new();
  2244. if (lsess == NULL) {
  2245. return 0;
  2246. }
  2247. supportedCiphers = SSL_get_ciphers(ssl);
  2248. numCiphers = sk_num(supportedCiphers);
  2249. for (i = 0; i < numCiphers; ++i) {
  2250. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  2251. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  2252. }
  2253. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  2254. break;
  2255. }
  2256. }
  2257. if (i != numCiphers) {
  2258. int b = 0x01;
  2259. SSL_SESSION_set_cipher(lsess, cipher);
  2260. for (i = 0; i < 32; i++, b += 0x22) {
  2261. if (b >= 0x100)
  2262. b = 0x01;
  2263. local_psk[i] = b;
  2264. }
  2265. *id = local_psk;
  2266. *idlen = 32;
  2267. *sess = lsess;
  2268. return 1;
  2269. }
  2270. else {
  2271. *id = NULL;
  2272. *idlen = 0;
  2273. *sess = NULL;
  2274. SSL_SESSION_free(lsess);
  2275. return 0;
  2276. }
  2277. #else
  2278. (void)ssl;
  2279. (void)md;
  2280. (void)id;
  2281. (void)idlen;
  2282. (void)sess;
  2283. return 0;
  2284. #endif
  2285. }
  2286. static WC_INLINE unsigned int my_psk_client_cs_cb(WOLFSSL* ssl,
  2287. const char* hint, char* identity, unsigned int id_max_len,
  2288. unsigned char* key, unsigned int key_max_len, const char* ciphersuite)
  2289. {
  2290. int i;
  2291. int b = 0x01;
  2292. (void)ssl;
  2293. (void)hint;
  2294. (void)key_max_len;
  2295. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  2296. /* Multiple calls for each cipher suite. First identity byte indicates the
  2297. * number of identites seen so far for cipher suite. */
  2298. if (identity[0] != 0) {
  2299. return 0;
  2300. }
  2301. #endif
  2302. /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */
  2303. XSTRNCPY(identity, kIdentityStr, id_max_len);
  2304. XSTRNCAT(identity, ciphersuite + XSTRLEN(ciphersuite) - 6, id_max_len);
  2305. for (i = 0; i < 32; i++, b += 0x22) {
  2306. if (b >= 0x100)
  2307. b = 0x01;
  2308. key[i] = b;
  2309. }
  2310. return 32; /* length of key in octets or 0 for error */
  2311. }
  2312. #endif /* !NO_PSK */
  2313. #if defined(WOLFSSL_USER_CURRTIME)
  2314. extern double current_time(int reset);
  2315. #elif defined(USE_WINDOWS_API)
  2316. #define WIN32_LEAN_AND_MEAN
  2317. #include <windows.h>
  2318. static WC_INLINE double current_time(int reset)
  2319. {
  2320. static int init = 0;
  2321. static LARGE_INTEGER freq;
  2322. LARGE_INTEGER count;
  2323. if (!init) {
  2324. QueryPerformanceFrequency(&freq);
  2325. init = 1;
  2326. }
  2327. QueryPerformanceCounter(&count);
  2328. (void)reset;
  2329. return (double)count.QuadPart / freq.QuadPart;
  2330. }
  2331. #elif defined(WOLFSSL_TIRTOS)
  2332. extern double current_time();
  2333. #elif defined(WOLFSSL_ZEPHYR)
  2334. extern double current_time();
  2335. #else
  2336. #if !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_TCP_NET) && !defined(WOLFSSL_CHIBIOS)
  2337. #ifndef NETOS
  2338. #include <sys/time.h>
  2339. #endif
  2340. static WC_INLINE double current_time(int reset)
  2341. {
  2342. struct timeval tv;
  2343. if (gettimeofday(&tv, NULL) < 0)
  2344. err_sys_with_errno("gettimeofday");
  2345. (void)reset;
  2346. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
  2347. }
  2348. #else
  2349. extern double current_time(int reset);
  2350. #endif
  2351. #endif /* USE_WINDOWS_API */
  2352. #ifdef WOLFSSL_CALLBACKS
  2353. /* only for debug use! */
  2354. static WC_INLINE void msgDebugCb(int write_p, int version, int content_type,
  2355. const void *buf, size_t len, WOLFSSL *ssl, void *arg)
  2356. {
  2357. size_t z;
  2358. byte* pt;
  2359. printf("Version %02X, content type = %d\n", version, content_type);
  2360. printf("%s ", (write_p)? "WRITING" : "READING");
  2361. pt = (byte*)buf;
  2362. printf("DATA [%zu]: ", len);
  2363. for (z = 0; z < len; z++)
  2364. printf("%02X", pt[z]);
  2365. printf("\n");
  2366. (void)arg;
  2367. (void)ssl;
  2368. }
  2369. #endif /* WOLFSSL_CALLBACKS */
  2370. #if defined(HAVE_OCSP) && defined(WOLFSSL_NONBLOCK_OCSP)
  2371. static WC_INLINE int OCSPIOCb(void* ioCtx, const char* url, int urlSz,
  2372. unsigned char* request, int requestSz, unsigned char** response)
  2373. {
  2374. #ifdef TEST_NONBLOCK_CERTS
  2375. static int ioCbCnt = 0;
  2376. #endif
  2377. (void)ioCtx;
  2378. (void)url;
  2379. (void)urlSz;
  2380. (void)request;
  2381. (void)requestSz;
  2382. (void)response;
  2383. #ifdef TEST_NONBLOCK_CERTS
  2384. if (ioCbCnt) {
  2385. ioCbCnt = 0;
  2386. return EmbedOcspLookup(ioCtx, url, urlSz, request, requestSz, response);
  2387. }
  2388. else {
  2389. ioCbCnt = 1;
  2390. return WOLFSSL_CBIO_ERR_WANT_READ;
  2391. }
  2392. #else
  2393. return EmbedOcspLookup(ioCtx, url, urlSz, request, requestSz, response);
  2394. #endif
  2395. }
  2396. static WC_INLINE void OCSPRespFreeCb(void* ioCtx, unsigned char* response)
  2397. {
  2398. EmbedOcspRespFree(ioCtx, response);
  2399. }
  2400. #endif
  2401. #if !defined(NO_CERTS)
  2402. #if !defined(NO_FILESYSTEM) || \
  2403. (defined(NO_FILESYSTEM) && defined(FORCE_BUFFER_TEST)) && \
  2404. !defined(NETOS)
  2405. /* reads file size, allocates buffer, reads into buffer, returns buffer */
  2406. static WC_INLINE int load_file(const char* fname, byte** buf, size_t* bufLen)
  2407. {
  2408. int ret;
  2409. long int fileSz;
  2410. XFILE lFile;
  2411. if (fname == NULL || buf == NULL || bufLen == NULL)
  2412. return BAD_FUNC_ARG;
  2413. /* set defaults */
  2414. *buf = NULL;
  2415. *bufLen = 0;
  2416. /* open file (read-only binary) */
  2417. lFile = XFOPEN(fname, "rb");
  2418. if (!lFile) {
  2419. fprintf(stderr, "Error loading %s\n", fname);
  2420. return BAD_PATH_ERROR;
  2421. }
  2422. LIBCALL_CHECK_RET(XFSEEK(lFile, 0, XSEEK_END));
  2423. fileSz = (int)ftell(lFile);
  2424. LIBCALL_CHECK_RET(XFSEEK(lFile, 0, XSEEK_SET));
  2425. if (fileSz > 0) {
  2426. *bufLen = (size_t)fileSz;
  2427. *buf = (byte*)malloc(*bufLen);
  2428. if (*buf == NULL) {
  2429. ret = MEMORY_E;
  2430. fprintf(stderr,
  2431. "Error allocating %lu bytes\n", (unsigned long)*bufLen);
  2432. }
  2433. else {
  2434. size_t readLen = fread(*buf, *bufLen, 1, lFile);
  2435. /* check response code */
  2436. ret = (readLen > 0) ? 0 : -1;
  2437. }
  2438. }
  2439. else {
  2440. ret = BUFFER_E;
  2441. }
  2442. fclose(lFile);
  2443. return ret;
  2444. }
  2445. enum {
  2446. WOLFSSL_CA = 1,
  2447. WOLFSSL_CERT = 2,
  2448. WOLFSSL_KEY = 3,
  2449. WOLFSSL_CERT_CHAIN = 4,
  2450. };
  2451. static WC_INLINE void load_buffer(WOLFSSL_CTX* ctx, const char* fname, int type)
  2452. {
  2453. int format = WOLFSSL_FILETYPE_PEM;
  2454. byte* buff = NULL;
  2455. size_t sz = 0;
  2456. if (load_file(fname, &buff, &sz) != 0) {
  2457. err_sys("can't open file for buffer load "
  2458. "Please run from wolfSSL home directory if not");
  2459. }
  2460. /* determine format */
  2461. if (strstr(fname, ".der"))
  2462. format = WOLFSSL_FILETYPE_ASN1;
  2463. if (type == WOLFSSL_CA) {
  2464. if (wolfSSL_CTX_load_verify_buffer(ctx, buff, (long)sz, format)
  2465. != WOLFSSL_SUCCESS)
  2466. err_sys("can't load buffer ca file");
  2467. }
  2468. else if (type == WOLFSSL_CERT) {
  2469. if (wolfSSL_CTX_use_certificate_buffer(ctx, buff, (long)sz,
  2470. format) != WOLFSSL_SUCCESS)
  2471. err_sys("can't load buffer cert file");
  2472. }
  2473. else if (type == WOLFSSL_KEY) {
  2474. if (wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, (long)sz,
  2475. format) != WOLFSSL_SUCCESS)
  2476. err_sys("can't load buffer key file");
  2477. }
  2478. else if (type == WOLFSSL_CERT_CHAIN) {
  2479. if (wolfSSL_CTX_use_certificate_chain_buffer_format(ctx, buff,
  2480. (long)sz, format) != WOLFSSL_SUCCESS)
  2481. err_sys("can't load cert chain buffer");
  2482. }
  2483. if (buff)
  2484. free(buff);
  2485. }
  2486. static WC_INLINE void load_ssl_buffer(WOLFSSL* ssl, const char* fname, int type)
  2487. {
  2488. int format = WOLFSSL_FILETYPE_PEM;
  2489. byte* buff = NULL;
  2490. size_t sz = 0;
  2491. if (load_file(fname, &buff, &sz) != 0) {
  2492. err_sys("can't open file for buffer load "
  2493. "Please run from wolfSSL home directory if not");
  2494. }
  2495. /* determine format */
  2496. if (strstr(fname, ".der"))
  2497. format = WOLFSSL_FILETYPE_ASN1;
  2498. if (type == WOLFSSL_CA) {
  2499. /* verify certs (CA's) use the shared ctx->cm (WOLFSSL_CERT_MANAGER) */
  2500. WOLFSSL_CTX* ctx = wolfSSL_get_SSL_CTX(ssl);
  2501. if (wolfSSL_CTX_load_verify_buffer(ctx, buff, (long)sz, format)
  2502. != WOLFSSL_SUCCESS)
  2503. err_sys("can't load buffer ca file");
  2504. }
  2505. else if (type == WOLFSSL_CERT) {
  2506. if (wolfSSL_use_certificate_buffer(ssl, buff, (long)sz,
  2507. format) != WOLFSSL_SUCCESS)
  2508. err_sys("can't load buffer cert file");
  2509. }
  2510. else if (type == WOLFSSL_KEY) {
  2511. if (wolfSSL_use_PrivateKey_buffer(ssl, buff, (long)sz,
  2512. format) != WOLFSSL_SUCCESS)
  2513. err_sys("can't load buffer key file");
  2514. }
  2515. else if (type == WOLFSSL_CERT_CHAIN) {
  2516. if (wolfSSL_use_certificate_chain_buffer_format(ssl, buff,
  2517. (long)sz, format) != WOLFSSL_SUCCESS)
  2518. err_sys("can't load cert chain buffer");
  2519. }
  2520. if (buff)
  2521. free(buff);
  2522. }
  2523. #ifdef TEST_PK_PRIVKEY
  2524. static WC_INLINE int load_key_file(const char* fname, byte** derBuf, word32* derLen)
  2525. {
  2526. int ret;
  2527. byte* buf = NULL;
  2528. size_t bufLen;
  2529. ret = load_file(fname, &buf, &bufLen);
  2530. if (ret != 0)
  2531. return ret;
  2532. *derBuf = (byte*)malloc(bufLen);
  2533. if (*derBuf == NULL) {
  2534. free(buf);
  2535. return MEMORY_E;
  2536. }
  2537. ret = wc_KeyPemToDer(buf, (word32)bufLen, *derBuf, (word32)bufLen, NULL);
  2538. if (ret < 0) {
  2539. free(buf);
  2540. free(*derBuf);
  2541. return ret;
  2542. }
  2543. *derLen = ret;
  2544. free(buf);
  2545. return 0;
  2546. }
  2547. #endif /* TEST_PK_PRIVKEY */
  2548. #endif /* !NO_FILESYSTEM || (NO_FILESYSTEM && FORCE_BUFFER_TEST) */
  2549. #endif /* !NO_CERTS */
  2550. enum {
  2551. VERIFY_OVERRIDE_ERROR,
  2552. VERIFY_FORCE_FAIL,
  2553. VERIFY_USE_PREVERFIY,
  2554. VERIFY_OVERRIDE_DATE_ERR,
  2555. };
  2556. static THREAD_LS_T int myVerifyAction = VERIFY_OVERRIDE_ERROR;
  2557. /* The verify callback is called for every certificate only when
  2558. * --enable-opensslextra is defined because it sets WOLFSSL_ALWAYS_VERIFY_CB and
  2559. * WOLFSSL_VERIFY_CB_ALL_CERTS.
  2560. * Normal cases of the verify callback only occur on certificate failures when the
  2561. * wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, myVerify); is called
  2562. */
  2563. static WC_INLINE int myVerify(int preverify, WOLFSSL_X509_STORE_CTX* store)
  2564. {
  2565. char buffer[WOLFSSL_MAX_ERROR_SZ];
  2566. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  2567. WOLFSSL_X509* peer;
  2568. #if defined(SHOW_CERTS) && !defined(NO_FILESYSTEM) && \
  2569. !defined(OPENSSL_EXTRA_X509_SMALL)
  2570. WOLFSSL_BIO* bio = NULL;
  2571. WOLFSSL_STACK* sk = NULL;
  2572. X509* x509 = NULL;
  2573. #endif
  2574. #endif
  2575. /* Verify Callback Arguments:
  2576. * preverify: 1=Verify Okay, 0=Failure
  2577. * store->error: Failure error code (0 indicates no failure)
  2578. * store->current_cert: Current WOLFSSL_X509 object (only with OPENSSL_EXTRA)
  2579. * store->error_depth: Current Index
  2580. * store->domain: Subject CN as string (null term)
  2581. * store->totalCerts: Number of certs presented by peer
  2582. * store->certs[i]: A `WOLFSSL_BUFFER_INFO` with plain DER for each cert
  2583. * store->store: WOLFSSL_X509_STORE with CA cert chain
  2584. * store->store->cm: WOLFSSL_CERT_MANAGER
  2585. * store->ex_data: The WOLFSSL object pointer
  2586. * store->discardSessionCerts: When set to non-zero value session certs
  2587. will be discarded (only with SESSION_CERTS)
  2588. */
  2589. fprintf(stderr, "In verification callback, error = %d, %s\n", store->error,
  2590. wolfSSL_ERR_error_string(store->error, buffer));
  2591. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  2592. peer = store->current_cert;
  2593. if (peer) {
  2594. char* issuer = wolfSSL_X509_NAME_oneline(
  2595. wolfSSL_X509_get_issuer_name(peer), 0, 0);
  2596. char* subject = wolfSSL_X509_NAME_oneline(
  2597. wolfSSL_X509_get_subject_name(peer), 0, 0);
  2598. printf("\tPeer's cert info:\n issuer : %s\n subject: %s\n",
  2599. issuer ? issuer : "[none]",
  2600. subject ? subject : "[none]");
  2601. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  2602. if (issuer != NULL && subject != NULL) {
  2603. /* preverify needs to be self-signer error for Qt compat.
  2604. * Should be ASN_SELF_SIGNED_E */
  2605. if (XSTRCMP(issuer, subject) == 0 && preverify == ASN_NO_SIGNER_E)
  2606. return 0;
  2607. }
  2608. #endif
  2609. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  2610. XFREE(issuer, 0, DYNAMIC_TYPE_OPENSSL);
  2611. #if defined(SHOW_CERTS) && !defined(NO_FILESYSTEM) && \
  2612. !defined(OPENSSL_EXTRA_X509_SMALL)
  2613. /* avoid printing duplicate certs */
  2614. if (store->depth == 1) {
  2615. int i;
  2616. /* retrieve x509 certs and display them on stdout */
  2617. sk = wolfSSL_X509_STORE_GetCerts(store);
  2618. for (i = 0; i < wolfSSL_sk_X509_num(sk); i++) {
  2619. x509 = wolfSSL_sk_X509_value(sk, i);
  2620. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  2621. if (bio != NULL) {
  2622. wolfSSL_BIO_set_fp(bio, stdout, BIO_NOCLOSE);
  2623. wolfSSL_X509_print(bio, x509);
  2624. wolfSSL_BIO_free(bio);
  2625. }
  2626. }
  2627. wolfSSL_sk_X509_pop_free(sk, NULL);
  2628. }
  2629. #endif
  2630. }
  2631. else
  2632. fprintf(stderr, "\tPeer has no cert!\n");
  2633. #else
  2634. printf("\tPeer certs: %d\n", store->totalCerts);
  2635. #ifdef SHOW_CERTS
  2636. { int i;
  2637. for (i=0; i<store->totalCerts; i++) {
  2638. WOLFSSL_BUFFER_INFO* cert = &store->certs[i];
  2639. printf("\t\tCert %d: Ptr %p, Len %u\n", i, cert->buffer, cert->length);
  2640. }
  2641. }
  2642. #endif /* SHOW_CERTS */
  2643. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  2644. printf("\tSubject's domain name at %d is %s\n", store->error_depth, store->domain);
  2645. /* Testing forced fail case by return zero */
  2646. if (myVerifyAction == VERIFY_FORCE_FAIL) {
  2647. return 0; /* test failure case */
  2648. }
  2649. if (myVerifyAction == VERIFY_OVERRIDE_DATE_ERR &&
  2650. (store->error == ASN_BEFORE_DATE_E || store->error == ASN_AFTER_DATE_E)) {
  2651. printf("Overriding cert date error as example for bad clock testing\n");
  2652. return 1;
  2653. }
  2654. /* If error indicate we are overriding it for testing purposes */
  2655. if (store->error != 0 && myVerifyAction == VERIFY_OVERRIDE_ERROR) {
  2656. printf("\tAllowing failed certificate check, testing only "
  2657. "(shouldn't do this in production)\n");
  2658. }
  2659. /* A non-zero return code indicates failure override */
  2660. return (myVerifyAction == VERIFY_OVERRIDE_ERROR) ? 1 : preverify;
  2661. }
  2662. #ifdef HAVE_EXT_CACHE
  2663. static WC_INLINE WOLFSSL_SESSION* mySessGetCb(WOLFSSL* ssl,
  2664. const unsigned char* id, int id_len, int* copy)
  2665. {
  2666. (void)ssl;
  2667. (void)id;
  2668. (void)id_len;
  2669. (void)copy;
  2670. /* using internal cache, this is for testing only */
  2671. return NULL;
  2672. }
  2673. static WC_INLINE int mySessNewCb(WOLFSSL* ssl, WOLFSSL_SESSION* session)
  2674. {
  2675. (void)ssl;
  2676. (void)session;
  2677. /* using internal cache, this is for testing only */
  2678. return 0;
  2679. }
  2680. static WC_INLINE void mySessRemCb(WOLFSSL_CTX* ctx, WOLFSSL_SESSION* session)
  2681. {
  2682. (void)ctx;
  2683. (void)session;
  2684. /* using internal cache, this is for testing only */
  2685. }
  2686. #endif /* HAVE_EXT_CACHE */
  2687. #ifdef HAVE_CRL
  2688. static WC_INLINE void CRL_CallBack(const char* url)
  2689. {
  2690. printf("CRL callback url = %s\n", url);
  2691. }
  2692. #endif
  2693. #ifndef NO_DH
  2694. static WC_INLINE void SetDH(WOLFSSL* ssl)
  2695. {
  2696. /* dh1024 p */
  2697. static const unsigned char p[] =
  2698. {
  2699. 0xE6, 0x96, 0x9D, 0x3D, 0x49, 0x5B, 0xE3, 0x2C, 0x7C, 0xF1, 0x80, 0xC3,
  2700. 0xBD, 0xD4, 0x79, 0x8E, 0x91, 0xB7, 0x81, 0x82, 0x51, 0xBB, 0x05, 0x5E,
  2701. 0x2A, 0x20, 0x64, 0x90, 0x4A, 0x79, 0xA7, 0x70, 0xFA, 0x15, 0xA2, 0x59,
  2702. 0xCB, 0xD5, 0x23, 0xA6, 0xA6, 0xEF, 0x09, 0xC4, 0x30, 0x48, 0xD5, 0xA2,
  2703. 0x2F, 0x97, 0x1F, 0x3C, 0x20, 0x12, 0x9B, 0x48, 0x00, 0x0E, 0x6E, 0xDD,
  2704. 0x06, 0x1C, 0xBC, 0x05, 0x3E, 0x37, 0x1D, 0x79, 0x4E, 0x53, 0x27, 0xDF,
  2705. 0x61, 0x1E, 0xBB, 0xBE, 0x1B, 0xAC, 0x9B, 0x5C, 0x60, 0x44, 0xCF, 0x02,
  2706. 0x3D, 0x76, 0xE0, 0x5E, 0xEA, 0x9B, 0xAD, 0x99, 0x1B, 0x13, 0xA6, 0x3C,
  2707. 0x97, 0x4E, 0x9E, 0xF1, 0x83, 0x9E, 0xB5, 0xDB, 0x12, 0x51, 0x36, 0xF7,
  2708. 0x26, 0x2E, 0x56, 0xA8, 0x87, 0x15, 0x38, 0xDF, 0xD8, 0x23, 0xC6, 0x50,
  2709. 0x50, 0x85, 0xE2, 0x1F, 0x0D, 0xD5, 0xC8, 0x6B,
  2710. };
  2711. /* dh1024 g */
  2712. static const unsigned char g[] =
  2713. {
  2714. 0x02,
  2715. };
  2716. wolfSSL_SetTmpDH(ssl, p, sizeof(p), g, sizeof(g));
  2717. }
  2718. static WC_INLINE void SetDHCtx(WOLFSSL_CTX* ctx)
  2719. {
  2720. /* dh1024 p */
  2721. static const unsigned char p[] =
  2722. {
  2723. 0xE6, 0x96, 0x9D, 0x3D, 0x49, 0x5B, 0xE3, 0x2C, 0x7C, 0xF1, 0x80, 0xC3,
  2724. 0xBD, 0xD4, 0x79, 0x8E, 0x91, 0xB7, 0x81, 0x82, 0x51, 0xBB, 0x05, 0x5E,
  2725. 0x2A, 0x20, 0x64, 0x90, 0x4A, 0x79, 0xA7, 0x70, 0xFA, 0x15, 0xA2, 0x59,
  2726. 0xCB, 0xD5, 0x23, 0xA6, 0xA6, 0xEF, 0x09, 0xC4, 0x30, 0x48, 0xD5, 0xA2,
  2727. 0x2F, 0x97, 0x1F, 0x3C, 0x20, 0x12, 0x9B, 0x48, 0x00, 0x0E, 0x6E, 0xDD,
  2728. 0x06, 0x1C, 0xBC, 0x05, 0x3E, 0x37, 0x1D, 0x79, 0x4E, 0x53, 0x27, 0xDF,
  2729. 0x61, 0x1E, 0xBB, 0xBE, 0x1B, 0xAC, 0x9B, 0x5C, 0x60, 0x44, 0xCF, 0x02,
  2730. 0x3D, 0x76, 0xE0, 0x5E, 0xEA, 0x9B, 0xAD, 0x99, 0x1B, 0x13, 0xA6, 0x3C,
  2731. 0x97, 0x4E, 0x9E, 0xF1, 0x83, 0x9E, 0xB5, 0xDB, 0x12, 0x51, 0x36, 0xF7,
  2732. 0x26, 0x2E, 0x56, 0xA8, 0x87, 0x15, 0x38, 0xDF, 0xD8, 0x23, 0xC6, 0x50,
  2733. 0x50, 0x85, 0xE2, 0x1F, 0x0D, 0xD5, 0xC8, 0x6B,
  2734. };
  2735. /* dh1024 g */
  2736. static const unsigned char g[] =
  2737. {
  2738. 0x02,
  2739. };
  2740. wolfSSL_CTX_SetTmpDH(ctx, p, sizeof(p), g, sizeof(g));
  2741. }
  2742. #endif /* NO_DH */
  2743. #ifndef NO_CERTS
  2744. static WC_INLINE void CaCb(unsigned char* der, int sz, int type)
  2745. {
  2746. (void)der;
  2747. printf("Got CA cache add callback, derSz = %d, type = %d\n", sz, type);
  2748. }
  2749. #endif /* !NO_CERTS */
  2750. /* Wolf Root Directory Helper */
  2751. /* KEIL-RL File System does not support relative directory */
  2752. #if !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_FS) && !defined(WOLFSSL_TIRTOS)
  2753. /* Maximum depth to search for WolfSSL root */
  2754. #define MAX_WOLF_ROOT_DEPTH 5
  2755. static WC_INLINE int ChangeToWolfRoot(void)
  2756. {
  2757. #if !defined(NO_FILESYSTEM) || defined(FORCE_BUFFER_TEST) && \
  2758. !defined(NETOS)
  2759. int depth;
  2760. for(depth = 0; depth <= MAX_WOLF_ROOT_DEPTH; depth++) {
  2761. int res;
  2762. XFILE keyFile = XFOPEN(dhParamFile, "rb");
  2763. if (keyFile != NULL) {
  2764. fclose(keyFile);
  2765. return depth;
  2766. }
  2767. #ifdef USE_WINDOWS_API
  2768. res = SetCurrentDirectoryA("..\\");
  2769. #elif defined(NETOS)
  2770. return 0;
  2771. #else
  2772. res = chdir("../");
  2773. #endif
  2774. if (res < 0) {
  2775. printf("chdir to ../ failed!\n");
  2776. break;
  2777. }
  2778. }
  2779. err_sys("wolf root not found");
  2780. return -1;
  2781. #else
  2782. return 0;
  2783. #endif
  2784. }
  2785. #endif /* !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_FS) && !defined(WOLFSSL_TIRTOS) */
  2786. #if defined(ATOMIC_USER) && !defined(WOLFSSL_AEAD_ONLY)
  2787. /* Atomic Encrypt Context example */
  2788. typedef struct AtomicEncCtx {
  2789. int keySetup; /* have we done key setup yet */
  2790. Aes aes; /* for aes example */
  2791. } AtomicEncCtx;
  2792. /* Atomic Decrypt Context example */
  2793. typedef struct AtomicDecCtx {
  2794. int keySetup; /* have we done key setup yet */
  2795. Aes aes; /* for aes example */
  2796. } AtomicDecCtx;
  2797. #if !defined(NO_HMAC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  2798. static WC_INLINE int myMacEncryptCb(WOLFSSL* ssl, unsigned char* macOut,
  2799. const unsigned char* macIn, unsigned int macInSz, int macContent,
  2800. int macVerify, unsigned char* encOut, const unsigned char* encIn,
  2801. unsigned int encSz, void* ctx)
  2802. {
  2803. int ret;
  2804. Hmac hmac;
  2805. byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
  2806. AtomicEncCtx* encCtx = (AtomicEncCtx*)ctx;
  2807. const char* tlsStr = "TLS";
  2808. /* example supports (d)tls aes */
  2809. if (wolfSSL_GetBulkCipher(ssl) != wolfssl_aes) {
  2810. printf("myMacEncryptCb not using AES\n");
  2811. return -1;
  2812. }
  2813. if (strstr(wolfSSL_get_version(ssl), tlsStr) == NULL) {
  2814. printf("myMacEncryptCb not using (D)TLS\n");
  2815. return -1;
  2816. }
  2817. /* hmac, not needed if aead mode */
  2818. wolfSSL_SetTlsHmacInner(ssl, myInner, macInSz, macContent, macVerify);
  2819. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  2820. if (ret != 0)
  2821. return ret;
  2822. ret = wc_HmacSetKey(&hmac, wolfSSL_GetHmacType(ssl),
  2823. wolfSSL_GetMacSecret(ssl, macVerify), wolfSSL_GetHmacSize(ssl));
  2824. if (ret != 0)
  2825. return ret;
  2826. ret = wc_HmacUpdate(&hmac, myInner, sizeof(myInner));
  2827. if (ret != 0)
  2828. return ret;
  2829. ret = wc_HmacUpdate(&hmac, macIn, macInSz);
  2830. if (ret != 0)
  2831. return ret;
  2832. ret = wc_HmacFinal(&hmac, macOut);
  2833. if (ret != 0)
  2834. return ret;
  2835. /* encrypt setup on first time */
  2836. if (encCtx->keySetup == 0) {
  2837. int keyLen = wolfSSL_GetKeySize(ssl);
  2838. const byte* key;
  2839. const byte* iv;
  2840. if (wolfSSL_GetSide(ssl) == WOLFSSL_CLIENT_END) {
  2841. key = wolfSSL_GetClientWriteKey(ssl);
  2842. iv = wolfSSL_GetClientWriteIV(ssl);
  2843. }
  2844. else {
  2845. key = wolfSSL_GetServerWriteKey(ssl);
  2846. iv = wolfSSL_GetServerWriteIV(ssl);
  2847. }
  2848. ret = wc_AesSetKey(&encCtx->aes, key, keyLen, iv, AES_ENCRYPTION);
  2849. if (ret != 0) {
  2850. fprintf(stderr, "AesSetKey failed in myMacEncryptCb\n");
  2851. return ret;
  2852. }
  2853. encCtx->keySetup = 1;
  2854. }
  2855. /* encrypt */
  2856. return wc_AesCbcEncrypt(&encCtx->aes, encOut, encIn, encSz);
  2857. }
  2858. static WC_INLINE int myDecryptVerifyCb(WOLFSSL* ssl,
  2859. unsigned char* decOut, const unsigned char* decIn,
  2860. unsigned int decSz, int macContent, int macVerify,
  2861. unsigned int* padSz, void* ctx)
  2862. {
  2863. AtomicDecCtx* decCtx = (AtomicDecCtx*)ctx;
  2864. int ret = 0;
  2865. int macInSz = 0;
  2866. int ivExtra = 0;
  2867. int digestSz = wolfSSL_GetHmacSize(ssl);
  2868. unsigned int pad = 0;
  2869. unsigned int padByte = 0;
  2870. Hmac hmac;
  2871. byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
  2872. byte verify[WC_MAX_DIGEST_SIZE];
  2873. const char* tlsStr = "TLS";
  2874. /* example supports (d)tls aes */
  2875. if (wolfSSL_GetBulkCipher(ssl) != wolfssl_aes) {
  2876. printf("myMacEncryptCb not using AES\n");
  2877. return -1;
  2878. }
  2879. if (strstr(wolfSSL_get_version(ssl), tlsStr) == NULL) {
  2880. printf("myMacEncryptCb not using (D)TLS\n");
  2881. return -1;
  2882. }
  2883. /*decrypt */
  2884. if (decCtx->keySetup == 0) {
  2885. int keyLen = wolfSSL_GetKeySize(ssl);
  2886. const byte* key;
  2887. const byte* iv;
  2888. /* decrypt is from other side (peer) */
  2889. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  2890. key = wolfSSL_GetClientWriteKey(ssl);
  2891. iv = wolfSSL_GetClientWriteIV(ssl);
  2892. }
  2893. else {
  2894. key = wolfSSL_GetServerWriteKey(ssl);
  2895. iv = wolfSSL_GetServerWriteIV(ssl);
  2896. }
  2897. ret = wc_AesSetKey(&decCtx->aes, key, keyLen, iv, AES_DECRYPTION);
  2898. if (ret != 0) {
  2899. fprintf(stderr, "AesSetKey failed in myDecryptVerifyCb\n");
  2900. return ret;
  2901. }
  2902. decCtx->keySetup = 1;
  2903. }
  2904. /* decrypt */
  2905. ret = wc_AesCbcDecrypt(&decCtx->aes, decOut, decIn, decSz);
  2906. if (ret != 0)
  2907. return ret;
  2908. if (wolfSSL_GetCipherType(ssl) == WOLFSSL_AEAD_TYPE) {
  2909. *padSz = wolfSSL_GetAeadMacSize(ssl);
  2910. return 0; /* hmac, not needed if aead mode */
  2911. }
  2912. if (wolfSSL_GetCipherType(ssl) == WOLFSSL_BLOCK_TYPE) {
  2913. pad = *(decOut + decSz - 1);
  2914. padByte = 1;
  2915. if (wolfSSL_IsTLSv1_1(ssl))
  2916. ivExtra = wolfSSL_GetCipherBlockSize(ssl);
  2917. }
  2918. *padSz = wolfSSL_GetHmacSize(ssl) + pad + padByte;
  2919. macInSz = decSz - ivExtra - digestSz - pad - padByte;
  2920. wolfSSL_SetTlsHmacInner(ssl, myInner, macInSz, macContent, macVerify);
  2921. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  2922. if (ret != 0)
  2923. return ret;
  2924. ret = wc_HmacSetKey(&hmac, wolfSSL_GetHmacType(ssl),
  2925. wolfSSL_GetMacSecret(ssl, macVerify), digestSz);
  2926. if (ret != 0)
  2927. return ret;
  2928. ret = wc_HmacUpdate(&hmac, myInner, sizeof(myInner));
  2929. if (ret != 0)
  2930. return ret;
  2931. ret = wc_HmacUpdate(&hmac, decOut + ivExtra, macInSz);
  2932. if (ret != 0)
  2933. return ret;
  2934. ret = wc_HmacFinal(&hmac, verify);
  2935. if (ret != 0)
  2936. return ret;
  2937. if (XMEMCMP(verify, decOut + decSz - digestSz - pad - padByte,
  2938. digestSz) != 0) {
  2939. printf("myDecryptVerify verify failed\n");
  2940. return -1;
  2941. }
  2942. return ret;
  2943. }
  2944. #ifdef HAVE_ENCRYPT_THEN_MAC
  2945. static WC_INLINE int myEncryptMacCb(WOLFSSL* ssl, unsigned char* macOut,
  2946. int content, int macVerify, unsigned char* encOut,
  2947. const unsigned char* encIn, unsigned int encSz, void* ctx)
  2948. {
  2949. int ret;
  2950. Hmac hmac;
  2951. AtomicEncCtx* encCtx = (AtomicEncCtx*)ctx;
  2952. byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
  2953. const char* tlsStr = "TLS";
  2954. /* example supports (d)tls aes */
  2955. if (wolfSSL_GetBulkCipher(ssl) != wolfssl_aes) {
  2956. printf("myMacEncryptCb not using AES\n");
  2957. return -1;
  2958. }
  2959. if (strstr(wolfSSL_get_version(ssl), tlsStr) == NULL) {
  2960. printf("myMacEncryptCb not using (D)TLS\n");
  2961. return -1;
  2962. }
  2963. /* encrypt setup on first time */
  2964. if (encCtx->keySetup == 0) {
  2965. int keyLen = wolfSSL_GetKeySize(ssl);
  2966. const byte* key;
  2967. const byte* iv;
  2968. if (wolfSSL_GetSide(ssl) == WOLFSSL_CLIENT_END) {
  2969. key = wolfSSL_GetClientWriteKey(ssl);
  2970. iv = wolfSSL_GetClientWriteIV(ssl);
  2971. }
  2972. else {
  2973. key = wolfSSL_GetServerWriteKey(ssl);
  2974. iv = wolfSSL_GetServerWriteIV(ssl);
  2975. }
  2976. ret = wc_AesSetKey(&encCtx->aes, key, keyLen, iv, AES_ENCRYPTION);
  2977. if (ret != 0) {
  2978. fprintf(stderr, "AesSetKey failed in myMacEncryptCb\n");
  2979. return ret;
  2980. }
  2981. encCtx->keySetup = 1;
  2982. }
  2983. /* encrypt */
  2984. ret = wc_AesCbcEncrypt(&encCtx->aes, encOut, encIn, encSz);
  2985. if (ret != 0)
  2986. return ret;
  2987. /* Reconstruct record header. */
  2988. wolfSSL_SetTlsHmacInner(ssl, myInner, encSz, content, macVerify);
  2989. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  2990. if (ret != 0)
  2991. return ret;
  2992. ret = wc_HmacSetKey(&hmac, wolfSSL_GetHmacType(ssl),
  2993. wolfSSL_GetMacSecret(ssl, macVerify), wolfSSL_GetHmacSize(ssl));
  2994. if (ret != 0)
  2995. return ret;
  2996. ret = wc_HmacUpdate(&hmac, myInner, sizeof(myInner));
  2997. if (ret != 0)
  2998. return ret;
  2999. ret = wc_HmacUpdate(&hmac, encOut, encSz);
  3000. if (ret != 0)
  3001. return ret;
  3002. return wc_HmacFinal(&hmac, macOut);
  3003. }
  3004. static WC_INLINE int myVerifyDecryptCb(WOLFSSL* ssl,
  3005. unsigned char* decOut, const unsigned char* decIn,
  3006. unsigned int decSz, int content, int macVerify,
  3007. unsigned int* padSz, void* ctx)
  3008. {
  3009. AtomicDecCtx* decCtx = (AtomicDecCtx*)ctx;
  3010. int ret = 0;
  3011. int digestSz = wolfSSL_GetHmacSize(ssl);
  3012. Hmac hmac;
  3013. byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
  3014. byte verify[WC_MAX_DIGEST_SIZE];
  3015. const char* tlsStr = "TLS";
  3016. /* example supports (d)tls aes */
  3017. if (wolfSSL_GetBulkCipher(ssl) != wolfssl_aes) {
  3018. printf("myMacEncryptCb not using AES\n");
  3019. return -1;
  3020. }
  3021. if (strstr(wolfSSL_get_version(ssl), tlsStr) == NULL) {
  3022. printf("myMacEncryptCb not using (D)TLS\n");
  3023. return -1;
  3024. }
  3025. /* Reconstruct record header. */
  3026. wolfSSL_SetTlsHmacInner(ssl, myInner, decSz, content, macVerify);
  3027. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  3028. if (ret != 0)
  3029. return ret;
  3030. ret = wc_HmacSetKey(&hmac, wolfSSL_GetHmacType(ssl),
  3031. wolfSSL_GetMacSecret(ssl, macVerify), digestSz);
  3032. if (ret != 0)
  3033. return ret;
  3034. ret = wc_HmacUpdate(&hmac, myInner, sizeof(myInner));
  3035. if (ret != 0)
  3036. return ret;
  3037. ret = wc_HmacUpdate(&hmac, decIn, decSz);
  3038. if (ret != 0)
  3039. return ret;
  3040. ret = wc_HmacFinal(&hmac, verify);
  3041. if (ret != 0)
  3042. return ret;
  3043. if (XMEMCMP(verify, decOut + decSz, digestSz) != 0) {
  3044. printf("myDecryptVerify verify failed\n");
  3045. return -1;
  3046. }
  3047. /* decrypt */
  3048. if (decCtx->keySetup == 0) {
  3049. int keyLen = wolfSSL_GetKeySize(ssl);
  3050. const byte* key;
  3051. const byte* iv;
  3052. /* decrypt is from other side (peer) */
  3053. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  3054. key = wolfSSL_GetClientWriteKey(ssl);
  3055. iv = wolfSSL_GetClientWriteIV(ssl);
  3056. }
  3057. else {
  3058. key = wolfSSL_GetServerWriteKey(ssl);
  3059. iv = wolfSSL_GetServerWriteIV(ssl);
  3060. }
  3061. ret = wc_AesSetKey(&decCtx->aes, key, keyLen, iv, AES_DECRYPTION);
  3062. if (ret != 0) {
  3063. fprintf(stderr, "AesSetKey failed in myDecryptVerifyCb\n");
  3064. return ret;
  3065. }
  3066. decCtx->keySetup = 1;
  3067. }
  3068. /* decrypt */
  3069. ret = wc_AesCbcDecrypt(&decCtx->aes, decOut, decIn, decSz);
  3070. if (ret != 0)
  3071. return ret;
  3072. *padSz = *(decOut + decSz - 1) + 1;
  3073. return 0;
  3074. }
  3075. #endif /* HAVE_ENCRYPT_THEN_MAC */
  3076. #endif /* !NO_HMAC && !NO_AES && HAVE_AES_CBC */
  3077. static WC_INLINE void SetupAtomicUser(WOLFSSL_CTX* ctx, WOLFSSL* ssl)
  3078. {
  3079. AtomicEncCtx* encCtx;
  3080. AtomicDecCtx* decCtx;
  3081. encCtx = (AtomicEncCtx*)malloc(sizeof(AtomicEncCtx));
  3082. if (encCtx == NULL)
  3083. err_sys_with_errno("AtomicEncCtx malloc failed");
  3084. XMEMSET(encCtx, 0, sizeof(AtomicEncCtx));
  3085. decCtx = (AtomicDecCtx*)malloc(sizeof(AtomicDecCtx));
  3086. if (decCtx == NULL) {
  3087. free(encCtx);
  3088. err_sys_with_errno("AtomicDecCtx malloc failed");
  3089. }
  3090. XMEMSET(decCtx, 0, sizeof(AtomicDecCtx));
  3091. #if !defined(NO_HMAC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  3092. wolfSSL_CTX_SetMacEncryptCb(ctx, myMacEncryptCb);
  3093. wolfSSL_SetMacEncryptCtx(ssl, encCtx);
  3094. wolfSSL_CTX_SetDecryptVerifyCb(ctx, myDecryptVerifyCb);
  3095. wolfSSL_SetDecryptVerifyCtx(ssl, decCtx);
  3096. #ifdef HAVE_ENCRYPT_THEN_MAC
  3097. wolfSSL_CTX_SetEncryptMacCb(ctx, myEncryptMacCb);
  3098. wolfSSL_SetEncryptMacCtx(ssl, encCtx);
  3099. wolfSSL_CTX_SetVerifyDecryptCb(ctx, myVerifyDecryptCb);
  3100. wolfSSL_SetVerifyDecryptCtx(ssl, decCtx);
  3101. #endif
  3102. #else
  3103. (void)ctx;
  3104. (void)ssl;
  3105. #endif
  3106. }
  3107. static WC_INLINE void FreeAtomicUser(WOLFSSL* ssl)
  3108. {
  3109. AtomicEncCtx* encCtx = (AtomicEncCtx*)wolfSSL_GetMacEncryptCtx(ssl);
  3110. AtomicDecCtx* decCtx = (AtomicDecCtx*)wolfSSL_GetDecryptVerifyCtx(ssl);
  3111. /* Encrypt-Then-MAC callbacks use same contexts. */
  3112. if (encCtx != NULL) {
  3113. if (encCtx->keySetup == 1)
  3114. wc_AesFree(&encCtx->aes);
  3115. free(encCtx);
  3116. }
  3117. if (decCtx != NULL) {
  3118. if (decCtx->keySetup == 1)
  3119. wc_AesFree(&decCtx->aes);
  3120. free(decCtx);
  3121. }
  3122. }
  3123. #endif /* ATOMIC_USER */
  3124. #ifdef WOLFSSL_STATIC_MEMORY
  3125. static WC_INLINE int wolfSSL_PrintStats(WOLFSSL_MEM_STATS* stats)
  3126. {
  3127. word16 i;
  3128. if (stats == NULL) {
  3129. return 0;
  3130. }
  3131. /* print to stderr so is on the same pipe as WOLFSSL_DEBUG */
  3132. fprintf(stderr, "Total mallocs = %d\n", stats->totalAlloc);
  3133. fprintf(stderr, "Total frees = %d\n", stats->totalFr);
  3134. fprintf(stderr, "Current mallocs = %d\n", stats->curAlloc);
  3135. fprintf(stderr, "Available IO = %d\n", stats->avaIO);
  3136. fprintf(stderr, "Max con. handshakes = %d\n", stats->maxHa);
  3137. fprintf(stderr, "Max con. IO = %d\n", stats->maxIO);
  3138. fprintf(stderr, "State of memory blocks: size : available \n");
  3139. for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
  3140. fprintf(stderr, " : %d\t : %d\n", stats->blockSz[i],
  3141. stats->avaBlock[i]);
  3142. }
  3143. return 1;
  3144. }
  3145. static WC_INLINE int wolfSSL_PrintStatsConn(WOLFSSL_MEM_CONN_STATS* stats)
  3146. {
  3147. if (stats == NULL) {
  3148. return 0;
  3149. }
  3150. fprintf(stderr, "peak connection memory = %d\n", stats->peakMem);
  3151. fprintf(stderr, "current memory in use = %d\n", stats->curMem);
  3152. fprintf(stderr, "peak connection allocs = %d\n", stats->peakAlloc);
  3153. fprintf(stderr, "current connection allocs = %d\n",stats->curAlloc);
  3154. fprintf(stderr, "total connection allocs = %d\n", stats->totalAlloc);
  3155. fprintf(stderr, "total connection frees = %d\n\n", stats->totalFr);
  3156. return 1;
  3157. }
  3158. #endif /* WOLFSSL_STATIC_MEMORY */
  3159. #ifdef HAVE_PK_CALLBACKS
  3160. typedef struct PkCbInfo {
  3161. const char* ourKey;
  3162. #ifdef TEST_PK_PRIVKEY
  3163. union {
  3164. #ifdef HAVE_ECC
  3165. /* only ECC PK callback with TLS v1.2 needs this */
  3166. ecc_key ecc;
  3167. #endif
  3168. } keyGen;
  3169. int hasKeyGen;
  3170. #endif
  3171. } PkCbInfo;
  3172. #if defined(DEBUG_PK_CB) || defined(TEST_PK_PRIVKEY)
  3173. #define WOLFSSL_PKMSG(...) printf(__VA_ARGS__)
  3174. #else
  3175. #define WOLFSSL_PKMSG(...) WC_DO_NOTHING
  3176. #endif
  3177. #ifdef HAVE_ECC
  3178. static WC_INLINE int myEccKeyGen(WOLFSSL* ssl, ecc_key* key, word32 keySz,
  3179. int ecc_curve, void* ctx)
  3180. {
  3181. int ret;
  3182. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  3183. ecc_key* new_key;
  3184. #ifdef TEST_PK_PRIVKEY
  3185. new_key = cbInfo ? &cbInfo->keyGen.ecc : key;
  3186. #else
  3187. new_key = key;
  3188. #endif
  3189. (void)ssl;
  3190. (void)cbInfo;
  3191. WOLFSSL_PKMSG("PK ECC KeyGen: keySz %u, Curve ID %d\n", keySz, ecc_curve);
  3192. ret = wc_ecc_init(new_key);
  3193. if (ret == 0) {
  3194. WC_RNG *rng = wolfSSL_GetRNG(ssl);
  3195. /* create new key */
  3196. ret = wc_ecc_make_key_ex(rng, keySz, new_key, ecc_curve);
  3197. #ifdef TEST_PK_PRIVKEY
  3198. if (ret == 0 && new_key != key) {
  3199. byte qx[MAX_ECC_BYTES], qy[MAX_ECC_BYTES];
  3200. word32 qxLen = sizeof(qx), qyLen = sizeof(qy);
  3201. /* extract public portion from new key into `key` arg */
  3202. ret = wc_ecc_export_public_raw(new_key, qx, &qxLen, qy, &qyLen);
  3203. if (ret == 0) {
  3204. /* load public portion only into key */
  3205. ret = wc_ecc_import_unsigned(key, qx, qy, NULL, ecc_curve);
  3206. }
  3207. (void)qxLen;
  3208. (void)qyLen;
  3209. }
  3210. if (ret == 0 && cbInfo != NULL) {
  3211. cbInfo->hasKeyGen = 1;
  3212. }
  3213. #endif
  3214. }
  3215. WOLFSSL_PKMSG("PK ECC KeyGen: ret %d\n", ret);
  3216. return ret;
  3217. }
  3218. static WC_INLINE int myEccSign(WOLFSSL* ssl, const byte* in, word32 inSz,
  3219. byte* out, word32* outSz, const byte* key, word32 keySz, void* ctx)
  3220. {
  3221. int ret;
  3222. word32 idx = 0;
  3223. ecc_key myKey;
  3224. byte* keyBuf = (byte*)key;
  3225. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  3226. (void)ssl;
  3227. (void)cbInfo;
  3228. WOLFSSL_PKMSG("PK ECC Sign: inSz %u, keySz %u\n", inSz, keySz);
  3229. #ifdef TEST_PK_PRIVKEY
  3230. ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz);
  3231. if (ret != 0)
  3232. return ret;
  3233. #endif
  3234. ret = wc_ecc_init(&myKey);
  3235. if (ret == 0) {
  3236. ret = wc_EccPrivateKeyDecode(keyBuf, &idx, &myKey, keySz);
  3237. if (ret == 0) {
  3238. WC_RNG *rng = wolfSSL_GetRNG(ssl);
  3239. WOLFSSL_PKMSG("PK ECC Sign: Curve ID %d\n", myKey.dp->id);
  3240. ret = wc_ecc_sign_hash(in, inSz, out, outSz, rng, &myKey);
  3241. }
  3242. wc_ecc_free(&myKey);
  3243. }
  3244. #ifdef TEST_PK_PRIVKEY
  3245. free(keyBuf);
  3246. #endif
  3247. WOLFSSL_PKMSG("PK ECC Sign: ret %d outSz %u\n", ret, *outSz);
  3248. return ret;
  3249. }
  3250. static WC_INLINE int myEccVerify(WOLFSSL* ssl, const byte* sig, word32 sigSz,
  3251. const byte* hash, word32 hashSz, const byte* key, word32 keySz,
  3252. int* result, void* ctx)
  3253. {
  3254. int ret;
  3255. word32 idx = 0;
  3256. ecc_key myKey;
  3257. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  3258. (void)ssl;
  3259. (void)cbInfo;
  3260. WOLFSSL_PKMSG("PK ECC Verify: sigSz %u, hashSz %u, keySz %u\n", sigSz, hashSz, keySz);
  3261. ret = wc_ecc_init(&myKey);
  3262. if (ret == 0) {
  3263. ret = wc_EccPublicKeyDecode(key, &idx, &myKey, keySz);
  3264. if (ret == 0)
  3265. ret = wc_ecc_verify_hash(sig, sigSz, hash, hashSz, result, &myKey);
  3266. wc_ecc_free(&myKey);
  3267. }
  3268. WOLFSSL_PKMSG("PK ECC Verify: ret %d, result %d\n", ret, *result);
  3269. return ret;
  3270. }
  3271. static WC_INLINE int myEccSharedSecret(WOLFSSL* ssl, ecc_key* otherKey,
  3272. unsigned char* pubKeyDer, unsigned int* pubKeySz,
  3273. unsigned char* out, unsigned int* outlen,
  3274. int side, void* ctx)
  3275. {
  3276. int ret;
  3277. ecc_key* privKey = NULL;
  3278. ecc_key* pubKey = NULL;
  3279. ecc_key tmpKey;
  3280. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  3281. (void)ssl;
  3282. (void)cbInfo;
  3283. WOLFSSL_PKMSG("PK ECC PMS: Side %s, Peer Curve %d\n",
  3284. side == WOLFSSL_CLIENT_END ? "client" : "server", otherKey->dp->id);
  3285. ret = wc_ecc_init(&tmpKey);
  3286. if (ret != 0) {
  3287. return ret;
  3288. }
  3289. /* for client: create and export public key */
  3290. if (side == WOLFSSL_CLIENT_END) {
  3291. #ifdef TEST_PK_PRIVKEY
  3292. privKey = cbInfo ? &cbInfo->keyGen.ecc : &tmpKey;
  3293. #else
  3294. privKey = &tmpKey;
  3295. #endif
  3296. pubKey = otherKey;
  3297. /* TLS v1.2 and older we must generate a key here for the client only.
  3298. * TLS v1.3 calls key gen early with key share */
  3299. if (wolfSSL_GetVersion(ssl) < WOLFSSL_TLSV1_3) {
  3300. ret = myEccKeyGen(ssl, privKey, 0, otherKey->dp->id, ctx);
  3301. if (ret == 0) {
  3302. ret = wc_ecc_export_x963(privKey, pubKeyDer, pubKeySz);
  3303. }
  3304. }
  3305. }
  3306. /* for server: import public key */
  3307. else if (side == WOLFSSL_SERVER_END) {
  3308. #ifdef TEST_PK_PRIVKEY
  3309. privKey = cbInfo ? &cbInfo->keyGen.ecc : otherKey;
  3310. #else
  3311. privKey = otherKey;
  3312. #endif
  3313. pubKey = &tmpKey;
  3314. ret = wc_ecc_import_x963_ex(pubKeyDer, *pubKeySz, pubKey,
  3315. otherKey->dp->id);
  3316. }
  3317. else {
  3318. ret = BAD_FUNC_ARG;
  3319. }
  3320. if (privKey == NULL || pubKey == NULL) {
  3321. ret = BAD_FUNC_ARG;
  3322. }
  3323. #if defined(ECC_TIMING_RESISTANT) && !defined(HAVE_FIPS) && \
  3324. !defined(HAVE_SELFTEST)
  3325. if (ret == 0) {
  3326. ret = wc_ecc_set_rng(privKey, wolfSSL_GetRNG(ssl));
  3327. }
  3328. #endif
  3329. /* generate shared secret and return it */
  3330. if (ret == 0) {
  3331. ret = wc_ecc_shared_secret(privKey, pubKey, out, outlen);
  3332. #ifdef WOLFSSL_ASYNC_CRYPT
  3333. if (ret == WC_PENDING_E) {
  3334. ret = wc_AsyncWait(ret, &privKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  3335. }
  3336. #endif
  3337. }
  3338. #ifdef TEST_PK_PRIVKEY
  3339. if (cbInfo && cbInfo->hasKeyGen) {
  3340. wc_ecc_free(&cbInfo->keyGen.ecc);
  3341. cbInfo->hasKeyGen = 0;
  3342. }
  3343. #endif
  3344. wc_ecc_free(&tmpKey);
  3345. WOLFSSL_PKMSG("PK ECC PMS: ret %d, PubKeySz %u, OutLen %u\n", ret, *pubKeySz, *outlen);
  3346. return ret;
  3347. }
  3348. #endif /* HAVE_ECC */
  3349. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  3350. static WC_INLINE int myHkdfExtract(byte* prk, const byte* salt, word32 saltLen,
  3351. byte* ikm, word32 ikmLen, int digest, void* ctx)
  3352. {
  3353. int ret;
  3354. int len = 0;
  3355. switch (digest) {
  3356. #ifndef NO_SHA256
  3357. case WC_SHA256:
  3358. len = WC_SHA256_DIGEST_SIZE;
  3359. break;
  3360. #endif
  3361. #ifdef WOLFSSL_SHA384
  3362. case WC_SHA384:
  3363. len = WC_SHA384_DIGEST_SIZE;
  3364. break;
  3365. #endif
  3366. #ifdef WOLFSSL_TLS13_SHA512
  3367. case WC_SHA512:
  3368. len = WC_SHA512_DIGEST_SIZE;
  3369. break;
  3370. #endif
  3371. default:
  3372. return BAD_FUNC_ARG;
  3373. }
  3374. /* When length is 0 then use zeroed data of digest length. */
  3375. if (ikmLen == 0) {
  3376. ikmLen = len;
  3377. XMEMSET(ikm, 0, len);
  3378. }
  3379. (void)ctx;
  3380. ret = wc_HKDF_Extract(digest, salt, saltLen, ikm, ikmLen, prk);
  3381. WOLFSSL_PKMSG("PK HKDF Extract: ret %d saltLen %d ikmLen %d\n", ret, saltLen,
  3382. ikmLen);
  3383. return ret;
  3384. }
  3385. #endif /* HAVE_HKDF && !NO_HMAC */
  3386. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  3387. #ifdef HAVE_ED25519_SIGN
  3388. static WC_INLINE int myEd25519Sign(WOLFSSL* ssl, const byte* in, word32 inSz,
  3389. byte* out, word32* outSz, const byte* key, word32 keySz, void* ctx)
  3390. {
  3391. int ret;
  3392. word32 idx = 0;
  3393. ed25519_key myKey;
  3394. byte* keyBuf = (byte*)key;
  3395. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  3396. (void)ssl;
  3397. (void)cbInfo;
  3398. WOLFSSL_PKMSG("PK 25519 Sign: inSz %d, keySz %d\n", inSz, keySz);
  3399. #ifdef TEST_PK_PRIVKEY
  3400. ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz);
  3401. if (ret != 0)
  3402. return ret;
  3403. #endif
  3404. ret = wc_ed25519_init(&myKey);
  3405. if (ret == 0) {
  3406. ret = wc_Ed25519PrivateKeyDecode(keyBuf, &idx, &myKey, keySz);
  3407. if (ret == 0) {
  3408. ret = wc_ed25519_make_public(&myKey, myKey.p, ED25519_PUB_KEY_SIZE);
  3409. }
  3410. if (ret == 0) {
  3411. myKey.pubKeySet = 1;
  3412. ret = wc_ed25519_sign_msg(in, inSz, out, outSz, &myKey);
  3413. }
  3414. wc_ed25519_free(&myKey);
  3415. }
  3416. #ifdef TEST_PK_PRIVKEY
  3417. free(keyBuf);
  3418. #endif
  3419. WOLFSSL_PKMSG("PK 25519 Sign: ret %d, outSz %d\n", ret, *outSz);
  3420. return ret;
  3421. }
  3422. #endif /* HAVE_ED25519_SIGN */
  3423. #ifdef HAVE_ED25519_VERIFY
  3424. static WC_INLINE int myEd25519Verify(WOLFSSL* ssl, const byte* sig, word32 sigSz,
  3425. const byte* msg, word32 msgSz, const byte* key, word32 keySz,
  3426. int* result, void* ctx)
  3427. {
  3428. int ret;
  3429. ed25519_key myKey;
  3430. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  3431. (void)ssl;
  3432. (void)cbInfo;
  3433. WOLFSSL_PKMSG("PK 25519 Verify: sigSz %d, msgSz %d, keySz %d\n", sigSz, msgSz, keySz);
  3434. ret = wc_ed25519_init(&myKey);
  3435. if (ret == 0) {
  3436. ret = wc_ed25519_import_public(key, keySz, &myKey);
  3437. if (ret == 0) {
  3438. ret = wc_ed25519_verify_msg(sig, sigSz, msg, msgSz, result, &myKey);
  3439. }
  3440. wc_ed25519_free(&myKey);
  3441. }
  3442. WOLFSSL_PKMSG("PK 25519 Verify: ret %d, result %d\n", ret, *result);
  3443. return ret;
  3444. }
  3445. #endif /* HAVE_ED25519_VERIFY */
  3446. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_IMPORT */
  3447. #ifdef HAVE_CURVE25519
  3448. static WC_INLINE int myX25519KeyGen(WOLFSSL* ssl, curve25519_key* key,
  3449. unsigned int keySz, void* ctx)
  3450. {
  3451. int ret;
  3452. WC_RNG rng;
  3453. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  3454. (void)ssl;
  3455. (void)cbInfo;
  3456. WOLFSSL_PKMSG("PK 25519 KeyGen: keySz %u\n", keySz);
  3457. ret = wc_InitRng(&rng);
  3458. if (ret != 0)
  3459. return ret;
  3460. ret = wc_curve25519_make_key(&rng, keySz, key);
  3461. wc_FreeRng(&rng);
  3462. WOLFSSL_PKMSG("PK 25519 KeyGen: ret %d\n", ret);
  3463. return ret;
  3464. }
  3465. static WC_INLINE int myX25519SharedSecret(WOLFSSL* ssl, curve25519_key* otherKey,
  3466. unsigned char* pubKeyDer, unsigned int* pubKeySz,
  3467. unsigned char* out, unsigned int* outlen,
  3468. int side, void* ctx)
  3469. {
  3470. int ret;
  3471. curve25519_key* privKey = NULL;
  3472. curve25519_key* pubKey = NULL;
  3473. curve25519_key tmpKey;
  3474. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  3475. (void)ssl;
  3476. (void)cbInfo;
  3477. WOLFSSL_PKMSG("PK 25519 PMS: side %s\n",
  3478. side == WOLFSSL_CLIENT_END ? "client" : "server");
  3479. ret = wc_curve25519_init(&tmpKey);
  3480. if (ret != 0) {
  3481. return ret;
  3482. }
  3483. /* for client: create and export public key */
  3484. if (side == WOLFSSL_CLIENT_END) {
  3485. WC_RNG rng;
  3486. privKey = &tmpKey;
  3487. pubKey = otherKey;
  3488. ret = wc_InitRng(&rng);
  3489. if (ret == 0) {
  3490. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, privKey);
  3491. if (ret == 0) {
  3492. ret = wc_curve25519_export_public_ex(privKey, pubKeyDer,
  3493. pubKeySz, EC25519_LITTLE_ENDIAN);
  3494. }
  3495. wc_FreeRng(&rng);
  3496. }
  3497. }
  3498. /* for server: import public key */
  3499. else if (side == WOLFSSL_SERVER_END) {
  3500. privKey = otherKey;
  3501. pubKey = &tmpKey;
  3502. ret = wc_curve25519_import_public_ex(pubKeyDer, *pubKeySz, pubKey,
  3503. EC25519_LITTLE_ENDIAN);
  3504. }
  3505. else {
  3506. ret = BAD_FUNC_ARG;
  3507. }
  3508. /* generate shared secret and return it */
  3509. if (ret == 0) {
  3510. ret = wc_curve25519_shared_secret_ex(privKey, pubKey, out, outlen,
  3511. EC25519_LITTLE_ENDIAN);
  3512. }
  3513. wc_curve25519_free(&tmpKey);
  3514. WOLFSSL_PKMSG("PK 25519 PMS: ret %d, pubKeySz %u, outLen %u\n",
  3515. ret, *pubKeySz, *outlen);
  3516. return ret;
  3517. }
  3518. #endif /* HAVE_CURVE25519 */
  3519. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  3520. #ifdef HAVE_ED448_SIGN
  3521. static WC_INLINE int myEd448Sign(WOLFSSL* ssl, const byte* in, word32 inSz,
  3522. byte* out, word32* outSz, const byte* key, word32 keySz, void* ctx)
  3523. {
  3524. int ret;
  3525. word32 idx = 0;
  3526. ed448_key myKey;
  3527. byte* keyBuf = (byte*)key;
  3528. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  3529. (void)ssl;
  3530. (void)cbInfo;
  3531. WOLFSSL_PKMSG("PK 448 Sign: inSz %u, keySz %u\n", inSz, keySz);
  3532. #ifdef TEST_PK_PRIVKEY
  3533. ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz);
  3534. if (ret != 0)
  3535. return ret;
  3536. #endif
  3537. ret = wc_ed448_init(&myKey);
  3538. if (ret == 0) {
  3539. ret = wc_Ed448PrivateKeyDecode(keyBuf, &idx, &myKey, keySz);
  3540. if (ret == 0) {
  3541. ret = wc_ed448_make_public(&myKey, myKey.p, ED448_PUB_KEY_SIZE);
  3542. }
  3543. if (ret == 0) {
  3544. myKey.pubKeySet = 1;
  3545. ret = wc_ed448_sign_msg(in, inSz, out, outSz, &myKey, NULL, 0);
  3546. }
  3547. wc_ed448_free(&myKey);
  3548. }
  3549. #ifdef TEST_PK_PRIVKEY
  3550. free(keyBuf);
  3551. #endif
  3552. WOLFSSL_PKMSG("PK 448 Sign: ret %d, outSz %u\n", ret, *outSz);
  3553. return ret;
  3554. }
  3555. #endif /* HAVE_ED448_SIGN */
  3556. #ifdef HAVE_ED448_VERIFY
  3557. static WC_INLINE int myEd448Verify(WOLFSSL* ssl, const byte* sig, word32 sigSz,
  3558. const byte* msg, word32 msgSz, const byte* key, word32 keySz,
  3559. int* result, void* ctx)
  3560. {
  3561. int ret;
  3562. ed448_key myKey;
  3563. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  3564. (void)ssl;
  3565. (void)cbInfo;
  3566. WOLFSSL_PKMSG("PK 448 Verify: sigSz %u, msgSz %u, keySz %u\n", sigSz, msgSz,
  3567. keySz);
  3568. ret = wc_ed448_init(&myKey);
  3569. if (ret == 0) {
  3570. ret = wc_ed448_import_public(key, keySz, &myKey);
  3571. if (ret == 0) {
  3572. ret = wc_ed448_verify_msg(sig, sigSz, msg, msgSz, result, &myKey,
  3573. NULL, 0);
  3574. }
  3575. wc_ed448_free(&myKey);
  3576. }
  3577. WOLFSSL_PKMSG("PK 448 Verify: ret %d, result %d\n", ret, *result);
  3578. return ret;
  3579. }
  3580. #endif /* HAVE_ED448_VERIFY */
  3581. #endif /* HAVE_ED448 && HAVE_ED448_KEY_IMPORT */
  3582. #ifdef HAVE_CURVE448
  3583. static WC_INLINE int myX448KeyGen(WOLFSSL* ssl, curve448_key* key,
  3584. unsigned int keySz, void* ctx)
  3585. {
  3586. int ret;
  3587. WC_RNG rng;
  3588. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  3589. (void)ssl;
  3590. (void)cbInfo;
  3591. WOLFSSL_PKMSG("PK 448 KeyGen: keySz %u\n", keySz);
  3592. ret = wc_InitRng(&rng);
  3593. if (ret != 0)
  3594. return ret;
  3595. ret = wc_curve448_make_key(&rng, keySz, key);
  3596. wc_FreeRng(&rng);
  3597. WOLFSSL_PKMSG("PK 448 KeyGen: ret %d\n", ret);
  3598. return ret;
  3599. }
  3600. static WC_INLINE int myX448SharedSecret(WOLFSSL* ssl, curve448_key* otherKey,
  3601. unsigned char* pubKeyDer, unsigned int* pubKeySz,
  3602. unsigned char* out, unsigned int* outlen,
  3603. int side, void* ctx)
  3604. {
  3605. int ret;
  3606. curve448_key* privKey = NULL;
  3607. curve448_key* pubKey = NULL;
  3608. curve448_key tmpKey;
  3609. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  3610. (void)ssl;
  3611. (void)cbInfo;
  3612. WOLFSSL_PKMSG("PK 448 PMS: side %s\n",
  3613. side == WOLFSSL_CLIENT_END ? "client" : "server");
  3614. ret = wc_curve448_init(&tmpKey);
  3615. if (ret != 0) {
  3616. return ret;
  3617. }
  3618. /* for client: create and export public key */
  3619. if (side == WOLFSSL_CLIENT_END) {
  3620. WC_RNG rng;
  3621. privKey = &tmpKey;
  3622. pubKey = otherKey;
  3623. ret = wc_InitRng(&rng);
  3624. if (ret == 0) {
  3625. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, privKey);
  3626. if (ret == 0) {
  3627. ret = wc_curve448_export_public_ex(privKey, pubKeyDer,
  3628. pubKeySz, EC448_LITTLE_ENDIAN);
  3629. }
  3630. wc_FreeRng(&rng);
  3631. }
  3632. }
  3633. /* for server: import public key */
  3634. else if (side == WOLFSSL_SERVER_END) {
  3635. privKey = otherKey;
  3636. pubKey = &tmpKey;
  3637. ret = wc_curve448_import_public_ex(pubKeyDer, *pubKeySz, pubKey,
  3638. EC448_LITTLE_ENDIAN);
  3639. }
  3640. else {
  3641. ret = BAD_FUNC_ARG;
  3642. }
  3643. /* generate shared secret and return it */
  3644. if (ret == 0) {
  3645. ret = wc_curve448_shared_secret_ex(privKey, pubKey, out, outlen,
  3646. EC448_LITTLE_ENDIAN);
  3647. }
  3648. wc_curve448_free(&tmpKey);
  3649. WOLFSSL_PKMSG("PK 448 PMS: ret %d, pubKeySz %u, outLen %u\n",
  3650. ret, *pubKeySz, *outlen);
  3651. return ret;
  3652. }
  3653. #endif /* HAVE_CURVE448 */
  3654. #ifndef NO_DH
  3655. static WC_INLINE int myDhCallback(WOLFSSL* ssl, struct DhKey* key,
  3656. const unsigned char* priv, unsigned int privSz,
  3657. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  3658. unsigned char* out, unsigned int* outlen,
  3659. void* ctx)
  3660. {
  3661. int ret;
  3662. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  3663. (void)ssl;
  3664. (void)cbInfo;
  3665. /* return 0 on success */
  3666. ret = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  3667. WOLFSSL_PKMSG("PK ED Agree: ret %d, privSz %u, pubKeySz %u, outlen %u\n",
  3668. ret, privSz, pubKeySz, *outlen);
  3669. return ret;
  3670. }
  3671. #endif /* !NO_DH */
  3672. #ifndef NO_RSA
  3673. static WC_INLINE int myRsaSign(WOLFSSL* ssl, const byte* in, word32 inSz,
  3674. byte* out, word32* outSz, const byte* key, word32 keySz, void* ctx)
  3675. {
  3676. WC_RNG rng;
  3677. int ret;
  3678. word32 idx = 0;
  3679. RsaKey myKey;
  3680. byte* keyBuf = (byte*)key;
  3681. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  3682. (void)ssl;
  3683. (void)cbInfo;
  3684. WOLFSSL_PKMSG("PK RSA Sign: inSz %u, keySz %u\n", inSz, keySz);
  3685. #ifdef TEST_PK_PRIVKEY
  3686. ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz);
  3687. if (ret != 0)
  3688. return ret;
  3689. #endif
  3690. ret = wc_InitRng(&rng);
  3691. if (ret != 0)
  3692. return ret;
  3693. ret = wc_InitRsaKey(&myKey, NULL);
  3694. if (ret == 0) {
  3695. ret = wc_RsaPrivateKeyDecode(keyBuf, &idx, &myKey, keySz);
  3696. if (ret == 0)
  3697. ret = wc_RsaSSL_Sign(in, inSz, out, *outSz, &myKey, &rng);
  3698. if (ret > 0) { /* save and convert to 0 success */
  3699. *outSz = ret;
  3700. ret = 0;
  3701. }
  3702. wc_FreeRsaKey(&myKey);
  3703. }
  3704. wc_FreeRng(&rng);
  3705. #ifdef TEST_PK_PRIVKEY
  3706. free(keyBuf);
  3707. #endif
  3708. WOLFSSL_PKMSG("PK RSA Sign: ret %d, outSz %u\n", ret, *outSz);
  3709. return ret;
  3710. }
  3711. static WC_INLINE int myRsaVerify(WOLFSSL* ssl, byte* sig, word32 sigSz,
  3712. byte** out, const byte* key, word32 keySz, void* ctx)
  3713. {
  3714. int ret;
  3715. word32 idx = 0;
  3716. RsaKey myKey;
  3717. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  3718. (void)ssl;
  3719. (void)cbInfo;
  3720. WOLFSSL_PKMSG("PK RSA Verify: sigSz %u, keySz %u\n", sigSz, keySz);
  3721. ret = wc_InitRsaKey(&myKey, NULL);
  3722. if (ret == 0) {
  3723. ret = wc_RsaPublicKeyDecode(key, &idx, &myKey, keySz);
  3724. if (ret == 0)
  3725. ret = wc_RsaSSL_VerifyInline(sig, sigSz, out, &myKey);
  3726. wc_FreeRsaKey(&myKey);
  3727. }
  3728. WOLFSSL_PKMSG("PK RSA Verify: ret %d\n", ret);
  3729. return ret;
  3730. }
  3731. static WC_INLINE int myRsaSignCheck(WOLFSSL* ssl, byte* sig, word32 sigSz,
  3732. byte** out, const byte* key, word32 keySz, void* ctx)
  3733. {
  3734. int ret;
  3735. word32 idx = 0;
  3736. RsaKey myKey;
  3737. byte* keyBuf = (byte*)key;
  3738. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  3739. (void)ssl;
  3740. (void)cbInfo;
  3741. WOLFSSL_PKMSG("PK RSA SignCheck: sigSz %u, keySz %u\n", sigSz, keySz);
  3742. #ifdef TEST_PK_PRIVKEY
  3743. ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz);
  3744. if (ret != 0)
  3745. return ret;
  3746. #endif
  3747. ret = wc_InitRsaKey(&myKey, NULL);
  3748. if (ret == 0) {
  3749. ret = wc_RsaPrivateKeyDecode(keyBuf, &idx, &myKey, keySz);
  3750. if (ret == 0)
  3751. ret = wc_RsaSSL_VerifyInline(sig, sigSz, out, &myKey);
  3752. wc_FreeRsaKey(&myKey);
  3753. }
  3754. #ifdef TEST_PK_PRIVKEY
  3755. free(keyBuf);
  3756. #endif
  3757. WOLFSSL_PKMSG("PK RSA SignCheck: ret %d\n", ret);
  3758. return ret;
  3759. }
  3760. #ifdef WC_RSA_PSS
  3761. static WC_INLINE int myRsaPssSign(WOLFSSL* ssl, const byte* in, word32 inSz,
  3762. byte* out, word32* outSz, int hash, int mgf, const byte* key,
  3763. word32 keySz, void* ctx)
  3764. {
  3765. enum wc_HashType hashType = WC_HASH_TYPE_NONE;
  3766. WC_RNG rng;
  3767. int ret;
  3768. word32 idx = 0;
  3769. RsaKey myKey;
  3770. byte* keyBuf = (byte*)key;
  3771. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  3772. (void)ssl;
  3773. (void)cbInfo;
  3774. WOLFSSL_PKMSG("PK RSA PSS Sign: inSz %u, hash %d, mgf %d, keySz %u\n",
  3775. inSz, hash, mgf, keySz);
  3776. #ifdef TEST_PK_PRIVKEY
  3777. ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz);
  3778. if (ret != 0)
  3779. return ret;
  3780. #endif
  3781. switch (hash) {
  3782. #ifndef NO_SHA256
  3783. case SHA256h:
  3784. hashType = WC_HASH_TYPE_SHA256;
  3785. break;
  3786. #endif
  3787. #ifdef WOLFSSL_SHA384
  3788. case SHA384h:
  3789. hashType = WC_HASH_TYPE_SHA384;
  3790. break;
  3791. #endif
  3792. #ifdef WOLFSSL_SHA512
  3793. case SHA512h:
  3794. hashType = WC_HASH_TYPE_SHA512;
  3795. break;
  3796. #endif
  3797. }
  3798. ret = wc_InitRng(&rng);
  3799. if (ret != 0)
  3800. return ret;
  3801. ret = wc_InitRsaKey(&myKey, NULL);
  3802. if (ret == 0) {
  3803. ret = wc_RsaPrivateKeyDecode(keyBuf, &idx, &myKey, keySz);
  3804. if (ret == 0) {
  3805. ret = wc_RsaPSS_Sign(in, inSz, out, *outSz, hashType, mgf, &myKey,
  3806. &rng);
  3807. }
  3808. if (ret > 0) { /* save and convert to 0 success */
  3809. *outSz = ret;
  3810. ret = 0;
  3811. }
  3812. wc_FreeRsaKey(&myKey);
  3813. }
  3814. wc_FreeRng(&rng);
  3815. #ifdef TEST_PK_PRIVKEY
  3816. free(keyBuf);
  3817. #endif
  3818. WOLFSSL_PKMSG("PK RSA PSS Sign: ret %d, outSz %u\n", ret, *outSz);
  3819. return ret;
  3820. }
  3821. static WC_INLINE int myRsaPssVerify(WOLFSSL* ssl, byte* sig, word32 sigSz,
  3822. byte** out, int hash, int mgf, const byte* key, word32 keySz, void* ctx)
  3823. {
  3824. int ret;
  3825. word32 idx = 0;
  3826. RsaKey myKey;
  3827. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  3828. enum wc_HashType hashType = WC_HASH_TYPE_NONE;
  3829. (void)ssl;
  3830. (void)cbInfo;
  3831. WOLFSSL_PKMSG("PK RSA PSS Verify: sigSz %u, hash %d, mgf %d, keySz %u\n",
  3832. sigSz, hash, mgf, keySz);
  3833. switch (hash) {
  3834. #ifndef NO_SHA256
  3835. case SHA256h:
  3836. hashType = WC_HASH_TYPE_SHA256;
  3837. break;
  3838. #endif
  3839. #ifdef WOLFSSL_SHA384
  3840. case SHA384h:
  3841. hashType = WC_HASH_TYPE_SHA384;
  3842. break;
  3843. #endif
  3844. #ifdef WOLFSSL_SHA512
  3845. case SHA512h:
  3846. hashType = WC_HASH_TYPE_SHA512;
  3847. break;
  3848. #endif
  3849. }
  3850. ret = wc_InitRsaKey(&myKey, NULL);
  3851. if (ret == 0) {
  3852. ret = wc_RsaPublicKeyDecode(key, &idx, &myKey, keySz);
  3853. if (ret == 0) {
  3854. ret = wc_RsaPSS_VerifyInline(sig, sigSz, out, hashType, mgf,
  3855. &myKey);
  3856. }
  3857. wc_FreeRsaKey(&myKey);
  3858. }
  3859. WOLFSSL_PKMSG("PK RSA PSS Verify: ret %d\n", ret);
  3860. return ret;
  3861. }
  3862. static WC_INLINE int myRsaPssSignCheck(WOLFSSL* ssl, byte* sig, word32 sigSz,
  3863. byte** out, int hash, int mgf, const byte* key, word32 keySz, void* ctx)
  3864. {
  3865. int ret;
  3866. word32 idx = 0;
  3867. RsaKey myKey;
  3868. byte* keyBuf = (byte*)key;
  3869. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  3870. enum wc_HashType hashType = WC_HASH_TYPE_NONE;
  3871. (void)ssl;
  3872. (void)cbInfo;
  3873. WOLFSSL_PKMSG("PK RSA PSS SignCheck: sigSz %u, hash %d, mgf %d, keySz %u\n",
  3874. sigSz, hash, mgf, keySz);
  3875. #ifdef TEST_PK_PRIVKEY
  3876. ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz);
  3877. if (ret != 0)
  3878. return ret;
  3879. #endif
  3880. switch (hash) {
  3881. #ifndef NO_SHA256
  3882. case SHA256h:
  3883. hashType = WC_HASH_TYPE_SHA256;
  3884. break;
  3885. #endif
  3886. #ifdef WOLFSSL_SHA384
  3887. case SHA384h:
  3888. hashType = WC_HASH_TYPE_SHA384;
  3889. break;
  3890. #endif
  3891. #ifdef WOLFSSL_SHA512
  3892. case SHA512h:
  3893. hashType = WC_HASH_TYPE_SHA512;
  3894. break;
  3895. #endif
  3896. }
  3897. ret = wc_InitRsaKey(&myKey, NULL);
  3898. if (ret == 0) {
  3899. ret = wc_RsaPrivateKeyDecode(keyBuf, &idx, &myKey, keySz);
  3900. if (ret == 0) {
  3901. ret = wc_RsaPSS_VerifyInline(sig, sigSz, out, hashType, mgf,
  3902. &myKey);
  3903. }
  3904. wc_FreeRsaKey(&myKey);
  3905. }
  3906. #ifdef TEST_PK_PRIVKEY
  3907. free(keyBuf);
  3908. #endif
  3909. WOLFSSL_PKMSG("PK RSA PSS SignCheck: ret %d\n", ret);
  3910. return ret;
  3911. }
  3912. #endif
  3913. static WC_INLINE int myRsaEnc(WOLFSSL* ssl, const byte* in, word32 inSz,
  3914. byte* out, word32* outSz, const byte* key,
  3915. word32 keySz, void* ctx)
  3916. {
  3917. int ret;
  3918. word32 idx = 0;
  3919. RsaKey myKey;
  3920. WC_RNG rng;
  3921. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  3922. (void)ssl;
  3923. (void)cbInfo;
  3924. WOLFSSL_PKMSG("PK RSA Enc: inSz %u, keySz %u\n", inSz, keySz);
  3925. ret = wc_InitRng(&rng);
  3926. if (ret != 0)
  3927. return ret;
  3928. ret = wc_InitRsaKey(&myKey, NULL);
  3929. if (ret == 0) {
  3930. ret = wc_RsaPublicKeyDecode(key, &idx, &myKey, keySz);
  3931. if (ret == 0) {
  3932. ret = wc_RsaPublicEncrypt(in, inSz, out, *outSz, &myKey, &rng);
  3933. if (ret > 0) {
  3934. *outSz = ret;
  3935. ret = 0; /* reset to success */
  3936. }
  3937. }
  3938. wc_FreeRsaKey(&myKey);
  3939. }
  3940. wc_FreeRng(&rng);
  3941. WOLFSSL_PKMSG("PK RSA Enc: ret %d, outSz %u\n", ret, *outSz);
  3942. return ret;
  3943. }
  3944. static WC_INLINE int myRsaDec(WOLFSSL* ssl, byte* in, word32 inSz,
  3945. byte** out,
  3946. const byte* key, word32 keySz, void* ctx)
  3947. {
  3948. int ret;
  3949. word32 idx = 0;
  3950. RsaKey myKey;
  3951. byte* keyBuf = (byte*)key;
  3952. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  3953. (void)ssl;
  3954. (void)cbInfo;
  3955. WOLFSSL_PKMSG("PK RSA Dec: inSz %u, keySz %u\n", inSz, keySz);
  3956. #ifdef TEST_PK_PRIVKEY
  3957. ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz);
  3958. if (ret != 0)
  3959. return ret;
  3960. #endif
  3961. ret = wc_InitRsaKey(&myKey, NULL);
  3962. if (ret == 0) {
  3963. ret = wc_RsaPrivateKeyDecode(keyBuf, &idx, &myKey, keySz);
  3964. if (ret == 0) {
  3965. #ifdef WC_RSA_BLINDING
  3966. ret = wc_RsaSetRNG(&myKey, wolfSSL_GetRNG(ssl));
  3967. if (ret != 0) {
  3968. wc_FreeRsaKey(&myKey);
  3969. return ret;
  3970. }
  3971. #endif
  3972. ret = wc_RsaPrivateDecryptInline(in, inSz, out, &myKey);
  3973. }
  3974. wc_FreeRsaKey(&myKey);
  3975. }
  3976. #ifdef TEST_PK_PRIVKEY
  3977. free(keyBuf);
  3978. #endif
  3979. WOLFSSL_PKMSG("PK RSA Dec: ret %d\n", ret);
  3980. return ret;
  3981. }
  3982. #endif /* NO_RSA */
  3983. static WC_INLINE int myGenMaster(WOLFSSL* ssl, void* ctx)
  3984. {
  3985. int ret;
  3986. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  3987. (void)ssl;
  3988. (void)cbInfo;
  3989. WOLFSSL_PKMSG("Gen Master");
  3990. /* fall through to original routine */
  3991. ret = PROTOCOLCB_UNAVAILABLE;
  3992. WOLFSSL_PKMSG("Gen Master: ret %d\n", ret);
  3993. return ret;
  3994. }
  3995. static WC_INLINE int myGenPreMaster(WOLFSSL* ssl, byte *premaster,
  3996. word32 preSz, void* ctx)
  3997. {
  3998. int ret;
  3999. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  4000. (void) ssl;
  4001. (void) cbInfo;
  4002. (void) premaster;
  4003. (void) preSz;
  4004. WOLFSSL_PKMSG("Gen Pre-Master Cb");
  4005. /* fall through to original routine */
  4006. ret = PROTOCOLCB_UNAVAILABLE;
  4007. WOLFSSL_PKMSG("Gen Pre-Master Cb: ret %d\n", ret);
  4008. return ret;
  4009. }
  4010. static WC_INLINE int myGenSessionKey(WOLFSSL* ssl, void* ctx)
  4011. {
  4012. int ret;
  4013. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  4014. (void)ssl;
  4015. (void)cbInfo;
  4016. WOLFSSL_PKMSG("Gen Master Cb");
  4017. /* fall through to original routine */
  4018. ret = PROTOCOLCB_UNAVAILABLE;
  4019. WOLFSSL_PKMSG("Gen Master Cb: ret %d\n", ret);
  4020. return ret;
  4021. }
  4022. static WC_INLINE int mySetEncryptKeys(WOLFSSL* ssl, void* ctx)
  4023. {
  4024. int ret;
  4025. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  4026. (void)ssl;
  4027. (void)cbInfo;
  4028. WOLFSSL_PKMSG("Set Encrypt Keys Cb");
  4029. /* fall through to original routine */
  4030. ret = PROTOCOLCB_UNAVAILABLE;
  4031. WOLFSSL_PKMSG("Set Encrypt Keys Cb: ret %d\n", ret);
  4032. return ret;
  4033. }
  4034. #if !defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_AEAD_ONLY)
  4035. static WC_INLINE int myVerifyMac(WOLFSSL *ssl, const byte* message,
  4036. word32 messageSz, word32 macSz, word32 content, void* ctx)
  4037. {
  4038. int ret;
  4039. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  4040. (void)ssl;
  4041. (void)message;
  4042. (void)messageSz;
  4043. (void)macSz;
  4044. (void)content;
  4045. (void)cbInfo;
  4046. WOLFSSL_PKMSG("Verify Mac Cb");
  4047. /* fall through to original routine */
  4048. ret = PROTOCOLCB_UNAVAILABLE;
  4049. WOLFSSL_PKMSG("Verify Mac Cb: ret %d\n", ret);
  4050. return ret;
  4051. }
  4052. #endif
  4053. static WC_INLINE int myTlsFinished(WOLFSSL* ssl,
  4054. const byte *side,
  4055. const byte *handshake_hash, word32 hashSz,
  4056. byte *hashes, void* ctx)
  4057. {
  4058. int ret;
  4059. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  4060. (void)ssl;
  4061. (void)cbInfo;
  4062. (void)side;
  4063. (void)handshake_hash;
  4064. (void)hashSz;
  4065. (void)hashes;
  4066. WOLFSSL_PKMSG("Tls Finished Cb");
  4067. /* fall through to original routine */
  4068. ret = PROTOCOLCB_UNAVAILABLE;
  4069. WOLFSSL_PKMSG("Tls Finished Cb: ret %d\n", ret);
  4070. return ret;
  4071. }
  4072. static WC_INLINE void SetupPkCallbacks(WOLFSSL_CTX* ctx)
  4073. {
  4074. (void)ctx;
  4075. #ifdef HAVE_ECC
  4076. wolfSSL_CTX_SetEccKeyGenCb(ctx, myEccKeyGen);
  4077. wolfSSL_CTX_SetEccSignCb(ctx, myEccSign);
  4078. wolfSSL_CTX_SetEccVerifyCb(ctx, myEccVerify);
  4079. wolfSSL_CTX_SetEccSharedSecretCb(ctx, myEccSharedSecret);
  4080. #endif /* HAVE_ECC */
  4081. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  4082. wolfSSL_CTX_SetHKDFExtractCb(ctx, myHkdfExtract);
  4083. #endif /* HAVE_HKDF && !NO_HMAC */
  4084. #ifndef NO_DH
  4085. wolfSSL_CTX_SetDhAgreeCb(ctx, myDhCallback);
  4086. #endif
  4087. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  4088. #ifdef HAVE_ED25519_SIGN
  4089. wolfSSL_CTX_SetEd25519SignCb(ctx, myEd25519Sign);
  4090. #endif
  4091. #ifdef HAVE_ED25519_VERIFY
  4092. wolfSSL_CTX_SetEd25519VerifyCb(ctx, myEd25519Verify);
  4093. #endif
  4094. #endif
  4095. #ifdef HAVE_CURVE25519
  4096. wolfSSL_CTX_SetX25519KeyGenCb(ctx, myX25519KeyGen);
  4097. wolfSSL_CTX_SetX25519SharedSecretCb(ctx, myX25519SharedSecret);
  4098. #endif
  4099. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  4100. #if defined(HAVE_ED448_SIGN)
  4101. wolfSSL_CTX_SetEd448SignCb(ctx, myEd448Sign);
  4102. #endif
  4103. #if defined(HAVE_ED448_VERIFY)
  4104. wolfSSL_CTX_SetEd448VerifyCb(ctx, myEd448Verify);
  4105. #endif
  4106. #endif
  4107. #ifdef HAVE_CURVE448
  4108. wolfSSL_CTX_SetX448KeyGenCb(ctx, myX448KeyGen);
  4109. wolfSSL_CTX_SetX448SharedSecretCb(ctx, myX448SharedSecret);
  4110. #endif
  4111. #ifndef NO_RSA
  4112. wolfSSL_CTX_SetRsaSignCb(ctx, myRsaSign);
  4113. wolfSSL_CTX_SetRsaVerifyCb(ctx, myRsaVerify);
  4114. wolfSSL_CTX_SetRsaSignCheckCb(ctx, myRsaSignCheck);
  4115. #ifdef WC_RSA_PSS
  4116. wolfSSL_CTX_SetRsaPssSignCb(ctx, myRsaPssSign);
  4117. wolfSSL_CTX_SetRsaPssVerifyCb(ctx, myRsaPssVerify);
  4118. wolfSSL_CTX_SetRsaPssSignCheckCb(ctx, myRsaPssSignCheck);
  4119. #endif
  4120. wolfSSL_CTX_SetRsaEncCb(ctx, myRsaEnc);
  4121. wolfSSL_CTX_SetRsaDecCb(ctx, myRsaDec);
  4122. #endif /* NO_RSA */
  4123. #ifndef NO_CERTS
  4124. wolfSSL_CTX_SetGenMasterSecretCb(ctx, myGenMaster);
  4125. wolfSSL_CTX_SetGenPreMasterCb(ctx, myGenPreMaster);
  4126. wolfSSL_CTX_SetGenSessionKeyCb(ctx, myGenSessionKey);
  4127. wolfSSL_CTX_SetEncryptKeysCb(ctx, mySetEncryptKeys);
  4128. #if !defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_AEAD_ONLY)
  4129. wolfSSL_CTX_SetVerifyMacCb(ctx, myVerifyMac);
  4130. #endif
  4131. wolfSSL_CTX_SetTlsFinishedCb(ctx, myTlsFinished);
  4132. #endif /* NO_CERTS */
  4133. }
  4134. static WC_INLINE void SetupPkCallbackContexts(WOLFSSL* ssl, void* myCtx)
  4135. {
  4136. #ifdef HAVE_ECC
  4137. wolfSSL_SetEccKeyGenCtx(ssl, myCtx);
  4138. wolfSSL_SetEccSignCtx(ssl, myCtx);
  4139. wolfSSL_SetEccVerifyCtx(ssl, myCtx);
  4140. wolfSSL_SetEccSharedSecretCtx(ssl, myCtx);
  4141. #endif /* HAVE_ECC */
  4142. #ifdef HAVE_HKDF
  4143. wolfSSL_SetHKDFExtractCtx(ssl, myCtx);
  4144. #endif /* HAVE_HKDF */
  4145. #ifndef NO_DH
  4146. wolfSSL_SetDhAgreeCtx(ssl, myCtx);
  4147. #endif
  4148. #ifdef HAVE_ED25519
  4149. wolfSSL_SetEd25519SignCtx(ssl, myCtx);
  4150. wolfSSL_SetEd25519VerifyCtx(ssl, myCtx);
  4151. #endif
  4152. #ifdef HAVE_CURVE25519
  4153. wolfSSL_SetX25519KeyGenCtx(ssl, myCtx);
  4154. wolfSSL_SetX25519SharedSecretCtx(ssl, myCtx);
  4155. #endif
  4156. #ifdef HAVE_ED448
  4157. wolfSSL_SetEd448SignCtx(ssl, myCtx);
  4158. wolfSSL_SetEd448VerifyCtx(ssl, myCtx);
  4159. #endif
  4160. #ifdef HAVE_CURVE448
  4161. wolfSSL_SetX448KeyGenCtx(ssl, myCtx);
  4162. wolfSSL_SetX448SharedSecretCtx(ssl, myCtx);
  4163. #endif
  4164. #ifndef NO_RSA
  4165. wolfSSL_SetRsaSignCtx(ssl, myCtx);
  4166. wolfSSL_SetRsaVerifyCtx(ssl, myCtx);
  4167. #ifdef WC_RSA_PSS
  4168. wolfSSL_SetRsaPssSignCtx(ssl, myCtx);
  4169. wolfSSL_SetRsaPssVerifyCtx(ssl, myCtx);
  4170. #endif
  4171. wolfSSL_SetRsaEncCtx(ssl, myCtx);
  4172. wolfSSL_SetRsaDecCtx(ssl, myCtx);
  4173. #endif /* NO_RSA */
  4174. #ifndef NO_CERTS
  4175. wolfSSL_SetGenMasterSecretCtx(ssl, myCtx);
  4176. wolfSSL_SetGenPreMasterCtx(ssl, myCtx);
  4177. wolfSSL_SetGenSessionKeyCtx(ssl, myCtx);
  4178. wolfSSL_SetEncryptKeysCtx(ssl, myCtx);
  4179. #if !defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_AEAD_ONLY)
  4180. wolfSSL_SetVerifyMacCtx(ssl, myCtx);
  4181. #endif
  4182. wolfSSL_SetTlsFinishedCtx(ssl, myCtx);
  4183. #endif
  4184. }
  4185. #endif /* HAVE_PK_CALLBACKS */
  4186. #ifdef USE_WOLFSSL_IO
  4187. static WC_INLINE int SimulateWantWriteIOSendCb(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  4188. {
  4189. static int wantWriteFlag = 1;
  4190. int sd = *(int*)ctx;
  4191. (void)ssl;
  4192. if (!wantWriteFlag)
  4193. {
  4194. int sent;
  4195. wantWriteFlag = 1;
  4196. sent = wolfIO_Send(sd, buf, sz, 0);
  4197. if (sent < 0) {
  4198. int err = errno;
  4199. if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) {
  4200. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  4201. }
  4202. else if (err == SOCKET_ECONNRESET) {
  4203. return WOLFSSL_CBIO_ERR_CONN_RST;
  4204. }
  4205. else if (err == SOCKET_EINTR) {
  4206. return WOLFSSL_CBIO_ERR_ISR;
  4207. }
  4208. else if (err == SOCKET_EPIPE) {
  4209. return WOLFSSL_CBIO_ERR_CONN_CLOSE;
  4210. }
  4211. else {
  4212. return WOLFSSL_CBIO_ERR_GENERAL;
  4213. }
  4214. }
  4215. return sent;
  4216. }
  4217. else
  4218. {
  4219. wantWriteFlag = 0;
  4220. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  4221. }
  4222. }
  4223. #endif /* USE_WOLFSSL_IO */
  4224. #if defined(__hpux__) || defined(__MINGW32__) || defined (WOLFSSL_TIRTOS) \
  4225. || defined(_MSC_VER)
  4226. /* HP/UX doesn't have strsep, needed by test/suites.c */
  4227. static WC_INLINE char* strsep(char **stringp, const char *delim)
  4228. {
  4229. char* start;
  4230. char* end;
  4231. start = *stringp;
  4232. if (start == NULL)
  4233. return NULL;
  4234. if ((end = strpbrk(start, delim))) {
  4235. *end++ = '\0';
  4236. *stringp = end;
  4237. } else {
  4238. *stringp = NULL;
  4239. }
  4240. return start;
  4241. }
  4242. #endif /* __hpux__ and others */
  4243. /* Create unique filename, len is length of tempfn name, assuming
  4244. len does not include null terminating character,
  4245. num is number of characters in tempfn name to randomize */
  4246. static WC_INLINE const char* mymktemp(char *tempfn, int len, int num)
  4247. {
  4248. int x, size;
  4249. static const char alphanum[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  4250. "abcdefghijklmnopqrstuvwxyz";
  4251. WC_RNG rng;
  4252. byte out = 0;
  4253. if (tempfn == NULL || len < 1 || num < 1 || len <= num) {
  4254. fprintf(stderr, "Bad input\n");
  4255. return NULL;
  4256. }
  4257. size = len - 1;
  4258. if (wc_InitRng(&rng) != 0) {
  4259. fprintf(stderr, "InitRng failed\n");
  4260. return NULL;
  4261. }
  4262. for (x = size; x > size - num; x--) {
  4263. if (wc_RNG_GenerateBlock(&rng,(byte*)&out, sizeof(out)) != 0) {
  4264. fprintf(stderr, "RNG_GenerateBlock failed\n");
  4265. return NULL;
  4266. }
  4267. tempfn[x] = alphanum[out % (sizeof(alphanum) - 1)];
  4268. }
  4269. tempfn[len] = '\0';
  4270. wc_FreeRng(&rng);
  4271. (void)rng; /* for WC_NO_RNG case */
  4272. return tempfn;
  4273. }
  4274. #if defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  4275. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || \
  4276. defined(HAVE_AESGCM))
  4277. #define HAVE_TEST_SESSION_TICKET
  4278. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  4279. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  4280. #define WOLFSSL_TICKET_KEY_SZ CHACHA20_POLY1305_AEAD_KEYSIZE
  4281. #elif defined(HAVE_AESGCM)
  4282. #include <wolfssl/wolfcrypt/aes.h>
  4283. #include <wolfssl/wolfcrypt/wc_encrypt.h> /* AES IV sizes in FIPS mode */
  4284. #define WOLFSSL_TICKET_KEY_SZ AES_256_KEY_SIZE
  4285. #endif
  4286. typedef struct key_ctx {
  4287. byte name[WOLFSSL_TICKET_NAME_SZ]; /* name for this context */
  4288. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  4289. } key_ctx;
  4290. static THREAD_LS_T key_ctx myKey_ctx;
  4291. static THREAD_LS_T WC_RNG myKey_rng;
  4292. static WC_INLINE int TicketInit(void)
  4293. {
  4294. int ret = wc_InitRng(&myKey_rng);
  4295. if (ret == 0) {
  4296. ret = wc_RNG_GenerateBlock(&myKey_rng, myKey_ctx.key,
  4297. sizeof(myKey_ctx.key));
  4298. }
  4299. if (ret == 0) {
  4300. ret = wc_RNG_GenerateBlock(&myKey_rng, myKey_ctx.name,
  4301. sizeof(myKey_ctx.name));
  4302. }
  4303. return ret;
  4304. }
  4305. static WC_INLINE void TicketCleanup(void)
  4306. {
  4307. wc_FreeRng(&myKey_rng);
  4308. }
  4309. typedef enum MyTicketState {
  4310. MY_TICKET_STATE_NONE,
  4311. MY_TICKET_STATE_INIT,
  4312. MY_TICKET_STATE_RNG,
  4313. MY_TICKET_STATE_CIPHER_SETUP,
  4314. MY_TICKET_STATE_CIPHER,
  4315. MY_TICKET_STATE_FINAL
  4316. } MyTicketState;
  4317. typedef struct MyTicketCtx {
  4318. MyTicketState state;
  4319. byte aad[WOLFSSL_TICKET_NAME_SZ + WOLFSSL_TICKET_IV_SZ + 2];
  4320. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  4321. /* chahca20/poly1305 */
  4322. #elif defined(HAVE_AESGCM)
  4323. Aes aes;
  4324. #endif
  4325. } MyTicketCtx;
  4326. static WC_INLINE int myTicketEncCb(WOLFSSL* ssl,
  4327. byte key_name[WOLFSSL_TICKET_NAME_SZ],
  4328. byte iv[WOLFSSL_TICKET_IV_SZ],
  4329. byte mac[WOLFSSL_TICKET_MAC_SZ],
  4330. int enc, byte* ticket, int inLen, int* outLen,
  4331. void* userCtx)
  4332. {
  4333. int ret = 0;
  4334. MyTicketCtx tickCtx_lcl;
  4335. MyTicketCtx* tickCtx = (MyTicketCtx*)userCtx;
  4336. (void)ssl;
  4337. if (tickCtx == NULL) {
  4338. /* for test cases where userCtx is not set use local stack for context */
  4339. XMEMSET(&tickCtx_lcl, 0, sizeof(tickCtx_lcl));
  4340. tickCtx = &tickCtx_lcl;
  4341. }
  4342. switch (tickCtx->state) {
  4343. case MY_TICKET_STATE_NONE:
  4344. case MY_TICKET_STATE_INIT:
  4345. {
  4346. /* encrypt */
  4347. if (enc) {
  4348. XMEMCPY(key_name, myKey_ctx.name, WOLFSSL_TICKET_NAME_SZ);
  4349. }
  4350. else {
  4351. /* see if we know this key */
  4352. if (XMEMCMP(key_name, myKey_ctx.name, WOLFSSL_TICKET_NAME_SZ) != 0) {
  4353. printf("client presented unknown ticket key name %s\n", key_name);
  4354. return WOLFSSL_TICKET_RET_FATAL;
  4355. }
  4356. }
  4357. tickCtx->state = MY_TICKET_STATE_RNG;
  4358. }
  4359. FALL_THROUGH;
  4360. case MY_TICKET_STATE_RNG:
  4361. {
  4362. if (enc) {
  4363. ret = wc_RNG_GenerateBlock(&myKey_rng, iv, WOLFSSL_TICKET_IV_SZ);
  4364. if (ret != 0)
  4365. break;
  4366. }
  4367. tickCtx->state = MY_TICKET_STATE_CIPHER_SETUP;
  4368. }
  4369. FALL_THROUGH;
  4370. case MY_TICKET_STATE_CIPHER_SETUP:
  4371. {
  4372. byte* tmp = tickCtx->aad;
  4373. word16 sLen = XHTONS(inLen);
  4374. /* build aad from key name, iv, and length */
  4375. XMEMCPY(tmp, key_name, WOLFSSL_TICKET_NAME_SZ);
  4376. tmp += WOLFSSL_TICKET_NAME_SZ;
  4377. XMEMCPY(tmp, iv, WOLFSSL_TICKET_IV_SZ);
  4378. tmp += WOLFSSL_TICKET_IV_SZ;
  4379. XMEMCPY(tmp, &sLen, sizeof(sLen));
  4380. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  4381. #elif defined(HAVE_AESGCM)
  4382. ret = wc_AesInit(&tickCtx->aes, NULL, INVALID_DEVID);
  4383. if (ret == 0) {
  4384. ret = wc_AesGcmSetKey(&tickCtx->aes, myKey_ctx.key,
  4385. sizeof(myKey_ctx.key));
  4386. }
  4387. if (ret != 0)
  4388. break;
  4389. #endif
  4390. tickCtx->state = MY_TICKET_STATE_CIPHER;
  4391. }
  4392. FALL_THROUGH;
  4393. case MY_TICKET_STATE_CIPHER:
  4394. {
  4395. int aadSz = (int)sizeof(tickCtx->aad);
  4396. /* encrypt */
  4397. if (enc) {
  4398. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  4399. ret = wc_ChaCha20Poly1305_Encrypt(myKey_ctx.key, iv,
  4400. tickCtx->aad, aadSz,
  4401. ticket, inLen,
  4402. ticket,
  4403. mac);
  4404. #elif defined(HAVE_AESGCM)
  4405. ret = wc_AesGcmEncrypt(&tickCtx->aes, ticket, ticket, inLen,
  4406. iv, GCM_NONCE_MID_SZ, mac, AES_BLOCK_SIZE,
  4407. tickCtx->aad, aadSz);
  4408. #endif
  4409. }
  4410. /* decrypt */
  4411. else {
  4412. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  4413. ret = wc_ChaCha20Poly1305_Decrypt(myKey_ctx.key, iv,
  4414. tickCtx->aad, aadSz,
  4415. ticket, inLen,
  4416. mac,
  4417. ticket);
  4418. #elif defined(HAVE_AESGCM)
  4419. ret = wc_AesGcmDecrypt(&tickCtx->aes, ticket, ticket, inLen,
  4420. iv, GCM_NONCE_MID_SZ, mac, AES_BLOCK_SIZE,
  4421. tickCtx->aad, aadSz);
  4422. #endif
  4423. }
  4424. if (ret != 0) {
  4425. break;
  4426. }
  4427. tickCtx->state = MY_TICKET_STATE_FINAL;
  4428. }
  4429. FALL_THROUGH;
  4430. case MY_TICKET_STATE_FINAL:
  4431. *outLen = inLen; /* no padding in this mode */
  4432. break;
  4433. } /* switch */
  4434. #ifdef WOLFSSL_ASYNC_CRYPT
  4435. if (ret == WC_PENDING_E) {
  4436. return ret;
  4437. }
  4438. #endif
  4439. /* cleanup */
  4440. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  4441. #elif defined(HAVE_AESGCM)
  4442. wc_AesFree(&tickCtx->aes);
  4443. #endif
  4444. /* reset context */
  4445. XMEMSET(tickCtx, 0, sizeof(MyTicketCtx));
  4446. return (ret == 0) ? WOLFSSL_TICKET_RET_OK : WOLFSSL_TICKET_RET_REJECT;
  4447. }
  4448. #endif /* HAVE_SESSION_TICKET && ((HAVE_CHACHA && HAVE_POLY1305) || HAVE_AESGCM) */
  4449. static WC_INLINE word16 GetRandomPort(void)
  4450. {
  4451. word16 port = 0;
  4452. /* Generate random port for testing */
  4453. WC_RNG rng;
  4454. if (wc_InitRng(&rng) == 0) {
  4455. if (wc_RNG_GenerateBlock(&rng, (byte*)&port, sizeof(port)) == 0) {
  4456. port |= 0xC000; /* Make sure its in the 49152 - 65535 range */
  4457. }
  4458. wc_FreeRng(&rng);
  4459. }
  4460. (void)rng; /* for WC_NO_RNG case */
  4461. return port;
  4462. }
  4463. #ifdef WOLFSSL_EARLY_DATA
  4464. static WC_INLINE void EarlyDataStatus(WOLFSSL* ssl)
  4465. {
  4466. int earlyData_status;
  4467. #ifdef OPENSSL_EXTRA
  4468. earlyData_status = SSL_get_early_data_status(ssl);
  4469. #else
  4470. earlyData_status = wolfSSL_get_early_data_status(ssl);
  4471. #endif
  4472. if (earlyData_status < 0) return;
  4473. printf("Early Data was ");
  4474. switch(earlyData_status) {
  4475. case WOLFSSL_EARLY_DATA_NOT_SENT:
  4476. printf("not sent.\n");
  4477. break;
  4478. case WOLFSSL_EARLY_DATA_REJECTED:
  4479. printf("rejected.\n");
  4480. break;
  4481. case WOLFSSL_EARLY_DATA_ACCEPTED:
  4482. printf("accepted\n");
  4483. break;
  4484. default:
  4485. printf("unknown...\n");
  4486. }
  4487. }
  4488. #endif /* WOLFSSL_EARLY_DATA */
  4489. #if defined(HAVE_SESSION_TICKET) || defined (WOLFSSL_DTLS13)
  4490. static WC_INLINE int process_handshake_messages(WOLFSSL* ssl, int blocking,
  4491. int* zero_return)
  4492. {
  4493. char foo[1];
  4494. int ret = 0;
  4495. int dtls;
  4496. if (zero_return == NULL || ssl == NULL)
  4497. return -1;
  4498. dtls = wolfSSL_dtls(ssl);
  4499. (void)dtls;
  4500. *zero_return = 0;
  4501. if (!blocking) {
  4502. int timeout = DEFAULT_TIMEOUT_SEC;
  4503. #ifdef WOLFSSL_DTLS
  4504. if (dtls) {
  4505. timeout = wolfSSL_dtls_get_current_timeout(ssl);
  4506. #ifdef WOLFSSL_DTLS13
  4507. if (timeout > 4 && wolfSSL_dtls13_use_quick_timeout(ssl))
  4508. timeout /= 4;
  4509. #endif /* WOLFSSL_DTLS13 */
  4510. }
  4511. #endif /* WOLFSSL_DTLS */
  4512. ret = tcp_select(wolfSSL_get_fd(ssl), timeout);
  4513. if (ret == TEST_ERROR_READY) {
  4514. err_sys("tcp_select error");
  4515. return -1;
  4516. }
  4517. if (ret == TEST_TIMEOUT) {
  4518. #ifdef WOLFSSL_DTLS
  4519. if (dtls) {
  4520. ret = wolfSSL_dtls_got_timeout(ssl);
  4521. if (ret != WOLFSSL_SUCCESS && !wolfSSL_want_write(ssl) &&
  4522. !wolfSSL_want_read(ssl)) {
  4523. err_sys("got timeout error");
  4524. return -1;
  4525. }
  4526. }
  4527. #endif /* WOLFSSL_DTLS */
  4528. /* do the peek to detect if the peer closed the connection*/
  4529. }
  4530. }
  4531. ret = wolfSSL_peek(ssl, foo, 0);
  4532. if (ret < 0 && !wolfSSL_want_read(ssl) && !wolfSSL_want_write(ssl)) {
  4533. ret = wolfSSL_get_error(ssl, ret);
  4534. if (ret == WOLFSSL_ERROR_ZERO_RETURN)
  4535. *zero_return = 1;
  4536. return -1;
  4537. }
  4538. return 0;
  4539. }
  4540. #endif /* HAVE_SESSION_TICKET || WOLFSSL_DTLS13 */
  4541. static WC_INLINE void printBuffer(const byte *buf, int size)
  4542. {
  4543. int i;
  4544. for (i = 0; i < size; i++)
  4545. printf("%x", buf[i]);
  4546. }
  4547. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  4548. defined(DEBUG_UNIT_TEST_CERTS)
  4549. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName);
  4550. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName);
  4551. #endif
  4552. #define DTLS_CID_BUFFER_SIZE 256
  4553. #if !defined(NO_FILESYSTEM) && ( \
  4554. defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  4555. && defined(WOLFSSL_TLS13) && \
  4556. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))\
  4557. || \
  4558. (defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  4559. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)) \
  4560. || \
  4561. (defined(HAVE_SECURE_RENEGOTIATION) && \
  4562. !defined(NO_RSA) && \
  4563. defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && \
  4564. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  4565. defined(HAVE_AESGCM)) \
  4566. ) || \
  4567. (defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
  4568. !defined(WOLFSSL_TICKET_DECRYPT_NO_CREATE) && \
  4569. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  4570. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)) || \
  4571. (defined(WOLFSSL_EXTRA_ALERTS) && !defined(WOLFSSL_NO_TLS12) && \
  4572. !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  4573. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  4574. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT))
  4575. #define TEST_MEMIO_BUF_SZ (64 * 1024)
  4576. struct test_memio_ctx
  4577. {
  4578. byte c_buff[TEST_MEMIO_BUF_SZ];
  4579. int c_len;
  4580. const char* c_ciphers;
  4581. byte s_buff[TEST_MEMIO_BUF_SZ];
  4582. int s_len;
  4583. const char* s_ciphers;
  4584. };
  4585. static WC_INLINE int test_memio_write_cb(WOLFSSL *ssl, char *data, int sz,
  4586. void *ctx)
  4587. {
  4588. struct test_memio_ctx *test_ctx;
  4589. byte *buf;
  4590. int *len;
  4591. test_ctx = (struct test_memio_ctx*)ctx;
  4592. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  4593. buf = test_ctx->c_buff;
  4594. len = &test_ctx->c_len;
  4595. }
  4596. else {
  4597. buf = test_ctx->s_buff;
  4598. len = &test_ctx->s_len;
  4599. }
  4600. if ((unsigned)(*len + sz) > TEST_MEMIO_BUF_SZ)
  4601. return WOLFSSL_CBIO_ERR_WANT_READ;
  4602. XMEMCPY(buf + *len, data, sz);
  4603. *len += sz;
  4604. return sz;
  4605. }
  4606. static WC_INLINE int test_memio_read_cb(WOLFSSL *ssl, char *data, int sz,
  4607. void *ctx)
  4608. {
  4609. struct test_memio_ctx *test_ctx;
  4610. int read_sz;
  4611. byte *buf;
  4612. int *len;
  4613. test_ctx = (struct test_memio_ctx*)ctx;
  4614. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  4615. buf = test_ctx->s_buff;
  4616. len = &test_ctx->s_len;
  4617. }
  4618. else {
  4619. buf = test_ctx->c_buff;
  4620. len = &test_ctx->c_len;
  4621. }
  4622. if (*len == 0)
  4623. return WOLFSSL_CBIO_ERR_WANT_READ;
  4624. read_sz = sz < *len ? sz : *len;
  4625. XMEMCPY(data, buf, read_sz);
  4626. XMEMMOVE(buf, buf + read_sz, *len - read_sz);
  4627. *len -= read_sz;
  4628. return read_sz;
  4629. }
  4630. static WC_INLINE int test_memio_do_handshake(WOLFSSL *ssl_c, WOLFSSL *ssl_s,
  4631. int max_rounds, int *rounds)
  4632. {
  4633. byte handshake_complete = 0, hs_c = 0, hs_s = 0;
  4634. int ret, err;
  4635. if (rounds != NULL)
  4636. *rounds = 0;
  4637. while (!handshake_complete && max_rounds > 0) {
  4638. if (!hs_c) {
  4639. ret = wolfSSL_connect(ssl_c);
  4640. if (ret == WOLFSSL_SUCCESS) {
  4641. hs_c = 1;
  4642. }
  4643. else {
  4644. err = wolfSSL_get_error(ssl_c, ret);
  4645. if (err != WOLFSSL_ERROR_WANT_READ &&
  4646. err != WOLFSSL_ERROR_WANT_WRITE)
  4647. return -1;
  4648. }
  4649. }
  4650. if (!hs_s) {
  4651. ret = wolfSSL_accept(ssl_s);
  4652. if (ret == WOLFSSL_SUCCESS) {
  4653. hs_s = 1;
  4654. }
  4655. else {
  4656. err = wolfSSL_get_error(ssl_s, ret);
  4657. if (err != WOLFSSL_ERROR_WANT_READ &&
  4658. err != WOLFSSL_ERROR_WANT_WRITE)
  4659. return -1;
  4660. }
  4661. }
  4662. handshake_complete = hs_c && hs_s;
  4663. max_rounds--;
  4664. if (rounds != NULL)
  4665. *rounds = *rounds + 1;
  4666. }
  4667. if (!handshake_complete)
  4668. return -1;
  4669. return 0;
  4670. }
  4671. static WC_INLINE int test_memio_setup(struct test_memio_ctx *ctx,
  4672. WOLFSSL_CTX **ctx_c, WOLFSSL_CTX **ctx_s, WOLFSSL **ssl_c, WOLFSSL **ssl_s,
  4673. method_provider method_c, method_provider method_s)
  4674. {
  4675. int ret;
  4676. if (ctx_c != NULL && *ctx_c == NULL) {
  4677. *ctx_c = wolfSSL_CTX_new(method_c());
  4678. if (*ctx_c == NULL)
  4679. return -1;
  4680. #ifndef NO_CERTS
  4681. ret = wolfSSL_CTX_load_verify_locations(*ctx_c, caCertFile, 0);
  4682. if (ret != WOLFSSL_SUCCESS)
  4683. return -1;
  4684. #endif /* NO_CERTS */
  4685. wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
  4686. wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
  4687. if (ctx->c_ciphers != NULL) {
  4688. ret = wolfSSL_CTX_set_cipher_list(*ctx_c, ctx->c_ciphers);
  4689. if (ret != WOLFSSL_SUCCESS)
  4690. return -1;
  4691. }
  4692. }
  4693. if (ctx_s != NULL && *ctx_s == NULL) {
  4694. *ctx_s = wolfSSL_CTX_new(method_s());
  4695. if (*ctx_s == NULL)
  4696. return -1;
  4697. #ifndef NO_CERTS
  4698. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, svrKeyFile,
  4699. WOLFSSL_FILETYPE_PEM);
  4700. if (ret != WOLFSSL_SUCCESS)
  4701. return- -1;
  4702. ret = wolfSSL_CTX_use_certificate_file(*ctx_s, svrCertFile,
  4703. WOLFSSL_FILETYPE_PEM);
  4704. if (ret != WOLFSSL_SUCCESS)
  4705. return -1;
  4706. #endif
  4707. wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
  4708. wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
  4709. if (ctx->s_ciphers != NULL) {
  4710. ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
  4711. if (ret != WOLFSSL_SUCCESS)
  4712. return -1;
  4713. }
  4714. }
  4715. if (ctx_c != NULL && ssl_c != NULL) {
  4716. *ssl_c = wolfSSL_new(*ctx_c);
  4717. if (*ssl_c == NULL)
  4718. return -1;
  4719. wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
  4720. wolfSSL_SetIOReadCtx(*ssl_c, ctx);
  4721. }
  4722. if (ctx_s != NULL && ssl_s != NULL) {
  4723. *ssl_s = wolfSSL_new(*ctx_s);
  4724. if (*ssl_s == NULL)
  4725. return -1;
  4726. wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
  4727. wolfSSL_SetIOReadCtx(*ssl_s, ctx);
  4728. #if !defined(NO_DH)
  4729. SetDH(*ssl_s);
  4730. #endif
  4731. }
  4732. return 0;
  4733. }
  4734. #endif
  4735. #endif /* wolfSSL_TEST_H */