tls.c 428 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069
  1. /* tls.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #ifndef WOLFCRYPT_ONLY
  26. #include <wolfssl/ssl.h>
  27. #include <wolfssl/internal.h>
  28. #include <wolfssl/error-ssl.h>
  29. #include <wolfssl/wolfcrypt/hash.h>
  30. #include <wolfssl/wolfcrypt/hmac.h>
  31. #include <wolfssl/wolfcrypt/kdf.h>
  32. #ifdef NO_INLINE
  33. #include <wolfssl/wolfcrypt/misc.h>
  34. #else
  35. #define WOLFSSL_MISC_INCLUDED
  36. #include <wolfcrypt/src/misc.c>
  37. #endif
  38. #ifdef HAVE_CURVE25519
  39. #include <wolfssl/wolfcrypt/curve25519.h>
  40. #endif
  41. #ifdef HAVE_CURVE448
  42. #include <wolfssl/wolfcrypt/curve448.h>
  43. #endif
  44. #ifdef HAVE_PQC
  45. #include <wolfssl/wolfcrypt/kyber.h>
  46. #ifdef WOLFSSL_WC_KYBER
  47. #include <wolfssl/wolfcrypt/wc_kyber.h>
  48. #elif defined(HAVE_LIBOQS) || defined(HAVE_PQM4)
  49. #include <wolfssl/wolfcrypt/ext_kyber.h>
  50. #endif
  51. #endif
  52. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  53. #include <wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h>
  54. #endif
  55. #include <wolfssl/wolfcrypt/hpke.h>
  56. #ifndef NO_TLS
  57. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  58. static int TLSX_KeyShare_IsSupported(int namedGroup);
  59. static void TLSX_KeyShare_FreeAll(KeyShareEntry* list, void* heap);
  60. #endif
  61. #ifdef HAVE_SUPPORTED_CURVES
  62. static int TLSX_PopulateSupportedGroups(WOLFSSL* ssl, TLSX** extensions);
  63. #endif
  64. /* Digest enable checks */
  65. #ifdef NO_OLD_TLS /* TLS 1.2 only */
  66. #if defined(NO_SHA256) && !defined(WOLFSSL_SHA384) && \
  67. !defined(WOLFSSL_SHA512)
  68. #error Must have SHA256, SHA384 or SHA512 enabled for TLS 1.2
  69. #endif
  70. #else /* TLS 1.1 or older */
  71. #if defined(NO_MD5) && defined(NO_SHA)
  72. #error Must have SHA1 and MD5 enabled for old TLS
  73. #endif
  74. #endif
  75. #ifdef WOLFSSL_TLS13
  76. #if !defined(NO_DH) && \
  77. !defined(HAVE_FFDHE_2048) && !defined(HAVE_FFDHE_3072) && \
  78. !defined(HAVE_FFDHE_4096) && !defined(HAVE_FFDHE_6144) && \
  79. !defined(HAVE_FFDHE_8192)
  80. #error Please configure your TLS 1.3 DH key size using either: HAVE_FFDHE_2048, HAVE_FFDHE_3072, HAVE_FFDHE_4096, HAVE_FFDHE_6144 or HAVE_FFDHE_8192
  81. #endif
  82. #if !defined(NO_RSA) && !defined(WC_RSA_PSS)
  83. #error The build option WC_RSA_PSS is required for TLS 1.3 with RSA
  84. #endif
  85. #ifndef HAVE_TLS_EXTENSIONS
  86. #ifndef _MSC_VER
  87. #error "The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3"
  88. #else
  89. #pragma message("Error: The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3")
  90. #endif
  91. #endif
  92. #endif
  93. /* Warn if secrets logging is enabled */
  94. #if defined(SHOW_SECRETS) || defined(WOLFSSL_SSLKEYLOGFILE)
  95. #ifndef _MSC_VER
  96. #warning The SHOW_SECRETS and WOLFSSL_SSLKEYLOGFILE options should only be used for debugging and never in a production environment
  97. #else
  98. #pragma message("Warning: The SHOW_SECRETS and WOLFSSL_SSLKEYLOGFILE options should only be used for debugging and never in a production environment")
  99. #endif
  100. #endif
  101. /* Optional Pre-Master-Secret logging for Wireshark */
  102. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_SSLKEYLOGFILE)
  103. #ifndef WOLFSSL_SSLKEYLOGFILE_OUTPUT
  104. #define WOLFSSL_SSLKEYLOGFILE_OUTPUT "sslkeylog.log"
  105. #endif
  106. #endif
  107. #ifndef WOLFSSL_NO_TLS12
  108. #ifdef WOLFSSL_SHA384
  109. #define HSHASH_SZ WC_SHA384_DIGEST_SIZE
  110. #else
  111. #define HSHASH_SZ FINISHED_SZ
  112. #endif
  113. int BuildTlsHandshakeHash(WOLFSSL* ssl, byte* hash, word32* hashLen)
  114. {
  115. int ret = 0;
  116. word32 hashSz = FINISHED_SZ;
  117. if (ssl == NULL || hash == NULL || hashLen == NULL || *hashLen < HSHASH_SZ)
  118. return BAD_FUNC_ARG;
  119. /* for constant timing perform these even if error */
  120. #ifndef NO_OLD_TLS
  121. ret |= wc_Md5GetHash(&ssl->hsHashes->hashMd5, hash);
  122. ret |= wc_ShaGetHash(&ssl->hsHashes->hashSha, &hash[WC_MD5_DIGEST_SIZE]);
  123. #endif
  124. if (IsAtLeastTLSv1_2(ssl)) {
  125. #ifndef NO_SHA256
  126. if (ssl->specs.mac_algorithm <= sha256_mac ||
  127. ssl->specs.mac_algorithm == blake2b_mac) {
  128. ret |= wc_Sha256GetHash(&ssl->hsHashes->hashSha256, hash);
  129. hashSz = WC_SHA256_DIGEST_SIZE;
  130. }
  131. #endif
  132. #ifdef WOLFSSL_SHA384
  133. if (ssl->specs.mac_algorithm == sha384_mac) {
  134. ret |= wc_Sha384GetHash(&ssl->hsHashes->hashSha384, hash);
  135. hashSz = WC_SHA384_DIGEST_SIZE;
  136. }
  137. #endif
  138. }
  139. *hashLen = hashSz;
  140. #ifdef WOLFSSL_CHECK_MEM_ZERO
  141. wc_MemZero_Add("TLS handshake hash", hash, hashSz);
  142. #endif
  143. if (ret != 0) {
  144. ret = BUILD_MSG_ERROR;
  145. WOLFSSL_ERROR_VERBOSE(ret);
  146. }
  147. return ret;
  148. }
  149. int BuildTlsFinished(WOLFSSL* ssl, Hashes* hashes, const byte* sender)
  150. {
  151. int ret;
  152. const byte* side = NULL;
  153. word32 hashSz = HSHASH_SZ;
  154. #if !defined(WOLFSSL_ASYNC_CRYPT) || defined(WC_ASYNC_NO_HASH)
  155. byte handshake_hash[HSHASH_SZ];
  156. #else
  157. WC_DECLARE_VAR(handshake_hash, byte, HSHASH_SZ, ssl->heap);
  158. if (handshake_hash == NULL)
  159. return MEMORY_E;
  160. #endif
  161. ret = BuildTlsHandshakeHash(ssl, handshake_hash, &hashSz);
  162. if (ret == 0) {
  163. if (XSTRNCMP((const char*)sender, (const char*)kTlsClientStr,
  164. SIZEOF_SENDER) == 0) {
  165. side = kTlsClientFinStr;
  166. }
  167. else if (XSTRNCMP((const char*)sender, (const char*)kTlsServerStr,
  168. SIZEOF_SENDER) == 0) {
  169. side = kTlsServerFinStr;
  170. }
  171. else {
  172. ret = BAD_FUNC_ARG;
  173. WOLFSSL_MSG("Unexpected sender value");
  174. }
  175. }
  176. if (ret == 0) {
  177. #ifdef WOLFSSL_HAVE_PRF
  178. #if !defined(NO_CERTS) && defined(HAVE_PK_CALLBACKS)
  179. if (ssl->ctx->TlsFinishedCb) {
  180. void* ctx = wolfSSL_GetTlsFinishedCtx(ssl);
  181. ret = ssl->ctx->TlsFinishedCb(ssl, side, handshake_hash, hashSz,
  182. (byte*)hashes, ctx);
  183. }
  184. if (!ssl->ctx->TlsFinishedCb || ret == PROTOCOLCB_UNAVAILABLE)
  185. #endif
  186. {
  187. PRIVATE_KEY_UNLOCK();
  188. ret = wc_PRF_TLS((byte*)hashes, TLS_FINISHED_SZ,
  189. ssl->arrays->masterSecret, SECRET_LEN, side,
  190. FINISHED_LABEL_SZ, handshake_hash, hashSz,
  191. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  192. ssl->heap, ssl->devId);
  193. PRIVATE_KEY_LOCK();
  194. }
  195. ForceZero(handshake_hash, hashSz);
  196. #else
  197. /* Pseudo random function must be enabled in the configuration. */
  198. ret = PRF_MISSING;
  199. WOLFSSL_ERROR_VERBOSE(ret);
  200. WOLFSSL_MSG("Pseudo-random function is not enabled");
  201. (void)side;
  202. (void)hashes;
  203. #endif
  204. }
  205. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  206. WC_FREE_VAR(handshake_hash, ssl->heap);
  207. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  208. wc_MemZero_Check(handshake_hash, HSHASH_SZ);
  209. #endif
  210. return ret;
  211. }
  212. #endif /* !WOLFSSL_NO_TLS12 */
  213. #ifndef NO_OLD_TLS
  214. #ifdef WOLFSSL_ALLOW_TLSV10
  215. ProtocolVersion MakeTLSv1(void)
  216. {
  217. ProtocolVersion pv;
  218. pv.major = SSLv3_MAJOR;
  219. pv.minor = TLSv1_MINOR;
  220. return pv;
  221. }
  222. #endif /* WOLFSSL_ALLOW_TLSV10 */
  223. ProtocolVersion MakeTLSv1_1(void)
  224. {
  225. ProtocolVersion pv;
  226. pv.major = SSLv3_MAJOR;
  227. pv.minor = TLSv1_1_MINOR;
  228. return pv;
  229. }
  230. #endif /* !NO_OLD_TLS */
  231. #ifndef WOLFSSL_NO_TLS12
  232. ProtocolVersion MakeTLSv1_2(void)
  233. {
  234. ProtocolVersion pv;
  235. pv.major = SSLv3_MAJOR;
  236. pv.minor = TLSv1_2_MINOR;
  237. return pv;
  238. }
  239. #endif /* !WOLFSSL_NO_TLS12 */
  240. #ifdef WOLFSSL_TLS13
  241. /* The TLS v1.3 protocol version.
  242. *
  243. * returns the protocol version data for TLS v1.3.
  244. */
  245. ProtocolVersion MakeTLSv1_3(void)
  246. {
  247. ProtocolVersion pv;
  248. pv.major = SSLv3_MAJOR;
  249. pv.minor = TLSv1_3_MINOR;
  250. return pv;
  251. }
  252. #endif
  253. #ifndef WOLFSSL_NO_TLS12
  254. #ifdef HAVE_EXTENDED_MASTER
  255. static const byte ext_master_label[EXT_MASTER_LABEL_SZ + 1] =
  256. "extended master secret";
  257. #endif
  258. static const byte master_label[MASTER_LABEL_SZ + 1] = "master secret";
  259. static const byte key_label [KEY_LABEL_SZ + 1] = "key expansion";
  260. static int _DeriveTlsKeys(byte* key_dig, word32 key_dig_len,
  261. const byte* ms, word32 msLen,
  262. const byte* sr, const byte* cr,
  263. int tls1_2, int hash_type,
  264. void* heap, int devId)
  265. {
  266. int ret;
  267. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  268. WC_DECLARE_VAR(seed, byte, SEED_LEN, heap);
  269. if (seed == NULL)
  270. return MEMORY_E;
  271. #else
  272. byte seed[SEED_LEN];
  273. #endif
  274. XMEMCPY(seed, sr, RAN_LEN);
  275. XMEMCPY(seed + RAN_LEN, cr, RAN_LEN);
  276. #ifdef WOLFSSL_HAVE_PRF
  277. PRIVATE_KEY_UNLOCK();
  278. ret = wc_PRF_TLS(key_dig, key_dig_len, ms, msLen, key_label, KEY_LABEL_SZ,
  279. seed, SEED_LEN, tls1_2, hash_type, heap, devId);
  280. PRIVATE_KEY_LOCK();
  281. #else
  282. /* Pseudo random function must be enabled in the configuration. */
  283. ret = PRF_MISSING;
  284. WOLFSSL_ERROR_VERBOSE(ret);
  285. WOLFSSL_MSG("Pseudo-random function is not enabled");
  286. (void)key_dig;
  287. (void)key_dig_len;
  288. (void)ms;
  289. (void)msLen;
  290. (void)tls1_2;
  291. (void)hash_type;
  292. (void)heap;
  293. (void)devId;
  294. (void)key_label;
  295. (void)master_label;
  296. #ifdef HAVE_EXTENDED_MASTER
  297. (void)ext_master_label;
  298. #endif
  299. #endif
  300. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  301. WC_FREE_VAR(seed, heap);
  302. #endif
  303. return ret;
  304. }
  305. /* External facing wrapper so user can call as well, 0 on success */
  306. int wolfSSL_DeriveTlsKeys(byte* key_dig, word32 key_dig_len,
  307. const byte* ms, word32 msLen,
  308. const byte* sr, const byte* cr,
  309. int tls1_2, int hash_type)
  310. {
  311. return _DeriveTlsKeys(key_dig, key_dig_len, ms, msLen, sr, cr, tls1_2,
  312. hash_type, NULL, INVALID_DEVID);
  313. }
  314. int DeriveTlsKeys(WOLFSSL* ssl)
  315. {
  316. int ret;
  317. int key_dig_len = 2 * ssl->specs.hash_size +
  318. 2 * ssl->specs.key_size +
  319. 2 * ssl->specs.iv_size;
  320. #ifdef WOLFSSL_SMALL_STACK
  321. byte* key_dig;
  322. #else
  323. byte key_dig[MAX_PRF_DIG];
  324. #endif
  325. #ifdef WOLFSSL_SMALL_STACK
  326. key_dig = (byte*)XMALLOC(MAX_PRF_DIG, ssl->heap, DYNAMIC_TYPE_DIGEST);
  327. if (key_dig == NULL) {
  328. return MEMORY_E;
  329. }
  330. #endif
  331. #if !defined(NO_CERTS) && defined(HAVE_PK_CALLBACKS)
  332. ret = PROTOCOLCB_UNAVAILABLE;
  333. if (ssl->ctx->GenSessionKeyCb) {
  334. void* ctx = wolfSSL_GetGenSessionKeyCtx(ssl);
  335. ret = ssl->ctx->GenSessionKeyCb(ssl, ctx);
  336. }
  337. if (!ssl->ctx->GenSessionKeyCb || ret == PROTOCOLCB_UNAVAILABLE)
  338. #endif
  339. ret = _DeriveTlsKeys(key_dig, key_dig_len,
  340. ssl->arrays->masterSecret, SECRET_LEN,
  341. ssl->arrays->serverRandom, ssl->arrays->clientRandom,
  342. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  343. ssl->heap, ssl->devId);
  344. if (ret == 0)
  345. ret = StoreKeys(ssl, key_dig, PROVISION_CLIENT_SERVER);
  346. #ifdef WOLFSSL_SMALL_STACK
  347. XFREE(key_dig, ssl->heap, DYNAMIC_TYPE_DIGEST);
  348. #endif
  349. return ret;
  350. }
  351. static int _MakeTlsMasterSecret(byte* ms, word32 msLen,
  352. const byte* pms, word32 pmsLen,
  353. const byte* cr, const byte* sr,
  354. int tls1_2, int hash_type,
  355. void* heap, int devId)
  356. {
  357. int ret;
  358. #if !defined(WOLFSSL_ASYNC_CRYPT) || defined(WC_ASYNC_NO_HASH)
  359. byte seed[SEED_LEN];
  360. #else
  361. WC_DECLARE_VAR(seed, byte, SEED_LEN, heap);
  362. if (seed == NULL)
  363. return MEMORY_E;
  364. #endif
  365. XMEMCPY(seed, cr, RAN_LEN);
  366. XMEMCPY(seed + RAN_LEN, sr, RAN_LEN);
  367. #ifdef WOLFSSL_HAVE_PRF
  368. PRIVATE_KEY_UNLOCK();
  369. ret = wc_PRF_TLS(ms, msLen, pms, pmsLen, master_label, MASTER_LABEL_SZ,
  370. seed, SEED_LEN, tls1_2, hash_type, heap, devId);
  371. PRIVATE_KEY_LOCK();
  372. #else
  373. /* Pseudo random function must be enabled in the configuration. */
  374. ret = PRF_MISSING;
  375. WOLFSSL_MSG("Pseudo-random function is not enabled");
  376. (void)ms;
  377. (void)msLen;
  378. (void)pms;
  379. (void)pmsLen;
  380. (void)tls1_2;
  381. (void)hash_type;
  382. (void)heap;
  383. (void)devId;
  384. #endif
  385. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  386. WC_FREE_VAR(seed, heap);
  387. #endif
  388. return ret;
  389. }
  390. /* External facing wrapper so user can call as well, 0 on success */
  391. int wolfSSL_MakeTlsMasterSecret(byte* ms, word32 msLen,
  392. const byte* pms, word32 pmsLen,
  393. const byte* cr, const byte* sr,
  394. int tls1_2, int hash_type)
  395. {
  396. return _MakeTlsMasterSecret(ms, msLen, pms, pmsLen, cr, sr, tls1_2,
  397. hash_type, NULL, INVALID_DEVID);
  398. }
  399. #ifdef HAVE_EXTENDED_MASTER
  400. static int _MakeTlsExtendedMasterSecret(byte* ms, word32 msLen,
  401. const byte* pms, word32 pmsLen,
  402. const byte* sHash, word32 sHashLen,
  403. int tls1_2, int hash_type,
  404. void* heap, int devId)
  405. {
  406. int ret;
  407. #ifdef WOLFSSL_HAVE_PRF
  408. PRIVATE_KEY_UNLOCK();
  409. ret = wc_PRF_TLS(ms, msLen, pms, pmsLen, ext_master_label, EXT_MASTER_LABEL_SZ,
  410. sHash, sHashLen, tls1_2, hash_type, heap, devId);
  411. PRIVATE_KEY_LOCK();
  412. #else
  413. /* Pseudo random function must be enabled in the configuration. */
  414. ret = PRF_MISSING;
  415. WOLFSSL_MSG("Pseudo-random function is not enabled");
  416. (void)ms;
  417. (void)msLen;
  418. (void)pms;
  419. (void)pmsLen;
  420. (void)sHash;
  421. (void)sHashLen;
  422. (void)tls1_2;
  423. (void)hash_type;
  424. (void)heap;
  425. (void)devId;
  426. #endif
  427. return ret;
  428. }
  429. /* External facing wrapper so user can call as well, 0 on success */
  430. int wolfSSL_MakeTlsExtendedMasterSecret(byte* ms, word32 msLen,
  431. const byte* pms, word32 pmsLen,
  432. const byte* sHash, word32 sHashLen,
  433. int tls1_2, int hash_type)
  434. {
  435. return _MakeTlsExtendedMasterSecret(ms, msLen, pms, pmsLen, sHash, sHashLen,
  436. tls1_2, hash_type, NULL, INVALID_DEVID);
  437. }
  438. #endif /* HAVE_EXTENDED_MASTER */
  439. int MakeTlsMasterSecret(WOLFSSL* ssl)
  440. {
  441. int ret;
  442. #ifdef HAVE_EXTENDED_MASTER
  443. if (ssl->options.haveEMS) {
  444. word32 hashSz = HSHASH_SZ;
  445. #ifdef WOLFSSL_SMALL_STACK
  446. byte* handshake_hash = (byte*)XMALLOC(HSHASH_SZ, ssl->heap,
  447. DYNAMIC_TYPE_DIGEST);
  448. if (handshake_hash == NULL)
  449. return MEMORY_E;
  450. #else
  451. byte handshake_hash[HSHASH_SZ];
  452. #endif
  453. ret = BuildTlsHandshakeHash(ssl, handshake_hash, &hashSz);
  454. if (ret == 0) {
  455. ret = _MakeTlsExtendedMasterSecret(
  456. ssl->arrays->masterSecret, SECRET_LEN,
  457. ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz,
  458. handshake_hash, hashSz,
  459. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  460. ssl->heap, ssl->devId);
  461. ForceZero(handshake_hash, hashSz);
  462. }
  463. #ifdef WOLFSSL_SMALL_STACK
  464. XFREE(handshake_hash, ssl->heap, DYNAMIC_TYPE_DIGEST);
  465. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  466. wc_MemZero_Check(handshake_hash, HSHASH_SZ);
  467. #endif
  468. }
  469. else
  470. #endif /* HAVE_EXTENDED_MASTER */
  471. {
  472. #if !defined(NO_CERTS) && defined(HAVE_PK_CALLBACKS)
  473. ret = PROTOCOLCB_UNAVAILABLE;
  474. if (ssl->ctx->GenMasterCb) {
  475. void* ctx = wolfSSL_GetGenMasterSecretCtx(ssl);
  476. ret = ssl->ctx->GenMasterCb(ssl, ctx);
  477. }
  478. if (!ssl->ctx->GenMasterCb || ret == PROTOCOLCB_UNAVAILABLE)
  479. #endif
  480. {
  481. ret = _MakeTlsMasterSecret(ssl->arrays->masterSecret,
  482. SECRET_LEN, ssl->arrays->preMasterSecret,
  483. ssl->arrays->preMasterSz, ssl->arrays->clientRandom,
  484. ssl->arrays->serverRandom, IsAtLeastTLSv1_2(ssl),
  485. ssl->specs.mac_algorithm, ssl->heap, ssl->devId);
  486. }
  487. }
  488. if (ret == 0) {
  489. #ifdef SHOW_SECRETS
  490. /* Wireshark Pre-Master-Secret Format:
  491. * CLIENT_RANDOM <clientrandom> <mastersecret>
  492. */
  493. const char* CLIENT_RANDOM_LABEL = "CLIENT_RANDOM";
  494. int i, pmsPos = 0;
  495. char pmsBuf[13 + 1 + 64 + 1 + 96 + 1 + 1];
  496. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%s ",
  497. CLIENT_RANDOM_LABEL);
  498. pmsPos += XSTRLEN(CLIENT_RANDOM_LABEL) + 1;
  499. for (i = 0; i < RAN_LEN; i++) {
  500. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%02x",
  501. ssl->arrays->clientRandom[i]);
  502. pmsPos += 2;
  503. }
  504. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, " ");
  505. pmsPos += 1;
  506. for (i = 0; i < SECRET_LEN; i++) {
  507. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%02x",
  508. ssl->arrays->masterSecret[i]);
  509. pmsPos += 2;
  510. }
  511. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "\n");
  512. pmsPos += 1;
  513. /* print master secret */
  514. puts(pmsBuf);
  515. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_SSLKEYLOGFILE)
  516. {
  517. FILE* f = XFOPEN(WOLFSSL_SSLKEYLOGFILE_OUTPUT, "a");
  518. if (f != XBADFILE) {
  519. XFWRITE(pmsBuf, 1, pmsPos, f);
  520. XFCLOSE(f);
  521. }
  522. }
  523. #endif
  524. #endif /* SHOW_SECRETS */
  525. ret = DeriveTlsKeys(ssl);
  526. }
  527. return ret;
  528. }
  529. /* Used by EAP-TLS and EAP-TTLS to derive keying material from
  530. * the master_secret. */
  531. int wolfSSL_make_eap_keys(WOLFSSL* ssl, void* msk, unsigned int len,
  532. const char* label)
  533. {
  534. int ret;
  535. #ifdef WOLFSSL_SMALL_STACK
  536. byte* seed;
  537. #else
  538. byte seed[SEED_LEN];
  539. #endif
  540. #ifdef WOLFSSL_SMALL_STACK
  541. seed = (byte*)XMALLOC(SEED_LEN, ssl->heap, DYNAMIC_TYPE_SEED);
  542. if (seed == NULL)
  543. return MEMORY_E;
  544. #endif
  545. /*
  546. * As per RFC-5281, the order of the client and server randoms is reversed
  547. * from that used by the TLS protocol to derive keys.
  548. */
  549. XMEMCPY(seed, ssl->arrays->clientRandom, RAN_LEN);
  550. XMEMCPY(seed + RAN_LEN, ssl->arrays->serverRandom, RAN_LEN);
  551. #ifdef WOLFSSL_HAVE_PRF
  552. PRIVATE_KEY_UNLOCK();
  553. ret = wc_PRF_TLS((byte*)msk, len, ssl->arrays->masterSecret, SECRET_LEN,
  554. (const byte *)label, (word32)XSTRLEN(label), seed, SEED_LEN,
  555. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  556. ssl->heap, ssl->devId);
  557. PRIVATE_KEY_LOCK();
  558. #else
  559. /* Pseudo random function must be enabled in the configuration. */
  560. ret = PRF_MISSING;
  561. WOLFSSL_MSG("Pseudo-random function is not enabled");
  562. (void)msk;
  563. (void)len;
  564. (void)label;
  565. #endif
  566. #ifdef WOLFSSL_SMALL_STACK
  567. XFREE(seed, ssl->heap, DYNAMIC_TYPE_SEED);
  568. #endif
  569. return ret;
  570. }
  571. /* return HMAC digest type in wolfSSL format */
  572. int wolfSSL_GetHmacType(WOLFSSL* ssl)
  573. {
  574. if (ssl == NULL)
  575. return BAD_FUNC_ARG;
  576. switch (ssl->specs.mac_algorithm) {
  577. #ifndef NO_MD5
  578. case md5_mac:
  579. {
  580. return WC_MD5;
  581. }
  582. #endif
  583. #ifndef NO_SHA256
  584. case sha256_mac:
  585. {
  586. return WC_SHA256;
  587. }
  588. #endif
  589. #ifdef WOLFSSL_SHA384
  590. case sha384_mac:
  591. {
  592. return WC_SHA384;
  593. }
  594. #endif
  595. #ifndef NO_SHA
  596. case sha_mac:
  597. {
  598. return WC_SHA;
  599. }
  600. #endif
  601. #ifdef HAVE_BLAKE2
  602. case blake2b_mac:
  603. {
  604. return BLAKE2B_ID;
  605. }
  606. #endif
  607. default:
  608. {
  609. return WOLFSSL_FATAL_ERROR;
  610. }
  611. }
  612. }
  613. int wolfSSL_SetTlsHmacInner(WOLFSSL* ssl, byte* inner, word32 sz, int content,
  614. int verify)
  615. {
  616. if (ssl == NULL || inner == NULL)
  617. return BAD_FUNC_ARG;
  618. XMEMSET(inner, 0, WOLFSSL_TLS_HMAC_INNER_SZ);
  619. WriteSEQ(ssl, verify, inner);
  620. inner[SEQ_SZ] = (byte)content;
  621. inner[SEQ_SZ + ENUM_LEN] = ssl->version.major;
  622. inner[SEQ_SZ + ENUM_LEN + ENUM_LEN] = ssl->version.minor;
  623. c16toa((word16)sz, inner + SEQ_SZ + ENUM_LEN + VERSION_SZ);
  624. return 0;
  625. }
  626. #ifndef WOLFSSL_AEAD_ONLY
  627. #if !defined(WOLFSSL_NO_HASH_RAW) && !defined(HAVE_FIPS) && \
  628. !defined(HAVE_SELFTEST)
  629. /* Update the hash in the HMAC.
  630. *
  631. * hmac HMAC object.
  632. * data Data to be hashed.
  633. * sz Size of data to hash.
  634. * returns 0 on success, otherwise failure.
  635. */
  636. static int Hmac_HashUpdate(Hmac* hmac, const byte* data, word32 sz)
  637. {
  638. int ret = BAD_FUNC_ARG;
  639. switch (hmac->macType) {
  640. #ifndef NO_SHA
  641. case WC_SHA:
  642. ret = wc_ShaUpdate(&hmac->hash.sha, data, sz);
  643. break;
  644. #endif /* !NO_SHA */
  645. #ifndef NO_SHA256
  646. case WC_SHA256:
  647. ret = wc_Sha256Update(&hmac->hash.sha256, data, sz);
  648. break;
  649. #endif /* !NO_SHA256 */
  650. #ifdef WOLFSSL_SHA384
  651. case WC_SHA384:
  652. ret = wc_Sha384Update(&hmac->hash.sha384, data, sz);
  653. break;
  654. #endif /* WOLFSSL_SHA384 */
  655. #ifdef WOLFSSL_SHA512
  656. case WC_SHA512:
  657. ret = wc_Sha512Update(&hmac->hash.sha512, data, sz);
  658. break;
  659. #endif /* WOLFSSL_SHA512 */
  660. default:
  661. break;
  662. }
  663. return ret;
  664. }
  665. /* Finalize the hash but don't put the EOC, padding or length in.
  666. *
  667. * hmac HMAC object.
  668. * hash Hash result.
  669. * returns 0 on success, otherwise failure.
  670. */
  671. static int Hmac_HashFinalRaw(Hmac* hmac, unsigned char* hash)
  672. {
  673. int ret = BAD_FUNC_ARG;
  674. switch (hmac->macType) {
  675. #ifndef NO_SHA
  676. case WC_SHA:
  677. ret = wc_ShaFinalRaw(&hmac->hash.sha, hash);
  678. break;
  679. #endif /* !NO_SHA */
  680. #ifndef NO_SHA256
  681. case WC_SHA256:
  682. ret = wc_Sha256FinalRaw(&hmac->hash.sha256, hash);
  683. break;
  684. #endif /* !NO_SHA256 */
  685. #ifdef WOLFSSL_SHA384
  686. case WC_SHA384:
  687. ret = wc_Sha384FinalRaw(&hmac->hash.sha384, hash);
  688. break;
  689. #endif /* WOLFSSL_SHA384 */
  690. #ifdef WOLFSSL_SHA512
  691. case WC_SHA512:
  692. ret = wc_Sha512FinalRaw(&hmac->hash.sha512, hash);
  693. break;
  694. #endif /* WOLFSSL_SHA512 */
  695. default:
  696. break;
  697. }
  698. return ret;
  699. }
  700. /* Finalize the HMAC by performing outer hash.
  701. *
  702. * hmac HMAC object.
  703. * mac MAC result.
  704. * returns 0 on success, otherwise failure.
  705. */
  706. static int Hmac_OuterHash(Hmac* hmac, unsigned char* mac)
  707. {
  708. int ret = BAD_FUNC_ARG;
  709. wc_HashAlg hash;
  710. enum wc_HashType hashType = (enum wc_HashType)hmac->macType;
  711. int digestSz = wc_HashGetDigestSize(hashType);
  712. int blockSz = wc_HashGetBlockSize(hashType);
  713. if ((digestSz >= 0) && (blockSz >= 0)) {
  714. ret = wc_HashInit(&hash, hashType);
  715. }
  716. if (ret == 0) {
  717. ret = wc_HashUpdate(&hash, hashType, (byte*)hmac->opad,
  718. blockSz);
  719. if (ret == 0)
  720. ret = wc_HashUpdate(&hash, hashType, (byte*)hmac->innerHash,
  721. digestSz);
  722. if (ret == 0)
  723. ret = wc_HashFinal(&hash, hashType, mac);
  724. wc_HashFree(&hash, hashType);
  725. }
  726. return ret;
  727. }
  728. /* Calculate the HMAC of the header + message data.
  729. * Constant time implementation using wc_Sha*FinalRaw().
  730. *
  731. * hmac HMAC object.
  732. * digest MAC result.
  733. * in Message data.
  734. * sz Size of the message data.
  735. * header Constructed record header with length of handshake data.
  736. * returns 0 on success, otherwise failure.
  737. */
  738. static int Hmac_UpdateFinal_CT(Hmac* hmac, byte* digest, const byte* in,
  739. word32 sz, int macLen, byte* header)
  740. {
  741. byte lenBytes[8];
  742. int i, j;
  743. unsigned int k;
  744. int blockBits, blockMask;
  745. int lastBlockLen, extraLen, eocIndex;
  746. int blocks, safeBlocks, lenBlock, eocBlock;
  747. unsigned int maxLen;
  748. int blockSz, padSz;
  749. int ret;
  750. word32 realLen;
  751. byte extraBlock;
  752. switch (hmac->macType) {
  753. #ifndef NO_SHA
  754. case WC_SHA:
  755. blockSz = WC_SHA_BLOCK_SIZE;
  756. blockBits = 6;
  757. padSz = WC_SHA_BLOCK_SIZE - WC_SHA_PAD_SIZE + 1;
  758. break;
  759. #endif /* !NO_SHA */
  760. #ifndef NO_SHA256
  761. case WC_SHA256:
  762. blockSz = WC_SHA256_BLOCK_SIZE;
  763. blockBits = 6;
  764. padSz = WC_SHA256_BLOCK_SIZE - WC_SHA256_PAD_SIZE + 1;
  765. break;
  766. #endif /* !NO_SHA256 */
  767. #ifdef WOLFSSL_SHA384
  768. case WC_SHA384:
  769. blockSz = WC_SHA384_BLOCK_SIZE;
  770. blockBits = 7;
  771. padSz = WC_SHA384_BLOCK_SIZE - WC_SHA384_PAD_SIZE + 1;
  772. break;
  773. #endif /* WOLFSSL_SHA384 */
  774. #ifdef WOLFSSL_SHA512
  775. case WC_SHA512:
  776. blockSz = WC_SHA512_BLOCK_SIZE;
  777. blockBits = 7;
  778. padSz = WC_SHA512_BLOCK_SIZE - WC_SHA512_PAD_SIZE + 1;
  779. break;
  780. #endif /* WOLFSSL_SHA512 */
  781. default:
  782. return BAD_FUNC_ARG;
  783. }
  784. blockMask = blockSz - 1;
  785. /* Size of data to HMAC if padding length byte is zero. */
  786. maxLen = WOLFSSL_TLS_HMAC_INNER_SZ + sz - 1 - macLen;
  787. /* Complete data (including padding) has block for EOC and/or length. */
  788. extraBlock = ctSetLTE((maxLen + padSz) & blockMask, padSz);
  789. /* Total number of blocks for data including padding. */
  790. blocks = ((maxLen + blockSz - 1) >> blockBits) + extraBlock;
  791. /* Up to last 6 blocks can be hashed safely. */
  792. safeBlocks = blocks - 6;
  793. /* Length of message data. */
  794. realLen = maxLen - in[sz - 1];
  795. /* Number of message bytes in last block. */
  796. lastBlockLen = realLen & blockMask;
  797. /* Number of padding bytes in last block. */
  798. extraLen = ((blockSz * 2 - padSz - lastBlockLen) & blockMask) + 1;
  799. /* Number of blocks to create for hash. */
  800. lenBlock = (realLen + extraLen) >> blockBits;
  801. /* Block containing EOC byte. */
  802. eocBlock = realLen >> blockBits;
  803. /* Index of EOC byte in block. */
  804. eocIndex = realLen & blockMask;
  805. /* Add length of hmac's ipad to total length. */
  806. realLen += blockSz;
  807. /* Length as bits - 8 bytes bigendian. */
  808. c32toa(realLen >> ((sizeof(word32) * 8) - 3), lenBytes);
  809. c32toa(realLen << 3, lenBytes + sizeof(word32));
  810. ret = Hmac_HashUpdate(hmac, (unsigned char*)hmac->ipad, blockSz);
  811. if (ret != 0)
  812. return ret;
  813. XMEMSET(hmac->innerHash, 0, macLen);
  814. if (safeBlocks > 0) {
  815. ret = Hmac_HashUpdate(hmac, header, WOLFSSL_TLS_HMAC_INNER_SZ);
  816. if (ret != 0)
  817. return ret;
  818. ret = Hmac_HashUpdate(hmac, in, safeBlocks * blockSz -
  819. WOLFSSL_TLS_HMAC_INNER_SZ);
  820. if (ret != 0)
  821. return ret;
  822. }
  823. else
  824. safeBlocks = 0;
  825. XMEMSET(digest, 0, macLen);
  826. k = safeBlocks * blockSz;
  827. for (i = safeBlocks; i < blocks; i++) {
  828. unsigned char hashBlock[WC_MAX_BLOCK_SIZE];
  829. unsigned char isEocBlock = ctMaskEq(i, eocBlock);
  830. unsigned char isOutBlock = ctMaskEq(i, lenBlock);
  831. for (j = 0; j < blockSz; j++) {
  832. unsigned char atEoc = ctMaskEq(j, eocIndex) & isEocBlock;
  833. unsigned char pastEoc = ctMaskGT(j, eocIndex) & isEocBlock;
  834. unsigned char b = 0;
  835. if (k < WOLFSSL_TLS_HMAC_INNER_SZ)
  836. b = header[k];
  837. else if (k < maxLen)
  838. b = in[k - WOLFSSL_TLS_HMAC_INNER_SZ];
  839. k++;
  840. b = ctMaskSel(atEoc, 0x80, b);
  841. b &= (unsigned char)~(word32)pastEoc;
  842. b &= ((unsigned char)~(word32)isOutBlock) | isEocBlock;
  843. if (j >= blockSz - 8) {
  844. b = ctMaskSel(isOutBlock, lenBytes[j - (blockSz - 8)], b);
  845. }
  846. hashBlock[j] = b;
  847. }
  848. ret = Hmac_HashUpdate(hmac, hashBlock, blockSz);
  849. if (ret != 0)
  850. return ret;
  851. ret = Hmac_HashFinalRaw(hmac, hashBlock);
  852. if (ret != 0)
  853. return ret;
  854. for (j = 0; j < macLen; j++)
  855. ((unsigned char*)hmac->innerHash)[j] |= hashBlock[j] & isOutBlock;
  856. }
  857. ret = Hmac_OuterHash(hmac, digest);
  858. return ret;
  859. }
  860. #endif
  861. #if defined(WOLFSSL_NO_HASH_RAW) || defined(HAVE_FIPS) || \
  862. defined(HAVE_SELFTEST) || defined(HAVE_BLAKE2)
  863. /* Calculate the HMAC of the header + message data.
  864. * Constant time implementation using normal hashing operations.
  865. * Update-Final need to be constant time.
  866. *
  867. * hmac HMAC object.
  868. * digest MAC result.
  869. * in Message data.
  870. * sz Size of the message data.
  871. * header Constructed record header with length of handshake data.
  872. * returns 0 on success, otherwise failure.
  873. */
  874. static int Hmac_UpdateFinal(Hmac* hmac, byte* digest, const byte* in,
  875. word32 sz, byte* header)
  876. {
  877. byte dummy[WC_MAX_BLOCK_SIZE] = {0};
  878. int ret;
  879. word32 msgSz, blockSz, macSz, padSz, maxSz, realSz;
  880. word32 currSz, offset = 0;
  881. int msgBlocks, blocks, blockBits;
  882. int i;
  883. switch (hmac->macType) {
  884. #ifndef NO_SHA
  885. case WC_SHA:
  886. blockSz = WC_SHA_BLOCK_SIZE;
  887. blockBits = 6;
  888. macSz = WC_SHA_DIGEST_SIZE;
  889. padSz = WC_SHA_BLOCK_SIZE - WC_SHA_PAD_SIZE + 1;
  890. break;
  891. #endif /* !NO_SHA */
  892. #ifndef NO_SHA256
  893. case WC_SHA256:
  894. blockSz = WC_SHA256_BLOCK_SIZE;
  895. blockBits = 6;
  896. macSz = WC_SHA256_DIGEST_SIZE;
  897. padSz = WC_SHA256_BLOCK_SIZE - WC_SHA256_PAD_SIZE + 1;
  898. break;
  899. #endif /* !NO_SHA256 */
  900. #ifdef WOLFSSL_SHA384
  901. case WC_SHA384:
  902. blockSz = WC_SHA384_BLOCK_SIZE;
  903. blockBits = 7;
  904. macSz = WC_SHA384_DIGEST_SIZE;
  905. padSz = WC_SHA384_BLOCK_SIZE - WC_SHA384_PAD_SIZE + 1;
  906. break;
  907. #endif /* WOLFSSL_SHA384 */
  908. #ifdef WOLFSSL_SHA512
  909. case WC_SHA512:
  910. blockSz = WC_SHA512_BLOCK_SIZE;
  911. blockBits = 7;
  912. macSz = WC_SHA512_DIGEST_SIZE;
  913. padSz = WC_SHA512_BLOCK_SIZE - WC_SHA512_PAD_SIZE + 1;
  914. break;
  915. #endif /* WOLFSSL_SHA512 */
  916. #ifdef HAVE_BLAKE2
  917. case WC_HASH_TYPE_BLAKE2B:
  918. blockSz = BLAKE2B_BLOCKBYTES;
  919. blockBits = 7;
  920. macSz = BLAKE2B_256;
  921. padSz = 0;
  922. break;
  923. #endif /* HAVE_BLAKE2 */
  924. default:
  925. return BAD_FUNC_ARG;
  926. }
  927. msgSz = sz - (1 + in[sz - 1] + macSz);
  928. /* Make negative result 0 */
  929. msgSz &= ~(0 - (msgSz >> 31));
  930. realSz = WOLFSSL_TLS_HMAC_INNER_SZ + msgSz;
  931. maxSz = WOLFSSL_TLS_HMAC_INNER_SZ + (sz - 1) - macSz;
  932. /* Calculate #blocks processed in HMAC for max and real data. */
  933. blocks = maxSz >> blockBits;
  934. blocks += ((maxSz + padSz) % blockSz) < padSz;
  935. msgBlocks = realSz >> blockBits;
  936. /* #Extra blocks to process. */
  937. blocks -= msgBlocks + ((((realSz + padSz) % blockSz) < padSz) ? 1 : 0);
  938. /* Calculate whole blocks. */
  939. msgBlocks--;
  940. ret = wc_HmacUpdate(hmac, header, WOLFSSL_TLS_HMAC_INNER_SZ);
  941. if (ret == 0) {
  942. /* Fill the rest of the block with any available data. */
  943. currSz = ctMaskLT(msgSz, blockSz) & msgSz;
  944. currSz |= ctMaskGTE(msgSz, blockSz) & blockSz;
  945. currSz -= WOLFSSL_TLS_HMAC_INNER_SZ;
  946. currSz &= ~(0 - (currSz >> 31));
  947. ret = wc_HmacUpdate(hmac, in, currSz);
  948. offset = currSz;
  949. }
  950. if (ret == 0) {
  951. /* Do the hash operations on a block basis. */
  952. for (i = 0; i < msgBlocks; i++, offset += blockSz) {
  953. ret = wc_HmacUpdate(hmac, in + offset, blockSz);
  954. if (ret != 0)
  955. break;
  956. }
  957. }
  958. if (ret == 0)
  959. ret = wc_HmacUpdate(hmac, in + offset, msgSz - offset);
  960. if (ret == 0)
  961. ret = wc_HmacFinal(hmac, digest);
  962. if (ret == 0) {
  963. /* Do the dummy hash operations. Do at least one. */
  964. for (i = 0; i < blocks + 1; i++) {
  965. ret = wc_HmacUpdate(hmac, dummy, blockSz);
  966. if (ret != 0)
  967. break;
  968. }
  969. }
  970. return ret;
  971. }
  972. #endif
  973. int TLS_hmac(WOLFSSL* ssl, byte* digest, const byte* in, word32 sz, int padSz,
  974. int content, int verify, int epochOrder)
  975. {
  976. Hmac hmac;
  977. byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
  978. int ret = 0;
  979. const byte* macSecret = NULL;
  980. word32 hashSz = 0;
  981. if (ssl == NULL)
  982. return BAD_FUNC_ARG;
  983. #ifdef HAVE_TRUNCATED_HMAC
  984. hashSz = ssl->truncated_hmac ? (byte)TRUNCATED_HMAC_SZ
  985. : ssl->specs.hash_size;
  986. #else
  987. hashSz = ssl->specs.hash_size;
  988. #endif
  989. #ifdef HAVE_FUZZER
  990. /* Fuzz "in" buffer with sz to be used in HMAC algorithm */
  991. if (ssl->fuzzerCb) {
  992. if (verify && padSz >= 0) {
  993. ssl->fuzzerCb(ssl, in, sz + hashSz + padSz + 1, FUZZ_HMAC,
  994. ssl->fuzzerCtx);
  995. }
  996. else {
  997. ssl->fuzzerCb(ssl, in, sz, FUZZ_HMAC, ssl->fuzzerCtx);
  998. }
  999. }
  1000. #endif
  1001. if (!ssl->options.dtls)
  1002. wolfSSL_SetTlsHmacInner(ssl, myInner, sz, content, verify);
  1003. else
  1004. wolfSSL_SetTlsHmacInner(ssl, myInner, sz, content, epochOrder);
  1005. ret = wc_HmacInit(&hmac, ssl->heap, ssl->devId);
  1006. if (ret != 0)
  1007. return ret;
  1008. #ifdef WOLFSSL_DTLS
  1009. if (ssl->options.dtls)
  1010. macSecret = wolfSSL_GetDtlsMacSecret(ssl, verify, epochOrder);
  1011. else
  1012. macSecret = wolfSSL_GetMacSecret(ssl, verify);
  1013. #else
  1014. macSecret = wolfSSL_GetMacSecret(ssl, verify);
  1015. #endif
  1016. ret = wc_HmacSetKey(&hmac, wolfSSL_GetHmacType(ssl),
  1017. macSecret,
  1018. ssl->specs.hash_size);
  1019. if (ret == 0) {
  1020. /* Constant time verification required. */
  1021. if (verify && padSz >= 0) {
  1022. #if !defined(WOLFSSL_NO_HASH_RAW) && !defined(HAVE_FIPS) && \
  1023. !defined(HAVE_SELFTEST)
  1024. #ifdef HAVE_BLAKE2
  1025. if (wolfSSL_GetHmacType(ssl) == WC_HASH_TYPE_BLAKE2B) {
  1026. ret = Hmac_UpdateFinal(&hmac, digest, in,
  1027. sz + hashSz + padSz + 1, myInner);
  1028. }
  1029. else
  1030. #endif
  1031. {
  1032. ret = Hmac_UpdateFinal_CT(&hmac, digest, in,
  1033. sz + hashSz + padSz + 1, hashSz, myInner);
  1034. }
  1035. #else
  1036. ret = Hmac_UpdateFinal(&hmac, digest, in, sz + hashSz + padSz + 1,
  1037. myInner);
  1038. #endif
  1039. }
  1040. else {
  1041. ret = wc_HmacUpdate(&hmac, myInner, sizeof(myInner));
  1042. if (ret == 0)
  1043. ret = wc_HmacUpdate(&hmac, in, sz); /* content */
  1044. if (ret == 0)
  1045. ret = wc_HmacFinal(&hmac, digest);
  1046. }
  1047. }
  1048. wc_HmacFree(&hmac);
  1049. return ret;
  1050. }
  1051. #endif /* WOLFSSL_AEAD_ONLY */
  1052. #endif /* !WOLFSSL_NO_TLS12 */
  1053. #ifdef HAVE_TLS_EXTENSIONS
  1054. /**
  1055. * The TLSX semaphore is used to calculate the size of the extensions to be sent
  1056. * from one peer to another.
  1057. */
  1058. /** Supports up to 72 flags. Increase as needed. */
  1059. #define SEMAPHORE_SIZE 9
  1060. /**
  1061. * Converts the extension type (id) to an index in the semaphore.
  1062. *
  1063. * Official reference for TLS extension types:
  1064. * http://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xml
  1065. *
  1066. * Motivation:
  1067. * Previously, we used the extension type itself as the index of that
  1068. * extension in the semaphore as the extension types were declared
  1069. * sequentially, but maintain a semaphore as big as the number of available
  1070. * extensions is no longer an option since the release of renegotiation_info.
  1071. *
  1072. * How to update:
  1073. * Assign extension types that extrapolate the number of available semaphores
  1074. * to the first available index going backwards in the semaphore array.
  1075. * When adding a new extension type that don't extrapolate the number of
  1076. * available semaphores, check for a possible collision with with a
  1077. * 'remapped' extension type.
  1078. *
  1079. * Update TLSX_Parse for duplicate detection if more added above 62.
  1080. */
  1081. static WC_INLINE word16 TLSX_ToSemaphore(word16 type)
  1082. {
  1083. switch (type) {
  1084. case TLSX_RENEGOTIATION_INFO: /* 0xFF01 */
  1085. return 63;
  1086. #ifdef WOLFSSL_QUIC
  1087. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT: /* 0xffa5 */
  1088. return 64;
  1089. #endif
  1090. #if defined(HAVE_ECH)
  1091. case TLSX_ECH: /* 0xfe0d */
  1092. return 65;
  1093. #endif
  1094. default:
  1095. if (type > 62) {
  1096. /* This message SHOULD only happens during the adding of
  1097. new TLS extensions in which its IANA number overflows
  1098. the current semaphore's range, or if its number already
  1099. is assigned to be used by another extension.
  1100. Use this check value for the new extension and decrement
  1101. the check value by one. */
  1102. WOLFSSL_MSG("### TLSX semaphore collision or overflow detected!");
  1103. }
  1104. }
  1105. return type;
  1106. }
  1107. /** Checks if a specific light (tls extension) is not set in the semaphore. */
  1108. #define IS_OFF(semaphore, light) \
  1109. (!(((semaphore)[(light) / 8] & (byte) (0x01 << ((light) % 8)))))
  1110. /** Turn on a specific light (tls extension) in the semaphore. */
  1111. /* the semaphore marks the extensions already written to the message */
  1112. #define TURN_ON(semaphore, light) \
  1113. ((semaphore)[(light) / 8] |= (byte) (0x01 << ((light) % 8)))
  1114. /** Turn off a specific light (tls extension) in the semaphore. */
  1115. #define TURN_OFF(semaphore, light) \
  1116. ((semaphore)[(light) / 8] &= (byte) ~(0x01 << ((light) % 8)))
  1117. /** Creates a new extension. */
  1118. static TLSX* TLSX_New(TLSX_Type type, const void* data, void* heap)
  1119. {
  1120. TLSX* extension = (TLSX*)XMALLOC(sizeof(TLSX), heap, DYNAMIC_TYPE_TLSX);
  1121. (void)heap;
  1122. if (extension) {
  1123. extension->type = type;
  1124. extension->data = (void*)data;
  1125. extension->resp = 0;
  1126. extension->next = NULL;
  1127. }
  1128. return extension;
  1129. }
  1130. /**
  1131. * Creates a new extension and pushes it to the provided list.
  1132. * Checks for duplicate extensions, keeps the newest.
  1133. */
  1134. int TLSX_Push(TLSX** list, TLSX_Type type, const void* data, void* heap)
  1135. {
  1136. TLSX* extension = TLSX_New(type, data, heap);
  1137. if (extension == NULL)
  1138. return MEMORY_E;
  1139. /* pushes the new extension on the list. */
  1140. extension->next = *list;
  1141. *list = extension;
  1142. /* remove duplicate extensions, there should be only one of each type. */
  1143. do {
  1144. if (extension->next && extension->next->type == type) {
  1145. TLSX *next = extension->next;
  1146. extension->next = next->next;
  1147. next->next = NULL;
  1148. TLSX_FreeAll(next, heap);
  1149. /* there is no way to occur more than
  1150. * two extensions of the same type.
  1151. */
  1152. break;
  1153. }
  1154. } while ((extension = extension->next));
  1155. return 0;
  1156. }
  1157. #ifdef WOLFSSL_TLS13
  1158. /**
  1159. * Creates a new extension and prepend it to the provided list.
  1160. * Checks for duplicate extensions, keeps the newest.
  1161. */
  1162. static int TLSX_Prepend(TLSX** list, TLSX_Type type, void* data, void* heap)
  1163. {
  1164. TLSX* extension = TLSX_New(type, data, heap);
  1165. TLSX* curr = *list;
  1166. if (extension == NULL)
  1167. return MEMORY_E;
  1168. /* remove duplicate extensions, there should be only one of each type. */
  1169. while (curr && curr->next) {
  1170. if (curr->next->type == type) {
  1171. TLSX *next = curr->next;
  1172. curr->next = next->next;
  1173. next->next = NULL;
  1174. TLSX_FreeAll(next, heap);
  1175. }
  1176. curr = curr->next;
  1177. }
  1178. if (curr)
  1179. curr->next = extension;
  1180. else
  1181. *list = extension;
  1182. return 0;
  1183. }
  1184. #endif /* WOLFSSL_TLS13 */
  1185. #ifndef NO_WOLFSSL_CLIENT
  1186. int TLSX_CheckUnsupportedExtension(WOLFSSL* ssl, TLSX_Type type);
  1187. int TLSX_CheckUnsupportedExtension(WOLFSSL* ssl, TLSX_Type type)
  1188. {
  1189. TLSX *extension = TLSX_Find(ssl->extensions, type);
  1190. if (!extension)
  1191. extension = TLSX_Find(ssl->ctx->extensions, type);
  1192. return extension == NULL;
  1193. }
  1194. int TLSX_HandleUnsupportedExtension(WOLFSSL* ssl);
  1195. int TLSX_HandleUnsupportedExtension(WOLFSSL* ssl)
  1196. {
  1197. SendAlert(ssl, alert_fatal, unsupported_extension);
  1198. WOLFSSL_ERROR_VERBOSE(UNSUPPORTED_EXTENSION);
  1199. return UNSUPPORTED_EXTENSION;
  1200. }
  1201. #else
  1202. #define TLSX_CheckUnsupportedExtension(ssl, type) 0
  1203. #define TLSX_HandleUnsupportedExtension(ssl) 0
  1204. #endif
  1205. #if !defined(NO_WOLFSSL_SERVER) || defined(WOLFSSL_TLS13)
  1206. /** Mark an extension to be sent back to the client. */
  1207. static void TLSX_SetResponse(WOLFSSL* ssl, TLSX_Type type)
  1208. {
  1209. TLSX *extension = TLSX_Find(ssl->extensions, type);
  1210. if (extension)
  1211. extension->resp = 1;
  1212. }
  1213. #endif
  1214. /******************************************************************************/
  1215. /* Application-Layer Protocol Negotiation */
  1216. /******************************************************************************/
  1217. #ifdef HAVE_ALPN
  1218. /** Creates a new ALPN object, providing protocol name to use. */
  1219. static ALPN* TLSX_ALPN_New(char *protocol_name, word16 protocol_nameSz,
  1220. void* heap)
  1221. {
  1222. ALPN *alpn;
  1223. WOLFSSL_ENTER("TLSX_ALPN_New");
  1224. if (protocol_name == NULL ||
  1225. protocol_nameSz > WOLFSSL_MAX_ALPN_PROTO_NAME_LEN) {
  1226. WOLFSSL_MSG("Invalid arguments");
  1227. return NULL;
  1228. }
  1229. alpn = (ALPN*)XMALLOC(sizeof(ALPN), heap, DYNAMIC_TYPE_TLSX);
  1230. if (alpn == NULL) {
  1231. WOLFSSL_MSG("Memory failure");
  1232. return NULL;
  1233. }
  1234. alpn->next = NULL;
  1235. alpn->negotiated = 0;
  1236. alpn->options = 0;
  1237. alpn->protocol_name = (char*)XMALLOC(protocol_nameSz + 1,
  1238. heap, DYNAMIC_TYPE_TLSX);
  1239. if (alpn->protocol_name == NULL) {
  1240. WOLFSSL_MSG("Memory failure");
  1241. XFREE(alpn, heap, DYNAMIC_TYPE_TLSX);
  1242. return NULL;
  1243. }
  1244. XMEMCPY(alpn->protocol_name, protocol_name, protocol_nameSz);
  1245. alpn->protocol_name[protocol_nameSz] = 0;
  1246. (void)heap;
  1247. return alpn;
  1248. }
  1249. /** Releases an ALPN object. */
  1250. static void TLSX_ALPN_Free(ALPN *alpn, void* heap)
  1251. {
  1252. (void)heap;
  1253. if (alpn == NULL)
  1254. return;
  1255. XFREE(alpn->protocol_name, heap, DYNAMIC_TYPE_TLSX);
  1256. XFREE(alpn, heap, DYNAMIC_TYPE_TLSX);
  1257. }
  1258. /** Releases all ALPN objects in the provided list. */
  1259. static void TLSX_ALPN_FreeAll(ALPN *list, void* heap)
  1260. {
  1261. ALPN* alpn;
  1262. while ((alpn = list)) {
  1263. list = alpn->next;
  1264. TLSX_ALPN_Free(alpn, heap);
  1265. }
  1266. }
  1267. /** Tells the buffered size of the ALPN objects in a list. */
  1268. static word16 TLSX_ALPN_GetSize(ALPN *list)
  1269. {
  1270. ALPN* alpn;
  1271. word16 length = OPAQUE16_LEN; /* list length */
  1272. while ((alpn = list)) {
  1273. list = alpn->next;
  1274. length++; /* protocol name length is on one byte */
  1275. length += (word16)XSTRLEN(alpn->protocol_name);
  1276. }
  1277. return length;
  1278. }
  1279. /** Writes the ALPN objects of a list in a buffer. */
  1280. static word16 TLSX_ALPN_Write(ALPN *list, byte *output)
  1281. {
  1282. ALPN* alpn;
  1283. word16 length = 0;
  1284. word16 offset = OPAQUE16_LEN; /* list length offset */
  1285. while ((alpn = list)) {
  1286. list = alpn->next;
  1287. length = (word16)XSTRLEN(alpn->protocol_name);
  1288. /* protocol name length */
  1289. output[offset++] = (byte)length;
  1290. /* protocol name value */
  1291. XMEMCPY(output + offset, alpn->protocol_name, length);
  1292. offset += length;
  1293. }
  1294. /* writing list length */
  1295. c16toa(offset - OPAQUE16_LEN, output);
  1296. return offset;
  1297. }
  1298. /** Finds a protocol name in the provided ALPN list */
  1299. static ALPN* TLSX_ALPN_Find(ALPN *list, char *protocol_name, word16 size)
  1300. {
  1301. ALPN *alpn;
  1302. if (list == NULL || protocol_name == NULL)
  1303. return NULL;
  1304. alpn = list;
  1305. while (alpn != NULL && (
  1306. (word16)XSTRLEN(alpn->protocol_name) != size ||
  1307. XSTRNCMP(alpn->protocol_name, protocol_name, size)))
  1308. alpn = alpn->next;
  1309. return alpn;
  1310. }
  1311. /** Set the ALPN matching client and server requirements */
  1312. static int TLSX_SetALPN(TLSX** extensions, const void* data, word16 size,
  1313. void* heap)
  1314. {
  1315. ALPN *alpn;
  1316. int ret;
  1317. if (extensions == NULL || data == NULL)
  1318. return BAD_FUNC_ARG;
  1319. alpn = TLSX_ALPN_New((char *)data, size, heap);
  1320. if (alpn == NULL) {
  1321. WOLFSSL_MSG("Memory failure");
  1322. return MEMORY_E;
  1323. }
  1324. alpn->negotiated = 1;
  1325. ret = TLSX_Push(extensions, TLSX_APPLICATION_LAYER_PROTOCOL, (void*)alpn,
  1326. heap);
  1327. if (ret != 0) {
  1328. TLSX_ALPN_Free(alpn, heap);
  1329. return ret;
  1330. }
  1331. return WOLFSSL_SUCCESS;
  1332. }
  1333. static int ALPN_find_match(WOLFSSL *ssl, TLSX **pextension,
  1334. const byte **psel, byte *psel_len,
  1335. const byte *alpn_val, word16 alpn_val_len)
  1336. {
  1337. TLSX *extension;
  1338. ALPN *alpn, *list;
  1339. const byte *sel = NULL, *s;
  1340. byte sel_len = 0, wlen;
  1341. extension = TLSX_Find(ssl->extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
  1342. if (extension == NULL)
  1343. extension = TLSX_Find(ssl->ctx->extensions,
  1344. TLSX_APPLICATION_LAYER_PROTOCOL);
  1345. /* No ALPN configured here */
  1346. if (extension == NULL || extension->data == NULL) {
  1347. *pextension = NULL;
  1348. *psel = NULL;
  1349. *psel_len = 0;
  1350. return 0;
  1351. }
  1352. list = (ALPN*)extension->data;
  1353. for (s = alpn_val;
  1354. (s - alpn_val) < alpn_val_len;
  1355. s += wlen) {
  1356. wlen = *s++; /* bounds already checked on save */
  1357. alpn = TLSX_ALPN_Find(list, (char*)s, wlen);
  1358. if (alpn != NULL) {
  1359. WOLFSSL_MSG("ALPN protocol match");
  1360. sel = s,
  1361. sel_len = wlen;
  1362. break;
  1363. }
  1364. }
  1365. if (sel == NULL) {
  1366. WOLFSSL_MSG("No ALPN protocol match");
  1367. /* do nothing if no protocol match between client and server and option
  1368. is set to continue (like OpenSSL) */
  1369. if (list->options & WOLFSSL_ALPN_CONTINUE_ON_MISMATCH) {
  1370. WOLFSSL_MSG("Continue on mismatch");
  1371. }
  1372. else {
  1373. SendAlert(ssl, alert_fatal, no_application_protocol);
  1374. WOLFSSL_ERROR_VERBOSE(UNKNOWN_ALPN_PROTOCOL_NAME_E);
  1375. return UNKNOWN_ALPN_PROTOCOL_NAME_E;
  1376. }
  1377. }
  1378. *pextension = extension;
  1379. *psel = sel;
  1380. *psel_len = sel_len;
  1381. return 0;
  1382. }
  1383. int ALPN_Select(WOLFSSL *ssl)
  1384. {
  1385. TLSX *extension;
  1386. const byte *sel = NULL;
  1387. byte sel_len = 0;
  1388. int r = 0;
  1389. WOLFSSL_ENTER("ALPN_Select");
  1390. if (ssl->alpn_peer_requested == NULL)
  1391. return 0;
  1392. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
  1393. if (ssl->alpnSelect != NULL && ssl->options.side == WOLFSSL_SERVER_END) {
  1394. if (ssl->alpnSelect(ssl, &sel, &sel_len, ssl->alpn_peer_requested,
  1395. ssl->alpn_peer_requested_length,
  1396. ssl->alpnSelectArg) == 0) {
  1397. WOLFSSL_MSG("ALPN protocol match");
  1398. }
  1399. else {
  1400. sel = NULL;
  1401. sel_len = 0;
  1402. }
  1403. }
  1404. #endif
  1405. if (sel == NULL) {
  1406. r = ALPN_find_match(ssl, &extension, &sel, &sel_len,
  1407. ssl->alpn_peer_requested,
  1408. ssl->alpn_peer_requested_length);
  1409. if (r != 0)
  1410. return r;
  1411. }
  1412. if (sel != NULL) {
  1413. /* set the matching negotiated protocol */
  1414. r = TLSX_SetALPN(&ssl->extensions, sel, sel_len, ssl->heap);
  1415. if (r != WOLFSSL_SUCCESS) {
  1416. WOLFSSL_MSG("TLSX_SetALPN failed");
  1417. return BUFFER_ERROR;
  1418. }
  1419. /* reply to ALPN extension sent from peer */
  1420. #ifndef NO_WOLFSSL_SERVER
  1421. TLSX_SetResponse(ssl, TLSX_APPLICATION_LAYER_PROTOCOL);
  1422. #endif
  1423. }
  1424. return 0;
  1425. }
  1426. /** Parses a buffer of ALPN extensions and set the first one matching
  1427. * client and server requirements */
  1428. static int TLSX_ALPN_ParseAndSet(WOLFSSL *ssl, const byte *input, word16 length,
  1429. byte isRequest)
  1430. {
  1431. word16 size = 0, offset = 0, wlen;
  1432. int r = BUFFER_ERROR;
  1433. const byte *s;
  1434. if (OPAQUE16_LEN > length)
  1435. return BUFFER_ERROR;
  1436. ato16(input, &size);
  1437. offset += OPAQUE16_LEN;
  1438. /* validating alpn list length */
  1439. if (size == 0 || length != OPAQUE16_LEN + size)
  1440. return BUFFER_ERROR;
  1441. /* validating length of entries before accepting */
  1442. for (s = input + offset; (s - input) < size; s += wlen) {
  1443. wlen = *s++;
  1444. if (wlen == 0 || (s + wlen - input) > length)
  1445. return BUFFER_ERROR;
  1446. }
  1447. if (isRequest) {
  1448. /* keep the list sent by peer, if this is from a request. We
  1449. * use it later in ALPN_Select() for evaluation. */
  1450. if (ssl->alpn_peer_requested != NULL) {
  1451. XFREE(ssl->alpn_peer_requested, ssl->heap, DYNAMIC_TYPE_ALPN);
  1452. ssl->alpn_peer_requested_length = 0;
  1453. }
  1454. ssl->alpn_peer_requested = (byte *)XMALLOC(size, ssl->heap,
  1455. DYNAMIC_TYPE_ALPN);
  1456. if (ssl->alpn_peer_requested == NULL) {
  1457. return MEMORY_ERROR;
  1458. }
  1459. ssl->alpn_peer_requested_length = size;
  1460. XMEMCPY(ssl->alpn_peer_requested, (char*)input + offset, size);
  1461. }
  1462. else {
  1463. /* a response, we should find the value in our config */
  1464. const byte *sel = NULL;
  1465. byte sel_len = 0;
  1466. TLSX *extension = NULL;
  1467. r = ALPN_find_match(ssl, &extension, &sel, &sel_len, input + offset, size);
  1468. if (r != 0)
  1469. return r;
  1470. if (sel != NULL) {
  1471. /* set the matching negotiated protocol */
  1472. r = TLSX_SetALPN(&ssl->extensions, sel, sel_len, ssl->heap);
  1473. if (r != WOLFSSL_SUCCESS) {
  1474. WOLFSSL_MSG("TLSX_SetALPN failed");
  1475. return BUFFER_ERROR;
  1476. }
  1477. }
  1478. /* If we had nothing configured, the response is unexpected */
  1479. else if (extension == NULL) {
  1480. r = TLSX_HandleUnsupportedExtension(ssl);
  1481. if (r != 0)
  1482. return r;
  1483. }
  1484. }
  1485. return 0;
  1486. }
  1487. /** Add a protocol name to the list of accepted usable ones */
  1488. int TLSX_UseALPN(TLSX** extensions, const void* data, word16 size, byte options,
  1489. void* heap)
  1490. {
  1491. ALPN *alpn;
  1492. TLSX *extension;
  1493. int ret;
  1494. if (extensions == NULL || data == NULL)
  1495. return BAD_FUNC_ARG;
  1496. alpn = TLSX_ALPN_New((char *)data, size, heap);
  1497. if (alpn == NULL) {
  1498. WOLFSSL_MSG("Memory failure");
  1499. return MEMORY_E;
  1500. }
  1501. /* Set Options of ALPN */
  1502. alpn->options = options;
  1503. extension = TLSX_Find(*extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
  1504. if (extension == NULL) {
  1505. ret = TLSX_Push(extensions, TLSX_APPLICATION_LAYER_PROTOCOL,
  1506. (void*)alpn, heap);
  1507. if (ret != 0) {
  1508. TLSX_ALPN_Free(alpn, heap);
  1509. return ret;
  1510. }
  1511. }
  1512. else {
  1513. /* push new ALPN object to extension data. */
  1514. alpn->next = (ALPN*)extension->data;
  1515. extension->data = (void*)alpn;
  1516. }
  1517. return WOLFSSL_SUCCESS;
  1518. }
  1519. /** Get the protocol name set by the server */
  1520. int TLSX_ALPN_GetRequest(TLSX* extensions, void** data, word16 *dataSz)
  1521. {
  1522. TLSX *extension;
  1523. ALPN *alpn;
  1524. if (extensions == NULL || data == NULL || dataSz == NULL)
  1525. return BAD_FUNC_ARG;
  1526. extension = TLSX_Find(extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
  1527. if (extension == NULL) {
  1528. WOLFSSL_MSG("TLS extension not found");
  1529. WOLFSSL_ERROR_VERBOSE(WOLFSSL_ALPN_NOT_FOUND);
  1530. return WOLFSSL_ALPN_NOT_FOUND;
  1531. }
  1532. alpn = (ALPN *)extension->data;
  1533. if (alpn == NULL) {
  1534. WOLFSSL_MSG("ALPN extension not found");
  1535. *data = NULL;
  1536. *dataSz = 0;
  1537. WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR);
  1538. return WOLFSSL_FATAL_ERROR;
  1539. }
  1540. if (alpn->negotiated != 1) {
  1541. /* consider as an error */
  1542. if (alpn->options & WOLFSSL_ALPN_FAILED_ON_MISMATCH) {
  1543. WOLFSSL_MSG("No protocol match with peer -> Failed");
  1544. WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR);
  1545. return WOLFSSL_FATAL_ERROR;
  1546. }
  1547. /* continue without negotiated protocol */
  1548. WOLFSSL_MSG("No protocol match with peer -> Continue");
  1549. WOLFSSL_ERROR_VERBOSE(WOLFSSL_ALPN_NOT_FOUND);
  1550. return WOLFSSL_ALPN_NOT_FOUND;
  1551. }
  1552. if (alpn->next != NULL) {
  1553. WOLFSSL_MSG("Only one protocol name must be accepted");
  1554. WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR);
  1555. return WOLFSSL_FATAL_ERROR;
  1556. }
  1557. *data = alpn->protocol_name;
  1558. *dataSz = (word16)XSTRLEN((char*)*data);
  1559. return WOLFSSL_SUCCESS;
  1560. }
  1561. #define ALPN_FREE_ALL TLSX_ALPN_FreeAll
  1562. #define ALPN_GET_SIZE TLSX_ALPN_GetSize
  1563. #define ALPN_WRITE TLSX_ALPN_Write
  1564. #define ALPN_PARSE TLSX_ALPN_ParseAndSet
  1565. #else /* HAVE_ALPN */
  1566. #define ALPN_FREE_ALL(list, heap)
  1567. #define ALPN_GET_SIZE(list) 0
  1568. #define ALPN_WRITE(a, b) 0
  1569. #define ALPN_PARSE(a, b, c, d) 0
  1570. #endif /* HAVE_ALPN */
  1571. /******************************************************************************/
  1572. /* Server Name Indication */
  1573. /******************************************************************************/
  1574. #ifdef HAVE_SNI
  1575. /** Creates a new SNI object. */
  1576. static SNI* TLSX_SNI_New(byte type, const void* data, word16 size, void* heap)
  1577. {
  1578. SNI* sni = (SNI*)XMALLOC(sizeof(SNI), heap, DYNAMIC_TYPE_TLSX);
  1579. (void)heap;
  1580. if (sni) {
  1581. sni->type = type;
  1582. sni->next = NULL;
  1583. #ifndef NO_WOLFSSL_SERVER
  1584. sni->options = 0;
  1585. sni->status = WOLFSSL_SNI_NO_MATCH;
  1586. #endif
  1587. switch (sni->type) {
  1588. case WOLFSSL_SNI_HOST_NAME:
  1589. sni->data.host_name = (char*)XMALLOC(size + 1, heap,
  1590. DYNAMIC_TYPE_TLSX);
  1591. if (sni->data.host_name) {
  1592. XSTRNCPY(sni->data.host_name, (const char*)data, size);
  1593. sni->data.host_name[size] = '\0';
  1594. } else {
  1595. XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
  1596. sni = NULL;
  1597. }
  1598. break;
  1599. default: /* invalid type */
  1600. XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
  1601. sni = NULL;
  1602. }
  1603. }
  1604. return sni;
  1605. }
  1606. /** Releases a SNI object. */
  1607. static void TLSX_SNI_Free(SNI* sni, void* heap)
  1608. {
  1609. if (sni) {
  1610. switch (sni->type) {
  1611. case WOLFSSL_SNI_HOST_NAME:
  1612. XFREE(sni->data.host_name, heap, DYNAMIC_TYPE_TLSX);
  1613. break;
  1614. }
  1615. XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
  1616. }
  1617. (void)heap;
  1618. }
  1619. /** Releases all SNI objects in the provided list. */
  1620. static void TLSX_SNI_FreeAll(SNI* list, void* heap)
  1621. {
  1622. SNI* sni;
  1623. while ((sni = list)) {
  1624. list = sni->next;
  1625. TLSX_SNI_Free(sni, heap);
  1626. }
  1627. }
  1628. /** Tells the buffered size of the SNI objects in a list. */
  1629. static word16 TLSX_SNI_GetSize(SNI* list)
  1630. {
  1631. SNI* sni;
  1632. word16 length = OPAQUE16_LEN; /* list length */
  1633. while ((sni = list)) {
  1634. list = sni->next;
  1635. length += ENUM_LEN + OPAQUE16_LEN; /* sni type + sni length */
  1636. switch (sni->type) {
  1637. case WOLFSSL_SNI_HOST_NAME:
  1638. length += (word16)XSTRLEN((char*)sni->data.host_name);
  1639. break;
  1640. }
  1641. }
  1642. return length;
  1643. }
  1644. /** Writes the SNI objects of a list in a buffer. */
  1645. static word16 TLSX_SNI_Write(SNI* list, byte* output)
  1646. {
  1647. SNI* sni;
  1648. word16 length = 0;
  1649. word16 offset = OPAQUE16_LEN; /* list length offset */
  1650. while ((sni = list)) {
  1651. list = sni->next;
  1652. output[offset++] = sni->type; /* sni type */
  1653. switch (sni->type) {
  1654. case WOLFSSL_SNI_HOST_NAME:
  1655. length = (word16)XSTRLEN((char*)sni->data.host_name);
  1656. c16toa(length, output + offset); /* sni length */
  1657. offset += OPAQUE16_LEN;
  1658. XMEMCPY(output + offset, sni->data.host_name, length);
  1659. offset += length;
  1660. break;
  1661. }
  1662. }
  1663. c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
  1664. return offset;
  1665. }
  1666. /** Finds a SNI object in the provided list. */
  1667. static SNI* TLSX_SNI_Find(SNI *list, byte type)
  1668. {
  1669. SNI* sni = list;
  1670. while (sni && sni->type != type)
  1671. sni = sni->next;
  1672. return sni;
  1673. }
  1674. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  1675. /** Sets the status of a SNI object. */
  1676. static void TLSX_SNI_SetStatus(TLSX* extensions, byte type, byte status)
  1677. {
  1678. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1679. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1680. if (sni)
  1681. sni->status = status;
  1682. }
  1683. #endif
  1684. /** Gets the status of a SNI object. */
  1685. byte TLSX_SNI_Status(TLSX* extensions, byte type)
  1686. {
  1687. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1688. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1689. if (sni)
  1690. return sni->status;
  1691. return 0;
  1692. }
  1693. /** Parses a buffer of SNI extensions. */
  1694. static int TLSX_SNI_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  1695. byte isRequest)
  1696. {
  1697. #ifndef NO_WOLFSSL_SERVER
  1698. word16 size = 0;
  1699. word16 offset = 0;
  1700. int cacheOnly = 0;
  1701. SNI *sni = NULL;
  1702. byte type;
  1703. int matchStat;
  1704. byte matched;
  1705. #if defined(HAVE_ECH)
  1706. WOLFSSL_ECH* ech = NULL;
  1707. WOLFSSL_EchConfig* workingConfig;
  1708. TLSX* echX;
  1709. #endif
  1710. #endif /* !NO_WOLFSSL_SERVER */
  1711. TLSX *extension = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
  1712. if (!extension)
  1713. extension = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
  1714. if (!isRequest) {
  1715. #ifndef NO_WOLFSSL_CLIENT
  1716. if (!extension || !extension->data)
  1717. return TLSX_HandleUnsupportedExtension(ssl);
  1718. if (length > 0)
  1719. return BUFFER_ERROR; /* SNI response MUST be empty. */
  1720. /* This call enables wolfSSL_SNI_GetRequest() to be called in the
  1721. * client side to fetch the used SNI. It will only work if the SNI
  1722. * was set at the SSL object level. Right now we only support one
  1723. * name type, WOLFSSL_SNI_HOST_NAME, but in the future, the
  1724. * inclusion of other name types will turn this method inaccurate,
  1725. * as the extension response doesn't contains information of which
  1726. * name was accepted.
  1727. */
  1728. TLSX_SNI_SetStatus(ssl->extensions, WOLFSSL_SNI_HOST_NAME,
  1729. WOLFSSL_SNI_REAL_MATCH);
  1730. return 0;
  1731. #endif
  1732. }
  1733. #ifndef NO_WOLFSSL_SERVER
  1734. if (!extension || !extension->data) {
  1735. /* This will keep SNI even though TLSX_UseSNI has not been called.
  1736. * Enable it so that the received sni is available to functions
  1737. * that use a custom callback when SNI is received.
  1738. */
  1739. #ifdef WOLFSSL_ALWAYS_KEEP_SNI
  1740. cacheOnly = 1;
  1741. #endif
  1742. if (ssl->ctx->sniRecvCb) {
  1743. cacheOnly = 1;
  1744. }
  1745. if (cacheOnly) {
  1746. WOLFSSL_MSG("Forcing SSL object to store SNI parameter");
  1747. }
  1748. else {
  1749. /* Skipping, SNI not enabled at server side. */
  1750. return 0;
  1751. }
  1752. }
  1753. if (OPAQUE16_LEN > length)
  1754. return BUFFER_ERROR;
  1755. ato16(input, &size);
  1756. offset += OPAQUE16_LEN;
  1757. /* validating sni list length */
  1758. if (length != OPAQUE16_LEN + size || size == 0)
  1759. return BUFFER_ERROR;
  1760. /* SNI was badly specified and only one type is now recognized and allowed.
  1761. * Only one SNI value per type (RFC6066), so, no loop. */
  1762. type = input[offset++];
  1763. if (type != WOLFSSL_SNI_HOST_NAME)
  1764. return BUFFER_ERROR;
  1765. if (offset + OPAQUE16_LEN > length)
  1766. return BUFFER_ERROR;
  1767. ato16(input + offset, &size);
  1768. offset += OPAQUE16_LEN;
  1769. if (offset + size != length || size == 0)
  1770. return BUFFER_ERROR;
  1771. if (!cacheOnly && !(sni = TLSX_SNI_Find((SNI*)extension->data, type)))
  1772. return 0; /* not using this type of SNI. */
  1773. #ifdef WOLFSSL_TLS13
  1774. /* Don't process the second ClientHello SNI extension if there
  1775. * was problems with the first.
  1776. */
  1777. if (!cacheOnly && sni->status != 0)
  1778. return 0;
  1779. #endif
  1780. matched = cacheOnly || (XSTRLEN(sni->data.host_name) == size &&
  1781. XSTRNCMP(sni->data.host_name, (const char*)input + offset, size) == 0);
  1782. #if defined(HAVE_ECH)
  1783. echX = TLSX_Find(ssl->extensions, TLSX_ECH);
  1784. if (echX != NULL)
  1785. ech = (WOLFSSL_ECH*)(echX->data);
  1786. if (!matched && ech != NULL) {
  1787. workingConfig = ech->echConfig;
  1788. while (workingConfig != NULL) {
  1789. matched = XSTRLEN(workingConfig->publicName) == size &&
  1790. XSTRNCMP(workingConfig->publicName,
  1791. (const char*)input + offset, size) == 0;
  1792. if (matched)
  1793. break;
  1794. workingConfig = workingConfig->next;
  1795. }
  1796. }
  1797. #endif
  1798. if (matched || sni->options & WOLFSSL_SNI_ANSWER_ON_MISMATCH) {
  1799. int r = TLSX_UseSNI(&ssl->extensions, type, input + offset, size,
  1800. ssl->heap);
  1801. if (r != WOLFSSL_SUCCESS)
  1802. return r; /* throws error. */
  1803. if (cacheOnly) {
  1804. WOLFSSL_MSG("Forcing storage of SNI, Fake match");
  1805. matchStat = WOLFSSL_SNI_FORCE_KEEP;
  1806. }
  1807. else if (matched) {
  1808. WOLFSSL_MSG("SNI did match!");
  1809. matchStat = WOLFSSL_SNI_REAL_MATCH;
  1810. }
  1811. else {
  1812. WOLFSSL_MSG("fake SNI match from ANSWER_ON_MISMATCH");
  1813. matchStat = WOLFSSL_SNI_FAKE_MATCH;
  1814. }
  1815. TLSX_SNI_SetStatus(ssl->extensions, type, (byte)matchStat);
  1816. if (!cacheOnly)
  1817. TLSX_SetResponse(ssl, TLSX_SERVER_NAME);
  1818. }
  1819. else if (!(sni->options & WOLFSSL_SNI_CONTINUE_ON_MISMATCH)) {
  1820. SendAlert(ssl, alert_fatal, unrecognized_name);
  1821. WOLFSSL_ERROR_VERBOSE(UNKNOWN_SNI_HOST_NAME_E);
  1822. return UNKNOWN_SNI_HOST_NAME_E;
  1823. }
  1824. #else
  1825. (void)input;
  1826. #endif /* !NO_WOLFSSL_SERVER */
  1827. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1828. (void)length;
  1829. #endif
  1830. return 0;
  1831. }
  1832. static int TLSX_SNI_VerifyParse(WOLFSSL* ssl, byte isRequest)
  1833. {
  1834. (void)ssl;
  1835. if (isRequest) {
  1836. #ifndef NO_WOLFSSL_SERVER
  1837. TLSX* ctx_ext = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
  1838. TLSX* ssl_ext = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
  1839. SNI* ctx_sni = ctx_ext ? (SNI*)ctx_ext->data : NULL;
  1840. SNI* ssl_sni = ssl_ext ? (SNI*)ssl_ext->data : NULL;
  1841. SNI* sni = NULL;
  1842. for (; ctx_sni; ctx_sni = ctx_sni->next) {
  1843. if (ctx_sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) {
  1844. sni = TLSX_SNI_Find(ssl_sni, ctx_sni->type);
  1845. if (sni) {
  1846. if (sni->status != WOLFSSL_SNI_NO_MATCH)
  1847. continue;
  1848. /* if ssl level overrides ctx level, it is ok. */
  1849. if ((sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) == 0)
  1850. continue;
  1851. }
  1852. SendAlert(ssl, alert_fatal, handshake_failure);
  1853. WOLFSSL_ERROR_VERBOSE(SNI_ABSENT_ERROR);
  1854. return SNI_ABSENT_ERROR;
  1855. }
  1856. }
  1857. for (; ssl_sni; ssl_sni = ssl_sni->next) {
  1858. if (ssl_sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) {
  1859. if (ssl_sni->status != WOLFSSL_SNI_NO_MATCH)
  1860. continue;
  1861. SendAlert(ssl, alert_fatal, handshake_failure);
  1862. WOLFSSL_ERROR_VERBOSE(SNI_ABSENT_ERROR);
  1863. return SNI_ABSENT_ERROR;
  1864. }
  1865. }
  1866. #endif /* NO_WOLFSSL_SERVER */
  1867. }
  1868. return 0;
  1869. }
  1870. int TLSX_UseSNI(TLSX** extensions, byte type, const void* data, word16 size,
  1871. void* heap)
  1872. {
  1873. TLSX* extension;
  1874. SNI* sni = NULL;
  1875. if (extensions == NULL || data == NULL)
  1876. return BAD_FUNC_ARG;
  1877. if ((sni = TLSX_SNI_New(type, data, size, heap)) == NULL)
  1878. return MEMORY_E;
  1879. extension = TLSX_Find(*extensions, TLSX_SERVER_NAME);
  1880. if (!extension) {
  1881. int ret = TLSX_Push(extensions, TLSX_SERVER_NAME, (void*)sni, heap);
  1882. if (ret != 0) {
  1883. TLSX_SNI_Free(sni, heap);
  1884. return ret;
  1885. }
  1886. }
  1887. else {
  1888. /* push new SNI object to extension data. */
  1889. sni->next = (SNI*)extension->data;
  1890. extension->data = (void*)sni;
  1891. /* remove duplicate SNI, there should be only one of each type. */
  1892. do {
  1893. if (sni->next && sni->next->type == type) {
  1894. SNI* next = sni->next;
  1895. sni->next = next->next;
  1896. TLSX_SNI_Free(next, heap);
  1897. /* there is no way to occur more than
  1898. * two SNIs of the same type.
  1899. */
  1900. break;
  1901. }
  1902. } while ((sni = sni->next));
  1903. }
  1904. return WOLFSSL_SUCCESS;
  1905. }
  1906. #ifndef NO_WOLFSSL_SERVER
  1907. /** Tells the SNI requested by the client. */
  1908. word16 TLSX_SNI_GetRequest(TLSX* extensions, byte type, void** data)
  1909. {
  1910. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1911. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1912. if (sni && sni->status != WOLFSSL_SNI_NO_MATCH) {
  1913. switch (sni->type) {
  1914. case WOLFSSL_SNI_HOST_NAME:
  1915. if (data) {
  1916. *data = sni->data.host_name;
  1917. return (word16)XSTRLEN((char*)*data);
  1918. }
  1919. }
  1920. }
  1921. return 0;
  1922. }
  1923. /** Sets the options for a SNI object. */
  1924. void TLSX_SNI_SetOptions(TLSX* extensions, byte type, byte options)
  1925. {
  1926. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1927. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1928. if (sni)
  1929. sni->options = options;
  1930. }
  1931. /** Retrieves a SNI request from a client hello buffer. */
  1932. int TLSX_SNI_GetFromBuffer(const byte* clientHello, word32 helloSz,
  1933. byte type, byte* sni, word32* inOutSz)
  1934. {
  1935. word32 offset = 0;
  1936. word32 len32 = 0;
  1937. word16 len16 = 0;
  1938. if (helloSz < RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ + CLIENT_HELLO_FIRST)
  1939. return INCOMPLETE_DATA;
  1940. /* TLS record header */
  1941. if ((enum ContentType) clientHello[offset++] != handshake) {
  1942. /* checking for SSLv2.0 client hello according to: */
  1943. /* http://tools.ietf.org/html/rfc4346#appendix-E.1 */
  1944. if ((enum HandShakeType) clientHello[++offset] == client_hello) {
  1945. offset += ENUM_LEN + VERSION_SZ; /* skip version */
  1946. ato16(clientHello + offset, &len16);
  1947. offset += OPAQUE16_LEN;
  1948. if (len16 % 3) /* cipher_spec_length must be multiple of 3 */
  1949. return BUFFER_ERROR;
  1950. ato16(clientHello + offset, &len16);
  1951. /* Returning SNI_UNSUPPORTED do not increment offset here */
  1952. if (len16 != 0) /* session_id_length must be 0 */
  1953. return BUFFER_ERROR;
  1954. WOLFSSL_ERROR_VERBOSE(SNI_UNSUPPORTED);
  1955. return SNI_UNSUPPORTED;
  1956. }
  1957. return BUFFER_ERROR;
  1958. }
  1959. if (clientHello[offset++] != SSLv3_MAJOR)
  1960. return BUFFER_ERROR;
  1961. if (clientHello[offset++] < TLSv1_MINOR) {
  1962. WOLFSSL_ERROR_VERBOSE(SNI_UNSUPPORTED);
  1963. return SNI_UNSUPPORTED;
  1964. }
  1965. ato16(clientHello + offset, &len16);
  1966. offset += OPAQUE16_LEN;
  1967. if (offset + len16 > helloSz)
  1968. return INCOMPLETE_DATA;
  1969. /* Handshake header */
  1970. if ((enum HandShakeType) clientHello[offset] != client_hello)
  1971. return BUFFER_ERROR;
  1972. c24to32(clientHello + offset + 1, &len32);
  1973. offset += HANDSHAKE_HEADER_SZ;
  1974. if (offset + len32 > helloSz)
  1975. return BUFFER_ERROR;
  1976. /* client hello */
  1977. offset += VERSION_SZ + RAN_LEN; /* version, random */
  1978. if (helloSz < offset + clientHello[offset])
  1979. return BUFFER_ERROR;
  1980. offset += ENUM_LEN + clientHello[offset]; /* skip session id */
  1981. /* cypher suites */
  1982. if (helloSz < offset + OPAQUE16_LEN)
  1983. return BUFFER_ERROR;
  1984. ato16(clientHello + offset, &len16);
  1985. offset += OPAQUE16_LEN;
  1986. if (helloSz < offset + len16)
  1987. return BUFFER_ERROR;
  1988. offset += len16; /* skip cypher suites */
  1989. /* compression methods */
  1990. if (helloSz < offset + 1)
  1991. return BUFFER_ERROR;
  1992. if (helloSz < offset + clientHello[offset])
  1993. return BUFFER_ERROR;
  1994. offset += ENUM_LEN + clientHello[offset]; /* skip compression methods */
  1995. /* extensions */
  1996. if (helloSz < offset + OPAQUE16_LEN)
  1997. return 0; /* no extensions in client hello. */
  1998. ato16(clientHello + offset, &len16);
  1999. offset += OPAQUE16_LEN;
  2000. if (helloSz < offset + len16)
  2001. return BUFFER_ERROR;
  2002. while (len16 >= OPAQUE16_LEN + OPAQUE16_LEN) {
  2003. word16 extType;
  2004. word16 extLen;
  2005. ato16(clientHello + offset, &extType);
  2006. offset += OPAQUE16_LEN;
  2007. ato16(clientHello + offset, &extLen);
  2008. offset += OPAQUE16_LEN;
  2009. if (helloSz < offset + extLen)
  2010. return BUFFER_ERROR;
  2011. if (extType != TLSX_SERVER_NAME) {
  2012. offset += extLen; /* skip extension */
  2013. } else {
  2014. word16 listLen;
  2015. ato16(clientHello + offset, &listLen);
  2016. offset += OPAQUE16_LEN;
  2017. if (helloSz < offset + listLen)
  2018. return BUFFER_ERROR;
  2019. while (listLen > ENUM_LEN + OPAQUE16_LEN) {
  2020. byte sniType = clientHello[offset++];
  2021. word16 sniLen;
  2022. ato16(clientHello + offset, &sniLen);
  2023. offset += OPAQUE16_LEN;
  2024. if (helloSz < offset + sniLen)
  2025. return BUFFER_ERROR;
  2026. if (sniType != type) {
  2027. offset += sniLen;
  2028. listLen -= min(ENUM_LEN + OPAQUE16_LEN + sniLen, listLen);
  2029. continue;
  2030. }
  2031. *inOutSz = min(sniLen, *inOutSz);
  2032. XMEMCPY(sni, clientHello + offset, *inOutSz);
  2033. return WOLFSSL_SUCCESS;
  2034. }
  2035. }
  2036. len16 -= min(2 * OPAQUE16_LEN + extLen, len16);
  2037. }
  2038. return len16 ? BUFFER_ERROR : 0;
  2039. }
  2040. #endif
  2041. #define SNI_FREE_ALL TLSX_SNI_FreeAll
  2042. #define SNI_GET_SIZE TLSX_SNI_GetSize
  2043. #define SNI_WRITE TLSX_SNI_Write
  2044. #define SNI_PARSE TLSX_SNI_Parse
  2045. #define SNI_VERIFY_PARSE TLSX_SNI_VerifyParse
  2046. #else
  2047. #define SNI_FREE_ALL(list, heap)
  2048. #define SNI_GET_SIZE(list) 0
  2049. #define SNI_WRITE(a, b) 0
  2050. #define SNI_PARSE(a, b, c, d) 0
  2051. #define SNI_VERIFY_PARSE(a, b) 0
  2052. #endif /* HAVE_SNI */
  2053. /******************************************************************************/
  2054. /* Trusted CA Key Indication */
  2055. /******************************************************************************/
  2056. #ifdef HAVE_TRUSTED_CA
  2057. /** Creates a new TCA object. */
  2058. static TCA* TLSX_TCA_New(byte type, const byte* id, word16 idSz, void* heap)
  2059. {
  2060. TCA* tca = (TCA*)XMALLOC(sizeof(TCA), heap, DYNAMIC_TYPE_TLSX);
  2061. if (tca) {
  2062. XMEMSET(tca, 0, sizeof(TCA));
  2063. tca->type = type;
  2064. switch (type) {
  2065. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2066. break;
  2067. #ifndef NO_SHA
  2068. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2069. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2070. if (idSz == WC_SHA_DIGEST_SIZE &&
  2071. (tca->id =
  2072. (byte*)XMALLOC(idSz, heap, DYNAMIC_TYPE_TLSX))) {
  2073. XMEMCPY(tca->id, id, idSz);
  2074. tca->idSz = idSz;
  2075. }
  2076. else {
  2077. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2078. tca = NULL;
  2079. }
  2080. break;
  2081. #endif
  2082. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2083. if (idSz > 0 &&
  2084. (tca->id =
  2085. (byte*)XMALLOC(idSz, heap, DYNAMIC_TYPE_TLSX))) {
  2086. XMEMCPY(tca->id, id, idSz);
  2087. tca->idSz = idSz;
  2088. }
  2089. else {
  2090. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2091. tca = NULL;
  2092. }
  2093. break;
  2094. default: /* invalid type */
  2095. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2096. tca = NULL;
  2097. }
  2098. }
  2099. (void)heap;
  2100. return tca;
  2101. }
  2102. /** Releases a TCA object. */
  2103. static void TLSX_TCA_Free(TCA* tca, void* heap)
  2104. {
  2105. (void)heap;
  2106. if (tca) {
  2107. if (tca->id)
  2108. XFREE(tca->id, heap, DYNAMIC_TYPE_TLSX);
  2109. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2110. }
  2111. }
  2112. /** Releases all TCA objects in the provided list. */
  2113. static void TLSX_TCA_FreeAll(TCA* list, void* heap)
  2114. {
  2115. TCA* tca;
  2116. while ((tca = list)) {
  2117. list = tca->next;
  2118. TLSX_TCA_Free(tca, heap);
  2119. }
  2120. }
  2121. /** Tells the buffered size of the TCA objects in a list. */
  2122. static word16 TLSX_TCA_GetSize(TCA* list)
  2123. {
  2124. TCA* tca;
  2125. word16 length = OPAQUE16_LEN; /* list length */
  2126. while ((tca = list)) {
  2127. list = tca->next;
  2128. length += ENUM_LEN; /* tca type */
  2129. switch (tca->type) {
  2130. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2131. break;
  2132. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2133. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2134. length += tca->idSz;
  2135. break;
  2136. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2137. length += OPAQUE16_LEN + tca->idSz;
  2138. break;
  2139. }
  2140. }
  2141. return length;
  2142. }
  2143. /** Writes the TCA objects of a list in a buffer. */
  2144. static word16 TLSX_TCA_Write(TCA* list, byte* output)
  2145. {
  2146. TCA* tca;
  2147. word16 offset = OPAQUE16_LEN; /* list length offset */
  2148. while ((tca = list)) {
  2149. list = tca->next;
  2150. output[offset++] = tca->type; /* tca type */
  2151. switch (tca->type) {
  2152. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2153. break;
  2154. #ifndef NO_SHA
  2155. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2156. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2157. if (tca->id != NULL) {
  2158. XMEMCPY(output + offset, tca->id, tca->idSz);
  2159. offset += tca->idSz;
  2160. }
  2161. else {
  2162. /* ID missing. Set to an empty string. */
  2163. c16toa(0, output + offset);
  2164. offset += OPAQUE16_LEN;
  2165. }
  2166. break;
  2167. #endif
  2168. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2169. if (tca->id != NULL) {
  2170. c16toa(tca->idSz, output + offset); /* tca length */
  2171. offset += OPAQUE16_LEN;
  2172. XMEMCPY(output + offset, tca->id, tca->idSz);
  2173. offset += tca->idSz;
  2174. }
  2175. else {
  2176. /* ID missing. Set to an empty string. */
  2177. c16toa(0, output + offset);
  2178. offset += OPAQUE16_LEN;
  2179. }
  2180. break;
  2181. default:
  2182. /* ID unknown. Set to an empty string. */
  2183. c16toa(0, output + offset);
  2184. offset += OPAQUE16_LEN;
  2185. }
  2186. }
  2187. c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
  2188. return offset;
  2189. }
  2190. #ifndef NO_WOLFSSL_SERVER
  2191. static TCA* TLSX_TCA_Find(TCA *list, byte type, const byte* id, word16 idSz)
  2192. {
  2193. TCA* tca = list;
  2194. while (tca && tca->type != type && type != WOLFSSL_TRUSTED_CA_PRE_AGREED &&
  2195. idSz != tca->idSz && !XMEMCMP(id, tca->id, idSz))
  2196. tca = tca->next;
  2197. return tca;
  2198. }
  2199. #endif /* NO_WOLFSSL_SERVER */
  2200. /** Parses a buffer of TCA extensions. */
  2201. static int TLSX_TCA_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2202. byte isRequest)
  2203. {
  2204. #ifndef NO_WOLFSSL_SERVER
  2205. word16 size = 0;
  2206. word16 offset = 0;
  2207. #endif
  2208. TLSX *extension = TLSX_Find(ssl->extensions, TLSX_TRUSTED_CA_KEYS);
  2209. if (!extension)
  2210. extension = TLSX_Find(ssl->ctx->extensions, TLSX_TRUSTED_CA_KEYS);
  2211. if (!isRequest) {
  2212. #ifndef NO_WOLFSSL_CLIENT
  2213. if (!extension || !extension->data)
  2214. return TLSX_HandleUnsupportedExtension(ssl);
  2215. if (length > 0)
  2216. return BUFFER_ERROR; /* TCA response MUST be empty. */
  2217. /* Set the flag that we're good for keys */
  2218. TLSX_SetResponse(ssl, TLSX_TRUSTED_CA_KEYS);
  2219. return 0;
  2220. #endif
  2221. }
  2222. #ifndef NO_WOLFSSL_SERVER
  2223. if (!extension || !extension->data) {
  2224. /* Skipping, TCA not enabled at server side. */
  2225. return 0;
  2226. }
  2227. if (OPAQUE16_LEN > length)
  2228. return BUFFER_ERROR;
  2229. ato16(input, &size);
  2230. offset += OPAQUE16_LEN;
  2231. /* validating tca list length */
  2232. if (length != OPAQUE16_LEN + size)
  2233. return BUFFER_ERROR;
  2234. for (size = 0; offset < length; offset += size) {
  2235. TCA *tca = NULL;
  2236. byte type;
  2237. const byte* id = NULL;
  2238. word16 idSz = 0;
  2239. if (offset + ENUM_LEN > length)
  2240. return BUFFER_ERROR;
  2241. type = input[offset++];
  2242. switch (type) {
  2243. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2244. break;
  2245. #ifndef NO_SHA
  2246. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2247. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2248. if (offset + WC_SHA_DIGEST_SIZE > length)
  2249. return BUFFER_ERROR;
  2250. idSz = WC_SHA_DIGEST_SIZE;
  2251. id = input + offset;
  2252. offset += idSz;
  2253. break;
  2254. #endif
  2255. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2256. if (offset + OPAQUE16_LEN > length)
  2257. return BUFFER_ERROR;
  2258. ato16(input + offset, &idSz);
  2259. offset += OPAQUE16_LEN;
  2260. if ((offset > length) || (idSz > length - offset))
  2261. return BUFFER_ERROR;
  2262. id = input + offset;
  2263. offset += idSz;
  2264. break;
  2265. default:
  2266. WOLFSSL_ERROR_VERBOSE(TCA_INVALID_ID_TYPE);
  2267. return TCA_INVALID_ID_TYPE;
  2268. }
  2269. /* Find the type/ID in the TCA list. */
  2270. tca = TLSX_TCA_Find((TCA*)extension->data, type, id, idSz);
  2271. if (tca != NULL) {
  2272. /* Found it. Set the response flag and break out of the loop. */
  2273. TLSX_SetResponse(ssl, TLSX_TRUSTED_CA_KEYS);
  2274. break;
  2275. }
  2276. }
  2277. #else
  2278. (void)input;
  2279. #endif
  2280. return 0;
  2281. }
  2282. /* Checks to see if the server sent a response for the TCA. */
  2283. static int TLSX_TCA_VerifyParse(WOLFSSL* ssl, byte isRequest)
  2284. {
  2285. (void)ssl;
  2286. if (!isRequest) {
  2287. #ifndef NO_WOLFSSL_CLIENT
  2288. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_TRUSTED_CA_KEYS);
  2289. if (extension && !extension->resp) {
  2290. SendAlert(ssl, alert_fatal, handshake_failure);
  2291. WOLFSSL_ERROR_VERBOSE(TCA_ABSENT_ERROR);
  2292. return TCA_ABSENT_ERROR;
  2293. }
  2294. #endif /* NO_WOLFSSL_CLIENT */
  2295. }
  2296. return 0;
  2297. }
  2298. int TLSX_UseTrustedCA(TLSX** extensions, byte type,
  2299. const byte* id, word16 idSz, void* heap)
  2300. {
  2301. TLSX* extension;
  2302. TCA* tca = NULL;
  2303. if (extensions == NULL)
  2304. return BAD_FUNC_ARG;
  2305. if ((tca = TLSX_TCA_New(type, id, idSz, heap)) == NULL)
  2306. return MEMORY_E;
  2307. extension = TLSX_Find(*extensions, TLSX_TRUSTED_CA_KEYS);
  2308. if (!extension) {
  2309. int ret = TLSX_Push(extensions, TLSX_TRUSTED_CA_KEYS, (void*)tca, heap);
  2310. if (ret != 0) {
  2311. TLSX_TCA_Free(tca, heap);
  2312. return ret;
  2313. }
  2314. }
  2315. else {
  2316. /* push new TCA object to extension data. */
  2317. tca->next = (TCA*)extension->data;
  2318. extension->data = (void*)tca;
  2319. }
  2320. return WOLFSSL_SUCCESS;
  2321. }
  2322. #define TCA_FREE_ALL TLSX_TCA_FreeAll
  2323. #define TCA_GET_SIZE TLSX_TCA_GetSize
  2324. #define TCA_WRITE TLSX_TCA_Write
  2325. #define TCA_PARSE TLSX_TCA_Parse
  2326. #define TCA_VERIFY_PARSE TLSX_TCA_VerifyParse
  2327. #else /* HAVE_TRUSTED_CA */
  2328. #define TCA_FREE_ALL(list, heap)
  2329. #define TCA_GET_SIZE(list) 0
  2330. #define TCA_WRITE(a, b) 0
  2331. #define TCA_PARSE(a, b, c, d) 0
  2332. #define TCA_VERIFY_PARSE(a, b) 0
  2333. #endif /* HAVE_TRUSTED_CA */
  2334. /******************************************************************************/
  2335. /* Max Fragment Length Negotiation */
  2336. /******************************************************************************/
  2337. #ifdef HAVE_MAX_FRAGMENT
  2338. static word16 TLSX_MFL_Write(byte* data, byte* output)
  2339. {
  2340. output[0] = data[0];
  2341. return ENUM_LEN;
  2342. }
  2343. static int TLSX_MFL_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2344. byte isRequest)
  2345. {
  2346. if (length != ENUM_LEN)
  2347. return BUFFER_ERROR;
  2348. #ifdef WOLFSSL_OLD_UNSUPPORTED_EXTENSION
  2349. (void) isRequest;
  2350. #else
  2351. if (!isRequest)
  2352. if (TLSX_CheckUnsupportedExtension(ssl, TLSX_MAX_FRAGMENT_LENGTH))
  2353. return TLSX_HandleUnsupportedExtension(ssl);
  2354. #endif
  2355. switch (*input) {
  2356. case WOLFSSL_MFL_2_8 : ssl->max_fragment = 256; break;
  2357. case WOLFSSL_MFL_2_9 : ssl->max_fragment = 512; break;
  2358. case WOLFSSL_MFL_2_10: ssl->max_fragment = 1024; break;
  2359. case WOLFSSL_MFL_2_11: ssl->max_fragment = 2048; break;
  2360. case WOLFSSL_MFL_2_12: ssl->max_fragment = 4096; break;
  2361. case WOLFSSL_MFL_2_13: ssl->max_fragment = 8192; break;
  2362. default:
  2363. SendAlert(ssl, alert_fatal, illegal_parameter);
  2364. WOLFSSL_ERROR_VERBOSE(UNKNOWN_MAX_FRAG_LEN_E);
  2365. return UNKNOWN_MAX_FRAG_LEN_E;
  2366. }
  2367. #ifndef NO_WOLFSSL_SERVER
  2368. if (isRequest) {
  2369. int ret = TLSX_UseMaxFragment(&ssl->extensions, *input, ssl->heap);
  2370. if (ret != WOLFSSL_SUCCESS)
  2371. return ret; /* throw error */
  2372. TLSX_SetResponse(ssl, TLSX_MAX_FRAGMENT_LENGTH);
  2373. }
  2374. #endif
  2375. return 0;
  2376. }
  2377. int TLSX_UseMaxFragment(TLSX** extensions, byte mfl, void* heap)
  2378. {
  2379. byte* data = NULL;
  2380. int ret = 0;
  2381. if (extensions == NULL || mfl < WOLFSSL_MFL_MIN || mfl > WOLFSSL_MFL_MAX)
  2382. return BAD_FUNC_ARG;
  2383. data = (byte*)XMALLOC(ENUM_LEN, heap, DYNAMIC_TYPE_TLSX);
  2384. if (data == NULL)
  2385. return MEMORY_E;
  2386. data[0] = mfl;
  2387. ret = TLSX_Push(extensions, TLSX_MAX_FRAGMENT_LENGTH, data, heap);
  2388. if (ret != 0) {
  2389. XFREE(data, heap, DYNAMIC_TYPE_TLSX);
  2390. return ret;
  2391. }
  2392. return WOLFSSL_SUCCESS;
  2393. }
  2394. #define MFL_FREE_ALL(data, heap) XFREE(data, (heap), DYNAMIC_TYPE_TLSX)
  2395. #define MFL_GET_SIZE(data) ENUM_LEN
  2396. #define MFL_WRITE TLSX_MFL_Write
  2397. #define MFL_PARSE TLSX_MFL_Parse
  2398. #else
  2399. #define MFL_FREE_ALL(a, b)
  2400. #define MFL_GET_SIZE(a) 0
  2401. #define MFL_WRITE(a, b) 0
  2402. #define MFL_PARSE(a, b, c, d) 0
  2403. #endif /* HAVE_MAX_FRAGMENT */
  2404. /******************************************************************************/
  2405. /* Truncated HMAC */
  2406. /******************************************************************************/
  2407. #ifdef HAVE_TRUNCATED_HMAC
  2408. static int TLSX_THM_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2409. byte isRequest)
  2410. {
  2411. if (length != 0 || input == NULL)
  2412. return BUFFER_ERROR;
  2413. if (!isRequest) {
  2414. #ifndef WOLFSSL_OLD_UNSUPPORTED_EXTENSION
  2415. if (TLSX_CheckUnsupportedExtension(ssl, TLSX_TRUNCATED_HMAC))
  2416. return TLSX_HandleUnsupportedExtension(ssl);
  2417. #endif
  2418. }
  2419. else {
  2420. #ifndef NO_WOLFSSL_SERVER
  2421. int ret = TLSX_UseTruncatedHMAC(&ssl->extensions, ssl->heap);
  2422. if (ret != WOLFSSL_SUCCESS)
  2423. return ret; /* throw error */
  2424. TLSX_SetResponse(ssl, TLSX_TRUNCATED_HMAC);
  2425. #endif
  2426. }
  2427. ssl->truncated_hmac = 1;
  2428. return 0;
  2429. }
  2430. int TLSX_UseTruncatedHMAC(TLSX** extensions, void* heap)
  2431. {
  2432. int ret = 0;
  2433. if (extensions == NULL)
  2434. return BAD_FUNC_ARG;
  2435. ret = TLSX_Push(extensions, TLSX_TRUNCATED_HMAC, NULL, heap);
  2436. if (ret != 0)
  2437. return ret;
  2438. return WOLFSSL_SUCCESS;
  2439. }
  2440. #define THM_PARSE TLSX_THM_Parse
  2441. #else
  2442. #define THM_PARSE(a, b, c, d) 0
  2443. #endif /* HAVE_TRUNCATED_HMAC */
  2444. /******************************************************************************/
  2445. /* Certificate Status Request */
  2446. /******************************************************************************/
  2447. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  2448. static void TLSX_CSR_Free(CertificateStatusRequest* csr, void* heap)
  2449. {
  2450. switch (csr->status_type) {
  2451. case WOLFSSL_CSR_OCSP:
  2452. FreeOcspRequest(&csr->request.ocsp);
  2453. break;
  2454. }
  2455. XFREE(csr, heap, DYNAMIC_TYPE_TLSX);
  2456. (void)heap;
  2457. }
  2458. static word16 TLSX_CSR_GetSize(CertificateStatusRequest* csr, byte isRequest)
  2459. {
  2460. word16 size = 0;
  2461. /* shut up compiler warnings */
  2462. (void) csr; (void) isRequest;
  2463. #ifndef NO_WOLFSSL_CLIENT
  2464. if (isRequest) {
  2465. switch (csr->status_type) {
  2466. case WOLFSSL_CSR_OCSP:
  2467. size += ENUM_LEN + 2 * OPAQUE16_LEN;
  2468. if (csr->request.ocsp.nonceSz)
  2469. size += OCSP_NONCE_EXT_SZ;
  2470. break;
  2471. }
  2472. }
  2473. #endif
  2474. #if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER)
  2475. if (!isRequest && csr->ssl->options.tls1_3)
  2476. return OPAQUE8_LEN + OPAQUE24_LEN + csr->response.length;
  2477. #endif
  2478. return size;
  2479. }
  2480. static word16 TLSX_CSR_Write(CertificateStatusRequest* csr, byte* output,
  2481. byte isRequest)
  2482. {
  2483. /* shut up compiler warnings */
  2484. (void) csr; (void) output; (void) isRequest;
  2485. #ifndef NO_WOLFSSL_CLIENT
  2486. if (isRequest) {
  2487. word16 offset = 0;
  2488. word16 length = 0;
  2489. /* type */
  2490. output[offset++] = csr->status_type;
  2491. switch (csr->status_type) {
  2492. case WOLFSSL_CSR_OCSP:
  2493. /* responder id list */
  2494. c16toa(0, output + offset);
  2495. offset += OPAQUE16_LEN;
  2496. /* request extensions */
  2497. if (csr->request.ocsp.nonceSz)
  2498. length = (word16)EncodeOcspRequestExtensions(
  2499. &csr->request.ocsp,
  2500. output + offset + OPAQUE16_LEN,
  2501. OCSP_NONCE_EXT_SZ);
  2502. c16toa(length, output + offset);
  2503. offset += OPAQUE16_LEN + length;
  2504. break;
  2505. }
  2506. return offset;
  2507. }
  2508. #endif
  2509. #if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER)
  2510. if (!isRequest && csr->ssl->options.tls1_3) {
  2511. word16 offset = 0;
  2512. output[offset++] = csr->status_type;
  2513. c32to24(csr->response.length, output + offset);
  2514. offset += OPAQUE24_LEN;
  2515. XMEMCPY(output + offset, csr->response.buffer, csr->response.length);
  2516. offset += csr->response.length;
  2517. return offset;
  2518. }
  2519. #endif
  2520. return 0;
  2521. }
  2522. static int TLSX_CSR_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2523. byte isRequest)
  2524. {
  2525. int ret;
  2526. #if !defined(NO_WOLFSSL_SERVER)
  2527. byte status_type;
  2528. word16 size = 0;
  2529. #if defined(WOLFSSL_TLS13)
  2530. DecodedCert* cert;
  2531. #endif
  2532. #endif
  2533. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER) \
  2534. && defined(WOLFSSL_TLS13)
  2535. OcspRequest* request;
  2536. TLSX* extension;
  2537. CertificateStatusRequest* csr;
  2538. #endif
  2539. #if !defined(NO_WOLFSSL_CLIENT) && defined(WOLFSSL_TLS13) \
  2540. || !defined(NO_WOLFSSL_SERVER)
  2541. word32 offset = 0;
  2542. #endif
  2543. #if !defined(NO_WOLFSSL_CLIENT) && defined(WOLFSSL_TLS13)
  2544. word32 resp_length = 0;
  2545. #endif
  2546. /* shut up compiler warnings */
  2547. (void) ssl; (void) input;
  2548. if (!isRequest) {
  2549. #ifndef NO_WOLFSSL_CLIENT
  2550. extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2551. csr = extension ? (CertificateStatusRequest*)extension->data : NULL;
  2552. if (!csr) {
  2553. /* look at context level */
  2554. extension = TLSX_Find(ssl->ctx->extensions, TLSX_STATUS_REQUEST);
  2555. csr = extension ? (CertificateStatusRequest*)extension->data : NULL;
  2556. if (!csr) /* unexpected extension */
  2557. return TLSX_HandleUnsupportedExtension(ssl);
  2558. /* enable extension at ssl level */
  2559. ret = TLSX_UseCertificateStatusRequest(&ssl->extensions,
  2560. csr->status_type, csr->options, ssl,
  2561. ssl->heap, ssl->devId);
  2562. if (ret != WOLFSSL_SUCCESS)
  2563. return ret;
  2564. switch (csr->status_type) {
  2565. case WOLFSSL_CSR_OCSP:
  2566. /* propagate nonce */
  2567. if (csr->request.ocsp.nonceSz) {
  2568. request =
  2569. (OcspRequest*)TLSX_CSR_GetRequest(ssl->extensions);
  2570. if (request) {
  2571. XMEMCPY(request->nonce, csr->request.ocsp.nonce,
  2572. csr->request.ocsp.nonceSz);
  2573. request->nonceSz = csr->request.ocsp.nonceSz;
  2574. }
  2575. }
  2576. break;
  2577. }
  2578. }
  2579. ssl->status_request = 1;
  2580. #ifdef WOLFSSL_TLS13
  2581. if (ssl->options.tls1_3) {
  2582. /* Get the new extension potentially created above. */
  2583. extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2584. csr = extension ? (CertificateStatusRequest*)extension->data : NULL;
  2585. if (csr == NULL)
  2586. return MEMORY_ERROR;
  2587. ret = 0;
  2588. if (OPAQUE8_LEN + OPAQUE24_LEN > length)
  2589. ret = BUFFER_ERROR;
  2590. if (ret == 0 && input[offset++] != WOLFSSL_CSR_OCSP) {
  2591. ret = BAD_CERTIFICATE_STATUS_ERROR;
  2592. WOLFSSL_ERROR_VERBOSE(ret);
  2593. }
  2594. if (ret == 0) {
  2595. c24to32(input + offset, &resp_length);
  2596. offset += OPAQUE24_LEN;
  2597. if (offset + resp_length != length)
  2598. ret = BUFFER_ERROR;
  2599. }
  2600. if (ret == 0) {
  2601. csr->response.buffer = (byte*)(input + offset);
  2602. csr->response.length = resp_length;
  2603. }
  2604. return ret;
  2605. }
  2606. else
  2607. #endif
  2608. {
  2609. /* extension_data MUST be empty. */
  2610. return length ? BUFFER_ERROR : 0;
  2611. }
  2612. #endif
  2613. }
  2614. else {
  2615. #ifndef NO_WOLFSSL_SERVER
  2616. if (length == 0)
  2617. return 0;
  2618. status_type = input[offset++];
  2619. switch (status_type) {
  2620. case WOLFSSL_CSR_OCSP: {
  2621. /* skip responder_id_list */
  2622. if ((int)(length - offset) < OPAQUE16_LEN)
  2623. return BUFFER_ERROR;
  2624. ato16(input + offset, &size);
  2625. offset += OPAQUE16_LEN + size;
  2626. /* skip request_extensions */
  2627. if ((int)(length - offset) < OPAQUE16_LEN)
  2628. return BUFFER_ERROR;
  2629. ato16(input + offset, &size);
  2630. offset += OPAQUE16_LEN + size;
  2631. if (offset > length)
  2632. return BUFFER_ERROR;
  2633. /* is able to send OCSP response? */
  2634. if (SSL_CM(ssl) == NULL || !SSL_CM(ssl)->ocspStaplingEnabled)
  2635. return 0;
  2636. }
  2637. break;
  2638. /* unknown status type */
  2639. default:
  2640. return 0;
  2641. }
  2642. /* if using status_request and already sending it, skip this one */
  2643. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  2644. if (ssl->status_request_v2)
  2645. return 0;
  2646. #endif
  2647. /* accept the first good status_type and return */
  2648. ret = TLSX_UseCertificateStatusRequest(&ssl->extensions, status_type,
  2649. 0, ssl, ssl->heap, ssl->devId);
  2650. if (ret != WOLFSSL_SUCCESS)
  2651. return ret; /* throw error */
  2652. #if defined(WOLFSSL_TLS13)
  2653. if (ssl->options.tls1_3) {
  2654. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), ssl->heap,
  2655. DYNAMIC_TYPE_DCERT);
  2656. if (cert == NULL) {
  2657. return MEMORY_E;
  2658. }
  2659. InitDecodedCert(cert, ssl->buffers.certificate->buffer,
  2660. ssl->buffers.certificate->length, ssl->heap);
  2661. ret = ParseCert(cert, CERT_TYPE, 1, SSL_CM(ssl));
  2662. if (ret != 0 ) {
  2663. XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
  2664. return ret;
  2665. }
  2666. ret = TLSX_CSR_InitRequest(ssl->extensions, cert, ssl->heap);
  2667. if (ret != 0 ) {
  2668. XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
  2669. return ret;
  2670. }
  2671. XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
  2672. extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2673. csr = extension ?
  2674. (CertificateStatusRequest*)extension->data : NULL;
  2675. if (csr == NULL)
  2676. return MEMORY_ERROR;
  2677. request = &csr->request.ocsp;
  2678. ret = CreateOcspResponse(ssl, &request, &csr->response);
  2679. if (ret != 0)
  2680. return ret;
  2681. if (csr->response.buffer)
  2682. TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST);
  2683. }
  2684. else
  2685. #endif
  2686. TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST);
  2687. ssl->status_request = status_type;
  2688. #endif
  2689. }
  2690. return 0;
  2691. }
  2692. int TLSX_CSR_InitRequest(TLSX* extensions, DecodedCert* cert, void* heap)
  2693. {
  2694. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST);
  2695. CertificateStatusRequest* csr = extension ?
  2696. (CertificateStatusRequest*)extension->data : NULL;
  2697. int ret = 0;
  2698. if (csr) {
  2699. switch (csr->status_type) {
  2700. case WOLFSSL_CSR_OCSP: {
  2701. byte nonce[MAX_OCSP_NONCE_SZ];
  2702. int nonceSz = csr->request.ocsp.nonceSz;
  2703. /* preserve nonce */
  2704. XMEMCPY(nonce, csr->request.ocsp.nonce, nonceSz);
  2705. if ((ret = InitOcspRequest(&csr->request.ocsp, cert, 0, heap))
  2706. != 0)
  2707. return ret;
  2708. /* restore nonce */
  2709. XMEMCPY(csr->request.ocsp.nonce, nonce, nonceSz);
  2710. csr->request.ocsp.nonceSz = nonceSz;
  2711. }
  2712. break;
  2713. }
  2714. }
  2715. return ret;
  2716. }
  2717. void* TLSX_CSR_GetRequest(TLSX* extensions)
  2718. {
  2719. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST);
  2720. CertificateStatusRequest* csr = extension ?
  2721. (CertificateStatusRequest*)extension->data : NULL;
  2722. if (csr) {
  2723. switch (csr->status_type) {
  2724. case WOLFSSL_CSR_OCSP:
  2725. return &csr->request.ocsp;
  2726. }
  2727. }
  2728. return NULL;
  2729. }
  2730. int TLSX_CSR_ForceRequest(WOLFSSL* ssl)
  2731. {
  2732. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2733. CertificateStatusRequest* csr = extension ?
  2734. (CertificateStatusRequest*)extension->data : NULL;
  2735. if (csr) {
  2736. switch (csr->status_type) {
  2737. case WOLFSSL_CSR_OCSP:
  2738. if (SSL_CM(ssl)->ocspEnabled) {
  2739. csr->request.ocsp.ssl = ssl;
  2740. return CheckOcspRequest(SSL_CM(ssl)->ocsp,
  2741. &csr->request.ocsp, NULL);
  2742. }
  2743. else {
  2744. WOLFSSL_ERROR_VERBOSE(OCSP_LOOKUP_FAIL);
  2745. return OCSP_LOOKUP_FAIL;
  2746. }
  2747. }
  2748. }
  2749. return 0;
  2750. }
  2751. int TLSX_UseCertificateStatusRequest(TLSX** extensions, byte status_type,
  2752. byte options, WOLFSSL* ssl, void* heap,
  2753. int devId)
  2754. {
  2755. CertificateStatusRequest* csr = NULL;
  2756. int ret = 0;
  2757. if (!extensions || status_type != WOLFSSL_CSR_OCSP)
  2758. return BAD_FUNC_ARG;
  2759. csr = (CertificateStatusRequest*)
  2760. XMALLOC(sizeof(CertificateStatusRequest), heap, DYNAMIC_TYPE_TLSX);
  2761. if (!csr)
  2762. return MEMORY_E;
  2763. ForceZero(csr, sizeof(CertificateStatusRequest));
  2764. csr->status_type = status_type;
  2765. csr->options = options;
  2766. csr->ssl = ssl;
  2767. switch (csr->status_type) {
  2768. case WOLFSSL_CSR_OCSP:
  2769. if (options & WOLFSSL_CSR_OCSP_USE_NONCE) {
  2770. WC_RNG rng;
  2771. #ifndef HAVE_FIPS
  2772. ret = wc_InitRng_ex(&rng, heap, devId);
  2773. #else
  2774. ret = wc_InitRng(&rng);
  2775. (void)devId;
  2776. #endif
  2777. if (ret == 0) {
  2778. if (wc_RNG_GenerateBlock(&rng, csr->request.ocsp.nonce,
  2779. MAX_OCSP_NONCE_SZ) == 0)
  2780. csr->request.ocsp.nonceSz = MAX_OCSP_NONCE_SZ;
  2781. wc_FreeRng(&rng);
  2782. }
  2783. }
  2784. break;
  2785. }
  2786. if ((ret = TLSX_Push(extensions, TLSX_STATUS_REQUEST, csr, heap)) != 0) {
  2787. XFREE(csr, heap, DYNAMIC_TYPE_TLSX);
  2788. return ret;
  2789. }
  2790. return WOLFSSL_SUCCESS;
  2791. }
  2792. #define CSR_FREE_ALL TLSX_CSR_Free
  2793. #define CSR_GET_SIZE TLSX_CSR_GetSize
  2794. #define CSR_WRITE TLSX_CSR_Write
  2795. #define CSR_PARSE TLSX_CSR_Parse
  2796. #else
  2797. #define CSR_FREE_ALL(data, heap)
  2798. #define CSR_GET_SIZE(a, b) 0
  2799. #define CSR_WRITE(a, b, c) 0
  2800. #define CSR_PARSE(a, b, c, d) 0
  2801. #endif /* HAVE_CERTIFICATE_STATUS_REQUEST */
  2802. /******************************************************************************/
  2803. /* Certificate Status Request v2 */
  2804. /******************************************************************************/
  2805. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  2806. static void TLSX_CSR2_FreeAll(CertificateStatusRequestItemV2* csr2, void* heap)
  2807. {
  2808. CertificateStatusRequestItemV2* next;
  2809. for (; csr2; csr2 = next) {
  2810. next = csr2->next;
  2811. switch (csr2->status_type) {
  2812. case WOLFSSL_CSR2_OCSP:
  2813. case WOLFSSL_CSR2_OCSP_MULTI:
  2814. while(csr2->requests--)
  2815. FreeOcspRequest(&csr2->request.ocsp[csr2->requests]);
  2816. break;
  2817. }
  2818. XFREE(csr2, heap, DYNAMIC_TYPE_TLSX);
  2819. }
  2820. (void)heap;
  2821. }
  2822. static word16 TLSX_CSR2_GetSize(CertificateStatusRequestItemV2* csr2,
  2823. byte isRequest)
  2824. {
  2825. word16 size = 0;
  2826. /* shut up compiler warnings */
  2827. (void) csr2; (void) isRequest;
  2828. #ifndef NO_WOLFSSL_CLIENT
  2829. if (isRequest) {
  2830. CertificateStatusRequestItemV2* next;
  2831. for (size = OPAQUE16_LEN; csr2; csr2 = next) {
  2832. next = csr2->next;
  2833. switch (csr2->status_type) {
  2834. case WOLFSSL_CSR2_OCSP:
  2835. case WOLFSSL_CSR2_OCSP_MULTI:
  2836. size += ENUM_LEN + 3 * OPAQUE16_LEN;
  2837. if (csr2->request.ocsp[0].nonceSz)
  2838. size += OCSP_NONCE_EXT_SZ;
  2839. break;
  2840. }
  2841. }
  2842. }
  2843. #endif
  2844. return size;
  2845. }
  2846. static word16 TLSX_CSR2_Write(CertificateStatusRequestItemV2* csr2,
  2847. byte* output, byte isRequest)
  2848. {
  2849. /* shut up compiler warnings */
  2850. (void) csr2; (void) output; (void) isRequest;
  2851. #ifndef NO_WOLFSSL_CLIENT
  2852. if (isRequest) {
  2853. word16 offset;
  2854. word16 length;
  2855. for (offset = OPAQUE16_LEN; csr2 != NULL; csr2 = csr2->next) {
  2856. /* status_type */
  2857. output[offset++] = csr2->status_type;
  2858. /* request */
  2859. switch (csr2->status_type) {
  2860. case WOLFSSL_CSR2_OCSP:
  2861. case WOLFSSL_CSR2_OCSP_MULTI:
  2862. /* request_length */
  2863. length = 2 * OPAQUE16_LEN;
  2864. if (csr2->request.ocsp[0].nonceSz)
  2865. length += OCSP_NONCE_EXT_SZ;
  2866. c16toa(length, output + offset);
  2867. offset += OPAQUE16_LEN;
  2868. /* responder id list */
  2869. c16toa(0, output + offset);
  2870. offset += OPAQUE16_LEN;
  2871. /* request extensions */
  2872. length = 0;
  2873. if (csr2->request.ocsp[0].nonceSz)
  2874. length = (word16)EncodeOcspRequestExtensions(
  2875. &csr2->request.ocsp[0],
  2876. output + offset + OPAQUE16_LEN,
  2877. OCSP_NONCE_EXT_SZ);
  2878. c16toa(length, output + offset);
  2879. offset += OPAQUE16_LEN + length;
  2880. break;
  2881. }
  2882. }
  2883. /* list size */
  2884. c16toa(offset - OPAQUE16_LEN, output);
  2885. return offset;
  2886. }
  2887. #endif
  2888. return 0;
  2889. }
  2890. static int TLSX_CSR2_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2891. byte isRequest)
  2892. {
  2893. int ret;
  2894. /* shut up compiler warnings */
  2895. (void) ssl; (void) input;
  2896. if (!isRequest) {
  2897. #ifndef NO_WOLFSSL_CLIENT
  2898. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST_V2);
  2899. CertificateStatusRequestItemV2* csr2 = extension ?
  2900. (CertificateStatusRequestItemV2*)extension->data : NULL;
  2901. if (!csr2) {
  2902. /* look at context level */
  2903. extension = TLSX_Find(ssl->ctx->extensions, TLSX_STATUS_REQUEST_V2);
  2904. csr2 = extension ?
  2905. (CertificateStatusRequestItemV2*)extension->data : NULL;
  2906. if (!csr2) /* unexpected extension */
  2907. return TLSX_HandleUnsupportedExtension(ssl);
  2908. /* enable extension at ssl level */
  2909. for (; csr2; csr2 = csr2->next) {
  2910. ret = TLSX_UseCertificateStatusRequestV2(&ssl->extensions,
  2911. csr2->status_type, csr2->options, ssl->heap,
  2912. ssl->devId);
  2913. if (ret != WOLFSSL_SUCCESS)
  2914. return ret;
  2915. switch (csr2->status_type) {
  2916. case WOLFSSL_CSR2_OCSP:
  2917. /* followed by */
  2918. case WOLFSSL_CSR2_OCSP_MULTI:
  2919. /* propagate nonce */
  2920. if (csr2->request.ocsp[0].nonceSz) {
  2921. OcspRequest* request =
  2922. (OcspRequest*)TLSX_CSR2_GetRequest(ssl->extensions,
  2923. csr2->status_type, 0);
  2924. if (request) {
  2925. XMEMCPY(request->nonce,
  2926. csr2->request.ocsp[0].nonce,
  2927. csr2->request.ocsp[0].nonceSz);
  2928. request->nonceSz =
  2929. csr2->request.ocsp[0].nonceSz;
  2930. }
  2931. }
  2932. break;
  2933. }
  2934. }
  2935. }
  2936. ssl->status_request_v2 = 1;
  2937. return length ? BUFFER_ERROR : 0; /* extension_data MUST be empty. */
  2938. #endif
  2939. }
  2940. else {
  2941. #ifndef NO_WOLFSSL_SERVER
  2942. byte status_type;
  2943. word16 request_length;
  2944. word16 offset = 0;
  2945. word16 size = 0;
  2946. /* list size */
  2947. if (offset + OPAQUE16_LEN >= length) {
  2948. return BUFFER_E;
  2949. }
  2950. ato16(input + offset, &request_length);
  2951. offset += OPAQUE16_LEN;
  2952. if (length - OPAQUE16_LEN != request_length)
  2953. return BUFFER_ERROR;
  2954. while (length > offset) {
  2955. if ((int)(length - offset) < ENUM_LEN + OPAQUE16_LEN)
  2956. return BUFFER_ERROR;
  2957. status_type = input[offset++];
  2958. ato16(input + offset, &request_length);
  2959. offset += OPAQUE16_LEN;
  2960. if (length - offset < request_length)
  2961. return BUFFER_ERROR;
  2962. switch (status_type) {
  2963. case WOLFSSL_CSR2_OCSP:
  2964. case WOLFSSL_CSR2_OCSP_MULTI:
  2965. /* skip responder_id_list */
  2966. if ((int)(length - offset) < OPAQUE16_LEN)
  2967. return BUFFER_ERROR;
  2968. ato16(input + offset, &size);
  2969. if (length - offset < size)
  2970. return BUFFER_ERROR;
  2971. offset += OPAQUE16_LEN + size;
  2972. /* skip request_extensions */
  2973. if ((int)(length - offset) < OPAQUE16_LEN)
  2974. return BUFFER_ERROR;
  2975. ato16(input + offset, &size);
  2976. if (length - offset < size)
  2977. return BUFFER_ERROR;
  2978. offset += OPAQUE16_LEN + size;
  2979. if (offset > length)
  2980. return BUFFER_ERROR;
  2981. /* is able to send OCSP response? */
  2982. if (SSL_CM(ssl) == NULL
  2983. || !SSL_CM(ssl)->ocspStaplingEnabled)
  2984. continue;
  2985. break;
  2986. default:
  2987. /* unknown status type, skipping! */
  2988. offset += request_length;
  2989. continue;
  2990. }
  2991. /* if using status_request and already sending it, remove it
  2992. * and prefer to use the v2 version */
  2993. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  2994. if (ssl->status_request) {
  2995. ssl->status_request = 0;
  2996. TLSX_Remove(&ssl->extensions, TLSX_STATUS_REQUEST, ssl->heap);
  2997. }
  2998. #endif
  2999. /* TLS 1.3 servers MUST NOT act upon presence or information in
  3000. * this extension (RFC 8448 Section 4.4.2.1).
  3001. */
  3002. if (!IsAtLeastTLSv1_3(ssl->version)) {
  3003. /* accept the first good status_type and return */
  3004. ret = TLSX_UseCertificateStatusRequestV2(&ssl->extensions,
  3005. status_type, 0, ssl->heap, ssl->devId);
  3006. if (ret != WOLFSSL_SUCCESS)
  3007. return ret; /* throw error */
  3008. TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST_V2);
  3009. ssl->status_request_v2 = status_type;
  3010. }
  3011. return 0;
  3012. }
  3013. #endif
  3014. }
  3015. return 0;
  3016. }
  3017. int TLSX_CSR2_InitRequests(TLSX* extensions, DecodedCert* cert, byte isPeer,
  3018. void* heap)
  3019. {
  3020. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST_V2);
  3021. CertificateStatusRequestItemV2* csr2 = extension ?
  3022. (CertificateStatusRequestItemV2*)extension->data : NULL;
  3023. int ret = 0;
  3024. for (; csr2; csr2 = csr2->next) {
  3025. switch (csr2->status_type) {
  3026. case WOLFSSL_CSR2_OCSP:
  3027. if (!isPeer || csr2->requests != 0)
  3028. break;
  3029. FALL_THROUGH; /* followed by */
  3030. case WOLFSSL_CSR2_OCSP_MULTI: {
  3031. if (csr2->requests < 1 + MAX_CHAIN_DEPTH) {
  3032. byte nonce[MAX_OCSP_NONCE_SZ];
  3033. int nonceSz = csr2->request.ocsp[0].nonceSz;
  3034. /* preserve nonce, replicating nonce of ocsp[0] */
  3035. XMEMCPY(nonce, csr2->request.ocsp[0].nonce, nonceSz);
  3036. if ((ret = InitOcspRequest(
  3037. &csr2->request.ocsp[csr2->requests], cert,
  3038. 0, heap)) != 0)
  3039. return ret;
  3040. /* restore nonce */
  3041. XMEMCPY(csr2->request.ocsp[csr2->requests].nonce,
  3042. nonce, nonceSz);
  3043. csr2->request.ocsp[csr2->requests].nonceSz = nonceSz;
  3044. csr2->requests++;
  3045. }
  3046. }
  3047. break;
  3048. }
  3049. }
  3050. (void)cert;
  3051. return ret;
  3052. }
  3053. void* TLSX_CSR2_GetRequest(TLSX* extensions, byte status_type, byte idx)
  3054. {
  3055. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST_V2);
  3056. CertificateStatusRequestItemV2* csr2 = extension ?
  3057. (CertificateStatusRequestItemV2*)extension->data : NULL;
  3058. for (; csr2; csr2 = csr2->next) {
  3059. if (csr2->status_type == status_type) {
  3060. switch (csr2->status_type) {
  3061. case WOLFSSL_CSR2_OCSP:
  3062. /* followed by */
  3063. case WOLFSSL_CSR2_OCSP_MULTI:
  3064. /* requests are initialized in the reverse order */
  3065. return idx < csr2->requests
  3066. ? &csr2->request.ocsp[csr2->requests - idx - 1]
  3067. : NULL;
  3068. }
  3069. }
  3070. }
  3071. return NULL;
  3072. }
  3073. int TLSX_CSR2_ForceRequest(WOLFSSL* ssl)
  3074. {
  3075. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST_V2);
  3076. CertificateStatusRequestItemV2* csr2 = extension ?
  3077. (CertificateStatusRequestItemV2*)extension->data : NULL;
  3078. /* forces only the first one */
  3079. if (csr2) {
  3080. switch (csr2->status_type) {
  3081. case WOLFSSL_CSR2_OCSP:
  3082. /* followed by */
  3083. case WOLFSSL_CSR2_OCSP_MULTI:
  3084. if (SSL_CM(ssl)->ocspEnabled) {
  3085. csr2->request.ocsp[0].ssl = ssl;
  3086. return CheckOcspRequest(SSL_CM(ssl)->ocsp,
  3087. &csr2->request.ocsp[0], NULL);
  3088. }
  3089. else {
  3090. WOLFSSL_ERROR_VERBOSE(OCSP_LOOKUP_FAIL);
  3091. return OCSP_LOOKUP_FAIL;
  3092. }
  3093. }
  3094. }
  3095. return 0;
  3096. }
  3097. int TLSX_UseCertificateStatusRequestV2(TLSX** extensions, byte status_type,
  3098. byte options, void* heap, int devId)
  3099. {
  3100. TLSX* extension = NULL;
  3101. CertificateStatusRequestItemV2* csr2 = NULL;
  3102. int ret = 0;
  3103. if (!extensions)
  3104. return BAD_FUNC_ARG;
  3105. if (status_type != WOLFSSL_CSR2_OCSP
  3106. && status_type != WOLFSSL_CSR2_OCSP_MULTI)
  3107. return BAD_FUNC_ARG;
  3108. csr2 = (CertificateStatusRequestItemV2*)
  3109. XMALLOC(sizeof(CertificateStatusRequestItemV2), heap, DYNAMIC_TYPE_TLSX);
  3110. if (!csr2)
  3111. return MEMORY_E;
  3112. ForceZero(csr2, sizeof(CertificateStatusRequestItemV2));
  3113. csr2->status_type = status_type;
  3114. csr2->options = options;
  3115. csr2->next = NULL;
  3116. switch (csr2->status_type) {
  3117. case WOLFSSL_CSR2_OCSP:
  3118. case WOLFSSL_CSR2_OCSP_MULTI:
  3119. if (options & WOLFSSL_CSR2_OCSP_USE_NONCE) {
  3120. WC_RNG rng;
  3121. #ifndef HAVE_FIPS
  3122. ret = wc_InitRng_ex(&rng, heap, devId);
  3123. #else
  3124. ret = wc_InitRng(&rng);
  3125. (void)devId;
  3126. #endif
  3127. if (ret == 0) {
  3128. if (wc_RNG_GenerateBlock(&rng, csr2->request.ocsp[0].nonce,
  3129. MAX_OCSP_NONCE_SZ) == 0)
  3130. csr2->request.ocsp[0].nonceSz = MAX_OCSP_NONCE_SZ;
  3131. wc_FreeRng(&rng);
  3132. }
  3133. }
  3134. break;
  3135. }
  3136. /* append new item */
  3137. if ((extension = TLSX_Find(*extensions, TLSX_STATUS_REQUEST_V2))) {
  3138. CertificateStatusRequestItemV2* last =
  3139. (CertificateStatusRequestItemV2*)extension->data;
  3140. for (; last->next; last = last->next);
  3141. last->next = csr2;
  3142. }
  3143. else if ((ret = TLSX_Push(extensions, TLSX_STATUS_REQUEST_V2, csr2,heap))) {
  3144. XFREE(csr2, heap, DYNAMIC_TYPE_TLSX);
  3145. return ret;
  3146. }
  3147. return WOLFSSL_SUCCESS;
  3148. }
  3149. #define CSR2_FREE_ALL TLSX_CSR2_FreeAll
  3150. #define CSR2_GET_SIZE TLSX_CSR2_GetSize
  3151. #define CSR2_WRITE TLSX_CSR2_Write
  3152. #define CSR2_PARSE TLSX_CSR2_Parse
  3153. #else
  3154. #define CSR2_FREE_ALL(data, heap)
  3155. #define CSR2_GET_SIZE(a, b) 0
  3156. #define CSR2_WRITE(a, b, c) 0
  3157. #define CSR2_PARSE(a, b, c, d) 0
  3158. #endif /* HAVE_CERTIFICATE_STATUS_REQUEST_V2 */
  3159. /******************************************************************************/
  3160. /* Supported Elliptic Curves */
  3161. /******************************************************************************/
  3162. #ifdef HAVE_SUPPORTED_CURVES
  3163. #if !defined(HAVE_ECC) && !defined(HAVE_CURVE25519) && !defined(HAVE_CURVE448) \
  3164. && !defined(HAVE_FFDHE) && !defined(HAVE_PQC)
  3165. #error Elliptic Curves Extension requires Elliptic Curve Cryptography or liboqs groups. \
  3166. Use --enable-ecc and/or --enable-liboqs in the configure script or \
  3167. define HAVE_ECC. Alternatively use FFDHE for DH ciphersuites.
  3168. #endif
  3169. static int TLSX_SupportedCurve_New(SupportedCurve** curve, word16 name,
  3170. void* heap)
  3171. {
  3172. if (curve == NULL)
  3173. return BAD_FUNC_ARG;
  3174. (void)heap;
  3175. *curve = (SupportedCurve*)XMALLOC(sizeof(SupportedCurve), heap,
  3176. DYNAMIC_TYPE_TLSX);
  3177. if (*curve == NULL)
  3178. return MEMORY_E;
  3179. (*curve)->name = name;
  3180. (*curve)->next = NULL;
  3181. return 0;
  3182. }
  3183. static int TLSX_PointFormat_New(PointFormat** point, byte format, void* heap)
  3184. {
  3185. if (point == NULL)
  3186. return BAD_FUNC_ARG;
  3187. (void)heap;
  3188. *point = (PointFormat*)XMALLOC(sizeof(PointFormat), heap,
  3189. DYNAMIC_TYPE_TLSX);
  3190. if (*point == NULL)
  3191. return MEMORY_E;
  3192. (*point)->format = format;
  3193. (*point)->next = NULL;
  3194. return 0;
  3195. }
  3196. static void TLSX_SupportedCurve_FreeAll(SupportedCurve* list, void* heap)
  3197. {
  3198. SupportedCurve* curve;
  3199. while ((curve = list)) {
  3200. list = curve->next;
  3201. XFREE(curve, heap, DYNAMIC_TYPE_TLSX);
  3202. }
  3203. (void)heap;
  3204. }
  3205. static void TLSX_PointFormat_FreeAll(PointFormat* list, void* heap)
  3206. {
  3207. PointFormat* point;
  3208. while ((point = list)) {
  3209. list = point->next;
  3210. XFREE(point, heap, DYNAMIC_TYPE_TLSX);
  3211. }
  3212. (void)heap;
  3213. }
  3214. static int TLSX_SupportedCurve_Append(SupportedCurve* list, word16 name,
  3215. void* heap)
  3216. {
  3217. int ret = BAD_FUNC_ARG;
  3218. while (list) {
  3219. if (list->name == name) {
  3220. ret = 0; /* curve already in use */
  3221. break;
  3222. }
  3223. if (list->next == NULL) {
  3224. ret = TLSX_SupportedCurve_New(&list->next, name, heap);
  3225. break;
  3226. }
  3227. list = list->next;
  3228. }
  3229. return ret;
  3230. }
  3231. static int TLSX_PointFormat_Append(PointFormat* list, byte format, void* heap)
  3232. {
  3233. int ret = BAD_FUNC_ARG;
  3234. while (list) {
  3235. if (list->format == format) {
  3236. ret = 0; /* format already in use */
  3237. break;
  3238. }
  3239. if (list->next == NULL) {
  3240. ret = TLSX_PointFormat_New(&list->next, format, heap);
  3241. break;
  3242. }
  3243. list = list->next;
  3244. }
  3245. return ret;
  3246. }
  3247. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
  3248. #if defined(HAVE_FFDHE) && (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3249. defined(HAVE_CURVE448))
  3250. static void TLSX_SupportedCurve_ValidateRequest(const WOLFSSL* ssl,
  3251. const byte* semaphore)
  3252. {
  3253. /* If all pre-defined parameter types for key exchange are supported then
  3254. * always send SupportedGroups extension.
  3255. */
  3256. (void)ssl;
  3257. (void)semaphore;
  3258. }
  3259. #else
  3260. static void TLSX_SupportedCurve_ValidateRequest(WOLFSSL* ssl, byte* semaphore)
  3261. {
  3262. word16 i;
  3263. for (i = 0; i < ssl->suites->suiteSz; i += 2) {
  3264. if (ssl->suites->suites[i] == TLS13_BYTE)
  3265. return;
  3266. if ((ssl->suites->suites[i] == ECC_BYTE) ||
  3267. (ssl->suites->suites[i] == ECDHE_PSK_BYTE) ||
  3268. (ssl->suites->suites[i] == CHACHA_BYTE)) {
  3269. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3270. defined(HAVE_CURVE448)
  3271. return;
  3272. #endif
  3273. }
  3274. #ifdef HAVE_FFDHE
  3275. else {
  3276. return;
  3277. }
  3278. #endif
  3279. }
  3280. /* turns semaphore on to avoid sending this extension. */
  3281. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_GROUPS));
  3282. }
  3283. #endif
  3284. /* Only send PointFormats if TLSv13, ECC or CHACHA cipher suite present.
  3285. */
  3286. static void TLSX_PointFormat_ValidateRequest(WOLFSSL* ssl, byte* semaphore)
  3287. {
  3288. word16 i;
  3289. for (i = 0; i < ssl->suites->suiteSz; i += 2) {
  3290. if (ssl->suites->suites[i] == TLS13_BYTE)
  3291. return;
  3292. if ((ssl->suites->suites[i] == ECC_BYTE) ||
  3293. (ssl->suites->suites[i] == ECDHE_PSK_BYTE) ||
  3294. (ssl->suites->suites[i] == CHACHA_BYTE)) {
  3295. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3296. defined(HAVE_CURVE448)
  3297. return;
  3298. #endif
  3299. }
  3300. }
  3301. #ifdef HAVE_FFDHE
  3302. (void)semaphore;
  3303. return;
  3304. #else
  3305. /* turns semaphore on to avoid sending this extension. */
  3306. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  3307. #endif
  3308. }
  3309. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
  3310. #ifndef NO_WOLFSSL_SERVER
  3311. static void TLSX_PointFormat_ValidateResponse(WOLFSSL* ssl, byte* semaphore)
  3312. {
  3313. #if defined(HAVE_FFDHE) || defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3314. defined(HAVE_CURVE448)
  3315. (void)semaphore;
  3316. #endif
  3317. if (ssl->options.cipherSuite0 == TLS13_BYTE)
  3318. return;
  3319. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  3320. if (ssl->options.cipherSuite0 == ECC_BYTE ||
  3321. ssl->options.cipherSuite0 == ECDHE_PSK_BYTE ||
  3322. ssl->options.cipherSuite0 == CHACHA_BYTE) {
  3323. return;
  3324. }
  3325. #endif
  3326. /* turns semaphore on to avoid sending this extension. */
  3327. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  3328. }
  3329. #endif /* !NO_WOLFSSL_SERVER */
  3330. #ifndef NO_WOLFSSL_CLIENT
  3331. static word16 TLSX_SupportedCurve_GetSize(SupportedCurve* list)
  3332. {
  3333. SupportedCurve* curve;
  3334. word16 length = OPAQUE16_LEN; /* list length */
  3335. while ((curve = list)) {
  3336. list = curve->next;
  3337. length += OPAQUE16_LEN; /* curve length */
  3338. }
  3339. return length;
  3340. }
  3341. #endif
  3342. static word16 TLSX_PointFormat_GetSize(PointFormat* list)
  3343. {
  3344. PointFormat* point;
  3345. word16 length = ENUM_LEN; /* list length */
  3346. while ((point = list)) {
  3347. list = point->next;
  3348. length += ENUM_LEN; /* format length */
  3349. }
  3350. return length;
  3351. }
  3352. #ifndef NO_WOLFSSL_CLIENT
  3353. static word16 TLSX_SupportedCurve_Write(SupportedCurve* list, byte* output)
  3354. {
  3355. word16 offset = OPAQUE16_LEN;
  3356. while (list) {
  3357. c16toa(list->name, output + offset);
  3358. offset += OPAQUE16_LEN;
  3359. list = list->next;
  3360. }
  3361. c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
  3362. return offset;
  3363. }
  3364. #endif
  3365. static word16 TLSX_PointFormat_Write(PointFormat* list, byte* output)
  3366. {
  3367. word16 offset = ENUM_LEN;
  3368. while (list) {
  3369. output[offset++] = list->format;
  3370. list = list->next;
  3371. }
  3372. output[0] = (byte)(offset - ENUM_LEN);
  3373. return offset;
  3374. }
  3375. #if !defined(NO_WOLFSSL_SERVER) || (defined(WOLFSSL_TLS13) && \
  3376. !defined(WOLFSSL_NO_SERVER_GROUPS_EXT))
  3377. static int TLSX_SupportedCurve_Parse(WOLFSSL* ssl, const byte* input,
  3378. word16 length, byte isRequest)
  3379. {
  3380. word16 offset;
  3381. word16 name;
  3382. int ret;
  3383. if(!isRequest && !IsAtLeastTLSv1_3(ssl->version)) {
  3384. #ifdef WOLFSSL_ALLOW_SERVER_SC_EXT
  3385. return 0;
  3386. #else
  3387. return BUFFER_ERROR; /* servers doesn't send this extension. */
  3388. #endif
  3389. }
  3390. if (OPAQUE16_LEN > length || length % OPAQUE16_LEN)
  3391. return BUFFER_ERROR;
  3392. ato16(input, &offset);
  3393. /* validating curve list length */
  3394. if (length != OPAQUE16_LEN + offset)
  3395. return BUFFER_ERROR;
  3396. offset = OPAQUE16_LEN;
  3397. if (offset == length)
  3398. return 0;
  3399. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
  3400. if (!isRequest) {
  3401. TLSX* extension;
  3402. SupportedCurve* curve;
  3403. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3404. if (extension != NULL) {
  3405. /* Replace client list with server list of supported groups. */
  3406. curve = (SupportedCurve*)extension->data;
  3407. extension->data = NULL;
  3408. TLSX_SupportedCurve_FreeAll(curve, ssl->heap);
  3409. ato16(input + offset, &name);
  3410. offset += OPAQUE16_LEN;
  3411. ret = TLSX_SupportedCurve_New(&curve, name, ssl->heap);
  3412. if (ret != 0)
  3413. return ret; /* throw error */
  3414. extension->data = (void*)curve;
  3415. }
  3416. }
  3417. #endif
  3418. for (; offset < length; offset += OPAQUE16_LEN) {
  3419. ato16(input + offset, &name);
  3420. ret = TLSX_UseSupportedCurve(&ssl->extensions, name, ssl->heap);
  3421. /* If it is BAD_FUNC_ARG then it is a group we do not support, but
  3422. * that is fine. */
  3423. if (ret != WOLFSSL_SUCCESS && ret != BAD_FUNC_ARG) {
  3424. return ret;
  3425. }
  3426. }
  3427. return 0;
  3428. }
  3429. #endif
  3430. #if !defined(NO_WOLFSSL_SERVER)
  3431. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
  3432. /* Checks the priority of the groups on the server and set the supported groups
  3433. * response if there is a group not advertised by the client that is preferred.
  3434. *
  3435. * ssl SSL/TLS object.
  3436. * returns 0 on success, otherwise an error.
  3437. */
  3438. int TLSX_SupportedCurve_CheckPriority(WOLFSSL* ssl)
  3439. {
  3440. int ret;
  3441. TLSX* extension;
  3442. TLSX* priority = NULL;
  3443. TLSX* ext = NULL;
  3444. word16 name;
  3445. SupportedCurve* curve;
  3446. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3447. /* May be doing PSK with no key exchange. */
  3448. if (extension == NULL)
  3449. return 0;
  3450. ret = TLSX_PopulateSupportedGroups(ssl, &priority);
  3451. if (ret != WOLFSSL_SUCCESS) {
  3452. TLSX_FreeAll(priority, ssl->heap);
  3453. return ret;
  3454. }
  3455. ext = TLSX_Find(priority, TLSX_SUPPORTED_GROUPS);
  3456. if (ext == NULL) {
  3457. WOLFSSL_MSG("Could not find supported groups extension");
  3458. TLSX_FreeAll(priority, ssl->heap);
  3459. return 0;
  3460. }
  3461. curve = (SupportedCurve*)ext->data;
  3462. name = curve->name;
  3463. curve = (SupportedCurve*)extension->data;
  3464. while (curve != NULL) {
  3465. if (curve->name == name)
  3466. break;
  3467. curve = curve->next;
  3468. }
  3469. if (curve == NULL) {
  3470. /* Couldn't find the preferred group in client list. */
  3471. extension->resp = 1;
  3472. /* Send server list back and free client list. */
  3473. curve = (SupportedCurve*)extension->data;
  3474. extension->data = ext->data;
  3475. ext->data = curve;
  3476. }
  3477. TLSX_FreeAll(priority, ssl->heap);
  3478. return 0;
  3479. }
  3480. #endif /* WOLFSSL_TLS13 && !WOLFSSL_NO_SERVER_GROUPS_EXT */
  3481. #if defined(HAVE_FFDHE) && !defined(WOLFSSL_NO_TLS12)
  3482. #ifdef HAVE_PUBLIC_FFDHE
  3483. static int tlsx_ffdhe_find_group(WOLFSSL* ssl, SupportedCurve* clientGroup,
  3484. SupportedCurve* serverGroup)
  3485. {
  3486. int ret = 0;
  3487. SupportedCurve* group;
  3488. const DhParams* params = NULL;
  3489. for (; serverGroup != NULL; serverGroup = serverGroup->next) {
  3490. if (!WOLFSSL_NAMED_GROUP_IS_FFHDE(serverGroup->name))
  3491. continue;
  3492. for (group = clientGroup; group != NULL; group = group->next) {
  3493. if (serverGroup->name != group->name)
  3494. continue;
  3495. switch (serverGroup->name) {
  3496. #ifdef HAVE_FFDHE_2048
  3497. case WOLFSSL_FFDHE_2048:
  3498. params = wc_Dh_ffdhe2048_Get();
  3499. break;
  3500. #endif
  3501. #ifdef HAVE_FFDHE_3072
  3502. case WOLFSSL_FFDHE_3072:
  3503. params = wc_Dh_ffdhe3072_Get();
  3504. break;
  3505. #endif
  3506. #ifdef HAVE_FFDHE_4096
  3507. case WOLFSSL_FFDHE_4096:
  3508. params = wc_Dh_ffdhe4096_Get();
  3509. break;
  3510. #endif
  3511. #ifdef HAVE_FFDHE_6144
  3512. case WOLFSSL_FFDHE_6144:
  3513. params = wc_Dh_ffdhe6144_Get();
  3514. break;
  3515. #endif
  3516. #ifdef HAVE_FFDHE_8192
  3517. case WOLFSSL_FFDHE_8192:
  3518. params = wc_Dh_ffdhe8192_Get();
  3519. break;
  3520. #endif
  3521. default:
  3522. break;
  3523. }
  3524. if (params == NULL) {
  3525. ret = BAD_FUNC_ARG;
  3526. break;
  3527. }
  3528. if (params->p_len >= ssl->options.minDhKeySz &&
  3529. params->p_len <= ssl->options.maxDhKeySz) {
  3530. break;
  3531. }
  3532. }
  3533. if (ret != 0)
  3534. break;
  3535. if ((group != NULL) && (serverGroup->name == group->name))
  3536. break;
  3537. }
  3538. if ((ret == 0) && (serverGroup != NULL) && (params != NULL)) {
  3539. ssl->buffers.serverDH_P.buffer = (unsigned char *)params->p;
  3540. ssl->buffers.serverDH_P.length = params->p_len;
  3541. ssl->buffers.serverDH_G.buffer = (unsigned char *)params->g;
  3542. ssl->buffers.serverDH_G.length = params->g_len;
  3543. ssl->namedGroup = serverGroup->name;
  3544. #if !defined(WOLFSSL_OLD_PRIME_CHECK) && \
  3545. !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  3546. ssl->options.dhDoKeyTest = 0;
  3547. #endif
  3548. ssl->options.haveDH = 1;
  3549. }
  3550. return ret;
  3551. }
  3552. #else
  3553. static int tlsx_ffdhe_find_group(WOLFSSL* ssl, SupportedCurve* clientGroup,
  3554. SupportedCurve* serverGroup)
  3555. {
  3556. int ret = 0;
  3557. SupportedCurve* group;
  3558. word32 p_len;
  3559. for (; serverGroup != NULL; serverGroup = serverGroup->next) {
  3560. if (!WOLFSSL_NAMED_GROUP_IS_FFHDE(serverGroup->name))
  3561. continue;
  3562. for (group = clientGroup; group != NULL; group = group->next) {
  3563. if (serverGroup->name != group->name)
  3564. continue;
  3565. wc_DhGetNamedKeyParamSize(serverGroup->name, &p_len, NULL, NULL);
  3566. if (p_len == 0) {
  3567. ret = BAD_FUNC_ARG;
  3568. break;
  3569. }
  3570. if (p_len >= ssl->options.minDhKeySz &&
  3571. p_len <= ssl->options.maxDhKeySz) {
  3572. break;
  3573. }
  3574. }
  3575. if (ret != 0)
  3576. break;
  3577. if ((group != NULL) && (serverGroup->name == group->name))
  3578. break;
  3579. }
  3580. if ((ret == 0) && (serverGroup != NULL)) {
  3581. word32 pSz, gSz;
  3582. ssl->buffers.serverDH_P.buffer = NULL;
  3583. ssl->buffers.serverDH_G.buffer = NULL;
  3584. ret = wc_DhGetNamedKeyParamSize(serverGroup->name, &pSz, &gSz, NULL);
  3585. if (ret == 0) {
  3586. ssl->buffers.serverDH_P.buffer =
  3587. (byte*)XMALLOC(pSz, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  3588. if (ssl->buffers.serverDH_P.buffer == NULL)
  3589. ret = MEMORY_E;
  3590. else
  3591. ssl->buffers.serverDH_P.length = pSz;
  3592. }
  3593. if (ret == 0) {
  3594. ssl->buffers.serverDH_G.buffer =
  3595. (byte*)XMALLOC(gSz, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  3596. if (ssl->buffers.serverDH_G.buffer == NULL) {
  3597. ret = MEMORY_E;
  3598. } else
  3599. ssl->buffers.serverDH_G.length = gSz;
  3600. }
  3601. if (ret == 0) {
  3602. ret = wc_DhCopyNamedKey(serverGroup->name,
  3603. ssl->buffers.serverDH_P.buffer, &pSz,
  3604. ssl->buffers.serverDH_G.buffer, &gSz,
  3605. NULL, NULL);
  3606. }
  3607. if (ret == 0) {
  3608. ssl->buffers.weOwnDH = 1;
  3609. ssl->namedGroup = serverGroup->name;
  3610. #if !defined(WOLFSSL_OLD_PRIME_CHECK) && \
  3611. !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  3612. ssl->options.dhDoKeyTest = 0;
  3613. #endif
  3614. ssl->options.haveDH = 1;
  3615. }
  3616. else {
  3617. if (ssl->buffers.serverDH_P.buffer != NULL) {
  3618. XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap,
  3619. DYNAMIC_TYPE_PUBLIC_KEY);
  3620. ssl->buffers.serverDH_P.length = 0;
  3621. ssl->buffers.serverDH_P.buffer = NULL;
  3622. }
  3623. if (ssl->buffers.serverDH_G.buffer != NULL) {
  3624. XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap,
  3625. DYNAMIC_TYPE_PUBLIC_KEY);
  3626. ssl->buffers.serverDH_G.length = 0;
  3627. ssl->buffers.serverDH_G.buffer = NULL;
  3628. }
  3629. }
  3630. }
  3631. return ret;
  3632. }
  3633. #endif
  3634. /* Set the highest priority common FFDHE group on the server as compared to
  3635. * client extensions.
  3636. *
  3637. * ssl SSL/TLS object.
  3638. * returns 0 on success, otherwise an error.
  3639. */
  3640. int TLSX_SupportedFFDHE_Set(WOLFSSL* ssl)
  3641. {
  3642. int ret;
  3643. TLSX* priority = NULL;
  3644. TLSX* ext = NULL;
  3645. TLSX* extension;
  3646. SupportedCurve* clientGroup;
  3647. SupportedCurve* serverGroup;
  3648. SupportedCurve* group;
  3649. int found = 0;
  3650. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3651. /* May be doing PSK with no key exchange. */
  3652. if (extension == NULL)
  3653. return 0;
  3654. clientGroup = (SupportedCurve*)extension->data;
  3655. for (group = clientGroup; group != NULL; group = group->next) {
  3656. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(group->name)) {
  3657. found = 1;
  3658. break;
  3659. }
  3660. }
  3661. if (!found)
  3662. return 0;
  3663. if (ssl->buffers.serverDH_P.buffer && ssl->buffers.weOwnDH) {
  3664. XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap,
  3665. DYNAMIC_TYPE_PUBLIC_KEY);
  3666. }
  3667. if (ssl->buffers.serverDH_G.buffer && ssl->buffers.weOwnDH) {
  3668. XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap,
  3669. DYNAMIC_TYPE_PUBLIC_KEY);
  3670. }
  3671. ssl->buffers.serverDH_P.buffer = NULL;
  3672. ssl->buffers.serverDH_G.buffer = NULL;
  3673. ssl->buffers.weOwnDH = 0;
  3674. ssl->options.haveDH = 0;
  3675. ret = TLSX_PopulateSupportedGroups(ssl, &priority);
  3676. if (ret == WOLFSSL_SUCCESS) {
  3677. ext = TLSX_Find(priority, TLSX_SUPPORTED_GROUPS);
  3678. serverGroup = (SupportedCurve*)ext->data;
  3679. ret = tlsx_ffdhe_find_group(ssl, clientGroup, serverGroup);
  3680. }
  3681. TLSX_FreeAll(priority, ssl->heap);
  3682. return ret;
  3683. }
  3684. #endif /* HAVE_FFDHE && !WOLFSSL_NO_TLS12 */
  3685. #endif /* !NO_WOLFSSL_SERVER */
  3686. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
  3687. /* Return the preferred group.
  3688. *
  3689. * ssl SSL/TLS object.
  3690. * checkSupported Whether to check for the first supported group.
  3691. * returns BAD_FUNC_ARG if no group found, otherwise the group.
  3692. */
  3693. int TLSX_SupportedCurve_Preferred(WOLFSSL* ssl, int checkSupported)
  3694. {
  3695. TLSX* extension;
  3696. SupportedCurve* curve;
  3697. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3698. if (extension == NULL)
  3699. return BAD_FUNC_ARG;
  3700. curve = (SupportedCurve*)extension->data;
  3701. while (curve != NULL) {
  3702. if (!checkSupported || TLSX_KeyShare_IsSupported(curve->name))
  3703. return curve->name;
  3704. curve = curve->next;
  3705. }
  3706. return BAD_FUNC_ARG;
  3707. }
  3708. #endif /* HAVE_SUPPORTED_CURVES */
  3709. #ifndef NO_WOLFSSL_SERVER
  3710. static int TLSX_PointFormat_Parse(WOLFSSL* ssl, const byte* input,
  3711. word16 length, byte isRequest)
  3712. {
  3713. int ret;
  3714. /* validating formats list length */
  3715. if (ENUM_LEN > length || length != (word16)ENUM_LEN + input[0])
  3716. return BUFFER_ERROR;
  3717. if (isRequest) {
  3718. /* adding uncompressed point format to response */
  3719. ret = TLSX_UsePointFormat(&ssl->extensions, WOLFSSL_EC_PF_UNCOMPRESSED,
  3720. ssl->heap);
  3721. if (ret != WOLFSSL_SUCCESS)
  3722. return ret; /* throw error */
  3723. TLSX_SetResponse(ssl, TLSX_EC_POINT_FORMATS);
  3724. }
  3725. return 0;
  3726. }
  3727. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  3728. int TLSX_ValidateSupportedCurves(WOLFSSL* ssl, byte first, byte second) {
  3729. TLSX* extension = NULL;
  3730. SupportedCurve* curve = NULL;
  3731. word32 oid = 0;
  3732. word32 defOid = 0;
  3733. word32 defSz = 80; /* Maximum known curve size is 66. */
  3734. word32 nextOid = 0;
  3735. word32 nextSz = 80; /* Maximum known curve size is 66. */
  3736. word32 currOid = ssl->ecdhCurveOID;
  3737. int ephmSuite = 0;
  3738. word16 octets = 0; /* according to 'ecc_set_type ecc_sets[];' */
  3739. int key = 0; /* validate key */
  3740. (void)oid;
  3741. if (first == CHACHA_BYTE) {
  3742. switch (second) {
  3743. case TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
  3744. case TLS_PSK_WITH_CHACHA20_POLY1305_SHA256:
  3745. case TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
  3746. case TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256:
  3747. return 1; /* no suite restriction */
  3748. case TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
  3749. case TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256:
  3750. case TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
  3751. break;
  3752. }
  3753. }
  3754. if (first == ECC_BYTE || first == ECDHE_PSK_BYTE || first == CHACHA_BYTE)
  3755. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3756. if (!extension)
  3757. return 1; /* no suite restriction */
  3758. for (curve = (SupportedCurve*)extension->data;
  3759. curve && !key;
  3760. curve = curve->next) {
  3761. #ifdef OPENSSL_EXTRA
  3762. /* skip if name is not in supported ECC range
  3763. * or disabled by user */
  3764. if (curve->name > WOLFSSL_ECC_MAX ||
  3765. wolfSSL_curve_is_disabled(ssl, curve->name))
  3766. continue;
  3767. #endif
  3768. /* find supported curve */
  3769. switch (curve->name) {
  3770. #ifdef HAVE_ECC
  3771. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  3772. #ifndef NO_ECC_SECP
  3773. case WOLFSSL_ECC_SECP160R1:
  3774. oid = ECC_SECP160R1_OID;
  3775. octets = 20;
  3776. break;
  3777. #endif /* !NO_ECC_SECP */
  3778. #ifdef HAVE_ECC_SECPR2
  3779. case WOLFSSL_ECC_SECP160R2:
  3780. oid = ECC_SECP160R2_OID;
  3781. octets = 20;
  3782. break;
  3783. #endif /* HAVE_ECC_SECPR2 */
  3784. #ifdef HAVE_ECC_KOBLITZ
  3785. case WOLFSSL_ECC_SECP160K1:
  3786. oid = ECC_SECP160K1_OID;
  3787. octets = 20;
  3788. break;
  3789. #endif /* HAVE_ECC_KOBLITZ */
  3790. #endif
  3791. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  3792. #ifndef NO_ECC_SECP
  3793. case WOLFSSL_ECC_SECP192R1:
  3794. oid = ECC_SECP192R1_OID;
  3795. octets = 24;
  3796. break;
  3797. #endif /* !NO_ECC_SECP */
  3798. #ifdef HAVE_ECC_KOBLITZ
  3799. case WOLFSSL_ECC_SECP192K1:
  3800. oid = ECC_SECP192K1_OID;
  3801. octets = 24;
  3802. break;
  3803. #endif /* HAVE_ECC_KOBLITZ */
  3804. #endif
  3805. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  3806. #ifndef NO_ECC_SECP
  3807. case WOLFSSL_ECC_SECP224R1:
  3808. oid = ECC_SECP224R1_OID;
  3809. octets = 28;
  3810. break;
  3811. #endif /* !NO_ECC_SECP */
  3812. #ifdef HAVE_ECC_KOBLITZ
  3813. case WOLFSSL_ECC_SECP224K1:
  3814. oid = ECC_SECP224K1_OID;
  3815. octets = 28;
  3816. break;
  3817. #endif /* HAVE_ECC_KOBLITZ */
  3818. #endif
  3819. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  3820. #ifndef NO_ECC_SECP
  3821. case WOLFSSL_ECC_SECP256R1:
  3822. oid = ECC_SECP256R1_OID;
  3823. octets = 32;
  3824. break;
  3825. #endif /* !NO_ECC_SECP */
  3826. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  3827. #endif
  3828. #if (defined(HAVE_CURVE25519) || defined(HAVE_ED25519)) && ECC_MIN_KEY_SZ <= 256
  3829. case WOLFSSL_ECC_X25519:
  3830. oid = ECC_X25519_OID;
  3831. octets = 32;
  3832. break;
  3833. #endif /* HAVE_CURVE25519 */
  3834. #ifdef HAVE_ECC
  3835. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  3836. #ifdef HAVE_ECC_KOBLITZ
  3837. case WOLFSSL_ECC_SECP256K1:
  3838. oid = ECC_SECP256K1_OID;
  3839. octets = 32;
  3840. break;
  3841. #endif /* HAVE_ECC_KOBLITZ */
  3842. #ifdef HAVE_ECC_BRAINPOOL
  3843. case WOLFSSL_ECC_BRAINPOOLP256R1:
  3844. oid = ECC_BRAINPOOLP256R1_OID;
  3845. octets = 32;
  3846. break;
  3847. #endif /* HAVE_ECC_BRAINPOOL */
  3848. #endif
  3849. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  3850. #ifndef NO_ECC_SECP
  3851. case WOLFSSL_ECC_SECP384R1:
  3852. oid = ECC_SECP384R1_OID;
  3853. octets = 48;
  3854. break;
  3855. #endif /* !NO_ECC_SECP */
  3856. #ifdef HAVE_ECC_BRAINPOOL
  3857. case WOLFSSL_ECC_BRAINPOOLP384R1:
  3858. oid = ECC_BRAINPOOLP384R1_OID;
  3859. octets = 48;
  3860. break;
  3861. #endif /* HAVE_ECC_BRAINPOOL */
  3862. #endif
  3863. #endif
  3864. #if (defined(HAVE_CURVE448) || defined(HAVE_ED448)) && ECC_MIN_KEY_SZ <= 448
  3865. case WOLFSSL_ECC_X448:
  3866. oid = ECC_X448_OID;
  3867. octets = 57;
  3868. break;
  3869. #endif /* HAVE_CURVE448 */
  3870. #ifdef HAVE_ECC
  3871. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  3872. #ifdef HAVE_ECC_BRAINPOOL
  3873. case WOLFSSL_ECC_BRAINPOOLP512R1:
  3874. oid = ECC_BRAINPOOLP512R1_OID;
  3875. octets = 64;
  3876. break;
  3877. #endif /* HAVE_ECC_BRAINPOOL */
  3878. #endif
  3879. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  3880. #ifndef NO_ECC_SECP
  3881. case WOLFSSL_ECC_SECP521R1:
  3882. oid = ECC_SECP521R1_OID;
  3883. octets = 66;
  3884. break;
  3885. #endif /* !NO_ECC_SECP */
  3886. #endif
  3887. #endif
  3888. default: continue; /* unsupported curve */
  3889. }
  3890. #ifdef HAVE_ECC
  3891. /* Set default Oid */
  3892. if (defOid == 0 && ssl->eccTempKeySz <= octets && defSz > octets) {
  3893. defOid = oid;
  3894. defSz = octets;
  3895. }
  3896. /* The eccTempKeySz is the preferred ephemeral key size */
  3897. if (currOid == 0 && ssl->eccTempKeySz == octets)
  3898. currOid = oid;
  3899. if ((nextOid == 0 || nextSz > octets) && ssl->eccTempKeySz <= octets) {
  3900. nextOid = oid;
  3901. nextSz = octets;
  3902. }
  3903. #else
  3904. if (defOid == 0 && defSz > octets) {
  3905. defOid = oid;
  3906. defSz = octets;
  3907. }
  3908. if (currOid == 0)
  3909. currOid = oid;
  3910. if (nextOid == 0 || nextSz > octets) {
  3911. nextOid = oid;
  3912. nextSz = octets;
  3913. }
  3914. #endif
  3915. if (first == ECC_BYTE) {
  3916. switch (second) {
  3917. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
  3918. /* ECDHE_ECDSA */
  3919. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA:
  3920. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA:
  3921. case TLS_ECDHE_ECDSA_WITH_RC4_128_SHA:
  3922. case TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA:
  3923. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256:
  3924. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384:
  3925. case TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
  3926. case TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:
  3927. case TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8:
  3928. case TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8:
  3929. key |= ssl->ecdhCurveOID == oid;
  3930. ephmSuite = 1;
  3931. break;
  3932. #ifdef WOLFSSL_STATIC_DH
  3933. /* ECDH_ECDSA */
  3934. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA:
  3935. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA:
  3936. case TLS_ECDH_ECDSA_WITH_RC4_128_SHA:
  3937. case TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA:
  3938. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256:
  3939. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384:
  3940. case TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256:
  3941. case TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384:
  3942. if (oid == ECC_X25519_OID && defOid == oid) {
  3943. defOid = 0;
  3944. defSz = 80;
  3945. }
  3946. if (oid == ECC_X448_OID && defOid == oid) {
  3947. defOid = 0;
  3948. defSz = 80;
  3949. }
  3950. key |= ssl->pkCurveOID == oid;
  3951. break;
  3952. #endif /* WOLFSSL_STATIC_DH */
  3953. #endif /* HAVE_ECC || HAVE_ED25519 || HAVE_ED448 */
  3954. #ifndef NO_RSA
  3955. /* ECDHE_RSA */
  3956. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA:
  3957. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA:
  3958. case TLS_ECDHE_RSA_WITH_RC4_128_SHA:
  3959. case TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA:
  3960. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256:
  3961. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384:
  3962. case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:
  3963. case TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384:
  3964. key |= ssl->ecdhCurveOID == oid;
  3965. ephmSuite = 1;
  3966. break;
  3967. #if defined(HAVE_ECC) && defined(WOLFSSL_STATIC_DH)
  3968. /* ECDH_RSA */
  3969. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA:
  3970. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA:
  3971. case TLS_ECDH_RSA_WITH_RC4_128_SHA:
  3972. case TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA:
  3973. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256:
  3974. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384:
  3975. case TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256:
  3976. case TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384:
  3977. if (oid == ECC_X25519_OID && defOid == oid) {
  3978. defOid = 0;
  3979. defSz = 80;
  3980. }
  3981. if (oid == ECC_X448_OID && defOid == oid) {
  3982. defOid = 0;
  3983. defSz = 80;
  3984. }
  3985. key |= ssl->pkCurveOID == oid;
  3986. break;
  3987. #endif /* HAVE_ECC && WOLFSSL_STATIC_DH */
  3988. #endif
  3989. default:
  3990. if (oid == ECC_X25519_OID && defOid == oid) {
  3991. defOid = 0;
  3992. defSz = 80;
  3993. }
  3994. if (oid == ECC_X448_OID && defOid == oid) {
  3995. defOid = 0;
  3996. defSz = 80;
  3997. }
  3998. key = 1;
  3999. break;
  4000. }
  4001. }
  4002. /* ChaCha20-Poly1305 ECC cipher suites */
  4003. if (first == CHACHA_BYTE) {
  4004. switch (second) {
  4005. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
  4006. /* ECDHE_ECDSA */
  4007. case TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 :
  4008. case TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 :
  4009. key |= ssl->ecdhCurveOID == oid;
  4010. ephmSuite = 1;
  4011. break;
  4012. #endif /* HAVE_ECC || HAVE_ED25519 || HAVE_ED448 */
  4013. #ifndef NO_RSA
  4014. /* ECDHE_RSA */
  4015. case TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 :
  4016. case TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 :
  4017. key |= ssl->ecdhCurveOID == oid;
  4018. ephmSuite = 1;
  4019. break;
  4020. #endif
  4021. default:
  4022. key = 1;
  4023. break;
  4024. }
  4025. }
  4026. }
  4027. /* Choose the default if it is at the required strength. */
  4028. #ifdef HAVE_ECC
  4029. if (ssl->ecdhCurveOID == 0 && defSz == ssl->eccTempKeySz)
  4030. #else
  4031. if (ssl->ecdhCurveOID == 0)
  4032. #endif
  4033. {
  4034. key = 1;
  4035. ssl->ecdhCurveOID = defOid;
  4036. }
  4037. /* Choose any curve at the required strength. */
  4038. if (ssl->ecdhCurveOID == 0) {
  4039. key = 1;
  4040. ssl->ecdhCurveOID = currOid;
  4041. }
  4042. /* Choose the default if it is at the next highest strength. */
  4043. if (ssl->ecdhCurveOID == 0 && defSz == nextSz)
  4044. ssl->ecdhCurveOID = defOid;
  4045. /* Choose any curve at the next highest strength. */
  4046. if (ssl->ecdhCurveOID == 0)
  4047. ssl->ecdhCurveOID = nextOid;
  4048. /* No curve and ephemeral ECC suite requires a matching curve. */
  4049. if (ssl->ecdhCurveOID == 0 && ephmSuite)
  4050. key = 0;
  4051. return key;
  4052. }
  4053. #endif
  4054. #endif /* NO_WOLFSSL_SERVER */
  4055. int TLSX_UseSupportedCurve(TLSX** extensions, word16 name, void* heap)
  4056. {
  4057. TLSX* extension = NULL;
  4058. SupportedCurve* curve = NULL;
  4059. int ret;
  4060. if (extensions == NULL) {
  4061. return BAD_FUNC_ARG;
  4062. }
  4063. #ifdef WOLFSSL_TLS13
  4064. if (! TLSX_KeyShare_IsSupported(name)) {
  4065. return BAD_FUNC_ARG;
  4066. }
  4067. #endif
  4068. extension = TLSX_Find(*extensions, TLSX_SUPPORTED_GROUPS);
  4069. if (!extension) {
  4070. ret = TLSX_SupportedCurve_New(&curve, name, heap);
  4071. if (ret != 0)
  4072. return ret;
  4073. ret = TLSX_Push(extensions, TLSX_SUPPORTED_GROUPS, curve, heap);
  4074. if (ret != 0) {
  4075. XFREE(curve, heap, DYNAMIC_TYPE_TLSX);
  4076. return ret;
  4077. }
  4078. }
  4079. else {
  4080. ret = TLSX_SupportedCurve_Append((SupportedCurve*)extension->data, name,
  4081. heap);
  4082. if (ret != 0)
  4083. return ret;
  4084. }
  4085. return WOLFSSL_SUCCESS;
  4086. }
  4087. int TLSX_UsePointFormat(TLSX** extensions, byte format, void* heap)
  4088. {
  4089. TLSX* extension = NULL;
  4090. PointFormat* point = NULL;
  4091. int ret = 0;
  4092. if (extensions == NULL)
  4093. return BAD_FUNC_ARG;
  4094. extension = TLSX_Find(*extensions, TLSX_EC_POINT_FORMATS);
  4095. if (!extension) {
  4096. ret = TLSX_PointFormat_New(&point, format, heap);
  4097. if (ret != 0)
  4098. return ret;
  4099. ret = TLSX_Push(extensions, TLSX_EC_POINT_FORMATS, point, heap);
  4100. if (ret != 0) {
  4101. XFREE(point, heap, DYNAMIC_TYPE_TLSX);
  4102. return ret;
  4103. }
  4104. }
  4105. else {
  4106. ret = TLSX_PointFormat_Append((PointFormat*)extension->data, format,
  4107. heap);
  4108. if (ret != 0)
  4109. return ret;
  4110. }
  4111. return WOLFSSL_SUCCESS;
  4112. }
  4113. #define EC_FREE_ALL TLSX_SupportedCurve_FreeAll
  4114. #define EC_VALIDATE_REQUEST TLSX_SupportedCurve_ValidateRequest
  4115. #ifndef NO_WOLFSSL_CLIENT
  4116. #define EC_GET_SIZE TLSX_SupportedCurve_GetSize
  4117. #define EC_WRITE TLSX_SupportedCurve_Write
  4118. #else
  4119. #define EC_GET_SIZE(list) 0
  4120. #define EC_WRITE(a, b) 0
  4121. #endif
  4122. #if !defined(NO_WOLFSSL_SERVER) || (defined(WOLFSSL_TLS13) && \
  4123. !defined(WOLFSSL_NO_SERVER_GROUPS_EXT))
  4124. #define EC_PARSE TLSX_SupportedCurve_Parse
  4125. #else
  4126. #define EC_PARSE(a, b, c, d) 0
  4127. #endif
  4128. #define PF_FREE_ALL TLSX_PointFormat_FreeAll
  4129. #define PF_VALIDATE_REQUEST TLSX_PointFormat_ValidateRequest
  4130. #define PF_VALIDATE_RESPONSE TLSX_PointFormat_ValidateResponse
  4131. #define PF_GET_SIZE TLSX_PointFormat_GetSize
  4132. #define PF_WRITE TLSX_PointFormat_Write
  4133. #ifndef NO_WOLFSSL_SERVER
  4134. #define PF_PARSE TLSX_PointFormat_Parse
  4135. #else
  4136. #define PF_PARSE(a, b, c, d) 0
  4137. #endif
  4138. #else
  4139. #define EC_FREE_ALL(list, heap)
  4140. #define EC_GET_SIZE(list) 0
  4141. #define EC_WRITE(a, b) 0
  4142. #define EC_PARSE(a, b, c, d) 0
  4143. #define EC_VALIDATE_REQUEST(a, b)
  4144. #define PF_FREE_ALL(list, heap)
  4145. #define PF_GET_SIZE(list) 0
  4146. #define PF_WRITE(a, b) 0
  4147. #define PF_PARSE(a, b, c, d) 0
  4148. #define PF_VALIDATE_REQUEST(a, b)
  4149. #define PF_VALIDATE_RESPONSE(a, b)
  4150. #endif /* HAVE_SUPPORTED_CURVES */
  4151. /******************************************************************************/
  4152. /* Renegotiation Indication */
  4153. /******************************************************************************/
  4154. #if defined(HAVE_SECURE_RENEGOTIATION) \
  4155. || defined(HAVE_SERVER_RENEGOTIATION_INFO)
  4156. static byte TLSX_SecureRenegotiation_GetSize(SecureRenegotiation* data,
  4157. int isRequest)
  4158. {
  4159. byte length = OPAQUE8_LEN; /* empty info length */
  4160. /* data will be NULL for HAVE_SERVER_RENEGOTIATION_INFO only */
  4161. if (data && data->enabled && data->verifySet) {
  4162. /* client sends client_verify_data only */
  4163. length += TLS_FINISHED_SZ;
  4164. /* server also sends server_verify_data */
  4165. if (!isRequest)
  4166. length += TLS_FINISHED_SZ;
  4167. }
  4168. return length;
  4169. }
  4170. static word16 TLSX_SecureRenegotiation_Write(SecureRenegotiation* data,
  4171. byte* output, int isRequest)
  4172. {
  4173. word16 offset = OPAQUE8_LEN; /* RenegotiationInfo length */
  4174. if (data && data->enabled && data->verifySet) {
  4175. /* client sends client_verify_data only */
  4176. XMEMCPY(output + offset, data->client_verify_data, TLS_FINISHED_SZ);
  4177. offset += TLS_FINISHED_SZ;
  4178. /* server also sends server_verify_data */
  4179. if (!isRequest) {
  4180. XMEMCPY(output + offset, data->server_verify_data, TLS_FINISHED_SZ);
  4181. offset += TLS_FINISHED_SZ;
  4182. }
  4183. }
  4184. output[0] = (byte)(offset - 1); /* info length - self */
  4185. return offset;
  4186. }
  4187. static int TLSX_SecureRenegotiation_Parse(WOLFSSL* ssl, const byte* input,
  4188. word16 length, byte isRequest)
  4189. {
  4190. int ret = SECURE_RENEGOTIATION_E;
  4191. if (length >= OPAQUE8_LEN) {
  4192. if (isRequest) {
  4193. #ifndef NO_WOLFSSL_SERVER
  4194. if (ssl->secure_renegotiation == NULL) {
  4195. ret = wolfSSL_UseSecureRenegotiation(ssl);
  4196. if (ret == WOLFSSL_SUCCESS)
  4197. ret = 0;
  4198. }
  4199. if (ret != 0 && ret != SECURE_RENEGOTIATION_E) {
  4200. }
  4201. else if (ssl->secure_renegotiation == NULL) {
  4202. }
  4203. else if (!ssl->secure_renegotiation->enabled) {
  4204. if (*input == 0) {
  4205. input++; /* get past size */
  4206. ssl->secure_renegotiation->enabled = 1;
  4207. TLSX_SetResponse(ssl, TLSX_RENEGOTIATION_INFO);
  4208. ret = 0;
  4209. }
  4210. else {
  4211. /* already in error state */
  4212. WOLFSSL_MSG("SCR client verify data present");
  4213. }
  4214. }
  4215. else if (*input == TLS_FINISHED_SZ) {
  4216. if (length < TLS_FINISHED_SZ + 1) {
  4217. WOLFSSL_MSG("SCR malformed buffer");
  4218. ret = BUFFER_E;
  4219. }
  4220. else {
  4221. input++; /* get past size */
  4222. /* validate client verify data */
  4223. if (XMEMCMP(input,
  4224. ssl->secure_renegotiation->client_verify_data,
  4225. TLS_FINISHED_SZ) == 0) {
  4226. WOLFSSL_MSG("SCR client verify data match");
  4227. TLSX_SetResponse(ssl, TLSX_RENEGOTIATION_INFO);
  4228. ret = 0; /* verified */
  4229. }
  4230. else {
  4231. /* already in error state */
  4232. WOLFSSL_MSG("SCR client verify data Failure");
  4233. }
  4234. }
  4235. }
  4236. #endif
  4237. }
  4238. else if (ssl->secure_renegotiation != NULL) {
  4239. #ifndef NO_WOLFSSL_CLIENT
  4240. if (!ssl->secure_renegotiation->enabled) {
  4241. if (*input == 0) {
  4242. ssl->secure_renegotiation->enabled = 1;
  4243. ret = 0;
  4244. }
  4245. }
  4246. else if (*input == 2 * TLS_FINISHED_SZ &&
  4247. length == 2 * TLS_FINISHED_SZ + OPAQUE8_LEN) {
  4248. input++; /* get past size */
  4249. /* validate client and server verify data */
  4250. if (XMEMCMP(input,
  4251. ssl->secure_renegotiation->client_verify_data,
  4252. TLS_FINISHED_SZ) == 0 &&
  4253. XMEMCMP(input + TLS_FINISHED_SZ,
  4254. ssl->secure_renegotiation->server_verify_data,
  4255. TLS_FINISHED_SZ) == 0) {
  4256. WOLFSSL_MSG("SCR client and server verify data match");
  4257. ret = 0; /* verified */
  4258. }
  4259. else {
  4260. /* already in error state */
  4261. WOLFSSL_MSG("SCR client and server verify data Failure");
  4262. }
  4263. }
  4264. #endif
  4265. }
  4266. }
  4267. if (ret != 0) {
  4268. WOLFSSL_ERROR_VERBOSE(ret);
  4269. SendAlert(ssl, alert_fatal, handshake_failure);
  4270. }
  4271. return ret;
  4272. }
  4273. int TLSX_UseSecureRenegotiation(TLSX** extensions, void* heap)
  4274. {
  4275. int ret = 0;
  4276. SecureRenegotiation* data;
  4277. data = (SecureRenegotiation*)XMALLOC(sizeof(SecureRenegotiation), heap,
  4278. DYNAMIC_TYPE_TLSX);
  4279. if (data == NULL)
  4280. return MEMORY_E;
  4281. XMEMSET(data, 0, sizeof(SecureRenegotiation));
  4282. ret = TLSX_Push(extensions, TLSX_RENEGOTIATION_INFO, data, heap);
  4283. if (ret != 0) {
  4284. XFREE(data, heap, DYNAMIC_TYPE_TLSX);
  4285. return ret;
  4286. }
  4287. return WOLFSSL_SUCCESS;
  4288. }
  4289. #ifdef HAVE_SERVER_RENEGOTIATION_INFO
  4290. int TLSX_AddEmptyRenegotiationInfo(TLSX** extensions, void* heap)
  4291. {
  4292. int ret;
  4293. /* send empty renegotiation_info extension */
  4294. TLSX* ext = TLSX_Find(*extensions, TLSX_RENEGOTIATION_INFO);
  4295. if (ext == NULL) {
  4296. ret = TLSX_UseSecureRenegotiation(extensions, heap);
  4297. if (ret != WOLFSSL_SUCCESS)
  4298. return ret;
  4299. ext = TLSX_Find(*extensions, TLSX_RENEGOTIATION_INFO);
  4300. }
  4301. if (ext)
  4302. ext->resp = 1;
  4303. return WOLFSSL_SUCCESS;
  4304. }
  4305. #endif /* HAVE_SERVER_RENEGOTIATION_INFO */
  4306. #define SCR_FREE_ALL(data, heap) XFREE(data, (heap), DYNAMIC_TYPE_TLSX)
  4307. #define SCR_GET_SIZE TLSX_SecureRenegotiation_GetSize
  4308. #define SCR_WRITE TLSX_SecureRenegotiation_Write
  4309. #define SCR_PARSE TLSX_SecureRenegotiation_Parse
  4310. #else
  4311. #define SCR_FREE_ALL(a, heap)
  4312. #define SCR_GET_SIZE(a, b) 0
  4313. #define SCR_WRITE(a, b, c) 0
  4314. #define SCR_PARSE(a, b, c, d) 0
  4315. #endif /* HAVE_SECURE_RENEGOTIATION || HAVE_SERVER_RENEGOTIATION_INFO */
  4316. /******************************************************************************/
  4317. /* Session Tickets */
  4318. /******************************************************************************/
  4319. #ifdef HAVE_SESSION_TICKET
  4320. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
  4321. static void TLSX_SessionTicket_ValidateRequest(WOLFSSL* ssl)
  4322. {
  4323. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_SESSION_TICKET);
  4324. SessionTicket* ticket = extension ?
  4325. (SessionTicket*)extension->data : NULL;
  4326. if (ticket) {
  4327. /* TODO validate ticket timeout here! */
  4328. if (ticket->lifetime == 0xfffffff) {
  4329. /* send empty ticket on timeout */
  4330. TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
  4331. }
  4332. }
  4333. }
  4334. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
  4335. static word16 TLSX_SessionTicket_GetSize(SessionTicket* ticket, int isRequest)
  4336. {
  4337. (void)isRequest;
  4338. return ticket ? ticket->size : 0;
  4339. }
  4340. static word16 TLSX_SessionTicket_Write(SessionTicket* ticket, byte* output,
  4341. int isRequest)
  4342. {
  4343. word16 offset = 0; /* empty ticket */
  4344. if (isRequest && ticket) {
  4345. XMEMCPY(output + offset, ticket->data, ticket->size);
  4346. offset += ticket->size;
  4347. }
  4348. return offset;
  4349. }
  4350. static int TLSX_SessionTicket_Parse(WOLFSSL* ssl, const byte* input,
  4351. word16 length, byte isRequest)
  4352. {
  4353. int ret = 0;
  4354. (void) input; /* avoid unused parameter if NO_WOLFSSL_SERVER defined */
  4355. if (!isRequest) {
  4356. if (TLSX_CheckUnsupportedExtension(ssl, TLSX_SESSION_TICKET))
  4357. return TLSX_HandleUnsupportedExtension(ssl);
  4358. if (length != 0)
  4359. return BUFFER_ERROR;
  4360. #ifndef NO_WOLFSSL_CLIENT
  4361. ssl->expect_session_ticket = 1;
  4362. #endif
  4363. }
  4364. #ifndef NO_WOLFSSL_SERVER
  4365. else {
  4366. /* server side */
  4367. if (ssl->ctx->ticketEncCb == NULL) {
  4368. WOLFSSL_MSG("Client sent session ticket, server has no callback");
  4369. return 0;
  4370. }
  4371. if (length > SESSION_TICKET_LEN) {
  4372. ret = BAD_TICKET_MSG_SZ;
  4373. WOLFSSL_ERROR_VERBOSE(ret);
  4374. } else if (IsAtLeastTLSv1_3(ssl->version)) {
  4375. WOLFSSL_MSG("Process client ticket rejected, TLS 1.3 no support");
  4376. ssl->options.rejectTicket = 1;
  4377. ret = 0; /* not fatal */
  4378. } else if (ssl->options.noTicketTls12) {
  4379. /* ignore ticket request */
  4380. } else if (length == 0) {
  4381. /* blank ticket */
  4382. ret = TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
  4383. if (ret == WOLFSSL_SUCCESS) {
  4384. ret = 0;
  4385. /* send blank ticket */
  4386. TLSX_SetResponse(ssl, TLSX_SESSION_TICKET);
  4387. ssl->options.createTicket = 1; /* will send ticket msg */
  4388. ssl->options.useTicket = 1;
  4389. ssl->options.resuming = 0; /* no standard resumption */
  4390. ssl->arrays->sessionIDSz = 0; /* no echo on blank ticket */
  4391. }
  4392. } else {
  4393. /* got actual ticket from client */
  4394. ret = DoClientTicket(ssl, input, length);
  4395. if (ret == WOLFSSL_TICKET_RET_OK) { /* use ticket to resume */
  4396. WOLFSSL_MSG("Using existing client ticket");
  4397. ssl->options.useTicket = 1;
  4398. ssl->options.resuming = 1;
  4399. /* SERVER: ticket is peer auth. */
  4400. ssl->options.peerAuthGood = 1;
  4401. } else if (ret == WOLFSSL_TICKET_RET_CREATE) {
  4402. WOLFSSL_MSG("Using existing client ticket, creating new one");
  4403. ret = TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
  4404. if (ret == WOLFSSL_SUCCESS) {
  4405. ret = 0;
  4406. TLSX_SetResponse(ssl, TLSX_SESSION_TICKET);
  4407. /* send blank ticket */
  4408. ssl->options.createTicket = 1; /* will send ticket msg */
  4409. ssl->options.useTicket = 1;
  4410. ssl->options.resuming = 1;
  4411. /* SERVER: ticket is peer auth. */
  4412. ssl->options.peerAuthGood = 1;
  4413. }
  4414. } else if (ret == WOLFSSL_TICKET_RET_REJECT) {
  4415. WOLFSSL_MSG("Process client ticket rejected, not using");
  4416. ssl->options.rejectTicket = 1;
  4417. ret = 0; /* not fatal */
  4418. } else if (ret == VERSION_ERROR) {
  4419. WOLFSSL_MSG("Process client ticket rejected, bad TLS version");
  4420. ssl->options.rejectTicket = 1;
  4421. ret = 0; /* not fatal */
  4422. } else if (ret == WOLFSSL_TICKET_RET_FATAL) {
  4423. WOLFSSL_MSG("Process client ticket fatal error, not using");
  4424. } else if (ret < 0) {
  4425. WOLFSSL_MSG("Process client ticket unknown error, not using");
  4426. }
  4427. }
  4428. }
  4429. #endif /* NO_WOLFSSL_SERVER */
  4430. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  4431. (void)ssl;
  4432. #endif
  4433. return ret;
  4434. }
  4435. WOLFSSL_LOCAL SessionTicket* TLSX_SessionTicket_Create(word32 lifetime,
  4436. byte* data, word16 size, void* heap)
  4437. {
  4438. SessionTicket* ticket = (SessionTicket*)XMALLOC(sizeof(SessionTicket),
  4439. heap, DYNAMIC_TYPE_TLSX);
  4440. if (ticket) {
  4441. ticket->data = (byte*)XMALLOC(size, heap, DYNAMIC_TYPE_TLSX);
  4442. if (ticket->data == NULL) {
  4443. XFREE(ticket, heap, DYNAMIC_TYPE_TLSX);
  4444. return NULL;
  4445. }
  4446. XMEMCPY(ticket->data, data, size);
  4447. ticket->size = size;
  4448. ticket->lifetime = lifetime;
  4449. }
  4450. (void)heap;
  4451. return ticket;
  4452. }
  4453. WOLFSSL_LOCAL void TLSX_SessionTicket_Free(SessionTicket* ticket, void* heap)
  4454. {
  4455. if (ticket) {
  4456. XFREE(ticket->data, heap, DYNAMIC_TYPE_TLSX);
  4457. XFREE(ticket, heap, DYNAMIC_TYPE_TLSX);
  4458. }
  4459. (void)heap;
  4460. }
  4461. int TLSX_UseSessionTicket(TLSX** extensions, SessionTicket* ticket, void* heap)
  4462. {
  4463. int ret = 0;
  4464. if (extensions == NULL)
  4465. return BAD_FUNC_ARG;
  4466. /* If the ticket is NULL, the client will request a new ticket from the
  4467. server. Otherwise, the client will use it in the next client hello. */
  4468. if ((ret = TLSX_Push(extensions, TLSX_SESSION_TICKET, (void*)ticket, heap))
  4469. != 0)
  4470. return ret;
  4471. return WOLFSSL_SUCCESS;
  4472. }
  4473. #define WOLF_STK_VALIDATE_REQUEST TLSX_SessionTicket_ValidateRequest
  4474. #define WOLF_STK_GET_SIZE TLSX_SessionTicket_GetSize
  4475. #define WOLF_STK_WRITE TLSX_SessionTicket_Write
  4476. #define WOLF_STK_PARSE TLSX_SessionTicket_Parse
  4477. #define WOLF_STK_FREE(stk, heap) TLSX_SessionTicket_Free((SessionTicket*)(stk),(heap))
  4478. #else
  4479. #define WOLF_STK_FREE(a, b)
  4480. #define WOLF_STK_VALIDATE_REQUEST(a)
  4481. #define WOLF_STK_GET_SIZE(a, b) 0
  4482. #define WOLF_STK_WRITE(a, b, c) 0
  4483. #define WOLF_STK_PARSE(a, b, c, d) 0
  4484. #endif /* HAVE_SESSION_TICKET */
  4485. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  4486. /******************************************************************************/
  4487. /* Encrypt-then-MAC */
  4488. /******************************************************************************/
  4489. #ifndef WOLFSSL_NO_TLS12
  4490. static int TLSX_EncryptThenMac_Use(WOLFSSL* ssl);
  4491. /**
  4492. * Get the size of the Encrypt-Then-MAC extension.
  4493. *
  4494. * msgType Type of message to put extension into.
  4495. * pSz Size of extension data.
  4496. * return SANITY_MSG_E when the message is not allowed to have extension and
  4497. * 0 otherwise.
  4498. */
  4499. static int TLSX_EncryptThenMac_GetSize(byte msgType, word16* pSz)
  4500. {
  4501. (void)pSz;
  4502. if (msgType != client_hello && msgType != server_hello) {
  4503. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4504. return SANITY_MSG_E;
  4505. }
  4506. /* Empty extension */
  4507. return 0;
  4508. }
  4509. /**
  4510. * Write the Encrypt-Then-MAC extension.
  4511. *
  4512. * data Unused
  4513. * output Extension data buffer. Unused.
  4514. * msgType Type of message to put extension into.
  4515. * pSz Size of extension data.
  4516. * return SANITY_MSG_E when the message is not allowed to have extension and
  4517. * 0 otherwise.
  4518. */
  4519. static int TLSX_EncryptThenMac_Write(void* data, byte* output, byte msgType,
  4520. word16* pSz)
  4521. {
  4522. (void)data;
  4523. (void)output;
  4524. (void)pSz;
  4525. if (msgType != client_hello && msgType != server_hello) {
  4526. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4527. return SANITY_MSG_E;
  4528. }
  4529. /* Empty extension */
  4530. return 0;
  4531. }
  4532. /**
  4533. * Parse the Encrypt-Then-MAC extension.
  4534. *
  4535. * ssl SSL object
  4536. * input Extension data buffer.
  4537. * length Length of this extension's data.
  4538. * msgType Type of message to extension appeared in.
  4539. * return SANITY_MSG_E when the message is not allowed to have extension,
  4540. * BUFFER_ERROR when the extension's data is invalid,
  4541. * MEMORY_E when unable to allocate memory and
  4542. * 0 otherwise.
  4543. */
  4544. static int TLSX_EncryptThenMac_Parse(WOLFSSL* ssl, const byte* input,
  4545. word16 length, byte msgType)
  4546. {
  4547. int ret;
  4548. (void)input;
  4549. if (msgType != client_hello && msgType != server_hello) {
  4550. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4551. return SANITY_MSG_E;
  4552. }
  4553. /* Empty extension */
  4554. if (length != 0)
  4555. return BUFFER_ERROR;
  4556. if (msgType == client_hello) {
  4557. /* Check the user hasn't disallowed use of Encrypt-Then-Mac. */
  4558. if (!ssl->options.disallowEncThenMac) {
  4559. ssl->options.encThenMac = 1;
  4560. /* Set the extension reply. */
  4561. ret = TLSX_EncryptThenMac_Use(ssl);
  4562. if (ret != 0)
  4563. return ret;
  4564. }
  4565. return 0;
  4566. }
  4567. /* Server Hello */
  4568. if (ssl->options.disallowEncThenMac) {
  4569. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4570. return SANITY_MSG_E;
  4571. }
  4572. ssl->options.encThenMac = 1;
  4573. return 0;
  4574. }
  4575. /**
  4576. * Add the Encrypt-Then-MAC extension to list.
  4577. *
  4578. * ssl SSL object
  4579. * return MEMORY_E when unable to allocate memory and 0 otherwise.
  4580. */
  4581. static int TLSX_EncryptThenMac_Use(WOLFSSL* ssl)
  4582. {
  4583. int ret = 0;
  4584. TLSX* extension;
  4585. /* Find the Encrypt-Then-Mac extension if it exists. */
  4586. extension = TLSX_Find(ssl->extensions, TLSX_ENCRYPT_THEN_MAC);
  4587. if (extension == NULL) {
  4588. /* Push new Encrypt-Then-Mac extension. */
  4589. ret = TLSX_Push(&ssl->extensions, TLSX_ENCRYPT_THEN_MAC, NULL,
  4590. ssl->heap);
  4591. if (ret != 0)
  4592. return ret;
  4593. }
  4594. return 0;
  4595. }
  4596. /**
  4597. * Set the Encrypt-Then-MAC extension as one to respond too.
  4598. *
  4599. * ssl SSL object
  4600. * return EXT_MISSING when EncryptThenMac extension not in list.
  4601. */
  4602. int TLSX_EncryptThenMac_Respond(WOLFSSL* ssl)
  4603. {
  4604. TLSX* extension;
  4605. extension = TLSX_Find(ssl->extensions, TLSX_ENCRYPT_THEN_MAC);
  4606. if (extension == NULL)
  4607. return EXT_MISSING;
  4608. extension->resp = 1;
  4609. return 0;
  4610. }
  4611. #define ETM_GET_SIZE TLSX_EncryptThenMac_GetSize
  4612. #define ETM_WRITE TLSX_EncryptThenMac_Write
  4613. #define ETM_PARSE TLSX_EncryptThenMac_Parse
  4614. #else
  4615. #define ETM_GET_SIZE(a, b) 0
  4616. #define ETM_WRITE(a, b, c, d) 0
  4617. #define ETM_PARSE(a, b, c, d) 0
  4618. #endif /* !WOLFSSL_NO_TLS12 */
  4619. #endif /* HAVE_ENCRYPT_THEN_MAC && !WOLFSSL_AEAD_ONLY */
  4620. #ifdef WOLFSSL_SRTP
  4621. /******************************************************************************/
  4622. /* DTLS SRTP (Secure Real-time Transport Protocol) */
  4623. /******************************************************************************/
  4624. /* Only support single SRTP profile */
  4625. typedef struct TlsxSrtp {
  4626. word16 profileCount;
  4627. word16 ids; /* selected bits */
  4628. } TlsxSrtp;
  4629. static int TLSX_UseSRTP_GetSize(TlsxSrtp *srtp)
  4630. {
  4631. /* SRTP Profile Len (2)
  4632. * SRTP Profiles (2)
  4633. * MKI (master key id) Length */
  4634. return (OPAQUE16_LEN + (srtp->profileCount * OPAQUE16_LEN) + 1);
  4635. }
  4636. static TlsxSrtp* TLSX_UseSRTP_New(word16 ids, void* heap)
  4637. {
  4638. TlsxSrtp* srtp;
  4639. int i;
  4640. srtp = (TlsxSrtp*)XMALLOC(sizeof(TlsxSrtp), heap, DYNAMIC_TYPE_TLSX);
  4641. if (srtp == NULL) {
  4642. WOLFSSL_MSG("TLSX SRTP Memory failure");
  4643. return NULL;
  4644. }
  4645. /* count and test each bit set */
  4646. srtp->profileCount = 0;
  4647. for (i=0; i<16; i++) {
  4648. if (ids & (1 << i)) {
  4649. srtp->profileCount++;
  4650. }
  4651. }
  4652. srtp->ids = ids;
  4653. return srtp;
  4654. }
  4655. static void TLSX_UseSRTP_Free(TlsxSrtp *srtp, void* heap)
  4656. {
  4657. if (srtp != NULL) {
  4658. XFREE(srtp, heap, DYNAMIC_TYPE_TLSX);
  4659. }
  4660. (void)heap;
  4661. }
  4662. static int TLSX_UseSRTP_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  4663. byte isRequest)
  4664. {
  4665. int ret = BAD_FUNC_ARG;
  4666. word16 profile_len = 0;
  4667. word16 profile_value = 0;
  4668. word16 offset = 0;
  4669. #ifndef NO_WOLFSSL_SERVER
  4670. int i;
  4671. TlsxSrtp* srtp = NULL;
  4672. #endif
  4673. if (length < OPAQUE16_LEN) {
  4674. return BUFFER_ERROR;
  4675. }
  4676. /* reset selected DTLS SRTP profile ID */
  4677. ssl->dtlsSrtpId = 0;
  4678. /* total length, not include itself */
  4679. ato16(input, &profile_len);
  4680. offset += OPAQUE16_LEN;
  4681. if (!isRequest) {
  4682. #ifndef NO_WOLFSSL_CLIENT
  4683. if (length < offset + OPAQUE16_LEN)
  4684. return BUFFER_ERROR;
  4685. ato16(input + offset, &profile_value);
  4686. /* check that the profile received was in the ones we support */
  4687. if (profile_value < 16 &&
  4688. (ssl->dtlsSrtpProfiles & (1 << profile_value))) {
  4689. ssl->dtlsSrtpId = profile_value;
  4690. ret = 0; /* success */
  4691. }
  4692. #endif
  4693. }
  4694. #ifndef NO_WOLFSSL_SERVER
  4695. else {
  4696. /* parse remainder one profile at a time, looking for match in CTX */
  4697. ret = 0;
  4698. for (i=offset; i<length; i+=OPAQUE16_LEN) {
  4699. ato16(input+i, &profile_value);
  4700. /* find first match */
  4701. if (profile_value < 16 &&
  4702. ssl->dtlsSrtpProfiles & (1 << profile_value)) {
  4703. ssl->dtlsSrtpId = profile_value;
  4704. /* make sure we respond with selected SRTP id selected */
  4705. srtp = TLSX_UseSRTP_New((1 << profile_value), ssl->heap);
  4706. if (srtp != NULL) {
  4707. ret = TLSX_Push(&ssl->extensions, TLSX_USE_SRTP,
  4708. (void*)srtp, ssl->heap);
  4709. if (ret == 0) {
  4710. TLSX_SetResponse(ssl, TLSX_USE_SRTP);
  4711. /* successfully set extension */
  4712. }
  4713. }
  4714. else {
  4715. ret = MEMORY_E;
  4716. }
  4717. break;
  4718. }
  4719. }
  4720. }
  4721. if (ret == 0 && ssl->dtlsSrtpId == 0) {
  4722. WOLFSSL_MSG("TLSX_UseSRTP_Parse profile not found!");
  4723. /* not fatal */
  4724. }
  4725. else if (ret != 0) {
  4726. ssl->dtlsSrtpId = 0;
  4727. TLSX_UseSRTP_Free(srtp, ssl->heap);
  4728. }
  4729. #endif
  4730. (void)profile_len;
  4731. return ret;
  4732. }
  4733. static word16 TLSX_UseSRTP_Write(TlsxSrtp* srtp, byte* output)
  4734. {
  4735. word16 offset = 0;
  4736. int i, j;
  4737. c16toa(srtp->profileCount*2, output+offset);
  4738. offset += OPAQUE16_LEN;
  4739. for (i=0; i< srtp->profileCount; i+=2) {
  4740. for (j=0; j<16; j++) {
  4741. if (srtp->ids & (1 << j)) {
  4742. c16toa(j, output+offset);
  4743. offset += OPAQUE16_LEN;
  4744. }
  4745. }
  4746. }
  4747. output[offset++] = 0x00; /* MKI Length */
  4748. return offset;
  4749. }
  4750. static int TLSX_UseSRTP(TLSX** extensions, word16 profiles, void* heap)
  4751. {
  4752. int ret = 0;
  4753. TLSX* extension;
  4754. if (extensions == NULL) {
  4755. return BAD_FUNC_ARG;
  4756. }
  4757. extension = TLSX_Find(*extensions, TLSX_USE_SRTP);
  4758. if (extension == NULL) {
  4759. TlsxSrtp* srtp = TLSX_UseSRTP_New(profiles, heap);
  4760. if (srtp == NULL) {
  4761. return MEMORY_E;
  4762. }
  4763. ret = TLSX_Push(extensions, TLSX_USE_SRTP, (void*)srtp, heap);
  4764. if (ret != 0) {
  4765. TLSX_UseSRTP_Free(srtp, heap);
  4766. }
  4767. }
  4768. return ret;
  4769. }
  4770. #ifndef NO_WOLFSSL_SERVER
  4771. #define SRTP_FREE TLSX_UseSRTP_Free
  4772. #define SRTP_PARSE TLSX_UseSRTP_Parse
  4773. #define SRTP_WRITE TLSX_UseSRTP_Write
  4774. #define SRTP_GET_SIZE TLSX_UseSRTP_GetSize
  4775. #else
  4776. #define SRTP_FREE(a, b)
  4777. #define SRTP_PARSE(a, b, c, d) 0
  4778. #define SRTP_WRITE(a, b) 0
  4779. #define SRTP_GET_SIZE(a) 0
  4780. #endif
  4781. #endif /* WOLFSSL_SRTP */
  4782. /******************************************************************************/
  4783. /* Supported Versions */
  4784. /******************************************************************************/
  4785. #ifdef WOLFSSL_TLS13
  4786. static WC_INLINE int versionIsGreater(byte isDtls, byte a, byte b)
  4787. {
  4788. (void)isDtls;
  4789. #ifdef WOLFSSL_DTLS
  4790. /* DTLS version increases backwards (-1,-2,-3,etc) */
  4791. if (isDtls)
  4792. return a < b;
  4793. #endif /* WOLFSSL_DTLS */
  4794. return a > b;
  4795. }
  4796. static WC_INLINE int versionIsLesser(byte isDtls, byte a, byte b)
  4797. {
  4798. (void)isDtls;
  4799. #ifdef WOLFSSL_DTLS
  4800. /* DTLS version increases backwards (-1,-2,-3,etc) */
  4801. if (isDtls)
  4802. return a > b;
  4803. #endif /* WOLFSSL_DTLS */
  4804. return a < b;
  4805. }
  4806. static WC_INLINE int versionIsAtLeast(byte isDtls, byte a, byte b)
  4807. {
  4808. (void)isDtls;
  4809. #ifdef WOLFSSL_DTLS
  4810. /* DTLS version increases backwards (-1,-2,-3,etc) */
  4811. if (isDtls)
  4812. return a <= b;
  4813. #endif /* WOLFSSL_DTLS */
  4814. return a >= b;
  4815. }
  4816. static WC_INLINE int versionIsLessEqual(byte isDtls, byte a, byte b)
  4817. {
  4818. (void)isDtls;
  4819. #ifdef WOLFSSL_DTLS
  4820. /* DTLS version increases backwards (-1,-2,-3,etc) */
  4821. if (isDtls)
  4822. return a >= b;
  4823. #endif /* WOLFSSL_DTLS */
  4824. return a <= b;
  4825. }
  4826. /* Return the size of the SupportedVersions extension's data.
  4827. *
  4828. * data The SSL/TLS object.
  4829. * msgType The type of the message this extension is being written into.
  4830. * returns the length of data that will be in the extension.
  4831. */
  4832. static int TLSX_SupportedVersions_GetSize(void* data, byte msgType, word16* pSz)
  4833. {
  4834. WOLFSSL* ssl = (WOLFSSL*)data;
  4835. byte tls13Minor, tls12Minor, tls11Minor, isDtls;
  4836. isDtls = !!ssl->options.dtls;
  4837. tls13Minor = (byte)(isDtls ? DTLSv1_3_MINOR : TLSv1_3_MINOR);
  4838. tls12Minor = (byte)(isDtls ? DTLSv1_2_MINOR : TLSv1_2_MINOR);
  4839. tls11Minor = (byte)(isDtls ? DTLS_MINOR : TLSv1_1_MINOR);
  4840. /* unused on some configuration */
  4841. (void)tls12Minor;
  4842. (void)tls13Minor;
  4843. (void)tls11Minor;
  4844. if (msgType == client_hello) {
  4845. /* TLS v1.2 and TLS v1.3 */
  4846. int cnt = 0;
  4847. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls13Minor)
  4848. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4849. defined(WOLFSSL_WPAS_SMALL)
  4850. && (ssl->options.mask & SSL_OP_NO_TLSv1_3) == 0
  4851. #endif
  4852. ) {
  4853. cnt++;
  4854. }
  4855. if (ssl->options.downgrade) {
  4856. #ifndef WOLFSSL_NO_TLS12
  4857. if (versionIsLessEqual(
  4858. isDtls, ssl->options.minDowngrade, tls12Minor)
  4859. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4860. defined(WOLFSSL_WPAS_SMALL)
  4861. && (ssl->options.mask & SSL_OP_NO_TLSv1_2) == 0
  4862. #endif
  4863. ) {
  4864. cnt++;
  4865. }
  4866. #endif
  4867. #ifndef NO_OLD_TLS
  4868. if (versionIsLessEqual(
  4869. isDtls, ssl->options.minDowngrade, tls11Minor)
  4870. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4871. defined(WOLFSSL_WPAS_SMALL)
  4872. && (ssl->options.mask & SSL_OP_NO_TLSv1_1) == 0
  4873. #endif
  4874. ) {
  4875. cnt++;
  4876. }
  4877. #ifdef WOLFSSL_ALLOW_TLSV10
  4878. if (!ssl->options.dtls && (ssl->options.minDowngrade <= TLSv1_MINOR)
  4879. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4880. defined(WOLFSSL_WPAS_SMALL)
  4881. && (ssl->options.mask & SSL_OP_NO_TLSv1) == 0
  4882. #endif
  4883. ) {
  4884. cnt++;
  4885. }
  4886. #endif
  4887. #endif
  4888. }
  4889. *pSz += (word16)(OPAQUE8_LEN + cnt * OPAQUE16_LEN);
  4890. }
  4891. else if (msgType == server_hello || msgType == hello_retry_request) {
  4892. *pSz += OPAQUE16_LEN;
  4893. }
  4894. else {
  4895. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4896. return SANITY_MSG_E;
  4897. }
  4898. return 0;
  4899. }
  4900. /* Writes the SupportedVersions extension into the buffer.
  4901. *
  4902. * data The SSL/TLS object.
  4903. * output The buffer to write the extension into.
  4904. * msgType The type of the message this extension is being written into.
  4905. * returns the length of data that was written.
  4906. */
  4907. static int TLSX_SupportedVersions_Write(void* data, byte* output,
  4908. byte msgType, word16* pSz)
  4909. {
  4910. WOLFSSL* ssl = (WOLFSSL*)data;
  4911. byte major;
  4912. byte* cnt;
  4913. byte tls13minor, tls12minor, tls11minor, isDtls = 0;
  4914. tls13minor = (byte)TLSv1_3_MINOR;
  4915. tls12minor = (byte)TLSv1_2_MINOR;
  4916. tls11minor = (byte)TLSv1_1_MINOR;
  4917. /* unused in some configuration */
  4918. (void)tls11minor;
  4919. (void)tls12minor;
  4920. #ifdef WOLFSSL_DTLS13
  4921. if (ssl->options.dtls) {
  4922. tls13minor = (byte)DTLSv1_3_MINOR;
  4923. tls12minor = (byte)DTLSv1_2_MINOR;
  4924. tls11minor = (byte)DTLS_MINOR;
  4925. isDtls = 1;
  4926. }
  4927. #endif /* WOLFSSL_DTLS13 */
  4928. if (msgType == client_hello) {
  4929. major = ssl->ctx->method->version.major;
  4930. cnt = output++;
  4931. *cnt = 0;
  4932. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls13minor)
  4933. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4934. defined(WOLFSSL_WPAS_SMALL)
  4935. && (ssl->options.mask & SSL_OP_NO_TLSv1_3) == 0
  4936. #endif
  4937. ) {
  4938. *cnt += OPAQUE16_LEN;
  4939. #ifdef WOLFSSL_TLS13_DRAFT
  4940. /* The TLS draft major number. */
  4941. *(output++) = TLS_DRAFT_MAJOR;
  4942. /* Version of draft supported. */
  4943. *(output++) = TLS_DRAFT_MINOR;
  4944. #else
  4945. *(output++) = major;
  4946. *(output++) = tls13minor;
  4947. #endif
  4948. }
  4949. if (ssl->options.downgrade) {
  4950. #ifndef WOLFSSL_NO_TLS12
  4951. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls12minor)
  4952. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4953. defined(WOLFSSL_WPAS_SMALL)
  4954. && (ssl->options.mask & SSL_OP_NO_TLSv1_2) == 0
  4955. #endif
  4956. ) {
  4957. *cnt += OPAQUE16_LEN;
  4958. *(output++) = major;
  4959. *(output++) = tls12minor;
  4960. }
  4961. #endif
  4962. #ifndef NO_OLD_TLS
  4963. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls11minor)
  4964. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4965. defined(WOLFSSL_WPAS_SMALL)
  4966. && (ssl->options.mask & SSL_OP_NO_TLSv1_1) == 0
  4967. #endif
  4968. ) {
  4969. *cnt += OPAQUE16_LEN;
  4970. *(output++) = major;
  4971. *(output++) = tls11minor;
  4972. }
  4973. #ifdef WOLFSSL_ALLOW_TLSV10
  4974. if (!ssl->options.dtls && (ssl->options.minDowngrade <= TLSv1_MINOR)
  4975. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4976. defined(WOLFSSL_WPAS_SMALL)
  4977. && (ssl->options.mask & SSL_OP_NO_TLSv1) == 0
  4978. #endif
  4979. ) {
  4980. *cnt += OPAQUE16_LEN;
  4981. *(output++) = major;
  4982. *(output++) = (byte)TLSv1_MINOR;
  4983. }
  4984. #endif
  4985. #endif
  4986. }
  4987. *pSz += (word16)(OPAQUE8_LEN + *cnt);
  4988. }
  4989. else if (msgType == server_hello || msgType == hello_retry_request) {
  4990. output[0] = ssl->version.major;
  4991. output[1] = ssl->version.minor;
  4992. *pSz += OPAQUE16_LEN;
  4993. }
  4994. else {
  4995. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4996. return SANITY_MSG_E;
  4997. }
  4998. return 0;
  4999. }
  5000. /* Parse the SupportedVersions extension.
  5001. *
  5002. * ssl The SSL/TLS object.
  5003. * input The buffer with the extension data.
  5004. * length The length of the extension data.
  5005. * msgType The type of the message this extension is being parsed from.
  5006. * returns 0 on success, otherwise failure.
  5007. */
  5008. static int TLSX_SupportedVersions_Parse(WOLFSSL* ssl, const byte* input,
  5009. word16 length, byte msgType)
  5010. {
  5011. ProtocolVersion pv = ssl->ctx->method->version;
  5012. int i;
  5013. int len;
  5014. byte newMinor = 0;
  5015. int set = 0;
  5016. int ret;
  5017. byte major, minor;
  5018. byte tls13minor, tls12minor;
  5019. byte isDtls;
  5020. tls13minor = TLSv1_3_MINOR;
  5021. tls12minor = TLSv1_2_MINOR;
  5022. isDtls = ssl->options.dtls == 1;
  5023. #ifdef WOLFSSL_DTLS13
  5024. if (ssl->options.dtls) {
  5025. tls13minor = DTLSv1_3_MINOR;
  5026. tls12minor = DTLSv1_2_MINOR;
  5027. }
  5028. #endif /* WOLFSSL_DTLS13 */
  5029. if (msgType == client_hello) {
  5030. /* Must contain a length and at least one version. */
  5031. if (length < OPAQUE8_LEN + OPAQUE16_LEN || (length & 1) != 1)
  5032. return BUFFER_ERROR;
  5033. len = *input;
  5034. /* Protocol version array must fill rest of data. */
  5035. if (length != (word16)OPAQUE8_LEN + len)
  5036. return BUFFER_ERROR;
  5037. input++;
  5038. /* Find first match. */
  5039. for (i = 0; i < len; i += OPAQUE16_LEN) {
  5040. major = input[i];
  5041. minor = input[i + OPAQUE8_LEN];
  5042. #ifdef WOLFSSL_TLS13_DRAFT
  5043. if (major == TLS_DRAFT_MAJOR && minor == TLS_DRAFT_MINOR) {
  5044. major = SSLv3_MAJOR;
  5045. minor = TLSv1_3_MINOR;
  5046. }
  5047. #else
  5048. if (major == TLS_DRAFT_MAJOR)
  5049. continue;
  5050. #endif
  5051. if (major != pv.major)
  5052. continue;
  5053. /* No upgrade allowed. */
  5054. if (versionIsGreater(isDtls, minor, ssl->version.minor))
  5055. continue;
  5056. /* Check downgrade. */
  5057. if (versionIsLesser(isDtls, minor, ssl->version.minor)) {
  5058. if (!ssl->options.downgrade)
  5059. continue;
  5060. if (versionIsLesser(
  5061. isDtls, minor, ssl->options.minDowngrade))
  5062. continue;
  5063. if (newMinor == 0 &&
  5064. versionIsGreater(
  5065. isDtls, minor, ssl->options.oldMinor)) {
  5066. /* Downgrade the version. */
  5067. ssl->version.minor = minor;
  5068. }
  5069. }
  5070. if (versionIsAtLeast(isDtls, minor, tls13minor)) {
  5071. ssl->options.tls1_3 = 1;
  5072. /* TLS v1.3 requires supported version extension */
  5073. if (TLSX_Find(ssl->extensions, TLSX_SUPPORTED_VERSIONS) == NULL) {
  5074. ret = TLSX_Prepend(&ssl->extensions,
  5075. TLSX_SUPPORTED_VERSIONS, ssl, ssl->heap);
  5076. if (ret != 0) {
  5077. return ret;
  5078. }
  5079. TLSX_SetResponse(ssl, TLSX_SUPPORTED_VERSIONS);
  5080. }
  5081. if (versionIsGreater(isDtls, minor, newMinor)) {
  5082. ssl->version.minor = minor;
  5083. newMinor = minor;
  5084. }
  5085. }
  5086. else if (versionIsGreater(
  5087. isDtls, minor, ssl->options.oldMinor))
  5088. ssl->options.oldMinor = minor;
  5089. set = 1;
  5090. }
  5091. if (!set) {
  5092. SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version);
  5093. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5094. return VERSION_ERROR;
  5095. }
  5096. }
  5097. else if (msgType == server_hello || msgType == hello_retry_request) {
  5098. /* Must contain one version. */
  5099. if (length != OPAQUE16_LEN)
  5100. return BUFFER_ERROR;
  5101. major = input[0];
  5102. minor = input[OPAQUE8_LEN];
  5103. if (major != pv.major) {
  5104. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5105. return VERSION_ERROR;
  5106. }
  5107. /* Can't downgrade with this extension below TLS v1.3. */
  5108. if (versionIsLesser(isDtls, minor, tls13minor)) {
  5109. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5110. return VERSION_ERROR;
  5111. }
  5112. /* Version is TLS v1.2 to handle downgrading from TLS v1.3+. */
  5113. if (ssl->options.downgrade && ssl->version.minor == tls12minor) {
  5114. /* Set minor version back to TLS v1.3+ */
  5115. ssl->version.minor = ssl->ctx->method->version.minor;
  5116. }
  5117. /* No upgrade allowed. */
  5118. if (versionIsLesser(isDtls, ssl->version.minor, minor)) {
  5119. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5120. return VERSION_ERROR;
  5121. }
  5122. /* Check downgrade. */
  5123. if (versionIsGreater(isDtls, ssl->version.minor, minor)) {
  5124. if (!ssl->options.downgrade) {
  5125. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5126. return VERSION_ERROR;
  5127. }
  5128. if (versionIsLesser(
  5129. isDtls, minor, ssl->options.minDowngrade)) {
  5130. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5131. return VERSION_ERROR;
  5132. }
  5133. /* Downgrade the version. */
  5134. ssl->version.minor = minor;
  5135. }
  5136. }
  5137. else {
  5138. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5139. return SANITY_MSG_E;
  5140. }
  5141. return 0;
  5142. }
  5143. /* Sets a new SupportedVersions extension into the extension list.
  5144. *
  5145. * extensions The list of extensions.
  5146. * data The extensions specific data.
  5147. * heap The heap used for allocation.
  5148. * returns 0 on success, otherwise failure.
  5149. */
  5150. static int TLSX_SetSupportedVersions(TLSX** extensions, const void* data,
  5151. void* heap)
  5152. {
  5153. if (extensions == NULL || data == NULL)
  5154. return BAD_FUNC_ARG;
  5155. return TLSX_Push(extensions, TLSX_SUPPORTED_VERSIONS, data, heap);
  5156. }
  5157. #define SV_GET_SIZE TLSX_SupportedVersions_GetSize
  5158. #define SV_WRITE TLSX_SupportedVersions_Write
  5159. #define SV_PARSE TLSX_SupportedVersions_Parse
  5160. #else
  5161. #define SV_GET_SIZE(a, b, c) 0
  5162. #define SV_WRITE(a, b, c, d) 0
  5163. #define SV_PARSE(a, b, c, d) 0
  5164. #endif /* WOLFSSL_TLS13 */
  5165. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  5166. /******************************************************************************/
  5167. /* Cookie */
  5168. /******************************************************************************/
  5169. /* Free the cookie data.
  5170. *
  5171. * cookie Cookie data.
  5172. * heap The heap used for allocation.
  5173. */
  5174. static void TLSX_Cookie_FreeAll(Cookie* cookie, void* heap)
  5175. {
  5176. (void)heap;
  5177. if (cookie != NULL)
  5178. XFREE(cookie, heap, DYNAMIC_TYPE_TLSX);
  5179. }
  5180. /* Get the size of the encoded Cookie extension.
  5181. * In messages: ClientHello and HelloRetryRequest.
  5182. *
  5183. * cookie The cookie to write.
  5184. * msgType The type of the message this extension is being written into.
  5185. * returns the number of bytes of the encoded Cookie extension.
  5186. */
  5187. static int TLSX_Cookie_GetSize(Cookie* cookie, byte msgType, word16* pSz)
  5188. {
  5189. if (msgType == client_hello || msgType == hello_retry_request) {
  5190. *pSz += OPAQUE16_LEN + cookie->len;
  5191. }
  5192. else {
  5193. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5194. return SANITY_MSG_E;
  5195. }
  5196. return 0;
  5197. }
  5198. /* Writes the Cookie extension into the output buffer.
  5199. * Assumes that the the output buffer is big enough to hold data.
  5200. * In messages: ClientHello and HelloRetryRequest.
  5201. *
  5202. * cookie The cookie to write.
  5203. * output The buffer to write into.
  5204. * msgType The type of the message this extension is being written into.
  5205. * returns the number of bytes written into the buffer.
  5206. */
  5207. static int TLSX_Cookie_Write(Cookie* cookie, byte* output, byte msgType,
  5208. word16* pSz)
  5209. {
  5210. if (msgType == client_hello || msgType == hello_retry_request) {
  5211. c16toa(cookie->len, output);
  5212. output += OPAQUE16_LEN;
  5213. XMEMCPY(output, &cookie->data, cookie->len);
  5214. *pSz += OPAQUE16_LEN + cookie->len;
  5215. }
  5216. else {
  5217. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5218. return SANITY_MSG_E;
  5219. }
  5220. return 0;
  5221. }
  5222. /* Parse the Cookie extension.
  5223. * In messages: ClientHello and HelloRetryRequest.
  5224. *
  5225. * ssl The SSL/TLS object.
  5226. * input The extension data.
  5227. * length The length of the extension data.
  5228. * msgType The type of the message this extension is being parsed from.
  5229. * returns 0 on success and other values indicate failure.
  5230. */
  5231. static int TLSX_Cookie_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  5232. byte msgType)
  5233. {
  5234. word16 len;
  5235. word16 idx = 0;
  5236. TLSX* extension;
  5237. Cookie* cookie;
  5238. if (msgType != client_hello && msgType != hello_retry_request) {
  5239. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5240. return SANITY_MSG_E;
  5241. }
  5242. /* Message contains length and Cookie which must be at least one byte
  5243. * in length.
  5244. */
  5245. if (length < OPAQUE16_LEN + 1)
  5246. return BUFFER_E;
  5247. ato16(input + idx, &len);
  5248. idx += OPAQUE16_LEN;
  5249. if (length - idx != len)
  5250. return BUFFER_E;
  5251. if (msgType == hello_retry_request)
  5252. return TLSX_Cookie_Use(ssl, input + idx, len, NULL, 0, 0);
  5253. /* client_hello */
  5254. extension = TLSX_Find(ssl->extensions, TLSX_COOKIE);
  5255. if (extension == NULL) {
  5256. #ifdef WOLFSSL_DTLS13
  5257. if (ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version))
  5258. /* Allow a cookie extension with DTLS 1.3 because it is possible
  5259. * that a different SSL instance sent the cookie but we are now
  5260. * receiving it. */
  5261. return TLSX_Cookie_Use(ssl, input + idx, len, NULL, 0, 0);
  5262. else
  5263. #endif
  5264. {
  5265. WOLFSSL_ERROR_VERBOSE(HRR_COOKIE_ERROR);
  5266. return HRR_COOKIE_ERROR;
  5267. }
  5268. }
  5269. cookie = (Cookie*)extension->data;
  5270. if (cookie->len != len || XMEMCMP(&cookie->data, input + idx, len) != 0) {
  5271. WOLFSSL_ERROR_VERBOSE(HRR_COOKIE_ERROR);
  5272. return HRR_COOKIE_ERROR;
  5273. }
  5274. /* Request seen. */
  5275. extension->resp = 0;
  5276. return 0;
  5277. }
  5278. /* Use the data to create a new Cookie object in the extensions.
  5279. *
  5280. * ssl SSL/TLS object.
  5281. * data Cookie data.
  5282. * len Length of cookie data in bytes.
  5283. * mac MAC data.
  5284. * macSz Length of MAC data in bytes.
  5285. * resp Indicates the extension will go into a response (HelloRetryRequest).
  5286. * returns 0 on success and other values indicate failure.
  5287. */
  5288. int TLSX_Cookie_Use(WOLFSSL* ssl, const byte* data, word16 len, byte* mac,
  5289. byte macSz, int resp)
  5290. {
  5291. int ret = 0;
  5292. TLSX* extension;
  5293. Cookie* cookie;
  5294. /* Find the cookie extension if it exists. */
  5295. extension = TLSX_Find(ssl->extensions, TLSX_COOKIE);
  5296. if (extension == NULL) {
  5297. /* Push new cookie extension. */
  5298. ret = TLSX_Push(&ssl->extensions, TLSX_COOKIE, NULL, ssl->heap);
  5299. if (ret != 0)
  5300. return ret;
  5301. extension = TLSX_Find(ssl->extensions, TLSX_COOKIE);
  5302. if (extension == NULL)
  5303. return MEMORY_E;
  5304. }
  5305. /* The Cookie structure has one byte for cookie data already. */
  5306. cookie = (Cookie*)XMALLOC(sizeof(Cookie) + len + macSz - 1, ssl->heap,
  5307. DYNAMIC_TYPE_TLSX);
  5308. if (cookie == NULL)
  5309. return MEMORY_E;
  5310. cookie->len = len + macSz;
  5311. XMEMCPY(&cookie->data, data, len);
  5312. if (mac != NULL)
  5313. XMEMCPY(&cookie->data + len, mac, macSz);
  5314. if (extension->data != NULL)
  5315. XFREE(extension->data, ssl->heap, DYNAMIC_TYPE_TLSX);
  5316. extension->data = (void*)cookie;
  5317. extension->resp = (byte)resp;
  5318. return 0;
  5319. }
  5320. #define CKE_FREE_ALL TLSX_Cookie_FreeAll
  5321. #define CKE_GET_SIZE TLSX_Cookie_GetSize
  5322. #define CKE_WRITE TLSX_Cookie_Write
  5323. #define CKE_PARSE TLSX_Cookie_Parse
  5324. #else
  5325. #define CKE_FREE_ALL(a, b) 0
  5326. #define CKE_GET_SIZE(a, b, c) 0
  5327. #define CKE_WRITE(a, b, c, d) 0
  5328. #define CKE_PARSE(a, b, c, d) 0
  5329. #endif
  5330. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  5331. /******************************************************************************/
  5332. /* Signature Algorithms */
  5333. /******************************************************************************/
  5334. /* Return the size of the SignatureAlgorithms extension's data.
  5335. *
  5336. * data Unused
  5337. * returns the length of data that will be in the extension.
  5338. */
  5339. static word16 TLSX_SignatureAlgorithms_GetSize(void* data)
  5340. {
  5341. WOLFSSL* ssl = (WOLFSSL*)data;
  5342. return OPAQUE16_LEN + ssl->suites->hashSigAlgoSz;
  5343. }
  5344. /* Creates a bit string of supported hash algorithms with RSA PSS.
  5345. * The bit string is used when determining which signature algorithm to use
  5346. * when creating the CertificateVerify message.
  5347. * Note: Valid data has an even length as each signature algorithm is two bytes.
  5348. *
  5349. * ssl The SSL/TLS object.
  5350. * input The buffer with the list of supported signature algorithms.
  5351. * length The length of the list in bytes.
  5352. * returns 0 on success, BUFFER_ERROR when the length is not even.
  5353. */
  5354. static int TLSX_SignatureAlgorithms_MapPss(WOLFSSL *ssl, const byte* input,
  5355. word16 length)
  5356. {
  5357. word16 i;
  5358. if ((length & 1) == 1)
  5359. return BUFFER_ERROR;
  5360. ssl->pssAlgo = 0;
  5361. for (i = 0; i < length; i += 2) {
  5362. if (input[i] == rsa_pss_sa_algo && input[i + 1] <= sha512_mac)
  5363. ssl->pssAlgo |= 1 << input[i + 1];
  5364. #ifdef WOLFSSL_TLS13
  5365. if (input[i] == rsa_pss_sa_algo && input[i + 1] >= pss_sha256 &&
  5366. input[i + 1] <= pss_sha512) {
  5367. ssl->pssAlgo |= 1 << input[i + 1];
  5368. }
  5369. #endif
  5370. }
  5371. return 0;
  5372. }
  5373. /* Writes the SignatureAlgorithms extension into the buffer.
  5374. *
  5375. * data Unused
  5376. * output The buffer to write the extension into.
  5377. * returns the length of data that was written.
  5378. */
  5379. static word16 TLSX_SignatureAlgorithms_Write(void* data, byte* output)
  5380. {
  5381. WOLFSSL* ssl = (WOLFSSL*)data;
  5382. c16toa(ssl->suites->hashSigAlgoSz, output);
  5383. XMEMCPY(output + OPAQUE16_LEN, ssl->suites->hashSigAlgo,
  5384. ssl->suites->hashSigAlgoSz);
  5385. TLSX_SignatureAlgorithms_MapPss(ssl, output + OPAQUE16_LEN,
  5386. ssl->suites->hashSigAlgoSz);
  5387. return OPAQUE16_LEN + ssl->suites->hashSigAlgoSz;
  5388. }
  5389. /* Parse the SignatureAlgorithms extension.
  5390. *
  5391. * ssl The SSL/TLS object.
  5392. * input The buffer with the extension data.
  5393. * length The length of the extension data.
  5394. * returns 0 on success, otherwise failure.
  5395. */
  5396. static int TLSX_SignatureAlgorithms_Parse(WOLFSSL *ssl, const byte* input,
  5397. word16 length, byte isRequest, Suites* suites)
  5398. {
  5399. word16 len;
  5400. if (!isRequest)
  5401. return BUFFER_ERROR;
  5402. /* Must contain a length and at least algorithm. */
  5403. if (length < OPAQUE16_LEN + OPAQUE16_LEN || (length & 1) != 0)
  5404. return BUFFER_ERROR;
  5405. ato16(input, &len);
  5406. input += OPAQUE16_LEN;
  5407. /* Algorithm array must fill rest of data. */
  5408. if (length != OPAQUE16_LEN + len)
  5409. return BUFFER_ERROR;
  5410. /* Sig Algo list size must be even. */
  5411. if (suites->hashSigAlgoSz % 2 != 0)
  5412. return BUFFER_ERROR;
  5413. /* truncate hashSigAlgo list if too long */
  5414. suites->hashSigAlgoSz = len;
  5415. if (suites->hashSigAlgoSz > WOLFSSL_MAX_SIGALGO) {
  5416. WOLFSSL_MSG("TLSX SigAlgo list exceeds max, truncating");
  5417. suites->hashSigAlgoSz = WOLFSSL_MAX_SIGALGO;
  5418. }
  5419. XMEMCPY(suites->hashSigAlgo, input, suites->hashSigAlgoSz);
  5420. return TLSX_SignatureAlgorithms_MapPss(ssl, input, len);
  5421. }
  5422. /* Sets a new SignatureAlgorithms extension into the extension list.
  5423. *
  5424. * extensions The list of extensions.
  5425. * data The extensions specific data.
  5426. * heap The heap used for allocation.
  5427. * returns 0 on success, otherwise failure.
  5428. */
  5429. static int TLSX_SetSignatureAlgorithms(TLSX** extensions, const void* data,
  5430. void* heap)
  5431. {
  5432. if (extensions == NULL)
  5433. return BAD_FUNC_ARG;
  5434. return TLSX_Push(extensions, TLSX_SIGNATURE_ALGORITHMS, data, heap);
  5435. }
  5436. #define SA_GET_SIZE TLSX_SignatureAlgorithms_GetSize
  5437. #define SA_WRITE TLSX_SignatureAlgorithms_Write
  5438. #define SA_PARSE TLSX_SignatureAlgorithms_Parse
  5439. #endif
  5440. /******************************************************************************/
  5441. /* Signature Algorithms Certificate */
  5442. /******************************************************************************/
  5443. #if defined(WOLFSSL_TLS13) && !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  5444. /* Return the size of the SignatureAlgorithms extension's data.
  5445. *
  5446. * data Unused
  5447. * returns the length of data that will be in the extension.
  5448. */
  5449. static word16 TLSX_SignatureAlgorithmsCert_GetSize(void* data)
  5450. {
  5451. WOLFSSL* ssl = (WOLFSSL*)data;
  5452. return OPAQUE16_LEN + ssl->certHashSigAlgoSz;
  5453. }
  5454. /* Writes the SignatureAlgorithmsCert extension into the buffer.
  5455. *
  5456. * data Unused
  5457. * output The buffer to write the extension into.
  5458. * returns the length of data that was written.
  5459. */
  5460. static word16 TLSX_SignatureAlgorithmsCert_Write(void* data, byte* output)
  5461. {
  5462. WOLFSSL* ssl = (WOLFSSL*)data;
  5463. c16toa(ssl->certHashSigAlgoSz, output);
  5464. XMEMCPY(output + OPAQUE16_LEN, ssl->certHashSigAlgo,
  5465. ssl->certHashSigAlgoSz);
  5466. return OPAQUE16_LEN + ssl->certHashSigAlgoSz;
  5467. }
  5468. /* Parse the SignatureAlgorithmsCert extension.
  5469. *
  5470. * ssl The SSL/TLS object.
  5471. * input The buffer with the extension data.
  5472. * length The length of the extension data.
  5473. * returns 0 on success, otherwise failure.
  5474. */
  5475. static int TLSX_SignatureAlgorithmsCert_Parse(WOLFSSL *ssl, const byte* input,
  5476. word16 length, byte isRequest)
  5477. {
  5478. word16 len;
  5479. if (!isRequest)
  5480. return BUFFER_ERROR;
  5481. /* Must contain a length and at least algorithm. */
  5482. if (length < OPAQUE16_LEN + OPAQUE16_LEN || (length & 1) != 0)
  5483. return BUFFER_ERROR;
  5484. ato16(input, &len);
  5485. input += OPAQUE16_LEN;
  5486. /* Algorithm array must fill rest of data. */
  5487. if (length != OPAQUE16_LEN + len)
  5488. return BUFFER_ERROR;
  5489. /* truncate hashSigAlgo list if too long */
  5490. ssl->certHashSigAlgoSz = len;
  5491. if (ssl->certHashSigAlgoSz > WOLFSSL_MAX_SIGALGO) {
  5492. WOLFSSL_MSG("TLSX SigAlgo list exceeds max, truncating");
  5493. ssl->certHashSigAlgoSz = WOLFSSL_MAX_SIGALGO;
  5494. }
  5495. XMEMCPY(ssl->certHashSigAlgo, input, ssl->certHashSigAlgoSz);
  5496. return 0;
  5497. }
  5498. /* Sets a new SignatureAlgorithmsCert extension into the extension list.
  5499. *
  5500. * extensions The list of extensions.
  5501. * data The extensions specific data.
  5502. * heap The heap used for allocation.
  5503. * returns 0 on success, otherwise failure.
  5504. */
  5505. static int TLSX_SetSignatureAlgorithmsCert(TLSX** extensions, const void* data,
  5506. void* heap)
  5507. {
  5508. if (extensions == NULL)
  5509. return BAD_FUNC_ARG;
  5510. return TLSX_Push(extensions, TLSX_SIGNATURE_ALGORITHMS_CERT, data, heap);
  5511. }
  5512. #define SAC_GET_SIZE TLSX_SignatureAlgorithmsCert_GetSize
  5513. #define SAC_WRITE TLSX_SignatureAlgorithmsCert_Write
  5514. #define SAC_PARSE TLSX_SignatureAlgorithmsCert_Parse
  5515. #endif /* WOLFSSL_TLS13 */
  5516. /******************************************************************************/
  5517. /* Key Share */
  5518. /******************************************************************************/
  5519. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  5520. /* Create a key share entry using named Diffie-Hellman parameters group.
  5521. * Generates a key pair.
  5522. *
  5523. * ssl The SSL/TLS object.
  5524. * kse The key share entry object.
  5525. * returns 0 on success, otherwise failure.
  5526. */
  5527. static int TLSX_KeyShare_GenDhKey(WOLFSSL *ssl, KeyShareEntry* kse)
  5528. {
  5529. int ret = 0;
  5530. #if !defined(NO_DH) && (!defined(NO_CERTS) || !defined(NO_PSK))
  5531. word32 pSz = 0, pvtSz = 0;
  5532. DhKey* dhKey = (DhKey*)kse->key;
  5533. /* Pick the parameters from the named group. */
  5534. #ifdef HAVE_PUBLIC_FFDHE
  5535. const DhParams* params = NULL;
  5536. switch (kse->group) {
  5537. #ifdef HAVE_FFDHE_2048
  5538. case WOLFSSL_FFDHE_2048:
  5539. params = wc_Dh_ffdhe2048_Get();
  5540. kse->keyLen = 29;
  5541. break;
  5542. #endif
  5543. #ifdef HAVE_FFDHE_3072
  5544. case WOLFSSL_FFDHE_3072:
  5545. params = wc_Dh_ffdhe3072_Get();
  5546. kse->keyLen = 34;
  5547. break;
  5548. #endif
  5549. #ifdef HAVE_FFDHE_4096
  5550. case WOLFSSL_FFDHE_4096:
  5551. params = wc_Dh_ffdhe4096_Get();
  5552. kse->keyLen = 39;
  5553. break;
  5554. #endif
  5555. #ifdef HAVE_FFDHE_6144
  5556. case WOLFSSL_FFDHE_6144:
  5557. params = wc_Dh_ffdhe6144_Get();
  5558. kse->keyLen = 46;
  5559. break;
  5560. #endif
  5561. #ifdef HAVE_FFDHE_8192
  5562. case WOLFSSL_FFDHE_8192:
  5563. params = wc_Dh_ffdhe8192_Get();
  5564. kse->keyLen = 52;
  5565. break;
  5566. #endif
  5567. default:
  5568. break;
  5569. }
  5570. if (params == NULL)
  5571. return BAD_FUNC_ARG;
  5572. pSz = params->p_len;
  5573. pvtSz = kse->keyLen;
  5574. #else
  5575. kse->keyLen = wc_DhGetNamedKeyMinSize(kse->group);
  5576. if (kse->keyLen == 0) {
  5577. return BAD_FUNC_ARG;
  5578. }
  5579. ret = wc_DhGetNamedKeyParamSize(kse->group, &pSz, NULL, NULL);
  5580. if (ret != 0) {
  5581. return BAD_FUNC_ARG;
  5582. }
  5583. pvtSz = kse->keyLen;
  5584. #endif
  5585. kse->pubKeyLen = pSz;
  5586. /* Trigger Key Generation */
  5587. if (kse->pubKey == NULL || kse->privKey == NULL) {
  5588. if (kse->key == NULL) {
  5589. kse->key = (DhKey*)XMALLOC(sizeof(DhKey), ssl->heap,
  5590. DYNAMIC_TYPE_DH);
  5591. if (kse->key == NULL)
  5592. return MEMORY_E;
  5593. /* Setup Key */
  5594. ret = wc_InitDhKey_ex((DhKey*)kse->key, ssl->heap, ssl->devId);
  5595. if (ret == 0) {
  5596. dhKey = (DhKey*)kse->key;
  5597. #ifdef HAVE_PUBLIC_FFDHE
  5598. ret = wc_DhSetKey(dhKey, params->p, params->p_len, params->g,
  5599. params->g_len);
  5600. #else
  5601. ret = wc_DhSetNamedKey(dhKey, kse->group);
  5602. #endif
  5603. }
  5604. }
  5605. /* Allocate space for the private and public key */
  5606. if (ret == 0 && kse->pubKey == NULL) {
  5607. kse->pubKey = (byte*)XMALLOC(kse->pubKeyLen, ssl->heap,
  5608. DYNAMIC_TYPE_PUBLIC_KEY);
  5609. if (kse->pubKey == NULL)
  5610. ret = MEMORY_E;
  5611. }
  5612. if (ret == 0 && kse->privKey == NULL) {
  5613. kse->privKey = (byte*)XMALLOC(kse->keyLen, ssl->heap,
  5614. DYNAMIC_TYPE_PRIVATE_KEY);
  5615. if (kse->privKey == NULL)
  5616. ret = MEMORY_E;
  5617. }
  5618. if (ret == 0) {
  5619. #if defined(WOLFSSL_STATIC_EPHEMERAL) && defined(WOLFSSL_DH_EXTRA)
  5620. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_DH, kse->key);
  5621. if (ret == 0) {
  5622. ret = wc_DhExportKeyPair(dhKey,
  5623. (byte*)kse->privKey, &kse->keyLen, /* private */
  5624. kse->pubKey, &kse->pubKeyLen /* public */
  5625. );
  5626. }
  5627. else
  5628. #endif
  5629. {
  5630. /* Generate a new key pair */
  5631. /* For async this is called once and when event is done, the
  5632. * provided buffers will be populated.
  5633. * Final processing is zero pad below. */
  5634. ret = DhGenKeyPair(ssl, dhKey,
  5635. (byte*)kse->privKey, &kse->keyLen, /* private */
  5636. kse->pubKey, &kse->pubKeyLen /* public */
  5637. );
  5638. #ifdef WOLFSSL_ASYNC_CRYPT
  5639. if (ret == WC_PENDING_E) {
  5640. return ret;
  5641. }
  5642. #endif
  5643. }
  5644. }
  5645. }
  5646. if (ret == 0) {
  5647. if (pSz != kse->pubKeyLen) {
  5648. /* Zero pad the front of the public key to match prime "p" size */
  5649. XMEMMOVE(kse->pubKey + pSz - kse->pubKeyLen, kse->pubKey,
  5650. kse->pubKeyLen);
  5651. XMEMSET(kse->pubKey, 0, pSz - kse->pubKeyLen);
  5652. kse->pubKeyLen = pSz;
  5653. }
  5654. if (pvtSz != kse->keyLen) {
  5655. /* Zero pad the front of the private key */
  5656. XMEMMOVE(kse->privKey + pvtSz - kse->keyLen, kse->privKey,
  5657. kse->keyLen);
  5658. XMEMSET(kse->privKey, 0, pvtSz - kse->keyLen);
  5659. kse->keyLen = pvtSz;
  5660. }
  5661. #ifdef WOLFSSL_DEBUG_TLS
  5662. WOLFSSL_MSG("Public DH Key");
  5663. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  5664. #endif
  5665. }
  5666. /* Always release the DH key to free up memory.
  5667. * The DhKey will be setup again in TLSX_KeyShare_ProcessDh */
  5668. if (dhKey != NULL)
  5669. wc_FreeDhKey(dhKey);
  5670. if (kse->key != NULL) {
  5671. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_DH);
  5672. kse->key = NULL;
  5673. }
  5674. if (ret != 0) {
  5675. /* Cleanup on error, otherwise data owned by key share entry */
  5676. if (kse->privKey != NULL) {
  5677. XFREE(kse->privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5678. kse->privKey = NULL;
  5679. }
  5680. if (kse->pubKey != NULL) {
  5681. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5682. kse->pubKey = NULL;
  5683. }
  5684. }
  5685. #else
  5686. (void)ssl;
  5687. (void)kse;
  5688. ret = NOT_COMPILED_IN;
  5689. WOLFSSL_ERROR_VERBOSE(ret);
  5690. #endif
  5691. return ret;
  5692. }
  5693. /* Create a key share entry using X25519 parameters group.
  5694. * Generates a key pair.
  5695. *
  5696. * ssl The SSL/TLS object.
  5697. * kse The key share entry object.
  5698. * returns 0 on success, otherwise failure.
  5699. */
  5700. static int TLSX_KeyShare_GenX25519Key(WOLFSSL *ssl, KeyShareEntry* kse)
  5701. {
  5702. int ret = 0;
  5703. #ifdef HAVE_CURVE25519
  5704. curve25519_key* key = (curve25519_key*)kse->key;
  5705. if (kse->key == NULL) {
  5706. /* Allocate a Curve25519 key to hold private key. */
  5707. kse->key = (curve25519_key*)XMALLOC(sizeof(curve25519_key), ssl->heap,
  5708. DYNAMIC_TYPE_PRIVATE_KEY);
  5709. if (kse->key == NULL) {
  5710. WOLFSSL_MSG("GenX25519Key memory error");
  5711. return MEMORY_E;
  5712. }
  5713. /* Make an Curve25519 key. */
  5714. ret = wc_curve25519_init_ex((curve25519_key*)kse->key, ssl->heap,
  5715. INVALID_DEVID);
  5716. if (ret == 0) {
  5717. /* setting "key" means okay to call wc_curve25519_free */
  5718. key = (curve25519_key*)kse->key;
  5719. #ifdef WOLFSSL_STATIC_EPHEMERAL
  5720. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_CURVE25519, kse->key);
  5721. if (ret != 0)
  5722. #endif
  5723. {
  5724. ret = wc_curve25519_make_key(ssl->rng, CURVE25519_KEYSIZE, key);
  5725. }
  5726. }
  5727. }
  5728. if (ret == 0 && kse->pubKey == NULL) {
  5729. /* Allocate space for the public key. */
  5730. kse->pubKey = (byte*)XMALLOC(CURVE25519_KEYSIZE, ssl->heap,
  5731. DYNAMIC_TYPE_PUBLIC_KEY);
  5732. if (kse->pubKey == NULL) {
  5733. WOLFSSL_MSG("GenX25519Key pub memory error");
  5734. ret = MEMORY_E;
  5735. }
  5736. }
  5737. if (ret == 0) {
  5738. /* Export Curve25519 public key. */
  5739. kse->pubKeyLen = CURVE25519_KEYSIZE;
  5740. if (wc_curve25519_export_public_ex(key, kse->pubKey, &kse->pubKeyLen,
  5741. EC25519_LITTLE_ENDIAN) != 0) {
  5742. ret = ECC_EXPORT_ERROR;
  5743. WOLFSSL_ERROR_VERBOSE(ret);
  5744. }
  5745. kse->pubKeyLen = CURVE25519_KEYSIZE; /* always CURVE25519_KEYSIZE */
  5746. }
  5747. #ifdef WOLFSSL_DEBUG_TLS
  5748. if (ret == 0) {
  5749. WOLFSSL_MSG("Public Curve25519 Key");
  5750. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  5751. }
  5752. #endif
  5753. if (ret != 0) {
  5754. /* Data owned by key share entry otherwise. */
  5755. if (kse->pubKey != NULL) {
  5756. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5757. kse->pubKey = NULL;
  5758. }
  5759. if (key != NULL)
  5760. wc_curve25519_free(key);
  5761. if (kse->key != NULL) {
  5762. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5763. kse->key = NULL;
  5764. }
  5765. }
  5766. #else
  5767. (void)ssl;
  5768. (void)kse;
  5769. ret = NOT_COMPILED_IN;
  5770. WOLFSSL_ERROR_VERBOSE(ret);
  5771. #endif /* HAVE_CURVE25519 */
  5772. return ret;
  5773. }
  5774. /* Create a key share entry using X448 parameters group.
  5775. * Generates a key pair.
  5776. *
  5777. * ssl The SSL/TLS object.
  5778. * kse The key share entry object.
  5779. * returns 0 on success, otherwise failure.
  5780. */
  5781. static int TLSX_KeyShare_GenX448Key(WOLFSSL *ssl, KeyShareEntry* kse)
  5782. {
  5783. int ret = 0;
  5784. #ifdef HAVE_CURVE448
  5785. curve448_key* key = (curve448_key*)kse->key;
  5786. if (kse->key == NULL) {
  5787. /* Allocate a Curve448 key to hold private key. */
  5788. kse->key = (curve448_key*)XMALLOC(sizeof(curve448_key), ssl->heap,
  5789. DYNAMIC_TYPE_PRIVATE_KEY);
  5790. if (kse->key == NULL) {
  5791. WOLFSSL_MSG("GenX448Key memory error");
  5792. return MEMORY_E;
  5793. }
  5794. /* Make an Curve448 key. */
  5795. ret = wc_curve448_init((curve448_key*)kse->key);
  5796. if (ret == 0) {
  5797. key = (curve448_key*)kse->key;
  5798. #ifdef WOLFSSL_STATIC_EPHEMERAL
  5799. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_CURVE448, kse->key);
  5800. if (ret != 0)
  5801. #endif
  5802. {
  5803. ret = wc_curve448_make_key(ssl->rng, CURVE448_KEY_SIZE, key);
  5804. }
  5805. }
  5806. }
  5807. if (ret == 0 && kse->pubKey == NULL) {
  5808. /* Allocate space for the public key. */
  5809. kse->pubKey = (byte*)XMALLOC(CURVE448_KEY_SIZE, ssl->heap,
  5810. DYNAMIC_TYPE_PUBLIC_KEY);
  5811. if (kse->pubKey == NULL) {
  5812. WOLFSSL_MSG("GenX448Key pub memory error");
  5813. ret = MEMORY_E;
  5814. }
  5815. }
  5816. if (ret == 0) {
  5817. /* Export Curve448 public key. */
  5818. kse->pubKeyLen = CURVE448_KEY_SIZE;
  5819. if (wc_curve448_export_public_ex(key, kse->pubKey, &kse->pubKeyLen,
  5820. EC448_LITTLE_ENDIAN) != 0) {
  5821. ret = ECC_EXPORT_ERROR;
  5822. }
  5823. kse->pubKeyLen = CURVE448_KEY_SIZE; /* always CURVE448_KEY_SIZE */
  5824. }
  5825. #ifdef WOLFSSL_DEBUG_TLS
  5826. if (ret == 0) {
  5827. WOLFSSL_MSG("Public Curve448 Key");
  5828. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  5829. }
  5830. #endif
  5831. if (ret != 0) {
  5832. /* Data owned by key share entry otherwise. */
  5833. if (kse->pubKey != NULL) {
  5834. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5835. kse->pubKey = NULL;
  5836. }
  5837. if (key != NULL)
  5838. wc_curve448_free(key);
  5839. if (kse->key != NULL) {
  5840. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5841. kse->key = NULL;
  5842. }
  5843. }
  5844. #else
  5845. (void)ssl;
  5846. (void)kse;
  5847. ret = NOT_COMPILED_IN;
  5848. WOLFSSL_ERROR_VERBOSE(ret);
  5849. #endif /* HAVE_CURVE448 */
  5850. return ret;
  5851. }
  5852. /* Create a key share entry using named elliptic curve parameters group.
  5853. * Generates a key pair.
  5854. *
  5855. * ssl The SSL/TLS object.
  5856. * kse The key share entry object.
  5857. * returns 0 on success, otherwise failure.
  5858. */
  5859. static int TLSX_KeyShare_GenEccKey(WOLFSSL *ssl, KeyShareEntry* kse)
  5860. {
  5861. int ret = 0;
  5862. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT)
  5863. word32 keySize = 0;
  5864. word16 curveId = (word16) ECC_CURVE_INVALID;
  5865. ecc_key* eccKey = (ecc_key*)kse->key;
  5866. /* TODO: [TLS13] The key sizes should come from wolfcrypt. */
  5867. /* Translate named group to a curve id. */
  5868. switch (kse->group) {
  5869. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  5870. #ifndef NO_ECC_SECP
  5871. case WOLFSSL_ECC_SECP256R1:
  5872. curveId = ECC_SECP256R1;
  5873. keySize = 32;
  5874. break;
  5875. #endif /* !NO_ECC_SECP */
  5876. #endif
  5877. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  5878. #ifndef NO_ECC_SECP
  5879. case WOLFSSL_ECC_SECP384R1:
  5880. curveId = ECC_SECP384R1;
  5881. keySize = 48;
  5882. break;
  5883. #endif /* !NO_ECC_SECP */
  5884. #endif
  5885. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  5886. #ifndef NO_ECC_SECP
  5887. case WOLFSSL_ECC_SECP521R1:
  5888. curveId = ECC_SECP521R1;
  5889. keySize = 66;
  5890. break;
  5891. #endif /* !NO_ECC_SECP */
  5892. #endif
  5893. default:
  5894. WOLFSSL_ERROR_VERBOSE(BAD_FUNC_ARG);
  5895. return BAD_FUNC_ARG;
  5896. }
  5897. if (kse->key == NULL) {
  5898. kse->keyLen = keySize;
  5899. kse->pubKeyLen = keySize * 2 + 1;
  5900. #if defined(WOLFSSL_RENESAS_TSIP_TLS) && (WOLFSSL_RENESAS_TSIP_VER >= 115)
  5901. ret = tsip_Tls13GenEccKeyPair(ssl, kse);
  5902. if (ret != CRYPTOCB_UNAVAILABLE) {
  5903. return ret;
  5904. }
  5905. #endif
  5906. /* Allocate an ECC key to hold private key. */
  5907. kse->key = (byte*)XMALLOC(sizeof(ecc_key), ssl->heap, DYNAMIC_TYPE_ECC);
  5908. if (kse->key == NULL) {
  5909. WOLFSSL_MSG("EccTempKey Memory error");
  5910. return MEMORY_E;
  5911. }
  5912. /* Make an ECC key */
  5913. ret = wc_ecc_init_ex((ecc_key*)kse->key, ssl->heap, ssl->devId);
  5914. if (ret == 0) {
  5915. /* setting eccKey means okay to call wc_ecc_free */
  5916. eccKey = (ecc_key*)kse->key;
  5917. #ifdef WOLFSSL_STATIC_EPHEMERAL
  5918. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_ECDH, kse->key);
  5919. if (ret != 0)
  5920. #endif
  5921. {
  5922. /* set curve info for EccMakeKey "peer" info */
  5923. ret = wc_ecc_set_curve(eccKey, kse->keyLen, curveId);
  5924. if (ret == 0) {
  5925. /* Generate ephemeral ECC key */
  5926. /* For async this is called once and when event is done, the
  5927. * provided buffers in key be populated.
  5928. * Final processing is x963 key export below. */
  5929. ret = EccMakeKey(ssl, eccKey, eccKey);
  5930. }
  5931. #ifdef WOLFSSL_ASYNC_CRYPT
  5932. if (ret == WC_PENDING_E)
  5933. return ret;
  5934. #endif
  5935. }
  5936. }
  5937. }
  5938. if (ret == 0 && kse->pubKey == NULL) {
  5939. /* Allocate space for the public key */
  5940. kse->pubKey = (byte*)XMALLOC(kse->pubKeyLen, ssl->heap,
  5941. DYNAMIC_TYPE_PUBLIC_KEY);
  5942. if (kse->pubKey == NULL) {
  5943. WOLFSSL_MSG("Key data Memory error");
  5944. ret = MEMORY_E;
  5945. }
  5946. }
  5947. if (ret == 0) {
  5948. XMEMSET(kse->pubKey, 0, kse->pubKeyLen);
  5949. /* Export public key. */
  5950. PRIVATE_KEY_UNLOCK();
  5951. if (wc_ecc_export_x963(eccKey, kse->pubKey, &kse->pubKeyLen) != 0) {
  5952. ret = ECC_EXPORT_ERROR;
  5953. WOLFSSL_ERROR_VERBOSE(ret);
  5954. }
  5955. PRIVATE_KEY_LOCK();
  5956. }
  5957. #ifdef WOLFSSL_DEBUG_TLS
  5958. if (ret == 0) {
  5959. WOLFSSL_MSG("Public ECC Key");
  5960. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  5961. }
  5962. #endif
  5963. if (ret != 0) {
  5964. /* Cleanup on error, otherwise data owned by key share entry */
  5965. if (kse->pubKey != NULL) {
  5966. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5967. kse->pubKey = NULL;
  5968. }
  5969. if (eccKey != NULL)
  5970. wc_ecc_free(eccKey);
  5971. if (kse->key != NULL) {
  5972. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5973. kse->key = NULL;
  5974. }
  5975. }
  5976. #else
  5977. (void)ssl;
  5978. (void)kse;
  5979. ret = NOT_COMPILED_IN;
  5980. WOLFSSL_ERROR_VERBOSE(ret);
  5981. #endif /* HAVE_ECC && HAVE_ECC_KEY_EXPORT */
  5982. return ret;
  5983. }
  5984. #ifdef HAVE_PQC
  5985. static int kyber_id2type(int id, int *type)
  5986. {
  5987. int ret = 0;
  5988. switch (id) {
  5989. #ifdef WOLFSSL_KYBER512
  5990. case WOLFSSL_KYBER_LEVEL1:
  5991. *type = KYBER512;
  5992. break;
  5993. #endif
  5994. #ifdef WOLFSSL_KYBER768
  5995. case WOLFSSL_KYBER_LEVEL3:
  5996. *type = KYBER768;
  5997. break;
  5998. #endif
  5999. #ifdef WOLFSSL_KYBER1024
  6000. case WOLFSSL_KYBER_LEVEL5:
  6001. *type = KYBER1024;
  6002. break;
  6003. #endif
  6004. default:
  6005. ret = NOT_COMPILED_IN;
  6006. break;
  6007. }
  6008. return ret;
  6009. }
  6010. typedef struct PqcHybridMapping {
  6011. int hybrid;
  6012. int ecc;
  6013. int pqc;
  6014. } PqcHybridMapping;
  6015. static const PqcHybridMapping pqc_hybrid_mapping[] = {
  6016. {.hybrid = WOLFSSL_P256_KYBER_LEVEL1, .ecc = WOLFSSL_ECC_SECP256R1,
  6017. .pqc = WOLFSSL_KYBER_LEVEL1},
  6018. {.hybrid = WOLFSSL_P384_KYBER_LEVEL3, .ecc = WOLFSSL_ECC_SECP384R1,
  6019. .pqc = WOLFSSL_KYBER_LEVEL3},
  6020. {.hybrid = WOLFSSL_P521_KYBER_LEVEL5, .ecc = WOLFSSL_ECC_SECP521R1,
  6021. .pqc = WOLFSSL_KYBER_LEVEL5},
  6022. {.hybrid = 0, .ecc = 0, .pqc = 0}
  6023. };
  6024. /* This will map an ecc-pqs hybrid group into its ecc group and pqc kem group.
  6025. * If it cannot find a mapping then *pqc is set to group. ecc is optional. */
  6026. static void findEccPqc(int *ecc, int *pqc, int group)
  6027. {
  6028. int i;
  6029. if (pqc == NULL) {
  6030. return;
  6031. }
  6032. *pqc = 0;
  6033. if (ecc != NULL) {
  6034. *ecc = 0;
  6035. }
  6036. for (i = 0; pqc_hybrid_mapping[i].hybrid != 0; i++) {
  6037. if (pqc_hybrid_mapping[i].hybrid == group) {
  6038. *pqc = pqc_hybrid_mapping[i].pqc;
  6039. if (ecc != NULL) {
  6040. *ecc = pqc_hybrid_mapping[i].ecc;
  6041. }
  6042. break;
  6043. }
  6044. }
  6045. if (*pqc == 0) {
  6046. /* It is not a hybrid, so maybe its simple. */
  6047. *pqc = group;
  6048. }
  6049. }
  6050. /* Create a key share entry using liboqs parameters group.
  6051. * Generates a key pair.
  6052. *
  6053. * ssl The SSL/TLS object.
  6054. * kse The key share entry object.
  6055. * returns 0 on success, otherwise failure.
  6056. */
  6057. static int TLSX_KeyShare_GenPqcKey(WOLFSSL *ssl, KeyShareEntry* kse)
  6058. {
  6059. int ret = 0;
  6060. int type = 0;
  6061. KyberKey kem[1];
  6062. byte* pubKey = NULL;
  6063. byte* privKey = NULL;
  6064. KeyShareEntry *ecc_kse = NULL;
  6065. int oqs_group = 0;
  6066. int ecc_group = 0;
  6067. word32 privSz = 0;
  6068. word32 pubSz = 0;
  6069. findEccPqc(&ecc_group, &oqs_group, kse->group);
  6070. ret = kyber_id2type(oqs_group, &type);
  6071. if (ret == NOT_COMPILED_IN) {
  6072. WOLFSSL_MSG("Invalid Kyber algorithm specified.");
  6073. ret = BAD_FUNC_ARG;
  6074. }
  6075. if (ret == 0) {
  6076. ret = wc_KyberKey_Init(type, kem, ssl->heap, ssl->devId);
  6077. if (ret != 0) {
  6078. WOLFSSL_MSG("Failed to intialize Kyber Key.");
  6079. }
  6080. }
  6081. if (ret == 0) {
  6082. ecc_kse = (KeyShareEntry*)XMALLOC(sizeof(*ecc_kse), ssl->heap,
  6083. DYNAMIC_TYPE_TLSX);
  6084. if (ecc_kse == NULL) {
  6085. WOLFSSL_MSG("ecc_kse memory allocation failure");
  6086. ret = MEMORY_ERROR;
  6087. }
  6088. }
  6089. if (ret == 0) {
  6090. XMEMSET(ecc_kse, 0, sizeof(*ecc_kse));
  6091. ret = wc_KyberKey_PrivateKeySize(kem, &privSz);
  6092. }
  6093. if (ret == 0) {
  6094. ret = wc_KyberKey_PublicKeySize(kem, &pubSz);
  6095. }
  6096. if (ret == 0 && ecc_group != 0) {
  6097. ecc_kse->group = ecc_group;
  6098. ret = TLSX_KeyShare_GenEccKey(ssl, ecc_kse);
  6099. /* If fail, no error message, TLSX_KeyShare_GenEccKey will do it. */
  6100. }
  6101. if (ret == 0) {
  6102. pubKey = (byte*)XMALLOC(ecc_kse->pubKeyLen + pubSz, ssl->heap,
  6103. DYNAMIC_TYPE_PUBLIC_KEY);
  6104. if (pubKey == NULL) {
  6105. WOLFSSL_MSG("pubkey memory allocation failure");
  6106. ret = MEMORY_ERROR;
  6107. }
  6108. }
  6109. if (ret == 0) {
  6110. privKey = (byte*)XMALLOC(privSz, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6111. if (privKey == NULL) {
  6112. WOLFSSL_MSG("privkey memory allocation failure");
  6113. ret = MEMORY_ERROR;
  6114. }
  6115. }
  6116. if (ret == 0) {
  6117. ret = wc_KyberKey_MakeKey(kem, ssl->rng);
  6118. if (ret != 0) {
  6119. WOLFSSL_MSG("Kyber keygen failure");
  6120. }
  6121. }
  6122. if (ret == 0) {
  6123. ret = wc_KyberKey_EncodePublicKey(kem, pubKey + ecc_kse->pubKeyLen,
  6124. pubSz);
  6125. }
  6126. if (ret == 0) {
  6127. ret = wc_KyberKey_EncodePrivateKey(kem, privKey, privSz);
  6128. }
  6129. if (ret == 0) {
  6130. XMEMCPY(pubKey, ecc_kse->pubKey, ecc_kse->pubKeyLen);
  6131. kse->pubKey = pubKey;
  6132. kse->pubKeyLen = ecc_kse->pubKeyLen + pubSz;
  6133. pubKey = NULL;
  6134. /* Note we are saving the OQS private key and ECC private key
  6135. * separately. That's because the ECC private key is not simply a
  6136. * buffer. Its is an ecc_key struct.
  6137. */
  6138. kse->privKey = privKey;
  6139. privKey = NULL;
  6140. kse->key = ecc_kse->key;
  6141. ecc_kse->key = NULL;
  6142. }
  6143. #ifdef WOLFSSL_DEBUG_TLS
  6144. WOLFSSL_MSG("Public Kyber Key");
  6145. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen );
  6146. #endif
  6147. wc_KyberKey_Free(kem);
  6148. TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
  6149. if (pubKey != NULL)
  6150. XFREE(pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6151. if (privKey != NULL)
  6152. XFREE(privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6153. return ret;
  6154. }
  6155. #endif /* HAVE_PQC */
  6156. /* Generate a secret/key using the key share entry.
  6157. *
  6158. * ssl The SSL/TLS object.
  6159. * kse The key share entry holding peer data.
  6160. */
  6161. static int TLSX_KeyShare_GenKey(WOLFSSL *ssl, KeyShareEntry *kse)
  6162. {
  6163. int ret;
  6164. /* Named FFDHE groups have a bit set to identify them. */
  6165. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(kse->group))
  6166. ret = TLSX_KeyShare_GenDhKey(ssl, kse);
  6167. else if (kse->group == WOLFSSL_ECC_X25519)
  6168. ret = TLSX_KeyShare_GenX25519Key(ssl, kse);
  6169. else if (kse->group == WOLFSSL_ECC_X448)
  6170. ret = TLSX_KeyShare_GenX448Key(ssl, kse);
  6171. #ifdef HAVE_PQC
  6172. else if (WOLFSSL_NAMED_GROUP_IS_PQC(kse->group))
  6173. ret = TLSX_KeyShare_GenPqcKey(ssl, kse);
  6174. #endif
  6175. else
  6176. ret = TLSX_KeyShare_GenEccKey(ssl, kse);
  6177. #ifdef WOLFSSL_ASYNC_CRYPT
  6178. kse->lastRet = ret;
  6179. #endif
  6180. return ret;
  6181. }
  6182. /* Free the key share dynamic data.
  6183. *
  6184. * list The linked list of key share entry objects.
  6185. * heap The heap used for allocation.
  6186. */
  6187. static void TLSX_KeyShare_FreeAll(KeyShareEntry* list, void* heap)
  6188. {
  6189. KeyShareEntry* current;
  6190. while ((current = list) != NULL) {
  6191. list = current->next;
  6192. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(current->group)) {
  6193. #ifndef NO_DH
  6194. wc_FreeDhKey((DhKey*)current->key);
  6195. #endif
  6196. }
  6197. else if (current->group == WOLFSSL_ECC_X25519) {
  6198. #ifdef HAVE_CURVE25519
  6199. wc_curve25519_free((curve25519_key*)current->key);
  6200. #endif
  6201. }
  6202. else if (current->group == WOLFSSL_ECC_X448) {
  6203. #ifdef HAVE_CURVE448
  6204. wc_curve448_free((curve448_key*)current->key);
  6205. #endif
  6206. }
  6207. #ifdef HAVE_PQC
  6208. else if (WOLFSSL_NAMED_GROUP_IS_PQC(current->group) &&
  6209. current->key != NULL) {
  6210. ForceZero((byte*)current->key, current->keyLen);
  6211. }
  6212. #endif
  6213. else {
  6214. #ifdef HAVE_ECC
  6215. wc_ecc_free((ecc_key*)current->key);
  6216. #endif
  6217. }
  6218. XFREE(current->key, heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6219. #if !defined(NO_DH) && (!defined(NO_CERTS) || !defined(NO_PSK))
  6220. XFREE(current->privKey, heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6221. #endif
  6222. XFREE(current->pubKey, heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6223. XFREE(current->ke, heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6224. XFREE(current, heap, DYNAMIC_TYPE_TLSX);
  6225. }
  6226. (void)heap;
  6227. }
  6228. /* Get the size of the encoded key share extension.
  6229. *
  6230. * list The linked list of key share extensions.
  6231. * msgType The type of the message this extension is being written into.
  6232. * returns the number of bytes of the encoded key share extension.
  6233. */
  6234. static word16 TLSX_KeyShare_GetSize(KeyShareEntry* list, byte msgType)
  6235. {
  6236. word16 len = 0;
  6237. byte isRequest = (msgType == client_hello);
  6238. KeyShareEntry* current;
  6239. /* The named group the server wants to use. */
  6240. if (msgType == hello_retry_request)
  6241. return OPAQUE16_LEN;
  6242. /* List of key exchange groups. */
  6243. if (isRequest)
  6244. len += OPAQUE16_LEN;
  6245. while ((current = list) != NULL) {
  6246. list = current->next;
  6247. if (!isRequest && current->pubKey == NULL)
  6248. continue;
  6249. len += (word16)(KE_GROUP_LEN + OPAQUE16_LEN + current->pubKeyLen);
  6250. }
  6251. return len;
  6252. }
  6253. /* Writes the key share extension into the output buffer.
  6254. * Assumes that the the output buffer is big enough to hold data.
  6255. *
  6256. * list The linked list of key share entries.
  6257. * output The buffer to write into.
  6258. * msgType The type of the message this extension is being written into.
  6259. * returns the number of bytes written into the buffer.
  6260. */
  6261. static word16 TLSX_KeyShare_Write(KeyShareEntry* list, byte* output,
  6262. byte msgType)
  6263. {
  6264. word16 i = 0;
  6265. byte isRequest = (msgType == client_hello);
  6266. KeyShareEntry* current;
  6267. if (msgType == hello_retry_request) {
  6268. c16toa(list->group, output);
  6269. return OPAQUE16_LEN;
  6270. }
  6271. /* ClientHello has a list but ServerHello is only the chosen. */
  6272. if (isRequest)
  6273. i += OPAQUE16_LEN;
  6274. /* Write out all in the list. */
  6275. while ((current = list) != NULL) {
  6276. list = current->next;
  6277. if (!isRequest && current->pubKey == NULL)
  6278. continue;
  6279. c16toa(current->group, &output[i]);
  6280. i += KE_GROUP_LEN;
  6281. c16toa((word16)(current->pubKeyLen), &output[i]);
  6282. i += OPAQUE16_LEN;
  6283. XMEMCPY(&output[i], current->pubKey, current->pubKeyLen);
  6284. i += (word16)current->pubKeyLen;
  6285. }
  6286. /* Write the length of the list if required. */
  6287. if (isRequest)
  6288. c16toa(i - OPAQUE16_LEN, output);
  6289. return i;
  6290. }
  6291. /* Process the DH key share extension on the client side.
  6292. *
  6293. * ssl The SSL/TLS object.
  6294. * keyShareEntry The key share entry object to use to calculate shared secret.
  6295. * returns 0 on success and other values indicate failure.
  6296. */
  6297. static int TLSX_KeyShare_ProcessDh(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6298. {
  6299. int ret = 0;
  6300. #if !defined(NO_DH) && (!defined(NO_CERTS) || !defined(NO_PSK))
  6301. word32 pSz = 0;
  6302. DhKey* dhKey = (DhKey*)keyShareEntry->key;
  6303. #ifdef HAVE_PUBLIC_FFDHE
  6304. const DhParams* params = NULL;
  6305. switch (keyShareEntry->group) {
  6306. #ifdef HAVE_FFDHE_2048
  6307. case WOLFSSL_FFDHE_2048:
  6308. params = wc_Dh_ffdhe2048_Get();
  6309. break;
  6310. #endif
  6311. #ifdef HAVE_FFDHE_3072
  6312. case WOLFSSL_FFDHE_3072:
  6313. params = wc_Dh_ffdhe3072_Get();
  6314. break;
  6315. #endif
  6316. #ifdef HAVE_FFDHE_4096
  6317. case WOLFSSL_FFDHE_4096:
  6318. params = wc_Dh_ffdhe4096_Get();
  6319. break;
  6320. #endif
  6321. #ifdef HAVE_FFDHE_6144
  6322. case WOLFSSL_FFDHE_6144:
  6323. params = wc_Dh_ffdhe6144_Get();
  6324. break;
  6325. #endif
  6326. #ifdef HAVE_FFDHE_8192
  6327. case WOLFSSL_FFDHE_8192:
  6328. params = wc_Dh_ffdhe8192_Get();
  6329. break;
  6330. #endif
  6331. default:
  6332. break;
  6333. }
  6334. if (params == NULL) {
  6335. WOLFSSL_ERROR_VERBOSE(PEER_KEY_ERROR);
  6336. return PEER_KEY_ERROR;
  6337. }
  6338. pSz = params->p_len;
  6339. #else
  6340. ret = wc_DhGetNamedKeyParamSize(keyShareEntry->group, &pSz, NULL, NULL);
  6341. if (ret != 0 || pSz == 0) {
  6342. WOLFSSL_ERROR_VERBOSE(PEER_KEY_ERROR);
  6343. return PEER_KEY_ERROR;
  6344. }
  6345. #endif
  6346. /* if DhKey is not setup, do it now */
  6347. if (keyShareEntry->key == NULL) {
  6348. keyShareEntry->key = (DhKey*)XMALLOC(sizeof(DhKey), ssl->heap,
  6349. DYNAMIC_TYPE_DH);
  6350. if (keyShareEntry->key == NULL)
  6351. return MEMORY_E;
  6352. /* Setup Key */
  6353. ret = wc_InitDhKey_ex((DhKey*)keyShareEntry->key, ssl->heap, ssl->devId);
  6354. if (ret == 0) {
  6355. dhKey = (DhKey*)keyShareEntry->key;
  6356. /* Set key */
  6357. #ifdef HAVE_PUBLIC_FFDHE
  6358. ret = wc_DhSetKey(dhKey, params->p, params->p_len, params->g,
  6359. params->g_len);
  6360. #else
  6361. ret = wc_DhSetNamedKey(dhKey, keyShareEntry->group);
  6362. #endif
  6363. }
  6364. }
  6365. if (ret == 0
  6366. #ifdef WOLFSSL_ASYNC_CRYPT
  6367. && keyShareEntry->lastRet == 0 /* don't enter here if WC_PENDING_E */
  6368. #endif
  6369. ) {
  6370. #ifdef WOLFSSL_DEBUG_TLS
  6371. WOLFSSL_MSG("Peer DH Key");
  6372. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6373. #endif
  6374. ssl->options.dhKeySz = (word16)pSz;
  6375. /* Derive secret from private key and peer's public key. */
  6376. ret = DhAgree(ssl, dhKey,
  6377. (const byte*)keyShareEntry->privKey, keyShareEntry->keyLen, /* our private */
  6378. keyShareEntry->ke, keyShareEntry->keLen, /* peer's public key */
  6379. ssl->arrays->preMasterSecret, &ssl->arrays->preMasterSz, /* secret */
  6380. NULL, 0
  6381. );
  6382. #ifdef WOLFSSL_ASYNC_CRYPT
  6383. if (ret == WC_PENDING_E) {
  6384. return ret;
  6385. }
  6386. #endif
  6387. }
  6388. /* RFC 8446 Section 7.4.1:
  6389. * ... left-padded with zeros up to the size of the prime. ...
  6390. */
  6391. if (ret == 0 && (word32)ssl->options.dhKeySz > ssl->arrays->preMasterSz) {
  6392. word32 diff = (word32)ssl->options.dhKeySz - ssl->arrays->preMasterSz;
  6393. XMEMMOVE(ssl->arrays->preMasterSecret + diff,
  6394. ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz);
  6395. XMEMSET(ssl->arrays->preMasterSecret, 0, diff);
  6396. ssl->arrays->preMasterSz = ssl->options.dhKeySz;
  6397. }
  6398. /* done with key share, release resources */
  6399. if (dhKey)
  6400. wc_FreeDhKey(dhKey);
  6401. if (keyShareEntry->key) {
  6402. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_DH);
  6403. keyShareEntry->key = NULL;
  6404. }
  6405. if (keyShareEntry->privKey != NULL) {
  6406. XFREE(keyShareEntry->privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6407. keyShareEntry->privKey = NULL;
  6408. }
  6409. if (keyShareEntry->pubKey != NULL) {
  6410. XFREE(keyShareEntry->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6411. keyShareEntry->pubKey = NULL;
  6412. }
  6413. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6414. keyShareEntry->ke = NULL;
  6415. #else
  6416. (void)ssl;
  6417. (void)keyShareEntry;
  6418. ret = PEER_KEY_ERROR;
  6419. WOLFSSL_ERROR_VERBOSE(ret);
  6420. #endif
  6421. return ret;
  6422. }
  6423. /* Process the X25519 key share extension on the client side.
  6424. *
  6425. * ssl The SSL/TLS object.
  6426. * keyShareEntry The key share entry object to use to calculate shared secret.
  6427. * returns 0 on success and other values indicate failure.
  6428. */
  6429. static int TLSX_KeyShare_ProcessX25519(WOLFSSL* ssl,
  6430. KeyShareEntry* keyShareEntry)
  6431. {
  6432. int ret;
  6433. #ifdef HAVE_CURVE25519
  6434. curve25519_key* key = (curve25519_key*)keyShareEntry->key;
  6435. curve25519_key* peerX25519Key;
  6436. #ifdef HAVE_ECC
  6437. if (ssl->peerEccKey != NULL) {
  6438. wc_ecc_free(ssl->peerEccKey);
  6439. ssl->peerEccKey = NULL;
  6440. ssl->peerEccKeyPresent = 0;
  6441. }
  6442. #endif
  6443. peerX25519Key = (curve25519_key*)XMALLOC(sizeof(curve25519_key), ssl->heap,
  6444. DYNAMIC_TYPE_TLSX);
  6445. if (peerX25519Key == NULL) {
  6446. WOLFSSL_MSG("PeerEccKey Memory error");
  6447. return MEMORY_ERROR;
  6448. }
  6449. ret = wc_curve25519_init(peerX25519Key);
  6450. if (ret != 0) {
  6451. XFREE(peerX25519Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6452. return ret;
  6453. }
  6454. #ifdef WOLFSSL_DEBUG_TLS
  6455. WOLFSSL_MSG("Peer Curve25519 Key");
  6456. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6457. #endif
  6458. if (wc_curve25519_check_public(keyShareEntry->ke, keyShareEntry->keLen,
  6459. EC25519_LITTLE_ENDIAN) != 0) {
  6460. ret = ECC_PEERKEY_ERROR;
  6461. WOLFSSL_ERROR_VERBOSE(ret);
  6462. }
  6463. if (ret == 0) {
  6464. if (wc_curve25519_import_public_ex(keyShareEntry->ke,
  6465. keyShareEntry->keLen, peerX25519Key,
  6466. EC25519_LITTLE_ENDIAN) != 0) {
  6467. ret = ECC_PEERKEY_ERROR;
  6468. WOLFSSL_ERROR_VERBOSE(ret);
  6469. }
  6470. }
  6471. if (ret == 0) {
  6472. ssl->ecdhCurveOID = ECC_X25519_OID;
  6473. ret = wc_curve25519_shared_secret_ex(key, peerX25519Key,
  6474. ssl->arrays->preMasterSecret,
  6475. &ssl->arrays->preMasterSz,
  6476. EC25519_LITTLE_ENDIAN);
  6477. }
  6478. wc_curve25519_free(peerX25519Key);
  6479. XFREE(peerX25519Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6480. wc_curve25519_free((curve25519_key*)keyShareEntry->key);
  6481. if (keyShareEntry->key != NULL) {
  6482. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6483. keyShareEntry->key = NULL;
  6484. }
  6485. #else
  6486. (void)ssl;
  6487. (void)keyShareEntry;
  6488. ret = PEER_KEY_ERROR;
  6489. WOLFSSL_ERROR_VERBOSE(ret);
  6490. #endif /* HAVE_CURVE25519 */
  6491. return ret;
  6492. }
  6493. /* Process the X448 key share extension on the client side.
  6494. *
  6495. * ssl The SSL/TLS object.
  6496. * keyShareEntry The key share entry object to use to calculate shared secret.
  6497. * returns 0 on success and other values indicate failure.
  6498. */
  6499. static int TLSX_KeyShare_ProcessX448(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6500. {
  6501. int ret;
  6502. #ifdef HAVE_CURVE448
  6503. curve448_key* key = (curve448_key*)keyShareEntry->key;
  6504. curve448_key* peerX448Key;
  6505. #ifdef HAVE_ECC
  6506. if (ssl->peerEccKey != NULL) {
  6507. wc_ecc_free(ssl->peerEccKey);
  6508. ssl->peerEccKey = NULL;
  6509. ssl->peerEccKeyPresent = 0;
  6510. }
  6511. #endif
  6512. peerX448Key = (curve448_key*)XMALLOC(sizeof(curve448_key), ssl->heap,
  6513. DYNAMIC_TYPE_TLSX);
  6514. if (peerX448Key == NULL) {
  6515. WOLFSSL_MSG("PeerEccKey Memory error");
  6516. return MEMORY_ERROR;
  6517. }
  6518. ret = wc_curve448_init(peerX448Key);
  6519. if (ret != 0) {
  6520. XFREE(peerX448Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6521. return ret;
  6522. }
  6523. #ifdef WOLFSSL_DEBUG_TLS
  6524. WOLFSSL_MSG("Peer Curve448 Key");
  6525. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6526. #endif
  6527. if (wc_curve448_check_public(keyShareEntry->ke, keyShareEntry->keLen,
  6528. EC448_LITTLE_ENDIAN) != 0) {
  6529. ret = ECC_PEERKEY_ERROR;
  6530. WOLFSSL_ERROR_VERBOSE(ret);
  6531. }
  6532. if (ret == 0) {
  6533. if (wc_curve448_import_public_ex(keyShareEntry->ke,
  6534. keyShareEntry->keLen, peerX448Key,
  6535. EC448_LITTLE_ENDIAN) != 0) {
  6536. ret = ECC_PEERKEY_ERROR;
  6537. WOLFSSL_ERROR_VERBOSE(ret);
  6538. }
  6539. }
  6540. if (ret == 0) {
  6541. ssl->ecdhCurveOID = ECC_X448_OID;
  6542. ret = wc_curve448_shared_secret_ex(key, peerX448Key,
  6543. ssl->arrays->preMasterSecret,
  6544. &ssl->arrays->preMasterSz,
  6545. EC448_LITTLE_ENDIAN);
  6546. }
  6547. wc_curve448_free(peerX448Key);
  6548. XFREE(peerX448Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6549. wc_curve448_free((curve448_key*)keyShareEntry->key);
  6550. if (keyShareEntry->key != NULL) {
  6551. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6552. keyShareEntry->key = NULL;
  6553. }
  6554. #else
  6555. (void)ssl;
  6556. (void)keyShareEntry;
  6557. ret = PEER_KEY_ERROR;
  6558. WOLFSSL_ERROR_VERBOSE(ret);
  6559. #endif /* HAVE_CURVE448 */
  6560. return ret;
  6561. }
  6562. /* Process the ECC key share extension on the client side.
  6563. *
  6564. * ssl The SSL/TLS object.
  6565. * keyShareEntry The key share entry object to use to calculate shared secret.
  6566. * returns 0 on success and other values indicate failure.
  6567. */
  6568. static int TLSX_KeyShare_ProcessEcc(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6569. {
  6570. int ret = 0;
  6571. #ifdef HAVE_ECC
  6572. int curveId = ECC_CURVE_INVALID;
  6573. ecc_key* eccKey = (ecc_key*)keyShareEntry->key;
  6574. /* find supported curve */
  6575. switch (keyShareEntry->group) {
  6576. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  6577. #ifndef NO_ECC_SECP
  6578. case WOLFSSL_ECC_SECP256R1:
  6579. curveId = ECC_SECP256R1;
  6580. break;
  6581. #endif /* !NO_ECC_SECP */
  6582. #endif
  6583. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  6584. #ifndef NO_ECC_SECP
  6585. case WOLFSSL_ECC_SECP384R1:
  6586. curveId = ECC_SECP384R1;
  6587. break;
  6588. #endif /* !NO_ECC_SECP */
  6589. #endif
  6590. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  6591. #ifndef NO_ECC_SECP
  6592. case WOLFSSL_ECC_SECP521R1:
  6593. curveId = ECC_SECP521R1;
  6594. break;
  6595. #endif /* !NO_ECC_SECP */
  6596. #endif
  6597. #if defined(HAVE_X448) && ECC_MIN_KEY_SZ <= 448
  6598. case WOLFSSL_ECC_X448:
  6599. curveId = ECC_X448;
  6600. break;
  6601. #endif
  6602. default:
  6603. /* unsupported curve */
  6604. WOLFSSL_ERROR_VERBOSE(ECC_PEERKEY_ERROR);
  6605. return ECC_PEERKEY_ERROR;
  6606. }
  6607. #ifdef WOLFSSL_ASYNC_CRYPT
  6608. if (keyShareEntry->lastRet == 0) /* don't enter here if WC_PENDING_E */
  6609. #endif
  6610. {
  6611. #ifdef WOLFSSL_DEBUG_TLS
  6612. WOLFSSL_MSG("Peer ECC Key");
  6613. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6614. #endif
  6615. if (ssl->peerEccKey != NULL) {
  6616. wc_ecc_free(ssl->peerEccKey);
  6617. XFREE(ssl->peerEccKey, ssl->heap, DYNAMIC_TYPE_ECC);
  6618. ssl->peerEccKeyPresent = 0;
  6619. }
  6620. #if defined(WOLFSSL_RENESAS_TSIP_TLS) && (WOLFSSL_RENESAS_TSIP_VER >= 115)
  6621. ret = tsip_Tls13GenSharedSecret(ssl, keyShareEntry);
  6622. if (ret != CRYPTOCB_UNAVAILABLE) {
  6623. return ret;
  6624. }
  6625. ret = 0;
  6626. #endif
  6627. ssl->peerEccKey = (ecc_key*)XMALLOC(sizeof(ecc_key), ssl->heap,
  6628. DYNAMIC_TYPE_ECC);
  6629. if (ssl->peerEccKey == NULL) {
  6630. WOLFSSL_MSG("PeerEccKey Memory error");
  6631. ret = MEMORY_ERROR;
  6632. }
  6633. if (ret == 0) {
  6634. ret = wc_ecc_init_ex(ssl->peerEccKey, ssl->heap, ssl->devId);
  6635. }
  6636. /* Point is validated by import function. */
  6637. if (ret == 0) {
  6638. ret = wc_ecc_import_x963_ex(keyShareEntry->ke, keyShareEntry->keLen,
  6639. ssl->peerEccKey, curveId);
  6640. if (ret != 0) {
  6641. ret = ECC_PEERKEY_ERROR;
  6642. WOLFSSL_ERROR_VERBOSE(ret);
  6643. }
  6644. }
  6645. if (ret == 0) {
  6646. ssl->ecdhCurveOID = ssl->peerEccKey->dp->oidSum;
  6647. ssl->peerEccKeyPresent = 1;
  6648. }
  6649. }
  6650. if (ret == 0 && eccKey == NULL)
  6651. ret = BAD_FUNC_ARG;
  6652. if (ret == 0) {
  6653. ret = EccSharedSecret(ssl, eccKey, ssl->peerEccKey,
  6654. keyShareEntry->ke, &keyShareEntry->keLen,
  6655. ssl->arrays->preMasterSecret, &ssl->arrays->preMasterSz,
  6656. ssl->options.side
  6657. );
  6658. #ifdef WOLFSSL_ASYNC_CRYPT
  6659. if (ret == WC_PENDING_E)
  6660. return ret;
  6661. #endif
  6662. }
  6663. /* done with key share, release resources */
  6664. if (ssl->peerEccKey != NULL
  6665. #ifdef HAVE_PK_CALLBACKS
  6666. && ssl->ctx->EccSharedSecretCb == NULL
  6667. #endif
  6668. ) {
  6669. wc_ecc_free(ssl->peerEccKey);
  6670. XFREE(ssl->peerEccKey, ssl->heap, DYNAMIC_TYPE_ECC);
  6671. ssl->peerEccKey = NULL;
  6672. ssl->peerEccKeyPresent = 0;
  6673. }
  6674. if (keyShareEntry->key) {
  6675. wc_ecc_free((ecc_key*)keyShareEntry->key);
  6676. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_ECC);
  6677. keyShareEntry->key = NULL;
  6678. }
  6679. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6680. keyShareEntry->ke = NULL;
  6681. #else
  6682. (void)ssl;
  6683. (void)keyShareEntry;
  6684. ret = PEER_KEY_ERROR;
  6685. WOLFSSL_ERROR_VERBOSE(ret);
  6686. #endif /* HAVE_ECC */
  6687. return ret;
  6688. }
  6689. #ifdef HAVE_PQC
  6690. /* Process the Kyber key share extension on the client side.
  6691. *
  6692. * ssl The SSL/TLS object.
  6693. * keyShareEntry The key share entry object to use to calculate shared secret.
  6694. * returns 0 on success and other values indicate failure.
  6695. */
  6696. static int TLSX_KeyShare_ProcessPqc(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6697. {
  6698. int ret = 0;
  6699. int type;
  6700. KyberKey kem[1];
  6701. byte* sharedSecret = NULL;
  6702. word32 sharedSecretLen = 0;
  6703. int oqs_group = 0;
  6704. int ecc_group = 0;
  6705. ecc_key eccpubkey;
  6706. word32 outlen = 0;
  6707. word32 privSz = 0;
  6708. word32 ctSz = 0;
  6709. word32 ssSz = 0;
  6710. if (keyShareEntry->ke == NULL) {
  6711. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  6712. return BAD_FUNC_ARG;
  6713. }
  6714. if (ssl->options.side == WOLFSSL_SERVER_END) {
  6715. /* I am the server, the shared secret has already been generated and
  6716. * is in keyShareEntry->ke; copy it to the pre-master secret
  6717. * pre-allocated buffer. */
  6718. if (keyShareEntry->keLen > ENCRYPT_LEN) {
  6719. WOLFSSL_MSG("shared secret is too long.");
  6720. return LENGTH_ERROR;
  6721. }
  6722. XMEMCPY(ssl->arrays->preMasterSecret, keyShareEntry->ke,
  6723. keyShareEntry->keLen);
  6724. ssl->arrays->preMasterSz = keyShareEntry->keLen;
  6725. XFREE(keyShareEntry->ke, sl->heap, DYNAMIC_TYPE_SECRET)
  6726. keyShareEntry->ke = NULL;
  6727. keyShareEntry->keLen = 0;
  6728. return 0;
  6729. }
  6730. /* I am the client, the ciphertext is in keyShareEntry->ke */
  6731. findEccPqc(&ecc_group, &oqs_group, keyShareEntry->group);
  6732. ret = kyber_id2type(oqs_group, &type);
  6733. if (ret != 0) {
  6734. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  6735. ret = BAD_FUNC_ARG;
  6736. }
  6737. if (ret == 0) {
  6738. ret = wc_KyberKey_Init(type, kem, ssl->heap, INVALID_DEVID);
  6739. if (ret != 0) {
  6740. WOLFSSL_MSG("Error creating Kyber KEM");
  6741. }
  6742. }
  6743. if (ret == 0) {
  6744. ret = wc_KyberKey_SharedSecretSize(kem, &ssSz);
  6745. }
  6746. if (ret == 0) {
  6747. sharedSecretLen = ssSz;
  6748. switch (ecc_group) {
  6749. case WOLFSSL_ECC_SECP256R1:
  6750. sharedSecretLen += 32;
  6751. outlen = 32;
  6752. break;
  6753. case WOLFSSL_ECC_SECP384R1:
  6754. sharedSecretLen += 48;
  6755. outlen = 48;
  6756. break;
  6757. case WOLFSSL_ECC_SECP521R1:
  6758. sharedSecretLen += 66;
  6759. outlen = 66;
  6760. break;
  6761. default:
  6762. break;
  6763. }
  6764. ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
  6765. if (ret != 0) {
  6766. WOLFSSL_MSG("Memory allocation error.");
  6767. ret = MEMORY_E;
  6768. }
  6769. }
  6770. if (ret == 0) {
  6771. sharedSecret = (byte*)XMALLOC(sharedSecretLen, ssl->heap,
  6772. DYNAMIC_TYPE_TLSX);
  6773. if (sharedSecret == NULL) {
  6774. WOLFSSL_MSG("Memory allocation error.");
  6775. ret = MEMORY_E;
  6776. }
  6777. }
  6778. if (ret == 0) {
  6779. ret = wc_KyberKey_CipherTextSize(kem, &ctSz);
  6780. }
  6781. if (ret == 0) {
  6782. ret = wc_KyberKey_PrivateKeySize(kem, &privSz);
  6783. }
  6784. if (ret == 0) {
  6785. ret = wc_KyberKey_DecodePrivateKey(kem, keyShareEntry->privKey, privSz);
  6786. }
  6787. if (ret == 0) {
  6788. ret = wc_KyberKey_Decapsulate(kem, sharedSecret + outlen,
  6789. keyShareEntry->ke + keyShareEntry->keLen - ctSz, ctSz);
  6790. if (ret != 0) {
  6791. WOLFSSL_MSG("wc_KyberKey decapsulation failure.");
  6792. ret = BAD_FUNC_ARG;
  6793. }
  6794. }
  6795. if (ecc_group != 0) {
  6796. if (ret == 0) {
  6797. /* Point is validated by import function. */
  6798. ret = wc_ecc_import_x963(keyShareEntry->ke,
  6799. keyShareEntry->keLen - ctSz,
  6800. &eccpubkey);
  6801. if (ret != 0) {
  6802. WOLFSSL_MSG("ECC Public key import error.");
  6803. }
  6804. }
  6805. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  6806. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  6807. !defined(HAVE_SELFTEST)
  6808. if (ret == 0) {
  6809. ret = wc_ecc_set_rng(keyShareEntry->key, ssl->rng);
  6810. if (ret != 0) {
  6811. WOLFSSL_MSG("Failure to set the ECC private key RNG.");
  6812. }
  6813. }
  6814. #endif
  6815. if (ret == 0) {
  6816. PRIVATE_KEY_UNLOCK();
  6817. ret = wc_ecc_shared_secret(keyShareEntry->key, &eccpubkey,
  6818. sharedSecret, &outlen);
  6819. PRIVATE_KEY_LOCK();
  6820. if (outlen != sharedSecretLen - ssSz) {
  6821. WOLFSSL_MSG("ECC shared secret derivation error.");
  6822. ret = BAD_FUNC_ARG;
  6823. }
  6824. }
  6825. }
  6826. if ((ret == 0) && (sharedSecretLen > ENCRYPT_LEN)) {
  6827. WOLFSSL_MSG("shared secret is too long.");
  6828. ret = LENGTH_ERROR;
  6829. }
  6830. if (ret == 0) {
  6831. /* Copy the shared secret to the pre-master secret pre-allocated
  6832. * buffer. */
  6833. XMEMCPY(ssl->arrays->preMasterSecret, sharedSecret, sharedSecretLen);
  6834. ssl->arrays->preMasterSz = (word32) sharedSecretLen;
  6835. }
  6836. if (sharedSecret != NULL) {
  6837. XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_SECRET);
  6838. }
  6839. wc_ecc_free(&eccpubkey);
  6840. wc_KyberKey_Free(kem);
  6841. return ret;
  6842. }
  6843. #endif /* HAVE_PQC */
  6844. /* Process the key share extension on the client side.
  6845. *
  6846. * ssl The SSL/TLS object.
  6847. * keyShareEntry The key share entry object to use to calculate shared secret.
  6848. * returns 0 on success and other values indicate failure.
  6849. */
  6850. static int TLSX_KeyShare_Process(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6851. {
  6852. int ret;
  6853. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  6854. ssl->session->namedGroup = keyShareEntry->group;
  6855. #endif
  6856. /* reset the pre master secret size */
  6857. if (ssl->arrays->preMasterSz == 0)
  6858. ssl->arrays->preMasterSz = ENCRYPT_LEN;
  6859. /* Use Key Share Data from server. */
  6860. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(keyShareEntry->group))
  6861. ret = TLSX_KeyShare_ProcessDh(ssl, keyShareEntry);
  6862. else if (keyShareEntry->group == WOLFSSL_ECC_X25519)
  6863. ret = TLSX_KeyShare_ProcessX25519(ssl, keyShareEntry);
  6864. else if (keyShareEntry->group == WOLFSSL_ECC_X448)
  6865. ret = TLSX_KeyShare_ProcessX448(ssl, keyShareEntry);
  6866. #ifdef HAVE_PQC
  6867. else if (WOLFSSL_NAMED_GROUP_IS_PQC(keyShareEntry->group))
  6868. ret = TLSX_KeyShare_ProcessPqc(ssl, keyShareEntry);
  6869. #endif
  6870. else
  6871. ret = TLSX_KeyShare_ProcessEcc(ssl, keyShareEntry);
  6872. #ifdef WOLFSSL_DEBUG_TLS
  6873. if (ret == 0) {
  6874. WOLFSSL_MSG("KE Secret");
  6875. WOLFSSL_BUFFER(ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz);
  6876. }
  6877. #endif
  6878. #ifdef WOLFSSL_ASYNC_CRYPT
  6879. keyShareEntry->lastRet = ret;
  6880. #endif
  6881. return ret;
  6882. }
  6883. /* Parse an entry of the KeyShare extension.
  6884. *
  6885. * ssl The SSL/TLS object.
  6886. * input The extension data.
  6887. * length The length of the extension data.
  6888. * kse The new key share entry object.
  6889. * returns a positive number to indicate amount of data parsed and a negative
  6890. * number on error.
  6891. */
  6892. static int TLSX_KeyShareEntry_Parse(WOLFSSL* ssl, const byte* input,
  6893. word16 length, KeyShareEntry **kse)
  6894. {
  6895. int ret;
  6896. word16 group;
  6897. word16 keLen;
  6898. int offset = 0;
  6899. byte* ke;
  6900. if (length < OPAQUE16_LEN + OPAQUE16_LEN)
  6901. return BUFFER_ERROR;
  6902. /* Named group */
  6903. ato16(&input[offset], &group);
  6904. offset += OPAQUE16_LEN;
  6905. /* Key exchange data - public key. */
  6906. ato16(&input[offset], &keLen);
  6907. offset += OPAQUE16_LEN;
  6908. if (keLen == 0)
  6909. return INVALID_PARAMETER;
  6910. if (keLen > length - offset)
  6911. return BUFFER_ERROR;
  6912. #ifdef HAVE_PQC
  6913. if (WOLFSSL_NAMED_GROUP_IS_PQC(group) &&
  6914. ssl->options.side == WOLFSSL_SERVER_END) {
  6915. /* For KEMs, the public key is not stored. Casting away const because
  6916. * we know for KEMs, it will be read-only.*/
  6917. ke = (byte *)&input[offset];
  6918. } else
  6919. #endif
  6920. {
  6921. /* Store a copy in the key share object. */
  6922. ke = (byte*)XMALLOC(keLen, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6923. if (ke == NULL)
  6924. return MEMORY_E;
  6925. XMEMCPY(ke, &input[offset], keLen);
  6926. }
  6927. /* Populate a key share object in the extension. */
  6928. ret = TLSX_KeyShare_Use(ssl, group, keLen, ke, kse);
  6929. if (ret != 0) {
  6930. if (ke != &input[offset]) {
  6931. XFREE(ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6932. }
  6933. return ret;
  6934. }
  6935. /* Total length of the parsed data. */
  6936. return offset + keLen;
  6937. }
  6938. /* Searches the groups sent for the specified named group.
  6939. *
  6940. * ssl SSL/TLS object.
  6941. * name Group name to match.
  6942. * returns 1 when the extension has the group name and 0 otherwise.
  6943. */
  6944. static int TLSX_KeyShare_Find(WOLFSSL* ssl, word16 group)
  6945. {
  6946. TLSX* extension;
  6947. KeyShareEntry* list;
  6948. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  6949. if (extension == NULL) {
  6950. extension = TLSX_Find(ssl->ctx->extensions, TLSX_KEY_SHARE);
  6951. if (extension == NULL)
  6952. return 0;
  6953. }
  6954. list = (KeyShareEntry*)extension->data;
  6955. while (list != NULL) {
  6956. if (list->group == group)
  6957. return 1;
  6958. list = list->next;
  6959. }
  6960. return 0;
  6961. }
  6962. /* Searches the supported groups extension for the specified named group.
  6963. *
  6964. * ssl The SSL/TLS object.
  6965. * name The group name to match.
  6966. * returns 1 when the extension has the group name and 0 otherwise.
  6967. */
  6968. static int TLSX_SupportedGroups_Find(WOLFSSL* ssl, word16 name)
  6969. {
  6970. #ifdef HAVE_SUPPORTED_CURVES
  6971. TLSX* extension;
  6972. SupportedCurve* curve = NULL;
  6973. if ((extension = TLSX_Find(ssl->extensions,
  6974. TLSX_SUPPORTED_GROUPS)) == NULL) {
  6975. if ((extension = TLSX_Find(ssl->ctx->extensions,
  6976. TLSX_SUPPORTED_GROUPS)) == NULL) {
  6977. return 0;
  6978. }
  6979. }
  6980. for (curve = (SupportedCurve*)extension->data; curve; curve = curve->next) {
  6981. if (curve->name == name)
  6982. return 1;
  6983. }
  6984. #endif
  6985. (void)ssl;
  6986. (void)name;
  6987. return 0;
  6988. }
  6989. /* Parse the KeyShare extension.
  6990. * Different formats in different messages.
  6991. *
  6992. * ssl The SSL/TLS object.
  6993. * input The extension data.
  6994. * length The length of the extension data.
  6995. * msgType The type of the message this extension is being parsed from.
  6996. * returns 0 on success and other values indicate failure.
  6997. */
  6998. static int TLSX_KeyShare_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  6999. byte msgType)
  7000. {
  7001. int ret;
  7002. KeyShareEntry *keyShareEntry = NULL;
  7003. word16 group;
  7004. if (msgType == client_hello) {
  7005. int offset = 0;
  7006. word16 len;
  7007. TLSX* extension;
  7008. /* Add a KeyShare extension if it doesn't exist. */
  7009. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7010. if (extension == NULL) {
  7011. /* Push new KeyShare extension. */
  7012. ret = TLSX_Push(&ssl->extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  7013. if (ret != 0)
  7014. return ret;
  7015. }
  7016. if (length < OPAQUE16_LEN)
  7017. return BUFFER_ERROR;
  7018. /* ClientHello contains zero or more key share entries. */
  7019. ato16(input, &len);
  7020. if (len != length - OPAQUE16_LEN)
  7021. return BUFFER_ERROR;
  7022. offset += OPAQUE16_LEN;
  7023. while (offset < (int)length) {
  7024. ret = TLSX_KeyShareEntry_Parse(ssl, &input[offset],
  7025. length - (word16)offset,
  7026. &keyShareEntry);
  7027. if (ret < 0)
  7028. return ret;
  7029. offset += ret;
  7030. }
  7031. ret = 0;
  7032. }
  7033. else if (msgType == server_hello) {
  7034. int len;
  7035. if (length < OPAQUE16_LEN)
  7036. return BUFFER_ERROR;
  7037. /* The data is the named group the server wants to use. */
  7038. ato16(input, &group);
  7039. /* Check the selected group was supported by ClientHello extensions. */
  7040. if (!TLSX_SupportedGroups_Find(ssl, group)) {
  7041. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7042. return BAD_KEY_SHARE_DATA;
  7043. }
  7044. /* Check if the group was sent. */
  7045. if (!TLSX_KeyShare_Find(ssl, group)) {
  7046. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7047. return BAD_KEY_SHARE_DATA;
  7048. }
  7049. /* ServerHello contains one key share entry. */
  7050. len = TLSX_KeyShareEntry_Parse(ssl, input, length, &keyShareEntry);
  7051. if (len != (int)length)
  7052. return BUFFER_ERROR;
  7053. /* Not in list sent if there isn't a private key. */
  7054. if (keyShareEntry == NULL || (keyShareEntry->key == NULL
  7055. #if !defined(NO_DH) || defined(HAVE_PQC)
  7056. && keyShareEntry->privKey == NULL
  7057. #endif
  7058. )) {
  7059. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7060. return BAD_KEY_SHARE_DATA;
  7061. }
  7062. /* Process the entry to calculate the secret. */
  7063. ret = TLSX_KeyShare_Process(ssl, keyShareEntry);
  7064. if (ret == 0)
  7065. ssl->session->namedGroup = ssl->namedGroup = group;
  7066. }
  7067. else if (msgType == hello_retry_request) {
  7068. if (length != OPAQUE16_LEN)
  7069. return BUFFER_ERROR;
  7070. /* The data is the named group the server wants to use. */
  7071. ato16(input, &group);
  7072. #ifdef WOLFSSL_ASYNC_CRYPT
  7073. /* only perform find and clear TLSX if not returning from async */
  7074. if (ssl->error != WC_PENDING_E)
  7075. #endif
  7076. {
  7077. /* Check the selected group was supported by ClientHello extensions. */
  7078. if (!TLSX_SupportedGroups_Find(ssl, group)) {
  7079. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7080. return BAD_KEY_SHARE_DATA;
  7081. }
  7082. /* Check if the group was sent. */
  7083. if (TLSX_KeyShare_Find(ssl, group)) {
  7084. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7085. return BAD_KEY_SHARE_DATA;
  7086. }
  7087. /* Clear out unusable key shares. */
  7088. ret = TLSX_KeyShare_Empty(ssl);
  7089. if (ret != 0)
  7090. return ret;
  7091. }
  7092. #ifdef HAVE_PQC
  7093. /* For post-quantum groups, do this in TLSX_PopulateExtensions(). */
  7094. if (!WOLFSSL_NAMED_GROUP_IS_PQC(group))
  7095. #endif
  7096. ret = TLSX_KeyShare_Use(ssl, group, 0, NULL, NULL);
  7097. }
  7098. else {
  7099. /* Not a message type that is allowed to have this extension. */
  7100. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  7101. return SANITY_MSG_E;
  7102. }
  7103. return ret;
  7104. }
  7105. /* Create a new key share entry and put it into the list.
  7106. *
  7107. * list The linked list of key share entries.
  7108. * group The named group.
  7109. * heap The memory to allocate with.
  7110. * keyShareEntry The new key share entry object.
  7111. * returns 0 on success and other values indicate failure.
  7112. */
  7113. static int TLSX_KeyShare_New(KeyShareEntry** list, int group, void *heap,
  7114. KeyShareEntry** keyShareEntry)
  7115. {
  7116. KeyShareEntry* kse;
  7117. KeyShareEntry** next;
  7118. kse = (KeyShareEntry*)XMALLOC(sizeof(KeyShareEntry), heap,
  7119. DYNAMIC_TYPE_TLSX);
  7120. if (kse == NULL)
  7121. return MEMORY_E;
  7122. XMEMSET(kse, 0, sizeof(*kse));
  7123. kse->group = (word16)group;
  7124. /* Add it to the back and maintain the links. */
  7125. while (*list != NULL) {
  7126. /* Assign to temporary to work around compiler bug found by customer. */
  7127. next = &((*list)->next);
  7128. list = next;
  7129. }
  7130. *list = kse;
  7131. *keyShareEntry = kse;
  7132. (void)heap;
  7133. return 0;
  7134. }
  7135. #ifdef HAVE_PQC
  7136. static int server_generate_pqc_ciphertext(WOLFSSL* ssl,
  7137. KeyShareEntry* keyShareEntry, byte* data, word16 len)
  7138. {
  7139. /* I am the server. The data parameter is the client's public key. I need
  7140. * to generate the public information (AKA ciphertext) and shared secret
  7141. * here. Note the "public information" is equivalent to a the public key in
  7142. * key exchange parlance. That's why it is being assigned to pubKey.
  7143. */
  7144. int type;
  7145. KyberKey kem[1];
  7146. byte* sharedSecret = NULL;
  7147. byte* ciphertext = NULL;
  7148. int ret = 0;
  7149. int oqs_group = 0;
  7150. int ecc_group = 0;
  7151. KeyShareEntry *ecc_kse = NULL;
  7152. ecc_key eccpubkey;
  7153. word32 outlen = 0;
  7154. word32 pubSz = 0;
  7155. word32 ctSz = 0;
  7156. word32 ssSz = 0;
  7157. findEccPqc(&ecc_group, &oqs_group, keyShareEntry->group);
  7158. ret = kyber_id2type(oqs_group, &type);
  7159. if (ret != 0) {
  7160. WOLFSSL_MSG("Invalid Kyber algorithm specified.");
  7161. ret = BAD_FUNC_ARG;
  7162. }
  7163. if (ret == 0) {
  7164. ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
  7165. if (ret != 0) {
  7166. WOLFSSL_MSG("Could not do ECC public key initialization.");
  7167. ret = MEMORY_E;
  7168. }
  7169. }
  7170. if (ret == 0) {
  7171. ecc_kse = (KeyShareEntry*)XMALLOC(sizeof(*ecc_kse), ssl->heap,
  7172. DYNAMIC_TYPE_TLSX);
  7173. if (ecc_kse == NULL) {
  7174. WOLFSSL_MSG("ecc_kse memory allocation failure");
  7175. ret = MEMORY_ERROR;
  7176. }
  7177. }
  7178. if (ret == 0) {
  7179. XMEMSET(ecc_kse, 0, sizeof(*ecc_kse));
  7180. }
  7181. if (ret == 0 && ecc_group != 0) {
  7182. ecc_kse->group = ecc_group;
  7183. ret = TLSX_KeyShare_GenEccKey(ssl, ecc_kse);
  7184. if (ret != 0) {
  7185. /* No message, TLSX_KeyShare_GenEccKey() will do it. */
  7186. return ret;
  7187. }
  7188. ret = 0;
  7189. }
  7190. if (ret == 0) {
  7191. ret = wc_KyberKey_Init(type, kem, ssl->heap, INVALID_DEVID);
  7192. if (ret != 0) {
  7193. WOLFSSL_MSG("Error creating Kyber KEM");
  7194. }
  7195. }
  7196. if (ret == 0) {
  7197. ret = wc_KyberKey_PublicKeySize(kem, &pubSz);
  7198. }
  7199. if (ret == 0) {
  7200. ret = wc_KyberKey_CipherTextSize(kem, &ctSz);
  7201. }
  7202. if (ret == 0) {
  7203. ret = wc_KyberKey_SharedSecretSize(kem, &ssSz);
  7204. }
  7205. if (ret == 0 && len != pubSz + ecc_kse->pubKeyLen) {
  7206. WOLFSSL_MSG("Invalid public key.");
  7207. ret = BAD_FUNC_ARG;
  7208. }
  7209. if (ret == 0) {
  7210. sharedSecret = (byte*)XMALLOC(ecc_kse->keyLen + ssSz, ssl->heap,
  7211. DYNAMIC_TYPE_TLSX);
  7212. ciphertext = (byte*)XMALLOC(ecc_kse->pubKeyLen + ctSz, ssl->heap,
  7213. DYNAMIC_TYPE_TLSX);
  7214. if (sharedSecret == NULL || ciphertext == NULL) {
  7215. WOLFSSL_MSG("Ciphertext/shared secret memory allocation failure.");
  7216. ret = MEMORY_E;
  7217. }
  7218. }
  7219. if (ecc_group != 0) {
  7220. if (ret == 0) {
  7221. /* Point is validated by import function. */
  7222. ret = wc_ecc_import_x963(data, len - pubSz, &eccpubkey);
  7223. if (ret != 0) {
  7224. WOLFSSL_MSG("Bad ECC public key.");
  7225. }
  7226. }
  7227. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  7228. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  7229. !defined(HAVE_SELFTEST)
  7230. if (ret == 0) {
  7231. ret = wc_ecc_set_rng(ecc_kse->key, ssl->rng);
  7232. }
  7233. #endif
  7234. if (ret == 0) {
  7235. outlen = ecc_kse->keyLen;
  7236. PRIVATE_KEY_UNLOCK();
  7237. ret = wc_ecc_shared_secret(ecc_kse->key, &eccpubkey,
  7238. sharedSecret,
  7239. &outlen);
  7240. PRIVATE_KEY_LOCK();
  7241. if (outlen != ecc_kse->keyLen) {
  7242. WOLFSSL_MSG("Data length mismatch.");
  7243. ret = BAD_FUNC_ARG;
  7244. }
  7245. }
  7246. }
  7247. if (ret == 0) {
  7248. ret = wc_KyberKey_DecodePublicKey(kem, data + ecc_kse->pubKeyLen,
  7249. pubSz);
  7250. }
  7251. if (ret == 0) {
  7252. ret = wc_KyberKey_Encapsulate(kem, ciphertext + ecc_kse->pubKeyLen,
  7253. sharedSecret + outlen, ssl->rng);
  7254. if (ret != 0) {
  7255. WOLFSSL_MSG("wc_KyberKey encapsulation failure.");
  7256. }
  7257. }
  7258. if (ret == 0) {
  7259. if (keyShareEntry->ke != NULL) {
  7260. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7261. }
  7262. keyShareEntry->ke = sharedSecret;
  7263. keyShareEntry->keLen = outlen + ssSz;
  7264. sharedSecret = NULL;
  7265. XMEMCPY(ciphertext, ecc_kse->pubKey, ecc_kse->pubKeyLen);
  7266. keyShareEntry->pubKey = ciphertext;
  7267. keyShareEntry->pubKeyLen = (word32)(ecc_kse->pubKeyLen + ctSz);
  7268. ciphertext = NULL;
  7269. }
  7270. TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
  7271. if (sharedSecret != NULL)
  7272. XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_TLSX);
  7273. if (ciphertext != NULL)
  7274. XFREE(ciphertext, ssl->heap, DYNAMIC_TYPE_TLSX);
  7275. wc_ecc_free(&eccpubkey);
  7276. wc_KyberKey_Free(kem);
  7277. return ret;
  7278. }
  7279. #endif /* HAVE_PQC */
  7280. /* Use the data to create a new key share object in the extensions.
  7281. *
  7282. * ssl The SSL/TLS object.
  7283. * group The named group.
  7284. * len The length of the public key data.
  7285. * data The public key data.
  7286. * kse The new key share entry object.
  7287. * returns 0 on success and other values indicate failure.
  7288. */
  7289. int TLSX_KeyShare_Use(WOLFSSL* ssl, word16 group, word16 len, byte* data,
  7290. KeyShareEntry **kse)
  7291. {
  7292. int ret = 0;
  7293. TLSX* extension;
  7294. KeyShareEntry* keyShareEntry = NULL;
  7295. /* Find the KeyShare extension if it exists. */
  7296. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7297. if (extension == NULL) {
  7298. /* Push new KeyShare extension. */
  7299. ret = TLSX_Push(&ssl->extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  7300. if (ret != 0)
  7301. return ret;
  7302. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7303. if (extension == NULL)
  7304. return MEMORY_E;
  7305. }
  7306. extension->resp = 0;
  7307. /* Try to find the key share entry with this group. */
  7308. keyShareEntry = (KeyShareEntry*)extension->data;
  7309. while (keyShareEntry != NULL) {
  7310. if (keyShareEntry->group == group)
  7311. break;
  7312. keyShareEntry = keyShareEntry->next;
  7313. }
  7314. /* Create a new key share entry if not found. */
  7315. if (keyShareEntry == NULL) {
  7316. ret = TLSX_KeyShare_New((KeyShareEntry**)&extension->data, group,
  7317. ssl->heap, &keyShareEntry);
  7318. if (ret != 0)
  7319. return ret;
  7320. }
  7321. #ifdef HAVE_PQC
  7322. if (WOLFSSL_NAMED_GROUP_IS_PQC(group) &&
  7323. ssl->options.side == WOLFSSL_SERVER_END) {
  7324. ret = server_generate_pqc_ciphertext(ssl, keyShareEntry, data,
  7325. len);
  7326. if (ret != 0)
  7327. return ret;
  7328. }
  7329. else
  7330. #endif
  7331. if (data != NULL) {
  7332. if (keyShareEntry->ke != NULL) {
  7333. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7334. }
  7335. keyShareEntry->ke = data;
  7336. keyShareEntry->keLen = len;
  7337. }
  7338. else {
  7339. /* Generate a key pair. */
  7340. ret = TLSX_KeyShare_GenKey(ssl, keyShareEntry);
  7341. if (ret != 0)
  7342. return ret;
  7343. }
  7344. if (kse != NULL)
  7345. *kse = keyShareEntry;
  7346. return 0;
  7347. }
  7348. /* Set an empty Key Share extension.
  7349. *
  7350. * ssl The SSL/TLS object.
  7351. * returns 0 on success and other values indicate failure.
  7352. */
  7353. int TLSX_KeyShare_Empty(WOLFSSL* ssl)
  7354. {
  7355. int ret = 0;
  7356. TLSX* extension;
  7357. /* Find the KeyShare extension if it exists. */
  7358. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7359. if (extension == NULL) {
  7360. /* Push new KeyShare extension. */
  7361. ret = TLSX_Push(&ssl->extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  7362. }
  7363. else if (extension->data != NULL) {
  7364. TLSX_KeyShare_FreeAll((KeyShareEntry*)extension->data, ssl->heap);
  7365. extension->data = NULL;
  7366. }
  7367. return ret;
  7368. }
  7369. /* Returns whether this group is supported.
  7370. *
  7371. * namedGroup The named group to check.
  7372. * returns 1 when supported or 0 otherwise.
  7373. */
  7374. static int TLSX_KeyShare_IsSupported(int namedGroup)
  7375. {
  7376. switch (namedGroup) {
  7377. #ifdef HAVE_FFDHE_2048
  7378. case WOLFSSL_FFDHE_2048:
  7379. break;
  7380. #endif
  7381. #ifdef HAVE_FFDHE_3072
  7382. case WOLFSSL_FFDHE_3072:
  7383. break;
  7384. #endif
  7385. #ifdef HAVE_FFDHE_4096
  7386. case WOLFSSL_FFDHE_4096:
  7387. break;
  7388. #endif
  7389. #ifdef HAVE_FFDHE_6144
  7390. case WOLFSSL_FFDHE_6144:
  7391. break;
  7392. #endif
  7393. #ifdef HAVE_FFDHE_8192
  7394. case WOLFSSL_FFDHE_8192:
  7395. break;
  7396. #endif
  7397. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  7398. #ifdef HAVE_ECC_KOBLITZ
  7399. case WOLFSSL_ECC_SECP256K1:
  7400. break;
  7401. #endif
  7402. #ifndef NO_ECC_SECP
  7403. case WOLFSSL_ECC_SECP256R1:
  7404. break;
  7405. #endif /* !NO_ECC_SECP */
  7406. #ifdef HAVE_ECC_BRAINPOOL
  7407. case WOLFSSL_ECC_BRAINPOOLP256R1:
  7408. break;
  7409. #endif
  7410. #endif
  7411. #if defined(HAVE_CURVE25519) && ECC_MIN_KEY_SZ <= 256
  7412. case WOLFSSL_ECC_X25519:
  7413. break;
  7414. #endif
  7415. #if defined(HAVE_CURVE448) && ECC_MIN_KEY_SZ <= 448
  7416. case WOLFSSL_ECC_X448:
  7417. break;
  7418. #endif
  7419. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  7420. #ifndef NO_ECC_SECP
  7421. case WOLFSSL_ECC_SECP384R1:
  7422. break;
  7423. #endif /* !NO_ECC_SECP */
  7424. #ifdef HAVE_ECC_BRAINPOOL
  7425. case WOLFSSL_ECC_BRAINPOOLP384R1:
  7426. break;
  7427. #endif
  7428. #endif
  7429. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  7430. #ifndef NO_ECC_SECP
  7431. case WOLFSSL_ECC_SECP521R1:
  7432. break;
  7433. #endif /* !NO_ECC_SECP */
  7434. #endif
  7435. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  7436. #ifdef HAVE_ECC_KOBLITZ
  7437. case WOLFSSL_ECC_SECP160K1:
  7438. break;
  7439. #endif
  7440. #ifndef NO_ECC_SECP
  7441. case WOLFSSL_ECC_SECP160R1:
  7442. break;
  7443. #endif
  7444. #ifdef HAVE_ECC_SECPR2
  7445. case WOLFSSL_ECC_SECP160R2:
  7446. break;
  7447. #endif
  7448. #endif
  7449. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  7450. #ifdef HAVE_ECC_KOBLITZ
  7451. case WOLFSSL_ECC_SECP192K1:
  7452. break;
  7453. #endif
  7454. #ifndef NO_ECC_SECP
  7455. case WOLFSSL_ECC_SECP192R1:
  7456. break;
  7457. #endif
  7458. #endif
  7459. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  7460. #ifdef HAVE_ECC_KOBLITZ
  7461. case WOLFSSL_ECC_SECP224K1:
  7462. break;
  7463. #endif
  7464. #ifndef NO_ECC_SECP
  7465. case WOLFSSL_ECC_SECP224R1:
  7466. break;
  7467. #endif
  7468. #endif
  7469. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  7470. #ifdef HAVE_ECC_BRAINPOOL
  7471. case WOLFSSL_ECC_BRAINPOOLP512R1:
  7472. break;
  7473. #endif
  7474. #endif
  7475. #ifdef HAVE_PQC
  7476. #ifdef WOLFSSL_WC_KYBER
  7477. #ifdef WOLFSSL_KYBER512
  7478. case WOLFSSL_KYBER_LEVEL1:
  7479. #endif
  7480. #ifdef WOLFSSL_KYBER768
  7481. case WOLFSSL_KYBER_LEVEL3:
  7482. #endif
  7483. #ifdef WOLFSSL_KYBER1024
  7484. case WOLFSSL_KYBER_LEVEL5:
  7485. #endif
  7486. break;
  7487. #elif defined(HAVE_LIBOQS)
  7488. case WOLFSSL_KYBER_LEVEL1:
  7489. case WOLFSSL_KYBER_LEVEL3:
  7490. case WOLFSSL_KYBER_LEVEL5:
  7491. case WOLFSSL_P256_KYBER_LEVEL1:
  7492. case WOLFSSL_P384_KYBER_LEVEL3:
  7493. case WOLFSSL_P521_KYBER_LEVEL5:
  7494. {
  7495. int ret;
  7496. int id;
  7497. findEccPqc(NULL, &namedGroup, namedGroup);
  7498. ret = kyber_id2type(namedGroup, &id);
  7499. if (ret == NOT_COMPILED_IN) {
  7500. return 0;
  7501. }
  7502. if (! ext_kyber_enabled(id)) {
  7503. return 0;
  7504. }
  7505. break;
  7506. }
  7507. #elif defined(HAVE_PQM4)
  7508. case WOLFSSL_KYBER_LEVEL1:
  7509. break;
  7510. #endif
  7511. #endif /* HAVE_PQC */
  7512. default:
  7513. return 0;
  7514. }
  7515. return 1;
  7516. }
  7517. /* Examines the application specified group ranking and returns the rank of the
  7518. * group.
  7519. * If no group ranking set then all groups are rank 0 (highest).
  7520. *
  7521. * ssl The SSL/TLS object.
  7522. * group The group to check ranking for.
  7523. * returns ranking from 0 to MAX_GROUP_COUNT-1 or -1 when group not in list.
  7524. */
  7525. static int TLSX_KeyShare_GroupRank(WOLFSSL* ssl, int group)
  7526. {
  7527. byte i;
  7528. if (ssl->numGroups == 0) {
  7529. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  7530. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  7531. #ifndef NO_ECC_SECP
  7532. ssl->group[ssl->numGroups++] = WOLFSSL_ECC_SECP256R1;
  7533. #endif
  7534. #endif
  7535. #endif
  7536. #ifndef HAVE_FIPS
  7537. #if defined(HAVE_CURVE25519) && ECC_MIN_KEY_SZ <= 256
  7538. ssl->group[ssl->numGroups++] = WOLFSSL_ECC_X25519;
  7539. #endif
  7540. #endif
  7541. #ifndef HAVE_FIPS
  7542. #if defined(HAVE_CURVE448) && ECC_MIN_KEY_SZ <= 448
  7543. ssl->group[ssl->numGroups++] = WOLFSSL_ECC_X448;
  7544. #endif
  7545. #endif
  7546. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  7547. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  7548. #ifndef NO_ECC_SECP
  7549. ssl->group[ssl->numGroups++] = WOLFSSL_ECC_SECP384R1;
  7550. #endif
  7551. #endif
  7552. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  7553. #ifndef NO_ECC_SECP
  7554. ssl->group[ssl->numGroups++] = WOLFSSL_ECC_SECP521R1;
  7555. #endif
  7556. #endif
  7557. #endif
  7558. /* Add FFDHE supported groups. */
  7559. #ifdef HAVE_FFDHE_2048
  7560. ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_2048;
  7561. #endif
  7562. #ifdef HAVE_FFDHE_3072
  7563. ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_3072;
  7564. #endif
  7565. #ifdef HAVE_FFDHE_4096
  7566. ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_4096;
  7567. #endif
  7568. #ifdef HAVE_FFDHE_6144
  7569. ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_6144;
  7570. #endif
  7571. #ifdef HAVE_FFDHE_8192
  7572. ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_8192;
  7573. #endif
  7574. #ifdef HAVE_PQC
  7575. /* For the liboqs groups we need to do a runtime check because
  7576. * liboqs could be compiled to make an algorithm unavailable.
  7577. */
  7578. #ifdef WOLFSSL_WC_KYBER
  7579. #ifdef WOLFSSL_KYBER512
  7580. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_LEVEL1))
  7581. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_LEVEL1;
  7582. #endif
  7583. #ifdef WOLFSSL_KYBER768
  7584. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_LEVEL3))
  7585. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_LEVEL3;
  7586. #endif
  7587. #ifdef WOLFSSL_KYBER1024
  7588. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_LEVEL5))
  7589. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_LEVEL5;
  7590. #endif
  7591. #elif defined(HAVE_LIBOQS)
  7592. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_LEVEL1))
  7593. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_LEVEL1;
  7594. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_LEVEL3))
  7595. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_LEVEL3;
  7596. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_LEVEL5))
  7597. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_LEVEL5;
  7598. if (TLSX_KeyShare_IsSupported(WOLFSSL_P256_KYBER_LEVEL1))
  7599. ssl->group[ssl->numGroups++] = WOLFSSL_P256_KYBER_LEVEL1;
  7600. if (TLSX_KeyShare_IsSupported(WOLFSSL_P384_KYBER_LEVEL3))
  7601. ssl->group[ssl->numGroups++] = WOLFSSL_P384_KYBER_LEVEL3;
  7602. if (TLSX_KeyShare_IsSupported(WOLFSSL_P521_KYBER_LEVEL5))
  7603. ssl->group[ssl->numGroups++] = WOLFSSL_P521_KYBER_LEVEL5;
  7604. #elif defined(HAVE_PQM4)
  7605. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_LEVEL1))
  7606. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_LEVEL1;
  7607. #endif /* HAVE_LIBOQS */
  7608. #endif /* HAVE_PQC */
  7609. }
  7610. for (i = 0; i < ssl->numGroups; i++)
  7611. if (ssl->group[i] == (word16)group)
  7612. return i;
  7613. return -1;
  7614. }
  7615. /* Set a key share that is supported by the client into extensions.
  7616. *
  7617. * ssl The SSL/TLS object.
  7618. * returns BAD_KEY_SHARE_DATA if no supported group has a key share,
  7619. * 0 if a supported group has a key share and other values indicate an error.
  7620. */
  7621. static int TLSX_KeyShare_SetSupported(WOLFSSL* ssl)
  7622. {
  7623. int ret;
  7624. #ifdef HAVE_SUPPORTED_CURVES
  7625. TLSX* extension;
  7626. SupportedCurve* curve = NULL;
  7627. SupportedCurve* preferredCurve = NULL;
  7628. int preferredRank = WOLFSSL_MAX_GROUP_COUNT;
  7629. int rank;
  7630. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  7631. if (extension != NULL)
  7632. curve = (SupportedCurve*)extension->data;
  7633. /* Use server's preference order. */
  7634. for (; curve != NULL; curve = curve->next) {
  7635. if (!TLSX_KeyShare_IsSupported(curve->name))
  7636. continue;
  7637. if (wolfSSL_curve_is_disabled(ssl, curve->name))
  7638. continue;
  7639. rank = TLSX_KeyShare_GroupRank(ssl, curve->name);
  7640. if (rank == -1)
  7641. continue;
  7642. if (rank < preferredRank) {
  7643. preferredCurve = curve;
  7644. preferredRank = rank;
  7645. }
  7646. }
  7647. curve = preferredCurve;
  7648. if (curve == NULL) {
  7649. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7650. return BAD_KEY_SHARE_DATA;
  7651. }
  7652. /* Delete the old key share data list. */
  7653. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7654. if (extension != NULL) {
  7655. KeyShareEntry* kse = (KeyShareEntry*)extension->data;
  7656. #ifdef WOLFSSL_ASYNC_CRYPT
  7657. /* for async don't free, call `TLSX_KeyShare_Use` again */
  7658. if (kse && kse->lastRet != WC_PENDING_E)
  7659. #endif
  7660. {
  7661. TLSX_KeyShare_FreeAll(kse, ssl->heap);
  7662. extension->data = NULL;
  7663. }
  7664. }
  7665. /* Add in the chosen group. */
  7666. ret = TLSX_KeyShare_Use(ssl, curve->name, 0, NULL, NULL);
  7667. if (ret != 0 && ret != WC_PENDING_E)
  7668. return ret;
  7669. /* Set extension to be in response. */
  7670. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7671. extension->resp = 1;
  7672. #else
  7673. (void)ssl;
  7674. WOLFSSL_ERROR_VERBOSE(NOT_COMPILED_IN);
  7675. ret = NOT_COMPILED_IN;
  7676. #endif
  7677. return ret;
  7678. }
  7679. /* Ensure there is a key pair that can be used for key exchange.
  7680. *
  7681. * ssl The SSL/TLS object.
  7682. * doHelloRetry If set to non-zero will do hello_retry
  7683. * returns 0 on success and other values indicate failure.
  7684. */
  7685. int TLSX_KeyShare_Establish(WOLFSSL *ssl, int* doHelloRetry)
  7686. {
  7687. int ret;
  7688. TLSX* extension;
  7689. KeyShareEntry* clientKSE = NULL;
  7690. KeyShareEntry* serverKSE;
  7691. KeyShareEntry* list = NULL;
  7692. KeyShareEntry* preferredKSE = NULL;
  7693. int preferredRank = WOLFSSL_MAX_GROUP_COUNT;
  7694. int rank;
  7695. /* Find the KeyShare extension if it exists. */
  7696. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7697. if (extension != NULL)
  7698. list = (KeyShareEntry*)extension->data;
  7699. if (extension && extension->resp == 1) {
  7700. ret = 0;
  7701. #ifdef WOLFSSL_ASYNC_CRYPT
  7702. /* in async case make sure key generation is finalized */
  7703. serverKSE = (KeyShareEntry*)extension->data;
  7704. if (serverKSE->lastRet == WC_PENDING_E) {
  7705. if (ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE)
  7706. *doHelloRetry = 1;
  7707. ret = TLSX_KeyShare_GenKey(ssl, serverKSE);
  7708. }
  7709. #endif
  7710. return ret;
  7711. }
  7712. /* Use server's preference order. */
  7713. for (clientKSE = list; clientKSE != NULL; clientKSE = clientKSE->next) {
  7714. if (clientKSE->ke == NULL)
  7715. continue;
  7716. /* Check consistency now - extensions in any order. */
  7717. if (!TLSX_SupportedGroups_Find(ssl, clientKSE->group))
  7718. continue;
  7719. if (!WOLFSSL_NAMED_GROUP_IS_FFHDE(clientKSE->group)) {
  7720. /* Check max value supported. */
  7721. if (clientKSE->group > WOLFSSL_ECC_MAX) {
  7722. #ifdef HAVE_PQC
  7723. if (!WOLFSSL_NAMED_GROUP_IS_PQC(clientKSE->group))
  7724. #endif
  7725. continue;
  7726. }
  7727. if (wolfSSL_curve_is_disabled(ssl, clientKSE->group))
  7728. continue;
  7729. }
  7730. if (!TLSX_KeyShare_IsSupported(clientKSE->group))
  7731. continue;
  7732. rank = TLSX_KeyShare_GroupRank(ssl, clientKSE->group);
  7733. if (rank == -1)
  7734. continue;
  7735. if (rank < preferredRank) {
  7736. preferredKSE = clientKSE;
  7737. preferredRank = rank;
  7738. }
  7739. }
  7740. clientKSE = preferredKSE;
  7741. /* No supported group found - send HelloRetryRequest. */
  7742. if (clientKSE == NULL) {
  7743. /* Set KEY_SHARE_ERROR to indicate HelloRetryRequest required. */
  7744. *doHelloRetry = 1;
  7745. return TLSX_KeyShare_SetSupported(ssl);
  7746. }
  7747. list = NULL;
  7748. /* Generate a new key pair except in the case of OQS KEM because we
  7749. * are going to encapsulate and that does not require us to generate a
  7750. * key pair.
  7751. */
  7752. ret = TLSX_KeyShare_New(&list, clientKSE->group, ssl->heap, &serverKSE);
  7753. if (ret != 0)
  7754. return ret;
  7755. if (clientKSE->key == NULL) {
  7756. #ifdef HAVE_PQC
  7757. if (WOLFSSL_NAMED_GROUP_IS_PQC(clientKSE->group)) {
  7758. /* Going to need the public key (AKA ciphertext). */
  7759. serverKSE->pubKey = clientKSE->pubKey;
  7760. clientKSE->pubKey = NULL;
  7761. serverKSE->pubKeyLen = clientKSE->pubKeyLen;
  7762. clientKSE->pubKeyLen = 0;
  7763. }
  7764. else
  7765. #endif
  7766. {
  7767. ret = TLSX_KeyShare_GenKey(ssl, serverKSE);
  7768. }
  7769. /* for async do setup of serverKSE below, but return WC_PENDING_E */
  7770. if (ret != 0
  7771. #ifdef WOLFSSL_ASYNC_CRYPT
  7772. && ret != WC_PENDING_E
  7773. #endif
  7774. ) {
  7775. return ret;
  7776. }
  7777. }
  7778. else {
  7779. /* transfer buffers to serverKSE */
  7780. serverKSE->key = clientKSE->key;
  7781. clientKSE->key = NULL;
  7782. serverKSE->keyLen = clientKSE->keyLen;
  7783. serverKSE->pubKey = clientKSE->pubKey;
  7784. clientKSE->pubKey = NULL;
  7785. serverKSE->pubKeyLen = clientKSE->pubKeyLen;
  7786. #ifndef NO_DH
  7787. serverKSE->privKey = clientKSE->privKey;
  7788. clientKSE->privKey = NULL;
  7789. #endif
  7790. }
  7791. serverKSE->ke = clientKSE->ke;
  7792. serverKSE->keLen = clientKSE->keLen;
  7793. clientKSE->ke = NULL;
  7794. clientKSE->keLen = 0;
  7795. TLSX_KeyShare_FreeAll((KeyShareEntry*)extension->data, ssl->heap);
  7796. extension->data = (void *)serverKSE;
  7797. extension->resp = 1;
  7798. return ret;
  7799. }
  7800. /* Derive the shared secret of the key exchange.
  7801. *
  7802. * ssl The SSL/TLS object.
  7803. * returns 0 on success and other values indicate failure.
  7804. */
  7805. int TLSX_KeyShare_DeriveSecret(WOLFSSL *ssl)
  7806. {
  7807. int ret;
  7808. TLSX* extension;
  7809. KeyShareEntry* list = NULL;
  7810. #ifdef WOLFSSL_ASYNC_CRYPT
  7811. ret = wolfSSL_AsyncPop(ssl, NULL);
  7812. /* Check for error */
  7813. if (ret != WC_NOT_PENDING_E && ret < 0) {
  7814. return ret;
  7815. }
  7816. #endif
  7817. /* Find the KeyShare extension if it exists. */
  7818. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7819. if (extension != NULL)
  7820. list = (KeyShareEntry*)extension->data;
  7821. if (list == NULL)
  7822. return KEY_SHARE_ERROR;
  7823. /* Calculate secret. */
  7824. ret = TLSX_KeyShare_Process(ssl, list);
  7825. return ret;
  7826. }
  7827. #define KS_FREE_ALL TLSX_KeyShare_FreeAll
  7828. #define KS_GET_SIZE TLSX_KeyShare_GetSize
  7829. #define KS_WRITE TLSX_KeyShare_Write
  7830. #define KS_PARSE TLSX_KeyShare_Parse
  7831. #else
  7832. #define KS_FREE_ALL(a, b)
  7833. #define KS_GET_SIZE(a, b) 0
  7834. #define KS_WRITE(a, b, c) 0
  7835. #define KS_PARSE(a, b, c, d) 0
  7836. #endif /* WOLFSSL_TLS13 */
  7837. /******************************************************************************/
  7838. /* Pre-Shared Key */
  7839. /******************************************************************************/
  7840. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  7841. /* Free the pre-shared key dynamic data.
  7842. *
  7843. * list The linked list of key share entry objects.
  7844. * heap The heap used for allocation.
  7845. */
  7846. static void TLSX_PreSharedKey_FreeAll(PreSharedKey* list, void* heap)
  7847. {
  7848. PreSharedKey* current;
  7849. while ((current = list) != NULL) {
  7850. list = current->next;
  7851. XFREE(current->identity, heap, DYNAMIC_TYPE_TLSX);
  7852. XFREE(current, heap, DYNAMIC_TYPE_TLSX);
  7853. }
  7854. (void)heap;
  7855. }
  7856. /* Get the size of the encoded pre shared key extension.
  7857. *
  7858. * list The linked list of pre-shared key extensions.
  7859. * msgType The type of the message this extension is being written into.
  7860. * returns the number of bytes of the encoded pre-shared key extension or
  7861. * SANITY_MSG_E to indicate invalid message type.
  7862. */
  7863. static int TLSX_PreSharedKey_GetSize(PreSharedKey* list, byte msgType,
  7864. word16* pSz)
  7865. {
  7866. if (msgType == client_hello) {
  7867. /* Length of identities + Length of binders. */
  7868. word16 len = OPAQUE16_LEN + OPAQUE16_LEN;
  7869. while (list != NULL) {
  7870. /* Each entry has: identity, ticket age and binder. */
  7871. len += OPAQUE16_LEN + list->identityLen + OPAQUE32_LEN +
  7872. OPAQUE8_LEN + (word16)list->binderLen;
  7873. list = list->next;
  7874. }
  7875. *pSz += len;
  7876. return 0;
  7877. }
  7878. if (msgType == server_hello) {
  7879. *pSz += OPAQUE16_LEN;
  7880. return 0;
  7881. }
  7882. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  7883. return SANITY_MSG_E;
  7884. }
  7885. /* The number of bytes to be written for the binders.
  7886. *
  7887. * list The linked list of pre-shared key extensions.
  7888. * msgType The type of the message this extension is being written into.
  7889. * returns the number of bytes of the encoded pre-shared key extension or
  7890. * SANITY_MSG_E to indicate invalid message type.
  7891. */
  7892. int TLSX_PreSharedKey_GetSizeBinders(PreSharedKey* list, byte msgType,
  7893. word16* pSz)
  7894. {
  7895. word16 len;
  7896. if (msgType != client_hello) {
  7897. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  7898. return SANITY_MSG_E;
  7899. }
  7900. /* Length of all binders. */
  7901. len = OPAQUE16_LEN;
  7902. while (list != NULL) {
  7903. len += OPAQUE8_LEN + (word16)list->binderLen;
  7904. list = list->next;
  7905. }
  7906. *pSz = len;
  7907. return 0;
  7908. }
  7909. /* Writes the pre-shared key extension into the output buffer - binders only.
  7910. * Assumes that the the output buffer is big enough to hold data.
  7911. *
  7912. * list The linked list of key share entries.
  7913. * output The buffer to write into.
  7914. * msgType The type of the message this extension is being written into.
  7915. * returns the number of bytes written into the buffer.
  7916. */
  7917. int TLSX_PreSharedKey_WriteBinders(PreSharedKey* list, byte* output,
  7918. byte msgType, word16* pSz)
  7919. {
  7920. PreSharedKey* current = list;
  7921. word16 idx = 0;
  7922. word16 lenIdx;
  7923. word16 len;
  7924. if (msgType != client_hello) {
  7925. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  7926. return SANITY_MSG_E;
  7927. }
  7928. /* Skip length of all binders. */
  7929. lenIdx = idx;
  7930. idx += OPAQUE16_LEN;
  7931. while (current != NULL) {
  7932. /* Binder data length. */
  7933. output[idx++] = (byte)current->binderLen;
  7934. /* Binder data. */
  7935. XMEMCPY(output + idx, current->binder, current->binderLen);
  7936. idx += (word16)current->binderLen;
  7937. current = current->next;
  7938. }
  7939. /* Length of the binders. */
  7940. len = idx - lenIdx - OPAQUE16_LEN;
  7941. c16toa(len, output + lenIdx);
  7942. *pSz = idx;
  7943. return 0;
  7944. }
  7945. /* Writes the pre-shared key extension into the output buffer.
  7946. * Assumes that the the output buffer is big enough to hold data.
  7947. *
  7948. * list The linked list of key share entries.
  7949. * output The buffer to write into.
  7950. * msgType The type of the message this extension is being written into.
  7951. * returns the number of bytes written into the buffer.
  7952. */
  7953. static int TLSX_PreSharedKey_Write(PreSharedKey* list, byte* output,
  7954. byte msgType, word16* pSz)
  7955. {
  7956. if (msgType == client_hello) {
  7957. PreSharedKey* current = list;
  7958. word16 idx = 0;
  7959. word16 lenIdx;
  7960. word16 len;
  7961. int ret;
  7962. /* Write identites only. Binders after HMACing over this. */
  7963. lenIdx = idx;
  7964. idx += OPAQUE16_LEN;
  7965. while (current != NULL) {
  7966. /* Identity length */
  7967. c16toa(current->identityLen, output + idx);
  7968. idx += OPAQUE16_LEN;
  7969. /* Identity data */
  7970. XMEMCPY(output + idx, current->identity, current->identityLen);
  7971. idx += current->identityLen;
  7972. /* Obfuscated ticket age. */
  7973. c32toa(current->ticketAge, output + idx);
  7974. idx += OPAQUE32_LEN;
  7975. current = current->next;
  7976. }
  7977. /* Length of the identites. */
  7978. len = idx - lenIdx - OPAQUE16_LEN;
  7979. c16toa(len, output + lenIdx);
  7980. /* Don't include binders here.
  7981. * The binders are based on the hash of all the ClientHello data up to
  7982. * and include the identities written above.
  7983. */
  7984. ret = TLSX_PreSharedKey_GetSizeBinders(list, msgType, &len);
  7985. if (ret < 0)
  7986. return ret;
  7987. *pSz += idx + len;
  7988. }
  7989. else if (msgType == server_hello) {
  7990. word16 i;
  7991. /* Find the index of the chosen identity. */
  7992. for (i=0; list != NULL && !list->chosen; i++)
  7993. list = list->next;
  7994. if (list == NULL) {
  7995. WOLFSSL_ERROR_VERBOSE(BUILD_MSG_ERROR);
  7996. return BUILD_MSG_ERROR;
  7997. }
  7998. /* The index of the identity chosen by the server from the list supplied
  7999. * by the client.
  8000. */
  8001. c16toa(i, output);
  8002. *pSz += OPAQUE16_LEN;
  8003. }
  8004. else {
  8005. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8006. return SANITY_MSG_E;
  8007. }
  8008. return 0;
  8009. }
  8010. /* Parse the pre-shared key extension.
  8011. * Different formats in different messages.
  8012. *
  8013. * ssl The SSL/TLS object.
  8014. * input The extension data.
  8015. * length The length of the extension data.
  8016. * msgType The type of the message this extension is being parsed from.
  8017. * returns 0 on success and other values indicate failure.
  8018. */
  8019. static int TLSX_PreSharedKey_Parse(WOLFSSL* ssl, const byte* input,
  8020. word16 length, byte msgType)
  8021. {
  8022. TLSX* extension;
  8023. PreSharedKey* list;
  8024. if (msgType == client_hello) {
  8025. int ret;
  8026. word16 len;
  8027. word16 idx = 0;
  8028. TLSX_Remove(&ssl->extensions, TLSX_PRE_SHARED_KEY, ssl->heap);
  8029. /* Length of identities and of binders. */
  8030. if ((int)(length - idx) < OPAQUE16_LEN + OPAQUE16_LEN)
  8031. return BUFFER_E;
  8032. /* Length of identities. */
  8033. ato16(input + idx, &len);
  8034. idx += OPAQUE16_LEN;
  8035. if (len < MIN_PSK_ID_LEN || length - idx < len)
  8036. return BUFFER_E;
  8037. /* Create a pre-shared key object for each identity. */
  8038. while (len > 0) {
  8039. const byte* identity;
  8040. word16 identityLen;
  8041. word32 age;
  8042. if (len < OPAQUE16_LEN)
  8043. return BUFFER_E;
  8044. /* Length of identity. */
  8045. ato16(input + idx, &identityLen);
  8046. idx += OPAQUE16_LEN;
  8047. if (len < OPAQUE16_LEN + identityLen + OPAQUE32_LEN ||
  8048. identityLen > MAX_PSK_ID_LEN)
  8049. return BUFFER_E;
  8050. /* Cache identity pointer. */
  8051. identity = input + idx;
  8052. idx += identityLen;
  8053. /* Ticket age. */
  8054. ato32(input + idx, &age);
  8055. idx += OPAQUE32_LEN;
  8056. ret = TLSX_PreSharedKey_Use(ssl, identity, identityLen, age, no_mac,
  8057. 0, 0, 1, NULL);
  8058. if (ret != 0)
  8059. return ret;
  8060. /* Done with this identity. */
  8061. len -= OPAQUE16_LEN + identityLen + OPAQUE32_LEN;
  8062. }
  8063. /* Find the list of identities sent to server. */
  8064. extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
  8065. if (extension == NULL)
  8066. return PSK_KEY_ERROR;
  8067. list = (PreSharedKey*)extension->data;
  8068. /* Length of binders. */
  8069. if (idx + OPAQUE16_LEN > length)
  8070. return BUFFER_E;
  8071. ato16(input + idx, &len);
  8072. idx += OPAQUE16_LEN;
  8073. if (len < MIN_PSK_BINDERS_LEN || length - idx < len)
  8074. return BUFFER_E;
  8075. /* Set binder for each identity. */
  8076. while (list != NULL && len > 0) {
  8077. /* Length of binder */
  8078. list->binderLen = input[idx++];
  8079. if (list->binderLen < WC_SHA256_DIGEST_SIZE ||
  8080. list->binderLen > WC_MAX_DIGEST_SIZE)
  8081. return BUFFER_E;
  8082. if (len < OPAQUE8_LEN + list->binderLen)
  8083. return BUFFER_E;
  8084. /* Copy binder into static buffer. */
  8085. XMEMCPY(list->binder, input + idx, list->binderLen);
  8086. idx += (word16)list->binderLen;
  8087. /* Done with binder entry. */
  8088. len -= OPAQUE8_LEN + (word16)list->binderLen;
  8089. /* Next identity. */
  8090. list = list->next;
  8091. }
  8092. if (list != NULL || len != 0)
  8093. return BUFFER_E;
  8094. return 0;
  8095. }
  8096. if (msgType == server_hello) {
  8097. word16 idx;
  8098. /* Index of identity chosen by server. */
  8099. if (length != OPAQUE16_LEN)
  8100. return BUFFER_E;
  8101. ato16(input, &idx);
  8102. #ifdef WOLFSSL_EARLY_DATA
  8103. ssl->options.pskIdIndex = idx + 1;
  8104. #endif
  8105. /* Find the list of identities sent to server. */
  8106. extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
  8107. if (extension == NULL)
  8108. return PSK_KEY_ERROR;
  8109. list = (PreSharedKey*)extension->data;
  8110. /* Mark the identity as chosen. */
  8111. for (; list != NULL && idx > 0; idx--)
  8112. list = list->next;
  8113. if (list == NULL) {
  8114. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  8115. return PSK_KEY_ERROR;
  8116. }
  8117. list->chosen = 1;
  8118. #ifdef HAVE_SESSION_TICKET
  8119. if (list->resumption) {
  8120. /* Check that the session's details are the same as the server's. */
  8121. if (ssl->options.cipherSuite0 != ssl->session->cipherSuite0 ||
  8122. ssl->options.cipherSuite != ssl->session->cipherSuite ||
  8123. ssl->session->version.major != ssl->ctx->method->version.major ||
  8124. ssl->session->version.minor != ssl->ctx->method->version.minor) {
  8125. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  8126. return PSK_KEY_ERROR;
  8127. }
  8128. }
  8129. #endif
  8130. return 0;
  8131. }
  8132. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8133. return SANITY_MSG_E;
  8134. }
  8135. /* Create a new pre-shared key and put it into the list.
  8136. *
  8137. * list The linked list of pre-shared key.
  8138. * identity The identity.
  8139. * len The length of the identity data.
  8140. * heap The memory to allocate with.
  8141. * preSharedKey The new pre-shared key object.
  8142. * returns 0 on success and other values indicate failure.
  8143. */
  8144. static int TLSX_PreSharedKey_New(PreSharedKey** list, const byte* identity,
  8145. word16 len, void *heap,
  8146. PreSharedKey** preSharedKey)
  8147. {
  8148. PreSharedKey* psk;
  8149. PreSharedKey** next;
  8150. psk = (PreSharedKey*)XMALLOC(sizeof(PreSharedKey), heap, DYNAMIC_TYPE_TLSX);
  8151. if (psk == NULL)
  8152. return MEMORY_E;
  8153. XMEMSET(psk, 0, sizeof(*psk));
  8154. /* Make a copy of the identity data. */
  8155. psk->identity = (byte*)XMALLOC(len, heap, DYNAMIC_TYPE_TLSX);
  8156. if (psk->identity == NULL) {
  8157. XFREE(psk, heap, DYNAMIC_TYPE_TLSX);
  8158. return MEMORY_E;
  8159. }
  8160. XMEMCPY(psk->identity, identity, len);
  8161. psk->identityLen = len;
  8162. /* Add it to the end and maintain the links. */
  8163. while (*list != NULL) {
  8164. /* Assign to temporary to work around compiler bug found by customer. */
  8165. next = &((*list)->next);
  8166. list = next;
  8167. }
  8168. *list = psk;
  8169. *preSharedKey = psk;
  8170. (void)heap;
  8171. return 0;
  8172. }
  8173. static WC_INLINE byte GetHmacLength(int hmac)
  8174. {
  8175. switch (hmac) {
  8176. #ifndef NO_SHA256
  8177. case sha256_mac:
  8178. return WC_SHA256_DIGEST_SIZE;
  8179. #endif
  8180. #ifdef WOLFSSL_SHA384
  8181. case sha384_mac:
  8182. return WC_SHA384_DIGEST_SIZE;
  8183. #endif
  8184. #ifdef WOLFSSL_SHA512
  8185. case sha512_mac:
  8186. return WC_SHA512_DIGEST_SIZE;
  8187. #endif
  8188. }
  8189. return 0;
  8190. }
  8191. /* Use the data to create a new pre-shared key object in the extensions.
  8192. *
  8193. * ssl The SSL/TLS object.
  8194. * identity The identity.
  8195. * len The length of the identity data.
  8196. * age The age of the identity.
  8197. * hmac The HMAC algorithm.
  8198. * ciphersuite0 The first byte of the ciphersuite to use.
  8199. * ciphersuite The second byte of the ciphersuite to use.
  8200. * resumption The PSK is for resumption of a session.
  8201. * preSharedKey The new pre-shared key object.
  8202. * returns 0 on success and other values indicate failure.
  8203. */
  8204. int TLSX_PreSharedKey_Use(WOLFSSL* ssl, const byte* identity, word16 len,
  8205. word32 age, byte hmac, byte cipherSuite0,
  8206. byte cipherSuite, byte resumption,
  8207. PreSharedKey **preSharedKey)
  8208. {
  8209. int ret = 0;
  8210. TLSX* extension;
  8211. PreSharedKey* psk = NULL;
  8212. /* Find the pre-shared key extension if it exists. */
  8213. extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
  8214. if (extension == NULL) {
  8215. /* Push new pre-shared key extension. */
  8216. ret = TLSX_Push(&ssl->extensions, TLSX_PRE_SHARED_KEY, NULL, ssl->heap);
  8217. if (ret != 0)
  8218. return ret;
  8219. extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
  8220. if (extension == NULL)
  8221. return MEMORY_E;
  8222. }
  8223. /* Try to find the pre-shared key with this identity. */
  8224. psk = (PreSharedKey*)extension->data;
  8225. while (psk != NULL) {
  8226. if ((psk->identityLen == len) &&
  8227. (XMEMCMP(psk->identity, identity, len) == 0)) {
  8228. break;
  8229. }
  8230. psk = psk->next;
  8231. }
  8232. /* Create a new pre-shared key object if not found. */
  8233. if (psk == NULL) {
  8234. ret = TLSX_PreSharedKey_New((PreSharedKey**)&extension->data, identity,
  8235. len, ssl->heap, &psk);
  8236. if (ret != 0)
  8237. return ret;
  8238. }
  8239. /* Update/set age and HMAC algorithm. */
  8240. psk->ticketAge = age;
  8241. psk->hmac = hmac;
  8242. psk->cipherSuite0 = cipherSuite0;
  8243. psk->cipherSuite = cipherSuite;
  8244. psk->resumption = resumption;
  8245. psk->binderLen = GetHmacLength(psk->hmac);
  8246. if (preSharedKey != NULL)
  8247. *preSharedKey = psk;
  8248. return 0;
  8249. }
  8250. #define PSK_FREE_ALL TLSX_PreSharedKey_FreeAll
  8251. #define PSK_GET_SIZE TLSX_PreSharedKey_GetSize
  8252. #define PSK_WRITE TLSX_PreSharedKey_Write
  8253. #define PSK_PARSE TLSX_PreSharedKey_Parse
  8254. #else
  8255. #define PSK_FREE_ALL(a, b)
  8256. #define PSK_GET_SIZE(a, b, c) 0
  8257. #define PSK_WRITE(a, b, c, d) 0
  8258. #define PSK_PARSE(a, b, c, d) 0
  8259. #endif
  8260. /******************************************************************************/
  8261. /* PSK Key Exchange Modes */
  8262. /******************************************************************************/
  8263. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  8264. /* Get the size of the encoded PSK KE modes extension.
  8265. * Only in ClientHello.
  8266. *
  8267. * modes The PSK KE mode bit string.
  8268. * msgType The type of the message this extension is being written into.
  8269. * returns the number of bytes of the encoded PSK KE mode extension.
  8270. */
  8271. static int TLSX_PskKeModes_GetSize(byte modes, byte msgType, word16* pSz)
  8272. {
  8273. if (msgType == client_hello) {
  8274. /* Format: Len | Modes* */
  8275. word16 len = OPAQUE8_LEN;
  8276. /* Check whether each possible mode is to be written. */
  8277. if (modes & (1 << PSK_KE))
  8278. len += OPAQUE8_LEN;
  8279. if (modes & (1 << PSK_DHE_KE))
  8280. len += OPAQUE8_LEN;
  8281. *pSz += len;
  8282. return 0;
  8283. }
  8284. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8285. return SANITY_MSG_E;
  8286. }
  8287. /* Writes the PSK KE modes extension into the output buffer.
  8288. * Assumes that the the output buffer is big enough to hold data.
  8289. * Only in ClientHello.
  8290. *
  8291. * modes The PSK KE mode bit string.
  8292. * output The buffer to write into.
  8293. * msgType The type of the message this extension is being written into.
  8294. * returns the number of bytes written into the buffer.
  8295. */
  8296. static int TLSX_PskKeModes_Write(byte modes, byte* output, byte msgType,
  8297. word16* pSz)
  8298. {
  8299. if (msgType == client_hello) {
  8300. /* Format: Len | Modes* */
  8301. word16 idx = OPAQUE8_LEN;
  8302. /* Write out each possible mode. */
  8303. if (modes & (1 << PSK_KE))
  8304. output[idx++] = PSK_KE;
  8305. if (modes & (1 << PSK_DHE_KE))
  8306. output[idx++] = PSK_DHE_KE;
  8307. /* Write out length of mode list. */
  8308. output[0] = (byte)(idx - OPAQUE8_LEN);
  8309. *pSz += idx;
  8310. return 0;
  8311. }
  8312. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8313. return SANITY_MSG_E;
  8314. }
  8315. /* Parse the PSK KE modes extension.
  8316. * Only in ClientHello.
  8317. *
  8318. * ssl The SSL/TLS object.
  8319. * input The extension data.
  8320. * length The length of the extension data.
  8321. * msgType The type of the message this extension is being parsed from.
  8322. * returns 0 on success and other values indicate failure.
  8323. */
  8324. static int TLSX_PskKeModes_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  8325. byte msgType)
  8326. {
  8327. int ret;
  8328. if (msgType == client_hello) {
  8329. /* Format: Len | Modes* */
  8330. int idx = 0;
  8331. word16 len;
  8332. byte modes = 0;
  8333. /* Ensure length byte exists. */
  8334. if (length < OPAQUE8_LEN)
  8335. return BUFFER_E;
  8336. /* Get length of mode list and ensure that is the only data. */
  8337. len = input[0];
  8338. if (length - OPAQUE8_LEN != len)
  8339. return BUFFER_E;
  8340. idx = OPAQUE8_LEN;
  8341. /* Set a bit for each recognized modes. */
  8342. while (len > 0) {
  8343. /* Ignore unrecognized modes. */
  8344. if (input[idx] <= PSK_DHE_KE)
  8345. modes |= 1 << input[idx];
  8346. idx++;
  8347. len--;
  8348. }
  8349. ret = TLSX_PskKeModes_Use(ssl, modes);
  8350. if (ret != 0)
  8351. return ret;
  8352. return 0;
  8353. }
  8354. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8355. return SANITY_MSG_E;
  8356. }
  8357. /* Use the data to create a new PSK Key Exchange Modes object in the extensions.
  8358. *
  8359. * ssl The SSL/TLS object.
  8360. * modes The PSK key exchange modes.
  8361. * returns 0 on success and other values indicate failure.
  8362. */
  8363. int TLSX_PskKeModes_Use(WOLFSSL* ssl, byte modes)
  8364. {
  8365. int ret = 0;
  8366. TLSX* extension;
  8367. /* Find the PSK key exchange modes extension if it exists. */
  8368. extension = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES);
  8369. if (extension == NULL) {
  8370. /* Push new PSK key exchange modes extension. */
  8371. ret = TLSX_Push(&ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES, NULL,
  8372. ssl->heap);
  8373. if (ret != 0)
  8374. return ret;
  8375. extension = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES);
  8376. if (extension == NULL)
  8377. return MEMORY_E;
  8378. }
  8379. extension->val = modes;
  8380. return 0;
  8381. }
  8382. #define PKM_GET_SIZE TLSX_PskKeModes_GetSize
  8383. #define PKM_WRITE TLSX_PskKeModes_Write
  8384. #define PKM_PARSE TLSX_PskKeModes_Parse
  8385. #else
  8386. #define PKM_GET_SIZE(a, b, c) 0
  8387. #define PKM_WRITE(a, b, c, d) 0
  8388. #define PKM_PARSE(a, b, c, d) 0
  8389. #endif
  8390. /******************************************************************************/
  8391. /* Post-Handshake Authentication */
  8392. /******************************************************************************/
  8393. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  8394. /* Get the size of the encoded Post-Handshake Authentication extension.
  8395. * Only in ClientHello.
  8396. *
  8397. * msgType The type of the message this extension is being written into.
  8398. * returns the number of bytes of the encoded Post-Handshake Authentication
  8399. * extension.
  8400. */
  8401. static int TLSX_PostHandAuth_GetSize(byte msgType, word16* pSz)
  8402. {
  8403. if (msgType == client_hello) {
  8404. *pSz += 0;
  8405. return 0;
  8406. }
  8407. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8408. return SANITY_MSG_E;
  8409. }
  8410. /* Writes the Post-Handshake Authentication extension into the output buffer.
  8411. * Assumes that the the output buffer is big enough to hold data.
  8412. * Only in ClientHello.
  8413. *
  8414. * output The buffer to write into.
  8415. * msgType The type of the message this extension is being written into.
  8416. * returns the number of bytes written into the buffer.
  8417. */
  8418. static int TLSX_PostHandAuth_Write(byte* output, byte msgType, word16* pSz)
  8419. {
  8420. (void)output;
  8421. if (msgType == client_hello) {
  8422. *pSz += 0;
  8423. return 0;
  8424. }
  8425. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8426. return SANITY_MSG_E;
  8427. }
  8428. /* Parse the Post-Handshake Authentication extension.
  8429. * Only in ClientHello.
  8430. *
  8431. * ssl The SSL/TLS object.
  8432. * input The extension data.
  8433. * length The length of the extension data.
  8434. * msgType The type of the message this extension is being parsed from.
  8435. * returns 0 on success and other values indicate failure.
  8436. */
  8437. static int TLSX_PostHandAuth_Parse(WOLFSSL* ssl, const byte* input,
  8438. word16 length, byte msgType)
  8439. {
  8440. (void)input;
  8441. if (msgType == client_hello) {
  8442. /* Ensure extension is empty. */
  8443. if (length != 0)
  8444. return BUFFER_E;
  8445. ssl->options.postHandshakeAuth = 1;
  8446. return 0;
  8447. }
  8448. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8449. return SANITY_MSG_E;
  8450. }
  8451. /* Create a new Post-handshake authentication object in the extensions.
  8452. *
  8453. * ssl The SSL/TLS object.
  8454. * returns 0 on success and other values indicate failure.
  8455. */
  8456. static int TLSX_PostHandAuth_Use(WOLFSSL* ssl)
  8457. {
  8458. int ret = 0;
  8459. TLSX* extension;
  8460. /* Find the PSK key exchange modes extension if it exists. */
  8461. extension = TLSX_Find(ssl->extensions, TLSX_POST_HANDSHAKE_AUTH);
  8462. if (extension == NULL) {
  8463. /* Push new Post-handshake Authentication extension. */
  8464. ret = TLSX_Push(&ssl->extensions, TLSX_POST_HANDSHAKE_AUTH, NULL,
  8465. ssl->heap);
  8466. if (ret != 0)
  8467. return ret;
  8468. }
  8469. return 0;
  8470. }
  8471. #define PHA_GET_SIZE TLSX_PostHandAuth_GetSize
  8472. #define PHA_WRITE TLSX_PostHandAuth_Write
  8473. #define PHA_PARSE TLSX_PostHandAuth_Parse
  8474. #else
  8475. #define PHA_GET_SIZE(a, b) 0
  8476. #define PHA_WRITE(a, b, c) 0
  8477. #define PHA_PARSE(a, b, c, d) 0
  8478. #endif
  8479. /******************************************************************************/
  8480. /* Early Data Indication */
  8481. /******************************************************************************/
  8482. #ifdef WOLFSSL_EARLY_DATA
  8483. /* Get the size of the encoded Early Data Indication extension.
  8484. * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
  8485. *
  8486. * msgType The type of the message this extension is being written into.
  8487. * returns the number of bytes of the encoded Early Data Indication extension.
  8488. */
  8489. static int TLSX_EarlyData_GetSize(byte msgType, word16* pSz)
  8490. {
  8491. int ret = 0;
  8492. if (msgType == client_hello || msgType == encrypted_extensions)
  8493. *pSz += 0;
  8494. else if (msgType == session_ticket)
  8495. *pSz += OPAQUE32_LEN;
  8496. else {
  8497. ret = SANITY_MSG_E;
  8498. WOLFSSL_ERROR_VERBOSE(ret);
  8499. }
  8500. return ret;
  8501. }
  8502. /* Writes the Early Data Indicator extension into the output buffer.
  8503. * Assumes that the the output buffer is big enough to hold data.
  8504. * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
  8505. *
  8506. * maxSz The maximum early data size.
  8507. * output The buffer to write into.
  8508. * msgType The type of the message this extension is being written into.
  8509. * returns the number of bytes written into the buffer.
  8510. */
  8511. static int TLSX_EarlyData_Write(word32 maxSz, byte* output, byte msgType,
  8512. word16* pSz)
  8513. {
  8514. if (msgType == client_hello || msgType == encrypted_extensions)
  8515. return 0;
  8516. else if (msgType == session_ticket) {
  8517. c32toa(maxSz, output);
  8518. *pSz += OPAQUE32_LEN;
  8519. return 0;
  8520. }
  8521. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8522. return SANITY_MSG_E;
  8523. }
  8524. /* Parse the Early Data Indicator extension.
  8525. * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
  8526. *
  8527. * ssl The SSL/TLS object.
  8528. * input The extension data.
  8529. * length The length of the extension data.
  8530. * msgType The type of the message this extension is being parsed from.
  8531. * returns 0 on success and other values indicate failure.
  8532. */
  8533. static int TLSX_EarlyData_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  8534. byte msgType)
  8535. {
  8536. WOLFSSL_ENTER("TLSX_EarlyData_Parse");
  8537. if (msgType == client_hello) {
  8538. if (length != 0)
  8539. return BUFFER_E;
  8540. if (ssl->earlyData == expecting_early_data) {
  8541. if (ssl->options.maxEarlyDataSz != 0)
  8542. ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_ACCEPTED;
  8543. else
  8544. ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_REJECTED;
  8545. return TLSX_EarlyData_Use(ssl, 0, 0);
  8546. }
  8547. ssl->earlyData = early_data_ext;
  8548. return 0;
  8549. }
  8550. if (msgType == encrypted_extensions) {
  8551. if (length != 0)
  8552. return BUFFER_E;
  8553. /* Ensure the index of PSK identity chosen by server is 0.
  8554. * Index is plus one to handle 'not set' value of 0.
  8555. */
  8556. if (ssl->options.pskIdIndex != 1) {
  8557. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  8558. return PSK_KEY_ERROR;
  8559. }
  8560. if (ssl->options.side == WOLFSSL_CLIENT_END) {
  8561. /* the extension from server comes in */
  8562. ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_ACCEPTED;
  8563. }
  8564. return TLSX_EarlyData_Use(ssl, 1, 1);
  8565. }
  8566. if (msgType == session_ticket) {
  8567. word32 maxSz;
  8568. if (length != OPAQUE32_LEN)
  8569. return BUFFER_E;
  8570. ato32(input, &maxSz);
  8571. ssl->session->maxEarlyDataSz = maxSz;
  8572. return 0;
  8573. }
  8574. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8575. return SANITY_MSG_E;
  8576. }
  8577. /* Use the data to create a new Early Data object in the extensions.
  8578. *
  8579. * ssl The SSL/TLS object.
  8580. * maxSz The maximum early data size.
  8581. * is_response if this extension is part of a response
  8582. * returns 0 on success and other values indicate failure.
  8583. */
  8584. int TLSX_EarlyData_Use(WOLFSSL* ssl, word32 maxSz, int is_response)
  8585. {
  8586. int ret = 0;
  8587. TLSX* extension;
  8588. /* Find the early data extension if it exists. */
  8589. extension = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA);
  8590. if (extension == NULL) {
  8591. /* Push new early data extension. */
  8592. ret = TLSX_Push(&ssl->extensions, TLSX_EARLY_DATA, NULL, ssl->heap);
  8593. if (ret != 0)
  8594. return ret;
  8595. extension = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA);
  8596. if (extension == NULL)
  8597. return MEMORY_E;
  8598. }
  8599. extension->resp = is_response;
  8600. /* In QUIC, earlydata size is either 0 or 0xffffffff.
  8601. * Override any size between, possibly left from our initial value */
  8602. extension->val = (WOLFSSL_IS_QUIC(ssl) && is_response && maxSz > 0) ?
  8603. WOLFSSL_MAX_32BIT : maxSz;
  8604. return 0;
  8605. }
  8606. #define EDI_GET_SIZE TLSX_EarlyData_GetSize
  8607. #define EDI_WRITE TLSX_EarlyData_Write
  8608. #define EDI_PARSE TLSX_EarlyData_Parse
  8609. #else
  8610. #define EDI_GET_SIZE(a, b) 0
  8611. #define EDI_WRITE(a, b, c, d) 0
  8612. #define EDI_PARSE(a, b, c, d) 0
  8613. #endif
  8614. /******************************************************************************/
  8615. /* QUIC transport parameter extension */
  8616. /******************************************************************************/
  8617. #ifdef WOLFSSL_QUIC
  8618. static word16 TLSX_QuicTP_GetSize(TLSX* extension)
  8619. {
  8620. const QuicTransportParam *tp = (QuicTransportParam*)extension->data;
  8621. return tp ? tp->len : 0;
  8622. }
  8623. int TLSX_QuicTP_Use(WOLFSSL* ssl, TLSX_Type ext_type, int is_response)
  8624. {
  8625. int ret = 0;
  8626. TLSX* extension;
  8627. WOLFSSL_ENTER("TLSX_QuicTP_Use");
  8628. if (ssl->quic.transport_local == NULL) {
  8629. /* RFC9000, ch 7.3: "An endpoint MUST treat the absence of [...]
  8630. * from either endpoint [...] as a connection error of type
  8631. * TRANSPORT_PARAMETER_ERROR."
  8632. */
  8633. ret = QUIC_TP_MISSING_E;
  8634. goto cleanup;
  8635. }
  8636. extension = TLSX_Find(ssl->extensions, ext_type);
  8637. if (extension == NULL) {
  8638. ret = TLSX_Push(&ssl->extensions, ext_type, NULL, ssl->heap);
  8639. if (ret != 0)
  8640. goto cleanup;
  8641. extension = TLSX_Find(ssl->extensions, ext_type);
  8642. if (extension == NULL) {
  8643. ret = MEMORY_E;
  8644. goto cleanup;
  8645. }
  8646. }
  8647. if (extension->data) {
  8648. QuicTransportParam_free((QuicTransportParam*)extension->data, ssl->heap);
  8649. extension->data = NULL;
  8650. }
  8651. extension->resp = is_response;
  8652. extension->data = (void*)QuicTransportParam_dup(ssl->quic.transport_local, ssl->heap);
  8653. if (!extension->data) {
  8654. ret = MEMORY_E;
  8655. goto cleanup;
  8656. }
  8657. cleanup:
  8658. WOLFSSL_LEAVE("TLSX_QuicTP_Use", ret);
  8659. return ret;
  8660. }
  8661. static word16 TLSX_QuicTP_Write(QuicTransportParam *tp, byte* output)
  8662. {
  8663. word16 len = 0;
  8664. WOLFSSL_ENTER("TLSX_QuicTP_Write");
  8665. if (tp && tp->len) {
  8666. XMEMCPY(output, tp->data, tp->len);
  8667. len = tp->len;
  8668. }
  8669. WOLFSSL_LEAVE("TLSX_QuicTP_Write", len);
  8670. return len;
  8671. }
  8672. static int TLSX_QuicTP_Parse(WOLFSSL *ssl, const byte *input, size_t len, int ext_type, int msgType)
  8673. {
  8674. const QuicTransportParam *tp, **ptp;
  8675. (void)msgType;
  8676. tp = QuicTransportParam_new(input, len, ssl->heap);
  8677. if (!tp) {
  8678. return MEMORY_E;
  8679. }
  8680. ptp = (ext_type == TLSX_KEY_QUIC_TP_PARAMS_DRAFT) ?
  8681. &ssl->quic.transport_peer_draft : &ssl->quic.transport_peer;
  8682. if (*ptp) {
  8683. QTP_FREE(*ptp, ssl->heap);
  8684. }
  8685. *ptp = tp;
  8686. return 0;
  8687. }
  8688. #define QTP_GET_SIZE TLSX_QuicTP_GetSize
  8689. #define QTP_USE TLSX_QuicTP_Use
  8690. #define QTP_WRITE TLSX_QuicTP_Write
  8691. #define QTP_PARSE TLSX_QuicTP_Parse
  8692. #endif /* WOLFSSL_QUIC */
  8693. #if defined(WOLFSSL_DTLS_CID)
  8694. #define CID_GET_SIZE TLSX_ConnectionID_GetSize
  8695. #define CID_WRITE TLSX_ConnectionID_Write
  8696. #define CID_PARSE TLSX_ConnectionID_Parse
  8697. #define CID_FREE TLSX_ConnectionID_Free
  8698. #else
  8699. #define CID_GET_SIZE(a) 0
  8700. #define CID_WRITE(a, b) 0
  8701. #define CID_PARSE(a, b, c, d) 0
  8702. #define CID_FREE(a, b) 0
  8703. #endif /* defined(WOLFSSL_DTLS_CID) */
  8704. /******************************************************************************/
  8705. /* TLS Extensions Framework */
  8706. /******************************************************************************/
  8707. /** Finds an extension in the provided list. */
  8708. TLSX* TLSX_Find(TLSX* list, TLSX_Type type)
  8709. {
  8710. TLSX* extension = list;
  8711. while (extension && extension->type != type)
  8712. extension = extension->next;
  8713. return extension;
  8714. }
  8715. /** Remove an extension. */
  8716. void TLSX_Remove(TLSX** list, TLSX_Type type, void* heap)
  8717. {
  8718. TLSX* extension;
  8719. TLSX** next;
  8720. if (list == NULL)
  8721. return;
  8722. extension = *list;
  8723. next = list;
  8724. while (extension && extension->type != type) {
  8725. next = &extension->next;
  8726. extension = extension->next;
  8727. }
  8728. if (extension) {
  8729. *next = extension->next;
  8730. extension->next = NULL;
  8731. TLSX_FreeAll(extension, heap);
  8732. }
  8733. }
  8734. #if defined(HAVE_ECH)
  8735. #define GREASE_ECH_SIZE 160
  8736. #define MAX_PUBLIC_NAME_SZ 256
  8737. #define TLS_INFO_CONST_STRING "tls ech"
  8738. #define TLS_INFO_CONST_STRING_SZ 7
  8739. /* return status after setting up ech to write a grease ech */
  8740. static int TLSX_GreaseECH_Use(TLSX** extensions, void* heap, WC_RNG* rng)
  8741. {
  8742. int ret = 0;
  8743. WOLFSSL_ECH* ech;
  8744. if (extensions == NULL)
  8745. return BAD_FUNC_ARG;
  8746. ech = (WOLFSSL_ECH*)XMALLOC(sizeof(WOLFSSL_ECH), heap,
  8747. DYNAMIC_TYPE_TMP_BUFFER);
  8748. if (ech == NULL)
  8749. return MEMORY_E;
  8750. ForceZero(ech, sizeof(WOLFSSL_ECH));
  8751. ech->state = ECH_WRITE_GREASE;
  8752. /* 0 for outer */
  8753. ech->type = ECH_TYPE_OUTER;
  8754. /* kemId */
  8755. ech->kemId = DHKEM_X25519_HKDF_SHA256;
  8756. /* cipherSuite kdf */
  8757. ech->cipherSuite.kdfId = HKDF_SHA256;
  8758. /* cipherSuite aead */
  8759. ech->cipherSuite.aeadId = HPKE_AES_128_GCM;
  8760. /* random configId */
  8761. ret = wc_RNG_GenerateByte(rng, &(ech->configId));
  8762. /* curve25519 encLen */
  8763. ech->encLen = DHKEM_X25519_ENC_LEN;
  8764. if (ret == 0)
  8765. ret = TLSX_Push(extensions, TLSX_ECH, ech, heap);
  8766. if (ret != 0) {
  8767. XFREE(ech, heap, DYNAMIC_TYPE_TMP_BUFFER);
  8768. }
  8769. return ret;
  8770. }
  8771. /* return status after setting up ech to write real ech */
  8772. static int TLSX_ECH_Use(WOLFSSL_EchConfig* echConfig, TLSX** extensions,
  8773. void* heap, WC_RNG* rng)
  8774. {
  8775. int ret = 0;
  8776. int suiteIndex;
  8777. WOLFSSL_ECH* ech;
  8778. if (extensions == NULL)
  8779. return BAD_FUNC_ARG;
  8780. /* find a supported cipher suite */
  8781. suiteIndex = EchConfigGetSupportedCipherSuite(echConfig);
  8782. if (suiteIndex < 0)
  8783. return suiteIndex;
  8784. ech = (WOLFSSL_ECH*)XMALLOC(sizeof(WOLFSSL_ECH), heap,
  8785. DYNAMIC_TYPE_TMP_BUFFER);
  8786. if (ech == NULL)
  8787. return MEMORY_E;
  8788. ForceZero(ech, sizeof(WOLFSSL_ECH));
  8789. ech->state = ECH_WRITE_REAL;
  8790. ech->echConfig = echConfig;
  8791. /* 0 for outer */
  8792. ech->type = ECH_TYPE_OUTER;
  8793. /* kemId */
  8794. ech->kemId = echConfig->kemId;
  8795. /* cipherSuite kdf */
  8796. ech->cipherSuite.kdfId = echConfig->cipherSuites[suiteIndex].kdfId;
  8797. /* cipherSuite aead */
  8798. ech->cipherSuite.aeadId = echConfig->cipherSuites[suiteIndex].aeadId;
  8799. /* configId */
  8800. ech->configId = echConfig->configId;
  8801. /* encLen */
  8802. switch (echConfig->kemId)
  8803. {
  8804. case DHKEM_P256_HKDF_SHA256:
  8805. ech->encLen = DHKEM_P256_ENC_LEN;
  8806. break;
  8807. case DHKEM_P384_HKDF_SHA384:
  8808. ech->encLen = DHKEM_P384_ENC_LEN;
  8809. break;
  8810. case DHKEM_P521_HKDF_SHA512:
  8811. ech->encLen = DHKEM_P521_ENC_LEN;
  8812. break;
  8813. case DHKEM_X25519_HKDF_SHA256:
  8814. ech->encLen = DHKEM_X25519_ENC_LEN;
  8815. break;
  8816. case DHKEM_X448_HKDF_SHA512:
  8817. ech->encLen = DHKEM_X448_ENC_LEN;
  8818. break;
  8819. }
  8820. /* setup hpke */
  8821. ech->hpke = (Hpke*)XMALLOC(sizeof(Hpke), heap, DYNAMIC_TYPE_TMP_BUFFER);
  8822. if (ech->hpke == NULL) {
  8823. XFREE(ech, heap, DYNAMIC_TYPE_TMP_BUFFER);
  8824. return MEMORY_E;
  8825. }
  8826. ret = wc_HpkeInit(ech->hpke, ech->kemId, ech->cipherSuite.kdfId,
  8827. ech->cipherSuite.aeadId, heap);
  8828. /* setup the ephemeralKey */
  8829. if (ret == 0)
  8830. ret = wc_HpkeGenerateKeyPair(ech->hpke, &ech->ephemeralKey, rng);
  8831. if (ret == 0)
  8832. ret = TLSX_Push(extensions, TLSX_ECH, ech, heap);
  8833. if (ret != 0) {
  8834. XFREE(ech->hpke, heap, DYNAMIC_TYPE_TMP_BUFFER);
  8835. XFREE(ech, heap, DYNAMIC_TYPE_TMP_BUFFER);
  8836. }
  8837. return ret;
  8838. }
  8839. /* return status after setting up ech to read and decrypt */
  8840. static int TLSX_ServerECH_Use(TLSX** extensions, void* heap,
  8841. WOLFSSL_EchConfig* configs)
  8842. {
  8843. int ret;
  8844. WOLFSSL_ECH* ech;
  8845. TLSX* echX;
  8846. /* if we already have ech don't override it */
  8847. echX = TLSX_Find(*extensions, TLSX_ECH);
  8848. if (echX != NULL)
  8849. return 0;
  8850. if (extensions == NULL)
  8851. return BAD_FUNC_ARG;
  8852. ech = (WOLFSSL_ECH*)XMALLOC(sizeof(WOLFSSL_ECH), heap,
  8853. DYNAMIC_TYPE_TMP_BUFFER);
  8854. if (ech == NULL)
  8855. return MEMORY_E;
  8856. ForceZero(ech, sizeof(WOLFSSL_ECH));
  8857. ech->state = ECH_WRITE_NONE;
  8858. /* 0 for outer */
  8859. ech->type = ECH_TYPE_OUTER;
  8860. ech->echConfig = configs;
  8861. /* setup the rest of the settings when we receive ech from the client */
  8862. ret = TLSX_Push(extensions, TLSX_ECH, ech, heap);
  8863. if (ret != 0)
  8864. XFREE(ech, heap, DYNAMIC_TYPE_TMP_BUFFER);
  8865. return ret;
  8866. }
  8867. /* return length after writing the ech */
  8868. static int TLSX_ECH_Write(WOLFSSL_ECH* ech, byte* writeBuf, word16* offset)
  8869. {
  8870. int ret = 0;
  8871. int rngRet = -1;
  8872. word32 configsLen = 0;
  8873. void* ephemeralKey = NULL;
  8874. byte* writeBuf_p = writeBuf;
  8875. #ifdef WOLFSSL_SMALL_STACK
  8876. Hpke* hpke = NULL;
  8877. WC_RNG* rng = NULL;
  8878. #else
  8879. Hpke hpke[1];
  8880. WC_RNG rng[1];
  8881. #endif
  8882. WOLFSSL_MSG("TLSX_ECH_Write");
  8883. if (ech->state == ECH_WRITE_NONE || ech->state == ECH_PARSED_INTERNAL)
  8884. return 0;
  8885. if (ech->state == ECH_WRITE_RETRY_CONFIGS) {
  8886. /* get size then write */
  8887. ret = GetEchConfigsEx(ech->echConfig, NULL, &configsLen);
  8888. if (ret != LENGTH_ONLY_E)
  8889. return ret;
  8890. ret = GetEchConfigsEx(ech->echConfig, writeBuf, &configsLen);
  8891. if (ret != WOLFSSL_SUCCESS)
  8892. return ret;
  8893. return configsLen;
  8894. }
  8895. #ifdef WOLFSSL_SMALL_STACK
  8896. hpke = (Hpke*)XMALLOC(sizeof(Hpke), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8897. if (hpke == NULL)
  8898. return MEMORY_E;
  8899. rng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
  8900. if (rng == NULL) {
  8901. XFREE(hpke, NULL, DYNAMIC_TYPE_RNG);
  8902. return MEMORY_E;
  8903. }
  8904. #endif
  8905. /* type */
  8906. *writeBuf_p = ech->type;
  8907. writeBuf_p += sizeof(ech->type);
  8908. /* outer has body, inner does not */
  8909. if (ech->type == ECH_TYPE_OUTER) {
  8910. /* kdfId */
  8911. c16toa(ech->cipherSuite.kdfId, writeBuf_p);
  8912. writeBuf_p += sizeof(ech->cipherSuite.kdfId);
  8913. /* aeadId */
  8914. c16toa(ech->cipherSuite.aeadId, writeBuf_p);
  8915. writeBuf_p += sizeof(ech->cipherSuite.aeadId);
  8916. /* configId */
  8917. *writeBuf_p = ech->configId;
  8918. writeBuf_p += sizeof(ech->configId);
  8919. /* encLen */
  8920. c16toa(ech->encLen, writeBuf_p);
  8921. writeBuf_p += 2;
  8922. if (ech->state == ECH_WRITE_GREASE) {
  8923. /* hpke init */
  8924. ret = wc_HpkeInit(hpke, ech->kemId, ech->cipherSuite.kdfId,
  8925. ech->cipherSuite.aeadId, NULL);
  8926. if (ret == 0)
  8927. rngRet = ret = wc_InitRng(rng);
  8928. /* create the ephemeralKey */
  8929. if (ret == 0)
  8930. ret = wc_HpkeGenerateKeyPair(hpke, &ephemeralKey, rng);
  8931. /* enc */
  8932. if (ret == 0) {
  8933. ret = wc_HpkeSerializePublicKey(hpke, ephemeralKey, writeBuf_p,
  8934. &ech->encLen);
  8935. writeBuf_p += ech->encLen;
  8936. }
  8937. if (ret == 0) {
  8938. /* innerClientHelloLen */
  8939. c16toa(GREASE_ECH_SIZE + ((writeBuf_p + 2 - writeBuf) % 32),
  8940. writeBuf_p);
  8941. writeBuf_p += 2;
  8942. /* innerClientHello */
  8943. ret = wc_RNG_GenerateBlock(rng, writeBuf_p, GREASE_ECH_SIZE +
  8944. ((writeBuf_p - writeBuf) % 32));
  8945. writeBuf_p += GREASE_ECH_SIZE + ((writeBuf_p - writeBuf) % 32);
  8946. }
  8947. if (rngRet == 0)
  8948. wc_FreeRng(rng);
  8949. if (ephemeralKey != NULL)
  8950. wc_HpkeFreeKey(hpke, hpke->kem, ephemeralKey, hpke->heap);
  8951. }
  8952. else {
  8953. /* write enc to writeBuf_p */
  8954. ret = wc_HpkeSerializePublicKey(ech->hpke, ech->ephemeralKey,
  8955. writeBuf_p, &ech->encLen);
  8956. writeBuf_p += ech->encLen;
  8957. /* innerClientHelloLen */
  8958. c16toa(ech->innerClientHelloLen, writeBuf_p);
  8959. writeBuf_p += 2;
  8960. /* set payload offset for when we finalize */
  8961. ech->outerClientPayload = writeBuf_p;
  8962. /* write zeros for payload */
  8963. XMEMSET(writeBuf_p, 0, ech->innerClientHelloLen);
  8964. writeBuf_p += ech->innerClientHelloLen;
  8965. }
  8966. }
  8967. #ifdef WOLFSSL_SMALL_STACK
  8968. XFREE(hpke, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8969. XFREE(rng, NULL, DYNAMIC_TYPE_RNG);
  8970. #endif
  8971. if (ret == 0)
  8972. *offset += (writeBuf_p - writeBuf);
  8973. return ret;
  8974. }
  8975. /* return the size needed for the ech extension */
  8976. static int TLSX_ECH_GetSize(WOLFSSL_ECH* ech)
  8977. {
  8978. int ret;
  8979. word32 size;
  8980. if (ech->state == ECH_WRITE_GREASE) {
  8981. size = sizeof(ech->type) + sizeof(ech->cipherSuite) +
  8982. sizeof(ech->configId) + sizeof(word16) + ech->encLen +
  8983. sizeof(word16);
  8984. size += GREASE_ECH_SIZE + (size % 32);
  8985. }
  8986. else if (ech->state == ECH_WRITE_NONE ||
  8987. ech->state == ECH_PARSED_INTERNAL) {
  8988. size = 0;
  8989. }
  8990. else if (ech->state == ECH_WRITE_RETRY_CONFIGS) {
  8991. /* get the size of the raw configs */
  8992. ret = GetEchConfigsEx(ech->echConfig, NULL, &size);
  8993. if (ret != LENGTH_ONLY_E)
  8994. return ret;
  8995. }
  8996. else if (ech->type == ECH_TYPE_INNER)
  8997. {
  8998. size = sizeof(ech->type);
  8999. }
  9000. else
  9001. {
  9002. size = sizeof(ech->type) + sizeof(ech->cipherSuite) +
  9003. sizeof(ech->configId) + sizeof(word16) + ech->encLen +
  9004. sizeof(word16) + ech->innerClientHelloLen;
  9005. }
  9006. return (int)size;
  9007. }
  9008. /* return status after attempting to open the hpke encrypted ech extension, if
  9009. * successful the inner client hello will be stored in
  9010. * ech->innerClientHelloLen */
  9011. static int TLSX_ExtractEch(WOLFSSL_ECH* ech, WOLFSSL_EchConfig* echConfig,
  9012. byte* aad, word32 aadLen, void* heap)
  9013. {
  9014. int ret = 0;
  9015. int expectedEncLen;
  9016. int i;
  9017. word32 rawConfigLen = 0;
  9018. byte* info = NULL;
  9019. word32 infoLen = 0;
  9020. if (ech == NULL || echConfig == NULL || aad == NULL)
  9021. return BAD_FUNC_ARG;
  9022. /* verify the kem and key len */
  9023. switch (echConfig->kemId)
  9024. {
  9025. case DHKEM_P256_HKDF_SHA256:
  9026. expectedEncLen = DHKEM_P256_ENC_LEN;
  9027. break;
  9028. case DHKEM_P384_HKDF_SHA384:
  9029. expectedEncLen = DHKEM_P384_ENC_LEN;
  9030. break;
  9031. case DHKEM_P521_HKDF_SHA512:
  9032. expectedEncLen = DHKEM_P521_ENC_LEN;
  9033. break;
  9034. case DHKEM_X25519_HKDF_SHA256:
  9035. expectedEncLen = DHKEM_X25519_ENC_LEN;
  9036. break;
  9037. case DHKEM_X448_HKDF_SHA512:
  9038. expectedEncLen = DHKEM_X448_ENC_LEN;
  9039. break;
  9040. default:
  9041. expectedEncLen = 0;
  9042. break;
  9043. }
  9044. if (expectedEncLen != ech->encLen)
  9045. return BAD_FUNC_ARG;
  9046. /* verify the cipher suite */
  9047. for (i = 0; i < echConfig->numCipherSuites; i++) {
  9048. if (echConfig->cipherSuites[i].kdfId == ech->cipherSuite.kdfId &&
  9049. echConfig->cipherSuites[i].aeadId == ech->cipherSuite.aeadId) {
  9050. break;
  9051. }
  9052. }
  9053. if (i >= echConfig->numCipherSuites) {
  9054. return BAD_FUNC_ARG;
  9055. }
  9056. ech->hpke = (Hpke*)XMALLOC(sizeof(Hpke), heap, DYNAMIC_TYPE_TMP_BUFFER);
  9057. if (ech->hpke == NULL)
  9058. return MEMORY_E;
  9059. ret = wc_HpkeInit(ech->hpke, echConfig->kemId, ech->cipherSuite.kdfId,
  9060. ech->cipherSuite.aeadId, heap);
  9061. /* get the rawConfigLen */
  9062. if (ret == 0)
  9063. ret = GetEchConfig(echConfig, NULL, &rawConfigLen);
  9064. if (ret == LENGTH_ONLY_E)
  9065. ret = 0;
  9066. /* create info */
  9067. if (ret == 0) {
  9068. infoLen = TLS_INFO_CONST_STRING_SZ + 1 + rawConfigLen;
  9069. info = (byte*)XMALLOC(infoLen, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9070. if (info == NULL)
  9071. ret = MEMORY_E;
  9072. else {
  9073. XMEMCPY(info, (byte*)TLS_INFO_CONST_STRING,
  9074. TLS_INFO_CONST_STRING_SZ + 1);
  9075. ret = GetEchConfig(echConfig, info +
  9076. TLS_INFO_CONST_STRING_SZ + 1, &rawConfigLen);
  9077. }
  9078. }
  9079. /* decrypt the ech payload */
  9080. if (ret == 0)
  9081. ret = wc_HpkeOpenBase(ech->hpke, echConfig->receiverPrivkey, ech->enc,
  9082. ech->encLen, info, infoLen, aad, aadLen, ech->outerClientPayload,
  9083. ech->innerClientHelloLen,
  9084. ech->innerClientHello + HANDSHAKE_HEADER_SZ);
  9085. if (ret != 0) {
  9086. XFREE(ech->hpke, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9087. ech->hpke = NULL;
  9088. }
  9089. if (info != NULL)
  9090. XFREE(info, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9091. return ret;
  9092. }
  9093. /* parse the ech extension, if internal update ech->state and return, if
  9094. * external attempt to extract the inner client_hello, return the status */
  9095. static int TLSX_ECH_Parse(WOLFSSL* ssl, const byte* readBuf, word16 size,
  9096. byte msgType)
  9097. {
  9098. int ret = 0;
  9099. int i;
  9100. TLSX* echX;
  9101. WOLFSSL_ECH* ech;
  9102. WOLFSSL_EchConfig* echConfig;
  9103. byte* aadCopy;
  9104. byte* readBuf_p = (byte*)readBuf;
  9105. WOLFSSL_MSG("TLSX_ECH_Parse");
  9106. if (size == 0)
  9107. return BAD_FUNC_ARG;
  9108. if (msgType == encrypted_extensions) {
  9109. ret = wolfSSL_SetEchConfigs(ssl, readBuf, size);
  9110. if (ret == WOLFSSL_SUCCESS)
  9111. ret = 0;
  9112. }
  9113. else if (msgType == client_hello && ssl->ctx->echConfigs != NULL) {
  9114. echX = TLSX_Find(ssl->extensions, TLSX_ECH);
  9115. if (echX == NULL)
  9116. return BAD_FUNC_ARG;
  9117. ech = (WOLFSSL_ECH*)echX->data;
  9118. /* read the ech parameters before the payload */
  9119. ech->type = *readBuf_p;
  9120. readBuf_p++;
  9121. if (ech->type == ECH_TYPE_INNER) {
  9122. ech->state = ECH_PARSED_INTERNAL;
  9123. return 0;
  9124. }
  9125. /* technically the payload would only be 1 byte at this length */
  9126. if (size < 11 + ech->encLen)
  9127. return BAD_FUNC_ARG;
  9128. ato16(readBuf_p, &ech->cipherSuite.kdfId);
  9129. readBuf_p += 2;
  9130. ato16(readBuf_p, &ech->cipherSuite.aeadId);
  9131. readBuf_p += 2;
  9132. ech->configId = *readBuf_p;
  9133. readBuf_p++;
  9134. ato16(readBuf_p, &ech->encLen);
  9135. readBuf_p += 2;
  9136. if (ech->encLen > HPKE_Npk_MAX)
  9137. return BAD_FUNC_ARG;
  9138. XMEMCPY(ech->enc, readBuf_p, ech->encLen);
  9139. readBuf_p += ech->encLen;
  9140. ato16(readBuf_p, &ech->innerClientHelloLen);
  9141. ech->innerClientHelloLen -= AES_BLOCK_SIZE;
  9142. readBuf_p += 2;
  9143. ech->outerClientPayload = readBuf_p;
  9144. /* make a copy of the aad */
  9145. aadCopy = (byte*)XMALLOC(ech->aadLen, ssl->heap,
  9146. DYNAMIC_TYPE_TMP_BUFFER);
  9147. if (aadCopy == NULL)
  9148. return MEMORY_E;
  9149. XMEMCPY(aadCopy, ech->aad, ech->aadLen);
  9150. /* set the ech payload of the copy to zeros */
  9151. XMEMSET(aadCopy + (readBuf_p - ech->aad), 0,
  9152. ech->innerClientHelloLen + AES_BLOCK_SIZE);
  9153. /* allocate the inner payload buffer */
  9154. ech->innerClientHello =
  9155. (byte*)XMALLOC(ech->innerClientHelloLen + HANDSHAKE_HEADER_SZ,
  9156. ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  9157. if (ech->innerClientHello == NULL) {
  9158. XFREE(aadCopy, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  9159. return MEMORY_E;
  9160. }
  9161. /* first check if the config id matches */
  9162. echConfig = ssl->ctx->echConfigs;
  9163. while (echConfig != NULL) {
  9164. /* decrypt with this config */
  9165. if (echConfig->configId == ech->configId) {
  9166. ret = TLSX_ExtractEch(ech, echConfig, aadCopy, ech->aadLen,
  9167. ssl->heap);
  9168. break;
  9169. }
  9170. echConfig = echConfig->next;
  9171. }
  9172. /* try to decrypt with all configs */
  9173. if (echConfig == NULL || ret != 0) {
  9174. echConfig = ssl->ctx->echConfigs;
  9175. while (echConfig != NULL) {
  9176. ret = TLSX_ExtractEch(ech, echConfig, aadCopy, ech->aadLen,
  9177. ssl->heap);
  9178. if (ret== 0)
  9179. break;
  9180. echConfig = echConfig->next;
  9181. }
  9182. }
  9183. /* if we failed to extract */
  9184. if (ret != 0) {
  9185. XFREE(ech->innerClientHello, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  9186. ech->innerClientHello = NULL;
  9187. ech->state = ECH_WRITE_RETRY_CONFIGS;
  9188. }
  9189. else {
  9190. i = 0;
  9191. /* decrement until before the padding */
  9192. while (ech->innerClientHello[ech->innerClientHelloLen +
  9193. HANDSHAKE_HEADER_SZ - i - 1] != ECH_TYPE_INNER) {
  9194. i++;
  9195. }
  9196. /* subtract the length of the padding from the length */
  9197. ech->innerClientHelloLen -= i;
  9198. }
  9199. XFREE(aadCopy, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  9200. return 0;
  9201. }
  9202. return ret;
  9203. }
  9204. /* free the ech struct and the dynamic buffer it uses */
  9205. static void TLSX_ECH_Free(WOLFSSL_ECH* ech, void* heap)
  9206. {
  9207. if (ech->innerClientHello != NULL)
  9208. XFREE(ech->innerClientHello, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9209. if (ech->ephemeralKey != NULL)
  9210. wc_HpkeFreeKey(ech->hpke, ech->hpke->kem, ech->ephemeralKey,
  9211. ech->hpke->heap);
  9212. if (ech->hpke != NULL)
  9213. XFREE(ech->hpke, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9214. XFREE(ech, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9215. (void)heap;
  9216. }
  9217. /* encrypt the client hello and store it in ech->outerClientPayload, return
  9218. * status */
  9219. int TLSX_FinalizeEch(WOLFSSL_ECH* ech, byte* aad, word32 aadLen)
  9220. {
  9221. int ret;
  9222. void* receiverPubkey = NULL;
  9223. byte* info;
  9224. int infoLen;
  9225. byte* aadCopy;
  9226. /* import the server public key */
  9227. ret = wc_HpkeDeserializePublicKey(ech->hpke, &receiverPubkey,
  9228. ech->echConfig->receiverPubkey, ech->encLen);
  9229. if (ret == 0) {
  9230. /* create info */
  9231. infoLen = TLS_INFO_CONST_STRING_SZ + 1 + ech->echConfig->rawLen;
  9232. info = (byte*)XMALLOC(infoLen, ech->hpke->heap,
  9233. DYNAMIC_TYPE_TMP_BUFFER);
  9234. if (info == NULL)
  9235. ret = MEMORY_E;
  9236. if (ret == 0) {
  9237. /* puts the null byte in for me */
  9238. XMEMCPY(info, (byte*)TLS_INFO_CONST_STRING, TLS_INFO_CONST_STRING_SZ
  9239. + 1);
  9240. XMEMCPY(info + TLS_INFO_CONST_STRING_SZ + 1, ech->echConfig->raw,
  9241. ech->echConfig->rawLen);
  9242. /* make a copy of the aad since we overwrite it */
  9243. aadCopy = (byte*)XMALLOC(aadLen, ech->hpke->heap,
  9244. DYNAMIC_TYPE_TMP_BUFFER);
  9245. if (aadCopy == NULL) {
  9246. XFREE(info, ech->hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  9247. ret = MEMORY_E;
  9248. }
  9249. }
  9250. if (ret == 0) {
  9251. XMEMCPY(aadCopy, aad, aadLen);
  9252. /* seal the payload */
  9253. ret = wc_HpkeSealBase(ech->hpke, ech->ephemeralKey, receiverPubkey,
  9254. info, infoLen, aadCopy, aadLen, ech->innerClientHello,
  9255. ech->innerClientHelloLen - ech->hpke->Nt,
  9256. ech->outerClientPayload);
  9257. XFREE(info, ech->hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  9258. XFREE(aadCopy, ech->hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  9259. }
  9260. }
  9261. if (receiverPubkey != NULL)
  9262. wc_HpkeFreeKey(ech->hpke, ech->hpke->kem, receiverPubkey,
  9263. ech->hpke->heap);
  9264. return ret;
  9265. }
  9266. #define GREASE_ECH_USE TLSX_GreaseECH_Use
  9267. #define ECH_USE TLSX_ECH_Use
  9268. #define SERVER_ECH_USE TLSX_ServerECH_Use
  9269. #define ECH_WRITE TLSX_ECH_Write
  9270. #define ECH_GET_SIZE TLSX_ECH_GetSize
  9271. #define ECH_PARSE TLSX_ECH_Parse
  9272. #define ECH_FREE TLSX_ECH_Free
  9273. #endif
  9274. /** Releases all extensions in the provided list. */
  9275. void TLSX_FreeAll(TLSX* list, void* heap)
  9276. {
  9277. TLSX* extension;
  9278. while ((extension = list)) {
  9279. list = extension->next;
  9280. switch (extension->type) {
  9281. #ifdef HAVE_SNI
  9282. case TLSX_SERVER_NAME:
  9283. SNI_FREE_ALL((SNI*)extension->data, heap);
  9284. break;
  9285. #endif
  9286. case TLSX_TRUSTED_CA_KEYS:
  9287. TCA_FREE_ALL((TCA*)extension->data, heap);
  9288. break;
  9289. case TLSX_MAX_FRAGMENT_LENGTH:
  9290. MFL_FREE_ALL(extension->data, heap);
  9291. break;
  9292. case TLSX_EXTENDED_MASTER_SECRET:
  9293. case TLSX_TRUNCATED_HMAC:
  9294. /* Nothing to do. */
  9295. break;
  9296. case TLSX_SUPPORTED_GROUPS:
  9297. EC_FREE_ALL((SupportedCurve*)extension->data, heap);
  9298. break;
  9299. case TLSX_EC_POINT_FORMATS:
  9300. PF_FREE_ALL((PointFormat*)extension->data, heap);
  9301. break;
  9302. case TLSX_STATUS_REQUEST:
  9303. CSR_FREE_ALL((CertificateStatusRequest*)extension->data, heap);
  9304. break;
  9305. case TLSX_STATUS_REQUEST_V2:
  9306. CSR2_FREE_ALL((CertificateStatusRequestItemV2*)extension->data,
  9307. heap);
  9308. break;
  9309. case TLSX_RENEGOTIATION_INFO:
  9310. SCR_FREE_ALL(extension->data, heap);
  9311. break;
  9312. case TLSX_SESSION_TICKET:
  9313. WOLF_STK_FREE(extension->data, heap);
  9314. break;
  9315. case TLSX_APPLICATION_LAYER_PROTOCOL:
  9316. ALPN_FREE_ALL((ALPN*)extension->data, heap);
  9317. break;
  9318. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9319. case TLSX_SIGNATURE_ALGORITHMS:
  9320. break;
  9321. #endif
  9322. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  9323. case TLSX_ENCRYPT_THEN_MAC:
  9324. break;
  9325. #endif
  9326. #ifdef WOLFSSL_TLS13
  9327. case TLSX_SUPPORTED_VERSIONS:
  9328. break;
  9329. #ifdef WOLFSSL_SEND_HRR_COOKIE
  9330. case TLSX_COOKIE:
  9331. CKE_FREE_ALL((Cookie*)extension->data, heap);
  9332. break;
  9333. #endif
  9334. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9335. case TLSX_PRE_SHARED_KEY:
  9336. PSK_FREE_ALL((PreSharedKey*)extension->data, heap);
  9337. break;
  9338. case TLSX_PSK_KEY_EXCHANGE_MODES:
  9339. break;
  9340. #endif
  9341. #ifdef WOLFSSL_EARLY_DATA
  9342. case TLSX_EARLY_DATA:
  9343. break;
  9344. #endif
  9345. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  9346. case TLSX_POST_HANDSHAKE_AUTH:
  9347. break;
  9348. #endif
  9349. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9350. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  9351. break;
  9352. #endif
  9353. case TLSX_KEY_SHARE:
  9354. KS_FREE_ALL((KeyShareEntry*)extension->data, heap);
  9355. break;
  9356. #endif
  9357. #ifdef WOLFSSL_SRTP
  9358. case TLSX_USE_SRTP:
  9359. SRTP_FREE((TlsxSrtp*)extension->data, heap);
  9360. break;
  9361. #endif
  9362. #ifdef WOLFSSL_QUIC
  9363. case TLSX_KEY_QUIC_TP_PARAMS:
  9364. FALL_THROUGH;
  9365. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  9366. QTP_FREE((QuicTransportParam*)extension->data, heap);
  9367. break;
  9368. #endif
  9369. #ifdef WOLFSSL_DTLS_CID
  9370. case TLSX_CONNECTION_ID:
  9371. CID_FREE((byte*)extension->data, heap);
  9372. break;
  9373. #endif /* WOLFSSL_DTLS_CID */
  9374. #if defined(HAVE_ECH)
  9375. case TLSX_ECH:
  9376. ECH_FREE((WOLFSSL_ECH*)extension->data, heap);
  9377. break;
  9378. #endif
  9379. default:
  9380. break;
  9381. }
  9382. XFREE(extension, heap, DYNAMIC_TYPE_TLSX);
  9383. }
  9384. (void)heap;
  9385. }
  9386. /** Checks if the tls extensions are supported based on the protocol version. */
  9387. int TLSX_SupportExtensions(WOLFSSL* ssl) {
  9388. return ssl && (IsTLS(ssl) || ssl->version.major == DTLS_MAJOR);
  9389. }
  9390. /** Tells the buffered size of the extensions in a list. */
  9391. static int TLSX_GetSize(TLSX* list, byte* semaphore, byte msgType,
  9392. word16* pLength)
  9393. {
  9394. int ret = 0;
  9395. TLSX* extension;
  9396. word16 length = 0;
  9397. byte isRequest = (msgType == client_hello ||
  9398. msgType == certificate_request);
  9399. while ((extension = list)) {
  9400. list = extension->next;
  9401. /* only extensions marked as response are sent back to the client. */
  9402. if (!isRequest && !extension->resp)
  9403. continue; /* skip! */
  9404. /* ssl level extensions are expected to override ctx level ones. */
  9405. if (!IS_OFF(semaphore, TLSX_ToSemaphore(extension->type)))
  9406. continue; /* skip! */
  9407. /* extension type + extension data length. */
  9408. length += HELLO_EXT_TYPE_SZ + OPAQUE16_LEN;
  9409. switch (extension->type) {
  9410. #ifdef HAVE_SNI
  9411. case TLSX_SERVER_NAME:
  9412. /* SNI only sends the name on the request. */
  9413. if (isRequest)
  9414. length += SNI_GET_SIZE((SNI*)extension->data);
  9415. break;
  9416. #endif
  9417. case TLSX_TRUSTED_CA_KEYS:
  9418. /* TCA only sends the list on the request. */
  9419. if (isRequest)
  9420. length += TCA_GET_SIZE((TCA*)extension->data);
  9421. break;
  9422. case TLSX_MAX_FRAGMENT_LENGTH:
  9423. length += MFL_GET_SIZE(extension->data);
  9424. break;
  9425. case TLSX_EXTENDED_MASTER_SECRET:
  9426. case TLSX_TRUNCATED_HMAC:
  9427. /* always empty. */
  9428. break;
  9429. case TLSX_SUPPORTED_GROUPS:
  9430. length += EC_GET_SIZE((SupportedCurve*)extension->data);
  9431. break;
  9432. case TLSX_EC_POINT_FORMATS:
  9433. length += PF_GET_SIZE((PointFormat*)extension->data);
  9434. break;
  9435. case TLSX_STATUS_REQUEST:
  9436. length += CSR_GET_SIZE(
  9437. (CertificateStatusRequest*)extension->data, isRequest);
  9438. break;
  9439. case TLSX_STATUS_REQUEST_V2:
  9440. length += CSR2_GET_SIZE(
  9441. (CertificateStatusRequestItemV2*)extension->data,
  9442. isRequest);
  9443. break;
  9444. case TLSX_RENEGOTIATION_INFO:
  9445. length += SCR_GET_SIZE((SecureRenegotiation*)extension->data,
  9446. isRequest);
  9447. break;
  9448. case TLSX_SESSION_TICKET:
  9449. length += WOLF_STK_GET_SIZE((SessionTicket*)extension->data,
  9450. isRequest);
  9451. break;
  9452. case TLSX_APPLICATION_LAYER_PROTOCOL:
  9453. length += ALPN_GET_SIZE((ALPN*)extension->data);
  9454. break;
  9455. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9456. case TLSX_SIGNATURE_ALGORITHMS:
  9457. length += SA_GET_SIZE(extension->data);
  9458. break;
  9459. #endif
  9460. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  9461. case TLSX_ENCRYPT_THEN_MAC:
  9462. ret = ETM_GET_SIZE(msgType, &length);
  9463. break;
  9464. #endif /* HAVE_ENCRYPT_THEN_MAC */
  9465. #ifdef WOLFSSL_TLS13
  9466. case TLSX_SUPPORTED_VERSIONS:
  9467. ret = SV_GET_SIZE(extension->data, msgType, &length);
  9468. break;
  9469. #ifdef WOLFSSL_SEND_HRR_COOKIE
  9470. case TLSX_COOKIE:
  9471. ret = CKE_GET_SIZE((Cookie*)extension->data, msgType, &length);
  9472. break;
  9473. #endif
  9474. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9475. case TLSX_PRE_SHARED_KEY:
  9476. ret = PSK_GET_SIZE((PreSharedKey*)extension->data, msgType,
  9477. &length);
  9478. break;
  9479. case TLSX_PSK_KEY_EXCHANGE_MODES:
  9480. ret = PKM_GET_SIZE((byte)extension->val, msgType, &length);
  9481. break;
  9482. #endif
  9483. #ifdef WOLFSSL_EARLY_DATA
  9484. case TLSX_EARLY_DATA:
  9485. ret = EDI_GET_SIZE(msgType, &length);
  9486. break;
  9487. #endif
  9488. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  9489. case TLSX_POST_HANDSHAKE_AUTH:
  9490. ret = PHA_GET_SIZE(msgType, &length);
  9491. break;
  9492. #endif
  9493. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9494. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  9495. length += SAC_GET_SIZE(extension->data);
  9496. break;
  9497. #endif
  9498. case TLSX_KEY_SHARE:
  9499. length += KS_GET_SIZE((KeyShareEntry*)extension->data, msgType);
  9500. break;
  9501. #endif
  9502. #ifdef WOLFSSL_SRTP
  9503. case TLSX_USE_SRTP:
  9504. length += SRTP_GET_SIZE((TlsxSrtp*)extension->data);
  9505. break;
  9506. #endif
  9507. #ifdef WOLFSSL_QUIC
  9508. case TLSX_KEY_QUIC_TP_PARAMS:
  9509. FALL_THROUGH; /* followed by */
  9510. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  9511. length += QTP_GET_SIZE(extension);
  9512. break;
  9513. #endif
  9514. #ifdef WOLFSSL_DTLS_CID
  9515. case TLSX_CONNECTION_ID:
  9516. length += CID_GET_SIZE((byte*)extension->data);
  9517. break;
  9518. #endif /* WOLFSSL_DTLS_CID */
  9519. #if defined(HAVE_ECH)
  9520. case TLSX_ECH:
  9521. length += ECH_GET_SIZE((WOLFSSL_ECH*)extension->data);
  9522. break;
  9523. #endif
  9524. default:
  9525. break;
  9526. }
  9527. /* marks the extension as processed so ctx level */
  9528. /* extensions don't overlap with ssl level ones. */
  9529. TURN_ON(semaphore, TLSX_ToSemaphore(extension->type));
  9530. }
  9531. *pLength += length;
  9532. return ret;
  9533. }
  9534. /** Writes the extensions of a list in a buffer. */
  9535. static int TLSX_Write(TLSX* list, byte* output, byte* semaphore,
  9536. byte msgType, word16* pOffset)
  9537. {
  9538. int ret = 0;
  9539. TLSX* extension;
  9540. word16 offset = 0;
  9541. word16 length_offset = 0;
  9542. byte isRequest = (msgType == client_hello ||
  9543. msgType == certificate_request);
  9544. while ((extension = list)) {
  9545. list = extension->next;
  9546. /* only extensions marked as response are written in a response. */
  9547. if (!isRequest && !extension->resp)
  9548. continue; /* skip! */
  9549. /* ssl level extensions are expected to override ctx level ones. */
  9550. if (!IS_OFF(semaphore, TLSX_ToSemaphore(extension->type)))
  9551. continue; /* skip! */
  9552. /* writes extension type. */
  9553. c16toa(extension->type, output + offset);
  9554. offset += HELLO_EXT_TYPE_SZ + OPAQUE16_LEN;
  9555. length_offset = offset;
  9556. /* extension data should be written internally. */
  9557. switch (extension->type) {
  9558. #ifdef HAVE_SNI
  9559. case TLSX_SERVER_NAME:
  9560. if (isRequest) {
  9561. WOLFSSL_MSG("SNI extension to write");
  9562. offset += SNI_WRITE((SNI*)extension->data, output + offset);
  9563. }
  9564. break;
  9565. #endif
  9566. case TLSX_TRUSTED_CA_KEYS:
  9567. WOLFSSL_MSG("Trusted CA Indication extension to write");
  9568. if (isRequest) {
  9569. offset += TCA_WRITE((TCA*)extension->data, output + offset);
  9570. }
  9571. break;
  9572. case TLSX_MAX_FRAGMENT_LENGTH:
  9573. WOLFSSL_MSG("Max Fragment Length extension to write");
  9574. offset += MFL_WRITE((byte*)extension->data, output + offset);
  9575. break;
  9576. case TLSX_EXTENDED_MASTER_SECRET:
  9577. WOLFSSL_MSG("Extended Master Secret");
  9578. /* always empty. */
  9579. break;
  9580. case TLSX_TRUNCATED_HMAC:
  9581. WOLFSSL_MSG("Truncated HMAC extension to write");
  9582. /* always empty. */
  9583. break;
  9584. case TLSX_SUPPORTED_GROUPS:
  9585. WOLFSSL_MSG("Supported Groups extension to write");
  9586. offset += EC_WRITE((SupportedCurve*)extension->data,
  9587. output + offset);
  9588. break;
  9589. case TLSX_EC_POINT_FORMATS:
  9590. WOLFSSL_MSG("Point Formats extension to write");
  9591. offset += PF_WRITE((PointFormat*)extension->data,
  9592. output + offset);
  9593. break;
  9594. case TLSX_STATUS_REQUEST:
  9595. WOLFSSL_MSG("Certificate Status Request extension to write");
  9596. offset += CSR_WRITE((CertificateStatusRequest*)extension->data,
  9597. output + offset, isRequest);
  9598. break;
  9599. case TLSX_STATUS_REQUEST_V2:
  9600. WOLFSSL_MSG("Certificate Status Request v2 extension to write");
  9601. offset += CSR2_WRITE(
  9602. (CertificateStatusRequestItemV2*)extension->data,
  9603. output + offset, isRequest);
  9604. break;
  9605. case TLSX_RENEGOTIATION_INFO:
  9606. WOLFSSL_MSG("Secure Renegotiation extension to write");
  9607. offset += SCR_WRITE((SecureRenegotiation*)extension->data,
  9608. output + offset, isRequest);
  9609. break;
  9610. case TLSX_SESSION_TICKET:
  9611. WOLFSSL_MSG("Session Ticket extension to write");
  9612. offset += WOLF_STK_WRITE((SessionTicket*)extension->data,
  9613. output + offset, isRequest);
  9614. break;
  9615. case TLSX_APPLICATION_LAYER_PROTOCOL:
  9616. WOLFSSL_MSG("ALPN extension to write");
  9617. offset += ALPN_WRITE((ALPN*)extension->data, output + offset);
  9618. break;
  9619. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9620. case TLSX_SIGNATURE_ALGORITHMS:
  9621. WOLFSSL_MSG("Signature Algorithms extension to write");
  9622. offset += SA_WRITE(extension->data, output + offset);
  9623. break;
  9624. #endif
  9625. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  9626. case TLSX_ENCRYPT_THEN_MAC:
  9627. WOLFSSL_MSG("Encrypt-Then-Mac extension to write");
  9628. ret = ETM_WRITE(extension->data, output, msgType, &offset);
  9629. break;
  9630. #endif /* HAVE_ENCRYPT_THEN_MAC */
  9631. #ifdef WOLFSSL_TLS13
  9632. case TLSX_SUPPORTED_VERSIONS:
  9633. WOLFSSL_MSG("Supported Versions extension to write");
  9634. ret = SV_WRITE(extension->data, output + offset, msgType, &offset);
  9635. break;
  9636. #ifdef WOLFSSL_SEND_HRR_COOKIE
  9637. case TLSX_COOKIE:
  9638. WOLFSSL_MSG("Cookie extension to write");
  9639. ret = CKE_WRITE((Cookie*)extension->data, output + offset,
  9640. msgType, &offset);
  9641. break;
  9642. #endif
  9643. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9644. case TLSX_PRE_SHARED_KEY:
  9645. WOLFSSL_MSG("Pre-Shared Key extension to write");
  9646. ret = PSK_WRITE((PreSharedKey*)extension->data, output + offset,
  9647. msgType, &offset);
  9648. break;
  9649. case TLSX_PSK_KEY_EXCHANGE_MODES:
  9650. WOLFSSL_MSG("PSK Key Exchange Modes extension to write");
  9651. ret = PKM_WRITE((byte)extension->val, output + offset, msgType,
  9652. &offset);
  9653. break;
  9654. #endif
  9655. #ifdef WOLFSSL_EARLY_DATA
  9656. case TLSX_EARLY_DATA:
  9657. WOLFSSL_MSG("Early Data extension to write");
  9658. ret = EDI_WRITE(extension->val, output + offset, msgType,
  9659. &offset);
  9660. break;
  9661. #endif
  9662. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  9663. case TLSX_POST_HANDSHAKE_AUTH:
  9664. WOLFSSL_MSG("Post-Handshake Authentication extension to write");
  9665. ret = PHA_WRITE(output + offset, msgType, &offset);
  9666. break;
  9667. #endif
  9668. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9669. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  9670. WOLFSSL_MSG("Signature Algorithms extension to write");
  9671. offset += SAC_WRITE(extension->data, output + offset);
  9672. break;
  9673. #endif
  9674. case TLSX_KEY_SHARE:
  9675. WOLFSSL_MSG("Key Share extension to write");
  9676. offset += KS_WRITE((KeyShareEntry*)extension->data,
  9677. output + offset, msgType);
  9678. break;
  9679. #endif
  9680. #ifdef WOLFSSL_SRTP
  9681. case TLSX_USE_SRTP:
  9682. offset += SRTP_WRITE((TlsxSrtp*)extension->data, output+offset);
  9683. break;
  9684. #endif
  9685. #ifdef WOLFSSL_QUIC
  9686. case TLSX_KEY_QUIC_TP_PARAMS:
  9687. FALL_THROUGH;
  9688. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  9689. WOLFSSL_MSG("QUIC transport parameter to write");
  9690. offset += QTP_WRITE((QuicTransportParam*)extension->data,
  9691. output + offset);
  9692. break;
  9693. #endif
  9694. #ifdef WOLFSSL_DTLS_CID
  9695. case TLSX_CONNECTION_ID:
  9696. offset += CID_WRITE((byte*)extension->data, output+offset);
  9697. break;
  9698. #endif /* WOLFSSL_DTLS_CID */
  9699. #if defined(HAVE_ECH)
  9700. case TLSX_ECH:
  9701. ret = ECH_WRITE((WOLFSSL_ECH*)extension->data,
  9702. output + offset, &offset);
  9703. break;
  9704. #endif
  9705. default:
  9706. break;
  9707. }
  9708. /* writes extension data length. */
  9709. c16toa(offset - length_offset, output + length_offset - OPAQUE16_LEN);
  9710. /* marks the extension as processed so ctx level */
  9711. /* extensions don't overlap with ssl level ones. */
  9712. TURN_ON(semaphore, TLSX_ToSemaphore(extension->type));
  9713. /* if we encountered an error propigate it */
  9714. if (ret != 0)
  9715. break;
  9716. }
  9717. *pOffset += offset;
  9718. return ret;
  9719. }
  9720. #ifdef HAVE_SUPPORTED_CURVES
  9721. /* Populates the default supported groups / curves */
  9722. static int TLSX_PopulateSupportedGroups(WOLFSSL* ssl, TLSX** extensions)
  9723. {
  9724. int ret = WOLFSSL_SUCCESS;
  9725. #ifdef WOLFSSL_TLS13
  9726. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9727. if (ssl->options.resuming && ssl->session->namedGroup != 0) {
  9728. return TLSX_UseSupportedCurve(extensions, ssl->session->namedGroup,
  9729. ssl->heap);
  9730. }
  9731. #endif
  9732. if (ssl->numGroups != 0) {
  9733. int i;
  9734. for (i = 0; i < ssl->numGroups; i++) {
  9735. ret = TLSX_UseSupportedCurve(extensions, ssl->group[i], ssl->heap);
  9736. if (ret != WOLFSSL_SUCCESS)
  9737. return ret;
  9738. }
  9739. return WOLFSSL_SUCCESS;
  9740. }
  9741. #endif /* WOLFSSL_TLS13 */
  9742. #if defined(HAVE_ECC)
  9743. /* list in order by strength, since not all servers choose by strength */
  9744. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  9745. #ifndef NO_ECC_SECP
  9746. ret = TLSX_UseSupportedCurve(extensions,
  9747. WOLFSSL_ECC_SECP521R1, ssl->heap);
  9748. if (ret != WOLFSSL_SUCCESS) return ret;
  9749. #endif
  9750. #endif
  9751. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  9752. #ifdef HAVE_ECC_BRAINPOOL
  9753. ret = TLSX_UseSupportedCurve(extensions,
  9754. WOLFSSL_ECC_BRAINPOOLP512R1, ssl->heap);
  9755. if (ret != WOLFSSL_SUCCESS) return ret;
  9756. #endif
  9757. #endif
  9758. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  9759. #ifndef NO_ECC_SECP
  9760. ret = TLSX_UseSupportedCurve(extensions,
  9761. WOLFSSL_ECC_SECP384R1, ssl->heap);
  9762. if (ret != WOLFSSL_SUCCESS) return ret;
  9763. #endif
  9764. #ifdef HAVE_ECC_BRAINPOOL
  9765. ret = TLSX_UseSupportedCurve(extensions,
  9766. WOLFSSL_ECC_BRAINPOOLP384R1, ssl->heap);
  9767. if (ret != WOLFSSL_SUCCESS) return ret;
  9768. #endif
  9769. #endif
  9770. #endif /* HAVE_ECC */
  9771. #ifndef HAVE_FIPS
  9772. #if defined(HAVE_CURVE448) && ECC_MIN_KEY_SZ <= 448
  9773. ret = TLSX_UseSupportedCurve(extensions,
  9774. WOLFSSL_ECC_X448, ssl->heap);
  9775. if (ret != WOLFSSL_SUCCESS) return ret;
  9776. #endif
  9777. #endif /* HAVE_FIPS */
  9778. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  9779. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  9780. #ifndef NO_ECC_SECP
  9781. ret = TLSX_UseSupportedCurve(extensions,
  9782. WOLFSSL_ECC_SECP256R1, ssl->heap);
  9783. if (ret != WOLFSSL_SUCCESS) return ret;
  9784. #endif
  9785. #ifdef HAVE_ECC_KOBLITZ
  9786. ret = TLSX_UseSupportedCurve(extensions,
  9787. WOLFSSL_ECC_SECP256K1, ssl->heap);
  9788. if (ret != WOLFSSL_SUCCESS) return ret;
  9789. #endif
  9790. #ifdef HAVE_ECC_BRAINPOOL
  9791. ret = TLSX_UseSupportedCurve(extensions,
  9792. WOLFSSL_ECC_BRAINPOOLP256R1, ssl->heap);
  9793. if (ret != WOLFSSL_SUCCESS) return ret;
  9794. #endif
  9795. #endif
  9796. #endif /* HAVE_ECC */
  9797. #ifndef HAVE_FIPS
  9798. #if defined(HAVE_CURVE25519) && ECC_MIN_KEY_SZ <= 256
  9799. ret = TLSX_UseSupportedCurve(extensions,
  9800. WOLFSSL_ECC_X25519, ssl->heap);
  9801. if (ret != WOLFSSL_SUCCESS) return ret;
  9802. #endif
  9803. #endif /* HAVE_FIPS */
  9804. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  9805. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  9806. #ifndef NO_ECC_SECP
  9807. ret = TLSX_UseSupportedCurve(extensions,
  9808. WOLFSSL_ECC_SECP224R1, ssl->heap);
  9809. if (ret != WOLFSSL_SUCCESS) return ret;
  9810. #endif
  9811. #ifdef HAVE_ECC_KOBLITZ
  9812. ret = TLSX_UseSupportedCurve(extensions,
  9813. WOLFSSL_ECC_SECP224K1, ssl->heap);
  9814. if (ret != WOLFSSL_SUCCESS) return ret;
  9815. #endif
  9816. #endif
  9817. #ifndef HAVE_FIPS
  9818. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  9819. #ifndef NO_ECC_SECP
  9820. ret = TLSX_UseSupportedCurve(extensions,
  9821. WOLFSSL_ECC_SECP192R1, ssl->heap);
  9822. if (ret != WOLFSSL_SUCCESS) return ret;
  9823. #endif
  9824. #ifdef HAVE_ECC_KOBLITZ
  9825. ret = TLSX_UseSupportedCurve(extensions,
  9826. WOLFSSL_ECC_SECP192K1, ssl->heap);
  9827. if (ret != WOLFSSL_SUCCESS) return ret;
  9828. #endif
  9829. #endif
  9830. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  9831. #ifndef NO_ECC_SECP
  9832. ret = TLSX_UseSupportedCurve(extensions,
  9833. WOLFSSL_ECC_SECP160R1, ssl->heap);
  9834. if (ret != WOLFSSL_SUCCESS) return ret;
  9835. #endif
  9836. #ifdef HAVE_ECC_SECPR2
  9837. ret = TLSX_UseSupportedCurve(extensions,
  9838. WOLFSSL_ECC_SECP160R2, ssl->heap);
  9839. if (ret != WOLFSSL_SUCCESS) return ret;
  9840. #endif
  9841. #ifdef HAVE_ECC_KOBLITZ
  9842. ret = TLSX_UseSupportedCurve(extensions,
  9843. WOLFSSL_ECC_SECP160K1, ssl->heap);
  9844. if (ret != WOLFSSL_SUCCESS) return ret;
  9845. #endif
  9846. #endif
  9847. #endif /* HAVE_FIPS */
  9848. #endif /* HAVE_ECC */
  9849. #ifndef NO_DH
  9850. /* Add FFDHE supported groups. */
  9851. #ifdef HAVE_FFDHE_8192
  9852. if (8192/8 >= ssl->options.minDhKeySz &&
  9853. 8192/8 <= ssl->options.maxDhKeySz) {
  9854. ret = TLSX_UseSupportedCurve(extensions,
  9855. WOLFSSL_FFDHE_8192, ssl->heap);
  9856. if (ret != WOLFSSL_SUCCESS)
  9857. return ret;
  9858. }
  9859. #endif
  9860. #ifdef HAVE_FFDHE_6144
  9861. if (6144/8 >= ssl->options.minDhKeySz &&
  9862. 6144/8 <= ssl->options.maxDhKeySz) {
  9863. ret = TLSX_UseSupportedCurve(extensions,
  9864. WOLFSSL_FFDHE_6144, ssl->heap);
  9865. if (ret != WOLFSSL_SUCCESS)
  9866. return ret;
  9867. }
  9868. #endif
  9869. #ifdef HAVE_FFDHE_4096
  9870. if (4096/8 >= ssl->options.minDhKeySz &&
  9871. 4096/8 <= ssl->options.maxDhKeySz) {
  9872. ret = TLSX_UseSupportedCurve(extensions,
  9873. WOLFSSL_FFDHE_4096, ssl->heap);
  9874. if (ret != WOLFSSL_SUCCESS)
  9875. return ret;
  9876. }
  9877. #endif
  9878. #ifdef HAVE_FFDHE_3072
  9879. if (3072/8 >= ssl->options.minDhKeySz &&
  9880. 3072/8 <= ssl->options.maxDhKeySz) {
  9881. ret = TLSX_UseSupportedCurve(extensions,
  9882. WOLFSSL_FFDHE_3072, ssl->heap);
  9883. if (ret != WOLFSSL_SUCCESS)
  9884. return ret;
  9885. }
  9886. #endif
  9887. #ifdef HAVE_FFDHE_2048
  9888. if (2048/8 >= ssl->options.minDhKeySz &&
  9889. 2048/8 <= ssl->options.maxDhKeySz) {
  9890. ret = TLSX_UseSupportedCurve(extensions,
  9891. WOLFSSL_FFDHE_2048, ssl->heap);
  9892. if (ret != WOLFSSL_SUCCESS)
  9893. return ret;
  9894. }
  9895. #endif
  9896. #endif
  9897. #ifdef HAVE_PQC
  9898. #ifdef WOLFSSL_WC_KYBER
  9899. #ifdef WOLFSSL_KYBER512
  9900. if (ret == WOLFSSL_SUCCESS)
  9901. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL1,
  9902. ssl->heap);
  9903. #endif
  9904. #ifdef WOLFSSL_KYBER768
  9905. if (ret == WOLFSSL_SUCCESS)
  9906. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL3,
  9907. ssl->heap);
  9908. #endif
  9909. #ifdef WOLFSSL_KYBER768
  9910. if (ret == WOLFSSL_SUCCESS)
  9911. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL5,
  9912. ssl->heap);
  9913. #endif
  9914. #elif defined(HAVE_LIBOQS)
  9915. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL1, ssl->heap);
  9916. if (ret == WOLFSSL_SUCCESS)
  9917. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL3,
  9918. ssl->heap);
  9919. if (ret == WOLFSSL_SUCCESS)
  9920. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL5,
  9921. ssl->heap);
  9922. if (ret == WOLFSSL_SUCCESS)
  9923. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P256_KYBER_LEVEL1,
  9924. ssl->heap);
  9925. if (ret == WOLFSSL_SUCCESS)
  9926. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P384_KYBER_LEVEL3,
  9927. ssl->heap);
  9928. if (ret == WOLFSSL_SUCCESS)
  9929. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P521_KYBER_LEVEL5,
  9930. ssl->heap);
  9931. #elif defined(HAVE_PQM4)
  9932. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL1, ssl->heap);
  9933. #endif /* HAVE_LIBOQS */
  9934. #endif /* HAVE_PQC */
  9935. (void)ssl;
  9936. (void)extensions;
  9937. return ret;
  9938. }
  9939. #endif /* HAVE_SUPPORTED_CURVES */
  9940. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  9941. static const word16 preferredGroup[] = {
  9942. #if defined(HAVE_ECC) && (!defined(NO_ECC256) || \
  9943. defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP) && ECC_MIN_KEY_SZ <= 256
  9944. WOLFSSL_ECC_SECP256R1,
  9945. #endif
  9946. #if defined(HAVE_CURVE25519) && ECC_MIN_KEY_SZ <= 256
  9947. WOLFSSL_ECC_X25519,
  9948. #endif
  9949. #if defined(HAVE_CURVE448) && ECC_MIN_KEY_SZ <= 448
  9950. WOLFSSL_ECC_X448,
  9951. #endif
  9952. #if defined(HAVE_ECC) && (!defined(NO_ECC384) || \
  9953. defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP) && ECC_MIN_KEY_SZ <= 384
  9954. WOLFSSL_ECC_SECP384R1,
  9955. #endif
  9956. #if defined(HAVE_ECC) && (!defined(NO_ECC521) || \
  9957. defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP) && ECC_MIN_KEY_SZ <= 521
  9958. WOLFSSL_ECC_SECP521R1,
  9959. #endif
  9960. #if defined(HAVE_FFDHE_2048)
  9961. WOLFSSL_FFDHE_2048,
  9962. #endif
  9963. #if defined(HAVE_FFDHE_3072)
  9964. WOLFSSL_FFDHE_3072,
  9965. #endif
  9966. #if defined(HAVE_FFDHE_4096)
  9967. WOLFSSL_FFDHE_4096,
  9968. #endif
  9969. #if defined(HAVE_FFDHE_6144)
  9970. WOLFSSL_FFDHE_6144,
  9971. #endif
  9972. #if defined(HAVE_FFDHE_8192)
  9973. WOLFSSL_FFDHE_8192,
  9974. #endif
  9975. WOLFSSL_NAMED_GROUP_INVALID
  9976. };
  9977. #endif /* WOLFSSL_TLS13 && HAVE_SUPPORTED_CURVES */
  9978. int TLSX_PopulateExtensions(WOLFSSL* ssl, byte isServer)
  9979. {
  9980. int ret = 0;
  9981. byte* public_key = NULL;
  9982. word16 public_key_len = 0;
  9983. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  9984. int usingPSK = 0;
  9985. #endif
  9986. #if defined(HAVE_SUPPORTED_CURVES) && defined(WOLFSSL_TLS13)
  9987. TLSX* extension = NULL;
  9988. word16 namedGroup = WOLFSSL_NAMED_GROUP_INVALID;
  9989. #endif
  9990. /* server will add extension depending on what is parsed from client */
  9991. if (!isServer) {
  9992. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  9993. if (!ssl->options.disallowEncThenMac) {
  9994. ret = TLSX_EncryptThenMac_Use(ssl);
  9995. if (ret != 0)
  9996. return ret;
  9997. }
  9998. #endif
  9999. #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  10000. defined(HAVE_CURVE448)) && defined(HAVE_SUPPORTED_CURVES)
  10001. if (!ssl->options.userCurves && !ssl->ctx->userCurves) {
  10002. if (TLSX_Find(ssl->ctx->extensions,
  10003. TLSX_SUPPORTED_GROUPS) == NULL) {
  10004. ret = TLSX_PopulateSupportedGroups(ssl, &ssl->extensions);
  10005. if (ret != WOLFSSL_SUCCESS)
  10006. return ret;
  10007. }
  10008. }
  10009. if ((!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade) &&
  10010. TLSX_Find(ssl->ctx->extensions, TLSX_EC_POINT_FORMATS) == NULL &&
  10011. TLSX_Find(ssl->extensions, TLSX_EC_POINT_FORMATS) == NULL) {
  10012. ret = TLSX_UsePointFormat(&ssl->extensions,
  10013. WOLFSSL_EC_PF_UNCOMPRESSED, ssl->heap);
  10014. if (ret != WOLFSSL_SUCCESS)
  10015. return ret;
  10016. }
  10017. #endif /* (HAVE_ECC || CURVE25519 || CURVE448) && HAVE_SUPPORTED_CURVES */
  10018. #ifdef WOLFSSL_SRTP
  10019. if (ssl->options.dtls && ssl->dtlsSrtpProfiles != 0) {
  10020. WOLFSSL_MSG("Adding DTLS SRTP extension");
  10021. if ((ret = TLSX_UseSRTP(&ssl->extensions, ssl->dtlsSrtpProfiles,
  10022. ssl->heap)) != 0) {
  10023. return ret;
  10024. }
  10025. }
  10026. #endif
  10027. } /* is not server */
  10028. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10029. WOLFSSL_MSG("Adding signature algorithms extension");
  10030. if ((ret = TLSX_SetSignatureAlgorithms(&ssl->extensions, ssl, ssl->heap))
  10031. != 0) {
  10032. return ret;
  10033. }
  10034. #else
  10035. ret = 0;
  10036. #endif
  10037. #ifdef WOLFSSL_TLS13
  10038. if (!isServer && IsAtLeastTLSv1_3(ssl->version)) {
  10039. /* Add mandatory TLS v1.3 extension: supported version */
  10040. WOLFSSL_MSG("Adding supported versions extension");
  10041. if ((ret = TLSX_SetSupportedVersions(&ssl->extensions, ssl,
  10042. ssl->heap)) != 0) {
  10043. return ret;
  10044. }
  10045. #if !defined(HAVE_ECC) && !defined(HAVE_CURVE25519) && \
  10046. !defined(HAVE_CURVE448) && defined(HAVE_SUPPORTED_CURVES)
  10047. if (TLSX_Find(ssl->ctx->extensions, TLSX_SUPPORTED_GROUPS) == NULL) {
  10048. /* Put in DH groups for TLS 1.3 only. */
  10049. ret = TLSX_PopulateSupportedGroups(ssl, &ssl->extensions);
  10050. if (ret != WOLFSSL_SUCCESS)
  10051. return ret;
  10052. /* ret value will be overwritten in !NO_PSK case */
  10053. #ifdef NO_PSK
  10054. ret = 0;
  10055. #endif
  10056. }
  10057. #endif /* !(HAVE_ECC || CURVE25519 || CURVE448) && HAVE_SUPPORTED_CURVES */
  10058. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10059. if (ssl->certHashSigAlgoSz > 0) {
  10060. WOLFSSL_MSG("Adding signature algorithms cert extension");
  10061. if ((ret = TLSX_SetSignatureAlgorithmsCert(&ssl->extensions,
  10062. ssl, ssl->heap)) != 0) {
  10063. return ret;
  10064. }
  10065. }
  10066. #endif
  10067. #if defined(HAVE_SUPPORTED_CURVES)
  10068. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  10069. if (extension == NULL) {
  10070. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10071. if (ssl->options.resuming && ssl->session->namedGroup != 0)
  10072. namedGroup = ssl->session->namedGroup;
  10073. else
  10074. #endif
  10075. if (ssl->numGroups > 0) {
  10076. int set = 0;
  10077. int i, j;
  10078. /* try to find the highest element in ssl->group[]
  10079. * that is contained in preferredGroup[].
  10080. */
  10081. namedGroup = preferredGroup[0];
  10082. for (i = 0; i < ssl->numGroups && !set; i++) {
  10083. for (j = 0; preferredGroup[j] != WOLFSSL_NAMED_GROUP_INVALID; j++) {
  10084. if (preferredGroup[j] == ssl->group[i]) {
  10085. namedGroup = ssl->group[i];
  10086. set = 1;
  10087. break;
  10088. }
  10089. }
  10090. }
  10091. }
  10092. else {
  10093. /* Choose the most preferred group. */
  10094. namedGroup = preferredGroup[0];
  10095. }
  10096. }
  10097. else {
  10098. KeyShareEntry* kse = (KeyShareEntry*)extension->data;
  10099. if (kse)
  10100. namedGroup = kse->group;
  10101. }
  10102. if (namedGroup > 0) {
  10103. #ifdef HAVE_PQC
  10104. /* For KEMs, the key share has already been generated, but not
  10105. * if we are resuming. */
  10106. if (!WOLFSSL_NAMED_GROUP_IS_PQC(namedGroup)
  10107. #ifdef HAVE_SESSION_TICKET
  10108. || ssl->options.resuming
  10109. #endif /* HAVE_SESSION_TICKET */
  10110. )
  10111. #endif /* HAVE_PQC */
  10112. ret = TLSX_KeyShare_Use(ssl, namedGroup, 0, NULL, NULL);
  10113. if (ret != 0)
  10114. return ret;
  10115. }
  10116. #endif /* HAVE_SUPPORTED_CURVES */
  10117. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10118. TLSX_Remove(&ssl->extensions, TLSX_PRE_SHARED_KEY, ssl->heap);
  10119. #endif
  10120. #if defined(HAVE_SESSION_TICKET)
  10121. if (ssl->options.resuming && ssl->session->ticketLen > 0) {
  10122. WOLFSSL_SESSION* sess = ssl->session;
  10123. #ifdef WOLFSSL_32BIT_MILLI_TIME
  10124. word32 now, milli;
  10125. #else
  10126. word64 now, milli;
  10127. #endif
  10128. if (sess->ticketLen > MAX_PSK_ID_LEN) {
  10129. WOLFSSL_MSG("Session ticket length for PSK ext is too large");
  10130. return BUFFER_ERROR;
  10131. }
  10132. /* Determine the MAC algorithm for the cipher suite used. */
  10133. ssl->options.cipherSuite0 = sess->cipherSuite0;
  10134. ssl->options.cipherSuite = sess->cipherSuite;
  10135. ret = SetCipherSpecs(ssl);
  10136. if (ret != 0)
  10137. return ret;
  10138. now = TimeNowInMilliseconds();
  10139. if (now == 0)
  10140. return GETTIME_ERROR;
  10141. #ifdef WOLFSSL_32BIT_MILLI_TIME
  10142. if (now < sess->ticketSeen)
  10143. milli = (0xFFFFFFFFU - sess->ticketSeen) + 1 + now;
  10144. else
  10145. milli = now - sess->ticketSeen;
  10146. milli += sess->ticketAdd;
  10147. /* Pre-shared key is mandatory extension for resumption. */
  10148. ret = TLSX_PreSharedKey_Use(ssl, sess->ticket, sess->ticketLen,
  10149. milli, ssl->specs.mac_algorithm, ssl->options.cipherSuite0,
  10150. ssl->options.cipherSuite, 1, NULL);
  10151. #else
  10152. milli = now - sess->ticketSeen + sess->ticketAdd;
  10153. /* Pre-shared key is mandatory extension for resumption. */
  10154. ret = TLSX_PreSharedKey_Use(ssl, sess->ticket, sess->ticketLen,
  10155. (word32)milli, ssl->specs.mac_algorithm,
  10156. ssl->options.cipherSuite0, ssl->options.cipherSuite, 1,
  10157. NULL);
  10158. #endif
  10159. if (ret != 0)
  10160. return ret;
  10161. usingPSK = 1;
  10162. }
  10163. #endif
  10164. #ifndef NO_PSK
  10165. #ifndef WOLFSSL_PSK_ONE_ID
  10166. if (ssl->options.client_psk_cs_cb != NULL) {
  10167. int i;
  10168. for (i = 0; i < ssl->suites->suiteSz; i += 2) {
  10169. byte cipherSuite0 = ssl->suites->suites[i + 0];
  10170. byte cipherSuite = ssl->suites->suites[i + 1];
  10171. unsigned int keySz;
  10172. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  10173. int cnt = 0;
  10174. #endif
  10175. #ifdef HAVE_NULL_CIPHER
  10176. if (cipherSuite0 == ECC_BYTE ||
  10177. cipherSuite0 == ECDHE_PSK_BYTE) {
  10178. if (cipherSuite != TLS_SHA256_SHA256 &&
  10179. cipherSuite != TLS_SHA384_SHA384) {
  10180. continue;
  10181. }
  10182. }
  10183. else
  10184. #endif
  10185. if (cipherSuite0 != TLS13_BYTE)
  10186. continue;
  10187. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  10188. do {
  10189. ssl->arrays->client_identity[0] = cnt;
  10190. #endif
  10191. ssl->arrays->client_identity[MAX_PSK_ID_LEN] = '\0';
  10192. keySz = ssl->options.client_psk_cs_cb(
  10193. ssl, ssl->arrays->server_hint,
  10194. ssl->arrays->client_identity, MAX_PSK_ID_LEN,
  10195. ssl->arrays->psk_key, MAX_PSK_KEY_LEN,
  10196. GetCipherNameInternal(cipherSuite0, cipherSuite));
  10197. if (keySz > 0) {
  10198. ssl->arrays->psk_keySz = keySz;
  10199. ret = TLSX_PreSharedKey_Use(ssl,
  10200. (byte*)ssl->arrays->client_identity,
  10201. (word16)XSTRLEN(ssl->arrays->client_identity),
  10202. 0, SuiteMac(ssl->suites->suites + i),
  10203. cipherSuite0, cipherSuite, 0, NULL);
  10204. if (ret != 0)
  10205. return ret;
  10206. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  10207. cnt++;
  10208. #endif
  10209. }
  10210. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  10211. }
  10212. while (keySz > 0);
  10213. #endif
  10214. }
  10215. usingPSK = 1;
  10216. }
  10217. else
  10218. #endif
  10219. if (ssl->options.client_psk_cb != NULL ||
  10220. ssl->options.client_psk_tls13_cb != NULL) {
  10221. /* Default ciphersuite. */
  10222. byte cipherSuite0 = TLS13_BYTE;
  10223. byte cipherSuite = WOLFSSL_DEF_PSK_CIPHER;
  10224. int cipherSuiteFlags = WOLFSSL_CIPHER_SUITE_FLAG_NONE;
  10225. const char* cipherName = NULL;
  10226. if (ssl->options.client_psk_tls13_cb != NULL) {
  10227. ssl->arrays->psk_keySz = ssl->options.client_psk_tls13_cb(
  10228. ssl, ssl->arrays->server_hint,
  10229. ssl->arrays->client_identity, MAX_PSK_ID_LEN,
  10230. ssl->arrays->psk_key, MAX_PSK_KEY_LEN, &cipherName);
  10231. if (GetCipherSuiteFromName(cipherName, &cipherSuite0,
  10232. &cipherSuite, &cipherSuiteFlags) != 0) {
  10233. return PSK_KEY_ERROR;
  10234. }
  10235. }
  10236. else {
  10237. ssl->arrays->psk_keySz = ssl->options.client_psk_cb(ssl,
  10238. ssl->arrays->server_hint, ssl->arrays->client_identity,
  10239. MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN);
  10240. }
  10241. #if defined(OPENSSL_EXTRA)
  10242. /* OpenSSL treats 0 as a PSK key length of 0
  10243. * and meaning no PSK available.
  10244. */
  10245. if (ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN) {
  10246. return PSK_KEY_ERROR;
  10247. }
  10248. if (ssl->arrays->psk_keySz > 0) {
  10249. #else
  10250. if (ssl->arrays->psk_keySz == 0 ||
  10251. ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN) {
  10252. return PSK_KEY_ERROR;
  10253. }
  10254. #endif
  10255. ssl->arrays->client_identity[MAX_PSK_ID_LEN] = '\0';
  10256. ssl->options.cipherSuite0 = cipherSuite0;
  10257. ssl->options.cipherSuite = cipherSuite;
  10258. (void)cipherSuiteFlags;
  10259. ret = SetCipherSpecs(ssl);
  10260. if (ret != 0)
  10261. return ret;
  10262. ret = TLSX_PreSharedKey_Use(ssl,
  10263. (byte*)ssl->arrays->client_identity,
  10264. (word16)XSTRLEN(ssl->arrays->client_identity),
  10265. 0, ssl->specs.mac_algorithm,
  10266. cipherSuite0, cipherSuite, 0,
  10267. NULL);
  10268. if (ret != 0)
  10269. return ret;
  10270. usingPSK = 1;
  10271. #if defined(OPENSSL_EXTRA)
  10272. }
  10273. #endif
  10274. }
  10275. #endif /* !NO_PSK */
  10276. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10277. /* Some servers do not generate session tickets unless
  10278. * the extension is seen in a non-resume client hello.
  10279. * We used to send it only if we were otherwise using PSK.
  10280. * Now always send it. Define NO_TLSX_PSKKEM_PLAIN_ANNOUNCE
  10281. * to revert to the old behaviour. */
  10282. #ifdef NO_TLSX_PSKKEM_PLAIN_ANNOUNCE
  10283. if (usingPSK)
  10284. #endif
  10285. {
  10286. byte modes;
  10287. (void)usingPSK;
  10288. /* Pre-shared key modes: mandatory extension for resumption. */
  10289. modes = 1 << PSK_KE;
  10290. #if !defined(NO_DH) || defined(HAVE_ECC) || \
  10291. defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  10292. if (!ssl->options.noPskDheKe)
  10293. modes |= 1 << PSK_DHE_KE;
  10294. #endif
  10295. ret = TLSX_PskKeModes_Use(ssl, modes);
  10296. if (ret != 0)
  10297. return ret;
  10298. }
  10299. #endif
  10300. #if defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  10301. if (!isServer && ssl->options.postHandshakeAuth) {
  10302. ret = TLSX_PostHandAuth_Use(ssl);
  10303. if (ret != 0)
  10304. return ret;
  10305. }
  10306. #endif
  10307. #if defined(HAVE_ECH)
  10308. /* GREASE ECH */
  10309. if (ssl->echConfigs == NULL) {
  10310. ret = GREASE_ECH_USE(&(ssl->extensions), ssl->heap, ssl->rng);
  10311. }
  10312. else if (ssl->echConfigs != NULL) {
  10313. ret = ECH_USE(ssl->echConfigs, &(ssl->extensions), ssl->heap,
  10314. ssl->rng);
  10315. }
  10316. #endif
  10317. }
  10318. #if defined(HAVE_ECH)
  10319. else if (IsAtLeastTLSv1_3(ssl->version)) {
  10320. if (ssl->ctx->echConfigs != NULL) {
  10321. ret = SERVER_ECH_USE(&(ssl->extensions), ssl->heap,
  10322. ssl->ctx->echConfigs);
  10323. if (ret == 0)
  10324. TLSX_SetResponse(ssl, TLSX_ECH);
  10325. }
  10326. }
  10327. #endif
  10328. #endif
  10329. (void)isServer;
  10330. (void)public_key;
  10331. (void)public_key_len;
  10332. (void)ssl;
  10333. return ret;
  10334. }
  10335. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
  10336. #if defined(HAVE_ECH)
  10337. /* because the size of ech depends on the size of other extensions we need to
  10338. * get the size with ech special and process ech last, return status */
  10339. static int TLSX_GetSizeWithEch(WOLFSSL* ssl, byte* semaphore, byte msgType,
  10340. word16* pLength)
  10341. {
  10342. int ret = 0;
  10343. TLSX* echX = NULL;
  10344. TLSX* serverNameX = NULL;
  10345. TLSX** extensions = NULL;
  10346. #ifdef WOLFSSL_SMALL_STACK
  10347. char* tmpServerName = NULL;
  10348. #else
  10349. char tmpServerName[MAX_PUBLIC_NAME_SZ];
  10350. #endif
  10351. /* calculate the rest of the extensions length with inner ech */
  10352. if (ssl->extensions)
  10353. echX = TLSX_Find(ssl->extensions, TLSX_ECH);
  10354. if (echX == NULL && ssl->ctx && ssl->ctx->extensions)
  10355. echX = TLSX_Find(ssl->ctx->extensions, TLSX_ECH);
  10356. /* if type is outer change sni to public name */
  10357. if (echX != NULL && ((WOLFSSL_ECH*)echX->data)->type == ECH_TYPE_OUTER) {
  10358. if (ssl->extensions) {
  10359. serverNameX = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
  10360. if (serverNameX != NULL)
  10361. extensions = &ssl->extensions;
  10362. }
  10363. if (serverNameX == NULL && ssl->ctx && ssl->ctx->extensions) {
  10364. serverNameX = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
  10365. extensions = &ssl->ctx->extensions;
  10366. }
  10367. /* store the inner server name */
  10368. if (serverNameX != NULL) {
  10369. char* hostName = ((SNI*)serverNameX->data)->data.host_name;
  10370. word32 hostNameSz = (word32)XSTRLEN(hostName) + 1;
  10371. #ifdef WOLFSSL_SMALL_STACK
  10372. tmpServerName = (char*)XMALLOC(hostNameSz, ssl->heap,
  10373. DYNAMIC_TYPE_TMP_BUFFER);
  10374. if (tmpServerName == NULL)
  10375. return MEMORY_E;
  10376. #else
  10377. /* truncate if too long */
  10378. if (hostNameSz > MAX_PUBLIC_NAME_SZ)
  10379. hostNameSz = MAX_PUBLIC_NAME_SZ;
  10380. #endif
  10381. XMEMCPY(tmpServerName, hostName, hostNameSz);
  10382. }
  10383. /* remove the inner server name */
  10384. TLSX_Remove(extensions, TLSX_SERVER_NAME, ssl->heap);
  10385. ret = TLSX_UseSNI(extensions, WOLFSSL_SNI_HOST_NAME,
  10386. ((WOLFSSL_ECH*)echX->data)->echConfig->publicName,
  10387. XSTRLEN(((WOLFSSL_ECH*)echX->data)->echConfig->publicName),
  10388. ssl->heap);
  10389. /* set the public name as the server name */
  10390. if (ret == WOLFSSL_SUCCESS)
  10391. ret = 0;
  10392. }
  10393. if (ret == 0 && ssl->extensions)
  10394. ret = TLSX_GetSize(ssl->extensions, semaphore, msgType, pLength);
  10395. if (ret == 0 && ssl->ctx && ssl->ctx->extensions)
  10396. ret = TLSX_GetSize(ssl->ctx->extensions, semaphore, msgType, pLength);
  10397. if (serverNameX != NULL) {
  10398. /* remove the public name SNI */
  10399. TLSX_Remove(extensions, TLSX_SERVER_NAME, ssl->heap);
  10400. ret = TLSX_UseSNI(extensions, WOLFSSL_SNI_HOST_NAME,
  10401. tmpServerName, XSTRLEN(tmpServerName), ssl->heap);
  10402. /* restore the inner server name */
  10403. if (ret == WOLFSSL_SUCCESS)
  10404. ret = 0;
  10405. }
  10406. #ifdef WOLFSSL_SMALL_STACK
  10407. XFREE(tmpServerName, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  10408. #endif
  10409. return ret;
  10410. }
  10411. #endif
  10412. /** Tells the buffered size of extensions to be sent into the client hello. */
  10413. int TLSX_GetRequestSize(WOLFSSL* ssl, byte msgType, word16* pLength)
  10414. {
  10415. int ret = 0;
  10416. word16 length = 0;
  10417. byte semaphore[SEMAPHORE_SIZE] = {0};
  10418. if (!TLSX_SupportExtensions(ssl))
  10419. return 0;
  10420. if (msgType == client_hello) {
  10421. EC_VALIDATE_REQUEST(ssl, semaphore);
  10422. PF_VALIDATE_REQUEST(ssl, semaphore);
  10423. WOLF_STK_VALIDATE_REQUEST(ssl);
  10424. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10425. if (ssl->suites->hashSigAlgoSz == 0)
  10426. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  10427. #endif
  10428. #if defined(WOLFSSL_TLS13)
  10429. if (!IsAtLeastTLSv1_2(ssl))
  10430. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10431. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  10432. if (!IsAtLeastTLSv1_3(ssl->version)) {
  10433. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10434. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10435. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10436. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PSK_KEY_EXCHANGE_MODES));
  10437. #endif
  10438. #ifdef WOLFSSL_EARLY_DATA
  10439. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  10440. #endif
  10441. #ifdef WOLFSSL_SEND_HRR_COOKIE
  10442. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  10443. #endif
  10444. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  10445. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_POST_HANDSHAKE_AUTH));
  10446. #endif
  10447. }
  10448. #endif
  10449. #endif /* WOLFSSL_TLS13 */
  10450. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  10451. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  10452. if (!SSL_CM(ssl)->ocspStaplingEnabled) {
  10453. /* mark already sent, so it won't send it */
  10454. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10455. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  10456. }
  10457. #endif
  10458. }
  10459. #ifdef WOLFSSL_TLS13
  10460. #ifndef NO_CERTS
  10461. else if (msgType == certificate_request) {
  10462. /* Don't send out any extension except those that are turned off. */
  10463. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10464. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10465. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  10466. #endif
  10467. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  10468. * TLSX_CERTIFICATE_AUTHORITIES, OID_FILTERS
  10469. * TLSX_STATUS_REQUEST
  10470. */
  10471. }
  10472. #endif
  10473. #endif
  10474. #if defined(HAVE_ECH)
  10475. if (ssl->options.useEch == 1 && msgType == client_hello) {
  10476. ret = TLSX_GetSizeWithEch(ssl, semaphore, msgType, &length);
  10477. if (ret != 0)
  10478. return ret;
  10479. }
  10480. else
  10481. #endif
  10482. {
  10483. if (ssl->extensions) {
  10484. ret = TLSX_GetSize(ssl->extensions, semaphore, msgType, &length);
  10485. if (ret != 0)
  10486. return ret;
  10487. }
  10488. if (ssl->ctx && ssl->ctx->extensions) {
  10489. ret = TLSX_GetSize(ssl->ctx->extensions, semaphore, msgType,
  10490. &length);
  10491. if (ret != 0)
  10492. return ret;
  10493. }
  10494. }
  10495. #ifdef HAVE_EXTENDED_MASTER
  10496. if (msgType == client_hello && ssl->options.haveEMS &&
  10497. (!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade)) {
  10498. length += HELLO_EXT_SZ;
  10499. }
  10500. #endif
  10501. if (length)
  10502. length += OPAQUE16_LEN; /* for total length storage. */
  10503. *pLength += length;
  10504. return ret;
  10505. }
  10506. #if defined(HAVE_ECH)
  10507. /* return status after writing the extensions with ech written last */
  10508. static int TLSX_WriteWithEch(WOLFSSL* ssl, byte* output, byte* semaphore,
  10509. byte msgType, word16* pOffset)
  10510. {
  10511. int ret = 0;
  10512. TLSX* echX = NULL;
  10513. TLSX* serverNameX = NULL;
  10514. TLSX** extensions = NULL;
  10515. #ifdef WOLFSSL_SMALL_STACK
  10516. char* tmpServerName = NULL;
  10517. #else
  10518. char tmpServerName[MAX_PUBLIC_NAME_SZ];
  10519. #endif
  10520. /* get the echX from either extensions or ctx */
  10521. if (ssl->extensions)
  10522. echX = TLSX_Find(ssl->extensions, TLSX_ECH);
  10523. if (echX == NULL && ssl->ctx && ssl->ctx->extensions) {
  10524. /* if not NULL the semaphore will stop it from being counted */
  10525. if (echX == NULL)
  10526. echX = TLSX_Find(ssl->ctx->extensions, TLSX_ECH);
  10527. }
  10528. /* if type is outer change sni to public name */
  10529. if (echX != NULL && ((WOLFSSL_ECH*)echX->data)->type == ECH_TYPE_OUTER) {
  10530. if (ssl->extensions) {
  10531. serverNameX = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
  10532. if (serverNameX != NULL)
  10533. extensions = &ssl->extensions;
  10534. }
  10535. if (serverNameX == NULL && ssl->ctx && ssl->ctx->extensions) {
  10536. serverNameX = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
  10537. extensions = &ssl->ctx->extensions;
  10538. }
  10539. /* store the inner server name */
  10540. if (serverNameX != NULL) {
  10541. char* hostName = ((SNI*)serverNameX->data)->data.host_name;
  10542. word32 hostNameSz = (word32)XSTRLEN(hostName) + 1;
  10543. #ifdef WOLFSSL_SMALL_STACK
  10544. tmpServerName = (char*)XMALLOC(hostNameSz, ssl->heap,
  10545. DYNAMIC_TYPE_TMP_BUFFER);
  10546. if (tmpServerName == NULL)
  10547. return MEMORY_E;
  10548. #else
  10549. /* truncate if too long */
  10550. if (hostNameSz > MAX_PUBLIC_NAME_SZ)
  10551. hostNameSz = MAX_PUBLIC_NAME_SZ;
  10552. #endif
  10553. XMEMCPY(tmpServerName, hostName, hostNameSz);
  10554. }
  10555. /* remove the inner server name */
  10556. TLSX_Remove(extensions, TLSX_SERVER_NAME, ssl->heap);
  10557. ret = TLSX_UseSNI(extensions, WOLFSSL_SNI_HOST_NAME,
  10558. ((WOLFSSL_ECH*)echX->data)->echConfig->publicName,
  10559. XSTRLEN(((WOLFSSL_ECH*)echX->data)->echConfig->publicName),
  10560. ssl->heap);
  10561. /* set the public name as the server name */
  10562. if (ret == WOLFSSL_SUCCESS)
  10563. ret = 0;
  10564. }
  10565. if (echX != NULL) {
  10566. /* turn ech on so it doesn't write, then write it last */
  10567. TURN_ON(semaphore, TLSX_ToSemaphore(echX->type));
  10568. }
  10569. if (ret == 0 && ssl->extensions) {
  10570. ret = TLSX_Write(ssl->extensions, output + *pOffset, semaphore,
  10571. msgType, pOffset);
  10572. }
  10573. if (ret == 0 && ssl->ctx && ssl->ctx->extensions) {
  10574. ret = TLSX_Write(ssl->ctx->extensions, output + *pOffset, semaphore,
  10575. msgType, pOffset);
  10576. }
  10577. if (echX != NULL) {
  10578. /* turn off and write it last */
  10579. TURN_OFF(semaphore, TLSX_ToSemaphore(echX->type));
  10580. }
  10581. if (ret == 0 && ssl->extensions) {
  10582. ret = TLSX_Write(ssl->extensions, output + *pOffset, semaphore,
  10583. msgType, pOffset);
  10584. }
  10585. if (ret == 0 && ssl->ctx && ssl->ctx->extensions) {
  10586. ret = TLSX_Write(ssl->ctx->extensions, output + *pOffset, semaphore,
  10587. msgType, pOffset);
  10588. }
  10589. if (serverNameX != NULL) {
  10590. /* remove the public name SNI */
  10591. TLSX_Remove(extensions, TLSX_SERVER_NAME, ssl->heap);
  10592. ret = TLSX_UseSNI(extensions, WOLFSSL_SNI_HOST_NAME, tmpServerName,
  10593. XSTRLEN(tmpServerName), ssl->heap);
  10594. /* restore the inner server name */
  10595. if (ret == WOLFSSL_SUCCESS)
  10596. ret = 0;
  10597. }
  10598. #ifdef WOLFSSL_SMALL_STACK
  10599. XFREE(tmpServerName, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  10600. #endif
  10601. return ret;
  10602. }
  10603. #endif
  10604. /** Writes the extensions to be sent into the client hello. */
  10605. int TLSX_WriteRequest(WOLFSSL* ssl, byte* output, byte msgType, word16* pOffset)
  10606. {
  10607. int ret = 0;
  10608. word16 offset = 0;
  10609. byte semaphore[SEMAPHORE_SIZE] = {0};
  10610. if (!TLSX_SupportExtensions(ssl) || output == NULL)
  10611. return 0;
  10612. offset += OPAQUE16_LEN; /* extensions length */
  10613. if (msgType == client_hello) {
  10614. EC_VALIDATE_REQUEST(ssl, semaphore);
  10615. PF_VALIDATE_REQUEST(ssl, semaphore);
  10616. WOLF_STK_VALIDATE_REQUEST(ssl);
  10617. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10618. if (ssl->suites->hashSigAlgoSz == 0)
  10619. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  10620. #endif
  10621. #ifdef WOLFSSL_TLS13
  10622. if (!IsAtLeastTLSv1_2(ssl)) {
  10623. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10624. }
  10625. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  10626. if (!IsAtLeastTLSv1_3(ssl->version)) {
  10627. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10628. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10629. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PSK_KEY_EXCHANGE_MODES));
  10630. #endif
  10631. #ifdef WOLFSSL_EARLY_DATA
  10632. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  10633. #endif
  10634. #ifdef WOLFSSL_SEND_HRR_COOKIE
  10635. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  10636. #endif
  10637. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  10638. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_POST_HANDSHAKE_AUTH));
  10639. #endif
  10640. }
  10641. #endif
  10642. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10643. /* Must write Pre-shared Key extension at the end in TLS v1.3.
  10644. * Must not write out Pre-shared Key extension in earlier versions of
  10645. * protocol.
  10646. */
  10647. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10648. #endif
  10649. #endif /* WOLFSSL_TLS13 */
  10650. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  10651. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  10652. /* mark already sent, so it won't send it */
  10653. if (!SSL_CM(ssl)->ocspStaplingEnabled) {
  10654. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10655. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  10656. }
  10657. #endif
  10658. }
  10659. #ifdef WOLFSSL_TLS13
  10660. #ifndef NO_CERTS
  10661. else if (msgType == certificate_request) {
  10662. /* Don't send out any extension except those that are turned off. */
  10663. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10664. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10665. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  10666. #endif
  10667. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  10668. * TLSX_CERTIFICATE_AUTHORITIES, TLSX_OID_FILTERS
  10669. * TLSX_STATUS_REQUEST
  10670. */
  10671. }
  10672. #endif
  10673. #endif
  10674. #if defined(HAVE_ECH)
  10675. if (ssl->options.useEch == 1 && msgType == client_hello) {
  10676. ret = TLSX_WriteWithEch(ssl, output, semaphore,
  10677. msgType, &offset);
  10678. if (ret != 0)
  10679. return ret;
  10680. }
  10681. else
  10682. #endif
  10683. {
  10684. if (ssl->extensions) {
  10685. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  10686. msgType, &offset);
  10687. if (ret != 0)
  10688. return ret;
  10689. }
  10690. if (ssl->ctx && ssl->ctx->extensions) {
  10691. ret = TLSX_Write(ssl->ctx->extensions, output + offset, semaphore,
  10692. msgType, &offset);
  10693. if (ret != 0)
  10694. return ret;
  10695. }
  10696. }
  10697. #ifdef HAVE_EXTENDED_MASTER
  10698. if (msgType == client_hello && ssl->options.haveEMS &&
  10699. (!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade)) {
  10700. WOLFSSL_MSG("EMS extension to write");
  10701. c16toa(HELLO_EXT_EXTMS, output + offset);
  10702. offset += HELLO_EXT_TYPE_SZ;
  10703. c16toa(0, output + offset);
  10704. offset += HELLO_EXT_SZ_SZ;
  10705. }
  10706. #endif
  10707. #ifdef WOLFSSL_TLS13
  10708. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10709. if (msgType == client_hello && IsAtLeastTLSv1_3(ssl->version)) {
  10710. /* Write out what we can of Pre-shared key extension. */
  10711. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10712. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  10713. client_hello, &offset);
  10714. if (ret != 0)
  10715. return ret;
  10716. }
  10717. #endif
  10718. #endif
  10719. if (offset > OPAQUE16_LEN || msgType != client_hello)
  10720. c16toa(offset - OPAQUE16_LEN, output); /* extensions length */
  10721. *pOffset += offset;
  10722. return ret;
  10723. }
  10724. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
  10725. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_SERVER)
  10726. /** Tells the buffered size of extensions to be sent into the server hello. */
  10727. int TLSX_GetResponseSize(WOLFSSL* ssl, byte msgType, word16* pLength)
  10728. {
  10729. int ret = 0;
  10730. word16 length = 0;
  10731. byte semaphore[SEMAPHORE_SIZE] = {0};
  10732. switch (msgType) {
  10733. #ifndef NO_WOLFSSL_SERVER
  10734. case server_hello:
  10735. PF_VALIDATE_RESPONSE(ssl, semaphore);
  10736. #ifdef WOLFSSL_TLS13
  10737. if (IsAtLeastTLSv1_3(ssl->version)) {
  10738. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10739. TURN_OFF(semaphore,
  10740. TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10741. #ifdef HAVE_SUPPORTED_CURVES
  10742. if (!ssl->options.noPskDheKe) {
  10743. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10744. }
  10745. #endif
  10746. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10747. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10748. #endif
  10749. #ifdef WOLFSSL_DTLS_CID
  10750. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  10751. #endif
  10752. }
  10753. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  10754. else {
  10755. #ifdef HAVE_SUPPORTED_CURVES
  10756. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10757. #endif
  10758. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10759. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10760. #endif
  10761. }
  10762. #endif
  10763. #endif /* WOLFSSL_TLS13 */
  10764. break;
  10765. #ifdef WOLFSSL_TLS13
  10766. case hello_retry_request:
  10767. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10768. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10769. #ifdef HAVE_SUPPORTED_CURVES
  10770. if (!ssl->options.noPskDheKe)
  10771. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10772. #endif
  10773. #ifdef WOLFSSL_SEND_HRR_COOKIE
  10774. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  10775. #endif
  10776. break;
  10777. #endif
  10778. #ifdef WOLFSSL_TLS13
  10779. case encrypted_extensions:
  10780. /* Send out all extension except those that are turned on. */
  10781. #ifdef HAVE_ECC
  10782. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  10783. #endif
  10784. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10785. #ifdef HAVE_SESSION_TICKET
  10786. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SESSION_TICKET));
  10787. #endif
  10788. #ifdef HAVE_SUPPORTED_CURVES
  10789. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10790. #endif
  10791. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10792. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10793. #endif
  10794. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  10795. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10796. #endif
  10797. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  10798. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  10799. #endif
  10800. #if defined(HAVE_SERVER_RENEGOTIATION_INFO)
  10801. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_RENEGOTIATION_INFO));
  10802. #endif
  10803. #ifdef WOLFSSL_DTLS_CID
  10804. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  10805. #endif /* WOLFSSL_DTLS_CID */
  10806. break;
  10807. #ifdef WOLFSSL_EARLY_DATA
  10808. case session_ticket:
  10809. if (ssl->options.tls1_3) {
  10810. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10811. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  10812. }
  10813. break;
  10814. #endif
  10815. #endif
  10816. #endif
  10817. #ifdef WOLFSSL_TLS13
  10818. #ifndef NO_CERTS
  10819. case certificate:
  10820. /* Don't send out any extension except those that are turned off. */
  10821. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10822. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10823. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  10824. * TLSX_SERVER_CERTIFICATE_TYPE
  10825. */
  10826. break;
  10827. #endif
  10828. #endif
  10829. }
  10830. #ifdef HAVE_EXTENDED_MASTER
  10831. if (ssl->options.haveEMS && msgType == server_hello &&
  10832. !IsAtLeastTLSv1_3(ssl->version)) {
  10833. length += HELLO_EXT_SZ;
  10834. }
  10835. #endif
  10836. if (TLSX_SupportExtensions(ssl)) {
  10837. ret = TLSX_GetSize(ssl->extensions, semaphore, msgType, &length);
  10838. if (ret != 0)
  10839. return ret;
  10840. }
  10841. /* All the response data is set at the ssl object only, so no ctx here. */
  10842. if (length || msgType != server_hello)
  10843. length += OPAQUE16_LEN; /* for total length storage. */
  10844. *pLength += length;
  10845. return ret;
  10846. }
  10847. /** Writes the server hello extensions into a buffer. */
  10848. int TLSX_WriteResponse(WOLFSSL *ssl, byte* output, byte msgType, word16* pOffset)
  10849. {
  10850. int ret = 0;
  10851. word16 offset = 0;
  10852. if (TLSX_SupportExtensions(ssl) && output) {
  10853. byte semaphore[SEMAPHORE_SIZE] = {0};
  10854. switch (msgType) {
  10855. #ifndef NO_WOLFSSL_SERVER
  10856. case server_hello:
  10857. PF_VALIDATE_RESPONSE(ssl, semaphore);
  10858. #ifdef WOLFSSL_TLS13
  10859. if (IsAtLeastTLSv1_3(ssl->version)) {
  10860. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10861. TURN_OFF(semaphore,
  10862. TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10863. #ifdef HAVE_SUPPORTED_CURVES
  10864. if (!ssl->options.noPskDheKe)
  10865. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10866. #endif
  10867. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10868. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10869. #endif
  10870. #ifdef WOLFSSL_DTLS_CID
  10871. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  10872. #endif /* WOLFSSL_DTLS_CID */
  10873. }
  10874. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  10875. else {
  10876. #ifdef HAVE_SUPPORTED_CURVES
  10877. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10878. #endif
  10879. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10880. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10881. #endif
  10882. }
  10883. #endif
  10884. #endif
  10885. break;
  10886. #ifdef WOLFSSL_TLS13
  10887. case hello_retry_request:
  10888. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10889. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10890. #ifdef HAVE_SUPPORTED_CURVES
  10891. if (!ssl->options.noPskDheKe)
  10892. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10893. #endif
  10894. /* Cookie is written below as last extension. */
  10895. break;
  10896. #endif
  10897. #ifdef WOLFSSL_TLS13
  10898. case encrypted_extensions:
  10899. /* Send out all extension except those that are turned on. */
  10900. #ifdef HAVE_ECC
  10901. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  10902. #endif
  10903. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10904. #ifdef HAVE_SESSION_TICKET
  10905. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SESSION_TICKET));
  10906. #endif
  10907. #ifdef HAVE_SUPPORTED_CURVES
  10908. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10909. #endif
  10910. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10911. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10912. #endif
  10913. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  10914. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10915. #endif
  10916. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  10917. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  10918. #endif
  10919. #if defined(HAVE_SERVER_RENEGOTIATION_INFO)
  10920. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_RENEGOTIATION_INFO));
  10921. #endif
  10922. #ifdef WOLFSSL_DTLS_CID
  10923. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  10924. #endif /* WOLFSSL_DTLS_CID */
  10925. break;
  10926. #ifdef WOLFSSL_EARLY_DATA
  10927. case session_ticket:
  10928. if (ssl->options.tls1_3) {
  10929. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10930. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  10931. }
  10932. break;
  10933. #endif
  10934. #endif
  10935. #endif
  10936. #ifdef WOLFSSL_TLS13
  10937. #ifndef NO_CERTS
  10938. case certificate:
  10939. /* Don't send out any extension except those that are turned
  10940. * off. */
  10941. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10942. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10943. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  10944. * TLSX_SERVER_CERTIFICATE_TYPE
  10945. */
  10946. break;
  10947. #endif
  10948. #endif
  10949. default:
  10950. break;
  10951. }
  10952. offset += OPAQUE16_LEN; /* extensions length */
  10953. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  10954. msgType, &offset);
  10955. if (ret != 0)
  10956. return ret;
  10957. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  10958. if (msgType == hello_retry_request) {
  10959. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10960. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  10961. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  10962. msgType, &offset);
  10963. if (ret != 0)
  10964. return ret;
  10965. }
  10966. #endif
  10967. #ifdef HAVE_EXTENDED_MASTER
  10968. if (ssl->options.haveEMS && msgType == server_hello &&
  10969. !IsAtLeastTLSv1_3(ssl->version)) {
  10970. WOLFSSL_MSG("EMS extension to write");
  10971. c16toa(HELLO_EXT_EXTMS, output + offset);
  10972. offset += HELLO_EXT_TYPE_SZ;
  10973. c16toa(0, output + offset);
  10974. offset += HELLO_EXT_SZ_SZ;
  10975. }
  10976. #endif
  10977. if (offset > OPAQUE16_LEN || msgType != server_hello)
  10978. c16toa(offset - OPAQUE16_LEN, output); /* extensions length */
  10979. }
  10980. if (pOffset)
  10981. *pOffset += offset;
  10982. return ret;
  10983. }
  10984. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_SERVER */
  10985. #ifdef WOLFSSL_TLS13
  10986. int TLSX_ParseVersion(WOLFSSL* ssl, const byte* input, word16 length,
  10987. byte msgType, int* found)
  10988. {
  10989. int ret = 0;
  10990. int offset = 0;
  10991. *found = 0;
  10992. while (offset < (int)length) {
  10993. word16 type;
  10994. word16 size;
  10995. if (offset + (2 * OPAQUE16_LEN) > length) {
  10996. ret = BUFFER_ERROR;
  10997. break;
  10998. }
  10999. ato16(input + offset, &type);
  11000. offset += HELLO_EXT_TYPE_SZ;
  11001. ato16(input + offset, &size);
  11002. offset += OPAQUE16_LEN;
  11003. if (offset + size > length) {
  11004. ret = BUFFER_ERROR;
  11005. break;
  11006. }
  11007. if (type == TLSX_SUPPORTED_VERSIONS) {
  11008. *found = 1;
  11009. WOLFSSL_MSG("Supported Versions extension received");
  11010. ret = SV_PARSE(ssl, input + offset, size, msgType);
  11011. break;
  11012. }
  11013. offset += size;
  11014. }
  11015. return ret;
  11016. }
  11017. #endif
  11018. /** Parses a buffer of TLS extensions. */
  11019. int TLSX_Parse(WOLFSSL* ssl, const byte* input, word16 length, byte msgType,
  11020. Suites *suites)
  11021. {
  11022. int ret = 0;
  11023. word16 offset = 0;
  11024. byte isRequest = (msgType == client_hello ||
  11025. msgType == certificate_request);
  11026. #ifdef HAVE_EXTENDED_MASTER
  11027. byte pendingEMS = 0;
  11028. #endif
  11029. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  11030. int pskDone = 0;
  11031. #endif
  11032. byte seenType[SEMAPHORE_SIZE]; /* Seen known extensions. */
  11033. if (!ssl || !input || (isRequest && !suites))
  11034. return BAD_FUNC_ARG;
  11035. /* No known extensions seen yet. */
  11036. XMEMSET(seenType, 0, sizeof(seenType));
  11037. while (ret == 0 && offset < length) {
  11038. word16 type;
  11039. word16 size;
  11040. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  11041. if (msgType == client_hello && pskDone) {
  11042. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  11043. return PSK_KEY_ERROR;
  11044. }
  11045. #endif
  11046. if (length - offset < HELLO_EXT_TYPE_SZ + OPAQUE16_LEN)
  11047. return BUFFER_ERROR;
  11048. ato16(input + offset, &type);
  11049. offset += HELLO_EXT_TYPE_SZ;
  11050. ato16(input + offset, &size);
  11051. offset += OPAQUE16_LEN;
  11052. /* Check we have a bit for extension type. */
  11053. if ((type <= 62) || (type == TLSX_RENEGOTIATION_INFO)
  11054. #ifdef WOLFSSL_QUIC
  11055. || (type == TLSX_KEY_QUIC_TP_PARAMS_DRAFT)
  11056. #endif
  11057. )
  11058. {
  11059. /* Detect duplicate recognized extensions. */
  11060. if (IS_OFF(seenType, TLSX_ToSemaphore(type))) {
  11061. TURN_ON(seenType, TLSX_ToSemaphore(type));
  11062. }
  11063. else {
  11064. return DUPLICATE_TLS_EXT_E;
  11065. }
  11066. }
  11067. if (length - offset < size)
  11068. return BUFFER_ERROR;
  11069. switch (type) {
  11070. #ifdef HAVE_SNI
  11071. case TLSX_SERVER_NAME:
  11072. WOLFSSL_MSG("SNI extension received");
  11073. #ifdef WOLFSSL_DEBUG_TLS
  11074. WOLFSSL_BUFFER(input + offset, size);
  11075. #endif
  11076. #ifdef WOLFSSL_TLS13
  11077. if (IsAtLeastTLSv1_3(ssl->version)) {
  11078. if (msgType != client_hello &&
  11079. msgType != encrypted_extensions)
  11080. return EXT_NOT_ALLOWED;
  11081. }
  11082. else
  11083. #endif
  11084. {
  11085. if (msgType != client_hello &&
  11086. msgType != server_hello)
  11087. return EXT_NOT_ALLOWED;
  11088. }
  11089. ret = SNI_PARSE(ssl, input + offset, size, isRequest);
  11090. break;
  11091. #endif
  11092. case TLSX_TRUSTED_CA_KEYS:
  11093. WOLFSSL_MSG("Trusted CA extension received");
  11094. #ifdef WOLFSSL_DEBUG_TLS
  11095. WOLFSSL_BUFFER(input + offset, size);
  11096. #endif
  11097. #ifdef WOLFSSL_TLS13
  11098. if (IsAtLeastTLSv1_3(ssl->version)) {
  11099. if (msgType != client_hello &&
  11100. msgType != encrypted_extensions)
  11101. return EXT_NOT_ALLOWED;
  11102. }
  11103. else
  11104. #endif
  11105. {
  11106. if (msgType != client_hello)
  11107. return EXT_NOT_ALLOWED;
  11108. }
  11109. ret = TCA_PARSE(ssl, input + offset, size, isRequest);
  11110. break;
  11111. case TLSX_MAX_FRAGMENT_LENGTH:
  11112. WOLFSSL_MSG("Max Fragment Length extension received");
  11113. #ifdef WOLFSSL_DEBUG_TLS
  11114. WOLFSSL_BUFFER(input + offset, size);
  11115. #endif
  11116. #ifdef WOLFSSL_TLS13
  11117. if (IsAtLeastTLSv1_3(ssl->version)) {
  11118. if (msgType != client_hello &&
  11119. msgType != encrypted_extensions) {
  11120. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11121. return EXT_NOT_ALLOWED;
  11122. }
  11123. }
  11124. else
  11125. #endif
  11126. {
  11127. if (msgType != client_hello &&
  11128. msgType != server_hello) {
  11129. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11130. return EXT_NOT_ALLOWED;
  11131. }
  11132. }
  11133. ret = MFL_PARSE(ssl, input + offset, size, isRequest);
  11134. break;
  11135. case TLSX_TRUNCATED_HMAC:
  11136. WOLFSSL_MSG("Truncated HMAC extension received");
  11137. #ifdef WOLFSSL_DEBUG_TLS
  11138. WOLFSSL_BUFFER(input + offset, size);
  11139. #endif
  11140. #ifdef WOLFSSL_TLS13
  11141. if (IsAtLeastTLSv1_3(ssl->version))
  11142. break;
  11143. #endif
  11144. if (msgType != client_hello)
  11145. return EXT_NOT_ALLOWED;
  11146. ret = THM_PARSE(ssl, input + offset, size, isRequest);
  11147. break;
  11148. case TLSX_SUPPORTED_GROUPS:
  11149. WOLFSSL_MSG("Supported Groups extension received");
  11150. #ifdef WOLFSSL_DEBUG_TLS
  11151. WOLFSSL_BUFFER(input + offset, size);
  11152. #endif
  11153. #ifdef WOLFSSL_TLS13
  11154. if (IsAtLeastTLSv1_3(ssl->version)) {
  11155. if (msgType != client_hello &&
  11156. msgType != encrypted_extensions) {
  11157. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11158. return EXT_NOT_ALLOWED;
  11159. }
  11160. }
  11161. else
  11162. #endif
  11163. {
  11164. if (msgType != client_hello) {
  11165. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11166. return EXT_NOT_ALLOWED;
  11167. }
  11168. }
  11169. ret = EC_PARSE(ssl, input + offset, size, isRequest);
  11170. break;
  11171. case TLSX_EC_POINT_FORMATS:
  11172. WOLFSSL_MSG("Point Formats extension received");
  11173. #ifdef WOLFSSL_DEBUG_TLS
  11174. WOLFSSL_BUFFER(input + offset, size);
  11175. #endif
  11176. #ifdef WOLFSSL_TLS13
  11177. if (IsAtLeastTLSv1_3(ssl->version))
  11178. break;
  11179. #endif
  11180. if (msgType != client_hello &&
  11181. msgType != server_hello) {
  11182. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11183. return EXT_NOT_ALLOWED;
  11184. }
  11185. ret = PF_PARSE(ssl, input + offset, size, isRequest);
  11186. break;
  11187. case TLSX_STATUS_REQUEST:
  11188. WOLFSSL_MSG("Certificate Status Request extension received");
  11189. #ifdef WOLFSSL_DEBUG_TLS
  11190. WOLFSSL_BUFFER(input + offset, size);
  11191. #endif
  11192. #ifdef WOLFSSL_TLS13
  11193. if (IsAtLeastTLSv1_3(ssl->version)) {
  11194. if (msgType != client_hello &&
  11195. msgType != certificate_request &&
  11196. msgType != certificate)
  11197. return EXT_NOT_ALLOWED;
  11198. }
  11199. else
  11200. #endif
  11201. {
  11202. if (msgType != client_hello &&
  11203. msgType != server_hello)
  11204. return EXT_NOT_ALLOWED;
  11205. }
  11206. ret = CSR_PARSE(ssl, input + offset, size, isRequest);
  11207. break;
  11208. case TLSX_STATUS_REQUEST_V2:
  11209. WOLFSSL_MSG("Certificate Status Request v2 extension received");
  11210. #ifdef WOLFSSL_DEBUG_TLS
  11211. WOLFSSL_BUFFER(input + offset, size);
  11212. #endif
  11213. #if defined(WOLFSSL_TLS13) && defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  11214. if (IsAtLeastTLSv1_3(ssl->version)) {
  11215. if (msgType != client_hello &&
  11216. msgType != certificate_request &&
  11217. msgType != certificate)
  11218. return EXT_NOT_ALLOWED;
  11219. }
  11220. else
  11221. #endif
  11222. {
  11223. if (msgType != client_hello &&
  11224. msgType != server_hello)
  11225. return EXT_NOT_ALLOWED;
  11226. }
  11227. ret = CSR2_PARSE(ssl, input + offset, size, isRequest);
  11228. break;
  11229. #ifdef HAVE_EXTENDED_MASTER
  11230. case HELLO_EXT_EXTMS:
  11231. WOLFSSL_MSG("Extended Master Secret extension received");
  11232. #ifdef WOLFSSL_DEBUG_TLS
  11233. WOLFSSL_BUFFER(input + offset, size);
  11234. #endif
  11235. #if defined(WOLFSSL_TLS13)
  11236. if (IsAtLeastTLSv1_3(ssl->version))
  11237. break;
  11238. #endif
  11239. if (msgType != client_hello &&
  11240. msgType != server_hello)
  11241. return EXT_NOT_ALLOWED;
  11242. if (size != 0)
  11243. return BUFFER_ERROR;
  11244. #ifndef NO_WOLFSSL_SERVER
  11245. if (isRequest)
  11246. ssl->options.haveEMS = 1;
  11247. #endif
  11248. pendingEMS = 1;
  11249. break;
  11250. #endif
  11251. case TLSX_RENEGOTIATION_INFO:
  11252. WOLFSSL_MSG("Secure Renegotiation extension received");
  11253. #ifdef WOLFSSL_DEBUG_TLS
  11254. WOLFSSL_BUFFER(input + offset, size);
  11255. #endif
  11256. #ifdef WOLFSSL_TLS13
  11257. if (IsAtLeastTLSv1_3(ssl->version))
  11258. break;
  11259. #endif
  11260. if (msgType != client_hello &&
  11261. msgType != server_hello)
  11262. return EXT_NOT_ALLOWED;
  11263. ret = SCR_PARSE(ssl, input + offset, size, isRequest);
  11264. break;
  11265. case TLSX_SESSION_TICKET:
  11266. WOLFSSL_MSG("Session Ticket extension received");
  11267. #ifdef WOLFSSL_DEBUG_TLS
  11268. WOLFSSL_BUFFER(input + offset, size);
  11269. #endif
  11270. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)
  11271. if (IsAtLeastTLSv1_3(ssl->version)) {
  11272. if (msgType != client_hello)
  11273. return EXT_NOT_ALLOWED;
  11274. }
  11275. else
  11276. #endif
  11277. {
  11278. if (msgType != client_hello &&
  11279. msgType != server_hello)
  11280. return EXT_NOT_ALLOWED;
  11281. }
  11282. ret = WOLF_STK_PARSE(ssl, input + offset, size, isRequest);
  11283. break;
  11284. case TLSX_APPLICATION_LAYER_PROTOCOL:
  11285. WOLFSSL_MSG("ALPN extension received");
  11286. #ifdef WOLFSSL_DEBUG_TLS
  11287. WOLFSSL_BUFFER(input + offset, size);
  11288. #endif
  11289. #if defined(WOLFSSL_TLS13) && defined(HAVE_ALPN)
  11290. if (IsAtLeastTLSv1_3(ssl->version)) {
  11291. if (msgType != client_hello &&
  11292. msgType != encrypted_extensions)
  11293. return EXT_NOT_ALLOWED;
  11294. }
  11295. else
  11296. #endif
  11297. {
  11298. if (msgType != client_hello &&
  11299. msgType != server_hello)
  11300. return EXT_NOT_ALLOWED;
  11301. }
  11302. ret = ALPN_PARSE(ssl, input + offset, size, isRequest);
  11303. break;
  11304. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  11305. case TLSX_SIGNATURE_ALGORITHMS:
  11306. WOLFSSL_MSG("Signature Algorithms extension received");
  11307. #ifdef WOLFSSL_DEBUG_TLS
  11308. WOLFSSL_BUFFER(input + offset, size);
  11309. #endif
  11310. if (!IsAtLeastTLSv1_2(ssl))
  11311. break;
  11312. #ifdef WOLFSSL_TLS13
  11313. if (IsAtLeastTLSv1_3(ssl->version)) {
  11314. if (msgType != client_hello &&
  11315. msgType != certificate_request)
  11316. return EXT_NOT_ALLOWED;
  11317. }
  11318. else
  11319. #endif
  11320. {
  11321. if (msgType != client_hello)
  11322. return EXT_NOT_ALLOWED;
  11323. }
  11324. ret = SA_PARSE(ssl, input + offset, size, isRequest, suites);
  11325. break;
  11326. #endif
  11327. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  11328. case TLSX_ENCRYPT_THEN_MAC:
  11329. WOLFSSL_MSG("Encrypt-Then-Mac extension received");
  11330. /* Ignore for TLS 1.3+ */
  11331. if (IsAtLeastTLSv1_3(ssl->version))
  11332. break;
  11333. if (msgType != client_hello &&
  11334. msgType != server_hello)
  11335. return EXT_NOT_ALLOWED;
  11336. ret = ETM_PARSE(ssl, input + offset, size, msgType);
  11337. break;
  11338. #endif /* HAVE_ENCRYPT_THEN_MAC */
  11339. #ifdef WOLFSSL_TLS13
  11340. case TLSX_SUPPORTED_VERSIONS:
  11341. WOLFSSL_MSG("Skipping Supported Versions - already processed");
  11342. #ifdef WOLFSSL_DEBUG_TLS
  11343. WOLFSSL_BUFFER(input + offset, size);
  11344. #endif
  11345. if (msgType != client_hello &&
  11346. msgType != server_hello &&
  11347. msgType != hello_retry_request)
  11348. return EXT_NOT_ALLOWED;
  11349. break;
  11350. #ifdef WOLFSSL_SEND_HRR_COOKIE
  11351. case TLSX_COOKIE:
  11352. WOLFSSL_MSG("Cookie extension received");
  11353. #ifdef WOLFSSL_DEBUG_TLS
  11354. WOLFSSL_BUFFER(input + offset, size);
  11355. #endif
  11356. if (!IsAtLeastTLSv1_3(ssl->version))
  11357. break;
  11358. if (msgType != client_hello &&
  11359. msgType != hello_retry_request) {
  11360. return EXT_NOT_ALLOWED;
  11361. }
  11362. ret = CKE_PARSE(ssl, input + offset, size, msgType);
  11363. break;
  11364. #endif
  11365. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11366. case TLSX_PRE_SHARED_KEY:
  11367. WOLFSSL_MSG("Pre-Shared Key extension received");
  11368. #ifdef WOLFSSL_DEBUG_TLS
  11369. WOLFSSL_BUFFER(input + offset, size);
  11370. #endif
  11371. if (!IsAtLeastTLSv1_3(ssl->version))
  11372. break;
  11373. if (msgType != client_hello &&
  11374. msgType != server_hello) {
  11375. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11376. return EXT_NOT_ALLOWED;
  11377. }
  11378. ret = PSK_PARSE(ssl, input + offset, size, msgType);
  11379. pskDone = 1;
  11380. break;
  11381. case TLSX_PSK_KEY_EXCHANGE_MODES:
  11382. WOLFSSL_MSG("PSK Key Exchange Modes extension received");
  11383. #ifdef WOLFSSL_DEBUG_TLS
  11384. WOLFSSL_BUFFER(input + offset, size);
  11385. #endif
  11386. if (!IsAtLeastTLSv1_3(ssl->version))
  11387. break;
  11388. if (msgType != client_hello) {
  11389. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11390. return EXT_NOT_ALLOWED;
  11391. }
  11392. ret = PKM_PARSE(ssl, input + offset, size, msgType);
  11393. break;
  11394. #endif
  11395. #ifdef WOLFSSL_EARLY_DATA
  11396. case TLSX_EARLY_DATA:
  11397. WOLFSSL_MSG("Early Data extension received");
  11398. #ifdef WOLFSSL_DEBUG_TLS
  11399. WOLFSSL_BUFFER(input + offset, size);
  11400. #endif
  11401. if (!IsAtLeastTLSv1_3(ssl->version))
  11402. break;
  11403. if (msgType != client_hello && msgType != session_ticket &&
  11404. msgType != encrypted_extensions) {
  11405. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11406. return EXT_NOT_ALLOWED;
  11407. }
  11408. ret = EDI_PARSE(ssl, input + offset, size, msgType);
  11409. break;
  11410. #endif
  11411. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  11412. case TLSX_POST_HANDSHAKE_AUTH:
  11413. WOLFSSL_MSG("Post Handshake Authentication extension received");
  11414. #ifdef WOLFSSL_DEBUG_TLS
  11415. WOLFSSL_BUFFER(input + offset, size);
  11416. #endif
  11417. if (!IsAtLeastTLSv1_3(ssl->version))
  11418. break;
  11419. if (msgType != client_hello) {
  11420. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11421. return EXT_NOT_ALLOWED;
  11422. }
  11423. ret = PHA_PARSE(ssl, input + offset, size, msgType);
  11424. break;
  11425. #endif
  11426. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  11427. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  11428. WOLFSSL_MSG("Signature Algorithms extension received");
  11429. #ifdef WOLFSSL_DEBUG_TLS
  11430. WOLFSSL_BUFFER(input + offset, size);
  11431. #endif
  11432. if (!IsAtLeastTLSv1_3(ssl->version))
  11433. break;
  11434. if (msgType != client_hello &&
  11435. msgType != certificate_request) {
  11436. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11437. return EXT_NOT_ALLOWED;
  11438. }
  11439. ret = SAC_PARSE(ssl, input + offset, size, isRequest);
  11440. break;
  11441. #endif
  11442. case TLSX_KEY_SHARE:
  11443. WOLFSSL_MSG("Key Share extension received");
  11444. #ifdef WOLFSSL_DEBUG_TLS
  11445. WOLFSSL_BUFFER(input + offset, size);
  11446. #endif
  11447. #ifdef HAVE_SUPPORTED_CURVES
  11448. if (!IsAtLeastTLSv1_3(ssl->version))
  11449. break;
  11450. if (msgType != client_hello && msgType != server_hello &&
  11451. msgType != hello_retry_request) {
  11452. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11453. return EXT_NOT_ALLOWED;
  11454. }
  11455. #endif
  11456. ret = KS_PARSE(ssl, input + offset, size, msgType);
  11457. break;
  11458. #endif
  11459. #ifdef WOLFSSL_SRTP
  11460. case TLSX_USE_SRTP:
  11461. WOLFSSL_MSG("Use SRTP extension received");
  11462. ret = SRTP_PARSE(ssl, input + offset, size, isRequest);
  11463. break;
  11464. #endif
  11465. #ifdef WOLFSSL_QUIC
  11466. case TLSX_KEY_QUIC_TP_PARAMS:
  11467. FALL_THROUGH;
  11468. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  11469. WOLFSSL_MSG("QUIC transport parameter received");
  11470. #ifdef WOLFSSL_DEBUG_TLS
  11471. WOLFSSL_BUFFER(input + offset, size);
  11472. #endif
  11473. if (IsAtLeastTLSv1_3(ssl->version) &&
  11474. msgType != client_hello &&
  11475. msgType != server_hello &&
  11476. msgType != encrypted_extensions) {
  11477. return EXT_NOT_ALLOWED;
  11478. }
  11479. else if (!IsAtLeastTLSv1_3(ssl->version) &&
  11480. msgType == encrypted_extensions) {
  11481. return EXT_NOT_ALLOWED;
  11482. }
  11483. else if (WOLFSSL_IS_QUIC(ssl)) {
  11484. ret = QTP_PARSE(ssl, input + offset, size, type, msgType);
  11485. }
  11486. else {
  11487. WOLFSSL_MSG("QUIC transport param TLS extension type, but no QUIC");
  11488. return EXT_NOT_ALLOWED; /* be safe, this should not happen */
  11489. }
  11490. break;
  11491. #endif /* WOLFSSL_QUIC */
  11492. #if defined(WOLFSSL_DTLS_CID)
  11493. case TLSX_CONNECTION_ID:
  11494. /* connection ID not supported in DTLSv1.2 */
  11495. if (!IsAtLeastTLSv1_3(ssl->version))
  11496. break;
  11497. if (msgType != client_hello && msgType != server_hello)
  11498. return EXT_NOT_ALLOWED;
  11499. WOLFSSL_MSG("ConnectionID extension received");
  11500. ret = CID_PARSE(ssl, input + offset, size, isRequest);
  11501. break;
  11502. #endif /* defined(WOLFSSL_DTLS_CID) */
  11503. #if defined(HAVE_ECH)
  11504. case TLSX_ECH:
  11505. ret = ECH_PARSE(ssl, input + offset, size, msgType);
  11506. break;
  11507. #endif
  11508. default:
  11509. WOLFSSL_MSG("Unknown TLS extension type");
  11510. }
  11511. /* offset should be updated here! */
  11512. offset += size;
  11513. }
  11514. #ifdef HAVE_EXTENDED_MASTER
  11515. if (IsAtLeastTLSv1_3(ssl->version) && msgType == hello_retry_request) {
  11516. /* Don't change EMS status until server_hello received.
  11517. * Second ClientHello must have same extensions.
  11518. */
  11519. }
  11520. else if (!isRequest && ssl->options.haveEMS && !pendingEMS)
  11521. ssl->options.haveEMS = 0;
  11522. #endif
  11523. if (ret == 0)
  11524. ret = SNI_VERIFY_PARSE(ssl, isRequest);
  11525. if (ret == 0)
  11526. ret = TCA_VERIFY_PARSE(ssl, isRequest);
  11527. return ret;
  11528. }
  11529. /* undefining semaphore macros */
  11530. #undef IS_OFF
  11531. #undef TURN_ON
  11532. #undef SEMAPHORE_SIZE
  11533. #endif /* HAVE_TLS_EXTENSIONS */
  11534. #ifndef NO_WOLFSSL_CLIENT
  11535. WOLFSSL_METHOD* wolfTLS_client_method(void)
  11536. {
  11537. return wolfTLS_client_method_ex(NULL);
  11538. }
  11539. WOLFSSL_METHOD* wolfTLS_client_method_ex(void* heap)
  11540. {
  11541. WOLFSSL_METHOD* method =
  11542. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11543. heap, DYNAMIC_TYPE_METHOD);
  11544. (void)heap;
  11545. WOLFSSL_ENTER("TLS_client_method_ex");
  11546. if (method) {
  11547. #if defined(WOLFSSL_TLS13)
  11548. InitSSL_Method(method, MakeTLSv1_3());
  11549. #elif !defined(WOLFSSL_NO_TLS12)
  11550. InitSSL_Method(method, MakeTLSv1_2());
  11551. #elif !defined(NO_OLD_TLS)
  11552. InitSSL_Method(method, MakeTLSv1_1());
  11553. #elif defined(WOLFSSL_ALLOW_TLSV10)
  11554. InitSSL_Method(method, MakeTLSv1());
  11555. #else
  11556. #error No TLS version enabled!
  11557. #endif
  11558. method->downgrade = 1;
  11559. method->side = WOLFSSL_CLIENT_END;
  11560. }
  11561. return method;
  11562. }
  11563. #ifndef NO_OLD_TLS
  11564. #ifdef WOLFSSL_ALLOW_TLSV10
  11565. WOLFSSL_METHOD* wolfTLSv1_client_method(void)
  11566. {
  11567. return wolfTLSv1_client_method_ex(NULL);
  11568. }
  11569. WOLFSSL_METHOD* wolfTLSv1_client_method_ex(void* heap)
  11570. {
  11571. WOLFSSL_METHOD* method =
  11572. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11573. heap, DYNAMIC_TYPE_METHOD);
  11574. (void)heap;
  11575. WOLFSSL_ENTER("TLSv1_client_method_ex");
  11576. if (method)
  11577. InitSSL_Method(method, MakeTLSv1());
  11578. return method;
  11579. }
  11580. #endif /* WOLFSSL_ALLOW_TLSV10 */
  11581. WOLFSSL_METHOD* wolfTLSv1_1_client_method(void)
  11582. {
  11583. return wolfTLSv1_1_client_method_ex(NULL);
  11584. }
  11585. WOLFSSL_METHOD* wolfTLSv1_1_client_method_ex(void* heap)
  11586. {
  11587. WOLFSSL_METHOD* method =
  11588. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11589. heap, DYNAMIC_TYPE_METHOD);
  11590. (void)heap;
  11591. WOLFSSL_ENTER("TLSv1_1_client_method_ex");
  11592. if (method)
  11593. InitSSL_Method(method, MakeTLSv1_1());
  11594. return method;
  11595. }
  11596. #endif /* !NO_OLD_TLS */
  11597. #ifndef WOLFSSL_NO_TLS12
  11598. WOLFSSL_ABI
  11599. WOLFSSL_METHOD* wolfTLSv1_2_client_method(void)
  11600. {
  11601. return wolfTLSv1_2_client_method_ex(NULL);
  11602. }
  11603. WOLFSSL_METHOD* wolfTLSv1_2_client_method_ex(void* heap)
  11604. {
  11605. WOLFSSL_METHOD* method =
  11606. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11607. heap, DYNAMIC_TYPE_METHOD);
  11608. (void)heap;
  11609. WOLFSSL_ENTER("TLSv1_2_client_method_ex");
  11610. if (method)
  11611. InitSSL_Method(method, MakeTLSv1_2());
  11612. return method;
  11613. }
  11614. #endif /* WOLFSSL_NO_TLS12 */
  11615. #ifdef WOLFSSL_TLS13
  11616. /* The TLS v1.3 client method data.
  11617. *
  11618. * returns the method data for a TLS v1.3 client.
  11619. */
  11620. WOLFSSL_ABI
  11621. WOLFSSL_METHOD* wolfTLSv1_3_client_method(void)
  11622. {
  11623. return wolfTLSv1_3_client_method_ex(NULL);
  11624. }
  11625. /* The TLS v1.3 client method data.
  11626. *
  11627. * heap The heap used for allocation.
  11628. * returns the method data for a TLS v1.3 client.
  11629. */
  11630. WOLFSSL_METHOD* wolfTLSv1_3_client_method_ex(void* heap)
  11631. {
  11632. WOLFSSL_METHOD* method = (WOLFSSL_METHOD*)
  11633. XMALLOC(sizeof(WOLFSSL_METHOD), heap,
  11634. DYNAMIC_TYPE_METHOD);
  11635. (void)heap;
  11636. WOLFSSL_ENTER("TLSv1_3_client_method_ex");
  11637. if (method)
  11638. InitSSL_Method(method, MakeTLSv1_3());
  11639. return method;
  11640. }
  11641. #endif /* WOLFSSL_TLS13 */
  11642. #ifdef WOLFSSL_DTLS
  11643. WOLFSSL_METHOD* wolfDTLS_client_method(void)
  11644. {
  11645. return wolfDTLS_client_method_ex(NULL);
  11646. }
  11647. WOLFSSL_METHOD* wolfDTLS_client_method_ex(void* heap)
  11648. {
  11649. WOLFSSL_METHOD* method =
  11650. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11651. heap, DYNAMIC_TYPE_METHOD);
  11652. (void)heap;
  11653. WOLFSSL_ENTER("DTLS_client_method_ex");
  11654. if (method) {
  11655. #if defined(WOLFSSL_DTLS13)
  11656. InitSSL_Method(method, MakeDTLSv1_3());
  11657. #elif !defined(WOLFSSL_NO_TLS12)
  11658. InitSSL_Method(method, MakeDTLSv1_2());
  11659. #elif !defined(NO_OLD_TLS)
  11660. InitSSL_Method(method, MakeDTLSv1());
  11661. #else
  11662. #error No DTLS version enabled!
  11663. #endif
  11664. method->downgrade = 1;
  11665. method->side = WOLFSSL_CLIENT_END;
  11666. }
  11667. return method;
  11668. }
  11669. #ifndef NO_OLD_TLS
  11670. WOLFSSL_METHOD* wolfDTLSv1_client_method(void)
  11671. {
  11672. return wolfDTLSv1_client_method_ex(NULL);
  11673. }
  11674. WOLFSSL_METHOD* wolfDTLSv1_client_method_ex(void* heap)
  11675. {
  11676. WOLFSSL_METHOD* method =
  11677. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11678. heap, DYNAMIC_TYPE_METHOD);
  11679. (void)heap;
  11680. WOLFSSL_ENTER("DTLSv1_client_method_ex");
  11681. if (method)
  11682. InitSSL_Method(method, MakeDTLSv1());
  11683. return method;
  11684. }
  11685. #endif /* NO_OLD_TLS */
  11686. #ifndef WOLFSSL_NO_TLS12
  11687. WOLFSSL_METHOD* wolfDTLSv1_2_client_method(void)
  11688. {
  11689. return wolfDTLSv1_2_client_method_ex(NULL);
  11690. }
  11691. WOLFSSL_METHOD* wolfDTLSv1_2_client_method_ex(void* heap)
  11692. {
  11693. WOLFSSL_METHOD* method =
  11694. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11695. heap, DYNAMIC_TYPE_METHOD);
  11696. (void)heap;
  11697. WOLFSSL_ENTER("DTLSv1_2_client_method_ex");
  11698. if (method)
  11699. InitSSL_Method(method, MakeDTLSv1_2());
  11700. (void)heap;
  11701. return method;
  11702. }
  11703. #endif /* !WOLFSSL_NO_TLS12 */
  11704. #endif /* WOLFSSL_DTLS */
  11705. #endif /* NO_WOLFSSL_CLIENT */
  11706. /* EITHER SIDE METHODS */
  11707. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  11708. #ifndef NO_OLD_TLS
  11709. #ifdef WOLFSSL_ALLOW_TLSV10
  11710. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  11711. *
  11712. * Returns a pointer to a WOLFSSL_METHOD struct
  11713. */
  11714. WOLFSSL_METHOD* wolfTLSv1_method(void)
  11715. {
  11716. return wolfTLSv1_method_ex(NULL);
  11717. }
  11718. WOLFSSL_METHOD* wolfTLSv1_method_ex(void* heap)
  11719. {
  11720. WOLFSSL_METHOD* m;
  11721. WOLFSSL_ENTER("TLSv1_method");
  11722. #ifndef NO_WOLFSSL_CLIENT
  11723. m = wolfTLSv1_client_method_ex(heap);
  11724. #else
  11725. m = wolfTLSv1_server_method_ex(heap);
  11726. #endif
  11727. if (m != NULL) {
  11728. m->side = WOLFSSL_NEITHER_END;
  11729. }
  11730. return m;
  11731. }
  11732. #endif /* WOLFSSL_ALLOW_TLSV10 */
  11733. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  11734. *
  11735. * Returns a pointer to a WOLFSSL_METHOD struct
  11736. */
  11737. WOLFSSL_METHOD* wolfTLSv1_1_method(void)
  11738. {
  11739. return wolfTLSv1_1_method_ex(NULL);
  11740. }
  11741. WOLFSSL_METHOD* wolfTLSv1_1_method_ex(void* heap)
  11742. {
  11743. WOLFSSL_METHOD* m;
  11744. WOLFSSL_ENTER("TLSv1_1_method");
  11745. #ifndef NO_WOLFSSL_CLIENT
  11746. m = wolfTLSv1_1_client_method_ex(heap);
  11747. #else
  11748. m = wolfTLSv1_1_server_method_ex(heap);
  11749. #endif
  11750. if (m != NULL) {
  11751. m->side = WOLFSSL_NEITHER_END;
  11752. }
  11753. return m;
  11754. }
  11755. #endif /* !NO_OLD_TLS */
  11756. #ifndef WOLFSSL_NO_TLS12
  11757. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  11758. *
  11759. * Returns a pointer to a WOLFSSL_METHOD struct
  11760. */
  11761. WOLFSSL_METHOD* wolfTLSv1_2_method(void)
  11762. {
  11763. return wolfTLSv1_2_method_ex(NULL);
  11764. }
  11765. WOLFSSL_METHOD* wolfTLSv1_2_method_ex(void* heap)
  11766. {
  11767. WOLFSSL_METHOD* m;
  11768. WOLFSSL_ENTER("TLSv1_2_method");
  11769. #ifndef NO_WOLFSSL_CLIENT
  11770. m = wolfTLSv1_2_client_method_ex(heap);
  11771. #else
  11772. m = wolfTLSv1_2_server_method_ex(heap);
  11773. #endif
  11774. if (m != NULL) {
  11775. m->side = WOLFSSL_NEITHER_END;
  11776. }
  11777. return m;
  11778. }
  11779. #endif /* !WOLFSSL_NO_TLS12 */
  11780. #ifdef WOLFSSL_TLS13
  11781. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  11782. *
  11783. * Returns a pointer to a WOLFSSL_METHOD struct
  11784. */
  11785. WOLFSSL_METHOD* wolfTLSv1_3_method(void)
  11786. {
  11787. return wolfTLSv1_3_method_ex(NULL);
  11788. }
  11789. WOLFSSL_METHOD* wolfTLSv1_3_method_ex(void* heap)
  11790. {
  11791. WOLFSSL_METHOD* m;
  11792. WOLFSSL_ENTER("TLSv1_3_method");
  11793. #ifndef NO_WOLFSSL_CLIENT
  11794. m = wolfTLSv1_3_client_method_ex(heap);
  11795. #else
  11796. m = wolfTLSv1_3_server_method_ex(heap);
  11797. #endif
  11798. if (m != NULL) {
  11799. m->side = WOLFSSL_NEITHER_END;
  11800. }
  11801. return m;
  11802. }
  11803. #endif /* WOLFSSL_TLS13 */
  11804. #ifdef WOLFSSL_DTLS
  11805. WOLFSSL_METHOD* wolfDTLS_method(void)
  11806. {
  11807. return wolfDTLS_method_ex(NULL);
  11808. }
  11809. WOLFSSL_METHOD* wolfDTLS_method_ex(void* heap)
  11810. {
  11811. WOLFSSL_METHOD* m;
  11812. WOLFSSL_ENTER("DTLS_method_ex");
  11813. #ifndef NO_WOLFSSL_CLIENT
  11814. m = wolfDTLS_client_method_ex(heap);
  11815. #else
  11816. m = wolfDTLS_server_method_ex(heap);
  11817. #endif
  11818. if (m != NULL) {
  11819. m->side = WOLFSSL_NEITHER_END;
  11820. }
  11821. return m;
  11822. }
  11823. #ifndef NO_OLD_TLS
  11824. WOLFSSL_METHOD* wolfDTLSv1_method(void)
  11825. {
  11826. return wolfDTLSv1_method_ex(NULL);
  11827. }
  11828. WOLFSSL_METHOD* wolfDTLSv1_method_ex(void* heap)
  11829. {
  11830. WOLFSSL_METHOD* m;
  11831. WOLFSSL_ENTER("DTLSv1_method_ex");
  11832. #ifndef NO_WOLFSSL_CLIENT
  11833. m = wolfDTLSv1_client_method_ex(heap);
  11834. #else
  11835. m = wolfDTLSv1_server_method_ex(heap);
  11836. #endif
  11837. if (m != NULL) {
  11838. m->side = WOLFSSL_NEITHER_END;
  11839. }
  11840. return m;
  11841. }
  11842. #endif /* !NO_OLD_TLS */
  11843. #ifndef WOLFSSL_NO_TLS12
  11844. WOLFSSL_METHOD* wolfDTLSv1_2_method(void)
  11845. {
  11846. return wolfDTLSv1_2_method_ex(NULL);
  11847. }
  11848. WOLFSSL_METHOD* wolfDTLSv1_2_method_ex(void* heap)
  11849. {
  11850. WOLFSSL_METHOD* m;
  11851. WOLFSSL_ENTER("DTLSv1_2_method");
  11852. #ifndef NO_WOLFSSL_CLIENT
  11853. m = wolfDTLSv1_2_client_method_ex(heap);
  11854. #else
  11855. m = wolfDTLSv1_2_server_method_ex(heap);
  11856. #endif
  11857. if (m != NULL) {
  11858. m->side = WOLFSSL_NEITHER_END;
  11859. }
  11860. return m;
  11861. }
  11862. #endif /* !WOLFSSL_NO_TLS12 */
  11863. #endif /* WOLFSSL_DTLS */
  11864. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  11865. #ifndef NO_WOLFSSL_SERVER
  11866. WOLFSSL_METHOD* wolfTLS_server_method(void)
  11867. {
  11868. return wolfTLS_server_method_ex(NULL);
  11869. }
  11870. WOLFSSL_METHOD* wolfTLS_server_method_ex(void* heap)
  11871. {
  11872. WOLFSSL_METHOD* method =
  11873. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11874. heap, DYNAMIC_TYPE_METHOD);
  11875. (void)heap;
  11876. WOLFSSL_ENTER("TLS_server_method_ex");
  11877. if (method) {
  11878. #if defined(WOLFSSL_TLS13)
  11879. InitSSL_Method(method, MakeTLSv1_3());
  11880. #elif !defined(WOLFSSL_NO_TLS12)
  11881. InitSSL_Method(method, MakeTLSv1_2());
  11882. #elif !defined(NO_OLD_TLS)
  11883. InitSSL_Method(method, MakeTLSv1_1());
  11884. #elif defined(WOLFSSL_ALLOW_TLSV10)
  11885. InitSSL_Method(method, MakeTLSv1());
  11886. #else
  11887. #error No TLS version enabled!
  11888. #endif
  11889. method->downgrade = 1;
  11890. method->side = WOLFSSL_SERVER_END;
  11891. }
  11892. return method;
  11893. }
  11894. #ifndef NO_OLD_TLS
  11895. #ifdef WOLFSSL_ALLOW_TLSV10
  11896. WOLFSSL_METHOD* wolfTLSv1_server_method(void)
  11897. {
  11898. return wolfTLSv1_server_method_ex(NULL);
  11899. }
  11900. WOLFSSL_METHOD* wolfTLSv1_server_method_ex(void* heap)
  11901. {
  11902. WOLFSSL_METHOD* method =
  11903. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11904. heap, DYNAMIC_TYPE_METHOD);
  11905. (void)heap;
  11906. WOLFSSL_ENTER("TLSv1_server_method_ex");
  11907. if (method) {
  11908. InitSSL_Method(method, MakeTLSv1());
  11909. method->side = WOLFSSL_SERVER_END;
  11910. }
  11911. return method;
  11912. }
  11913. #endif /* WOLFSSL_ALLOW_TLSV10 */
  11914. WOLFSSL_METHOD* wolfTLSv1_1_server_method(void)
  11915. {
  11916. return wolfTLSv1_1_server_method_ex(NULL);
  11917. }
  11918. WOLFSSL_METHOD* wolfTLSv1_1_server_method_ex(void* heap)
  11919. {
  11920. WOLFSSL_METHOD* method =
  11921. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11922. heap, DYNAMIC_TYPE_METHOD);
  11923. (void)heap;
  11924. WOLFSSL_ENTER("TLSv1_1_server_method_ex");
  11925. if (method) {
  11926. InitSSL_Method(method, MakeTLSv1_1());
  11927. method->side = WOLFSSL_SERVER_END;
  11928. }
  11929. return method;
  11930. }
  11931. #endif /* !NO_OLD_TLS */
  11932. #ifndef WOLFSSL_NO_TLS12
  11933. WOLFSSL_ABI
  11934. WOLFSSL_METHOD* wolfTLSv1_2_server_method(void)
  11935. {
  11936. return wolfTLSv1_2_server_method_ex(NULL);
  11937. }
  11938. WOLFSSL_METHOD* wolfTLSv1_2_server_method_ex(void* heap)
  11939. {
  11940. WOLFSSL_METHOD* method =
  11941. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11942. heap, DYNAMIC_TYPE_METHOD);
  11943. (void)heap;
  11944. WOLFSSL_ENTER("TLSv1_2_server_method_ex");
  11945. if (method) {
  11946. InitSSL_Method(method, MakeTLSv1_2());
  11947. method->side = WOLFSSL_SERVER_END;
  11948. }
  11949. return method;
  11950. }
  11951. #endif /* !WOLFSSL_NO_TLS12 */
  11952. #ifdef WOLFSSL_TLS13
  11953. /* The TLS v1.3 server method data.
  11954. *
  11955. * returns the method data for a TLS v1.3 server.
  11956. */
  11957. WOLFSSL_ABI
  11958. WOLFSSL_METHOD* wolfTLSv1_3_server_method(void)
  11959. {
  11960. return wolfTLSv1_3_server_method_ex(NULL);
  11961. }
  11962. /* The TLS v1.3 server method data.
  11963. *
  11964. * heap The heap used for allocation.
  11965. * returns the method data for a TLS v1.3 server.
  11966. */
  11967. WOLFSSL_METHOD* wolfTLSv1_3_server_method_ex(void* heap)
  11968. {
  11969. WOLFSSL_METHOD* method =
  11970. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11971. heap, DYNAMIC_TYPE_METHOD);
  11972. (void)heap;
  11973. WOLFSSL_ENTER("TLSv1_3_server_method_ex");
  11974. if (method) {
  11975. InitSSL_Method(method, MakeTLSv1_3());
  11976. method->side = WOLFSSL_SERVER_END;
  11977. }
  11978. return method;
  11979. }
  11980. #endif /* WOLFSSL_TLS13 */
  11981. #ifdef WOLFSSL_DTLS
  11982. WOLFSSL_METHOD* wolfDTLS_server_method(void)
  11983. {
  11984. return wolfDTLS_server_method_ex(NULL);
  11985. }
  11986. WOLFSSL_METHOD* wolfDTLS_server_method_ex(void* heap)
  11987. {
  11988. WOLFSSL_METHOD* method =
  11989. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11990. heap, DYNAMIC_TYPE_METHOD);
  11991. (void)heap;
  11992. WOLFSSL_ENTER("DTLS_server_method_ex");
  11993. if (method) {
  11994. #if defined(WOLFSSL_DTLS13)
  11995. InitSSL_Method(method, MakeDTLSv1_3());
  11996. #elif !defined(WOLFSSL_NO_TLS12)
  11997. InitSSL_Method(method, MakeDTLSv1_2());
  11998. #elif !defined(NO_OLD_TLS)
  11999. InitSSL_Method(method, MakeDTLSv1());
  12000. #else
  12001. #error No DTLS version enabled!
  12002. #endif
  12003. method->downgrade = 1;
  12004. method->side = WOLFSSL_SERVER_END;
  12005. }
  12006. return method;
  12007. }
  12008. #ifndef NO_OLD_TLS
  12009. WOLFSSL_METHOD* wolfDTLSv1_server_method(void)
  12010. {
  12011. return wolfDTLSv1_server_method_ex(NULL);
  12012. }
  12013. WOLFSSL_METHOD* wolfDTLSv1_server_method_ex(void* heap)
  12014. {
  12015. WOLFSSL_METHOD* method =
  12016. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12017. heap, DYNAMIC_TYPE_METHOD);
  12018. (void)heap;
  12019. WOLFSSL_ENTER("DTLSv1_server_method_ex");
  12020. if (method) {
  12021. InitSSL_Method(method, MakeDTLSv1());
  12022. method->side = WOLFSSL_SERVER_END;
  12023. }
  12024. return method;
  12025. }
  12026. #endif /* !NO_OLD_TLS */
  12027. #ifndef WOLFSSL_NO_TLS12
  12028. WOLFSSL_METHOD* wolfDTLSv1_2_server_method(void)
  12029. {
  12030. return wolfDTLSv1_2_server_method_ex(NULL);
  12031. }
  12032. WOLFSSL_METHOD* wolfDTLSv1_2_server_method_ex(void* heap)
  12033. {
  12034. WOLFSSL_METHOD* method =
  12035. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12036. heap, DYNAMIC_TYPE_METHOD);
  12037. WOLFSSL_ENTER("DTLSv1_2_server_method_ex");
  12038. (void)heap;
  12039. if (method) {
  12040. InitSSL_Method(method, MakeDTLSv1_2());
  12041. method->side = WOLFSSL_SERVER_END;
  12042. }
  12043. (void)heap;
  12044. return method;
  12045. }
  12046. #endif /* !WOLFSSL_NO_TLS12 */
  12047. #endif /* WOLFSSL_DTLS */
  12048. #endif /* NO_WOLFSSL_SERVER */
  12049. #endif /* NO_TLS */
  12050. #endif /* WOLFCRYPT_ONLY */