test.h 163 KB

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