tls.c 437 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402
  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. #ifdef WOLFSSL_SM3
  139. if (ssl->specs.mac_algorithm == sm3_mac) {
  140. ret |= wc_Sm3GetHash(&ssl->hsHashes->hashSm3, hash);
  141. hashSz = WC_SM3_DIGEST_SIZE;
  142. }
  143. #endif
  144. }
  145. *hashLen = hashSz;
  146. #ifdef WOLFSSL_CHECK_MEM_ZERO
  147. wc_MemZero_Add("TLS handshake hash", hash, hashSz);
  148. #endif
  149. if (ret != 0) {
  150. ret = BUILD_MSG_ERROR;
  151. WOLFSSL_ERROR_VERBOSE(ret);
  152. }
  153. return ret;
  154. }
  155. int BuildTlsFinished(WOLFSSL* ssl, Hashes* hashes, const byte* sender)
  156. {
  157. int ret;
  158. const byte* side = NULL;
  159. word32 hashSz = HSHASH_SZ;
  160. #if !defined(WOLFSSL_ASYNC_CRYPT) || defined(WC_ASYNC_NO_HASH)
  161. byte handshake_hash[HSHASH_SZ];
  162. #else
  163. WC_DECLARE_VAR(handshake_hash, byte, HSHASH_SZ, ssl->heap);
  164. if (handshake_hash == NULL)
  165. return MEMORY_E;
  166. #endif
  167. ret = BuildTlsHandshakeHash(ssl, handshake_hash, &hashSz);
  168. if (ret == 0) {
  169. if (XSTRNCMP((const char*)sender, (const char*)kTlsClientStr,
  170. SIZEOF_SENDER) == 0) {
  171. side = kTlsClientFinStr;
  172. }
  173. else if (XSTRNCMP((const char*)sender, (const char*)kTlsServerStr,
  174. SIZEOF_SENDER) == 0) {
  175. side = kTlsServerFinStr;
  176. }
  177. else {
  178. ret = BAD_FUNC_ARG;
  179. WOLFSSL_MSG("Unexpected sender value");
  180. }
  181. }
  182. if (ret == 0) {
  183. #ifdef WOLFSSL_HAVE_PRF
  184. #if !defined(NO_CERTS) && defined(HAVE_PK_CALLBACKS)
  185. if (ssl->ctx->TlsFinishedCb) {
  186. void* ctx = wolfSSL_GetTlsFinishedCtx(ssl);
  187. ret = ssl->ctx->TlsFinishedCb(ssl, side, handshake_hash, hashSz,
  188. (byte*)hashes, ctx);
  189. }
  190. if (!ssl->ctx->TlsFinishedCb || ret == PROTOCOLCB_UNAVAILABLE)
  191. #endif
  192. {
  193. PRIVATE_KEY_UNLOCK();
  194. ret = wc_PRF_TLS((byte*)hashes, TLS_FINISHED_SZ,
  195. ssl->arrays->masterSecret, SECRET_LEN, side,
  196. FINISHED_LABEL_SZ, handshake_hash, hashSz,
  197. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  198. ssl->heap, ssl->devId);
  199. PRIVATE_KEY_LOCK();
  200. }
  201. ForceZero(handshake_hash, hashSz);
  202. #else
  203. /* Pseudo random function must be enabled in the configuration. */
  204. ret = PRF_MISSING;
  205. WOLFSSL_ERROR_VERBOSE(ret);
  206. WOLFSSL_MSG("Pseudo-random function is not enabled");
  207. (void)side;
  208. (void)hashes;
  209. #endif
  210. }
  211. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  212. WC_FREE_VAR(handshake_hash, ssl->heap);
  213. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  214. wc_MemZero_Check(handshake_hash, HSHASH_SZ);
  215. #endif
  216. return ret;
  217. }
  218. #endif /* !WOLFSSL_NO_TLS12 */
  219. #ifndef NO_OLD_TLS
  220. #ifdef WOLFSSL_ALLOW_TLSV10
  221. ProtocolVersion MakeTLSv1(void)
  222. {
  223. ProtocolVersion pv;
  224. pv.major = SSLv3_MAJOR;
  225. pv.minor = TLSv1_MINOR;
  226. return pv;
  227. }
  228. #endif /* WOLFSSL_ALLOW_TLSV10 */
  229. ProtocolVersion MakeTLSv1_1(void)
  230. {
  231. ProtocolVersion pv;
  232. pv.major = SSLv3_MAJOR;
  233. pv.minor = TLSv1_1_MINOR;
  234. return pv;
  235. }
  236. #endif /* !NO_OLD_TLS */
  237. #ifndef WOLFSSL_NO_TLS12
  238. ProtocolVersion MakeTLSv1_2(void)
  239. {
  240. ProtocolVersion pv;
  241. pv.major = SSLv3_MAJOR;
  242. pv.minor = TLSv1_2_MINOR;
  243. return pv;
  244. }
  245. #endif /* !WOLFSSL_NO_TLS12 */
  246. #ifdef WOLFSSL_TLS13
  247. /* The TLS v1.3 protocol version.
  248. *
  249. * returns the protocol version data for TLS v1.3.
  250. */
  251. ProtocolVersion MakeTLSv1_3(void)
  252. {
  253. ProtocolVersion pv;
  254. pv.major = SSLv3_MAJOR;
  255. pv.minor = TLSv1_3_MINOR;
  256. return pv;
  257. }
  258. #endif
  259. #ifndef WOLFSSL_NO_TLS12
  260. #ifdef HAVE_EXTENDED_MASTER
  261. static const byte ext_master_label[EXT_MASTER_LABEL_SZ + 1] =
  262. "extended master secret";
  263. #endif
  264. static const byte master_label[MASTER_LABEL_SZ + 1] = "master secret";
  265. static const byte key_label [KEY_LABEL_SZ + 1] = "key expansion";
  266. static int _DeriveTlsKeys(byte* key_dig, word32 key_dig_len,
  267. const byte* ms, word32 msLen,
  268. const byte* sr, const byte* cr,
  269. int tls1_2, int hash_type,
  270. void* heap, int devId)
  271. {
  272. int ret;
  273. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  274. WC_DECLARE_VAR(seed, byte, SEED_LEN, heap);
  275. if (seed == NULL)
  276. return MEMORY_E;
  277. #else
  278. byte seed[SEED_LEN];
  279. #endif
  280. XMEMCPY(seed, sr, RAN_LEN);
  281. XMEMCPY(seed + RAN_LEN, cr, RAN_LEN);
  282. #ifdef WOLFSSL_HAVE_PRF
  283. PRIVATE_KEY_UNLOCK();
  284. ret = wc_PRF_TLS(key_dig, key_dig_len, ms, msLen, key_label, KEY_LABEL_SZ,
  285. seed, SEED_LEN, tls1_2, hash_type, heap, devId);
  286. PRIVATE_KEY_LOCK();
  287. #else
  288. /* Pseudo random function must be enabled in the configuration. */
  289. ret = PRF_MISSING;
  290. WOLFSSL_ERROR_VERBOSE(ret);
  291. WOLFSSL_MSG("Pseudo-random function is not enabled");
  292. (void)key_dig;
  293. (void)key_dig_len;
  294. (void)ms;
  295. (void)msLen;
  296. (void)tls1_2;
  297. (void)hash_type;
  298. (void)heap;
  299. (void)devId;
  300. (void)key_label;
  301. (void)master_label;
  302. #ifdef HAVE_EXTENDED_MASTER
  303. (void)ext_master_label;
  304. #endif
  305. #endif
  306. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  307. WC_FREE_VAR(seed, heap);
  308. #endif
  309. return ret;
  310. }
  311. /* External facing wrapper so user can call as well, 0 on success */
  312. int wolfSSL_DeriveTlsKeys(byte* key_dig, word32 key_dig_len,
  313. const byte* ms, word32 msLen,
  314. const byte* sr, const byte* cr,
  315. int tls1_2, int hash_type)
  316. {
  317. return _DeriveTlsKeys(key_dig, key_dig_len, ms, msLen, sr, cr, tls1_2,
  318. hash_type, NULL, INVALID_DEVID);
  319. }
  320. int DeriveTlsKeys(WOLFSSL* ssl)
  321. {
  322. int ret;
  323. int key_dig_len = 2 * ssl->specs.hash_size +
  324. 2 * ssl->specs.key_size +
  325. 2 * ssl->specs.iv_size;
  326. #ifdef WOLFSSL_SMALL_STACK
  327. byte* key_dig;
  328. #else
  329. byte key_dig[MAX_PRF_DIG];
  330. #endif
  331. #ifdef WOLFSSL_SMALL_STACK
  332. key_dig = (byte*)XMALLOC(MAX_PRF_DIG, ssl->heap, DYNAMIC_TYPE_DIGEST);
  333. if (key_dig == NULL) {
  334. return MEMORY_E;
  335. }
  336. #endif
  337. #if !defined(NO_CERTS) && defined(HAVE_PK_CALLBACKS)
  338. ret = PROTOCOLCB_UNAVAILABLE;
  339. if (ssl->ctx->GenSessionKeyCb) {
  340. void* ctx = wolfSSL_GetGenSessionKeyCtx(ssl);
  341. ret = ssl->ctx->GenSessionKeyCb(ssl, ctx);
  342. }
  343. if (!ssl->ctx->GenSessionKeyCb || ret == PROTOCOLCB_UNAVAILABLE)
  344. #endif
  345. ret = _DeriveTlsKeys(key_dig, key_dig_len,
  346. ssl->arrays->masterSecret, SECRET_LEN,
  347. ssl->arrays->serverRandom, ssl->arrays->clientRandom,
  348. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  349. ssl->heap, ssl->devId);
  350. if (ret == 0)
  351. ret = StoreKeys(ssl, key_dig, PROVISION_CLIENT_SERVER);
  352. #ifdef WOLFSSL_SMALL_STACK
  353. XFREE(key_dig, ssl->heap, DYNAMIC_TYPE_DIGEST);
  354. #endif
  355. return ret;
  356. }
  357. static int _MakeTlsMasterSecret(byte* ms, word32 msLen,
  358. const byte* pms, word32 pmsLen,
  359. const byte* cr, const byte* sr,
  360. int tls1_2, int hash_type,
  361. void* heap, int devId)
  362. {
  363. int ret;
  364. #if !defined(WOLFSSL_ASYNC_CRYPT) || defined(WC_ASYNC_NO_HASH)
  365. byte seed[SEED_LEN];
  366. #else
  367. WC_DECLARE_VAR(seed, byte, SEED_LEN, heap);
  368. if (seed == NULL)
  369. return MEMORY_E;
  370. #endif
  371. XMEMCPY(seed, cr, RAN_LEN);
  372. XMEMCPY(seed + RAN_LEN, sr, RAN_LEN);
  373. #ifdef WOLFSSL_HAVE_PRF
  374. PRIVATE_KEY_UNLOCK();
  375. ret = wc_PRF_TLS(ms, msLen, pms, pmsLen, master_label, MASTER_LABEL_SZ,
  376. seed, SEED_LEN, tls1_2, hash_type, heap, devId);
  377. PRIVATE_KEY_LOCK();
  378. #else
  379. /* Pseudo random function must be enabled in the configuration. */
  380. ret = PRF_MISSING;
  381. WOLFSSL_MSG("Pseudo-random function is not enabled");
  382. (void)ms;
  383. (void)msLen;
  384. (void)pms;
  385. (void)pmsLen;
  386. (void)tls1_2;
  387. (void)hash_type;
  388. (void)heap;
  389. (void)devId;
  390. #endif
  391. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  392. WC_FREE_VAR(seed, heap);
  393. #endif
  394. return ret;
  395. }
  396. /* External facing wrapper so user can call as well, 0 on success */
  397. int wolfSSL_MakeTlsMasterSecret(byte* ms, word32 msLen,
  398. const byte* pms, word32 pmsLen,
  399. const byte* cr, const byte* sr,
  400. int tls1_2, int hash_type)
  401. {
  402. return _MakeTlsMasterSecret(ms, msLen, pms, pmsLen, cr, sr, tls1_2,
  403. hash_type, NULL, INVALID_DEVID);
  404. }
  405. #ifdef HAVE_EXTENDED_MASTER
  406. static int _MakeTlsExtendedMasterSecret(byte* ms, word32 msLen,
  407. const byte* pms, word32 pmsLen,
  408. const byte* sHash, word32 sHashLen,
  409. int tls1_2, int hash_type,
  410. void* heap, int devId)
  411. {
  412. int ret;
  413. #ifdef WOLFSSL_HAVE_PRF
  414. PRIVATE_KEY_UNLOCK();
  415. ret = wc_PRF_TLS(ms, msLen, pms, pmsLen, ext_master_label, EXT_MASTER_LABEL_SZ,
  416. sHash, sHashLen, tls1_2, hash_type, heap, devId);
  417. PRIVATE_KEY_LOCK();
  418. #else
  419. /* Pseudo random function must be enabled in the configuration. */
  420. ret = PRF_MISSING;
  421. WOLFSSL_MSG("Pseudo-random function is not enabled");
  422. (void)ms;
  423. (void)msLen;
  424. (void)pms;
  425. (void)pmsLen;
  426. (void)sHash;
  427. (void)sHashLen;
  428. (void)tls1_2;
  429. (void)hash_type;
  430. (void)heap;
  431. (void)devId;
  432. #endif
  433. return ret;
  434. }
  435. /* External facing wrapper so user can call as well, 0 on success */
  436. int wolfSSL_MakeTlsExtendedMasterSecret(byte* ms, word32 msLen,
  437. const byte* pms, word32 pmsLen,
  438. const byte* sHash, word32 sHashLen,
  439. int tls1_2, int hash_type)
  440. {
  441. return _MakeTlsExtendedMasterSecret(ms, msLen, pms, pmsLen, sHash, sHashLen,
  442. tls1_2, hash_type, NULL, INVALID_DEVID);
  443. }
  444. #endif /* HAVE_EXTENDED_MASTER */
  445. int MakeTlsMasterSecret(WOLFSSL* ssl)
  446. {
  447. int ret;
  448. #ifdef HAVE_EXTENDED_MASTER
  449. if (ssl->options.haveEMS) {
  450. word32 hashSz = HSHASH_SZ;
  451. #ifdef WOLFSSL_SMALL_STACK
  452. byte* handshake_hash = (byte*)XMALLOC(HSHASH_SZ, ssl->heap,
  453. DYNAMIC_TYPE_DIGEST);
  454. if (handshake_hash == NULL)
  455. return MEMORY_E;
  456. #else
  457. byte handshake_hash[HSHASH_SZ];
  458. #endif
  459. ret = BuildTlsHandshakeHash(ssl, handshake_hash, &hashSz);
  460. if (ret == 0) {
  461. ret = _MakeTlsExtendedMasterSecret(
  462. ssl->arrays->masterSecret, SECRET_LEN,
  463. ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz,
  464. handshake_hash, hashSz,
  465. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  466. ssl->heap, ssl->devId);
  467. ForceZero(handshake_hash, hashSz);
  468. }
  469. #ifdef WOLFSSL_SMALL_STACK
  470. XFREE(handshake_hash, ssl->heap, DYNAMIC_TYPE_DIGEST);
  471. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  472. wc_MemZero_Check(handshake_hash, HSHASH_SZ);
  473. #endif
  474. }
  475. else
  476. #endif /* HAVE_EXTENDED_MASTER */
  477. {
  478. #if !defined(NO_CERTS) && defined(HAVE_PK_CALLBACKS)
  479. ret = PROTOCOLCB_UNAVAILABLE;
  480. if (ssl->ctx->GenMasterCb) {
  481. void* ctx = wolfSSL_GetGenMasterSecretCtx(ssl);
  482. ret = ssl->ctx->GenMasterCb(ssl, ctx);
  483. }
  484. if (!ssl->ctx->GenMasterCb || ret == PROTOCOLCB_UNAVAILABLE)
  485. #endif
  486. {
  487. ret = _MakeTlsMasterSecret(ssl->arrays->masterSecret,
  488. SECRET_LEN, ssl->arrays->preMasterSecret,
  489. ssl->arrays->preMasterSz, ssl->arrays->clientRandom,
  490. ssl->arrays->serverRandom, IsAtLeastTLSv1_2(ssl),
  491. ssl->specs.mac_algorithm, ssl->heap, ssl->devId);
  492. }
  493. }
  494. if (ret == 0) {
  495. #ifdef SHOW_SECRETS
  496. /* Wireshark Pre-Master-Secret Format:
  497. * CLIENT_RANDOM <clientrandom> <mastersecret>
  498. */
  499. const char* CLIENT_RANDOM_LABEL = "CLIENT_RANDOM";
  500. int i, pmsPos = 0;
  501. char pmsBuf[13 + 1 + 64 + 1 + 96 + 1 + 1];
  502. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%s ",
  503. CLIENT_RANDOM_LABEL);
  504. pmsPos += XSTRLEN(CLIENT_RANDOM_LABEL) + 1;
  505. for (i = 0; i < RAN_LEN; i++) {
  506. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%02x",
  507. ssl->arrays->clientRandom[i]);
  508. pmsPos += 2;
  509. }
  510. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, " ");
  511. pmsPos += 1;
  512. for (i = 0; i < SECRET_LEN; i++) {
  513. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%02x",
  514. ssl->arrays->masterSecret[i]);
  515. pmsPos += 2;
  516. }
  517. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "\n");
  518. pmsPos += 1;
  519. /* print master secret */
  520. puts(pmsBuf);
  521. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_SSLKEYLOGFILE)
  522. {
  523. FILE* f = XFOPEN(WOLFSSL_SSLKEYLOGFILE_OUTPUT, "a");
  524. if (f != XBADFILE) {
  525. XFWRITE(pmsBuf, 1, pmsPos, f);
  526. XFCLOSE(f);
  527. }
  528. }
  529. #endif
  530. #endif /* SHOW_SECRETS */
  531. ret = DeriveTlsKeys(ssl);
  532. }
  533. return ret;
  534. }
  535. /* Used by EAP-TLS and EAP-TTLS to derive keying material from
  536. * the master_secret. */
  537. int wolfSSL_make_eap_keys(WOLFSSL* ssl, void* msk, unsigned int len,
  538. const char* label)
  539. {
  540. int ret;
  541. #ifdef WOLFSSL_SMALL_STACK
  542. byte* seed;
  543. #else
  544. byte seed[SEED_LEN];
  545. #endif
  546. #ifdef WOLFSSL_SMALL_STACK
  547. seed = (byte*)XMALLOC(SEED_LEN, ssl->heap, DYNAMIC_TYPE_SEED);
  548. if (seed == NULL)
  549. return MEMORY_E;
  550. #endif
  551. /*
  552. * As per RFC-5281, the order of the client and server randoms is reversed
  553. * from that used by the TLS protocol to derive keys.
  554. */
  555. XMEMCPY(seed, ssl->arrays->clientRandom, RAN_LEN);
  556. XMEMCPY(seed + RAN_LEN, ssl->arrays->serverRandom, RAN_LEN);
  557. #ifdef WOLFSSL_HAVE_PRF
  558. PRIVATE_KEY_UNLOCK();
  559. ret = wc_PRF_TLS((byte*)msk, len, ssl->arrays->masterSecret, SECRET_LEN,
  560. (const byte *)label, (word32)XSTRLEN(label), seed, SEED_LEN,
  561. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  562. ssl->heap, ssl->devId);
  563. PRIVATE_KEY_LOCK();
  564. #else
  565. /* Pseudo random function must be enabled in the configuration. */
  566. ret = PRF_MISSING;
  567. WOLFSSL_MSG("Pseudo-random function is not enabled");
  568. (void)msk;
  569. (void)len;
  570. (void)label;
  571. #endif
  572. #ifdef WOLFSSL_SMALL_STACK
  573. XFREE(seed, ssl->heap, DYNAMIC_TYPE_SEED);
  574. #endif
  575. return ret;
  576. }
  577. int wolfSSL_GetHmacType_ex(CipherSpecs* specs)
  578. {
  579. if (specs == NULL)
  580. return BAD_FUNC_ARG;
  581. switch (specs->mac_algorithm) {
  582. #ifndef NO_MD5
  583. case md5_mac:
  584. {
  585. return WC_MD5;
  586. }
  587. #endif
  588. #ifndef NO_SHA256
  589. case sha256_mac:
  590. {
  591. return WC_SHA256;
  592. }
  593. #endif
  594. #ifdef WOLFSSL_SHA384
  595. case sha384_mac:
  596. {
  597. return WC_SHA384;
  598. }
  599. #endif
  600. #ifdef WOLFSSL_SM3
  601. case sm3_mac:
  602. {
  603. return WC_SM3;
  604. }
  605. #endif
  606. #ifndef NO_SHA
  607. case sha_mac:
  608. {
  609. return WC_SHA;
  610. }
  611. #endif
  612. #ifdef HAVE_BLAKE2
  613. case blake2b_mac:
  614. {
  615. return BLAKE2B_ID;
  616. }
  617. #endif
  618. default:
  619. {
  620. return WOLFSSL_FATAL_ERROR;
  621. }
  622. }
  623. }
  624. /* return HMAC digest type in wolfSSL format */
  625. int wolfSSL_GetHmacType(WOLFSSL* ssl)
  626. {
  627. if (ssl == NULL)
  628. return BAD_FUNC_ARG;
  629. return wolfSSL_GetHmacType_ex(&ssl->specs);
  630. }
  631. int wolfSSL_SetTlsHmacInner(WOLFSSL* ssl, byte* inner, word32 sz, int content,
  632. int verify)
  633. {
  634. if (ssl == NULL || inner == NULL)
  635. return BAD_FUNC_ARG;
  636. XMEMSET(inner, 0, WOLFSSL_TLS_HMAC_INNER_SZ);
  637. WriteSEQ(ssl, verify, inner);
  638. inner[SEQ_SZ] = (byte)content;
  639. inner[SEQ_SZ + ENUM_LEN] = ssl->version.major;
  640. inner[SEQ_SZ + ENUM_LEN + ENUM_LEN] = ssl->version.minor;
  641. c16toa((word16)sz, inner + SEQ_SZ + ENUM_LEN + VERSION_SZ);
  642. return 0;
  643. }
  644. #ifndef WOLFSSL_AEAD_ONLY
  645. #if !defined(WOLFSSL_NO_HASH_RAW) && !defined(HAVE_FIPS) && \
  646. !defined(HAVE_SELFTEST)
  647. /* Update the hash in the HMAC.
  648. *
  649. * hmac HMAC object.
  650. * data Data to be hashed.
  651. * sz Size of data to hash.
  652. * returns 0 on success, otherwise failure.
  653. */
  654. static int Hmac_HashUpdate(Hmac* hmac, const byte* data, word32 sz)
  655. {
  656. int ret = BAD_FUNC_ARG;
  657. switch (hmac->macType) {
  658. #ifndef NO_SHA
  659. case WC_SHA:
  660. ret = wc_ShaUpdate(&hmac->hash.sha, data, sz);
  661. break;
  662. #endif /* !NO_SHA */
  663. #ifndef NO_SHA256
  664. case WC_SHA256:
  665. ret = wc_Sha256Update(&hmac->hash.sha256, data, sz);
  666. break;
  667. #endif /* !NO_SHA256 */
  668. #ifdef WOLFSSL_SHA384
  669. case WC_SHA384:
  670. ret = wc_Sha384Update(&hmac->hash.sha384, data, sz);
  671. break;
  672. #endif /* WOLFSSL_SHA384 */
  673. #ifdef WOLFSSL_SHA512
  674. case WC_SHA512:
  675. ret = wc_Sha512Update(&hmac->hash.sha512, data, sz);
  676. break;
  677. #endif /* WOLFSSL_SHA512 */
  678. #ifdef WOLFSSL_SM3
  679. case WC_SM3:
  680. ret = wc_Sm3Update(&hmac->hash.sm3, data, sz);
  681. break;
  682. #endif /* WOLFSSL_SM3 */
  683. default:
  684. break;
  685. }
  686. return ret;
  687. }
  688. /* Finalize the hash but don't put the EOC, padding or length in.
  689. *
  690. * hmac HMAC object.
  691. * hash Hash result.
  692. * returns 0 on success, otherwise failure.
  693. */
  694. static int Hmac_HashFinalRaw(Hmac* hmac, unsigned char* hash)
  695. {
  696. int ret = BAD_FUNC_ARG;
  697. switch (hmac->macType) {
  698. #ifndef NO_SHA
  699. case WC_SHA:
  700. ret = wc_ShaFinalRaw(&hmac->hash.sha, hash);
  701. break;
  702. #endif /* !NO_SHA */
  703. #ifndef NO_SHA256
  704. case WC_SHA256:
  705. ret = wc_Sha256FinalRaw(&hmac->hash.sha256, hash);
  706. break;
  707. #endif /* !NO_SHA256 */
  708. #ifdef WOLFSSL_SHA384
  709. case WC_SHA384:
  710. ret = wc_Sha384FinalRaw(&hmac->hash.sha384, hash);
  711. break;
  712. #endif /* WOLFSSL_SHA384 */
  713. #ifdef WOLFSSL_SHA512
  714. case WC_SHA512:
  715. ret = wc_Sha512FinalRaw(&hmac->hash.sha512, hash);
  716. break;
  717. #endif /* WOLFSSL_SHA512 */
  718. #ifdef WOLFSSL_SM3
  719. case WC_SM3:
  720. ret = wc_Sm3FinalRaw(&hmac->hash.sm3, hash);
  721. break;
  722. #endif /* WOLFSSL_SM3 */
  723. default:
  724. break;
  725. }
  726. return ret;
  727. }
  728. /* Finalize the HMAC by performing outer hash.
  729. *
  730. * hmac HMAC object.
  731. * mac MAC result.
  732. * returns 0 on success, otherwise failure.
  733. */
  734. static int Hmac_OuterHash(Hmac* hmac, unsigned char* mac)
  735. {
  736. int ret = BAD_FUNC_ARG;
  737. wc_HashAlg hash;
  738. enum wc_HashType hashType = (enum wc_HashType)hmac->macType;
  739. int digestSz = wc_HashGetDigestSize(hashType);
  740. int blockSz = wc_HashGetBlockSize(hashType);
  741. if ((digestSz >= 0) && (blockSz >= 0)) {
  742. ret = wc_HashInit(&hash, hashType);
  743. }
  744. if (ret == 0) {
  745. ret = wc_HashUpdate(&hash, hashType, (byte*)hmac->opad,
  746. blockSz);
  747. if (ret == 0)
  748. ret = wc_HashUpdate(&hash, hashType, (byte*)hmac->innerHash,
  749. digestSz);
  750. if (ret == 0)
  751. ret = wc_HashFinal(&hash, hashType, mac);
  752. wc_HashFree(&hash, hashType);
  753. }
  754. return ret;
  755. }
  756. /* Calculate the HMAC of the header + message data.
  757. * Constant time implementation using wc_Sha*FinalRaw().
  758. *
  759. * hmac HMAC object.
  760. * digest MAC result.
  761. * in Message data.
  762. * sz Size of the message data.
  763. * header Constructed record header with length of handshake data.
  764. * returns 0 on success, otherwise failure.
  765. */
  766. static int Hmac_UpdateFinal_CT(Hmac* hmac, byte* digest, const byte* in,
  767. word32 sz, int macLen, byte* header)
  768. {
  769. byte lenBytes[8];
  770. int i, j;
  771. unsigned int k;
  772. int blockBits, blockMask;
  773. int lastBlockLen, extraLen, eocIndex;
  774. int blocks, safeBlocks, lenBlock, eocBlock;
  775. unsigned int maxLen;
  776. int blockSz, padSz;
  777. int ret;
  778. word32 realLen;
  779. byte extraBlock;
  780. switch (hmac->macType) {
  781. #ifndef NO_SHA
  782. case WC_SHA:
  783. blockSz = WC_SHA_BLOCK_SIZE;
  784. blockBits = 6;
  785. padSz = WC_SHA_BLOCK_SIZE - WC_SHA_PAD_SIZE + 1;
  786. break;
  787. #endif /* !NO_SHA */
  788. #ifndef NO_SHA256
  789. case WC_SHA256:
  790. blockSz = WC_SHA256_BLOCK_SIZE;
  791. blockBits = 6;
  792. padSz = WC_SHA256_BLOCK_SIZE - WC_SHA256_PAD_SIZE + 1;
  793. break;
  794. #endif /* !NO_SHA256 */
  795. #ifdef WOLFSSL_SHA384
  796. case WC_SHA384:
  797. blockSz = WC_SHA384_BLOCK_SIZE;
  798. blockBits = 7;
  799. padSz = WC_SHA384_BLOCK_SIZE - WC_SHA384_PAD_SIZE + 1;
  800. break;
  801. #endif /* WOLFSSL_SHA384 */
  802. #ifdef WOLFSSL_SHA512
  803. case WC_SHA512:
  804. blockSz = WC_SHA512_BLOCK_SIZE;
  805. blockBits = 7;
  806. padSz = WC_SHA512_BLOCK_SIZE - WC_SHA512_PAD_SIZE + 1;
  807. break;
  808. #endif /* WOLFSSL_SHA512 */
  809. #ifdef WOLFSSL_SM3
  810. case WC_SM3:
  811. blockSz = WC_SM3_BLOCK_SIZE;
  812. blockBits = 6;
  813. padSz = WC_SM3_BLOCK_SIZE - WC_SM3_PAD_SIZE + 1;
  814. break;
  815. #endif /* WOLFSSL_SM3 */
  816. default:
  817. return BAD_FUNC_ARG;
  818. }
  819. blockMask = blockSz - 1;
  820. /* Size of data to HMAC if padding length byte is zero. */
  821. maxLen = WOLFSSL_TLS_HMAC_INNER_SZ + sz - 1 - macLen;
  822. /* Complete data (including padding) has block for EOC and/or length. */
  823. extraBlock = ctSetLTE((maxLen + padSz) & blockMask, padSz);
  824. /* Total number of blocks for data including padding. */
  825. blocks = ((maxLen + blockSz - 1) >> blockBits) + extraBlock;
  826. /* Up to last 6 blocks can be hashed safely. */
  827. safeBlocks = blocks - 6;
  828. /* Length of message data. */
  829. realLen = maxLen - in[sz - 1];
  830. /* Number of message bytes in last block. */
  831. lastBlockLen = realLen & blockMask;
  832. /* Number of padding bytes in last block. */
  833. extraLen = ((blockSz * 2 - padSz - lastBlockLen) & blockMask) + 1;
  834. /* Number of blocks to create for hash. */
  835. lenBlock = (realLen + extraLen) >> blockBits;
  836. /* Block containing EOC byte. */
  837. eocBlock = realLen >> blockBits;
  838. /* Index of EOC byte in block. */
  839. eocIndex = realLen & blockMask;
  840. /* Add length of hmac's ipad to total length. */
  841. realLen += blockSz;
  842. /* Length as bits - 8 bytes bigendian. */
  843. c32toa(realLen >> ((sizeof(word32) * 8) - 3), lenBytes);
  844. c32toa(realLen << 3, lenBytes + sizeof(word32));
  845. ret = Hmac_HashUpdate(hmac, (unsigned char*)hmac->ipad, blockSz);
  846. if (ret != 0)
  847. return ret;
  848. XMEMSET(hmac->innerHash, 0, macLen);
  849. if (safeBlocks > 0) {
  850. ret = Hmac_HashUpdate(hmac, header, WOLFSSL_TLS_HMAC_INNER_SZ);
  851. if (ret != 0)
  852. return ret;
  853. ret = Hmac_HashUpdate(hmac, in, safeBlocks * blockSz -
  854. WOLFSSL_TLS_HMAC_INNER_SZ);
  855. if (ret != 0)
  856. return ret;
  857. }
  858. else
  859. safeBlocks = 0;
  860. XMEMSET(digest, 0, macLen);
  861. k = safeBlocks * blockSz;
  862. for (i = safeBlocks; i < blocks; i++) {
  863. unsigned char hashBlock[WC_MAX_BLOCK_SIZE];
  864. unsigned char isEocBlock = ctMaskEq(i, eocBlock);
  865. unsigned char isOutBlock = ctMaskEq(i, lenBlock);
  866. for (j = 0; j < blockSz; j++) {
  867. unsigned char atEoc = ctMaskEq(j, eocIndex) & isEocBlock;
  868. unsigned char pastEoc = ctMaskGT(j, eocIndex) & isEocBlock;
  869. unsigned char b = 0;
  870. if (k < WOLFSSL_TLS_HMAC_INNER_SZ)
  871. b = header[k];
  872. else if (k < maxLen)
  873. b = in[k - WOLFSSL_TLS_HMAC_INNER_SZ];
  874. k++;
  875. b = ctMaskSel(atEoc, 0x80, b);
  876. b &= (unsigned char)~(word32)pastEoc;
  877. b &= ((unsigned char)~(word32)isOutBlock) | isEocBlock;
  878. if (j >= blockSz - 8) {
  879. b = ctMaskSel(isOutBlock, lenBytes[j - (blockSz - 8)], b);
  880. }
  881. hashBlock[j] = b;
  882. }
  883. ret = Hmac_HashUpdate(hmac, hashBlock, blockSz);
  884. if (ret != 0)
  885. return ret;
  886. ret = Hmac_HashFinalRaw(hmac, hashBlock);
  887. if (ret != 0)
  888. return ret;
  889. for (j = 0; j < macLen; j++)
  890. ((unsigned char*)hmac->innerHash)[j] |= hashBlock[j] & isOutBlock;
  891. }
  892. ret = Hmac_OuterHash(hmac, digest);
  893. return ret;
  894. }
  895. #endif
  896. #if defined(WOLFSSL_NO_HASH_RAW) || defined(HAVE_FIPS) || \
  897. defined(HAVE_SELFTEST) || defined(HAVE_BLAKE2)
  898. /* Calculate the HMAC of the header + message data.
  899. * Constant time implementation using normal hashing operations.
  900. * Update-Final need to be constant time.
  901. *
  902. * hmac HMAC object.
  903. * digest MAC result.
  904. * in Message data.
  905. * sz Size of the message data.
  906. * header Constructed record header with length of handshake data.
  907. * returns 0 on success, otherwise failure.
  908. */
  909. static int Hmac_UpdateFinal(Hmac* hmac, byte* digest, const byte* in,
  910. word32 sz, byte* header)
  911. {
  912. byte dummy[WC_MAX_BLOCK_SIZE] = {0};
  913. int ret;
  914. word32 msgSz, blockSz, macSz, padSz, maxSz, realSz;
  915. word32 offset = 0;
  916. int msgBlocks, blocks, blockBits;
  917. int i;
  918. switch (hmac->macType) {
  919. #ifndef NO_SHA
  920. case WC_SHA:
  921. blockSz = WC_SHA_BLOCK_SIZE;
  922. blockBits = 6;
  923. macSz = WC_SHA_DIGEST_SIZE;
  924. padSz = WC_SHA_BLOCK_SIZE - WC_SHA_PAD_SIZE + 1;
  925. break;
  926. #endif /* !NO_SHA */
  927. #ifndef NO_SHA256
  928. case WC_SHA256:
  929. blockSz = WC_SHA256_BLOCK_SIZE;
  930. blockBits = 6;
  931. macSz = WC_SHA256_DIGEST_SIZE;
  932. padSz = WC_SHA256_BLOCK_SIZE - WC_SHA256_PAD_SIZE + 1;
  933. break;
  934. #endif /* !NO_SHA256 */
  935. #ifdef WOLFSSL_SHA384
  936. case WC_SHA384:
  937. blockSz = WC_SHA384_BLOCK_SIZE;
  938. blockBits = 7;
  939. macSz = WC_SHA384_DIGEST_SIZE;
  940. padSz = WC_SHA384_BLOCK_SIZE - WC_SHA384_PAD_SIZE + 1;
  941. break;
  942. #endif /* WOLFSSL_SHA384 */
  943. #ifdef WOLFSSL_SHA512
  944. case WC_SHA512:
  945. blockSz = WC_SHA512_BLOCK_SIZE;
  946. blockBits = 7;
  947. macSz = WC_SHA512_DIGEST_SIZE;
  948. padSz = WC_SHA512_BLOCK_SIZE - WC_SHA512_PAD_SIZE + 1;
  949. break;
  950. #endif /* WOLFSSL_SHA512 */
  951. #ifdef HAVE_BLAKE2
  952. case WC_HASH_TYPE_BLAKE2B:
  953. blockSz = BLAKE2B_BLOCKBYTES;
  954. blockBits = 7;
  955. macSz = BLAKE2B_256;
  956. padSz = 0;
  957. break;
  958. #endif /* HAVE_BLAKE2 */
  959. default:
  960. return BAD_FUNC_ARG;
  961. }
  962. msgSz = sz - (1 + in[sz - 1] + macSz);
  963. /* Make negative result 0 */
  964. msgSz &= ~(0 - (msgSz >> 31));
  965. realSz = WOLFSSL_TLS_HMAC_INNER_SZ + msgSz;
  966. maxSz = WOLFSSL_TLS_HMAC_INNER_SZ + (sz - 1) - macSz;
  967. /* Calculate #blocks processed in HMAC for max and real data. */
  968. blocks = maxSz >> blockBits;
  969. blocks += ((maxSz + padSz) % blockSz) < padSz;
  970. msgBlocks = realSz >> blockBits;
  971. /* #Extra blocks to process. */
  972. blocks -= msgBlocks + ((((realSz + padSz) % blockSz) < padSz) ? 1 : 0);
  973. /* Calculate whole blocks. */
  974. msgBlocks--;
  975. ret = wc_HmacUpdate(hmac, header, WOLFSSL_TLS_HMAC_INNER_SZ);
  976. if (ret == 0) {
  977. /* Fill the rest of the block with any available data. */
  978. word32 currSz = ctMaskLT(msgSz, blockSz) & msgSz;
  979. currSz |= ctMaskGTE(msgSz, blockSz) & blockSz;
  980. currSz -= WOLFSSL_TLS_HMAC_INNER_SZ;
  981. currSz &= ~(0 - (currSz >> 31));
  982. ret = wc_HmacUpdate(hmac, in, currSz);
  983. offset = currSz;
  984. }
  985. if (ret == 0) {
  986. /* Do the hash operations on a block basis. */
  987. for (i = 0; i < msgBlocks; i++, offset += blockSz) {
  988. ret = wc_HmacUpdate(hmac, in + offset, blockSz);
  989. if (ret != 0)
  990. break;
  991. }
  992. }
  993. if (ret == 0)
  994. ret = wc_HmacUpdate(hmac, in + offset, msgSz - offset);
  995. if (ret == 0)
  996. ret = wc_HmacFinal(hmac, digest);
  997. if (ret == 0) {
  998. /* Do the dummy hash operations. Do at least one. */
  999. for (i = 0; i < blocks + 1; i++) {
  1000. ret = wc_HmacUpdate(hmac, dummy, blockSz);
  1001. if (ret != 0)
  1002. break;
  1003. }
  1004. }
  1005. return ret;
  1006. }
  1007. #endif
  1008. int TLS_hmac(WOLFSSL* ssl, byte* digest, const byte* in, word32 sz, int padSz,
  1009. int content, int verify, int epochOrder)
  1010. {
  1011. Hmac hmac;
  1012. byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
  1013. int ret = 0;
  1014. const byte* macSecret = NULL;
  1015. word32 hashSz = 0;
  1016. if (ssl == NULL)
  1017. return BAD_FUNC_ARG;
  1018. #ifdef HAVE_TRUNCATED_HMAC
  1019. hashSz = ssl->truncated_hmac ? (byte)TRUNCATED_HMAC_SZ
  1020. : ssl->specs.hash_size;
  1021. #else
  1022. hashSz = ssl->specs.hash_size;
  1023. #endif
  1024. #ifdef HAVE_FUZZER
  1025. /* Fuzz "in" buffer with sz to be used in HMAC algorithm */
  1026. if (ssl->fuzzerCb) {
  1027. if (verify && padSz >= 0) {
  1028. ssl->fuzzerCb(ssl, in, sz + hashSz + padSz + 1, FUZZ_HMAC,
  1029. ssl->fuzzerCtx);
  1030. }
  1031. else {
  1032. ssl->fuzzerCb(ssl, in, sz, FUZZ_HMAC, ssl->fuzzerCtx);
  1033. }
  1034. }
  1035. #endif
  1036. if (!ssl->options.dtls)
  1037. wolfSSL_SetTlsHmacInner(ssl, myInner, sz, content, verify);
  1038. else
  1039. wolfSSL_SetTlsHmacInner(ssl, myInner, sz, content, epochOrder);
  1040. ret = wc_HmacInit(&hmac, ssl->heap, ssl->devId);
  1041. if (ret != 0)
  1042. return ret;
  1043. #ifdef WOLFSSL_DTLS
  1044. if (ssl->options.dtls)
  1045. macSecret = wolfSSL_GetDtlsMacSecret(ssl, verify, epochOrder);
  1046. else
  1047. macSecret = wolfSSL_GetMacSecret(ssl, verify);
  1048. #else
  1049. macSecret = wolfSSL_GetMacSecret(ssl, verify);
  1050. #endif
  1051. ret = wc_HmacSetKey(&hmac, wolfSSL_GetHmacType(ssl),
  1052. macSecret,
  1053. ssl->specs.hash_size);
  1054. if (ret == 0) {
  1055. /* Constant time verification required. */
  1056. if (verify && padSz >= 0) {
  1057. #if !defined(WOLFSSL_NO_HASH_RAW) && !defined(HAVE_FIPS) && \
  1058. !defined(HAVE_SELFTEST)
  1059. #ifdef HAVE_BLAKE2
  1060. if (wolfSSL_GetHmacType(ssl) == WC_HASH_TYPE_BLAKE2B) {
  1061. ret = Hmac_UpdateFinal(&hmac, digest, in,
  1062. sz + hashSz + padSz + 1, myInner);
  1063. }
  1064. else
  1065. #endif
  1066. {
  1067. ret = Hmac_UpdateFinal_CT(&hmac, digest, in,
  1068. sz + hashSz + padSz + 1, hashSz, myInner);
  1069. }
  1070. #else
  1071. ret = Hmac_UpdateFinal(&hmac, digest, in, sz + hashSz + padSz + 1,
  1072. myInner);
  1073. #endif
  1074. }
  1075. else {
  1076. ret = wc_HmacUpdate(&hmac, myInner, sizeof(myInner));
  1077. if (ret == 0)
  1078. ret = wc_HmacUpdate(&hmac, in, sz); /* content */
  1079. if (ret == 0)
  1080. ret = wc_HmacFinal(&hmac, digest);
  1081. }
  1082. }
  1083. wc_HmacFree(&hmac);
  1084. return ret;
  1085. }
  1086. #endif /* WOLFSSL_AEAD_ONLY */
  1087. #endif /* !WOLFSSL_NO_TLS12 */
  1088. #ifdef HAVE_TLS_EXTENSIONS
  1089. /**
  1090. * The TLSX semaphore is used to calculate the size of the extensions to be sent
  1091. * from one peer to another.
  1092. */
  1093. /** Supports up to 72 flags. Increase as needed. */
  1094. #define SEMAPHORE_SIZE 9
  1095. /**
  1096. * Converts the extension type (id) to an index in the semaphore.
  1097. *
  1098. * Official reference for TLS extension types:
  1099. * http://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xml
  1100. *
  1101. * Motivation:
  1102. * Previously, we used the extension type itself as the index of that
  1103. * extension in the semaphore as the extension types were declared
  1104. * sequentially, but maintain a semaphore as big as the number of available
  1105. * extensions is no longer an option since the release of renegotiation_info.
  1106. *
  1107. * How to update:
  1108. * Assign extension types that extrapolate the number of available semaphores
  1109. * to the first available index going backwards in the semaphore array.
  1110. * When adding a new extension type that don't extrapolate the number of
  1111. * available semaphores, check for a possible collision with with a
  1112. * 'remapped' extension type.
  1113. *
  1114. * Update TLSX_Parse for duplicate detection if more added above 62.
  1115. */
  1116. static WC_INLINE word16 TLSX_ToSemaphore(word16 type)
  1117. {
  1118. switch (type) {
  1119. case TLSX_RENEGOTIATION_INFO: /* 0xFF01 */
  1120. return 63;
  1121. #ifdef WOLFSSL_QUIC
  1122. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT: /* 0xffa5 */
  1123. return 64;
  1124. #endif
  1125. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  1126. case TLSX_ECH: /* 0xfe0d */
  1127. return 65;
  1128. #endif
  1129. default:
  1130. if (type > 62) {
  1131. /* This message SHOULD only happens during the adding of
  1132. new TLS extensions in which its IANA number overflows
  1133. the current semaphore's range, or if its number already
  1134. is assigned to be used by another extension.
  1135. Use this check value for the new extension and decrement
  1136. the check value by one. */
  1137. WOLFSSL_MSG("### TLSX semaphore collision or overflow detected!");
  1138. }
  1139. }
  1140. return type;
  1141. }
  1142. /** Checks if a specific light (tls extension) is not set in the semaphore. */
  1143. #define IS_OFF(semaphore, light) \
  1144. (!(((semaphore)[(light) / 8] & (byte) (0x01 << ((light) % 8)))))
  1145. /** Turn on a specific light (tls extension) in the semaphore. */
  1146. /* the semaphore marks the extensions already written to the message */
  1147. #define TURN_ON(semaphore, light) \
  1148. ((semaphore)[(light) / 8] |= (byte) (0x01 << ((light) % 8)))
  1149. /** Turn off a specific light (tls extension) in the semaphore. */
  1150. #define TURN_OFF(semaphore, light) \
  1151. ((semaphore)[(light) / 8] &= (byte) ~(0x01 << ((light) % 8)))
  1152. /** Creates a new extension. */
  1153. static TLSX* TLSX_New(TLSX_Type type, const void* data, void* heap)
  1154. {
  1155. TLSX* extension = (TLSX*)XMALLOC(sizeof(TLSX), heap, DYNAMIC_TYPE_TLSX);
  1156. (void)heap;
  1157. if (extension) {
  1158. extension->type = type;
  1159. extension->data = (void*)data;
  1160. extension->resp = 0;
  1161. extension->next = NULL;
  1162. }
  1163. return extension;
  1164. }
  1165. /**
  1166. * Creates a new extension and appends it to the provided list.
  1167. * Checks for duplicate extensions, keeps the newest.
  1168. */
  1169. int TLSX_Append(TLSX** list, TLSX_Type type, const void* data, void* heap)
  1170. {
  1171. TLSX* extension = TLSX_New(type, data, heap);
  1172. TLSX* cur;
  1173. TLSX** prevNext = list;
  1174. if (extension == NULL)
  1175. return MEMORY_E;
  1176. for (cur = *list; cur != NULL;) {
  1177. if (cur->type == type) {
  1178. *prevNext = cur->next;
  1179. cur->next = NULL;
  1180. TLSX_FreeAll(cur, heap);
  1181. cur = *prevNext;
  1182. }
  1183. else {
  1184. prevNext = &cur->next;
  1185. cur = cur->next;
  1186. }
  1187. }
  1188. /* Append the extension to the list */
  1189. *prevNext = extension;
  1190. return 0;
  1191. }
  1192. /**
  1193. * Creates a new extension and pushes it to the provided list.
  1194. * Checks for duplicate extensions, keeps the newest.
  1195. */
  1196. int TLSX_Push(TLSX** list, TLSX_Type type, const void* data, void* heap)
  1197. {
  1198. TLSX* extension = TLSX_New(type, data, heap);
  1199. if (extension == NULL)
  1200. return MEMORY_E;
  1201. /* pushes the new extension on the list. */
  1202. extension->next = *list;
  1203. *list = extension;
  1204. /* remove duplicate extensions, there should be only one of each type. */
  1205. do {
  1206. if (extension->next && extension->next->type == type) {
  1207. TLSX *next = extension->next;
  1208. extension->next = next->next;
  1209. next->next = NULL;
  1210. TLSX_FreeAll(next, heap);
  1211. /* there is no way to occur more than
  1212. * two extensions of the same type.
  1213. */
  1214. break;
  1215. }
  1216. } while ((extension = extension->next));
  1217. return 0;
  1218. }
  1219. #ifndef NO_WOLFSSL_CLIENT
  1220. int TLSX_CheckUnsupportedExtension(WOLFSSL* ssl, TLSX_Type type);
  1221. int TLSX_CheckUnsupportedExtension(WOLFSSL* ssl, TLSX_Type type)
  1222. {
  1223. TLSX *extension = TLSX_Find(ssl->extensions, type);
  1224. if (!extension)
  1225. extension = TLSX_Find(ssl->ctx->extensions, type);
  1226. return extension == NULL;
  1227. }
  1228. int TLSX_HandleUnsupportedExtension(WOLFSSL* ssl);
  1229. int TLSX_HandleUnsupportedExtension(WOLFSSL* ssl)
  1230. {
  1231. SendAlert(ssl, alert_fatal, unsupported_extension);
  1232. WOLFSSL_ERROR_VERBOSE(UNSUPPORTED_EXTENSION);
  1233. return UNSUPPORTED_EXTENSION;
  1234. }
  1235. #else
  1236. #define TLSX_CheckUnsupportedExtension(ssl, type) 0
  1237. #define TLSX_HandleUnsupportedExtension(ssl) 0
  1238. #endif
  1239. #if !defined(NO_WOLFSSL_SERVER) || defined(WOLFSSL_TLS13)
  1240. void TLSX_SetResponse(WOLFSSL* ssl, TLSX_Type type);
  1241. /** Mark an extension to be sent back to the client. */
  1242. void TLSX_SetResponse(WOLFSSL* ssl, TLSX_Type type)
  1243. {
  1244. TLSX *extension = TLSX_Find(ssl->extensions, type);
  1245. if (extension)
  1246. extension->resp = 1;
  1247. }
  1248. #endif
  1249. /******************************************************************************/
  1250. /* Application-Layer Protocol Negotiation */
  1251. /******************************************************************************/
  1252. #ifdef HAVE_ALPN
  1253. /** Creates a new ALPN object, providing protocol name to use. */
  1254. static ALPN* TLSX_ALPN_New(char *protocol_name, word16 protocol_nameSz,
  1255. void* heap)
  1256. {
  1257. ALPN *alpn;
  1258. WOLFSSL_ENTER("TLSX_ALPN_New");
  1259. if (protocol_name == NULL ||
  1260. protocol_nameSz > WOLFSSL_MAX_ALPN_PROTO_NAME_LEN) {
  1261. WOLFSSL_MSG("Invalid arguments");
  1262. return NULL;
  1263. }
  1264. alpn = (ALPN*)XMALLOC(sizeof(ALPN), heap, DYNAMIC_TYPE_TLSX);
  1265. if (alpn == NULL) {
  1266. WOLFSSL_MSG("Memory failure");
  1267. return NULL;
  1268. }
  1269. alpn->next = NULL;
  1270. alpn->negotiated = 0;
  1271. alpn->options = 0;
  1272. alpn->protocol_name = (char*)XMALLOC(protocol_nameSz + 1,
  1273. heap, DYNAMIC_TYPE_TLSX);
  1274. if (alpn->protocol_name == NULL) {
  1275. WOLFSSL_MSG("Memory failure");
  1276. XFREE(alpn, heap, DYNAMIC_TYPE_TLSX);
  1277. return NULL;
  1278. }
  1279. XMEMCPY(alpn->protocol_name, protocol_name, protocol_nameSz);
  1280. alpn->protocol_name[protocol_nameSz] = 0;
  1281. (void)heap;
  1282. return alpn;
  1283. }
  1284. /** Releases an ALPN object. */
  1285. static void TLSX_ALPN_Free(ALPN *alpn, void* heap)
  1286. {
  1287. (void)heap;
  1288. if (alpn == NULL)
  1289. return;
  1290. XFREE(alpn->protocol_name, heap, DYNAMIC_TYPE_TLSX);
  1291. XFREE(alpn, heap, DYNAMIC_TYPE_TLSX);
  1292. }
  1293. /** Releases all ALPN objects in the provided list. */
  1294. static void TLSX_ALPN_FreeAll(ALPN *list, void* heap)
  1295. {
  1296. ALPN* alpn;
  1297. while ((alpn = list)) {
  1298. list = alpn->next;
  1299. TLSX_ALPN_Free(alpn, heap);
  1300. }
  1301. }
  1302. /** Tells the buffered size of the ALPN objects in a list. */
  1303. static word16 TLSX_ALPN_GetSize(ALPN *list)
  1304. {
  1305. ALPN* alpn;
  1306. word16 length = OPAQUE16_LEN; /* list length */
  1307. while ((alpn = list)) {
  1308. list = alpn->next;
  1309. length++; /* protocol name length is on one byte */
  1310. length += (word16)XSTRLEN(alpn->protocol_name);
  1311. }
  1312. return length;
  1313. }
  1314. /** Writes the ALPN objects of a list in a buffer. */
  1315. static word16 TLSX_ALPN_Write(ALPN *list, byte *output)
  1316. {
  1317. ALPN* alpn;
  1318. word16 length = 0;
  1319. word16 offset = OPAQUE16_LEN; /* list length offset */
  1320. while ((alpn = list)) {
  1321. list = alpn->next;
  1322. length = (word16)XSTRLEN(alpn->protocol_name);
  1323. /* protocol name length */
  1324. output[offset++] = (byte)length;
  1325. /* protocol name value */
  1326. XMEMCPY(output + offset, alpn->protocol_name, length);
  1327. offset += length;
  1328. }
  1329. /* writing list length */
  1330. c16toa(offset - OPAQUE16_LEN, output);
  1331. return offset;
  1332. }
  1333. /** Finds a protocol name in the provided ALPN list */
  1334. static ALPN* TLSX_ALPN_Find(ALPN *list, char *protocol_name, word16 size)
  1335. {
  1336. ALPN *alpn;
  1337. if (list == NULL || protocol_name == NULL)
  1338. return NULL;
  1339. alpn = list;
  1340. while (alpn != NULL && (
  1341. (word16)XSTRLEN(alpn->protocol_name) != size ||
  1342. XSTRNCMP(alpn->protocol_name, protocol_name, size)))
  1343. alpn = alpn->next;
  1344. return alpn;
  1345. }
  1346. /** Set the ALPN matching client and server requirements */
  1347. static int TLSX_SetALPN(TLSX** extensions, const void* data, word16 size,
  1348. void* heap)
  1349. {
  1350. ALPN *alpn;
  1351. int ret;
  1352. if (extensions == NULL || data == NULL)
  1353. return BAD_FUNC_ARG;
  1354. alpn = TLSX_ALPN_New((char *)data, size, heap);
  1355. if (alpn == NULL) {
  1356. WOLFSSL_MSG("Memory failure");
  1357. return MEMORY_E;
  1358. }
  1359. alpn->negotiated = 1;
  1360. ret = TLSX_Push(extensions, TLSX_APPLICATION_LAYER_PROTOCOL, (void*)alpn,
  1361. heap);
  1362. if (ret != 0) {
  1363. TLSX_ALPN_Free(alpn, heap);
  1364. return ret;
  1365. }
  1366. return WOLFSSL_SUCCESS;
  1367. }
  1368. static int ALPN_find_match(WOLFSSL *ssl, TLSX **pextension,
  1369. const byte **psel, byte *psel_len,
  1370. const byte *alpn_val, word16 alpn_val_len)
  1371. {
  1372. TLSX *extension;
  1373. ALPN *alpn, *list;
  1374. const byte *sel = NULL, *s;
  1375. byte sel_len = 0, wlen;
  1376. extension = TLSX_Find(ssl->extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
  1377. if (extension == NULL)
  1378. extension = TLSX_Find(ssl->ctx->extensions,
  1379. TLSX_APPLICATION_LAYER_PROTOCOL);
  1380. /* No ALPN configured here */
  1381. if (extension == NULL || extension->data == NULL) {
  1382. *pextension = NULL;
  1383. *psel = NULL;
  1384. *psel_len = 0;
  1385. return 0;
  1386. }
  1387. list = (ALPN*)extension->data;
  1388. for (s = alpn_val;
  1389. (s - alpn_val) < alpn_val_len;
  1390. s += wlen) {
  1391. wlen = *s++; /* bounds already checked on save */
  1392. alpn = TLSX_ALPN_Find(list, (char*)s, wlen);
  1393. if (alpn != NULL) {
  1394. WOLFSSL_MSG("ALPN protocol match");
  1395. sel = s,
  1396. sel_len = wlen;
  1397. break;
  1398. }
  1399. }
  1400. if (sel == NULL) {
  1401. WOLFSSL_MSG("No ALPN protocol match");
  1402. /* do nothing if no protocol match between client and server and option
  1403. is set to continue (like OpenSSL) */
  1404. if (list->options & WOLFSSL_ALPN_CONTINUE_ON_MISMATCH) {
  1405. WOLFSSL_MSG("Continue on mismatch");
  1406. }
  1407. else {
  1408. SendAlert(ssl, alert_fatal, no_application_protocol);
  1409. WOLFSSL_ERROR_VERBOSE(UNKNOWN_ALPN_PROTOCOL_NAME_E);
  1410. return UNKNOWN_ALPN_PROTOCOL_NAME_E;
  1411. }
  1412. }
  1413. *pextension = extension;
  1414. *psel = sel;
  1415. *psel_len = sel_len;
  1416. return 0;
  1417. }
  1418. int ALPN_Select(WOLFSSL *ssl)
  1419. {
  1420. TLSX *extension;
  1421. const byte *sel = NULL;
  1422. byte sel_len = 0;
  1423. int r = 0;
  1424. WOLFSSL_ENTER("ALPN_Select");
  1425. if (ssl->alpn_peer_requested == NULL)
  1426. return 0;
  1427. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
  1428. if (ssl->alpnSelect != NULL && ssl->options.side == WOLFSSL_SERVER_END) {
  1429. if (ssl->alpnSelect(ssl, &sel, &sel_len, ssl->alpn_peer_requested,
  1430. ssl->alpn_peer_requested_length,
  1431. ssl->alpnSelectArg) == 0) {
  1432. WOLFSSL_MSG("ALPN protocol match");
  1433. }
  1434. else {
  1435. sel = NULL;
  1436. sel_len = 0;
  1437. }
  1438. }
  1439. #endif
  1440. if (sel == NULL) {
  1441. r = ALPN_find_match(ssl, &extension, &sel, &sel_len,
  1442. ssl->alpn_peer_requested,
  1443. ssl->alpn_peer_requested_length);
  1444. if (r != 0)
  1445. return r;
  1446. }
  1447. if (sel != NULL) {
  1448. /* set the matching negotiated protocol */
  1449. r = TLSX_SetALPN(&ssl->extensions, sel, sel_len, ssl->heap);
  1450. if (r != WOLFSSL_SUCCESS) {
  1451. WOLFSSL_MSG("TLSX_SetALPN failed");
  1452. return BUFFER_ERROR;
  1453. }
  1454. /* reply to ALPN extension sent from peer */
  1455. #ifndef NO_WOLFSSL_SERVER
  1456. TLSX_SetResponse(ssl, TLSX_APPLICATION_LAYER_PROTOCOL);
  1457. #endif
  1458. }
  1459. return 0;
  1460. }
  1461. /** Parses a buffer of ALPN extensions and set the first one matching
  1462. * client and server requirements */
  1463. static int TLSX_ALPN_ParseAndSet(WOLFSSL *ssl, const byte *input, word16 length,
  1464. byte isRequest)
  1465. {
  1466. word16 size = 0, offset = 0, wlen;
  1467. int r = BUFFER_ERROR;
  1468. const byte *s;
  1469. if (OPAQUE16_LEN > length)
  1470. return BUFFER_ERROR;
  1471. ato16(input, &size);
  1472. offset += OPAQUE16_LEN;
  1473. /* validating alpn list length */
  1474. if (size == 0 || length != OPAQUE16_LEN + size)
  1475. return BUFFER_ERROR;
  1476. /* validating length of entries before accepting */
  1477. for (s = input + offset; (s - input) < size; s += wlen) {
  1478. wlen = *s++;
  1479. if (wlen == 0 || (s + wlen - input) > length)
  1480. return BUFFER_ERROR;
  1481. }
  1482. if (isRequest) {
  1483. /* keep the list sent by peer, if this is from a request. We
  1484. * use it later in ALPN_Select() for evaluation. */
  1485. if (ssl->alpn_peer_requested != NULL) {
  1486. XFREE(ssl->alpn_peer_requested, ssl->heap, DYNAMIC_TYPE_ALPN);
  1487. ssl->alpn_peer_requested_length = 0;
  1488. }
  1489. ssl->alpn_peer_requested = (byte *)XMALLOC(size, ssl->heap,
  1490. DYNAMIC_TYPE_ALPN);
  1491. if (ssl->alpn_peer_requested == NULL) {
  1492. return MEMORY_ERROR;
  1493. }
  1494. ssl->alpn_peer_requested_length = size;
  1495. XMEMCPY(ssl->alpn_peer_requested, (char*)input + offset, size);
  1496. }
  1497. else {
  1498. /* a response, we should find the value in our config */
  1499. const byte *sel = NULL;
  1500. byte sel_len = 0;
  1501. TLSX *extension = NULL;
  1502. r = ALPN_find_match(ssl, &extension, &sel, &sel_len, input + offset, size);
  1503. if (r != 0)
  1504. return r;
  1505. if (sel != NULL) {
  1506. /* set the matching negotiated protocol */
  1507. r = TLSX_SetALPN(&ssl->extensions, sel, sel_len, ssl->heap);
  1508. if (r != WOLFSSL_SUCCESS) {
  1509. WOLFSSL_MSG("TLSX_SetALPN failed");
  1510. return BUFFER_ERROR;
  1511. }
  1512. }
  1513. /* If we had nothing configured, the response is unexpected */
  1514. else if (extension == NULL) {
  1515. r = TLSX_HandleUnsupportedExtension(ssl);
  1516. if (r != 0)
  1517. return r;
  1518. }
  1519. }
  1520. return 0;
  1521. }
  1522. /** Add a protocol name to the list of accepted usable ones */
  1523. int TLSX_UseALPN(TLSX** extensions, const void* data, word16 size, byte options,
  1524. void* heap)
  1525. {
  1526. ALPN *alpn;
  1527. TLSX *extension;
  1528. int ret;
  1529. if (extensions == NULL || data == NULL)
  1530. return BAD_FUNC_ARG;
  1531. alpn = TLSX_ALPN_New((char *)data, size, heap);
  1532. if (alpn == NULL) {
  1533. WOLFSSL_MSG("Memory failure");
  1534. return MEMORY_E;
  1535. }
  1536. /* Set Options of ALPN */
  1537. alpn->options = options;
  1538. extension = TLSX_Find(*extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
  1539. if (extension == NULL) {
  1540. ret = TLSX_Push(extensions, TLSX_APPLICATION_LAYER_PROTOCOL,
  1541. (void*)alpn, heap);
  1542. if (ret != 0) {
  1543. TLSX_ALPN_Free(alpn, heap);
  1544. return ret;
  1545. }
  1546. }
  1547. else {
  1548. /* push new ALPN object to extension data. */
  1549. alpn->next = (ALPN*)extension->data;
  1550. extension->data = (void*)alpn;
  1551. }
  1552. return WOLFSSL_SUCCESS;
  1553. }
  1554. /** Get the protocol name set by the server */
  1555. int TLSX_ALPN_GetRequest(TLSX* extensions, void** data, word16 *dataSz)
  1556. {
  1557. TLSX *extension;
  1558. ALPN *alpn;
  1559. if (extensions == NULL || data == NULL || dataSz == NULL)
  1560. return BAD_FUNC_ARG;
  1561. *data = NULL;
  1562. *dataSz = 0;
  1563. extension = TLSX_Find(extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
  1564. if (extension == NULL) {
  1565. WOLFSSL_MSG("TLS extension not found");
  1566. WOLFSSL_ERROR_VERBOSE(WOLFSSL_ALPN_NOT_FOUND);
  1567. return WOLFSSL_ALPN_NOT_FOUND;
  1568. }
  1569. alpn = (ALPN *)extension->data;
  1570. if (alpn == NULL) {
  1571. WOLFSSL_MSG("ALPN extension not found");
  1572. WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR);
  1573. return WOLFSSL_FATAL_ERROR;
  1574. }
  1575. if (alpn->negotiated != 1) {
  1576. /* consider as an error */
  1577. if (alpn->options & WOLFSSL_ALPN_FAILED_ON_MISMATCH) {
  1578. WOLFSSL_MSG("No protocol match with peer -> Failed");
  1579. WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR);
  1580. return WOLFSSL_FATAL_ERROR;
  1581. }
  1582. /* continue without negotiated protocol */
  1583. WOLFSSL_MSG("No protocol match with peer -> Continue");
  1584. WOLFSSL_ERROR_VERBOSE(WOLFSSL_ALPN_NOT_FOUND);
  1585. return WOLFSSL_ALPN_NOT_FOUND;
  1586. }
  1587. if (alpn->next != NULL) {
  1588. WOLFSSL_MSG("Only one protocol name must be accepted");
  1589. WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR);
  1590. return WOLFSSL_FATAL_ERROR;
  1591. }
  1592. *data = alpn->protocol_name;
  1593. *dataSz = (word16)XSTRLEN((char*)*data);
  1594. return WOLFSSL_SUCCESS;
  1595. }
  1596. #define ALPN_FREE_ALL TLSX_ALPN_FreeAll
  1597. #define ALPN_GET_SIZE TLSX_ALPN_GetSize
  1598. #define ALPN_WRITE TLSX_ALPN_Write
  1599. #define ALPN_PARSE TLSX_ALPN_ParseAndSet
  1600. #else /* HAVE_ALPN */
  1601. #define ALPN_FREE_ALL(list, heap)
  1602. #define ALPN_GET_SIZE(list) 0
  1603. #define ALPN_WRITE(a, b) 0
  1604. #define ALPN_PARSE(a, b, c, d) 0
  1605. #endif /* HAVE_ALPN */
  1606. /******************************************************************************/
  1607. /* Server Name Indication */
  1608. /******************************************************************************/
  1609. #ifdef HAVE_SNI
  1610. /** Creates a new SNI object. */
  1611. static SNI* TLSX_SNI_New(byte type, const void* data, word16 size, void* heap)
  1612. {
  1613. SNI* sni = (SNI*)XMALLOC(sizeof(SNI), heap, DYNAMIC_TYPE_TLSX);
  1614. (void)heap;
  1615. if (sni) {
  1616. sni->type = type;
  1617. sni->next = NULL;
  1618. #ifndef NO_WOLFSSL_SERVER
  1619. sni->options = 0;
  1620. sni->status = WOLFSSL_SNI_NO_MATCH;
  1621. #endif
  1622. switch (sni->type) {
  1623. case WOLFSSL_SNI_HOST_NAME:
  1624. sni->data.host_name = (char*)XMALLOC(size + 1, heap,
  1625. DYNAMIC_TYPE_TLSX);
  1626. if (sni->data.host_name) {
  1627. XSTRNCPY(sni->data.host_name, (const char*)data, size);
  1628. sni->data.host_name[size] = '\0';
  1629. } else {
  1630. XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
  1631. sni = NULL;
  1632. }
  1633. break;
  1634. default: /* invalid type */
  1635. XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
  1636. sni = NULL;
  1637. }
  1638. }
  1639. return sni;
  1640. }
  1641. /** Releases a SNI object. */
  1642. static void TLSX_SNI_Free(SNI* sni, void* heap)
  1643. {
  1644. if (sni) {
  1645. switch (sni->type) {
  1646. case WOLFSSL_SNI_HOST_NAME:
  1647. XFREE(sni->data.host_name, heap, DYNAMIC_TYPE_TLSX);
  1648. break;
  1649. }
  1650. XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
  1651. }
  1652. (void)heap;
  1653. }
  1654. /** Releases all SNI objects in the provided list. */
  1655. static void TLSX_SNI_FreeAll(SNI* list, void* heap)
  1656. {
  1657. SNI* sni;
  1658. while ((sni = list)) {
  1659. list = sni->next;
  1660. TLSX_SNI_Free(sni, heap);
  1661. }
  1662. }
  1663. /** Tells the buffered size of the SNI objects in a list. */
  1664. static word16 TLSX_SNI_GetSize(SNI* list)
  1665. {
  1666. SNI* sni;
  1667. word16 length = OPAQUE16_LEN; /* list length */
  1668. while ((sni = list)) {
  1669. list = sni->next;
  1670. length += ENUM_LEN + OPAQUE16_LEN; /* sni type + sni length */
  1671. switch (sni->type) {
  1672. case WOLFSSL_SNI_HOST_NAME:
  1673. length += (word16)XSTRLEN((char*)sni->data.host_name);
  1674. break;
  1675. }
  1676. }
  1677. return length;
  1678. }
  1679. /** Writes the SNI objects of a list in a buffer. */
  1680. static word16 TLSX_SNI_Write(SNI* list, byte* output)
  1681. {
  1682. SNI* sni;
  1683. word16 length = 0;
  1684. word16 offset = OPAQUE16_LEN; /* list length offset */
  1685. while ((sni = list)) {
  1686. list = sni->next;
  1687. output[offset++] = sni->type; /* sni type */
  1688. switch (sni->type) {
  1689. case WOLFSSL_SNI_HOST_NAME:
  1690. length = (word16)XSTRLEN((char*)sni->data.host_name);
  1691. c16toa(length, output + offset); /* sni length */
  1692. offset += OPAQUE16_LEN;
  1693. XMEMCPY(output + offset, sni->data.host_name, length);
  1694. offset += length;
  1695. break;
  1696. }
  1697. }
  1698. c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
  1699. return offset;
  1700. }
  1701. /** Finds a SNI object in the provided list. */
  1702. static SNI* TLSX_SNI_Find(SNI *list, byte type)
  1703. {
  1704. SNI* sni = list;
  1705. while (sni && sni->type != type)
  1706. sni = sni->next;
  1707. return sni;
  1708. }
  1709. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  1710. /** Sets the status of a SNI object. */
  1711. static void TLSX_SNI_SetStatus(TLSX* extensions, byte type, byte status)
  1712. {
  1713. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1714. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1715. if (sni)
  1716. sni->status = status;
  1717. }
  1718. #endif
  1719. /** Gets the status of a SNI object. */
  1720. byte TLSX_SNI_Status(TLSX* extensions, byte type)
  1721. {
  1722. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1723. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1724. if (sni)
  1725. return sni->status;
  1726. return 0;
  1727. }
  1728. /** Parses a buffer of SNI extensions. */
  1729. static int TLSX_SNI_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  1730. byte isRequest)
  1731. {
  1732. #ifndef NO_WOLFSSL_SERVER
  1733. word16 size = 0;
  1734. word16 offset = 0;
  1735. int cacheOnly = 0;
  1736. SNI *sni = NULL;
  1737. byte type;
  1738. byte matched;
  1739. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  1740. WOLFSSL_ECH* ech = NULL;
  1741. WOLFSSL_EchConfig* workingConfig;
  1742. TLSX* echX;
  1743. #endif
  1744. #endif /* !NO_WOLFSSL_SERVER */
  1745. TLSX *extension = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
  1746. if (!extension)
  1747. extension = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
  1748. if (!isRequest) {
  1749. #ifndef NO_WOLFSSL_CLIENT
  1750. if (!extension || !extension->data)
  1751. return TLSX_HandleUnsupportedExtension(ssl);
  1752. if (length > 0)
  1753. return BUFFER_ERROR; /* SNI response MUST be empty. */
  1754. /* This call enables wolfSSL_SNI_GetRequest() to be called in the
  1755. * client side to fetch the used SNI. It will only work if the SNI
  1756. * was set at the SSL object level. Right now we only support one
  1757. * name type, WOLFSSL_SNI_HOST_NAME, but in the future, the
  1758. * inclusion of other name types will turn this method inaccurate,
  1759. * as the extension response doesn't contains information of which
  1760. * name was accepted.
  1761. */
  1762. TLSX_SNI_SetStatus(ssl->extensions, WOLFSSL_SNI_HOST_NAME,
  1763. WOLFSSL_SNI_REAL_MATCH);
  1764. return 0;
  1765. #endif
  1766. }
  1767. #ifndef NO_WOLFSSL_SERVER
  1768. if (!extension || !extension->data) {
  1769. /* This will keep SNI even though TLSX_UseSNI has not been called.
  1770. * Enable it so that the received sni is available to functions
  1771. * that use a custom callback when SNI is received.
  1772. */
  1773. #ifdef WOLFSSL_ALWAYS_KEEP_SNI
  1774. cacheOnly = 1;
  1775. #endif
  1776. if (ssl->ctx->sniRecvCb) {
  1777. cacheOnly = 1;
  1778. }
  1779. if (cacheOnly) {
  1780. WOLFSSL_MSG("Forcing SSL object to store SNI parameter");
  1781. }
  1782. else {
  1783. /* Skipping, SNI not enabled at server side. */
  1784. return 0;
  1785. }
  1786. }
  1787. if (OPAQUE16_LEN > length)
  1788. return BUFFER_ERROR;
  1789. ato16(input, &size);
  1790. offset += OPAQUE16_LEN;
  1791. /* validating sni list length */
  1792. if (length != OPAQUE16_LEN + size || size == 0)
  1793. return BUFFER_ERROR;
  1794. /* SNI was badly specified and only one type is now recognized and allowed.
  1795. * Only one SNI value per type (RFC6066), so, no loop. */
  1796. type = input[offset++];
  1797. if (type != WOLFSSL_SNI_HOST_NAME)
  1798. return BUFFER_ERROR;
  1799. if (offset + OPAQUE16_LEN > length)
  1800. return BUFFER_ERROR;
  1801. ato16(input + offset, &size);
  1802. offset += OPAQUE16_LEN;
  1803. if (offset + size != length || size == 0)
  1804. return BUFFER_ERROR;
  1805. if (!cacheOnly && !(sni = TLSX_SNI_Find((SNI*)extension->data, type)))
  1806. return 0; /* not using this type of SNI. */
  1807. #ifdef WOLFSSL_TLS13
  1808. /* Don't process the second ClientHello SNI extension if there
  1809. * was problems with the first.
  1810. */
  1811. if (!cacheOnly && sni->status != 0)
  1812. return 0;
  1813. #endif
  1814. matched = cacheOnly || (XSTRLEN(sni->data.host_name) == size &&
  1815. XSTRNCMP(sni->data.host_name, (const char*)input + offset, size) == 0);
  1816. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  1817. echX = TLSX_Find(ssl->extensions, TLSX_ECH);
  1818. if (echX != NULL)
  1819. ech = (WOLFSSL_ECH*)(echX->data);
  1820. if (!matched && ech != NULL) {
  1821. workingConfig = ech->echConfig;
  1822. while (workingConfig != NULL) {
  1823. matched = XSTRLEN(workingConfig->publicName) == size &&
  1824. XSTRNCMP(workingConfig->publicName,
  1825. (const char*)input + offset, size) == 0;
  1826. if (matched)
  1827. break;
  1828. workingConfig = workingConfig->next;
  1829. }
  1830. }
  1831. #endif
  1832. if (matched || sni->options & WOLFSSL_SNI_ANSWER_ON_MISMATCH) {
  1833. int matchStat;
  1834. int r = TLSX_UseSNI(&ssl->extensions, type, input + offset, size,
  1835. ssl->heap);
  1836. if (r != WOLFSSL_SUCCESS)
  1837. return r; /* throws error. */
  1838. if (cacheOnly) {
  1839. WOLFSSL_MSG("Forcing storage of SNI, Fake match");
  1840. matchStat = WOLFSSL_SNI_FORCE_KEEP;
  1841. }
  1842. else if (matched) {
  1843. WOLFSSL_MSG("SNI did match!");
  1844. matchStat = WOLFSSL_SNI_REAL_MATCH;
  1845. }
  1846. else {
  1847. WOLFSSL_MSG("fake SNI match from ANSWER_ON_MISMATCH");
  1848. matchStat = WOLFSSL_SNI_FAKE_MATCH;
  1849. }
  1850. TLSX_SNI_SetStatus(ssl->extensions, type, (byte)matchStat);
  1851. if (!cacheOnly)
  1852. TLSX_SetResponse(ssl, TLSX_SERVER_NAME);
  1853. }
  1854. else if (!(sni->options & WOLFSSL_SNI_CONTINUE_ON_MISMATCH)) {
  1855. SendAlert(ssl, alert_fatal, unrecognized_name);
  1856. WOLFSSL_ERROR_VERBOSE(UNKNOWN_SNI_HOST_NAME_E);
  1857. return UNKNOWN_SNI_HOST_NAME_E;
  1858. }
  1859. #else
  1860. (void)input;
  1861. #endif /* !NO_WOLFSSL_SERVER */
  1862. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1863. (void)length;
  1864. #endif
  1865. return 0;
  1866. }
  1867. static int TLSX_SNI_VerifyParse(WOLFSSL* ssl, byte isRequest)
  1868. {
  1869. (void)ssl;
  1870. if (isRequest) {
  1871. #ifndef NO_WOLFSSL_SERVER
  1872. TLSX* ctx_ext = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
  1873. TLSX* ssl_ext = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
  1874. SNI* ctx_sni = ctx_ext ? (SNI*)ctx_ext->data : NULL;
  1875. SNI* ssl_sni = ssl_ext ? (SNI*)ssl_ext->data : NULL;
  1876. SNI* sni = NULL;
  1877. for (; ctx_sni; ctx_sni = ctx_sni->next) {
  1878. if (ctx_sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) {
  1879. sni = TLSX_SNI_Find(ssl_sni, ctx_sni->type);
  1880. if (sni) {
  1881. if (sni->status != WOLFSSL_SNI_NO_MATCH)
  1882. continue;
  1883. /* if ssl level overrides ctx level, it is ok. */
  1884. if ((sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) == 0)
  1885. continue;
  1886. }
  1887. SendAlert(ssl, alert_fatal, handshake_failure);
  1888. WOLFSSL_ERROR_VERBOSE(SNI_ABSENT_ERROR);
  1889. return SNI_ABSENT_ERROR;
  1890. }
  1891. }
  1892. for (; ssl_sni; ssl_sni = ssl_sni->next) {
  1893. if (ssl_sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) {
  1894. if (ssl_sni->status != WOLFSSL_SNI_NO_MATCH)
  1895. continue;
  1896. SendAlert(ssl, alert_fatal, handshake_failure);
  1897. WOLFSSL_ERROR_VERBOSE(SNI_ABSENT_ERROR);
  1898. return SNI_ABSENT_ERROR;
  1899. }
  1900. }
  1901. #endif /* NO_WOLFSSL_SERVER */
  1902. }
  1903. return 0;
  1904. }
  1905. int TLSX_UseSNI(TLSX** extensions, byte type, const void* data, word16 size,
  1906. void* heap)
  1907. {
  1908. TLSX* extension;
  1909. SNI* sni = NULL;
  1910. if (extensions == NULL || data == NULL)
  1911. return BAD_FUNC_ARG;
  1912. if ((sni = TLSX_SNI_New(type, data, size, heap)) == NULL)
  1913. return MEMORY_E;
  1914. extension = TLSX_Find(*extensions, TLSX_SERVER_NAME);
  1915. if (!extension) {
  1916. int ret = TLSX_Push(extensions, TLSX_SERVER_NAME, (void*)sni, heap);
  1917. if (ret != 0) {
  1918. TLSX_SNI_Free(sni, heap);
  1919. return ret;
  1920. }
  1921. }
  1922. else {
  1923. /* push new SNI object to extension data. */
  1924. sni->next = (SNI*)extension->data;
  1925. extension->data = (void*)sni;
  1926. /* remove duplicate SNI, there should be only one of each type. */
  1927. do {
  1928. if (sni->next && sni->next->type == type) {
  1929. SNI* next = sni->next;
  1930. sni->next = next->next;
  1931. TLSX_SNI_Free(next, heap);
  1932. /* there is no way to occur more than
  1933. * two SNIs of the same type.
  1934. */
  1935. break;
  1936. }
  1937. } while ((sni = sni->next));
  1938. }
  1939. return WOLFSSL_SUCCESS;
  1940. }
  1941. #ifndef NO_WOLFSSL_SERVER
  1942. /** Tells the SNI requested by the client. */
  1943. word16 TLSX_SNI_GetRequest(TLSX* extensions, byte type, void** data)
  1944. {
  1945. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1946. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1947. if (sni && sni->status != WOLFSSL_SNI_NO_MATCH) {
  1948. switch (sni->type) {
  1949. case WOLFSSL_SNI_HOST_NAME:
  1950. if (data) {
  1951. *data = sni->data.host_name;
  1952. return (word16)XSTRLEN((char*)*data);
  1953. }
  1954. }
  1955. }
  1956. return 0;
  1957. }
  1958. /** Sets the options for a SNI object. */
  1959. void TLSX_SNI_SetOptions(TLSX* extensions, byte type, byte options)
  1960. {
  1961. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1962. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1963. if (sni)
  1964. sni->options = options;
  1965. }
  1966. /** Retrieves a SNI request from a client hello buffer. */
  1967. int TLSX_SNI_GetFromBuffer(const byte* clientHello, word32 helloSz,
  1968. byte type, byte* sni, word32* inOutSz)
  1969. {
  1970. word32 offset = 0;
  1971. word32 len32 = 0;
  1972. word16 len16 = 0;
  1973. if (helloSz < RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ + CLIENT_HELLO_FIRST)
  1974. return INCOMPLETE_DATA;
  1975. /* TLS record header */
  1976. if ((enum ContentType) clientHello[offset++] != handshake) {
  1977. /* checking for SSLv2.0 client hello according to: */
  1978. /* http://tools.ietf.org/html/rfc4346#appendix-E.1 */
  1979. if ((enum HandShakeType) clientHello[++offset] == client_hello) {
  1980. offset += ENUM_LEN + VERSION_SZ; /* skip version */
  1981. ato16(clientHello + offset, &len16);
  1982. offset += OPAQUE16_LEN;
  1983. if (len16 % 3) /* cipher_spec_length must be multiple of 3 */
  1984. return BUFFER_ERROR;
  1985. ato16(clientHello + offset, &len16);
  1986. /* Returning SNI_UNSUPPORTED do not increment offset here */
  1987. if (len16 != 0) /* session_id_length must be 0 */
  1988. return BUFFER_ERROR;
  1989. WOLFSSL_ERROR_VERBOSE(SNI_UNSUPPORTED);
  1990. return SNI_UNSUPPORTED;
  1991. }
  1992. return BUFFER_ERROR;
  1993. }
  1994. if (clientHello[offset++] != SSLv3_MAJOR)
  1995. return BUFFER_ERROR;
  1996. if (clientHello[offset++] < TLSv1_MINOR) {
  1997. WOLFSSL_ERROR_VERBOSE(SNI_UNSUPPORTED);
  1998. return SNI_UNSUPPORTED;
  1999. }
  2000. ato16(clientHello + offset, &len16);
  2001. offset += OPAQUE16_LEN;
  2002. if (offset + len16 > helloSz)
  2003. return INCOMPLETE_DATA;
  2004. /* Handshake header */
  2005. if ((enum HandShakeType) clientHello[offset] != client_hello)
  2006. return BUFFER_ERROR;
  2007. c24to32(clientHello + offset + 1, &len32);
  2008. offset += HANDSHAKE_HEADER_SZ;
  2009. if (offset + len32 > helloSz)
  2010. return BUFFER_ERROR;
  2011. /* client hello */
  2012. offset += VERSION_SZ + RAN_LEN; /* version, random */
  2013. if (helloSz < offset + clientHello[offset])
  2014. return BUFFER_ERROR;
  2015. offset += ENUM_LEN + clientHello[offset]; /* skip session id */
  2016. /* cypher suites */
  2017. if (helloSz < offset + OPAQUE16_LEN)
  2018. return BUFFER_ERROR;
  2019. ato16(clientHello + offset, &len16);
  2020. offset += OPAQUE16_LEN;
  2021. if (helloSz < offset + len16)
  2022. return BUFFER_ERROR;
  2023. offset += len16; /* skip cypher suites */
  2024. /* compression methods */
  2025. if (helloSz < offset + 1)
  2026. return BUFFER_ERROR;
  2027. if (helloSz < offset + clientHello[offset])
  2028. return BUFFER_ERROR;
  2029. offset += ENUM_LEN + clientHello[offset]; /* skip compression methods */
  2030. /* extensions */
  2031. if (helloSz < offset + OPAQUE16_LEN)
  2032. return 0; /* no extensions in client hello. */
  2033. ato16(clientHello + offset, &len16);
  2034. offset += OPAQUE16_LEN;
  2035. if (helloSz < offset + len16)
  2036. return BUFFER_ERROR;
  2037. while (len16 >= OPAQUE16_LEN + OPAQUE16_LEN) {
  2038. word16 extType;
  2039. word16 extLen;
  2040. ato16(clientHello + offset, &extType);
  2041. offset += OPAQUE16_LEN;
  2042. ato16(clientHello + offset, &extLen);
  2043. offset += OPAQUE16_LEN;
  2044. if (helloSz < offset + extLen)
  2045. return BUFFER_ERROR;
  2046. if (extType != TLSX_SERVER_NAME) {
  2047. offset += extLen; /* skip extension */
  2048. } else {
  2049. word16 listLen;
  2050. ato16(clientHello + offset, &listLen);
  2051. offset += OPAQUE16_LEN;
  2052. if (helloSz < offset + listLen)
  2053. return BUFFER_ERROR;
  2054. while (listLen > ENUM_LEN + OPAQUE16_LEN) {
  2055. byte sniType = clientHello[offset++];
  2056. word16 sniLen;
  2057. ato16(clientHello + offset, &sniLen);
  2058. offset += OPAQUE16_LEN;
  2059. if (helloSz < offset + sniLen)
  2060. return BUFFER_ERROR;
  2061. if (sniType != type) {
  2062. offset += sniLen;
  2063. listLen -= min(ENUM_LEN + OPAQUE16_LEN + sniLen, listLen);
  2064. continue;
  2065. }
  2066. *inOutSz = min(sniLen, *inOutSz);
  2067. XMEMCPY(sni, clientHello + offset, *inOutSz);
  2068. return WOLFSSL_SUCCESS;
  2069. }
  2070. }
  2071. len16 -= min(2 * OPAQUE16_LEN + extLen, len16);
  2072. }
  2073. return len16 ? BUFFER_ERROR : 0;
  2074. }
  2075. #endif
  2076. #define SNI_FREE_ALL TLSX_SNI_FreeAll
  2077. #define SNI_GET_SIZE TLSX_SNI_GetSize
  2078. #define SNI_WRITE TLSX_SNI_Write
  2079. #define SNI_PARSE TLSX_SNI_Parse
  2080. #define SNI_VERIFY_PARSE TLSX_SNI_VerifyParse
  2081. #else
  2082. #define SNI_FREE_ALL(list, heap)
  2083. #define SNI_GET_SIZE(list) 0
  2084. #define SNI_WRITE(a, b) 0
  2085. #define SNI_PARSE(a, b, c, d) 0
  2086. #define SNI_VERIFY_PARSE(a, b) 0
  2087. #endif /* HAVE_SNI */
  2088. /******************************************************************************/
  2089. /* Trusted CA Key Indication */
  2090. /******************************************************************************/
  2091. #ifdef HAVE_TRUSTED_CA
  2092. /** Creates a new TCA object. */
  2093. static TCA* TLSX_TCA_New(byte type, const byte* id, word16 idSz, void* heap)
  2094. {
  2095. TCA* tca = (TCA*)XMALLOC(sizeof(TCA), heap, DYNAMIC_TYPE_TLSX);
  2096. if (tca) {
  2097. XMEMSET(tca, 0, sizeof(TCA));
  2098. tca->type = type;
  2099. switch (type) {
  2100. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2101. break;
  2102. #ifndef NO_SHA
  2103. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2104. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2105. if (idSz == WC_SHA_DIGEST_SIZE &&
  2106. (tca->id =
  2107. (byte*)XMALLOC(idSz, heap, DYNAMIC_TYPE_TLSX))) {
  2108. XMEMCPY(tca->id, id, idSz);
  2109. tca->idSz = idSz;
  2110. }
  2111. else {
  2112. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2113. tca = NULL;
  2114. }
  2115. break;
  2116. #endif
  2117. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2118. if (idSz > 0 &&
  2119. (tca->id =
  2120. (byte*)XMALLOC(idSz, heap, DYNAMIC_TYPE_TLSX))) {
  2121. XMEMCPY(tca->id, id, idSz);
  2122. tca->idSz = idSz;
  2123. }
  2124. else {
  2125. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2126. tca = NULL;
  2127. }
  2128. break;
  2129. default: /* invalid type */
  2130. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2131. tca = NULL;
  2132. }
  2133. }
  2134. (void)heap;
  2135. return tca;
  2136. }
  2137. /** Releases a TCA object. */
  2138. static void TLSX_TCA_Free(TCA* tca, void* heap)
  2139. {
  2140. (void)heap;
  2141. if (tca) {
  2142. if (tca->id)
  2143. XFREE(tca->id, heap, DYNAMIC_TYPE_TLSX);
  2144. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2145. }
  2146. }
  2147. /** Releases all TCA objects in the provided list. */
  2148. static void TLSX_TCA_FreeAll(TCA* list, void* heap)
  2149. {
  2150. TCA* tca;
  2151. while ((tca = list)) {
  2152. list = tca->next;
  2153. TLSX_TCA_Free(tca, heap);
  2154. }
  2155. }
  2156. /** Tells the buffered size of the TCA objects in a list. */
  2157. static word16 TLSX_TCA_GetSize(TCA* list)
  2158. {
  2159. TCA* tca;
  2160. word16 length = OPAQUE16_LEN; /* list length */
  2161. while ((tca = list)) {
  2162. list = tca->next;
  2163. length += ENUM_LEN; /* tca type */
  2164. switch (tca->type) {
  2165. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2166. break;
  2167. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2168. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2169. length += tca->idSz;
  2170. break;
  2171. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2172. length += OPAQUE16_LEN + tca->idSz;
  2173. break;
  2174. }
  2175. }
  2176. return length;
  2177. }
  2178. /** Writes the TCA objects of a list in a buffer. */
  2179. static word16 TLSX_TCA_Write(TCA* list, byte* output)
  2180. {
  2181. TCA* tca;
  2182. word16 offset = OPAQUE16_LEN; /* list length offset */
  2183. while ((tca = list)) {
  2184. list = tca->next;
  2185. output[offset++] = tca->type; /* tca type */
  2186. switch (tca->type) {
  2187. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2188. break;
  2189. #ifndef NO_SHA
  2190. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2191. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2192. if (tca->id != NULL) {
  2193. XMEMCPY(output + offset, tca->id, tca->idSz);
  2194. offset += tca->idSz;
  2195. }
  2196. else {
  2197. /* ID missing. Set to an empty string. */
  2198. c16toa(0, output + offset);
  2199. offset += OPAQUE16_LEN;
  2200. }
  2201. break;
  2202. #endif
  2203. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2204. if (tca->id != NULL) {
  2205. c16toa(tca->idSz, output + offset); /* tca length */
  2206. offset += OPAQUE16_LEN;
  2207. XMEMCPY(output + offset, tca->id, tca->idSz);
  2208. offset += tca->idSz;
  2209. }
  2210. else {
  2211. /* ID missing. Set to an empty string. */
  2212. c16toa(0, output + offset);
  2213. offset += OPAQUE16_LEN;
  2214. }
  2215. break;
  2216. default:
  2217. /* ID unknown. Set to an empty string. */
  2218. c16toa(0, output + offset);
  2219. offset += OPAQUE16_LEN;
  2220. }
  2221. }
  2222. c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
  2223. return offset;
  2224. }
  2225. #ifndef NO_WOLFSSL_SERVER
  2226. static TCA* TLSX_TCA_Find(TCA *list, byte type, const byte* id, word16 idSz)
  2227. {
  2228. TCA* tca = list;
  2229. while (tca && tca->type != type && type != WOLFSSL_TRUSTED_CA_PRE_AGREED &&
  2230. idSz != tca->idSz && !XMEMCMP(id, tca->id, idSz))
  2231. tca = tca->next;
  2232. return tca;
  2233. }
  2234. #endif /* NO_WOLFSSL_SERVER */
  2235. /** Parses a buffer of TCA extensions. */
  2236. static int TLSX_TCA_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2237. byte isRequest)
  2238. {
  2239. #ifndef NO_WOLFSSL_SERVER
  2240. word16 size = 0;
  2241. word16 offset = 0;
  2242. #endif
  2243. TLSX *extension = TLSX_Find(ssl->extensions, TLSX_TRUSTED_CA_KEYS);
  2244. if (!extension)
  2245. extension = TLSX_Find(ssl->ctx->extensions, TLSX_TRUSTED_CA_KEYS);
  2246. if (!isRequest) {
  2247. #ifndef NO_WOLFSSL_CLIENT
  2248. if (!extension || !extension->data)
  2249. return TLSX_HandleUnsupportedExtension(ssl);
  2250. if (length > 0)
  2251. return BUFFER_ERROR; /* TCA response MUST be empty. */
  2252. /* Set the flag that we're good for keys */
  2253. TLSX_SetResponse(ssl, TLSX_TRUSTED_CA_KEYS);
  2254. return 0;
  2255. #endif
  2256. }
  2257. #ifndef NO_WOLFSSL_SERVER
  2258. if (!extension || !extension->data) {
  2259. /* Skipping, TCA not enabled at server side. */
  2260. return 0;
  2261. }
  2262. if (OPAQUE16_LEN > length)
  2263. return BUFFER_ERROR;
  2264. ato16(input, &size);
  2265. offset += OPAQUE16_LEN;
  2266. /* validating tca list length */
  2267. if (length != OPAQUE16_LEN + size)
  2268. return BUFFER_ERROR;
  2269. for (size = 0; offset < length; offset += size) {
  2270. TCA *tca = NULL;
  2271. byte type;
  2272. const byte* id = NULL;
  2273. word16 idSz = 0;
  2274. if (offset + ENUM_LEN > length)
  2275. return BUFFER_ERROR;
  2276. type = input[offset++];
  2277. switch (type) {
  2278. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2279. break;
  2280. #ifndef NO_SHA
  2281. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2282. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2283. if (offset + WC_SHA_DIGEST_SIZE > length)
  2284. return BUFFER_ERROR;
  2285. idSz = WC_SHA_DIGEST_SIZE;
  2286. id = input + offset;
  2287. offset += idSz;
  2288. break;
  2289. #endif
  2290. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2291. if (offset + OPAQUE16_LEN > length)
  2292. return BUFFER_ERROR;
  2293. ato16(input + offset, &idSz);
  2294. offset += OPAQUE16_LEN;
  2295. if ((offset > length) || (idSz > length - offset))
  2296. return BUFFER_ERROR;
  2297. id = input + offset;
  2298. offset += idSz;
  2299. break;
  2300. default:
  2301. WOLFSSL_ERROR_VERBOSE(TCA_INVALID_ID_TYPE);
  2302. return TCA_INVALID_ID_TYPE;
  2303. }
  2304. /* Find the type/ID in the TCA list. */
  2305. tca = TLSX_TCA_Find((TCA*)extension->data, type, id, idSz);
  2306. if (tca != NULL) {
  2307. /* Found it. Set the response flag and break out of the loop. */
  2308. TLSX_SetResponse(ssl, TLSX_TRUSTED_CA_KEYS);
  2309. break;
  2310. }
  2311. }
  2312. #else
  2313. (void)input;
  2314. #endif
  2315. return 0;
  2316. }
  2317. /* Checks to see if the server sent a response for the TCA. */
  2318. static int TLSX_TCA_VerifyParse(WOLFSSL* ssl, byte isRequest)
  2319. {
  2320. (void)ssl;
  2321. if (!isRequest) {
  2322. #ifndef NO_WOLFSSL_CLIENT
  2323. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_TRUSTED_CA_KEYS);
  2324. if (extension && !extension->resp) {
  2325. SendAlert(ssl, alert_fatal, handshake_failure);
  2326. WOLFSSL_ERROR_VERBOSE(TCA_ABSENT_ERROR);
  2327. return TCA_ABSENT_ERROR;
  2328. }
  2329. #endif /* NO_WOLFSSL_CLIENT */
  2330. }
  2331. return 0;
  2332. }
  2333. int TLSX_UseTrustedCA(TLSX** extensions, byte type,
  2334. const byte* id, word16 idSz, void* heap)
  2335. {
  2336. TLSX* extension;
  2337. TCA* tca = NULL;
  2338. if (extensions == NULL)
  2339. return BAD_FUNC_ARG;
  2340. if ((tca = TLSX_TCA_New(type, id, idSz, heap)) == NULL)
  2341. return MEMORY_E;
  2342. extension = TLSX_Find(*extensions, TLSX_TRUSTED_CA_KEYS);
  2343. if (!extension) {
  2344. int ret = TLSX_Push(extensions, TLSX_TRUSTED_CA_KEYS, (void*)tca, heap);
  2345. if (ret != 0) {
  2346. TLSX_TCA_Free(tca, heap);
  2347. return ret;
  2348. }
  2349. }
  2350. else {
  2351. /* push new TCA object to extension data. */
  2352. tca->next = (TCA*)extension->data;
  2353. extension->data = (void*)tca;
  2354. }
  2355. return WOLFSSL_SUCCESS;
  2356. }
  2357. #define TCA_FREE_ALL TLSX_TCA_FreeAll
  2358. #define TCA_GET_SIZE TLSX_TCA_GetSize
  2359. #define TCA_WRITE TLSX_TCA_Write
  2360. #define TCA_PARSE TLSX_TCA_Parse
  2361. #define TCA_VERIFY_PARSE TLSX_TCA_VerifyParse
  2362. #else /* HAVE_TRUSTED_CA */
  2363. #define TCA_FREE_ALL(list, heap)
  2364. #define TCA_GET_SIZE(list) 0
  2365. #define TCA_WRITE(a, b) 0
  2366. #define TCA_PARSE(a, b, c, d) 0
  2367. #define TCA_VERIFY_PARSE(a, b) 0
  2368. #endif /* HAVE_TRUSTED_CA */
  2369. /******************************************************************************/
  2370. /* Max Fragment Length Negotiation */
  2371. /******************************************************************************/
  2372. #ifdef HAVE_MAX_FRAGMENT
  2373. static word16 TLSX_MFL_Write(byte* data, byte* output)
  2374. {
  2375. output[0] = data[0];
  2376. return ENUM_LEN;
  2377. }
  2378. static int TLSX_MFL_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2379. byte isRequest)
  2380. {
  2381. if (length != ENUM_LEN)
  2382. return BUFFER_ERROR;
  2383. #ifdef WOLFSSL_OLD_UNSUPPORTED_EXTENSION
  2384. (void) isRequest;
  2385. #else
  2386. if (!isRequest)
  2387. if (TLSX_CheckUnsupportedExtension(ssl, TLSX_MAX_FRAGMENT_LENGTH))
  2388. return TLSX_HandleUnsupportedExtension(ssl);
  2389. #endif
  2390. switch (*input) {
  2391. case WOLFSSL_MFL_2_8 : ssl->max_fragment = 256; break;
  2392. case WOLFSSL_MFL_2_9 : ssl->max_fragment = 512; break;
  2393. case WOLFSSL_MFL_2_10: ssl->max_fragment = 1024; break;
  2394. case WOLFSSL_MFL_2_11: ssl->max_fragment = 2048; break;
  2395. case WOLFSSL_MFL_2_12: ssl->max_fragment = 4096; break;
  2396. case WOLFSSL_MFL_2_13: ssl->max_fragment = 8192; break;
  2397. default:
  2398. SendAlert(ssl, alert_fatal, illegal_parameter);
  2399. WOLFSSL_ERROR_VERBOSE(UNKNOWN_MAX_FRAG_LEN_E);
  2400. return UNKNOWN_MAX_FRAG_LEN_E;
  2401. }
  2402. #ifndef NO_WOLFSSL_SERVER
  2403. if (isRequest) {
  2404. int ret = TLSX_UseMaxFragment(&ssl->extensions, *input, ssl->heap);
  2405. if (ret != WOLFSSL_SUCCESS)
  2406. return ret; /* throw error */
  2407. TLSX_SetResponse(ssl, TLSX_MAX_FRAGMENT_LENGTH);
  2408. }
  2409. #endif
  2410. return 0;
  2411. }
  2412. int TLSX_UseMaxFragment(TLSX** extensions, byte mfl, void* heap)
  2413. {
  2414. byte* data = NULL;
  2415. int ret = 0;
  2416. if (extensions == NULL || mfl < WOLFSSL_MFL_MIN || mfl > WOLFSSL_MFL_MAX)
  2417. return BAD_FUNC_ARG;
  2418. data = (byte*)XMALLOC(ENUM_LEN, heap, DYNAMIC_TYPE_TLSX);
  2419. if (data == NULL)
  2420. return MEMORY_E;
  2421. data[0] = mfl;
  2422. ret = TLSX_Push(extensions, TLSX_MAX_FRAGMENT_LENGTH, data, heap);
  2423. if (ret != 0) {
  2424. XFREE(data, heap, DYNAMIC_TYPE_TLSX);
  2425. return ret;
  2426. }
  2427. return WOLFSSL_SUCCESS;
  2428. }
  2429. #define MFL_FREE_ALL(data, heap) XFREE(data, (heap), DYNAMIC_TYPE_TLSX)
  2430. #define MFL_GET_SIZE(data) ENUM_LEN
  2431. #define MFL_WRITE TLSX_MFL_Write
  2432. #define MFL_PARSE TLSX_MFL_Parse
  2433. #else
  2434. #define MFL_FREE_ALL(a, b)
  2435. #define MFL_GET_SIZE(a) 0
  2436. #define MFL_WRITE(a, b) 0
  2437. #define MFL_PARSE(a, b, c, d) 0
  2438. #endif /* HAVE_MAX_FRAGMENT */
  2439. /******************************************************************************/
  2440. /* Truncated HMAC */
  2441. /******************************************************************************/
  2442. #ifdef HAVE_TRUNCATED_HMAC
  2443. static int TLSX_THM_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2444. byte isRequest)
  2445. {
  2446. if (length != 0 || input == NULL)
  2447. return BUFFER_ERROR;
  2448. if (!isRequest) {
  2449. #ifndef WOLFSSL_OLD_UNSUPPORTED_EXTENSION
  2450. if (TLSX_CheckUnsupportedExtension(ssl, TLSX_TRUNCATED_HMAC))
  2451. return TLSX_HandleUnsupportedExtension(ssl);
  2452. #endif
  2453. }
  2454. else {
  2455. #ifndef NO_WOLFSSL_SERVER
  2456. int ret = TLSX_UseTruncatedHMAC(&ssl->extensions, ssl->heap);
  2457. if (ret != WOLFSSL_SUCCESS)
  2458. return ret; /* throw error */
  2459. TLSX_SetResponse(ssl, TLSX_TRUNCATED_HMAC);
  2460. #endif
  2461. }
  2462. ssl->truncated_hmac = 1;
  2463. return 0;
  2464. }
  2465. int TLSX_UseTruncatedHMAC(TLSX** extensions, void* heap)
  2466. {
  2467. int ret = 0;
  2468. if (extensions == NULL)
  2469. return BAD_FUNC_ARG;
  2470. ret = TLSX_Push(extensions, TLSX_TRUNCATED_HMAC, NULL, heap);
  2471. if (ret != 0)
  2472. return ret;
  2473. return WOLFSSL_SUCCESS;
  2474. }
  2475. #define THM_PARSE TLSX_THM_Parse
  2476. #else
  2477. #define THM_PARSE(a, b, c, d) 0
  2478. #endif /* HAVE_TRUNCATED_HMAC */
  2479. /******************************************************************************/
  2480. /* Certificate Status Request */
  2481. /******************************************************************************/
  2482. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  2483. static void TLSX_CSR_Free(CertificateStatusRequest* csr, void* heap)
  2484. {
  2485. switch (csr->status_type) {
  2486. case WOLFSSL_CSR_OCSP:
  2487. FreeOcspRequest(&csr->request.ocsp);
  2488. break;
  2489. }
  2490. XFREE(csr, heap, DYNAMIC_TYPE_TLSX);
  2491. (void)heap;
  2492. }
  2493. static word16 TLSX_CSR_GetSize(CertificateStatusRequest* csr, byte isRequest)
  2494. {
  2495. word16 size = 0;
  2496. /* shut up compiler warnings */
  2497. (void) csr; (void) isRequest;
  2498. #ifndef NO_WOLFSSL_CLIENT
  2499. if (isRequest) {
  2500. switch (csr->status_type) {
  2501. case WOLFSSL_CSR_OCSP:
  2502. size += ENUM_LEN + 2 * OPAQUE16_LEN;
  2503. if (csr->request.ocsp.nonceSz)
  2504. size += OCSP_NONCE_EXT_SZ;
  2505. break;
  2506. }
  2507. }
  2508. #endif
  2509. #if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER)
  2510. if (!isRequest && csr->ssl->options.tls1_3)
  2511. return OPAQUE8_LEN + OPAQUE24_LEN + csr->response.length;
  2512. #endif
  2513. return size;
  2514. }
  2515. static word16 TLSX_CSR_Write(CertificateStatusRequest* csr, byte* output,
  2516. byte isRequest)
  2517. {
  2518. /* shut up compiler warnings */
  2519. (void) csr; (void) output; (void) isRequest;
  2520. #ifndef NO_WOLFSSL_CLIENT
  2521. if (isRequest) {
  2522. word16 offset = 0;
  2523. word16 length = 0;
  2524. /* type */
  2525. output[offset++] = csr->status_type;
  2526. switch (csr->status_type) {
  2527. case WOLFSSL_CSR_OCSP:
  2528. /* responder id list */
  2529. c16toa(0, output + offset);
  2530. offset += OPAQUE16_LEN;
  2531. /* request extensions */
  2532. if (csr->request.ocsp.nonceSz)
  2533. length = (word16)EncodeOcspRequestExtensions(
  2534. &csr->request.ocsp,
  2535. output + offset + OPAQUE16_LEN,
  2536. OCSP_NONCE_EXT_SZ);
  2537. c16toa(length, output + offset);
  2538. offset += OPAQUE16_LEN + length;
  2539. break;
  2540. }
  2541. return offset;
  2542. }
  2543. #endif
  2544. #if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER)
  2545. if (!isRequest && csr->ssl->options.tls1_3) {
  2546. word16 offset = 0;
  2547. output[offset++] = csr->status_type;
  2548. c32to24(csr->response.length, output + offset);
  2549. offset += OPAQUE24_LEN;
  2550. XMEMCPY(output + offset, csr->response.buffer, csr->response.length);
  2551. offset += csr->response.length;
  2552. return offset;
  2553. }
  2554. #endif
  2555. return 0;
  2556. }
  2557. static int TLSX_CSR_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2558. byte isRequest)
  2559. {
  2560. int ret;
  2561. #if !defined(NO_WOLFSSL_SERVER)
  2562. byte status_type;
  2563. word16 size = 0;
  2564. #if defined(WOLFSSL_TLS13)
  2565. DecodedCert* cert;
  2566. #endif
  2567. #endif
  2568. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER) \
  2569. && defined(WOLFSSL_TLS13)
  2570. OcspRequest* request;
  2571. TLSX* extension;
  2572. CertificateStatusRequest* csr;
  2573. #endif
  2574. #if !defined(NO_WOLFSSL_CLIENT) && defined(WOLFSSL_TLS13) \
  2575. || !defined(NO_WOLFSSL_SERVER)
  2576. word32 offset = 0;
  2577. #endif
  2578. #if !defined(NO_WOLFSSL_CLIENT) && defined(WOLFSSL_TLS13)
  2579. word32 resp_length = 0;
  2580. #endif
  2581. /* shut up compiler warnings */
  2582. (void) ssl; (void) input;
  2583. if (!isRequest) {
  2584. #ifndef NO_WOLFSSL_CLIENT
  2585. extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2586. csr = extension ? (CertificateStatusRequest*)extension->data : NULL;
  2587. if (!csr) {
  2588. /* look at context level */
  2589. extension = TLSX_Find(ssl->ctx->extensions, TLSX_STATUS_REQUEST);
  2590. csr = extension ? (CertificateStatusRequest*)extension->data : NULL;
  2591. if (!csr) /* unexpected extension */
  2592. return TLSX_HandleUnsupportedExtension(ssl);
  2593. /* enable extension at ssl level */
  2594. ret = TLSX_UseCertificateStatusRequest(&ssl->extensions,
  2595. csr->status_type, csr->options, ssl,
  2596. ssl->heap, ssl->devId);
  2597. if (ret != WOLFSSL_SUCCESS)
  2598. return ret;
  2599. switch (csr->status_type) {
  2600. case WOLFSSL_CSR_OCSP:
  2601. /* propagate nonce */
  2602. if (csr->request.ocsp.nonceSz) {
  2603. request =
  2604. (OcspRequest*)TLSX_CSR_GetRequest(ssl->extensions);
  2605. if (request) {
  2606. XMEMCPY(request->nonce, csr->request.ocsp.nonce,
  2607. csr->request.ocsp.nonceSz);
  2608. request->nonceSz = csr->request.ocsp.nonceSz;
  2609. }
  2610. }
  2611. break;
  2612. }
  2613. }
  2614. ssl->status_request = 1;
  2615. #ifdef WOLFSSL_TLS13
  2616. if (ssl->options.tls1_3) {
  2617. /* Get the new extension potentially created above. */
  2618. extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2619. csr = extension ? (CertificateStatusRequest*)extension->data : NULL;
  2620. if (csr == NULL)
  2621. return MEMORY_ERROR;
  2622. ret = 0;
  2623. if (OPAQUE8_LEN + OPAQUE24_LEN > length)
  2624. ret = BUFFER_ERROR;
  2625. if (ret == 0 && input[offset++] != WOLFSSL_CSR_OCSP) {
  2626. ret = BAD_CERTIFICATE_STATUS_ERROR;
  2627. WOLFSSL_ERROR_VERBOSE(ret);
  2628. }
  2629. if (ret == 0) {
  2630. c24to32(input + offset, &resp_length);
  2631. offset += OPAQUE24_LEN;
  2632. if (offset + resp_length != length)
  2633. ret = BUFFER_ERROR;
  2634. }
  2635. if (ret == 0) {
  2636. csr->response.buffer = (byte*)(input + offset);
  2637. csr->response.length = resp_length;
  2638. }
  2639. return ret;
  2640. }
  2641. else
  2642. #endif
  2643. {
  2644. /* extension_data MUST be empty. */
  2645. return length ? BUFFER_ERROR : 0;
  2646. }
  2647. #endif
  2648. }
  2649. else {
  2650. #ifndef NO_WOLFSSL_SERVER
  2651. if (length == 0)
  2652. return 0;
  2653. status_type = input[offset++];
  2654. switch (status_type) {
  2655. case WOLFSSL_CSR_OCSP: {
  2656. /* skip responder_id_list */
  2657. if ((int)(length - offset) < OPAQUE16_LEN)
  2658. return BUFFER_ERROR;
  2659. ato16(input + offset, &size);
  2660. offset += OPAQUE16_LEN + size;
  2661. /* skip request_extensions */
  2662. if ((int)(length - offset) < OPAQUE16_LEN)
  2663. return BUFFER_ERROR;
  2664. ato16(input + offset, &size);
  2665. offset += OPAQUE16_LEN + size;
  2666. if (offset > length)
  2667. return BUFFER_ERROR;
  2668. /* is able to send OCSP response? */
  2669. if (SSL_CM(ssl) == NULL || !SSL_CM(ssl)->ocspStaplingEnabled)
  2670. return 0;
  2671. }
  2672. break;
  2673. /* unknown status type */
  2674. default:
  2675. return 0;
  2676. }
  2677. /* if using status_request and already sending it, skip this one */
  2678. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  2679. if (ssl->status_request_v2)
  2680. return 0;
  2681. #endif
  2682. /* accept the first good status_type and return */
  2683. ret = TLSX_UseCertificateStatusRequest(&ssl->extensions, status_type,
  2684. 0, ssl, ssl->heap, ssl->devId);
  2685. if (ret != WOLFSSL_SUCCESS)
  2686. return ret; /* throw error */
  2687. #if defined(WOLFSSL_TLS13)
  2688. if (ssl->options.tls1_3) {
  2689. if (ssl->buffers.certificate == NULL) {
  2690. WOLFSSL_MSG("Certificate buffer not set!");
  2691. return BUFFER_ERROR;
  2692. }
  2693. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), ssl->heap,
  2694. DYNAMIC_TYPE_DCERT);
  2695. if (cert == NULL) {
  2696. return MEMORY_E;
  2697. }
  2698. InitDecodedCert(cert, ssl->buffers.certificate->buffer,
  2699. ssl->buffers.certificate->length, ssl->heap);
  2700. ret = ParseCert(cert, CERT_TYPE, 1, SSL_CM(ssl));
  2701. if (ret != 0 ) {
  2702. XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
  2703. return ret;
  2704. }
  2705. ret = TLSX_CSR_InitRequest(ssl->extensions, cert, ssl->heap);
  2706. if (ret != 0 ) {
  2707. XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
  2708. return ret;
  2709. }
  2710. XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
  2711. extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2712. csr = extension ?
  2713. (CertificateStatusRequest*)extension->data : NULL;
  2714. if (csr == NULL)
  2715. return MEMORY_ERROR;
  2716. request = &csr->request.ocsp;
  2717. ret = CreateOcspResponse(ssl, &request, &csr->response);
  2718. if (ret != 0)
  2719. return ret;
  2720. if (csr->response.buffer)
  2721. TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST);
  2722. }
  2723. else
  2724. #endif
  2725. TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST);
  2726. ssl->status_request = status_type;
  2727. #endif
  2728. }
  2729. return 0;
  2730. }
  2731. int TLSX_CSR_InitRequest(TLSX* extensions, DecodedCert* cert, void* heap)
  2732. {
  2733. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST);
  2734. CertificateStatusRequest* csr = extension ?
  2735. (CertificateStatusRequest*)extension->data : NULL;
  2736. int ret = 0;
  2737. if (csr) {
  2738. switch (csr->status_type) {
  2739. case WOLFSSL_CSR_OCSP: {
  2740. byte nonce[MAX_OCSP_NONCE_SZ];
  2741. int nonceSz = csr->request.ocsp.nonceSz;
  2742. /* preserve nonce */
  2743. XMEMCPY(nonce, csr->request.ocsp.nonce, nonceSz);
  2744. if ((ret = InitOcspRequest(&csr->request.ocsp, cert, 0, heap))
  2745. != 0)
  2746. return ret;
  2747. /* restore nonce */
  2748. XMEMCPY(csr->request.ocsp.nonce, nonce, nonceSz);
  2749. csr->request.ocsp.nonceSz = nonceSz;
  2750. }
  2751. break;
  2752. }
  2753. }
  2754. return ret;
  2755. }
  2756. void* TLSX_CSR_GetRequest(TLSX* extensions)
  2757. {
  2758. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST);
  2759. CertificateStatusRequest* csr = extension ?
  2760. (CertificateStatusRequest*)extension->data : NULL;
  2761. if (csr) {
  2762. switch (csr->status_type) {
  2763. case WOLFSSL_CSR_OCSP:
  2764. return &csr->request.ocsp;
  2765. }
  2766. }
  2767. return NULL;
  2768. }
  2769. int TLSX_CSR_ForceRequest(WOLFSSL* ssl)
  2770. {
  2771. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2772. CertificateStatusRequest* csr = extension ?
  2773. (CertificateStatusRequest*)extension->data : NULL;
  2774. if (csr) {
  2775. switch (csr->status_type) {
  2776. case WOLFSSL_CSR_OCSP:
  2777. if (SSL_CM(ssl)->ocspEnabled) {
  2778. csr->request.ocsp.ssl = ssl;
  2779. return CheckOcspRequest(SSL_CM(ssl)->ocsp,
  2780. &csr->request.ocsp, NULL);
  2781. }
  2782. else {
  2783. WOLFSSL_ERROR_VERBOSE(OCSP_LOOKUP_FAIL);
  2784. return OCSP_LOOKUP_FAIL;
  2785. }
  2786. }
  2787. }
  2788. return 0;
  2789. }
  2790. int TLSX_UseCertificateStatusRequest(TLSX** extensions, byte status_type,
  2791. byte options, WOLFSSL* ssl, void* heap,
  2792. int devId)
  2793. {
  2794. CertificateStatusRequest* csr = NULL;
  2795. int ret = 0;
  2796. if (!extensions || status_type != WOLFSSL_CSR_OCSP)
  2797. return BAD_FUNC_ARG;
  2798. csr = (CertificateStatusRequest*)
  2799. XMALLOC(sizeof(CertificateStatusRequest), heap, DYNAMIC_TYPE_TLSX);
  2800. if (!csr)
  2801. return MEMORY_E;
  2802. ForceZero(csr, sizeof(CertificateStatusRequest));
  2803. csr->status_type = status_type;
  2804. csr->options = options;
  2805. csr->ssl = ssl;
  2806. switch (csr->status_type) {
  2807. case WOLFSSL_CSR_OCSP:
  2808. if (options & WOLFSSL_CSR_OCSP_USE_NONCE) {
  2809. WC_RNG rng;
  2810. #ifndef HAVE_FIPS
  2811. ret = wc_InitRng_ex(&rng, heap, devId);
  2812. #else
  2813. ret = wc_InitRng(&rng);
  2814. (void)devId;
  2815. #endif
  2816. if (ret == 0) {
  2817. if (wc_RNG_GenerateBlock(&rng, csr->request.ocsp.nonce,
  2818. MAX_OCSP_NONCE_SZ) == 0)
  2819. csr->request.ocsp.nonceSz = MAX_OCSP_NONCE_SZ;
  2820. wc_FreeRng(&rng);
  2821. }
  2822. }
  2823. break;
  2824. }
  2825. if ((ret = TLSX_Push(extensions, TLSX_STATUS_REQUEST, csr, heap)) != 0) {
  2826. XFREE(csr, heap, DYNAMIC_TYPE_TLSX);
  2827. return ret;
  2828. }
  2829. return WOLFSSL_SUCCESS;
  2830. }
  2831. #define CSR_FREE_ALL TLSX_CSR_Free
  2832. #define CSR_GET_SIZE TLSX_CSR_GetSize
  2833. #define CSR_WRITE TLSX_CSR_Write
  2834. #define CSR_PARSE TLSX_CSR_Parse
  2835. #else
  2836. #define CSR_FREE_ALL(data, heap)
  2837. #define CSR_GET_SIZE(a, b) 0
  2838. #define CSR_WRITE(a, b, c) 0
  2839. #define CSR_PARSE(a, b, c, d) 0
  2840. #endif /* HAVE_CERTIFICATE_STATUS_REQUEST */
  2841. /******************************************************************************/
  2842. /* Certificate Status Request v2 */
  2843. /******************************************************************************/
  2844. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  2845. static void TLSX_CSR2_FreeAll(CertificateStatusRequestItemV2* csr2, void* heap)
  2846. {
  2847. CertificateStatusRequestItemV2* next;
  2848. for (; csr2; csr2 = next) {
  2849. next = csr2->next;
  2850. switch (csr2->status_type) {
  2851. case WOLFSSL_CSR2_OCSP:
  2852. case WOLFSSL_CSR2_OCSP_MULTI:
  2853. while(csr2->requests--)
  2854. FreeOcspRequest(&csr2->request.ocsp[csr2->requests]);
  2855. break;
  2856. }
  2857. XFREE(csr2, heap, DYNAMIC_TYPE_TLSX);
  2858. }
  2859. (void)heap;
  2860. }
  2861. static word16 TLSX_CSR2_GetSize(CertificateStatusRequestItemV2* csr2,
  2862. byte isRequest)
  2863. {
  2864. word16 size = 0;
  2865. /* shut up compiler warnings */
  2866. (void) csr2; (void) isRequest;
  2867. #ifndef NO_WOLFSSL_CLIENT
  2868. if (isRequest) {
  2869. CertificateStatusRequestItemV2* next;
  2870. for (size = OPAQUE16_LEN; csr2; csr2 = next) {
  2871. next = csr2->next;
  2872. switch (csr2->status_type) {
  2873. case WOLFSSL_CSR2_OCSP:
  2874. case WOLFSSL_CSR2_OCSP_MULTI:
  2875. size += ENUM_LEN + 3 * OPAQUE16_LEN;
  2876. if (csr2->request.ocsp[0].nonceSz)
  2877. size += OCSP_NONCE_EXT_SZ;
  2878. break;
  2879. }
  2880. }
  2881. }
  2882. #endif
  2883. return size;
  2884. }
  2885. static word16 TLSX_CSR2_Write(CertificateStatusRequestItemV2* csr2,
  2886. byte* output, byte isRequest)
  2887. {
  2888. /* shut up compiler warnings */
  2889. (void) csr2; (void) output; (void) isRequest;
  2890. #ifndef NO_WOLFSSL_CLIENT
  2891. if (isRequest) {
  2892. word16 offset;
  2893. word16 length;
  2894. for (offset = OPAQUE16_LEN; csr2 != NULL; csr2 = csr2->next) {
  2895. /* status_type */
  2896. output[offset++] = csr2->status_type;
  2897. /* request */
  2898. switch (csr2->status_type) {
  2899. case WOLFSSL_CSR2_OCSP:
  2900. case WOLFSSL_CSR2_OCSP_MULTI:
  2901. /* request_length */
  2902. length = 2 * OPAQUE16_LEN;
  2903. if (csr2->request.ocsp[0].nonceSz)
  2904. length += OCSP_NONCE_EXT_SZ;
  2905. c16toa(length, output + offset);
  2906. offset += OPAQUE16_LEN;
  2907. /* responder id list */
  2908. c16toa(0, output + offset);
  2909. offset += OPAQUE16_LEN;
  2910. /* request extensions */
  2911. length = 0;
  2912. if (csr2->request.ocsp[0].nonceSz)
  2913. length = (word16)EncodeOcspRequestExtensions(
  2914. &csr2->request.ocsp[0],
  2915. output + offset + OPAQUE16_LEN,
  2916. OCSP_NONCE_EXT_SZ);
  2917. c16toa(length, output + offset);
  2918. offset += OPAQUE16_LEN + length;
  2919. break;
  2920. }
  2921. }
  2922. /* list size */
  2923. c16toa(offset - OPAQUE16_LEN, output);
  2924. return offset;
  2925. }
  2926. #endif
  2927. return 0;
  2928. }
  2929. static int TLSX_CSR2_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2930. byte isRequest)
  2931. {
  2932. int ret;
  2933. /* shut up compiler warnings */
  2934. (void) ssl; (void) input;
  2935. if (!isRequest) {
  2936. #ifndef NO_WOLFSSL_CLIENT
  2937. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST_V2);
  2938. CertificateStatusRequestItemV2* csr2 = extension ?
  2939. (CertificateStatusRequestItemV2*)extension->data : NULL;
  2940. if (!csr2) {
  2941. /* look at context level */
  2942. extension = TLSX_Find(ssl->ctx->extensions, TLSX_STATUS_REQUEST_V2);
  2943. csr2 = extension ?
  2944. (CertificateStatusRequestItemV2*)extension->data : NULL;
  2945. if (!csr2) /* unexpected extension */
  2946. return TLSX_HandleUnsupportedExtension(ssl);
  2947. /* enable extension at ssl level */
  2948. for (; csr2; csr2 = csr2->next) {
  2949. ret = TLSX_UseCertificateStatusRequestV2(&ssl->extensions,
  2950. csr2->status_type, csr2->options, ssl->heap,
  2951. ssl->devId);
  2952. if (ret != WOLFSSL_SUCCESS)
  2953. return ret;
  2954. switch (csr2->status_type) {
  2955. case WOLFSSL_CSR2_OCSP:
  2956. /* followed by */
  2957. case WOLFSSL_CSR2_OCSP_MULTI:
  2958. /* propagate nonce */
  2959. if (csr2->request.ocsp[0].nonceSz) {
  2960. OcspRequest* request =
  2961. (OcspRequest*)TLSX_CSR2_GetRequest(ssl->extensions,
  2962. csr2->status_type, 0);
  2963. if (request) {
  2964. XMEMCPY(request->nonce,
  2965. csr2->request.ocsp[0].nonce,
  2966. csr2->request.ocsp[0].nonceSz);
  2967. request->nonceSz =
  2968. csr2->request.ocsp[0].nonceSz;
  2969. }
  2970. }
  2971. break;
  2972. }
  2973. }
  2974. }
  2975. ssl->status_request_v2 = 1;
  2976. return length ? BUFFER_ERROR : 0; /* extension_data MUST be empty. */
  2977. #endif
  2978. }
  2979. else {
  2980. #ifndef NO_WOLFSSL_SERVER
  2981. byte status_type;
  2982. word16 request_length;
  2983. word16 offset = 0;
  2984. word16 size = 0;
  2985. /* list size */
  2986. if (offset + OPAQUE16_LEN >= length) {
  2987. return BUFFER_E;
  2988. }
  2989. ato16(input + offset, &request_length);
  2990. offset += OPAQUE16_LEN;
  2991. if (length - OPAQUE16_LEN != request_length)
  2992. return BUFFER_ERROR;
  2993. while (length > offset) {
  2994. if ((int)(length - offset) < ENUM_LEN + OPAQUE16_LEN)
  2995. return BUFFER_ERROR;
  2996. status_type = input[offset++];
  2997. ato16(input + offset, &request_length);
  2998. offset += OPAQUE16_LEN;
  2999. if (length - offset < request_length)
  3000. return BUFFER_ERROR;
  3001. switch (status_type) {
  3002. case WOLFSSL_CSR2_OCSP:
  3003. case WOLFSSL_CSR2_OCSP_MULTI:
  3004. /* skip responder_id_list */
  3005. if ((int)(length - offset) < OPAQUE16_LEN)
  3006. return BUFFER_ERROR;
  3007. ato16(input + offset, &size);
  3008. if (length - offset < size)
  3009. return BUFFER_ERROR;
  3010. offset += OPAQUE16_LEN + size;
  3011. /* skip request_extensions */
  3012. if ((int)(length - offset) < OPAQUE16_LEN)
  3013. return BUFFER_ERROR;
  3014. ato16(input + offset, &size);
  3015. if (length - offset < size)
  3016. return BUFFER_ERROR;
  3017. offset += OPAQUE16_LEN + size;
  3018. if (offset > length)
  3019. return BUFFER_ERROR;
  3020. /* is able to send OCSP response? */
  3021. if (SSL_CM(ssl) == NULL
  3022. || !SSL_CM(ssl)->ocspStaplingEnabled)
  3023. continue;
  3024. break;
  3025. default:
  3026. /* unknown status type, skipping! */
  3027. offset += request_length;
  3028. continue;
  3029. }
  3030. /* if using status_request and already sending it, remove it
  3031. * and prefer to use the v2 version */
  3032. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  3033. if (ssl->status_request) {
  3034. ssl->status_request = 0;
  3035. TLSX_Remove(&ssl->extensions, TLSX_STATUS_REQUEST, ssl->heap);
  3036. }
  3037. #endif
  3038. /* TLS 1.3 servers MUST NOT act upon presence or information in
  3039. * this extension (RFC 8448 Section 4.4.2.1).
  3040. */
  3041. if (!IsAtLeastTLSv1_3(ssl->version)) {
  3042. /* accept the first good status_type and return */
  3043. ret = TLSX_UseCertificateStatusRequestV2(&ssl->extensions,
  3044. status_type, 0, ssl->heap, ssl->devId);
  3045. if (ret != WOLFSSL_SUCCESS)
  3046. return ret; /* throw error */
  3047. TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST_V2);
  3048. ssl->status_request_v2 = status_type;
  3049. }
  3050. return 0;
  3051. }
  3052. #endif
  3053. }
  3054. return 0;
  3055. }
  3056. int TLSX_CSR2_InitRequests(TLSX* extensions, DecodedCert* cert, byte isPeer,
  3057. void* heap)
  3058. {
  3059. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST_V2);
  3060. CertificateStatusRequestItemV2* csr2 = extension ?
  3061. (CertificateStatusRequestItemV2*)extension->data : NULL;
  3062. int ret = 0;
  3063. for (; csr2; csr2 = csr2->next) {
  3064. switch (csr2->status_type) {
  3065. case WOLFSSL_CSR2_OCSP:
  3066. if (!isPeer || csr2->requests != 0)
  3067. break;
  3068. FALL_THROUGH; /* followed by */
  3069. case WOLFSSL_CSR2_OCSP_MULTI: {
  3070. if (csr2->requests < 1 + MAX_CHAIN_DEPTH) {
  3071. byte nonce[MAX_OCSP_NONCE_SZ];
  3072. int nonceSz = csr2->request.ocsp[0].nonceSz;
  3073. /* preserve nonce, replicating nonce of ocsp[0] */
  3074. XMEMCPY(nonce, csr2->request.ocsp[0].nonce, nonceSz);
  3075. if ((ret = InitOcspRequest(
  3076. &csr2->request.ocsp[csr2->requests], cert,
  3077. 0, heap)) != 0)
  3078. return ret;
  3079. /* restore nonce */
  3080. XMEMCPY(csr2->request.ocsp[csr2->requests].nonce,
  3081. nonce, nonceSz);
  3082. csr2->request.ocsp[csr2->requests].nonceSz = nonceSz;
  3083. csr2->requests++;
  3084. }
  3085. }
  3086. break;
  3087. }
  3088. }
  3089. (void)cert;
  3090. return ret;
  3091. }
  3092. void* TLSX_CSR2_GetRequest(TLSX* extensions, byte status_type, byte idx)
  3093. {
  3094. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST_V2);
  3095. CertificateStatusRequestItemV2* csr2 = extension ?
  3096. (CertificateStatusRequestItemV2*)extension->data : NULL;
  3097. for (; csr2; csr2 = csr2->next) {
  3098. if (csr2->status_type == status_type) {
  3099. switch (csr2->status_type) {
  3100. case WOLFSSL_CSR2_OCSP:
  3101. /* followed by */
  3102. case WOLFSSL_CSR2_OCSP_MULTI:
  3103. /* requests are initialized in the reverse order */
  3104. return idx < csr2->requests
  3105. ? &csr2->request.ocsp[csr2->requests - idx - 1]
  3106. : NULL;
  3107. }
  3108. }
  3109. }
  3110. return NULL;
  3111. }
  3112. int TLSX_CSR2_ForceRequest(WOLFSSL* ssl)
  3113. {
  3114. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST_V2);
  3115. CertificateStatusRequestItemV2* csr2 = extension ?
  3116. (CertificateStatusRequestItemV2*)extension->data : NULL;
  3117. /* forces only the first one */
  3118. if (csr2) {
  3119. switch (csr2->status_type) {
  3120. case WOLFSSL_CSR2_OCSP:
  3121. /* followed by */
  3122. case WOLFSSL_CSR2_OCSP_MULTI:
  3123. if (SSL_CM(ssl)->ocspEnabled) {
  3124. csr2->request.ocsp[0].ssl = ssl;
  3125. return CheckOcspRequest(SSL_CM(ssl)->ocsp,
  3126. &csr2->request.ocsp[0], NULL);
  3127. }
  3128. else {
  3129. WOLFSSL_ERROR_VERBOSE(OCSP_LOOKUP_FAIL);
  3130. return OCSP_LOOKUP_FAIL;
  3131. }
  3132. }
  3133. }
  3134. return 0;
  3135. }
  3136. int TLSX_UseCertificateStatusRequestV2(TLSX** extensions, byte status_type,
  3137. byte options, void* heap, int devId)
  3138. {
  3139. TLSX* extension = NULL;
  3140. CertificateStatusRequestItemV2* csr2 = NULL;
  3141. int ret = 0;
  3142. if (!extensions)
  3143. return BAD_FUNC_ARG;
  3144. if (status_type != WOLFSSL_CSR2_OCSP
  3145. && status_type != WOLFSSL_CSR2_OCSP_MULTI)
  3146. return BAD_FUNC_ARG;
  3147. csr2 = (CertificateStatusRequestItemV2*)
  3148. XMALLOC(sizeof(CertificateStatusRequestItemV2), heap, DYNAMIC_TYPE_TLSX);
  3149. if (!csr2)
  3150. return MEMORY_E;
  3151. ForceZero(csr2, sizeof(CertificateStatusRequestItemV2));
  3152. csr2->status_type = status_type;
  3153. csr2->options = options;
  3154. csr2->next = NULL;
  3155. switch (csr2->status_type) {
  3156. case WOLFSSL_CSR2_OCSP:
  3157. case WOLFSSL_CSR2_OCSP_MULTI:
  3158. if (options & WOLFSSL_CSR2_OCSP_USE_NONCE) {
  3159. WC_RNG rng;
  3160. #ifndef HAVE_FIPS
  3161. ret = wc_InitRng_ex(&rng, heap, devId);
  3162. #else
  3163. ret = wc_InitRng(&rng);
  3164. (void)devId;
  3165. #endif
  3166. if (ret == 0) {
  3167. if (wc_RNG_GenerateBlock(&rng, csr2->request.ocsp[0].nonce,
  3168. MAX_OCSP_NONCE_SZ) == 0)
  3169. csr2->request.ocsp[0].nonceSz = MAX_OCSP_NONCE_SZ;
  3170. wc_FreeRng(&rng);
  3171. }
  3172. }
  3173. break;
  3174. }
  3175. /* append new item */
  3176. if ((extension = TLSX_Find(*extensions, TLSX_STATUS_REQUEST_V2))) {
  3177. CertificateStatusRequestItemV2* last =
  3178. (CertificateStatusRequestItemV2*)extension->data;
  3179. for (; last->next; last = last->next);
  3180. last->next = csr2;
  3181. }
  3182. else if ((ret = TLSX_Push(extensions, TLSX_STATUS_REQUEST_V2, csr2,heap))) {
  3183. XFREE(csr2, heap, DYNAMIC_TYPE_TLSX);
  3184. return ret;
  3185. }
  3186. return WOLFSSL_SUCCESS;
  3187. }
  3188. #define CSR2_FREE_ALL TLSX_CSR2_FreeAll
  3189. #define CSR2_GET_SIZE TLSX_CSR2_GetSize
  3190. #define CSR2_WRITE TLSX_CSR2_Write
  3191. #define CSR2_PARSE TLSX_CSR2_Parse
  3192. #else
  3193. #define CSR2_FREE_ALL(data, heap)
  3194. #define CSR2_GET_SIZE(a, b) 0
  3195. #define CSR2_WRITE(a, b, c) 0
  3196. #define CSR2_PARSE(a, b, c, d) 0
  3197. #endif /* HAVE_CERTIFICATE_STATUS_REQUEST_V2 */
  3198. /******************************************************************************/
  3199. /* Supported Elliptic Curves */
  3200. /******************************************************************************/
  3201. #ifdef HAVE_SUPPORTED_CURVES
  3202. #if !defined(HAVE_ECC) && !defined(HAVE_CURVE25519) && !defined(HAVE_CURVE448) \
  3203. && !defined(HAVE_FFDHE) && !defined(HAVE_PQC)
  3204. #error Elliptic Curves Extension requires Elliptic Curve Cryptography or liboqs groups. \
  3205. Use --enable-ecc and/or --enable-liboqs in the configure script or \
  3206. define HAVE_ECC. Alternatively use FFDHE for DH cipher suites.
  3207. #endif
  3208. static int TLSX_SupportedCurve_New(SupportedCurve** curve, word16 name,
  3209. void* heap)
  3210. {
  3211. if (curve == NULL)
  3212. return BAD_FUNC_ARG;
  3213. (void)heap;
  3214. *curve = (SupportedCurve*)XMALLOC(sizeof(SupportedCurve), heap,
  3215. DYNAMIC_TYPE_TLSX);
  3216. if (*curve == NULL)
  3217. return MEMORY_E;
  3218. (*curve)->name = name;
  3219. (*curve)->next = NULL;
  3220. return 0;
  3221. }
  3222. static int TLSX_PointFormat_New(PointFormat** point, byte format, void* heap)
  3223. {
  3224. if (point == NULL)
  3225. return BAD_FUNC_ARG;
  3226. (void)heap;
  3227. *point = (PointFormat*)XMALLOC(sizeof(PointFormat), heap,
  3228. DYNAMIC_TYPE_TLSX);
  3229. if (*point == NULL)
  3230. return MEMORY_E;
  3231. (*point)->format = format;
  3232. (*point)->next = NULL;
  3233. return 0;
  3234. }
  3235. static void TLSX_SupportedCurve_FreeAll(SupportedCurve* list, void* heap)
  3236. {
  3237. SupportedCurve* curve;
  3238. while ((curve = list)) {
  3239. list = curve->next;
  3240. XFREE(curve, heap, DYNAMIC_TYPE_TLSX);
  3241. }
  3242. (void)heap;
  3243. }
  3244. static void TLSX_PointFormat_FreeAll(PointFormat* list, void* heap)
  3245. {
  3246. PointFormat* point;
  3247. while ((point = list)) {
  3248. list = point->next;
  3249. XFREE(point, heap, DYNAMIC_TYPE_TLSX);
  3250. }
  3251. (void)heap;
  3252. }
  3253. static int TLSX_SupportedCurve_Append(SupportedCurve* list, word16 name,
  3254. void* heap)
  3255. {
  3256. int ret = BAD_FUNC_ARG;
  3257. while (list) {
  3258. if (list->name == name) {
  3259. ret = 0; /* curve already in use */
  3260. break;
  3261. }
  3262. if (list->next == NULL) {
  3263. ret = TLSX_SupportedCurve_New(&list->next, name, heap);
  3264. break;
  3265. }
  3266. list = list->next;
  3267. }
  3268. return ret;
  3269. }
  3270. static int TLSX_PointFormat_Append(PointFormat* list, byte format, void* heap)
  3271. {
  3272. int ret = BAD_FUNC_ARG;
  3273. while (list) {
  3274. if (list->format == format) {
  3275. ret = 0; /* format already in use */
  3276. break;
  3277. }
  3278. if (list->next == NULL) {
  3279. ret = TLSX_PointFormat_New(&list->next, format, heap);
  3280. break;
  3281. }
  3282. list = list->next;
  3283. }
  3284. return ret;
  3285. }
  3286. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
  3287. #if defined(HAVE_FFDHE) && (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3288. defined(HAVE_CURVE448))
  3289. static void TLSX_SupportedCurve_ValidateRequest(const WOLFSSL* ssl,
  3290. const byte* semaphore)
  3291. {
  3292. /* If all pre-defined parameter types for key exchange are supported then
  3293. * always send SupportedGroups extension.
  3294. */
  3295. (void)ssl;
  3296. (void)semaphore;
  3297. }
  3298. #else
  3299. static void TLSX_SupportedCurve_ValidateRequest(WOLFSSL* ssl, byte* semaphore)
  3300. {
  3301. word16 i;
  3302. const Suites* suites = WOLFSSL_SUITES(ssl);
  3303. for (i = 0; i < suites->suiteSz; i += 2) {
  3304. if (suites->suites[i] == TLS13_BYTE)
  3305. return;
  3306. #ifdef BUILD_TLS_SM4_GCM_SM3
  3307. if ((suites->suites[i] == CIPHER_BYTE) &&
  3308. (suites->suites[i+1] == TLS_SM4_GCM_SM3))
  3309. return;
  3310. #endif
  3311. #ifdef BUILD_TLS_SM4_CCM_SM3
  3312. if ((suites->suites[i] == CIPHER_BYTE) &&
  3313. (suites->suites[i+1] == TLS_SM4_CCM_SM3))
  3314. return;
  3315. #endif
  3316. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_SM4_CBC_SM3
  3317. if ((suites->suites[i] == SM_BYTE) &&
  3318. (suites->suites[i+1] == TLS_ECDHE_ECDSA_WITH_SM4_CBC_SM3))
  3319. return;
  3320. #endif
  3321. if ((suites->suites[i] == ECC_BYTE) ||
  3322. (suites->suites[i] == ECDHE_PSK_BYTE) ||
  3323. (suites->suites[i] == CHACHA_BYTE)) {
  3324. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3325. defined(HAVE_CURVE448)
  3326. return;
  3327. #endif
  3328. }
  3329. #ifdef HAVE_FFDHE
  3330. else {
  3331. return;
  3332. }
  3333. #endif
  3334. }
  3335. /* turns semaphore on to avoid sending this extension. */
  3336. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_GROUPS));
  3337. }
  3338. #endif
  3339. /* Only send PointFormats if TLSv13, ECC or CHACHA cipher suite present.
  3340. */
  3341. static void TLSX_PointFormat_ValidateRequest(WOLFSSL* ssl, byte* semaphore)
  3342. {
  3343. #ifdef HAVE_FFDHE
  3344. (void)ssl;
  3345. (void)semaphore;
  3346. #else
  3347. word16 i;
  3348. const Suites* suites = WOLFSSL_SUITES(ssl);
  3349. if (suites == NULL)
  3350. return;
  3351. for (i = 0; i < suites->suiteSz; i += 2) {
  3352. if (suites->suites[i] == TLS13_BYTE)
  3353. return;
  3354. #ifdef BUILD_TLS_SM4_GCM_SM3
  3355. if ((suites->suites[i] == CIPHER_BYTE) &&
  3356. (suites->suites[i+1] == TLS_SM4_GCM_SM3))
  3357. return;
  3358. #endif
  3359. #ifdef BUILD_TLS_SM4_CCM_SM3
  3360. if ((suites->suites[i] == CIPHER_BYTE) &&
  3361. (suites->suites[i+1] == TLS_SM4_CCM_SM3))
  3362. return;
  3363. #endif
  3364. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_SM4_CBC_SM3
  3365. if ((suites->suites[i] == SM_BYTE) &&
  3366. (suites->suites[i+1] == TLS_ECDHE_ECDSA_WITH_SM4_CBC_SM3))
  3367. return;
  3368. #endif
  3369. if ((suites->suites[i] == ECC_BYTE) ||
  3370. (suites->suites[i] == ECDHE_PSK_BYTE) ||
  3371. (suites->suites[i] == CHACHA_BYTE)) {
  3372. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3373. defined(HAVE_CURVE448)
  3374. return;
  3375. #endif
  3376. }
  3377. }
  3378. /* turns semaphore on to avoid sending this extension. */
  3379. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  3380. #endif
  3381. }
  3382. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
  3383. #ifndef NO_WOLFSSL_SERVER
  3384. static void TLSX_PointFormat_ValidateResponse(WOLFSSL* ssl, byte* semaphore)
  3385. {
  3386. #if defined(HAVE_FFDHE) || defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3387. defined(HAVE_CURVE448)
  3388. (void)semaphore;
  3389. #endif
  3390. if (ssl->options.cipherSuite0 == TLS13_BYTE)
  3391. return;
  3392. #ifdef BUILD_TLS_SM4_GCM_SM3
  3393. if ((ssl->options.cipherSuite0 == CIPHER_BYTE) &&
  3394. (ssl->options.cipherSuite == TLS_SM4_GCM_SM3))
  3395. return;
  3396. #endif
  3397. #ifdef BUILD_TLS_SM4_CCM_SM3
  3398. if ((ssl->options.cipherSuite0 == CIPHER_BYTE) &&
  3399. (ssl->options.cipherSuite == TLS_SM4_CCM_SM3))
  3400. return;
  3401. #endif
  3402. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_SM4_CBC_SM3
  3403. if ((ssl->options.cipherSuite0 == SM_BYTE) &&
  3404. (ssl->options.cipherSuite == TLS_ECDHE_ECDSA_WITH_SM4_CBC_SM3))
  3405. return;
  3406. #endif
  3407. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  3408. if (ssl->options.cipherSuite0 == ECC_BYTE ||
  3409. ssl->options.cipherSuite0 == ECDHE_PSK_BYTE ||
  3410. ssl->options.cipherSuite0 == CHACHA_BYTE) {
  3411. return;
  3412. }
  3413. #endif
  3414. /* turns semaphore on to avoid sending this extension. */
  3415. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  3416. }
  3417. #endif /* !NO_WOLFSSL_SERVER */
  3418. #ifndef NO_WOLFSSL_CLIENT
  3419. static word16 TLSX_SupportedCurve_GetSize(SupportedCurve* list)
  3420. {
  3421. SupportedCurve* curve;
  3422. word16 length = OPAQUE16_LEN; /* list length */
  3423. while ((curve = list)) {
  3424. list = curve->next;
  3425. length += OPAQUE16_LEN; /* curve length */
  3426. }
  3427. return length;
  3428. }
  3429. #endif
  3430. static word16 TLSX_PointFormat_GetSize(PointFormat* list)
  3431. {
  3432. PointFormat* point;
  3433. word16 length = ENUM_LEN; /* list length */
  3434. while ((point = list)) {
  3435. list = point->next;
  3436. length += ENUM_LEN; /* format length */
  3437. }
  3438. return length;
  3439. }
  3440. #ifndef NO_WOLFSSL_CLIENT
  3441. static word16 TLSX_SupportedCurve_Write(SupportedCurve* list, byte* output)
  3442. {
  3443. word16 offset = OPAQUE16_LEN;
  3444. while (list) {
  3445. c16toa(list->name, output + offset);
  3446. offset += OPAQUE16_LEN;
  3447. list = list->next;
  3448. }
  3449. c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
  3450. return offset;
  3451. }
  3452. #endif
  3453. static word16 TLSX_PointFormat_Write(PointFormat* list, byte* output)
  3454. {
  3455. word16 offset = ENUM_LEN;
  3456. while (list) {
  3457. output[offset++] = list->format;
  3458. list = list->next;
  3459. }
  3460. output[0] = (byte)(offset - ENUM_LEN);
  3461. return offset;
  3462. }
  3463. #if !defined(NO_WOLFSSL_SERVER) || (defined(WOLFSSL_TLS13) && \
  3464. !defined(WOLFSSL_NO_SERVER_GROUPS_EXT))
  3465. int TLSX_SupportedCurve_Parse(const WOLFSSL* ssl, const byte* input,
  3466. word16 length, byte isRequest, TLSX** extensions)
  3467. {
  3468. word16 offset;
  3469. word16 name;
  3470. int ret;
  3471. if(!isRequest && !IsAtLeastTLSv1_3(ssl->version)) {
  3472. #ifdef WOLFSSL_ALLOW_SERVER_SC_EXT
  3473. return 0;
  3474. #else
  3475. return BUFFER_ERROR; /* servers doesn't send this extension. */
  3476. #endif
  3477. }
  3478. if (OPAQUE16_LEN > length || length % OPAQUE16_LEN)
  3479. return BUFFER_ERROR;
  3480. ato16(input, &offset);
  3481. /* validating curve list length */
  3482. if (length != OPAQUE16_LEN + offset)
  3483. return BUFFER_ERROR;
  3484. offset = OPAQUE16_LEN;
  3485. if (offset == length)
  3486. return 0;
  3487. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
  3488. if (!isRequest) {
  3489. TLSX* extension;
  3490. SupportedCurve* curve;
  3491. extension = TLSX_Find(*extensions, TLSX_SUPPORTED_GROUPS);
  3492. if (extension != NULL) {
  3493. /* Replace client list with server list of supported groups. */
  3494. curve = (SupportedCurve*)extension->data;
  3495. extension->data = NULL;
  3496. TLSX_SupportedCurve_FreeAll(curve, ssl->heap);
  3497. ato16(input + offset, &name);
  3498. offset += OPAQUE16_LEN;
  3499. ret = TLSX_SupportedCurve_New(&curve, name, ssl->heap);
  3500. if (ret != 0)
  3501. return ret; /* throw error */
  3502. extension->data = (void*)curve;
  3503. }
  3504. }
  3505. #endif
  3506. for (; offset < length; offset += OPAQUE16_LEN) {
  3507. ato16(input + offset, &name);
  3508. ret = TLSX_UseSupportedCurve(extensions, name, ssl->heap);
  3509. /* If it is BAD_FUNC_ARG then it is a group we do not support, but
  3510. * that is fine. */
  3511. if (ret != WOLFSSL_SUCCESS && ret != BAD_FUNC_ARG) {
  3512. return ret;
  3513. }
  3514. }
  3515. return 0;
  3516. }
  3517. #endif
  3518. #if !defined(NO_WOLFSSL_SERVER)
  3519. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
  3520. /* Checks the priority of the groups on the server and set the supported groups
  3521. * response if there is a group not advertised by the client that is preferred.
  3522. *
  3523. * ssl SSL/TLS object.
  3524. * returns 0 on success, otherwise an error.
  3525. */
  3526. int TLSX_SupportedCurve_CheckPriority(WOLFSSL* ssl)
  3527. {
  3528. int ret;
  3529. TLSX* extension;
  3530. TLSX* priority = NULL;
  3531. TLSX* ext = NULL;
  3532. word16 name;
  3533. SupportedCurve* curve;
  3534. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3535. /* May be doing PSK with no key exchange. */
  3536. if (extension == NULL)
  3537. return 0;
  3538. ret = TLSX_PopulateSupportedGroups(ssl, &priority);
  3539. if (ret != WOLFSSL_SUCCESS) {
  3540. TLSX_FreeAll(priority, ssl->heap);
  3541. return ret;
  3542. }
  3543. ext = TLSX_Find(priority, TLSX_SUPPORTED_GROUPS);
  3544. if (ext == NULL) {
  3545. WOLFSSL_MSG("Could not find supported groups extension");
  3546. TLSX_FreeAll(priority, ssl->heap);
  3547. return 0;
  3548. }
  3549. curve = (SupportedCurve*)ext->data;
  3550. name = curve->name;
  3551. curve = (SupportedCurve*)extension->data;
  3552. while (curve != NULL) {
  3553. if (curve->name == name)
  3554. break;
  3555. curve = curve->next;
  3556. }
  3557. if (curve == NULL) {
  3558. /* Couldn't find the preferred group in client list. */
  3559. extension->resp = 1;
  3560. /* Send server list back and free client list. */
  3561. curve = (SupportedCurve*)extension->data;
  3562. extension->data = ext->data;
  3563. ext->data = curve;
  3564. }
  3565. TLSX_FreeAll(priority, ssl->heap);
  3566. return 0;
  3567. }
  3568. #endif /* WOLFSSL_TLS13 && !WOLFSSL_NO_SERVER_GROUPS_EXT */
  3569. #if defined(HAVE_FFDHE) && !defined(WOLFSSL_NO_TLS12)
  3570. #ifdef HAVE_PUBLIC_FFDHE
  3571. static int tlsx_ffdhe_find_group(WOLFSSL* ssl, SupportedCurve* clientGroup,
  3572. SupportedCurve* serverGroup)
  3573. {
  3574. int ret = 0;
  3575. SupportedCurve* group;
  3576. const DhParams* params = NULL;
  3577. for (; serverGroup != NULL; serverGroup = serverGroup->next) {
  3578. if (!WOLFSSL_NAMED_GROUP_IS_FFHDE(serverGroup->name))
  3579. continue;
  3580. for (group = clientGroup; group != NULL; group = group->next) {
  3581. if (serverGroup->name != group->name)
  3582. continue;
  3583. switch (serverGroup->name) {
  3584. #ifdef HAVE_FFDHE_2048
  3585. case WOLFSSL_FFDHE_2048:
  3586. params = wc_Dh_ffdhe2048_Get();
  3587. break;
  3588. #endif
  3589. #ifdef HAVE_FFDHE_3072
  3590. case WOLFSSL_FFDHE_3072:
  3591. params = wc_Dh_ffdhe3072_Get();
  3592. break;
  3593. #endif
  3594. #ifdef HAVE_FFDHE_4096
  3595. case WOLFSSL_FFDHE_4096:
  3596. params = wc_Dh_ffdhe4096_Get();
  3597. break;
  3598. #endif
  3599. #ifdef HAVE_FFDHE_6144
  3600. case WOLFSSL_FFDHE_6144:
  3601. params = wc_Dh_ffdhe6144_Get();
  3602. break;
  3603. #endif
  3604. #ifdef HAVE_FFDHE_8192
  3605. case WOLFSSL_FFDHE_8192:
  3606. params = wc_Dh_ffdhe8192_Get();
  3607. break;
  3608. #endif
  3609. default:
  3610. break;
  3611. }
  3612. if (params == NULL) {
  3613. ret = BAD_FUNC_ARG;
  3614. break;
  3615. }
  3616. if (params->p_len >= ssl->options.minDhKeySz &&
  3617. params->p_len <= ssl->options.maxDhKeySz) {
  3618. break;
  3619. }
  3620. }
  3621. if (ret != 0)
  3622. break;
  3623. if ((group != NULL) && (serverGroup->name == group->name))
  3624. break;
  3625. }
  3626. if ((ret == 0) && (serverGroup != NULL) && (params != NULL)) {
  3627. ssl->buffers.serverDH_P.buffer = (unsigned char *)params->p;
  3628. ssl->buffers.serverDH_P.length = params->p_len;
  3629. ssl->buffers.serverDH_G.buffer = (unsigned char *)params->g;
  3630. ssl->buffers.serverDH_G.length = params->g_len;
  3631. ssl->namedGroup = serverGroup->name;
  3632. #if !defined(WOLFSSL_OLD_PRIME_CHECK) && \
  3633. !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  3634. ssl->options.dhDoKeyTest = 0;
  3635. #endif
  3636. ssl->options.haveDH = 1;
  3637. }
  3638. return ret;
  3639. }
  3640. #else
  3641. static int tlsx_ffdhe_find_group(WOLFSSL* ssl, SupportedCurve* clientGroup,
  3642. SupportedCurve* serverGroup)
  3643. {
  3644. int ret = 0;
  3645. SupportedCurve* group;
  3646. word32 p_len;
  3647. for (; serverGroup != NULL; serverGroup = serverGroup->next) {
  3648. if (!WOLFSSL_NAMED_GROUP_IS_FFHDE(serverGroup->name))
  3649. continue;
  3650. for (group = clientGroup; group != NULL; group = group->next) {
  3651. if (serverGroup->name != group->name)
  3652. continue;
  3653. wc_DhGetNamedKeyParamSize(serverGroup->name, &p_len, NULL, NULL);
  3654. if (p_len == 0) {
  3655. ret = BAD_FUNC_ARG;
  3656. break;
  3657. }
  3658. if (p_len >= ssl->options.minDhKeySz &&
  3659. p_len <= ssl->options.maxDhKeySz) {
  3660. break;
  3661. }
  3662. }
  3663. if (ret != 0)
  3664. break;
  3665. if ((group != NULL) && (serverGroup->name == group->name))
  3666. break;
  3667. }
  3668. if ((ret == 0) && (serverGroup != NULL)) {
  3669. word32 pSz, gSz;
  3670. ssl->buffers.serverDH_P.buffer = NULL;
  3671. ssl->buffers.serverDH_G.buffer = NULL;
  3672. ret = wc_DhGetNamedKeyParamSize(serverGroup->name, &pSz, &gSz, NULL);
  3673. if (ret == 0) {
  3674. ssl->buffers.serverDH_P.buffer =
  3675. (byte*)XMALLOC(pSz, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  3676. if (ssl->buffers.serverDH_P.buffer == NULL)
  3677. ret = MEMORY_E;
  3678. else
  3679. ssl->buffers.serverDH_P.length = pSz;
  3680. }
  3681. if (ret == 0) {
  3682. ssl->buffers.serverDH_G.buffer =
  3683. (byte*)XMALLOC(gSz, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  3684. if (ssl->buffers.serverDH_G.buffer == NULL) {
  3685. ret = MEMORY_E;
  3686. } else
  3687. ssl->buffers.serverDH_G.length = gSz;
  3688. }
  3689. if (ret == 0) {
  3690. ret = wc_DhCopyNamedKey(serverGroup->name,
  3691. ssl->buffers.serverDH_P.buffer, &pSz,
  3692. ssl->buffers.serverDH_G.buffer, &gSz,
  3693. NULL, NULL);
  3694. }
  3695. if (ret == 0) {
  3696. ssl->buffers.weOwnDH = 1;
  3697. ssl->namedGroup = serverGroup->name;
  3698. #if !defined(WOLFSSL_OLD_PRIME_CHECK) && \
  3699. !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  3700. ssl->options.dhDoKeyTest = 0;
  3701. #endif
  3702. ssl->options.haveDH = 1;
  3703. }
  3704. else {
  3705. if (ssl->buffers.serverDH_P.buffer != NULL) {
  3706. XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap,
  3707. DYNAMIC_TYPE_PUBLIC_KEY);
  3708. ssl->buffers.serverDH_P.length = 0;
  3709. ssl->buffers.serverDH_P.buffer = NULL;
  3710. }
  3711. if (ssl->buffers.serverDH_G.buffer != NULL) {
  3712. XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap,
  3713. DYNAMIC_TYPE_PUBLIC_KEY);
  3714. ssl->buffers.serverDH_G.length = 0;
  3715. ssl->buffers.serverDH_G.buffer = NULL;
  3716. }
  3717. }
  3718. }
  3719. return ret;
  3720. }
  3721. #endif
  3722. /* Set the highest priority common FFDHE group on the server as compared to
  3723. * client extensions.
  3724. *
  3725. * ssl SSL/TLS object.
  3726. * returns 0 on success, otherwise an error.
  3727. */
  3728. int TLSX_SupportedFFDHE_Set(WOLFSSL* ssl)
  3729. {
  3730. int ret;
  3731. TLSX* priority = NULL;
  3732. TLSX* ext = NULL;
  3733. TLSX* extension;
  3734. SupportedCurve* clientGroup;
  3735. SupportedCurve* group;
  3736. int found = 0;
  3737. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3738. /* May be doing PSK with no key exchange. */
  3739. if (extension == NULL)
  3740. return 0;
  3741. clientGroup = (SupportedCurve*)extension->data;
  3742. for (group = clientGroup; group != NULL; group = group->next) {
  3743. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(group->name)) {
  3744. found = 1;
  3745. break;
  3746. }
  3747. }
  3748. if (!found)
  3749. return 0;
  3750. if (ssl->buffers.serverDH_P.buffer && ssl->buffers.weOwnDH) {
  3751. XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap,
  3752. DYNAMIC_TYPE_PUBLIC_KEY);
  3753. }
  3754. if (ssl->buffers.serverDH_G.buffer && ssl->buffers.weOwnDH) {
  3755. XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap,
  3756. DYNAMIC_TYPE_PUBLIC_KEY);
  3757. }
  3758. ssl->buffers.serverDH_P.buffer = NULL;
  3759. ssl->buffers.serverDH_G.buffer = NULL;
  3760. ssl->buffers.weOwnDH = 0;
  3761. ssl->options.haveDH = 0;
  3762. ret = TLSX_PopulateSupportedGroups(ssl, &priority);
  3763. if (ret == WOLFSSL_SUCCESS) {
  3764. SupportedCurve* serverGroup;
  3765. ext = TLSX_Find(priority, TLSX_SUPPORTED_GROUPS);
  3766. serverGroup = (SupportedCurve*)ext->data;
  3767. ret = tlsx_ffdhe_find_group(ssl, clientGroup, serverGroup);
  3768. }
  3769. TLSX_FreeAll(priority, ssl->heap);
  3770. return ret;
  3771. }
  3772. #endif /* HAVE_FFDHE && !WOLFSSL_NO_TLS12 */
  3773. #endif /* !NO_WOLFSSL_SERVER */
  3774. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
  3775. /* Return the preferred group.
  3776. *
  3777. * ssl SSL/TLS object.
  3778. * checkSupported Whether to check for the first supported group.
  3779. * returns BAD_FUNC_ARG if no group found, otherwise the group.
  3780. */
  3781. int TLSX_SupportedCurve_Preferred(WOLFSSL* ssl, int checkSupported)
  3782. {
  3783. TLSX* extension;
  3784. SupportedCurve* curve;
  3785. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3786. if (extension == NULL)
  3787. return BAD_FUNC_ARG;
  3788. curve = (SupportedCurve*)extension->data;
  3789. while (curve != NULL) {
  3790. if (!checkSupported || TLSX_KeyShare_IsSupported(curve->name))
  3791. return curve->name;
  3792. curve = curve->next;
  3793. }
  3794. return BAD_FUNC_ARG;
  3795. }
  3796. #endif /* HAVE_SUPPORTED_CURVES */
  3797. #ifndef NO_WOLFSSL_SERVER
  3798. static int TLSX_PointFormat_Parse(WOLFSSL* ssl, const byte* input,
  3799. word16 length, byte isRequest)
  3800. {
  3801. int ret;
  3802. /* validating formats list length */
  3803. if (ENUM_LEN > length || length != (word16)ENUM_LEN + input[0])
  3804. return BUFFER_ERROR;
  3805. if (isRequest) {
  3806. /* adding uncompressed point format to response */
  3807. ret = TLSX_UsePointFormat(&ssl->extensions, WOLFSSL_EC_PF_UNCOMPRESSED,
  3808. ssl->heap);
  3809. if (ret != WOLFSSL_SUCCESS)
  3810. return ret; /* throw error */
  3811. TLSX_SetResponse(ssl, TLSX_EC_POINT_FORMATS);
  3812. }
  3813. return 0;
  3814. }
  3815. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  3816. int TLSX_ValidateSupportedCurves(const WOLFSSL* ssl, byte first, byte second,
  3817. word32* ecdhCurveOID) {
  3818. TLSX* extension = NULL;
  3819. SupportedCurve* curve = NULL;
  3820. word32 oid = 0;
  3821. word32 defOid = 0;
  3822. word32 defSz = 80; /* Maximum known curve size is 66. */
  3823. word32 nextOid = 0;
  3824. word32 nextSz = 80; /* Maximum known curve size is 66. */
  3825. word32 currOid = ssl->ecdhCurveOID;
  3826. int ephmSuite = 0;
  3827. word16 octets = 0; /* according to 'ecc_set_type ecc_sets[];' */
  3828. int key = 0; /* validate key */
  3829. (void)oid;
  3830. if (first == CHACHA_BYTE) {
  3831. switch (second) {
  3832. case TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
  3833. case TLS_PSK_WITH_CHACHA20_POLY1305_SHA256:
  3834. case TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
  3835. case TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256:
  3836. return 1; /* no suite restriction */
  3837. case TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
  3838. case TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256:
  3839. case TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
  3840. break;
  3841. }
  3842. }
  3843. if (first == ECC_BYTE || first == ECDHE_PSK_BYTE || first == CHACHA_BYTE)
  3844. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3845. if (!extension)
  3846. return 1; /* no suite restriction */
  3847. for (curve = (SupportedCurve*)extension->data;
  3848. curve && !key;
  3849. curve = curve->next) {
  3850. #ifdef OPENSSL_EXTRA
  3851. /* skip if name is not in supported ECC range
  3852. * or disabled by user */
  3853. if (curve->name > WOLFSSL_ECC_MAX ||
  3854. wolfSSL_curve_is_disabled(ssl, curve->name))
  3855. continue;
  3856. #endif
  3857. /* find supported curve */
  3858. switch (curve->name) {
  3859. #ifdef HAVE_ECC
  3860. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  3861. #ifndef NO_ECC_SECP
  3862. case WOLFSSL_ECC_SECP160R1:
  3863. oid = ECC_SECP160R1_OID;
  3864. octets = 20;
  3865. break;
  3866. #endif /* !NO_ECC_SECP */
  3867. #ifdef HAVE_ECC_SECPR2
  3868. case WOLFSSL_ECC_SECP160R2:
  3869. oid = ECC_SECP160R2_OID;
  3870. octets = 20;
  3871. break;
  3872. #endif /* HAVE_ECC_SECPR2 */
  3873. #ifdef HAVE_ECC_KOBLITZ
  3874. case WOLFSSL_ECC_SECP160K1:
  3875. oid = ECC_SECP160K1_OID;
  3876. octets = 20;
  3877. break;
  3878. #endif /* HAVE_ECC_KOBLITZ */
  3879. #endif
  3880. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  3881. #ifndef NO_ECC_SECP
  3882. case WOLFSSL_ECC_SECP192R1:
  3883. oid = ECC_SECP192R1_OID;
  3884. octets = 24;
  3885. break;
  3886. #endif /* !NO_ECC_SECP */
  3887. #ifdef HAVE_ECC_KOBLITZ
  3888. case WOLFSSL_ECC_SECP192K1:
  3889. oid = ECC_SECP192K1_OID;
  3890. octets = 24;
  3891. break;
  3892. #endif /* HAVE_ECC_KOBLITZ */
  3893. #endif
  3894. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  3895. #ifndef NO_ECC_SECP
  3896. case WOLFSSL_ECC_SECP224R1:
  3897. oid = ECC_SECP224R1_OID;
  3898. octets = 28;
  3899. break;
  3900. #endif /* !NO_ECC_SECP */
  3901. #ifdef HAVE_ECC_KOBLITZ
  3902. case WOLFSSL_ECC_SECP224K1:
  3903. oid = ECC_SECP224K1_OID;
  3904. octets = 28;
  3905. break;
  3906. #endif /* HAVE_ECC_KOBLITZ */
  3907. #endif
  3908. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  3909. #ifndef NO_ECC_SECP
  3910. case WOLFSSL_ECC_SECP256R1:
  3911. oid = ECC_SECP256R1_OID;
  3912. octets = 32;
  3913. break;
  3914. #endif /* !NO_ECC_SECP */
  3915. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  3916. #endif
  3917. #if (defined(HAVE_CURVE25519) || defined(HAVE_ED25519)) && ECC_MIN_KEY_SZ <= 256
  3918. case WOLFSSL_ECC_X25519:
  3919. oid = ECC_X25519_OID;
  3920. octets = 32;
  3921. break;
  3922. #endif /* HAVE_CURVE25519 */
  3923. #ifdef HAVE_ECC
  3924. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  3925. #ifdef HAVE_ECC_KOBLITZ
  3926. case WOLFSSL_ECC_SECP256K1:
  3927. oid = ECC_SECP256K1_OID;
  3928. octets = 32;
  3929. break;
  3930. #endif /* HAVE_ECC_KOBLITZ */
  3931. #ifdef HAVE_ECC_BRAINPOOL
  3932. case WOLFSSL_ECC_BRAINPOOLP256R1:
  3933. oid = ECC_BRAINPOOLP256R1_OID;
  3934. octets = 32;
  3935. break;
  3936. #endif /* HAVE_ECC_BRAINPOOL */
  3937. #ifdef WOLFSSL_SM2
  3938. case WOLFSSL_ECC_SM2P256V1:
  3939. oid = ECC_SM2P256V1_OID;
  3940. octets = 32;
  3941. break;
  3942. #endif /* WOLFSSL_SM2 */
  3943. #endif
  3944. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  3945. #ifndef NO_ECC_SECP
  3946. case WOLFSSL_ECC_SECP384R1:
  3947. oid = ECC_SECP384R1_OID;
  3948. octets = 48;
  3949. break;
  3950. #endif /* !NO_ECC_SECP */
  3951. #ifdef HAVE_ECC_BRAINPOOL
  3952. case WOLFSSL_ECC_BRAINPOOLP384R1:
  3953. oid = ECC_BRAINPOOLP384R1_OID;
  3954. octets = 48;
  3955. break;
  3956. #endif /* HAVE_ECC_BRAINPOOL */
  3957. #endif
  3958. #endif
  3959. #if (defined(HAVE_CURVE448) || defined(HAVE_ED448)) && ECC_MIN_KEY_SZ <= 448
  3960. case WOLFSSL_ECC_X448:
  3961. oid = ECC_X448_OID;
  3962. octets = 57;
  3963. break;
  3964. #endif /* HAVE_CURVE448 */
  3965. #ifdef HAVE_ECC
  3966. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  3967. #ifdef HAVE_ECC_BRAINPOOL
  3968. case WOLFSSL_ECC_BRAINPOOLP512R1:
  3969. oid = ECC_BRAINPOOLP512R1_OID;
  3970. octets = 64;
  3971. break;
  3972. #endif /* HAVE_ECC_BRAINPOOL */
  3973. #endif
  3974. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  3975. #ifndef NO_ECC_SECP
  3976. case WOLFSSL_ECC_SECP521R1:
  3977. oid = ECC_SECP521R1_OID;
  3978. octets = 66;
  3979. break;
  3980. #endif /* !NO_ECC_SECP */
  3981. #endif
  3982. #endif
  3983. default: continue; /* unsupported curve */
  3984. }
  3985. #ifdef HAVE_ECC
  3986. /* Set default Oid */
  3987. if (defOid == 0 && ssl->eccTempKeySz <= octets && defSz > octets) {
  3988. defOid = oid;
  3989. defSz = octets;
  3990. }
  3991. /* The eccTempKeySz is the preferred ephemeral key size */
  3992. if (currOid == 0 && ssl->eccTempKeySz == octets)
  3993. currOid = oid;
  3994. if ((nextOid == 0 || nextSz > octets) && ssl->eccTempKeySz <= octets) {
  3995. nextOid = oid;
  3996. nextSz = octets;
  3997. }
  3998. #else
  3999. if (defOid == 0 && defSz > octets) {
  4000. defOid = oid;
  4001. defSz = octets;
  4002. }
  4003. if (currOid == 0)
  4004. currOid = oid;
  4005. if (nextOid == 0 || nextSz > octets) {
  4006. nextOid = oid;
  4007. nextSz = octets;
  4008. }
  4009. #endif
  4010. if (first == ECC_BYTE) {
  4011. switch (second) {
  4012. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
  4013. /* ECDHE_ECDSA */
  4014. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA:
  4015. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA:
  4016. case TLS_ECDHE_ECDSA_WITH_RC4_128_SHA:
  4017. case TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA:
  4018. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256:
  4019. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384:
  4020. case TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
  4021. case TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:
  4022. case TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8:
  4023. case TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8:
  4024. key |= ssl->ecdhCurveOID == oid;
  4025. ephmSuite = 1;
  4026. break;
  4027. #ifdef WOLFSSL_STATIC_DH
  4028. /* ECDH_ECDSA */
  4029. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA:
  4030. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA:
  4031. case TLS_ECDH_ECDSA_WITH_RC4_128_SHA:
  4032. case TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA:
  4033. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256:
  4034. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384:
  4035. case TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256:
  4036. case TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384:
  4037. if (oid == ECC_X25519_OID && defOid == oid) {
  4038. defOid = 0;
  4039. defSz = 80;
  4040. }
  4041. if (oid == ECC_X448_OID && defOid == oid) {
  4042. defOid = 0;
  4043. defSz = 80;
  4044. }
  4045. key |= ssl->pkCurveOID == oid;
  4046. break;
  4047. #endif /* WOLFSSL_STATIC_DH */
  4048. #endif /* HAVE_ECC || HAVE_ED25519 || HAVE_ED448 */
  4049. #ifndef NO_RSA
  4050. /* ECDHE_RSA */
  4051. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA:
  4052. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA:
  4053. case TLS_ECDHE_RSA_WITH_RC4_128_SHA:
  4054. case TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA:
  4055. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256:
  4056. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384:
  4057. case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:
  4058. case TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384:
  4059. key |= ssl->ecdhCurveOID == oid;
  4060. ephmSuite = 1;
  4061. break;
  4062. #if defined(HAVE_ECC) && defined(WOLFSSL_STATIC_DH)
  4063. /* ECDH_RSA */
  4064. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA:
  4065. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA:
  4066. case TLS_ECDH_RSA_WITH_RC4_128_SHA:
  4067. case TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA:
  4068. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256:
  4069. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384:
  4070. case TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256:
  4071. case TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384:
  4072. if (oid == ECC_X25519_OID && defOid == oid) {
  4073. defOid = 0;
  4074. defSz = 80;
  4075. }
  4076. if (oid == ECC_X448_OID && defOid == oid) {
  4077. defOid = 0;
  4078. defSz = 80;
  4079. }
  4080. key |= ssl->pkCurveOID == oid;
  4081. break;
  4082. #endif /* HAVE_ECC && WOLFSSL_STATIC_DH */
  4083. #endif
  4084. default:
  4085. if (oid == ECC_X25519_OID && defOid == oid) {
  4086. defOid = 0;
  4087. defSz = 80;
  4088. }
  4089. if (oid == ECC_X448_OID && defOid == oid) {
  4090. defOid = 0;
  4091. defSz = 80;
  4092. }
  4093. key = 1;
  4094. break;
  4095. }
  4096. }
  4097. /* ChaCha20-Poly1305 ECC cipher suites */
  4098. if (first == CHACHA_BYTE) {
  4099. switch (second) {
  4100. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
  4101. /* ECDHE_ECDSA */
  4102. case TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 :
  4103. case TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 :
  4104. key |= ssl->ecdhCurveOID == oid;
  4105. ephmSuite = 1;
  4106. break;
  4107. #endif /* HAVE_ECC || HAVE_ED25519 || HAVE_ED448 */
  4108. #ifndef NO_RSA
  4109. /* ECDHE_RSA */
  4110. case TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 :
  4111. case TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 :
  4112. key |= ssl->ecdhCurveOID == oid;
  4113. ephmSuite = 1;
  4114. break;
  4115. #endif
  4116. default:
  4117. key = 1;
  4118. break;
  4119. }
  4120. }
  4121. }
  4122. *ecdhCurveOID = ssl->ecdhCurveOID;
  4123. /* Choose the default if it is at the required strength. */
  4124. #ifdef HAVE_ECC
  4125. if (*ecdhCurveOID == 0 && defSz == ssl->eccTempKeySz)
  4126. #else
  4127. if (*ecdhCurveOID == 0)
  4128. #endif
  4129. {
  4130. key = 1;
  4131. *ecdhCurveOID = defOid;
  4132. }
  4133. /* Choose any curve at the required strength. */
  4134. if (*ecdhCurveOID == 0) {
  4135. key = 1;
  4136. *ecdhCurveOID = currOid;
  4137. }
  4138. /* Choose the default if it is at the next highest strength. */
  4139. if (*ecdhCurveOID == 0 && defSz == nextSz)
  4140. *ecdhCurveOID = defOid;
  4141. /* Choose any curve at the next highest strength. */
  4142. if (*ecdhCurveOID == 0)
  4143. *ecdhCurveOID = nextOid;
  4144. /* No curve and ephemeral ECC suite requires a matching curve. */
  4145. if (*ecdhCurveOID == 0 && ephmSuite)
  4146. key = 0;
  4147. return key;
  4148. }
  4149. #endif
  4150. #endif /* NO_WOLFSSL_SERVER */
  4151. int TLSX_SupportedCurve_Copy(TLSX* src, TLSX** dst, void* heap)
  4152. {
  4153. TLSX* extension;
  4154. int ret;
  4155. extension = TLSX_Find(src, TLSX_SUPPORTED_GROUPS);
  4156. if (extension != NULL) {
  4157. SupportedCurve* curve;
  4158. for (curve = (SupportedCurve*)extension->data; curve != NULL;
  4159. curve = curve->next) {
  4160. ret = TLSX_UseSupportedCurve(dst, curve->name, heap);
  4161. if (ret != WOLFSSL_SUCCESS)
  4162. return MEMORY_E;
  4163. }
  4164. }
  4165. return 0;
  4166. }
  4167. int TLSX_UseSupportedCurve(TLSX** extensions, word16 name, void* heap)
  4168. {
  4169. TLSX* extension = NULL;
  4170. SupportedCurve* curve = NULL;
  4171. int ret;
  4172. if (extensions == NULL) {
  4173. return BAD_FUNC_ARG;
  4174. }
  4175. #ifdef WOLFSSL_TLS13
  4176. if (! TLSX_KeyShare_IsSupported(name)) {
  4177. return BAD_FUNC_ARG;
  4178. }
  4179. #endif
  4180. extension = TLSX_Find(*extensions, TLSX_SUPPORTED_GROUPS);
  4181. if (!extension) {
  4182. ret = TLSX_SupportedCurve_New(&curve, name, heap);
  4183. if (ret != 0)
  4184. return ret;
  4185. ret = TLSX_Push(extensions, TLSX_SUPPORTED_GROUPS, curve, heap);
  4186. if (ret != 0) {
  4187. XFREE(curve, heap, DYNAMIC_TYPE_TLSX);
  4188. return ret;
  4189. }
  4190. }
  4191. else {
  4192. ret = TLSX_SupportedCurve_Append((SupportedCurve*)extension->data, name,
  4193. heap);
  4194. if (ret != 0)
  4195. return ret;
  4196. }
  4197. return WOLFSSL_SUCCESS;
  4198. }
  4199. int TLSX_UsePointFormat(TLSX** extensions, byte format, void* heap)
  4200. {
  4201. TLSX* extension = NULL;
  4202. PointFormat* point = NULL;
  4203. int ret = 0;
  4204. if (extensions == NULL)
  4205. return BAD_FUNC_ARG;
  4206. extension = TLSX_Find(*extensions, TLSX_EC_POINT_FORMATS);
  4207. if (!extension) {
  4208. ret = TLSX_PointFormat_New(&point, format, heap);
  4209. if (ret != 0)
  4210. return ret;
  4211. ret = TLSX_Push(extensions, TLSX_EC_POINT_FORMATS, point, heap);
  4212. if (ret != 0) {
  4213. XFREE(point, heap, DYNAMIC_TYPE_TLSX);
  4214. return ret;
  4215. }
  4216. }
  4217. else {
  4218. ret = TLSX_PointFormat_Append((PointFormat*)extension->data, format,
  4219. heap);
  4220. if (ret != 0)
  4221. return ret;
  4222. }
  4223. return WOLFSSL_SUCCESS;
  4224. }
  4225. #define EC_FREE_ALL TLSX_SupportedCurve_FreeAll
  4226. #define EC_VALIDATE_REQUEST TLSX_SupportedCurve_ValidateRequest
  4227. #ifndef NO_WOLFSSL_CLIENT
  4228. #define EC_GET_SIZE TLSX_SupportedCurve_GetSize
  4229. #define EC_WRITE TLSX_SupportedCurve_Write
  4230. #else
  4231. #define EC_GET_SIZE(list) 0
  4232. #define EC_WRITE(a, b) 0
  4233. #endif
  4234. #if !defined(NO_WOLFSSL_SERVER) || (defined(WOLFSSL_TLS13) && \
  4235. !defined(WOLFSSL_NO_SERVER_GROUPS_EXT))
  4236. #define EC_PARSE TLSX_SupportedCurve_Parse
  4237. #else
  4238. #define EC_PARSE(a, b, c, d, e) 0
  4239. #endif
  4240. #define PF_FREE_ALL TLSX_PointFormat_FreeAll
  4241. #define PF_VALIDATE_REQUEST TLSX_PointFormat_ValidateRequest
  4242. #define PF_VALIDATE_RESPONSE TLSX_PointFormat_ValidateResponse
  4243. #define PF_GET_SIZE TLSX_PointFormat_GetSize
  4244. #define PF_WRITE TLSX_PointFormat_Write
  4245. #ifndef NO_WOLFSSL_SERVER
  4246. #define PF_PARSE TLSX_PointFormat_Parse
  4247. #else
  4248. #define PF_PARSE(a, b, c, d) 0
  4249. #endif
  4250. #else
  4251. #define EC_FREE_ALL(list, heap)
  4252. #define EC_GET_SIZE(list) 0
  4253. #define EC_WRITE(a, b) 0
  4254. #define EC_PARSE(a, b, c, d, e) 0
  4255. #define EC_VALIDATE_REQUEST(a, b)
  4256. #define PF_FREE_ALL(list, heap)
  4257. #define PF_GET_SIZE(list) 0
  4258. #define PF_WRITE(a, b) 0
  4259. #define PF_PARSE(a, b, c, d) 0
  4260. #define PF_VALIDATE_REQUEST(a, b)
  4261. #define PF_VALIDATE_RESPONSE(a, b)
  4262. #endif /* HAVE_SUPPORTED_CURVES */
  4263. /******************************************************************************/
  4264. /* Renegotiation Indication */
  4265. /******************************************************************************/
  4266. #if defined(HAVE_SECURE_RENEGOTIATION) \
  4267. || defined(HAVE_SERVER_RENEGOTIATION_INFO)
  4268. static byte TLSX_SecureRenegotiation_GetSize(SecureRenegotiation* data,
  4269. int isRequest)
  4270. {
  4271. byte length = OPAQUE8_LEN; /* empty info length */
  4272. /* data will be NULL for HAVE_SERVER_RENEGOTIATION_INFO only */
  4273. if (data && data->enabled && data->verifySet) {
  4274. /* client sends client_verify_data only */
  4275. length += TLS_FINISHED_SZ;
  4276. /* server also sends server_verify_data */
  4277. if (!isRequest)
  4278. length += TLS_FINISHED_SZ;
  4279. }
  4280. return length;
  4281. }
  4282. static word16 TLSX_SecureRenegotiation_Write(SecureRenegotiation* data,
  4283. byte* output, int isRequest)
  4284. {
  4285. word16 offset = OPAQUE8_LEN; /* RenegotiationInfo length */
  4286. if (data && data->enabled && data->verifySet) {
  4287. /* client sends client_verify_data only */
  4288. XMEMCPY(output + offset, data->client_verify_data, TLS_FINISHED_SZ);
  4289. offset += TLS_FINISHED_SZ;
  4290. /* server also sends server_verify_data */
  4291. if (!isRequest) {
  4292. XMEMCPY(output + offset, data->server_verify_data, TLS_FINISHED_SZ);
  4293. offset += TLS_FINISHED_SZ;
  4294. }
  4295. }
  4296. output[0] = (byte)(offset - 1); /* info length - self */
  4297. return offset;
  4298. }
  4299. static int TLSX_SecureRenegotiation_Parse(WOLFSSL* ssl, const byte* input,
  4300. word16 length, byte isRequest)
  4301. {
  4302. int ret = SECURE_RENEGOTIATION_E;
  4303. if (length >= OPAQUE8_LEN) {
  4304. if (isRequest) {
  4305. #ifndef NO_WOLFSSL_SERVER
  4306. if (ssl->secure_renegotiation == NULL) {
  4307. ret = wolfSSL_UseSecureRenegotiation(ssl);
  4308. if (ret == WOLFSSL_SUCCESS)
  4309. ret = 0;
  4310. }
  4311. if (ret != 0 && ret != SECURE_RENEGOTIATION_E) {
  4312. }
  4313. else if (ssl->secure_renegotiation == NULL) {
  4314. }
  4315. else if (!ssl->secure_renegotiation->enabled) {
  4316. if (*input == 0) {
  4317. input++; /* get past size */
  4318. ssl->secure_renegotiation->enabled = 1;
  4319. TLSX_SetResponse(ssl, TLSX_RENEGOTIATION_INFO);
  4320. ret = 0;
  4321. }
  4322. else {
  4323. /* already in error state */
  4324. WOLFSSL_MSG("SCR client verify data present");
  4325. }
  4326. }
  4327. else if (*input == TLS_FINISHED_SZ) {
  4328. if (length < TLS_FINISHED_SZ + 1) {
  4329. WOLFSSL_MSG("SCR malformed buffer");
  4330. ret = BUFFER_E;
  4331. }
  4332. else {
  4333. input++; /* get past size */
  4334. /* validate client verify data */
  4335. if (XMEMCMP(input,
  4336. ssl->secure_renegotiation->client_verify_data,
  4337. TLS_FINISHED_SZ) == 0) {
  4338. WOLFSSL_MSG("SCR client verify data match");
  4339. TLSX_SetResponse(ssl, TLSX_RENEGOTIATION_INFO);
  4340. ret = 0; /* verified */
  4341. }
  4342. else {
  4343. /* already in error state */
  4344. WOLFSSL_MSG("SCR client verify data Failure");
  4345. }
  4346. }
  4347. }
  4348. #endif
  4349. }
  4350. else if (ssl->secure_renegotiation != NULL) {
  4351. #ifndef NO_WOLFSSL_CLIENT
  4352. if (!ssl->secure_renegotiation->enabled) {
  4353. if (*input == 0) {
  4354. ssl->secure_renegotiation->enabled = 1;
  4355. ret = 0;
  4356. }
  4357. }
  4358. else if (*input == 2 * TLS_FINISHED_SZ &&
  4359. length == 2 * TLS_FINISHED_SZ + OPAQUE8_LEN) {
  4360. input++; /* get past size */
  4361. /* validate client and server verify data */
  4362. if (XMEMCMP(input,
  4363. ssl->secure_renegotiation->client_verify_data,
  4364. TLS_FINISHED_SZ) == 0 &&
  4365. XMEMCMP(input + TLS_FINISHED_SZ,
  4366. ssl->secure_renegotiation->server_verify_data,
  4367. TLS_FINISHED_SZ) == 0) {
  4368. WOLFSSL_MSG("SCR client and server verify data match");
  4369. ret = 0; /* verified */
  4370. }
  4371. else {
  4372. /* already in error state */
  4373. WOLFSSL_MSG("SCR client and server verify data Failure");
  4374. }
  4375. }
  4376. #endif
  4377. }
  4378. }
  4379. if (ret != 0) {
  4380. WOLFSSL_ERROR_VERBOSE(ret);
  4381. SendAlert(ssl, alert_fatal, handshake_failure);
  4382. }
  4383. return ret;
  4384. }
  4385. int TLSX_UseSecureRenegotiation(TLSX** extensions, void* heap)
  4386. {
  4387. int ret = 0;
  4388. SecureRenegotiation* data;
  4389. data = (SecureRenegotiation*)XMALLOC(sizeof(SecureRenegotiation), heap,
  4390. DYNAMIC_TYPE_TLSX);
  4391. if (data == NULL)
  4392. return MEMORY_E;
  4393. XMEMSET(data, 0, sizeof(SecureRenegotiation));
  4394. ret = TLSX_Push(extensions, TLSX_RENEGOTIATION_INFO, data, heap);
  4395. if (ret != 0) {
  4396. XFREE(data, heap, DYNAMIC_TYPE_TLSX);
  4397. return ret;
  4398. }
  4399. return WOLFSSL_SUCCESS;
  4400. }
  4401. #ifdef HAVE_SERVER_RENEGOTIATION_INFO
  4402. int TLSX_AddEmptyRenegotiationInfo(TLSX** extensions, void* heap)
  4403. {
  4404. int ret;
  4405. /* send empty renegotiation_info extension */
  4406. TLSX* ext = TLSX_Find(*extensions, TLSX_RENEGOTIATION_INFO);
  4407. if (ext == NULL) {
  4408. ret = TLSX_UseSecureRenegotiation(extensions, heap);
  4409. if (ret != WOLFSSL_SUCCESS)
  4410. return ret;
  4411. ext = TLSX_Find(*extensions, TLSX_RENEGOTIATION_INFO);
  4412. }
  4413. if (ext)
  4414. ext->resp = 1;
  4415. return WOLFSSL_SUCCESS;
  4416. }
  4417. #endif /* HAVE_SERVER_RENEGOTIATION_INFO */
  4418. #define SCR_FREE_ALL(data, heap) XFREE(data, (heap), DYNAMIC_TYPE_TLSX)
  4419. #define SCR_GET_SIZE TLSX_SecureRenegotiation_GetSize
  4420. #define SCR_WRITE TLSX_SecureRenegotiation_Write
  4421. #define SCR_PARSE TLSX_SecureRenegotiation_Parse
  4422. #else
  4423. #define SCR_FREE_ALL(a, heap)
  4424. #define SCR_GET_SIZE(a, b) 0
  4425. #define SCR_WRITE(a, b, c) 0
  4426. #define SCR_PARSE(a, b, c, d) 0
  4427. #endif /* HAVE_SECURE_RENEGOTIATION || HAVE_SERVER_RENEGOTIATION_INFO */
  4428. /******************************************************************************/
  4429. /* Session Tickets */
  4430. /******************************************************************************/
  4431. #ifdef HAVE_SESSION_TICKET
  4432. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
  4433. static void TLSX_SessionTicket_ValidateRequest(WOLFSSL* ssl)
  4434. {
  4435. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_SESSION_TICKET);
  4436. SessionTicket* ticket = extension ?
  4437. (SessionTicket*)extension->data : NULL;
  4438. if (ticket) {
  4439. /* TODO validate ticket timeout here! */
  4440. if (ticket->lifetime == 0xfffffff) {
  4441. /* send empty ticket on timeout */
  4442. TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
  4443. }
  4444. }
  4445. }
  4446. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
  4447. static word16 TLSX_SessionTicket_GetSize(SessionTicket* ticket, int isRequest)
  4448. {
  4449. (void)isRequest;
  4450. return ticket ? ticket->size : 0;
  4451. }
  4452. static word16 TLSX_SessionTicket_Write(SessionTicket* ticket, byte* output,
  4453. int isRequest)
  4454. {
  4455. word16 offset = 0; /* empty ticket */
  4456. if (isRequest && ticket) {
  4457. XMEMCPY(output + offset, ticket->data, ticket->size);
  4458. offset += ticket->size;
  4459. }
  4460. return offset;
  4461. }
  4462. static int TLSX_SessionTicket_Parse(WOLFSSL* ssl, const byte* input,
  4463. word16 length, byte isRequest)
  4464. {
  4465. int ret = 0;
  4466. (void) input; /* avoid unused parameter if NO_WOLFSSL_SERVER defined */
  4467. if (!isRequest) {
  4468. if (TLSX_CheckUnsupportedExtension(ssl, TLSX_SESSION_TICKET))
  4469. return TLSX_HandleUnsupportedExtension(ssl);
  4470. if (length != 0)
  4471. return BUFFER_ERROR;
  4472. #ifndef NO_WOLFSSL_CLIENT
  4473. ssl->expect_session_ticket = 1;
  4474. #endif
  4475. }
  4476. #ifndef NO_WOLFSSL_SERVER
  4477. else {
  4478. /* server side */
  4479. if (ssl->ctx->ticketEncCb == NULL) {
  4480. WOLFSSL_MSG("Client sent session ticket, server has no callback");
  4481. return 0;
  4482. }
  4483. if (length > SESSION_TICKET_LEN) {
  4484. ret = BAD_TICKET_MSG_SZ;
  4485. WOLFSSL_ERROR_VERBOSE(ret);
  4486. } else if (IsAtLeastTLSv1_3(ssl->version)) {
  4487. WOLFSSL_MSG("Process client ticket rejected, TLS 1.3 no support");
  4488. ssl->options.rejectTicket = 1;
  4489. ret = 0; /* not fatal */
  4490. } else if (ssl->options.noTicketTls12) {
  4491. /* ignore ticket request */
  4492. } else if (length == 0) {
  4493. /* blank ticket */
  4494. ret = TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
  4495. if (ret == WOLFSSL_SUCCESS) {
  4496. ret = 0;
  4497. /* send blank ticket */
  4498. TLSX_SetResponse(ssl, TLSX_SESSION_TICKET);
  4499. ssl->options.createTicket = 1; /* will send ticket msg */
  4500. ssl->options.useTicket = 1;
  4501. ssl->options.resuming = 0; /* no standard resumption */
  4502. ssl->arrays->sessionIDSz = 0; /* no echo on blank ticket */
  4503. }
  4504. } else {
  4505. /* got actual ticket from client */
  4506. ret = DoClientTicket(ssl, input, length);
  4507. if (ret == WOLFSSL_TICKET_RET_OK) { /* use ticket to resume */
  4508. WOLFSSL_MSG("Using existing client ticket");
  4509. ssl->options.useTicket = 1;
  4510. ssl->options.resuming = 1;
  4511. /* SERVER: ticket is peer auth. */
  4512. ssl->options.peerAuthGood = 1;
  4513. } else if (ret == WOLFSSL_TICKET_RET_CREATE) {
  4514. WOLFSSL_MSG("Using existing client ticket, creating new one");
  4515. ret = TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
  4516. if (ret == WOLFSSL_SUCCESS) {
  4517. ret = 0;
  4518. TLSX_SetResponse(ssl, TLSX_SESSION_TICKET);
  4519. /* send blank ticket */
  4520. ssl->options.createTicket = 1; /* will send ticket msg */
  4521. ssl->options.useTicket = 1;
  4522. ssl->options.resuming = 1;
  4523. /* SERVER: ticket is peer auth. */
  4524. ssl->options.peerAuthGood = 1;
  4525. }
  4526. } else if (ret == WOLFSSL_TICKET_RET_REJECT) {
  4527. WOLFSSL_MSG("Process client ticket rejected, not using");
  4528. ssl->options.rejectTicket = 1;
  4529. ret = 0; /* not fatal */
  4530. } else if (ret == VERSION_ERROR) {
  4531. WOLFSSL_MSG("Process client ticket rejected, bad TLS version");
  4532. ssl->options.rejectTicket = 1;
  4533. ret = 0; /* not fatal */
  4534. } else if (ret == WOLFSSL_TICKET_RET_FATAL) {
  4535. WOLFSSL_MSG("Process client ticket fatal error, not using");
  4536. } else if (ret < 0) {
  4537. WOLFSSL_MSG("Process client ticket unknown error, not using");
  4538. }
  4539. }
  4540. }
  4541. #endif /* NO_WOLFSSL_SERVER */
  4542. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  4543. (void)ssl;
  4544. #endif
  4545. return ret;
  4546. }
  4547. WOLFSSL_LOCAL SessionTicket* TLSX_SessionTicket_Create(word32 lifetime,
  4548. byte* data, word16 size, void* heap)
  4549. {
  4550. SessionTicket* ticket = (SessionTicket*)XMALLOC(sizeof(SessionTicket),
  4551. heap, DYNAMIC_TYPE_TLSX);
  4552. if (ticket) {
  4553. ticket->data = (byte*)XMALLOC(size, heap, DYNAMIC_TYPE_TLSX);
  4554. if (ticket->data == NULL) {
  4555. XFREE(ticket, heap, DYNAMIC_TYPE_TLSX);
  4556. return NULL;
  4557. }
  4558. XMEMCPY(ticket->data, data, size);
  4559. ticket->size = size;
  4560. ticket->lifetime = lifetime;
  4561. }
  4562. (void)heap;
  4563. return ticket;
  4564. }
  4565. WOLFSSL_LOCAL void TLSX_SessionTicket_Free(SessionTicket* ticket, void* heap)
  4566. {
  4567. if (ticket) {
  4568. XFREE(ticket->data, heap, DYNAMIC_TYPE_TLSX);
  4569. XFREE(ticket, heap, DYNAMIC_TYPE_TLSX);
  4570. }
  4571. (void)heap;
  4572. }
  4573. int TLSX_UseSessionTicket(TLSX** extensions, SessionTicket* ticket, void* heap)
  4574. {
  4575. int ret = 0;
  4576. if (extensions == NULL)
  4577. return BAD_FUNC_ARG;
  4578. /* If the ticket is NULL, the client will request a new ticket from the
  4579. server. Otherwise, the client will use it in the next client hello. */
  4580. if ((ret = TLSX_Push(extensions, TLSX_SESSION_TICKET, (void*)ticket, heap))
  4581. != 0)
  4582. return ret;
  4583. return WOLFSSL_SUCCESS;
  4584. }
  4585. #define WOLF_STK_VALIDATE_REQUEST TLSX_SessionTicket_ValidateRequest
  4586. #define WOLF_STK_GET_SIZE TLSX_SessionTicket_GetSize
  4587. #define WOLF_STK_WRITE TLSX_SessionTicket_Write
  4588. #define WOLF_STK_PARSE TLSX_SessionTicket_Parse
  4589. #define WOLF_STK_FREE(stk, heap) TLSX_SessionTicket_Free((SessionTicket*)(stk),(heap))
  4590. #else
  4591. #define WOLF_STK_FREE(a, b)
  4592. #define WOLF_STK_VALIDATE_REQUEST(a)
  4593. #define WOLF_STK_GET_SIZE(a, b) 0
  4594. #define WOLF_STK_WRITE(a, b, c) 0
  4595. #define WOLF_STK_PARSE(a, b, c, d) 0
  4596. #endif /* HAVE_SESSION_TICKET */
  4597. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  4598. /******************************************************************************/
  4599. /* Encrypt-then-MAC */
  4600. /******************************************************************************/
  4601. #ifndef WOLFSSL_NO_TLS12
  4602. static int TLSX_EncryptThenMac_Use(WOLFSSL* ssl);
  4603. /**
  4604. * Get the size of the Encrypt-Then-MAC extension.
  4605. *
  4606. * msgType Type of message to put extension into.
  4607. * pSz Size of extension data.
  4608. * return SANITY_MSG_E when the message is not allowed to have extension and
  4609. * 0 otherwise.
  4610. */
  4611. static int TLSX_EncryptThenMac_GetSize(byte msgType, word16* pSz)
  4612. {
  4613. (void)pSz;
  4614. if (msgType != client_hello && msgType != server_hello) {
  4615. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4616. return SANITY_MSG_E;
  4617. }
  4618. /* Empty extension */
  4619. return 0;
  4620. }
  4621. /**
  4622. * Write the Encrypt-Then-MAC extension.
  4623. *
  4624. * data Unused
  4625. * output Extension data buffer. Unused.
  4626. * msgType Type of message to put extension into.
  4627. * pSz Size of extension data.
  4628. * return SANITY_MSG_E when the message is not allowed to have extension and
  4629. * 0 otherwise.
  4630. */
  4631. static int TLSX_EncryptThenMac_Write(void* data, byte* output, byte msgType,
  4632. word16* pSz)
  4633. {
  4634. (void)data;
  4635. (void)output;
  4636. (void)pSz;
  4637. if (msgType != client_hello && msgType != server_hello) {
  4638. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4639. return SANITY_MSG_E;
  4640. }
  4641. /* Empty extension */
  4642. return 0;
  4643. }
  4644. /**
  4645. * Parse the Encrypt-Then-MAC extension.
  4646. *
  4647. * ssl SSL object
  4648. * input Extension data buffer.
  4649. * length Length of this extension's data.
  4650. * msgType Type of message to extension appeared in.
  4651. * return SANITY_MSG_E when the message is not allowed to have extension,
  4652. * BUFFER_ERROR when the extension's data is invalid,
  4653. * MEMORY_E when unable to allocate memory and
  4654. * 0 otherwise.
  4655. */
  4656. static int TLSX_EncryptThenMac_Parse(WOLFSSL* ssl, const byte* input,
  4657. word16 length, byte msgType)
  4658. {
  4659. int ret;
  4660. (void)input;
  4661. if (msgType != client_hello && msgType != server_hello) {
  4662. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4663. return SANITY_MSG_E;
  4664. }
  4665. /* Empty extension */
  4666. if (length != 0)
  4667. return BUFFER_ERROR;
  4668. if (msgType == client_hello) {
  4669. /* Check the user hasn't disallowed use of Encrypt-Then-Mac. */
  4670. if (!ssl->options.disallowEncThenMac) {
  4671. ssl->options.encThenMac = 1;
  4672. /* Set the extension reply. */
  4673. ret = TLSX_EncryptThenMac_Use(ssl);
  4674. if (ret != 0)
  4675. return ret;
  4676. }
  4677. return 0;
  4678. }
  4679. /* Server Hello */
  4680. if (ssl->options.disallowEncThenMac) {
  4681. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4682. return SANITY_MSG_E;
  4683. }
  4684. ssl->options.encThenMac = 1;
  4685. return 0;
  4686. }
  4687. /**
  4688. * Add the Encrypt-Then-MAC extension to list.
  4689. *
  4690. * ssl SSL object
  4691. * return MEMORY_E when unable to allocate memory and 0 otherwise.
  4692. */
  4693. static int TLSX_EncryptThenMac_Use(WOLFSSL* ssl)
  4694. {
  4695. int ret = 0;
  4696. TLSX* extension;
  4697. /* Find the Encrypt-Then-Mac extension if it exists. */
  4698. extension = TLSX_Find(ssl->extensions, TLSX_ENCRYPT_THEN_MAC);
  4699. if (extension == NULL) {
  4700. /* Push new Encrypt-Then-Mac extension. */
  4701. ret = TLSX_Push(&ssl->extensions, TLSX_ENCRYPT_THEN_MAC, NULL,
  4702. ssl->heap);
  4703. if (ret != 0)
  4704. return ret;
  4705. }
  4706. return 0;
  4707. }
  4708. /**
  4709. * Set the Encrypt-Then-MAC extension as one to respond too.
  4710. *
  4711. * ssl SSL object
  4712. * return EXT_MISSING when EncryptThenMac extension not in list.
  4713. */
  4714. int TLSX_EncryptThenMac_Respond(WOLFSSL* ssl)
  4715. {
  4716. TLSX* extension;
  4717. extension = TLSX_Find(ssl->extensions, TLSX_ENCRYPT_THEN_MAC);
  4718. if (extension == NULL)
  4719. return EXT_MISSING;
  4720. extension->resp = 1;
  4721. return 0;
  4722. }
  4723. #define ETM_GET_SIZE TLSX_EncryptThenMac_GetSize
  4724. #define ETM_WRITE TLSX_EncryptThenMac_Write
  4725. #define ETM_PARSE TLSX_EncryptThenMac_Parse
  4726. #else
  4727. #define ETM_GET_SIZE(a, b) 0
  4728. #define ETM_WRITE(a, b, c, d) 0
  4729. #define ETM_PARSE(a, b, c, d) 0
  4730. #endif /* !WOLFSSL_NO_TLS12 */
  4731. #endif /* HAVE_ENCRYPT_THEN_MAC && !WOLFSSL_AEAD_ONLY */
  4732. #ifdef WOLFSSL_SRTP
  4733. /******************************************************************************/
  4734. /* DTLS SRTP (Secure Real-time Transport Protocol) */
  4735. /******************************************************************************/
  4736. /* Only support single SRTP profile */
  4737. typedef struct TlsxSrtp {
  4738. word16 profileCount;
  4739. word16 ids; /* selected bits */
  4740. } TlsxSrtp;
  4741. static int TLSX_UseSRTP_GetSize(TlsxSrtp *srtp)
  4742. {
  4743. /* SRTP Profile Len (2)
  4744. * SRTP Profiles (2)
  4745. * MKI (master key id) Length */
  4746. return (OPAQUE16_LEN + (srtp->profileCount * OPAQUE16_LEN) + 1);
  4747. }
  4748. static TlsxSrtp* TLSX_UseSRTP_New(word16 ids, void* heap)
  4749. {
  4750. TlsxSrtp* srtp;
  4751. int i;
  4752. srtp = (TlsxSrtp*)XMALLOC(sizeof(TlsxSrtp), heap, DYNAMIC_TYPE_TLSX);
  4753. if (srtp == NULL) {
  4754. WOLFSSL_MSG("TLSX SRTP Memory failure");
  4755. return NULL;
  4756. }
  4757. /* count and test each bit set */
  4758. srtp->profileCount = 0;
  4759. for (i=0; i<16; i++) {
  4760. if (ids & (1 << i)) {
  4761. srtp->profileCount++;
  4762. }
  4763. }
  4764. srtp->ids = ids;
  4765. return srtp;
  4766. }
  4767. static void TLSX_UseSRTP_Free(TlsxSrtp *srtp, void* heap)
  4768. {
  4769. if (srtp != NULL) {
  4770. XFREE(srtp, heap, DYNAMIC_TYPE_TLSX);
  4771. }
  4772. (void)heap;
  4773. }
  4774. static int TLSX_UseSRTP_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  4775. byte isRequest)
  4776. {
  4777. int ret = BAD_FUNC_ARG;
  4778. word16 profile_len = 0;
  4779. word16 profile_value = 0;
  4780. word16 offset = 0;
  4781. #ifndef NO_WOLFSSL_SERVER
  4782. int i;
  4783. TlsxSrtp* srtp = NULL;
  4784. #endif
  4785. if (length < OPAQUE16_LEN) {
  4786. return BUFFER_ERROR;
  4787. }
  4788. /* reset selected DTLS SRTP profile ID */
  4789. ssl->dtlsSrtpId = 0;
  4790. /* total length, not include itself */
  4791. ato16(input, &profile_len);
  4792. offset += OPAQUE16_LEN;
  4793. if (!isRequest) {
  4794. #ifndef NO_WOLFSSL_CLIENT
  4795. if (length < offset + OPAQUE16_LEN)
  4796. return BUFFER_ERROR;
  4797. ato16(input + offset, &profile_value);
  4798. /* check that the profile received was in the ones we support */
  4799. if (profile_value < 16 &&
  4800. (ssl->dtlsSrtpProfiles & (1 << profile_value))) {
  4801. ssl->dtlsSrtpId = profile_value;
  4802. ret = 0; /* success */
  4803. }
  4804. #endif
  4805. }
  4806. #ifndef NO_WOLFSSL_SERVER
  4807. else {
  4808. /* parse remainder one profile at a time, looking for match in CTX */
  4809. ret = 0;
  4810. for (i=offset; i<length; i+=OPAQUE16_LEN) {
  4811. ato16(input+i, &profile_value);
  4812. /* find first match */
  4813. if (profile_value < 16 &&
  4814. ssl->dtlsSrtpProfiles & (1 << profile_value)) {
  4815. ssl->dtlsSrtpId = profile_value;
  4816. /* make sure we respond with selected SRTP id selected */
  4817. srtp = TLSX_UseSRTP_New((1 << profile_value), ssl->heap);
  4818. if (srtp != NULL) {
  4819. ret = TLSX_Push(&ssl->extensions, TLSX_USE_SRTP,
  4820. (void*)srtp, ssl->heap);
  4821. if (ret == 0) {
  4822. TLSX_SetResponse(ssl, TLSX_USE_SRTP);
  4823. /* successfully set extension */
  4824. }
  4825. }
  4826. else {
  4827. ret = MEMORY_E;
  4828. }
  4829. break;
  4830. }
  4831. }
  4832. }
  4833. if (ret == 0 && ssl->dtlsSrtpId == 0) {
  4834. WOLFSSL_MSG("TLSX_UseSRTP_Parse profile not found!");
  4835. /* not fatal */
  4836. }
  4837. else if (ret != 0) {
  4838. ssl->dtlsSrtpId = 0;
  4839. TLSX_UseSRTP_Free(srtp, ssl->heap);
  4840. }
  4841. #endif
  4842. (void)profile_len;
  4843. return ret;
  4844. }
  4845. static word16 TLSX_UseSRTP_Write(TlsxSrtp* srtp, byte* output)
  4846. {
  4847. word16 offset = 0;
  4848. int i, j;
  4849. c16toa(srtp->profileCount * 2, output + offset);
  4850. offset += OPAQUE16_LEN;
  4851. j = 0;
  4852. for (i = 0; i < srtp->profileCount; i++) {
  4853. for (; j < 16; j++) {
  4854. if (srtp->ids & (1 << j)) {
  4855. c16toa(j, output + offset);
  4856. offset += OPAQUE16_LEN;
  4857. }
  4858. }
  4859. }
  4860. output[offset++] = 0x00; /* MKI Length */
  4861. return offset;
  4862. }
  4863. static int TLSX_UseSRTP(TLSX** extensions, word16 profiles, void* heap)
  4864. {
  4865. int ret = 0;
  4866. TLSX* extension;
  4867. if (extensions == NULL) {
  4868. return BAD_FUNC_ARG;
  4869. }
  4870. extension = TLSX_Find(*extensions, TLSX_USE_SRTP);
  4871. if (extension == NULL) {
  4872. TlsxSrtp* srtp = TLSX_UseSRTP_New(profiles, heap);
  4873. if (srtp == NULL) {
  4874. return MEMORY_E;
  4875. }
  4876. ret = TLSX_Push(extensions, TLSX_USE_SRTP, (void*)srtp, heap);
  4877. if (ret != 0) {
  4878. TLSX_UseSRTP_Free(srtp, heap);
  4879. }
  4880. }
  4881. return ret;
  4882. }
  4883. #ifndef NO_WOLFSSL_SERVER
  4884. #define SRTP_FREE TLSX_UseSRTP_Free
  4885. #define SRTP_PARSE TLSX_UseSRTP_Parse
  4886. #define SRTP_WRITE TLSX_UseSRTP_Write
  4887. #define SRTP_GET_SIZE TLSX_UseSRTP_GetSize
  4888. #else
  4889. #define SRTP_FREE(a, b)
  4890. #define SRTP_PARSE(a, b, c, d) 0
  4891. #define SRTP_WRITE(a, b) 0
  4892. #define SRTP_GET_SIZE(a) 0
  4893. #endif
  4894. #endif /* WOLFSSL_SRTP */
  4895. /******************************************************************************/
  4896. /* Supported Versions */
  4897. /******************************************************************************/
  4898. #ifdef WOLFSSL_TLS13
  4899. static WC_INLINE int versionIsGreater(byte isDtls, byte a, byte b)
  4900. {
  4901. (void)isDtls;
  4902. #ifdef WOLFSSL_DTLS
  4903. /* DTLS version increases backwards (-1,-2,-3,etc) */
  4904. if (isDtls)
  4905. return a < b;
  4906. #endif /* WOLFSSL_DTLS */
  4907. return a > b;
  4908. }
  4909. static WC_INLINE int versionIsLesser(byte isDtls, byte a, byte b)
  4910. {
  4911. (void)isDtls;
  4912. #ifdef WOLFSSL_DTLS
  4913. /* DTLS version increases backwards (-1,-2,-3,etc) */
  4914. if (isDtls)
  4915. return a > b;
  4916. #endif /* WOLFSSL_DTLS */
  4917. return a < b;
  4918. }
  4919. static WC_INLINE int versionIsAtLeast(byte isDtls, byte a, byte b)
  4920. {
  4921. (void)isDtls;
  4922. #ifdef WOLFSSL_DTLS
  4923. /* DTLS version increases backwards (-1,-2,-3,etc) */
  4924. if (isDtls)
  4925. return a <= b;
  4926. #endif /* WOLFSSL_DTLS */
  4927. return a >= b;
  4928. }
  4929. static WC_INLINE int versionIsLessEqual(byte isDtls, byte a, byte b)
  4930. {
  4931. (void)isDtls;
  4932. #ifdef WOLFSSL_DTLS
  4933. /* DTLS version increases backwards (-1,-2,-3,etc) */
  4934. if (isDtls)
  4935. return a >= b;
  4936. #endif /* WOLFSSL_DTLS */
  4937. return a <= b;
  4938. }
  4939. /* Return the size of the SupportedVersions extension's data.
  4940. *
  4941. * data The SSL/TLS object.
  4942. * msgType The type of the message this extension is being written into.
  4943. * returns the length of data that will be in the extension.
  4944. */
  4945. static int TLSX_SupportedVersions_GetSize(void* data, byte msgType, word16* pSz)
  4946. {
  4947. WOLFSSL* ssl = (WOLFSSL*)data;
  4948. byte tls13Minor, tls12Minor, tls11Minor, isDtls;
  4949. isDtls = !!ssl->options.dtls;
  4950. tls13Minor = (byte)(isDtls ? DTLSv1_3_MINOR : TLSv1_3_MINOR);
  4951. tls12Minor = (byte)(isDtls ? DTLSv1_2_MINOR : TLSv1_2_MINOR);
  4952. tls11Minor = (byte)(isDtls ? DTLS_MINOR : TLSv1_1_MINOR);
  4953. /* unused on some configuration */
  4954. (void)tls12Minor;
  4955. (void)tls13Minor;
  4956. (void)tls11Minor;
  4957. if (msgType == client_hello) {
  4958. /* TLS v1.2 and TLS v1.3 */
  4959. int cnt = 0;
  4960. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls13Minor)
  4961. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4962. defined(WOLFSSL_WPAS_SMALL)
  4963. && (ssl->options.mask & SSL_OP_NO_TLSv1_3) == 0
  4964. #endif
  4965. ) {
  4966. cnt++;
  4967. }
  4968. if (ssl->options.downgrade) {
  4969. #ifndef WOLFSSL_NO_TLS12
  4970. if (versionIsLessEqual(
  4971. isDtls, ssl->options.minDowngrade, tls12Minor)
  4972. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4973. defined(WOLFSSL_WPAS_SMALL)
  4974. && (ssl->options.mask & SSL_OP_NO_TLSv1_2) == 0
  4975. #endif
  4976. ) {
  4977. cnt++;
  4978. }
  4979. #endif
  4980. #ifndef NO_OLD_TLS
  4981. if (versionIsLessEqual(
  4982. isDtls, ssl->options.minDowngrade, tls11Minor)
  4983. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4984. defined(WOLFSSL_WPAS_SMALL)
  4985. && (ssl->options.mask & SSL_OP_NO_TLSv1_1) == 0
  4986. #endif
  4987. ) {
  4988. cnt++;
  4989. }
  4990. #ifdef WOLFSSL_ALLOW_TLSV10
  4991. if (!ssl->options.dtls && (ssl->options.minDowngrade <= TLSv1_MINOR)
  4992. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4993. defined(WOLFSSL_WPAS_SMALL)
  4994. && (ssl->options.mask & SSL_OP_NO_TLSv1) == 0
  4995. #endif
  4996. ) {
  4997. cnt++;
  4998. }
  4999. #endif
  5000. #endif
  5001. }
  5002. *pSz += (word16)(OPAQUE8_LEN + cnt * OPAQUE16_LEN);
  5003. }
  5004. else if (msgType == server_hello || msgType == hello_retry_request) {
  5005. *pSz += OPAQUE16_LEN;
  5006. }
  5007. else {
  5008. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5009. return SANITY_MSG_E;
  5010. }
  5011. return 0;
  5012. }
  5013. /* Writes the SupportedVersions extension into the buffer.
  5014. *
  5015. * data The SSL/TLS object.
  5016. * output The buffer to write the extension into.
  5017. * msgType The type of the message this extension is being written into.
  5018. * returns the length of data that was written.
  5019. */
  5020. static int TLSX_SupportedVersions_Write(void* data, byte* output,
  5021. byte msgType, word16* pSz)
  5022. {
  5023. WOLFSSL* ssl = (WOLFSSL*)data;
  5024. byte tls13minor, tls12minor, tls11minor, isDtls = 0;
  5025. tls13minor = (byte)TLSv1_3_MINOR;
  5026. tls12minor = (byte)TLSv1_2_MINOR;
  5027. tls11minor = (byte)TLSv1_1_MINOR;
  5028. /* unused in some configuration */
  5029. (void)tls11minor;
  5030. (void)tls12minor;
  5031. #ifdef WOLFSSL_DTLS13
  5032. if (ssl->options.dtls) {
  5033. tls13minor = (byte)DTLSv1_3_MINOR;
  5034. tls12minor = (byte)DTLSv1_2_MINOR;
  5035. tls11minor = (byte)DTLS_MINOR;
  5036. isDtls = 1;
  5037. }
  5038. #endif /* WOLFSSL_DTLS13 */
  5039. if (msgType == client_hello) {
  5040. byte major = ssl->ctx->method->version.major;
  5041. byte* cnt = output++;
  5042. *cnt = 0;
  5043. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls13minor)
  5044. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  5045. defined(WOLFSSL_WPAS_SMALL)
  5046. && (ssl->options.mask & SSL_OP_NO_TLSv1_3) == 0
  5047. #endif
  5048. ) {
  5049. *cnt += OPAQUE16_LEN;
  5050. #ifdef WOLFSSL_TLS13_DRAFT
  5051. /* The TLS draft major number. */
  5052. *(output++) = TLS_DRAFT_MAJOR;
  5053. /* Version of draft supported. */
  5054. *(output++) = TLS_DRAFT_MINOR;
  5055. #else
  5056. *(output++) = major;
  5057. *(output++) = tls13minor;
  5058. #endif
  5059. }
  5060. if (ssl->options.downgrade) {
  5061. #ifndef WOLFSSL_NO_TLS12
  5062. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls12minor)
  5063. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  5064. defined(WOLFSSL_WPAS_SMALL)
  5065. && (ssl->options.mask & SSL_OP_NO_TLSv1_2) == 0
  5066. #endif
  5067. ) {
  5068. *cnt += OPAQUE16_LEN;
  5069. *(output++) = major;
  5070. *(output++) = tls12minor;
  5071. }
  5072. #endif
  5073. #ifndef NO_OLD_TLS
  5074. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls11minor)
  5075. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  5076. defined(WOLFSSL_WPAS_SMALL)
  5077. && (ssl->options.mask & SSL_OP_NO_TLSv1_1) == 0
  5078. #endif
  5079. ) {
  5080. *cnt += OPAQUE16_LEN;
  5081. *(output++) = major;
  5082. *(output++) = tls11minor;
  5083. }
  5084. #ifdef WOLFSSL_ALLOW_TLSV10
  5085. if (!ssl->options.dtls && (ssl->options.minDowngrade <= TLSv1_MINOR)
  5086. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  5087. defined(WOLFSSL_WPAS_SMALL)
  5088. && (ssl->options.mask & SSL_OP_NO_TLSv1) == 0
  5089. #endif
  5090. ) {
  5091. *cnt += OPAQUE16_LEN;
  5092. *(output++) = major;
  5093. *(output++) = (byte)TLSv1_MINOR;
  5094. }
  5095. #endif
  5096. #endif
  5097. }
  5098. *pSz += (word16)(OPAQUE8_LEN + *cnt);
  5099. }
  5100. else if (msgType == server_hello || msgType == hello_retry_request) {
  5101. output[0] = ssl->version.major;
  5102. output[1] = ssl->version.minor;
  5103. *pSz += OPAQUE16_LEN;
  5104. }
  5105. else {
  5106. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5107. return SANITY_MSG_E;
  5108. }
  5109. return 0;
  5110. }
  5111. /* Parse the SupportedVersions extension.
  5112. *
  5113. * ssl The SSL/TLS object.
  5114. * input The buffer with the extension data.
  5115. * length The length of the extension data.
  5116. * msgType The type of the message this extension is being parsed from.
  5117. * pv The output ProtocolVersion for the negotiated version
  5118. * opts The output options structure. Can be NULL.
  5119. * exts The output extensions list. Can be NULL.
  5120. * returns 0 on success, otherwise failure.
  5121. */
  5122. int TLSX_SupportedVersions_Parse(const WOLFSSL* ssl, const byte* input,
  5123. word16 length, byte msgType, ProtocolVersion* pv, Options* opts,
  5124. TLSX** exts)
  5125. {
  5126. /* The client's greatest minor version that we support */
  5127. byte clientGreatestMinor = SSLv3_MINOR;
  5128. int ret;
  5129. byte major, minor;
  5130. byte tls13minor, tls12minor;
  5131. byte isDtls;
  5132. tls13minor = TLSv1_3_MINOR;
  5133. tls12minor = TLSv1_2_MINOR;
  5134. isDtls = ssl->options.dtls == 1;
  5135. #ifdef WOLFSSL_DTLS13
  5136. if (ssl->options.dtls) {
  5137. tls13minor = DTLSv1_3_MINOR;
  5138. tls12minor = DTLSv1_2_MINOR;
  5139. clientGreatestMinor = DTLS_MINOR;
  5140. }
  5141. #endif /* WOLFSSL_DTLS13 */
  5142. if (msgType == client_hello) {
  5143. int i;
  5144. int len;
  5145. int set = 0;
  5146. /* Must contain a length and at least one version. */
  5147. if (length < OPAQUE8_LEN + OPAQUE16_LEN || (length & 1) != 1)
  5148. return BUFFER_ERROR;
  5149. len = *input;
  5150. /* Protocol version array must fill rest of data. */
  5151. if (length != (word16)OPAQUE8_LEN + len)
  5152. return BUFFER_ERROR;
  5153. input++;
  5154. /* Find first match. */
  5155. for (i = 0; i < len; i += OPAQUE16_LEN) {
  5156. major = input[i];
  5157. minor = input[i + OPAQUE8_LEN];
  5158. #ifdef WOLFSSL_TLS13_DRAFT
  5159. if (major == TLS_DRAFT_MAJOR && minor == TLS_DRAFT_MINOR) {
  5160. major = SSLv3_MAJOR;
  5161. minor = TLSv1_3_MINOR;
  5162. }
  5163. #else
  5164. if (major == TLS_DRAFT_MAJOR)
  5165. continue;
  5166. #endif
  5167. if (major != ssl->ctx->method->version.major)
  5168. continue;
  5169. /* No upgrade allowed. */
  5170. if (versionIsGreater(isDtls, minor, ssl->version.minor))
  5171. continue;
  5172. /* Check downgrade. */
  5173. if (versionIsLesser(isDtls, minor, ssl->version.minor)) {
  5174. if (!ssl->options.downgrade)
  5175. continue;
  5176. if (versionIsLesser(isDtls, minor, ssl->options.minDowngrade))
  5177. continue;
  5178. }
  5179. if (versionIsGreater(isDtls, minor, clientGreatestMinor))
  5180. clientGreatestMinor = minor;
  5181. set = 1;
  5182. }
  5183. if (!set) {
  5184. /* No common supported version was negotiated */
  5185. SendAlert((WOLFSSL*)ssl, alert_fatal,
  5186. wolfssl_alert_protocol_version);
  5187. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5188. return VERSION_ERROR;
  5189. }
  5190. pv->minor = clientGreatestMinor;
  5191. if (versionIsAtLeast(isDtls, clientGreatestMinor, tls13minor)) {
  5192. if (opts != NULL)
  5193. opts->tls1_3 = 1;
  5194. /* TLS v1.3 requires supported version extension */
  5195. if (exts != NULL &&
  5196. TLSX_Find(*exts, TLSX_SUPPORTED_VERSIONS) == NULL) {
  5197. ret = TLSX_Push(exts,
  5198. TLSX_SUPPORTED_VERSIONS, ssl, ssl->heap);
  5199. if (ret != 0) {
  5200. return ret;
  5201. }
  5202. /* *exts should be pointing to the TLSX_SUPPORTED_VERSIONS
  5203. * ext in the list since it was pushed. */
  5204. (*exts)->resp = 1;
  5205. }
  5206. }
  5207. }
  5208. else if (msgType == server_hello || msgType == hello_retry_request) {
  5209. /* Must contain one version. */
  5210. if (length != OPAQUE16_LEN)
  5211. return BUFFER_ERROR;
  5212. major = input[0];
  5213. minor = input[OPAQUE8_LEN];
  5214. if (major != ssl->ctx->method->version.major) {
  5215. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5216. return VERSION_ERROR;
  5217. }
  5218. /* Can't downgrade with this extension below TLS v1.3. */
  5219. if (versionIsLesser(isDtls, minor, tls13minor)) {
  5220. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5221. return VERSION_ERROR;
  5222. }
  5223. /* Version is TLS v1.2 to handle downgrading from TLS v1.3+. */
  5224. if (ssl->options.downgrade && ssl->version.minor == tls12minor) {
  5225. /* Set minor version back to TLS v1.3+ */
  5226. pv->minor = ssl->ctx->method->version.minor;
  5227. }
  5228. /* No upgrade allowed. */
  5229. if (versionIsLesser(isDtls, ssl->version.minor, minor)) {
  5230. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5231. return VERSION_ERROR;
  5232. }
  5233. /* Check downgrade. */
  5234. if (versionIsGreater(isDtls, ssl->version.minor, minor)) {
  5235. if (!ssl->options.downgrade) {
  5236. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5237. return VERSION_ERROR;
  5238. }
  5239. if (versionIsLesser(
  5240. isDtls, minor, ssl->options.minDowngrade)) {
  5241. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5242. return VERSION_ERROR;
  5243. }
  5244. /* Downgrade the version. */
  5245. pv->minor = minor;
  5246. }
  5247. }
  5248. else {
  5249. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5250. return SANITY_MSG_E;
  5251. }
  5252. return 0;
  5253. }
  5254. /* Sets a new SupportedVersions extension into the extension list.
  5255. *
  5256. * extensions The list of extensions.
  5257. * data The extensions specific data.
  5258. * heap The heap used for allocation.
  5259. * returns 0 on success, otherwise failure.
  5260. */
  5261. static int TLSX_SetSupportedVersions(TLSX** extensions, const void* data,
  5262. void* heap)
  5263. {
  5264. if (extensions == NULL || data == NULL)
  5265. return BAD_FUNC_ARG;
  5266. return TLSX_Push(extensions, TLSX_SUPPORTED_VERSIONS, data, heap);
  5267. }
  5268. #define SV_GET_SIZE TLSX_SupportedVersions_GetSize
  5269. #define SV_WRITE TLSX_SupportedVersions_Write
  5270. #define SV_PARSE TLSX_SupportedVersions_Parse
  5271. #else
  5272. #define SV_GET_SIZE(a, b, c) 0
  5273. #define SV_WRITE(a, b, c, d) 0
  5274. #define SV_PARSE(a, b, c, d, e, f, g) 0
  5275. #endif /* WOLFSSL_TLS13 */
  5276. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  5277. /******************************************************************************/
  5278. /* Cookie */
  5279. /******************************************************************************/
  5280. /* Free the cookie data.
  5281. *
  5282. * cookie Cookie data.
  5283. * heap The heap used for allocation.
  5284. */
  5285. static void TLSX_Cookie_FreeAll(Cookie* cookie, void* heap)
  5286. {
  5287. (void)heap;
  5288. if (cookie != NULL)
  5289. XFREE(cookie, heap, DYNAMIC_TYPE_TLSX);
  5290. }
  5291. /* Get the size of the encoded Cookie extension.
  5292. * In messages: ClientHello and HelloRetryRequest.
  5293. *
  5294. * cookie The cookie to write.
  5295. * msgType The type of the message this extension is being written into.
  5296. * returns the number of bytes of the encoded Cookie extension.
  5297. */
  5298. static int TLSX_Cookie_GetSize(Cookie* cookie, byte msgType, word16* pSz)
  5299. {
  5300. if (msgType == client_hello || msgType == hello_retry_request) {
  5301. *pSz += OPAQUE16_LEN + cookie->len;
  5302. }
  5303. else {
  5304. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5305. return SANITY_MSG_E;
  5306. }
  5307. return 0;
  5308. }
  5309. /* Writes the Cookie extension into the output buffer.
  5310. * Assumes that the the output buffer is big enough to hold data.
  5311. * In messages: ClientHello and HelloRetryRequest.
  5312. *
  5313. * cookie The cookie to write.
  5314. * output The buffer to write into.
  5315. * msgType The type of the message this extension is being written into.
  5316. * returns the number of bytes written into the buffer.
  5317. */
  5318. static int TLSX_Cookie_Write(Cookie* cookie, byte* output, byte msgType,
  5319. word16* pSz)
  5320. {
  5321. if (msgType == client_hello || msgType == hello_retry_request) {
  5322. c16toa(cookie->len, output);
  5323. output += OPAQUE16_LEN;
  5324. XMEMCPY(output, cookie->data, cookie->len);
  5325. *pSz += OPAQUE16_LEN + cookie->len;
  5326. }
  5327. else {
  5328. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5329. return SANITY_MSG_E;
  5330. }
  5331. return 0;
  5332. }
  5333. /* Parse the Cookie extension.
  5334. * In messages: ClientHello and HelloRetryRequest.
  5335. *
  5336. * ssl The SSL/TLS object.
  5337. * input The extension data.
  5338. * length The length of the extension data.
  5339. * msgType The type of the message this extension is being parsed from.
  5340. * returns 0 on success and other values indicate failure.
  5341. */
  5342. static int TLSX_Cookie_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  5343. byte msgType)
  5344. {
  5345. word16 len;
  5346. word16 idx = 0;
  5347. TLSX* extension;
  5348. Cookie* cookie;
  5349. if (msgType != client_hello && msgType != hello_retry_request) {
  5350. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5351. return SANITY_MSG_E;
  5352. }
  5353. /* Message contains length and Cookie which must be at least one byte
  5354. * in length.
  5355. */
  5356. if (length < OPAQUE16_LEN + 1)
  5357. return BUFFER_E;
  5358. ato16(input + idx, &len);
  5359. idx += OPAQUE16_LEN;
  5360. if (length - idx != len)
  5361. return BUFFER_E;
  5362. if (msgType == hello_retry_request)
  5363. return TLSX_Cookie_Use(ssl, input + idx, len, NULL, 0, 0,
  5364. &ssl->extensions);
  5365. /* client_hello */
  5366. extension = TLSX_Find(ssl->extensions, TLSX_COOKIE);
  5367. if (extension == NULL) {
  5368. #ifdef WOLFSSL_DTLS13
  5369. if (ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version))
  5370. /* Allow a cookie extension with DTLS 1.3 because it is possible
  5371. * that a different SSL instance sent the cookie but we are now
  5372. * receiving it. */
  5373. return TLSX_Cookie_Use(ssl, input + idx, len, NULL, 0, 0,
  5374. &ssl->extensions);
  5375. else
  5376. #endif
  5377. {
  5378. WOLFSSL_ERROR_VERBOSE(HRR_COOKIE_ERROR);
  5379. return HRR_COOKIE_ERROR;
  5380. }
  5381. }
  5382. cookie = (Cookie*)extension->data;
  5383. if (cookie->len != len || XMEMCMP(cookie->data, input + idx, len) != 0) {
  5384. WOLFSSL_ERROR_VERBOSE(HRR_COOKIE_ERROR);
  5385. return HRR_COOKIE_ERROR;
  5386. }
  5387. /* Request seen. */
  5388. extension->resp = 0;
  5389. return 0;
  5390. }
  5391. /* Use the data to create a new Cookie object in the extensions.
  5392. *
  5393. * ssl SSL/TLS object.
  5394. * data Cookie data.
  5395. * len Length of cookie data in bytes.
  5396. * mac MAC data.
  5397. * macSz Length of MAC data in bytes.
  5398. * resp Indicates the extension will go into a response (HelloRetryRequest).
  5399. * returns 0 on success and other values indicate failure.
  5400. */
  5401. int TLSX_Cookie_Use(const WOLFSSL* ssl, const byte* data, word16 len, byte* mac,
  5402. byte macSz, int resp, TLSX** exts)
  5403. {
  5404. int ret = 0;
  5405. TLSX* extension;
  5406. Cookie* cookie;
  5407. /* Find the cookie extension if it exists. */
  5408. extension = TLSX_Find(*exts, TLSX_COOKIE);
  5409. if (extension == NULL) {
  5410. /* Push new cookie extension. */
  5411. ret = TLSX_Push(exts, TLSX_COOKIE, NULL, ssl->heap);
  5412. if (ret != 0)
  5413. return ret;
  5414. extension = TLSX_Find(*exts, TLSX_COOKIE);
  5415. if (extension == NULL)
  5416. return MEMORY_E;
  5417. }
  5418. cookie = (Cookie*)XMALLOC(sizeof(Cookie) + len + macSz, ssl->heap,
  5419. DYNAMIC_TYPE_TLSX);
  5420. if (cookie == NULL)
  5421. return MEMORY_E;
  5422. cookie->len = len + macSz;
  5423. XMEMCPY(cookie->data, data, len);
  5424. if (mac != NULL)
  5425. XMEMCPY(cookie->data + len, mac, macSz);
  5426. if (extension->data != NULL)
  5427. XFREE(extension->data, ssl->heap, DYNAMIC_TYPE_TLSX);
  5428. extension->data = (void*)cookie;
  5429. extension->resp = (byte)resp;
  5430. return 0;
  5431. }
  5432. #define CKE_FREE_ALL TLSX_Cookie_FreeAll
  5433. #define CKE_GET_SIZE TLSX_Cookie_GetSize
  5434. #define CKE_WRITE TLSX_Cookie_Write
  5435. #define CKE_PARSE TLSX_Cookie_Parse
  5436. #else
  5437. #define CKE_FREE_ALL(a, b) 0
  5438. #define CKE_GET_SIZE(a, b, c) 0
  5439. #define CKE_WRITE(a, b, c, d) 0
  5440. #define CKE_PARSE(a, b, c, d) 0
  5441. #endif
  5442. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  5443. /******************************************************************************/
  5444. /* Signature Algorithms */
  5445. /******************************************************************************/
  5446. /* Return the size of the SignatureAlgorithms extension's data.
  5447. *
  5448. * data Unused
  5449. * returns the length of data that will be in the extension.
  5450. */
  5451. static word16 TLSX_SignatureAlgorithms_GetSize(void* data)
  5452. {
  5453. SignatureAlgorithms* sa = (SignatureAlgorithms*)data;
  5454. if (sa->hashSigAlgoSz == 0)
  5455. return OPAQUE16_LEN + WOLFSSL_SUITES(sa->ssl)->hashSigAlgoSz;
  5456. else
  5457. return OPAQUE16_LEN + sa->hashSigAlgoSz;
  5458. }
  5459. /* Creates a bit string of supported hash algorithms with RSA PSS.
  5460. * The bit string is used when determining which signature algorithm to use
  5461. * when creating the CertificateVerify message.
  5462. * Note: Valid data has an even length as each signature algorithm is two bytes.
  5463. *
  5464. * ssl The SSL/TLS object.
  5465. * input The buffer with the list of supported signature algorithms.
  5466. * length The length of the list in bytes.
  5467. * returns 0 on success, BUFFER_ERROR when the length is not even.
  5468. */
  5469. static int TLSX_SignatureAlgorithms_MapPss(WOLFSSL *ssl, const byte* input,
  5470. word16 length)
  5471. {
  5472. word16 i;
  5473. if ((length & 1) == 1)
  5474. return BUFFER_ERROR;
  5475. ssl->pssAlgo = 0;
  5476. for (i = 0; i < length; i += 2) {
  5477. if (input[i] == rsa_pss_sa_algo && input[i + 1] <= sha512_mac)
  5478. ssl->pssAlgo |= 1 << input[i + 1];
  5479. #ifdef WOLFSSL_TLS13
  5480. if (input[i] == rsa_pss_sa_algo && input[i + 1] >= pss_sha256 &&
  5481. input[i + 1] <= pss_sha512) {
  5482. ssl->pssAlgo |= 1 << input[i + 1];
  5483. }
  5484. #endif
  5485. }
  5486. return 0;
  5487. }
  5488. /* Writes the SignatureAlgorithms extension into the buffer.
  5489. *
  5490. * data Unused
  5491. * output The buffer to write the extension into.
  5492. * returns the length of data that was written.
  5493. */
  5494. static word16 TLSX_SignatureAlgorithms_Write(void* data, byte* output)
  5495. {
  5496. SignatureAlgorithms* sa = (SignatureAlgorithms*)data;
  5497. const Suites* suites = WOLFSSL_SUITES(sa->ssl);
  5498. word16 hashSigAlgoSz;
  5499. if (sa->hashSigAlgoSz == 0) {
  5500. c16toa(suites->hashSigAlgoSz, output);
  5501. XMEMCPY(output + OPAQUE16_LEN, suites->hashSigAlgo,
  5502. suites->hashSigAlgoSz);
  5503. hashSigAlgoSz = suites->hashSigAlgoSz;
  5504. }
  5505. else {
  5506. c16toa(sa->hashSigAlgoSz, output);
  5507. XMEMCPY(output + OPAQUE16_LEN, sa->hashSigAlgo,
  5508. sa->hashSigAlgoSz);
  5509. hashSigAlgoSz = sa->hashSigAlgoSz;
  5510. }
  5511. #ifndef NO_RSA
  5512. TLSX_SignatureAlgorithms_MapPss(sa->ssl, output + OPAQUE16_LEN,
  5513. hashSigAlgoSz);
  5514. #endif
  5515. return OPAQUE16_LEN + hashSigAlgoSz;
  5516. }
  5517. /* Parse the SignatureAlgorithms extension.
  5518. *
  5519. * ssl The SSL/TLS object.
  5520. * input The buffer with the extension data.
  5521. * length The length of the extension data.
  5522. * returns 0 on success, otherwise failure.
  5523. */
  5524. static int TLSX_SignatureAlgorithms_Parse(WOLFSSL *ssl, const byte* input,
  5525. word16 length, byte isRequest, Suites* suites)
  5526. {
  5527. word16 len;
  5528. if (!isRequest)
  5529. return BUFFER_ERROR;
  5530. /* Must contain a length and at least algorithm. */
  5531. if (length < OPAQUE16_LEN + OPAQUE16_LEN || (length & 1) != 0)
  5532. return BUFFER_ERROR;
  5533. ato16(input, &len);
  5534. input += OPAQUE16_LEN;
  5535. /* Algorithm array must fill rest of data. */
  5536. if (length != OPAQUE16_LEN + len)
  5537. return BUFFER_ERROR;
  5538. /* Sig Algo list size must be even. */
  5539. if (suites->hashSigAlgoSz % 2 != 0)
  5540. return BUFFER_ERROR;
  5541. /* truncate hashSigAlgo list if too long */
  5542. suites->hashSigAlgoSz = len;
  5543. if (suites->hashSigAlgoSz > WOLFSSL_MAX_SIGALGO) {
  5544. WOLFSSL_MSG("TLSX SigAlgo list exceeds max, truncating");
  5545. suites->hashSigAlgoSz = WOLFSSL_MAX_SIGALGO;
  5546. }
  5547. XMEMCPY(suites->hashSigAlgo, input, suites->hashSigAlgoSz);
  5548. return TLSX_SignatureAlgorithms_MapPss(ssl, input, len);
  5549. }
  5550. /* Sets a new SignatureAlgorithms extension into the extension list.
  5551. *
  5552. * extensions The list of extensions.
  5553. * data The extensions specific data.
  5554. * heap The heap used for allocation.
  5555. * returns 0 on success, otherwise failure.
  5556. */
  5557. static int TLSX_SetSignatureAlgorithms(TLSX** extensions, WOLFSSL* ssl,
  5558. void* heap)
  5559. {
  5560. SignatureAlgorithms* sa;
  5561. int ret;
  5562. if (extensions == NULL)
  5563. return BAD_FUNC_ARG;
  5564. /* Already present */
  5565. if (TLSX_Find(*extensions, TLSX_SIGNATURE_ALGORITHMS) != NULL)
  5566. return 0;
  5567. sa = TLSX_SignatureAlgorithms_New(ssl, 0, heap);
  5568. if (sa == NULL)
  5569. return MEMORY_ERROR;
  5570. ret = TLSX_Push(extensions, TLSX_SIGNATURE_ALGORITHMS, sa, heap);
  5571. if (ret != 0)
  5572. TLSX_SignatureAlgorithms_FreeAll(sa, heap);
  5573. return ret;
  5574. }
  5575. SignatureAlgorithms* TLSX_SignatureAlgorithms_New(WOLFSSL* ssl,
  5576. word16 hashSigAlgoSz, void* heap)
  5577. {
  5578. SignatureAlgorithms* sa;
  5579. (void)heap;
  5580. sa = (SignatureAlgorithms*)XMALLOC(sizeof(*sa) + hashSigAlgoSz, heap,
  5581. DYNAMIC_TYPE_TLSX);
  5582. if (sa != NULL) {
  5583. XMEMSET(sa, 0, sizeof(*sa) + hashSigAlgoSz);
  5584. sa->ssl = ssl;
  5585. sa->hashSigAlgoSz = hashSigAlgoSz;
  5586. }
  5587. return sa;
  5588. }
  5589. void TLSX_SignatureAlgorithms_FreeAll(SignatureAlgorithms* sa,
  5590. void* heap)
  5591. {
  5592. XFREE(sa, heap, DYNAMIC_TYPE_TLSX);
  5593. (void)heap;
  5594. }
  5595. #define SA_GET_SIZE TLSX_SignatureAlgorithms_GetSize
  5596. #define SA_WRITE TLSX_SignatureAlgorithms_Write
  5597. #define SA_PARSE TLSX_SignatureAlgorithms_Parse
  5598. #define SA_FREE_ALL TLSX_SignatureAlgorithms_FreeAll
  5599. #endif
  5600. /******************************************************************************/
  5601. /* Signature Algorithms Certificate */
  5602. /******************************************************************************/
  5603. #if defined(WOLFSSL_TLS13) && !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  5604. /* Return the size of the SignatureAlgorithms extension's data.
  5605. *
  5606. * data Unused
  5607. * returns the length of data that will be in the extension.
  5608. */
  5609. static word16 TLSX_SignatureAlgorithmsCert_GetSize(void* data)
  5610. {
  5611. WOLFSSL* ssl = (WOLFSSL*)data;
  5612. return OPAQUE16_LEN + ssl->certHashSigAlgoSz;
  5613. }
  5614. /* Writes the SignatureAlgorithmsCert extension into the buffer.
  5615. *
  5616. * data Unused
  5617. * output The buffer to write the extension into.
  5618. * returns the length of data that was written.
  5619. */
  5620. static word16 TLSX_SignatureAlgorithmsCert_Write(void* data, byte* output)
  5621. {
  5622. WOLFSSL* ssl = (WOLFSSL*)data;
  5623. c16toa(ssl->certHashSigAlgoSz, output);
  5624. XMEMCPY(output + OPAQUE16_LEN, ssl->certHashSigAlgo,
  5625. ssl->certHashSigAlgoSz);
  5626. return OPAQUE16_LEN + ssl->certHashSigAlgoSz;
  5627. }
  5628. /* Parse the SignatureAlgorithmsCert extension.
  5629. *
  5630. * ssl The SSL/TLS object.
  5631. * input The buffer with the extension data.
  5632. * length The length of the extension data.
  5633. * returns 0 on success, otherwise failure.
  5634. */
  5635. static int TLSX_SignatureAlgorithmsCert_Parse(WOLFSSL *ssl, const byte* input,
  5636. word16 length, byte isRequest)
  5637. {
  5638. word16 len;
  5639. if (!isRequest)
  5640. return BUFFER_ERROR;
  5641. /* Must contain a length and at least algorithm. */
  5642. if (length < OPAQUE16_LEN + OPAQUE16_LEN || (length & 1) != 0)
  5643. return BUFFER_ERROR;
  5644. ato16(input, &len);
  5645. input += OPAQUE16_LEN;
  5646. /* Algorithm array must fill rest of data. */
  5647. if (length != OPAQUE16_LEN + len)
  5648. return BUFFER_ERROR;
  5649. /* truncate hashSigAlgo list if too long */
  5650. ssl->certHashSigAlgoSz = len;
  5651. if (ssl->certHashSigAlgoSz > WOLFSSL_MAX_SIGALGO) {
  5652. WOLFSSL_MSG("TLSX SigAlgo list exceeds max, truncating");
  5653. ssl->certHashSigAlgoSz = WOLFSSL_MAX_SIGALGO;
  5654. }
  5655. XMEMCPY(ssl->certHashSigAlgo, input, ssl->certHashSigAlgoSz);
  5656. return 0;
  5657. }
  5658. /* Sets a new SignatureAlgorithmsCert extension into the extension list.
  5659. *
  5660. * extensions The list of extensions.
  5661. * data The extensions specific data.
  5662. * heap The heap used for allocation.
  5663. * returns 0 on success, otherwise failure.
  5664. */
  5665. static int TLSX_SetSignatureAlgorithmsCert(TLSX** extensions,
  5666. const WOLFSSL* data, void* heap)
  5667. {
  5668. if (extensions == NULL)
  5669. return BAD_FUNC_ARG;
  5670. return TLSX_Push(extensions, TLSX_SIGNATURE_ALGORITHMS_CERT, data, heap);
  5671. }
  5672. #define SAC_GET_SIZE TLSX_SignatureAlgorithmsCert_GetSize
  5673. #define SAC_WRITE TLSX_SignatureAlgorithmsCert_Write
  5674. #define SAC_PARSE TLSX_SignatureAlgorithmsCert_Parse
  5675. #endif /* WOLFSSL_TLS13 */
  5676. /******************************************************************************/
  5677. /* Key Share */
  5678. /******************************************************************************/
  5679. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  5680. /* Create a key share entry using named Diffie-Hellman parameters group.
  5681. * Generates a key pair.
  5682. *
  5683. * ssl The SSL/TLS object.
  5684. * kse The key share entry object.
  5685. * returns 0 on success, otherwise failure.
  5686. */
  5687. static int TLSX_KeyShare_GenDhKey(WOLFSSL *ssl, KeyShareEntry* kse)
  5688. {
  5689. int ret = 0;
  5690. #if !defined(NO_DH) && (!defined(NO_CERTS) || !defined(NO_PSK))
  5691. word32 pSz = 0, pvtSz = 0;
  5692. DhKey* dhKey = (DhKey*)kse->key;
  5693. /* Pick the parameters from the named group. */
  5694. #ifdef HAVE_PUBLIC_FFDHE
  5695. const DhParams* params = NULL;
  5696. switch (kse->group) {
  5697. #ifdef HAVE_FFDHE_2048
  5698. case WOLFSSL_FFDHE_2048:
  5699. params = wc_Dh_ffdhe2048_Get();
  5700. pvtSz = 29;
  5701. break;
  5702. #endif
  5703. #ifdef HAVE_FFDHE_3072
  5704. case WOLFSSL_FFDHE_3072:
  5705. params = wc_Dh_ffdhe3072_Get();
  5706. pvtSz = 34;
  5707. break;
  5708. #endif
  5709. #ifdef HAVE_FFDHE_4096
  5710. case WOLFSSL_FFDHE_4096:
  5711. params = wc_Dh_ffdhe4096_Get();
  5712. pvtSz = 39;
  5713. break;
  5714. #endif
  5715. #ifdef HAVE_FFDHE_6144
  5716. case WOLFSSL_FFDHE_6144:
  5717. params = wc_Dh_ffdhe6144_Get();
  5718. pvtSz = 46;
  5719. break;
  5720. #endif
  5721. #ifdef HAVE_FFDHE_8192
  5722. case WOLFSSL_FFDHE_8192:
  5723. params = wc_Dh_ffdhe8192_Get();
  5724. pvtSz = 52;
  5725. break;
  5726. #endif
  5727. default:
  5728. break;
  5729. }
  5730. if (params == NULL)
  5731. return BAD_FUNC_ARG;
  5732. pSz = params->p_len;
  5733. #else
  5734. pvtSz = wc_DhGetNamedKeyMinSize(kse->group);
  5735. if (pvtSz == 0) {
  5736. return BAD_FUNC_ARG;
  5737. }
  5738. ret = wc_DhGetNamedKeyParamSize(kse->group, &pSz, NULL, NULL);
  5739. if (ret != 0) {
  5740. return BAD_FUNC_ARG;
  5741. }
  5742. #endif
  5743. /* Trigger Key Generation */
  5744. if (kse->pubKey == NULL || kse->privKey == NULL) {
  5745. if (kse->key == NULL) {
  5746. kse->key = (DhKey*)XMALLOC(sizeof(DhKey), ssl->heap,
  5747. DYNAMIC_TYPE_DH);
  5748. if (kse->key == NULL)
  5749. return MEMORY_E;
  5750. /* Setup Key */
  5751. ret = wc_InitDhKey_ex((DhKey*)kse->key, ssl->heap, ssl->devId);
  5752. if (ret == 0) {
  5753. dhKey = (DhKey*)kse->key;
  5754. #ifdef HAVE_PUBLIC_FFDHE
  5755. ret = wc_DhSetKey(dhKey, params->p, params->p_len, params->g,
  5756. params->g_len);
  5757. #else
  5758. ret = wc_DhSetNamedKey(dhKey, kse->group);
  5759. #endif
  5760. }
  5761. }
  5762. /* Allocate space for the private and public key */
  5763. if (ret == 0 && kse->pubKey == NULL) {
  5764. kse->pubKey = (byte*)XMALLOC(pSz, ssl->heap,
  5765. DYNAMIC_TYPE_PUBLIC_KEY);
  5766. if (kse->pubKey == NULL)
  5767. ret = MEMORY_E;
  5768. }
  5769. if (ret == 0 && kse->privKey == NULL) {
  5770. kse->privKey = (byte*)XMALLOC(pvtSz, ssl->heap,
  5771. DYNAMIC_TYPE_PRIVATE_KEY);
  5772. if (kse->privKey == NULL)
  5773. ret = MEMORY_E;
  5774. }
  5775. if (ret == 0) {
  5776. #if defined(WOLFSSL_STATIC_EPHEMERAL) && defined(WOLFSSL_DH_EXTRA)
  5777. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_DH, kse->key);
  5778. kse->pubKeyLen = pSz;
  5779. kse->keyLen = pvtSz;
  5780. if (ret == 0) {
  5781. ret = wc_DhExportKeyPair(dhKey,
  5782. (byte*)kse->privKey, &kse->keyLen, /* private */
  5783. kse->pubKey, &kse->pubKeyLen /* public */
  5784. );
  5785. }
  5786. else
  5787. #endif
  5788. {
  5789. /* Generate a new key pair */
  5790. /* For async this is called once and when event is done, the
  5791. * provided buffers will be populated.
  5792. * Final processing is zero pad below. */
  5793. kse->pubKeyLen = pSz;
  5794. kse->keyLen = pvtSz;
  5795. ret = DhGenKeyPair(ssl, dhKey,
  5796. (byte*)kse->privKey, &kse->keyLen, /* private */
  5797. kse->pubKey, &kse->pubKeyLen /* public */
  5798. );
  5799. #ifdef WOLFSSL_ASYNC_CRYPT
  5800. if (ret == WC_PENDING_E) {
  5801. return ret;
  5802. }
  5803. #endif
  5804. }
  5805. }
  5806. }
  5807. if (ret == 0) {
  5808. if (pSz != kse->pubKeyLen) {
  5809. /* Zero pad the front of the public key to match prime "p" size */
  5810. XMEMMOVE(kse->pubKey + pSz - kse->pubKeyLen, kse->pubKey,
  5811. kse->pubKeyLen);
  5812. XMEMSET(kse->pubKey, 0, pSz - kse->pubKeyLen);
  5813. kse->pubKeyLen = pSz;
  5814. }
  5815. if (pvtSz != kse->keyLen) {
  5816. /* Zero pad the front of the private key */
  5817. XMEMMOVE(kse->privKey + pvtSz - kse->keyLen, kse->privKey,
  5818. kse->keyLen);
  5819. XMEMSET(kse->privKey, 0, pvtSz - kse->keyLen);
  5820. kse->keyLen = pvtSz;
  5821. }
  5822. #ifdef WOLFSSL_DEBUG_TLS
  5823. WOLFSSL_MSG("Public DH Key");
  5824. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  5825. #endif
  5826. }
  5827. /* Always release the DH key to free up memory.
  5828. * The DhKey will be setup again in TLSX_KeyShare_ProcessDh */
  5829. if (dhKey != NULL)
  5830. wc_FreeDhKey(dhKey);
  5831. if (kse->key != NULL) {
  5832. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_DH);
  5833. kse->key = NULL;
  5834. }
  5835. if (ret != 0) {
  5836. /* Cleanup on error, otherwise data owned by key share entry */
  5837. if (kse->privKey != NULL) {
  5838. XFREE(kse->privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5839. kse->privKey = NULL;
  5840. }
  5841. if (kse->pubKey != NULL) {
  5842. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5843. kse->pubKey = NULL;
  5844. }
  5845. }
  5846. #else
  5847. (void)ssl;
  5848. (void)kse;
  5849. ret = NOT_COMPILED_IN;
  5850. WOLFSSL_ERROR_VERBOSE(ret);
  5851. #endif
  5852. return ret;
  5853. }
  5854. /* Create a key share entry using X25519 parameters group.
  5855. * Generates a key pair.
  5856. *
  5857. * ssl The SSL/TLS object.
  5858. * kse The key share entry object.
  5859. * returns 0 on success, otherwise failure.
  5860. */
  5861. static int TLSX_KeyShare_GenX25519Key(WOLFSSL *ssl, KeyShareEntry* kse)
  5862. {
  5863. int ret = 0;
  5864. #ifdef HAVE_CURVE25519
  5865. curve25519_key* key = (curve25519_key*)kse->key;
  5866. if (kse->key == NULL) {
  5867. /* Allocate a Curve25519 key to hold private key. */
  5868. kse->key = (curve25519_key*)XMALLOC(sizeof(curve25519_key), ssl->heap,
  5869. DYNAMIC_TYPE_PRIVATE_KEY);
  5870. if (kse->key == NULL) {
  5871. WOLFSSL_MSG("GenX25519Key memory error");
  5872. return MEMORY_E;
  5873. }
  5874. /* Make an Curve25519 key. */
  5875. ret = wc_curve25519_init_ex((curve25519_key*)kse->key, ssl->heap,
  5876. INVALID_DEVID);
  5877. if (ret == 0) {
  5878. /* setting "key" means okay to call wc_curve25519_free */
  5879. key = (curve25519_key*)kse->key;
  5880. #ifdef WOLFSSL_STATIC_EPHEMERAL
  5881. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_CURVE25519, kse->key);
  5882. if (ret != 0)
  5883. #endif
  5884. {
  5885. ret = wc_curve25519_make_key(ssl->rng, CURVE25519_KEYSIZE, key);
  5886. }
  5887. }
  5888. }
  5889. if (ret == 0 && kse->pubKey == NULL) {
  5890. /* Allocate space for the public key. */
  5891. kse->pubKey = (byte*)XMALLOC(CURVE25519_KEYSIZE, ssl->heap,
  5892. DYNAMIC_TYPE_PUBLIC_KEY);
  5893. if (kse->pubKey == NULL) {
  5894. WOLFSSL_MSG("GenX25519Key pub memory error");
  5895. ret = MEMORY_E;
  5896. }
  5897. }
  5898. if (ret == 0) {
  5899. /* Export Curve25519 public key. */
  5900. kse->pubKeyLen = CURVE25519_KEYSIZE;
  5901. if (wc_curve25519_export_public_ex(key, kse->pubKey, &kse->pubKeyLen,
  5902. EC25519_LITTLE_ENDIAN) != 0) {
  5903. ret = ECC_EXPORT_ERROR;
  5904. WOLFSSL_ERROR_VERBOSE(ret);
  5905. }
  5906. kse->pubKeyLen = CURVE25519_KEYSIZE; /* always CURVE25519_KEYSIZE */
  5907. }
  5908. #ifdef WOLFSSL_DEBUG_TLS
  5909. if (ret == 0) {
  5910. WOLFSSL_MSG("Public Curve25519 Key");
  5911. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  5912. }
  5913. #endif
  5914. if (ret != 0) {
  5915. /* Data owned by key share entry otherwise. */
  5916. if (kse->pubKey != NULL) {
  5917. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5918. kse->pubKey = NULL;
  5919. }
  5920. if (key != NULL)
  5921. wc_curve25519_free(key);
  5922. if (kse->key != NULL) {
  5923. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5924. kse->key = NULL;
  5925. }
  5926. }
  5927. #else
  5928. (void)ssl;
  5929. (void)kse;
  5930. ret = NOT_COMPILED_IN;
  5931. WOLFSSL_ERROR_VERBOSE(ret);
  5932. #endif /* HAVE_CURVE25519 */
  5933. return ret;
  5934. }
  5935. /* Create a key share entry using X448 parameters group.
  5936. * Generates a key pair.
  5937. *
  5938. * ssl The SSL/TLS object.
  5939. * kse The key share entry object.
  5940. * returns 0 on success, otherwise failure.
  5941. */
  5942. static int TLSX_KeyShare_GenX448Key(WOLFSSL *ssl, KeyShareEntry* kse)
  5943. {
  5944. int ret = 0;
  5945. #ifdef HAVE_CURVE448
  5946. curve448_key* key = (curve448_key*)kse->key;
  5947. if (kse->key == NULL) {
  5948. /* Allocate a Curve448 key to hold private key. */
  5949. kse->key = (curve448_key*)XMALLOC(sizeof(curve448_key), ssl->heap,
  5950. DYNAMIC_TYPE_PRIVATE_KEY);
  5951. if (kse->key == NULL) {
  5952. WOLFSSL_MSG("GenX448Key memory error");
  5953. return MEMORY_E;
  5954. }
  5955. /* Make an Curve448 key. */
  5956. ret = wc_curve448_init((curve448_key*)kse->key);
  5957. if (ret == 0) {
  5958. key = (curve448_key*)kse->key;
  5959. #ifdef WOLFSSL_STATIC_EPHEMERAL
  5960. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_CURVE448, kse->key);
  5961. if (ret != 0)
  5962. #endif
  5963. {
  5964. ret = wc_curve448_make_key(ssl->rng, CURVE448_KEY_SIZE, key);
  5965. }
  5966. }
  5967. }
  5968. if (ret == 0 && kse->pubKey == NULL) {
  5969. /* Allocate space for the public key. */
  5970. kse->pubKey = (byte*)XMALLOC(CURVE448_KEY_SIZE, ssl->heap,
  5971. DYNAMIC_TYPE_PUBLIC_KEY);
  5972. if (kse->pubKey == NULL) {
  5973. WOLFSSL_MSG("GenX448Key pub memory error");
  5974. ret = MEMORY_E;
  5975. }
  5976. }
  5977. if (ret == 0) {
  5978. /* Export Curve448 public key. */
  5979. kse->pubKeyLen = CURVE448_KEY_SIZE;
  5980. if (wc_curve448_export_public_ex(key, kse->pubKey, &kse->pubKeyLen,
  5981. EC448_LITTLE_ENDIAN) != 0) {
  5982. ret = ECC_EXPORT_ERROR;
  5983. }
  5984. kse->pubKeyLen = CURVE448_KEY_SIZE; /* always CURVE448_KEY_SIZE */
  5985. }
  5986. #ifdef WOLFSSL_DEBUG_TLS
  5987. if (ret == 0) {
  5988. WOLFSSL_MSG("Public Curve448 Key");
  5989. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  5990. }
  5991. #endif
  5992. if (ret != 0) {
  5993. /* Data owned by key share entry otherwise. */
  5994. if (kse->pubKey != NULL) {
  5995. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5996. kse->pubKey = NULL;
  5997. }
  5998. if (key != NULL)
  5999. wc_curve448_free(key);
  6000. if (kse->key != NULL) {
  6001. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6002. kse->key = NULL;
  6003. }
  6004. }
  6005. #else
  6006. (void)ssl;
  6007. (void)kse;
  6008. ret = NOT_COMPILED_IN;
  6009. WOLFSSL_ERROR_VERBOSE(ret);
  6010. #endif /* HAVE_CURVE448 */
  6011. return ret;
  6012. }
  6013. /* Create a key share entry using named elliptic curve parameters group.
  6014. * Generates a key pair.
  6015. *
  6016. * ssl The SSL/TLS object.
  6017. * kse The key share entry object.
  6018. * returns 0 on success, otherwise failure.
  6019. */
  6020. static int TLSX_KeyShare_GenEccKey(WOLFSSL *ssl, KeyShareEntry* kse)
  6021. {
  6022. int ret = 0;
  6023. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT)
  6024. word32 keySize = 0;
  6025. word16 curveId = (word16) ECC_CURVE_INVALID;
  6026. ecc_key* eccKey = (ecc_key*)kse->key;
  6027. /* TODO: [TLS13] The key sizes should come from wolfcrypt. */
  6028. /* Translate named group to a curve id. */
  6029. switch (kse->group) {
  6030. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  6031. #ifndef NO_ECC_SECP
  6032. case WOLFSSL_ECC_SECP256R1:
  6033. curveId = ECC_SECP256R1;
  6034. keySize = 32;
  6035. break;
  6036. #endif /* !NO_ECC_SECP */
  6037. #ifdef WOLFSSL_SM2
  6038. case WOLFSSL_ECC_SM2P256V1:
  6039. curveId = ECC_SM2P256V1;
  6040. keySize = 32;
  6041. break;
  6042. #endif /* !NO_ECC_SECP */
  6043. #endif
  6044. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  6045. #ifndef NO_ECC_SECP
  6046. case WOLFSSL_ECC_SECP384R1:
  6047. curveId = ECC_SECP384R1;
  6048. keySize = 48;
  6049. break;
  6050. #endif /* !NO_ECC_SECP */
  6051. #endif
  6052. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  6053. #ifndef NO_ECC_SECP
  6054. case WOLFSSL_ECC_SECP521R1:
  6055. curveId = ECC_SECP521R1;
  6056. keySize = 66;
  6057. break;
  6058. #endif /* !NO_ECC_SECP */
  6059. #endif
  6060. default:
  6061. WOLFSSL_ERROR_VERBOSE(BAD_FUNC_ARG);
  6062. return BAD_FUNC_ARG;
  6063. }
  6064. if (kse->key == NULL) {
  6065. kse->keyLen = keySize;
  6066. kse->pubKeyLen = keySize * 2 + 1;
  6067. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  6068. ret = tsip_Tls13GenEccKeyPair(ssl, kse);
  6069. if (ret != CRYPTOCB_UNAVAILABLE) {
  6070. return ret;
  6071. }
  6072. #endif
  6073. /* Allocate an ECC key to hold private key. */
  6074. kse->key = (byte*)XMALLOC(sizeof(ecc_key), ssl->heap, DYNAMIC_TYPE_ECC);
  6075. if (kse->key == NULL) {
  6076. WOLFSSL_MSG("EccTempKey Memory error");
  6077. return MEMORY_E;
  6078. }
  6079. /* Make an ECC key */
  6080. ret = wc_ecc_init_ex((ecc_key*)kse->key, ssl->heap, ssl->devId);
  6081. if (ret == 0) {
  6082. /* setting eccKey means okay to call wc_ecc_free */
  6083. eccKey = (ecc_key*)kse->key;
  6084. #ifdef WOLFSSL_STATIC_EPHEMERAL
  6085. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_ECDH, kse->key);
  6086. if (ret != 0)
  6087. #endif
  6088. {
  6089. /* set curve info for EccMakeKey "peer" info */
  6090. ret = wc_ecc_set_curve(eccKey, kse->keyLen, curveId);
  6091. if (ret == 0) {
  6092. /* Generate ephemeral ECC key */
  6093. /* For async this is called once and when event is done, the
  6094. * provided buffers in key be populated.
  6095. * Final processing is x963 key export below. */
  6096. ret = EccMakeKey(ssl, eccKey, eccKey);
  6097. }
  6098. #ifdef WOLFSSL_ASYNC_CRYPT
  6099. if (ret == WC_PENDING_E)
  6100. return ret;
  6101. #endif
  6102. }
  6103. }
  6104. }
  6105. if (ret == 0 && kse->pubKey == NULL) {
  6106. /* Allocate space for the public key */
  6107. kse->pubKey = (byte*)XMALLOC(kse->pubKeyLen, ssl->heap,
  6108. DYNAMIC_TYPE_PUBLIC_KEY);
  6109. if (kse->pubKey == NULL) {
  6110. WOLFSSL_MSG("Key data Memory error");
  6111. ret = MEMORY_E;
  6112. }
  6113. }
  6114. if (ret == 0) {
  6115. XMEMSET(kse->pubKey, 0, kse->pubKeyLen);
  6116. /* Export public key. */
  6117. PRIVATE_KEY_UNLOCK();
  6118. if (wc_ecc_export_x963(eccKey, kse->pubKey, &kse->pubKeyLen) != 0) {
  6119. ret = ECC_EXPORT_ERROR;
  6120. WOLFSSL_ERROR_VERBOSE(ret);
  6121. }
  6122. PRIVATE_KEY_LOCK();
  6123. }
  6124. #ifdef WOLFSSL_DEBUG_TLS
  6125. if (ret == 0) {
  6126. WOLFSSL_MSG("Public ECC Key");
  6127. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  6128. }
  6129. #endif
  6130. if (ret != 0) {
  6131. /* Cleanup on error, otherwise data owned by key share entry */
  6132. if (kse->pubKey != NULL) {
  6133. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6134. kse->pubKey = NULL;
  6135. }
  6136. if (eccKey != NULL)
  6137. wc_ecc_free(eccKey);
  6138. if (kse->key != NULL) {
  6139. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6140. kse->key = NULL;
  6141. }
  6142. }
  6143. #else
  6144. (void)ssl;
  6145. (void)kse;
  6146. ret = NOT_COMPILED_IN;
  6147. WOLFSSL_ERROR_VERBOSE(ret);
  6148. #endif /* HAVE_ECC && HAVE_ECC_KEY_EXPORT */
  6149. return ret;
  6150. }
  6151. #ifdef HAVE_PQC
  6152. static int kyber_id2type(int id, int *type)
  6153. {
  6154. int ret = 0;
  6155. switch (id) {
  6156. #ifdef WOLFSSL_KYBER512
  6157. case WOLFSSL_KYBER_LEVEL1:
  6158. *type = KYBER512;
  6159. break;
  6160. #endif
  6161. #ifdef WOLFSSL_KYBER768
  6162. case WOLFSSL_KYBER_LEVEL3:
  6163. *type = KYBER768;
  6164. break;
  6165. #endif
  6166. #ifdef WOLFSSL_KYBER1024
  6167. case WOLFSSL_KYBER_LEVEL5:
  6168. *type = KYBER1024;
  6169. break;
  6170. #endif
  6171. default:
  6172. ret = NOT_COMPILED_IN;
  6173. break;
  6174. }
  6175. return ret;
  6176. }
  6177. typedef struct PqcHybridMapping {
  6178. int hybrid;
  6179. int ecc;
  6180. int pqc;
  6181. } PqcHybridMapping;
  6182. static const PqcHybridMapping pqc_hybrid_mapping[] = {
  6183. {.hybrid = WOLFSSL_P256_KYBER_LEVEL1, .ecc = WOLFSSL_ECC_SECP256R1,
  6184. .pqc = WOLFSSL_KYBER_LEVEL1},
  6185. {.hybrid = WOLFSSL_P384_KYBER_LEVEL3, .ecc = WOLFSSL_ECC_SECP384R1,
  6186. .pqc = WOLFSSL_KYBER_LEVEL3},
  6187. {.hybrid = WOLFSSL_P521_KYBER_LEVEL5, .ecc = WOLFSSL_ECC_SECP521R1,
  6188. .pqc = WOLFSSL_KYBER_LEVEL5},
  6189. {.hybrid = 0, .ecc = 0, .pqc = 0}
  6190. };
  6191. /* This will map an ecc-pqs hybrid group into its ecc group and pqc kem group.
  6192. * If it cannot find a mapping then *pqc is set to group. ecc is optional. */
  6193. static void findEccPqc(int *ecc, int *pqc, int group)
  6194. {
  6195. int i;
  6196. if (pqc == NULL) {
  6197. return;
  6198. }
  6199. *pqc = 0;
  6200. if (ecc != NULL) {
  6201. *ecc = 0;
  6202. }
  6203. for (i = 0; pqc_hybrid_mapping[i].hybrid != 0; i++) {
  6204. if (pqc_hybrid_mapping[i].hybrid == group) {
  6205. *pqc = pqc_hybrid_mapping[i].pqc;
  6206. if (ecc != NULL) {
  6207. *ecc = pqc_hybrid_mapping[i].ecc;
  6208. }
  6209. break;
  6210. }
  6211. }
  6212. if (*pqc == 0) {
  6213. /* It is not a hybrid, so maybe its simple. */
  6214. *pqc = group;
  6215. }
  6216. }
  6217. /* Create a key share entry using liboqs parameters group.
  6218. * Generates a key pair.
  6219. *
  6220. * ssl The SSL/TLS object.
  6221. * kse The key share entry object.
  6222. * returns 0 on success, otherwise failure.
  6223. */
  6224. static int TLSX_KeyShare_GenPqcKey(WOLFSSL *ssl, KeyShareEntry* kse)
  6225. {
  6226. int ret = 0;
  6227. int type = 0;
  6228. KyberKey kem[1];
  6229. byte* pubKey = NULL;
  6230. byte* privKey = NULL;
  6231. KeyShareEntry *ecc_kse = NULL;
  6232. int oqs_group = 0;
  6233. int ecc_group = 0;
  6234. word32 privSz = 0;
  6235. word32 pubSz = 0;
  6236. findEccPqc(&ecc_group, &oqs_group, kse->group);
  6237. ret = kyber_id2type(oqs_group, &type);
  6238. if (ret == NOT_COMPILED_IN) {
  6239. WOLFSSL_MSG("Invalid Kyber algorithm specified.");
  6240. ret = BAD_FUNC_ARG;
  6241. }
  6242. if (ret == 0) {
  6243. ret = wc_KyberKey_Init(type, kem, ssl->heap, ssl->devId);
  6244. if (ret != 0) {
  6245. WOLFSSL_MSG("Failed to initialize Kyber Key.");
  6246. }
  6247. }
  6248. if (ret == 0) {
  6249. ecc_kse = (KeyShareEntry*)XMALLOC(sizeof(*ecc_kse), ssl->heap,
  6250. DYNAMIC_TYPE_TLSX);
  6251. if (ecc_kse == NULL) {
  6252. WOLFSSL_MSG("ecc_kse memory allocation failure");
  6253. ret = MEMORY_ERROR;
  6254. }
  6255. }
  6256. if (ret == 0) {
  6257. XMEMSET(ecc_kse, 0, sizeof(*ecc_kse));
  6258. ret = wc_KyberKey_PrivateKeySize(kem, &privSz);
  6259. }
  6260. if (ret == 0) {
  6261. ret = wc_KyberKey_PublicKeySize(kem, &pubSz);
  6262. }
  6263. if (ret == 0 && ecc_group != 0) {
  6264. ecc_kse->group = ecc_group;
  6265. ret = TLSX_KeyShare_GenEccKey(ssl, ecc_kse);
  6266. /* If fail, no error message, TLSX_KeyShare_GenEccKey will do it. */
  6267. }
  6268. if (ret == 0) {
  6269. pubKey = (byte*)XMALLOC(ecc_kse->pubKeyLen + pubSz, ssl->heap,
  6270. DYNAMIC_TYPE_PUBLIC_KEY);
  6271. if (pubKey == NULL) {
  6272. WOLFSSL_MSG("pubkey memory allocation failure");
  6273. ret = MEMORY_ERROR;
  6274. }
  6275. }
  6276. if (ret == 0) {
  6277. privKey = (byte*)XMALLOC(privSz, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6278. if (privKey == NULL) {
  6279. WOLFSSL_MSG("privkey memory allocation failure");
  6280. ret = MEMORY_ERROR;
  6281. }
  6282. }
  6283. if (ret == 0) {
  6284. ret = wc_KyberKey_MakeKey(kem, ssl->rng);
  6285. if (ret != 0) {
  6286. WOLFSSL_MSG("Kyber keygen failure");
  6287. }
  6288. }
  6289. if (ret == 0) {
  6290. ret = wc_KyberKey_EncodePublicKey(kem, pubKey + ecc_kse->pubKeyLen,
  6291. pubSz);
  6292. }
  6293. if (ret == 0) {
  6294. ret = wc_KyberKey_EncodePrivateKey(kem, privKey, privSz);
  6295. }
  6296. if (ret == 0) {
  6297. XMEMCPY(pubKey, ecc_kse->pubKey, ecc_kse->pubKeyLen);
  6298. kse->pubKey = pubKey;
  6299. kse->pubKeyLen = ecc_kse->pubKeyLen + pubSz;
  6300. pubKey = NULL;
  6301. /* Note we are saving the OQS private key and ECC private key
  6302. * separately. That's because the ECC private key is not simply a
  6303. * buffer. Its is an ecc_key struct.
  6304. */
  6305. kse->privKey = privKey;
  6306. privKey = NULL;
  6307. kse->key = ecc_kse->key;
  6308. ecc_kse->key = NULL;
  6309. }
  6310. #ifdef WOLFSSL_DEBUG_TLS
  6311. WOLFSSL_MSG("Public Kyber Key");
  6312. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen );
  6313. #endif
  6314. wc_KyberKey_Free(kem);
  6315. TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
  6316. if (pubKey != NULL)
  6317. XFREE(pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6318. if (privKey != NULL)
  6319. XFREE(privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6320. return ret;
  6321. }
  6322. #endif /* HAVE_PQC */
  6323. /* Generate a secret/key using the key share entry.
  6324. *
  6325. * ssl The SSL/TLS object.
  6326. * kse The key share entry holding peer data.
  6327. */
  6328. int TLSX_KeyShare_GenKey(WOLFSSL *ssl, KeyShareEntry *kse)
  6329. {
  6330. int ret;
  6331. /* Named FFDHE groups have a bit set to identify them. */
  6332. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(kse->group))
  6333. ret = TLSX_KeyShare_GenDhKey(ssl, kse);
  6334. else if (kse->group == WOLFSSL_ECC_X25519)
  6335. ret = TLSX_KeyShare_GenX25519Key(ssl, kse);
  6336. else if (kse->group == WOLFSSL_ECC_X448)
  6337. ret = TLSX_KeyShare_GenX448Key(ssl, kse);
  6338. #ifdef HAVE_PQC
  6339. else if (WOLFSSL_NAMED_GROUP_IS_PQC(kse->group))
  6340. ret = TLSX_KeyShare_GenPqcKey(ssl, kse);
  6341. #endif
  6342. else
  6343. ret = TLSX_KeyShare_GenEccKey(ssl, kse);
  6344. #ifdef WOLFSSL_ASYNC_CRYPT
  6345. kse->lastRet = ret;
  6346. #endif
  6347. return ret;
  6348. }
  6349. /* Free the key share dynamic data.
  6350. *
  6351. * list The linked list of key share entry objects.
  6352. * heap The heap used for allocation.
  6353. */
  6354. static void TLSX_KeyShare_FreeAll(KeyShareEntry* list, void* heap)
  6355. {
  6356. KeyShareEntry* current;
  6357. while ((current = list) != NULL) {
  6358. list = current->next;
  6359. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(current->group)) {
  6360. #ifndef NO_DH
  6361. wc_FreeDhKey((DhKey*)current->key);
  6362. #endif
  6363. }
  6364. else if (current->group == WOLFSSL_ECC_X25519) {
  6365. #ifdef HAVE_CURVE25519
  6366. wc_curve25519_free((curve25519_key*)current->key);
  6367. #endif
  6368. }
  6369. else if (current->group == WOLFSSL_ECC_X448) {
  6370. #ifdef HAVE_CURVE448
  6371. wc_curve448_free((curve448_key*)current->key);
  6372. #endif
  6373. }
  6374. #ifdef HAVE_PQC
  6375. else if (WOLFSSL_NAMED_GROUP_IS_PQC(current->group) &&
  6376. current->key != NULL) {
  6377. ForceZero((byte*)current->key, current->keyLen);
  6378. }
  6379. #endif
  6380. else {
  6381. #ifdef HAVE_ECC
  6382. wc_ecc_free((ecc_key*)current->key);
  6383. #endif
  6384. }
  6385. XFREE(current->key, heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6386. #if !defined(NO_DH) && (!defined(NO_CERTS) || !defined(NO_PSK))
  6387. XFREE(current->privKey, heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6388. #endif
  6389. XFREE(current->pubKey, heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6390. XFREE(current->ke, heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6391. XFREE(current, heap, DYNAMIC_TYPE_TLSX);
  6392. }
  6393. (void)heap;
  6394. }
  6395. /* Get the size of the encoded key share extension.
  6396. *
  6397. * list The linked list of key share extensions.
  6398. * msgType The type of the message this extension is being written into.
  6399. * returns the number of bytes of the encoded key share extension.
  6400. */
  6401. static word16 TLSX_KeyShare_GetSize(KeyShareEntry* list, byte msgType)
  6402. {
  6403. word16 len = 0;
  6404. byte isRequest = (msgType == client_hello);
  6405. KeyShareEntry* current;
  6406. /* The named group the server wants to use. */
  6407. if (msgType == hello_retry_request)
  6408. return OPAQUE16_LEN;
  6409. /* List of key exchange groups. */
  6410. if (isRequest)
  6411. len += OPAQUE16_LEN;
  6412. while ((current = list) != NULL) {
  6413. list = current->next;
  6414. if (!isRequest && current->pubKey == NULL)
  6415. continue;
  6416. len += (word16)(KE_GROUP_LEN + OPAQUE16_LEN + current->pubKeyLen);
  6417. }
  6418. return len;
  6419. }
  6420. /* Writes the key share extension into the output buffer.
  6421. * Assumes that the the output buffer is big enough to hold data.
  6422. *
  6423. * list The linked list of key share entries.
  6424. * output The buffer to write into.
  6425. * msgType The type of the message this extension is being written into.
  6426. * returns the number of bytes written into the buffer.
  6427. */
  6428. static word16 TLSX_KeyShare_Write(KeyShareEntry* list, byte* output,
  6429. byte msgType)
  6430. {
  6431. word16 i = 0;
  6432. byte isRequest = (msgType == client_hello);
  6433. KeyShareEntry* current;
  6434. if (msgType == hello_retry_request) {
  6435. c16toa(list->group, output);
  6436. return OPAQUE16_LEN;
  6437. }
  6438. /* ClientHello has a list but ServerHello is only the chosen. */
  6439. if (isRequest)
  6440. i += OPAQUE16_LEN;
  6441. /* Write out all in the list. */
  6442. while ((current = list) != NULL) {
  6443. list = current->next;
  6444. if (!isRequest && current->pubKey == NULL)
  6445. continue;
  6446. c16toa(current->group, &output[i]);
  6447. i += KE_GROUP_LEN;
  6448. c16toa((word16)(current->pubKeyLen), &output[i]);
  6449. i += OPAQUE16_LEN;
  6450. XMEMCPY(&output[i], current->pubKey, current->pubKeyLen);
  6451. i += (word16)current->pubKeyLen;
  6452. }
  6453. /* Write the length of the list if required. */
  6454. if (isRequest)
  6455. c16toa(i - OPAQUE16_LEN, output);
  6456. return i;
  6457. }
  6458. /* Process the DH key share extension on the client side.
  6459. *
  6460. * ssl The SSL/TLS object.
  6461. * keyShareEntry The key share entry object to use to calculate shared secret.
  6462. * returns 0 on success and other values indicate failure.
  6463. */
  6464. static int TLSX_KeyShare_ProcessDh(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6465. {
  6466. int ret = 0;
  6467. #if !defined(NO_DH) && (!defined(NO_CERTS) || !defined(NO_PSK))
  6468. word32 pSz = 0;
  6469. DhKey* dhKey = (DhKey*)keyShareEntry->key;
  6470. #ifdef HAVE_PUBLIC_FFDHE
  6471. const DhParams* params = NULL;
  6472. switch (keyShareEntry->group) {
  6473. #ifdef HAVE_FFDHE_2048
  6474. case WOLFSSL_FFDHE_2048:
  6475. params = wc_Dh_ffdhe2048_Get();
  6476. break;
  6477. #endif
  6478. #ifdef HAVE_FFDHE_3072
  6479. case WOLFSSL_FFDHE_3072:
  6480. params = wc_Dh_ffdhe3072_Get();
  6481. break;
  6482. #endif
  6483. #ifdef HAVE_FFDHE_4096
  6484. case WOLFSSL_FFDHE_4096:
  6485. params = wc_Dh_ffdhe4096_Get();
  6486. break;
  6487. #endif
  6488. #ifdef HAVE_FFDHE_6144
  6489. case WOLFSSL_FFDHE_6144:
  6490. params = wc_Dh_ffdhe6144_Get();
  6491. break;
  6492. #endif
  6493. #ifdef HAVE_FFDHE_8192
  6494. case WOLFSSL_FFDHE_8192:
  6495. params = wc_Dh_ffdhe8192_Get();
  6496. break;
  6497. #endif
  6498. default:
  6499. break;
  6500. }
  6501. if (params == NULL) {
  6502. WOLFSSL_ERROR_VERBOSE(PEER_KEY_ERROR);
  6503. return PEER_KEY_ERROR;
  6504. }
  6505. pSz = params->p_len;
  6506. #else
  6507. ret = wc_DhGetNamedKeyParamSize(keyShareEntry->group, &pSz, NULL, NULL);
  6508. if (ret != 0 || pSz == 0) {
  6509. WOLFSSL_ERROR_VERBOSE(PEER_KEY_ERROR);
  6510. return PEER_KEY_ERROR;
  6511. }
  6512. #endif
  6513. /* if DhKey is not setup, do it now */
  6514. if (keyShareEntry->key == NULL) {
  6515. keyShareEntry->key = (DhKey*)XMALLOC(sizeof(DhKey), ssl->heap,
  6516. DYNAMIC_TYPE_DH);
  6517. if (keyShareEntry->key == NULL)
  6518. return MEMORY_E;
  6519. /* Setup Key */
  6520. ret = wc_InitDhKey_ex((DhKey*)keyShareEntry->key, ssl->heap, ssl->devId);
  6521. if (ret == 0) {
  6522. dhKey = (DhKey*)keyShareEntry->key;
  6523. /* Set key */
  6524. #ifdef HAVE_PUBLIC_FFDHE
  6525. ret = wc_DhSetKey(dhKey, params->p, params->p_len, params->g,
  6526. params->g_len);
  6527. #else
  6528. ret = wc_DhSetNamedKey(dhKey, keyShareEntry->group);
  6529. #endif
  6530. }
  6531. }
  6532. if (ret == 0
  6533. #ifdef WOLFSSL_ASYNC_CRYPT
  6534. && keyShareEntry->lastRet == 0 /* don't enter here if WC_PENDING_E */
  6535. #endif
  6536. ) {
  6537. #ifdef WOLFSSL_DEBUG_TLS
  6538. WOLFSSL_MSG("Peer DH Key");
  6539. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6540. #endif
  6541. ssl->options.dhKeySz = (word16)pSz;
  6542. /* Derive secret from private key and peer's public key. */
  6543. ret = DhAgree(ssl, dhKey,
  6544. (const byte*)keyShareEntry->privKey, keyShareEntry->keyLen, /* our private */
  6545. keyShareEntry->ke, keyShareEntry->keLen, /* peer's public key */
  6546. ssl->arrays->preMasterSecret, &ssl->arrays->preMasterSz, /* secret */
  6547. NULL, 0
  6548. );
  6549. #ifdef WOLFSSL_ASYNC_CRYPT
  6550. if (ret == WC_PENDING_E) {
  6551. return ret;
  6552. }
  6553. #endif
  6554. }
  6555. /* RFC 8446 Section 7.4.1:
  6556. * ... left-padded with zeros up to the size of the prime. ...
  6557. */
  6558. if (ret == 0 && (word32)ssl->options.dhKeySz > ssl->arrays->preMasterSz) {
  6559. word32 diff = (word32)ssl->options.dhKeySz - ssl->arrays->preMasterSz;
  6560. XMEMMOVE(ssl->arrays->preMasterSecret + diff,
  6561. ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz);
  6562. XMEMSET(ssl->arrays->preMasterSecret, 0, diff);
  6563. ssl->arrays->preMasterSz = ssl->options.dhKeySz;
  6564. }
  6565. /* done with key share, release resources */
  6566. if (dhKey)
  6567. wc_FreeDhKey(dhKey);
  6568. if (keyShareEntry->key) {
  6569. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_DH);
  6570. keyShareEntry->key = NULL;
  6571. }
  6572. if (keyShareEntry->privKey != NULL) {
  6573. XFREE(keyShareEntry->privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6574. keyShareEntry->privKey = NULL;
  6575. }
  6576. if (keyShareEntry->pubKey != NULL) {
  6577. XFREE(keyShareEntry->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6578. keyShareEntry->pubKey = NULL;
  6579. }
  6580. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6581. keyShareEntry->ke = NULL;
  6582. #else
  6583. (void)ssl;
  6584. (void)keyShareEntry;
  6585. ret = PEER_KEY_ERROR;
  6586. WOLFSSL_ERROR_VERBOSE(ret);
  6587. #endif
  6588. return ret;
  6589. }
  6590. /* Process the X25519 key share extension on the client side.
  6591. *
  6592. * ssl The SSL/TLS object.
  6593. * keyShareEntry The key share entry object to use to calculate shared secret.
  6594. * returns 0 on success and other values indicate failure.
  6595. */
  6596. static int TLSX_KeyShare_ProcessX25519(WOLFSSL* ssl,
  6597. KeyShareEntry* keyShareEntry)
  6598. {
  6599. int ret;
  6600. #ifdef HAVE_CURVE25519
  6601. curve25519_key* key = (curve25519_key*)keyShareEntry->key;
  6602. curve25519_key* peerX25519Key;
  6603. #ifdef HAVE_ECC
  6604. if (ssl->peerEccKey != NULL) {
  6605. wc_ecc_free(ssl->peerEccKey);
  6606. ssl->peerEccKey = NULL;
  6607. ssl->peerEccKeyPresent = 0;
  6608. }
  6609. #endif
  6610. peerX25519Key = (curve25519_key*)XMALLOC(sizeof(curve25519_key), ssl->heap,
  6611. DYNAMIC_TYPE_TLSX);
  6612. if (peerX25519Key == NULL) {
  6613. WOLFSSL_MSG("PeerEccKey Memory error");
  6614. return MEMORY_ERROR;
  6615. }
  6616. ret = wc_curve25519_init(peerX25519Key);
  6617. if (ret != 0) {
  6618. XFREE(peerX25519Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6619. return ret;
  6620. }
  6621. #ifdef WOLFSSL_DEBUG_TLS
  6622. WOLFSSL_MSG("Peer Curve25519 Key");
  6623. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6624. #endif
  6625. if (wc_curve25519_check_public(keyShareEntry->ke, keyShareEntry->keLen,
  6626. EC25519_LITTLE_ENDIAN) != 0) {
  6627. ret = ECC_PEERKEY_ERROR;
  6628. WOLFSSL_ERROR_VERBOSE(ret);
  6629. }
  6630. if (ret == 0) {
  6631. if (wc_curve25519_import_public_ex(keyShareEntry->ke,
  6632. keyShareEntry->keLen, peerX25519Key,
  6633. EC25519_LITTLE_ENDIAN) != 0) {
  6634. ret = ECC_PEERKEY_ERROR;
  6635. WOLFSSL_ERROR_VERBOSE(ret);
  6636. }
  6637. }
  6638. if (ret == 0) {
  6639. ssl->ecdhCurveOID = ECC_X25519_OID;
  6640. ret = wc_curve25519_shared_secret_ex(key, peerX25519Key,
  6641. ssl->arrays->preMasterSecret,
  6642. &ssl->arrays->preMasterSz,
  6643. EC25519_LITTLE_ENDIAN);
  6644. }
  6645. wc_curve25519_free(peerX25519Key);
  6646. XFREE(peerX25519Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6647. wc_curve25519_free((curve25519_key*)keyShareEntry->key);
  6648. if (keyShareEntry->key != NULL) {
  6649. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6650. keyShareEntry->key = NULL;
  6651. }
  6652. #else
  6653. (void)ssl;
  6654. (void)keyShareEntry;
  6655. ret = PEER_KEY_ERROR;
  6656. WOLFSSL_ERROR_VERBOSE(ret);
  6657. #endif /* HAVE_CURVE25519 */
  6658. return ret;
  6659. }
  6660. /* Process the X448 key share extension on the client side.
  6661. *
  6662. * ssl The SSL/TLS object.
  6663. * keyShareEntry The key share entry object to use to calculate shared secret.
  6664. * returns 0 on success and other values indicate failure.
  6665. */
  6666. static int TLSX_KeyShare_ProcessX448(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6667. {
  6668. int ret;
  6669. #ifdef HAVE_CURVE448
  6670. curve448_key* key = (curve448_key*)keyShareEntry->key;
  6671. curve448_key* peerX448Key;
  6672. #ifdef HAVE_ECC
  6673. if (ssl->peerEccKey != NULL) {
  6674. wc_ecc_free(ssl->peerEccKey);
  6675. ssl->peerEccKey = NULL;
  6676. ssl->peerEccKeyPresent = 0;
  6677. }
  6678. #endif
  6679. peerX448Key = (curve448_key*)XMALLOC(sizeof(curve448_key), ssl->heap,
  6680. DYNAMIC_TYPE_TLSX);
  6681. if (peerX448Key == NULL) {
  6682. WOLFSSL_MSG("PeerEccKey Memory error");
  6683. return MEMORY_ERROR;
  6684. }
  6685. ret = wc_curve448_init(peerX448Key);
  6686. if (ret != 0) {
  6687. XFREE(peerX448Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6688. return ret;
  6689. }
  6690. #ifdef WOLFSSL_DEBUG_TLS
  6691. WOLFSSL_MSG("Peer Curve448 Key");
  6692. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6693. #endif
  6694. if (wc_curve448_check_public(keyShareEntry->ke, keyShareEntry->keLen,
  6695. EC448_LITTLE_ENDIAN) != 0) {
  6696. ret = ECC_PEERKEY_ERROR;
  6697. WOLFSSL_ERROR_VERBOSE(ret);
  6698. }
  6699. if (ret == 0) {
  6700. if (wc_curve448_import_public_ex(keyShareEntry->ke,
  6701. keyShareEntry->keLen, peerX448Key,
  6702. EC448_LITTLE_ENDIAN) != 0) {
  6703. ret = ECC_PEERKEY_ERROR;
  6704. WOLFSSL_ERROR_VERBOSE(ret);
  6705. }
  6706. }
  6707. if (ret == 0) {
  6708. ssl->ecdhCurveOID = ECC_X448_OID;
  6709. ret = wc_curve448_shared_secret_ex(key, peerX448Key,
  6710. ssl->arrays->preMasterSecret,
  6711. &ssl->arrays->preMasterSz,
  6712. EC448_LITTLE_ENDIAN);
  6713. }
  6714. wc_curve448_free(peerX448Key);
  6715. XFREE(peerX448Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6716. wc_curve448_free((curve448_key*)keyShareEntry->key);
  6717. if (keyShareEntry->key != NULL) {
  6718. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6719. keyShareEntry->key = NULL;
  6720. }
  6721. #else
  6722. (void)ssl;
  6723. (void)keyShareEntry;
  6724. ret = PEER_KEY_ERROR;
  6725. WOLFSSL_ERROR_VERBOSE(ret);
  6726. #endif /* HAVE_CURVE448 */
  6727. return ret;
  6728. }
  6729. /* Process the ECC key share extension on the client side.
  6730. *
  6731. * ssl The SSL/TLS object.
  6732. * keyShareEntry The key share entry object to use to calculate shared secret.
  6733. * returns 0 on success and other values indicate failure.
  6734. */
  6735. static int TLSX_KeyShare_ProcessEcc(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6736. {
  6737. int ret = 0;
  6738. #ifdef HAVE_ECC
  6739. int curveId = ECC_CURVE_INVALID;
  6740. ecc_key* eccKey = (ecc_key*)keyShareEntry->key;
  6741. /* find supported curve */
  6742. switch (keyShareEntry->group) {
  6743. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  6744. #ifndef NO_ECC_SECP
  6745. case WOLFSSL_ECC_SECP256R1:
  6746. curveId = ECC_SECP256R1;
  6747. break;
  6748. #endif /* !NO_ECC_SECP */
  6749. #ifdef WOLFSSL_SM2
  6750. case WOLFSSL_ECC_SM2P256V1:
  6751. curveId = ECC_SM2P256V1;
  6752. break;
  6753. #endif
  6754. #endif
  6755. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  6756. #ifndef NO_ECC_SECP
  6757. case WOLFSSL_ECC_SECP384R1:
  6758. curveId = ECC_SECP384R1;
  6759. break;
  6760. #endif /* !NO_ECC_SECP */
  6761. #endif
  6762. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  6763. #ifndef NO_ECC_SECP
  6764. case WOLFSSL_ECC_SECP521R1:
  6765. curveId = ECC_SECP521R1;
  6766. break;
  6767. #endif /* !NO_ECC_SECP */
  6768. #endif
  6769. #if defined(HAVE_X448) && ECC_MIN_KEY_SZ <= 448
  6770. case WOLFSSL_ECC_X448:
  6771. curveId = ECC_X448;
  6772. break;
  6773. #endif
  6774. default:
  6775. /* unsupported curve */
  6776. WOLFSSL_ERROR_VERBOSE(ECC_PEERKEY_ERROR);
  6777. return ECC_PEERKEY_ERROR;
  6778. }
  6779. #ifdef WOLFSSL_ASYNC_CRYPT
  6780. if (keyShareEntry->lastRet == 0) /* don't enter here if WC_PENDING_E */
  6781. #endif
  6782. {
  6783. #ifdef WOLFSSL_DEBUG_TLS
  6784. WOLFSSL_MSG("Peer ECC Key");
  6785. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6786. #endif
  6787. if (ssl->peerEccKey != NULL) {
  6788. wc_ecc_free(ssl->peerEccKey);
  6789. XFREE(ssl->peerEccKey, ssl->heap, DYNAMIC_TYPE_ECC);
  6790. ssl->peerEccKeyPresent = 0;
  6791. }
  6792. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  6793. ret = tsip_Tls13GenSharedSecret(ssl, keyShareEntry);
  6794. if (ret != CRYPTOCB_UNAVAILABLE) {
  6795. return ret;
  6796. }
  6797. ret = 0;
  6798. #endif
  6799. ssl->peerEccKey = (ecc_key*)XMALLOC(sizeof(ecc_key), ssl->heap,
  6800. DYNAMIC_TYPE_ECC);
  6801. if (ssl->peerEccKey == NULL) {
  6802. WOLFSSL_MSG("PeerEccKey Memory error");
  6803. ret = MEMORY_ERROR;
  6804. }
  6805. if (ret == 0) {
  6806. ret = wc_ecc_init_ex(ssl->peerEccKey, ssl->heap, ssl->devId);
  6807. }
  6808. /* Point is validated by import function. */
  6809. if (ret == 0) {
  6810. ret = wc_ecc_import_x963_ex(keyShareEntry->ke, keyShareEntry->keLen,
  6811. ssl->peerEccKey, curveId);
  6812. if (ret != 0) {
  6813. ret = ECC_PEERKEY_ERROR;
  6814. WOLFSSL_ERROR_VERBOSE(ret);
  6815. }
  6816. }
  6817. if (ret == 0) {
  6818. ssl->ecdhCurveOID = ssl->peerEccKey->dp->oidSum;
  6819. ssl->peerEccKeyPresent = 1;
  6820. }
  6821. }
  6822. if (ret == 0 && eccKey == NULL)
  6823. ret = BAD_FUNC_ARG;
  6824. if (ret == 0) {
  6825. ret = EccSharedSecret(ssl, eccKey, ssl->peerEccKey,
  6826. keyShareEntry->ke, &keyShareEntry->keLen,
  6827. ssl->arrays->preMasterSecret, &ssl->arrays->preMasterSz,
  6828. ssl->options.side
  6829. );
  6830. #ifdef WOLFSSL_ASYNC_CRYPT
  6831. if (ret == WC_PENDING_E)
  6832. return ret;
  6833. #endif
  6834. }
  6835. /* done with key share, release resources */
  6836. if (ssl->peerEccKey != NULL
  6837. #ifdef HAVE_PK_CALLBACKS
  6838. && ssl->ctx->EccSharedSecretCb == NULL
  6839. #endif
  6840. ) {
  6841. wc_ecc_free(ssl->peerEccKey);
  6842. XFREE(ssl->peerEccKey, ssl->heap, DYNAMIC_TYPE_ECC);
  6843. ssl->peerEccKey = NULL;
  6844. ssl->peerEccKeyPresent = 0;
  6845. }
  6846. if (keyShareEntry->key) {
  6847. wc_ecc_free((ecc_key*)keyShareEntry->key);
  6848. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_ECC);
  6849. keyShareEntry->key = NULL;
  6850. }
  6851. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6852. keyShareEntry->ke = NULL;
  6853. #else
  6854. (void)ssl;
  6855. (void)keyShareEntry;
  6856. ret = PEER_KEY_ERROR;
  6857. WOLFSSL_ERROR_VERBOSE(ret);
  6858. #endif /* HAVE_ECC */
  6859. return ret;
  6860. }
  6861. #ifdef HAVE_PQC
  6862. /* Process the Kyber key share extension on the client side.
  6863. *
  6864. * ssl The SSL/TLS object.
  6865. * keyShareEntry The key share entry object to use to calculate shared secret.
  6866. * returns 0 on success and other values indicate failure.
  6867. */
  6868. static int TLSX_KeyShare_ProcessPqc(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6869. {
  6870. int ret = 0;
  6871. int type;
  6872. KyberKey kem[1];
  6873. byte* sharedSecret = NULL;
  6874. word32 sharedSecretLen = 0;
  6875. int oqs_group = 0;
  6876. int ecc_group = 0;
  6877. ecc_key eccpubkey;
  6878. word32 outlen = 0;
  6879. word32 privSz = 0;
  6880. word32 ctSz = 0;
  6881. word32 ssSz = 0;
  6882. if (keyShareEntry->ke == NULL) {
  6883. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  6884. return BAD_FUNC_ARG;
  6885. }
  6886. if (ssl->options.side == WOLFSSL_SERVER_END) {
  6887. /* I am the server, the shared secret has already been generated and
  6888. * is in keyShareEntry->ke; copy it to the pre-master secret
  6889. * pre-allocated buffer. */
  6890. if (keyShareEntry->keLen > ENCRYPT_LEN) {
  6891. WOLFSSL_MSG("shared secret is too long.");
  6892. return LENGTH_ERROR;
  6893. }
  6894. XMEMCPY(ssl->arrays->preMasterSecret, keyShareEntry->ke,
  6895. keyShareEntry->keLen);
  6896. ssl->arrays->preMasterSz = keyShareEntry->keLen;
  6897. XFREE(keyShareEntry->ke, sl->heap, DYNAMIC_TYPE_SECRET)
  6898. keyShareEntry->ke = NULL;
  6899. keyShareEntry->keLen = 0;
  6900. return 0;
  6901. }
  6902. /* I am the client, the ciphertext is in keyShareEntry->ke */
  6903. findEccPqc(&ecc_group, &oqs_group, keyShareEntry->group);
  6904. ret = kyber_id2type(oqs_group, &type);
  6905. if (ret != 0) {
  6906. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  6907. ret = BAD_FUNC_ARG;
  6908. }
  6909. if (ret == 0) {
  6910. ret = wc_KyberKey_Init(type, kem, ssl->heap, INVALID_DEVID);
  6911. if (ret != 0) {
  6912. WOLFSSL_MSG("Error creating Kyber KEM");
  6913. }
  6914. }
  6915. if (ret == 0) {
  6916. ret = wc_KyberKey_SharedSecretSize(kem, &ssSz);
  6917. }
  6918. if (ret == 0) {
  6919. sharedSecretLen = ssSz;
  6920. switch (ecc_group) {
  6921. case WOLFSSL_ECC_SECP256R1:
  6922. sharedSecretLen += 32;
  6923. outlen = 32;
  6924. break;
  6925. case WOLFSSL_ECC_SECP384R1:
  6926. sharedSecretLen += 48;
  6927. outlen = 48;
  6928. break;
  6929. case WOLFSSL_ECC_SECP521R1:
  6930. sharedSecretLen += 66;
  6931. outlen = 66;
  6932. break;
  6933. default:
  6934. break;
  6935. }
  6936. ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
  6937. if (ret != 0) {
  6938. WOLFSSL_MSG("Memory allocation error.");
  6939. ret = MEMORY_E;
  6940. }
  6941. }
  6942. if (ret == 0) {
  6943. sharedSecret = (byte*)XMALLOC(sharedSecretLen, ssl->heap,
  6944. DYNAMIC_TYPE_TLSX);
  6945. if (sharedSecret == NULL) {
  6946. WOLFSSL_MSG("Memory allocation error.");
  6947. ret = MEMORY_E;
  6948. }
  6949. }
  6950. if (ret == 0) {
  6951. ret = wc_KyberKey_CipherTextSize(kem, &ctSz);
  6952. }
  6953. if (ret == 0) {
  6954. ret = wc_KyberKey_PrivateKeySize(kem, &privSz);
  6955. }
  6956. if (ret == 0) {
  6957. ret = wc_KyberKey_DecodePrivateKey(kem, keyShareEntry->privKey, privSz);
  6958. }
  6959. if (ret == 0) {
  6960. ret = wc_KyberKey_Decapsulate(kem, sharedSecret + outlen,
  6961. keyShareEntry->ke + keyShareEntry->keLen - ctSz, ctSz);
  6962. if (ret != 0) {
  6963. WOLFSSL_MSG("wc_KyberKey decapsulation failure.");
  6964. ret = BAD_FUNC_ARG;
  6965. }
  6966. }
  6967. if (ecc_group != 0) {
  6968. if (ret == 0) {
  6969. /* Point is validated by import function. */
  6970. ret = wc_ecc_import_x963(keyShareEntry->ke,
  6971. keyShareEntry->keLen - ctSz,
  6972. &eccpubkey);
  6973. if (ret != 0) {
  6974. WOLFSSL_MSG("ECC Public key import error.");
  6975. }
  6976. }
  6977. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  6978. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  6979. !defined(HAVE_SELFTEST)
  6980. if (ret == 0) {
  6981. ret = wc_ecc_set_rng(keyShareEntry->key, ssl->rng);
  6982. if (ret != 0) {
  6983. WOLFSSL_MSG("Failure to set the ECC private key RNG.");
  6984. }
  6985. }
  6986. #endif
  6987. if (ret == 0) {
  6988. PRIVATE_KEY_UNLOCK();
  6989. ret = wc_ecc_shared_secret(keyShareEntry->key, &eccpubkey,
  6990. sharedSecret, &outlen);
  6991. PRIVATE_KEY_LOCK();
  6992. if (outlen != sharedSecretLen - ssSz) {
  6993. WOLFSSL_MSG("ECC shared secret derivation error.");
  6994. ret = BAD_FUNC_ARG;
  6995. }
  6996. }
  6997. }
  6998. if ((ret == 0) && (sharedSecretLen > ENCRYPT_LEN)) {
  6999. WOLFSSL_MSG("shared secret is too long.");
  7000. ret = LENGTH_ERROR;
  7001. }
  7002. if (ret == 0) {
  7003. /* Copy the shared secret to the pre-master secret pre-allocated
  7004. * buffer. */
  7005. XMEMCPY(ssl->arrays->preMasterSecret, sharedSecret, sharedSecretLen);
  7006. ssl->arrays->preMasterSz = (word32) sharedSecretLen;
  7007. }
  7008. if (sharedSecret != NULL) {
  7009. XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_SECRET);
  7010. }
  7011. wc_ecc_free(&eccpubkey);
  7012. wc_KyberKey_Free(kem);
  7013. return ret;
  7014. }
  7015. #endif /* HAVE_PQC */
  7016. /* Process the key share extension on the client side.
  7017. *
  7018. * ssl The SSL/TLS object.
  7019. * keyShareEntry The key share entry object to use to calculate shared secret.
  7020. * returns 0 on success and other values indicate failure.
  7021. */
  7022. static int TLSX_KeyShare_Process(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  7023. {
  7024. int ret;
  7025. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  7026. ssl->session->namedGroup = keyShareEntry->group;
  7027. #endif
  7028. /* reset the pre master secret size */
  7029. if (ssl->arrays->preMasterSz == 0)
  7030. ssl->arrays->preMasterSz = ENCRYPT_LEN;
  7031. /* Use Key Share Data from server. */
  7032. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(keyShareEntry->group))
  7033. ret = TLSX_KeyShare_ProcessDh(ssl, keyShareEntry);
  7034. else if (keyShareEntry->group == WOLFSSL_ECC_X25519)
  7035. ret = TLSX_KeyShare_ProcessX25519(ssl, keyShareEntry);
  7036. else if (keyShareEntry->group == WOLFSSL_ECC_X448)
  7037. ret = TLSX_KeyShare_ProcessX448(ssl, keyShareEntry);
  7038. #ifdef HAVE_PQC
  7039. else if (WOLFSSL_NAMED_GROUP_IS_PQC(keyShareEntry->group))
  7040. ret = TLSX_KeyShare_ProcessPqc(ssl, keyShareEntry);
  7041. #endif
  7042. else
  7043. ret = TLSX_KeyShare_ProcessEcc(ssl, keyShareEntry);
  7044. #ifdef WOLFSSL_DEBUG_TLS
  7045. if (ret == 0) {
  7046. WOLFSSL_MSG("KE Secret");
  7047. WOLFSSL_BUFFER(ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz);
  7048. }
  7049. #endif
  7050. #ifdef WOLFSSL_ASYNC_CRYPT
  7051. keyShareEntry->lastRet = ret;
  7052. #endif
  7053. return ret;
  7054. }
  7055. /* Parse an entry of the KeyShare extension.
  7056. *
  7057. * ssl The SSL/TLS object.
  7058. * input The extension data.
  7059. * length The length of the extension data.
  7060. * kse The new key share entry object.
  7061. * returns a positive number to indicate amount of data parsed and a negative
  7062. * number on error.
  7063. */
  7064. static int TLSX_KeyShareEntry_Parse(const WOLFSSL* ssl, const byte* input,
  7065. word16 length, KeyShareEntry **kse, TLSX** extensions)
  7066. {
  7067. int ret;
  7068. word16 group;
  7069. word16 keLen;
  7070. int offset = 0;
  7071. byte* ke;
  7072. if (length < OPAQUE16_LEN + OPAQUE16_LEN)
  7073. return BUFFER_ERROR;
  7074. /* Named group */
  7075. ato16(&input[offset], &group);
  7076. offset += OPAQUE16_LEN;
  7077. /* Key exchange data - public key. */
  7078. ato16(&input[offset], &keLen);
  7079. offset += OPAQUE16_LEN;
  7080. if (keLen == 0)
  7081. return INVALID_PARAMETER;
  7082. if (keLen > length - offset)
  7083. return BUFFER_ERROR;
  7084. #ifdef HAVE_PQC
  7085. if (WOLFSSL_NAMED_GROUP_IS_PQC(group) &&
  7086. ssl->options.side == WOLFSSL_SERVER_END) {
  7087. /* For KEMs, the public key is not stored. Casting away const because
  7088. * we know for KEMs, it will be read-only.*/
  7089. ke = (byte *)&input[offset];
  7090. } else
  7091. #endif
  7092. {
  7093. /* Store a copy in the key share object. */
  7094. ke = (byte*)XMALLOC(keLen, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7095. if (ke == NULL)
  7096. return MEMORY_E;
  7097. XMEMCPY(ke, &input[offset], keLen);
  7098. }
  7099. /* Populate a key share object in the extension. */
  7100. ret = TLSX_KeyShare_Use(ssl, group, keLen, ke, kse, extensions);
  7101. if (ret != 0) {
  7102. if (ke != &input[offset]) {
  7103. XFREE(ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7104. }
  7105. return ret;
  7106. }
  7107. /* Total length of the parsed data. */
  7108. return offset + keLen;
  7109. }
  7110. /* Searches the groups sent for the specified named group.
  7111. *
  7112. * ssl SSL/TLS object.
  7113. * name Group name to match.
  7114. * returns 1 when the extension has the group name and 0 otherwise.
  7115. */
  7116. static int TLSX_KeyShare_Find(WOLFSSL* ssl, word16 group)
  7117. {
  7118. TLSX* extension;
  7119. KeyShareEntry* list;
  7120. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7121. if (extension == NULL) {
  7122. extension = TLSX_Find(ssl->ctx->extensions, TLSX_KEY_SHARE);
  7123. if (extension == NULL)
  7124. return 0;
  7125. }
  7126. list = (KeyShareEntry*)extension->data;
  7127. while (list != NULL) {
  7128. if (list->group == group)
  7129. return 1;
  7130. list = list->next;
  7131. }
  7132. return 0;
  7133. }
  7134. /* Searches the supported groups extension for the specified named group.
  7135. *
  7136. * ssl The SSL/TLS object.
  7137. * name The group name to match.
  7138. * returns 1 when the extension has the group name and 0 otherwise.
  7139. */
  7140. static int TLSX_SupportedGroups_Find(const WOLFSSL* ssl, word16 name,
  7141. TLSX* extensions)
  7142. {
  7143. #ifdef HAVE_SUPPORTED_CURVES
  7144. TLSX* extension;
  7145. SupportedCurve* curve = NULL;
  7146. if ((extension = TLSX_Find(extensions,
  7147. TLSX_SUPPORTED_GROUPS)) == NULL) {
  7148. if ((extension = TLSX_Find(ssl->ctx->extensions,
  7149. TLSX_SUPPORTED_GROUPS)) == NULL) {
  7150. return 0;
  7151. }
  7152. }
  7153. for (curve = (SupportedCurve*)extension->data; curve; curve = curve->next) {
  7154. if (curve->name == name)
  7155. return 1;
  7156. }
  7157. #endif
  7158. (void)ssl;
  7159. (void)name;
  7160. return 0;
  7161. }
  7162. int TLSX_KeyShare_Parse_ClientHello(const WOLFSSL* ssl,
  7163. const byte* input, word16 length, TLSX** extensions)
  7164. {
  7165. int ret;
  7166. int offset = 0;
  7167. word16 len;
  7168. TLSX* extension;
  7169. /* Add a KeyShare extension if it doesn't exist even if peer sent no
  7170. * entries. The presence of this extension signals that the peer can be
  7171. * negotiated with. */
  7172. extension = TLSX_Find(*extensions, TLSX_KEY_SHARE);
  7173. if (extension == NULL) {
  7174. /* Push new KeyShare extension. */
  7175. ret = TLSX_Push(extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  7176. if (ret != 0)
  7177. return ret;
  7178. }
  7179. if (length < OPAQUE16_LEN)
  7180. return BUFFER_ERROR;
  7181. /* ClientHello contains zero or more key share entries. */
  7182. ato16(input, &len);
  7183. if (len != length - OPAQUE16_LEN)
  7184. return BUFFER_ERROR;
  7185. offset += OPAQUE16_LEN;
  7186. while (offset < (int)length) {
  7187. ret = TLSX_KeyShareEntry_Parse(ssl, &input[offset],
  7188. length - (word16)offset, NULL, extensions);
  7189. if (ret < 0)
  7190. return ret;
  7191. offset += ret;
  7192. }
  7193. return 0;
  7194. }
  7195. /* Parse the KeyShare extension.
  7196. * Different formats in different messages.
  7197. *
  7198. * ssl The SSL/TLS object.
  7199. * input The extension data.
  7200. * length The length of the extension data.
  7201. * msgType The type of the message this extension is being parsed from.
  7202. * returns 0 on success and other values indicate failure.
  7203. */
  7204. int TLSX_KeyShare_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  7205. byte msgType)
  7206. {
  7207. int ret;
  7208. KeyShareEntry *keyShareEntry = NULL;
  7209. word16 group;
  7210. if (msgType == client_hello) {
  7211. ret = TLSX_KeyShare_Parse_ClientHello(ssl, input, length,
  7212. &ssl->extensions);
  7213. }
  7214. else if (msgType == server_hello) {
  7215. int len;
  7216. if (length < OPAQUE16_LEN)
  7217. return BUFFER_ERROR;
  7218. /* The data is the named group the server wants to use. */
  7219. ato16(input, &group);
  7220. /* Check the selected group was supported by ClientHello extensions. */
  7221. if (!TLSX_SupportedGroups_Find(ssl, group, ssl->extensions)) {
  7222. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7223. return BAD_KEY_SHARE_DATA;
  7224. }
  7225. /* Check if the group was sent. */
  7226. if (!TLSX_KeyShare_Find(ssl, group)) {
  7227. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7228. return BAD_KEY_SHARE_DATA;
  7229. }
  7230. /* ServerHello contains one key share entry. */
  7231. len = TLSX_KeyShareEntry_Parse(ssl, input, length, &keyShareEntry,
  7232. &ssl->extensions);
  7233. if (len != (int)length)
  7234. return BUFFER_ERROR;
  7235. /* Not in list sent if there isn't a private key. */
  7236. if (keyShareEntry == NULL || (keyShareEntry->key == NULL
  7237. #if !defined(NO_DH) || defined(HAVE_PQC)
  7238. && keyShareEntry->privKey == NULL
  7239. #endif
  7240. )) {
  7241. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7242. return BAD_KEY_SHARE_DATA;
  7243. }
  7244. /* Process the entry to calculate the secret. */
  7245. ret = TLSX_KeyShare_Process(ssl, keyShareEntry);
  7246. if (ret == 0)
  7247. ssl->session->namedGroup = ssl->namedGroup = group;
  7248. }
  7249. else if (msgType == hello_retry_request) {
  7250. if (length != OPAQUE16_LEN)
  7251. return BUFFER_ERROR;
  7252. /* The data is the named group the server wants to use. */
  7253. ato16(input, &group);
  7254. #ifdef WOLFSSL_ASYNC_CRYPT
  7255. /* only perform find and clear TLSX if not returning from async */
  7256. if (ssl->error != WC_PENDING_E)
  7257. #endif
  7258. {
  7259. /* Check the selected group was supported by ClientHello extensions. */
  7260. if (!TLSX_SupportedGroups_Find(ssl, group, ssl->extensions)) {
  7261. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7262. return BAD_KEY_SHARE_DATA;
  7263. }
  7264. /* Check if the group was sent. */
  7265. if (TLSX_KeyShare_Find(ssl, group)) {
  7266. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7267. return BAD_KEY_SHARE_DATA;
  7268. }
  7269. /* Clear out unusable key shares. */
  7270. ret = TLSX_KeyShare_Empty(ssl);
  7271. if (ret != 0)
  7272. return ret;
  7273. }
  7274. #ifdef HAVE_PQC
  7275. /* For post-quantum groups, do this in TLSX_PopulateExtensions(). */
  7276. if (!WOLFSSL_NAMED_GROUP_IS_PQC(group))
  7277. #endif
  7278. ret = TLSX_KeyShare_Use(ssl, group, 0, NULL, NULL, &ssl->extensions);
  7279. if (ret == 0)
  7280. ssl->session->namedGroup = ssl->namedGroup = group;
  7281. }
  7282. else {
  7283. /* Not a message type that is allowed to have this extension. */
  7284. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  7285. return SANITY_MSG_E;
  7286. }
  7287. return ret;
  7288. }
  7289. /* Create a new key share entry and put it into the list.
  7290. *
  7291. * list The linked list of key share entries.
  7292. * group The named group.
  7293. * heap The memory to allocate with.
  7294. * keyShareEntry The new key share entry object.
  7295. * returns 0 on success and other values indicate failure.
  7296. */
  7297. static int TLSX_KeyShare_New(KeyShareEntry** list, int group, void *heap,
  7298. KeyShareEntry** keyShareEntry)
  7299. {
  7300. KeyShareEntry* kse;
  7301. KeyShareEntry** next;
  7302. kse = (KeyShareEntry*)XMALLOC(sizeof(KeyShareEntry), heap,
  7303. DYNAMIC_TYPE_TLSX);
  7304. if (kse == NULL)
  7305. return MEMORY_E;
  7306. XMEMSET(kse, 0, sizeof(*kse));
  7307. kse->group = (word16)group;
  7308. /* Add it to the back and maintain the links. */
  7309. while (*list != NULL) {
  7310. /* Assign to temporary to work around compiler bug found by customer. */
  7311. next = &((*list)->next);
  7312. list = next;
  7313. }
  7314. *list = kse;
  7315. *keyShareEntry = kse;
  7316. (void)heap;
  7317. return 0;
  7318. }
  7319. #ifdef HAVE_PQC
  7320. static int server_generate_pqc_ciphertext(WOLFSSL* ssl,
  7321. KeyShareEntry* keyShareEntry, byte* data, word16 len)
  7322. {
  7323. /* I am the server. The data parameter is the client's public key. I need
  7324. * to generate the public information (AKA ciphertext) and shared secret
  7325. * here. Note the "public information" is equivalent to a the public key in
  7326. * key exchange parlance. That's why it is being assigned to pubKey.
  7327. */
  7328. int type;
  7329. KyberKey kem[1];
  7330. byte* sharedSecret = NULL;
  7331. byte* ciphertext = NULL;
  7332. int ret = 0;
  7333. int oqs_group = 0;
  7334. int ecc_group = 0;
  7335. KeyShareEntry *ecc_kse = NULL;
  7336. ecc_key eccpubkey;
  7337. word32 outlen = 0;
  7338. word32 pubSz = 0;
  7339. word32 ctSz = 0;
  7340. word32 ssSz = 0;
  7341. findEccPqc(&ecc_group, &oqs_group, keyShareEntry->group);
  7342. ret = kyber_id2type(oqs_group, &type);
  7343. if (ret != 0) {
  7344. WOLFSSL_MSG("Invalid Kyber algorithm specified.");
  7345. ret = BAD_FUNC_ARG;
  7346. }
  7347. if (ret == 0) {
  7348. ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
  7349. if (ret != 0) {
  7350. WOLFSSL_MSG("Could not do ECC public key initialization.");
  7351. ret = MEMORY_E;
  7352. }
  7353. }
  7354. if (ret == 0) {
  7355. ecc_kse = (KeyShareEntry*)XMALLOC(sizeof(*ecc_kse), ssl->heap,
  7356. DYNAMIC_TYPE_TLSX);
  7357. if (ecc_kse == NULL) {
  7358. WOLFSSL_MSG("ecc_kse memory allocation failure");
  7359. ret = MEMORY_ERROR;
  7360. }
  7361. }
  7362. if (ret == 0) {
  7363. XMEMSET(ecc_kse, 0, sizeof(*ecc_kse));
  7364. }
  7365. if (ret == 0 && ecc_group != 0) {
  7366. ecc_kse->group = ecc_group;
  7367. ret = TLSX_KeyShare_GenEccKey(ssl, ecc_kse);
  7368. if (ret != 0) {
  7369. /* No message, TLSX_KeyShare_GenEccKey() will do it. */
  7370. return ret;
  7371. }
  7372. ret = 0;
  7373. }
  7374. if (ret == 0) {
  7375. ret = wc_KyberKey_Init(type, kem, ssl->heap, INVALID_DEVID);
  7376. if (ret != 0) {
  7377. WOLFSSL_MSG("Error creating Kyber KEM");
  7378. }
  7379. }
  7380. if (ret == 0) {
  7381. ret = wc_KyberKey_PublicKeySize(kem, &pubSz);
  7382. }
  7383. if (ret == 0) {
  7384. ret = wc_KyberKey_CipherTextSize(kem, &ctSz);
  7385. }
  7386. if (ret == 0) {
  7387. ret = wc_KyberKey_SharedSecretSize(kem, &ssSz);
  7388. }
  7389. if (ret == 0 && len != pubSz + ecc_kse->pubKeyLen) {
  7390. WOLFSSL_MSG("Invalid public key.");
  7391. ret = BAD_FUNC_ARG;
  7392. }
  7393. if (ret == 0) {
  7394. sharedSecret = (byte*)XMALLOC(ecc_kse->keyLen + ssSz, ssl->heap,
  7395. DYNAMIC_TYPE_TLSX);
  7396. ciphertext = (byte*)XMALLOC(ecc_kse->pubKeyLen + ctSz, ssl->heap,
  7397. DYNAMIC_TYPE_TLSX);
  7398. if (sharedSecret == NULL || ciphertext == NULL) {
  7399. WOLFSSL_MSG("Ciphertext/shared secret memory allocation failure.");
  7400. ret = MEMORY_E;
  7401. }
  7402. }
  7403. if (ecc_group != 0) {
  7404. if (ret == 0) {
  7405. /* Point is validated by import function. */
  7406. ret = wc_ecc_import_x963(data, len - pubSz, &eccpubkey);
  7407. if (ret != 0) {
  7408. WOLFSSL_MSG("Bad ECC public key.");
  7409. }
  7410. }
  7411. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  7412. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  7413. !defined(HAVE_SELFTEST)
  7414. if (ret == 0) {
  7415. ret = wc_ecc_set_rng(ecc_kse->key, ssl->rng);
  7416. }
  7417. #endif
  7418. if (ret == 0) {
  7419. outlen = ecc_kse->keyLen;
  7420. PRIVATE_KEY_UNLOCK();
  7421. ret = wc_ecc_shared_secret(ecc_kse->key, &eccpubkey,
  7422. sharedSecret,
  7423. &outlen);
  7424. PRIVATE_KEY_LOCK();
  7425. if (outlen != ecc_kse->keyLen) {
  7426. WOLFSSL_MSG("Data length mismatch.");
  7427. ret = BAD_FUNC_ARG;
  7428. }
  7429. }
  7430. }
  7431. if (ret == 0) {
  7432. ret = wc_KyberKey_DecodePublicKey(kem, data + ecc_kse->pubKeyLen,
  7433. pubSz);
  7434. }
  7435. if (ret == 0) {
  7436. ret = wc_KyberKey_Encapsulate(kem, ciphertext + ecc_kse->pubKeyLen,
  7437. sharedSecret + outlen, ssl->rng);
  7438. if (ret != 0) {
  7439. WOLFSSL_MSG("wc_KyberKey encapsulation failure.");
  7440. }
  7441. }
  7442. if (ret == 0) {
  7443. if (keyShareEntry->ke != NULL) {
  7444. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7445. }
  7446. keyShareEntry->ke = sharedSecret;
  7447. keyShareEntry->keLen = outlen + ssSz;
  7448. sharedSecret = NULL;
  7449. XMEMCPY(ciphertext, ecc_kse->pubKey, ecc_kse->pubKeyLen);
  7450. keyShareEntry->pubKey = ciphertext;
  7451. keyShareEntry->pubKeyLen = (word32)(ecc_kse->pubKeyLen + ctSz);
  7452. ciphertext = NULL;
  7453. }
  7454. TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
  7455. if (sharedSecret != NULL)
  7456. XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_TLSX);
  7457. if (ciphertext != NULL)
  7458. XFREE(ciphertext, ssl->heap, DYNAMIC_TYPE_TLSX);
  7459. wc_ecc_free(&eccpubkey);
  7460. wc_KyberKey_Free(kem);
  7461. return ret;
  7462. }
  7463. #endif /* HAVE_PQC */
  7464. /* Use the data to create a new key share object in the extensions.
  7465. *
  7466. * ssl The SSL/TLS object.
  7467. * group The named group.
  7468. * len The length of the public key data.
  7469. * data The public key data.
  7470. * kse The new key share entry object.
  7471. * returns 0 on success and other values indicate failure.
  7472. */
  7473. int TLSX_KeyShare_Use(const WOLFSSL* ssl, word16 group, word16 len, byte* data,
  7474. KeyShareEntry **kse, TLSX** extensions)
  7475. {
  7476. int ret = 0;
  7477. TLSX* extension;
  7478. KeyShareEntry* keyShareEntry = NULL;
  7479. /* Find the KeyShare extension if it exists. */
  7480. extension = TLSX_Find(*extensions, TLSX_KEY_SHARE);
  7481. if (extension == NULL) {
  7482. /* Push new KeyShare extension. */
  7483. ret = TLSX_Push(extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  7484. if (ret != 0)
  7485. return ret;
  7486. extension = TLSX_Find(*extensions, TLSX_KEY_SHARE);
  7487. if (extension == NULL)
  7488. return MEMORY_E;
  7489. }
  7490. extension->resp = 0;
  7491. /* Try to find the key share entry with this group. */
  7492. keyShareEntry = (KeyShareEntry*)extension->data;
  7493. while (keyShareEntry != NULL) {
  7494. if (keyShareEntry->group == group)
  7495. break;
  7496. keyShareEntry = keyShareEntry->next;
  7497. }
  7498. /* Create a new key share entry if not found. */
  7499. if (keyShareEntry == NULL) {
  7500. ret = TLSX_KeyShare_New((KeyShareEntry**)&extension->data, group,
  7501. ssl->heap, &keyShareEntry);
  7502. if (ret != 0)
  7503. return ret;
  7504. }
  7505. #ifdef HAVE_PQC
  7506. if (WOLFSSL_NAMED_GROUP_IS_PQC(group) &&
  7507. ssl->options.side == WOLFSSL_SERVER_END) {
  7508. ret = server_generate_pqc_ciphertext((WOLFSSL*)ssl, keyShareEntry, data,
  7509. len);
  7510. if (ret != 0)
  7511. return ret;
  7512. }
  7513. else
  7514. #endif
  7515. if (data != NULL) {
  7516. if (keyShareEntry->ke != NULL) {
  7517. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7518. }
  7519. keyShareEntry->ke = data;
  7520. keyShareEntry->keLen = len;
  7521. }
  7522. else {
  7523. /* Generate a key pair. Casting to non-const since changes inside are
  7524. * minimal but would require an extensive redesign to refactor. Also
  7525. * this path shouldn't be taken when parsing a ClientHello in stateless
  7526. * mode. */
  7527. ret = TLSX_KeyShare_GenKey((WOLFSSL*)ssl, keyShareEntry);
  7528. if (ret != 0)
  7529. return ret;
  7530. }
  7531. if (kse != NULL)
  7532. *kse = keyShareEntry;
  7533. return 0;
  7534. }
  7535. /* Set an empty Key Share extension.
  7536. *
  7537. * ssl The SSL/TLS object.
  7538. * returns 0 on success and other values indicate failure.
  7539. */
  7540. int TLSX_KeyShare_Empty(WOLFSSL* ssl)
  7541. {
  7542. int ret = 0;
  7543. TLSX* extension;
  7544. /* Find the KeyShare extension if it exists. */
  7545. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7546. if (extension == NULL) {
  7547. /* Push new KeyShare extension. */
  7548. ret = TLSX_Push(&ssl->extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  7549. }
  7550. else if (extension->data != NULL) {
  7551. TLSX_KeyShare_FreeAll((KeyShareEntry*)extension->data, ssl->heap);
  7552. extension->data = NULL;
  7553. }
  7554. return ret;
  7555. }
  7556. /* Returns whether this group is supported.
  7557. *
  7558. * namedGroup The named group to check.
  7559. * returns 1 when supported or 0 otherwise.
  7560. */
  7561. static int TLSX_KeyShare_IsSupported(int namedGroup)
  7562. {
  7563. switch (namedGroup) {
  7564. #ifdef HAVE_FFDHE_2048
  7565. case WOLFSSL_FFDHE_2048:
  7566. break;
  7567. #endif
  7568. #ifdef HAVE_FFDHE_3072
  7569. case WOLFSSL_FFDHE_3072:
  7570. break;
  7571. #endif
  7572. #ifdef HAVE_FFDHE_4096
  7573. case WOLFSSL_FFDHE_4096:
  7574. break;
  7575. #endif
  7576. #ifdef HAVE_FFDHE_6144
  7577. case WOLFSSL_FFDHE_6144:
  7578. break;
  7579. #endif
  7580. #ifdef HAVE_FFDHE_8192
  7581. case WOLFSSL_FFDHE_8192:
  7582. break;
  7583. #endif
  7584. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  7585. #ifdef HAVE_ECC_KOBLITZ
  7586. case WOLFSSL_ECC_SECP256K1:
  7587. break;
  7588. #endif
  7589. #ifndef NO_ECC_SECP
  7590. case WOLFSSL_ECC_SECP256R1:
  7591. break;
  7592. #endif /* !NO_ECC_SECP */
  7593. #ifdef HAVE_ECC_BRAINPOOL
  7594. case WOLFSSL_ECC_BRAINPOOLP256R1:
  7595. break;
  7596. #endif
  7597. #ifdef WOLFSSL_SM2
  7598. case WOLFSSL_ECC_SM2P256V1:
  7599. break;
  7600. #endif /* WOLFSSL_SM2 */
  7601. #endif
  7602. #if defined(HAVE_CURVE25519) && ECC_MIN_KEY_SZ <= 256
  7603. case WOLFSSL_ECC_X25519:
  7604. break;
  7605. #endif
  7606. #if defined(HAVE_CURVE448) && ECC_MIN_KEY_SZ <= 448
  7607. case WOLFSSL_ECC_X448:
  7608. break;
  7609. #endif
  7610. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  7611. #ifndef NO_ECC_SECP
  7612. case WOLFSSL_ECC_SECP384R1:
  7613. break;
  7614. #endif /* !NO_ECC_SECP */
  7615. #ifdef HAVE_ECC_BRAINPOOL
  7616. case WOLFSSL_ECC_BRAINPOOLP384R1:
  7617. break;
  7618. #endif
  7619. #endif
  7620. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  7621. #ifndef NO_ECC_SECP
  7622. case WOLFSSL_ECC_SECP521R1:
  7623. break;
  7624. #endif /* !NO_ECC_SECP */
  7625. #endif
  7626. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  7627. #ifdef HAVE_ECC_KOBLITZ
  7628. case WOLFSSL_ECC_SECP160K1:
  7629. break;
  7630. #endif
  7631. #ifndef NO_ECC_SECP
  7632. case WOLFSSL_ECC_SECP160R1:
  7633. break;
  7634. #endif
  7635. #ifdef HAVE_ECC_SECPR2
  7636. case WOLFSSL_ECC_SECP160R2:
  7637. break;
  7638. #endif
  7639. #endif
  7640. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  7641. #ifdef HAVE_ECC_KOBLITZ
  7642. case WOLFSSL_ECC_SECP192K1:
  7643. break;
  7644. #endif
  7645. #ifndef NO_ECC_SECP
  7646. case WOLFSSL_ECC_SECP192R1:
  7647. break;
  7648. #endif
  7649. #endif
  7650. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  7651. #ifdef HAVE_ECC_KOBLITZ
  7652. case WOLFSSL_ECC_SECP224K1:
  7653. break;
  7654. #endif
  7655. #ifndef NO_ECC_SECP
  7656. case WOLFSSL_ECC_SECP224R1:
  7657. break;
  7658. #endif
  7659. #endif
  7660. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  7661. #ifdef HAVE_ECC_BRAINPOOL
  7662. case WOLFSSL_ECC_BRAINPOOLP512R1:
  7663. break;
  7664. #endif
  7665. #endif
  7666. #ifdef HAVE_PQC
  7667. #ifdef WOLFSSL_WC_KYBER
  7668. #ifdef WOLFSSL_KYBER512
  7669. case WOLFSSL_KYBER_LEVEL1:
  7670. #endif
  7671. #ifdef WOLFSSL_KYBER768
  7672. case WOLFSSL_KYBER_LEVEL3:
  7673. #endif
  7674. #ifdef WOLFSSL_KYBER1024
  7675. case WOLFSSL_KYBER_LEVEL5:
  7676. #endif
  7677. break;
  7678. #elif defined(HAVE_LIBOQS)
  7679. case WOLFSSL_KYBER_LEVEL1:
  7680. case WOLFSSL_KYBER_LEVEL3:
  7681. case WOLFSSL_KYBER_LEVEL5:
  7682. case WOLFSSL_P256_KYBER_LEVEL1:
  7683. case WOLFSSL_P384_KYBER_LEVEL3:
  7684. case WOLFSSL_P521_KYBER_LEVEL5:
  7685. {
  7686. int ret;
  7687. int id;
  7688. findEccPqc(NULL, &namedGroup, namedGroup);
  7689. ret = kyber_id2type(namedGroup, &id);
  7690. if (ret == NOT_COMPILED_IN) {
  7691. return 0;
  7692. }
  7693. if (! ext_kyber_enabled(id)) {
  7694. return 0;
  7695. }
  7696. break;
  7697. }
  7698. #elif defined(HAVE_PQM4)
  7699. case WOLFSSL_KYBER_LEVEL1:
  7700. break;
  7701. #endif
  7702. #endif /* HAVE_PQC */
  7703. default:
  7704. return 0;
  7705. }
  7706. return 1;
  7707. }
  7708. static const word16 preferredGroup[] = {
  7709. #if defined(HAVE_ECC) && (!defined(NO_ECC256) || \
  7710. defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP) && ECC_MIN_KEY_SZ <= 256
  7711. WOLFSSL_ECC_SECP256R1,
  7712. #if !defined(HAVE_FIPS) && defined(WOLFSSL_SM2)
  7713. WOLFSSL_ECC_SM2P256V1,
  7714. #endif
  7715. #endif
  7716. #if defined(HAVE_CURVE25519) && ECC_MIN_KEY_SZ <= 256
  7717. WOLFSSL_ECC_X25519,
  7718. #endif
  7719. #if defined(HAVE_CURVE448) && ECC_MIN_KEY_SZ <= 448
  7720. WOLFSSL_ECC_X448,
  7721. #endif
  7722. #if defined(HAVE_ECC) && (!defined(NO_ECC384) || \
  7723. defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP) && ECC_MIN_KEY_SZ <= 384
  7724. WOLFSSL_ECC_SECP384R1,
  7725. #endif
  7726. #if defined(HAVE_ECC) && (!defined(NO_ECC521) || \
  7727. defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP) && ECC_MIN_KEY_SZ <= 521
  7728. WOLFSSL_ECC_SECP521R1,
  7729. #endif
  7730. #if defined(HAVE_FFDHE_2048)
  7731. WOLFSSL_FFDHE_2048,
  7732. #endif
  7733. #if defined(HAVE_FFDHE_3072)
  7734. WOLFSSL_FFDHE_3072,
  7735. #endif
  7736. #if defined(HAVE_FFDHE_4096)
  7737. WOLFSSL_FFDHE_4096,
  7738. #endif
  7739. #if defined(HAVE_FFDHE_6144)
  7740. WOLFSSL_FFDHE_6144,
  7741. #endif
  7742. #if defined(HAVE_FFDHE_8192)
  7743. WOLFSSL_FFDHE_8192,
  7744. #endif
  7745. #ifdef WOLFSSL_WC_KYBER
  7746. #ifdef WOLFSSL_KYBER512
  7747. WOLFSSL_KYBER_LEVEL1,
  7748. #endif
  7749. #ifdef WOLFSSL_KYBER768
  7750. WOLFSSL_KYBER_LEVEL3,
  7751. #endif
  7752. #ifdef WOLFSSL_KYBER1024
  7753. WOLFSSL_KYBER_LEVEL5,
  7754. #endif
  7755. #elif defined(HAVE_LIBOQS)
  7756. /* These require a runtime call to TLSX_KeyShare_IsSupported to use */
  7757. WOLFSSL_KYBER_LEVEL1,
  7758. WOLFSSL_KYBER_LEVEL3,
  7759. WOLFSSL_KYBER_LEVEL5,
  7760. WOLFSSL_P256_KYBER_LEVEL1,
  7761. WOLFSSL_P384_KYBER_LEVEL3,
  7762. WOLFSSL_P521_KYBER_LEVEL5,
  7763. #elif defined(HAVE_PQM4)
  7764. WOLFSSL_KYBER_LEVEL1,
  7765. #endif
  7766. WOLFSSL_NAMED_GROUP_INVALID
  7767. };
  7768. #define PREFERRED_GROUP_SZ \
  7769. ((sizeof(preferredGroup)/sizeof(*preferredGroup)) - 1)
  7770. /* -1 for the invalid group */
  7771. /* Examines the application specified group ranking and returns the rank of the
  7772. * group.
  7773. * If no group ranking set then all groups are rank 0 (highest).
  7774. *
  7775. * ssl The SSL/TLS object.
  7776. * group The group to check ranking for.
  7777. * returns ranking from 0 to MAX_GROUP_COUNT-1 or -1 when group not in list.
  7778. */
  7779. static int TLSX_KeyShare_GroupRank(const WOLFSSL* ssl, int group)
  7780. {
  7781. byte i;
  7782. const word16* groups;
  7783. byte numGroups;
  7784. if (ssl->numGroups == 0) {
  7785. groups = preferredGroup;
  7786. numGroups = PREFERRED_GROUP_SZ;
  7787. }
  7788. else {
  7789. groups = ssl->group;
  7790. numGroups = ssl->numGroups;
  7791. }
  7792. #ifdef HAVE_LIBOQS
  7793. if (!TLSX_KeyShare_IsSupported(group))
  7794. return -1;
  7795. #endif
  7796. for (i = 0; i < numGroups; i++)
  7797. if (groups[i] == (word16)group)
  7798. return i;
  7799. return -1;
  7800. }
  7801. /* Set a key share that is supported by the client into extensions.
  7802. *
  7803. * ssl The SSL/TLS object.
  7804. * returns BAD_KEY_SHARE_DATA if no supported group has a key share,
  7805. * 0 if a supported group has a key share and other values indicate an error.
  7806. */
  7807. int TLSX_KeyShare_SetSupported(const WOLFSSL* ssl, TLSX** extensions)
  7808. {
  7809. int ret;
  7810. #ifdef HAVE_SUPPORTED_CURVES
  7811. TLSX* extension;
  7812. SupportedCurve* curve = NULL;
  7813. SupportedCurve* preferredCurve = NULL;
  7814. KeyShareEntry* kse = NULL;
  7815. int preferredRank = WOLFSSL_MAX_GROUP_COUNT;
  7816. int rank;
  7817. extension = TLSX_Find(*extensions, TLSX_SUPPORTED_GROUPS);
  7818. if (extension != NULL)
  7819. curve = (SupportedCurve*)extension->data;
  7820. /* Use server's preference order. */
  7821. for (; curve != NULL; curve = curve->next) {
  7822. if (!TLSX_KeyShare_IsSupported(curve->name))
  7823. continue;
  7824. if (wolfSSL_curve_is_disabled(ssl, curve->name))
  7825. continue;
  7826. rank = TLSX_KeyShare_GroupRank(ssl, curve->name);
  7827. if (rank == -1)
  7828. continue;
  7829. if (rank < preferredRank) {
  7830. preferredCurve = curve;
  7831. preferredRank = rank;
  7832. }
  7833. }
  7834. curve = preferredCurve;
  7835. if (curve == NULL) {
  7836. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7837. return BAD_KEY_SHARE_DATA;
  7838. }
  7839. #ifdef WOLFSSL_ASYNC_CRYPT
  7840. /* Check the old key share data list. */
  7841. extension = TLSX_Find(*extensions, TLSX_KEY_SHARE);
  7842. if (extension != NULL) {
  7843. kse = (KeyShareEntry*)extension->data;
  7844. /* We should not be computing keys if we are only going to advertise
  7845. * our choice here. */
  7846. if (kse != NULL && kse->lastRet == WC_PENDING_E) {
  7847. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7848. return BAD_KEY_SHARE_DATA;
  7849. }
  7850. }
  7851. #endif
  7852. /* Push new KeyShare extension. This will also free the old one */
  7853. ret = TLSX_Push(extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  7854. if (ret != 0)
  7855. return ret;
  7856. /* Extension got pushed to head */
  7857. extension = *extensions;
  7858. /* Push the selected curve */
  7859. ret = TLSX_KeyShare_New((KeyShareEntry**)&extension->data, curve->name,
  7860. ssl->heap, &kse);
  7861. if (ret != 0)
  7862. return ret;
  7863. /* Set extension to be in response. */
  7864. extension->resp = 1;
  7865. #else
  7866. (void)ssl;
  7867. WOLFSSL_ERROR_VERBOSE(NOT_COMPILED_IN);
  7868. ret = NOT_COMPILED_IN;
  7869. #endif
  7870. return ret;
  7871. }
  7872. /* Server side KSE processing */
  7873. int TLSX_KeyShare_Choose(const WOLFSSL *ssl, TLSX* extensions,
  7874. byte cipherSuite0, byte cipherSuite, KeyShareEntry** kse, byte* searched)
  7875. {
  7876. TLSX* extension;
  7877. KeyShareEntry* clientKSE = NULL;
  7878. KeyShareEntry* list = NULL;
  7879. KeyShareEntry* preferredKSE = NULL;
  7880. int preferredRank = WOLFSSL_MAX_GROUP_COUNT;
  7881. int rank;
  7882. (void)cipherSuite0;
  7883. (void)cipherSuite;
  7884. if (ssl == NULL || ssl->options.side != WOLFSSL_SERVER_END)
  7885. return BAD_FUNC_ARG;
  7886. *searched = 0;
  7887. /* Find the KeyShare extension if it exists. */
  7888. extension = TLSX_Find(extensions, TLSX_KEY_SHARE);
  7889. if (extension != NULL)
  7890. list = (KeyShareEntry*)extension->data;
  7891. if (extension && extension->resp == 1) {
  7892. /* Outside of the async case this path should not be taken. */
  7893. int ret = INCOMPLETE_DATA;
  7894. #ifdef WOLFSSL_ASYNC_CRYPT
  7895. /* in async case make sure key generation is finalized */
  7896. KeyShareEntry* serverKSE = (KeyShareEntry*)extension->data;
  7897. if (serverKSE && serverKSE->lastRet == WC_PENDING_E) {
  7898. if (ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE)
  7899. *searched = 1;
  7900. ret = TLSX_KeyShare_GenKey((WOLFSSL*)ssl, serverKSE);
  7901. }
  7902. #endif
  7903. return ret;
  7904. }
  7905. /* Use server's preference order. */
  7906. for (clientKSE = list; clientKSE != NULL; clientKSE = clientKSE->next) {
  7907. if (clientKSE->ke == NULL)
  7908. continue;
  7909. #ifdef WOLFSSL_SM2
  7910. if ((cipherSuite0 == CIPHER_BYTE) &&
  7911. ((cipherSuite == TLS_SM4_GCM_SM3) ||
  7912. (cipherSuite == TLS_SM4_CCM_SM3))) {
  7913. if (clientKSE->group != WOLFSSL_ECC_SM2P256V1) {
  7914. continue;
  7915. }
  7916. }
  7917. else if (clientKSE->group == WOLFSSL_ECC_SM2P256V1) {
  7918. continue;
  7919. }
  7920. #endif
  7921. /* Check consistency now - extensions in any order. */
  7922. if (!TLSX_SupportedGroups_Find(ssl, clientKSE->group, extensions))
  7923. continue;
  7924. if (!WOLFSSL_NAMED_GROUP_IS_FFHDE(clientKSE->group)) {
  7925. /* Check max value supported. */
  7926. if (clientKSE->group > WOLFSSL_ECC_MAX) {
  7927. #ifdef HAVE_PQC
  7928. if (!WOLFSSL_NAMED_GROUP_IS_PQC(clientKSE->group))
  7929. #endif
  7930. continue;
  7931. }
  7932. if (wolfSSL_curve_is_disabled(ssl, clientKSE->group))
  7933. continue;
  7934. }
  7935. if (!TLSX_KeyShare_IsSupported(clientKSE->group))
  7936. continue;
  7937. rank = TLSX_KeyShare_GroupRank(ssl, clientKSE->group);
  7938. if (rank == -1)
  7939. continue;
  7940. if (rank < preferredRank) {
  7941. preferredKSE = clientKSE;
  7942. preferredRank = rank;
  7943. }
  7944. }
  7945. *kse = preferredKSE;
  7946. *searched = 1;
  7947. return 0;
  7948. }
  7949. /* Server side KSE processing */
  7950. int TLSX_KeyShare_Setup(WOLFSSL *ssl, KeyShareEntry* clientKSE)
  7951. {
  7952. int ret;
  7953. TLSX* extension;
  7954. KeyShareEntry* serverKSE;
  7955. KeyShareEntry* list = NULL;
  7956. if (ssl == NULL || ssl->options.side != WOLFSSL_SERVER_END)
  7957. return BAD_FUNC_ARG;
  7958. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7959. if (extension == NULL)
  7960. return BAD_STATE_E;
  7961. if (clientKSE == NULL) {
  7962. #ifdef WOLFSSL_ASYNC_CRYPT
  7963. /* Not necessarily an error. The key may have already been setup. */
  7964. if (extension != NULL && extension->resp == 1) {
  7965. serverKSE = (KeyShareEntry*)extension->data;
  7966. if (serverKSE != NULL) {
  7967. /* in async case make sure key generation is finalized */
  7968. if (serverKSE->lastRet == WC_PENDING_E)
  7969. return TLSX_KeyShare_GenKey((WOLFSSL*)ssl, serverKSE);
  7970. else if (serverKSE->lastRet == 0)
  7971. return 0;
  7972. }
  7973. }
  7974. #endif
  7975. return BAD_FUNC_ARG;
  7976. }
  7977. /* Generate a new key pair except in the case of OQS KEM because we
  7978. * are going to encapsulate and that does not require us to generate a
  7979. * key pair.
  7980. */
  7981. ret = TLSX_KeyShare_New(&list, clientKSE->group, ssl->heap, &serverKSE);
  7982. if (ret != 0)
  7983. return ret;
  7984. if (clientKSE->key == NULL) {
  7985. #ifdef HAVE_PQC
  7986. if (WOLFSSL_NAMED_GROUP_IS_PQC(clientKSE->group)) {
  7987. /* Going to need the public key (AKA ciphertext). */
  7988. serverKSE->pubKey = clientKSE->pubKey;
  7989. clientKSE->pubKey = NULL;
  7990. serverKSE->pubKeyLen = clientKSE->pubKeyLen;
  7991. clientKSE->pubKeyLen = 0;
  7992. }
  7993. else
  7994. #endif
  7995. {
  7996. ret = TLSX_KeyShare_GenKey(ssl, serverKSE);
  7997. }
  7998. /* for async do setup of serverKSE below, but return WC_PENDING_E */
  7999. if (ret != 0
  8000. #ifdef WOLFSSL_ASYNC_CRYPT
  8001. && ret != WC_PENDING_E
  8002. #endif
  8003. ) {
  8004. TLSX_KeyShare_FreeAll(list, ssl->heap);
  8005. return ret;
  8006. }
  8007. }
  8008. else {
  8009. /* transfer buffers to serverKSE */
  8010. serverKSE->key = clientKSE->key;
  8011. clientKSE->key = NULL;
  8012. serverKSE->keyLen = clientKSE->keyLen;
  8013. serverKSE->pubKey = clientKSE->pubKey;
  8014. clientKSE->pubKey = NULL;
  8015. serverKSE->pubKeyLen = clientKSE->pubKeyLen;
  8016. #ifndef NO_DH
  8017. serverKSE->privKey = clientKSE->privKey;
  8018. clientKSE->privKey = NULL;
  8019. #endif
  8020. }
  8021. serverKSE->ke = clientKSE->ke;
  8022. serverKSE->keLen = clientKSE->keLen;
  8023. clientKSE->ke = NULL;
  8024. clientKSE->keLen = 0;
  8025. TLSX_KeyShare_FreeAll((KeyShareEntry*)extension->data, ssl->heap);
  8026. extension->data = (void *)serverKSE;
  8027. extension->resp = 1;
  8028. return ret;
  8029. }
  8030. /* Ensure there is a key pair that can be used for key exchange.
  8031. *
  8032. * ssl The SSL/TLS object.
  8033. * doHelloRetry If set to non-zero will do hello_retry
  8034. * returns 0 on success and other values indicate failure.
  8035. */
  8036. int TLSX_KeyShare_Establish(WOLFSSL *ssl, int* doHelloRetry)
  8037. {
  8038. int ret;
  8039. KeyShareEntry* clientKSE = NULL;
  8040. byte searched = 0;
  8041. *doHelloRetry = 0;
  8042. ret = TLSX_KeyShare_Choose(ssl, ssl->extensions, ssl->cipher.cipherSuite0,
  8043. ssl->cipher.cipherSuite, &clientKSE, &searched);
  8044. if (ret != 0 || !searched)
  8045. return ret;
  8046. /* No supported group found - send HelloRetryRequest. */
  8047. if (clientKSE == NULL) {
  8048. /* Set KEY_SHARE_ERROR to indicate HelloRetryRequest required. */
  8049. *doHelloRetry = 1;
  8050. return TLSX_KeyShare_SetSupported(ssl, &ssl->extensions);
  8051. }
  8052. return TLSX_KeyShare_Setup(ssl, clientKSE);
  8053. }
  8054. /* Derive the shared secret of the key exchange.
  8055. *
  8056. * ssl The SSL/TLS object.
  8057. * returns 0 on success and other values indicate failure.
  8058. */
  8059. int TLSX_KeyShare_DeriveSecret(WOLFSSL *ssl)
  8060. {
  8061. int ret;
  8062. TLSX* extension;
  8063. KeyShareEntry* list = NULL;
  8064. #ifdef WOLFSSL_ASYNC_CRYPT
  8065. ret = wolfSSL_AsyncPop(ssl, NULL);
  8066. /* Check for error */
  8067. if (ret != WC_NOT_PENDING_E && ret < 0) {
  8068. return ret;
  8069. }
  8070. #endif
  8071. /* Find the KeyShare extension if it exists. */
  8072. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  8073. if (extension != NULL)
  8074. list = (KeyShareEntry*)extension->data;
  8075. if (list == NULL)
  8076. return KEY_SHARE_ERROR;
  8077. /* Calculate secret. */
  8078. ret = TLSX_KeyShare_Process(ssl, list);
  8079. return ret;
  8080. }
  8081. #define KS_FREE_ALL TLSX_KeyShare_FreeAll
  8082. #define KS_GET_SIZE TLSX_KeyShare_GetSize
  8083. #define KS_WRITE TLSX_KeyShare_Write
  8084. #define KS_PARSE TLSX_KeyShare_Parse
  8085. #else
  8086. #define KS_FREE_ALL(a, b)
  8087. #define KS_GET_SIZE(a, b) 0
  8088. #define KS_WRITE(a, b, c) 0
  8089. #define KS_PARSE(a, b, c, d) 0
  8090. #endif /* WOLFSSL_TLS13 */
  8091. /******************************************************************************/
  8092. /* Pre-Shared Key */
  8093. /******************************************************************************/
  8094. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  8095. /* Free the pre-shared key dynamic data.
  8096. *
  8097. * list The linked list of key share entry objects.
  8098. * heap The heap used for allocation.
  8099. */
  8100. static void TLSX_PreSharedKey_FreeAll(PreSharedKey* list, void* heap)
  8101. {
  8102. PreSharedKey* current;
  8103. while ((current = list) != NULL) {
  8104. list = current->next;
  8105. XFREE(current->identity, heap, DYNAMIC_TYPE_TLSX);
  8106. XFREE(current, heap, DYNAMIC_TYPE_TLSX);
  8107. }
  8108. (void)heap;
  8109. }
  8110. /* Get the size of the encoded pre shared key extension.
  8111. *
  8112. * list The linked list of pre-shared key extensions.
  8113. * msgType The type of the message this extension is being written into.
  8114. * returns the number of bytes of the encoded pre-shared key extension or
  8115. * SANITY_MSG_E to indicate invalid message type.
  8116. */
  8117. static int TLSX_PreSharedKey_GetSize(PreSharedKey* list, byte msgType,
  8118. word16* pSz)
  8119. {
  8120. if (msgType == client_hello) {
  8121. /* Length of identities + Length of binders. */
  8122. word16 len = OPAQUE16_LEN + OPAQUE16_LEN;
  8123. while (list != NULL) {
  8124. /* Each entry has: identity, ticket age and binder. */
  8125. len += OPAQUE16_LEN + list->identityLen + OPAQUE32_LEN +
  8126. OPAQUE8_LEN + (word16)list->binderLen;
  8127. list = list->next;
  8128. }
  8129. *pSz += len;
  8130. return 0;
  8131. }
  8132. if (msgType == server_hello) {
  8133. *pSz += OPAQUE16_LEN;
  8134. return 0;
  8135. }
  8136. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8137. return SANITY_MSG_E;
  8138. }
  8139. /* The number of bytes to be written for the binders.
  8140. *
  8141. * list The linked list of pre-shared key extensions.
  8142. * msgType The type of the message this extension is being written into.
  8143. * returns the number of bytes of the encoded pre-shared key extension or
  8144. * SANITY_MSG_E to indicate invalid message type.
  8145. */
  8146. int TLSX_PreSharedKey_GetSizeBinders(PreSharedKey* list, byte msgType,
  8147. word16* pSz)
  8148. {
  8149. word16 len;
  8150. if (msgType != client_hello) {
  8151. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8152. return SANITY_MSG_E;
  8153. }
  8154. /* Length of all binders. */
  8155. len = OPAQUE16_LEN;
  8156. while (list != NULL) {
  8157. len += OPAQUE8_LEN + (word16)list->binderLen;
  8158. list = list->next;
  8159. }
  8160. *pSz = len;
  8161. return 0;
  8162. }
  8163. /* Writes the pre-shared key extension into the output buffer - binders only.
  8164. * Assumes that the the output buffer is big enough to hold data.
  8165. *
  8166. * list The linked list of key share entries.
  8167. * output The buffer to write into.
  8168. * msgType The type of the message this extension is being written into.
  8169. * returns the number of bytes written into the buffer.
  8170. */
  8171. int TLSX_PreSharedKey_WriteBinders(PreSharedKey* list, byte* output,
  8172. byte msgType, word16* pSz)
  8173. {
  8174. PreSharedKey* current = list;
  8175. word16 idx = 0;
  8176. word16 lenIdx;
  8177. word16 len;
  8178. if (msgType != client_hello) {
  8179. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8180. return SANITY_MSG_E;
  8181. }
  8182. /* Skip length of all binders. */
  8183. lenIdx = idx;
  8184. idx += OPAQUE16_LEN;
  8185. while (current != NULL) {
  8186. /* Binder data length. */
  8187. output[idx++] = (byte)current->binderLen;
  8188. /* Binder data. */
  8189. XMEMCPY(output + idx, current->binder, current->binderLen);
  8190. idx += (word16)current->binderLen;
  8191. current = current->next;
  8192. }
  8193. /* Length of the binders. */
  8194. len = idx - lenIdx - OPAQUE16_LEN;
  8195. c16toa(len, output + lenIdx);
  8196. *pSz = idx;
  8197. return 0;
  8198. }
  8199. /* Writes the pre-shared key extension into the output buffer.
  8200. * Assumes that the the output buffer is big enough to hold data.
  8201. *
  8202. * list The linked list of key share entries.
  8203. * output The buffer to write into.
  8204. * msgType The type of the message this extension is being written into.
  8205. * returns the number of bytes written into the buffer.
  8206. */
  8207. static int TLSX_PreSharedKey_Write(PreSharedKey* list, byte* output,
  8208. byte msgType, word16* pSz)
  8209. {
  8210. if (msgType == client_hello) {
  8211. PreSharedKey* current = list;
  8212. word16 idx = 0;
  8213. word16 lenIdx;
  8214. word16 len;
  8215. int ret;
  8216. /* Write identites only. Binders after HMACing over this. */
  8217. lenIdx = idx;
  8218. idx += OPAQUE16_LEN;
  8219. while (current != NULL) {
  8220. /* Identity length */
  8221. c16toa(current->identityLen, output + idx);
  8222. idx += OPAQUE16_LEN;
  8223. /* Identity data */
  8224. XMEMCPY(output + idx, current->identity, current->identityLen);
  8225. idx += current->identityLen;
  8226. /* Obfuscated ticket age. */
  8227. c32toa(current->ticketAge, output + idx);
  8228. idx += OPAQUE32_LEN;
  8229. current = current->next;
  8230. }
  8231. /* Length of the identites. */
  8232. len = idx - lenIdx - OPAQUE16_LEN;
  8233. c16toa(len, output + lenIdx);
  8234. /* Don't include binders here.
  8235. * The binders are based on the hash of all the ClientHello data up to
  8236. * and include the identities written above.
  8237. */
  8238. ret = TLSX_PreSharedKey_GetSizeBinders(list, msgType, &len);
  8239. if (ret < 0)
  8240. return ret;
  8241. *pSz += idx + len;
  8242. }
  8243. else if (msgType == server_hello) {
  8244. word16 i;
  8245. /* Find the index of the chosen identity. */
  8246. for (i=0; list != NULL && !list->chosen; i++)
  8247. list = list->next;
  8248. if (list == NULL) {
  8249. WOLFSSL_ERROR_VERBOSE(BUILD_MSG_ERROR);
  8250. return BUILD_MSG_ERROR;
  8251. }
  8252. /* The index of the identity chosen by the server from the list supplied
  8253. * by the client.
  8254. */
  8255. c16toa(i, output);
  8256. *pSz += OPAQUE16_LEN;
  8257. }
  8258. else {
  8259. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8260. return SANITY_MSG_E;
  8261. }
  8262. return 0;
  8263. }
  8264. int TLSX_PreSharedKey_Parse_ClientHello(TLSX** extensions, const byte* input,
  8265. word16 length, void* heap)
  8266. {
  8267. int ret;
  8268. word16 len;
  8269. word16 idx = 0;
  8270. TLSX* extension;
  8271. PreSharedKey* list;
  8272. TLSX_Remove(extensions, TLSX_PRE_SHARED_KEY, heap);
  8273. /* Length of identities and of binders. */
  8274. if ((int)(length - idx) < OPAQUE16_LEN + OPAQUE16_LEN)
  8275. return BUFFER_E;
  8276. /* Length of identities. */
  8277. ato16(input + idx, &len);
  8278. idx += OPAQUE16_LEN;
  8279. if (len < MIN_PSK_ID_LEN || length - idx < len)
  8280. return BUFFER_E;
  8281. /* Create a pre-shared key object for each identity. */
  8282. while (len > 0) {
  8283. const byte* identity;
  8284. word16 identityLen;
  8285. word32 age;
  8286. if (len < OPAQUE16_LEN)
  8287. return BUFFER_E;
  8288. /* Length of identity. */
  8289. ato16(input + idx, &identityLen);
  8290. idx += OPAQUE16_LEN;
  8291. if (len < OPAQUE16_LEN + identityLen + OPAQUE32_LEN ||
  8292. identityLen > MAX_PSK_ID_LEN)
  8293. return BUFFER_E;
  8294. /* Cache identity pointer. */
  8295. identity = input + idx;
  8296. idx += identityLen;
  8297. /* Ticket age. */
  8298. ato32(input + idx, &age);
  8299. idx += OPAQUE32_LEN;
  8300. ret = TLSX_PreSharedKey_Use(extensions, identity, identityLen, age, no_mac,
  8301. 0, 0, 1, NULL, heap);
  8302. if (ret != 0)
  8303. return ret;
  8304. /* Done with this identity. */
  8305. len -= OPAQUE16_LEN + identityLen + OPAQUE32_LEN;
  8306. }
  8307. /* Find the list of identities sent to server. */
  8308. extension = TLSX_Find(*extensions, TLSX_PRE_SHARED_KEY);
  8309. if (extension == NULL)
  8310. return PSK_KEY_ERROR;
  8311. list = (PreSharedKey*)extension->data;
  8312. /* Length of binders. */
  8313. if (idx + OPAQUE16_LEN > length)
  8314. return BUFFER_E;
  8315. ato16(input + idx, &len);
  8316. idx += OPAQUE16_LEN;
  8317. if (len < MIN_PSK_BINDERS_LEN || length - idx < len)
  8318. return BUFFER_E;
  8319. /* Set binder for each identity. */
  8320. while (list != NULL && len > 0) {
  8321. /* Length of binder */
  8322. list->binderLen = input[idx++];
  8323. if (list->binderLen < WC_SHA256_DIGEST_SIZE ||
  8324. list->binderLen > WC_MAX_DIGEST_SIZE)
  8325. return BUFFER_E;
  8326. if (len < OPAQUE8_LEN + list->binderLen)
  8327. return BUFFER_E;
  8328. /* Copy binder into static buffer. */
  8329. XMEMCPY(list->binder, input + idx, list->binderLen);
  8330. idx += (word16)list->binderLen;
  8331. /* Done with binder entry. */
  8332. len -= OPAQUE8_LEN + (word16)list->binderLen;
  8333. /* Next identity. */
  8334. list = list->next;
  8335. }
  8336. if (list != NULL || len != 0)
  8337. return BUFFER_E;
  8338. return 0;
  8339. }
  8340. /* Parse the pre-shared key extension.
  8341. * Different formats in different messages.
  8342. *
  8343. * ssl The SSL/TLS object.
  8344. * input The extension data.
  8345. * length The length of the extension data.
  8346. * msgType The type of the message this extension is being parsed from.
  8347. * returns 0 on success and other values indicate failure.
  8348. */
  8349. static int TLSX_PreSharedKey_Parse(WOLFSSL* ssl, const byte* input,
  8350. word16 length, byte msgType)
  8351. {
  8352. if (msgType == client_hello) {
  8353. return TLSX_PreSharedKey_Parse_ClientHello(&ssl->extensions, input,
  8354. length, ssl->heap);
  8355. }
  8356. if (msgType == server_hello) {
  8357. word16 idx;
  8358. PreSharedKey* list;
  8359. TLSX* extension;
  8360. /* Index of identity chosen by server. */
  8361. if (length != OPAQUE16_LEN)
  8362. return BUFFER_E;
  8363. ato16(input, &idx);
  8364. #ifdef WOLFSSL_EARLY_DATA
  8365. ssl->options.pskIdIndex = idx + 1;
  8366. #endif
  8367. /* Find the list of identities sent to server. */
  8368. extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
  8369. if (extension == NULL)
  8370. return PSK_KEY_ERROR;
  8371. list = (PreSharedKey*)extension->data;
  8372. /* Mark the identity as chosen. */
  8373. for (; list != NULL && idx > 0; idx--)
  8374. list = list->next;
  8375. if (list == NULL) {
  8376. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  8377. return PSK_KEY_ERROR;
  8378. }
  8379. list->chosen = 1;
  8380. #ifdef HAVE_SESSION_TICKET
  8381. if (list->resumption) {
  8382. /* Check that the session's details are the same as the server's. */
  8383. if (ssl->options.cipherSuite0 != ssl->session->cipherSuite0 ||
  8384. ssl->options.cipherSuite != ssl->session->cipherSuite ||
  8385. ssl->session->version.major != ssl->ctx->method->version.major ||
  8386. ssl->session->version.minor != ssl->ctx->method->version.minor) {
  8387. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  8388. return PSK_KEY_ERROR;
  8389. }
  8390. }
  8391. #endif
  8392. return 0;
  8393. }
  8394. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8395. return SANITY_MSG_E;
  8396. }
  8397. /* Create a new pre-shared key and put it into the list.
  8398. *
  8399. * list The linked list of pre-shared key.
  8400. * identity The identity.
  8401. * len The length of the identity data.
  8402. * heap The memory to allocate with.
  8403. * preSharedKey The new pre-shared key object.
  8404. * returns 0 on success and other values indicate failure.
  8405. */
  8406. static int TLSX_PreSharedKey_New(PreSharedKey** list, const byte* identity,
  8407. word16 len, void *heap,
  8408. PreSharedKey** preSharedKey)
  8409. {
  8410. PreSharedKey* psk;
  8411. PreSharedKey** next;
  8412. psk = (PreSharedKey*)XMALLOC(sizeof(PreSharedKey), heap, DYNAMIC_TYPE_TLSX);
  8413. if (psk == NULL)
  8414. return MEMORY_E;
  8415. XMEMSET(psk, 0, sizeof(*psk));
  8416. /* Make a copy of the identity data. */
  8417. psk->identity = (byte*)XMALLOC(len + NULL_TERM_LEN, heap,
  8418. DYNAMIC_TYPE_TLSX);
  8419. if (psk->identity == NULL) {
  8420. XFREE(psk, heap, DYNAMIC_TYPE_TLSX);
  8421. return MEMORY_E;
  8422. }
  8423. XMEMCPY(psk->identity, identity, len);
  8424. psk->identityLen = len;
  8425. /* Use a NULL terminator in case it is a C string */
  8426. psk->identity[psk->identityLen] = '\0';
  8427. /* Add it to the end and maintain the links. */
  8428. while (*list != NULL) {
  8429. /* Assign to temporary to work around compiler bug found by customer. */
  8430. next = &((*list)->next);
  8431. list = next;
  8432. }
  8433. *list = psk;
  8434. *preSharedKey = psk;
  8435. (void)heap;
  8436. return 0;
  8437. }
  8438. static WC_INLINE byte GetHmacLength(int hmac)
  8439. {
  8440. switch (hmac) {
  8441. #ifndef NO_SHA256
  8442. case sha256_mac:
  8443. return WC_SHA256_DIGEST_SIZE;
  8444. #endif
  8445. #ifdef WOLFSSL_SHA384
  8446. case sha384_mac:
  8447. return WC_SHA384_DIGEST_SIZE;
  8448. #endif
  8449. #ifdef WOLFSSL_SHA512
  8450. case sha512_mac:
  8451. return WC_SHA512_DIGEST_SIZE;
  8452. #endif
  8453. #ifdef WOLFSSL_SM3
  8454. case sm3_mac:
  8455. return WC_SM3_DIGEST_SIZE;
  8456. #endif
  8457. }
  8458. return 0;
  8459. }
  8460. /* Use the data to create a new pre-shared key object in the extensions.
  8461. *
  8462. * ssl The SSL/TLS object.
  8463. * identity The identity.
  8464. * len The length of the identity data.
  8465. * age The age of the identity.
  8466. * hmac The HMAC algorithm.
  8467. * cipherSuite0 The first byte of the cipher suite to use.
  8468. * cipherSuite The second byte of the cipher suite to use.
  8469. * resumption The PSK is for resumption of a session.
  8470. * preSharedKey The new pre-shared key object.
  8471. * returns 0 on success and other values indicate failure.
  8472. */
  8473. int TLSX_PreSharedKey_Use(TLSX** extensions, const byte* identity, word16 len,
  8474. word32 age, byte hmac, byte cipherSuite0,
  8475. byte cipherSuite, byte resumption,
  8476. PreSharedKey **preSharedKey, void* heap)
  8477. {
  8478. int ret = 0;
  8479. TLSX* extension;
  8480. PreSharedKey* psk = NULL;
  8481. /* Find the pre-shared key extension if it exists. */
  8482. extension = TLSX_Find(*extensions, TLSX_PRE_SHARED_KEY);
  8483. if (extension == NULL) {
  8484. /* Push new pre-shared key extension. */
  8485. ret = TLSX_Push(extensions, TLSX_PRE_SHARED_KEY, NULL, heap);
  8486. if (ret != 0)
  8487. return ret;
  8488. extension = TLSX_Find(*extensions, TLSX_PRE_SHARED_KEY);
  8489. if (extension == NULL)
  8490. return MEMORY_E;
  8491. }
  8492. /* Try to find the pre-shared key with this identity. */
  8493. psk = (PreSharedKey*)extension->data;
  8494. while (psk != NULL) {
  8495. if ((psk->identityLen == len) &&
  8496. (XMEMCMP(psk->identity, identity, len) == 0)) {
  8497. break;
  8498. }
  8499. psk = psk->next;
  8500. }
  8501. /* Create a new pre-shared key object if not found. */
  8502. if (psk == NULL) {
  8503. ret = TLSX_PreSharedKey_New((PreSharedKey**)&extension->data, identity,
  8504. len, heap, &psk);
  8505. if (ret != 0)
  8506. return ret;
  8507. }
  8508. /* Update/set age and HMAC algorithm. */
  8509. psk->ticketAge = age;
  8510. psk->hmac = hmac;
  8511. psk->cipherSuite0 = cipherSuite0;
  8512. psk->cipherSuite = cipherSuite;
  8513. psk->resumption = resumption;
  8514. psk->binderLen = GetHmacLength(psk->hmac);
  8515. if (preSharedKey != NULL)
  8516. *preSharedKey = psk;
  8517. return 0;
  8518. }
  8519. #define PSK_FREE_ALL TLSX_PreSharedKey_FreeAll
  8520. #define PSK_GET_SIZE TLSX_PreSharedKey_GetSize
  8521. #define PSK_WRITE TLSX_PreSharedKey_Write
  8522. #define PSK_PARSE TLSX_PreSharedKey_Parse
  8523. #else
  8524. #define PSK_FREE_ALL(a, b)
  8525. #define PSK_GET_SIZE(a, b, c) 0
  8526. #define PSK_WRITE(a, b, c, d) 0
  8527. #define PSK_PARSE(a, b, c, d) 0
  8528. #endif
  8529. /******************************************************************************/
  8530. /* PSK Key Exchange Modes */
  8531. /******************************************************************************/
  8532. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  8533. /* Get the size of the encoded PSK KE modes extension.
  8534. * Only in ClientHello.
  8535. *
  8536. * modes The PSK KE mode bit string.
  8537. * msgType The type of the message this extension is being written into.
  8538. * returns the number of bytes of the encoded PSK KE mode extension.
  8539. */
  8540. static int TLSX_PskKeModes_GetSize(byte modes, byte msgType, word16* pSz)
  8541. {
  8542. if (msgType == client_hello) {
  8543. /* Format: Len | Modes* */
  8544. word16 len = OPAQUE8_LEN;
  8545. /* Check whether each possible mode is to be written. */
  8546. if (modes & (1 << PSK_KE))
  8547. len += OPAQUE8_LEN;
  8548. if (modes & (1 << PSK_DHE_KE))
  8549. len += OPAQUE8_LEN;
  8550. *pSz += len;
  8551. return 0;
  8552. }
  8553. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8554. return SANITY_MSG_E;
  8555. }
  8556. /* Writes the PSK KE modes extension into the output buffer.
  8557. * Assumes that the the output buffer is big enough to hold data.
  8558. * Only in ClientHello.
  8559. *
  8560. * modes The PSK KE mode bit string.
  8561. * output The buffer to write into.
  8562. * msgType The type of the message this extension is being written into.
  8563. * returns the number of bytes written into the buffer.
  8564. */
  8565. static int TLSX_PskKeModes_Write(byte modes, byte* output, byte msgType,
  8566. word16* pSz)
  8567. {
  8568. if (msgType == client_hello) {
  8569. /* Format: Len | Modes* */
  8570. word16 idx = OPAQUE8_LEN;
  8571. /* Write out each possible mode. */
  8572. if (modes & (1 << PSK_KE))
  8573. output[idx++] = PSK_KE;
  8574. if (modes & (1 << PSK_DHE_KE))
  8575. output[idx++] = PSK_DHE_KE;
  8576. /* Write out length of mode list. */
  8577. output[0] = (byte)(idx - OPAQUE8_LEN);
  8578. *pSz += idx;
  8579. return 0;
  8580. }
  8581. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8582. return SANITY_MSG_E;
  8583. }
  8584. int TLSX_PskKeyModes_Parse_Modes(const byte* input, word16 length, byte msgType,
  8585. byte* modes)
  8586. {
  8587. if (msgType == client_hello) {
  8588. /* Format: Len | Modes* */
  8589. int idx = 0;
  8590. word16 len;
  8591. *modes = 0;
  8592. /* Ensure length byte exists. */
  8593. if (length < OPAQUE8_LEN)
  8594. return BUFFER_E;
  8595. /* Get length of mode list and ensure that is the only data. */
  8596. len = input[0];
  8597. if (length - OPAQUE8_LEN != len)
  8598. return BUFFER_E;
  8599. idx = OPAQUE8_LEN;
  8600. /* Set a bit for each recognized modes. */
  8601. while (len > 0) {
  8602. /* Ignore unrecognized modes. */
  8603. if (input[idx] <= PSK_DHE_KE)
  8604. *modes |= 1 << input[idx];
  8605. idx++;
  8606. len--;
  8607. }
  8608. return 0;
  8609. }
  8610. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8611. return SANITY_MSG_E;
  8612. }
  8613. /* Parse the PSK KE modes extension.
  8614. * Only in ClientHello.
  8615. *
  8616. * ssl The SSL/TLS object.
  8617. * input The extension data.
  8618. * length The length of the extension data.
  8619. * msgType The type of the message this extension is being parsed from.
  8620. * returns 0 on success and other values indicate failure.
  8621. */
  8622. static int TLSX_PskKeModes_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  8623. byte msgType)
  8624. {
  8625. int ret;
  8626. byte modes;
  8627. ret = TLSX_PskKeyModes_Parse_Modes(input, length, msgType, &modes);
  8628. if (ret == 0)
  8629. ret = TLSX_PskKeyModes_Use(ssl, modes);
  8630. WOLFSSL_ERROR_VERBOSE(ret);
  8631. return ret;
  8632. }
  8633. /* Use the data to create a new PSK Key Exchange Modes object in the extensions.
  8634. *
  8635. * ssl The SSL/TLS object.
  8636. * modes The PSK key exchange modes.
  8637. * returns 0 on success and other values indicate failure.
  8638. */
  8639. int TLSX_PskKeyModes_Use(WOLFSSL* ssl, byte modes)
  8640. {
  8641. int ret = 0;
  8642. TLSX* extension;
  8643. /* Find the PSK key exchange modes extension if it exists. */
  8644. extension = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES);
  8645. if (extension == NULL) {
  8646. /* Push new PSK key exchange modes extension. */
  8647. ret = TLSX_Push(&ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES, NULL,
  8648. ssl->heap);
  8649. if (ret != 0)
  8650. return ret;
  8651. extension = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES);
  8652. if (extension == NULL)
  8653. return MEMORY_E;
  8654. }
  8655. extension->val = modes;
  8656. return 0;
  8657. }
  8658. #define PKM_GET_SIZE TLSX_PskKeModes_GetSize
  8659. #define PKM_WRITE TLSX_PskKeModes_Write
  8660. #define PKM_PARSE TLSX_PskKeModes_Parse
  8661. #else
  8662. #define PKM_GET_SIZE(a, b, c) 0
  8663. #define PKM_WRITE(a, b, c, d) 0
  8664. #define PKM_PARSE(a, b, c, d) 0
  8665. #endif
  8666. /******************************************************************************/
  8667. /* Post-Handshake Authentication */
  8668. /******************************************************************************/
  8669. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  8670. /* Get the size of the encoded Post-Handshake Authentication extension.
  8671. * Only in ClientHello.
  8672. *
  8673. * msgType The type of the message this extension is being written into.
  8674. * returns the number of bytes of the encoded Post-Handshake Authentication
  8675. * extension.
  8676. */
  8677. static int TLSX_PostHandAuth_GetSize(byte msgType, word16* pSz)
  8678. {
  8679. if (msgType == client_hello) {
  8680. *pSz += 0;
  8681. return 0;
  8682. }
  8683. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8684. return SANITY_MSG_E;
  8685. }
  8686. /* Writes the Post-Handshake Authentication extension into the output buffer.
  8687. * Assumes that the the output buffer is big enough to hold data.
  8688. * Only in ClientHello.
  8689. *
  8690. * output The buffer to write into.
  8691. * msgType The type of the message this extension is being written into.
  8692. * returns the number of bytes written into the buffer.
  8693. */
  8694. static int TLSX_PostHandAuth_Write(byte* output, byte msgType, word16* pSz)
  8695. {
  8696. (void)output;
  8697. if (msgType == client_hello) {
  8698. *pSz += 0;
  8699. return 0;
  8700. }
  8701. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8702. return SANITY_MSG_E;
  8703. }
  8704. /* Parse the Post-Handshake Authentication extension.
  8705. * Only in ClientHello.
  8706. *
  8707. * ssl The SSL/TLS object.
  8708. * input The extension data.
  8709. * length The length of the extension data.
  8710. * msgType The type of the message this extension is being parsed from.
  8711. * returns 0 on success and other values indicate failure.
  8712. */
  8713. static int TLSX_PostHandAuth_Parse(WOLFSSL* ssl, const byte* input,
  8714. word16 length, byte msgType)
  8715. {
  8716. (void)input;
  8717. if (msgType == client_hello) {
  8718. /* Ensure extension is empty. */
  8719. if (length != 0)
  8720. return BUFFER_E;
  8721. ssl->options.postHandshakeAuth = 1;
  8722. return 0;
  8723. }
  8724. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8725. return SANITY_MSG_E;
  8726. }
  8727. /* Create a new Post-handshake authentication object in the extensions.
  8728. *
  8729. * ssl The SSL/TLS object.
  8730. * returns 0 on success and other values indicate failure.
  8731. */
  8732. static int TLSX_PostHandAuth_Use(WOLFSSL* ssl)
  8733. {
  8734. int ret = 0;
  8735. TLSX* extension;
  8736. /* Find the PSK key exchange modes extension if it exists. */
  8737. extension = TLSX_Find(ssl->extensions, TLSX_POST_HANDSHAKE_AUTH);
  8738. if (extension == NULL) {
  8739. /* Push new Post-handshake Authentication extension. */
  8740. ret = TLSX_Push(&ssl->extensions, TLSX_POST_HANDSHAKE_AUTH, NULL,
  8741. ssl->heap);
  8742. if (ret != 0)
  8743. return ret;
  8744. }
  8745. return 0;
  8746. }
  8747. #define PHA_GET_SIZE TLSX_PostHandAuth_GetSize
  8748. #define PHA_WRITE TLSX_PostHandAuth_Write
  8749. #define PHA_PARSE TLSX_PostHandAuth_Parse
  8750. #else
  8751. #define PHA_GET_SIZE(a, b) 0
  8752. #define PHA_WRITE(a, b, c) 0
  8753. #define PHA_PARSE(a, b, c, d) 0
  8754. #endif
  8755. /******************************************************************************/
  8756. /* Early Data Indication */
  8757. /******************************************************************************/
  8758. #ifdef WOLFSSL_EARLY_DATA
  8759. /* Get the size of the encoded Early Data Indication extension.
  8760. * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
  8761. *
  8762. * msgType The type of the message this extension is being written into.
  8763. * returns the number of bytes of the encoded Early Data Indication extension.
  8764. */
  8765. static int TLSX_EarlyData_GetSize(byte msgType, word16* pSz)
  8766. {
  8767. int ret = 0;
  8768. if (msgType == client_hello || msgType == encrypted_extensions)
  8769. *pSz += 0;
  8770. else if (msgType == session_ticket)
  8771. *pSz += OPAQUE32_LEN;
  8772. else {
  8773. ret = SANITY_MSG_E;
  8774. WOLFSSL_ERROR_VERBOSE(ret);
  8775. }
  8776. return ret;
  8777. }
  8778. /* Writes the Early Data Indicator extension into the output buffer.
  8779. * Assumes that the the output buffer is big enough to hold data.
  8780. * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
  8781. *
  8782. * maxSz The maximum early data size.
  8783. * output The buffer to write into.
  8784. * msgType The type of the message this extension is being written into.
  8785. * returns the number of bytes written into the buffer.
  8786. */
  8787. static int TLSX_EarlyData_Write(word32 maxSz, byte* output, byte msgType,
  8788. word16* pSz)
  8789. {
  8790. if (msgType == client_hello || msgType == encrypted_extensions)
  8791. return 0;
  8792. else if (msgType == session_ticket) {
  8793. c32toa(maxSz, output);
  8794. *pSz += OPAQUE32_LEN;
  8795. return 0;
  8796. }
  8797. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8798. return SANITY_MSG_E;
  8799. }
  8800. /* Parse the Early Data Indicator extension.
  8801. * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
  8802. *
  8803. * ssl The SSL/TLS object.
  8804. * input The extension data.
  8805. * length The length of the extension data.
  8806. * msgType The type of the message this extension is being parsed from.
  8807. * returns 0 on success and other values indicate failure.
  8808. */
  8809. static int TLSX_EarlyData_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  8810. byte msgType)
  8811. {
  8812. WOLFSSL_ENTER("TLSX_EarlyData_Parse");
  8813. if (msgType == client_hello) {
  8814. if (length != 0)
  8815. return BUFFER_E;
  8816. if (ssl->earlyData == expecting_early_data) {
  8817. if (ssl->options.maxEarlyDataSz != 0)
  8818. ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_ACCEPTED;
  8819. else
  8820. ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_REJECTED;
  8821. return TLSX_EarlyData_Use(ssl, 0, 0);
  8822. }
  8823. ssl->earlyData = early_data_ext;
  8824. return 0;
  8825. }
  8826. if (msgType == encrypted_extensions) {
  8827. if (length != 0)
  8828. return BUFFER_E;
  8829. /* Ensure the index of PSK identity chosen by server is 0.
  8830. * Index is plus one to handle 'not set' value of 0.
  8831. */
  8832. if (ssl->options.pskIdIndex != 1) {
  8833. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  8834. return PSK_KEY_ERROR;
  8835. }
  8836. if (ssl->options.side == WOLFSSL_CLIENT_END) {
  8837. /* the extension from server comes in */
  8838. ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_ACCEPTED;
  8839. }
  8840. return TLSX_EarlyData_Use(ssl, 1, 1);
  8841. }
  8842. if (msgType == session_ticket) {
  8843. word32 maxSz;
  8844. if (length != OPAQUE32_LEN)
  8845. return BUFFER_E;
  8846. ato32(input, &maxSz);
  8847. ssl->session->maxEarlyDataSz = maxSz;
  8848. return 0;
  8849. }
  8850. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8851. return SANITY_MSG_E;
  8852. }
  8853. /* Use the data to create a new Early Data object in the extensions.
  8854. *
  8855. * ssl The SSL/TLS object.
  8856. * maxSz The maximum early data size.
  8857. * is_response if this extension is part of a response
  8858. * returns 0 on success and other values indicate failure.
  8859. */
  8860. int TLSX_EarlyData_Use(WOLFSSL* ssl, word32 maxSz, int is_response)
  8861. {
  8862. int ret = 0;
  8863. TLSX* extension;
  8864. /* Find the early data extension if it exists. */
  8865. extension = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA);
  8866. if (extension == NULL) {
  8867. /* Push new early data extension. */
  8868. ret = TLSX_Push(&ssl->extensions, TLSX_EARLY_DATA, NULL, ssl->heap);
  8869. if (ret != 0)
  8870. return ret;
  8871. extension = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA);
  8872. if (extension == NULL)
  8873. return MEMORY_E;
  8874. }
  8875. extension->resp = is_response;
  8876. /* In QUIC, earlydata size is either 0 or 0xffffffff.
  8877. * Override any size between, possibly left from our initial value */
  8878. extension->val = (WOLFSSL_IS_QUIC(ssl) && is_response && maxSz > 0) ?
  8879. WOLFSSL_MAX_32BIT : maxSz;
  8880. return 0;
  8881. }
  8882. #define EDI_GET_SIZE TLSX_EarlyData_GetSize
  8883. #define EDI_WRITE TLSX_EarlyData_Write
  8884. #define EDI_PARSE TLSX_EarlyData_Parse
  8885. #else
  8886. #define EDI_GET_SIZE(a, b) 0
  8887. #define EDI_WRITE(a, b, c, d) 0
  8888. #define EDI_PARSE(a, b, c, d) 0
  8889. #endif
  8890. /******************************************************************************/
  8891. /* QUIC transport parameter extension */
  8892. /******************************************************************************/
  8893. #ifdef WOLFSSL_QUIC
  8894. static word16 TLSX_QuicTP_GetSize(TLSX* extension)
  8895. {
  8896. const QuicTransportParam *tp = (QuicTransportParam*)extension->data;
  8897. return tp ? tp->len : 0;
  8898. }
  8899. int TLSX_QuicTP_Use(WOLFSSL* ssl, TLSX_Type ext_type, int is_response)
  8900. {
  8901. int ret = 0;
  8902. TLSX* extension;
  8903. WOLFSSL_ENTER("TLSX_QuicTP_Use");
  8904. if (ssl->quic.transport_local == NULL) {
  8905. /* RFC9000, ch 7.3: "An endpoint MUST treat the absence of [...]
  8906. * from either endpoint [...] as a connection error of type
  8907. * TRANSPORT_PARAMETER_ERROR."
  8908. */
  8909. ret = QUIC_TP_MISSING_E;
  8910. goto cleanup;
  8911. }
  8912. extension = TLSX_Find(ssl->extensions, ext_type);
  8913. if (extension == NULL) {
  8914. ret = TLSX_Push(&ssl->extensions, ext_type, NULL, ssl->heap);
  8915. if (ret != 0)
  8916. goto cleanup;
  8917. extension = TLSX_Find(ssl->extensions, ext_type);
  8918. if (extension == NULL) {
  8919. ret = MEMORY_E;
  8920. goto cleanup;
  8921. }
  8922. }
  8923. if (extension->data) {
  8924. QuicTransportParam_free((QuicTransportParam*)extension->data, ssl->heap);
  8925. extension->data = NULL;
  8926. }
  8927. extension->resp = is_response;
  8928. extension->data = (void*)QuicTransportParam_dup(ssl->quic.transport_local, ssl->heap);
  8929. if (!extension->data) {
  8930. ret = MEMORY_E;
  8931. goto cleanup;
  8932. }
  8933. cleanup:
  8934. WOLFSSL_LEAVE("TLSX_QuicTP_Use", ret);
  8935. return ret;
  8936. }
  8937. static word16 TLSX_QuicTP_Write(QuicTransportParam *tp, byte* output)
  8938. {
  8939. word16 len = 0;
  8940. WOLFSSL_ENTER("TLSX_QuicTP_Write");
  8941. if (tp && tp->len) {
  8942. XMEMCPY(output, tp->data, tp->len);
  8943. len = tp->len;
  8944. }
  8945. WOLFSSL_LEAVE("TLSX_QuicTP_Write", len);
  8946. return len;
  8947. }
  8948. static int TLSX_QuicTP_Parse(WOLFSSL *ssl, const byte *input, size_t len, int ext_type, int msgType)
  8949. {
  8950. const QuicTransportParam *tp, **ptp;
  8951. (void)msgType;
  8952. tp = QuicTransportParam_new(input, len, ssl->heap);
  8953. if (!tp) {
  8954. return MEMORY_E;
  8955. }
  8956. ptp = (ext_type == TLSX_KEY_QUIC_TP_PARAMS_DRAFT) ?
  8957. &ssl->quic.transport_peer_draft : &ssl->quic.transport_peer;
  8958. if (*ptp) {
  8959. QTP_FREE(*ptp, ssl->heap);
  8960. }
  8961. *ptp = tp;
  8962. return 0;
  8963. }
  8964. #define QTP_GET_SIZE TLSX_QuicTP_GetSize
  8965. #define QTP_USE TLSX_QuicTP_Use
  8966. #define QTP_WRITE TLSX_QuicTP_Write
  8967. #define QTP_PARSE TLSX_QuicTP_Parse
  8968. #endif /* WOLFSSL_QUIC */
  8969. #if defined(WOLFSSL_DTLS_CID)
  8970. #define CID_GET_SIZE TLSX_ConnectionID_GetSize
  8971. #define CID_WRITE TLSX_ConnectionID_Write
  8972. #define CID_PARSE TLSX_ConnectionID_Parse
  8973. #define CID_FREE TLSX_ConnectionID_Free
  8974. #else
  8975. #define CID_GET_SIZE(a) 0
  8976. #define CID_WRITE(a, b) 0
  8977. #define CID_PARSE(a, b, c, d) 0
  8978. #define CID_FREE(a, b) 0
  8979. #endif /* defined(WOLFSSL_DTLS_CID) */
  8980. /******************************************************************************/
  8981. /* TLS Extensions Framework */
  8982. /******************************************************************************/
  8983. /** Finds an extension in the provided list. */
  8984. TLSX* TLSX_Find(TLSX* list, TLSX_Type type)
  8985. {
  8986. TLSX* extension = list;
  8987. while (extension && extension->type != type)
  8988. extension = extension->next;
  8989. return extension;
  8990. }
  8991. /** Remove an extension. */
  8992. void TLSX_Remove(TLSX** list, TLSX_Type type, void* heap)
  8993. {
  8994. TLSX* extension;
  8995. TLSX** next;
  8996. if (list == NULL)
  8997. return;
  8998. extension = *list;
  8999. next = list;
  9000. while (extension && extension->type != type) {
  9001. next = &extension->next;
  9002. extension = extension->next;
  9003. }
  9004. if (extension) {
  9005. *next = extension->next;
  9006. extension->next = NULL;
  9007. TLSX_FreeAll(extension, heap);
  9008. }
  9009. }
  9010. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  9011. #define GREASE_ECH_SIZE 160
  9012. #define MAX_PUBLIC_NAME_SZ 256
  9013. #define TLS_INFO_CONST_STRING "tls ech"
  9014. #define TLS_INFO_CONST_STRING_SZ 7
  9015. /* return status after setting up ech to write a grease ech */
  9016. static int TLSX_GreaseECH_Use(TLSX** extensions, void* heap, WC_RNG* rng)
  9017. {
  9018. int ret = 0;
  9019. WOLFSSL_ECH* ech;
  9020. if (extensions == NULL)
  9021. return BAD_FUNC_ARG;
  9022. ech = (WOLFSSL_ECH*)XMALLOC(sizeof(WOLFSSL_ECH), heap,
  9023. DYNAMIC_TYPE_TMP_BUFFER);
  9024. if (ech == NULL)
  9025. return MEMORY_E;
  9026. ForceZero(ech, sizeof(WOLFSSL_ECH));
  9027. ech->state = ECH_WRITE_GREASE;
  9028. /* 0 for outer */
  9029. ech->type = ECH_TYPE_OUTER;
  9030. /* kemId */
  9031. ech->kemId = DHKEM_X25519_HKDF_SHA256;
  9032. /* cipherSuite kdf */
  9033. ech->cipherSuite.kdfId = HKDF_SHA256;
  9034. /* cipherSuite aead */
  9035. ech->cipherSuite.aeadId = HPKE_AES_128_GCM;
  9036. /* random configId */
  9037. ret = wc_RNG_GenerateByte(rng, &(ech->configId));
  9038. /* curve25519 encLen */
  9039. ech->encLen = DHKEM_X25519_ENC_LEN;
  9040. if (ret == 0)
  9041. ret = TLSX_Push(extensions, TLSX_ECH, ech, heap);
  9042. if (ret != 0) {
  9043. XFREE(ech, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9044. }
  9045. return ret;
  9046. }
  9047. /* return status after setting up ech to write real ech */
  9048. static int TLSX_ECH_Use(WOLFSSL_EchConfig* echConfig, TLSX** extensions,
  9049. void* heap, WC_RNG* rng)
  9050. {
  9051. int ret = 0;
  9052. int suiteIndex;
  9053. WOLFSSL_ECH* ech;
  9054. if (extensions == NULL)
  9055. return BAD_FUNC_ARG;
  9056. /* find a supported cipher suite */
  9057. suiteIndex = EchConfigGetSupportedCipherSuite(echConfig);
  9058. if (suiteIndex < 0)
  9059. return suiteIndex;
  9060. ech = (WOLFSSL_ECH*)XMALLOC(sizeof(WOLFSSL_ECH), heap,
  9061. DYNAMIC_TYPE_TMP_BUFFER);
  9062. if (ech == NULL)
  9063. return MEMORY_E;
  9064. ForceZero(ech, sizeof(WOLFSSL_ECH));
  9065. ech->state = ECH_WRITE_REAL;
  9066. ech->echConfig = echConfig;
  9067. /* 0 for outer */
  9068. ech->type = ECH_TYPE_OUTER;
  9069. /* kemId */
  9070. ech->kemId = echConfig->kemId;
  9071. /* cipherSuite kdf */
  9072. ech->cipherSuite.kdfId = echConfig->cipherSuites[suiteIndex].kdfId;
  9073. /* cipherSuite aead */
  9074. ech->cipherSuite.aeadId = echConfig->cipherSuites[suiteIndex].aeadId;
  9075. /* configId */
  9076. ech->configId = echConfig->configId;
  9077. /* encLen */
  9078. switch (echConfig->kemId)
  9079. {
  9080. case DHKEM_P256_HKDF_SHA256:
  9081. ech->encLen = DHKEM_P256_ENC_LEN;
  9082. break;
  9083. case DHKEM_P384_HKDF_SHA384:
  9084. ech->encLen = DHKEM_P384_ENC_LEN;
  9085. break;
  9086. case DHKEM_P521_HKDF_SHA512:
  9087. ech->encLen = DHKEM_P521_ENC_LEN;
  9088. break;
  9089. case DHKEM_X25519_HKDF_SHA256:
  9090. ech->encLen = DHKEM_X25519_ENC_LEN;
  9091. break;
  9092. case DHKEM_X448_HKDF_SHA512:
  9093. ech->encLen = DHKEM_X448_ENC_LEN;
  9094. break;
  9095. }
  9096. /* setup hpke */
  9097. ech->hpke = (Hpke*)XMALLOC(sizeof(Hpke), heap, DYNAMIC_TYPE_TMP_BUFFER);
  9098. if (ech->hpke == NULL) {
  9099. XFREE(ech, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9100. return MEMORY_E;
  9101. }
  9102. ret = wc_HpkeInit(ech->hpke, ech->kemId, ech->cipherSuite.kdfId,
  9103. ech->cipherSuite.aeadId, heap);
  9104. /* setup the ephemeralKey */
  9105. if (ret == 0)
  9106. ret = wc_HpkeGenerateKeyPair(ech->hpke, &ech->ephemeralKey, rng);
  9107. if (ret == 0)
  9108. ret = TLSX_Push(extensions, TLSX_ECH, ech, heap);
  9109. if (ret != 0) {
  9110. XFREE(ech->hpke, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9111. XFREE(ech, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9112. }
  9113. return ret;
  9114. }
  9115. /* return status after setting up ech to read and decrypt */
  9116. static int TLSX_ServerECH_Use(TLSX** extensions, void* heap,
  9117. WOLFSSL_EchConfig* configs)
  9118. {
  9119. int ret;
  9120. WOLFSSL_ECH* ech;
  9121. TLSX* echX;
  9122. if (extensions == NULL)
  9123. return BAD_FUNC_ARG;
  9124. /* if we already have ech don't override it */
  9125. echX = TLSX_Find(*extensions, TLSX_ECH);
  9126. if (echX != NULL)
  9127. return 0;
  9128. ech = (WOLFSSL_ECH*)XMALLOC(sizeof(WOLFSSL_ECH), heap,
  9129. DYNAMIC_TYPE_TMP_BUFFER);
  9130. if (ech == NULL)
  9131. return MEMORY_E;
  9132. ForceZero(ech, sizeof(WOLFSSL_ECH));
  9133. ech->state = ECH_WRITE_NONE;
  9134. /* 0 for outer */
  9135. ech->type = ECH_TYPE_OUTER;
  9136. ech->echConfig = configs;
  9137. /* setup the rest of the settings when we receive ech from the client */
  9138. ret = TLSX_Push(extensions, TLSX_ECH, ech, heap);
  9139. if (ret != 0)
  9140. XFREE(ech, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9141. return ret;
  9142. }
  9143. /* return length after writing the ech */
  9144. static int TLSX_ECH_Write(WOLFSSL_ECH* ech, byte* writeBuf, word16* offset)
  9145. {
  9146. int ret = 0;
  9147. int rngRet = -1;
  9148. word32 configsLen = 0;
  9149. void* ephemeralKey = NULL;
  9150. byte* writeBuf_p = writeBuf;
  9151. #ifdef WOLFSSL_SMALL_STACK
  9152. Hpke* hpke = NULL;
  9153. WC_RNG* rng = NULL;
  9154. #else
  9155. Hpke hpke[1];
  9156. WC_RNG rng[1];
  9157. #endif
  9158. WOLFSSL_MSG("TLSX_ECH_Write");
  9159. if (ech->state == ECH_WRITE_NONE || ech->state == ECH_PARSED_INTERNAL)
  9160. return 0;
  9161. if (ech->state == ECH_WRITE_RETRY_CONFIGS) {
  9162. /* get size then write */
  9163. ret = GetEchConfigsEx(ech->echConfig, NULL, &configsLen);
  9164. if (ret != LENGTH_ONLY_E)
  9165. return ret;
  9166. ret = GetEchConfigsEx(ech->echConfig, writeBuf, &configsLen);
  9167. if (ret != WOLFSSL_SUCCESS)
  9168. return ret;
  9169. *offset += configsLen;
  9170. return 0;
  9171. }
  9172. #ifdef WOLFSSL_SMALL_STACK
  9173. hpke = (Hpke*)XMALLOC(sizeof(Hpke), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9174. if (hpke == NULL)
  9175. return MEMORY_E;
  9176. rng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
  9177. if (rng == NULL) {
  9178. XFREE(hpke, NULL, DYNAMIC_TYPE_RNG);
  9179. return MEMORY_E;
  9180. }
  9181. #endif
  9182. /* type */
  9183. *writeBuf_p = ech->type;
  9184. writeBuf_p += sizeof(ech->type);
  9185. /* outer has body, inner does not */
  9186. if (ech->type == ECH_TYPE_OUTER) {
  9187. /* kdfId */
  9188. c16toa(ech->cipherSuite.kdfId, writeBuf_p);
  9189. writeBuf_p += sizeof(ech->cipherSuite.kdfId);
  9190. /* aeadId */
  9191. c16toa(ech->cipherSuite.aeadId, writeBuf_p);
  9192. writeBuf_p += sizeof(ech->cipherSuite.aeadId);
  9193. /* configId */
  9194. *writeBuf_p = ech->configId;
  9195. writeBuf_p += sizeof(ech->configId);
  9196. /* encLen */
  9197. c16toa(ech->encLen, writeBuf_p);
  9198. writeBuf_p += 2;
  9199. if (ech->state == ECH_WRITE_GREASE) {
  9200. /* hpke init */
  9201. ret = wc_HpkeInit(hpke, ech->kemId, ech->cipherSuite.kdfId,
  9202. ech->cipherSuite.aeadId, NULL);
  9203. if (ret == 0)
  9204. rngRet = ret = wc_InitRng(rng);
  9205. /* create the ephemeralKey */
  9206. if (ret == 0)
  9207. ret = wc_HpkeGenerateKeyPair(hpke, &ephemeralKey, rng);
  9208. /* enc */
  9209. if (ret == 0) {
  9210. ret = wc_HpkeSerializePublicKey(hpke, ephemeralKey, writeBuf_p,
  9211. &ech->encLen);
  9212. writeBuf_p += ech->encLen;
  9213. }
  9214. if (ret == 0) {
  9215. /* innerClientHelloLen */
  9216. c16toa(GREASE_ECH_SIZE + ((writeBuf_p + 2 - writeBuf) % 32),
  9217. writeBuf_p);
  9218. writeBuf_p += 2;
  9219. /* innerClientHello */
  9220. ret = wc_RNG_GenerateBlock(rng, writeBuf_p, GREASE_ECH_SIZE +
  9221. ((writeBuf_p - writeBuf) % 32));
  9222. writeBuf_p += GREASE_ECH_SIZE + ((writeBuf_p - writeBuf) % 32);
  9223. }
  9224. if (rngRet == 0)
  9225. wc_FreeRng(rng);
  9226. if (ephemeralKey != NULL)
  9227. wc_HpkeFreeKey(hpke, hpke->kem, ephemeralKey, hpke->heap);
  9228. }
  9229. else {
  9230. /* write enc to writeBuf_p */
  9231. ret = wc_HpkeSerializePublicKey(ech->hpke, ech->ephemeralKey,
  9232. writeBuf_p, &ech->encLen);
  9233. writeBuf_p += ech->encLen;
  9234. /* innerClientHelloLen */
  9235. c16toa(ech->innerClientHelloLen, writeBuf_p);
  9236. writeBuf_p += 2;
  9237. /* set payload offset for when we finalize */
  9238. ech->outerClientPayload = writeBuf_p;
  9239. /* write zeros for payload */
  9240. XMEMSET(writeBuf_p, 0, ech->innerClientHelloLen);
  9241. writeBuf_p += ech->innerClientHelloLen;
  9242. }
  9243. }
  9244. #ifdef WOLFSSL_SMALL_STACK
  9245. XFREE(hpke, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9246. XFREE(rng, NULL, DYNAMIC_TYPE_RNG);
  9247. #endif
  9248. if (ret == 0)
  9249. *offset += (writeBuf_p - writeBuf);
  9250. return ret;
  9251. }
  9252. /* return the size needed for the ech extension */
  9253. static int TLSX_ECH_GetSize(WOLFSSL_ECH* ech)
  9254. {
  9255. int ret;
  9256. word32 size;
  9257. if (ech->state == ECH_WRITE_GREASE) {
  9258. size = sizeof(ech->type) + sizeof(ech->cipherSuite) +
  9259. sizeof(ech->configId) + sizeof(word16) + ech->encLen +
  9260. sizeof(word16);
  9261. size += GREASE_ECH_SIZE + (size % 32);
  9262. }
  9263. else if (ech->state == ECH_WRITE_NONE ||
  9264. ech->state == ECH_PARSED_INTERNAL) {
  9265. size = 0;
  9266. }
  9267. else if (ech->state == ECH_WRITE_RETRY_CONFIGS) {
  9268. /* get the size of the raw configs */
  9269. ret = GetEchConfigsEx(ech->echConfig, NULL, &size);
  9270. if (ret != LENGTH_ONLY_E)
  9271. return ret;
  9272. }
  9273. else if (ech->type == ECH_TYPE_INNER)
  9274. {
  9275. size = sizeof(ech->type);
  9276. }
  9277. else
  9278. {
  9279. size = sizeof(ech->type) + sizeof(ech->cipherSuite) +
  9280. sizeof(ech->configId) + sizeof(word16) + ech->encLen +
  9281. sizeof(word16) + ech->innerClientHelloLen;
  9282. }
  9283. return (int)size;
  9284. }
  9285. /* return status after attempting to open the hpke encrypted ech extension, if
  9286. * successful the inner client hello will be stored in
  9287. * ech->innerClientHelloLen */
  9288. static int TLSX_ExtractEch(WOLFSSL_ECH* ech, WOLFSSL_EchConfig* echConfig,
  9289. byte* aad, word32 aadLen, void* heap)
  9290. {
  9291. int ret = 0;
  9292. int expectedEncLen;
  9293. int i;
  9294. word32 rawConfigLen = 0;
  9295. byte* info = NULL;
  9296. word32 infoLen = 0;
  9297. if (ech == NULL || echConfig == NULL || aad == NULL)
  9298. return BAD_FUNC_ARG;
  9299. /* verify the kem and key len */
  9300. switch (echConfig->kemId)
  9301. {
  9302. case DHKEM_P256_HKDF_SHA256:
  9303. expectedEncLen = DHKEM_P256_ENC_LEN;
  9304. break;
  9305. case DHKEM_P384_HKDF_SHA384:
  9306. expectedEncLen = DHKEM_P384_ENC_LEN;
  9307. break;
  9308. case DHKEM_P521_HKDF_SHA512:
  9309. expectedEncLen = DHKEM_P521_ENC_LEN;
  9310. break;
  9311. case DHKEM_X25519_HKDF_SHA256:
  9312. expectedEncLen = DHKEM_X25519_ENC_LEN;
  9313. break;
  9314. case DHKEM_X448_HKDF_SHA512:
  9315. expectedEncLen = DHKEM_X448_ENC_LEN;
  9316. break;
  9317. default:
  9318. expectedEncLen = 0;
  9319. break;
  9320. }
  9321. if (expectedEncLen != ech->encLen)
  9322. return BAD_FUNC_ARG;
  9323. /* verify the cipher suite */
  9324. for (i = 0; i < echConfig->numCipherSuites; i++) {
  9325. if (echConfig->cipherSuites[i].kdfId == ech->cipherSuite.kdfId &&
  9326. echConfig->cipherSuites[i].aeadId == ech->cipherSuite.aeadId) {
  9327. break;
  9328. }
  9329. }
  9330. if (i >= echConfig->numCipherSuites) {
  9331. return BAD_FUNC_ARG;
  9332. }
  9333. ech->hpke = (Hpke*)XMALLOC(sizeof(Hpke), heap, DYNAMIC_TYPE_TMP_BUFFER);
  9334. if (ech->hpke == NULL)
  9335. return MEMORY_E;
  9336. ret = wc_HpkeInit(ech->hpke, echConfig->kemId, ech->cipherSuite.kdfId,
  9337. ech->cipherSuite.aeadId, heap);
  9338. /* get the rawConfigLen */
  9339. if (ret == 0)
  9340. ret = GetEchConfig(echConfig, NULL, &rawConfigLen);
  9341. if (ret == LENGTH_ONLY_E)
  9342. ret = 0;
  9343. /* create info */
  9344. if (ret == 0) {
  9345. infoLen = TLS_INFO_CONST_STRING_SZ + 1 + rawConfigLen;
  9346. info = (byte*)XMALLOC(infoLen, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9347. if (info == NULL)
  9348. ret = MEMORY_E;
  9349. else {
  9350. XMEMCPY(info, (byte*)TLS_INFO_CONST_STRING,
  9351. TLS_INFO_CONST_STRING_SZ + 1);
  9352. ret = GetEchConfig(echConfig, info +
  9353. TLS_INFO_CONST_STRING_SZ + 1, &rawConfigLen);
  9354. }
  9355. }
  9356. /* decrypt the ech payload */
  9357. if (ret == 0)
  9358. ret = wc_HpkeOpenBase(ech->hpke, echConfig->receiverPrivkey, ech->enc,
  9359. ech->encLen, info, infoLen, aad, aadLen, ech->outerClientPayload,
  9360. ech->innerClientHelloLen,
  9361. ech->innerClientHello + HANDSHAKE_HEADER_SZ);
  9362. if (ret != 0) {
  9363. XFREE(ech->hpke, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9364. ech->hpke = NULL;
  9365. }
  9366. if (info != NULL)
  9367. XFREE(info, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9368. return ret;
  9369. }
  9370. /* parse the ech extension, if internal update ech->state and return, if
  9371. * external attempt to extract the inner client_hello, return the status */
  9372. static int TLSX_ECH_Parse(WOLFSSL* ssl, const byte* readBuf, word16 size,
  9373. byte msgType)
  9374. {
  9375. int ret = 0;
  9376. int i;
  9377. TLSX* echX;
  9378. WOLFSSL_ECH* ech;
  9379. WOLFSSL_EchConfig* echConfig;
  9380. byte* aadCopy;
  9381. byte* readBuf_p = (byte*)readBuf;
  9382. WOLFSSL_MSG("TLSX_ECH_Parse");
  9383. if (size == 0)
  9384. return BAD_FUNC_ARG;
  9385. if (msgType == encrypted_extensions) {
  9386. ret = wolfSSL_SetEchConfigs(ssl, readBuf, size);
  9387. if (ret == WOLFSSL_SUCCESS)
  9388. ret = 0;
  9389. }
  9390. else if (msgType == client_hello && ssl->ctx->echConfigs != NULL) {
  9391. echX = TLSX_Find(ssl->extensions, TLSX_ECH);
  9392. if (echX == NULL)
  9393. return BAD_FUNC_ARG;
  9394. ech = (WOLFSSL_ECH*)echX->data;
  9395. /* read the ech parameters before the payload */
  9396. ech->type = *readBuf_p;
  9397. readBuf_p++;
  9398. if (ech->type == ECH_TYPE_INNER) {
  9399. ech->state = ECH_PARSED_INTERNAL;
  9400. return 0;
  9401. }
  9402. /* technically the payload would only be 1 byte at this length */
  9403. if (size < 11 + ech->encLen)
  9404. return BAD_FUNC_ARG;
  9405. ato16(readBuf_p, &ech->cipherSuite.kdfId);
  9406. readBuf_p += 2;
  9407. ato16(readBuf_p, &ech->cipherSuite.aeadId);
  9408. readBuf_p += 2;
  9409. ech->configId = *readBuf_p;
  9410. readBuf_p++;
  9411. ato16(readBuf_p, &ech->encLen);
  9412. readBuf_p += 2;
  9413. if (ech->encLen > HPKE_Npk_MAX)
  9414. return BAD_FUNC_ARG;
  9415. XMEMCPY(ech->enc, readBuf_p, ech->encLen);
  9416. readBuf_p += ech->encLen;
  9417. ato16(readBuf_p, &ech->innerClientHelloLen);
  9418. ech->innerClientHelloLen -= AES_BLOCK_SIZE;
  9419. readBuf_p += 2;
  9420. ech->outerClientPayload = readBuf_p;
  9421. /* make a copy of the aad */
  9422. aadCopy = (byte*)XMALLOC(ech->aadLen, ssl->heap,
  9423. DYNAMIC_TYPE_TMP_BUFFER);
  9424. if (aadCopy == NULL)
  9425. return MEMORY_E;
  9426. XMEMCPY(aadCopy, ech->aad, ech->aadLen);
  9427. /* set the ech payload of the copy to zeros */
  9428. XMEMSET(aadCopy + (readBuf_p - ech->aad), 0,
  9429. ech->innerClientHelloLen + AES_BLOCK_SIZE);
  9430. /* allocate the inner payload buffer */
  9431. ech->innerClientHello =
  9432. (byte*)XMALLOC(ech->innerClientHelloLen + HANDSHAKE_HEADER_SZ,
  9433. ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  9434. if (ech->innerClientHello == NULL) {
  9435. XFREE(aadCopy, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  9436. return MEMORY_E;
  9437. }
  9438. /* first check if the config id matches */
  9439. echConfig = ssl->ctx->echConfigs;
  9440. while (echConfig != NULL) {
  9441. /* decrypt with this config */
  9442. if (echConfig->configId == ech->configId) {
  9443. ret = TLSX_ExtractEch(ech, echConfig, aadCopy, ech->aadLen,
  9444. ssl->heap);
  9445. break;
  9446. }
  9447. echConfig = echConfig->next;
  9448. }
  9449. /* try to decrypt with all configs */
  9450. if (echConfig == NULL || ret != 0) {
  9451. echConfig = ssl->ctx->echConfigs;
  9452. while (echConfig != NULL) {
  9453. ret = TLSX_ExtractEch(ech, echConfig, aadCopy, ech->aadLen,
  9454. ssl->heap);
  9455. if (ret== 0)
  9456. break;
  9457. echConfig = echConfig->next;
  9458. }
  9459. }
  9460. /* if we failed to extract */
  9461. if (ret != 0) {
  9462. XFREE(ech->innerClientHello, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  9463. ech->innerClientHello = NULL;
  9464. ech->state = ECH_WRITE_RETRY_CONFIGS;
  9465. }
  9466. else {
  9467. i = 0;
  9468. /* decrement until before the padding */
  9469. while (ech->innerClientHello[ech->innerClientHelloLen +
  9470. HANDSHAKE_HEADER_SZ - i - 1] != ECH_TYPE_INNER) {
  9471. i++;
  9472. }
  9473. /* subtract the length of the padding from the length */
  9474. ech->innerClientHelloLen -= i;
  9475. }
  9476. XFREE(aadCopy, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  9477. return 0;
  9478. }
  9479. return ret;
  9480. }
  9481. /* free the ech struct and the dynamic buffer it uses */
  9482. static void TLSX_ECH_Free(WOLFSSL_ECH* ech, void* heap)
  9483. {
  9484. if (ech->innerClientHello != NULL)
  9485. XFREE(ech->innerClientHello, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9486. if (ech->ephemeralKey != NULL)
  9487. wc_HpkeFreeKey(ech->hpke, ech->hpke->kem, ech->ephemeralKey,
  9488. ech->hpke->heap);
  9489. if (ech->hpke != NULL)
  9490. XFREE(ech->hpke, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9491. XFREE(ech, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9492. (void)heap;
  9493. }
  9494. /* encrypt the client hello and store it in ech->outerClientPayload, return
  9495. * status */
  9496. int TLSX_FinalizeEch(WOLFSSL_ECH* ech, byte* aad, word32 aadLen)
  9497. {
  9498. int ret;
  9499. void* receiverPubkey = NULL;
  9500. byte* info;
  9501. int infoLen;
  9502. byte* aadCopy;
  9503. /* import the server public key */
  9504. ret = wc_HpkeDeserializePublicKey(ech->hpke, &receiverPubkey,
  9505. ech->echConfig->receiverPubkey, ech->encLen);
  9506. if (ret == 0) {
  9507. /* create info */
  9508. infoLen = TLS_INFO_CONST_STRING_SZ + 1 + ech->echConfig->rawLen;
  9509. info = (byte*)XMALLOC(infoLen, ech->hpke->heap,
  9510. DYNAMIC_TYPE_TMP_BUFFER);
  9511. if (info == NULL)
  9512. ret = MEMORY_E;
  9513. if (ret == 0) {
  9514. /* puts the null byte in for me */
  9515. XMEMCPY(info, (byte*)TLS_INFO_CONST_STRING, TLS_INFO_CONST_STRING_SZ
  9516. + 1);
  9517. XMEMCPY(info + TLS_INFO_CONST_STRING_SZ + 1, ech->echConfig->raw,
  9518. ech->echConfig->rawLen);
  9519. /* make a copy of the aad since we overwrite it */
  9520. aadCopy = (byte*)XMALLOC(aadLen, ech->hpke->heap,
  9521. DYNAMIC_TYPE_TMP_BUFFER);
  9522. if (aadCopy == NULL) {
  9523. XFREE(info, ech->hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  9524. ret = MEMORY_E;
  9525. }
  9526. }
  9527. if (ret == 0) {
  9528. XMEMCPY(aadCopy, aad, aadLen);
  9529. /* seal the payload */
  9530. ret = wc_HpkeSealBase(ech->hpke, ech->ephemeralKey, receiverPubkey,
  9531. info, infoLen, aadCopy, aadLen, ech->innerClientHello,
  9532. ech->innerClientHelloLen - ech->hpke->Nt,
  9533. ech->outerClientPayload);
  9534. XFREE(info, ech->hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  9535. XFREE(aadCopy, ech->hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  9536. }
  9537. }
  9538. if (receiverPubkey != NULL)
  9539. wc_HpkeFreeKey(ech->hpke, ech->hpke->kem, receiverPubkey,
  9540. ech->hpke->heap);
  9541. return ret;
  9542. }
  9543. #define GREASE_ECH_USE TLSX_GreaseECH_Use
  9544. #define ECH_USE TLSX_ECH_Use
  9545. #define SERVER_ECH_USE TLSX_ServerECH_Use
  9546. #define ECH_WRITE TLSX_ECH_Write
  9547. #define ECH_GET_SIZE TLSX_ECH_GetSize
  9548. #define ECH_PARSE TLSX_ECH_Parse
  9549. #define ECH_FREE TLSX_ECH_Free
  9550. #endif
  9551. /** Releases all extensions in the provided list. */
  9552. void TLSX_FreeAll(TLSX* list, void* heap)
  9553. {
  9554. TLSX* extension;
  9555. while ((extension = list)) {
  9556. list = extension->next;
  9557. switch (extension->type) {
  9558. #ifdef HAVE_SNI
  9559. case TLSX_SERVER_NAME:
  9560. SNI_FREE_ALL((SNI*)extension->data, heap);
  9561. break;
  9562. #endif
  9563. case TLSX_TRUSTED_CA_KEYS:
  9564. TCA_FREE_ALL((TCA*)extension->data, heap);
  9565. break;
  9566. case TLSX_MAX_FRAGMENT_LENGTH:
  9567. MFL_FREE_ALL(extension->data, heap);
  9568. break;
  9569. case TLSX_EXTENDED_MASTER_SECRET:
  9570. case TLSX_TRUNCATED_HMAC:
  9571. /* Nothing to do. */
  9572. break;
  9573. case TLSX_SUPPORTED_GROUPS:
  9574. EC_FREE_ALL((SupportedCurve*)extension->data, heap);
  9575. break;
  9576. case TLSX_EC_POINT_FORMATS:
  9577. PF_FREE_ALL((PointFormat*)extension->data, heap);
  9578. break;
  9579. case TLSX_STATUS_REQUEST:
  9580. CSR_FREE_ALL((CertificateStatusRequest*)extension->data, heap);
  9581. break;
  9582. case TLSX_STATUS_REQUEST_V2:
  9583. CSR2_FREE_ALL((CertificateStatusRequestItemV2*)extension->data,
  9584. heap);
  9585. break;
  9586. case TLSX_RENEGOTIATION_INFO:
  9587. SCR_FREE_ALL(extension->data, heap);
  9588. break;
  9589. case TLSX_SESSION_TICKET:
  9590. WOLF_STK_FREE(extension->data, heap);
  9591. break;
  9592. case TLSX_APPLICATION_LAYER_PROTOCOL:
  9593. ALPN_FREE_ALL((ALPN*)extension->data, heap);
  9594. break;
  9595. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9596. case TLSX_SIGNATURE_ALGORITHMS:
  9597. SA_FREE_ALL((SignatureAlgorithms*)extension->data, heap);
  9598. break;
  9599. #endif
  9600. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  9601. case TLSX_ENCRYPT_THEN_MAC:
  9602. break;
  9603. #endif
  9604. #ifdef WOLFSSL_TLS13
  9605. case TLSX_SUPPORTED_VERSIONS:
  9606. break;
  9607. #ifdef WOLFSSL_SEND_HRR_COOKIE
  9608. case TLSX_COOKIE:
  9609. CKE_FREE_ALL((Cookie*)extension->data, heap);
  9610. break;
  9611. #endif
  9612. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9613. case TLSX_PRE_SHARED_KEY:
  9614. PSK_FREE_ALL((PreSharedKey*)extension->data, heap);
  9615. break;
  9616. case TLSX_PSK_KEY_EXCHANGE_MODES:
  9617. break;
  9618. #endif
  9619. #ifdef WOLFSSL_EARLY_DATA
  9620. case TLSX_EARLY_DATA:
  9621. break;
  9622. #endif
  9623. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  9624. case TLSX_POST_HANDSHAKE_AUTH:
  9625. break;
  9626. #endif
  9627. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9628. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  9629. break;
  9630. #endif
  9631. case TLSX_KEY_SHARE:
  9632. KS_FREE_ALL((KeyShareEntry*)extension->data, heap);
  9633. break;
  9634. #endif
  9635. #ifdef WOLFSSL_SRTP
  9636. case TLSX_USE_SRTP:
  9637. SRTP_FREE((TlsxSrtp*)extension->data, heap);
  9638. break;
  9639. #endif
  9640. #ifdef WOLFSSL_QUIC
  9641. case TLSX_KEY_QUIC_TP_PARAMS:
  9642. FALL_THROUGH;
  9643. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  9644. QTP_FREE((QuicTransportParam*)extension->data, heap);
  9645. break;
  9646. #endif
  9647. #ifdef WOLFSSL_DTLS_CID
  9648. case TLSX_CONNECTION_ID:
  9649. CID_FREE((byte*)extension->data, heap);
  9650. break;
  9651. #endif /* WOLFSSL_DTLS_CID */
  9652. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  9653. case TLSX_ECH:
  9654. ECH_FREE((WOLFSSL_ECH*)extension->data, heap);
  9655. break;
  9656. #endif
  9657. default:
  9658. break;
  9659. }
  9660. XFREE(extension, heap, DYNAMIC_TYPE_TLSX);
  9661. }
  9662. (void)heap;
  9663. }
  9664. /** Checks if the tls extensions are supported based on the protocol version. */
  9665. int TLSX_SupportExtensions(WOLFSSL* ssl) {
  9666. return ssl && (IsTLS(ssl) || ssl->version.major == DTLS_MAJOR);
  9667. }
  9668. /** Tells the buffered size of the extensions in a list. */
  9669. static int TLSX_GetSize(TLSX* list, byte* semaphore, byte msgType,
  9670. word16* pLength)
  9671. {
  9672. int ret = 0;
  9673. TLSX* extension;
  9674. word16 length = 0;
  9675. byte isRequest = (msgType == client_hello ||
  9676. msgType == certificate_request);
  9677. while ((extension = list)) {
  9678. list = extension->next;
  9679. /* only extensions marked as response are sent back to the client. */
  9680. if (!isRequest && !extension->resp)
  9681. continue; /* skip! */
  9682. /* ssl level extensions are expected to override ctx level ones. */
  9683. if (!IS_OFF(semaphore, TLSX_ToSemaphore(extension->type)))
  9684. continue; /* skip! */
  9685. /* extension type + extension data length. */
  9686. length += HELLO_EXT_TYPE_SZ + OPAQUE16_LEN;
  9687. switch (extension->type) {
  9688. #ifdef HAVE_SNI
  9689. case TLSX_SERVER_NAME:
  9690. /* SNI only sends the name on the request. */
  9691. if (isRequest)
  9692. length += SNI_GET_SIZE((SNI*)extension->data);
  9693. break;
  9694. #endif
  9695. case TLSX_TRUSTED_CA_KEYS:
  9696. /* TCA only sends the list on the request. */
  9697. if (isRequest)
  9698. length += TCA_GET_SIZE((TCA*)extension->data);
  9699. break;
  9700. case TLSX_MAX_FRAGMENT_LENGTH:
  9701. length += MFL_GET_SIZE(extension->data);
  9702. break;
  9703. case TLSX_EXTENDED_MASTER_SECRET:
  9704. case TLSX_TRUNCATED_HMAC:
  9705. /* always empty. */
  9706. break;
  9707. case TLSX_SUPPORTED_GROUPS:
  9708. length += EC_GET_SIZE((SupportedCurve*)extension->data);
  9709. break;
  9710. case TLSX_EC_POINT_FORMATS:
  9711. length += PF_GET_SIZE((PointFormat*)extension->data);
  9712. break;
  9713. case TLSX_STATUS_REQUEST:
  9714. length += CSR_GET_SIZE(
  9715. (CertificateStatusRequest*)extension->data, isRequest);
  9716. break;
  9717. case TLSX_STATUS_REQUEST_V2:
  9718. length += CSR2_GET_SIZE(
  9719. (CertificateStatusRequestItemV2*)extension->data,
  9720. isRequest);
  9721. break;
  9722. case TLSX_RENEGOTIATION_INFO:
  9723. length += SCR_GET_SIZE((SecureRenegotiation*)extension->data,
  9724. isRequest);
  9725. break;
  9726. case TLSX_SESSION_TICKET:
  9727. length += WOLF_STK_GET_SIZE((SessionTicket*)extension->data,
  9728. isRequest);
  9729. break;
  9730. case TLSX_APPLICATION_LAYER_PROTOCOL:
  9731. length += ALPN_GET_SIZE((ALPN*)extension->data);
  9732. break;
  9733. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9734. case TLSX_SIGNATURE_ALGORITHMS:
  9735. length += SA_GET_SIZE(extension->data);
  9736. break;
  9737. #endif
  9738. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  9739. case TLSX_ENCRYPT_THEN_MAC:
  9740. ret = ETM_GET_SIZE(msgType, &length);
  9741. break;
  9742. #endif /* HAVE_ENCRYPT_THEN_MAC */
  9743. #ifdef WOLFSSL_TLS13
  9744. case TLSX_SUPPORTED_VERSIONS:
  9745. ret = SV_GET_SIZE(extension->data, msgType, &length);
  9746. break;
  9747. #ifdef WOLFSSL_SEND_HRR_COOKIE
  9748. case TLSX_COOKIE:
  9749. ret = CKE_GET_SIZE((Cookie*)extension->data, msgType, &length);
  9750. break;
  9751. #endif
  9752. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9753. case TLSX_PRE_SHARED_KEY:
  9754. ret = PSK_GET_SIZE((PreSharedKey*)extension->data, msgType,
  9755. &length);
  9756. break;
  9757. case TLSX_PSK_KEY_EXCHANGE_MODES:
  9758. ret = PKM_GET_SIZE((byte)extension->val, msgType, &length);
  9759. break;
  9760. #endif
  9761. #ifdef WOLFSSL_EARLY_DATA
  9762. case TLSX_EARLY_DATA:
  9763. ret = EDI_GET_SIZE(msgType, &length);
  9764. break;
  9765. #endif
  9766. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  9767. case TLSX_POST_HANDSHAKE_AUTH:
  9768. ret = PHA_GET_SIZE(msgType, &length);
  9769. break;
  9770. #endif
  9771. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9772. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  9773. length += SAC_GET_SIZE(extension->data);
  9774. break;
  9775. #endif
  9776. case TLSX_KEY_SHARE:
  9777. length += KS_GET_SIZE((KeyShareEntry*)extension->data, msgType);
  9778. break;
  9779. #endif
  9780. #ifdef WOLFSSL_SRTP
  9781. case TLSX_USE_SRTP:
  9782. length += SRTP_GET_SIZE((TlsxSrtp*)extension->data);
  9783. break;
  9784. #endif
  9785. #ifdef WOLFSSL_QUIC
  9786. case TLSX_KEY_QUIC_TP_PARAMS:
  9787. FALL_THROUGH; /* followed by */
  9788. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  9789. length += QTP_GET_SIZE(extension);
  9790. break;
  9791. #endif
  9792. #ifdef WOLFSSL_DTLS_CID
  9793. case TLSX_CONNECTION_ID:
  9794. length += CID_GET_SIZE((byte*)extension->data);
  9795. break;
  9796. #endif /* WOLFSSL_DTLS_CID */
  9797. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  9798. case TLSX_ECH:
  9799. length += ECH_GET_SIZE((WOLFSSL_ECH*)extension->data);
  9800. break;
  9801. #endif
  9802. default:
  9803. break;
  9804. }
  9805. /* marks the extension as processed so ctx level */
  9806. /* extensions don't overlap with ssl level ones. */
  9807. TURN_ON(semaphore, TLSX_ToSemaphore(extension->type));
  9808. }
  9809. *pLength += length;
  9810. return ret;
  9811. }
  9812. /** Writes the extensions of a list in a buffer. */
  9813. static int TLSX_Write(TLSX* list, byte* output, byte* semaphore,
  9814. byte msgType, word16* pOffset)
  9815. {
  9816. int ret = 0;
  9817. TLSX* extension;
  9818. word16 offset = 0;
  9819. word16 length_offset = 0;
  9820. byte isRequest = (msgType == client_hello ||
  9821. msgType == certificate_request);
  9822. while ((extension = list)) {
  9823. list = extension->next;
  9824. /* only extensions marked as response are written in a response. */
  9825. if (!isRequest && !extension->resp)
  9826. continue; /* skip! */
  9827. /* ssl level extensions are expected to override ctx level ones. */
  9828. if (!IS_OFF(semaphore, TLSX_ToSemaphore(extension->type)))
  9829. continue; /* skip! */
  9830. /* writes extension type. */
  9831. c16toa(extension->type, output + offset);
  9832. offset += HELLO_EXT_TYPE_SZ + OPAQUE16_LEN;
  9833. length_offset = offset;
  9834. /* extension data should be written internally. */
  9835. switch (extension->type) {
  9836. #ifdef HAVE_SNI
  9837. case TLSX_SERVER_NAME:
  9838. if (isRequest) {
  9839. WOLFSSL_MSG("SNI extension to write");
  9840. offset += SNI_WRITE((SNI*)extension->data, output + offset);
  9841. }
  9842. break;
  9843. #endif
  9844. case TLSX_TRUSTED_CA_KEYS:
  9845. WOLFSSL_MSG("Trusted CA Indication extension to write");
  9846. if (isRequest) {
  9847. offset += TCA_WRITE((TCA*)extension->data, output + offset);
  9848. }
  9849. break;
  9850. case TLSX_MAX_FRAGMENT_LENGTH:
  9851. WOLFSSL_MSG("Max Fragment Length extension to write");
  9852. offset += MFL_WRITE((byte*)extension->data, output + offset);
  9853. break;
  9854. case TLSX_EXTENDED_MASTER_SECRET:
  9855. WOLFSSL_MSG("Extended Master Secret");
  9856. /* always empty. */
  9857. break;
  9858. case TLSX_TRUNCATED_HMAC:
  9859. WOLFSSL_MSG("Truncated HMAC extension to write");
  9860. /* always empty. */
  9861. break;
  9862. case TLSX_SUPPORTED_GROUPS:
  9863. WOLFSSL_MSG("Supported Groups extension to write");
  9864. offset += EC_WRITE((SupportedCurve*)extension->data,
  9865. output + offset);
  9866. break;
  9867. case TLSX_EC_POINT_FORMATS:
  9868. WOLFSSL_MSG("Point Formats extension to write");
  9869. offset += PF_WRITE((PointFormat*)extension->data,
  9870. output + offset);
  9871. break;
  9872. case TLSX_STATUS_REQUEST:
  9873. WOLFSSL_MSG("Certificate Status Request extension to write");
  9874. offset += CSR_WRITE((CertificateStatusRequest*)extension->data,
  9875. output + offset, isRequest);
  9876. break;
  9877. case TLSX_STATUS_REQUEST_V2:
  9878. WOLFSSL_MSG("Certificate Status Request v2 extension to write");
  9879. offset += CSR2_WRITE(
  9880. (CertificateStatusRequestItemV2*)extension->data,
  9881. output + offset, isRequest);
  9882. break;
  9883. case TLSX_RENEGOTIATION_INFO:
  9884. WOLFSSL_MSG("Secure Renegotiation extension to write");
  9885. offset += SCR_WRITE((SecureRenegotiation*)extension->data,
  9886. output + offset, isRequest);
  9887. break;
  9888. case TLSX_SESSION_TICKET:
  9889. WOLFSSL_MSG("Session Ticket extension to write");
  9890. offset += WOLF_STK_WRITE((SessionTicket*)extension->data,
  9891. output + offset, isRequest);
  9892. break;
  9893. case TLSX_APPLICATION_LAYER_PROTOCOL:
  9894. WOLFSSL_MSG("ALPN extension to write");
  9895. offset += ALPN_WRITE((ALPN*)extension->data, output + offset);
  9896. break;
  9897. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9898. case TLSX_SIGNATURE_ALGORITHMS:
  9899. WOLFSSL_MSG("Signature Algorithms extension to write");
  9900. offset += SA_WRITE(extension->data, output + offset);
  9901. break;
  9902. #endif
  9903. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  9904. case TLSX_ENCRYPT_THEN_MAC:
  9905. WOLFSSL_MSG("Encrypt-Then-Mac extension to write");
  9906. ret = ETM_WRITE(extension->data, output, msgType, &offset);
  9907. break;
  9908. #endif /* HAVE_ENCRYPT_THEN_MAC */
  9909. #ifdef WOLFSSL_TLS13
  9910. case TLSX_SUPPORTED_VERSIONS:
  9911. WOLFSSL_MSG("Supported Versions extension to write");
  9912. ret = SV_WRITE(extension->data, output + offset, msgType, &offset);
  9913. break;
  9914. #ifdef WOLFSSL_SEND_HRR_COOKIE
  9915. case TLSX_COOKIE:
  9916. WOLFSSL_MSG("Cookie extension to write");
  9917. ret = CKE_WRITE((Cookie*)extension->data, output + offset,
  9918. msgType, &offset);
  9919. break;
  9920. #endif
  9921. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9922. case TLSX_PRE_SHARED_KEY:
  9923. WOLFSSL_MSG("Pre-Shared Key extension to write");
  9924. ret = PSK_WRITE((PreSharedKey*)extension->data, output + offset,
  9925. msgType, &offset);
  9926. break;
  9927. case TLSX_PSK_KEY_EXCHANGE_MODES:
  9928. WOLFSSL_MSG("PSK Key Exchange Modes extension to write");
  9929. ret = PKM_WRITE((byte)extension->val, output + offset, msgType,
  9930. &offset);
  9931. break;
  9932. #endif
  9933. #ifdef WOLFSSL_EARLY_DATA
  9934. case TLSX_EARLY_DATA:
  9935. WOLFSSL_MSG("Early Data extension to write");
  9936. ret = EDI_WRITE(extension->val, output + offset, msgType,
  9937. &offset);
  9938. break;
  9939. #endif
  9940. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  9941. case TLSX_POST_HANDSHAKE_AUTH:
  9942. WOLFSSL_MSG("Post-Handshake Authentication extension to write");
  9943. ret = PHA_WRITE(output + offset, msgType, &offset);
  9944. break;
  9945. #endif
  9946. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9947. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  9948. WOLFSSL_MSG("Signature Algorithms extension to write");
  9949. offset += SAC_WRITE(extension->data, output + offset);
  9950. break;
  9951. #endif
  9952. case TLSX_KEY_SHARE:
  9953. WOLFSSL_MSG("Key Share extension to write");
  9954. offset += KS_WRITE((KeyShareEntry*)extension->data,
  9955. output + offset, msgType);
  9956. break;
  9957. #endif
  9958. #ifdef WOLFSSL_SRTP
  9959. case TLSX_USE_SRTP:
  9960. offset += SRTP_WRITE((TlsxSrtp*)extension->data, output+offset);
  9961. break;
  9962. #endif
  9963. #ifdef WOLFSSL_QUIC
  9964. case TLSX_KEY_QUIC_TP_PARAMS:
  9965. FALL_THROUGH;
  9966. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  9967. WOLFSSL_MSG("QUIC transport parameter to write");
  9968. offset += QTP_WRITE((QuicTransportParam*)extension->data,
  9969. output + offset);
  9970. break;
  9971. #endif
  9972. #ifdef WOLFSSL_DTLS_CID
  9973. case TLSX_CONNECTION_ID:
  9974. offset += CID_WRITE((byte*)extension->data, output+offset);
  9975. break;
  9976. #endif /* WOLFSSL_DTLS_CID */
  9977. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  9978. case TLSX_ECH:
  9979. ret = ECH_WRITE((WOLFSSL_ECH*)extension->data,
  9980. output + offset, &offset);
  9981. break;
  9982. #endif
  9983. default:
  9984. break;
  9985. }
  9986. /* writes extension data length. */
  9987. c16toa(offset - length_offset, output + length_offset - OPAQUE16_LEN);
  9988. /* marks the extension as processed so ctx level */
  9989. /* extensions don't overlap with ssl level ones. */
  9990. TURN_ON(semaphore, TLSX_ToSemaphore(extension->type));
  9991. /* if we encountered an error propagate it */
  9992. if (ret != 0)
  9993. break;
  9994. }
  9995. *pOffset += offset;
  9996. return ret;
  9997. }
  9998. #ifdef HAVE_SUPPORTED_CURVES
  9999. /* Populates the default supported groups / curves */
  10000. static int TLSX_PopulateSupportedGroups(WOLFSSL* ssl, TLSX** extensions)
  10001. {
  10002. int ret = WOLFSSL_SUCCESS;
  10003. #ifdef WOLFSSL_TLS13
  10004. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10005. if (ssl->options.resuming && ssl->session->namedGroup != 0) {
  10006. return TLSX_UseSupportedCurve(extensions, ssl->session->namedGroup,
  10007. ssl->heap);
  10008. }
  10009. #endif
  10010. if (ssl->numGroups != 0) {
  10011. int i;
  10012. for (i = 0; i < ssl->numGroups; i++) {
  10013. ret = TLSX_UseSupportedCurve(extensions, ssl->group[i], ssl->heap);
  10014. if (ret != WOLFSSL_SUCCESS)
  10015. return ret;
  10016. }
  10017. return WOLFSSL_SUCCESS;
  10018. }
  10019. #endif /* WOLFSSL_TLS13 */
  10020. #if defined(HAVE_ECC)
  10021. /* list in order by strength, since not all servers choose by strength */
  10022. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  10023. #ifndef NO_ECC_SECP
  10024. ret = TLSX_UseSupportedCurve(extensions,
  10025. WOLFSSL_ECC_SECP521R1, ssl->heap);
  10026. if (ret != WOLFSSL_SUCCESS) return ret;
  10027. #endif
  10028. #endif
  10029. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  10030. #ifdef HAVE_ECC_BRAINPOOL
  10031. ret = TLSX_UseSupportedCurve(extensions,
  10032. WOLFSSL_ECC_BRAINPOOLP512R1, ssl->heap);
  10033. if (ret != WOLFSSL_SUCCESS) return ret;
  10034. #endif
  10035. #endif
  10036. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  10037. #ifndef NO_ECC_SECP
  10038. ret = TLSX_UseSupportedCurve(extensions,
  10039. WOLFSSL_ECC_SECP384R1, ssl->heap);
  10040. if (ret != WOLFSSL_SUCCESS) return ret;
  10041. #endif
  10042. #ifdef HAVE_ECC_BRAINPOOL
  10043. ret = TLSX_UseSupportedCurve(extensions,
  10044. WOLFSSL_ECC_BRAINPOOLP384R1, ssl->heap);
  10045. if (ret != WOLFSSL_SUCCESS) return ret;
  10046. #endif
  10047. #endif
  10048. #endif /* HAVE_ECC */
  10049. #ifndef HAVE_FIPS
  10050. #if defined(HAVE_CURVE448) && ECC_MIN_KEY_SZ <= 448
  10051. ret = TLSX_UseSupportedCurve(extensions,
  10052. WOLFSSL_ECC_X448, ssl->heap);
  10053. if (ret != WOLFSSL_SUCCESS) return ret;
  10054. #endif
  10055. #endif /* HAVE_FIPS */
  10056. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  10057. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  10058. #ifndef NO_ECC_SECP
  10059. ret = TLSX_UseSupportedCurve(extensions,
  10060. WOLFSSL_ECC_SECP256R1, ssl->heap);
  10061. if (ret != WOLFSSL_SUCCESS) return ret;
  10062. #endif
  10063. #ifdef HAVE_ECC_KOBLITZ
  10064. ret = TLSX_UseSupportedCurve(extensions,
  10065. WOLFSSL_ECC_SECP256K1, ssl->heap);
  10066. if (ret != WOLFSSL_SUCCESS) return ret;
  10067. #endif
  10068. #ifdef HAVE_ECC_BRAINPOOL
  10069. ret = TLSX_UseSupportedCurve(extensions,
  10070. WOLFSSL_ECC_BRAINPOOLP256R1, ssl->heap);
  10071. if (ret != WOLFSSL_SUCCESS) return ret;
  10072. #endif
  10073. #ifdef WOLFSSL_SM2
  10074. ret = TLSX_UseSupportedCurve(extensions,
  10075. WOLFSSL_ECC_SM2P256V1, ssl->heap);
  10076. if (ret != WOLFSSL_SUCCESS) return ret;
  10077. #endif
  10078. #endif
  10079. #endif /* HAVE_ECC */
  10080. #ifndef HAVE_FIPS
  10081. #if defined(HAVE_CURVE25519) && ECC_MIN_KEY_SZ <= 256
  10082. ret = TLSX_UseSupportedCurve(extensions,
  10083. WOLFSSL_ECC_X25519, ssl->heap);
  10084. if (ret != WOLFSSL_SUCCESS) return ret;
  10085. #endif
  10086. #endif /* HAVE_FIPS */
  10087. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  10088. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  10089. #ifndef NO_ECC_SECP
  10090. ret = TLSX_UseSupportedCurve(extensions,
  10091. WOLFSSL_ECC_SECP224R1, ssl->heap);
  10092. if (ret != WOLFSSL_SUCCESS) return ret;
  10093. #endif
  10094. #ifdef HAVE_ECC_KOBLITZ
  10095. ret = TLSX_UseSupportedCurve(extensions,
  10096. WOLFSSL_ECC_SECP224K1, ssl->heap);
  10097. if (ret != WOLFSSL_SUCCESS) return ret;
  10098. #endif
  10099. #endif
  10100. #ifndef HAVE_FIPS
  10101. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  10102. #ifndef NO_ECC_SECP
  10103. ret = TLSX_UseSupportedCurve(extensions,
  10104. WOLFSSL_ECC_SECP192R1, ssl->heap);
  10105. if (ret != WOLFSSL_SUCCESS) return ret;
  10106. #endif
  10107. #ifdef HAVE_ECC_KOBLITZ
  10108. ret = TLSX_UseSupportedCurve(extensions,
  10109. WOLFSSL_ECC_SECP192K1, ssl->heap);
  10110. if (ret != WOLFSSL_SUCCESS) return ret;
  10111. #endif
  10112. #endif
  10113. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  10114. #ifndef NO_ECC_SECP
  10115. ret = TLSX_UseSupportedCurve(extensions,
  10116. WOLFSSL_ECC_SECP160R1, ssl->heap);
  10117. if (ret != WOLFSSL_SUCCESS) return ret;
  10118. #endif
  10119. #ifdef HAVE_ECC_SECPR2
  10120. ret = TLSX_UseSupportedCurve(extensions,
  10121. WOLFSSL_ECC_SECP160R2, ssl->heap);
  10122. if (ret != WOLFSSL_SUCCESS) return ret;
  10123. #endif
  10124. #ifdef HAVE_ECC_KOBLITZ
  10125. ret = TLSX_UseSupportedCurve(extensions,
  10126. WOLFSSL_ECC_SECP160K1, ssl->heap);
  10127. if (ret != WOLFSSL_SUCCESS) return ret;
  10128. #endif
  10129. #endif
  10130. #endif /* HAVE_FIPS */
  10131. #endif /* HAVE_ECC */
  10132. #ifndef NO_DH
  10133. /* Add FFDHE supported groups. */
  10134. #ifdef HAVE_FFDHE_8192
  10135. if (8192/8 >= ssl->options.minDhKeySz &&
  10136. 8192/8 <= ssl->options.maxDhKeySz) {
  10137. ret = TLSX_UseSupportedCurve(extensions,
  10138. WOLFSSL_FFDHE_8192, ssl->heap);
  10139. if (ret != WOLFSSL_SUCCESS)
  10140. return ret;
  10141. }
  10142. #endif
  10143. #ifdef HAVE_FFDHE_6144
  10144. if (6144/8 >= ssl->options.minDhKeySz &&
  10145. 6144/8 <= ssl->options.maxDhKeySz) {
  10146. ret = TLSX_UseSupportedCurve(extensions,
  10147. WOLFSSL_FFDHE_6144, ssl->heap);
  10148. if (ret != WOLFSSL_SUCCESS)
  10149. return ret;
  10150. }
  10151. #endif
  10152. #ifdef HAVE_FFDHE_4096
  10153. if (4096/8 >= ssl->options.minDhKeySz &&
  10154. 4096/8 <= ssl->options.maxDhKeySz) {
  10155. ret = TLSX_UseSupportedCurve(extensions,
  10156. WOLFSSL_FFDHE_4096, ssl->heap);
  10157. if (ret != WOLFSSL_SUCCESS)
  10158. return ret;
  10159. }
  10160. #endif
  10161. #ifdef HAVE_FFDHE_3072
  10162. if (3072/8 >= ssl->options.minDhKeySz &&
  10163. 3072/8 <= ssl->options.maxDhKeySz) {
  10164. ret = TLSX_UseSupportedCurve(extensions,
  10165. WOLFSSL_FFDHE_3072, ssl->heap);
  10166. if (ret != WOLFSSL_SUCCESS)
  10167. return ret;
  10168. }
  10169. #endif
  10170. #ifdef HAVE_FFDHE_2048
  10171. if (2048/8 >= ssl->options.minDhKeySz &&
  10172. 2048/8 <= ssl->options.maxDhKeySz) {
  10173. ret = TLSX_UseSupportedCurve(extensions,
  10174. WOLFSSL_FFDHE_2048, ssl->heap);
  10175. if (ret != WOLFSSL_SUCCESS)
  10176. return ret;
  10177. }
  10178. #endif
  10179. #endif
  10180. #ifdef HAVE_PQC
  10181. #ifdef WOLFSSL_WC_KYBER
  10182. #ifdef WOLFSSL_KYBER512
  10183. if (ret == WOLFSSL_SUCCESS)
  10184. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL1,
  10185. ssl->heap);
  10186. #endif
  10187. #ifdef WOLFSSL_KYBER768
  10188. if (ret == WOLFSSL_SUCCESS)
  10189. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL3,
  10190. ssl->heap);
  10191. #endif
  10192. #ifdef WOLFSSL_KYBER768
  10193. if (ret == WOLFSSL_SUCCESS)
  10194. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL5,
  10195. ssl->heap);
  10196. #endif
  10197. #elif defined(HAVE_LIBOQS)
  10198. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL1, ssl->heap);
  10199. if (ret == WOLFSSL_SUCCESS)
  10200. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL3,
  10201. ssl->heap);
  10202. if (ret == WOLFSSL_SUCCESS)
  10203. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL5,
  10204. ssl->heap);
  10205. if (ret == WOLFSSL_SUCCESS)
  10206. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P256_KYBER_LEVEL1,
  10207. ssl->heap);
  10208. if (ret == WOLFSSL_SUCCESS)
  10209. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P384_KYBER_LEVEL3,
  10210. ssl->heap);
  10211. if (ret == WOLFSSL_SUCCESS)
  10212. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P521_KYBER_LEVEL5,
  10213. ssl->heap);
  10214. #elif defined(HAVE_PQM4)
  10215. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL1, ssl->heap);
  10216. #endif /* HAVE_LIBOQS */
  10217. #endif /* HAVE_PQC */
  10218. (void)ssl;
  10219. (void)extensions;
  10220. return ret;
  10221. }
  10222. #endif /* HAVE_SUPPORTED_CURVES */
  10223. int TLSX_PopulateExtensions(WOLFSSL* ssl, byte isServer)
  10224. {
  10225. int ret = 0;
  10226. byte* public_key = NULL;
  10227. word16 public_key_len = 0;
  10228. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  10229. int usingPSK = 0;
  10230. #endif
  10231. #if defined(HAVE_SUPPORTED_CURVES) && defined(WOLFSSL_TLS13)
  10232. TLSX* extension = NULL;
  10233. word16 namedGroup = WOLFSSL_NAMED_GROUP_INVALID;
  10234. #endif
  10235. /* server will add extension depending on what is parsed from client */
  10236. if (!isServer) {
  10237. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  10238. if (!ssl->options.disallowEncThenMac) {
  10239. ret = TLSX_EncryptThenMac_Use(ssl);
  10240. if (ret != 0)
  10241. return ret;
  10242. }
  10243. #endif
  10244. #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  10245. defined(HAVE_CURVE448)) && defined(HAVE_SUPPORTED_CURVES)
  10246. if (!ssl->options.userCurves && !ssl->ctx->userCurves) {
  10247. if (TLSX_Find(ssl->ctx->extensions,
  10248. TLSX_SUPPORTED_GROUPS) == NULL) {
  10249. ret = TLSX_PopulateSupportedGroups(ssl, &ssl->extensions);
  10250. if (ret != WOLFSSL_SUCCESS)
  10251. return ret;
  10252. }
  10253. }
  10254. if ((!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade) &&
  10255. TLSX_Find(ssl->ctx->extensions, TLSX_EC_POINT_FORMATS) == NULL &&
  10256. TLSX_Find(ssl->extensions, TLSX_EC_POINT_FORMATS) == NULL) {
  10257. ret = TLSX_UsePointFormat(&ssl->extensions,
  10258. WOLFSSL_EC_PF_UNCOMPRESSED, ssl->heap);
  10259. if (ret != WOLFSSL_SUCCESS)
  10260. return ret;
  10261. }
  10262. #endif /* (HAVE_ECC || CURVE25519 || CURVE448) && HAVE_SUPPORTED_CURVES */
  10263. #ifdef WOLFSSL_SRTP
  10264. if (ssl->options.dtls && ssl->dtlsSrtpProfiles != 0) {
  10265. WOLFSSL_MSG("Adding DTLS SRTP extension");
  10266. if ((ret = TLSX_UseSRTP(&ssl->extensions, ssl->dtlsSrtpProfiles,
  10267. ssl->heap)) != 0) {
  10268. return ret;
  10269. }
  10270. }
  10271. #endif
  10272. } /* is not server */
  10273. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10274. WOLFSSL_MSG("Adding signature algorithms extension");
  10275. if ((ret = TLSX_SetSignatureAlgorithms(&ssl->extensions, ssl, ssl->heap))
  10276. != 0) {
  10277. return ret;
  10278. }
  10279. #else
  10280. ret = 0;
  10281. #endif
  10282. #ifdef WOLFSSL_TLS13
  10283. if (!isServer && IsAtLeastTLSv1_3(ssl->version)) {
  10284. /* Add mandatory TLS v1.3 extension: supported version */
  10285. WOLFSSL_MSG("Adding supported versions extension");
  10286. if ((ret = TLSX_SetSupportedVersions(&ssl->extensions, ssl,
  10287. ssl->heap)) != 0) {
  10288. return ret;
  10289. }
  10290. #if !defined(HAVE_ECC) && !defined(HAVE_CURVE25519) && \
  10291. !defined(HAVE_CURVE448) && defined(HAVE_SUPPORTED_CURVES)
  10292. if (TLSX_Find(ssl->ctx->extensions, TLSX_SUPPORTED_GROUPS) == NULL) {
  10293. /* Put in DH groups for TLS 1.3 only. */
  10294. ret = TLSX_PopulateSupportedGroups(ssl, &ssl->extensions);
  10295. if (ret != WOLFSSL_SUCCESS)
  10296. return ret;
  10297. /* ret value will be overwritten in !NO_PSK case */
  10298. #ifdef NO_PSK
  10299. ret = 0;
  10300. #endif
  10301. }
  10302. #endif /* !(HAVE_ECC || CURVE25519 || CURVE448) && HAVE_SUPPORTED_CURVES */
  10303. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10304. if (ssl->certHashSigAlgoSz > 0) {
  10305. WOLFSSL_MSG("Adding signature algorithms cert extension");
  10306. if ((ret = TLSX_SetSignatureAlgorithmsCert(&ssl->extensions,
  10307. ssl, ssl->heap)) != 0) {
  10308. return ret;
  10309. }
  10310. }
  10311. #endif
  10312. #if defined(HAVE_SUPPORTED_CURVES)
  10313. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  10314. if (extension == NULL) {
  10315. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10316. if (ssl->options.resuming && ssl->session->namedGroup != 0)
  10317. namedGroup = ssl->session->namedGroup;
  10318. else
  10319. #endif
  10320. if (ssl->numGroups > 0) {
  10321. int set = 0;
  10322. int i, j;
  10323. /* try to find the highest element in ssl->group[]
  10324. * that is contained in preferredGroup[].
  10325. */
  10326. namedGroup = preferredGroup[0];
  10327. for (i = 0; i < ssl->numGroups && !set; i++) {
  10328. for (j = 0; preferredGroup[j] != WOLFSSL_NAMED_GROUP_INVALID; j++) {
  10329. if (preferredGroup[j] == ssl->group[i]
  10330. #ifdef HAVE_LIBOQS
  10331. && TLSX_KeyShare_IsSupported(preferredGroup[j])
  10332. #endif
  10333. ) {
  10334. namedGroup = ssl->group[i];
  10335. set = 1;
  10336. break;
  10337. }
  10338. }
  10339. }
  10340. if (!set)
  10341. namedGroup = WOLFSSL_NAMED_GROUP_INVALID;
  10342. }
  10343. else {
  10344. /* Choose the most preferred group. */
  10345. namedGroup = preferredGroup[0];
  10346. #ifdef HAVE_LIBOQS
  10347. if (!TLSX_KeyShare_IsSupported(namedGroup)) {
  10348. int i = 1;
  10349. for (;preferredGroup[i] != WOLFSSL_NAMED_GROUP_INVALID;
  10350. i++) {
  10351. if (TLSX_KeyShare_IsSupported(preferredGroup[i]))
  10352. break;
  10353. }
  10354. namedGroup = preferredGroup[i];
  10355. }
  10356. #endif
  10357. }
  10358. }
  10359. else {
  10360. KeyShareEntry* kse = (KeyShareEntry*)extension->data;
  10361. if (kse)
  10362. namedGroup = kse->group;
  10363. }
  10364. if (namedGroup != WOLFSSL_NAMED_GROUP_INVALID) {
  10365. #ifdef HAVE_PQC
  10366. /* For KEMs, the key share has already been generated, but not
  10367. * if we are resuming. */
  10368. if (!WOLFSSL_NAMED_GROUP_IS_PQC(namedGroup)
  10369. #ifdef HAVE_SESSION_TICKET
  10370. || ssl->options.resuming
  10371. #endif /* HAVE_SESSION_TICKET */
  10372. )
  10373. #endif /* HAVE_PQC */
  10374. {
  10375. ret = TLSX_KeyShare_Use(ssl, namedGroup, 0, NULL, NULL,
  10376. &ssl->extensions);
  10377. }
  10378. if (ret != 0)
  10379. return ret;
  10380. }
  10381. #endif /* HAVE_SUPPORTED_CURVES */
  10382. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10383. TLSX_Remove(&ssl->extensions, TLSX_PRE_SHARED_KEY, ssl->heap);
  10384. #endif
  10385. #if defined(HAVE_SESSION_TICKET)
  10386. if (ssl->options.resuming && ssl->session->ticketLen > 0) {
  10387. WOLFSSL_SESSION* sess = ssl->session;
  10388. #ifdef WOLFSSL_32BIT_MILLI_TIME
  10389. word32 now, milli;
  10390. #else
  10391. word64 now, milli;
  10392. #endif
  10393. if (sess->ticketLen > MAX_PSK_ID_LEN) {
  10394. WOLFSSL_MSG("Session ticket length for PSK ext is too large");
  10395. return BUFFER_ERROR;
  10396. }
  10397. /* Determine the MAC algorithm for the cipher suite used. */
  10398. ssl->options.cipherSuite0 = sess->cipherSuite0;
  10399. ssl->options.cipherSuite = sess->cipherSuite;
  10400. ret = SetCipherSpecs(ssl);
  10401. if (ret != 0)
  10402. return ret;
  10403. now = TimeNowInMilliseconds();
  10404. if (now == 0)
  10405. return GETTIME_ERROR;
  10406. #ifdef WOLFSSL_32BIT_MILLI_TIME
  10407. if (now < sess->ticketSeen)
  10408. milli = (0xFFFFFFFFU - sess->ticketSeen) + 1 + now;
  10409. else
  10410. milli = now - sess->ticketSeen;
  10411. milli += sess->ticketAdd;
  10412. /* Pre-shared key is mandatory extension for resumption. */
  10413. ret = TLSX_PreSharedKey_Use(&ssl->extensions, sess->ticket,
  10414. sess->ticketLen, milli, ssl->specs.mac_algorithm,
  10415. ssl->options.cipherSuite0, ssl->options.cipherSuite, 1,
  10416. NULL, ssl->heap);
  10417. #else
  10418. milli = now - sess->ticketSeen + sess->ticketAdd;
  10419. /* Pre-shared key is mandatory extension for resumption. */
  10420. ret = TLSX_PreSharedKey_Use(&ssl->extensions, sess->ticket,
  10421. sess->ticketLen, (word32)milli, ssl->specs.mac_algorithm,
  10422. ssl->options.cipherSuite0, ssl->options.cipherSuite, 1,
  10423. NULL, ssl->heap);
  10424. #endif
  10425. if (ret != 0)
  10426. return ret;
  10427. usingPSK = 1;
  10428. }
  10429. #endif
  10430. #ifndef NO_PSK
  10431. #ifndef WOLFSSL_PSK_ONE_ID
  10432. if (ssl->options.client_psk_cs_cb != NULL) {
  10433. int i;
  10434. const Suites* suites = WOLFSSL_SUITES(ssl);
  10435. for (i = 0; i < suites->suiteSz; i += 2) {
  10436. byte cipherSuite0 = suites->suites[i + 0];
  10437. byte cipherSuite = suites->suites[i + 1];
  10438. unsigned int keySz;
  10439. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  10440. int cnt = 0;
  10441. #endif
  10442. #ifdef HAVE_NULL_CIPHER
  10443. if (cipherSuite0 == ECC_BYTE ||
  10444. cipherSuite0 == ECDHE_PSK_BYTE) {
  10445. if (cipherSuite != TLS_SHA256_SHA256 &&
  10446. cipherSuite != TLS_SHA384_SHA384) {
  10447. continue;
  10448. }
  10449. }
  10450. else
  10451. #endif
  10452. #if (defined(WOLFSSL_SM4_GCM) || defined(WOLFSSL_SM4_CCM)) && \
  10453. defined(WOLFSSL_SM3)
  10454. if (cipherSuite0 == CIPHER_BYTE) {
  10455. if ((cipherSuite != TLS_SM4_GCM_SM3) &&
  10456. (cipherSuite != TLS_SM4_CCM_SM3)) {
  10457. continue;
  10458. }
  10459. }
  10460. else
  10461. #endif
  10462. if (cipherSuite0 != TLS13_BYTE)
  10463. continue;
  10464. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  10465. do {
  10466. ssl->arrays->client_identity[0] = cnt;
  10467. #endif
  10468. ssl->arrays->client_identity[MAX_PSK_ID_LEN] = '\0';
  10469. keySz = ssl->options.client_psk_cs_cb(
  10470. ssl, ssl->arrays->server_hint,
  10471. ssl->arrays->client_identity, MAX_PSK_ID_LEN,
  10472. ssl->arrays->psk_key, MAX_PSK_KEY_LEN,
  10473. GetCipherNameInternal(cipherSuite0, cipherSuite));
  10474. if (keySz > 0) {
  10475. ssl->arrays->psk_keySz = keySz;
  10476. ret = TLSX_PreSharedKey_Use(&ssl->extensions,
  10477. (byte*)ssl->arrays->client_identity,
  10478. (word16)XSTRLEN(ssl->arrays->client_identity),
  10479. 0, SuiteMac(WOLFSSL_SUITES(ssl)->suites + i),
  10480. cipherSuite0, cipherSuite, 0, NULL, ssl->heap);
  10481. if (ret != 0)
  10482. return ret;
  10483. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  10484. cnt++;
  10485. #endif
  10486. }
  10487. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  10488. }
  10489. while (keySz > 0);
  10490. #endif
  10491. }
  10492. usingPSK = 1;
  10493. }
  10494. else
  10495. #endif
  10496. if (ssl->options.client_psk_cb != NULL ||
  10497. ssl->options.client_psk_tls13_cb != NULL) {
  10498. /* Default cipher suite. */
  10499. byte cipherSuite0 = TLS13_BYTE;
  10500. byte cipherSuite = WOLFSSL_DEF_PSK_CIPHER;
  10501. int cipherSuiteFlags = WOLFSSL_CIPHER_SUITE_FLAG_NONE;
  10502. const char* cipherName = NULL;
  10503. if (ssl->options.client_psk_tls13_cb != NULL) {
  10504. ssl->arrays->psk_keySz = ssl->options.client_psk_tls13_cb(
  10505. ssl, ssl->arrays->server_hint,
  10506. ssl->arrays->client_identity, MAX_PSK_ID_LEN,
  10507. ssl->arrays->psk_key, MAX_PSK_KEY_LEN, &cipherName);
  10508. if (GetCipherSuiteFromName(cipherName, &cipherSuite0,
  10509. &cipherSuite, &cipherSuiteFlags) != 0) {
  10510. return PSK_KEY_ERROR;
  10511. }
  10512. }
  10513. else {
  10514. ssl->arrays->psk_keySz = ssl->options.client_psk_cb(ssl,
  10515. ssl->arrays->server_hint, ssl->arrays->client_identity,
  10516. MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN);
  10517. }
  10518. #if defined(OPENSSL_EXTRA)
  10519. /* OpenSSL treats 0 as a PSK key length of 0
  10520. * and meaning no PSK available.
  10521. */
  10522. if (ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN) {
  10523. return PSK_KEY_ERROR;
  10524. }
  10525. if (ssl->arrays->psk_keySz > 0) {
  10526. #else
  10527. if (ssl->arrays->psk_keySz == 0 ||
  10528. ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN) {
  10529. return PSK_KEY_ERROR;
  10530. }
  10531. #endif
  10532. ssl->arrays->client_identity[MAX_PSK_ID_LEN] = '\0';
  10533. ssl->options.cipherSuite0 = cipherSuite0;
  10534. ssl->options.cipherSuite = cipherSuite;
  10535. (void)cipherSuiteFlags;
  10536. ret = SetCipherSpecs(ssl);
  10537. if (ret != 0)
  10538. return ret;
  10539. ret = TLSX_PreSharedKey_Use(&ssl->extensions,
  10540. (byte*)ssl->arrays->client_identity,
  10541. (word16)XSTRLEN(ssl->arrays->client_identity),
  10542. 0, ssl->specs.mac_algorithm,
  10543. cipherSuite0, cipherSuite, 0,
  10544. NULL, ssl->heap);
  10545. if (ret != 0)
  10546. return ret;
  10547. usingPSK = 1;
  10548. #if defined(OPENSSL_EXTRA)
  10549. }
  10550. #endif
  10551. }
  10552. #endif /* !NO_PSK */
  10553. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10554. /* Some servers do not generate session tickets unless
  10555. * the extension is seen in a non-resume client hello.
  10556. * We used to send it only if we were otherwise using PSK.
  10557. * Now always send it. Define NO_TLSX_PSKKEM_PLAIN_ANNOUNCE
  10558. * to revert to the old behaviour. */
  10559. #ifdef NO_TLSX_PSKKEM_PLAIN_ANNOUNCE
  10560. if (usingPSK)
  10561. #endif
  10562. {
  10563. byte modes = 0;
  10564. (void)usingPSK;
  10565. /* Pre-shared key modes: mandatory extension for resumption. */
  10566. #ifdef HAVE_SUPPORTED_CURVES
  10567. if (!ssl->options.onlyPskDheKe)
  10568. #endif
  10569. {
  10570. modes = 1 << PSK_KE;
  10571. }
  10572. #if !defined(NO_DH) || defined(HAVE_ECC) || \
  10573. defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  10574. if (!ssl->options.noPskDheKe) {
  10575. modes |= 1 << PSK_DHE_KE;
  10576. }
  10577. #endif
  10578. ret = TLSX_PskKeyModes_Use(ssl, modes);
  10579. if (ret != 0)
  10580. return ret;
  10581. }
  10582. #endif
  10583. #if defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  10584. if (!isServer && ssl->options.postHandshakeAuth) {
  10585. ret = TLSX_PostHandAuth_Use(ssl);
  10586. if (ret != 0)
  10587. return ret;
  10588. }
  10589. #endif
  10590. #if defined(HAVE_ECH)
  10591. /* GREASE ECH */
  10592. if (ssl->echConfigs == NULL) {
  10593. ret = GREASE_ECH_USE(&(ssl->extensions), ssl->heap, ssl->rng);
  10594. }
  10595. else if (ssl->echConfigs != NULL) {
  10596. ret = ECH_USE(ssl->echConfigs, &(ssl->extensions), ssl->heap,
  10597. ssl->rng);
  10598. }
  10599. #endif
  10600. }
  10601. #if defined(HAVE_ECH)
  10602. else if (IsAtLeastTLSv1_3(ssl->version)) {
  10603. if (ssl->ctx->echConfigs != NULL) {
  10604. ret = SERVER_ECH_USE(&(ssl->extensions), ssl->heap,
  10605. ssl->ctx->echConfigs);
  10606. if (ret == 0)
  10607. TLSX_SetResponse(ssl, TLSX_ECH);
  10608. }
  10609. }
  10610. #endif
  10611. #endif
  10612. (void)isServer;
  10613. (void)public_key;
  10614. (void)public_key_len;
  10615. (void)ssl;
  10616. return ret;
  10617. }
  10618. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
  10619. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  10620. /* because the size of ech depends on the size of other extensions we need to
  10621. * get the size with ech special and process ech last, return status */
  10622. static int TLSX_GetSizeWithEch(WOLFSSL* ssl, byte* semaphore, byte msgType,
  10623. word16* pLength)
  10624. {
  10625. int ret = 0;
  10626. TLSX* echX = NULL;
  10627. TLSX* serverNameX = NULL;
  10628. TLSX** extensions = NULL;
  10629. #ifdef WOLFSSL_SMALL_STACK
  10630. char* tmpServerName = NULL;
  10631. #else
  10632. char tmpServerName[MAX_PUBLIC_NAME_SZ];
  10633. #endif
  10634. /* calculate the rest of the extensions length with inner ech */
  10635. if (ssl->extensions)
  10636. echX = TLSX_Find(ssl->extensions, TLSX_ECH);
  10637. if (echX == NULL && ssl->ctx && ssl->ctx->extensions)
  10638. echX = TLSX_Find(ssl->ctx->extensions, TLSX_ECH);
  10639. /* if type is outer change sni to public name */
  10640. if (echX != NULL && ((WOLFSSL_ECH*)echX->data)->type == ECH_TYPE_OUTER) {
  10641. if (ssl->extensions) {
  10642. serverNameX = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
  10643. if (serverNameX != NULL)
  10644. extensions = &ssl->extensions;
  10645. }
  10646. if (serverNameX == NULL && ssl->ctx && ssl->ctx->extensions) {
  10647. serverNameX = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
  10648. extensions = &ssl->ctx->extensions;
  10649. }
  10650. /* store the inner server name */
  10651. if (serverNameX != NULL) {
  10652. char* hostName = ((SNI*)serverNameX->data)->data.host_name;
  10653. word32 hostNameSz = (word32)XSTRLEN(hostName) + 1;
  10654. #ifdef WOLFSSL_SMALL_STACK
  10655. tmpServerName = (char*)XMALLOC(hostNameSz, ssl->heap,
  10656. DYNAMIC_TYPE_TMP_BUFFER);
  10657. if (tmpServerName == NULL)
  10658. return MEMORY_E;
  10659. #else
  10660. /* truncate if too long */
  10661. if (hostNameSz > MAX_PUBLIC_NAME_SZ)
  10662. hostNameSz = MAX_PUBLIC_NAME_SZ;
  10663. #endif
  10664. XMEMCPY(tmpServerName, hostName, hostNameSz);
  10665. }
  10666. /* remove the inner server name */
  10667. TLSX_Remove(extensions, TLSX_SERVER_NAME, ssl->heap);
  10668. ret = TLSX_UseSNI(extensions, WOLFSSL_SNI_HOST_NAME,
  10669. ((WOLFSSL_ECH*)echX->data)->echConfig->publicName,
  10670. XSTRLEN(((WOLFSSL_ECH*)echX->data)->echConfig->publicName),
  10671. ssl->heap);
  10672. /* set the public name as the server name */
  10673. if (ret == WOLFSSL_SUCCESS)
  10674. ret = 0;
  10675. }
  10676. if (ret == 0 && ssl->extensions)
  10677. ret = TLSX_GetSize(ssl->extensions, semaphore, msgType, pLength);
  10678. if (ret == 0 && ssl->ctx && ssl->ctx->extensions)
  10679. ret = TLSX_GetSize(ssl->ctx->extensions, semaphore, msgType, pLength);
  10680. if (serverNameX != NULL) {
  10681. /* remove the public name SNI */
  10682. TLSX_Remove(extensions, TLSX_SERVER_NAME, ssl->heap);
  10683. ret = TLSX_UseSNI(extensions, WOLFSSL_SNI_HOST_NAME,
  10684. tmpServerName, XSTRLEN(tmpServerName), ssl->heap);
  10685. /* restore the inner server name */
  10686. if (ret == WOLFSSL_SUCCESS)
  10687. ret = 0;
  10688. }
  10689. #ifdef WOLFSSL_SMALL_STACK
  10690. XFREE(tmpServerName, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  10691. #endif
  10692. return ret;
  10693. }
  10694. #endif
  10695. /** Tells the buffered size of extensions to be sent into the client hello. */
  10696. int TLSX_GetRequestSize(WOLFSSL* ssl, byte msgType, word16* pLength)
  10697. {
  10698. int ret = 0;
  10699. word16 length = 0;
  10700. byte semaphore[SEMAPHORE_SIZE] = {0};
  10701. if (!TLSX_SupportExtensions(ssl))
  10702. return 0;
  10703. if (msgType == client_hello) {
  10704. EC_VALIDATE_REQUEST(ssl, semaphore);
  10705. PF_VALIDATE_REQUEST(ssl, semaphore);
  10706. WOLF_STK_VALIDATE_REQUEST(ssl);
  10707. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10708. if (WOLFSSL_SUITES(ssl)->hashSigAlgoSz == 0)
  10709. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  10710. #endif
  10711. #if defined(WOLFSSL_TLS13)
  10712. if (!IsAtLeastTLSv1_2(ssl))
  10713. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10714. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  10715. if (!IsAtLeastTLSv1_3(ssl->version)) {
  10716. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10717. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10718. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10719. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PSK_KEY_EXCHANGE_MODES));
  10720. #endif
  10721. #ifdef WOLFSSL_EARLY_DATA
  10722. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  10723. #endif
  10724. #ifdef WOLFSSL_SEND_HRR_COOKIE
  10725. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  10726. #endif
  10727. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  10728. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_POST_HANDSHAKE_AUTH));
  10729. #endif
  10730. }
  10731. #endif
  10732. #endif /* WOLFSSL_TLS13 */
  10733. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  10734. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  10735. if (!SSL_CM(ssl)->ocspStaplingEnabled) {
  10736. /* mark already sent, so it won't send it */
  10737. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10738. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  10739. }
  10740. #endif
  10741. }
  10742. #ifdef WOLFSSL_TLS13
  10743. #ifndef NO_CERTS
  10744. else if (msgType == certificate_request) {
  10745. /* Don't send out any extension except those that are turned off. */
  10746. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10747. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10748. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  10749. #endif
  10750. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  10751. * TLSX_CERTIFICATE_AUTHORITIES, OID_FILTERS
  10752. * TLSX_STATUS_REQUEST
  10753. */
  10754. }
  10755. #endif
  10756. #if defined(HAVE_ECH)
  10757. if (ssl->options.useEch == 1 && msgType == client_hello) {
  10758. ret = TLSX_GetSizeWithEch(ssl, semaphore, msgType, &length);
  10759. if (ret != 0)
  10760. return ret;
  10761. }
  10762. else
  10763. #endif /* HAVE_ECH */
  10764. #endif /* WOLFSSL_TLS13 */
  10765. {
  10766. if (ssl->extensions) {
  10767. ret = TLSX_GetSize(ssl->extensions, semaphore, msgType, &length);
  10768. if (ret != 0)
  10769. return ret;
  10770. }
  10771. if (ssl->ctx && ssl->ctx->extensions) {
  10772. ret = TLSX_GetSize(ssl->ctx->extensions, semaphore, msgType,
  10773. &length);
  10774. if (ret != 0)
  10775. return ret;
  10776. }
  10777. }
  10778. #ifdef HAVE_EXTENDED_MASTER
  10779. if (msgType == client_hello && ssl->options.haveEMS &&
  10780. (!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade)) {
  10781. length += HELLO_EXT_SZ;
  10782. }
  10783. #endif
  10784. if (length)
  10785. length += OPAQUE16_LEN; /* for total length storage. */
  10786. *pLength += length;
  10787. return ret;
  10788. }
  10789. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  10790. /* return status after writing the extensions with ech written last */
  10791. static int TLSX_WriteWithEch(WOLFSSL* ssl, byte* output, byte* semaphore,
  10792. byte msgType, word16* pOffset)
  10793. {
  10794. int ret = 0;
  10795. TLSX* echX = NULL;
  10796. TLSX* serverNameX = NULL;
  10797. TLSX** extensions = NULL;
  10798. #ifdef WOLFSSL_SMALL_STACK
  10799. char* tmpServerName = NULL;
  10800. #else
  10801. char tmpServerName[MAX_PUBLIC_NAME_SZ];
  10802. #endif
  10803. /* get the echX from either extensions or ctx */
  10804. if (ssl->extensions)
  10805. echX = TLSX_Find(ssl->extensions, TLSX_ECH);
  10806. if (echX == NULL && ssl->ctx && ssl->ctx->extensions) {
  10807. /* if not NULL the semaphore will stop it from being counted */
  10808. if (echX == NULL)
  10809. echX = TLSX_Find(ssl->ctx->extensions, TLSX_ECH);
  10810. }
  10811. /* if type is outer change sni to public name */
  10812. if (echX != NULL && ((WOLFSSL_ECH*)echX->data)->type == ECH_TYPE_OUTER) {
  10813. if (ssl->extensions) {
  10814. serverNameX = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
  10815. if (serverNameX != NULL)
  10816. extensions = &ssl->extensions;
  10817. }
  10818. if (serverNameX == NULL && ssl->ctx && ssl->ctx->extensions) {
  10819. serverNameX = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
  10820. extensions = &ssl->ctx->extensions;
  10821. }
  10822. /* store the inner server name */
  10823. if (serverNameX != NULL) {
  10824. char* hostName = ((SNI*)serverNameX->data)->data.host_name;
  10825. word32 hostNameSz = (word32)XSTRLEN(hostName) + 1;
  10826. #ifdef WOLFSSL_SMALL_STACK
  10827. tmpServerName = (char*)XMALLOC(hostNameSz, ssl->heap,
  10828. DYNAMIC_TYPE_TMP_BUFFER);
  10829. if (tmpServerName == NULL)
  10830. return MEMORY_E;
  10831. #else
  10832. /* truncate if too long */
  10833. if (hostNameSz > MAX_PUBLIC_NAME_SZ)
  10834. hostNameSz = MAX_PUBLIC_NAME_SZ;
  10835. #endif
  10836. XMEMCPY(tmpServerName, hostName, hostNameSz);
  10837. }
  10838. /* remove the inner server name */
  10839. TLSX_Remove(extensions, TLSX_SERVER_NAME, ssl->heap);
  10840. ret = TLSX_UseSNI(extensions, WOLFSSL_SNI_HOST_NAME,
  10841. ((WOLFSSL_ECH*)echX->data)->echConfig->publicName,
  10842. XSTRLEN(((WOLFSSL_ECH*)echX->data)->echConfig->publicName),
  10843. ssl->heap);
  10844. /* set the public name as the server name */
  10845. if (ret == WOLFSSL_SUCCESS)
  10846. ret = 0;
  10847. }
  10848. if (echX != NULL) {
  10849. /* turn ech on so it doesn't write, then write it last */
  10850. TURN_ON(semaphore, TLSX_ToSemaphore(echX->type));
  10851. }
  10852. if (ret == 0 && ssl->extensions) {
  10853. ret = TLSX_Write(ssl->extensions, output + *pOffset, semaphore,
  10854. msgType, pOffset);
  10855. }
  10856. if (ret == 0 && ssl->ctx && ssl->ctx->extensions) {
  10857. ret = TLSX_Write(ssl->ctx->extensions, output + *pOffset, semaphore,
  10858. msgType, pOffset);
  10859. }
  10860. if (echX != NULL) {
  10861. /* turn off and write it last */
  10862. TURN_OFF(semaphore, TLSX_ToSemaphore(echX->type));
  10863. }
  10864. if (ret == 0 && ssl->extensions) {
  10865. ret = TLSX_Write(ssl->extensions, output + *pOffset, semaphore,
  10866. msgType, pOffset);
  10867. }
  10868. if (ret == 0 && ssl->ctx && ssl->ctx->extensions) {
  10869. ret = TLSX_Write(ssl->ctx->extensions, output + *pOffset, semaphore,
  10870. msgType, pOffset);
  10871. }
  10872. if (serverNameX != NULL) {
  10873. /* remove the public name SNI */
  10874. TLSX_Remove(extensions, TLSX_SERVER_NAME, ssl->heap);
  10875. ret = TLSX_UseSNI(extensions, WOLFSSL_SNI_HOST_NAME, tmpServerName,
  10876. XSTRLEN(tmpServerName), ssl->heap);
  10877. /* restore the inner server name */
  10878. if (ret == WOLFSSL_SUCCESS)
  10879. ret = 0;
  10880. }
  10881. #ifdef WOLFSSL_SMALL_STACK
  10882. XFREE(tmpServerName, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  10883. #endif
  10884. return ret;
  10885. }
  10886. #endif
  10887. /** Writes the extensions to be sent into the client hello. */
  10888. int TLSX_WriteRequest(WOLFSSL* ssl, byte* output, byte msgType, word16* pOffset)
  10889. {
  10890. int ret = 0;
  10891. word16 offset = 0;
  10892. byte semaphore[SEMAPHORE_SIZE] = {0};
  10893. if (!TLSX_SupportExtensions(ssl) || output == NULL)
  10894. return 0;
  10895. offset += OPAQUE16_LEN; /* extensions length */
  10896. if (msgType == client_hello) {
  10897. EC_VALIDATE_REQUEST(ssl, semaphore);
  10898. PF_VALIDATE_REQUEST(ssl, semaphore);
  10899. WOLF_STK_VALIDATE_REQUEST(ssl);
  10900. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10901. if (WOLFSSL_SUITES(ssl)->hashSigAlgoSz == 0)
  10902. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  10903. #endif
  10904. #ifdef WOLFSSL_TLS13
  10905. if (!IsAtLeastTLSv1_2(ssl)) {
  10906. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10907. }
  10908. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  10909. if (!IsAtLeastTLSv1_3(ssl->version)) {
  10910. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10911. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10912. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PSK_KEY_EXCHANGE_MODES));
  10913. #endif
  10914. #ifdef WOLFSSL_EARLY_DATA
  10915. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  10916. #endif
  10917. #ifdef WOLFSSL_SEND_HRR_COOKIE
  10918. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  10919. #endif
  10920. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  10921. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_POST_HANDSHAKE_AUTH));
  10922. #endif
  10923. }
  10924. #endif
  10925. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10926. /* Must write Pre-shared Key extension at the end in TLS v1.3.
  10927. * Must not write out Pre-shared Key extension in earlier versions of
  10928. * protocol.
  10929. */
  10930. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10931. #endif
  10932. #endif /* WOLFSSL_TLS13 */
  10933. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  10934. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  10935. /* mark already sent, so it won't send it */
  10936. if (!SSL_CM(ssl)->ocspStaplingEnabled) {
  10937. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10938. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  10939. }
  10940. #endif
  10941. }
  10942. #ifdef WOLFSSL_TLS13
  10943. #ifndef NO_CERTS
  10944. else if (msgType == certificate_request) {
  10945. /* Don't send out any extension except those that are turned off. */
  10946. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10947. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10948. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  10949. #endif
  10950. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  10951. * TLSX_CERTIFICATE_AUTHORITIES, TLSX_OID_FILTERS
  10952. * TLSX_STATUS_REQUEST
  10953. */
  10954. }
  10955. #endif
  10956. #endif
  10957. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  10958. if (ssl->options.useEch == 1 && msgType == client_hello) {
  10959. ret = TLSX_WriteWithEch(ssl, output, semaphore,
  10960. msgType, &offset);
  10961. if (ret != 0)
  10962. return ret;
  10963. }
  10964. else
  10965. #endif
  10966. {
  10967. if (ssl->extensions) {
  10968. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  10969. msgType, &offset);
  10970. if (ret != 0)
  10971. return ret;
  10972. }
  10973. if (ssl->ctx && ssl->ctx->extensions) {
  10974. ret = TLSX_Write(ssl->ctx->extensions, output + offset, semaphore,
  10975. msgType, &offset);
  10976. if (ret != 0)
  10977. return ret;
  10978. }
  10979. }
  10980. #ifdef HAVE_EXTENDED_MASTER
  10981. if (msgType == client_hello && ssl->options.haveEMS &&
  10982. (!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade)) {
  10983. WOLFSSL_MSG("EMS extension to write");
  10984. c16toa(HELLO_EXT_EXTMS, output + offset);
  10985. offset += HELLO_EXT_TYPE_SZ;
  10986. c16toa(0, output + offset);
  10987. offset += HELLO_EXT_SZ_SZ;
  10988. }
  10989. #endif
  10990. #ifdef WOLFSSL_TLS13
  10991. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10992. if (msgType == client_hello && IsAtLeastTLSv1_3(ssl->version)) {
  10993. /* Write out what we can of Pre-shared key extension. */
  10994. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10995. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  10996. client_hello, &offset);
  10997. if (ret != 0)
  10998. return ret;
  10999. }
  11000. #endif
  11001. #endif
  11002. if (offset > OPAQUE16_LEN || msgType != client_hello)
  11003. c16toa(offset - OPAQUE16_LEN, output); /* extensions length */
  11004. *pOffset += offset;
  11005. return ret;
  11006. }
  11007. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
  11008. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_SERVER)
  11009. /** Tells the buffered size of extensions to be sent into the server hello. */
  11010. int TLSX_GetResponseSize(WOLFSSL* ssl, byte msgType, word16* pLength)
  11011. {
  11012. int ret = 0;
  11013. word16 length = 0;
  11014. byte semaphore[SEMAPHORE_SIZE] = {0};
  11015. switch (msgType) {
  11016. #ifndef NO_WOLFSSL_SERVER
  11017. case server_hello:
  11018. PF_VALIDATE_RESPONSE(ssl, semaphore);
  11019. #ifdef WOLFSSL_TLS13
  11020. if (IsAtLeastTLSv1_3(ssl->version)) {
  11021. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  11022. TURN_OFF(semaphore,
  11023. TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  11024. #if defined(HAVE_SUPPORTED_CURVES)
  11025. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11026. if (!ssl->options.noPskDheKe)
  11027. #endif
  11028. {
  11029. /* Expect KeyShare extension in ServerHello. */
  11030. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  11031. }
  11032. #endif
  11033. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11034. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  11035. #endif
  11036. #ifdef WOLFSSL_DTLS_CID
  11037. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  11038. #endif
  11039. }
  11040. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  11041. else {
  11042. #ifdef HAVE_SUPPORTED_CURVES
  11043. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  11044. #endif
  11045. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11046. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  11047. #endif
  11048. }
  11049. #endif
  11050. #endif /* WOLFSSL_TLS13 */
  11051. break;
  11052. #ifdef WOLFSSL_TLS13
  11053. case hello_retry_request:
  11054. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  11055. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  11056. #ifdef HAVE_SUPPORTED_CURVES
  11057. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11058. if (!ssl->options.noPskDheKe)
  11059. #endif
  11060. {
  11061. /* Expect KeyShare extension in HelloRetryRequest. */
  11062. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  11063. }
  11064. #endif
  11065. #ifdef WOLFSSL_SEND_HRR_COOKIE
  11066. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  11067. #endif
  11068. break;
  11069. #endif
  11070. #ifdef WOLFSSL_TLS13
  11071. case encrypted_extensions:
  11072. /* Send out all extension except those that are turned on. */
  11073. #ifdef HAVE_ECC
  11074. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  11075. #endif
  11076. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  11077. #ifdef HAVE_SESSION_TICKET
  11078. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SESSION_TICKET));
  11079. #endif
  11080. #ifdef HAVE_SUPPORTED_CURVES
  11081. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  11082. #endif
  11083. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11084. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  11085. #endif
  11086. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  11087. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  11088. #endif
  11089. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  11090. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  11091. #endif
  11092. #if defined(HAVE_SERVER_RENEGOTIATION_INFO)
  11093. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_RENEGOTIATION_INFO));
  11094. #endif
  11095. #ifdef WOLFSSL_DTLS_CID
  11096. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  11097. #endif /* WOLFSSL_DTLS_CID */
  11098. break;
  11099. #ifdef WOLFSSL_EARLY_DATA
  11100. case session_ticket:
  11101. if (ssl->options.tls1_3) {
  11102. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  11103. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  11104. }
  11105. break;
  11106. #endif
  11107. #endif
  11108. #endif
  11109. #ifdef WOLFSSL_TLS13
  11110. #ifndef NO_CERTS
  11111. case certificate:
  11112. /* Don't send out any extension except those that are turned off. */
  11113. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  11114. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  11115. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  11116. * TLSX_SERVER_CERTIFICATE_TYPE
  11117. */
  11118. break;
  11119. #endif
  11120. #endif
  11121. }
  11122. #ifdef HAVE_EXTENDED_MASTER
  11123. if (ssl->options.haveEMS && msgType == server_hello &&
  11124. !IsAtLeastTLSv1_3(ssl->version)) {
  11125. length += HELLO_EXT_SZ;
  11126. }
  11127. #endif
  11128. if (TLSX_SupportExtensions(ssl)) {
  11129. ret = TLSX_GetSize(ssl->extensions, semaphore, msgType, &length);
  11130. if (ret != 0)
  11131. return ret;
  11132. }
  11133. /* All the response data is set at the ssl object only, so no ctx here. */
  11134. if (length || msgType != server_hello)
  11135. length += OPAQUE16_LEN; /* for total length storage. */
  11136. *pLength += length;
  11137. return ret;
  11138. }
  11139. /** Writes the server hello extensions into a buffer. */
  11140. int TLSX_WriteResponse(WOLFSSL *ssl, byte* output, byte msgType, word16* pOffset)
  11141. {
  11142. int ret = 0;
  11143. word16 offset = 0;
  11144. if (TLSX_SupportExtensions(ssl) && output) {
  11145. byte semaphore[SEMAPHORE_SIZE] = {0};
  11146. switch (msgType) {
  11147. #ifndef NO_WOLFSSL_SERVER
  11148. case server_hello:
  11149. PF_VALIDATE_RESPONSE(ssl, semaphore);
  11150. #ifdef WOLFSSL_TLS13
  11151. if (IsAtLeastTLSv1_3(ssl->version)) {
  11152. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  11153. TURN_OFF(semaphore,
  11154. TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  11155. #ifdef HAVE_SUPPORTED_CURVES
  11156. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11157. if (!ssl->options.noPskDheKe)
  11158. #endif
  11159. {
  11160. /* Write out KeyShare in ServerHello. */
  11161. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  11162. }
  11163. #endif
  11164. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11165. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  11166. #endif
  11167. #ifdef WOLFSSL_DTLS_CID
  11168. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  11169. #endif /* WOLFSSL_DTLS_CID */
  11170. }
  11171. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  11172. else {
  11173. #ifdef HAVE_SUPPORTED_CURVES
  11174. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  11175. #endif
  11176. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11177. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  11178. #endif
  11179. }
  11180. #endif
  11181. #endif
  11182. break;
  11183. #ifdef WOLFSSL_TLS13
  11184. case hello_retry_request:
  11185. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  11186. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  11187. #ifdef HAVE_SUPPORTED_CURVES
  11188. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11189. if (!ssl->options.noPskDheKe)
  11190. #endif
  11191. {
  11192. /* Write out KeyShare in HelloRetryRequest. */
  11193. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  11194. }
  11195. #endif
  11196. /* Cookie is written below as last extension. */
  11197. break;
  11198. #endif
  11199. #ifdef WOLFSSL_TLS13
  11200. case encrypted_extensions:
  11201. /* Send out all extension except those that are turned on. */
  11202. #ifdef HAVE_ECC
  11203. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  11204. #endif
  11205. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  11206. #ifdef HAVE_SESSION_TICKET
  11207. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SESSION_TICKET));
  11208. #endif
  11209. #ifdef HAVE_SUPPORTED_CURVES
  11210. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  11211. #endif
  11212. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11213. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  11214. #endif
  11215. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  11216. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  11217. #endif
  11218. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  11219. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  11220. #endif
  11221. #if defined(HAVE_SERVER_RENEGOTIATION_INFO)
  11222. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_RENEGOTIATION_INFO));
  11223. #endif
  11224. #ifdef WOLFSSL_DTLS_CID
  11225. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  11226. #endif /* WOLFSSL_DTLS_CID */
  11227. break;
  11228. #ifdef WOLFSSL_EARLY_DATA
  11229. case session_ticket:
  11230. if (ssl->options.tls1_3) {
  11231. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  11232. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  11233. }
  11234. break;
  11235. #endif
  11236. #endif
  11237. #endif
  11238. #ifdef WOLFSSL_TLS13
  11239. #ifndef NO_CERTS
  11240. case certificate:
  11241. /* Don't send out any extension except those that are turned
  11242. * off. */
  11243. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  11244. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  11245. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  11246. * TLSX_SERVER_CERTIFICATE_TYPE
  11247. */
  11248. break;
  11249. #endif
  11250. #endif
  11251. default:
  11252. break;
  11253. }
  11254. offset += OPAQUE16_LEN; /* extensions length */
  11255. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  11256. msgType, &offset);
  11257. if (ret != 0)
  11258. return ret;
  11259. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  11260. if (msgType == hello_retry_request) {
  11261. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  11262. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  11263. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  11264. msgType, &offset);
  11265. if (ret != 0)
  11266. return ret;
  11267. }
  11268. #endif
  11269. #ifdef HAVE_EXTENDED_MASTER
  11270. if (ssl->options.haveEMS && msgType == server_hello &&
  11271. !IsAtLeastTLSv1_3(ssl->version)) {
  11272. WOLFSSL_MSG("EMS extension to write");
  11273. c16toa(HELLO_EXT_EXTMS, output + offset);
  11274. offset += HELLO_EXT_TYPE_SZ;
  11275. c16toa(0, output + offset);
  11276. offset += HELLO_EXT_SZ_SZ;
  11277. }
  11278. #endif
  11279. if (offset > OPAQUE16_LEN || msgType != server_hello)
  11280. c16toa(offset - OPAQUE16_LEN, output); /* extensions length */
  11281. }
  11282. if (pOffset)
  11283. *pOffset += offset;
  11284. return ret;
  11285. }
  11286. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_SERVER */
  11287. #ifdef WOLFSSL_TLS13
  11288. int TLSX_ParseVersion(WOLFSSL* ssl, const byte* input, word16 length,
  11289. byte msgType, int* found)
  11290. {
  11291. int ret = 0;
  11292. int offset = 0;
  11293. *found = 0;
  11294. while (offset < (int)length) {
  11295. word16 type;
  11296. word16 size;
  11297. if (offset + (2 * OPAQUE16_LEN) > length) {
  11298. ret = BUFFER_ERROR;
  11299. break;
  11300. }
  11301. ato16(input + offset, &type);
  11302. offset += HELLO_EXT_TYPE_SZ;
  11303. ato16(input + offset, &size);
  11304. offset += OPAQUE16_LEN;
  11305. if (offset + size > length) {
  11306. ret = BUFFER_ERROR;
  11307. break;
  11308. }
  11309. if (type == TLSX_SUPPORTED_VERSIONS) {
  11310. *found = 1;
  11311. WOLFSSL_MSG("Supported Versions extension received");
  11312. ret = SV_PARSE(ssl, input + offset, size, msgType, &ssl->version,
  11313. &ssl->options, &ssl->extensions);
  11314. break;
  11315. }
  11316. offset += size;
  11317. }
  11318. return ret;
  11319. }
  11320. #endif
  11321. /** Parses a buffer of TLS extensions. */
  11322. int TLSX_Parse(WOLFSSL* ssl, const byte* input, word16 length, byte msgType,
  11323. Suites *suites)
  11324. {
  11325. int ret = 0;
  11326. word16 offset = 0;
  11327. byte isRequest = (msgType == client_hello ||
  11328. msgType == certificate_request);
  11329. #ifdef HAVE_EXTENDED_MASTER
  11330. byte pendingEMS = 0;
  11331. #endif
  11332. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  11333. int pskDone = 0;
  11334. #endif
  11335. byte seenType[SEMAPHORE_SIZE]; /* Seen known extensions. */
  11336. if (!ssl || !input || (isRequest && !suites))
  11337. return BAD_FUNC_ARG;
  11338. /* No known extensions seen yet. */
  11339. XMEMSET(seenType, 0, sizeof(seenType));
  11340. while (ret == 0 && offset < length) {
  11341. word16 type;
  11342. word16 size;
  11343. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  11344. if (msgType == client_hello && pskDone) {
  11345. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  11346. return PSK_KEY_ERROR;
  11347. }
  11348. #endif
  11349. if (length - offset < HELLO_EXT_TYPE_SZ + OPAQUE16_LEN)
  11350. return BUFFER_ERROR;
  11351. ato16(input + offset, &type);
  11352. offset += HELLO_EXT_TYPE_SZ;
  11353. ato16(input + offset, &size);
  11354. offset += OPAQUE16_LEN;
  11355. /* Check we have a bit for extension type. */
  11356. if ((type <= 62) || (type == TLSX_RENEGOTIATION_INFO)
  11357. #ifdef WOLFSSL_QUIC
  11358. || (type == TLSX_KEY_QUIC_TP_PARAMS_DRAFT)
  11359. #endif
  11360. )
  11361. {
  11362. /* Detect duplicate recognized extensions. */
  11363. if (IS_OFF(seenType, TLSX_ToSemaphore(type))) {
  11364. TURN_ON(seenType, TLSX_ToSemaphore(type));
  11365. }
  11366. else {
  11367. return DUPLICATE_TLS_EXT_E;
  11368. }
  11369. }
  11370. if (length - offset < size)
  11371. return BUFFER_ERROR;
  11372. switch (type) {
  11373. #ifdef HAVE_SNI
  11374. case TLSX_SERVER_NAME:
  11375. WOLFSSL_MSG("SNI extension received");
  11376. #ifdef WOLFSSL_DEBUG_TLS
  11377. WOLFSSL_BUFFER(input + offset, size);
  11378. #endif
  11379. #ifdef WOLFSSL_TLS13
  11380. if (IsAtLeastTLSv1_3(ssl->version)) {
  11381. if (msgType != client_hello &&
  11382. msgType != encrypted_extensions)
  11383. return EXT_NOT_ALLOWED;
  11384. }
  11385. else
  11386. #endif
  11387. {
  11388. if (msgType != client_hello &&
  11389. msgType != server_hello)
  11390. return EXT_NOT_ALLOWED;
  11391. }
  11392. ret = SNI_PARSE(ssl, input + offset, size, isRequest);
  11393. break;
  11394. #endif
  11395. case TLSX_TRUSTED_CA_KEYS:
  11396. WOLFSSL_MSG("Trusted CA extension received");
  11397. #ifdef WOLFSSL_DEBUG_TLS
  11398. WOLFSSL_BUFFER(input + offset, size);
  11399. #endif
  11400. #ifdef WOLFSSL_TLS13
  11401. /* RFC 8446 4.2.4 states trusted_ca_keys is not used
  11402. in TLS 1.3. */
  11403. if (IsAtLeastTLSv1_3(ssl->version)) {
  11404. return EXT_NOT_ALLOWED;
  11405. }
  11406. else
  11407. #endif
  11408. {
  11409. if (msgType != client_hello &&
  11410. msgType != server_hello)
  11411. return EXT_NOT_ALLOWED;
  11412. }
  11413. ret = TCA_PARSE(ssl, input + offset, size, isRequest);
  11414. break;
  11415. case TLSX_MAX_FRAGMENT_LENGTH:
  11416. WOLFSSL_MSG("Max Fragment Length extension received");
  11417. #ifdef WOLFSSL_DEBUG_TLS
  11418. WOLFSSL_BUFFER(input + offset, size);
  11419. #endif
  11420. #ifdef WOLFSSL_TLS13
  11421. if (IsAtLeastTLSv1_3(ssl->version)) {
  11422. if (msgType != client_hello &&
  11423. msgType != encrypted_extensions) {
  11424. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11425. return EXT_NOT_ALLOWED;
  11426. }
  11427. }
  11428. else
  11429. #endif
  11430. {
  11431. if (msgType != client_hello &&
  11432. msgType != server_hello) {
  11433. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11434. return EXT_NOT_ALLOWED;
  11435. }
  11436. }
  11437. ret = MFL_PARSE(ssl, input + offset, size, isRequest);
  11438. break;
  11439. case TLSX_TRUNCATED_HMAC:
  11440. WOLFSSL_MSG("Truncated HMAC extension received");
  11441. #ifdef WOLFSSL_DEBUG_TLS
  11442. WOLFSSL_BUFFER(input + offset, size);
  11443. #endif
  11444. #ifdef WOLFSSL_TLS13
  11445. if (IsAtLeastTLSv1_3(ssl->version))
  11446. break;
  11447. #endif
  11448. if (msgType != client_hello)
  11449. return EXT_NOT_ALLOWED;
  11450. ret = THM_PARSE(ssl, input + offset, size, isRequest);
  11451. break;
  11452. case TLSX_SUPPORTED_GROUPS:
  11453. WOLFSSL_MSG("Supported Groups extension received");
  11454. #ifdef WOLFSSL_DEBUG_TLS
  11455. WOLFSSL_BUFFER(input + offset, size);
  11456. #endif
  11457. #ifdef WOLFSSL_TLS13
  11458. if (IsAtLeastTLSv1_3(ssl->version)) {
  11459. if (msgType != client_hello &&
  11460. msgType != encrypted_extensions) {
  11461. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11462. return EXT_NOT_ALLOWED;
  11463. }
  11464. }
  11465. else
  11466. #endif
  11467. {
  11468. if (msgType != client_hello) {
  11469. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11470. return EXT_NOT_ALLOWED;
  11471. }
  11472. }
  11473. ret = EC_PARSE(ssl, input + offset, size, isRequest,
  11474. &ssl->extensions);
  11475. break;
  11476. case TLSX_EC_POINT_FORMATS:
  11477. WOLFSSL_MSG("Point Formats extension received");
  11478. #ifdef WOLFSSL_DEBUG_TLS
  11479. WOLFSSL_BUFFER(input + offset, size);
  11480. #endif
  11481. #ifdef WOLFSSL_TLS13
  11482. if (IsAtLeastTLSv1_3(ssl->version))
  11483. break;
  11484. #endif
  11485. if (msgType != client_hello &&
  11486. msgType != server_hello) {
  11487. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11488. return EXT_NOT_ALLOWED;
  11489. }
  11490. ret = PF_PARSE(ssl, input + offset, size, isRequest);
  11491. break;
  11492. case TLSX_STATUS_REQUEST:
  11493. WOLFSSL_MSG("Certificate Status Request extension received");
  11494. #ifdef WOLFSSL_DEBUG_TLS
  11495. WOLFSSL_BUFFER(input + offset, size);
  11496. #endif
  11497. #ifdef WOLFSSL_TLS13
  11498. if (IsAtLeastTLSv1_3(ssl->version)) {
  11499. if (msgType != client_hello &&
  11500. msgType != certificate_request &&
  11501. msgType != certificate)
  11502. return EXT_NOT_ALLOWED;
  11503. }
  11504. else
  11505. #endif
  11506. {
  11507. if (msgType != client_hello &&
  11508. msgType != server_hello)
  11509. return EXT_NOT_ALLOWED;
  11510. }
  11511. ret = CSR_PARSE(ssl, input + offset, size, isRequest);
  11512. break;
  11513. case TLSX_STATUS_REQUEST_V2:
  11514. WOLFSSL_MSG("Certificate Status Request v2 extension received");
  11515. #ifdef WOLFSSL_DEBUG_TLS
  11516. WOLFSSL_BUFFER(input + offset, size);
  11517. #endif
  11518. #if defined(WOLFSSL_TLS13) && defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  11519. if (IsAtLeastTLSv1_3(ssl->version)) {
  11520. if (msgType != client_hello &&
  11521. msgType != certificate_request &&
  11522. msgType != certificate)
  11523. return EXT_NOT_ALLOWED;
  11524. }
  11525. else
  11526. #endif
  11527. {
  11528. if (msgType != client_hello &&
  11529. msgType != server_hello)
  11530. return EXT_NOT_ALLOWED;
  11531. }
  11532. ret = CSR2_PARSE(ssl, input + offset, size, isRequest);
  11533. break;
  11534. #ifdef HAVE_EXTENDED_MASTER
  11535. case HELLO_EXT_EXTMS:
  11536. WOLFSSL_MSG("Extended Master Secret extension received");
  11537. #ifdef WOLFSSL_DEBUG_TLS
  11538. WOLFSSL_BUFFER(input + offset, size);
  11539. #endif
  11540. #if defined(WOLFSSL_TLS13)
  11541. if (IsAtLeastTLSv1_3(ssl->version))
  11542. break;
  11543. #endif
  11544. if (msgType != client_hello &&
  11545. msgType != server_hello)
  11546. return EXT_NOT_ALLOWED;
  11547. if (size != 0)
  11548. return BUFFER_ERROR;
  11549. #ifndef NO_WOLFSSL_SERVER
  11550. if (isRequest)
  11551. ssl->options.haveEMS = 1;
  11552. #endif
  11553. pendingEMS = 1;
  11554. break;
  11555. #endif
  11556. case TLSX_RENEGOTIATION_INFO:
  11557. WOLFSSL_MSG("Secure Renegotiation extension received");
  11558. #ifdef WOLFSSL_DEBUG_TLS
  11559. WOLFSSL_BUFFER(input + offset, size);
  11560. #endif
  11561. #ifdef WOLFSSL_TLS13
  11562. if (IsAtLeastTLSv1_3(ssl->version))
  11563. break;
  11564. #endif
  11565. if (msgType != client_hello &&
  11566. msgType != server_hello)
  11567. return EXT_NOT_ALLOWED;
  11568. ret = SCR_PARSE(ssl, input + offset, size, isRequest);
  11569. break;
  11570. case TLSX_SESSION_TICKET:
  11571. WOLFSSL_MSG("Session Ticket extension received");
  11572. #ifdef WOLFSSL_DEBUG_TLS
  11573. WOLFSSL_BUFFER(input + offset, size);
  11574. #endif
  11575. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)
  11576. if (IsAtLeastTLSv1_3(ssl->version)) {
  11577. if (msgType != client_hello)
  11578. return EXT_NOT_ALLOWED;
  11579. }
  11580. else
  11581. #endif
  11582. {
  11583. if (msgType != client_hello &&
  11584. msgType != server_hello)
  11585. return EXT_NOT_ALLOWED;
  11586. }
  11587. ret = WOLF_STK_PARSE(ssl, input + offset, size, isRequest);
  11588. break;
  11589. case TLSX_APPLICATION_LAYER_PROTOCOL:
  11590. WOLFSSL_MSG("ALPN extension received");
  11591. #ifdef WOLFSSL_DEBUG_TLS
  11592. WOLFSSL_BUFFER(input + offset, size);
  11593. #endif
  11594. #if defined(WOLFSSL_TLS13) && defined(HAVE_ALPN)
  11595. if (IsAtLeastTLSv1_3(ssl->version)) {
  11596. if (msgType != client_hello &&
  11597. msgType != encrypted_extensions)
  11598. return EXT_NOT_ALLOWED;
  11599. }
  11600. else
  11601. #endif
  11602. {
  11603. if (msgType != client_hello &&
  11604. msgType != server_hello)
  11605. return EXT_NOT_ALLOWED;
  11606. }
  11607. ret = ALPN_PARSE(ssl, input + offset, size, isRequest);
  11608. break;
  11609. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  11610. case TLSX_SIGNATURE_ALGORITHMS:
  11611. WOLFSSL_MSG("Signature Algorithms extension received");
  11612. #ifdef WOLFSSL_DEBUG_TLS
  11613. WOLFSSL_BUFFER(input + offset, size);
  11614. #endif
  11615. if (!IsAtLeastTLSv1_2(ssl))
  11616. break;
  11617. #ifdef WOLFSSL_TLS13
  11618. if (IsAtLeastTLSv1_3(ssl->version)) {
  11619. if (msgType != client_hello &&
  11620. msgType != certificate_request)
  11621. return EXT_NOT_ALLOWED;
  11622. }
  11623. else
  11624. #endif
  11625. {
  11626. if (msgType != client_hello)
  11627. return EXT_NOT_ALLOWED;
  11628. }
  11629. ret = SA_PARSE(ssl, input + offset, size, isRequest, suites);
  11630. break;
  11631. #endif
  11632. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  11633. case TLSX_ENCRYPT_THEN_MAC:
  11634. WOLFSSL_MSG("Encrypt-Then-Mac extension received");
  11635. /* Ignore for TLS 1.3+ */
  11636. if (IsAtLeastTLSv1_3(ssl->version))
  11637. break;
  11638. if (msgType != client_hello &&
  11639. msgType != server_hello)
  11640. return EXT_NOT_ALLOWED;
  11641. ret = ETM_PARSE(ssl, input + offset, size, msgType);
  11642. break;
  11643. #endif /* HAVE_ENCRYPT_THEN_MAC */
  11644. #ifdef WOLFSSL_TLS13
  11645. case TLSX_SUPPORTED_VERSIONS:
  11646. WOLFSSL_MSG("Skipping Supported Versions - already processed");
  11647. #ifdef WOLFSSL_DEBUG_TLS
  11648. WOLFSSL_BUFFER(input + offset, size);
  11649. #endif
  11650. if (msgType != client_hello &&
  11651. msgType != server_hello &&
  11652. msgType != hello_retry_request)
  11653. return EXT_NOT_ALLOWED;
  11654. break;
  11655. #ifdef WOLFSSL_SEND_HRR_COOKIE
  11656. case TLSX_COOKIE:
  11657. WOLFSSL_MSG("Cookie extension received");
  11658. #ifdef WOLFSSL_DEBUG_TLS
  11659. WOLFSSL_BUFFER(input + offset, size);
  11660. #endif
  11661. if (!IsAtLeastTLSv1_3(ssl->version))
  11662. break;
  11663. if (msgType != client_hello &&
  11664. msgType != hello_retry_request) {
  11665. return EXT_NOT_ALLOWED;
  11666. }
  11667. ret = CKE_PARSE(ssl, input + offset, size, msgType);
  11668. break;
  11669. #endif
  11670. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11671. case TLSX_PRE_SHARED_KEY:
  11672. WOLFSSL_MSG("Pre-Shared Key extension received");
  11673. #ifdef WOLFSSL_DEBUG_TLS
  11674. WOLFSSL_BUFFER(input + offset, size);
  11675. #endif
  11676. if (!IsAtLeastTLSv1_3(ssl->version))
  11677. break;
  11678. if (msgType != client_hello &&
  11679. msgType != server_hello) {
  11680. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11681. return EXT_NOT_ALLOWED;
  11682. }
  11683. ret = PSK_PARSE(ssl, input + offset, size, msgType);
  11684. pskDone = 1;
  11685. break;
  11686. case TLSX_PSK_KEY_EXCHANGE_MODES:
  11687. WOLFSSL_MSG("PSK Key Exchange Modes extension received");
  11688. #ifdef WOLFSSL_DEBUG_TLS
  11689. WOLFSSL_BUFFER(input + offset, size);
  11690. #endif
  11691. if (!IsAtLeastTLSv1_3(ssl->version))
  11692. break;
  11693. if (msgType != client_hello) {
  11694. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11695. return EXT_NOT_ALLOWED;
  11696. }
  11697. ret = PKM_PARSE(ssl, input + offset, size, msgType);
  11698. break;
  11699. #endif
  11700. #ifdef WOLFSSL_EARLY_DATA
  11701. case TLSX_EARLY_DATA:
  11702. WOLFSSL_MSG("Early Data extension received");
  11703. #ifdef WOLFSSL_DEBUG_TLS
  11704. WOLFSSL_BUFFER(input + offset, size);
  11705. #endif
  11706. if (!IsAtLeastTLSv1_3(ssl->version))
  11707. break;
  11708. if (msgType != client_hello && msgType != session_ticket &&
  11709. msgType != encrypted_extensions) {
  11710. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11711. return EXT_NOT_ALLOWED;
  11712. }
  11713. ret = EDI_PARSE(ssl, input + offset, size, msgType);
  11714. break;
  11715. #endif
  11716. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  11717. case TLSX_POST_HANDSHAKE_AUTH:
  11718. WOLFSSL_MSG("Post Handshake Authentication extension received");
  11719. #ifdef WOLFSSL_DEBUG_TLS
  11720. WOLFSSL_BUFFER(input + offset, size);
  11721. #endif
  11722. if (!IsAtLeastTLSv1_3(ssl->version))
  11723. break;
  11724. if (msgType != client_hello) {
  11725. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11726. return EXT_NOT_ALLOWED;
  11727. }
  11728. ret = PHA_PARSE(ssl, input + offset, size, msgType);
  11729. break;
  11730. #endif
  11731. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  11732. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  11733. WOLFSSL_MSG("Signature Algorithms extension received");
  11734. #ifdef WOLFSSL_DEBUG_TLS
  11735. WOLFSSL_BUFFER(input + offset, size);
  11736. #endif
  11737. if (!IsAtLeastTLSv1_3(ssl->version))
  11738. break;
  11739. if (msgType != client_hello &&
  11740. msgType != certificate_request) {
  11741. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11742. return EXT_NOT_ALLOWED;
  11743. }
  11744. ret = SAC_PARSE(ssl, input + offset, size, isRequest);
  11745. break;
  11746. #endif
  11747. case TLSX_KEY_SHARE:
  11748. WOLFSSL_MSG("Key Share extension received");
  11749. #ifdef WOLFSSL_DEBUG_TLS
  11750. WOLFSSL_BUFFER(input + offset, size);
  11751. #endif
  11752. #ifdef HAVE_SUPPORTED_CURVES
  11753. if (!IsAtLeastTLSv1_3(ssl->version))
  11754. break;
  11755. if (msgType != client_hello && msgType != server_hello &&
  11756. msgType != hello_retry_request) {
  11757. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11758. return EXT_NOT_ALLOWED;
  11759. }
  11760. #endif
  11761. ret = KS_PARSE(ssl, input + offset, size, msgType);
  11762. break;
  11763. #endif
  11764. #ifdef WOLFSSL_SRTP
  11765. case TLSX_USE_SRTP:
  11766. WOLFSSL_MSG("Use SRTP extension received");
  11767. ret = SRTP_PARSE(ssl, input + offset, size, isRequest);
  11768. break;
  11769. #endif
  11770. #ifdef WOLFSSL_QUIC
  11771. case TLSX_KEY_QUIC_TP_PARAMS:
  11772. FALL_THROUGH;
  11773. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  11774. WOLFSSL_MSG("QUIC transport parameter received");
  11775. #ifdef WOLFSSL_DEBUG_TLS
  11776. WOLFSSL_BUFFER(input + offset, size);
  11777. #endif
  11778. if (IsAtLeastTLSv1_3(ssl->version) &&
  11779. msgType != client_hello &&
  11780. msgType != server_hello &&
  11781. msgType != encrypted_extensions) {
  11782. return EXT_NOT_ALLOWED;
  11783. }
  11784. else if (!IsAtLeastTLSv1_3(ssl->version) &&
  11785. msgType == encrypted_extensions) {
  11786. return EXT_NOT_ALLOWED;
  11787. }
  11788. else if (WOLFSSL_IS_QUIC(ssl)) {
  11789. ret = QTP_PARSE(ssl, input + offset, size, type, msgType);
  11790. }
  11791. else {
  11792. WOLFSSL_MSG("QUIC transport param TLS extension type, but no QUIC");
  11793. return EXT_NOT_ALLOWED; /* be safe, this should not happen */
  11794. }
  11795. break;
  11796. #endif /* WOLFSSL_QUIC */
  11797. #if defined(WOLFSSL_DTLS_CID)
  11798. case TLSX_CONNECTION_ID:
  11799. /* connection ID not supported in DTLSv1.2 */
  11800. if (!IsAtLeastTLSv1_3(ssl->version))
  11801. break;
  11802. if (msgType != client_hello && msgType != server_hello)
  11803. return EXT_NOT_ALLOWED;
  11804. WOLFSSL_MSG("ConnectionID extension received");
  11805. ret = CID_PARSE(ssl, input + offset, size, isRequest);
  11806. break;
  11807. #endif /* defined(WOLFSSL_DTLS_CID) */
  11808. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  11809. case TLSX_ECH:
  11810. ret = ECH_PARSE(ssl, input + offset, size, msgType);
  11811. break;
  11812. #endif
  11813. default:
  11814. WOLFSSL_MSG("Unknown TLS extension type");
  11815. }
  11816. /* offset should be updated here! */
  11817. offset += size;
  11818. }
  11819. #ifdef HAVE_EXTENDED_MASTER
  11820. if (IsAtLeastTLSv1_3(ssl->version) && msgType == hello_retry_request) {
  11821. /* Don't change EMS status until server_hello received.
  11822. * Second ClientHello must have same extensions.
  11823. */
  11824. }
  11825. else if (!isRequest && ssl->options.haveEMS && !pendingEMS)
  11826. ssl->options.haveEMS = 0;
  11827. #endif
  11828. if (ret == 0)
  11829. ret = SNI_VERIFY_PARSE(ssl, isRequest);
  11830. if (ret == 0)
  11831. ret = TCA_VERIFY_PARSE(ssl, isRequest);
  11832. return ret;
  11833. }
  11834. /* undefining semaphore macros */
  11835. #undef IS_OFF
  11836. #undef TURN_ON
  11837. #undef SEMAPHORE_SIZE
  11838. #endif /* HAVE_TLS_EXTENSIONS */
  11839. #ifndef NO_WOLFSSL_CLIENT
  11840. WOLFSSL_METHOD* wolfTLS_client_method(void)
  11841. {
  11842. return wolfTLS_client_method_ex(NULL);
  11843. }
  11844. WOLFSSL_METHOD* wolfTLS_client_method_ex(void* heap)
  11845. {
  11846. WOLFSSL_METHOD* method =
  11847. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11848. heap, DYNAMIC_TYPE_METHOD);
  11849. (void)heap;
  11850. WOLFSSL_ENTER("TLS_client_method_ex");
  11851. if (method) {
  11852. #if defined(WOLFSSL_TLS13)
  11853. InitSSL_Method(method, MakeTLSv1_3());
  11854. #elif !defined(WOLFSSL_NO_TLS12)
  11855. InitSSL_Method(method, MakeTLSv1_2());
  11856. #elif !defined(NO_OLD_TLS)
  11857. InitSSL_Method(method, MakeTLSv1_1());
  11858. #elif defined(WOLFSSL_ALLOW_TLSV10)
  11859. InitSSL_Method(method, MakeTLSv1());
  11860. #else
  11861. #error No TLS version enabled!
  11862. #endif
  11863. method->downgrade = 1;
  11864. method->side = WOLFSSL_CLIENT_END;
  11865. }
  11866. return method;
  11867. }
  11868. #ifndef NO_OLD_TLS
  11869. #ifdef WOLFSSL_ALLOW_TLSV10
  11870. WOLFSSL_METHOD* wolfTLSv1_client_method(void)
  11871. {
  11872. return wolfTLSv1_client_method_ex(NULL);
  11873. }
  11874. WOLFSSL_METHOD* wolfTLSv1_client_method_ex(void* heap)
  11875. {
  11876. WOLFSSL_METHOD* method =
  11877. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11878. heap, DYNAMIC_TYPE_METHOD);
  11879. (void)heap;
  11880. WOLFSSL_ENTER("TLSv1_client_method_ex");
  11881. if (method)
  11882. InitSSL_Method(method, MakeTLSv1());
  11883. return method;
  11884. }
  11885. #endif /* WOLFSSL_ALLOW_TLSV10 */
  11886. WOLFSSL_METHOD* wolfTLSv1_1_client_method(void)
  11887. {
  11888. return wolfTLSv1_1_client_method_ex(NULL);
  11889. }
  11890. WOLFSSL_METHOD* wolfTLSv1_1_client_method_ex(void* heap)
  11891. {
  11892. WOLFSSL_METHOD* method =
  11893. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11894. heap, DYNAMIC_TYPE_METHOD);
  11895. (void)heap;
  11896. WOLFSSL_ENTER("TLSv1_1_client_method_ex");
  11897. if (method)
  11898. InitSSL_Method(method, MakeTLSv1_1());
  11899. return method;
  11900. }
  11901. #endif /* !NO_OLD_TLS */
  11902. #ifndef WOLFSSL_NO_TLS12
  11903. WOLFSSL_ABI
  11904. WOLFSSL_METHOD* wolfTLSv1_2_client_method(void)
  11905. {
  11906. return wolfTLSv1_2_client_method_ex(NULL);
  11907. }
  11908. WOLFSSL_METHOD* wolfTLSv1_2_client_method_ex(void* heap)
  11909. {
  11910. WOLFSSL_METHOD* method =
  11911. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11912. heap, DYNAMIC_TYPE_METHOD);
  11913. (void)heap;
  11914. WOLFSSL_ENTER("TLSv1_2_client_method_ex");
  11915. if (method)
  11916. InitSSL_Method(method, MakeTLSv1_2());
  11917. return method;
  11918. }
  11919. #endif /* WOLFSSL_NO_TLS12 */
  11920. #ifdef WOLFSSL_TLS13
  11921. /* The TLS v1.3 client method data.
  11922. *
  11923. * returns the method data for a TLS v1.3 client.
  11924. */
  11925. WOLFSSL_ABI
  11926. WOLFSSL_METHOD* wolfTLSv1_3_client_method(void)
  11927. {
  11928. return wolfTLSv1_3_client_method_ex(NULL);
  11929. }
  11930. /* The TLS v1.3 client method data.
  11931. *
  11932. * heap The heap used for allocation.
  11933. * returns the method data for a TLS v1.3 client.
  11934. */
  11935. WOLFSSL_METHOD* wolfTLSv1_3_client_method_ex(void* heap)
  11936. {
  11937. WOLFSSL_METHOD* method = (WOLFSSL_METHOD*)
  11938. XMALLOC(sizeof(WOLFSSL_METHOD), heap,
  11939. DYNAMIC_TYPE_METHOD);
  11940. (void)heap;
  11941. WOLFSSL_ENTER("TLSv1_3_client_method_ex");
  11942. if (method)
  11943. InitSSL_Method(method, MakeTLSv1_3());
  11944. return method;
  11945. }
  11946. #endif /* WOLFSSL_TLS13 */
  11947. #ifdef WOLFSSL_DTLS
  11948. WOLFSSL_METHOD* wolfDTLS_client_method(void)
  11949. {
  11950. return wolfDTLS_client_method_ex(NULL);
  11951. }
  11952. WOLFSSL_METHOD* wolfDTLS_client_method_ex(void* heap)
  11953. {
  11954. WOLFSSL_METHOD* method =
  11955. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11956. heap, DYNAMIC_TYPE_METHOD);
  11957. (void)heap;
  11958. WOLFSSL_ENTER("DTLS_client_method_ex");
  11959. if (method) {
  11960. #if defined(WOLFSSL_DTLS13)
  11961. InitSSL_Method(method, MakeDTLSv1_3());
  11962. #elif !defined(WOLFSSL_NO_TLS12)
  11963. InitSSL_Method(method, MakeDTLSv1_2());
  11964. #elif !defined(NO_OLD_TLS)
  11965. InitSSL_Method(method, MakeDTLSv1());
  11966. #else
  11967. #error No DTLS version enabled!
  11968. #endif
  11969. method->downgrade = 1;
  11970. method->side = WOLFSSL_CLIENT_END;
  11971. }
  11972. return method;
  11973. }
  11974. #ifndef NO_OLD_TLS
  11975. WOLFSSL_METHOD* wolfDTLSv1_client_method(void)
  11976. {
  11977. return wolfDTLSv1_client_method_ex(NULL);
  11978. }
  11979. WOLFSSL_METHOD* wolfDTLSv1_client_method_ex(void* heap)
  11980. {
  11981. WOLFSSL_METHOD* method =
  11982. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11983. heap, DYNAMIC_TYPE_METHOD);
  11984. (void)heap;
  11985. WOLFSSL_ENTER("DTLSv1_client_method_ex");
  11986. if (method)
  11987. InitSSL_Method(method, MakeDTLSv1());
  11988. return method;
  11989. }
  11990. #endif /* NO_OLD_TLS */
  11991. #ifndef WOLFSSL_NO_TLS12
  11992. WOLFSSL_METHOD* wolfDTLSv1_2_client_method(void)
  11993. {
  11994. return wolfDTLSv1_2_client_method_ex(NULL);
  11995. }
  11996. WOLFSSL_METHOD* wolfDTLSv1_2_client_method_ex(void* heap)
  11997. {
  11998. WOLFSSL_METHOD* method =
  11999. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12000. heap, DYNAMIC_TYPE_METHOD);
  12001. (void)heap;
  12002. WOLFSSL_ENTER("DTLSv1_2_client_method_ex");
  12003. if (method)
  12004. InitSSL_Method(method, MakeDTLSv1_2());
  12005. (void)heap;
  12006. return method;
  12007. }
  12008. #endif /* !WOLFSSL_NO_TLS12 */
  12009. #endif /* WOLFSSL_DTLS */
  12010. #endif /* NO_WOLFSSL_CLIENT */
  12011. /* EITHER SIDE METHODS */
  12012. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  12013. #ifndef NO_OLD_TLS
  12014. #ifdef WOLFSSL_ALLOW_TLSV10
  12015. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  12016. *
  12017. * Returns a pointer to a WOLFSSL_METHOD struct
  12018. */
  12019. WOLFSSL_METHOD* wolfTLSv1_method(void)
  12020. {
  12021. return wolfTLSv1_method_ex(NULL);
  12022. }
  12023. WOLFSSL_METHOD* wolfTLSv1_method_ex(void* heap)
  12024. {
  12025. WOLFSSL_METHOD* m;
  12026. WOLFSSL_ENTER("TLSv1_method");
  12027. #ifndef NO_WOLFSSL_CLIENT
  12028. m = wolfTLSv1_client_method_ex(heap);
  12029. #else
  12030. m = wolfTLSv1_server_method_ex(heap);
  12031. #endif
  12032. if (m != NULL) {
  12033. m->side = WOLFSSL_NEITHER_END;
  12034. }
  12035. return m;
  12036. }
  12037. #endif /* WOLFSSL_ALLOW_TLSV10 */
  12038. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  12039. *
  12040. * Returns a pointer to a WOLFSSL_METHOD struct
  12041. */
  12042. WOLFSSL_METHOD* wolfTLSv1_1_method(void)
  12043. {
  12044. return wolfTLSv1_1_method_ex(NULL);
  12045. }
  12046. WOLFSSL_METHOD* wolfTLSv1_1_method_ex(void* heap)
  12047. {
  12048. WOLFSSL_METHOD* m;
  12049. WOLFSSL_ENTER("TLSv1_1_method");
  12050. #ifndef NO_WOLFSSL_CLIENT
  12051. m = wolfTLSv1_1_client_method_ex(heap);
  12052. #else
  12053. m = wolfTLSv1_1_server_method_ex(heap);
  12054. #endif
  12055. if (m != NULL) {
  12056. m->side = WOLFSSL_NEITHER_END;
  12057. }
  12058. return m;
  12059. }
  12060. #endif /* !NO_OLD_TLS */
  12061. #ifndef WOLFSSL_NO_TLS12
  12062. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  12063. *
  12064. * Returns a pointer to a WOLFSSL_METHOD struct
  12065. */
  12066. WOLFSSL_METHOD* wolfTLSv1_2_method(void)
  12067. {
  12068. return wolfTLSv1_2_method_ex(NULL);
  12069. }
  12070. WOLFSSL_METHOD* wolfTLSv1_2_method_ex(void* heap)
  12071. {
  12072. WOLFSSL_METHOD* m;
  12073. WOLFSSL_ENTER("TLSv1_2_method");
  12074. #ifndef NO_WOLFSSL_CLIENT
  12075. m = wolfTLSv1_2_client_method_ex(heap);
  12076. #else
  12077. m = wolfTLSv1_2_server_method_ex(heap);
  12078. #endif
  12079. if (m != NULL) {
  12080. m->side = WOLFSSL_NEITHER_END;
  12081. }
  12082. return m;
  12083. }
  12084. #endif /* !WOLFSSL_NO_TLS12 */
  12085. #ifdef WOLFSSL_TLS13
  12086. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  12087. *
  12088. * Returns a pointer to a WOLFSSL_METHOD struct
  12089. */
  12090. WOLFSSL_METHOD* wolfTLSv1_3_method(void)
  12091. {
  12092. return wolfTLSv1_3_method_ex(NULL);
  12093. }
  12094. WOLFSSL_METHOD* wolfTLSv1_3_method_ex(void* heap)
  12095. {
  12096. WOLFSSL_METHOD* m;
  12097. WOLFSSL_ENTER("TLSv1_3_method");
  12098. #ifndef NO_WOLFSSL_CLIENT
  12099. m = wolfTLSv1_3_client_method_ex(heap);
  12100. #else
  12101. m = wolfTLSv1_3_server_method_ex(heap);
  12102. #endif
  12103. if (m != NULL) {
  12104. m->side = WOLFSSL_NEITHER_END;
  12105. }
  12106. return m;
  12107. }
  12108. #endif /* WOLFSSL_TLS13 */
  12109. #ifdef WOLFSSL_DTLS
  12110. WOLFSSL_METHOD* wolfDTLS_method(void)
  12111. {
  12112. return wolfDTLS_method_ex(NULL);
  12113. }
  12114. WOLFSSL_METHOD* wolfDTLS_method_ex(void* heap)
  12115. {
  12116. WOLFSSL_METHOD* m;
  12117. WOLFSSL_ENTER("DTLS_method_ex");
  12118. #ifndef NO_WOLFSSL_CLIENT
  12119. m = wolfDTLS_client_method_ex(heap);
  12120. #else
  12121. m = wolfDTLS_server_method_ex(heap);
  12122. #endif
  12123. if (m != NULL) {
  12124. m->side = WOLFSSL_NEITHER_END;
  12125. }
  12126. return m;
  12127. }
  12128. #ifndef NO_OLD_TLS
  12129. WOLFSSL_METHOD* wolfDTLSv1_method(void)
  12130. {
  12131. return wolfDTLSv1_method_ex(NULL);
  12132. }
  12133. WOLFSSL_METHOD* wolfDTLSv1_method_ex(void* heap)
  12134. {
  12135. WOLFSSL_METHOD* m;
  12136. WOLFSSL_ENTER("DTLSv1_method_ex");
  12137. #ifndef NO_WOLFSSL_CLIENT
  12138. m = wolfDTLSv1_client_method_ex(heap);
  12139. #else
  12140. m = wolfDTLSv1_server_method_ex(heap);
  12141. #endif
  12142. if (m != NULL) {
  12143. m->side = WOLFSSL_NEITHER_END;
  12144. }
  12145. return m;
  12146. }
  12147. #endif /* !NO_OLD_TLS */
  12148. #ifndef WOLFSSL_NO_TLS12
  12149. WOLFSSL_METHOD* wolfDTLSv1_2_method(void)
  12150. {
  12151. return wolfDTLSv1_2_method_ex(NULL);
  12152. }
  12153. WOLFSSL_METHOD* wolfDTLSv1_2_method_ex(void* heap)
  12154. {
  12155. WOLFSSL_METHOD* m;
  12156. WOLFSSL_ENTER("DTLSv1_2_method");
  12157. #ifndef NO_WOLFSSL_CLIENT
  12158. m = wolfDTLSv1_2_client_method_ex(heap);
  12159. #else
  12160. m = wolfDTLSv1_2_server_method_ex(heap);
  12161. #endif
  12162. if (m != NULL) {
  12163. m->side = WOLFSSL_NEITHER_END;
  12164. }
  12165. return m;
  12166. }
  12167. #endif /* !WOLFSSL_NO_TLS12 */
  12168. #endif /* WOLFSSL_DTLS */
  12169. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  12170. #ifndef NO_WOLFSSL_SERVER
  12171. WOLFSSL_METHOD* wolfTLS_server_method(void)
  12172. {
  12173. return wolfTLS_server_method_ex(NULL);
  12174. }
  12175. WOLFSSL_METHOD* wolfTLS_server_method_ex(void* heap)
  12176. {
  12177. WOLFSSL_METHOD* method =
  12178. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12179. heap, DYNAMIC_TYPE_METHOD);
  12180. (void)heap;
  12181. WOLFSSL_ENTER("TLS_server_method_ex");
  12182. if (method) {
  12183. #if defined(WOLFSSL_TLS13)
  12184. InitSSL_Method(method, MakeTLSv1_3());
  12185. #elif !defined(WOLFSSL_NO_TLS12)
  12186. InitSSL_Method(method, MakeTLSv1_2());
  12187. #elif !defined(NO_OLD_TLS)
  12188. InitSSL_Method(method, MakeTLSv1_1());
  12189. #elif defined(WOLFSSL_ALLOW_TLSV10)
  12190. InitSSL_Method(method, MakeTLSv1());
  12191. #else
  12192. #error No TLS version enabled!
  12193. #endif
  12194. method->downgrade = 1;
  12195. method->side = WOLFSSL_SERVER_END;
  12196. }
  12197. return method;
  12198. }
  12199. #ifndef NO_OLD_TLS
  12200. #ifdef WOLFSSL_ALLOW_TLSV10
  12201. WOLFSSL_METHOD* wolfTLSv1_server_method(void)
  12202. {
  12203. return wolfTLSv1_server_method_ex(NULL);
  12204. }
  12205. WOLFSSL_METHOD* wolfTLSv1_server_method_ex(void* heap)
  12206. {
  12207. WOLFSSL_METHOD* method =
  12208. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12209. heap, DYNAMIC_TYPE_METHOD);
  12210. (void)heap;
  12211. WOLFSSL_ENTER("TLSv1_server_method_ex");
  12212. if (method) {
  12213. InitSSL_Method(method, MakeTLSv1());
  12214. method->side = WOLFSSL_SERVER_END;
  12215. }
  12216. return method;
  12217. }
  12218. #endif /* WOLFSSL_ALLOW_TLSV10 */
  12219. WOLFSSL_METHOD* wolfTLSv1_1_server_method(void)
  12220. {
  12221. return wolfTLSv1_1_server_method_ex(NULL);
  12222. }
  12223. WOLFSSL_METHOD* wolfTLSv1_1_server_method_ex(void* heap)
  12224. {
  12225. WOLFSSL_METHOD* method =
  12226. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12227. heap, DYNAMIC_TYPE_METHOD);
  12228. (void)heap;
  12229. WOLFSSL_ENTER("TLSv1_1_server_method_ex");
  12230. if (method) {
  12231. InitSSL_Method(method, MakeTLSv1_1());
  12232. method->side = WOLFSSL_SERVER_END;
  12233. }
  12234. return method;
  12235. }
  12236. #endif /* !NO_OLD_TLS */
  12237. #ifndef WOLFSSL_NO_TLS12
  12238. WOLFSSL_ABI
  12239. WOLFSSL_METHOD* wolfTLSv1_2_server_method(void)
  12240. {
  12241. return wolfTLSv1_2_server_method_ex(NULL);
  12242. }
  12243. WOLFSSL_METHOD* wolfTLSv1_2_server_method_ex(void* heap)
  12244. {
  12245. WOLFSSL_METHOD* method =
  12246. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12247. heap, DYNAMIC_TYPE_METHOD);
  12248. (void)heap;
  12249. WOLFSSL_ENTER("TLSv1_2_server_method_ex");
  12250. if (method) {
  12251. InitSSL_Method(method, MakeTLSv1_2());
  12252. method->side = WOLFSSL_SERVER_END;
  12253. }
  12254. return method;
  12255. }
  12256. #endif /* !WOLFSSL_NO_TLS12 */
  12257. #ifdef WOLFSSL_TLS13
  12258. /* The TLS v1.3 server method data.
  12259. *
  12260. * returns the method data for a TLS v1.3 server.
  12261. */
  12262. WOLFSSL_ABI
  12263. WOLFSSL_METHOD* wolfTLSv1_3_server_method(void)
  12264. {
  12265. return wolfTLSv1_3_server_method_ex(NULL);
  12266. }
  12267. /* The TLS v1.3 server method data.
  12268. *
  12269. * heap The heap used for allocation.
  12270. * returns the method data for a TLS v1.3 server.
  12271. */
  12272. WOLFSSL_METHOD* wolfTLSv1_3_server_method_ex(void* heap)
  12273. {
  12274. WOLFSSL_METHOD* method =
  12275. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12276. heap, DYNAMIC_TYPE_METHOD);
  12277. (void)heap;
  12278. WOLFSSL_ENTER("TLSv1_3_server_method_ex");
  12279. if (method) {
  12280. InitSSL_Method(method, MakeTLSv1_3());
  12281. method->side = WOLFSSL_SERVER_END;
  12282. }
  12283. return method;
  12284. }
  12285. #endif /* WOLFSSL_TLS13 */
  12286. #ifdef WOLFSSL_DTLS
  12287. WOLFSSL_METHOD* wolfDTLS_server_method(void)
  12288. {
  12289. return wolfDTLS_server_method_ex(NULL);
  12290. }
  12291. WOLFSSL_METHOD* wolfDTLS_server_method_ex(void* heap)
  12292. {
  12293. WOLFSSL_METHOD* method =
  12294. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12295. heap, DYNAMIC_TYPE_METHOD);
  12296. (void)heap;
  12297. WOLFSSL_ENTER("DTLS_server_method_ex");
  12298. if (method) {
  12299. #if defined(WOLFSSL_DTLS13)
  12300. InitSSL_Method(method, MakeDTLSv1_3());
  12301. #elif !defined(WOLFSSL_NO_TLS12)
  12302. InitSSL_Method(method, MakeDTLSv1_2());
  12303. #elif !defined(NO_OLD_TLS)
  12304. InitSSL_Method(method, MakeDTLSv1());
  12305. #else
  12306. #error No DTLS version enabled!
  12307. #endif
  12308. method->downgrade = 1;
  12309. method->side = WOLFSSL_SERVER_END;
  12310. }
  12311. return method;
  12312. }
  12313. #ifndef NO_OLD_TLS
  12314. WOLFSSL_METHOD* wolfDTLSv1_server_method(void)
  12315. {
  12316. return wolfDTLSv1_server_method_ex(NULL);
  12317. }
  12318. WOLFSSL_METHOD* wolfDTLSv1_server_method_ex(void* heap)
  12319. {
  12320. WOLFSSL_METHOD* method =
  12321. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12322. heap, DYNAMIC_TYPE_METHOD);
  12323. (void)heap;
  12324. WOLFSSL_ENTER("DTLSv1_server_method_ex");
  12325. if (method) {
  12326. InitSSL_Method(method, MakeDTLSv1());
  12327. method->side = WOLFSSL_SERVER_END;
  12328. }
  12329. return method;
  12330. }
  12331. #endif /* !NO_OLD_TLS */
  12332. #ifndef WOLFSSL_NO_TLS12
  12333. WOLFSSL_METHOD* wolfDTLSv1_2_server_method(void)
  12334. {
  12335. return wolfDTLSv1_2_server_method_ex(NULL);
  12336. }
  12337. WOLFSSL_METHOD* wolfDTLSv1_2_server_method_ex(void* heap)
  12338. {
  12339. WOLFSSL_METHOD* method =
  12340. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12341. heap, DYNAMIC_TYPE_METHOD);
  12342. WOLFSSL_ENTER("DTLSv1_2_server_method_ex");
  12343. (void)heap;
  12344. if (method) {
  12345. InitSSL_Method(method, MakeDTLSv1_2());
  12346. method->side = WOLFSSL_SERVER_END;
  12347. }
  12348. (void)heap;
  12349. return method;
  12350. }
  12351. #endif /* !WOLFSSL_NO_TLS12 */
  12352. #endif /* WOLFSSL_DTLS */
  12353. #endif /* NO_WOLFSSL_SERVER */
  12354. #endif /* NO_TLS */
  12355. #endif /* WOLFCRYPT_ONLY */