tls.c 433 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264
  1. /* tls.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #ifndef WOLFCRYPT_ONLY
  26. #include <wolfssl/ssl.h>
  27. #include <wolfssl/internal.h>
  28. #include <wolfssl/error-ssl.h>
  29. #include <wolfssl/wolfcrypt/hash.h>
  30. #include <wolfssl/wolfcrypt/hmac.h>
  31. #include <wolfssl/wolfcrypt/kdf.h>
  32. #ifdef NO_INLINE
  33. #include <wolfssl/wolfcrypt/misc.h>
  34. #else
  35. #define WOLFSSL_MISC_INCLUDED
  36. #include <wolfcrypt/src/misc.c>
  37. #endif
  38. #ifdef HAVE_CURVE25519
  39. #include <wolfssl/wolfcrypt/curve25519.h>
  40. #endif
  41. #ifdef HAVE_CURVE448
  42. #include <wolfssl/wolfcrypt/curve448.h>
  43. #endif
  44. #ifdef HAVE_PQC
  45. #include <wolfssl/wolfcrypt/kyber.h>
  46. #ifdef WOLFSSL_WC_KYBER
  47. #include <wolfssl/wolfcrypt/wc_kyber.h>
  48. #elif defined(HAVE_LIBOQS) || defined(HAVE_PQM4)
  49. #include <wolfssl/wolfcrypt/ext_kyber.h>
  50. #endif
  51. #endif
  52. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  53. #include <wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h>
  54. #endif
  55. #include <wolfssl/wolfcrypt/hpke.h>
  56. #ifndef NO_TLS
  57. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  58. static int TLSX_KeyShare_IsSupported(int namedGroup);
  59. static void TLSX_KeyShare_FreeAll(KeyShareEntry* list, void* heap);
  60. #endif
  61. #ifdef HAVE_SUPPORTED_CURVES
  62. static int TLSX_PopulateSupportedGroups(WOLFSSL* ssl, TLSX** extensions);
  63. #endif
  64. /* Digest enable checks */
  65. #ifdef NO_OLD_TLS /* TLS 1.2 only */
  66. #if defined(NO_SHA256) && !defined(WOLFSSL_SHA384) && \
  67. !defined(WOLFSSL_SHA512)
  68. #error Must have SHA256, SHA384 or SHA512 enabled for TLS 1.2
  69. #endif
  70. #else /* TLS 1.1 or older */
  71. #if defined(NO_MD5) && defined(NO_SHA)
  72. #error Must have SHA1 and MD5 enabled for old TLS
  73. #endif
  74. #endif
  75. #ifdef WOLFSSL_TLS13
  76. #if !defined(NO_DH) && \
  77. !defined(HAVE_FFDHE_2048) && !defined(HAVE_FFDHE_3072) && \
  78. !defined(HAVE_FFDHE_4096) && !defined(HAVE_FFDHE_6144) && \
  79. !defined(HAVE_FFDHE_8192)
  80. #error Please configure your TLS 1.3 DH key size using either: HAVE_FFDHE_2048, HAVE_FFDHE_3072, HAVE_FFDHE_4096, HAVE_FFDHE_6144 or HAVE_FFDHE_8192
  81. #endif
  82. #if !defined(NO_RSA) && !defined(WC_RSA_PSS)
  83. #error The build option WC_RSA_PSS is required for TLS 1.3 with RSA
  84. #endif
  85. #ifndef HAVE_TLS_EXTENSIONS
  86. #ifndef _MSC_VER
  87. #error "The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3"
  88. #else
  89. #pragma message("Error: The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3")
  90. #endif
  91. #endif
  92. #endif
  93. /* Warn if secrets logging is enabled */
  94. #if defined(SHOW_SECRETS) || defined(WOLFSSL_SSLKEYLOGFILE)
  95. #ifndef _MSC_VER
  96. #warning The SHOW_SECRETS and WOLFSSL_SSLKEYLOGFILE options should only be used for debugging and never in a production environment
  97. #else
  98. #pragma message("Warning: The SHOW_SECRETS and WOLFSSL_SSLKEYLOGFILE options should only be used for debugging and never in a production environment")
  99. #endif
  100. #endif
  101. /* Optional Pre-Master-Secret logging for Wireshark */
  102. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_SSLKEYLOGFILE)
  103. #ifndef WOLFSSL_SSLKEYLOGFILE_OUTPUT
  104. #define WOLFSSL_SSLKEYLOGFILE_OUTPUT "sslkeylog.log"
  105. #endif
  106. #endif
  107. #ifndef WOLFSSL_NO_TLS12
  108. #ifdef WOLFSSL_SHA384
  109. #define HSHASH_SZ WC_SHA384_DIGEST_SIZE
  110. #else
  111. #define HSHASH_SZ FINISHED_SZ
  112. #endif
  113. int BuildTlsHandshakeHash(WOLFSSL* ssl, byte* hash, word32* hashLen)
  114. {
  115. int ret = 0;
  116. word32 hashSz = FINISHED_SZ;
  117. if (ssl == NULL || hash == NULL || hashLen == NULL || *hashLen < HSHASH_SZ)
  118. return BAD_FUNC_ARG;
  119. /* for constant timing perform these even if error */
  120. #ifndef NO_OLD_TLS
  121. ret |= wc_Md5GetHash(&ssl->hsHashes->hashMd5, hash);
  122. ret |= wc_ShaGetHash(&ssl->hsHashes->hashSha, &hash[WC_MD5_DIGEST_SIZE]);
  123. #endif
  124. if (IsAtLeastTLSv1_2(ssl)) {
  125. #ifndef NO_SHA256
  126. if (ssl->specs.mac_algorithm <= sha256_mac ||
  127. ssl->specs.mac_algorithm == blake2b_mac) {
  128. ret |= wc_Sha256GetHash(&ssl->hsHashes->hashSha256, hash);
  129. hashSz = WC_SHA256_DIGEST_SIZE;
  130. }
  131. #endif
  132. #ifdef WOLFSSL_SHA384
  133. if (ssl->specs.mac_algorithm == sha384_mac) {
  134. ret |= wc_Sha384GetHash(&ssl->hsHashes->hashSha384, hash);
  135. hashSz = WC_SHA384_DIGEST_SIZE;
  136. }
  137. #endif
  138. }
  139. *hashLen = hashSz;
  140. #ifdef WOLFSSL_CHECK_MEM_ZERO
  141. wc_MemZero_Add("TLS handshake hash", hash, hashSz);
  142. #endif
  143. if (ret != 0) {
  144. ret = BUILD_MSG_ERROR;
  145. WOLFSSL_ERROR_VERBOSE(ret);
  146. }
  147. return ret;
  148. }
  149. int BuildTlsFinished(WOLFSSL* ssl, Hashes* hashes, const byte* sender)
  150. {
  151. int ret;
  152. const byte* side = NULL;
  153. word32 hashSz = HSHASH_SZ;
  154. #if !defined(WOLFSSL_ASYNC_CRYPT) || defined(WC_ASYNC_NO_HASH)
  155. byte handshake_hash[HSHASH_SZ];
  156. #else
  157. WC_DECLARE_VAR(handshake_hash, byte, HSHASH_SZ, ssl->heap);
  158. if (handshake_hash == NULL)
  159. return MEMORY_E;
  160. #endif
  161. ret = BuildTlsHandshakeHash(ssl, handshake_hash, &hashSz);
  162. if (ret == 0) {
  163. if (XSTRNCMP((const char*)sender, (const char*)kTlsClientStr,
  164. SIZEOF_SENDER) == 0) {
  165. side = kTlsClientFinStr;
  166. }
  167. else if (XSTRNCMP((const char*)sender, (const char*)kTlsServerStr,
  168. SIZEOF_SENDER) == 0) {
  169. side = kTlsServerFinStr;
  170. }
  171. else {
  172. ret = BAD_FUNC_ARG;
  173. WOLFSSL_MSG("Unexpected sender value");
  174. }
  175. }
  176. if (ret == 0) {
  177. #ifdef WOLFSSL_HAVE_PRF
  178. #if !defined(NO_CERTS) && defined(HAVE_PK_CALLBACKS)
  179. if (ssl->ctx->TlsFinishedCb) {
  180. void* ctx = wolfSSL_GetTlsFinishedCtx(ssl);
  181. ret = ssl->ctx->TlsFinishedCb(ssl, side, handshake_hash, hashSz,
  182. (byte*)hashes, ctx);
  183. }
  184. if (!ssl->ctx->TlsFinishedCb || ret == PROTOCOLCB_UNAVAILABLE)
  185. #endif
  186. {
  187. PRIVATE_KEY_UNLOCK();
  188. ret = wc_PRF_TLS((byte*)hashes, TLS_FINISHED_SZ,
  189. ssl->arrays->masterSecret, SECRET_LEN, side,
  190. FINISHED_LABEL_SZ, handshake_hash, hashSz,
  191. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  192. ssl->heap, ssl->devId);
  193. PRIVATE_KEY_LOCK();
  194. }
  195. ForceZero(handshake_hash, hashSz);
  196. #else
  197. /* Pseudo random function must be enabled in the configuration. */
  198. ret = PRF_MISSING;
  199. WOLFSSL_ERROR_VERBOSE(ret);
  200. WOLFSSL_MSG("Pseudo-random function is not enabled");
  201. (void)side;
  202. (void)hashes;
  203. #endif
  204. }
  205. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  206. WC_FREE_VAR(handshake_hash, ssl->heap);
  207. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  208. wc_MemZero_Check(handshake_hash, HSHASH_SZ);
  209. #endif
  210. return ret;
  211. }
  212. #endif /* !WOLFSSL_NO_TLS12 */
  213. #ifndef NO_OLD_TLS
  214. #ifdef WOLFSSL_ALLOW_TLSV10
  215. ProtocolVersion MakeTLSv1(void)
  216. {
  217. ProtocolVersion pv;
  218. pv.major = SSLv3_MAJOR;
  219. pv.minor = TLSv1_MINOR;
  220. return pv;
  221. }
  222. #endif /* WOLFSSL_ALLOW_TLSV10 */
  223. ProtocolVersion MakeTLSv1_1(void)
  224. {
  225. ProtocolVersion pv;
  226. pv.major = SSLv3_MAJOR;
  227. pv.minor = TLSv1_1_MINOR;
  228. return pv;
  229. }
  230. #endif /* !NO_OLD_TLS */
  231. #ifndef WOLFSSL_NO_TLS12
  232. ProtocolVersion MakeTLSv1_2(void)
  233. {
  234. ProtocolVersion pv;
  235. pv.major = SSLv3_MAJOR;
  236. pv.minor = TLSv1_2_MINOR;
  237. return pv;
  238. }
  239. #endif /* !WOLFSSL_NO_TLS12 */
  240. #ifdef WOLFSSL_TLS13
  241. /* The TLS v1.3 protocol version.
  242. *
  243. * returns the protocol version data for TLS v1.3.
  244. */
  245. ProtocolVersion MakeTLSv1_3(void)
  246. {
  247. ProtocolVersion pv;
  248. pv.major = SSLv3_MAJOR;
  249. pv.minor = TLSv1_3_MINOR;
  250. return pv;
  251. }
  252. #endif
  253. #ifndef WOLFSSL_NO_TLS12
  254. #ifdef HAVE_EXTENDED_MASTER
  255. static const byte ext_master_label[EXT_MASTER_LABEL_SZ + 1] =
  256. "extended master secret";
  257. #endif
  258. static const byte master_label[MASTER_LABEL_SZ + 1] = "master secret";
  259. static const byte key_label [KEY_LABEL_SZ + 1] = "key expansion";
  260. static int _DeriveTlsKeys(byte* key_dig, word32 key_dig_len,
  261. const byte* ms, word32 msLen,
  262. const byte* sr, const byte* cr,
  263. int tls1_2, int hash_type,
  264. void* heap, int devId)
  265. {
  266. int ret;
  267. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  268. WC_DECLARE_VAR(seed, byte, SEED_LEN, heap);
  269. if (seed == NULL)
  270. return MEMORY_E;
  271. #else
  272. byte seed[SEED_LEN];
  273. #endif
  274. XMEMCPY(seed, sr, RAN_LEN);
  275. XMEMCPY(seed + RAN_LEN, cr, RAN_LEN);
  276. #ifdef WOLFSSL_HAVE_PRF
  277. PRIVATE_KEY_UNLOCK();
  278. ret = wc_PRF_TLS(key_dig, key_dig_len, ms, msLen, key_label, KEY_LABEL_SZ,
  279. seed, SEED_LEN, tls1_2, hash_type, heap, devId);
  280. PRIVATE_KEY_LOCK();
  281. #else
  282. /* Pseudo random function must be enabled in the configuration. */
  283. ret = PRF_MISSING;
  284. WOLFSSL_ERROR_VERBOSE(ret);
  285. WOLFSSL_MSG("Pseudo-random function is not enabled");
  286. (void)key_dig;
  287. (void)key_dig_len;
  288. (void)ms;
  289. (void)msLen;
  290. (void)tls1_2;
  291. (void)hash_type;
  292. (void)heap;
  293. (void)devId;
  294. (void)key_label;
  295. (void)master_label;
  296. #ifdef HAVE_EXTENDED_MASTER
  297. (void)ext_master_label;
  298. #endif
  299. #endif
  300. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  301. WC_FREE_VAR(seed, heap);
  302. #endif
  303. return ret;
  304. }
  305. /* External facing wrapper so user can call as well, 0 on success */
  306. int wolfSSL_DeriveTlsKeys(byte* key_dig, word32 key_dig_len,
  307. const byte* ms, word32 msLen,
  308. const byte* sr, const byte* cr,
  309. int tls1_2, int hash_type)
  310. {
  311. return _DeriveTlsKeys(key_dig, key_dig_len, ms, msLen, sr, cr, tls1_2,
  312. hash_type, NULL, INVALID_DEVID);
  313. }
  314. int DeriveTlsKeys(WOLFSSL* ssl)
  315. {
  316. int ret;
  317. int key_dig_len = 2 * ssl->specs.hash_size +
  318. 2 * ssl->specs.key_size +
  319. 2 * ssl->specs.iv_size;
  320. #ifdef WOLFSSL_SMALL_STACK
  321. byte* key_dig;
  322. #else
  323. byte key_dig[MAX_PRF_DIG];
  324. #endif
  325. #ifdef WOLFSSL_SMALL_STACK
  326. key_dig = (byte*)XMALLOC(MAX_PRF_DIG, ssl->heap, DYNAMIC_TYPE_DIGEST);
  327. if (key_dig == NULL) {
  328. return MEMORY_E;
  329. }
  330. #endif
  331. #if !defined(NO_CERTS) && defined(HAVE_PK_CALLBACKS)
  332. ret = PROTOCOLCB_UNAVAILABLE;
  333. if (ssl->ctx->GenSessionKeyCb) {
  334. void* ctx = wolfSSL_GetGenSessionKeyCtx(ssl);
  335. ret = ssl->ctx->GenSessionKeyCb(ssl, ctx);
  336. }
  337. if (!ssl->ctx->GenSessionKeyCb || ret == PROTOCOLCB_UNAVAILABLE)
  338. #endif
  339. ret = _DeriveTlsKeys(key_dig, key_dig_len,
  340. ssl->arrays->masterSecret, SECRET_LEN,
  341. ssl->arrays->serverRandom, ssl->arrays->clientRandom,
  342. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  343. ssl->heap, ssl->devId);
  344. if (ret == 0)
  345. ret = StoreKeys(ssl, key_dig, PROVISION_CLIENT_SERVER);
  346. #ifdef WOLFSSL_SMALL_STACK
  347. XFREE(key_dig, ssl->heap, DYNAMIC_TYPE_DIGEST);
  348. #endif
  349. return ret;
  350. }
  351. static int _MakeTlsMasterSecret(byte* ms, word32 msLen,
  352. const byte* pms, word32 pmsLen,
  353. const byte* cr, const byte* sr,
  354. int tls1_2, int hash_type,
  355. void* heap, int devId)
  356. {
  357. int ret;
  358. #if !defined(WOLFSSL_ASYNC_CRYPT) || defined(WC_ASYNC_NO_HASH)
  359. byte seed[SEED_LEN];
  360. #else
  361. WC_DECLARE_VAR(seed, byte, SEED_LEN, heap);
  362. if (seed == NULL)
  363. return MEMORY_E;
  364. #endif
  365. XMEMCPY(seed, cr, RAN_LEN);
  366. XMEMCPY(seed + RAN_LEN, sr, RAN_LEN);
  367. #ifdef WOLFSSL_HAVE_PRF
  368. PRIVATE_KEY_UNLOCK();
  369. ret = wc_PRF_TLS(ms, msLen, pms, pmsLen, master_label, MASTER_LABEL_SZ,
  370. seed, SEED_LEN, tls1_2, hash_type, heap, devId);
  371. PRIVATE_KEY_LOCK();
  372. #else
  373. /* Pseudo random function must be enabled in the configuration. */
  374. ret = PRF_MISSING;
  375. WOLFSSL_MSG("Pseudo-random function is not enabled");
  376. (void)ms;
  377. (void)msLen;
  378. (void)pms;
  379. (void)pmsLen;
  380. (void)tls1_2;
  381. (void)hash_type;
  382. (void)heap;
  383. (void)devId;
  384. #endif
  385. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  386. WC_FREE_VAR(seed, heap);
  387. #endif
  388. return ret;
  389. }
  390. /* External facing wrapper so user can call as well, 0 on success */
  391. int wolfSSL_MakeTlsMasterSecret(byte* ms, word32 msLen,
  392. const byte* pms, word32 pmsLen,
  393. const byte* cr, const byte* sr,
  394. int tls1_2, int hash_type)
  395. {
  396. return _MakeTlsMasterSecret(ms, msLen, pms, pmsLen, cr, sr, tls1_2,
  397. hash_type, NULL, INVALID_DEVID);
  398. }
  399. #ifdef HAVE_EXTENDED_MASTER
  400. static int _MakeTlsExtendedMasterSecret(byte* ms, word32 msLen,
  401. const byte* pms, word32 pmsLen,
  402. const byte* sHash, word32 sHashLen,
  403. int tls1_2, int hash_type,
  404. void* heap, int devId)
  405. {
  406. int ret;
  407. #ifdef WOLFSSL_HAVE_PRF
  408. PRIVATE_KEY_UNLOCK();
  409. ret = wc_PRF_TLS(ms, msLen, pms, pmsLen, ext_master_label, EXT_MASTER_LABEL_SZ,
  410. sHash, sHashLen, tls1_2, hash_type, heap, devId);
  411. PRIVATE_KEY_LOCK();
  412. #else
  413. /* Pseudo random function must be enabled in the configuration. */
  414. ret = PRF_MISSING;
  415. WOLFSSL_MSG("Pseudo-random function is not enabled");
  416. (void)ms;
  417. (void)msLen;
  418. (void)pms;
  419. (void)pmsLen;
  420. (void)sHash;
  421. (void)sHashLen;
  422. (void)tls1_2;
  423. (void)hash_type;
  424. (void)heap;
  425. (void)devId;
  426. #endif
  427. return ret;
  428. }
  429. /* External facing wrapper so user can call as well, 0 on success */
  430. int wolfSSL_MakeTlsExtendedMasterSecret(byte* ms, word32 msLen,
  431. const byte* pms, word32 pmsLen,
  432. const byte* sHash, word32 sHashLen,
  433. int tls1_2, int hash_type)
  434. {
  435. return _MakeTlsExtendedMasterSecret(ms, msLen, pms, pmsLen, sHash, sHashLen,
  436. tls1_2, hash_type, NULL, INVALID_DEVID);
  437. }
  438. #endif /* HAVE_EXTENDED_MASTER */
  439. int MakeTlsMasterSecret(WOLFSSL* ssl)
  440. {
  441. int ret;
  442. #ifdef HAVE_EXTENDED_MASTER
  443. if (ssl->options.haveEMS) {
  444. word32 hashSz = HSHASH_SZ;
  445. #ifdef WOLFSSL_SMALL_STACK
  446. byte* handshake_hash = (byte*)XMALLOC(HSHASH_SZ, ssl->heap,
  447. DYNAMIC_TYPE_DIGEST);
  448. if (handshake_hash == NULL)
  449. return MEMORY_E;
  450. #else
  451. byte handshake_hash[HSHASH_SZ];
  452. #endif
  453. ret = BuildTlsHandshakeHash(ssl, handshake_hash, &hashSz);
  454. if (ret == 0) {
  455. ret = _MakeTlsExtendedMasterSecret(
  456. ssl->arrays->masterSecret, SECRET_LEN,
  457. ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz,
  458. handshake_hash, hashSz,
  459. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  460. ssl->heap, ssl->devId);
  461. ForceZero(handshake_hash, hashSz);
  462. }
  463. #ifdef WOLFSSL_SMALL_STACK
  464. XFREE(handshake_hash, ssl->heap, DYNAMIC_TYPE_DIGEST);
  465. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  466. wc_MemZero_Check(handshake_hash, HSHASH_SZ);
  467. #endif
  468. }
  469. else
  470. #endif /* HAVE_EXTENDED_MASTER */
  471. {
  472. #if !defined(NO_CERTS) && defined(HAVE_PK_CALLBACKS)
  473. ret = PROTOCOLCB_UNAVAILABLE;
  474. if (ssl->ctx->GenMasterCb) {
  475. void* ctx = wolfSSL_GetGenMasterSecretCtx(ssl);
  476. ret = ssl->ctx->GenMasterCb(ssl, ctx);
  477. }
  478. if (!ssl->ctx->GenMasterCb || ret == PROTOCOLCB_UNAVAILABLE)
  479. #endif
  480. {
  481. ret = _MakeTlsMasterSecret(ssl->arrays->masterSecret,
  482. SECRET_LEN, ssl->arrays->preMasterSecret,
  483. ssl->arrays->preMasterSz, ssl->arrays->clientRandom,
  484. ssl->arrays->serverRandom, IsAtLeastTLSv1_2(ssl),
  485. ssl->specs.mac_algorithm, ssl->heap, ssl->devId);
  486. }
  487. }
  488. if (ret == 0) {
  489. #ifdef SHOW_SECRETS
  490. /* Wireshark Pre-Master-Secret Format:
  491. * CLIENT_RANDOM <clientrandom> <mastersecret>
  492. */
  493. const char* CLIENT_RANDOM_LABEL = "CLIENT_RANDOM";
  494. int i, pmsPos = 0;
  495. char pmsBuf[13 + 1 + 64 + 1 + 96 + 1 + 1];
  496. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%s ",
  497. CLIENT_RANDOM_LABEL);
  498. pmsPos += XSTRLEN(CLIENT_RANDOM_LABEL) + 1;
  499. for (i = 0; i < RAN_LEN; i++) {
  500. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%02x",
  501. ssl->arrays->clientRandom[i]);
  502. pmsPos += 2;
  503. }
  504. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, " ");
  505. pmsPos += 1;
  506. for (i = 0; i < SECRET_LEN; i++) {
  507. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%02x",
  508. ssl->arrays->masterSecret[i]);
  509. pmsPos += 2;
  510. }
  511. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "\n");
  512. pmsPos += 1;
  513. /* print master secret */
  514. puts(pmsBuf);
  515. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_SSLKEYLOGFILE)
  516. {
  517. FILE* f = XFOPEN(WOLFSSL_SSLKEYLOGFILE_OUTPUT, "a");
  518. if (f != XBADFILE) {
  519. XFWRITE(pmsBuf, 1, pmsPos, f);
  520. XFCLOSE(f);
  521. }
  522. }
  523. #endif
  524. #endif /* SHOW_SECRETS */
  525. ret = DeriveTlsKeys(ssl);
  526. }
  527. return ret;
  528. }
  529. /* Used by EAP-TLS and EAP-TTLS to derive keying material from
  530. * the master_secret. */
  531. int wolfSSL_make_eap_keys(WOLFSSL* ssl, void* msk, unsigned int len,
  532. const char* label)
  533. {
  534. int ret;
  535. #ifdef WOLFSSL_SMALL_STACK
  536. byte* seed;
  537. #else
  538. byte seed[SEED_LEN];
  539. #endif
  540. #ifdef WOLFSSL_SMALL_STACK
  541. seed = (byte*)XMALLOC(SEED_LEN, ssl->heap, DYNAMIC_TYPE_SEED);
  542. if (seed == NULL)
  543. return MEMORY_E;
  544. #endif
  545. /*
  546. * As per RFC-5281, the order of the client and server randoms is reversed
  547. * from that used by the TLS protocol to derive keys.
  548. */
  549. XMEMCPY(seed, ssl->arrays->clientRandom, RAN_LEN);
  550. XMEMCPY(seed + RAN_LEN, ssl->arrays->serverRandom, RAN_LEN);
  551. #ifdef WOLFSSL_HAVE_PRF
  552. PRIVATE_KEY_UNLOCK();
  553. ret = wc_PRF_TLS((byte*)msk, len, ssl->arrays->masterSecret, SECRET_LEN,
  554. (const byte *)label, (word32)XSTRLEN(label), seed, SEED_LEN,
  555. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  556. ssl->heap, ssl->devId);
  557. PRIVATE_KEY_LOCK();
  558. #else
  559. /* Pseudo random function must be enabled in the configuration. */
  560. ret = PRF_MISSING;
  561. WOLFSSL_MSG("Pseudo-random function is not enabled");
  562. (void)msk;
  563. (void)len;
  564. (void)label;
  565. #endif
  566. #ifdef WOLFSSL_SMALL_STACK
  567. XFREE(seed, ssl->heap, DYNAMIC_TYPE_SEED);
  568. #endif
  569. return ret;
  570. }
  571. int wolfSSL_GetHmacType_ex(CipherSpecs* specs)
  572. {
  573. if (specs == NULL)
  574. return BAD_FUNC_ARG;
  575. switch (specs->mac_algorithm) {
  576. #ifndef NO_MD5
  577. case md5_mac:
  578. {
  579. return WC_MD5;
  580. }
  581. #endif
  582. #ifndef NO_SHA256
  583. case sha256_mac:
  584. {
  585. return WC_SHA256;
  586. }
  587. #endif
  588. #ifdef WOLFSSL_SHA384
  589. case sha384_mac:
  590. {
  591. return WC_SHA384;
  592. }
  593. #endif
  594. #ifndef NO_SHA
  595. case sha_mac:
  596. {
  597. return WC_SHA;
  598. }
  599. #endif
  600. #ifdef HAVE_BLAKE2
  601. case blake2b_mac:
  602. {
  603. return BLAKE2B_ID;
  604. }
  605. #endif
  606. default:
  607. {
  608. return WOLFSSL_FATAL_ERROR;
  609. }
  610. }
  611. }
  612. /* return HMAC digest type in wolfSSL format */
  613. int wolfSSL_GetHmacType(WOLFSSL* ssl)
  614. {
  615. if (ssl == NULL)
  616. return BAD_FUNC_ARG;
  617. return wolfSSL_GetHmacType_ex(&ssl->specs);
  618. }
  619. int wolfSSL_SetTlsHmacInner(WOLFSSL* ssl, byte* inner, word32 sz, int content,
  620. int verify)
  621. {
  622. if (ssl == NULL || inner == NULL)
  623. return BAD_FUNC_ARG;
  624. XMEMSET(inner, 0, WOLFSSL_TLS_HMAC_INNER_SZ);
  625. WriteSEQ(ssl, verify, inner);
  626. inner[SEQ_SZ] = (byte)content;
  627. inner[SEQ_SZ + ENUM_LEN] = ssl->version.major;
  628. inner[SEQ_SZ + ENUM_LEN + ENUM_LEN] = ssl->version.minor;
  629. c16toa((word16)sz, inner + SEQ_SZ + ENUM_LEN + VERSION_SZ);
  630. return 0;
  631. }
  632. #ifndef WOLFSSL_AEAD_ONLY
  633. #if !defined(WOLFSSL_NO_HASH_RAW) && !defined(HAVE_FIPS) && \
  634. !defined(HAVE_SELFTEST)
  635. /* Update the hash in the HMAC.
  636. *
  637. * hmac HMAC object.
  638. * data Data to be hashed.
  639. * sz Size of data to hash.
  640. * returns 0 on success, otherwise failure.
  641. */
  642. static int Hmac_HashUpdate(Hmac* hmac, const byte* data, word32 sz)
  643. {
  644. int ret = BAD_FUNC_ARG;
  645. switch (hmac->macType) {
  646. #ifndef NO_SHA
  647. case WC_SHA:
  648. ret = wc_ShaUpdate(&hmac->hash.sha, data, sz);
  649. break;
  650. #endif /* !NO_SHA */
  651. #ifndef NO_SHA256
  652. case WC_SHA256:
  653. ret = wc_Sha256Update(&hmac->hash.sha256, data, sz);
  654. break;
  655. #endif /* !NO_SHA256 */
  656. #ifdef WOLFSSL_SHA384
  657. case WC_SHA384:
  658. ret = wc_Sha384Update(&hmac->hash.sha384, data, sz);
  659. break;
  660. #endif /* WOLFSSL_SHA384 */
  661. #ifdef WOLFSSL_SHA512
  662. case WC_SHA512:
  663. ret = wc_Sha512Update(&hmac->hash.sha512, data, sz);
  664. break;
  665. #endif /* WOLFSSL_SHA512 */
  666. default:
  667. break;
  668. }
  669. return ret;
  670. }
  671. /* Finalize the hash but don't put the EOC, padding or length in.
  672. *
  673. * hmac HMAC object.
  674. * hash Hash result.
  675. * returns 0 on success, otherwise failure.
  676. */
  677. static int Hmac_HashFinalRaw(Hmac* hmac, unsigned char* hash)
  678. {
  679. int ret = BAD_FUNC_ARG;
  680. switch (hmac->macType) {
  681. #ifndef NO_SHA
  682. case WC_SHA:
  683. ret = wc_ShaFinalRaw(&hmac->hash.sha, hash);
  684. break;
  685. #endif /* !NO_SHA */
  686. #ifndef NO_SHA256
  687. case WC_SHA256:
  688. ret = wc_Sha256FinalRaw(&hmac->hash.sha256, hash);
  689. break;
  690. #endif /* !NO_SHA256 */
  691. #ifdef WOLFSSL_SHA384
  692. case WC_SHA384:
  693. ret = wc_Sha384FinalRaw(&hmac->hash.sha384, hash);
  694. break;
  695. #endif /* WOLFSSL_SHA384 */
  696. #ifdef WOLFSSL_SHA512
  697. case WC_SHA512:
  698. ret = wc_Sha512FinalRaw(&hmac->hash.sha512, hash);
  699. break;
  700. #endif /* WOLFSSL_SHA512 */
  701. default:
  702. break;
  703. }
  704. return ret;
  705. }
  706. /* Finalize the HMAC by performing outer hash.
  707. *
  708. * hmac HMAC object.
  709. * mac MAC result.
  710. * returns 0 on success, otherwise failure.
  711. */
  712. static int Hmac_OuterHash(Hmac* hmac, unsigned char* mac)
  713. {
  714. int ret = BAD_FUNC_ARG;
  715. wc_HashAlg hash;
  716. enum wc_HashType hashType = (enum wc_HashType)hmac->macType;
  717. int digestSz = wc_HashGetDigestSize(hashType);
  718. int blockSz = wc_HashGetBlockSize(hashType);
  719. if ((digestSz >= 0) && (blockSz >= 0)) {
  720. ret = wc_HashInit(&hash, hashType);
  721. }
  722. if (ret == 0) {
  723. ret = wc_HashUpdate(&hash, hashType, (byte*)hmac->opad,
  724. blockSz);
  725. if (ret == 0)
  726. ret = wc_HashUpdate(&hash, hashType, (byte*)hmac->innerHash,
  727. digestSz);
  728. if (ret == 0)
  729. ret = wc_HashFinal(&hash, hashType, mac);
  730. wc_HashFree(&hash, hashType);
  731. }
  732. return ret;
  733. }
  734. /* Calculate the HMAC of the header + message data.
  735. * Constant time implementation using wc_Sha*FinalRaw().
  736. *
  737. * hmac HMAC object.
  738. * digest MAC result.
  739. * in Message data.
  740. * sz Size of the message data.
  741. * header Constructed record header with length of handshake data.
  742. * returns 0 on success, otherwise failure.
  743. */
  744. static int Hmac_UpdateFinal_CT(Hmac* hmac, byte* digest, const byte* in,
  745. word32 sz, int macLen, byte* header)
  746. {
  747. byte lenBytes[8];
  748. int i, j;
  749. unsigned int k;
  750. int blockBits, blockMask;
  751. int lastBlockLen, extraLen, eocIndex;
  752. int blocks, safeBlocks, lenBlock, eocBlock;
  753. unsigned int maxLen;
  754. int blockSz, padSz;
  755. int ret;
  756. word32 realLen;
  757. byte extraBlock;
  758. switch (hmac->macType) {
  759. #ifndef NO_SHA
  760. case WC_SHA:
  761. blockSz = WC_SHA_BLOCK_SIZE;
  762. blockBits = 6;
  763. padSz = WC_SHA_BLOCK_SIZE - WC_SHA_PAD_SIZE + 1;
  764. break;
  765. #endif /* !NO_SHA */
  766. #ifndef NO_SHA256
  767. case WC_SHA256:
  768. blockSz = WC_SHA256_BLOCK_SIZE;
  769. blockBits = 6;
  770. padSz = WC_SHA256_BLOCK_SIZE - WC_SHA256_PAD_SIZE + 1;
  771. break;
  772. #endif /* !NO_SHA256 */
  773. #ifdef WOLFSSL_SHA384
  774. case WC_SHA384:
  775. blockSz = WC_SHA384_BLOCK_SIZE;
  776. blockBits = 7;
  777. padSz = WC_SHA384_BLOCK_SIZE - WC_SHA384_PAD_SIZE + 1;
  778. break;
  779. #endif /* WOLFSSL_SHA384 */
  780. #ifdef WOLFSSL_SHA512
  781. case WC_SHA512:
  782. blockSz = WC_SHA512_BLOCK_SIZE;
  783. blockBits = 7;
  784. padSz = WC_SHA512_BLOCK_SIZE - WC_SHA512_PAD_SIZE + 1;
  785. break;
  786. #endif /* WOLFSSL_SHA512 */
  787. default:
  788. return BAD_FUNC_ARG;
  789. }
  790. blockMask = blockSz - 1;
  791. /* Size of data to HMAC if padding length byte is zero. */
  792. maxLen = WOLFSSL_TLS_HMAC_INNER_SZ + sz - 1 - macLen;
  793. /* Complete data (including padding) has block for EOC and/or length. */
  794. extraBlock = ctSetLTE((maxLen + padSz) & blockMask, padSz);
  795. /* Total number of blocks for data including padding. */
  796. blocks = ((maxLen + blockSz - 1) >> blockBits) + extraBlock;
  797. /* Up to last 6 blocks can be hashed safely. */
  798. safeBlocks = blocks - 6;
  799. /* Length of message data. */
  800. realLen = maxLen - in[sz - 1];
  801. /* Number of message bytes in last block. */
  802. lastBlockLen = realLen & blockMask;
  803. /* Number of padding bytes in last block. */
  804. extraLen = ((blockSz * 2 - padSz - lastBlockLen) & blockMask) + 1;
  805. /* Number of blocks to create for hash. */
  806. lenBlock = (realLen + extraLen) >> blockBits;
  807. /* Block containing EOC byte. */
  808. eocBlock = realLen >> blockBits;
  809. /* Index of EOC byte in block. */
  810. eocIndex = realLen & blockMask;
  811. /* Add length of hmac's ipad to total length. */
  812. realLen += blockSz;
  813. /* Length as bits - 8 bytes bigendian. */
  814. c32toa(realLen >> ((sizeof(word32) * 8) - 3), lenBytes);
  815. c32toa(realLen << 3, lenBytes + sizeof(word32));
  816. ret = Hmac_HashUpdate(hmac, (unsigned char*)hmac->ipad, blockSz);
  817. if (ret != 0)
  818. return ret;
  819. XMEMSET(hmac->innerHash, 0, macLen);
  820. if (safeBlocks > 0) {
  821. ret = Hmac_HashUpdate(hmac, header, WOLFSSL_TLS_HMAC_INNER_SZ);
  822. if (ret != 0)
  823. return ret;
  824. ret = Hmac_HashUpdate(hmac, in, safeBlocks * blockSz -
  825. WOLFSSL_TLS_HMAC_INNER_SZ);
  826. if (ret != 0)
  827. return ret;
  828. }
  829. else
  830. safeBlocks = 0;
  831. XMEMSET(digest, 0, macLen);
  832. k = safeBlocks * blockSz;
  833. for (i = safeBlocks; i < blocks; i++) {
  834. unsigned char hashBlock[WC_MAX_BLOCK_SIZE];
  835. unsigned char isEocBlock = ctMaskEq(i, eocBlock);
  836. unsigned char isOutBlock = ctMaskEq(i, lenBlock);
  837. for (j = 0; j < blockSz; j++) {
  838. unsigned char atEoc = ctMaskEq(j, eocIndex) & isEocBlock;
  839. unsigned char pastEoc = ctMaskGT(j, eocIndex) & isEocBlock;
  840. unsigned char b = 0;
  841. if (k < WOLFSSL_TLS_HMAC_INNER_SZ)
  842. b = header[k];
  843. else if (k < maxLen)
  844. b = in[k - WOLFSSL_TLS_HMAC_INNER_SZ];
  845. k++;
  846. b = ctMaskSel(atEoc, 0x80, b);
  847. b &= (unsigned char)~(word32)pastEoc;
  848. b &= ((unsigned char)~(word32)isOutBlock) | isEocBlock;
  849. if (j >= blockSz - 8) {
  850. b = ctMaskSel(isOutBlock, lenBytes[j - (blockSz - 8)], b);
  851. }
  852. hashBlock[j] = b;
  853. }
  854. ret = Hmac_HashUpdate(hmac, hashBlock, blockSz);
  855. if (ret != 0)
  856. return ret;
  857. ret = Hmac_HashFinalRaw(hmac, hashBlock);
  858. if (ret != 0)
  859. return ret;
  860. for (j = 0; j < macLen; j++)
  861. ((unsigned char*)hmac->innerHash)[j] |= hashBlock[j] & isOutBlock;
  862. }
  863. ret = Hmac_OuterHash(hmac, digest);
  864. return ret;
  865. }
  866. #endif
  867. #if defined(WOLFSSL_NO_HASH_RAW) || defined(HAVE_FIPS) || \
  868. defined(HAVE_SELFTEST) || defined(HAVE_BLAKE2)
  869. /* Calculate the HMAC of the header + message data.
  870. * Constant time implementation using normal hashing operations.
  871. * Update-Final need to be constant time.
  872. *
  873. * hmac HMAC object.
  874. * digest MAC result.
  875. * in Message data.
  876. * sz Size of the message data.
  877. * header Constructed record header with length of handshake data.
  878. * returns 0 on success, otherwise failure.
  879. */
  880. static int Hmac_UpdateFinal(Hmac* hmac, byte* digest, const byte* in,
  881. word32 sz, byte* header)
  882. {
  883. byte dummy[WC_MAX_BLOCK_SIZE] = {0};
  884. int ret;
  885. word32 msgSz, blockSz, macSz, padSz, maxSz, realSz;
  886. word32 offset = 0;
  887. int msgBlocks, blocks, blockBits;
  888. int i;
  889. switch (hmac->macType) {
  890. #ifndef NO_SHA
  891. case WC_SHA:
  892. blockSz = WC_SHA_BLOCK_SIZE;
  893. blockBits = 6;
  894. macSz = WC_SHA_DIGEST_SIZE;
  895. padSz = WC_SHA_BLOCK_SIZE - WC_SHA_PAD_SIZE + 1;
  896. break;
  897. #endif /* !NO_SHA */
  898. #ifndef NO_SHA256
  899. case WC_SHA256:
  900. blockSz = WC_SHA256_BLOCK_SIZE;
  901. blockBits = 6;
  902. macSz = WC_SHA256_DIGEST_SIZE;
  903. padSz = WC_SHA256_BLOCK_SIZE - WC_SHA256_PAD_SIZE + 1;
  904. break;
  905. #endif /* !NO_SHA256 */
  906. #ifdef WOLFSSL_SHA384
  907. case WC_SHA384:
  908. blockSz = WC_SHA384_BLOCK_SIZE;
  909. blockBits = 7;
  910. macSz = WC_SHA384_DIGEST_SIZE;
  911. padSz = WC_SHA384_BLOCK_SIZE - WC_SHA384_PAD_SIZE + 1;
  912. break;
  913. #endif /* WOLFSSL_SHA384 */
  914. #ifdef WOLFSSL_SHA512
  915. case WC_SHA512:
  916. blockSz = WC_SHA512_BLOCK_SIZE;
  917. blockBits = 7;
  918. macSz = WC_SHA512_DIGEST_SIZE;
  919. padSz = WC_SHA512_BLOCK_SIZE - WC_SHA512_PAD_SIZE + 1;
  920. break;
  921. #endif /* WOLFSSL_SHA512 */
  922. #ifdef HAVE_BLAKE2
  923. case WC_HASH_TYPE_BLAKE2B:
  924. blockSz = BLAKE2B_BLOCKBYTES;
  925. blockBits = 7;
  926. macSz = BLAKE2B_256;
  927. padSz = 0;
  928. break;
  929. #endif /* HAVE_BLAKE2 */
  930. default:
  931. return BAD_FUNC_ARG;
  932. }
  933. msgSz = sz - (1 + in[sz - 1] + macSz);
  934. /* Make negative result 0 */
  935. msgSz &= ~(0 - (msgSz >> 31));
  936. realSz = WOLFSSL_TLS_HMAC_INNER_SZ + msgSz;
  937. maxSz = WOLFSSL_TLS_HMAC_INNER_SZ + (sz - 1) - macSz;
  938. /* Calculate #blocks processed in HMAC for max and real data. */
  939. blocks = maxSz >> blockBits;
  940. blocks += ((maxSz + padSz) % blockSz) < padSz;
  941. msgBlocks = realSz >> blockBits;
  942. /* #Extra blocks to process. */
  943. blocks -= msgBlocks + ((((realSz + padSz) % blockSz) < padSz) ? 1 : 0);
  944. /* Calculate whole blocks. */
  945. msgBlocks--;
  946. ret = wc_HmacUpdate(hmac, header, WOLFSSL_TLS_HMAC_INNER_SZ);
  947. if (ret == 0) {
  948. /* Fill the rest of the block with any available data. */
  949. word32 currSz = ctMaskLT(msgSz, blockSz) & msgSz;
  950. currSz |= ctMaskGTE(msgSz, blockSz) & blockSz;
  951. currSz -= WOLFSSL_TLS_HMAC_INNER_SZ;
  952. currSz &= ~(0 - (currSz >> 31));
  953. ret = wc_HmacUpdate(hmac, in, currSz);
  954. offset = currSz;
  955. }
  956. if (ret == 0) {
  957. /* Do the hash operations on a block basis. */
  958. for (i = 0; i < msgBlocks; i++, offset += blockSz) {
  959. ret = wc_HmacUpdate(hmac, in + offset, blockSz);
  960. if (ret != 0)
  961. break;
  962. }
  963. }
  964. if (ret == 0)
  965. ret = wc_HmacUpdate(hmac, in + offset, msgSz - offset);
  966. if (ret == 0)
  967. ret = wc_HmacFinal(hmac, digest);
  968. if (ret == 0) {
  969. /* Do the dummy hash operations. Do at least one. */
  970. for (i = 0; i < blocks + 1; i++) {
  971. ret = wc_HmacUpdate(hmac, dummy, blockSz);
  972. if (ret != 0)
  973. break;
  974. }
  975. }
  976. return ret;
  977. }
  978. #endif
  979. int TLS_hmac(WOLFSSL* ssl, byte* digest, const byte* in, word32 sz, int padSz,
  980. int content, int verify, int epochOrder)
  981. {
  982. Hmac hmac;
  983. byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
  984. int ret = 0;
  985. const byte* macSecret = NULL;
  986. word32 hashSz = 0;
  987. if (ssl == NULL)
  988. return BAD_FUNC_ARG;
  989. #ifdef HAVE_TRUNCATED_HMAC
  990. hashSz = ssl->truncated_hmac ? (byte)TRUNCATED_HMAC_SZ
  991. : ssl->specs.hash_size;
  992. #else
  993. hashSz = ssl->specs.hash_size;
  994. #endif
  995. #ifdef HAVE_FUZZER
  996. /* Fuzz "in" buffer with sz to be used in HMAC algorithm */
  997. if (ssl->fuzzerCb) {
  998. if (verify && padSz >= 0) {
  999. ssl->fuzzerCb(ssl, in, sz + hashSz + padSz + 1, FUZZ_HMAC,
  1000. ssl->fuzzerCtx);
  1001. }
  1002. else {
  1003. ssl->fuzzerCb(ssl, in, sz, FUZZ_HMAC, ssl->fuzzerCtx);
  1004. }
  1005. }
  1006. #endif
  1007. if (!ssl->options.dtls)
  1008. wolfSSL_SetTlsHmacInner(ssl, myInner, sz, content, verify);
  1009. else
  1010. wolfSSL_SetTlsHmacInner(ssl, myInner, sz, content, epochOrder);
  1011. ret = wc_HmacInit(&hmac, ssl->heap, ssl->devId);
  1012. if (ret != 0)
  1013. return ret;
  1014. #ifdef WOLFSSL_DTLS
  1015. if (ssl->options.dtls)
  1016. macSecret = wolfSSL_GetDtlsMacSecret(ssl, verify, epochOrder);
  1017. else
  1018. macSecret = wolfSSL_GetMacSecret(ssl, verify);
  1019. #else
  1020. macSecret = wolfSSL_GetMacSecret(ssl, verify);
  1021. #endif
  1022. ret = wc_HmacSetKey(&hmac, wolfSSL_GetHmacType(ssl),
  1023. macSecret,
  1024. ssl->specs.hash_size);
  1025. if (ret == 0) {
  1026. /* Constant time verification required. */
  1027. if (verify && padSz >= 0) {
  1028. #if !defined(WOLFSSL_NO_HASH_RAW) && !defined(HAVE_FIPS) && \
  1029. !defined(HAVE_SELFTEST)
  1030. #ifdef HAVE_BLAKE2
  1031. if (wolfSSL_GetHmacType(ssl) == WC_HASH_TYPE_BLAKE2B) {
  1032. ret = Hmac_UpdateFinal(&hmac, digest, in,
  1033. sz + hashSz + padSz + 1, myInner);
  1034. }
  1035. else
  1036. #endif
  1037. {
  1038. ret = Hmac_UpdateFinal_CT(&hmac, digest, in,
  1039. sz + hashSz + padSz + 1, hashSz, myInner);
  1040. }
  1041. #else
  1042. ret = Hmac_UpdateFinal(&hmac, digest, in, sz + hashSz + padSz + 1,
  1043. myInner);
  1044. #endif
  1045. }
  1046. else {
  1047. ret = wc_HmacUpdate(&hmac, myInner, sizeof(myInner));
  1048. if (ret == 0)
  1049. ret = wc_HmacUpdate(&hmac, in, sz); /* content */
  1050. if (ret == 0)
  1051. ret = wc_HmacFinal(&hmac, digest);
  1052. }
  1053. }
  1054. wc_HmacFree(&hmac);
  1055. return ret;
  1056. }
  1057. #endif /* WOLFSSL_AEAD_ONLY */
  1058. #endif /* !WOLFSSL_NO_TLS12 */
  1059. #ifdef HAVE_TLS_EXTENSIONS
  1060. /**
  1061. * The TLSX semaphore is used to calculate the size of the extensions to be sent
  1062. * from one peer to another.
  1063. */
  1064. /** Supports up to 72 flags. Increase as needed. */
  1065. #define SEMAPHORE_SIZE 9
  1066. /**
  1067. * Converts the extension type (id) to an index in the semaphore.
  1068. *
  1069. * Official reference for TLS extension types:
  1070. * http://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xml
  1071. *
  1072. * Motivation:
  1073. * Previously, we used the extension type itself as the index of that
  1074. * extension in the semaphore as the extension types were declared
  1075. * sequentially, but maintain a semaphore as big as the number of available
  1076. * extensions is no longer an option since the release of renegotiation_info.
  1077. *
  1078. * How to update:
  1079. * Assign extension types that extrapolate the number of available semaphores
  1080. * to the first available index going backwards in the semaphore array.
  1081. * When adding a new extension type that don't extrapolate the number of
  1082. * available semaphores, check for a possible collision with with a
  1083. * 'remapped' extension type.
  1084. *
  1085. * Update TLSX_Parse for duplicate detection if more added above 62.
  1086. */
  1087. static WC_INLINE word16 TLSX_ToSemaphore(word16 type)
  1088. {
  1089. switch (type) {
  1090. case TLSX_RENEGOTIATION_INFO: /* 0xFF01 */
  1091. return 63;
  1092. #ifdef WOLFSSL_QUIC
  1093. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT: /* 0xffa5 */
  1094. return 64;
  1095. #endif
  1096. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  1097. case TLSX_ECH: /* 0xfe0d */
  1098. return 65;
  1099. #endif
  1100. default:
  1101. if (type > 62) {
  1102. /* This message SHOULD only happens during the adding of
  1103. new TLS extensions in which its IANA number overflows
  1104. the current semaphore's range, or if its number already
  1105. is assigned to be used by another extension.
  1106. Use this check value for the new extension and decrement
  1107. the check value by one. */
  1108. WOLFSSL_MSG("### TLSX semaphore collision or overflow detected!");
  1109. }
  1110. }
  1111. return type;
  1112. }
  1113. /** Checks if a specific light (tls extension) is not set in the semaphore. */
  1114. #define IS_OFF(semaphore, light) \
  1115. (!(((semaphore)[(light) / 8] & (byte) (0x01 << ((light) % 8)))))
  1116. /** Turn on a specific light (tls extension) in the semaphore. */
  1117. /* the semaphore marks the extensions already written to the message */
  1118. #define TURN_ON(semaphore, light) \
  1119. ((semaphore)[(light) / 8] |= (byte) (0x01 << ((light) % 8)))
  1120. /** Turn off a specific light (tls extension) in the semaphore. */
  1121. #define TURN_OFF(semaphore, light) \
  1122. ((semaphore)[(light) / 8] &= (byte) ~(0x01 << ((light) % 8)))
  1123. /** Creates a new extension. */
  1124. static TLSX* TLSX_New(TLSX_Type type, const void* data, void* heap)
  1125. {
  1126. TLSX* extension = (TLSX*)XMALLOC(sizeof(TLSX), heap, DYNAMIC_TYPE_TLSX);
  1127. (void)heap;
  1128. if (extension) {
  1129. extension->type = type;
  1130. extension->data = (void*)data;
  1131. extension->resp = 0;
  1132. extension->next = NULL;
  1133. }
  1134. return extension;
  1135. }
  1136. /**
  1137. * Creates a new extension and appends it to the provided list.
  1138. * Checks for duplicate extensions, keeps the newest.
  1139. */
  1140. int TLSX_Append(TLSX** list, TLSX_Type type, const void* data, void* heap)
  1141. {
  1142. TLSX* extension = TLSX_New(type, data, heap);
  1143. TLSX* cur;
  1144. TLSX** prevNext = list;
  1145. if (extension == NULL)
  1146. return MEMORY_E;
  1147. for (cur = *list; cur != NULL;) {
  1148. if (cur->type == type) {
  1149. *prevNext = cur->next;
  1150. cur->next = NULL;
  1151. TLSX_FreeAll(cur, heap);
  1152. cur = *prevNext;
  1153. }
  1154. else {
  1155. prevNext = &cur->next;
  1156. cur = cur->next;
  1157. }
  1158. }
  1159. /* Append the extension to the list */
  1160. *prevNext = extension;
  1161. return 0;
  1162. }
  1163. /**
  1164. * Creates a new extension and pushes it to the provided list.
  1165. * Checks for duplicate extensions, keeps the newest.
  1166. */
  1167. int TLSX_Push(TLSX** list, TLSX_Type type, const void* data, void* heap)
  1168. {
  1169. TLSX* extension = TLSX_New(type, data, heap);
  1170. if (extension == NULL)
  1171. return MEMORY_E;
  1172. /* pushes the new extension on the list. */
  1173. extension->next = *list;
  1174. *list = extension;
  1175. /* remove duplicate extensions, there should be only one of each type. */
  1176. do {
  1177. if (extension->next && extension->next->type == type) {
  1178. TLSX *next = extension->next;
  1179. extension->next = next->next;
  1180. next->next = NULL;
  1181. TLSX_FreeAll(next, heap);
  1182. /* there is no way to occur more than
  1183. * two extensions of the same type.
  1184. */
  1185. break;
  1186. }
  1187. } while ((extension = extension->next));
  1188. return 0;
  1189. }
  1190. #ifndef NO_WOLFSSL_CLIENT
  1191. int TLSX_CheckUnsupportedExtension(WOLFSSL* ssl, TLSX_Type type);
  1192. int TLSX_CheckUnsupportedExtension(WOLFSSL* ssl, TLSX_Type type)
  1193. {
  1194. TLSX *extension = TLSX_Find(ssl->extensions, type);
  1195. if (!extension)
  1196. extension = TLSX_Find(ssl->ctx->extensions, type);
  1197. return extension == NULL;
  1198. }
  1199. int TLSX_HandleUnsupportedExtension(WOLFSSL* ssl);
  1200. int TLSX_HandleUnsupportedExtension(WOLFSSL* ssl)
  1201. {
  1202. SendAlert(ssl, alert_fatal, unsupported_extension);
  1203. WOLFSSL_ERROR_VERBOSE(UNSUPPORTED_EXTENSION);
  1204. return UNSUPPORTED_EXTENSION;
  1205. }
  1206. #else
  1207. #define TLSX_CheckUnsupportedExtension(ssl, type) 0
  1208. #define TLSX_HandleUnsupportedExtension(ssl) 0
  1209. #endif
  1210. #if !defined(NO_WOLFSSL_SERVER) || defined(WOLFSSL_TLS13)
  1211. void TLSX_SetResponse(WOLFSSL* ssl, TLSX_Type type);
  1212. /** Mark an extension to be sent back to the client. */
  1213. void TLSX_SetResponse(WOLFSSL* ssl, TLSX_Type type)
  1214. {
  1215. TLSX *extension = TLSX_Find(ssl->extensions, type);
  1216. if (extension)
  1217. extension->resp = 1;
  1218. }
  1219. #endif
  1220. /******************************************************************************/
  1221. /* Application-Layer Protocol Negotiation */
  1222. /******************************************************************************/
  1223. #ifdef HAVE_ALPN
  1224. /** Creates a new ALPN object, providing protocol name to use. */
  1225. static ALPN* TLSX_ALPN_New(char *protocol_name, word16 protocol_nameSz,
  1226. void* heap)
  1227. {
  1228. ALPN *alpn;
  1229. WOLFSSL_ENTER("TLSX_ALPN_New");
  1230. if (protocol_name == NULL ||
  1231. protocol_nameSz > WOLFSSL_MAX_ALPN_PROTO_NAME_LEN) {
  1232. WOLFSSL_MSG("Invalid arguments");
  1233. return NULL;
  1234. }
  1235. alpn = (ALPN*)XMALLOC(sizeof(ALPN), heap, DYNAMIC_TYPE_TLSX);
  1236. if (alpn == NULL) {
  1237. WOLFSSL_MSG("Memory failure");
  1238. return NULL;
  1239. }
  1240. alpn->next = NULL;
  1241. alpn->negotiated = 0;
  1242. alpn->options = 0;
  1243. alpn->protocol_name = (char*)XMALLOC(protocol_nameSz + 1,
  1244. heap, DYNAMIC_TYPE_TLSX);
  1245. if (alpn->protocol_name == NULL) {
  1246. WOLFSSL_MSG("Memory failure");
  1247. XFREE(alpn, heap, DYNAMIC_TYPE_TLSX);
  1248. return NULL;
  1249. }
  1250. XMEMCPY(alpn->protocol_name, protocol_name, protocol_nameSz);
  1251. alpn->protocol_name[protocol_nameSz] = 0;
  1252. (void)heap;
  1253. return alpn;
  1254. }
  1255. /** Releases an ALPN object. */
  1256. static void TLSX_ALPN_Free(ALPN *alpn, void* heap)
  1257. {
  1258. (void)heap;
  1259. if (alpn == NULL)
  1260. return;
  1261. XFREE(alpn->protocol_name, heap, DYNAMIC_TYPE_TLSX);
  1262. XFREE(alpn, heap, DYNAMIC_TYPE_TLSX);
  1263. }
  1264. /** Releases all ALPN objects in the provided list. */
  1265. static void TLSX_ALPN_FreeAll(ALPN *list, void* heap)
  1266. {
  1267. ALPN* alpn;
  1268. while ((alpn = list)) {
  1269. list = alpn->next;
  1270. TLSX_ALPN_Free(alpn, heap);
  1271. }
  1272. }
  1273. /** Tells the buffered size of the ALPN objects in a list. */
  1274. static word16 TLSX_ALPN_GetSize(ALPN *list)
  1275. {
  1276. ALPN* alpn;
  1277. word16 length = OPAQUE16_LEN; /* list length */
  1278. while ((alpn = list)) {
  1279. list = alpn->next;
  1280. length++; /* protocol name length is on one byte */
  1281. length += (word16)XSTRLEN(alpn->protocol_name);
  1282. }
  1283. return length;
  1284. }
  1285. /** Writes the ALPN objects of a list in a buffer. */
  1286. static word16 TLSX_ALPN_Write(ALPN *list, byte *output)
  1287. {
  1288. ALPN* alpn;
  1289. word16 length = 0;
  1290. word16 offset = OPAQUE16_LEN; /* list length offset */
  1291. while ((alpn = list)) {
  1292. list = alpn->next;
  1293. length = (word16)XSTRLEN(alpn->protocol_name);
  1294. /* protocol name length */
  1295. output[offset++] = (byte)length;
  1296. /* protocol name value */
  1297. XMEMCPY(output + offset, alpn->protocol_name, length);
  1298. offset += length;
  1299. }
  1300. /* writing list length */
  1301. c16toa(offset - OPAQUE16_LEN, output);
  1302. return offset;
  1303. }
  1304. /** Finds a protocol name in the provided ALPN list */
  1305. static ALPN* TLSX_ALPN_Find(ALPN *list, char *protocol_name, word16 size)
  1306. {
  1307. ALPN *alpn;
  1308. if (list == NULL || protocol_name == NULL)
  1309. return NULL;
  1310. alpn = list;
  1311. while (alpn != NULL && (
  1312. (word16)XSTRLEN(alpn->protocol_name) != size ||
  1313. XSTRNCMP(alpn->protocol_name, protocol_name, size)))
  1314. alpn = alpn->next;
  1315. return alpn;
  1316. }
  1317. /** Set the ALPN matching client and server requirements */
  1318. static int TLSX_SetALPN(TLSX** extensions, const void* data, word16 size,
  1319. void* heap)
  1320. {
  1321. ALPN *alpn;
  1322. int ret;
  1323. if (extensions == NULL || data == NULL)
  1324. return BAD_FUNC_ARG;
  1325. alpn = TLSX_ALPN_New((char *)data, size, heap);
  1326. if (alpn == NULL) {
  1327. WOLFSSL_MSG("Memory failure");
  1328. return MEMORY_E;
  1329. }
  1330. alpn->negotiated = 1;
  1331. ret = TLSX_Push(extensions, TLSX_APPLICATION_LAYER_PROTOCOL, (void*)alpn,
  1332. heap);
  1333. if (ret != 0) {
  1334. TLSX_ALPN_Free(alpn, heap);
  1335. return ret;
  1336. }
  1337. return WOLFSSL_SUCCESS;
  1338. }
  1339. static int ALPN_find_match(WOLFSSL *ssl, TLSX **pextension,
  1340. const byte **psel, byte *psel_len,
  1341. const byte *alpn_val, word16 alpn_val_len)
  1342. {
  1343. TLSX *extension;
  1344. ALPN *alpn, *list;
  1345. const byte *sel = NULL, *s;
  1346. byte sel_len = 0, wlen;
  1347. extension = TLSX_Find(ssl->extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
  1348. if (extension == NULL)
  1349. extension = TLSX_Find(ssl->ctx->extensions,
  1350. TLSX_APPLICATION_LAYER_PROTOCOL);
  1351. /* No ALPN configured here */
  1352. if (extension == NULL || extension->data == NULL) {
  1353. *pextension = NULL;
  1354. *psel = NULL;
  1355. *psel_len = 0;
  1356. return 0;
  1357. }
  1358. list = (ALPN*)extension->data;
  1359. for (s = alpn_val;
  1360. (s - alpn_val) < alpn_val_len;
  1361. s += wlen) {
  1362. wlen = *s++; /* bounds already checked on save */
  1363. alpn = TLSX_ALPN_Find(list, (char*)s, wlen);
  1364. if (alpn != NULL) {
  1365. WOLFSSL_MSG("ALPN protocol match");
  1366. sel = s,
  1367. sel_len = wlen;
  1368. break;
  1369. }
  1370. }
  1371. if (sel == NULL) {
  1372. WOLFSSL_MSG("No ALPN protocol match");
  1373. /* do nothing if no protocol match between client and server and option
  1374. is set to continue (like OpenSSL) */
  1375. if (list->options & WOLFSSL_ALPN_CONTINUE_ON_MISMATCH) {
  1376. WOLFSSL_MSG("Continue on mismatch");
  1377. }
  1378. else {
  1379. SendAlert(ssl, alert_fatal, no_application_protocol);
  1380. WOLFSSL_ERROR_VERBOSE(UNKNOWN_ALPN_PROTOCOL_NAME_E);
  1381. return UNKNOWN_ALPN_PROTOCOL_NAME_E;
  1382. }
  1383. }
  1384. *pextension = extension;
  1385. *psel = sel;
  1386. *psel_len = sel_len;
  1387. return 0;
  1388. }
  1389. int ALPN_Select(WOLFSSL *ssl)
  1390. {
  1391. TLSX *extension;
  1392. const byte *sel = NULL;
  1393. byte sel_len = 0;
  1394. int r = 0;
  1395. WOLFSSL_ENTER("ALPN_Select");
  1396. if (ssl->alpn_peer_requested == NULL)
  1397. return 0;
  1398. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
  1399. if (ssl->alpnSelect != NULL && ssl->options.side == WOLFSSL_SERVER_END) {
  1400. if (ssl->alpnSelect(ssl, &sel, &sel_len, ssl->alpn_peer_requested,
  1401. ssl->alpn_peer_requested_length,
  1402. ssl->alpnSelectArg) == 0) {
  1403. WOLFSSL_MSG("ALPN protocol match");
  1404. }
  1405. else {
  1406. sel = NULL;
  1407. sel_len = 0;
  1408. }
  1409. }
  1410. #endif
  1411. if (sel == NULL) {
  1412. r = ALPN_find_match(ssl, &extension, &sel, &sel_len,
  1413. ssl->alpn_peer_requested,
  1414. ssl->alpn_peer_requested_length);
  1415. if (r != 0)
  1416. return r;
  1417. }
  1418. if (sel != NULL) {
  1419. /* set the matching negotiated protocol */
  1420. r = TLSX_SetALPN(&ssl->extensions, sel, sel_len, ssl->heap);
  1421. if (r != WOLFSSL_SUCCESS) {
  1422. WOLFSSL_MSG("TLSX_SetALPN failed");
  1423. return BUFFER_ERROR;
  1424. }
  1425. /* reply to ALPN extension sent from peer */
  1426. #ifndef NO_WOLFSSL_SERVER
  1427. TLSX_SetResponse(ssl, TLSX_APPLICATION_LAYER_PROTOCOL);
  1428. #endif
  1429. }
  1430. return 0;
  1431. }
  1432. /** Parses a buffer of ALPN extensions and set the first one matching
  1433. * client and server requirements */
  1434. static int TLSX_ALPN_ParseAndSet(WOLFSSL *ssl, const byte *input, word16 length,
  1435. byte isRequest)
  1436. {
  1437. word16 size = 0, offset = 0, wlen;
  1438. int r = BUFFER_ERROR;
  1439. const byte *s;
  1440. if (OPAQUE16_LEN > length)
  1441. return BUFFER_ERROR;
  1442. ato16(input, &size);
  1443. offset += OPAQUE16_LEN;
  1444. /* validating alpn list length */
  1445. if (size == 0 || length != OPAQUE16_LEN + size)
  1446. return BUFFER_ERROR;
  1447. /* validating length of entries before accepting */
  1448. for (s = input + offset; (s - input) < size; s += wlen) {
  1449. wlen = *s++;
  1450. if (wlen == 0 || (s + wlen - input) > length)
  1451. return BUFFER_ERROR;
  1452. }
  1453. if (isRequest) {
  1454. /* keep the list sent by peer, if this is from a request. We
  1455. * use it later in ALPN_Select() for evaluation. */
  1456. if (ssl->alpn_peer_requested != NULL) {
  1457. XFREE(ssl->alpn_peer_requested, ssl->heap, DYNAMIC_TYPE_ALPN);
  1458. ssl->alpn_peer_requested_length = 0;
  1459. }
  1460. ssl->alpn_peer_requested = (byte *)XMALLOC(size, ssl->heap,
  1461. DYNAMIC_TYPE_ALPN);
  1462. if (ssl->alpn_peer_requested == NULL) {
  1463. return MEMORY_ERROR;
  1464. }
  1465. ssl->alpn_peer_requested_length = size;
  1466. XMEMCPY(ssl->alpn_peer_requested, (char*)input + offset, size);
  1467. }
  1468. else {
  1469. /* a response, we should find the value in our config */
  1470. const byte *sel = NULL;
  1471. byte sel_len = 0;
  1472. TLSX *extension = NULL;
  1473. r = ALPN_find_match(ssl, &extension, &sel, &sel_len, input + offset, size);
  1474. if (r != 0)
  1475. return r;
  1476. if (sel != NULL) {
  1477. /* set the matching negotiated protocol */
  1478. r = TLSX_SetALPN(&ssl->extensions, sel, sel_len, ssl->heap);
  1479. if (r != WOLFSSL_SUCCESS) {
  1480. WOLFSSL_MSG("TLSX_SetALPN failed");
  1481. return BUFFER_ERROR;
  1482. }
  1483. }
  1484. /* If we had nothing configured, the response is unexpected */
  1485. else if (extension == NULL) {
  1486. r = TLSX_HandleUnsupportedExtension(ssl);
  1487. if (r != 0)
  1488. return r;
  1489. }
  1490. }
  1491. return 0;
  1492. }
  1493. /** Add a protocol name to the list of accepted usable ones */
  1494. int TLSX_UseALPN(TLSX** extensions, const void* data, word16 size, byte options,
  1495. void* heap)
  1496. {
  1497. ALPN *alpn;
  1498. TLSX *extension;
  1499. int ret;
  1500. if (extensions == NULL || data == NULL)
  1501. return BAD_FUNC_ARG;
  1502. alpn = TLSX_ALPN_New((char *)data, size, heap);
  1503. if (alpn == NULL) {
  1504. WOLFSSL_MSG("Memory failure");
  1505. return MEMORY_E;
  1506. }
  1507. /* Set Options of ALPN */
  1508. alpn->options = options;
  1509. extension = TLSX_Find(*extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
  1510. if (extension == NULL) {
  1511. ret = TLSX_Push(extensions, TLSX_APPLICATION_LAYER_PROTOCOL,
  1512. (void*)alpn, heap);
  1513. if (ret != 0) {
  1514. TLSX_ALPN_Free(alpn, heap);
  1515. return ret;
  1516. }
  1517. }
  1518. else {
  1519. /* push new ALPN object to extension data. */
  1520. alpn->next = (ALPN*)extension->data;
  1521. extension->data = (void*)alpn;
  1522. }
  1523. return WOLFSSL_SUCCESS;
  1524. }
  1525. /** Get the protocol name set by the server */
  1526. int TLSX_ALPN_GetRequest(TLSX* extensions, void** data, word16 *dataSz)
  1527. {
  1528. TLSX *extension;
  1529. ALPN *alpn;
  1530. if (extensions == NULL || data == NULL || dataSz == NULL)
  1531. return BAD_FUNC_ARG;
  1532. extension = TLSX_Find(extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
  1533. if (extension == NULL) {
  1534. WOLFSSL_MSG("TLS extension not found");
  1535. WOLFSSL_ERROR_VERBOSE(WOLFSSL_ALPN_NOT_FOUND);
  1536. return WOLFSSL_ALPN_NOT_FOUND;
  1537. }
  1538. alpn = (ALPN *)extension->data;
  1539. if (alpn == NULL) {
  1540. WOLFSSL_MSG("ALPN extension not found");
  1541. *data = NULL;
  1542. *dataSz = 0;
  1543. WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR);
  1544. return WOLFSSL_FATAL_ERROR;
  1545. }
  1546. if (alpn->negotiated != 1) {
  1547. /* consider as an error */
  1548. if (alpn->options & WOLFSSL_ALPN_FAILED_ON_MISMATCH) {
  1549. WOLFSSL_MSG("No protocol match with peer -> Failed");
  1550. WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR);
  1551. return WOLFSSL_FATAL_ERROR;
  1552. }
  1553. /* continue without negotiated protocol */
  1554. WOLFSSL_MSG("No protocol match with peer -> Continue");
  1555. WOLFSSL_ERROR_VERBOSE(WOLFSSL_ALPN_NOT_FOUND);
  1556. return WOLFSSL_ALPN_NOT_FOUND;
  1557. }
  1558. if (alpn->next != NULL) {
  1559. WOLFSSL_MSG("Only one protocol name must be accepted");
  1560. WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR);
  1561. return WOLFSSL_FATAL_ERROR;
  1562. }
  1563. *data = alpn->protocol_name;
  1564. *dataSz = (word16)XSTRLEN((char*)*data);
  1565. return WOLFSSL_SUCCESS;
  1566. }
  1567. #define ALPN_FREE_ALL TLSX_ALPN_FreeAll
  1568. #define ALPN_GET_SIZE TLSX_ALPN_GetSize
  1569. #define ALPN_WRITE TLSX_ALPN_Write
  1570. #define ALPN_PARSE TLSX_ALPN_ParseAndSet
  1571. #else /* HAVE_ALPN */
  1572. #define ALPN_FREE_ALL(list, heap)
  1573. #define ALPN_GET_SIZE(list) 0
  1574. #define ALPN_WRITE(a, b) 0
  1575. #define ALPN_PARSE(a, b, c, d) 0
  1576. #endif /* HAVE_ALPN */
  1577. /******************************************************************************/
  1578. /* Server Name Indication */
  1579. /******************************************************************************/
  1580. #ifdef HAVE_SNI
  1581. /** Creates a new SNI object. */
  1582. static SNI* TLSX_SNI_New(byte type, const void* data, word16 size, void* heap)
  1583. {
  1584. SNI* sni = (SNI*)XMALLOC(sizeof(SNI), heap, DYNAMIC_TYPE_TLSX);
  1585. (void)heap;
  1586. if (sni) {
  1587. sni->type = type;
  1588. sni->next = NULL;
  1589. #ifndef NO_WOLFSSL_SERVER
  1590. sni->options = 0;
  1591. sni->status = WOLFSSL_SNI_NO_MATCH;
  1592. #endif
  1593. switch (sni->type) {
  1594. case WOLFSSL_SNI_HOST_NAME:
  1595. sni->data.host_name = (char*)XMALLOC(size + 1, heap,
  1596. DYNAMIC_TYPE_TLSX);
  1597. if (sni->data.host_name) {
  1598. XSTRNCPY(sni->data.host_name, (const char*)data, size);
  1599. sni->data.host_name[size] = '\0';
  1600. } else {
  1601. XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
  1602. sni = NULL;
  1603. }
  1604. break;
  1605. default: /* invalid type */
  1606. XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
  1607. sni = NULL;
  1608. }
  1609. }
  1610. return sni;
  1611. }
  1612. /** Releases a SNI object. */
  1613. static void TLSX_SNI_Free(SNI* sni, void* heap)
  1614. {
  1615. if (sni) {
  1616. switch (sni->type) {
  1617. case WOLFSSL_SNI_HOST_NAME:
  1618. XFREE(sni->data.host_name, heap, DYNAMIC_TYPE_TLSX);
  1619. break;
  1620. }
  1621. XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
  1622. }
  1623. (void)heap;
  1624. }
  1625. /** Releases all SNI objects in the provided list. */
  1626. static void TLSX_SNI_FreeAll(SNI* list, void* heap)
  1627. {
  1628. SNI* sni;
  1629. while ((sni = list)) {
  1630. list = sni->next;
  1631. TLSX_SNI_Free(sni, heap);
  1632. }
  1633. }
  1634. /** Tells the buffered size of the SNI objects in a list. */
  1635. static word16 TLSX_SNI_GetSize(SNI* list)
  1636. {
  1637. SNI* sni;
  1638. word16 length = OPAQUE16_LEN; /* list length */
  1639. while ((sni = list)) {
  1640. list = sni->next;
  1641. length += ENUM_LEN + OPAQUE16_LEN; /* sni type + sni length */
  1642. switch (sni->type) {
  1643. case WOLFSSL_SNI_HOST_NAME:
  1644. length += (word16)XSTRLEN((char*)sni->data.host_name);
  1645. break;
  1646. }
  1647. }
  1648. return length;
  1649. }
  1650. /** Writes the SNI objects of a list in a buffer. */
  1651. static word16 TLSX_SNI_Write(SNI* list, byte* output)
  1652. {
  1653. SNI* sni;
  1654. word16 length = 0;
  1655. word16 offset = OPAQUE16_LEN; /* list length offset */
  1656. while ((sni = list)) {
  1657. list = sni->next;
  1658. output[offset++] = sni->type; /* sni type */
  1659. switch (sni->type) {
  1660. case WOLFSSL_SNI_HOST_NAME:
  1661. length = (word16)XSTRLEN((char*)sni->data.host_name);
  1662. c16toa(length, output + offset); /* sni length */
  1663. offset += OPAQUE16_LEN;
  1664. XMEMCPY(output + offset, sni->data.host_name, length);
  1665. offset += length;
  1666. break;
  1667. }
  1668. }
  1669. c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
  1670. return offset;
  1671. }
  1672. /** Finds a SNI object in the provided list. */
  1673. static SNI* TLSX_SNI_Find(SNI *list, byte type)
  1674. {
  1675. SNI* sni = list;
  1676. while (sni && sni->type != type)
  1677. sni = sni->next;
  1678. return sni;
  1679. }
  1680. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  1681. /** Sets the status of a SNI object. */
  1682. static void TLSX_SNI_SetStatus(TLSX* extensions, byte type, byte status)
  1683. {
  1684. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1685. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1686. if (sni)
  1687. sni->status = status;
  1688. }
  1689. #endif
  1690. /** Gets the status of a SNI object. */
  1691. byte TLSX_SNI_Status(TLSX* extensions, byte type)
  1692. {
  1693. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1694. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1695. if (sni)
  1696. return sni->status;
  1697. return 0;
  1698. }
  1699. /** Parses a buffer of SNI extensions. */
  1700. static int TLSX_SNI_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  1701. byte isRequest)
  1702. {
  1703. #ifndef NO_WOLFSSL_SERVER
  1704. word16 size = 0;
  1705. word16 offset = 0;
  1706. int cacheOnly = 0;
  1707. SNI *sni = NULL;
  1708. byte type;
  1709. byte matched;
  1710. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  1711. WOLFSSL_ECH* ech = NULL;
  1712. WOLFSSL_EchConfig* workingConfig;
  1713. TLSX* echX;
  1714. #endif
  1715. #endif /* !NO_WOLFSSL_SERVER */
  1716. TLSX *extension = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
  1717. if (!extension)
  1718. extension = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
  1719. if (!isRequest) {
  1720. #ifndef NO_WOLFSSL_CLIENT
  1721. if (!extension || !extension->data)
  1722. return TLSX_HandleUnsupportedExtension(ssl);
  1723. if (length > 0)
  1724. return BUFFER_ERROR; /* SNI response MUST be empty. */
  1725. /* This call enables wolfSSL_SNI_GetRequest() to be called in the
  1726. * client side to fetch the used SNI. It will only work if the SNI
  1727. * was set at the SSL object level. Right now we only support one
  1728. * name type, WOLFSSL_SNI_HOST_NAME, but in the future, the
  1729. * inclusion of other name types will turn this method inaccurate,
  1730. * as the extension response doesn't contains information of which
  1731. * name was accepted.
  1732. */
  1733. TLSX_SNI_SetStatus(ssl->extensions, WOLFSSL_SNI_HOST_NAME,
  1734. WOLFSSL_SNI_REAL_MATCH);
  1735. return 0;
  1736. #endif
  1737. }
  1738. #ifndef NO_WOLFSSL_SERVER
  1739. if (!extension || !extension->data) {
  1740. /* This will keep SNI even though TLSX_UseSNI has not been called.
  1741. * Enable it so that the received sni is available to functions
  1742. * that use a custom callback when SNI is received.
  1743. */
  1744. #ifdef WOLFSSL_ALWAYS_KEEP_SNI
  1745. cacheOnly = 1;
  1746. #endif
  1747. if (ssl->ctx->sniRecvCb) {
  1748. cacheOnly = 1;
  1749. }
  1750. if (cacheOnly) {
  1751. WOLFSSL_MSG("Forcing SSL object to store SNI parameter");
  1752. }
  1753. else {
  1754. /* Skipping, SNI not enabled at server side. */
  1755. return 0;
  1756. }
  1757. }
  1758. if (OPAQUE16_LEN > length)
  1759. return BUFFER_ERROR;
  1760. ato16(input, &size);
  1761. offset += OPAQUE16_LEN;
  1762. /* validating sni list length */
  1763. if (length != OPAQUE16_LEN + size || size == 0)
  1764. return BUFFER_ERROR;
  1765. /* SNI was badly specified and only one type is now recognized and allowed.
  1766. * Only one SNI value per type (RFC6066), so, no loop. */
  1767. type = input[offset++];
  1768. if (type != WOLFSSL_SNI_HOST_NAME)
  1769. return BUFFER_ERROR;
  1770. if (offset + OPAQUE16_LEN > length)
  1771. return BUFFER_ERROR;
  1772. ato16(input + offset, &size);
  1773. offset += OPAQUE16_LEN;
  1774. if (offset + size != length || size == 0)
  1775. return BUFFER_ERROR;
  1776. if (!cacheOnly && !(sni = TLSX_SNI_Find((SNI*)extension->data, type)))
  1777. return 0; /* not using this type of SNI. */
  1778. #ifdef WOLFSSL_TLS13
  1779. /* Don't process the second ClientHello SNI extension if there
  1780. * was problems with the first.
  1781. */
  1782. if (!cacheOnly && sni->status != 0)
  1783. return 0;
  1784. #endif
  1785. matched = cacheOnly || (XSTRLEN(sni->data.host_name) == size &&
  1786. XSTRNCMP(sni->data.host_name, (const char*)input + offset, size) == 0);
  1787. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  1788. echX = TLSX_Find(ssl->extensions, TLSX_ECH);
  1789. if (echX != NULL)
  1790. ech = (WOLFSSL_ECH*)(echX->data);
  1791. if (!matched && ech != NULL) {
  1792. workingConfig = ech->echConfig;
  1793. while (workingConfig != NULL) {
  1794. matched = XSTRLEN(workingConfig->publicName) == size &&
  1795. XSTRNCMP(workingConfig->publicName,
  1796. (const char*)input + offset, size) == 0;
  1797. if (matched)
  1798. break;
  1799. workingConfig = workingConfig->next;
  1800. }
  1801. }
  1802. #endif
  1803. if (matched || sni->options & WOLFSSL_SNI_ANSWER_ON_MISMATCH) {
  1804. int matchStat;
  1805. int r = TLSX_UseSNI(&ssl->extensions, type, input + offset, size,
  1806. ssl->heap);
  1807. if (r != WOLFSSL_SUCCESS)
  1808. return r; /* throws error. */
  1809. if (cacheOnly) {
  1810. WOLFSSL_MSG("Forcing storage of SNI, Fake match");
  1811. matchStat = WOLFSSL_SNI_FORCE_KEEP;
  1812. }
  1813. else if (matched) {
  1814. WOLFSSL_MSG("SNI did match!");
  1815. matchStat = WOLFSSL_SNI_REAL_MATCH;
  1816. }
  1817. else {
  1818. WOLFSSL_MSG("fake SNI match from ANSWER_ON_MISMATCH");
  1819. matchStat = WOLFSSL_SNI_FAKE_MATCH;
  1820. }
  1821. TLSX_SNI_SetStatus(ssl->extensions, type, (byte)matchStat);
  1822. if (!cacheOnly)
  1823. TLSX_SetResponse(ssl, TLSX_SERVER_NAME);
  1824. }
  1825. else if (!(sni->options & WOLFSSL_SNI_CONTINUE_ON_MISMATCH)) {
  1826. SendAlert(ssl, alert_fatal, unrecognized_name);
  1827. WOLFSSL_ERROR_VERBOSE(UNKNOWN_SNI_HOST_NAME_E);
  1828. return UNKNOWN_SNI_HOST_NAME_E;
  1829. }
  1830. #else
  1831. (void)input;
  1832. #endif /* !NO_WOLFSSL_SERVER */
  1833. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1834. (void)length;
  1835. #endif
  1836. return 0;
  1837. }
  1838. static int TLSX_SNI_VerifyParse(WOLFSSL* ssl, byte isRequest)
  1839. {
  1840. (void)ssl;
  1841. if (isRequest) {
  1842. #ifndef NO_WOLFSSL_SERVER
  1843. TLSX* ctx_ext = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
  1844. TLSX* ssl_ext = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
  1845. SNI* ctx_sni = ctx_ext ? (SNI*)ctx_ext->data : NULL;
  1846. SNI* ssl_sni = ssl_ext ? (SNI*)ssl_ext->data : NULL;
  1847. SNI* sni = NULL;
  1848. for (; ctx_sni; ctx_sni = ctx_sni->next) {
  1849. if (ctx_sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) {
  1850. sni = TLSX_SNI_Find(ssl_sni, ctx_sni->type);
  1851. if (sni) {
  1852. if (sni->status != WOLFSSL_SNI_NO_MATCH)
  1853. continue;
  1854. /* if ssl level overrides ctx level, it is ok. */
  1855. if ((sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) == 0)
  1856. continue;
  1857. }
  1858. SendAlert(ssl, alert_fatal, handshake_failure);
  1859. WOLFSSL_ERROR_VERBOSE(SNI_ABSENT_ERROR);
  1860. return SNI_ABSENT_ERROR;
  1861. }
  1862. }
  1863. for (; ssl_sni; ssl_sni = ssl_sni->next) {
  1864. if (ssl_sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) {
  1865. if (ssl_sni->status != WOLFSSL_SNI_NO_MATCH)
  1866. continue;
  1867. SendAlert(ssl, alert_fatal, handshake_failure);
  1868. WOLFSSL_ERROR_VERBOSE(SNI_ABSENT_ERROR);
  1869. return SNI_ABSENT_ERROR;
  1870. }
  1871. }
  1872. #endif /* NO_WOLFSSL_SERVER */
  1873. }
  1874. return 0;
  1875. }
  1876. int TLSX_UseSNI(TLSX** extensions, byte type, const void* data, word16 size,
  1877. void* heap)
  1878. {
  1879. TLSX* extension;
  1880. SNI* sni = NULL;
  1881. if (extensions == NULL || data == NULL)
  1882. return BAD_FUNC_ARG;
  1883. if ((sni = TLSX_SNI_New(type, data, size, heap)) == NULL)
  1884. return MEMORY_E;
  1885. extension = TLSX_Find(*extensions, TLSX_SERVER_NAME);
  1886. if (!extension) {
  1887. int ret = TLSX_Push(extensions, TLSX_SERVER_NAME, (void*)sni, heap);
  1888. if (ret != 0) {
  1889. TLSX_SNI_Free(sni, heap);
  1890. return ret;
  1891. }
  1892. }
  1893. else {
  1894. /* push new SNI object to extension data. */
  1895. sni->next = (SNI*)extension->data;
  1896. extension->data = (void*)sni;
  1897. /* remove duplicate SNI, there should be only one of each type. */
  1898. do {
  1899. if (sni->next && sni->next->type == type) {
  1900. SNI* next = sni->next;
  1901. sni->next = next->next;
  1902. TLSX_SNI_Free(next, heap);
  1903. /* there is no way to occur more than
  1904. * two SNIs of the same type.
  1905. */
  1906. break;
  1907. }
  1908. } while ((sni = sni->next));
  1909. }
  1910. return WOLFSSL_SUCCESS;
  1911. }
  1912. #ifndef NO_WOLFSSL_SERVER
  1913. /** Tells the SNI requested by the client. */
  1914. word16 TLSX_SNI_GetRequest(TLSX* extensions, byte type, void** data)
  1915. {
  1916. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1917. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1918. if (sni && sni->status != WOLFSSL_SNI_NO_MATCH) {
  1919. switch (sni->type) {
  1920. case WOLFSSL_SNI_HOST_NAME:
  1921. if (data) {
  1922. *data = sni->data.host_name;
  1923. return (word16)XSTRLEN((char*)*data);
  1924. }
  1925. }
  1926. }
  1927. return 0;
  1928. }
  1929. /** Sets the options for a SNI object. */
  1930. void TLSX_SNI_SetOptions(TLSX* extensions, byte type, byte options)
  1931. {
  1932. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1933. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1934. if (sni)
  1935. sni->options = options;
  1936. }
  1937. /** Retrieves a SNI request from a client hello buffer. */
  1938. int TLSX_SNI_GetFromBuffer(const byte* clientHello, word32 helloSz,
  1939. byte type, byte* sni, word32* inOutSz)
  1940. {
  1941. word32 offset = 0;
  1942. word32 len32 = 0;
  1943. word16 len16 = 0;
  1944. if (helloSz < RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ + CLIENT_HELLO_FIRST)
  1945. return INCOMPLETE_DATA;
  1946. /* TLS record header */
  1947. if ((enum ContentType) clientHello[offset++] != handshake) {
  1948. /* checking for SSLv2.0 client hello according to: */
  1949. /* http://tools.ietf.org/html/rfc4346#appendix-E.1 */
  1950. if ((enum HandShakeType) clientHello[++offset] == client_hello) {
  1951. offset += ENUM_LEN + VERSION_SZ; /* skip version */
  1952. ato16(clientHello + offset, &len16);
  1953. offset += OPAQUE16_LEN;
  1954. if (len16 % 3) /* cipher_spec_length must be multiple of 3 */
  1955. return BUFFER_ERROR;
  1956. ato16(clientHello + offset, &len16);
  1957. /* Returning SNI_UNSUPPORTED do not increment offset here */
  1958. if (len16 != 0) /* session_id_length must be 0 */
  1959. return BUFFER_ERROR;
  1960. WOLFSSL_ERROR_VERBOSE(SNI_UNSUPPORTED);
  1961. return SNI_UNSUPPORTED;
  1962. }
  1963. return BUFFER_ERROR;
  1964. }
  1965. if (clientHello[offset++] != SSLv3_MAJOR)
  1966. return BUFFER_ERROR;
  1967. if (clientHello[offset++] < TLSv1_MINOR) {
  1968. WOLFSSL_ERROR_VERBOSE(SNI_UNSUPPORTED);
  1969. return SNI_UNSUPPORTED;
  1970. }
  1971. ato16(clientHello + offset, &len16);
  1972. offset += OPAQUE16_LEN;
  1973. if (offset + len16 > helloSz)
  1974. return INCOMPLETE_DATA;
  1975. /* Handshake header */
  1976. if ((enum HandShakeType) clientHello[offset] != client_hello)
  1977. return BUFFER_ERROR;
  1978. c24to32(clientHello + offset + 1, &len32);
  1979. offset += HANDSHAKE_HEADER_SZ;
  1980. if (offset + len32 > helloSz)
  1981. return BUFFER_ERROR;
  1982. /* client hello */
  1983. offset += VERSION_SZ + RAN_LEN; /* version, random */
  1984. if (helloSz < offset + clientHello[offset])
  1985. return BUFFER_ERROR;
  1986. offset += ENUM_LEN + clientHello[offset]; /* skip session id */
  1987. /* cypher suites */
  1988. if (helloSz < offset + OPAQUE16_LEN)
  1989. return BUFFER_ERROR;
  1990. ato16(clientHello + offset, &len16);
  1991. offset += OPAQUE16_LEN;
  1992. if (helloSz < offset + len16)
  1993. return BUFFER_ERROR;
  1994. offset += len16; /* skip cypher suites */
  1995. /* compression methods */
  1996. if (helloSz < offset + 1)
  1997. return BUFFER_ERROR;
  1998. if (helloSz < offset + clientHello[offset])
  1999. return BUFFER_ERROR;
  2000. offset += ENUM_LEN + clientHello[offset]; /* skip compression methods */
  2001. /* extensions */
  2002. if (helloSz < offset + OPAQUE16_LEN)
  2003. return 0; /* no extensions in client hello. */
  2004. ato16(clientHello + offset, &len16);
  2005. offset += OPAQUE16_LEN;
  2006. if (helloSz < offset + len16)
  2007. return BUFFER_ERROR;
  2008. while (len16 >= OPAQUE16_LEN + OPAQUE16_LEN) {
  2009. word16 extType;
  2010. word16 extLen;
  2011. ato16(clientHello + offset, &extType);
  2012. offset += OPAQUE16_LEN;
  2013. ato16(clientHello + offset, &extLen);
  2014. offset += OPAQUE16_LEN;
  2015. if (helloSz < offset + extLen)
  2016. return BUFFER_ERROR;
  2017. if (extType != TLSX_SERVER_NAME) {
  2018. offset += extLen; /* skip extension */
  2019. } else {
  2020. word16 listLen;
  2021. ato16(clientHello + offset, &listLen);
  2022. offset += OPAQUE16_LEN;
  2023. if (helloSz < offset + listLen)
  2024. return BUFFER_ERROR;
  2025. while (listLen > ENUM_LEN + OPAQUE16_LEN) {
  2026. byte sniType = clientHello[offset++];
  2027. word16 sniLen;
  2028. ato16(clientHello + offset, &sniLen);
  2029. offset += OPAQUE16_LEN;
  2030. if (helloSz < offset + sniLen)
  2031. return BUFFER_ERROR;
  2032. if (sniType != type) {
  2033. offset += sniLen;
  2034. listLen -= min(ENUM_LEN + OPAQUE16_LEN + sniLen, listLen);
  2035. continue;
  2036. }
  2037. *inOutSz = min(sniLen, *inOutSz);
  2038. XMEMCPY(sni, clientHello + offset, *inOutSz);
  2039. return WOLFSSL_SUCCESS;
  2040. }
  2041. }
  2042. len16 -= min(2 * OPAQUE16_LEN + extLen, len16);
  2043. }
  2044. return len16 ? BUFFER_ERROR : 0;
  2045. }
  2046. #endif
  2047. #define SNI_FREE_ALL TLSX_SNI_FreeAll
  2048. #define SNI_GET_SIZE TLSX_SNI_GetSize
  2049. #define SNI_WRITE TLSX_SNI_Write
  2050. #define SNI_PARSE TLSX_SNI_Parse
  2051. #define SNI_VERIFY_PARSE TLSX_SNI_VerifyParse
  2052. #else
  2053. #define SNI_FREE_ALL(list, heap)
  2054. #define SNI_GET_SIZE(list) 0
  2055. #define SNI_WRITE(a, b) 0
  2056. #define SNI_PARSE(a, b, c, d) 0
  2057. #define SNI_VERIFY_PARSE(a, b) 0
  2058. #endif /* HAVE_SNI */
  2059. /******************************************************************************/
  2060. /* Trusted CA Key Indication */
  2061. /******************************************************************************/
  2062. #ifdef HAVE_TRUSTED_CA
  2063. /** Creates a new TCA object. */
  2064. static TCA* TLSX_TCA_New(byte type, const byte* id, word16 idSz, void* heap)
  2065. {
  2066. TCA* tca = (TCA*)XMALLOC(sizeof(TCA), heap, DYNAMIC_TYPE_TLSX);
  2067. if (tca) {
  2068. XMEMSET(tca, 0, sizeof(TCA));
  2069. tca->type = type;
  2070. switch (type) {
  2071. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2072. break;
  2073. #ifndef NO_SHA
  2074. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2075. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2076. if (idSz == WC_SHA_DIGEST_SIZE &&
  2077. (tca->id =
  2078. (byte*)XMALLOC(idSz, heap, DYNAMIC_TYPE_TLSX))) {
  2079. XMEMCPY(tca->id, id, idSz);
  2080. tca->idSz = idSz;
  2081. }
  2082. else {
  2083. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2084. tca = NULL;
  2085. }
  2086. break;
  2087. #endif
  2088. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2089. if (idSz > 0 &&
  2090. (tca->id =
  2091. (byte*)XMALLOC(idSz, heap, DYNAMIC_TYPE_TLSX))) {
  2092. XMEMCPY(tca->id, id, idSz);
  2093. tca->idSz = idSz;
  2094. }
  2095. else {
  2096. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2097. tca = NULL;
  2098. }
  2099. break;
  2100. default: /* invalid type */
  2101. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2102. tca = NULL;
  2103. }
  2104. }
  2105. (void)heap;
  2106. return tca;
  2107. }
  2108. /** Releases a TCA object. */
  2109. static void TLSX_TCA_Free(TCA* tca, void* heap)
  2110. {
  2111. (void)heap;
  2112. if (tca) {
  2113. if (tca->id)
  2114. XFREE(tca->id, heap, DYNAMIC_TYPE_TLSX);
  2115. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2116. }
  2117. }
  2118. /** Releases all TCA objects in the provided list. */
  2119. static void TLSX_TCA_FreeAll(TCA* list, void* heap)
  2120. {
  2121. TCA* tca;
  2122. while ((tca = list)) {
  2123. list = tca->next;
  2124. TLSX_TCA_Free(tca, heap);
  2125. }
  2126. }
  2127. /** Tells the buffered size of the TCA objects in a list. */
  2128. static word16 TLSX_TCA_GetSize(TCA* list)
  2129. {
  2130. TCA* tca;
  2131. word16 length = OPAQUE16_LEN; /* list length */
  2132. while ((tca = list)) {
  2133. list = tca->next;
  2134. length += ENUM_LEN; /* tca type */
  2135. switch (tca->type) {
  2136. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2137. break;
  2138. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2139. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2140. length += tca->idSz;
  2141. break;
  2142. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2143. length += OPAQUE16_LEN + tca->idSz;
  2144. break;
  2145. }
  2146. }
  2147. return length;
  2148. }
  2149. /** Writes the TCA objects of a list in a buffer. */
  2150. static word16 TLSX_TCA_Write(TCA* list, byte* output)
  2151. {
  2152. TCA* tca;
  2153. word16 offset = OPAQUE16_LEN; /* list length offset */
  2154. while ((tca = list)) {
  2155. list = tca->next;
  2156. output[offset++] = tca->type; /* tca type */
  2157. switch (tca->type) {
  2158. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2159. break;
  2160. #ifndef NO_SHA
  2161. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2162. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2163. if (tca->id != NULL) {
  2164. XMEMCPY(output + offset, tca->id, tca->idSz);
  2165. offset += tca->idSz;
  2166. }
  2167. else {
  2168. /* ID missing. Set to an empty string. */
  2169. c16toa(0, output + offset);
  2170. offset += OPAQUE16_LEN;
  2171. }
  2172. break;
  2173. #endif
  2174. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2175. if (tca->id != NULL) {
  2176. c16toa(tca->idSz, output + offset); /* tca length */
  2177. offset += OPAQUE16_LEN;
  2178. XMEMCPY(output + offset, tca->id, tca->idSz);
  2179. offset += tca->idSz;
  2180. }
  2181. else {
  2182. /* ID missing. Set to an empty string. */
  2183. c16toa(0, output + offset);
  2184. offset += OPAQUE16_LEN;
  2185. }
  2186. break;
  2187. default:
  2188. /* ID unknown. Set to an empty string. */
  2189. c16toa(0, output + offset);
  2190. offset += OPAQUE16_LEN;
  2191. }
  2192. }
  2193. c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
  2194. return offset;
  2195. }
  2196. #ifndef NO_WOLFSSL_SERVER
  2197. static TCA* TLSX_TCA_Find(TCA *list, byte type, const byte* id, word16 idSz)
  2198. {
  2199. TCA* tca = list;
  2200. while (tca && tca->type != type && type != WOLFSSL_TRUSTED_CA_PRE_AGREED &&
  2201. idSz != tca->idSz && !XMEMCMP(id, tca->id, idSz))
  2202. tca = tca->next;
  2203. return tca;
  2204. }
  2205. #endif /* NO_WOLFSSL_SERVER */
  2206. /** Parses a buffer of TCA extensions. */
  2207. static int TLSX_TCA_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2208. byte isRequest)
  2209. {
  2210. #ifndef NO_WOLFSSL_SERVER
  2211. word16 size = 0;
  2212. word16 offset = 0;
  2213. #endif
  2214. TLSX *extension = TLSX_Find(ssl->extensions, TLSX_TRUSTED_CA_KEYS);
  2215. if (!extension)
  2216. extension = TLSX_Find(ssl->ctx->extensions, TLSX_TRUSTED_CA_KEYS);
  2217. if (!isRequest) {
  2218. #ifndef NO_WOLFSSL_CLIENT
  2219. if (!extension || !extension->data)
  2220. return TLSX_HandleUnsupportedExtension(ssl);
  2221. if (length > 0)
  2222. return BUFFER_ERROR; /* TCA response MUST be empty. */
  2223. /* Set the flag that we're good for keys */
  2224. TLSX_SetResponse(ssl, TLSX_TRUSTED_CA_KEYS);
  2225. return 0;
  2226. #endif
  2227. }
  2228. #ifndef NO_WOLFSSL_SERVER
  2229. if (!extension || !extension->data) {
  2230. /* Skipping, TCA not enabled at server side. */
  2231. return 0;
  2232. }
  2233. if (OPAQUE16_LEN > length)
  2234. return BUFFER_ERROR;
  2235. ato16(input, &size);
  2236. offset += OPAQUE16_LEN;
  2237. /* validating tca list length */
  2238. if (length != OPAQUE16_LEN + size)
  2239. return BUFFER_ERROR;
  2240. for (size = 0; offset < length; offset += size) {
  2241. TCA *tca = NULL;
  2242. byte type;
  2243. const byte* id = NULL;
  2244. word16 idSz = 0;
  2245. if (offset + ENUM_LEN > length)
  2246. return BUFFER_ERROR;
  2247. type = input[offset++];
  2248. switch (type) {
  2249. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2250. break;
  2251. #ifndef NO_SHA
  2252. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2253. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2254. if (offset + WC_SHA_DIGEST_SIZE > length)
  2255. return BUFFER_ERROR;
  2256. idSz = WC_SHA_DIGEST_SIZE;
  2257. id = input + offset;
  2258. offset += idSz;
  2259. break;
  2260. #endif
  2261. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2262. if (offset + OPAQUE16_LEN > length)
  2263. return BUFFER_ERROR;
  2264. ato16(input + offset, &idSz);
  2265. offset += OPAQUE16_LEN;
  2266. if ((offset > length) || (idSz > length - offset))
  2267. return BUFFER_ERROR;
  2268. id = input + offset;
  2269. offset += idSz;
  2270. break;
  2271. default:
  2272. WOLFSSL_ERROR_VERBOSE(TCA_INVALID_ID_TYPE);
  2273. return TCA_INVALID_ID_TYPE;
  2274. }
  2275. /* Find the type/ID in the TCA list. */
  2276. tca = TLSX_TCA_Find((TCA*)extension->data, type, id, idSz);
  2277. if (tca != NULL) {
  2278. /* Found it. Set the response flag and break out of the loop. */
  2279. TLSX_SetResponse(ssl, TLSX_TRUSTED_CA_KEYS);
  2280. break;
  2281. }
  2282. }
  2283. #else
  2284. (void)input;
  2285. #endif
  2286. return 0;
  2287. }
  2288. /* Checks to see if the server sent a response for the TCA. */
  2289. static int TLSX_TCA_VerifyParse(WOLFSSL* ssl, byte isRequest)
  2290. {
  2291. (void)ssl;
  2292. if (!isRequest) {
  2293. #ifndef NO_WOLFSSL_CLIENT
  2294. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_TRUSTED_CA_KEYS);
  2295. if (extension && !extension->resp) {
  2296. SendAlert(ssl, alert_fatal, handshake_failure);
  2297. WOLFSSL_ERROR_VERBOSE(TCA_ABSENT_ERROR);
  2298. return TCA_ABSENT_ERROR;
  2299. }
  2300. #endif /* NO_WOLFSSL_CLIENT */
  2301. }
  2302. return 0;
  2303. }
  2304. int TLSX_UseTrustedCA(TLSX** extensions, byte type,
  2305. const byte* id, word16 idSz, void* heap)
  2306. {
  2307. TLSX* extension;
  2308. TCA* tca = NULL;
  2309. if (extensions == NULL)
  2310. return BAD_FUNC_ARG;
  2311. if ((tca = TLSX_TCA_New(type, id, idSz, heap)) == NULL)
  2312. return MEMORY_E;
  2313. extension = TLSX_Find(*extensions, TLSX_TRUSTED_CA_KEYS);
  2314. if (!extension) {
  2315. int ret = TLSX_Push(extensions, TLSX_TRUSTED_CA_KEYS, (void*)tca, heap);
  2316. if (ret != 0) {
  2317. TLSX_TCA_Free(tca, heap);
  2318. return ret;
  2319. }
  2320. }
  2321. else {
  2322. /* push new TCA object to extension data. */
  2323. tca->next = (TCA*)extension->data;
  2324. extension->data = (void*)tca;
  2325. }
  2326. return WOLFSSL_SUCCESS;
  2327. }
  2328. #define TCA_FREE_ALL TLSX_TCA_FreeAll
  2329. #define TCA_GET_SIZE TLSX_TCA_GetSize
  2330. #define TCA_WRITE TLSX_TCA_Write
  2331. #define TCA_PARSE TLSX_TCA_Parse
  2332. #define TCA_VERIFY_PARSE TLSX_TCA_VerifyParse
  2333. #else /* HAVE_TRUSTED_CA */
  2334. #define TCA_FREE_ALL(list, heap)
  2335. #define TCA_GET_SIZE(list) 0
  2336. #define TCA_WRITE(a, b) 0
  2337. #define TCA_PARSE(a, b, c, d) 0
  2338. #define TCA_VERIFY_PARSE(a, b) 0
  2339. #endif /* HAVE_TRUSTED_CA */
  2340. /******************************************************************************/
  2341. /* Max Fragment Length Negotiation */
  2342. /******************************************************************************/
  2343. #ifdef HAVE_MAX_FRAGMENT
  2344. static word16 TLSX_MFL_Write(byte* data, byte* output)
  2345. {
  2346. output[0] = data[0];
  2347. return ENUM_LEN;
  2348. }
  2349. static int TLSX_MFL_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2350. byte isRequest)
  2351. {
  2352. if (length != ENUM_LEN)
  2353. return BUFFER_ERROR;
  2354. #ifdef WOLFSSL_OLD_UNSUPPORTED_EXTENSION
  2355. (void) isRequest;
  2356. #else
  2357. if (!isRequest)
  2358. if (TLSX_CheckUnsupportedExtension(ssl, TLSX_MAX_FRAGMENT_LENGTH))
  2359. return TLSX_HandleUnsupportedExtension(ssl);
  2360. #endif
  2361. switch (*input) {
  2362. case WOLFSSL_MFL_2_8 : ssl->max_fragment = 256; break;
  2363. case WOLFSSL_MFL_2_9 : ssl->max_fragment = 512; break;
  2364. case WOLFSSL_MFL_2_10: ssl->max_fragment = 1024; break;
  2365. case WOLFSSL_MFL_2_11: ssl->max_fragment = 2048; break;
  2366. case WOLFSSL_MFL_2_12: ssl->max_fragment = 4096; break;
  2367. case WOLFSSL_MFL_2_13: ssl->max_fragment = 8192; break;
  2368. default:
  2369. SendAlert(ssl, alert_fatal, illegal_parameter);
  2370. WOLFSSL_ERROR_VERBOSE(UNKNOWN_MAX_FRAG_LEN_E);
  2371. return UNKNOWN_MAX_FRAG_LEN_E;
  2372. }
  2373. #ifndef NO_WOLFSSL_SERVER
  2374. if (isRequest) {
  2375. int ret = TLSX_UseMaxFragment(&ssl->extensions, *input, ssl->heap);
  2376. if (ret != WOLFSSL_SUCCESS)
  2377. return ret; /* throw error */
  2378. TLSX_SetResponse(ssl, TLSX_MAX_FRAGMENT_LENGTH);
  2379. }
  2380. #endif
  2381. return 0;
  2382. }
  2383. int TLSX_UseMaxFragment(TLSX** extensions, byte mfl, void* heap)
  2384. {
  2385. byte* data = NULL;
  2386. int ret = 0;
  2387. if (extensions == NULL || mfl < WOLFSSL_MFL_MIN || mfl > WOLFSSL_MFL_MAX)
  2388. return BAD_FUNC_ARG;
  2389. data = (byte*)XMALLOC(ENUM_LEN, heap, DYNAMIC_TYPE_TLSX);
  2390. if (data == NULL)
  2391. return MEMORY_E;
  2392. data[0] = mfl;
  2393. ret = TLSX_Push(extensions, TLSX_MAX_FRAGMENT_LENGTH, data, heap);
  2394. if (ret != 0) {
  2395. XFREE(data, heap, DYNAMIC_TYPE_TLSX);
  2396. return ret;
  2397. }
  2398. return WOLFSSL_SUCCESS;
  2399. }
  2400. #define MFL_FREE_ALL(data, heap) XFREE(data, (heap), DYNAMIC_TYPE_TLSX)
  2401. #define MFL_GET_SIZE(data) ENUM_LEN
  2402. #define MFL_WRITE TLSX_MFL_Write
  2403. #define MFL_PARSE TLSX_MFL_Parse
  2404. #else
  2405. #define MFL_FREE_ALL(a, b)
  2406. #define MFL_GET_SIZE(a) 0
  2407. #define MFL_WRITE(a, b) 0
  2408. #define MFL_PARSE(a, b, c, d) 0
  2409. #endif /* HAVE_MAX_FRAGMENT */
  2410. /******************************************************************************/
  2411. /* Truncated HMAC */
  2412. /******************************************************************************/
  2413. #ifdef HAVE_TRUNCATED_HMAC
  2414. static int TLSX_THM_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2415. byte isRequest)
  2416. {
  2417. if (length != 0 || input == NULL)
  2418. return BUFFER_ERROR;
  2419. if (!isRequest) {
  2420. #ifndef WOLFSSL_OLD_UNSUPPORTED_EXTENSION
  2421. if (TLSX_CheckUnsupportedExtension(ssl, TLSX_TRUNCATED_HMAC))
  2422. return TLSX_HandleUnsupportedExtension(ssl);
  2423. #endif
  2424. }
  2425. else {
  2426. #ifndef NO_WOLFSSL_SERVER
  2427. int ret = TLSX_UseTruncatedHMAC(&ssl->extensions, ssl->heap);
  2428. if (ret != WOLFSSL_SUCCESS)
  2429. return ret; /* throw error */
  2430. TLSX_SetResponse(ssl, TLSX_TRUNCATED_HMAC);
  2431. #endif
  2432. }
  2433. ssl->truncated_hmac = 1;
  2434. return 0;
  2435. }
  2436. int TLSX_UseTruncatedHMAC(TLSX** extensions, void* heap)
  2437. {
  2438. int ret = 0;
  2439. if (extensions == NULL)
  2440. return BAD_FUNC_ARG;
  2441. ret = TLSX_Push(extensions, TLSX_TRUNCATED_HMAC, NULL, heap);
  2442. if (ret != 0)
  2443. return ret;
  2444. return WOLFSSL_SUCCESS;
  2445. }
  2446. #define THM_PARSE TLSX_THM_Parse
  2447. #else
  2448. #define THM_PARSE(a, b, c, d) 0
  2449. #endif /* HAVE_TRUNCATED_HMAC */
  2450. /******************************************************************************/
  2451. /* Certificate Status Request */
  2452. /******************************************************************************/
  2453. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  2454. static void TLSX_CSR_Free(CertificateStatusRequest* csr, void* heap)
  2455. {
  2456. switch (csr->status_type) {
  2457. case WOLFSSL_CSR_OCSP:
  2458. FreeOcspRequest(&csr->request.ocsp);
  2459. break;
  2460. }
  2461. XFREE(csr, heap, DYNAMIC_TYPE_TLSX);
  2462. (void)heap;
  2463. }
  2464. static word16 TLSX_CSR_GetSize(CertificateStatusRequest* csr, byte isRequest)
  2465. {
  2466. word16 size = 0;
  2467. /* shut up compiler warnings */
  2468. (void) csr; (void) isRequest;
  2469. #ifndef NO_WOLFSSL_CLIENT
  2470. if (isRequest) {
  2471. switch (csr->status_type) {
  2472. case WOLFSSL_CSR_OCSP:
  2473. size += ENUM_LEN + 2 * OPAQUE16_LEN;
  2474. if (csr->request.ocsp.nonceSz)
  2475. size += OCSP_NONCE_EXT_SZ;
  2476. break;
  2477. }
  2478. }
  2479. #endif
  2480. #if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER)
  2481. if (!isRequest && csr->ssl->options.tls1_3)
  2482. return OPAQUE8_LEN + OPAQUE24_LEN + csr->response.length;
  2483. #endif
  2484. return size;
  2485. }
  2486. static word16 TLSX_CSR_Write(CertificateStatusRequest* csr, byte* output,
  2487. byte isRequest)
  2488. {
  2489. /* shut up compiler warnings */
  2490. (void) csr; (void) output; (void) isRequest;
  2491. #ifndef NO_WOLFSSL_CLIENT
  2492. if (isRequest) {
  2493. word16 offset = 0;
  2494. word16 length = 0;
  2495. /* type */
  2496. output[offset++] = csr->status_type;
  2497. switch (csr->status_type) {
  2498. case WOLFSSL_CSR_OCSP:
  2499. /* responder id list */
  2500. c16toa(0, output + offset);
  2501. offset += OPAQUE16_LEN;
  2502. /* request extensions */
  2503. if (csr->request.ocsp.nonceSz)
  2504. length = (word16)EncodeOcspRequestExtensions(
  2505. &csr->request.ocsp,
  2506. output + offset + OPAQUE16_LEN,
  2507. OCSP_NONCE_EXT_SZ);
  2508. c16toa(length, output + offset);
  2509. offset += OPAQUE16_LEN + length;
  2510. break;
  2511. }
  2512. return offset;
  2513. }
  2514. #endif
  2515. #if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER)
  2516. if (!isRequest && csr->ssl->options.tls1_3) {
  2517. word16 offset = 0;
  2518. output[offset++] = csr->status_type;
  2519. c32to24(csr->response.length, output + offset);
  2520. offset += OPAQUE24_LEN;
  2521. XMEMCPY(output + offset, csr->response.buffer, csr->response.length);
  2522. offset += csr->response.length;
  2523. return offset;
  2524. }
  2525. #endif
  2526. return 0;
  2527. }
  2528. static int TLSX_CSR_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2529. byte isRequest)
  2530. {
  2531. int ret;
  2532. #if !defined(NO_WOLFSSL_SERVER)
  2533. byte status_type;
  2534. word16 size = 0;
  2535. #if defined(WOLFSSL_TLS13)
  2536. DecodedCert* cert;
  2537. #endif
  2538. #endif
  2539. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER) \
  2540. && defined(WOLFSSL_TLS13)
  2541. OcspRequest* request;
  2542. TLSX* extension;
  2543. CertificateStatusRequest* csr;
  2544. #endif
  2545. #if !defined(NO_WOLFSSL_CLIENT) && defined(WOLFSSL_TLS13) \
  2546. || !defined(NO_WOLFSSL_SERVER)
  2547. word32 offset = 0;
  2548. #endif
  2549. #if !defined(NO_WOLFSSL_CLIENT) && defined(WOLFSSL_TLS13)
  2550. word32 resp_length = 0;
  2551. #endif
  2552. /* shut up compiler warnings */
  2553. (void) ssl; (void) input;
  2554. if (!isRequest) {
  2555. #ifndef NO_WOLFSSL_CLIENT
  2556. extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2557. csr = extension ? (CertificateStatusRequest*)extension->data : NULL;
  2558. if (!csr) {
  2559. /* look at context level */
  2560. extension = TLSX_Find(ssl->ctx->extensions, TLSX_STATUS_REQUEST);
  2561. csr = extension ? (CertificateStatusRequest*)extension->data : NULL;
  2562. if (!csr) /* unexpected extension */
  2563. return TLSX_HandleUnsupportedExtension(ssl);
  2564. /* enable extension at ssl level */
  2565. ret = TLSX_UseCertificateStatusRequest(&ssl->extensions,
  2566. csr->status_type, csr->options, ssl,
  2567. ssl->heap, ssl->devId);
  2568. if (ret != WOLFSSL_SUCCESS)
  2569. return ret;
  2570. switch (csr->status_type) {
  2571. case WOLFSSL_CSR_OCSP:
  2572. /* propagate nonce */
  2573. if (csr->request.ocsp.nonceSz) {
  2574. request =
  2575. (OcspRequest*)TLSX_CSR_GetRequest(ssl->extensions);
  2576. if (request) {
  2577. XMEMCPY(request->nonce, csr->request.ocsp.nonce,
  2578. csr->request.ocsp.nonceSz);
  2579. request->nonceSz = csr->request.ocsp.nonceSz;
  2580. }
  2581. }
  2582. break;
  2583. }
  2584. }
  2585. ssl->status_request = 1;
  2586. #ifdef WOLFSSL_TLS13
  2587. if (ssl->options.tls1_3) {
  2588. /* Get the new extension potentially created above. */
  2589. extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2590. csr = extension ? (CertificateStatusRequest*)extension->data : NULL;
  2591. if (csr == NULL)
  2592. return MEMORY_ERROR;
  2593. ret = 0;
  2594. if (OPAQUE8_LEN + OPAQUE24_LEN > length)
  2595. ret = BUFFER_ERROR;
  2596. if (ret == 0 && input[offset++] != WOLFSSL_CSR_OCSP) {
  2597. ret = BAD_CERTIFICATE_STATUS_ERROR;
  2598. WOLFSSL_ERROR_VERBOSE(ret);
  2599. }
  2600. if (ret == 0) {
  2601. c24to32(input + offset, &resp_length);
  2602. offset += OPAQUE24_LEN;
  2603. if (offset + resp_length != length)
  2604. ret = BUFFER_ERROR;
  2605. }
  2606. if (ret == 0) {
  2607. csr->response.buffer = (byte*)(input + offset);
  2608. csr->response.length = resp_length;
  2609. }
  2610. return ret;
  2611. }
  2612. else
  2613. #endif
  2614. {
  2615. /* extension_data MUST be empty. */
  2616. return length ? BUFFER_ERROR : 0;
  2617. }
  2618. #endif
  2619. }
  2620. else {
  2621. #ifndef NO_WOLFSSL_SERVER
  2622. if (length == 0)
  2623. return 0;
  2624. status_type = input[offset++];
  2625. switch (status_type) {
  2626. case WOLFSSL_CSR_OCSP: {
  2627. /* skip responder_id_list */
  2628. if ((int)(length - offset) < OPAQUE16_LEN)
  2629. return BUFFER_ERROR;
  2630. ato16(input + offset, &size);
  2631. offset += OPAQUE16_LEN + size;
  2632. /* skip request_extensions */
  2633. if ((int)(length - offset) < OPAQUE16_LEN)
  2634. return BUFFER_ERROR;
  2635. ato16(input + offset, &size);
  2636. offset += OPAQUE16_LEN + size;
  2637. if (offset > length)
  2638. return BUFFER_ERROR;
  2639. /* is able to send OCSP response? */
  2640. if (SSL_CM(ssl) == NULL || !SSL_CM(ssl)->ocspStaplingEnabled)
  2641. return 0;
  2642. }
  2643. break;
  2644. /* unknown status type */
  2645. default:
  2646. return 0;
  2647. }
  2648. /* if using status_request and already sending it, skip this one */
  2649. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  2650. if (ssl->status_request_v2)
  2651. return 0;
  2652. #endif
  2653. /* accept the first good status_type and return */
  2654. ret = TLSX_UseCertificateStatusRequest(&ssl->extensions, status_type,
  2655. 0, ssl, ssl->heap, ssl->devId);
  2656. if (ret != WOLFSSL_SUCCESS)
  2657. return ret; /* throw error */
  2658. #if defined(WOLFSSL_TLS13)
  2659. if (ssl->options.tls1_3) {
  2660. if (ssl->buffers.certificate == NULL) {
  2661. WOLFSSL_MSG("Certificate buffer not set!");
  2662. return BUFFER_ERROR;
  2663. }
  2664. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), ssl->heap,
  2665. DYNAMIC_TYPE_DCERT);
  2666. if (cert == NULL) {
  2667. return MEMORY_E;
  2668. }
  2669. InitDecodedCert(cert, ssl->buffers.certificate->buffer,
  2670. ssl->buffers.certificate->length, ssl->heap);
  2671. ret = ParseCert(cert, CERT_TYPE, 1, SSL_CM(ssl));
  2672. if (ret != 0 ) {
  2673. XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
  2674. return ret;
  2675. }
  2676. ret = TLSX_CSR_InitRequest(ssl->extensions, cert, ssl->heap);
  2677. if (ret != 0 ) {
  2678. XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
  2679. return ret;
  2680. }
  2681. XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
  2682. extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2683. csr = extension ?
  2684. (CertificateStatusRequest*)extension->data : NULL;
  2685. if (csr == NULL)
  2686. return MEMORY_ERROR;
  2687. request = &csr->request.ocsp;
  2688. ret = CreateOcspResponse(ssl, &request, &csr->response);
  2689. if (ret != 0)
  2690. return ret;
  2691. if (csr->response.buffer)
  2692. TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST);
  2693. }
  2694. else
  2695. #endif
  2696. TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST);
  2697. ssl->status_request = status_type;
  2698. #endif
  2699. }
  2700. return 0;
  2701. }
  2702. int TLSX_CSR_InitRequest(TLSX* extensions, DecodedCert* cert, void* heap)
  2703. {
  2704. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST);
  2705. CertificateStatusRequest* csr = extension ?
  2706. (CertificateStatusRequest*)extension->data : NULL;
  2707. int ret = 0;
  2708. if (csr) {
  2709. switch (csr->status_type) {
  2710. case WOLFSSL_CSR_OCSP: {
  2711. byte nonce[MAX_OCSP_NONCE_SZ];
  2712. int nonceSz = csr->request.ocsp.nonceSz;
  2713. /* preserve nonce */
  2714. XMEMCPY(nonce, csr->request.ocsp.nonce, nonceSz);
  2715. if ((ret = InitOcspRequest(&csr->request.ocsp, cert, 0, heap))
  2716. != 0)
  2717. return ret;
  2718. /* restore nonce */
  2719. XMEMCPY(csr->request.ocsp.nonce, nonce, nonceSz);
  2720. csr->request.ocsp.nonceSz = nonceSz;
  2721. }
  2722. break;
  2723. }
  2724. }
  2725. return ret;
  2726. }
  2727. void* TLSX_CSR_GetRequest(TLSX* extensions)
  2728. {
  2729. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST);
  2730. CertificateStatusRequest* csr = extension ?
  2731. (CertificateStatusRequest*)extension->data : NULL;
  2732. if (csr) {
  2733. switch (csr->status_type) {
  2734. case WOLFSSL_CSR_OCSP:
  2735. return &csr->request.ocsp;
  2736. }
  2737. }
  2738. return NULL;
  2739. }
  2740. int TLSX_CSR_ForceRequest(WOLFSSL* ssl)
  2741. {
  2742. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2743. CertificateStatusRequest* csr = extension ?
  2744. (CertificateStatusRequest*)extension->data : NULL;
  2745. if (csr) {
  2746. switch (csr->status_type) {
  2747. case WOLFSSL_CSR_OCSP:
  2748. if (SSL_CM(ssl)->ocspEnabled) {
  2749. csr->request.ocsp.ssl = ssl;
  2750. return CheckOcspRequest(SSL_CM(ssl)->ocsp,
  2751. &csr->request.ocsp, NULL);
  2752. }
  2753. else {
  2754. WOLFSSL_ERROR_VERBOSE(OCSP_LOOKUP_FAIL);
  2755. return OCSP_LOOKUP_FAIL;
  2756. }
  2757. }
  2758. }
  2759. return 0;
  2760. }
  2761. int TLSX_UseCertificateStatusRequest(TLSX** extensions, byte status_type,
  2762. byte options, WOLFSSL* ssl, void* heap,
  2763. int devId)
  2764. {
  2765. CertificateStatusRequest* csr = NULL;
  2766. int ret = 0;
  2767. if (!extensions || status_type != WOLFSSL_CSR_OCSP)
  2768. return BAD_FUNC_ARG;
  2769. csr = (CertificateStatusRequest*)
  2770. XMALLOC(sizeof(CertificateStatusRequest), heap, DYNAMIC_TYPE_TLSX);
  2771. if (!csr)
  2772. return MEMORY_E;
  2773. ForceZero(csr, sizeof(CertificateStatusRequest));
  2774. csr->status_type = status_type;
  2775. csr->options = options;
  2776. csr->ssl = ssl;
  2777. switch (csr->status_type) {
  2778. case WOLFSSL_CSR_OCSP:
  2779. if (options & WOLFSSL_CSR_OCSP_USE_NONCE) {
  2780. WC_RNG rng;
  2781. #ifndef HAVE_FIPS
  2782. ret = wc_InitRng_ex(&rng, heap, devId);
  2783. #else
  2784. ret = wc_InitRng(&rng);
  2785. (void)devId;
  2786. #endif
  2787. if (ret == 0) {
  2788. if (wc_RNG_GenerateBlock(&rng, csr->request.ocsp.nonce,
  2789. MAX_OCSP_NONCE_SZ) == 0)
  2790. csr->request.ocsp.nonceSz = MAX_OCSP_NONCE_SZ;
  2791. wc_FreeRng(&rng);
  2792. }
  2793. }
  2794. break;
  2795. }
  2796. if ((ret = TLSX_Push(extensions, TLSX_STATUS_REQUEST, csr, heap)) != 0) {
  2797. XFREE(csr, heap, DYNAMIC_TYPE_TLSX);
  2798. return ret;
  2799. }
  2800. return WOLFSSL_SUCCESS;
  2801. }
  2802. #define CSR_FREE_ALL TLSX_CSR_Free
  2803. #define CSR_GET_SIZE TLSX_CSR_GetSize
  2804. #define CSR_WRITE TLSX_CSR_Write
  2805. #define CSR_PARSE TLSX_CSR_Parse
  2806. #else
  2807. #define CSR_FREE_ALL(data, heap)
  2808. #define CSR_GET_SIZE(a, b) 0
  2809. #define CSR_WRITE(a, b, c) 0
  2810. #define CSR_PARSE(a, b, c, d) 0
  2811. #endif /* HAVE_CERTIFICATE_STATUS_REQUEST */
  2812. /******************************************************************************/
  2813. /* Certificate Status Request v2 */
  2814. /******************************************************************************/
  2815. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  2816. static void TLSX_CSR2_FreeAll(CertificateStatusRequestItemV2* csr2, void* heap)
  2817. {
  2818. CertificateStatusRequestItemV2* next;
  2819. for (; csr2; csr2 = next) {
  2820. next = csr2->next;
  2821. switch (csr2->status_type) {
  2822. case WOLFSSL_CSR2_OCSP:
  2823. case WOLFSSL_CSR2_OCSP_MULTI:
  2824. while(csr2->requests--)
  2825. FreeOcspRequest(&csr2->request.ocsp[csr2->requests]);
  2826. break;
  2827. }
  2828. XFREE(csr2, heap, DYNAMIC_TYPE_TLSX);
  2829. }
  2830. (void)heap;
  2831. }
  2832. static word16 TLSX_CSR2_GetSize(CertificateStatusRequestItemV2* csr2,
  2833. byte isRequest)
  2834. {
  2835. word16 size = 0;
  2836. /* shut up compiler warnings */
  2837. (void) csr2; (void) isRequest;
  2838. #ifndef NO_WOLFSSL_CLIENT
  2839. if (isRequest) {
  2840. CertificateStatusRequestItemV2* next;
  2841. for (size = OPAQUE16_LEN; csr2; csr2 = next) {
  2842. next = csr2->next;
  2843. switch (csr2->status_type) {
  2844. case WOLFSSL_CSR2_OCSP:
  2845. case WOLFSSL_CSR2_OCSP_MULTI:
  2846. size += ENUM_LEN + 3 * OPAQUE16_LEN;
  2847. if (csr2->request.ocsp[0].nonceSz)
  2848. size += OCSP_NONCE_EXT_SZ;
  2849. break;
  2850. }
  2851. }
  2852. }
  2853. #endif
  2854. return size;
  2855. }
  2856. static word16 TLSX_CSR2_Write(CertificateStatusRequestItemV2* csr2,
  2857. byte* output, byte isRequest)
  2858. {
  2859. /* shut up compiler warnings */
  2860. (void) csr2; (void) output; (void) isRequest;
  2861. #ifndef NO_WOLFSSL_CLIENT
  2862. if (isRequest) {
  2863. word16 offset;
  2864. word16 length;
  2865. for (offset = OPAQUE16_LEN; csr2 != NULL; csr2 = csr2->next) {
  2866. /* status_type */
  2867. output[offset++] = csr2->status_type;
  2868. /* request */
  2869. switch (csr2->status_type) {
  2870. case WOLFSSL_CSR2_OCSP:
  2871. case WOLFSSL_CSR2_OCSP_MULTI:
  2872. /* request_length */
  2873. length = 2 * OPAQUE16_LEN;
  2874. if (csr2->request.ocsp[0].nonceSz)
  2875. length += OCSP_NONCE_EXT_SZ;
  2876. c16toa(length, output + offset);
  2877. offset += OPAQUE16_LEN;
  2878. /* responder id list */
  2879. c16toa(0, output + offset);
  2880. offset += OPAQUE16_LEN;
  2881. /* request extensions */
  2882. length = 0;
  2883. if (csr2->request.ocsp[0].nonceSz)
  2884. length = (word16)EncodeOcspRequestExtensions(
  2885. &csr2->request.ocsp[0],
  2886. output + offset + OPAQUE16_LEN,
  2887. OCSP_NONCE_EXT_SZ);
  2888. c16toa(length, output + offset);
  2889. offset += OPAQUE16_LEN + length;
  2890. break;
  2891. }
  2892. }
  2893. /* list size */
  2894. c16toa(offset - OPAQUE16_LEN, output);
  2895. return offset;
  2896. }
  2897. #endif
  2898. return 0;
  2899. }
  2900. static int TLSX_CSR2_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2901. byte isRequest)
  2902. {
  2903. int ret;
  2904. /* shut up compiler warnings */
  2905. (void) ssl; (void) input;
  2906. if (!isRequest) {
  2907. #ifndef NO_WOLFSSL_CLIENT
  2908. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST_V2);
  2909. CertificateStatusRequestItemV2* csr2 = extension ?
  2910. (CertificateStatusRequestItemV2*)extension->data : NULL;
  2911. if (!csr2) {
  2912. /* look at context level */
  2913. extension = TLSX_Find(ssl->ctx->extensions, TLSX_STATUS_REQUEST_V2);
  2914. csr2 = extension ?
  2915. (CertificateStatusRequestItemV2*)extension->data : NULL;
  2916. if (!csr2) /* unexpected extension */
  2917. return TLSX_HandleUnsupportedExtension(ssl);
  2918. /* enable extension at ssl level */
  2919. for (; csr2; csr2 = csr2->next) {
  2920. ret = TLSX_UseCertificateStatusRequestV2(&ssl->extensions,
  2921. csr2->status_type, csr2->options, ssl->heap,
  2922. ssl->devId);
  2923. if (ret != WOLFSSL_SUCCESS)
  2924. return ret;
  2925. switch (csr2->status_type) {
  2926. case WOLFSSL_CSR2_OCSP:
  2927. /* followed by */
  2928. case WOLFSSL_CSR2_OCSP_MULTI:
  2929. /* propagate nonce */
  2930. if (csr2->request.ocsp[0].nonceSz) {
  2931. OcspRequest* request =
  2932. (OcspRequest*)TLSX_CSR2_GetRequest(ssl->extensions,
  2933. csr2->status_type, 0);
  2934. if (request) {
  2935. XMEMCPY(request->nonce,
  2936. csr2->request.ocsp[0].nonce,
  2937. csr2->request.ocsp[0].nonceSz);
  2938. request->nonceSz =
  2939. csr2->request.ocsp[0].nonceSz;
  2940. }
  2941. }
  2942. break;
  2943. }
  2944. }
  2945. }
  2946. ssl->status_request_v2 = 1;
  2947. return length ? BUFFER_ERROR : 0; /* extension_data MUST be empty. */
  2948. #endif
  2949. }
  2950. else {
  2951. #ifndef NO_WOLFSSL_SERVER
  2952. byte status_type;
  2953. word16 request_length;
  2954. word16 offset = 0;
  2955. word16 size = 0;
  2956. /* list size */
  2957. if (offset + OPAQUE16_LEN >= length) {
  2958. return BUFFER_E;
  2959. }
  2960. ato16(input + offset, &request_length);
  2961. offset += OPAQUE16_LEN;
  2962. if (length - OPAQUE16_LEN != request_length)
  2963. return BUFFER_ERROR;
  2964. while (length > offset) {
  2965. if ((int)(length - offset) < ENUM_LEN + OPAQUE16_LEN)
  2966. return BUFFER_ERROR;
  2967. status_type = input[offset++];
  2968. ato16(input + offset, &request_length);
  2969. offset += OPAQUE16_LEN;
  2970. if (length - offset < request_length)
  2971. return BUFFER_ERROR;
  2972. switch (status_type) {
  2973. case WOLFSSL_CSR2_OCSP:
  2974. case WOLFSSL_CSR2_OCSP_MULTI:
  2975. /* skip responder_id_list */
  2976. if ((int)(length - offset) < OPAQUE16_LEN)
  2977. return BUFFER_ERROR;
  2978. ato16(input + offset, &size);
  2979. if (length - offset < size)
  2980. return BUFFER_ERROR;
  2981. offset += OPAQUE16_LEN + size;
  2982. /* skip request_extensions */
  2983. if ((int)(length - offset) < OPAQUE16_LEN)
  2984. return BUFFER_ERROR;
  2985. ato16(input + offset, &size);
  2986. if (length - offset < size)
  2987. return BUFFER_ERROR;
  2988. offset += OPAQUE16_LEN + size;
  2989. if (offset > length)
  2990. return BUFFER_ERROR;
  2991. /* is able to send OCSP response? */
  2992. if (SSL_CM(ssl) == NULL
  2993. || !SSL_CM(ssl)->ocspStaplingEnabled)
  2994. continue;
  2995. break;
  2996. default:
  2997. /* unknown status type, skipping! */
  2998. offset += request_length;
  2999. continue;
  3000. }
  3001. /* if using status_request and already sending it, remove it
  3002. * and prefer to use the v2 version */
  3003. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  3004. if (ssl->status_request) {
  3005. ssl->status_request = 0;
  3006. TLSX_Remove(&ssl->extensions, TLSX_STATUS_REQUEST, ssl->heap);
  3007. }
  3008. #endif
  3009. /* TLS 1.3 servers MUST NOT act upon presence or information in
  3010. * this extension (RFC 8448 Section 4.4.2.1).
  3011. */
  3012. if (!IsAtLeastTLSv1_3(ssl->version)) {
  3013. /* accept the first good status_type and return */
  3014. ret = TLSX_UseCertificateStatusRequestV2(&ssl->extensions,
  3015. status_type, 0, ssl->heap, ssl->devId);
  3016. if (ret != WOLFSSL_SUCCESS)
  3017. return ret; /* throw error */
  3018. TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST_V2);
  3019. ssl->status_request_v2 = status_type;
  3020. }
  3021. return 0;
  3022. }
  3023. #endif
  3024. }
  3025. return 0;
  3026. }
  3027. int TLSX_CSR2_InitRequests(TLSX* extensions, DecodedCert* cert, byte isPeer,
  3028. void* heap)
  3029. {
  3030. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST_V2);
  3031. CertificateStatusRequestItemV2* csr2 = extension ?
  3032. (CertificateStatusRequestItemV2*)extension->data : NULL;
  3033. int ret = 0;
  3034. for (; csr2; csr2 = csr2->next) {
  3035. switch (csr2->status_type) {
  3036. case WOLFSSL_CSR2_OCSP:
  3037. if (!isPeer || csr2->requests != 0)
  3038. break;
  3039. FALL_THROUGH; /* followed by */
  3040. case WOLFSSL_CSR2_OCSP_MULTI: {
  3041. if (csr2->requests < 1 + MAX_CHAIN_DEPTH) {
  3042. byte nonce[MAX_OCSP_NONCE_SZ];
  3043. int nonceSz = csr2->request.ocsp[0].nonceSz;
  3044. /* preserve nonce, replicating nonce of ocsp[0] */
  3045. XMEMCPY(nonce, csr2->request.ocsp[0].nonce, nonceSz);
  3046. if ((ret = InitOcspRequest(
  3047. &csr2->request.ocsp[csr2->requests], cert,
  3048. 0, heap)) != 0)
  3049. return ret;
  3050. /* restore nonce */
  3051. XMEMCPY(csr2->request.ocsp[csr2->requests].nonce,
  3052. nonce, nonceSz);
  3053. csr2->request.ocsp[csr2->requests].nonceSz = nonceSz;
  3054. csr2->requests++;
  3055. }
  3056. }
  3057. break;
  3058. }
  3059. }
  3060. (void)cert;
  3061. return ret;
  3062. }
  3063. void* TLSX_CSR2_GetRequest(TLSX* extensions, byte status_type, byte idx)
  3064. {
  3065. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST_V2);
  3066. CertificateStatusRequestItemV2* csr2 = extension ?
  3067. (CertificateStatusRequestItemV2*)extension->data : NULL;
  3068. for (; csr2; csr2 = csr2->next) {
  3069. if (csr2->status_type == status_type) {
  3070. switch (csr2->status_type) {
  3071. case WOLFSSL_CSR2_OCSP:
  3072. /* followed by */
  3073. case WOLFSSL_CSR2_OCSP_MULTI:
  3074. /* requests are initialized in the reverse order */
  3075. return idx < csr2->requests
  3076. ? &csr2->request.ocsp[csr2->requests - idx - 1]
  3077. : NULL;
  3078. }
  3079. }
  3080. }
  3081. return NULL;
  3082. }
  3083. int TLSX_CSR2_ForceRequest(WOLFSSL* ssl)
  3084. {
  3085. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST_V2);
  3086. CertificateStatusRequestItemV2* csr2 = extension ?
  3087. (CertificateStatusRequestItemV2*)extension->data : NULL;
  3088. /* forces only the first one */
  3089. if (csr2) {
  3090. switch (csr2->status_type) {
  3091. case WOLFSSL_CSR2_OCSP:
  3092. /* followed by */
  3093. case WOLFSSL_CSR2_OCSP_MULTI:
  3094. if (SSL_CM(ssl)->ocspEnabled) {
  3095. csr2->request.ocsp[0].ssl = ssl;
  3096. return CheckOcspRequest(SSL_CM(ssl)->ocsp,
  3097. &csr2->request.ocsp[0], NULL);
  3098. }
  3099. else {
  3100. WOLFSSL_ERROR_VERBOSE(OCSP_LOOKUP_FAIL);
  3101. return OCSP_LOOKUP_FAIL;
  3102. }
  3103. }
  3104. }
  3105. return 0;
  3106. }
  3107. int TLSX_UseCertificateStatusRequestV2(TLSX** extensions, byte status_type,
  3108. byte options, void* heap, int devId)
  3109. {
  3110. TLSX* extension = NULL;
  3111. CertificateStatusRequestItemV2* csr2 = NULL;
  3112. int ret = 0;
  3113. if (!extensions)
  3114. return BAD_FUNC_ARG;
  3115. if (status_type != WOLFSSL_CSR2_OCSP
  3116. && status_type != WOLFSSL_CSR2_OCSP_MULTI)
  3117. return BAD_FUNC_ARG;
  3118. csr2 = (CertificateStatusRequestItemV2*)
  3119. XMALLOC(sizeof(CertificateStatusRequestItemV2), heap, DYNAMIC_TYPE_TLSX);
  3120. if (!csr2)
  3121. return MEMORY_E;
  3122. ForceZero(csr2, sizeof(CertificateStatusRequestItemV2));
  3123. csr2->status_type = status_type;
  3124. csr2->options = options;
  3125. csr2->next = NULL;
  3126. switch (csr2->status_type) {
  3127. case WOLFSSL_CSR2_OCSP:
  3128. case WOLFSSL_CSR2_OCSP_MULTI:
  3129. if (options & WOLFSSL_CSR2_OCSP_USE_NONCE) {
  3130. WC_RNG rng;
  3131. #ifndef HAVE_FIPS
  3132. ret = wc_InitRng_ex(&rng, heap, devId);
  3133. #else
  3134. ret = wc_InitRng(&rng);
  3135. (void)devId;
  3136. #endif
  3137. if (ret == 0) {
  3138. if (wc_RNG_GenerateBlock(&rng, csr2->request.ocsp[0].nonce,
  3139. MAX_OCSP_NONCE_SZ) == 0)
  3140. csr2->request.ocsp[0].nonceSz = MAX_OCSP_NONCE_SZ;
  3141. wc_FreeRng(&rng);
  3142. }
  3143. }
  3144. break;
  3145. }
  3146. /* append new item */
  3147. if ((extension = TLSX_Find(*extensions, TLSX_STATUS_REQUEST_V2))) {
  3148. CertificateStatusRequestItemV2* last =
  3149. (CertificateStatusRequestItemV2*)extension->data;
  3150. for (; last->next; last = last->next);
  3151. last->next = csr2;
  3152. }
  3153. else if ((ret = TLSX_Push(extensions, TLSX_STATUS_REQUEST_V2, csr2,heap))) {
  3154. XFREE(csr2, heap, DYNAMIC_TYPE_TLSX);
  3155. return ret;
  3156. }
  3157. return WOLFSSL_SUCCESS;
  3158. }
  3159. #define CSR2_FREE_ALL TLSX_CSR2_FreeAll
  3160. #define CSR2_GET_SIZE TLSX_CSR2_GetSize
  3161. #define CSR2_WRITE TLSX_CSR2_Write
  3162. #define CSR2_PARSE TLSX_CSR2_Parse
  3163. #else
  3164. #define CSR2_FREE_ALL(data, heap)
  3165. #define CSR2_GET_SIZE(a, b) 0
  3166. #define CSR2_WRITE(a, b, c) 0
  3167. #define CSR2_PARSE(a, b, c, d) 0
  3168. #endif /* HAVE_CERTIFICATE_STATUS_REQUEST_V2 */
  3169. /******************************************************************************/
  3170. /* Supported Elliptic Curves */
  3171. /******************************************************************************/
  3172. #ifdef HAVE_SUPPORTED_CURVES
  3173. #if !defined(HAVE_ECC) && !defined(HAVE_CURVE25519) && !defined(HAVE_CURVE448) \
  3174. && !defined(HAVE_FFDHE) && !defined(HAVE_PQC)
  3175. #error Elliptic Curves Extension requires Elliptic Curve Cryptography or liboqs groups. \
  3176. Use --enable-ecc and/or --enable-liboqs in the configure script or \
  3177. define HAVE_ECC. Alternatively use FFDHE for DH ciphersuites.
  3178. #endif
  3179. static int TLSX_SupportedCurve_New(SupportedCurve** curve, word16 name,
  3180. void* heap)
  3181. {
  3182. if (curve == NULL)
  3183. return BAD_FUNC_ARG;
  3184. (void)heap;
  3185. *curve = (SupportedCurve*)XMALLOC(sizeof(SupportedCurve), heap,
  3186. DYNAMIC_TYPE_TLSX);
  3187. if (*curve == NULL)
  3188. return MEMORY_E;
  3189. (*curve)->name = name;
  3190. (*curve)->next = NULL;
  3191. return 0;
  3192. }
  3193. static int TLSX_PointFormat_New(PointFormat** point, byte format, void* heap)
  3194. {
  3195. if (point == NULL)
  3196. return BAD_FUNC_ARG;
  3197. (void)heap;
  3198. *point = (PointFormat*)XMALLOC(sizeof(PointFormat), heap,
  3199. DYNAMIC_TYPE_TLSX);
  3200. if (*point == NULL)
  3201. return MEMORY_E;
  3202. (*point)->format = format;
  3203. (*point)->next = NULL;
  3204. return 0;
  3205. }
  3206. static void TLSX_SupportedCurve_FreeAll(SupportedCurve* list, void* heap)
  3207. {
  3208. SupportedCurve* curve;
  3209. while ((curve = list)) {
  3210. list = curve->next;
  3211. XFREE(curve, heap, DYNAMIC_TYPE_TLSX);
  3212. }
  3213. (void)heap;
  3214. }
  3215. static void TLSX_PointFormat_FreeAll(PointFormat* list, void* heap)
  3216. {
  3217. PointFormat* point;
  3218. while ((point = list)) {
  3219. list = point->next;
  3220. XFREE(point, heap, DYNAMIC_TYPE_TLSX);
  3221. }
  3222. (void)heap;
  3223. }
  3224. static int TLSX_SupportedCurve_Append(SupportedCurve* list, word16 name,
  3225. void* heap)
  3226. {
  3227. int ret = BAD_FUNC_ARG;
  3228. while (list) {
  3229. if (list->name == name) {
  3230. ret = 0; /* curve already in use */
  3231. break;
  3232. }
  3233. if (list->next == NULL) {
  3234. ret = TLSX_SupportedCurve_New(&list->next, name, heap);
  3235. break;
  3236. }
  3237. list = list->next;
  3238. }
  3239. return ret;
  3240. }
  3241. static int TLSX_PointFormat_Append(PointFormat* list, byte format, void* heap)
  3242. {
  3243. int ret = BAD_FUNC_ARG;
  3244. while (list) {
  3245. if (list->format == format) {
  3246. ret = 0; /* format already in use */
  3247. break;
  3248. }
  3249. if (list->next == NULL) {
  3250. ret = TLSX_PointFormat_New(&list->next, format, heap);
  3251. break;
  3252. }
  3253. list = list->next;
  3254. }
  3255. return ret;
  3256. }
  3257. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
  3258. #if defined(HAVE_FFDHE) && (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3259. defined(HAVE_CURVE448))
  3260. static void TLSX_SupportedCurve_ValidateRequest(const WOLFSSL* ssl,
  3261. const byte* semaphore)
  3262. {
  3263. /* If all pre-defined parameter types for key exchange are supported then
  3264. * always send SupportedGroups extension.
  3265. */
  3266. (void)ssl;
  3267. (void)semaphore;
  3268. }
  3269. #else
  3270. static void TLSX_SupportedCurve_ValidateRequest(WOLFSSL* ssl, byte* semaphore)
  3271. {
  3272. word16 i;
  3273. const Suites* suites = WOLFSSL_SUITES(ssl);
  3274. for (i = 0; i < suites->suiteSz; i += 2) {
  3275. if (suites->suites[i] == TLS13_BYTE)
  3276. return;
  3277. if ((suites->suites[i] == ECC_BYTE) ||
  3278. (suites->suites[i] == ECDHE_PSK_BYTE) ||
  3279. (suites->suites[i] == CHACHA_BYTE)) {
  3280. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3281. defined(HAVE_CURVE448)
  3282. return;
  3283. #endif
  3284. }
  3285. #ifdef HAVE_FFDHE
  3286. else {
  3287. return;
  3288. }
  3289. #endif
  3290. }
  3291. /* turns semaphore on to avoid sending this extension. */
  3292. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_GROUPS));
  3293. }
  3294. #endif
  3295. /* Only send PointFormats if TLSv13, ECC or CHACHA cipher suite present.
  3296. */
  3297. static void TLSX_PointFormat_ValidateRequest(WOLFSSL* ssl, byte* semaphore)
  3298. {
  3299. #ifdef HAVE_FFDHE
  3300. (void)ssl;
  3301. (void)semaphore;
  3302. #else
  3303. word16 i;
  3304. const Suites* suites = WOLFSSL_SUITES(ssl);
  3305. if (suites == NULL)
  3306. return;
  3307. for (i = 0; i < suites->suiteSz; i += 2) {
  3308. if (suites->suites[i] == TLS13_BYTE)
  3309. return;
  3310. if ((suites->suites[i] == ECC_BYTE) ||
  3311. (suites->suites[i] == ECDHE_PSK_BYTE) ||
  3312. (suites->suites[i] == CHACHA_BYTE)) {
  3313. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3314. defined(HAVE_CURVE448)
  3315. return;
  3316. #endif
  3317. }
  3318. }
  3319. /* turns semaphore on to avoid sending this extension. */
  3320. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  3321. #endif
  3322. }
  3323. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
  3324. #ifndef NO_WOLFSSL_SERVER
  3325. static void TLSX_PointFormat_ValidateResponse(WOLFSSL* ssl, byte* semaphore)
  3326. {
  3327. #if defined(HAVE_FFDHE) || defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3328. defined(HAVE_CURVE448)
  3329. (void)semaphore;
  3330. #endif
  3331. if (ssl->options.cipherSuite0 == TLS13_BYTE)
  3332. return;
  3333. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  3334. if (ssl->options.cipherSuite0 == ECC_BYTE ||
  3335. ssl->options.cipherSuite0 == ECDHE_PSK_BYTE ||
  3336. ssl->options.cipherSuite0 == CHACHA_BYTE) {
  3337. return;
  3338. }
  3339. #endif
  3340. /* turns semaphore on to avoid sending this extension. */
  3341. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  3342. }
  3343. #endif /* !NO_WOLFSSL_SERVER */
  3344. #ifndef NO_WOLFSSL_CLIENT
  3345. static word16 TLSX_SupportedCurve_GetSize(SupportedCurve* list)
  3346. {
  3347. SupportedCurve* curve;
  3348. word16 length = OPAQUE16_LEN; /* list length */
  3349. while ((curve = list)) {
  3350. list = curve->next;
  3351. length += OPAQUE16_LEN; /* curve length */
  3352. }
  3353. return length;
  3354. }
  3355. #endif
  3356. static word16 TLSX_PointFormat_GetSize(PointFormat* list)
  3357. {
  3358. PointFormat* point;
  3359. word16 length = ENUM_LEN; /* list length */
  3360. while ((point = list)) {
  3361. list = point->next;
  3362. length += ENUM_LEN; /* format length */
  3363. }
  3364. return length;
  3365. }
  3366. #ifndef NO_WOLFSSL_CLIENT
  3367. static word16 TLSX_SupportedCurve_Write(SupportedCurve* list, byte* output)
  3368. {
  3369. word16 offset = OPAQUE16_LEN;
  3370. while (list) {
  3371. c16toa(list->name, output + offset);
  3372. offset += OPAQUE16_LEN;
  3373. list = list->next;
  3374. }
  3375. c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
  3376. return offset;
  3377. }
  3378. #endif
  3379. static word16 TLSX_PointFormat_Write(PointFormat* list, byte* output)
  3380. {
  3381. word16 offset = ENUM_LEN;
  3382. while (list) {
  3383. output[offset++] = list->format;
  3384. list = list->next;
  3385. }
  3386. output[0] = (byte)(offset - ENUM_LEN);
  3387. return offset;
  3388. }
  3389. #if !defined(NO_WOLFSSL_SERVER) || (defined(WOLFSSL_TLS13) && \
  3390. !defined(WOLFSSL_NO_SERVER_GROUPS_EXT))
  3391. int TLSX_SupportedCurve_Parse(const WOLFSSL* ssl, const byte* input,
  3392. word16 length, byte isRequest, TLSX** extensions)
  3393. {
  3394. word16 offset;
  3395. word16 name;
  3396. int ret;
  3397. if(!isRequest && !IsAtLeastTLSv1_3(ssl->version)) {
  3398. #ifdef WOLFSSL_ALLOW_SERVER_SC_EXT
  3399. return 0;
  3400. #else
  3401. return BUFFER_ERROR; /* servers doesn't send this extension. */
  3402. #endif
  3403. }
  3404. if (OPAQUE16_LEN > length || length % OPAQUE16_LEN)
  3405. return BUFFER_ERROR;
  3406. ato16(input, &offset);
  3407. /* validating curve list length */
  3408. if (length != OPAQUE16_LEN + offset)
  3409. return BUFFER_ERROR;
  3410. offset = OPAQUE16_LEN;
  3411. if (offset == length)
  3412. return 0;
  3413. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
  3414. if (!isRequest) {
  3415. TLSX* extension;
  3416. SupportedCurve* curve;
  3417. extension = TLSX_Find(*extensions, TLSX_SUPPORTED_GROUPS);
  3418. if (extension != NULL) {
  3419. /* Replace client list with server list of supported groups. */
  3420. curve = (SupportedCurve*)extension->data;
  3421. extension->data = NULL;
  3422. TLSX_SupportedCurve_FreeAll(curve, ssl->heap);
  3423. ato16(input + offset, &name);
  3424. offset += OPAQUE16_LEN;
  3425. ret = TLSX_SupportedCurve_New(&curve, name, ssl->heap);
  3426. if (ret != 0)
  3427. return ret; /* throw error */
  3428. extension->data = (void*)curve;
  3429. }
  3430. }
  3431. #endif
  3432. for (; offset < length; offset += OPAQUE16_LEN) {
  3433. ato16(input + offset, &name);
  3434. ret = TLSX_UseSupportedCurve(extensions, name, ssl->heap);
  3435. /* If it is BAD_FUNC_ARG then it is a group we do not support, but
  3436. * that is fine. */
  3437. if (ret != WOLFSSL_SUCCESS && ret != BAD_FUNC_ARG) {
  3438. return ret;
  3439. }
  3440. }
  3441. return 0;
  3442. }
  3443. #endif
  3444. #if !defined(NO_WOLFSSL_SERVER)
  3445. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
  3446. /* Checks the priority of the groups on the server and set the supported groups
  3447. * response if there is a group not advertised by the client that is preferred.
  3448. *
  3449. * ssl SSL/TLS object.
  3450. * returns 0 on success, otherwise an error.
  3451. */
  3452. int TLSX_SupportedCurve_CheckPriority(WOLFSSL* ssl)
  3453. {
  3454. int ret;
  3455. TLSX* extension;
  3456. TLSX* priority = NULL;
  3457. TLSX* ext = NULL;
  3458. word16 name;
  3459. SupportedCurve* curve;
  3460. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3461. /* May be doing PSK with no key exchange. */
  3462. if (extension == NULL)
  3463. return 0;
  3464. ret = TLSX_PopulateSupportedGroups(ssl, &priority);
  3465. if (ret != WOLFSSL_SUCCESS) {
  3466. TLSX_FreeAll(priority, ssl->heap);
  3467. return ret;
  3468. }
  3469. ext = TLSX_Find(priority, TLSX_SUPPORTED_GROUPS);
  3470. if (ext == NULL) {
  3471. WOLFSSL_MSG("Could not find supported groups extension");
  3472. TLSX_FreeAll(priority, ssl->heap);
  3473. return 0;
  3474. }
  3475. curve = (SupportedCurve*)ext->data;
  3476. name = curve->name;
  3477. curve = (SupportedCurve*)extension->data;
  3478. while (curve != NULL) {
  3479. if (curve->name == name)
  3480. break;
  3481. curve = curve->next;
  3482. }
  3483. if (curve == NULL) {
  3484. /* Couldn't find the preferred group in client list. */
  3485. extension->resp = 1;
  3486. /* Send server list back and free client list. */
  3487. curve = (SupportedCurve*)extension->data;
  3488. extension->data = ext->data;
  3489. ext->data = curve;
  3490. }
  3491. TLSX_FreeAll(priority, ssl->heap);
  3492. return 0;
  3493. }
  3494. #endif /* WOLFSSL_TLS13 && !WOLFSSL_NO_SERVER_GROUPS_EXT */
  3495. #if defined(HAVE_FFDHE) && !defined(WOLFSSL_NO_TLS12)
  3496. #ifdef HAVE_PUBLIC_FFDHE
  3497. static int tlsx_ffdhe_find_group(WOLFSSL* ssl, SupportedCurve* clientGroup,
  3498. SupportedCurve* serverGroup)
  3499. {
  3500. int ret = 0;
  3501. SupportedCurve* group;
  3502. const DhParams* params = NULL;
  3503. for (; serverGroup != NULL; serverGroup = serverGroup->next) {
  3504. if (!WOLFSSL_NAMED_GROUP_IS_FFHDE(serverGroup->name))
  3505. continue;
  3506. for (group = clientGroup; group != NULL; group = group->next) {
  3507. if (serverGroup->name != group->name)
  3508. continue;
  3509. switch (serverGroup->name) {
  3510. #ifdef HAVE_FFDHE_2048
  3511. case WOLFSSL_FFDHE_2048:
  3512. params = wc_Dh_ffdhe2048_Get();
  3513. break;
  3514. #endif
  3515. #ifdef HAVE_FFDHE_3072
  3516. case WOLFSSL_FFDHE_3072:
  3517. params = wc_Dh_ffdhe3072_Get();
  3518. break;
  3519. #endif
  3520. #ifdef HAVE_FFDHE_4096
  3521. case WOLFSSL_FFDHE_4096:
  3522. params = wc_Dh_ffdhe4096_Get();
  3523. break;
  3524. #endif
  3525. #ifdef HAVE_FFDHE_6144
  3526. case WOLFSSL_FFDHE_6144:
  3527. params = wc_Dh_ffdhe6144_Get();
  3528. break;
  3529. #endif
  3530. #ifdef HAVE_FFDHE_8192
  3531. case WOLFSSL_FFDHE_8192:
  3532. params = wc_Dh_ffdhe8192_Get();
  3533. break;
  3534. #endif
  3535. default:
  3536. break;
  3537. }
  3538. if (params == NULL) {
  3539. ret = BAD_FUNC_ARG;
  3540. break;
  3541. }
  3542. if (params->p_len >= ssl->options.minDhKeySz &&
  3543. params->p_len <= ssl->options.maxDhKeySz) {
  3544. break;
  3545. }
  3546. }
  3547. if (ret != 0)
  3548. break;
  3549. if ((group != NULL) && (serverGroup->name == group->name))
  3550. break;
  3551. }
  3552. if ((ret == 0) && (serverGroup != NULL) && (params != NULL)) {
  3553. ssl->buffers.serverDH_P.buffer = (unsigned char *)params->p;
  3554. ssl->buffers.serverDH_P.length = params->p_len;
  3555. ssl->buffers.serverDH_G.buffer = (unsigned char *)params->g;
  3556. ssl->buffers.serverDH_G.length = params->g_len;
  3557. ssl->namedGroup = serverGroup->name;
  3558. #if !defined(WOLFSSL_OLD_PRIME_CHECK) && \
  3559. !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  3560. ssl->options.dhDoKeyTest = 0;
  3561. #endif
  3562. ssl->options.haveDH = 1;
  3563. }
  3564. return ret;
  3565. }
  3566. #else
  3567. static int tlsx_ffdhe_find_group(WOLFSSL* ssl, SupportedCurve* clientGroup,
  3568. SupportedCurve* serverGroup)
  3569. {
  3570. int ret = 0;
  3571. SupportedCurve* group;
  3572. word32 p_len;
  3573. for (; serverGroup != NULL; serverGroup = serverGroup->next) {
  3574. if (!WOLFSSL_NAMED_GROUP_IS_FFHDE(serverGroup->name))
  3575. continue;
  3576. for (group = clientGroup; group != NULL; group = group->next) {
  3577. if (serverGroup->name != group->name)
  3578. continue;
  3579. wc_DhGetNamedKeyParamSize(serverGroup->name, &p_len, NULL, NULL);
  3580. if (p_len == 0) {
  3581. ret = BAD_FUNC_ARG;
  3582. break;
  3583. }
  3584. if (p_len >= ssl->options.minDhKeySz &&
  3585. p_len <= ssl->options.maxDhKeySz) {
  3586. break;
  3587. }
  3588. }
  3589. if (ret != 0)
  3590. break;
  3591. if ((group != NULL) && (serverGroup->name == group->name))
  3592. break;
  3593. }
  3594. if ((ret == 0) && (serverGroup != NULL)) {
  3595. word32 pSz, gSz;
  3596. ssl->buffers.serverDH_P.buffer = NULL;
  3597. ssl->buffers.serverDH_G.buffer = NULL;
  3598. ret = wc_DhGetNamedKeyParamSize(serverGroup->name, &pSz, &gSz, NULL);
  3599. if (ret == 0) {
  3600. ssl->buffers.serverDH_P.buffer =
  3601. (byte*)XMALLOC(pSz, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  3602. if (ssl->buffers.serverDH_P.buffer == NULL)
  3603. ret = MEMORY_E;
  3604. else
  3605. ssl->buffers.serverDH_P.length = pSz;
  3606. }
  3607. if (ret == 0) {
  3608. ssl->buffers.serverDH_G.buffer =
  3609. (byte*)XMALLOC(gSz, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  3610. if (ssl->buffers.serverDH_G.buffer == NULL) {
  3611. ret = MEMORY_E;
  3612. } else
  3613. ssl->buffers.serverDH_G.length = gSz;
  3614. }
  3615. if (ret == 0) {
  3616. ret = wc_DhCopyNamedKey(serverGroup->name,
  3617. ssl->buffers.serverDH_P.buffer, &pSz,
  3618. ssl->buffers.serverDH_G.buffer, &gSz,
  3619. NULL, NULL);
  3620. }
  3621. if (ret == 0) {
  3622. ssl->buffers.weOwnDH = 1;
  3623. ssl->namedGroup = serverGroup->name;
  3624. #if !defined(WOLFSSL_OLD_PRIME_CHECK) && \
  3625. !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  3626. ssl->options.dhDoKeyTest = 0;
  3627. #endif
  3628. ssl->options.haveDH = 1;
  3629. }
  3630. else {
  3631. if (ssl->buffers.serverDH_P.buffer != NULL) {
  3632. XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap,
  3633. DYNAMIC_TYPE_PUBLIC_KEY);
  3634. ssl->buffers.serverDH_P.length = 0;
  3635. ssl->buffers.serverDH_P.buffer = NULL;
  3636. }
  3637. if (ssl->buffers.serverDH_G.buffer != NULL) {
  3638. XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap,
  3639. DYNAMIC_TYPE_PUBLIC_KEY);
  3640. ssl->buffers.serverDH_G.length = 0;
  3641. ssl->buffers.serverDH_G.buffer = NULL;
  3642. }
  3643. }
  3644. }
  3645. return ret;
  3646. }
  3647. #endif
  3648. /* Set the highest priority common FFDHE group on the server as compared to
  3649. * client extensions.
  3650. *
  3651. * ssl SSL/TLS object.
  3652. * returns 0 on success, otherwise an error.
  3653. */
  3654. int TLSX_SupportedFFDHE_Set(WOLFSSL* ssl)
  3655. {
  3656. int ret;
  3657. TLSX* priority = NULL;
  3658. TLSX* ext = NULL;
  3659. TLSX* extension;
  3660. SupportedCurve* clientGroup;
  3661. SupportedCurve* group;
  3662. int found = 0;
  3663. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3664. /* May be doing PSK with no key exchange. */
  3665. if (extension == NULL)
  3666. return 0;
  3667. clientGroup = (SupportedCurve*)extension->data;
  3668. for (group = clientGroup; group != NULL; group = group->next) {
  3669. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(group->name)) {
  3670. found = 1;
  3671. break;
  3672. }
  3673. }
  3674. if (!found)
  3675. return 0;
  3676. if (ssl->buffers.serverDH_P.buffer && ssl->buffers.weOwnDH) {
  3677. XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap,
  3678. DYNAMIC_TYPE_PUBLIC_KEY);
  3679. }
  3680. if (ssl->buffers.serverDH_G.buffer && ssl->buffers.weOwnDH) {
  3681. XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap,
  3682. DYNAMIC_TYPE_PUBLIC_KEY);
  3683. }
  3684. ssl->buffers.serverDH_P.buffer = NULL;
  3685. ssl->buffers.serverDH_G.buffer = NULL;
  3686. ssl->buffers.weOwnDH = 0;
  3687. ssl->options.haveDH = 0;
  3688. ret = TLSX_PopulateSupportedGroups(ssl, &priority);
  3689. if (ret == WOLFSSL_SUCCESS) {
  3690. SupportedCurve* serverGroup;
  3691. ext = TLSX_Find(priority, TLSX_SUPPORTED_GROUPS);
  3692. serverGroup = (SupportedCurve*)ext->data;
  3693. ret = tlsx_ffdhe_find_group(ssl, clientGroup, serverGroup);
  3694. }
  3695. TLSX_FreeAll(priority, ssl->heap);
  3696. return ret;
  3697. }
  3698. #endif /* HAVE_FFDHE && !WOLFSSL_NO_TLS12 */
  3699. #endif /* !NO_WOLFSSL_SERVER */
  3700. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
  3701. /* Return the preferred group.
  3702. *
  3703. * ssl SSL/TLS object.
  3704. * checkSupported Whether to check for the first supported group.
  3705. * returns BAD_FUNC_ARG if no group found, otherwise the group.
  3706. */
  3707. int TLSX_SupportedCurve_Preferred(WOLFSSL* ssl, int checkSupported)
  3708. {
  3709. TLSX* extension;
  3710. SupportedCurve* curve;
  3711. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3712. if (extension == NULL)
  3713. return BAD_FUNC_ARG;
  3714. curve = (SupportedCurve*)extension->data;
  3715. while (curve != NULL) {
  3716. if (!checkSupported || TLSX_KeyShare_IsSupported(curve->name))
  3717. return curve->name;
  3718. curve = curve->next;
  3719. }
  3720. return BAD_FUNC_ARG;
  3721. }
  3722. #endif /* HAVE_SUPPORTED_CURVES */
  3723. #ifndef NO_WOLFSSL_SERVER
  3724. static int TLSX_PointFormat_Parse(WOLFSSL* ssl, const byte* input,
  3725. word16 length, byte isRequest)
  3726. {
  3727. int ret;
  3728. /* validating formats list length */
  3729. if (ENUM_LEN > length || length != (word16)ENUM_LEN + input[0])
  3730. return BUFFER_ERROR;
  3731. if (isRequest) {
  3732. /* adding uncompressed point format to response */
  3733. ret = TLSX_UsePointFormat(&ssl->extensions, WOLFSSL_EC_PF_UNCOMPRESSED,
  3734. ssl->heap);
  3735. if (ret != WOLFSSL_SUCCESS)
  3736. return ret; /* throw error */
  3737. TLSX_SetResponse(ssl, TLSX_EC_POINT_FORMATS);
  3738. }
  3739. return 0;
  3740. }
  3741. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  3742. int TLSX_ValidateSupportedCurves(const WOLFSSL* ssl, byte first, byte second,
  3743. word32* ecdhCurveOID) {
  3744. TLSX* extension = NULL;
  3745. SupportedCurve* curve = NULL;
  3746. word32 oid = 0;
  3747. word32 defOid = 0;
  3748. word32 defSz = 80; /* Maximum known curve size is 66. */
  3749. word32 nextOid = 0;
  3750. word32 nextSz = 80; /* Maximum known curve size is 66. */
  3751. word32 currOid = ssl->ecdhCurveOID;
  3752. int ephmSuite = 0;
  3753. word16 octets = 0; /* according to 'ecc_set_type ecc_sets[];' */
  3754. int key = 0; /* validate key */
  3755. (void)oid;
  3756. if (first == CHACHA_BYTE) {
  3757. switch (second) {
  3758. case TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
  3759. case TLS_PSK_WITH_CHACHA20_POLY1305_SHA256:
  3760. case TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
  3761. case TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256:
  3762. return 1; /* no suite restriction */
  3763. case TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
  3764. case TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256:
  3765. case TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
  3766. break;
  3767. }
  3768. }
  3769. if (first == ECC_BYTE || first == ECDHE_PSK_BYTE || first == CHACHA_BYTE)
  3770. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3771. if (!extension)
  3772. return 1; /* no suite restriction */
  3773. for (curve = (SupportedCurve*)extension->data;
  3774. curve && !key;
  3775. curve = curve->next) {
  3776. #ifdef OPENSSL_EXTRA
  3777. /* skip if name is not in supported ECC range
  3778. * or disabled by user */
  3779. if (curve->name > WOLFSSL_ECC_MAX ||
  3780. wolfSSL_curve_is_disabled(ssl, curve->name))
  3781. continue;
  3782. #endif
  3783. /* find supported curve */
  3784. switch (curve->name) {
  3785. #ifdef HAVE_ECC
  3786. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  3787. #ifndef NO_ECC_SECP
  3788. case WOLFSSL_ECC_SECP160R1:
  3789. oid = ECC_SECP160R1_OID;
  3790. octets = 20;
  3791. break;
  3792. #endif /* !NO_ECC_SECP */
  3793. #ifdef HAVE_ECC_SECPR2
  3794. case WOLFSSL_ECC_SECP160R2:
  3795. oid = ECC_SECP160R2_OID;
  3796. octets = 20;
  3797. break;
  3798. #endif /* HAVE_ECC_SECPR2 */
  3799. #ifdef HAVE_ECC_KOBLITZ
  3800. case WOLFSSL_ECC_SECP160K1:
  3801. oid = ECC_SECP160K1_OID;
  3802. octets = 20;
  3803. break;
  3804. #endif /* HAVE_ECC_KOBLITZ */
  3805. #endif
  3806. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  3807. #ifndef NO_ECC_SECP
  3808. case WOLFSSL_ECC_SECP192R1:
  3809. oid = ECC_SECP192R1_OID;
  3810. octets = 24;
  3811. break;
  3812. #endif /* !NO_ECC_SECP */
  3813. #ifdef HAVE_ECC_KOBLITZ
  3814. case WOLFSSL_ECC_SECP192K1:
  3815. oid = ECC_SECP192K1_OID;
  3816. octets = 24;
  3817. break;
  3818. #endif /* HAVE_ECC_KOBLITZ */
  3819. #endif
  3820. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  3821. #ifndef NO_ECC_SECP
  3822. case WOLFSSL_ECC_SECP224R1:
  3823. oid = ECC_SECP224R1_OID;
  3824. octets = 28;
  3825. break;
  3826. #endif /* !NO_ECC_SECP */
  3827. #ifdef HAVE_ECC_KOBLITZ
  3828. case WOLFSSL_ECC_SECP224K1:
  3829. oid = ECC_SECP224K1_OID;
  3830. octets = 28;
  3831. break;
  3832. #endif /* HAVE_ECC_KOBLITZ */
  3833. #endif
  3834. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  3835. #ifndef NO_ECC_SECP
  3836. case WOLFSSL_ECC_SECP256R1:
  3837. oid = ECC_SECP256R1_OID;
  3838. octets = 32;
  3839. break;
  3840. #endif /* !NO_ECC_SECP */
  3841. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  3842. #endif
  3843. #if (defined(HAVE_CURVE25519) || defined(HAVE_ED25519)) && ECC_MIN_KEY_SZ <= 256
  3844. case WOLFSSL_ECC_X25519:
  3845. oid = ECC_X25519_OID;
  3846. octets = 32;
  3847. break;
  3848. #endif /* HAVE_CURVE25519 */
  3849. #ifdef HAVE_ECC
  3850. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  3851. #ifdef HAVE_ECC_KOBLITZ
  3852. case WOLFSSL_ECC_SECP256K1:
  3853. oid = ECC_SECP256K1_OID;
  3854. octets = 32;
  3855. break;
  3856. #endif /* HAVE_ECC_KOBLITZ */
  3857. #ifdef HAVE_ECC_BRAINPOOL
  3858. case WOLFSSL_ECC_BRAINPOOLP256R1:
  3859. oid = ECC_BRAINPOOLP256R1_OID;
  3860. octets = 32;
  3861. break;
  3862. #endif /* HAVE_ECC_BRAINPOOL */
  3863. #endif
  3864. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  3865. #ifndef NO_ECC_SECP
  3866. case WOLFSSL_ECC_SECP384R1:
  3867. oid = ECC_SECP384R1_OID;
  3868. octets = 48;
  3869. break;
  3870. #endif /* !NO_ECC_SECP */
  3871. #ifdef HAVE_ECC_BRAINPOOL
  3872. case WOLFSSL_ECC_BRAINPOOLP384R1:
  3873. oid = ECC_BRAINPOOLP384R1_OID;
  3874. octets = 48;
  3875. break;
  3876. #endif /* HAVE_ECC_BRAINPOOL */
  3877. #endif
  3878. #endif
  3879. #if (defined(HAVE_CURVE448) || defined(HAVE_ED448)) && ECC_MIN_KEY_SZ <= 448
  3880. case WOLFSSL_ECC_X448:
  3881. oid = ECC_X448_OID;
  3882. octets = 57;
  3883. break;
  3884. #endif /* HAVE_CURVE448 */
  3885. #ifdef HAVE_ECC
  3886. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  3887. #ifdef HAVE_ECC_BRAINPOOL
  3888. case WOLFSSL_ECC_BRAINPOOLP512R1:
  3889. oid = ECC_BRAINPOOLP512R1_OID;
  3890. octets = 64;
  3891. break;
  3892. #endif /* HAVE_ECC_BRAINPOOL */
  3893. #endif
  3894. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  3895. #ifndef NO_ECC_SECP
  3896. case WOLFSSL_ECC_SECP521R1:
  3897. oid = ECC_SECP521R1_OID;
  3898. octets = 66;
  3899. break;
  3900. #endif /* !NO_ECC_SECP */
  3901. #endif
  3902. #endif
  3903. default: continue; /* unsupported curve */
  3904. }
  3905. #ifdef HAVE_ECC
  3906. /* Set default Oid */
  3907. if (defOid == 0 && ssl->eccTempKeySz <= octets && defSz > octets) {
  3908. defOid = oid;
  3909. defSz = octets;
  3910. }
  3911. /* The eccTempKeySz is the preferred ephemeral key size */
  3912. if (currOid == 0 && ssl->eccTempKeySz == octets)
  3913. currOid = oid;
  3914. if ((nextOid == 0 || nextSz > octets) && ssl->eccTempKeySz <= octets) {
  3915. nextOid = oid;
  3916. nextSz = octets;
  3917. }
  3918. #else
  3919. if (defOid == 0 && defSz > octets) {
  3920. defOid = oid;
  3921. defSz = octets;
  3922. }
  3923. if (currOid == 0)
  3924. currOid = oid;
  3925. if (nextOid == 0 || nextSz > octets) {
  3926. nextOid = oid;
  3927. nextSz = octets;
  3928. }
  3929. #endif
  3930. if (first == ECC_BYTE) {
  3931. switch (second) {
  3932. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
  3933. /* ECDHE_ECDSA */
  3934. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA:
  3935. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA:
  3936. case TLS_ECDHE_ECDSA_WITH_RC4_128_SHA:
  3937. case TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA:
  3938. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256:
  3939. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384:
  3940. case TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
  3941. case TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:
  3942. case TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8:
  3943. case TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8:
  3944. key |= ssl->ecdhCurveOID == oid;
  3945. ephmSuite = 1;
  3946. break;
  3947. #ifdef WOLFSSL_STATIC_DH
  3948. /* ECDH_ECDSA */
  3949. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA:
  3950. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA:
  3951. case TLS_ECDH_ECDSA_WITH_RC4_128_SHA:
  3952. case TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA:
  3953. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256:
  3954. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384:
  3955. case TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256:
  3956. case TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384:
  3957. if (oid == ECC_X25519_OID && defOid == oid) {
  3958. defOid = 0;
  3959. defSz = 80;
  3960. }
  3961. if (oid == ECC_X448_OID && defOid == oid) {
  3962. defOid = 0;
  3963. defSz = 80;
  3964. }
  3965. key |= ssl->pkCurveOID == oid;
  3966. break;
  3967. #endif /* WOLFSSL_STATIC_DH */
  3968. #endif /* HAVE_ECC || HAVE_ED25519 || HAVE_ED448 */
  3969. #ifndef NO_RSA
  3970. /* ECDHE_RSA */
  3971. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA:
  3972. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA:
  3973. case TLS_ECDHE_RSA_WITH_RC4_128_SHA:
  3974. case TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA:
  3975. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256:
  3976. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384:
  3977. case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:
  3978. case TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384:
  3979. key |= ssl->ecdhCurveOID == oid;
  3980. ephmSuite = 1;
  3981. break;
  3982. #if defined(HAVE_ECC) && defined(WOLFSSL_STATIC_DH)
  3983. /* ECDH_RSA */
  3984. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA:
  3985. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA:
  3986. case TLS_ECDH_RSA_WITH_RC4_128_SHA:
  3987. case TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA:
  3988. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256:
  3989. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384:
  3990. case TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256:
  3991. case TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384:
  3992. if (oid == ECC_X25519_OID && defOid == oid) {
  3993. defOid = 0;
  3994. defSz = 80;
  3995. }
  3996. if (oid == ECC_X448_OID && defOid == oid) {
  3997. defOid = 0;
  3998. defSz = 80;
  3999. }
  4000. key |= ssl->pkCurveOID == oid;
  4001. break;
  4002. #endif /* HAVE_ECC && WOLFSSL_STATIC_DH */
  4003. #endif
  4004. default:
  4005. if (oid == ECC_X25519_OID && defOid == oid) {
  4006. defOid = 0;
  4007. defSz = 80;
  4008. }
  4009. if (oid == ECC_X448_OID && defOid == oid) {
  4010. defOid = 0;
  4011. defSz = 80;
  4012. }
  4013. key = 1;
  4014. break;
  4015. }
  4016. }
  4017. /* ChaCha20-Poly1305 ECC cipher suites */
  4018. if (first == CHACHA_BYTE) {
  4019. switch (second) {
  4020. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
  4021. /* ECDHE_ECDSA */
  4022. case TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 :
  4023. case TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 :
  4024. key |= ssl->ecdhCurveOID == oid;
  4025. ephmSuite = 1;
  4026. break;
  4027. #endif /* HAVE_ECC || HAVE_ED25519 || HAVE_ED448 */
  4028. #ifndef NO_RSA
  4029. /* ECDHE_RSA */
  4030. case TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 :
  4031. case TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 :
  4032. key |= ssl->ecdhCurveOID == oid;
  4033. ephmSuite = 1;
  4034. break;
  4035. #endif
  4036. default:
  4037. key = 1;
  4038. break;
  4039. }
  4040. }
  4041. }
  4042. *ecdhCurveOID = ssl->ecdhCurveOID;
  4043. /* Choose the default if it is at the required strength. */
  4044. #ifdef HAVE_ECC
  4045. if (*ecdhCurveOID == 0 && defSz == ssl->eccTempKeySz)
  4046. #else
  4047. if (*ecdhCurveOID == 0)
  4048. #endif
  4049. {
  4050. key = 1;
  4051. *ecdhCurveOID = defOid;
  4052. }
  4053. /* Choose any curve at the required strength. */
  4054. if (*ecdhCurveOID == 0) {
  4055. key = 1;
  4056. *ecdhCurveOID = currOid;
  4057. }
  4058. /* Choose the default if it is at the next highest strength. */
  4059. if (*ecdhCurveOID == 0 && defSz == nextSz)
  4060. *ecdhCurveOID = defOid;
  4061. /* Choose any curve at the next highest strength. */
  4062. if (*ecdhCurveOID == 0)
  4063. *ecdhCurveOID = nextOid;
  4064. /* No curve and ephemeral ECC suite requires a matching curve. */
  4065. if (*ecdhCurveOID == 0 && ephmSuite)
  4066. key = 0;
  4067. return key;
  4068. }
  4069. #endif
  4070. #endif /* NO_WOLFSSL_SERVER */
  4071. int TLSX_SupportedCurve_Copy(TLSX* src, TLSX** dst, void* heap)
  4072. {
  4073. TLSX* extension;
  4074. int ret;
  4075. extension = TLSX_Find(src, TLSX_SUPPORTED_GROUPS);
  4076. if (extension != NULL) {
  4077. SupportedCurve* curve;
  4078. for (curve = (SupportedCurve*)extension->data; curve != NULL;
  4079. curve = curve->next) {
  4080. ret = TLSX_UseSupportedCurve(dst, curve->name, heap);
  4081. if (ret != WOLFSSL_SUCCESS)
  4082. return MEMORY_E;
  4083. }
  4084. }
  4085. return 0;
  4086. }
  4087. int TLSX_UseSupportedCurve(TLSX** extensions, word16 name, void* heap)
  4088. {
  4089. TLSX* extension = NULL;
  4090. SupportedCurve* curve = NULL;
  4091. int ret;
  4092. if (extensions == NULL) {
  4093. return BAD_FUNC_ARG;
  4094. }
  4095. #ifdef WOLFSSL_TLS13
  4096. if (! TLSX_KeyShare_IsSupported(name)) {
  4097. return BAD_FUNC_ARG;
  4098. }
  4099. #endif
  4100. extension = TLSX_Find(*extensions, TLSX_SUPPORTED_GROUPS);
  4101. if (!extension) {
  4102. ret = TLSX_SupportedCurve_New(&curve, name, heap);
  4103. if (ret != 0)
  4104. return ret;
  4105. ret = TLSX_Push(extensions, TLSX_SUPPORTED_GROUPS, curve, heap);
  4106. if (ret != 0) {
  4107. XFREE(curve, heap, DYNAMIC_TYPE_TLSX);
  4108. return ret;
  4109. }
  4110. }
  4111. else {
  4112. ret = TLSX_SupportedCurve_Append((SupportedCurve*)extension->data, name,
  4113. heap);
  4114. if (ret != 0)
  4115. return ret;
  4116. }
  4117. return WOLFSSL_SUCCESS;
  4118. }
  4119. int TLSX_UsePointFormat(TLSX** extensions, byte format, void* heap)
  4120. {
  4121. TLSX* extension = NULL;
  4122. PointFormat* point = NULL;
  4123. int ret = 0;
  4124. if (extensions == NULL)
  4125. return BAD_FUNC_ARG;
  4126. extension = TLSX_Find(*extensions, TLSX_EC_POINT_FORMATS);
  4127. if (!extension) {
  4128. ret = TLSX_PointFormat_New(&point, format, heap);
  4129. if (ret != 0)
  4130. return ret;
  4131. ret = TLSX_Push(extensions, TLSX_EC_POINT_FORMATS, point, heap);
  4132. if (ret != 0) {
  4133. XFREE(point, heap, DYNAMIC_TYPE_TLSX);
  4134. return ret;
  4135. }
  4136. }
  4137. else {
  4138. ret = TLSX_PointFormat_Append((PointFormat*)extension->data, format,
  4139. heap);
  4140. if (ret != 0)
  4141. return ret;
  4142. }
  4143. return WOLFSSL_SUCCESS;
  4144. }
  4145. #define EC_FREE_ALL TLSX_SupportedCurve_FreeAll
  4146. #define EC_VALIDATE_REQUEST TLSX_SupportedCurve_ValidateRequest
  4147. #ifndef NO_WOLFSSL_CLIENT
  4148. #define EC_GET_SIZE TLSX_SupportedCurve_GetSize
  4149. #define EC_WRITE TLSX_SupportedCurve_Write
  4150. #else
  4151. #define EC_GET_SIZE(list) 0
  4152. #define EC_WRITE(a, b) 0
  4153. #endif
  4154. #if !defined(NO_WOLFSSL_SERVER) || (defined(WOLFSSL_TLS13) && \
  4155. !defined(WOLFSSL_NO_SERVER_GROUPS_EXT))
  4156. #define EC_PARSE TLSX_SupportedCurve_Parse
  4157. #else
  4158. #define EC_PARSE(a, b, c, d, e) 0
  4159. #endif
  4160. #define PF_FREE_ALL TLSX_PointFormat_FreeAll
  4161. #define PF_VALIDATE_REQUEST TLSX_PointFormat_ValidateRequest
  4162. #define PF_VALIDATE_RESPONSE TLSX_PointFormat_ValidateResponse
  4163. #define PF_GET_SIZE TLSX_PointFormat_GetSize
  4164. #define PF_WRITE TLSX_PointFormat_Write
  4165. #ifndef NO_WOLFSSL_SERVER
  4166. #define PF_PARSE TLSX_PointFormat_Parse
  4167. #else
  4168. #define PF_PARSE(a, b, c, d) 0
  4169. #endif
  4170. #else
  4171. #define EC_FREE_ALL(list, heap)
  4172. #define EC_GET_SIZE(list) 0
  4173. #define EC_WRITE(a, b) 0
  4174. #define EC_PARSE(a, b, c, d, e) 0
  4175. #define EC_VALIDATE_REQUEST(a, b)
  4176. #define PF_FREE_ALL(list, heap)
  4177. #define PF_GET_SIZE(list) 0
  4178. #define PF_WRITE(a, b) 0
  4179. #define PF_PARSE(a, b, c, d) 0
  4180. #define PF_VALIDATE_REQUEST(a, b)
  4181. #define PF_VALIDATE_RESPONSE(a, b)
  4182. #endif /* HAVE_SUPPORTED_CURVES */
  4183. /******************************************************************************/
  4184. /* Renegotiation Indication */
  4185. /******************************************************************************/
  4186. #if defined(HAVE_SECURE_RENEGOTIATION) \
  4187. || defined(HAVE_SERVER_RENEGOTIATION_INFO)
  4188. static byte TLSX_SecureRenegotiation_GetSize(SecureRenegotiation* data,
  4189. int isRequest)
  4190. {
  4191. byte length = OPAQUE8_LEN; /* empty info length */
  4192. /* data will be NULL for HAVE_SERVER_RENEGOTIATION_INFO only */
  4193. if (data && data->enabled && data->verifySet) {
  4194. /* client sends client_verify_data only */
  4195. length += TLS_FINISHED_SZ;
  4196. /* server also sends server_verify_data */
  4197. if (!isRequest)
  4198. length += TLS_FINISHED_SZ;
  4199. }
  4200. return length;
  4201. }
  4202. static word16 TLSX_SecureRenegotiation_Write(SecureRenegotiation* data,
  4203. byte* output, int isRequest)
  4204. {
  4205. word16 offset = OPAQUE8_LEN; /* RenegotiationInfo length */
  4206. if (data && data->enabled && data->verifySet) {
  4207. /* client sends client_verify_data only */
  4208. XMEMCPY(output + offset, data->client_verify_data, TLS_FINISHED_SZ);
  4209. offset += TLS_FINISHED_SZ;
  4210. /* server also sends server_verify_data */
  4211. if (!isRequest) {
  4212. XMEMCPY(output + offset, data->server_verify_data, TLS_FINISHED_SZ);
  4213. offset += TLS_FINISHED_SZ;
  4214. }
  4215. }
  4216. output[0] = (byte)(offset - 1); /* info length - self */
  4217. return offset;
  4218. }
  4219. static int TLSX_SecureRenegotiation_Parse(WOLFSSL* ssl, const byte* input,
  4220. word16 length, byte isRequest)
  4221. {
  4222. int ret = SECURE_RENEGOTIATION_E;
  4223. if (length >= OPAQUE8_LEN) {
  4224. if (isRequest) {
  4225. #ifndef NO_WOLFSSL_SERVER
  4226. if (ssl->secure_renegotiation == NULL) {
  4227. ret = wolfSSL_UseSecureRenegotiation(ssl);
  4228. if (ret == WOLFSSL_SUCCESS)
  4229. ret = 0;
  4230. }
  4231. if (ret != 0 && ret != SECURE_RENEGOTIATION_E) {
  4232. }
  4233. else if (ssl->secure_renegotiation == NULL) {
  4234. }
  4235. else if (!ssl->secure_renegotiation->enabled) {
  4236. if (*input == 0) {
  4237. input++; /* get past size */
  4238. ssl->secure_renegotiation->enabled = 1;
  4239. TLSX_SetResponse(ssl, TLSX_RENEGOTIATION_INFO);
  4240. ret = 0;
  4241. }
  4242. else {
  4243. /* already in error state */
  4244. WOLFSSL_MSG("SCR client verify data present");
  4245. }
  4246. }
  4247. else if (*input == TLS_FINISHED_SZ) {
  4248. if (length < TLS_FINISHED_SZ + 1) {
  4249. WOLFSSL_MSG("SCR malformed buffer");
  4250. ret = BUFFER_E;
  4251. }
  4252. else {
  4253. input++; /* get past size */
  4254. /* validate client verify data */
  4255. if (XMEMCMP(input,
  4256. ssl->secure_renegotiation->client_verify_data,
  4257. TLS_FINISHED_SZ) == 0) {
  4258. WOLFSSL_MSG("SCR client verify data match");
  4259. TLSX_SetResponse(ssl, TLSX_RENEGOTIATION_INFO);
  4260. ret = 0; /* verified */
  4261. }
  4262. else {
  4263. /* already in error state */
  4264. WOLFSSL_MSG("SCR client verify data Failure");
  4265. }
  4266. }
  4267. }
  4268. #endif
  4269. }
  4270. else if (ssl->secure_renegotiation != NULL) {
  4271. #ifndef NO_WOLFSSL_CLIENT
  4272. if (!ssl->secure_renegotiation->enabled) {
  4273. if (*input == 0) {
  4274. ssl->secure_renegotiation->enabled = 1;
  4275. ret = 0;
  4276. }
  4277. }
  4278. else if (*input == 2 * TLS_FINISHED_SZ &&
  4279. length == 2 * TLS_FINISHED_SZ + OPAQUE8_LEN) {
  4280. input++; /* get past size */
  4281. /* validate client and server verify data */
  4282. if (XMEMCMP(input,
  4283. ssl->secure_renegotiation->client_verify_data,
  4284. TLS_FINISHED_SZ) == 0 &&
  4285. XMEMCMP(input + TLS_FINISHED_SZ,
  4286. ssl->secure_renegotiation->server_verify_data,
  4287. TLS_FINISHED_SZ) == 0) {
  4288. WOLFSSL_MSG("SCR client and server verify data match");
  4289. ret = 0; /* verified */
  4290. }
  4291. else {
  4292. /* already in error state */
  4293. WOLFSSL_MSG("SCR client and server verify data Failure");
  4294. }
  4295. }
  4296. #endif
  4297. }
  4298. }
  4299. if (ret != 0) {
  4300. WOLFSSL_ERROR_VERBOSE(ret);
  4301. SendAlert(ssl, alert_fatal, handshake_failure);
  4302. }
  4303. return ret;
  4304. }
  4305. int TLSX_UseSecureRenegotiation(TLSX** extensions, void* heap)
  4306. {
  4307. int ret = 0;
  4308. SecureRenegotiation* data;
  4309. data = (SecureRenegotiation*)XMALLOC(sizeof(SecureRenegotiation), heap,
  4310. DYNAMIC_TYPE_TLSX);
  4311. if (data == NULL)
  4312. return MEMORY_E;
  4313. XMEMSET(data, 0, sizeof(SecureRenegotiation));
  4314. ret = TLSX_Push(extensions, TLSX_RENEGOTIATION_INFO, data, heap);
  4315. if (ret != 0) {
  4316. XFREE(data, heap, DYNAMIC_TYPE_TLSX);
  4317. return ret;
  4318. }
  4319. return WOLFSSL_SUCCESS;
  4320. }
  4321. #ifdef HAVE_SERVER_RENEGOTIATION_INFO
  4322. int TLSX_AddEmptyRenegotiationInfo(TLSX** extensions, void* heap)
  4323. {
  4324. int ret;
  4325. /* send empty renegotiation_info extension */
  4326. TLSX* ext = TLSX_Find(*extensions, TLSX_RENEGOTIATION_INFO);
  4327. if (ext == NULL) {
  4328. ret = TLSX_UseSecureRenegotiation(extensions, heap);
  4329. if (ret != WOLFSSL_SUCCESS)
  4330. return ret;
  4331. ext = TLSX_Find(*extensions, TLSX_RENEGOTIATION_INFO);
  4332. }
  4333. if (ext)
  4334. ext->resp = 1;
  4335. return WOLFSSL_SUCCESS;
  4336. }
  4337. #endif /* HAVE_SERVER_RENEGOTIATION_INFO */
  4338. #define SCR_FREE_ALL(data, heap) XFREE(data, (heap), DYNAMIC_TYPE_TLSX)
  4339. #define SCR_GET_SIZE TLSX_SecureRenegotiation_GetSize
  4340. #define SCR_WRITE TLSX_SecureRenegotiation_Write
  4341. #define SCR_PARSE TLSX_SecureRenegotiation_Parse
  4342. #else
  4343. #define SCR_FREE_ALL(a, heap)
  4344. #define SCR_GET_SIZE(a, b) 0
  4345. #define SCR_WRITE(a, b, c) 0
  4346. #define SCR_PARSE(a, b, c, d) 0
  4347. #endif /* HAVE_SECURE_RENEGOTIATION || HAVE_SERVER_RENEGOTIATION_INFO */
  4348. /******************************************************************************/
  4349. /* Session Tickets */
  4350. /******************************************************************************/
  4351. #ifdef HAVE_SESSION_TICKET
  4352. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
  4353. static void TLSX_SessionTicket_ValidateRequest(WOLFSSL* ssl)
  4354. {
  4355. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_SESSION_TICKET);
  4356. SessionTicket* ticket = extension ?
  4357. (SessionTicket*)extension->data : NULL;
  4358. if (ticket) {
  4359. /* TODO validate ticket timeout here! */
  4360. if (ticket->lifetime == 0xfffffff) {
  4361. /* send empty ticket on timeout */
  4362. TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
  4363. }
  4364. }
  4365. }
  4366. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
  4367. static word16 TLSX_SessionTicket_GetSize(SessionTicket* ticket, int isRequest)
  4368. {
  4369. (void)isRequest;
  4370. return ticket ? ticket->size : 0;
  4371. }
  4372. static word16 TLSX_SessionTicket_Write(SessionTicket* ticket, byte* output,
  4373. int isRequest)
  4374. {
  4375. word16 offset = 0; /* empty ticket */
  4376. if (isRequest && ticket) {
  4377. XMEMCPY(output + offset, ticket->data, ticket->size);
  4378. offset += ticket->size;
  4379. }
  4380. return offset;
  4381. }
  4382. static int TLSX_SessionTicket_Parse(WOLFSSL* ssl, const byte* input,
  4383. word16 length, byte isRequest)
  4384. {
  4385. int ret = 0;
  4386. (void) input; /* avoid unused parameter if NO_WOLFSSL_SERVER defined */
  4387. if (!isRequest) {
  4388. if (TLSX_CheckUnsupportedExtension(ssl, TLSX_SESSION_TICKET))
  4389. return TLSX_HandleUnsupportedExtension(ssl);
  4390. if (length != 0)
  4391. return BUFFER_ERROR;
  4392. #ifndef NO_WOLFSSL_CLIENT
  4393. ssl->expect_session_ticket = 1;
  4394. #endif
  4395. }
  4396. #ifndef NO_WOLFSSL_SERVER
  4397. else {
  4398. /* server side */
  4399. if (ssl->ctx->ticketEncCb == NULL) {
  4400. WOLFSSL_MSG("Client sent session ticket, server has no callback");
  4401. return 0;
  4402. }
  4403. if (length > SESSION_TICKET_LEN) {
  4404. ret = BAD_TICKET_MSG_SZ;
  4405. WOLFSSL_ERROR_VERBOSE(ret);
  4406. } else if (IsAtLeastTLSv1_3(ssl->version)) {
  4407. WOLFSSL_MSG("Process client ticket rejected, TLS 1.3 no support");
  4408. ssl->options.rejectTicket = 1;
  4409. ret = 0; /* not fatal */
  4410. } else if (ssl->options.noTicketTls12) {
  4411. /* ignore ticket request */
  4412. } else if (length == 0) {
  4413. /* blank ticket */
  4414. ret = TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
  4415. if (ret == WOLFSSL_SUCCESS) {
  4416. ret = 0;
  4417. /* send blank ticket */
  4418. TLSX_SetResponse(ssl, TLSX_SESSION_TICKET);
  4419. ssl->options.createTicket = 1; /* will send ticket msg */
  4420. ssl->options.useTicket = 1;
  4421. ssl->options.resuming = 0; /* no standard resumption */
  4422. ssl->arrays->sessionIDSz = 0; /* no echo on blank ticket */
  4423. }
  4424. } else {
  4425. /* got actual ticket from client */
  4426. ret = DoClientTicket(ssl, input, length);
  4427. if (ret == WOLFSSL_TICKET_RET_OK) { /* use ticket to resume */
  4428. WOLFSSL_MSG("Using existing client ticket");
  4429. ssl->options.useTicket = 1;
  4430. ssl->options.resuming = 1;
  4431. /* SERVER: ticket is peer auth. */
  4432. ssl->options.peerAuthGood = 1;
  4433. } else if (ret == WOLFSSL_TICKET_RET_CREATE) {
  4434. WOLFSSL_MSG("Using existing client ticket, creating new one");
  4435. ret = TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
  4436. if (ret == WOLFSSL_SUCCESS) {
  4437. ret = 0;
  4438. TLSX_SetResponse(ssl, TLSX_SESSION_TICKET);
  4439. /* send blank ticket */
  4440. ssl->options.createTicket = 1; /* will send ticket msg */
  4441. ssl->options.useTicket = 1;
  4442. ssl->options.resuming = 1;
  4443. /* SERVER: ticket is peer auth. */
  4444. ssl->options.peerAuthGood = 1;
  4445. }
  4446. } else if (ret == WOLFSSL_TICKET_RET_REJECT) {
  4447. WOLFSSL_MSG("Process client ticket rejected, not using");
  4448. ssl->options.rejectTicket = 1;
  4449. ret = 0; /* not fatal */
  4450. } else if (ret == VERSION_ERROR) {
  4451. WOLFSSL_MSG("Process client ticket rejected, bad TLS version");
  4452. ssl->options.rejectTicket = 1;
  4453. ret = 0; /* not fatal */
  4454. } else if (ret == WOLFSSL_TICKET_RET_FATAL) {
  4455. WOLFSSL_MSG("Process client ticket fatal error, not using");
  4456. } else if (ret < 0) {
  4457. WOLFSSL_MSG("Process client ticket unknown error, not using");
  4458. }
  4459. }
  4460. }
  4461. #endif /* NO_WOLFSSL_SERVER */
  4462. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  4463. (void)ssl;
  4464. #endif
  4465. return ret;
  4466. }
  4467. WOLFSSL_LOCAL SessionTicket* TLSX_SessionTicket_Create(word32 lifetime,
  4468. byte* data, word16 size, void* heap)
  4469. {
  4470. SessionTicket* ticket = (SessionTicket*)XMALLOC(sizeof(SessionTicket),
  4471. heap, DYNAMIC_TYPE_TLSX);
  4472. if (ticket) {
  4473. ticket->data = (byte*)XMALLOC(size, heap, DYNAMIC_TYPE_TLSX);
  4474. if (ticket->data == NULL) {
  4475. XFREE(ticket, heap, DYNAMIC_TYPE_TLSX);
  4476. return NULL;
  4477. }
  4478. XMEMCPY(ticket->data, data, size);
  4479. ticket->size = size;
  4480. ticket->lifetime = lifetime;
  4481. }
  4482. (void)heap;
  4483. return ticket;
  4484. }
  4485. WOLFSSL_LOCAL void TLSX_SessionTicket_Free(SessionTicket* ticket, void* heap)
  4486. {
  4487. if (ticket) {
  4488. XFREE(ticket->data, heap, DYNAMIC_TYPE_TLSX);
  4489. XFREE(ticket, heap, DYNAMIC_TYPE_TLSX);
  4490. }
  4491. (void)heap;
  4492. }
  4493. int TLSX_UseSessionTicket(TLSX** extensions, SessionTicket* ticket, void* heap)
  4494. {
  4495. int ret = 0;
  4496. if (extensions == NULL)
  4497. return BAD_FUNC_ARG;
  4498. /* If the ticket is NULL, the client will request a new ticket from the
  4499. server. Otherwise, the client will use it in the next client hello. */
  4500. if ((ret = TLSX_Push(extensions, TLSX_SESSION_TICKET, (void*)ticket, heap))
  4501. != 0)
  4502. return ret;
  4503. return WOLFSSL_SUCCESS;
  4504. }
  4505. #define WOLF_STK_VALIDATE_REQUEST TLSX_SessionTicket_ValidateRequest
  4506. #define WOLF_STK_GET_SIZE TLSX_SessionTicket_GetSize
  4507. #define WOLF_STK_WRITE TLSX_SessionTicket_Write
  4508. #define WOLF_STK_PARSE TLSX_SessionTicket_Parse
  4509. #define WOLF_STK_FREE(stk, heap) TLSX_SessionTicket_Free((SessionTicket*)(stk),(heap))
  4510. #else
  4511. #define WOLF_STK_FREE(a, b)
  4512. #define WOLF_STK_VALIDATE_REQUEST(a)
  4513. #define WOLF_STK_GET_SIZE(a, b) 0
  4514. #define WOLF_STK_WRITE(a, b, c) 0
  4515. #define WOLF_STK_PARSE(a, b, c, d) 0
  4516. #endif /* HAVE_SESSION_TICKET */
  4517. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  4518. /******************************************************************************/
  4519. /* Encrypt-then-MAC */
  4520. /******************************************************************************/
  4521. #ifndef WOLFSSL_NO_TLS12
  4522. static int TLSX_EncryptThenMac_Use(WOLFSSL* ssl);
  4523. /**
  4524. * Get the size of the Encrypt-Then-MAC extension.
  4525. *
  4526. * msgType Type of message to put extension into.
  4527. * pSz Size of extension data.
  4528. * return SANITY_MSG_E when the message is not allowed to have extension and
  4529. * 0 otherwise.
  4530. */
  4531. static int TLSX_EncryptThenMac_GetSize(byte msgType, word16* pSz)
  4532. {
  4533. (void)pSz;
  4534. if (msgType != client_hello && msgType != server_hello) {
  4535. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4536. return SANITY_MSG_E;
  4537. }
  4538. /* Empty extension */
  4539. return 0;
  4540. }
  4541. /**
  4542. * Write the Encrypt-Then-MAC extension.
  4543. *
  4544. * data Unused
  4545. * output Extension data buffer. Unused.
  4546. * msgType Type of message to put extension into.
  4547. * pSz Size of extension data.
  4548. * return SANITY_MSG_E when the message is not allowed to have extension and
  4549. * 0 otherwise.
  4550. */
  4551. static int TLSX_EncryptThenMac_Write(void* data, byte* output, byte msgType,
  4552. word16* pSz)
  4553. {
  4554. (void)data;
  4555. (void)output;
  4556. (void)pSz;
  4557. if (msgType != client_hello && msgType != server_hello) {
  4558. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4559. return SANITY_MSG_E;
  4560. }
  4561. /* Empty extension */
  4562. return 0;
  4563. }
  4564. /**
  4565. * Parse the Encrypt-Then-MAC extension.
  4566. *
  4567. * ssl SSL object
  4568. * input Extension data buffer.
  4569. * length Length of this extension's data.
  4570. * msgType Type of message to extension appeared in.
  4571. * return SANITY_MSG_E when the message is not allowed to have extension,
  4572. * BUFFER_ERROR when the extension's data is invalid,
  4573. * MEMORY_E when unable to allocate memory and
  4574. * 0 otherwise.
  4575. */
  4576. static int TLSX_EncryptThenMac_Parse(WOLFSSL* ssl, const byte* input,
  4577. word16 length, byte msgType)
  4578. {
  4579. int ret;
  4580. (void)input;
  4581. if (msgType != client_hello && msgType != server_hello) {
  4582. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4583. return SANITY_MSG_E;
  4584. }
  4585. /* Empty extension */
  4586. if (length != 0)
  4587. return BUFFER_ERROR;
  4588. if (msgType == client_hello) {
  4589. /* Check the user hasn't disallowed use of Encrypt-Then-Mac. */
  4590. if (!ssl->options.disallowEncThenMac) {
  4591. ssl->options.encThenMac = 1;
  4592. /* Set the extension reply. */
  4593. ret = TLSX_EncryptThenMac_Use(ssl);
  4594. if (ret != 0)
  4595. return ret;
  4596. }
  4597. return 0;
  4598. }
  4599. /* Server Hello */
  4600. if (ssl->options.disallowEncThenMac) {
  4601. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4602. return SANITY_MSG_E;
  4603. }
  4604. ssl->options.encThenMac = 1;
  4605. return 0;
  4606. }
  4607. /**
  4608. * Add the Encrypt-Then-MAC extension to list.
  4609. *
  4610. * ssl SSL object
  4611. * return MEMORY_E when unable to allocate memory and 0 otherwise.
  4612. */
  4613. static int TLSX_EncryptThenMac_Use(WOLFSSL* ssl)
  4614. {
  4615. int ret = 0;
  4616. TLSX* extension;
  4617. /* Find the Encrypt-Then-Mac extension if it exists. */
  4618. extension = TLSX_Find(ssl->extensions, TLSX_ENCRYPT_THEN_MAC);
  4619. if (extension == NULL) {
  4620. /* Push new Encrypt-Then-Mac extension. */
  4621. ret = TLSX_Push(&ssl->extensions, TLSX_ENCRYPT_THEN_MAC, NULL,
  4622. ssl->heap);
  4623. if (ret != 0)
  4624. return ret;
  4625. }
  4626. return 0;
  4627. }
  4628. /**
  4629. * Set the Encrypt-Then-MAC extension as one to respond too.
  4630. *
  4631. * ssl SSL object
  4632. * return EXT_MISSING when EncryptThenMac extension not in list.
  4633. */
  4634. int TLSX_EncryptThenMac_Respond(WOLFSSL* ssl)
  4635. {
  4636. TLSX* extension;
  4637. extension = TLSX_Find(ssl->extensions, TLSX_ENCRYPT_THEN_MAC);
  4638. if (extension == NULL)
  4639. return EXT_MISSING;
  4640. extension->resp = 1;
  4641. return 0;
  4642. }
  4643. #define ETM_GET_SIZE TLSX_EncryptThenMac_GetSize
  4644. #define ETM_WRITE TLSX_EncryptThenMac_Write
  4645. #define ETM_PARSE TLSX_EncryptThenMac_Parse
  4646. #else
  4647. #define ETM_GET_SIZE(a, b) 0
  4648. #define ETM_WRITE(a, b, c, d) 0
  4649. #define ETM_PARSE(a, b, c, d) 0
  4650. #endif /* !WOLFSSL_NO_TLS12 */
  4651. #endif /* HAVE_ENCRYPT_THEN_MAC && !WOLFSSL_AEAD_ONLY */
  4652. #ifdef WOLFSSL_SRTP
  4653. /******************************************************************************/
  4654. /* DTLS SRTP (Secure Real-time Transport Protocol) */
  4655. /******************************************************************************/
  4656. /* Only support single SRTP profile */
  4657. typedef struct TlsxSrtp {
  4658. word16 profileCount;
  4659. word16 ids; /* selected bits */
  4660. } TlsxSrtp;
  4661. static int TLSX_UseSRTP_GetSize(TlsxSrtp *srtp)
  4662. {
  4663. /* SRTP Profile Len (2)
  4664. * SRTP Profiles (2)
  4665. * MKI (master key id) Length */
  4666. return (OPAQUE16_LEN + (srtp->profileCount * OPAQUE16_LEN) + 1);
  4667. }
  4668. static TlsxSrtp* TLSX_UseSRTP_New(word16 ids, void* heap)
  4669. {
  4670. TlsxSrtp* srtp;
  4671. int i;
  4672. srtp = (TlsxSrtp*)XMALLOC(sizeof(TlsxSrtp), heap, DYNAMIC_TYPE_TLSX);
  4673. if (srtp == NULL) {
  4674. WOLFSSL_MSG("TLSX SRTP Memory failure");
  4675. return NULL;
  4676. }
  4677. /* count and test each bit set */
  4678. srtp->profileCount = 0;
  4679. for (i=0; i<16; i++) {
  4680. if (ids & (1 << i)) {
  4681. srtp->profileCount++;
  4682. }
  4683. }
  4684. srtp->ids = ids;
  4685. return srtp;
  4686. }
  4687. static void TLSX_UseSRTP_Free(TlsxSrtp *srtp, void* heap)
  4688. {
  4689. if (srtp != NULL) {
  4690. XFREE(srtp, heap, DYNAMIC_TYPE_TLSX);
  4691. }
  4692. (void)heap;
  4693. }
  4694. static int TLSX_UseSRTP_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  4695. byte isRequest)
  4696. {
  4697. int ret = BAD_FUNC_ARG;
  4698. word16 profile_len = 0;
  4699. word16 profile_value = 0;
  4700. word16 offset = 0;
  4701. #ifndef NO_WOLFSSL_SERVER
  4702. int i;
  4703. TlsxSrtp* srtp = NULL;
  4704. #endif
  4705. if (length < OPAQUE16_LEN) {
  4706. return BUFFER_ERROR;
  4707. }
  4708. /* reset selected DTLS SRTP profile ID */
  4709. ssl->dtlsSrtpId = 0;
  4710. /* total length, not include itself */
  4711. ato16(input, &profile_len);
  4712. offset += OPAQUE16_LEN;
  4713. if (!isRequest) {
  4714. #ifndef NO_WOLFSSL_CLIENT
  4715. if (length < offset + OPAQUE16_LEN)
  4716. return BUFFER_ERROR;
  4717. ato16(input + offset, &profile_value);
  4718. /* check that the profile received was in the ones we support */
  4719. if (profile_value < 16 &&
  4720. (ssl->dtlsSrtpProfiles & (1 << profile_value))) {
  4721. ssl->dtlsSrtpId = profile_value;
  4722. ret = 0; /* success */
  4723. }
  4724. #endif
  4725. }
  4726. #ifndef NO_WOLFSSL_SERVER
  4727. else {
  4728. /* parse remainder one profile at a time, looking for match in CTX */
  4729. ret = 0;
  4730. for (i=offset; i<length; i+=OPAQUE16_LEN) {
  4731. ato16(input+i, &profile_value);
  4732. /* find first match */
  4733. if (profile_value < 16 &&
  4734. ssl->dtlsSrtpProfiles & (1 << profile_value)) {
  4735. ssl->dtlsSrtpId = profile_value;
  4736. /* make sure we respond with selected SRTP id selected */
  4737. srtp = TLSX_UseSRTP_New((1 << profile_value), ssl->heap);
  4738. if (srtp != NULL) {
  4739. ret = TLSX_Push(&ssl->extensions, TLSX_USE_SRTP,
  4740. (void*)srtp, ssl->heap);
  4741. if (ret == 0) {
  4742. TLSX_SetResponse(ssl, TLSX_USE_SRTP);
  4743. /* successfully set extension */
  4744. }
  4745. }
  4746. else {
  4747. ret = MEMORY_E;
  4748. }
  4749. break;
  4750. }
  4751. }
  4752. }
  4753. if (ret == 0 && ssl->dtlsSrtpId == 0) {
  4754. WOLFSSL_MSG("TLSX_UseSRTP_Parse profile not found!");
  4755. /* not fatal */
  4756. }
  4757. else if (ret != 0) {
  4758. ssl->dtlsSrtpId = 0;
  4759. TLSX_UseSRTP_Free(srtp, ssl->heap);
  4760. }
  4761. #endif
  4762. (void)profile_len;
  4763. return ret;
  4764. }
  4765. static word16 TLSX_UseSRTP_Write(TlsxSrtp* srtp, byte* output)
  4766. {
  4767. word16 offset = 0;
  4768. int i, j;
  4769. c16toa(srtp->profileCount * 2, output + offset);
  4770. offset += OPAQUE16_LEN;
  4771. j = 0;
  4772. for (i = 0; i < srtp->profileCount; i++) {
  4773. for (; j < 16; j++) {
  4774. if (srtp->ids & (1 << j)) {
  4775. c16toa(j, output + offset);
  4776. offset += OPAQUE16_LEN;
  4777. }
  4778. }
  4779. }
  4780. output[offset++] = 0x00; /* MKI Length */
  4781. return offset;
  4782. }
  4783. static int TLSX_UseSRTP(TLSX** extensions, word16 profiles, void* heap)
  4784. {
  4785. int ret = 0;
  4786. TLSX* extension;
  4787. if (extensions == NULL) {
  4788. return BAD_FUNC_ARG;
  4789. }
  4790. extension = TLSX_Find(*extensions, TLSX_USE_SRTP);
  4791. if (extension == NULL) {
  4792. TlsxSrtp* srtp = TLSX_UseSRTP_New(profiles, heap);
  4793. if (srtp == NULL) {
  4794. return MEMORY_E;
  4795. }
  4796. ret = TLSX_Push(extensions, TLSX_USE_SRTP, (void*)srtp, heap);
  4797. if (ret != 0) {
  4798. TLSX_UseSRTP_Free(srtp, heap);
  4799. }
  4800. }
  4801. return ret;
  4802. }
  4803. #ifndef NO_WOLFSSL_SERVER
  4804. #define SRTP_FREE TLSX_UseSRTP_Free
  4805. #define SRTP_PARSE TLSX_UseSRTP_Parse
  4806. #define SRTP_WRITE TLSX_UseSRTP_Write
  4807. #define SRTP_GET_SIZE TLSX_UseSRTP_GetSize
  4808. #else
  4809. #define SRTP_FREE(a, b)
  4810. #define SRTP_PARSE(a, b, c, d) 0
  4811. #define SRTP_WRITE(a, b) 0
  4812. #define SRTP_GET_SIZE(a) 0
  4813. #endif
  4814. #endif /* WOLFSSL_SRTP */
  4815. /******************************************************************************/
  4816. /* Supported Versions */
  4817. /******************************************************************************/
  4818. #ifdef WOLFSSL_TLS13
  4819. static WC_INLINE int versionIsGreater(byte isDtls, byte a, byte b)
  4820. {
  4821. (void)isDtls;
  4822. #ifdef WOLFSSL_DTLS
  4823. /* DTLS version increases backwards (-1,-2,-3,etc) */
  4824. if (isDtls)
  4825. return a < b;
  4826. #endif /* WOLFSSL_DTLS */
  4827. return a > b;
  4828. }
  4829. static WC_INLINE int versionIsLesser(byte isDtls, byte a, byte b)
  4830. {
  4831. (void)isDtls;
  4832. #ifdef WOLFSSL_DTLS
  4833. /* DTLS version increases backwards (-1,-2,-3,etc) */
  4834. if (isDtls)
  4835. return a > b;
  4836. #endif /* WOLFSSL_DTLS */
  4837. return a < b;
  4838. }
  4839. static WC_INLINE int versionIsAtLeast(byte isDtls, byte a, byte b)
  4840. {
  4841. (void)isDtls;
  4842. #ifdef WOLFSSL_DTLS
  4843. /* DTLS version increases backwards (-1,-2,-3,etc) */
  4844. if (isDtls)
  4845. return a <= b;
  4846. #endif /* WOLFSSL_DTLS */
  4847. return a >= b;
  4848. }
  4849. static WC_INLINE int versionIsLessEqual(byte isDtls, byte a, byte b)
  4850. {
  4851. (void)isDtls;
  4852. #ifdef WOLFSSL_DTLS
  4853. /* DTLS version increases backwards (-1,-2,-3,etc) */
  4854. if (isDtls)
  4855. return a >= b;
  4856. #endif /* WOLFSSL_DTLS */
  4857. return a <= b;
  4858. }
  4859. /* Return the size of the SupportedVersions extension's data.
  4860. *
  4861. * data The SSL/TLS object.
  4862. * msgType The type of the message this extension is being written into.
  4863. * returns the length of data that will be in the extension.
  4864. */
  4865. static int TLSX_SupportedVersions_GetSize(void* data, byte msgType, word16* pSz)
  4866. {
  4867. WOLFSSL* ssl = (WOLFSSL*)data;
  4868. byte tls13Minor, tls12Minor, tls11Minor, isDtls;
  4869. isDtls = !!ssl->options.dtls;
  4870. tls13Minor = (byte)(isDtls ? DTLSv1_3_MINOR : TLSv1_3_MINOR);
  4871. tls12Minor = (byte)(isDtls ? DTLSv1_2_MINOR : TLSv1_2_MINOR);
  4872. tls11Minor = (byte)(isDtls ? DTLS_MINOR : TLSv1_1_MINOR);
  4873. /* unused on some configuration */
  4874. (void)tls12Minor;
  4875. (void)tls13Minor;
  4876. (void)tls11Minor;
  4877. if (msgType == client_hello) {
  4878. /* TLS v1.2 and TLS v1.3 */
  4879. int cnt = 0;
  4880. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls13Minor)
  4881. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4882. defined(WOLFSSL_WPAS_SMALL)
  4883. && (ssl->options.mask & SSL_OP_NO_TLSv1_3) == 0
  4884. #endif
  4885. ) {
  4886. cnt++;
  4887. }
  4888. if (ssl->options.downgrade) {
  4889. #ifndef WOLFSSL_NO_TLS12
  4890. if (versionIsLessEqual(
  4891. isDtls, ssl->options.minDowngrade, tls12Minor)
  4892. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4893. defined(WOLFSSL_WPAS_SMALL)
  4894. && (ssl->options.mask & SSL_OP_NO_TLSv1_2) == 0
  4895. #endif
  4896. ) {
  4897. cnt++;
  4898. }
  4899. #endif
  4900. #ifndef NO_OLD_TLS
  4901. if (versionIsLessEqual(
  4902. isDtls, ssl->options.minDowngrade, tls11Minor)
  4903. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4904. defined(WOLFSSL_WPAS_SMALL)
  4905. && (ssl->options.mask & SSL_OP_NO_TLSv1_1) == 0
  4906. #endif
  4907. ) {
  4908. cnt++;
  4909. }
  4910. #ifdef WOLFSSL_ALLOW_TLSV10
  4911. if (!ssl->options.dtls && (ssl->options.minDowngrade <= TLSv1_MINOR)
  4912. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4913. defined(WOLFSSL_WPAS_SMALL)
  4914. && (ssl->options.mask & SSL_OP_NO_TLSv1) == 0
  4915. #endif
  4916. ) {
  4917. cnt++;
  4918. }
  4919. #endif
  4920. #endif
  4921. }
  4922. *pSz += (word16)(OPAQUE8_LEN + cnt * OPAQUE16_LEN);
  4923. }
  4924. else if (msgType == server_hello || msgType == hello_retry_request) {
  4925. *pSz += OPAQUE16_LEN;
  4926. }
  4927. else {
  4928. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4929. return SANITY_MSG_E;
  4930. }
  4931. return 0;
  4932. }
  4933. /* Writes the SupportedVersions extension into the buffer.
  4934. *
  4935. * data The SSL/TLS object.
  4936. * output The buffer to write the extension into.
  4937. * msgType The type of the message this extension is being written into.
  4938. * returns the length of data that was written.
  4939. */
  4940. static int TLSX_SupportedVersions_Write(void* data, byte* output,
  4941. byte msgType, word16* pSz)
  4942. {
  4943. WOLFSSL* ssl = (WOLFSSL*)data;
  4944. byte tls13minor, tls12minor, tls11minor, isDtls = 0;
  4945. tls13minor = (byte)TLSv1_3_MINOR;
  4946. tls12minor = (byte)TLSv1_2_MINOR;
  4947. tls11minor = (byte)TLSv1_1_MINOR;
  4948. /* unused in some configuration */
  4949. (void)tls11minor;
  4950. (void)tls12minor;
  4951. #ifdef WOLFSSL_DTLS13
  4952. if (ssl->options.dtls) {
  4953. tls13minor = (byte)DTLSv1_3_MINOR;
  4954. tls12minor = (byte)DTLSv1_2_MINOR;
  4955. tls11minor = (byte)DTLS_MINOR;
  4956. isDtls = 1;
  4957. }
  4958. #endif /* WOLFSSL_DTLS13 */
  4959. if (msgType == client_hello) {
  4960. byte major = ssl->ctx->method->version.major;
  4961. byte* cnt = output++;
  4962. *cnt = 0;
  4963. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls13minor)
  4964. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4965. defined(WOLFSSL_WPAS_SMALL)
  4966. && (ssl->options.mask & SSL_OP_NO_TLSv1_3) == 0
  4967. #endif
  4968. ) {
  4969. *cnt += OPAQUE16_LEN;
  4970. #ifdef WOLFSSL_TLS13_DRAFT
  4971. /* The TLS draft major number. */
  4972. *(output++) = TLS_DRAFT_MAJOR;
  4973. /* Version of draft supported. */
  4974. *(output++) = TLS_DRAFT_MINOR;
  4975. #else
  4976. *(output++) = major;
  4977. *(output++) = tls13minor;
  4978. #endif
  4979. }
  4980. if (ssl->options.downgrade) {
  4981. #ifndef WOLFSSL_NO_TLS12
  4982. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls12minor)
  4983. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4984. defined(WOLFSSL_WPAS_SMALL)
  4985. && (ssl->options.mask & SSL_OP_NO_TLSv1_2) == 0
  4986. #endif
  4987. ) {
  4988. *cnt += OPAQUE16_LEN;
  4989. *(output++) = major;
  4990. *(output++) = tls12minor;
  4991. }
  4992. #endif
  4993. #ifndef NO_OLD_TLS
  4994. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls11minor)
  4995. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4996. defined(WOLFSSL_WPAS_SMALL)
  4997. && (ssl->options.mask & SSL_OP_NO_TLSv1_1) == 0
  4998. #endif
  4999. ) {
  5000. *cnt += OPAQUE16_LEN;
  5001. *(output++) = major;
  5002. *(output++) = tls11minor;
  5003. }
  5004. #ifdef WOLFSSL_ALLOW_TLSV10
  5005. if (!ssl->options.dtls && (ssl->options.minDowngrade <= TLSv1_MINOR)
  5006. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  5007. defined(WOLFSSL_WPAS_SMALL)
  5008. && (ssl->options.mask & SSL_OP_NO_TLSv1) == 0
  5009. #endif
  5010. ) {
  5011. *cnt += OPAQUE16_LEN;
  5012. *(output++) = major;
  5013. *(output++) = (byte)TLSv1_MINOR;
  5014. }
  5015. #endif
  5016. #endif
  5017. }
  5018. *pSz += (word16)(OPAQUE8_LEN + *cnt);
  5019. }
  5020. else if (msgType == server_hello || msgType == hello_retry_request) {
  5021. output[0] = ssl->version.major;
  5022. output[1] = ssl->version.minor;
  5023. *pSz += OPAQUE16_LEN;
  5024. }
  5025. else {
  5026. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5027. return SANITY_MSG_E;
  5028. }
  5029. return 0;
  5030. }
  5031. /* Parse the SupportedVersions extension.
  5032. *
  5033. * ssl The SSL/TLS object.
  5034. * input The buffer with the extension data.
  5035. * length The length of the extension data.
  5036. * msgType The type of the message this extension is being parsed from.
  5037. * pv The output ProtocolVersion for the negotiated version
  5038. * opts The output options structure. Can be NULL.
  5039. * exts The output extensions list. Can be NULL.
  5040. * returns 0 on success, otherwise failure.
  5041. */
  5042. int TLSX_SupportedVersions_Parse(const WOLFSSL* ssl, const byte* input,
  5043. word16 length, byte msgType, ProtocolVersion* pv, Options* opts,
  5044. TLSX** exts)
  5045. {
  5046. /* The client's greatest minor version that we support */
  5047. byte clientGreatestMinor = SSLv3_MINOR;
  5048. int ret;
  5049. byte major, minor;
  5050. byte tls13minor, tls12minor;
  5051. byte isDtls;
  5052. tls13minor = TLSv1_3_MINOR;
  5053. tls12minor = TLSv1_2_MINOR;
  5054. isDtls = ssl->options.dtls == 1;
  5055. #ifdef WOLFSSL_DTLS13
  5056. if (ssl->options.dtls) {
  5057. tls13minor = DTLSv1_3_MINOR;
  5058. tls12minor = DTLSv1_2_MINOR;
  5059. clientGreatestMinor = DTLS_MINOR;
  5060. }
  5061. #endif /* WOLFSSL_DTLS13 */
  5062. if (msgType == client_hello) {
  5063. int i;
  5064. int len;
  5065. int set = 0;
  5066. /* Must contain a length and at least one version. */
  5067. if (length < OPAQUE8_LEN + OPAQUE16_LEN || (length & 1) != 1)
  5068. return BUFFER_ERROR;
  5069. len = *input;
  5070. /* Protocol version array must fill rest of data. */
  5071. if (length != (word16)OPAQUE8_LEN + len)
  5072. return BUFFER_ERROR;
  5073. input++;
  5074. /* Find first match. */
  5075. for (i = 0; i < len; i += OPAQUE16_LEN) {
  5076. major = input[i];
  5077. minor = input[i + OPAQUE8_LEN];
  5078. #ifdef WOLFSSL_TLS13_DRAFT
  5079. if (major == TLS_DRAFT_MAJOR && minor == TLS_DRAFT_MINOR) {
  5080. major = SSLv3_MAJOR;
  5081. minor = TLSv1_3_MINOR;
  5082. }
  5083. #else
  5084. if (major == TLS_DRAFT_MAJOR)
  5085. continue;
  5086. #endif
  5087. if (major != ssl->ctx->method->version.major)
  5088. continue;
  5089. /* No upgrade allowed. */
  5090. if (versionIsGreater(isDtls, minor, ssl->version.minor))
  5091. continue;
  5092. /* Check downgrade. */
  5093. if (versionIsLesser(isDtls, minor, ssl->version.minor)) {
  5094. if (!ssl->options.downgrade)
  5095. continue;
  5096. if (versionIsLesser(isDtls, minor, ssl->options.minDowngrade))
  5097. continue;
  5098. }
  5099. if (versionIsGreater(isDtls, minor, clientGreatestMinor))
  5100. clientGreatestMinor = minor;
  5101. set = 1;
  5102. }
  5103. if (!set) {
  5104. /* No common supported version was negotiated */
  5105. SendAlert((WOLFSSL*)ssl, alert_fatal,
  5106. wolfssl_alert_protocol_version);
  5107. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5108. return VERSION_ERROR;
  5109. }
  5110. pv->minor = clientGreatestMinor;
  5111. if (versionIsAtLeast(isDtls, clientGreatestMinor, tls13minor)) {
  5112. if (opts != NULL)
  5113. opts->tls1_3 = 1;
  5114. /* TLS v1.3 requires supported version extension */
  5115. if (exts != NULL &&
  5116. TLSX_Find(*exts, TLSX_SUPPORTED_VERSIONS) == NULL) {
  5117. ret = TLSX_Push(exts,
  5118. TLSX_SUPPORTED_VERSIONS, ssl, ssl->heap);
  5119. if (ret != 0) {
  5120. return ret;
  5121. }
  5122. /* *exts should be pointing to the TLSX_SUPPORTED_VERSIONS
  5123. * ext in the list since it was pushed. */
  5124. (*exts)->resp = 1;
  5125. }
  5126. }
  5127. }
  5128. else if (msgType == server_hello || msgType == hello_retry_request) {
  5129. /* Must contain one version. */
  5130. if (length != OPAQUE16_LEN)
  5131. return BUFFER_ERROR;
  5132. major = input[0];
  5133. minor = input[OPAQUE8_LEN];
  5134. if (major != ssl->ctx->method->version.major) {
  5135. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5136. return VERSION_ERROR;
  5137. }
  5138. /* Can't downgrade with this extension below TLS v1.3. */
  5139. if (versionIsLesser(isDtls, minor, tls13minor)) {
  5140. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5141. return VERSION_ERROR;
  5142. }
  5143. /* Version is TLS v1.2 to handle downgrading from TLS v1.3+. */
  5144. if (ssl->options.downgrade && ssl->version.minor == tls12minor) {
  5145. /* Set minor version back to TLS v1.3+ */
  5146. pv->minor = ssl->ctx->method->version.minor;
  5147. }
  5148. /* No upgrade allowed. */
  5149. if (versionIsLesser(isDtls, ssl->version.minor, minor)) {
  5150. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5151. return VERSION_ERROR;
  5152. }
  5153. /* Check downgrade. */
  5154. if (versionIsGreater(isDtls, ssl->version.minor, minor)) {
  5155. if (!ssl->options.downgrade) {
  5156. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5157. return VERSION_ERROR;
  5158. }
  5159. if (versionIsLesser(
  5160. isDtls, minor, ssl->options.minDowngrade)) {
  5161. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5162. return VERSION_ERROR;
  5163. }
  5164. /* Downgrade the version. */
  5165. pv->minor = minor;
  5166. }
  5167. }
  5168. else {
  5169. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5170. return SANITY_MSG_E;
  5171. }
  5172. return 0;
  5173. }
  5174. /* Sets a new SupportedVersions extension into the extension list.
  5175. *
  5176. * extensions The list of extensions.
  5177. * data The extensions specific data.
  5178. * heap The heap used for allocation.
  5179. * returns 0 on success, otherwise failure.
  5180. */
  5181. static int TLSX_SetSupportedVersions(TLSX** extensions, const void* data,
  5182. void* heap)
  5183. {
  5184. if (extensions == NULL || data == NULL)
  5185. return BAD_FUNC_ARG;
  5186. return TLSX_Push(extensions, TLSX_SUPPORTED_VERSIONS, data, heap);
  5187. }
  5188. #define SV_GET_SIZE TLSX_SupportedVersions_GetSize
  5189. #define SV_WRITE TLSX_SupportedVersions_Write
  5190. #define SV_PARSE TLSX_SupportedVersions_Parse
  5191. #else
  5192. #define SV_GET_SIZE(a, b, c) 0
  5193. #define SV_WRITE(a, b, c, d) 0
  5194. #define SV_PARSE(a, b, c, d, e, f, g) 0
  5195. #endif /* WOLFSSL_TLS13 */
  5196. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  5197. /******************************************************************************/
  5198. /* Cookie */
  5199. /******************************************************************************/
  5200. /* Free the cookie data.
  5201. *
  5202. * cookie Cookie data.
  5203. * heap The heap used for allocation.
  5204. */
  5205. static void TLSX_Cookie_FreeAll(Cookie* cookie, void* heap)
  5206. {
  5207. (void)heap;
  5208. if (cookie != NULL)
  5209. XFREE(cookie, heap, DYNAMIC_TYPE_TLSX);
  5210. }
  5211. /* Get the size of the encoded Cookie extension.
  5212. * In messages: ClientHello and HelloRetryRequest.
  5213. *
  5214. * cookie The cookie to write.
  5215. * msgType The type of the message this extension is being written into.
  5216. * returns the number of bytes of the encoded Cookie extension.
  5217. */
  5218. static int TLSX_Cookie_GetSize(Cookie* cookie, byte msgType, word16* pSz)
  5219. {
  5220. if (msgType == client_hello || msgType == hello_retry_request) {
  5221. *pSz += OPAQUE16_LEN + cookie->len;
  5222. }
  5223. else {
  5224. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5225. return SANITY_MSG_E;
  5226. }
  5227. return 0;
  5228. }
  5229. /* Writes the Cookie extension into the output buffer.
  5230. * Assumes that the the output buffer is big enough to hold data.
  5231. * In messages: ClientHello and HelloRetryRequest.
  5232. *
  5233. * cookie The cookie to write.
  5234. * output The buffer to write into.
  5235. * msgType The type of the message this extension is being written into.
  5236. * returns the number of bytes written into the buffer.
  5237. */
  5238. static int TLSX_Cookie_Write(Cookie* cookie, byte* output, byte msgType,
  5239. word16* pSz)
  5240. {
  5241. if (msgType == client_hello || msgType == hello_retry_request) {
  5242. c16toa(cookie->len, output);
  5243. output += OPAQUE16_LEN;
  5244. XMEMCPY(output, cookie->data, cookie->len);
  5245. *pSz += OPAQUE16_LEN + cookie->len;
  5246. }
  5247. else {
  5248. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5249. return SANITY_MSG_E;
  5250. }
  5251. return 0;
  5252. }
  5253. /* Parse the Cookie extension.
  5254. * In messages: ClientHello and HelloRetryRequest.
  5255. *
  5256. * ssl The SSL/TLS object.
  5257. * input The extension data.
  5258. * length The length of the extension data.
  5259. * msgType The type of the message this extension is being parsed from.
  5260. * returns 0 on success and other values indicate failure.
  5261. */
  5262. static int TLSX_Cookie_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  5263. byte msgType)
  5264. {
  5265. word16 len;
  5266. word16 idx = 0;
  5267. TLSX* extension;
  5268. Cookie* cookie;
  5269. if (msgType != client_hello && msgType != hello_retry_request) {
  5270. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5271. return SANITY_MSG_E;
  5272. }
  5273. /* Message contains length and Cookie which must be at least one byte
  5274. * in length.
  5275. */
  5276. if (length < OPAQUE16_LEN + 1)
  5277. return BUFFER_E;
  5278. ato16(input + idx, &len);
  5279. idx += OPAQUE16_LEN;
  5280. if (length - idx != len)
  5281. return BUFFER_E;
  5282. if (msgType == hello_retry_request)
  5283. return TLSX_Cookie_Use(ssl, input + idx, len, NULL, 0, 0,
  5284. &ssl->extensions);
  5285. /* client_hello */
  5286. extension = TLSX_Find(ssl->extensions, TLSX_COOKIE);
  5287. if (extension == NULL) {
  5288. #ifdef WOLFSSL_DTLS13
  5289. if (ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version))
  5290. /* Allow a cookie extension with DTLS 1.3 because it is possible
  5291. * that a different SSL instance sent the cookie but we are now
  5292. * receiving it. */
  5293. return TLSX_Cookie_Use(ssl, input + idx, len, NULL, 0, 0,
  5294. &ssl->extensions);
  5295. else
  5296. #endif
  5297. {
  5298. WOLFSSL_ERROR_VERBOSE(HRR_COOKIE_ERROR);
  5299. return HRR_COOKIE_ERROR;
  5300. }
  5301. }
  5302. cookie = (Cookie*)extension->data;
  5303. if (cookie->len != len || XMEMCMP(cookie->data, input + idx, len) != 0) {
  5304. WOLFSSL_ERROR_VERBOSE(HRR_COOKIE_ERROR);
  5305. return HRR_COOKIE_ERROR;
  5306. }
  5307. /* Request seen. */
  5308. extension->resp = 0;
  5309. return 0;
  5310. }
  5311. /* Use the data to create a new Cookie object in the extensions.
  5312. *
  5313. * ssl SSL/TLS object.
  5314. * data Cookie data.
  5315. * len Length of cookie data in bytes.
  5316. * mac MAC data.
  5317. * macSz Length of MAC data in bytes.
  5318. * resp Indicates the extension will go into a response (HelloRetryRequest).
  5319. * returns 0 on success and other values indicate failure.
  5320. */
  5321. int TLSX_Cookie_Use(const WOLFSSL* ssl, const byte* data, word16 len, byte* mac,
  5322. byte macSz, int resp, TLSX** exts)
  5323. {
  5324. int ret = 0;
  5325. TLSX* extension;
  5326. Cookie* cookie;
  5327. /* Find the cookie extension if it exists. */
  5328. extension = TLSX_Find(*exts, TLSX_COOKIE);
  5329. if (extension == NULL) {
  5330. /* Push new cookie extension. */
  5331. ret = TLSX_Push(exts, TLSX_COOKIE, NULL, ssl->heap);
  5332. if (ret != 0)
  5333. return ret;
  5334. extension = TLSX_Find(*exts, TLSX_COOKIE);
  5335. if (extension == NULL)
  5336. return MEMORY_E;
  5337. }
  5338. cookie = (Cookie*)XMALLOC(sizeof(Cookie) + len + macSz, ssl->heap,
  5339. DYNAMIC_TYPE_TLSX);
  5340. if (cookie == NULL)
  5341. return MEMORY_E;
  5342. cookie->len = len + macSz;
  5343. XMEMCPY(cookie->data, data, len);
  5344. if (mac != NULL)
  5345. XMEMCPY(cookie->data + len, mac, macSz);
  5346. if (extension->data != NULL)
  5347. XFREE(extension->data, ssl->heap, DYNAMIC_TYPE_TLSX);
  5348. extension->data = (void*)cookie;
  5349. extension->resp = (byte)resp;
  5350. return 0;
  5351. }
  5352. #define CKE_FREE_ALL TLSX_Cookie_FreeAll
  5353. #define CKE_GET_SIZE TLSX_Cookie_GetSize
  5354. #define CKE_WRITE TLSX_Cookie_Write
  5355. #define CKE_PARSE TLSX_Cookie_Parse
  5356. #else
  5357. #define CKE_FREE_ALL(a, b) 0
  5358. #define CKE_GET_SIZE(a, b, c) 0
  5359. #define CKE_WRITE(a, b, c, d) 0
  5360. #define CKE_PARSE(a, b, c, d) 0
  5361. #endif
  5362. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  5363. /******************************************************************************/
  5364. /* Signature Algorithms */
  5365. /******************************************************************************/
  5366. /* Return the size of the SignatureAlgorithms extension's data.
  5367. *
  5368. * data Unused
  5369. * returns the length of data that will be in the extension.
  5370. */
  5371. static word16 TLSX_SignatureAlgorithms_GetSize(void* data)
  5372. {
  5373. SignatureAlgorithms* sa = (SignatureAlgorithms*)data;
  5374. if (sa->hashSigAlgoSz == 0)
  5375. return OPAQUE16_LEN + WOLFSSL_SUITES(sa->ssl)->hashSigAlgoSz;
  5376. else
  5377. return OPAQUE16_LEN + sa->hashSigAlgoSz;
  5378. }
  5379. /* Creates a bit string of supported hash algorithms with RSA PSS.
  5380. * The bit string is used when determining which signature algorithm to use
  5381. * when creating the CertificateVerify message.
  5382. * Note: Valid data has an even length as each signature algorithm is two bytes.
  5383. *
  5384. * ssl The SSL/TLS object.
  5385. * input The buffer with the list of supported signature algorithms.
  5386. * length The length of the list in bytes.
  5387. * returns 0 on success, BUFFER_ERROR when the length is not even.
  5388. */
  5389. static int TLSX_SignatureAlgorithms_MapPss(WOLFSSL *ssl, const byte* input,
  5390. word16 length)
  5391. {
  5392. word16 i;
  5393. if ((length & 1) == 1)
  5394. return BUFFER_ERROR;
  5395. ssl->pssAlgo = 0;
  5396. for (i = 0; i < length; i += 2) {
  5397. if (input[i] == rsa_pss_sa_algo && input[i + 1] <= sha512_mac)
  5398. ssl->pssAlgo |= 1 << input[i + 1];
  5399. #ifdef WOLFSSL_TLS13
  5400. if (input[i] == rsa_pss_sa_algo && input[i + 1] >= pss_sha256 &&
  5401. input[i + 1] <= pss_sha512) {
  5402. ssl->pssAlgo |= 1 << input[i + 1];
  5403. }
  5404. #endif
  5405. }
  5406. return 0;
  5407. }
  5408. /* Writes the SignatureAlgorithms extension into the buffer.
  5409. *
  5410. * data Unused
  5411. * output The buffer to write the extension into.
  5412. * returns the length of data that was written.
  5413. */
  5414. static word16 TLSX_SignatureAlgorithms_Write(void* data, byte* output)
  5415. {
  5416. SignatureAlgorithms* sa = (SignatureAlgorithms*)data;
  5417. const Suites* suites = WOLFSSL_SUITES(sa->ssl);
  5418. word16 hashSigAlgoSz;
  5419. if (sa->hashSigAlgoSz == 0) {
  5420. c16toa(suites->hashSigAlgoSz, output);
  5421. XMEMCPY(output + OPAQUE16_LEN, suites->hashSigAlgo,
  5422. suites->hashSigAlgoSz);
  5423. hashSigAlgoSz = suites->hashSigAlgoSz;
  5424. }
  5425. else {
  5426. c16toa(sa->hashSigAlgoSz, output);
  5427. XMEMCPY(output + OPAQUE16_LEN, sa->hashSigAlgo,
  5428. sa->hashSigAlgoSz);
  5429. hashSigAlgoSz = sa->hashSigAlgoSz;
  5430. }
  5431. #ifndef NO_RSA
  5432. TLSX_SignatureAlgorithms_MapPss(sa->ssl, output + OPAQUE16_LEN,
  5433. hashSigAlgoSz);
  5434. #endif
  5435. return OPAQUE16_LEN + hashSigAlgoSz;
  5436. }
  5437. /* Parse the SignatureAlgorithms extension.
  5438. *
  5439. * ssl The SSL/TLS object.
  5440. * input The buffer with the extension data.
  5441. * length The length of the extension data.
  5442. * returns 0 on success, otherwise failure.
  5443. */
  5444. static int TLSX_SignatureAlgorithms_Parse(WOLFSSL *ssl, const byte* input,
  5445. word16 length, byte isRequest, Suites* suites)
  5446. {
  5447. word16 len;
  5448. if (!isRequest)
  5449. return BUFFER_ERROR;
  5450. /* Must contain a length and at least algorithm. */
  5451. if (length < OPAQUE16_LEN + OPAQUE16_LEN || (length & 1) != 0)
  5452. return BUFFER_ERROR;
  5453. ato16(input, &len);
  5454. input += OPAQUE16_LEN;
  5455. /* Algorithm array must fill rest of data. */
  5456. if (length != OPAQUE16_LEN + len)
  5457. return BUFFER_ERROR;
  5458. /* Sig Algo list size must be even. */
  5459. if (suites->hashSigAlgoSz % 2 != 0)
  5460. return BUFFER_ERROR;
  5461. /* truncate hashSigAlgo list if too long */
  5462. suites->hashSigAlgoSz = len;
  5463. if (suites->hashSigAlgoSz > WOLFSSL_MAX_SIGALGO) {
  5464. WOLFSSL_MSG("TLSX SigAlgo list exceeds max, truncating");
  5465. suites->hashSigAlgoSz = WOLFSSL_MAX_SIGALGO;
  5466. }
  5467. XMEMCPY(suites->hashSigAlgo, input, suites->hashSigAlgoSz);
  5468. return TLSX_SignatureAlgorithms_MapPss(ssl, input, len);
  5469. }
  5470. /* Sets a new SignatureAlgorithms extension into the extension list.
  5471. *
  5472. * extensions The list of extensions.
  5473. * data The extensions specific data.
  5474. * heap The heap used for allocation.
  5475. * returns 0 on success, otherwise failure.
  5476. */
  5477. static int TLSX_SetSignatureAlgorithms(TLSX** extensions, WOLFSSL* ssl,
  5478. void* heap)
  5479. {
  5480. SignatureAlgorithms* sa;
  5481. int ret;
  5482. if (extensions == NULL)
  5483. return BAD_FUNC_ARG;
  5484. /* Already present */
  5485. if (TLSX_Find(*extensions, TLSX_SIGNATURE_ALGORITHMS) != NULL)
  5486. return 0;
  5487. sa = TLSX_SignatureAlgorithms_New(ssl, 0, heap);
  5488. if (sa == NULL)
  5489. return MEMORY_ERROR;
  5490. ret = TLSX_Push(extensions, TLSX_SIGNATURE_ALGORITHMS, sa, heap);
  5491. if (ret != 0)
  5492. TLSX_SignatureAlgorithms_FreeAll(sa, heap);
  5493. return ret;
  5494. }
  5495. SignatureAlgorithms* TLSX_SignatureAlgorithms_New(WOLFSSL* ssl,
  5496. word16 hashSigAlgoSz, void* heap)
  5497. {
  5498. SignatureAlgorithms* sa;
  5499. (void)heap;
  5500. sa = (SignatureAlgorithms*)XMALLOC(sizeof(*sa) + hashSigAlgoSz, heap,
  5501. DYNAMIC_TYPE_TLSX);
  5502. if (sa != NULL) {
  5503. XMEMSET(sa, 0, sizeof(*sa) + hashSigAlgoSz);
  5504. sa->ssl = ssl;
  5505. sa->hashSigAlgoSz = hashSigAlgoSz;
  5506. }
  5507. return sa;
  5508. }
  5509. void TLSX_SignatureAlgorithms_FreeAll(SignatureAlgorithms* sa,
  5510. void* heap)
  5511. {
  5512. XFREE(sa, heap, DYNAMIC_TYPE_TLSX);
  5513. (void)heap;
  5514. }
  5515. #define SA_GET_SIZE TLSX_SignatureAlgorithms_GetSize
  5516. #define SA_WRITE TLSX_SignatureAlgorithms_Write
  5517. #define SA_PARSE TLSX_SignatureAlgorithms_Parse
  5518. #define SA_FREE_ALL TLSX_SignatureAlgorithms_FreeAll
  5519. #endif
  5520. /******************************************************************************/
  5521. /* Signature Algorithms Certificate */
  5522. /******************************************************************************/
  5523. #if defined(WOLFSSL_TLS13) && !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  5524. /* Return the size of the SignatureAlgorithms extension's data.
  5525. *
  5526. * data Unused
  5527. * returns the length of data that will be in the extension.
  5528. */
  5529. static word16 TLSX_SignatureAlgorithmsCert_GetSize(void* data)
  5530. {
  5531. WOLFSSL* ssl = (WOLFSSL*)data;
  5532. return OPAQUE16_LEN + ssl->certHashSigAlgoSz;
  5533. }
  5534. /* Writes the SignatureAlgorithmsCert extension into the buffer.
  5535. *
  5536. * data Unused
  5537. * output The buffer to write the extension into.
  5538. * returns the length of data that was written.
  5539. */
  5540. static word16 TLSX_SignatureAlgorithmsCert_Write(void* data, byte* output)
  5541. {
  5542. WOLFSSL* ssl = (WOLFSSL*)data;
  5543. c16toa(ssl->certHashSigAlgoSz, output);
  5544. XMEMCPY(output + OPAQUE16_LEN, ssl->certHashSigAlgo,
  5545. ssl->certHashSigAlgoSz);
  5546. return OPAQUE16_LEN + ssl->certHashSigAlgoSz;
  5547. }
  5548. /* Parse the SignatureAlgorithmsCert extension.
  5549. *
  5550. * ssl The SSL/TLS object.
  5551. * input The buffer with the extension data.
  5552. * length The length of the extension data.
  5553. * returns 0 on success, otherwise failure.
  5554. */
  5555. static int TLSX_SignatureAlgorithmsCert_Parse(WOLFSSL *ssl, const byte* input,
  5556. word16 length, byte isRequest)
  5557. {
  5558. word16 len;
  5559. if (!isRequest)
  5560. return BUFFER_ERROR;
  5561. /* Must contain a length and at least algorithm. */
  5562. if (length < OPAQUE16_LEN + OPAQUE16_LEN || (length & 1) != 0)
  5563. return BUFFER_ERROR;
  5564. ato16(input, &len);
  5565. input += OPAQUE16_LEN;
  5566. /* Algorithm array must fill rest of data. */
  5567. if (length != OPAQUE16_LEN + len)
  5568. return BUFFER_ERROR;
  5569. /* truncate hashSigAlgo list if too long */
  5570. ssl->certHashSigAlgoSz = len;
  5571. if (ssl->certHashSigAlgoSz > WOLFSSL_MAX_SIGALGO) {
  5572. WOLFSSL_MSG("TLSX SigAlgo list exceeds max, truncating");
  5573. ssl->certHashSigAlgoSz = WOLFSSL_MAX_SIGALGO;
  5574. }
  5575. XMEMCPY(ssl->certHashSigAlgo, input, ssl->certHashSigAlgoSz);
  5576. return 0;
  5577. }
  5578. /* Sets a new SignatureAlgorithmsCert extension into the extension list.
  5579. *
  5580. * extensions The list of extensions.
  5581. * data The extensions specific data.
  5582. * heap The heap used for allocation.
  5583. * returns 0 on success, otherwise failure.
  5584. */
  5585. static int TLSX_SetSignatureAlgorithmsCert(TLSX** extensions,
  5586. const WOLFSSL* data, void* heap)
  5587. {
  5588. if (extensions == NULL)
  5589. return BAD_FUNC_ARG;
  5590. return TLSX_Push(extensions, TLSX_SIGNATURE_ALGORITHMS_CERT, data, heap);
  5591. }
  5592. #define SAC_GET_SIZE TLSX_SignatureAlgorithmsCert_GetSize
  5593. #define SAC_WRITE TLSX_SignatureAlgorithmsCert_Write
  5594. #define SAC_PARSE TLSX_SignatureAlgorithmsCert_Parse
  5595. #endif /* WOLFSSL_TLS13 */
  5596. /******************************************************************************/
  5597. /* Key Share */
  5598. /******************************************************************************/
  5599. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  5600. /* Create a key share entry using named Diffie-Hellman parameters group.
  5601. * Generates a key pair.
  5602. *
  5603. * ssl The SSL/TLS object.
  5604. * kse The key share entry object.
  5605. * returns 0 on success, otherwise failure.
  5606. */
  5607. static int TLSX_KeyShare_GenDhKey(WOLFSSL *ssl, KeyShareEntry* kse)
  5608. {
  5609. int ret = 0;
  5610. #if !defined(NO_DH) && (!defined(NO_CERTS) || !defined(NO_PSK))
  5611. word32 pSz = 0, pvtSz = 0;
  5612. DhKey* dhKey = (DhKey*)kse->key;
  5613. /* Pick the parameters from the named group. */
  5614. #ifdef HAVE_PUBLIC_FFDHE
  5615. const DhParams* params = NULL;
  5616. switch (kse->group) {
  5617. #ifdef HAVE_FFDHE_2048
  5618. case WOLFSSL_FFDHE_2048:
  5619. params = wc_Dh_ffdhe2048_Get();
  5620. pvtSz = 29;
  5621. break;
  5622. #endif
  5623. #ifdef HAVE_FFDHE_3072
  5624. case WOLFSSL_FFDHE_3072:
  5625. params = wc_Dh_ffdhe3072_Get();
  5626. pvtSz = 34;
  5627. break;
  5628. #endif
  5629. #ifdef HAVE_FFDHE_4096
  5630. case WOLFSSL_FFDHE_4096:
  5631. params = wc_Dh_ffdhe4096_Get();
  5632. pvtSz = 39;
  5633. break;
  5634. #endif
  5635. #ifdef HAVE_FFDHE_6144
  5636. case WOLFSSL_FFDHE_6144:
  5637. params = wc_Dh_ffdhe6144_Get();
  5638. pvtSz = 46;
  5639. break;
  5640. #endif
  5641. #ifdef HAVE_FFDHE_8192
  5642. case WOLFSSL_FFDHE_8192:
  5643. params = wc_Dh_ffdhe8192_Get();
  5644. pvtSz = 52;
  5645. break;
  5646. #endif
  5647. default:
  5648. break;
  5649. }
  5650. if (params == NULL)
  5651. return BAD_FUNC_ARG;
  5652. pSz = params->p_len;
  5653. #else
  5654. pvtSz = wc_DhGetNamedKeyMinSize(kse->group);
  5655. if (pvtSz == 0) {
  5656. return BAD_FUNC_ARG;
  5657. }
  5658. ret = wc_DhGetNamedKeyParamSize(kse->group, &pSz, NULL, NULL);
  5659. if (ret != 0) {
  5660. return BAD_FUNC_ARG;
  5661. }
  5662. #endif
  5663. /* Trigger Key Generation */
  5664. if (kse->pubKey == NULL || kse->privKey == NULL) {
  5665. if (kse->key == NULL) {
  5666. kse->key = (DhKey*)XMALLOC(sizeof(DhKey), ssl->heap,
  5667. DYNAMIC_TYPE_DH);
  5668. if (kse->key == NULL)
  5669. return MEMORY_E;
  5670. /* Setup Key */
  5671. ret = wc_InitDhKey_ex((DhKey*)kse->key, ssl->heap, ssl->devId);
  5672. if (ret == 0) {
  5673. dhKey = (DhKey*)kse->key;
  5674. #ifdef HAVE_PUBLIC_FFDHE
  5675. ret = wc_DhSetKey(dhKey, params->p, params->p_len, params->g,
  5676. params->g_len);
  5677. #else
  5678. ret = wc_DhSetNamedKey(dhKey, kse->group);
  5679. #endif
  5680. }
  5681. }
  5682. /* Allocate space for the private and public key */
  5683. if (ret == 0 && kse->pubKey == NULL) {
  5684. kse->pubKey = (byte*)XMALLOC(pSz, ssl->heap,
  5685. DYNAMIC_TYPE_PUBLIC_KEY);
  5686. if (kse->pubKey == NULL)
  5687. ret = MEMORY_E;
  5688. }
  5689. if (ret == 0 && kse->privKey == NULL) {
  5690. kse->privKey = (byte*)XMALLOC(pvtSz, ssl->heap,
  5691. DYNAMIC_TYPE_PRIVATE_KEY);
  5692. if (kse->privKey == NULL)
  5693. ret = MEMORY_E;
  5694. }
  5695. if (ret == 0) {
  5696. #if defined(WOLFSSL_STATIC_EPHEMERAL) && defined(WOLFSSL_DH_EXTRA)
  5697. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_DH, kse->key);
  5698. kse->pubKeyLen = pSz;
  5699. kse->keyLen = pvtSz;
  5700. if (ret == 0) {
  5701. ret = wc_DhExportKeyPair(dhKey,
  5702. (byte*)kse->privKey, &kse->keyLen, /* private */
  5703. kse->pubKey, &kse->pubKeyLen /* public */
  5704. );
  5705. }
  5706. else
  5707. #endif
  5708. {
  5709. /* Generate a new key pair */
  5710. /* For async this is called once and when event is done, the
  5711. * provided buffers will be populated.
  5712. * Final processing is zero pad below. */
  5713. kse->pubKeyLen = pSz;
  5714. kse->keyLen = pvtSz;
  5715. ret = DhGenKeyPair(ssl, dhKey,
  5716. (byte*)kse->privKey, &kse->keyLen, /* private */
  5717. kse->pubKey, &kse->pubKeyLen /* public */
  5718. );
  5719. #ifdef WOLFSSL_ASYNC_CRYPT
  5720. if (ret == WC_PENDING_E) {
  5721. return ret;
  5722. }
  5723. #endif
  5724. }
  5725. }
  5726. }
  5727. if (ret == 0) {
  5728. if (pSz != kse->pubKeyLen) {
  5729. /* Zero pad the front of the public key to match prime "p" size */
  5730. XMEMMOVE(kse->pubKey + pSz - kse->pubKeyLen, kse->pubKey,
  5731. kse->pubKeyLen);
  5732. XMEMSET(kse->pubKey, 0, pSz - kse->pubKeyLen);
  5733. kse->pubKeyLen = pSz;
  5734. }
  5735. if (pvtSz != kse->keyLen) {
  5736. /* Zero pad the front of the private key */
  5737. XMEMMOVE(kse->privKey + pvtSz - kse->keyLen, kse->privKey,
  5738. kse->keyLen);
  5739. XMEMSET(kse->privKey, 0, pvtSz - kse->keyLen);
  5740. kse->keyLen = pvtSz;
  5741. }
  5742. #ifdef WOLFSSL_DEBUG_TLS
  5743. WOLFSSL_MSG("Public DH Key");
  5744. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  5745. #endif
  5746. }
  5747. /* Always release the DH key to free up memory.
  5748. * The DhKey will be setup again in TLSX_KeyShare_ProcessDh */
  5749. if (dhKey != NULL)
  5750. wc_FreeDhKey(dhKey);
  5751. if (kse->key != NULL) {
  5752. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_DH);
  5753. kse->key = NULL;
  5754. }
  5755. if (ret != 0) {
  5756. /* Cleanup on error, otherwise data owned by key share entry */
  5757. if (kse->privKey != NULL) {
  5758. XFREE(kse->privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5759. kse->privKey = NULL;
  5760. }
  5761. if (kse->pubKey != NULL) {
  5762. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5763. kse->pubKey = NULL;
  5764. }
  5765. }
  5766. #else
  5767. (void)ssl;
  5768. (void)kse;
  5769. ret = NOT_COMPILED_IN;
  5770. WOLFSSL_ERROR_VERBOSE(ret);
  5771. #endif
  5772. return ret;
  5773. }
  5774. /* Create a key share entry using X25519 parameters group.
  5775. * Generates a key pair.
  5776. *
  5777. * ssl The SSL/TLS object.
  5778. * kse The key share entry object.
  5779. * returns 0 on success, otherwise failure.
  5780. */
  5781. static int TLSX_KeyShare_GenX25519Key(WOLFSSL *ssl, KeyShareEntry* kse)
  5782. {
  5783. int ret = 0;
  5784. #ifdef HAVE_CURVE25519
  5785. curve25519_key* key = (curve25519_key*)kse->key;
  5786. if (kse->key == NULL) {
  5787. /* Allocate a Curve25519 key to hold private key. */
  5788. kse->key = (curve25519_key*)XMALLOC(sizeof(curve25519_key), ssl->heap,
  5789. DYNAMIC_TYPE_PRIVATE_KEY);
  5790. if (kse->key == NULL) {
  5791. WOLFSSL_MSG("GenX25519Key memory error");
  5792. return MEMORY_E;
  5793. }
  5794. /* Make an Curve25519 key. */
  5795. ret = wc_curve25519_init_ex((curve25519_key*)kse->key, ssl->heap,
  5796. INVALID_DEVID);
  5797. if (ret == 0) {
  5798. /* setting "key" means okay to call wc_curve25519_free */
  5799. key = (curve25519_key*)kse->key;
  5800. #ifdef WOLFSSL_STATIC_EPHEMERAL
  5801. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_CURVE25519, kse->key);
  5802. if (ret != 0)
  5803. #endif
  5804. {
  5805. ret = wc_curve25519_make_key(ssl->rng, CURVE25519_KEYSIZE, key);
  5806. }
  5807. }
  5808. }
  5809. if (ret == 0 && kse->pubKey == NULL) {
  5810. /* Allocate space for the public key. */
  5811. kse->pubKey = (byte*)XMALLOC(CURVE25519_KEYSIZE, ssl->heap,
  5812. DYNAMIC_TYPE_PUBLIC_KEY);
  5813. if (kse->pubKey == NULL) {
  5814. WOLFSSL_MSG("GenX25519Key pub memory error");
  5815. ret = MEMORY_E;
  5816. }
  5817. }
  5818. if (ret == 0) {
  5819. /* Export Curve25519 public key. */
  5820. kse->pubKeyLen = CURVE25519_KEYSIZE;
  5821. if (wc_curve25519_export_public_ex(key, kse->pubKey, &kse->pubKeyLen,
  5822. EC25519_LITTLE_ENDIAN) != 0) {
  5823. ret = ECC_EXPORT_ERROR;
  5824. WOLFSSL_ERROR_VERBOSE(ret);
  5825. }
  5826. kse->pubKeyLen = CURVE25519_KEYSIZE; /* always CURVE25519_KEYSIZE */
  5827. }
  5828. #ifdef WOLFSSL_DEBUG_TLS
  5829. if (ret == 0) {
  5830. WOLFSSL_MSG("Public Curve25519 Key");
  5831. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  5832. }
  5833. #endif
  5834. if (ret != 0) {
  5835. /* Data owned by key share entry otherwise. */
  5836. if (kse->pubKey != NULL) {
  5837. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5838. kse->pubKey = NULL;
  5839. }
  5840. if (key != NULL)
  5841. wc_curve25519_free(key);
  5842. if (kse->key != NULL) {
  5843. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5844. kse->key = NULL;
  5845. }
  5846. }
  5847. #else
  5848. (void)ssl;
  5849. (void)kse;
  5850. ret = NOT_COMPILED_IN;
  5851. WOLFSSL_ERROR_VERBOSE(ret);
  5852. #endif /* HAVE_CURVE25519 */
  5853. return ret;
  5854. }
  5855. /* Create a key share entry using X448 parameters group.
  5856. * Generates a key pair.
  5857. *
  5858. * ssl The SSL/TLS object.
  5859. * kse The key share entry object.
  5860. * returns 0 on success, otherwise failure.
  5861. */
  5862. static int TLSX_KeyShare_GenX448Key(WOLFSSL *ssl, KeyShareEntry* kse)
  5863. {
  5864. int ret = 0;
  5865. #ifdef HAVE_CURVE448
  5866. curve448_key* key = (curve448_key*)kse->key;
  5867. if (kse->key == NULL) {
  5868. /* Allocate a Curve448 key to hold private key. */
  5869. kse->key = (curve448_key*)XMALLOC(sizeof(curve448_key), ssl->heap,
  5870. DYNAMIC_TYPE_PRIVATE_KEY);
  5871. if (kse->key == NULL) {
  5872. WOLFSSL_MSG("GenX448Key memory error");
  5873. return MEMORY_E;
  5874. }
  5875. /* Make an Curve448 key. */
  5876. ret = wc_curve448_init((curve448_key*)kse->key);
  5877. if (ret == 0) {
  5878. key = (curve448_key*)kse->key;
  5879. #ifdef WOLFSSL_STATIC_EPHEMERAL
  5880. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_CURVE448, kse->key);
  5881. if (ret != 0)
  5882. #endif
  5883. {
  5884. ret = wc_curve448_make_key(ssl->rng, CURVE448_KEY_SIZE, key);
  5885. }
  5886. }
  5887. }
  5888. if (ret == 0 && kse->pubKey == NULL) {
  5889. /* Allocate space for the public key. */
  5890. kse->pubKey = (byte*)XMALLOC(CURVE448_KEY_SIZE, ssl->heap,
  5891. DYNAMIC_TYPE_PUBLIC_KEY);
  5892. if (kse->pubKey == NULL) {
  5893. WOLFSSL_MSG("GenX448Key pub memory error");
  5894. ret = MEMORY_E;
  5895. }
  5896. }
  5897. if (ret == 0) {
  5898. /* Export Curve448 public key. */
  5899. kse->pubKeyLen = CURVE448_KEY_SIZE;
  5900. if (wc_curve448_export_public_ex(key, kse->pubKey, &kse->pubKeyLen,
  5901. EC448_LITTLE_ENDIAN) != 0) {
  5902. ret = ECC_EXPORT_ERROR;
  5903. }
  5904. kse->pubKeyLen = CURVE448_KEY_SIZE; /* always CURVE448_KEY_SIZE */
  5905. }
  5906. #ifdef WOLFSSL_DEBUG_TLS
  5907. if (ret == 0) {
  5908. WOLFSSL_MSG("Public Curve448 Key");
  5909. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  5910. }
  5911. #endif
  5912. if (ret != 0) {
  5913. /* Data owned by key share entry otherwise. */
  5914. if (kse->pubKey != NULL) {
  5915. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5916. kse->pubKey = NULL;
  5917. }
  5918. if (key != NULL)
  5919. wc_curve448_free(key);
  5920. if (kse->key != NULL) {
  5921. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5922. kse->key = NULL;
  5923. }
  5924. }
  5925. #else
  5926. (void)ssl;
  5927. (void)kse;
  5928. ret = NOT_COMPILED_IN;
  5929. WOLFSSL_ERROR_VERBOSE(ret);
  5930. #endif /* HAVE_CURVE448 */
  5931. return ret;
  5932. }
  5933. /* Create a key share entry using named elliptic curve parameters group.
  5934. * Generates a key pair.
  5935. *
  5936. * ssl The SSL/TLS object.
  5937. * kse The key share entry object.
  5938. * returns 0 on success, otherwise failure.
  5939. */
  5940. static int TLSX_KeyShare_GenEccKey(WOLFSSL *ssl, KeyShareEntry* kse)
  5941. {
  5942. int ret = 0;
  5943. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT)
  5944. word32 keySize = 0;
  5945. word16 curveId = (word16) ECC_CURVE_INVALID;
  5946. ecc_key* eccKey = (ecc_key*)kse->key;
  5947. /* TODO: [TLS13] The key sizes should come from wolfcrypt. */
  5948. /* Translate named group to a curve id. */
  5949. switch (kse->group) {
  5950. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  5951. #ifndef NO_ECC_SECP
  5952. case WOLFSSL_ECC_SECP256R1:
  5953. curveId = ECC_SECP256R1;
  5954. keySize = 32;
  5955. break;
  5956. #endif /* !NO_ECC_SECP */
  5957. #endif
  5958. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  5959. #ifndef NO_ECC_SECP
  5960. case WOLFSSL_ECC_SECP384R1:
  5961. curveId = ECC_SECP384R1;
  5962. keySize = 48;
  5963. break;
  5964. #endif /* !NO_ECC_SECP */
  5965. #endif
  5966. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  5967. #ifndef NO_ECC_SECP
  5968. case WOLFSSL_ECC_SECP521R1:
  5969. curveId = ECC_SECP521R1;
  5970. keySize = 66;
  5971. break;
  5972. #endif /* !NO_ECC_SECP */
  5973. #endif
  5974. default:
  5975. WOLFSSL_ERROR_VERBOSE(BAD_FUNC_ARG);
  5976. return BAD_FUNC_ARG;
  5977. }
  5978. if (kse->key == NULL) {
  5979. kse->keyLen = keySize;
  5980. kse->pubKeyLen = keySize * 2 + 1;
  5981. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  5982. ret = tsip_Tls13GenEccKeyPair(ssl, kse);
  5983. if (ret != CRYPTOCB_UNAVAILABLE) {
  5984. return ret;
  5985. }
  5986. #endif
  5987. /* Allocate an ECC key to hold private key. */
  5988. kse->key = (byte*)XMALLOC(sizeof(ecc_key), ssl->heap, DYNAMIC_TYPE_ECC);
  5989. if (kse->key == NULL) {
  5990. WOLFSSL_MSG("EccTempKey Memory error");
  5991. return MEMORY_E;
  5992. }
  5993. /* Make an ECC key */
  5994. ret = wc_ecc_init_ex((ecc_key*)kse->key, ssl->heap, ssl->devId);
  5995. if (ret == 0) {
  5996. /* setting eccKey means okay to call wc_ecc_free */
  5997. eccKey = (ecc_key*)kse->key;
  5998. #ifdef WOLFSSL_STATIC_EPHEMERAL
  5999. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_ECDH, kse->key);
  6000. if (ret != 0)
  6001. #endif
  6002. {
  6003. /* set curve info for EccMakeKey "peer" info */
  6004. ret = wc_ecc_set_curve(eccKey, kse->keyLen, curveId);
  6005. if (ret == 0) {
  6006. /* Generate ephemeral ECC key */
  6007. /* For async this is called once and when event is done, the
  6008. * provided buffers in key be populated.
  6009. * Final processing is x963 key export below. */
  6010. ret = EccMakeKey(ssl, eccKey, eccKey);
  6011. }
  6012. #ifdef WOLFSSL_ASYNC_CRYPT
  6013. if (ret == WC_PENDING_E)
  6014. return ret;
  6015. #endif
  6016. }
  6017. }
  6018. }
  6019. if (ret == 0 && kse->pubKey == NULL) {
  6020. /* Allocate space for the public key */
  6021. kse->pubKey = (byte*)XMALLOC(kse->pubKeyLen, ssl->heap,
  6022. DYNAMIC_TYPE_PUBLIC_KEY);
  6023. if (kse->pubKey == NULL) {
  6024. WOLFSSL_MSG("Key data Memory error");
  6025. ret = MEMORY_E;
  6026. }
  6027. }
  6028. if (ret == 0) {
  6029. XMEMSET(kse->pubKey, 0, kse->pubKeyLen);
  6030. /* Export public key. */
  6031. PRIVATE_KEY_UNLOCK();
  6032. if (wc_ecc_export_x963(eccKey, kse->pubKey, &kse->pubKeyLen) != 0) {
  6033. ret = ECC_EXPORT_ERROR;
  6034. WOLFSSL_ERROR_VERBOSE(ret);
  6035. }
  6036. PRIVATE_KEY_LOCK();
  6037. }
  6038. #ifdef WOLFSSL_DEBUG_TLS
  6039. if (ret == 0) {
  6040. WOLFSSL_MSG("Public ECC Key");
  6041. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  6042. }
  6043. #endif
  6044. if (ret != 0) {
  6045. /* Cleanup on error, otherwise data owned by key share entry */
  6046. if (kse->pubKey != NULL) {
  6047. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6048. kse->pubKey = NULL;
  6049. }
  6050. if (eccKey != NULL)
  6051. wc_ecc_free(eccKey);
  6052. if (kse->key != NULL) {
  6053. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6054. kse->key = NULL;
  6055. }
  6056. }
  6057. #else
  6058. (void)ssl;
  6059. (void)kse;
  6060. ret = NOT_COMPILED_IN;
  6061. WOLFSSL_ERROR_VERBOSE(ret);
  6062. #endif /* HAVE_ECC && HAVE_ECC_KEY_EXPORT */
  6063. return ret;
  6064. }
  6065. #ifdef HAVE_PQC
  6066. static int kyber_id2type(int id, int *type)
  6067. {
  6068. int ret = 0;
  6069. switch (id) {
  6070. #ifdef WOLFSSL_KYBER512
  6071. case WOLFSSL_KYBER_LEVEL1:
  6072. *type = KYBER512;
  6073. break;
  6074. #endif
  6075. #ifdef WOLFSSL_KYBER768
  6076. case WOLFSSL_KYBER_LEVEL3:
  6077. *type = KYBER768;
  6078. break;
  6079. #endif
  6080. #ifdef WOLFSSL_KYBER1024
  6081. case WOLFSSL_KYBER_LEVEL5:
  6082. *type = KYBER1024;
  6083. break;
  6084. #endif
  6085. default:
  6086. ret = NOT_COMPILED_IN;
  6087. break;
  6088. }
  6089. return ret;
  6090. }
  6091. typedef struct PqcHybridMapping {
  6092. int hybrid;
  6093. int ecc;
  6094. int pqc;
  6095. } PqcHybridMapping;
  6096. static const PqcHybridMapping pqc_hybrid_mapping[] = {
  6097. {.hybrid = WOLFSSL_P256_KYBER_LEVEL1, .ecc = WOLFSSL_ECC_SECP256R1,
  6098. .pqc = WOLFSSL_KYBER_LEVEL1},
  6099. {.hybrid = WOLFSSL_P384_KYBER_LEVEL3, .ecc = WOLFSSL_ECC_SECP384R1,
  6100. .pqc = WOLFSSL_KYBER_LEVEL3},
  6101. {.hybrid = WOLFSSL_P521_KYBER_LEVEL5, .ecc = WOLFSSL_ECC_SECP521R1,
  6102. .pqc = WOLFSSL_KYBER_LEVEL5},
  6103. {.hybrid = 0, .ecc = 0, .pqc = 0}
  6104. };
  6105. /* This will map an ecc-pqs hybrid group into its ecc group and pqc kem group.
  6106. * If it cannot find a mapping then *pqc is set to group. ecc is optional. */
  6107. static void findEccPqc(int *ecc, int *pqc, int group)
  6108. {
  6109. int i;
  6110. if (pqc == NULL) {
  6111. return;
  6112. }
  6113. *pqc = 0;
  6114. if (ecc != NULL) {
  6115. *ecc = 0;
  6116. }
  6117. for (i = 0; pqc_hybrid_mapping[i].hybrid != 0; i++) {
  6118. if (pqc_hybrid_mapping[i].hybrid == group) {
  6119. *pqc = pqc_hybrid_mapping[i].pqc;
  6120. if (ecc != NULL) {
  6121. *ecc = pqc_hybrid_mapping[i].ecc;
  6122. }
  6123. break;
  6124. }
  6125. }
  6126. if (*pqc == 0) {
  6127. /* It is not a hybrid, so maybe its simple. */
  6128. *pqc = group;
  6129. }
  6130. }
  6131. /* Create a key share entry using liboqs parameters group.
  6132. * Generates a key pair.
  6133. *
  6134. * ssl The SSL/TLS object.
  6135. * kse The key share entry object.
  6136. * returns 0 on success, otherwise failure.
  6137. */
  6138. static int TLSX_KeyShare_GenPqcKey(WOLFSSL *ssl, KeyShareEntry* kse)
  6139. {
  6140. int ret = 0;
  6141. int type = 0;
  6142. KyberKey kem[1];
  6143. byte* pubKey = NULL;
  6144. byte* privKey = NULL;
  6145. KeyShareEntry *ecc_kse = NULL;
  6146. int oqs_group = 0;
  6147. int ecc_group = 0;
  6148. word32 privSz = 0;
  6149. word32 pubSz = 0;
  6150. findEccPqc(&ecc_group, &oqs_group, kse->group);
  6151. ret = kyber_id2type(oqs_group, &type);
  6152. if (ret == NOT_COMPILED_IN) {
  6153. WOLFSSL_MSG("Invalid Kyber algorithm specified.");
  6154. ret = BAD_FUNC_ARG;
  6155. }
  6156. if (ret == 0) {
  6157. ret = wc_KyberKey_Init(type, kem, ssl->heap, ssl->devId);
  6158. if (ret != 0) {
  6159. WOLFSSL_MSG("Failed to initialize Kyber Key.");
  6160. }
  6161. }
  6162. if (ret == 0) {
  6163. ecc_kse = (KeyShareEntry*)XMALLOC(sizeof(*ecc_kse), ssl->heap,
  6164. DYNAMIC_TYPE_TLSX);
  6165. if (ecc_kse == NULL) {
  6166. WOLFSSL_MSG("ecc_kse memory allocation failure");
  6167. ret = MEMORY_ERROR;
  6168. }
  6169. }
  6170. if (ret == 0) {
  6171. XMEMSET(ecc_kse, 0, sizeof(*ecc_kse));
  6172. ret = wc_KyberKey_PrivateKeySize(kem, &privSz);
  6173. }
  6174. if (ret == 0) {
  6175. ret = wc_KyberKey_PublicKeySize(kem, &pubSz);
  6176. }
  6177. if (ret == 0 && ecc_group != 0) {
  6178. ecc_kse->group = ecc_group;
  6179. ret = TLSX_KeyShare_GenEccKey(ssl, ecc_kse);
  6180. /* If fail, no error message, TLSX_KeyShare_GenEccKey will do it. */
  6181. }
  6182. if (ret == 0) {
  6183. pubKey = (byte*)XMALLOC(ecc_kse->pubKeyLen + pubSz, ssl->heap,
  6184. DYNAMIC_TYPE_PUBLIC_KEY);
  6185. if (pubKey == NULL) {
  6186. WOLFSSL_MSG("pubkey memory allocation failure");
  6187. ret = MEMORY_ERROR;
  6188. }
  6189. }
  6190. if (ret == 0) {
  6191. privKey = (byte*)XMALLOC(privSz, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6192. if (privKey == NULL) {
  6193. WOLFSSL_MSG("privkey memory allocation failure");
  6194. ret = MEMORY_ERROR;
  6195. }
  6196. }
  6197. if (ret == 0) {
  6198. ret = wc_KyberKey_MakeKey(kem, ssl->rng);
  6199. if (ret != 0) {
  6200. WOLFSSL_MSG("Kyber keygen failure");
  6201. }
  6202. }
  6203. if (ret == 0) {
  6204. ret = wc_KyberKey_EncodePublicKey(kem, pubKey + ecc_kse->pubKeyLen,
  6205. pubSz);
  6206. }
  6207. if (ret == 0) {
  6208. ret = wc_KyberKey_EncodePrivateKey(kem, privKey, privSz);
  6209. }
  6210. if (ret == 0) {
  6211. XMEMCPY(pubKey, ecc_kse->pubKey, ecc_kse->pubKeyLen);
  6212. kse->pubKey = pubKey;
  6213. kse->pubKeyLen = ecc_kse->pubKeyLen + pubSz;
  6214. pubKey = NULL;
  6215. /* Note we are saving the OQS private key and ECC private key
  6216. * separately. That's because the ECC private key is not simply a
  6217. * buffer. Its is an ecc_key struct.
  6218. */
  6219. kse->privKey = privKey;
  6220. privKey = NULL;
  6221. kse->key = ecc_kse->key;
  6222. ecc_kse->key = NULL;
  6223. }
  6224. #ifdef WOLFSSL_DEBUG_TLS
  6225. WOLFSSL_MSG("Public Kyber Key");
  6226. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen );
  6227. #endif
  6228. wc_KyberKey_Free(kem);
  6229. TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
  6230. if (pubKey != NULL)
  6231. XFREE(pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6232. if (privKey != NULL)
  6233. XFREE(privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6234. return ret;
  6235. }
  6236. #endif /* HAVE_PQC */
  6237. /* Generate a secret/key using the key share entry.
  6238. *
  6239. * ssl The SSL/TLS object.
  6240. * kse The key share entry holding peer data.
  6241. */
  6242. int TLSX_KeyShare_GenKey(WOLFSSL *ssl, KeyShareEntry *kse)
  6243. {
  6244. int ret;
  6245. /* Named FFDHE groups have a bit set to identify them. */
  6246. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(kse->group))
  6247. ret = TLSX_KeyShare_GenDhKey(ssl, kse);
  6248. else if (kse->group == WOLFSSL_ECC_X25519)
  6249. ret = TLSX_KeyShare_GenX25519Key(ssl, kse);
  6250. else if (kse->group == WOLFSSL_ECC_X448)
  6251. ret = TLSX_KeyShare_GenX448Key(ssl, kse);
  6252. #ifdef HAVE_PQC
  6253. else if (WOLFSSL_NAMED_GROUP_IS_PQC(kse->group))
  6254. ret = TLSX_KeyShare_GenPqcKey(ssl, kse);
  6255. #endif
  6256. else
  6257. ret = TLSX_KeyShare_GenEccKey(ssl, kse);
  6258. #ifdef WOLFSSL_ASYNC_CRYPT
  6259. kse->lastRet = ret;
  6260. #endif
  6261. return ret;
  6262. }
  6263. /* Free the key share dynamic data.
  6264. *
  6265. * list The linked list of key share entry objects.
  6266. * heap The heap used for allocation.
  6267. */
  6268. static void TLSX_KeyShare_FreeAll(KeyShareEntry* list, void* heap)
  6269. {
  6270. KeyShareEntry* current;
  6271. while ((current = list) != NULL) {
  6272. list = current->next;
  6273. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(current->group)) {
  6274. #ifndef NO_DH
  6275. wc_FreeDhKey((DhKey*)current->key);
  6276. #endif
  6277. }
  6278. else if (current->group == WOLFSSL_ECC_X25519) {
  6279. #ifdef HAVE_CURVE25519
  6280. wc_curve25519_free((curve25519_key*)current->key);
  6281. #endif
  6282. }
  6283. else if (current->group == WOLFSSL_ECC_X448) {
  6284. #ifdef HAVE_CURVE448
  6285. wc_curve448_free((curve448_key*)current->key);
  6286. #endif
  6287. }
  6288. #ifdef HAVE_PQC
  6289. else if (WOLFSSL_NAMED_GROUP_IS_PQC(current->group) &&
  6290. current->key != NULL) {
  6291. ForceZero((byte*)current->key, current->keyLen);
  6292. }
  6293. #endif
  6294. else {
  6295. #ifdef HAVE_ECC
  6296. wc_ecc_free((ecc_key*)current->key);
  6297. #endif
  6298. }
  6299. XFREE(current->key, heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6300. #if !defined(NO_DH) && (!defined(NO_CERTS) || !defined(NO_PSK))
  6301. XFREE(current->privKey, heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6302. #endif
  6303. XFREE(current->pubKey, heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6304. XFREE(current->ke, heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6305. XFREE(current, heap, DYNAMIC_TYPE_TLSX);
  6306. }
  6307. (void)heap;
  6308. }
  6309. /* Get the size of the encoded key share extension.
  6310. *
  6311. * list The linked list of key share extensions.
  6312. * msgType The type of the message this extension is being written into.
  6313. * returns the number of bytes of the encoded key share extension.
  6314. */
  6315. static word16 TLSX_KeyShare_GetSize(KeyShareEntry* list, byte msgType)
  6316. {
  6317. word16 len = 0;
  6318. byte isRequest = (msgType == client_hello);
  6319. KeyShareEntry* current;
  6320. /* The named group the server wants to use. */
  6321. if (msgType == hello_retry_request)
  6322. return OPAQUE16_LEN;
  6323. /* List of key exchange groups. */
  6324. if (isRequest)
  6325. len += OPAQUE16_LEN;
  6326. while ((current = list) != NULL) {
  6327. list = current->next;
  6328. if (!isRequest && current->pubKey == NULL)
  6329. continue;
  6330. len += (word16)(KE_GROUP_LEN + OPAQUE16_LEN + current->pubKeyLen);
  6331. }
  6332. return len;
  6333. }
  6334. /* Writes the key share extension into the output buffer.
  6335. * Assumes that the the output buffer is big enough to hold data.
  6336. *
  6337. * list The linked list of key share entries.
  6338. * output The buffer to write into.
  6339. * msgType The type of the message this extension is being written into.
  6340. * returns the number of bytes written into the buffer.
  6341. */
  6342. static word16 TLSX_KeyShare_Write(KeyShareEntry* list, byte* output,
  6343. byte msgType)
  6344. {
  6345. word16 i = 0;
  6346. byte isRequest = (msgType == client_hello);
  6347. KeyShareEntry* current;
  6348. if (msgType == hello_retry_request) {
  6349. c16toa(list->group, output);
  6350. return OPAQUE16_LEN;
  6351. }
  6352. /* ClientHello has a list but ServerHello is only the chosen. */
  6353. if (isRequest)
  6354. i += OPAQUE16_LEN;
  6355. /* Write out all in the list. */
  6356. while ((current = list) != NULL) {
  6357. list = current->next;
  6358. if (!isRequest && current->pubKey == NULL)
  6359. continue;
  6360. c16toa(current->group, &output[i]);
  6361. i += KE_GROUP_LEN;
  6362. c16toa((word16)(current->pubKeyLen), &output[i]);
  6363. i += OPAQUE16_LEN;
  6364. XMEMCPY(&output[i], current->pubKey, current->pubKeyLen);
  6365. i += (word16)current->pubKeyLen;
  6366. }
  6367. /* Write the length of the list if required. */
  6368. if (isRequest)
  6369. c16toa(i - OPAQUE16_LEN, output);
  6370. return i;
  6371. }
  6372. /* Process the DH key share extension on the client side.
  6373. *
  6374. * ssl The SSL/TLS object.
  6375. * keyShareEntry The key share entry object to use to calculate shared secret.
  6376. * returns 0 on success and other values indicate failure.
  6377. */
  6378. static int TLSX_KeyShare_ProcessDh(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6379. {
  6380. int ret = 0;
  6381. #if !defined(NO_DH) && (!defined(NO_CERTS) || !defined(NO_PSK))
  6382. word32 pSz = 0;
  6383. DhKey* dhKey = (DhKey*)keyShareEntry->key;
  6384. #ifdef HAVE_PUBLIC_FFDHE
  6385. const DhParams* params = NULL;
  6386. switch (keyShareEntry->group) {
  6387. #ifdef HAVE_FFDHE_2048
  6388. case WOLFSSL_FFDHE_2048:
  6389. params = wc_Dh_ffdhe2048_Get();
  6390. break;
  6391. #endif
  6392. #ifdef HAVE_FFDHE_3072
  6393. case WOLFSSL_FFDHE_3072:
  6394. params = wc_Dh_ffdhe3072_Get();
  6395. break;
  6396. #endif
  6397. #ifdef HAVE_FFDHE_4096
  6398. case WOLFSSL_FFDHE_4096:
  6399. params = wc_Dh_ffdhe4096_Get();
  6400. break;
  6401. #endif
  6402. #ifdef HAVE_FFDHE_6144
  6403. case WOLFSSL_FFDHE_6144:
  6404. params = wc_Dh_ffdhe6144_Get();
  6405. break;
  6406. #endif
  6407. #ifdef HAVE_FFDHE_8192
  6408. case WOLFSSL_FFDHE_8192:
  6409. params = wc_Dh_ffdhe8192_Get();
  6410. break;
  6411. #endif
  6412. default:
  6413. break;
  6414. }
  6415. if (params == NULL) {
  6416. WOLFSSL_ERROR_VERBOSE(PEER_KEY_ERROR);
  6417. return PEER_KEY_ERROR;
  6418. }
  6419. pSz = params->p_len;
  6420. #else
  6421. ret = wc_DhGetNamedKeyParamSize(keyShareEntry->group, &pSz, NULL, NULL);
  6422. if (ret != 0 || pSz == 0) {
  6423. WOLFSSL_ERROR_VERBOSE(PEER_KEY_ERROR);
  6424. return PEER_KEY_ERROR;
  6425. }
  6426. #endif
  6427. /* if DhKey is not setup, do it now */
  6428. if (keyShareEntry->key == NULL) {
  6429. keyShareEntry->key = (DhKey*)XMALLOC(sizeof(DhKey), ssl->heap,
  6430. DYNAMIC_TYPE_DH);
  6431. if (keyShareEntry->key == NULL)
  6432. return MEMORY_E;
  6433. /* Setup Key */
  6434. ret = wc_InitDhKey_ex((DhKey*)keyShareEntry->key, ssl->heap, ssl->devId);
  6435. if (ret == 0) {
  6436. dhKey = (DhKey*)keyShareEntry->key;
  6437. /* Set key */
  6438. #ifdef HAVE_PUBLIC_FFDHE
  6439. ret = wc_DhSetKey(dhKey, params->p, params->p_len, params->g,
  6440. params->g_len);
  6441. #else
  6442. ret = wc_DhSetNamedKey(dhKey, keyShareEntry->group);
  6443. #endif
  6444. }
  6445. }
  6446. if (ret == 0
  6447. #ifdef WOLFSSL_ASYNC_CRYPT
  6448. && keyShareEntry->lastRet == 0 /* don't enter here if WC_PENDING_E */
  6449. #endif
  6450. ) {
  6451. #ifdef WOLFSSL_DEBUG_TLS
  6452. WOLFSSL_MSG("Peer DH Key");
  6453. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6454. #endif
  6455. ssl->options.dhKeySz = (word16)pSz;
  6456. /* Derive secret from private key and peer's public key. */
  6457. ret = DhAgree(ssl, dhKey,
  6458. (const byte*)keyShareEntry->privKey, keyShareEntry->keyLen, /* our private */
  6459. keyShareEntry->ke, keyShareEntry->keLen, /* peer's public key */
  6460. ssl->arrays->preMasterSecret, &ssl->arrays->preMasterSz, /* secret */
  6461. NULL, 0
  6462. );
  6463. #ifdef WOLFSSL_ASYNC_CRYPT
  6464. if (ret == WC_PENDING_E) {
  6465. return ret;
  6466. }
  6467. #endif
  6468. }
  6469. /* RFC 8446 Section 7.4.1:
  6470. * ... left-padded with zeros up to the size of the prime. ...
  6471. */
  6472. if (ret == 0 && (word32)ssl->options.dhKeySz > ssl->arrays->preMasterSz) {
  6473. word32 diff = (word32)ssl->options.dhKeySz - ssl->arrays->preMasterSz;
  6474. XMEMMOVE(ssl->arrays->preMasterSecret + diff,
  6475. ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz);
  6476. XMEMSET(ssl->arrays->preMasterSecret, 0, diff);
  6477. ssl->arrays->preMasterSz = ssl->options.dhKeySz;
  6478. }
  6479. /* done with key share, release resources */
  6480. if (dhKey)
  6481. wc_FreeDhKey(dhKey);
  6482. if (keyShareEntry->key) {
  6483. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_DH);
  6484. keyShareEntry->key = NULL;
  6485. }
  6486. if (keyShareEntry->privKey != NULL) {
  6487. XFREE(keyShareEntry->privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6488. keyShareEntry->privKey = NULL;
  6489. }
  6490. if (keyShareEntry->pubKey != NULL) {
  6491. XFREE(keyShareEntry->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6492. keyShareEntry->pubKey = NULL;
  6493. }
  6494. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6495. keyShareEntry->ke = NULL;
  6496. #else
  6497. (void)ssl;
  6498. (void)keyShareEntry;
  6499. ret = PEER_KEY_ERROR;
  6500. WOLFSSL_ERROR_VERBOSE(ret);
  6501. #endif
  6502. return ret;
  6503. }
  6504. /* Process the X25519 key share extension on the client side.
  6505. *
  6506. * ssl The SSL/TLS object.
  6507. * keyShareEntry The key share entry object to use to calculate shared secret.
  6508. * returns 0 on success and other values indicate failure.
  6509. */
  6510. static int TLSX_KeyShare_ProcessX25519(WOLFSSL* ssl,
  6511. KeyShareEntry* keyShareEntry)
  6512. {
  6513. int ret;
  6514. #ifdef HAVE_CURVE25519
  6515. curve25519_key* key = (curve25519_key*)keyShareEntry->key;
  6516. curve25519_key* peerX25519Key;
  6517. #ifdef HAVE_ECC
  6518. if (ssl->peerEccKey != NULL) {
  6519. wc_ecc_free(ssl->peerEccKey);
  6520. ssl->peerEccKey = NULL;
  6521. ssl->peerEccKeyPresent = 0;
  6522. }
  6523. #endif
  6524. peerX25519Key = (curve25519_key*)XMALLOC(sizeof(curve25519_key), ssl->heap,
  6525. DYNAMIC_TYPE_TLSX);
  6526. if (peerX25519Key == NULL) {
  6527. WOLFSSL_MSG("PeerEccKey Memory error");
  6528. return MEMORY_ERROR;
  6529. }
  6530. ret = wc_curve25519_init(peerX25519Key);
  6531. if (ret != 0) {
  6532. XFREE(peerX25519Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6533. return ret;
  6534. }
  6535. #ifdef WOLFSSL_DEBUG_TLS
  6536. WOLFSSL_MSG("Peer Curve25519 Key");
  6537. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6538. #endif
  6539. if (wc_curve25519_check_public(keyShareEntry->ke, keyShareEntry->keLen,
  6540. EC25519_LITTLE_ENDIAN) != 0) {
  6541. ret = ECC_PEERKEY_ERROR;
  6542. WOLFSSL_ERROR_VERBOSE(ret);
  6543. }
  6544. if (ret == 0) {
  6545. if (wc_curve25519_import_public_ex(keyShareEntry->ke,
  6546. keyShareEntry->keLen, peerX25519Key,
  6547. EC25519_LITTLE_ENDIAN) != 0) {
  6548. ret = ECC_PEERKEY_ERROR;
  6549. WOLFSSL_ERROR_VERBOSE(ret);
  6550. }
  6551. }
  6552. if (ret == 0) {
  6553. ssl->ecdhCurveOID = ECC_X25519_OID;
  6554. ret = wc_curve25519_shared_secret_ex(key, peerX25519Key,
  6555. ssl->arrays->preMasterSecret,
  6556. &ssl->arrays->preMasterSz,
  6557. EC25519_LITTLE_ENDIAN);
  6558. }
  6559. wc_curve25519_free(peerX25519Key);
  6560. XFREE(peerX25519Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6561. wc_curve25519_free((curve25519_key*)keyShareEntry->key);
  6562. if (keyShareEntry->key != NULL) {
  6563. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6564. keyShareEntry->key = NULL;
  6565. }
  6566. #else
  6567. (void)ssl;
  6568. (void)keyShareEntry;
  6569. ret = PEER_KEY_ERROR;
  6570. WOLFSSL_ERROR_VERBOSE(ret);
  6571. #endif /* HAVE_CURVE25519 */
  6572. return ret;
  6573. }
  6574. /* Process the X448 key share extension on the client side.
  6575. *
  6576. * ssl The SSL/TLS object.
  6577. * keyShareEntry The key share entry object to use to calculate shared secret.
  6578. * returns 0 on success and other values indicate failure.
  6579. */
  6580. static int TLSX_KeyShare_ProcessX448(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6581. {
  6582. int ret;
  6583. #ifdef HAVE_CURVE448
  6584. curve448_key* key = (curve448_key*)keyShareEntry->key;
  6585. curve448_key* peerX448Key;
  6586. #ifdef HAVE_ECC
  6587. if (ssl->peerEccKey != NULL) {
  6588. wc_ecc_free(ssl->peerEccKey);
  6589. ssl->peerEccKey = NULL;
  6590. ssl->peerEccKeyPresent = 0;
  6591. }
  6592. #endif
  6593. peerX448Key = (curve448_key*)XMALLOC(sizeof(curve448_key), ssl->heap,
  6594. DYNAMIC_TYPE_TLSX);
  6595. if (peerX448Key == NULL) {
  6596. WOLFSSL_MSG("PeerEccKey Memory error");
  6597. return MEMORY_ERROR;
  6598. }
  6599. ret = wc_curve448_init(peerX448Key);
  6600. if (ret != 0) {
  6601. XFREE(peerX448Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6602. return ret;
  6603. }
  6604. #ifdef WOLFSSL_DEBUG_TLS
  6605. WOLFSSL_MSG("Peer Curve448 Key");
  6606. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6607. #endif
  6608. if (wc_curve448_check_public(keyShareEntry->ke, keyShareEntry->keLen,
  6609. EC448_LITTLE_ENDIAN) != 0) {
  6610. ret = ECC_PEERKEY_ERROR;
  6611. WOLFSSL_ERROR_VERBOSE(ret);
  6612. }
  6613. if (ret == 0) {
  6614. if (wc_curve448_import_public_ex(keyShareEntry->ke,
  6615. keyShareEntry->keLen, peerX448Key,
  6616. EC448_LITTLE_ENDIAN) != 0) {
  6617. ret = ECC_PEERKEY_ERROR;
  6618. WOLFSSL_ERROR_VERBOSE(ret);
  6619. }
  6620. }
  6621. if (ret == 0) {
  6622. ssl->ecdhCurveOID = ECC_X448_OID;
  6623. ret = wc_curve448_shared_secret_ex(key, peerX448Key,
  6624. ssl->arrays->preMasterSecret,
  6625. &ssl->arrays->preMasterSz,
  6626. EC448_LITTLE_ENDIAN);
  6627. }
  6628. wc_curve448_free(peerX448Key);
  6629. XFREE(peerX448Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6630. wc_curve448_free((curve448_key*)keyShareEntry->key);
  6631. if (keyShareEntry->key != NULL) {
  6632. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6633. keyShareEntry->key = NULL;
  6634. }
  6635. #else
  6636. (void)ssl;
  6637. (void)keyShareEntry;
  6638. ret = PEER_KEY_ERROR;
  6639. WOLFSSL_ERROR_VERBOSE(ret);
  6640. #endif /* HAVE_CURVE448 */
  6641. return ret;
  6642. }
  6643. /* Process the ECC key share extension on the client side.
  6644. *
  6645. * ssl The SSL/TLS object.
  6646. * keyShareEntry The key share entry object to use to calculate shared secret.
  6647. * returns 0 on success and other values indicate failure.
  6648. */
  6649. static int TLSX_KeyShare_ProcessEcc(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6650. {
  6651. int ret = 0;
  6652. #ifdef HAVE_ECC
  6653. int curveId = ECC_CURVE_INVALID;
  6654. ecc_key* eccKey = (ecc_key*)keyShareEntry->key;
  6655. /* find supported curve */
  6656. switch (keyShareEntry->group) {
  6657. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  6658. #ifndef NO_ECC_SECP
  6659. case WOLFSSL_ECC_SECP256R1:
  6660. curveId = ECC_SECP256R1;
  6661. break;
  6662. #endif /* !NO_ECC_SECP */
  6663. #endif
  6664. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  6665. #ifndef NO_ECC_SECP
  6666. case WOLFSSL_ECC_SECP384R1:
  6667. curveId = ECC_SECP384R1;
  6668. break;
  6669. #endif /* !NO_ECC_SECP */
  6670. #endif
  6671. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  6672. #ifndef NO_ECC_SECP
  6673. case WOLFSSL_ECC_SECP521R1:
  6674. curveId = ECC_SECP521R1;
  6675. break;
  6676. #endif /* !NO_ECC_SECP */
  6677. #endif
  6678. #if defined(HAVE_X448) && ECC_MIN_KEY_SZ <= 448
  6679. case WOLFSSL_ECC_X448:
  6680. curveId = ECC_X448;
  6681. break;
  6682. #endif
  6683. default:
  6684. /* unsupported curve */
  6685. WOLFSSL_ERROR_VERBOSE(ECC_PEERKEY_ERROR);
  6686. return ECC_PEERKEY_ERROR;
  6687. }
  6688. #ifdef WOLFSSL_ASYNC_CRYPT
  6689. if (keyShareEntry->lastRet == 0) /* don't enter here if WC_PENDING_E */
  6690. #endif
  6691. {
  6692. #ifdef WOLFSSL_DEBUG_TLS
  6693. WOLFSSL_MSG("Peer ECC Key");
  6694. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6695. #endif
  6696. if (ssl->peerEccKey != NULL) {
  6697. wc_ecc_free(ssl->peerEccKey);
  6698. XFREE(ssl->peerEccKey, ssl->heap, DYNAMIC_TYPE_ECC);
  6699. ssl->peerEccKeyPresent = 0;
  6700. }
  6701. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  6702. ret = tsip_Tls13GenSharedSecret(ssl, keyShareEntry);
  6703. if (ret != CRYPTOCB_UNAVAILABLE) {
  6704. return ret;
  6705. }
  6706. ret = 0;
  6707. #endif
  6708. ssl->peerEccKey = (ecc_key*)XMALLOC(sizeof(ecc_key), ssl->heap,
  6709. DYNAMIC_TYPE_ECC);
  6710. if (ssl->peerEccKey == NULL) {
  6711. WOLFSSL_MSG("PeerEccKey Memory error");
  6712. ret = MEMORY_ERROR;
  6713. }
  6714. if (ret == 0) {
  6715. ret = wc_ecc_init_ex(ssl->peerEccKey, ssl->heap, ssl->devId);
  6716. }
  6717. /* Point is validated by import function. */
  6718. if (ret == 0) {
  6719. ret = wc_ecc_import_x963_ex(keyShareEntry->ke, keyShareEntry->keLen,
  6720. ssl->peerEccKey, curveId);
  6721. if (ret != 0) {
  6722. ret = ECC_PEERKEY_ERROR;
  6723. WOLFSSL_ERROR_VERBOSE(ret);
  6724. }
  6725. }
  6726. if (ret == 0) {
  6727. ssl->ecdhCurveOID = ssl->peerEccKey->dp->oidSum;
  6728. ssl->peerEccKeyPresent = 1;
  6729. }
  6730. }
  6731. if (ret == 0 && eccKey == NULL)
  6732. ret = BAD_FUNC_ARG;
  6733. if (ret == 0) {
  6734. ret = EccSharedSecret(ssl, eccKey, ssl->peerEccKey,
  6735. keyShareEntry->ke, &keyShareEntry->keLen,
  6736. ssl->arrays->preMasterSecret, &ssl->arrays->preMasterSz,
  6737. ssl->options.side
  6738. );
  6739. #ifdef WOLFSSL_ASYNC_CRYPT
  6740. if (ret == WC_PENDING_E)
  6741. return ret;
  6742. #endif
  6743. }
  6744. /* done with key share, release resources */
  6745. if (ssl->peerEccKey != NULL
  6746. #ifdef HAVE_PK_CALLBACKS
  6747. && ssl->ctx->EccSharedSecretCb == NULL
  6748. #endif
  6749. ) {
  6750. wc_ecc_free(ssl->peerEccKey);
  6751. XFREE(ssl->peerEccKey, ssl->heap, DYNAMIC_TYPE_ECC);
  6752. ssl->peerEccKey = NULL;
  6753. ssl->peerEccKeyPresent = 0;
  6754. }
  6755. if (keyShareEntry->key) {
  6756. wc_ecc_free((ecc_key*)keyShareEntry->key);
  6757. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_ECC);
  6758. keyShareEntry->key = NULL;
  6759. }
  6760. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6761. keyShareEntry->ke = NULL;
  6762. #else
  6763. (void)ssl;
  6764. (void)keyShareEntry;
  6765. ret = PEER_KEY_ERROR;
  6766. WOLFSSL_ERROR_VERBOSE(ret);
  6767. #endif /* HAVE_ECC */
  6768. return ret;
  6769. }
  6770. #ifdef HAVE_PQC
  6771. /* Process the Kyber key share extension on the client side.
  6772. *
  6773. * ssl The SSL/TLS object.
  6774. * keyShareEntry The key share entry object to use to calculate shared secret.
  6775. * returns 0 on success and other values indicate failure.
  6776. */
  6777. static int TLSX_KeyShare_ProcessPqc(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6778. {
  6779. int ret = 0;
  6780. int type;
  6781. KyberKey kem[1];
  6782. byte* sharedSecret = NULL;
  6783. word32 sharedSecretLen = 0;
  6784. int oqs_group = 0;
  6785. int ecc_group = 0;
  6786. ecc_key eccpubkey;
  6787. word32 outlen = 0;
  6788. word32 privSz = 0;
  6789. word32 ctSz = 0;
  6790. word32 ssSz = 0;
  6791. if (keyShareEntry->ke == NULL) {
  6792. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  6793. return BAD_FUNC_ARG;
  6794. }
  6795. if (ssl->options.side == WOLFSSL_SERVER_END) {
  6796. /* I am the server, the shared secret has already been generated and
  6797. * is in keyShareEntry->ke; copy it to the pre-master secret
  6798. * pre-allocated buffer. */
  6799. if (keyShareEntry->keLen > ENCRYPT_LEN) {
  6800. WOLFSSL_MSG("shared secret is too long.");
  6801. return LENGTH_ERROR;
  6802. }
  6803. XMEMCPY(ssl->arrays->preMasterSecret, keyShareEntry->ke,
  6804. keyShareEntry->keLen);
  6805. ssl->arrays->preMasterSz = keyShareEntry->keLen;
  6806. XFREE(keyShareEntry->ke, sl->heap, DYNAMIC_TYPE_SECRET)
  6807. keyShareEntry->ke = NULL;
  6808. keyShareEntry->keLen = 0;
  6809. return 0;
  6810. }
  6811. /* I am the client, the ciphertext is in keyShareEntry->ke */
  6812. findEccPqc(&ecc_group, &oqs_group, keyShareEntry->group);
  6813. ret = kyber_id2type(oqs_group, &type);
  6814. if (ret != 0) {
  6815. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  6816. ret = BAD_FUNC_ARG;
  6817. }
  6818. if (ret == 0) {
  6819. ret = wc_KyberKey_Init(type, kem, ssl->heap, INVALID_DEVID);
  6820. if (ret != 0) {
  6821. WOLFSSL_MSG("Error creating Kyber KEM");
  6822. }
  6823. }
  6824. if (ret == 0) {
  6825. ret = wc_KyberKey_SharedSecretSize(kem, &ssSz);
  6826. }
  6827. if (ret == 0) {
  6828. sharedSecretLen = ssSz;
  6829. switch (ecc_group) {
  6830. case WOLFSSL_ECC_SECP256R1:
  6831. sharedSecretLen += 32;
  6832. outlen = 32;
  6833. break;
  6834. case WOLFSSL_ECC_SECP384R1:
  6835. sharedSecretLen += 48;
  6836. outlen = 48;
  6837. break;
  6838. case WOLFSSL_ECC_SECP521R1:
  6839. sharedSecretLen += 66;
  6840. outlen = 66;
  6841. break;
  6842. default:
  6843. break;
  6844. }
  6845. ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
  6846. if (ret != 0) {
  6847. WOLFSSL_MSG("Memory allocation error.");
  6848. ret = MEMORY_E;
  6849. }
  6850. }
  6851. if (ret == 0) {
  6852. sharedSecret = (byte*)XMALLOC(sharedSecretLen, ssl->heap,
  6853. DYNAMIC_TYPE_TLSX);
  6854. if (sharedSecret == NULL) {
  6855. WOLFSSL_MSG("Memory allocation error.");
  6856. ret = MEMORY_E;
  6857. }
  6858. }
  6859. if (ret == 0) {
  6860. ret = wc_KyberKey_CipherTextSize(kem, &ctSz);
  6861. }
  6862. if (ret == 0) {
  6863. ret = wc_KyberKey_PrivateKeySize(kem, &privSz);
  6864. }
  6865. if (ret == 0) {
  6866. ret = wc_KyberKey_DecodePrivateKey(kem, keyShareEntry->privKey, privSz);
  6867. }
  6868. if (ret == 0) {
  6869. ret = wc_KyberKey_Decapsulate(kem, sharedSecret + outlen,
  6870. keyShareEntry->ke + keyShareEntry->keLen - ctSz, ctSz);
  6871. if (ret != 0) {
  6872. WOLFSSL_MSG("wc_KyberKey decapsulation failure.");
  6873. ret = BAD_FUNC_ARG;
  6874. }
  6875. }
  6876. if (ecc_group != 0) {
  6877. if (ret == 0) {
  6878. /* Point is validated by import function. */
  6879. ret = wc_ecc_import_x963(keyShareEntry->ke,
  6880. keyShareEntry->keLen - ctSz,
  6881. &eccpubkey);
  6882. if (ret != 0) {
  6883. WOLFSSL_MSG("ECC Public key import error.");
  6884. }
  6885. }
  6886. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  6887. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  6888. !defined(HAVE_SELFTEST)
  6889. if (ret == 0) {
  6890. ret = wc_ecc_set_rng(keyShareEntry->key, ssl->rng);
  6891. if (ret != 0) {
  6892. WOLFSSL_MSG("Failure to set the ECC private key RNG.");
  6893. }
  6894. }
  6895. #endif
  6896. if (ret == 0) {
  6897. PRIVATE_KEY_UNLOCK();
  6898. ret = wc_ecc_shared_secret(keyShareEntry->key, &eccpubkey,
  6899. sharedSecret, &outlen);
  6900. PRIVATE_KEY_LOCK();
  6901. if (outlen != sharedSecretLen - ssSz) {
  6902. WOLFSSL_MSG("ECC shared secret derivation error.");
  6903. ret = BAD_FUNC_ARG;
  6904. }
  6905. }
  6906. }
  6907. if ((ret == 0) && (sharedSecretLen > ENCRYPT_LEN)) {
  6908. WOLFSSL_MSG("shared secret is too long.");
  6909. ret = LENGTH_ERROR;
  6910. }
  6911. if (ret == 0) {
  6912. /* Copy the shared secret to the pre-master secret pre-allocated
  6913. * buffer. */
  6914. XMEMCPY(ssl->arrays->preMasterSecret, sharedSecret, sharedSecretLen);
  6915. ssl->arrays->preMasterSz = (word32) sharedSecretLen;
  6916. }
  6917. if (sharedSecret != NULL) {
  6918. XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_SECRET);
  6919. }
  6920. wc_ecc_free(&eccpubkey);
  6921. wc_KyberKey_Free(kem);
  6922. return ret;
  6923. }
  6924. #endif /* HAVE_PQC */
  6925. /* Process the key share extension on the client side.
  6926. *
  6927. * ssl The SSL/TLS object.
  6928. * keyShareEntry The key share entry object to use to calculate shared secret.
  6929. * returns 0 on success and other values indicate failure.
  6930. */
  6931. static int TLSX_KeyShare_Process(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6932. {
  6933. int ret;
  6934. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  6935. ssl->session->namedGroup = keyShareEntry->group;
  6936. #endif
  6937. /* reset the pre master secret size */
  6938. if (ssl->arrays->preMasterSz == 0)
  6939. ssl->arrays->preMasterSz = ENCRYPT_LEN;
  6940. /* Use Key Share Data from server. */
  6941. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(keyShareEntry->group))
  6942. ret = TLSX_KeyShare_ProcessDh(ssl, keyShareEntry);
  6943. else if (keyShareEntry->group == WOLFSSL_ECC_X25519)
  6944. ret = TLSX_KeyShare_ProcessX25519(ssl, keyShareEntry);
  6945. else if (keyShareEntry->group == WOLFSSL_ECC_X448)
  6946. ret = TLSX_KeyShare_ProcessX448(ssl, keyShareEntry);
  6947. #ifdef HAVE_PQC
  6948. else if (WOLFSSL_NAMED_GROUP_IS_PQC(keyShareEntry->group))
  6949. ret = TLSX_KeyShare_ProcessPqc(ssl, keyShareEntry);
  6950. #endif
  6951. else
  6952. ret = TLSX_KeyShare_ProcessEcc(ssl, keyShareEntry);
  6953. #ifdef WOLFSSL_DEBUG_TLS
  6954. if (ret == 0) {
  6955. WOLFSSL_MSG("KE Secret");
  6956. WOLFSSL_BUFFER(ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz);
  6957. }
  6958. #endif
  6959. #ifdef WOLFSSL_ASYNC_CRYPT
  6960. keyShareEntry->lastRet = ret;
  6961. #endif
  6962. return ret;
  6963. }
  6964. /* Parse an entry of the KeyShare extension.
  6965. *
  6966. * ssl The SSL/TLS object.
  6967. * input The extension data.
  6968. * length The length of the extension data.
  6969. * kse The new key share entry object.
  6970. * returns a positive number to indicate amount of data parsed and a negative
  6971. * number on error.
  6972. */
  6973. static int TLSX_KeyShareEntry_Parse(const WOLFSSL* ssl, const byte* input,
  6974. word16 length, KeyShareEntry **kse, TLSX** extensions)
  6975. {
  6976. int ret;
  6977. word16 group;
  6978. word16 keLen;
  6979. int offset = 0;
  6980. byte* ke;
  6981. if (length < OPAQUE16_LEN + OPAQUE16_LEN)
  6982. return BUFFER_ERROR;
  6983. /* Named group */
  6984. ato16(&input[offset], &group);
  6985. offset += OPAQUE16_LEN;
  6986. /* Key exchange data - public key. */
  6987. ato16(&input[offset], &keLen);
  6988. offset += OPAQUE16_LEN;
  6989. if (keLen == 0)
  6990. return INVALID_PARAMETER;
  6991. if (keLen > length - offset)
  6992. return BUFFER_ERROR;
  6993. #ifdef HAVE_PQC
  6994. if (WOLFSSL_NAMED_GROUP_IS_PQC(group) &&
  6995. ssl->options.side == WOLFSSL_SERVER_END) {
  6996. /* For KEMs, the public key is not stored. Casting away const because
  6997. * we know for KEMs, it will be read-only.*/
  6998. ke = (byte *)&input[offset];
  6999. } else
  7000. #endif
  7001. {
  7002. /* Store a copy in the key share object. */
  7003. ke = (byte*)XMALLOC(keLen, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7004. if (ke == NULL)
  7005. return MEMORY_E;
  7006. XMEMCPY(ke, &input[offset], keLen);
  7007. }
  7008. /* Populate a key share object in the extension. */
  7009. ret = TLSX_KeyShare_Use(ssl, group, keLen, ke, kse, extensions);
  7010. if (ret != 0) {
  7011. if (ke != &input[offset]) {
  7012. XFREE(ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7013. }
  7014. return ret;
  7015. }
  7016. /* Total length of the parsed data. */
  7017. return offset + keLen;
  7018. }
  7019. /* Searches the groups sent for the specified named group.
  7020. *
  7021. * ssl SSL/TLS object.
  7022. * name Group name to match.
  7023. * returns 1 when the extension has the group name and 0 otherwise.
  7024. */
  7025. static int TLSX_KeyShare_Find(WOLFSSL* ssl, word16 group)
  7026. {
  7027. TLSX* extension;
  7028. KeyShareEntry* list;
  7029. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7030. if (extension == NULL) {
  7031. extension = TLSX_Find(ssl->ctx->extensions, TLSX_KEY_SHARE);
  7032. if (extension == NULL)
  7033. return 0;
  7034. }
  7035. list = (KeyShareEntry*)extension->data;
  7036. while (list != NULL) {
  7037. if (list->group == group)
  7038. return 1;
  7039. list = list->next;
  7040. }
  7041. return 0;
  7042. }
  7043. /* Searches the supported groups extension for the specified named group.
  7044. *
  7045. * ssl The SSL/TLS object.
  7046. * name The group name to match.
  7047. * returns 1 when the extension has the group name and 0 otherwise.
  7048. */
  7049. static int TLSX_SupportedGroups_Find(const WOLFSSL* ssl, word16 name,
  7050. TLSX* extensions)
  7051. {
  7052. #ifdef HAVE_SUPPORTED_CURVES
  7053. TLSX* extension;
  7054. SupportedCurve* curve = NULL;
  7055. if ((extension = TLSX_Find(extensions,
  7056. TLSX_SUPPORTED_GROUPS)) == NULL) {
  7057. if ((extension = TLSX_Find(ssl->ctx->extensions,
  7058. TLSX_SUPPORTED_GROUPS)) == NULL) {
  7059. return 0;
  7060. }
  7061. }
  7062. for (curve = (SupportedCurve*)extension->data; curve; curve = curve->next) {
  7063. if (curve->name == name)
  7064. return 1;
  7065. }
  7066. #endif
  7067. (void)ssl;
  7068. (void)name;
  7069. return 0;
  7070. }
  7071. int TLSX_KeyShare_Parse_ClientHello(const WOLFSSL* ssl,
  7072. const byte* input, word16 length, TLSX** extensions)
  7073. {
  7074. int ret;
  7075. int offset = 0;
  7076. word16 len;
  7077. TLSX* extension;
  7078. /* Add a KeyShare extension if it doesn't exist even if peer sent no
  7079. * entries. The presence of this extension signals that the peer can be
  7080. * negotiated with. */
  7081. extension = TLSX_Find(*extensions, TLSX_KEY_SHARE);
  7082. if (extension == NULL) {
  7083. /* Push new KeyShare extension. */
  7084. ret = TLSX_Push(extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  7085. if (ret != 0)
  7086. return ret;
  7087. }
  7088. if (length < OPAQUE16_LEN)
  7089. return BUFFER_ERROR;
  7090. /* ClientHello contains zero or more key share entries. */
  7091. ato16(input, &len);
  7092. if (len != length - OPAQUE16_LEN)
  7093. return BUFFER_ERROR;
  7094. offset += OPAQUE16_LEN;
  7095. while (offset < (int)length) {
  7096. ret = TLSX_KeyShareEntry_Parse(ssl, &input[offset],
  7097. length - (word16)offset, NULL, extensions);
  7098. if (ret < 0)
  7099. return ret;
  7100. offset += ret;
  7101. }
  7102. return 0;
  7103. }
  7104. /* Parse the KeyShare extension.
  7105. * Different formats in different messages.
  7106. *
  7107. * ssl The SSL/TLS object.
  7108. * input The extension data.
  7109. * length The length of the extension data.
  7110. * msgType The type of the message this extension is being parsed from.
  7111. * returns 0 on success and other values indicate failure.
  7112. */
  7113. int TLSX_KeyShare_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  7114. byte msgType)
  7115. {
  7116. int ret;
  7117. KeyShareEntry *keyShareEntry = NULL;
  7118. word16 group;
  7119. if (msgType == client_hello) {
  7120. ret = TLSX_KeyShare_Parse_ClientHello(ssl, input, length,
  7121. &ssl->extensions);
  7122. }
  7123. else if (msgType == server_hello) {
  7124. int len;
  7125. if (length < OPAQUE16_LEN)
  7126. return BUFFER_ERROR;
  7127. /* The data is the named group the server wants to use. */
  7128. ato16(input, &group);
  7129. /* Check the selected group was supported by ClientHello extensions. */
  7130. if (!TLSX_SupportedGroups_Find(ssl, group, ssl->extensions)) {
  7131. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7132. return BAD_KEY_SHARE_DATA;
  7133. }
  7134. /* Check if the group was sent. */
  7135. if (!TLSX_KeyShare_Find(ssl, group)) {
  7136. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7137. return BAD_KEY_SHARE_DATA;
  7138. }
  7139. /* ServerHello contains one key share entry. */
  7140. len = TLSX_KeyShareEntry_Parse(ssl, input, length, &keyShareEntry,
  7141. &ssl->extensions);
  7142. if (len != (int)length)
  7143. return BUFFER_ERROR;
  7144. /* Not in list sent if there isn't a private key. */
  7145. if (keyShareEntry == NULL || (keyShareEntry->key == NULL
  7146. #if !defined(NO_DH) || defined(HAVE_PQC)
  7147. && keyShareEntry->privKey == NULL
  7148. #endif
  7149. )) {
  7150. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7151. return BAD_KEY_SHARE_DATA;
  7152. }
  7153. /* Process the entry to calculate the secret. */
  7154. ret = TLSX_KeyShare_Process(ssl, keyShareEntry);
  7155. if (ret == 0)
  7156. ssl->session->namedGroup = ssl->namedGroup = group;
  7157. }
  7158. else if (msgType == hello_retry_request) {
  7159. if (length != OPAQUE16_LEN)
  7160. return BUFFER_ERROR;
  7161. /* The data is the named group the server wants to use. */
  7162. ato16(input, &group);
  7163. #ifdef WOLFSSL_ASYNC_CRYPT
  7164. /* only perform find and clear TLSX if not returning from async */
  7165. if (ssl->error != WC_PENDING_E)
  7166. #endif
  7167. {
  7168. /* Check the selected group was supported by ClientHello extensions. */
  7169. if (!TLSX_SupportedGroups_Find(ssl, group, ssl->extensions)) {
  7170. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7171. return BAD_KEY_SHARE_DATA;
  7172. }
  7173. /* Check if the group was sent. */
  7174. if (TLSX_KeyShare_Find(ssl, group)) {
  7175. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7176. return BAD_KEY_SHARE_DATA;
  7177. }
  7178. /* Clear out unusable key shares. */
  7179. ret = TLSX_KeyShare_Empty(ssl);
  7180. if (ret != 0)
  7181. return ret;
  7182. }
  7183. #ifdef HAVE_PQC
  7184. /* For post-quantum groups, do this in TLSX_PopulateExtensions(). */
  7185. if (!WOLFSSL_NAMED_GROUP_IS_PQC(group))
  7186. #endif
  7187. ret = TLSX_KeyShare_Use(ssl, group, 0, NULL, NULL, &ssl->extensions);
  7188. if (ret == 0)
  7189. ssl->session->namedGroup = ssl->namedGroup = group;
  7190. }
  7191. else {
  7192. /* Not a message type that is allowed to have this extension. */
  7193. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  7194. return SANITY_MSG_E;
  7195. }
  7196. return ret;
  7197. }
  7198. /* Create a new key share entry and put it into the list.
  7199. *
  7200. * list The linked list of key share entries.
  7201. * group The named group.
  7202. * heap The memory to allocate with.
  7203. * keyShareEntry The new key share entry object.
  7204. * returns 0 on success and other values indicate failure.
  7205. */
  7206. static int TLSX_KeyShare_New(KeyShareEntry** list, int group, void *heap,
  7207. KeyShareEntry** keyShareEntry)
  7208. {
  7209. KeyShareEntry* kse;
  7210. KeyShareEntry** next;
  7211. kse = (KeyShareEntry*)XMALLOC(sizeof(KeyShareEntry), heap,
  7212. DYNAMIC_TYPE_TLSX);
  7213. if (kse == NULL)
  7214. return MEMORY_E;
  7215. XMEMSET(kse, 0, sizeof(*kse));
  7216. kse->group = (word16)group;
  7217. /* Add it to the back and maintain the links. */
  7218. while (*list != NULL) {
  7219. /* Assign to temporary to work around compiler bug found by customer. */
  7220. next = &((*list)->next);
  7221. list = next;
  7222. }
  7223. *list = kse;
  7224. *keyShareEntry = kse;
  7225. (void)heap;
  7226. return 0;
  7227. }
  7228. #ifdef HAVE_PQC
  7229. static int server_generate_pqc_ciphertext(WOLFSSL* ssl,
  7230. KeyShareEntry* keyShareEntry, byte* data, word16 len)
  7231. {
  7232. /* I am the server. The data parameter is the client's public key. I need
  7233. * to generate the public information (AKA ciphertext) and shared secret
  7234. * here. Note the "public information" is equivalent to a the public key in
  7235. * key exchange parlance. That's why it is being assigned to pubKey.
  7236. */
  7237. int type;
  7238. KyberKey kem[1];
  7239. byte* sharedSecret = NULL;
  7240. byte* ciphertext = NULL;
  7241. int ret = 0;
  7242. int oqs_group = 0;
  7243. int ecc_group = 0;
  7244. KeyShareEntry *ecc_kse = NULL;
  7245. ecc_key eccpubkey;
  7246. word32 outlen = 0;
  7247. word32 pubSz = 0;
  7248. word32 ctSz = 0;
  7249. word32 ssSz = 0;
  7250. findEccPqc(&ecc_group, &oqs_group, keyShareEntry->group);
  7251. ret = kyber_id2type(oqs_group, &type);
  7252. if (ret != 0) {
  7253. WOLFSSL_MSG("Invalid Kyber algorithm specified.");
  7254. ret = BAD_FUNC_ARG;
  7255. }
  7256. if (ret == 0) {
  7257. ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
  7258. if (ret != 0) {
  7259. WOLFSSL_MSG("Could not do ECC public key initialization.");
  7260. ret = MEMORY_E;
  7261. }
  7262. }
  7263. if (ret == 0) {
  7264. ecc_kse = (KeyShareEntry*)XMALLOC(sizeof(*ecc_kse), ssl->heap,
  7265. DYNAMIC_TYPE_TLSX);
  7266. if (ecc_kse == NULL) {
  7267. WOLFSSL_MSG("ecc_kse memory allocation failure");
  7268. ret = MEMORY_ERROR;
  7269. }
  7270. }
  7271. if (ret == 0) {
  7272. XMEMSET(ecc_kse, 0, sizeof(*ecc_kse));
  7273. }
  7274. if (ret == 0 && ecc_group != 0) {
  7275. ecc_kse->group = ecc_group;
  7276. ret = TLSX_KeyShare_GenEccKey(ssl, ecc_kse);
  7277. if (ret != 0) {
  7278. /* No message, TLSX_KeyShare_GenEccKey() will do it. */
  7279. return ret;
  7280. }
  7281. ret = 0;
  7282. }
  7283. if (ret == 0) {
  7284. ret = wc_KyberKey_Init(type, kem, ssl->heap, INVALID_DEVID);
  7285. if (ret != 0) {
  7286. WOLFSSL_MSG("Error creating Kyber KEM");
  7287. }
  7288. }
  7289. if (ret == 0) {
  7290. ret = wc_KyberKey_PublicKeySize(kem, &pubSz);
  7291. }
  7292. if (ret == 0) {
  7293. ret = wc_KyberKey_CipherTextSize(kem, &ctSz);
  7294. }
  7295. if (ret == 0) {
  7296. ret = wc_KyberKey_SharedSecretSize(kem, &ssSz);
  7297. }
  7298. if (ret == 0 && len != pubSz + ecc_kse->pubKeyLen) {
  7299. WOLFSSL_MSG("Invalid public key.");
  7300. ret = BAD_FUNC_ARG;
  7301. }
  7302. if (ret == 0) {
  7303. sharedSecret = (byte*)XMALLOC(ecc_kse->keyLen + ssSz, ssl->heap,
  7304. DYNAMIC_TYPE_TLSX);
  7305. ciphertext = (byte*)XMALLOC(ecc_kse->pubKeyLen + ctSz, ssl->heap,
  7306. DYNAMIC_TYPE_TLSX);
  7307. if (sharedSecret == NULL || ciphertext == NULL) {
  7308. WOLFSSL_MSG("Ciphertext/shared secret memory allocation failure.");
  7309. ret = MEMORY_E;
  7310. }
  7311. }
  7312. if (ecc_group != 0) {
  7313. if (ret == 0) {
  7314. /* Point is validated by import function. */
  7315. ret = wc_ecc_import_x963(data, len - pubSz, &eccpubkey);
  7316. if (ret != 0) {
  7317. WOLFSSL_MSG("Bad ECC public key.");
  7318. }
  7319. }
  7320. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  7321. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  7322. !defined(HAVE_SELFTEST)
  7323. if (ret == 0) {
  7324. ret = wc_ecc_set_rng(ecc_kse->key, ssl->rng);
  7325. }
  7326. #endif
  7327. if (ret == 0) {
  7328. outlen = ecc_kse->keyLen;
  7329. PRIVATE_KEY_UNLOCK();
  7330. ret = wc_ecc_shared_secret(ecc_kse->key, &eccpubkey,
  7331. sharedSecret,
  7332. &outlen);
  7333. PRIVATE_KEY_LOCK();
  7334. if (outlen != ecc_kse->keyLen) {
  7335. WOLFSSL_MSG("Data length mismatch.");
  7336. ret = BAD_FUNC_ARG;
  7337. }
  7338. }
  7339. }
  7340. if (ret == 0) {
  7341. ret = wc_KyberKey_DecodePublicKey(kem, data + ecc_kse->pubKeyLen,
  7342. pubSz);
  7343. }
  7344. if (ret == 0) {
  7345. ret = wc_KyberKey_Encapsulate(kem, ciphertext + ecc_kse->pubKeyLen,
  7346. sharedSecret + outlen, ssl->rng);
  7347. if (ret != 0) {
  7348. WOLFSSL_MSG("wc_KyberKey encapsulation failure.");
  7349. }
  7350. }
  7351. if (ret == 0) {
  7352. if (keyShareEntry->ke != NULL) {
  7353. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7354. }
  7355. keyShareEntry->ke = sharedSecret;
  7356. keyShareEntry->keLen = outlen + ssSz;
  7357. sharedSecret = NULL;
  7358. XMEMCPY(ciphertext, ecc_kse->pubKey, ecc_kse->pubKeyLen);
  7359. keyShareEntry->pubKey = ciphertext;
  7360. keyShareEntry->pubKeyLen = (word32)(ecc_kse->pubKeyLen + ctSz);
  7361. ciphertext = NULL;
  7362. }
  7363. TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
  7364. if (sharedSecret != NULL)
  7365. XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_TLSX);
  7366. if (ciphertext != NULL)
  7367. XFREE(ciphertext, ssl->heap, DYNAMIC_TYPE_TLSX);
  7368. wc_ecc_free(&eccpubkey);
  7369. wc_KyberKey_Free(kem);
  7370. return ret;
  7371. }
  7372. #endif /* HAVE_PQC */
  7373. /* Use the data to create a new key share object in the extensions.
  7374. *
  7375. * ssl The SSL/TLS object.
  7376. * group The named group.
  7377. * len The length of the public key data.
  7378. * data The public key data.
  7379. * kse The new key share entry object.
  7380. * returns 0 on success and other values indicate failure.
  7381. */
  7382. int TLSX_KeyShare_Use(const WOLFSSL* ssl, word16 group, word16 len, byte* data,
  7383. KeyShareEntry **kse, TLSX** extensions)
  7384. {
  7385. int ret = 0;
  7386. TLSX* extension;
  7387. KeyShareEntry* keyShareEntry = NULL;
  7388. /* Find the KeyShare extension if it exists. */
  7389. extension = TLSX_Find(*extensions, TLSX_KEY_SHARE);
  7390. if (extension == NULL) {
  7391. /* Push new KeyShare extension. */
  7392. ret = TLSX_Push(extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  7393. if (ret != 0)
  7394. return ret;
  7395. extension = TLSX_Find(*extensions, TLSX_KEY_SHARE);
  7396. if (extension == NULL)
  7397. return MEMORY_E;
  7398. }
  7399. extension->resp = 0;
  7400. /* Try to find the key share entry with this group. */
  7401. keyShareEntry = (KeyShareEntry*)extension->data;
  7402. while (keyShareEntry != NULL) {
  7403. if (keyShareEntry->group == group)
  7404. break;
  7405. keyShareEntry = keyShareEntry->next;
  7406. }
  7407. /* Create a new key share entry if not found. */
  7408. if (keyShareEntry == NULL) {
  7409. ret = TLSX_KeyShare_New((KeyShareEntry**)&extension->data, group,
  7410. ssl->heap, &keyShareEntry);
  7411. if (ret != 0)
  7412. return ret;
  7413. }
  7414. #ifdef HAVE_PQC
  7415. if (WOLFSSL_NAMED_GROUP_IS_PQC(group) &&
  7416. ssl->options.side == WOLFSSL_SERVER_END) {
  7417. ret = server_generate_pqc_ciphertext((WOLFSSL*)ssl, keyShareEntry, data,
  7418. len);
  7419. if (ret != 0)
  7420. return ret;
  7421. }
  7422. else
  7423. #endif
  7424. if (data != NULL) {
  7425. if (keyShareEntry->ke != NULL) {
  7426. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7427. }
  7428. keyShareEntry->ke = data;
  7429. keyShareEntry->keLen = len;
  7430. }
  7431. else {
  7432. /* Generate a key pair. Casting to non-const since changes inside are
  7433. * minimal but would require an extensive redesign to refactor. Also
  7434. * this path shouldn't be taken when parsing a ClientHello in stateless
  7435. * mode. */
  7436. ret = TLSX_KeyShare_GenKey((WOLFSSL*)ssl, keyShareEntry);
  7437. if (ret != 0)
  7438. return ret;
  7439. }
  7440. if (kse != NULL)
  7441. *kse = keyShareEntry;
  7442. return 0;
  7443. }
  7444. /* Set an empty Key Share extension.
  7445. *
  7446. * ssl The SSL/TLS object.
  7447. * returns 0 on success and other values indicate failure.
  7448. */
  7449. int TLSX_KeyShare_Empty(WOLFSSL* ssl)
  7450. {
  7451. int ret = 0;
  7452. TLSX* extension;
  7453. /* Find the KeyShare extension if it exists. */
  7454. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7455. if (extension == NULL) {
  7456. /* Push new KeyShare extension. */
  7457. ret = TLSX_Push(&ssl->extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  7458. }
  7459. else if (extension->data != NULL) {
  7460. TLSX_KeyShare_FreeAll((KeyShareEntry*)extension->data, ssl->heap);
  7461. extension->data = NULL;
  7462. }
  7463. return ret;
  7464. }
  7465. /* Returns whether this group is supported.
  7466. *
  7467. * namedGroup The named group to check.
  7468. * returns 1 when supported or 0 otherwise.
  7469. */
  7470. static int TLSX_KeyShare_IsSupported(int namedGroup)
  7471. {
  7472. switch (namedGroup) {
  7473. #ifdef HAVE_FFDHE_2048
  7474. case WOLFSSL_FFDHE_2048:
  7475. break;
  7476. #endif
  7477. #ifdef HAVE_FFDHE_3072
  7478. case WOLFSSL_FFDHE_3072:
  7479. break;
  7480. #endif
  7481. #ifdef HAVE_FFDHE_4096
  7482. case WOLFSSL_FFDHE_4096:
  7483. break;
  7484. #endif
  7485. #ifdef HAVE_FFDHE_6144
  7486. case WOLFSSL_FFDHE_6144:
  7487. break;
  7488. #endif
  7489. #ifdef HAVE_FFDHE_8192
  7490. case WOLFSSL_FFDHE_8192:
  7491. break;
  7492. #endif
  7493. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  7494. #ifdef HAVE_ECC_KOBLITZ
  7495. case WOLFSSL_ECC_SECP256K1:
  7496. break;
  7497. #endif
  7498. #ifndef NO_ECC_SECP
  7499. case WOLFSSL_ECC_SECP256R1:
  7500. break;
  7501. #endif /* !NO_ECC_SECP */
  7502. #ifdef HAVE_ECC_BRAINPOOL
  7503. case WOLFSSL_ECC_BRAINPOOLP256R1:
  7504. break;
  7505. #endif
  7506. #endif
  7507. #if defined(HAVE_CURVE25519) && ECC_MIN_KEY_SZ <= 256
  7508. case WOLFSSL_ECC_X25519:
  7509. break;
  7510. #endif
  7511. #if defined(HAVE_CURVE448) && ECC_MIN_KEY_SZ <= 448
  7512. case WOLFSSL_ECC_X448:
  7513. break;
  7514. #endif
  7515. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  7516. #ifndef NO_ECC_SECP
  7517. case WOLFSSL_ECC_SECP384R1:
  7518. break;
  7519. #endif /* !NO_ECC_SECP */
  7520. #ifdef HAVE_ECC_BRAINPOOL
  7521. case WOLFSSL_ECC_BRAINPOOLP384R1:
  7522. break;
  7523. #endif
  7524. #endif
  7525. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  7526. #ifndef NO_ECC_SECP
  7527. case WOLFSSL_ECC_SECP521R1:
  7528. break;
  7529. #endif /* !NO_ECC_SECP */
  7530. #endif
  7531. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  7532. #ifdef HAVE_ECC_KOBLITZ
  7533. case WOLFSSL_ECC_SECP160K1:
  7534. break;
  7535. #endif
  7536. #ifndef NO_ECC_SECP
  7537. case WOLFSSL_ECC_SECP160R1:
  7538. break;
  7539. #endif
  7540. #ifdef HAVE_ECC_SECPR2
  7541. case WOLFSSL_ECC_SECP160R2:
  7542. break;
  7543. #endif
  7544. #endif
  7545. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  7546. #ifdef HAVE_ECC_KOBLITZ
  7547. case WOLFSSL_ECC_SECP192K1:
  7548. break;
  7549. #endif
  7550. #ifndef NO_ECC_SECP
  7551. case WOLFSSL_ECC_SECP192R1:
  7552. break;
  7553. #endif
  7554. #endif
  7555. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  7556. #ifdef HAVE_ECC_KOBLITZ
  7557. case WOLFSSL_ECC_SECP224K1:
  7558. break;
  7559. #endif
  7560. #ifndef NO_ECC_SECP
  7561. case WOLFSSL_ECC_SECP224R1:
  7562. break;
  7563. #endif
  7564. #endif
  7565. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  7566. #ifdef HAVE_ECC_BRAINPOOL
  7567. case WOLFSSL_ECC_BRAINPOOLP512R1:
  7568. break;
  7569. #endif
  7570. #endif
  7571. #ifdef HAVE_PQC
  7572. #ifdef WOLFSSL_WC_KYBER
  7573. #ifdef WOLFSSL_KYBER512
  7574. case WOLFSSL_KYBER_LEVEL1:
  7575. #endif
  7576. #ifdef WOLFSSL_KYBER768
  7577. case WOLFSSL_KYBER_LEVEL3:
  7578. #endif
  7579. #ifdef WOLFSSL_KYBER1024
  7580. case WOLFSSL_KYBER_LEVEL5:
  7581. #endif
  7582. break;
  7583. #elif defined(HAVE_LIBOQS)
  7584. case WOLFSSL_KYBER_LEVEL1:
  7585. case WOLFSSL_KYBER_LEVEL3:
  7586. case WOLFSSL_KYBER_LEVEL5:
  7587. case WOLFSSL_P256_KYBER_LEVEL1:
  7588. case WOLFSSL_P384_KYBER_LEVEL3:
  7589. case WOLFSSL_P521_KYBER_LEVEL5:
  7590. {
  7591. int ret;
  7592. int id;
  7593. findEccPqc(NULL, &namedGroup, namedGroup);
  7594. ret = kyber_id2type(namedGroup, &id);
  7595. if (ret == NOT_COMPILED_IN) {
  7596. return 0;
  7597. }
  7598. if (! ext_kyber_enabled(id)) {
  7599. return 0;
  7600. }
  7601. break;
  7602. }
  7603. #elif defined(HAVE_PQM4)
  7604. case WOLFSSL_KYBER_LEVEL1:
  7605. break;
  7606. #endif
  7607. #endif /* HAVE_PQC */
  7608. default:
  7609. return 0;
  7610. }
  7611. return 1;
  7612. }
  7613. static const word16 preferredGroup[] = {
  7614. #if defined(HAVE_ECC) && (!defined(NO_ECC256) || \
  7615. defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP) && ECC_MIN_KEY_SZ <= 256
  7616. WOLFSSL_ECC_SECP256R1,
  7617. #endif
  7618. #if defined(HAVE_CURVE25519) && ECC_MIN_KEY_SZ <= 256
  7619. WOLFSSL_ECC_X25519,
  7620. #endif
  7621. #if defined(HAVE_CURVE448) && ECC_MIN_KEY_SZ <= 448
  7622. WOLFSSL_ECC_X448,
  7623. #endif
  7624. #if defined(HAVE_ECC) && (!defined(NO_ECC384) || \
  7625. defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP) && ECC_MIN_KEY_SZ <= 384
  7626. WOLFSSL_ECC_SECP384R1,
  7627. #endif
  7628. #if defined(HAVE_ECC) && (!defined(NO_ECC521) || \
  7629. defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP) && ECC_MIN_KEY_SZ <= 521
  7630. WOLFSSL_ECC_SECP521R1,
  7631. #endif
  7632. #if defined(HAVE_FFDHE_2048)
  7633. WOLFSSL_FFDHE_2048,
  7634. #endif
  7635. #if defined(HAVE_FFDHE_3072)
  7636. WOLFSSL_FFDHE_3072,
  7637. #endif
  7638. #if defined(HAVE_FFDHE_4096)
  7639. WOLFSSL_FFDHE_4096,
  7640. #endif
  7641. #if defined(HAVE_FFDHE_6144)
  7642. WOLFSSL_FFDHE_6144,
  7643. #endif
  7644. #if defined(HAVE_FFDHE_8192)
  7645. WOLFSSL_FFDHE_8192,
  7646. #endif
  7647. #ifdef WOLFSSL_WC_KYBER
  7648. #ifdef WOLFSSL_KYBER512
  7649. WOLFSSL_KYBER_LEVEL1,
  7650. #endif
  7651. #ifdef WOLFSSL_KYBER768
  7652. WOLFSSL_KYBER_LEVEL3,
  7653. #endif
  7654. #ifdef WOLFSSL_KYBER1024
  7655. WOLFSSL_KYBER_LEVEL5,
  7656. #endif
  7657. #elif defined(HAVE_LIBOQS)
  7658. /* These require a runtime call to TLSX_KeyShare_IsSupported to use */
  7659. WOLFSSL_KYBER_LEVEL1,
  7660. WOLFSSL_KYBER_LEVEL3,
  7661. WOLFSSL_KYBER_LEVEL5,
  7662. WOLFSSL_P256_KYBER_LEVEL1,
  7663. WOLFSSL_P384_KYBER_LEVEL3,
  7664. WOLFSSL_P521_KYBER_LEVEL5,
  7665. #elif defined(HAVE_PQM4)
  7666. WOLFSSL_KYBER_LEVEL1,
  7667. #endif
  7668. WOLFSSL_NAMED_GROUP_INVALID
  7669. };
  7670. #define PREFERRED_GROUP_SZ \
  7671. ((sizeof(preferredGroup)/sizeof(*preferredGroup)) - 1)
  7672. /* -1 for the invalid group */
  7673. /* Examines the application specified group ranking and returns the rank of the
  7674. * group.
  7675. * If no group ranking set then all groups are rank 0 (highest).
  7676. *
  7677. * ssl The SSL/TLS object.
  7678. * group The group to check ranking for.
  7679. * returns ranking from 0 to MAX_GROUP_COUNT-1 or -1 when group not in list.
  7680. */
  7681. static int TLSX_KeyShare_GroupRank(const WOLFSSL* ssl, int group)
  7682. {
  7683. byte i;
  7684. const word16* groups;
  7685. byte numGroups;
  7686. if (ssl->numGroups == 0) {
  7687. groups = preferredGroup;
  7688. numGroups = PREFERRED_GROUP_SZ;
  7689. }
  7690. else {
  7691. groups = ssl->group;
  7692. numGroups = ssl->numGroups;
  7693. }
  7694. #ifdef HAVE_LIBOQS
  7695. if (!TLSX_KeyShare_IsSupported(group))
  7696. return -1;
  7697. #endif
  7698. for (i = 0; i < numGroups; i++)
  7699. if (groups[i] == (word16)group)
  7700. return i;
  7701. return -1;
  7702. }
  7703. /* Set a key share that is supported by the client into extensions.
  7704. *
  7705. * ssl The SSL/TLS object.
  7706. * returns BAD_KEY_SHARE_DATA if no supported group has a key share,
  7707. * 0 if a supported group has a key share and other values indicate an error.
  7708. */
  7709. int TLSX_KeyShare_SetSupported(const WOLFSSL* ssl, TLSX** extensions)
  7710. {
  7711. int ret;
  7712. #ifdef HAVE_SUPPORTED_CURVES
  7713. TLSX* extension;
  7714. SupportedCurve* curve = NULL;
  7715. SupportedCurve* preferredCurve = NULL;
  7716. KeyShareEntry* kse = NULL;
  7717. int preferredRank = WOLFSSL_MAX_GROUP_COUNT;
  7718. int rank;
  7719. extension = TLSX_Find(*extensions, TLSX_SUPPORTED_GROUPS);
  7720. if (extension != NULL)
  7721. curve = (SupportedCurve*)extension->data;
  7722. /* Use server's preference order. */
  7723. for (; curve != NULL; curve = curve->next) {
  7724. if (!TLSX_KeyShare_IsSupported(curve->name))
  7725. continue;
  7726. if (wolfSSL_curve_is_disabled(ssl, curve->name))
  7727. continue;
  7728. rank = TLSX_KeyShare_GroupRank(ssl, curve->name);
  7729. if (rank == -1)
  7730. continue;
  7731. if (rank < preferredRank) {
  7732. preferredCurve = curve;
  7733. preferredRank = rank;
  7734. }
  7735. }
  7736. curve = preferredCurve;
  7737. if (curve == NULL) {
  7738. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7739. return BAD_KEY_SHARE_DATA;
  7740. }
  7741. #ifdef WOLFSSL_ASYNC_CRYPT
  7742. /* Check the old key share data list. */
  7743. extension = TLSX_Find(*extensions, TLSX_KEY_SHARE);
  7744. if (extension != NULL) {
  7745. kse = (KeyShareEntry*)extension->data;
  7746. /* We should not be computing keys if we are only going to advertise
  7747. * our choice here. */
  7748. if (kse != NULL && kse->lastRet == WC_PENDING_E) {
  7749. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7750. return BAD_KEY_SHARE_DATA;
  7751. }
  7752. }
  7753. #endif
  7754. /* Push new KeyShare extension. This will also free the old one */
  7755. ret = TLSX_Push(extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  7756. if (ret != 0)
  7757. return ret;
  7758. /* Extension got pushed to head */
  7759. extension = *extensions;
  7760. /* Push the selected curve */
  7761. ret = TLSX_KeyShare_New((KeyShareEntry**)&extension->data, curve->name,
  7762. ssl->heap, &kse);
  7763. if (ret != 0)
  7764. return ret;
  7765. /* Set extension to be in response. */
  7766. extension->resp = 1;
  7767. #else
  7768. (void)ssl;
  7769. WOLFSSL_ERROR_VERBOSE(NOT_COMPILED_IN);
  7770. ret = NOT_COMPILED_IN;
  7771. #endif
  7772. return ret;
  7773. }
  7774. /* Server side KSE processing */
  7775. int TLSX_KeyShare_Choose(const WOLFSSL *ssl, TLSX* extensions,
  7776. KeyShareEntry** kse, byte* searched)
  7777. {
  7778. TLSX* extension;
  7779. KeyShareEntry* clientKSE = NULL;
  7780. KeyShareEntry* list = NULL;
  7781. KeyShareEntry* preferredKSE = NULL;
  7782. int preferredRank = WOLFSSL_MAX_GROUP_COUNT;
  7783. int rank;
  7784. if (ssl == NULL || ssl->options.side != WOLFSSL_SERVER_END)
  7785. return BAD_FUNC_ARG;
  7786. *searched = 0;
  7787. /* Find the KeyShare extension if it exists. */
  7788. extension = TLSX_Find(extensions, TLSX_KEY_SHARE);
  7789. if (extension != NULL)
  7790. list = (KeyShareEntry*)extension->data;
  7791. if (extension && extension->resp == 1) {
  7792. /* Outside of the async case this path should not be taken. */
  7793. int ret = INCOMPLETE_DATA;
  7794. #ifdef WOLFSSL_ASYNC_CRYPT
  7795. /* in async case make sure key generation is finalized */
  7796. KeyShareEntry* serverKSE = (KeyShareEntry*)extension->data;
  7797. if (serverKSE && serverKSE->lastRet == WC_PENDING_E) {
  7798. if (ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE)
  7799. *searched = 1;
  7800. ret = TLSX_KeyShare_GenKey((WOLFSSL*)ssl, serverKSE);
  7801. }
  7802. #endif
  7803. return ret;
  7804. }
  7805. /* Use server's preference order. */
  7806. for (clientKSE = list; clientKSE != NULL; clientKSE = clientKSE->next) {
  7807. if (clientKSE->ke == NULL)
  7808. continue;
  7809. /* Check consistency now - extensions in any order. */
  7810. if (!TLSX_SupportedGroups_Find(ssl, clientKSE->group, extensions))
  7811. continue;
  7812. if (!WOLFSSL_NAMED_GROUP_IS_FFHDE(clientKSE->group)) {
  7813. /* Check max value supported. */
  7814. if (clientKSE->group > WOLFSSL_ECC_MAX) {
  7815. #ifdef HAVE_PQC
  7816. if (!WOLFSSL_NAMED_GROUP_IS_PQC(clientKSE->group))
  7817. #endif
  7818. continue;
  7819. }
  7820. if (wolfSSL_curve_is_disabled(ssl, clientKSE->group))
  7821. continue;
  7822. }
  7823. if (!TLSX_KeyShare_IsSupported(clientKSE->group))
  7824. continue;
  7825. rank = TLSX_KeyShare_GroupRank(ssl, clientKSE->group);
  7826. if (rank == -1)
  7827. continue;
  7828. if (rank < preferredRank) {
  7829. preferredKSE = clientKSE;
  7830. preferredRank = rank;
  7831. }
  7832. }
  7833. *kse = preferredKSE;
  7834. *searched = 1;
  7835. return 0;
  7836. }
  7837. /* Server side KSE processing */
  7838. int TLSX_KeyShare_Setup(WOLFSSL *ssl, KeyShareEntry* clientKSE)
  7839. {
  7840. int ret;
  7841. TLSX* extension;
  7842. KeyShareEntry* serverKSE;
  7843. KeyShareEntry* list = NULL;
  7844. if (ssl == NULL || ssl->options.side != WOLFSSL_SERVER_END)
  7845. return BAD_FUNC_ARG;
  7846. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7847. if (extension == NULL)
  7848. return BAD_STATE_E;
  7849. if (clientKSE == NULL) {
  7850. #ifdef WOLFSSL_ASYNC_CRYPT
  7851. /* Not necessarily an error. The key may have already been setup. */
  7852. if (extension != NULL && extension->resp == 1) {
  7853. serverKSE = (KeyShareEntry*)extension->data;
  7854. if (serverKSE != NULL) {
  7855. /* in async case make sure key generation is finalized */
  7856. if (serverKSE->lastRet == WC_PENDING_E)
  7857. return TLSX_KeyShare_GenKey((WOLFSSL*)ssl, serverKSE);
  7858. else if (serverKSE->lastRet == 0)
  7859. return 0;
  7860. }
  7861. }
  7862. #endif
  7863. return BAD_FUNC_ARG;
  7864. }
  7865. /* Generate a new key pair except in the case of OQS KEM because we
  7866. * are going to encapsulate and that does not require us to generate a
  7867. * key pair.
  7868. */
  7869. ret = TLSX_KeyShare_New(&list, clientKSE->group, ssl->heap, &serverKSE);
  7870. if (ret != 0)
  7871. return ret;
  7872. if (clientKSE->key == NULL) {
  7873. #ifdef HAVE_PQC
  7874. if (WOLFSSL_NAMED_GROUP_IS_PQC(clientKSE->group)) {
  7875. /* Going to need the public key (AKA ciphertext). */
  7876. serverKSE->pubKey = clientKSE->pubKey;
  7877. clientKSE->pubKey = NULL;
  7878. serverKSE->pubKeyLen = clientKSE->pubKeyLen;
  7879. clientKSE->pubKeyLen = 0;
  7880. }
  7881. else
  7882. #endif
  7883. {
  7884. ret = TLSX_KeyShare_GenKey(ssl, serverKSE);
  7885. }
  7886. /* for async do setup of serverKSE below, but return WC_PENDING_E */
  7887. if (ret != 0
  7888. #ifdef WOLFSSL_ASYNC_CRYPT
  7889. && ret != WC_PENDING_E
  7890. #endif
  7891. ) {
  7892. TLSX_KeyShare_FreeAll(list, ssl->heap);
  7893. return ret;
  7894. }
  7895. }
  7896. else {
  7897. /* transfer buffers to serverKSE */
  7898. serverKSE->key = clientKSE->key;
  7899. clientKSE->key = NULL;
  7900. serverKSE->keyLen = clientKSE->keyLen;
  7901. serverKSE->pubKey = clientKSE->pubKey;
  7902. clientKSE->pubKey = NULL;
  7903. serverKSE->pubKeyLen = clientKSE->pubKeyLen;
  7904. #ifndef NO_DH
  7905. serverKSE->privKey = clientKSE->privKey;
  7906. clientKSE->privKey = NULL;
  7907. #endif
  7908. }
  7909. serverKSE->ke = clientKSE->ke;
  7910. serverKSE->keLen = clientKSE->keLen;
  7911. clientKSE->ke = NULL;
  7912. clientKSE->keLen = 0;
  7913. TLSX_KeyShare_FreeAll((KeyShareEntry*)extension->data, ssl->heap);
  7914. extension->data = (void *)serverKSE;
  7915. extension->resp = 1;
  7916. return ret;
  7917. }
  7918. /* Ensure there is a key pair that can be used for key exchange.
  7919. *
  7920. * ssl The SSL/TLS object.
  7921. * doHelloRetry If set to non-zero will do hello_retry
  7922. * returns 0 on success and other values indicate failure.
  7923. */
  7924. int TLSX_KeyShare_Establish(WOLFSSL *ssl, int* doHelloRetry)
  7925. {
  7926. int ret;
  7927. KeyShareEntry* clientKSE = NULL;
  7928. byte searched = 0;
  7929. *doHelloRetry = 0;
  7930. ret = TLSX_KeyShare_Choose(ssl, ssl->extensions, &clientKSE, &searched);
  7931. if (ret != 0 || !searched)
  7932. return ret;
  7933. /* No supported group found - send HelloRetryRequest. */
  7934. if (clientKSE == NULL) {
  7935. /* Set KEY_SHARE_ERROR to indicate HelloRetryRequest required. */
  7936. *doHelloRetry = 1;
  7937. return TLSX_KeyShare_SetSupported(ssl, &ssl->extensions);
  7938. }
  7939. return TLSX_KeyShare_Setup(ssl, clientKSE);
  7940. }
  7941. /* Derive the shared secret of the key exchange.
  7942. *
  7943. * ssl The SSL/TLS object.
  7944. * returns 0 on success and other values indicate failure.
  7945. */
  7946. int TLSX_KeyShare_DeriveSecret(WOLFSSL *ssl)
  7947. {
  7948. int ret;
  7949. TLSX* extension;
  7950. KeyShareEntry* list = NULL;
  7951. #ifdef WOLFSSL_ASYNC_CRYPT
  7952. ret = wolfSSL_AsyncPop(ssl, NULL);
  7953. /* Check for error */
  7954. if (ret != WC_NOT_PENDING_E && ret < 0) {
  7955. return ret;
  7956. }
  7957. #endif
  7958. /* Find the KeyShare extension if it exists. */
  7959. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7960. if (extension != NULL)
  7961. list = (KeyShareEntry*)extension->data;
  7962. if (list == NULL)
  7963. return KEY_SHARE_ERROR;
  7964. /* Calculate secret. */
  7965. ret = TLSX_KeyShare_Process(ssl, list);
  7966. return ret;
  7967. }
  7968. #define KS_FREE_ALL TLSX_KeyShare_FreeAll
  7969. #define KS_GET_SIZE TLSX_KeyShare_GetSize
  7970. #define KS_WRITE TLSX_KeyShare_Write
  7971. #define KS_PARSE TLSX_KeyShare_Parse
  7972. #else
  7973. #define KS_FREE_ALL(a, b)
  7974. #define KS_GET_SIZE(a, b) 0
  7975. #define KS_WRITE(a, b, c) 0
  7976. #define KS_PARSE(a, b, c, d) 0
  7977. #endif /* WOLFSSL_TLS13 */
  7978. /******************************************************************************/
  7979. /* Pre-Shared Key */
  7980. /******************************************************************************/
  7981. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  7982. /* Free the pre-shared key dynamic data.
  7983. *
  7984. * list The linked list of key share entry objects.
  7985. * heap The heap used for allocation.
  7986. */
  7987. static void TLSX_PreSharedKey_FreeAll(PreSharedKey* list, void* heap)
  7988. {
  7989. PreSharedKey* current;
  7990. while ((current = list) != NULL) {
  7991. list = current->next;
  7992. XFREE(current->identity, heap, DYNAMIC_TYPE_TLSX);
  7993. XFREE(current, heap, DYNAMIC_TYPE_TLSX);
  7994. }
  7995. (void)heap;
  7996. }
  7997. /* Get the size of the encoded pre shared key extension.
  7998. *
  7999. * list The linked list of pre-shared key extensions.
  8000. * msgType The type of the message this extension is being written into.
  8001. * returns the number of bytes of the encoded pre-shared key extension or
  8002. * SANITY_MSG_E to indicate invalid message type.
  8003. */
  8004. static int TLSX_PreSharedKey_GetSize(PreSharedKey* list, byte msgType,
  8005. word16* pSz)
  8006. {
  8007. if (msgType == client_hello) {
  8008. /* Length of identities + Length of binders. */
  8009. word16 len = OPAQUE16_LEN + OPAQUE16_LEN;
  8010. while (list != NULL) {
  8011. /* Each entry has: identity, ticket age and binder. */
  8012. len += OPAQUE16_LEN + list->identityLen + OPAQUE32_LEN +
  8013. OPAQUE8_LEN + (word16)list->binderLen;
  8014. list = list->next;
  8015. }
  8016. *pSz += len;
  8017. return 0;
  8018. }
  8019. if (msgType == server_hello) {
  8020. *pSz += OPAQUE16_LEN;
  8021. return 0;
  8022. }
  8023. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8024. return SANITY_MSG_E;
  8025. }
  8026. /* The number of bytes to be written for the binders.
  8027. *
  8028. * list The linked list of pre-shared key extensions.
  8029. * msgType The type of the message this extension is being written into.
  8030. * returns the number of bytes of the encoded pre-shared key extension or
  8031. * SANITY_MSG_E to indicate invalid message type.
  8032. */
  8033. int TLSX_PreSharedKey_GetSizeBinders(PreSharedKey* list, byte msgType,
  8034. word16* pSz)
  8035. {
  8036. word16 len;
  8037. if (msgType != client_hello) {
  8038. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8039. return SANITY_MSG_E;
  8040. }
  8041. /* Length of all binders. */
  8042. len = OPAQUE16_LEN;
  8043. while (list != NULL) {
  8044. len += OPAQUE8_LEN + (word16)list->binderLen;
  8045. list = list->next;
  8046. }
  8047. *pSz = len;
  8048. return 0;
  8049. }
  8050. /* Writes the pre-shared key extension into the output buffer - binders only.
  8051. * Assumes that the the output buffer is big enough to hold data.
  8052. *
  8053. * list The linked list of key share entries.
  8054. * output The buffer to write into.
  8055. * msgType The type of the message this extension is being written into.
  8056. * returns the number of bytes written into the buffer.
  8057. */
  8058. int TLSX_PreSharedKey_WriteBinders(PreSharedKey* list, byte* output,
  8059. byte msgType, word16* pSz)
  8060. {
  8061. PreSharedKey* current = list;
  8062. word16 idx = 0;
  8063. word16 lenIdx;
  8064. word16 len;
  8065. if (msgType != client_hello) {
  8066. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8067. return SANITY_MSG_E;
  8068. }
  8069. /* Skip length of all binders. */
  8070. lenIdx = idx;
  8071. idx += OPAQUE16_LEN;
  8072. while (current != NULL) {
  8073. /* Binder data length. */
  8074. output[idx++] = (byte)current->binderLen;
  8075. /* Binder data. */
  8076. XMEMCPY(output + idx, current->binder, current->binderLen);
  8077. idx += (word16)current->binderLen;
  8078. current = current->next;
  8079. }
  8080. /* Length of the binders. */
  8081. len = idx - lenIdx - OPAQUE16_LEN;
  8082. c16toa(len, output + lenIdx);
  8083. *pSz = idx;
  8084. return 0;
  8085. }
  8086. /* Writes the pre-shared key extension into the output buffer.
  8087. * Assumes that the the output buffer is big enough to hold data.
  8088. *
  8089. * list The linked list of key share entries.
  8090. * output The buffer to write into.
  8091. * msgType The type of the message this extension is being written into.
  8092. * returns the number of bytes written into the buffer.
  8093. */
  8094. static int TLSX_PreSharedKey_Write(PreSharedKey* list, byte* output,
  8095. byte msgType, word16* pSz)
  8096. {
  8097. if (msgType == client_hello) {
  8098. PreSharedKey* current = list;
  8099. word16 idx = 0;
  8100. word16 lenIdx;
  8101. word16 len;
  8102. int ret;
  8103. /* Write identites only. Binders after HMACing over this. */
  8104. lenIdx = idx;
  8105. idx += OPAQUE16_LEN;
  8106. while (current != NULL) {
  8107. /* Identity length */
  8108. c16toa(current->identityLen, output + idx);
  8109. idx += OPAQUE16_LEN;
  8110. /* Identity data */
  8111. XMEMCPY(output + idx, current->identity, current->identityLen);
  8112. idx += current->identityLen;
  8113. /* Obfuscated ticket age. */
  8114. c32toa(current->ticketAge, output + idx);
  8115. idx += OPAQUE32_LEN;
  8116. current = current->next;
  8117. }
  8118. /* Length of the identites. */
  8119. len = idx - lenIdx - OPAQUE16_LEN;
  8120. c16toa(len, output + lenIdx);
  8121. /* Don't include binders here.
  8122. * The binders are based on the hash of all the ClientHello data up to
  8123. * and include the identities written above.
  8124. */
  8125. ret = TLSX_PreSharedKey_GetSizeBinders(list, msgType, &len);
  8126. if (ret < 0)
  8127. return ret;
  8128. *pSz += idx + len;
  8129. }
  8130. else if (msgType == server_hello) {
  8131. word16 i;
  8132. /* Find the index of the chosen identity. */
  8133. for (i=0; list != NULL && !list->chosen; i++)
  8134. list = list->next;
  8135. if (list == NULL) {
  8136. WOLFSSL_ERROR_VERBOSE(BUILD_MSG_ERROR);
  8137. return BUILD_MSG_ERROR;
  8138. }
  8139. /* The index of the identity chosen by the server from the list supplied
  8140. * by the client.
  8141. */
  8142. c16toa(i, output);
  8143. *pSz += OPAQUE16_LEN;
  8144. }
  8145. else {
  8146. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8147. return SANITY_MSG_E;
  8148. }
  8149. return 0;
  8150. }
  8151. int TLSX_PreSharedKey_Parse_ClientHello(TLSX** extensions, const byte* input,
  8152. word16 length, void* heap)
  8153. {
  8154. int ret;
  8155. word16 len;
  8156. word16 idx = 0;
  8157. TLSX* extension;
  8158. PreSharedKey* list;
  8159. TLSX_Remove(extensions, TLSX_PRE_SHARED_KEY, heap);
  8160. /* Length of identities and of binders. */
  8161. if ((int)(length - idx) < OPAQUE16_LEN + OPAQUE16_LEN)
  8162. return BUFFER_E;
  8163. /* Length of identities. */
  8164. ato16(input + idx, &len);
  8165. idx += OPAQUE16_LEN;
  8166. if (len < MIN_PSK_ID_LEN || length - idx < len)
  8167. return BUFFER_E;
  8168. /* Create a pre-shared key object for each identity. */
  8169. while (len > 0) {
  8170. const byte* identity;
  8171. word16 identityLen;
  8172. word32 age;
  8173. if (len < OPAQUE16_LEN)
  8174. return BUFFER_E;
  8175. /* Length of identity. */
  8176. ato16(input + idx, &identityLen);
  8177. idx += OPAQUE16_LEN;
  8178. if (len < OPAQUE16_LEN + identityLen + OPAQUE32_LEN ||
  8179. identityLen > MAX_PSK_ID_LEN)
  8180. return BUFFER_E;
  8181. /* Cache identity pointer. */
  8182. identity = input + idx;
  8183. idx += identityLen;
  8184. /* Ticket age. */
  8185. ato32(input + idx, &age);
  8186. idx += OPAQUE32_LEN;
  8187. ret = TLSX_PreSharedKey_Use(extensions, identity, identityLen, age, no_mac,
  8188. 0, 0, 1, NULL, heap);
  8189. if (ret != 0)
  8190. return ret;
  8191. /* Done with this identity. */
  8192. len -= OPAQUE16_LEN + identityLen + OPAQUE32_LEN;
  8193. }
  8194. /* Find the list of identities sent to server. */
  8195. extension = TLSX_Find(*extensions, TLSX_PRE_SHARED_KEY);
  8196. if (extension == NULL)
  8197. return PSK_KEY_ERROR;
  8198. list = (PreSharedKey*)extension->data;
  8199. /* Length of binders. */
  8200. if (idx + OPAQUE16_LEN > length)
  8201. return BUFFER_E;
  8202. ato16(input + idx, &len);
  8203. idx += OPAQUE16_LEN;
  8204. if (len < MIN_PSK_BINDERS_LEN || length - idx < len)
  8205. return BUFFER_E;
  8206. /* Set binder for each identity. */
  8207. while (list != NULL && len > 0) {
  8208. /* Length of binder */
  8209. list->binderLen = input[idx++];
  8210. if (list->binderLen < WC_SHA256_DIGEST_SIZE ||
  8211. list->binderLen > WC_MAX_DIGEST_SIZE)
  8212. return BUFFER_E;
  8213. if (len < OPAQUE8_LEN + list->binderLen)
  8214. return BUFFER_E;
  8215. /* Copy binder into static buffer. */
  8216. XMEMCPY(list->binder, input + idx, list->binderLen);
  8217. idx += (word16)list->binderLen;
  8218. /* Done with binder entry. */
  8219. len -= OPAQUE8_LEN + (word16)list->binderLen;
  8220. /* Next identity. */
  8221. list = list->next;
  8222. }
  8223. if (list != NULL || len != 0)
  8224. return BUFFER_E;
  8225. return 0;
  8226. }
  8227. /* Parse the pre-shared key extension.
  8228. * Different formats in different messages.
  8229. *
  8230. * ssl The SSL/TLS object.
  8231. * input The extension data.
  8232. * length The length of the extension data.
  8233. * msgType The type of the message this extension is being parsed from.
  8234. * returns 0 on success and other values indicate failure.
  8235. */
  8236. static int TLSX_PreSharedKey_Parse(WOLFSSL* ssl, const byte* input,
  8237. word16 length, byte msgType)
  8238. {
  8239. if (msgType == client_hello) {
  8240. return TLSX_PreSharedKey_Parse_ClientHello(&ssl->extensions, input,
  8241. length, ssl->heap);
  8242. }
  8243. if (msgType == server_hello) {
  8244. word16 idx;
  8245. PreSharedKey* list;
  8246. TLSX* extension;
  8247. /* Index of identity chosen by server. */
  8248. if (length != OPAQUE16_LEN)
  8249. return BUFFER_E;
  8250. ato16(input, &idx);
  8251. #ifdef WOLFSSL_EARLY_DATA
  8252. ssl->options.pskIdIndex = idx + 1;
  8253. #endif
  8254. /* Find the list of identities sent to server. */
  8255. extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
  8256. if (extension == NULL)
  8257. return PSK_KEY_ERROR;
  8258. list = (PreSharedKey*)extension->data;
  8259. /* Mark the identity as chosen. */
  8260. for (; list != NULL && idx > 0; idx--)
  8261. list = list->next;
  8262. if (list == NULL) {
  8263. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  8264. return PSK_KEY_ERROR;
  8265. }
  8266. list->chosen = 1;
  8267. #ifdef HAVE_SESSION_TICKET
  8268. if (list->resumption) {
  8269. /* Check that the session's details are the same as the server's. */
  8270. if (ssl->options.cipherSuite0 != ssl->session->cipherSuite0 ||
  8271. ssl->options.cipherSuite != ssl->session->cipherSuite ||
  8272. ssl->session->version.major != ssl->ctx->method->version.major ||
  8273. ssl->session->version.minor != ssl->ctx->method->version.minor) {
  8274. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  8275. return PSK_KEY_ERROR;
  8276. }
  8277. }
  8278. #endif
  8279. return 0;
  8280. }
  8281. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8282. return SANITY_MSG_E;
  8283. }
  8284. /* Create a new pre-shared key and put it into the list.
  8285. *
  8286. * list The linked list of pre-shared key.
  8287. * identity The identity.
  8288. * len The length of the identity data.
  8289. * heap The memory to allocate with.
  8290. * preSharedKey The new pre-shared key object.
  8291. * returns 0 on success and other values indicate failure.
  8292. */
  8293. static int TLSX_PreSharedKey_New(PreSharedKey** list, const byte* identity,
  8294. word16 len, void *heap,
  8295. PreSharedKey** preSharedKey)
  8296. {
  8297. PreSharedKey* psk;
  8298. PreSharedKey** next;
  8299. psk = (PreSharedKey*)XMALLOC(sizeof(PreSharedKey), heap, DYNAMIC_TYPE_TLSX);
  8300. if (psk == NULL)
  8301. return MEMORY_E;
  8302. XMEMSET(psk, 0, sizeof(*psk));
  8303. /* Make a copy of the identity data. */
  8304. psk->identity = (byte*)XMALLOC(len + NULL_TERM_LEN, heap,
  8305. DYNAMIC_TYPE_TLSX);
  8306. if (psk->identity == NULL) {
  8307. XFREE(psk, heap, DYNAMIC_TYPE_TLSX);
  8308. return MEMORY_E;
  8309. }
  8310. XMEMCPY(psk->identity, identity, len);
  8311. psk->identityLen = len;
  8312. /* Use a NULL terminator in case it is a C string */
  8313. psk->identity[psk->identityLen] = '\0';
  8314. /* Add it to the end and maintain the links. */
  8315. while (*list != NULL) {
  8316. /* Assign to temporary to work around compiler bug found by customer. */
  8317. next = &((*list)->next);
  8318. list = next;
  8319. }
  8320. *list = psk;
  8321. *preSharedKey = psk;
  8322. (void)heap;
  8323. return 0;
  8324. }
  8325. static WC_INLINE byte GetHmacLength(int hmac)
  8326. {
  8327. switch (hmac) {
  8328. #ifndef NO_SHA256
  8329. case sha256_mac:
  8330. return WC_SHA256_DIGEST_SIZE;
  8331. #endif
  8332. #ifdef WOLFSSL_SHA384
  8333. case sha384_mac:
  8334. return WC_SHA384_DIGEST_SIZE;
  8335. #endif
  8336. #ifdef WOLFSSL_SHA512
  8337. case sha512_mac:
  8338. return WC_SHA512_DIGEST_SIZE;
  8339. #endif
  8340. }
  8341. return 0;
  8342. }
  8343. /* Use the data to create a new pre-shared key object in the extensions.
  8344. *
  8345. * ssl The SSL/TLS object.
  8346. * identity The identity.
  8347. * len The length of the identity data.
  8348. * age The age of the identity.
  8349. * hmac The HMAC algorithm.
  8350. * ciphersuite0 The first byte of the ciphersuite to use.
  8351. * ciphersuite The second byte of the ciphersuite to use.
  8352. * resumption The PSK is for resumption of a session.
  8353. * preSharedKey The new pre-shared key object.
  8354. * returns 0 on success and other values indicate failure.
  8355. */
  8356. int TLSX_PreSharedKey_Use(TLSX** extensions, const byte* identity, word16 len,
  8357. word32 age, byte hmac, byte cipherSuite0,
  8358. byte cipherSuite, byte resumption,
  8359. PreSharedKey **preSharedKey, void* heap)
  8360. {
  8361. int ret = 0;
  8362. TLSX* extension;
  8363. PreSharedKey* psk = NULL;
  8364. /* Find the pre-shared key extension if it exists. */
  8365. extension = TLSX_Find(*extensions, TLSX_PRE_SHARED_KEY);
  8366. if (extension == NULL) {
  8367. /* Push new pre-shared key extension. */
  8368. ret = TLSX_Push(extensions, TLSX_PRE_SHARED_KEY, NULL, heap);
  8369. if (ret != 0)
  8370. return ret;
  8371. extension = TLSX_Find(*extensions, TLSX_PRE_SHARED_KEY);
  8372. if (extension == NULL)
  8373. return MEMORY_E;
  8374. }
  8375. /* Try to find the pre-shared key with this identity. */
  8376. psk = (PreSharedKey*)extension->data;
  8377. while (psk != NULL) {
  8378. if ((psk->identityLen == len) &&
  8379. (XMEMCMP(psk->identity, identity, len) == 0)) {
  8380. break;
  8381. }
  8382. psk = psk->next;
  8383. }
  8384. /* Create a new pre-shared key object if not found. */
  8385. if (psk == NULL) {
  8386. ret = TLSX_PreSharedKey_New((PreSharedKey**)&extension->data, identity,
  8387. len, heap, &psk);
  8388. if (ret != 0)
  8389. return ret;
  8390. }
  8391. /* Update/set age and HMAC algorithm. */
  8392. psk->ticketAge = age;
  8393. psk->hmac = hmac;
  8394. psk->cipherSuite0 = cipherSuite0;
  8395. psk->cipherSuite = cipherSuite;
  8396. psk->resumption = resumption;
  8397. psk->binderLen = GetHmacLength(psk->hmac);
  8398. if (preSharedKey != NULL)
  8399. *preSharedKey = psk;
  8400. return 0;
  8401. }
  8402. #define PSK_FREE_ALL TLSX_PreSharedKey_FreeAll
  8403. #define PSK_GET_SIZE TLSX_PreSharedKey_GetSize
  8404. #define PSK_WRITE TLSX_PreSharedKey_Write
  8405. #define PSK_PARSE TLSX_PreSharedKey_Parse
  8406. #else
  8407. #define PSK_FREE_ALL(a, b)
  8408. #define PSK_GET_SIZE(a, b, c) 0
  8409. #define PSK_WRITE(a, b, c, d) 0
  8410. #define PSK_PARSE(a, b, c, d) 0
  8411. #endif
  8412. /******************************************************************************/
  8413. /* PSK Key Exchange Modes */
  8414. /******************************************************************************/
  8415. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  8416. /* Get the size of the encoded PSK KE modes extension.
  8417. * Only in ClientHello.
  8418. *
  8419. * modes The PSK KE mode bit string.
  8420. * msgType The type of the message this extension is being written into.
  8421. * returns the number of bytes of the encoded PSK KE mode extension.
  8422. */
  8423. static int TLSX_PskKeModes_GetSize(byte modes, byte msgType, word16* pSz)
  8424. {
  8425. if (msgType == client_hello) {
  8426. /* Format: Len | Modes* */
  8427. word16 len = OPAQUE8_LEN;
  8428. /* Check whether each possible mode is to be written. */
  8429. if (modes & (1 << PSK_KE))
  8430. len += OPAQUE8_LEN;
  8431. if (modes & (1 << PSK_DHE_KE))
  8432. len += OPAQUE8_LEN;
  8433. *pSz += len;
  8434. return 0;
  8435. }
  8436. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8437. return SANITY_MSG_E;
  8438. }
  8439. /* Writes the PSK KE modes extension into the output buffer.
  8440. * Assumes that the the output buffer is big enough to hold data.
  8441. * Only in ClientHello.
  8442. *
  8443. * modes The PSK KE mode bit string.
  8444. * output The buffer to write into.
  8445. * msgType The type of the message this extension is being written into.
  8446. * returns the number of bytes written into the buffer.
  8447. */
  8448. static int TLSX_PskKeModes_Write(byte modes, byte* output, byte msgType,
  8449. word16* pSz)
  8450. {
  8451. if (msgType == client_hello) {
  8452. /* Format: Len | Modes* */
  8453. word16 idx = OPAQUE8_LEN;
  8454. /* Write out each possible mode. */
  8455. if (modes & (1 << PSK_KE))
  8456. output[idx++] = PSK_KE;
  8457. if (modes & (1 << PSK_DHE_KE))
  8458. output[idx++] = PSK_DHE_KE;
  8459. /* Write out length of mode list. */
  8460. output[0] = (byte)(idx - OPAQUE8_LEN);
  8461. *pSz += idx;
  8462. return 0;
  8463. }
  8464. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8465. return SANITY_MSG_E;
  8466. }
  8467. int TLSX_PskKeyModes_Parse_Modes(const byte* input, word16 length, byte msgType,
  8468. byte* modes)
  8469. {
  8470. if (msgType == client_hello) {
  8471. /* Format: Len | Modes* */
  8472. int idx = 0;
  8473. word16 len;
  8474. *modes = 0;
  8475. /* Ensure length byte exists. */
  8476. if (length < OPAQUE8_LEN)
  8477. return BUFFER_E;
  8478. /* Get length of mode list and ensure that is the only data. */
  8479. len = input[0];
  8480. if (length - OPAQUE8_LEN != len)
  8481. return BUFFER_E;
  8482. idx = OPAQUE8_LEN;
  8483. /* Set a bit for each recognized modes. */
  8484. while (len > 0) {
  8485. /* Ignore unrecognized modes. */
  8486. if (input[idx] <= PSK_DHE_KE)
  8487. *modes |= 1 << input[idx];
  8488. idx++;
  8489. len--;
  8490. }
  8491. return 0;
  8492. }
  8493. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8494. return SANITY_MSG_E;
  8495. }
  8496. /* Parse the PSK KE modes extension.
  8497. * Only in ClientHello.
  8498. *
  8499. * ssl The SSL/TLS object.
  8500. * input The extension data.
  8501. * length The length of the extension data.
  8502. * msgType The type of the message this extension is being parsed from.
  8503. * returns 0 on success and other values indicate failure.
  8504. */
  8505. static int TLSX_PskKeModes_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  8506. byte msgType)
  8507. {
  8508. int ret;
  8509. byte modes;
  8510. ret = TLSX_PskKeyModes_Parse_Modes(input, length, msgType, &modes);
  8511. if (ret == 0)
  8512. ret = TLSX_PskKeyModes_Use(ssl, modes);
  8513. WOLFSSL_ERROR_VERBOSE(ret);
  8514. return ret;
  8515. }
  8516. /* Use the data to create a new PSK Key Exchange Modes object in the extensions.
  8517. *
  8518. * ssl The SSL/TLS object.
  8519. * modes The PSK key exchange modes.
  8520. * returns 0 on success and other values indicate failure.
  8521. */
  8522. int TLSX_PskKeyModes_Use(WOLFSSL* ssl, byte modes)
  8523. {
  8524. int ret = 0;
  8525. TLSX* extension;
  8526. /* Find the PSK key exchange modes extension if it exists. */
  8527. extension = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES);
  8528. if (extension == NULL) {
  8529. /* Push new PSK key exchange modes extension. */
  8530. ret = TLSX_Push(&ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES, NULL,
  8531. ssl->heap);
  8532. if (ret != 0)
  8533. return ret;
  8534. extension = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES);
  8535. if (extension == NULL)
  8536. return MEMORY_E;
  8537. }
  8538. extension->val = modes;
  8539. return 0;
  8540. }
  8541. #define PKM_GET_SIZE TLSX_PskKeModes_GetSize
  8542. #define PKM_WRITE TLSX_PskKeModes_Write
  8543. #define PKM_PARSE TLSX_PskKeModes_Parse
  8544. #else
  8545. #define PKM_GET_SIZE(a, b, c) 0
  8546. #define PKM_WRITE(a, b, c, d) 0
  8547. #define PKM_PARSE(a, b, c, d) 0
  8548. #endif
  8549. /******************************************************************************/
  8550. /* Post-Handshake Authentication */
  8551. /******************************************************************************/
  8552. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  8553. /* Get the size of the encoded Post-Handshake Authentication extension.
  8554. * Only in ClientHello.
  8555. *
  8556. * msgType The type of the message this extension is being written into.
  8557. * returns the number of bytes of the encoded Post-Handshake Authentication
  8558. * extension.
  8559. */
  8560. static int TLSX_PostHandAuth_GetSize(byte msgType, word16* pSz)
  8561. {
  8562. if (msgType == client_hello) {
  8563. *pSz += 0;
  8564. return 0;
  8565. }
  8566. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8567. return SANITY_MSG_E;
  8568. }
  8569. /* Writes the Post-Handshake Authentication extension into the output buffer.
  8570. * Assumes that the the output buffer is big enough to hold data.
  8571. * Only in ClientHello.
  8572. *
  8573. * output The buffer to write into.
  8574. * msgType The type of the message this extension is being written into.
  8575. * returns the number of bytes written into the buffer.
  8576. */
  8577. static int TLSX_PostHandAuth_Write(byte* output, byte msgType, word16* pSz)
  8578. {
  8579. (void)output;
  8580. if (msgType == client_hello) {
  8581. *pSz += 0;
  8582. return 0;
  8583. }
  8584. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8585. return SANITY_MSG_E;
  8586. }
  8587. /* Parse the Post-Handshake Authentication extension.
  8588. * Only in ClientHello.
  8589. *
  8590. * ssl The SSL/TLS object.
  8591. * input The extension data.
  8592. * length The length of the extension data.
  8593. * msgType The type of the message this extension is being parsed from.
  8594. * returns 0 on success and other values indicate failure.
  8595. */
  8596. static int TLSX_PostHandAuth_Parse(WOLFSSL* ssl, const byte* input,
  8597. word16 length, byte msgType)
  8598. {
  8599. (void)input;
  8600. if (msgType == client_hello) {
  8601. /* Ensure extension is empty. */
  8602. if (length != 0)
  8603. return BUFFER_E;
  8604. ssl->options.postHandshakeAuth = 1;
  8605. return 0;
  8606. }
  8607. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8608. return SANITY_MSG_E;
  8609. }
  8610. /* Create a new Post-handshake authentication object in the extensions.
  8611. *
  8612. * ssl The SSL/TLS object.
  8613. * returns 0 on success and other values indicate failure.
  8614. */
  8615. static int TLSX_PostHandAuth_Use(WOLFSSL* ssl)
  8616. {
  8617. int ret = 0;
  8618. TLSX* extension;
  8619. /* Find the PSK key exchange modes extension if it exists. */
  8620. extension = TLSX_Find(ssl->extensions, TLSX_POST_HANDSHAKE_AUTH);
  8621. if (extension == NULL) {
  8622. /* Push new Post-handshake Authentication extension. */
  8623. ret = TLSX_Push(&ssl->extensions, TLSX_POST_HANDSHAKE_AUTH, NULL,
  8624. ssl->heap);
  8625. if (ret != 0)
  8626. return ret;
  8627. }
  8628. return 0;
  8629. }
  8630. #define PHA_GET_SIZE TLSX_PostHandAuth_GetSize
  8631. #define PHA_WRITE TLSX_PostHandAuth_Write
  8632. #define PHA_PARSE TLSX_PostHandAuth_Parse
  8633. #else
  8634. #define PHA_GET_SIZE(a, b) 0
  8635. #define PHA_WRITE(a, b, c) 0
  8636. #define PHA_PARSE(a, b, c, d) 0
  8637. #endif
  8638. /******************************************************************************/
  8639. /* Early Data Indication */
  8640. /******************************************************************************/
  8641. #ifdef WOLFSSL_EARLY_DATA
  8642. /* Get the size of the encoded Early Data Indication extension.
  8643. * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
  8644. *
  8645. * msgType The type of the message this extension is being written into.
  8646. * returns the number of bytes of the encoded Early Data Indication extension.
  8647. */
  8648. static int TLSX_EarlyData_GetSize(byte msgType, word16* pSz)
  8649. {
  8650. int ret = 0;
  8651. if (msgType == client_hello || msgType == encrypted_extensions)
  8652. *pSz += 0;
  8653. else if (msgType == session_ticket)
  8654. *pSz += OPAQUE32_LEN;
  8655. else {
  8656. ret = SANITY_MSG_E;
  8657. WOLFSSL_ERROR_VERBOSE(ret);
  8658. }
  8659. return ret;
  8660. }
  8661. /* Writes the Early Data Indicator extension into the output buffer.
  8662. * Assumes that the the output buffer is big enough to hold data.
  8663. * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
  8664. *
  8665. * maxSz The maximum early data size.
  8666. * output The buffer to write into.
  8667. * msgType The type of the message this extension is being written into.
  8668. * returns the number of bytes written into the buffer.
  8669. */
  8670. static int TLSX_EarlyData_Write(word32 maxSz, byte* output, byte msgType,
  8671. word16* pSz)
  8672. {
  8673. if (msgType == client_hello || msgType == encrypted_extensions)
  8674. return 0;
  8675. else if (msgType == session_ticket) {
  8676. c32toa(maxSz, output);
  8677. *pSz += OPAQUE32_LEN;
  8678. return 0;
  8679. }
  8680. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8681. return SANITY_MSG_E;
  8682. }
  8683. /* Parse the Early Data Indicator extension.
  8684. * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
  8685. *
  8686. * ssl The SSL/TLS object.
  8687. * input The extension data.
  8688. * length The length of the extension data.
  8689. * msgType The type of the message this extension is being parsed from.
  8690. * returns 0 on success and other values indicate failure.
  8691. */
  8692. static int TLSX_EarlyData_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  8693. byte msgType)
  8694. {
  8695. WOLFSSL_ENTER("TLSX_EarlyData_Parse");
  8696. if (msgType == client_hello) {
  8697. if (length != 0)
  8698. return BUFFER_E;
  8699. if (ssl->earlyData == expecting_early_data) {
  8700. if (ssl->options.maxEarlyDataSz != 0)
  8701. ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_ACCEPTED;
  8702. else
  8703. ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_REJECTED;
  8704. return TLSX_EarlyData_Use(ssl, 0, 0);
  8705. }
  8706. ssl->earlyData = early_data_ext;
  8707. return 0;
  8708. }
  8709. if (msgType == encrypted_extensions) {
  8710. if (length != 0)
  8711. return BUFFER_E;
  8712. /* Ensure the index of PSK identity chosen by server is 0.
  8713. * Index is plus one to handle 'not set' value of 0.
  8714. */
  8715. if (ssl->options.pskIdIndex != 1) {
  8716. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  8717. return PSK_KEY_ERROR;
  8718. }
  8719. if (ssl->options.side == WOLFSSL_CLIENT_END) {
  8720. /* the extension from server comes in */
  8721. ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_ACCEPTED;
  8722. }
  8723. return TLSX_EarlyData_Use(ssl, 1, 1);
  8724. }
  8725. if (msgType == session_ticket) {
  8726. word32 maxSz;
  8727. if (length != OPAQUE32_LEN)
  8728. return BUFFER_E;
  8729. ato32(input, &maxSz);
  8730. ssl->session->maxEarlyDataSz = maxSz;
  8731. return 0;
  8732. }
  8733. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8734. return SANITY_MSG_E;
  8735. }
  8736. /* Use the data to create a new Early Data object in the extensions.
  8737. *
  8738. * ssl The SSL/TLS object.
  8739. * maxSz The maximum early data size.
  8740. * is_response if this extension is part of a response
  8741. * returns 0 on success and other values indicate failure.
  8742. */
  8743. int TLSX_EarlyData_Use(WOLFSSL* ssl, word32 maxSz, int is_response)
  8744. {
  8745. int ret = 0;
  8746. TLSX* extension;
  8747. /* Find the early data extension if it exists. */
  8748. extension = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA);
  8749. if (extension == NULL) {
  8750. /* Push new early data extension. */
  8751. ret = TLSX_Push(&ssl->extensions, TLSX_EARLY_DATA, NULL, ssl->heap);
  8752. if (ret != 0)
  8753. return ret;
  8754. extension = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA);
  8755. if (extension == NULL)
  8756. return MEMORY_E;
  8757. }
  8758. extension->resp = is_response;
  8759. /* In QUIC, earlydata size is either 0 or 0xffffffff.
  8760. * Override any size between, possibly left from our initial value */
  8761. extension->val = (WOLFSSL_IS_QUIC(ssl) && is_response && maxSz > 0) ?
  8762. WOLFSSL_MAX_32BIT : maxSz;
  8763. return 0;
  8764. }
  8765. #define EDI_GET_SIZE TLSX_EarlyData_GetSize
  8766. #define EDI_WRITE TLSX_EarlyData_Write
  8767. #define EDI_PARSE TLSX_EarlyData_Parse
  8768. #else
  8769. #define EDI_GET_SIZE(a, b) 0
  8770. #define EDI_WRITE(a, b, c, d) 0
  8771. #define EDI_PARSE(a, b, c, d) 0
  8772. #endif
  8773. /******************************************************************************/
  8774. /* QUIC transport parameter extension */
  8775. /******************************************************************************/
  8776. #ifdef WOLFSSL_QUIC
  8777. static word16 TLSX_QuicTP_GetSize(TLSX* extension)
  8778. {
  8779. const QuicTransportParam *tp = (QuicTransportParam*)extension->data;
  8780. return tp ? tp->len : 0;
  8781. }
  8782. int TLSX_QuicTP_Use(WOLFSSL* ssl, TLSX_Type ext_type, int is_response)
  8783. {
  8784. int ret = 0;
  8785. TLSX* extension;
  8786. WOLFSSL_ENTER("TLSX_QuicTP_Use");
  8787. if (ssl->quic.transport_local == NULL) {
  8788. /* RFC9000, ch 7.3: "An endpoint MUST treat the absence of [...]
  8789. * from either endpoint [...] as a connection error of type
  8790. * TRANSPORT_PARAMETER_ERROR."
  8791. */
  8792. ret = QUIC_TP_MISSING_E;
  8793. goto cleanup;
  8794. }
  8795. extension = TLSX_Find(ssl->extensions, ext_type);
  8796. if (extension == NULL) {
  8797. ret = TLSX_Push(&ssl->extensions, ext_type, NULL, ssl->heap);
  8798. if (ret != 0)
  8799. goto cleanup;
  8800. extension = TLSX_Find(ssl->extensions, ext_type);
  8801. if (extension == NULL) {
  8802. ret = MEMORY_E;
  8803. goto cleanup;
  8804. }
  8805. }
  8806. if (extension->data) {
  8807. QuicTransportParam_free((QuicTransportParam*)extension->data, ssl->heap);
  8808. extension->data = NULL;
  8809. }
  8810. extension->resp = is_response;
  8811. extension->data = (void*)QuicTransportParam_dup(ssl->quic.transport_local, ssl->heap);
  8812. if (!extension->data) {
  8813. ret = MEMORY_E;
  8814. goto cleanup;
  8815. }
  8816. cleanup:
  8817. WOLFSSL_LEAVE("TLSX_QuicTP_Use", ret);
  8818. return ret;
  8819. }
  8820. static word16 TLSX_QuicTP_Write(QuicTransportParam *tp, byte* output)
  8821. {
  8822. word16 len = 0;
  8823. WOLFSSL_ENTER("TLSX_QuicTP_Write");
  8824. if (tp && tp->len) {
  8825. XMEMCPY(output, tp->data, tp->len);
  8826. len = tp->len;
  8827. }
  8828. WOLFSSL_LEAVE("TLSX_QuicTP_Write", len);
  8829. return len;
  8830. }
  8831. static int TLSX_QuicTP_Parse(WOLFSSL *ssl, const byte *input, size_t len, int ext_type, int msgType)
  8832. {
  8833. const QuicTransportParam *tp, **ptp;
  8834. (void)msgType;
  8835. tp = QuicTransportParam_new(input, len, ssl->heap);
  8836. if (!tp) {
  8837. return MEMORY_E;
  8838. }
  8839. ptp = (ext_type == TLSX_KEY_QUIC_TP_PARAMS_DRAFT) ?
  8840. &ssl->quic.transport_peer_draft : &ssl->quic.transport_peer;
  8841. if (*ptp) {
  8842. QTP_FREE(*ptp, ssl->heap);
  8843. }
  8844. *ptp = tp;
  8845. return 0;
  8846. }
  8847. #define QTP_GET_SIZE TLSX_QuicTP_GetSize
  8848. #define QTP_USE TLSX_QuicTP_Use
  8849. #define QTP_WRITE TLSX_QuicTP_Write
  8850. #define QTP_PARSE TLSX_QuicTP_Parse
  8851. #endif /* WOLFSSL_QUIC */
  8852. #if defined(WOLFSSL_DTLS_CID)
  8853. #define CID_GET_SIZE TLSX_ConnectionID_GetSize
  8854. #define CID_WRITE TLSX_ConnectionID_Write
  8855. #define CID_PARSE TLSX_ConnectionID_Parse
  8856. #define CID_FREE TLSX_ConnectionID_Free
  8857. #else
  8858. #define CID_GET_SIZE(a) 0
  8859. #define CID_WRITE(a, b) 0
  8860. #define CID_PARSE(a, b, c, d) 0
  8861. #define CID_FREE(a, b) 0
  8862. #endif /* defined(WOLFSSL_DTLS_CID) */
  8863. /******************************************************************************/
  8864. /* TLS Extensions Framework */
  8865. /******************************************************************************/
  8866. /** Finds an extension in the provided list. */
  8867. TLSX* TLSX_Find(TLSX* list, TLSX_Type type)
  8868. {
  8869. TLSX* extension = list;
  8870. while (extension && extension->type != type)
  8871. extension = extension->next;
  8872. return extension;
  8873. }
  8874. /** Remove an extension. */
  8875. void TLSX_Remove(TLSX** list, TLSX_Type type, void* heap)
  8876. {
  8877. TLSX* extension;
  8878. TLSX** next;
  8879. if (list == NULL)
  8880. return;
  8881. extension = *list;
  8882. next = list;
  8883. while (extension && extension->type != type) {
  8884. next = &extension->next;
  8885. extension = extension->next;
  8886. }
  8887. if (extension) {
  8888. *next = extension->next;
  8889. extension->next = NULL;
  8890. TLSX_FreeAll(extension, heap);
  8891. }
  8892. }
  8893. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  8894. #define GREASE_ECH_SIZE 160
  8895. #define MAX_PUBLIC_NAME_SZ 256
  8896. #define TLS_INFO_CONST_STRING "tls ech"
  8897. #define TLS_INFO_CONST_STRING_SZ 7
  8898. /* return status after setting up ech to write a grease ech */
  8899. static int TLSX_GreaseECH_Use(TLSX** extensions, void* heap, WC_RNG* rng)
  8900. {
  8901. int ret = 0;
  8902. WOLFSSL_ECH* ech;
  8903. if (extensions == NULL)
  8904. return BAD_FUNC_ARG;
  8905. ech = (WOLFSSL_ECH*)XMALLOC(sizeof(WOLFSSL_ECH), heap,
  8906. DYNAMIC_TYPE_TMP_BUFFER);
  8907. if (ech == NULL)
  8908. return MEMORY_E;
  8909. ForceZero(ech, sizeof(WOLFSSL_ECH));
  8910. ech->state = ECH_WRITE_GREASE;
  8911. /* 0 for outer */
  8912. ech->type = ECH_TYPE_OUTER;
  8913. /* kemId */
  8914. ech->kemId = DHKEM_X25519_HKDF_SHA256;
  8915. /* cipherSuite kdf */
  8916. ech->cipherSuite.kdfId = HKDF_SHA256;
  8917. /* cipherSuite aead */
  8918. ech->cipherSuite.aeadId = HPKE_AES_128_GCM;
  8919. /* random configId */
  8920. ret = wc_RNG_GenerateByte(rng, &(ech->configId));
  8921. /* curve25519 encLen */
  8922. ech->encLen = DHKEM_X25519_ENC_LEN;
  8923. if (ret == 0)
  8924. ret = TLSX_Push(extensions, TLSX_ECH, ech, heap);
  8925. if (ret != 0) {
  8926. XFREE(ech, heap, DYNAMIC_TYPE_TMP_BUFFER);
  8927. }
  8928. return ret;
  8929. }
  8930. /* return status after setting up ech to write real ech */
  8931. static int TLSX_ECH_Use(WOLFSSL_EchConfig* echConfig, TLSX** extensions,
  8932. void* heap, WC_RNG* rng)
  8933. {
  8934. int ret = 0;
  8935. int suiteIndex;
  8936. WOLFSSL_ECH* ech;
  8937. if (extensions == NULL)
  8938. return BAD_FUNC_ARG;
  8939. /* find a supported cipher suite */
  8940. suiteIndex = EchConfigGetSupportedCipherSuite(echConfig);
  8941. if (suiteIndex < 0)
  8942. return suiteIndex;
  8943. ech = (WOLFSSL_ECH*)XMALLOC(sizeof(WOLFSSL_ECH), heap,
  8944. DYNAMIC_TYPE_TMP_BUFFER);
  8945. if (ech == NULL)
  8946. return MEMORY_E;
  8947. ForceZero(ech, sizeof(WOLFSSL_ECH));
  8948. ech->state = ECH_WRITE_REAL;
  8949. ech->echConfig = echConfig;
  8950. /* 0 for outer */
  8951. ech->type = ECH_TYPE_OUTER;
  8952. /* kemId */
  8953. ech->kemId = echConfig->kemId;
  8954. /* cipherSuite kdf */
  8955. ech->cipherSuite.kdfId = echConfig->cipherSuites[suiteIndex].kdfId;
  8956. /* cipherSuite aead */
  8957. ech->cipherSuite.aeadId = echConfig->cipherSuites[suiteIndex].aeadId;
  8958. /* configId */
  8959. ech->configId = echConfig->configId;
  8960. /* encLen */
  8961. switch (echConfig->kemId)
  8962. {
  8963. case DHKEM_P256_HKDF_SHA256:
  8964. ech->encLen = DHKEM_P256_ENC_LEN;
  8965. break;
  8966. case DHKEM_P384_HKDF_SHA384:
  8967. ech->encLen = DHKEM_P384_ENC_LEN;
  8968. break;
  8969. case DHKEM_P521_HKDF_SHA512:
  8970. ech->encLen = DHKEM_P521_ENC_LEN;
  8971. break;
  8972. case DHKEM_X25519_HKDF_SHA256:
  8973. ech->encLen = DHKEM_X25519_ENC_LEN;
  8974. break;
  8975. case DHKEM_X448_HKDF_SHA512:
  8976. ech->encLen = DHKEM_X448_ENC_LEN;
  8977. break;
  8978. }
  8979. /* setup hpke */
  8980. ech->hpke = (Hpke*)XMALLOC(sizeof(Hpke), heap, DYNAMIC_TYPE_TMP_BUFFER);
  8981. if (ech->hpke == NULL) {
  8982. XFREE(ech, heap, DYNAMIC_TYPE_TMP_BUFFER);
  8983. return MEMORY_E;
  8984. }
  8985. ret = wc_HpkeInit(ech->hpke, ech->kemId, ech->cipherSuite.kdfId,
  8986. ech->cipherSuite.aeadId, heap);
  8987. /* setup the ephemeralKey */
  8988. if (ret == 0)
  8989. ret = wc_HpkeGenerateKeyPair(ech->hpke, &ech->ephemeralKey, rng);
  8990. if (ret == 0)
  8991. ret = TLSX_Push(extensions, TLSX_ECH, ech, heap);
  8992. if (ret != 0) {
  8993. XFREE(ech->hpke, heap, DYNAMIC_TYPE_TMP_BUFFER);
  8994. XFREE(ech, heap, DYNAMIC_TYPE_TMP_BUFFER);
  8995. }
  8996. return ret;
  8997. }
  8998. /* return status after setting up ech to read and decrypt */
  8999. static int TLSX_ServerECH_Use(TLSX** extensions, void* heap,
  9000. WOLFSSL_EchConfig* configs)
  9001. {
  9002. int ret;
  9003. WOLFSSL_ECH* ech;
  9004. TLSX* echX;
  9005. if (extensions == NULL)
  9006. return BAD_FUNC_ARG;
  9007. /* if we already have ech don't override it */
  9008. echX = TLSX_Find(*extensions, TLSX_ECH);
  9009. if (echX != NULL)
  9010. return 0;
  9011. ech = (WOLFSSL_ECH*)XMALLOC(sizeof(WOLFSSL_ECH), heap,
  9012. DYNAMIC_TYPE_TMP_BUFFER);
  9013. if (ech == NULL)
  9014. return MEMORY_E;
  9015. ForceZero(ech, sizeof(WOLFSSL_ECH));
  9016. ech->state = ECH_WRITE_NONE;
  9017. /* 0 for outer */
  9018. ech->type = ECH_TYPE_OUTER;
  9019. ech->echConfig = configs;
  9020. /* setup the rest of the settings when we receive ech from the client */
  9021. ret = TLSX_Push(extensions, TLSX_ECH, ech, heap);
  9022. if (ret != 0)
  9023. XFREE(ech, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9024. return ret;
  9025. }
  9026. /* return length after writing the ech */
  9027. static int TLSX_ECH_Write(WOLFSSL_ECH* ech, byte* writeBuf, word16* offset)
  9028. {
  9029. int ret = 0;
  9030. int rngRet = -1;
  9031. word32 configsLen = 0;
  9032. void* ephemeralKey = NULL;
  9033. byte* writeBuf_p = writeBuf;
  9034. #ifdef WOLFSSL_SMALL_STACK
  9035. Hpke* hpke = NULL;
  9036. WC_RNG* rng = NULL;
  9037. #else
  9038. Hpke hpke[1];
  9039. WC_RNG rng[1];
  9040. #endif
  9041. WOLFSSL_MSG("TLSX_ECH_Write");
  9042. if (ech->state == ECH_WRITE_NONE || ech->state == ECH_PARSED_INTERNAL)
  9043. return 0;
  9044. if (ech->state == ECH_WRITE_RETRY_CONFIGS) {
  9045. /* get size then write */
  9046. ret = GetEchConfigsEx(ech->echConfig, NULL, &configsLen);
  9047. if (ret != LENGTH_ONLY_E)
  9048. return ret;
  9049. ret = GetEchConfigsEx(ech->echConfig, writeBuf, &configsLen);
  9050. if (ret != WOLFSSL_SUCCESS)
  9051. return ret;
  9052. *offset += configsLen;
  9053. return 0;
  9054. }
  9055. #ifdef WOLFSSL_SMALL_STACK
  9056. hpke = (Hpke*)XMALLOC(sizeof(Hpke), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9057. if (hpke == NULL)
  9058. return MEMORY_E;
  9059. rng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
  9060. if (rng == NULL) {
  9061. XFREE(hpke, NULL, DYNAMIC_TYPE_RNG);
  9062. return MEMORY_E;
  9063. }
  9064. #endif
  9065. /* type */
  9066. *writeBuf_p = ech->type;
  9067. writeBuf_p += sizeof(ech->type);
  9068. /* outer has body, inner does not */
  9069. if (ech->type == ECH_TYPE_OUTER) {
  9070. /* kdfId */
  9071. c16toa(ech->cipherSuite.kdfId, writeBuf_p);
  9072. writeBuf_p += sizeof(ech->cipherSuite.kdfId);
  9073. /* aeadId */
  9074. c16toa(ech->cipherSuite.aeadId, writeBuf_p);
  9075. writeBuf_p += sizeof(ech->cipherSuite.aeadId);
  9076. /* configId */
  9077. *writeBuf_p = ech->configId;
  9078. writeBuf_p += sizeof(ech->configId);
  9079. /* encLen */
  9080. c16toa(ech->encLen, writeBuf_p);
  9081. writeBuf_p += 2;
  9082. if (ech->state == ECH_WRITE_GREASE) {
  9083. /* hpke init */
  9084. ret = wc_HpkeInit(hpke, ech->kemId, ech->cipherSuite.kdfId,
  9085. ech->cipherSuite.aeadId, NULL);
  9086. if (ret == 0)
  9087. rngRet = ret = wc_InitRng(rng);
  9088. /* create the ephemeralKey */
  9089. if (ret == 0)
  9090. ret = wc_HpkeGenerateKeyPair(hpke, &ephemeralKey, rng);
  9091. /* enc */
  9092. if (ret == 0) {
  9093. ret = wc_HpkeSerializePublicKey(hpke, ephemeralKey, writeBuf_p,
  9094. &ech->encLen);
  9095. writeBuf_p += ech->encLen;
  9096. }
  9097. if (ret == 0) {
  9098. /* innerClientHelloLen */
  9099. c16toa(GREASE_ECH_SIZE + ((writeBuf_p + 2 - writeBuf) % 32),
  9100. writeBuf_p);
  9101. writeBuf_p += 2;
  9102. /* innerClientHello */
  9103. ret = wc_RNG_GenerateBlock(rng, writeBuf_p, GREASE_ECH_SIZE +
  9104. ((writeBuf_p - writeBuf) % 32));
  9105. writeBuf_p += GREASE_ECH_SIZE + ((writeBuf_p - writeBuf) % 32);
  9106. }
  9107. if (rngRet == 0)
  9108. wc_FreeRng(rng);
  9109. if (ephemeralKey != NULL)
  9110. wc_HpkeFreeKey(hpke, hpke->kem, ephemeralKey, hpke->heap);
  9111. }
  9112. else {
  9113. /* write enc to writeBuf_p */
  9114. ret = wc_HpkeSerializePublicKey(ech->hpke, ech->ephemeralKey,
  9115. writeBuf_p, &ech->encLen);
  9116. writeBuf_p += ech->encLen;
  9117. /* innerClientHelloLen */
  9118. c16toa(ech->innerClientHelloLen, writeBuf_p);
  9119. writeBuf_p += 2;
  9120. /* set payload offset for when we finalize */
  9121. ech->outerClientPayload = writeBuf_p;
  9122. /* write zeros for payload */
  9123. XMEMSET(writeBuf_p, 0, ech->innerClientHelloLen);
  9124. writeBuf_p += ech->innerClientHelloLen;
  9125. }
  9126. }
  9127. #ifdef WOLFSSL_SMALL_STACK
  9128. XFREE(hpke, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9129. XFREE(rng, NULL, DYNAMIC_TYPE_RNG);
  9130. #endif
  9131. if (ret == 0)
  9132. *offset += (writeBuf_p - writeBuf);
  9133. return ret;
  9134. }
  9135. /* return the size needed for the ech extension */
  9136. static int TLSX_ECH_GetSize(WOLFSSL_ECH* ech)
  9137. {
  9138. int ret;
  9139. word32 size;
  9140. if (ech->state == ECH_WRITE_GREASE) {
  9141. size = sizeof(ech->type) + sizeof(ech->cipherSuite) +
  9142. sizeof(ech->configId) + sizeof(word16) + ech->encLen +
  9143. sizeof(word16);
  9144. size += GREASE_ECH_SIZE + (size % 32);
  9145. }
  9146. else if (ech->state == ECH_WRITE_NONE ||
  9147. ech->state == ECH_PARSED_INTERNAL) {
  9148. size = 0;
  9149. }
  9150. else if (ech->state == ECH_WRITE_RETRY_CONFIGS) {
  9151. /* get the size of the raw configs */
  9152. ret = GetEchConfigsEx(ech->echConfig, NULL, &size);
  9153. if (ret != LENGTH_ONLY_E)
  9154. return ret;
  9155. }
  9156. else if (ech->type == ECH_TYPE_INNER)
  9157. {
  9158. size = sizeof(ech->type);
  9159. }
  9160. else
  9161. {
  9162. size = sizeof(ech->type) + sizeof(ech->cipherSuite) +
  9163. sizeof(ech->configId) + sizeof(word16) + ech->encLen +
  9164. sizeof(word16) + ech->innerClientHelloLen;
  9165. }
  9166. return (int)size;
  9167. }
  9168. /* return status after attempting to open the hpke encrypted ech extension, if
  9169. * successful the inner client hello will be stored in
  9170. * ech->innerClientHelloLen */
  9171. static int TLSX_ExtractEch(WOLFSSL_ECH* ech, WOLFSSL_EchConfig* echConfig,
  9172. byte* aad, word32 aadLen, void* heap)
  9173. {
  9174. int ret = 0;
  9175. int expectedEncLen;
  9176. int i;
  9177. word32 rawConfigLen = 0;
  9178. byte* info = NULL;
  9179. word32 infoLen = 0;
  9180. if (ech == NULL || echConfig == NULL || aad == NULL)
  9181. return BAD_FUNC_ARG;
  9182. /* verify the kem and key len */
  9183. switch (echConfig->kemId)
  9184. {
  9185. case DHKEM_P256_HKDF_SHA256:
  9186. expectedEncLen = DHKEM_P256_ENC_LEN;
  9187. break;
  9188. case DHKEM_P384_HKDF_SHA384:
  9189. expectedEncLen = DHKEM_P384_ENC_LEN;
  9190. break;
  9191. case DHKEM_P521_HKDF_SHA512:
  9192. expectedEncLen = DHKEM_P521_ENC_LEN;
  9193. break;
  9194. case DHKEM_X25519_HKDF_SHA256:
  9195. expectedEncLen = DHKEM_X25519_ENC_LEN;
  9196. break;
  9197. case DHKEM_X448_HKDF_SHA512:
  9198. expectedEncLen = DHKEM_X448_ENC_LEN;
  9199. break;
  9200. default:
  9201. expectedEncLen = 0;
  9202. break;
  9203. }
  9204. if (expectedEncLen != ech->encLen)
  9205. return BAD_FUNC_ARG;
  9206. /* verify the cipher suite */
  9207. for (i = 0; i < echConfig->numCipherSuites; i++) {
  9208. if (echConfig->cipherSuites[i].kdfId == ech->cipherSuite.kdfId &&
  9209. echConfig->cipherSuites[i].aeadId == ech->cipherSuite.aeadId) {
  9210. break;
  9211. }
  9212. }
  9213. if (i >= echConfig->numCipherSuites) {
  9214. return BAD_FUNC_ARG;
  9215. }
  9216. ech->hpke = (Hpke*)XMALLOC(sizeof(Hpke), heap, DYNAMIC_TYPE_TMP_BUFFER);
  9217. if (ech->hpke == NULL)
  9218. return MEMORY_E;
  9219. ret = wc_HpkeInit(ech->hpke, echConfig->kemId, ech->cipherSuite.kdfId,
  9220. ech->cipherSuite.aeadId, heap);
  9221. /* get the rawConfigLen */
  9222. if (ret == 0)
  9223. ret = GetEchConfig(echConfig, NULL, &rawConfigLen);
  9224. if (ret == LENGTH_ONLY_E)
  9225. ret = 0;
  9226. /* create info */
  9227. if (ret == 0) {
  9228. infoLen = TLS_INFO_CONST_STRING_SZ + 1 + rawConfigLen;
  9229. info = (byte*)XMALLOC(infoLen, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9230. if (info == NULL)
  9231. ret = MEMORY_E;
  9232. else {
  9233. XMEMCPY(info, (byte*)TLS_INFO_CONST_STRING,
  9234. TLS_INFO_CONST_STRING_SZ + 1);
  9235. ret = GetEchConfig(echConfig, info +
  9236. TLS_INFO_CONST_STRING_SZ + 1, &rawConfigLen);
  9237. }
  9238. }
  9239. /* decrypt the ech payload */
  9240. if (ret == 0)
  9241. ret = wc_HpkeOpenBase(ech->hpke, echConfig->receiverPrivkey, ech->enc,
  9242. ech->encLen, info, infoLen, aad, aadLen, ech->outerClientPayload,
  9243. ech->innerClientHelloLen,
  9244. ech->innerClientHello + HANDSHAKE_HEADER_SZ);
  9245. if (ret != 0) {
  9246. XFREE(ech->hpke, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9247. ech->hpke = NULL;
  9248. }
  9249. if (info != NULL)
  9250. XFREE(info, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9251. return ret;
  9252. }
  9253. /* parse the ech extension, if internal update ech->state and return, if
  9254. * external attempt to extract the inner client_hello, return the status */
  9255. static int TLSX_ECH_Parse(WOLFSSL* ssl, const byte* readBuf, word16 size,
  9256. byte msgType)
  9257. {
  9258. int ret = 0;
  9259. int i;
  9260. TLSX* echX;
  9261. WOLFSSL_ECH* ech;
  9262. WOLFSSL_EchConfig* echConfig;
  9263. byte* aadCopy;
  9264. byte* readBuf_p = (byte*)readBuf;
  9265. WOLFSSL_MSG("TLSX_ECH_Parse");
  9266. if (size == 0)
  9267. return BAD_FUNC_ARG;
  9268. if (msgType == encrypted_extensions) {
  9269. ret = wolfSSL_SetEchConfigs(ssl, readBuf, size);
  9270. if (ret == WOLFSSL_SUCCESS)
  9271. ret = 0;
  9272. }
  9273. else if (msgType == client_hello && ssl->ctx->echConfigs != NULL) {
  9274. echX = TLSX_Find(ssl->extensions, TLSX_ECH);
  9275. if (echX == NULL)
  9276. return BAD_FUNC_ARG;
  9277. ech = (WOLFSSL_ECH*)echX->data;
  9278. /* read the ech parameters before the payload */
  9279. ech->type = *readBuf_p;
  9280. readBuf_p++;
  9281. if (ech->type == ECH_TYPE_INNER) {
  9282. ech->state = ECH_PARSED_INTERNAL;
  9283. return 0;
  9284. }
  9285. /* technically the payload would only be 1 byte at this length */
  9286. if (size < 11 + ech->encLen)
  9287. return BAD_FUNC_ARG;
  9288. ato16(readBuf_p, &ech->cipherSuite.kdfId);
  9289. readBuf_p += 2;
  9290. ato16(readBuf_p, &ech->cipherSuite.aeadId);
  9291. readBuf_p += 2;
  9292. ech->configId = *readBuf_p;
  9293. readBuf_p++;
  9294. ato16(readBuf_p, &ech->encLen);
  9295. readBuf_p += 2;
  9296. if (ech->encLen > HPKE_Npk_MAX)
  9297. return BAD_FUNC_ARG;
  9298. XMEMCPY(ech->enc, readBuf_p, ech->encLen);
  9299. readBuf_p += ech->encLen;
  9300. ato16(readBuf_p, &ech->innerClientHelloLen);
  9301. ech->innerClientHelloLen -= AES_BLOCK_SIZE;
  9302. readBuf_p += 2;
  9303. ech->outerClientPayload = readBuf_p;
  9304. /* make a copy of the aad */
  9305. aadCopy = (byte*)XMALLOC(ech->aadLen, ssl->heap,
  9306. DYNAMIC_TYPE_TMP_BUFFER);
  9307. if (aadCopy == NULL)
  9308. return MEMORY_E;
  9309. XMEMCPY(aadCopy, ech->aad, ech->aadLen);
  9310. /* set the ech payload of the copy to zeros */
  9311. XMEMSET(aadCopy + (readBuf_p - ech->aad), 0,
  9312. ech->innerClientHelloLen + AES_BLOCK_SIZE);
  9313. /* allocate the inner payload buffer */
  9314. ech->innerClientHello =
  9315. (byte*)XMALLOC(ech->innerClientHelloLen + HANDSHAKE_HEADER_SZ,
  9316. ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  9317. if (ech->innerClientHello == NULL) {
  9318. XFREE(aadCopy, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  9319. return MEMORY_E;
  9320. }
  9321. /* first check if the config id matches */
  9322. echConfig = ssl->ctx->echConfigs;
  9323. while (echConfig != NULL) {
  9324. /* decrypt with this config */
  9325. if (echConfig->configId == ech->configId) {
  9326. ret = TLSX_ExtractEch(ech, echConfig, aadCopy, ech->aadLen,
  9327. ssl->heap);
  9328. break;
  9329. }
  9330. echConfig = echConfig->next;
  9331. }
  9332. /* try to decrypt with all configs */
  9333. if (echConfig == NULL || ret != 0) {
  9334. echConfig = ssl->ctx->echConfigs;
  9335. while (echConfig != NULL) {
  9336. ret = TLSX_ExtractEch(ech, echConfig, aadCopy, ech->aadLen,
  9337. ssl->heap);
  9338. if (ret== 0)
  9339. break;
  9340. echConfig = echConfig->next;
  9341. }
  9342. }
  9343. /* if we failed to extract */
  9344. if (ret != 0) {
  9345. XFREE(ech->innerClientHello, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  9346. ech->innerClientHello = NULL;
  9347. ech->state = ECH_WRITE_RETRY_CONFIGS;
  9348. }
  9349. else {
  9350. i = 0;
  9351. /* decrement until before the padding */
  9352. while (ech->innerClientHello[ech->innerClientHelloLen +
  9353. HANDSHAKE_HEADER_SZ - i - 1] != ECH_TYPE_INNER) {
  9354. i++;
  9355. }
  9356. /* subtract the length of the padding from the length */
  9357. ech->innerClientHelloLen -= i;
  9358. }
  9359. XFREE(aadCopy, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  9360. return 0;
  9361. }
  9362. return ret;
  9363. }
  9364. /* free the ech struct and the dynamic buffer it uses */
  9365. static void TLSX_ECH_Free(WOLFSSL_ECH* ech, void* heap)
  9366. {
  9367. if (ech->innerClientHello != NULL)
  9368. XFREE(ech->innerClientHello, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9369. if (ech->ephemeralKey != NULL)
  9370. wc_HpkeFreeKey(ech->hpke, ech->hpke->kem, ech->ephemeralKey,
  9371. ech->hpke->heap);
  9372. if (ech->hpke != NULL)
  9373. XFREE(ech->hpke, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9374. XFREE(ech, heap, DYNAMIC_TYPE_TMP_BUFFER);
  9375. (void)heap;
  9376. }
  9377. /* encrypt the client hello and store it in ech->outerClientPayload, return
  9378. * status */
  9379. int TLSX_FinalizeEch(WOLFSSL_ECH* ech, byte* aad, word32 aadLen)
  9380. {
  9381. int ret;
  9382. void* receiverPubkey = NULL;
  9383. byte* info;
  9384. int infoLen;
  9385. byte* aadCopy;
  9386. /* import the server public key */
  9387. ret = wc_HpkeDeserializePublicKey(ech->hpke, &receiverPubkey,
  9388. ech->echConfig->receiverPubkey, ech->encLen);
  9389. if (ret == 0) {
  9390. /* create info */
  9391. infoLen = TLS_INFO_CONST_STRING_SZ + 1 + ech->echConfig->rawLen;
  9392. info = (byte*)XMALLOC(infoLen, ech->hpke->heap,
  9393. DYNAMIC_TYPE_TMP_BUFFER);
  9394. if (info == NULL)
  9395. ret = MEMORY_E;
  9396. if (ret == 0) {
  9397. /* puts the null byte in for me */
  9398. XMEMCPY(info, (byte*)TLS_INFO_CONST_STRING, TLS_INFO_CONST_STRING_SZ
  9399. + 1);
  9400. XMEMCPY(info + TLS_INFO_CONST_STRING_SZ + 1, ech->echConfig->raw,
  9401. ech->echConfig->rawLen);
  9402. /* make a copy of the aad since we overwrite it */
  9403. aadCopy = (byte*)XMALLOC(aadLen, ech->hpke->heap,
  9404. DYNAMIC_TYPE_TMP_BUFFER);
  9405. if (aadCopy == NULL) {
  9406. XFREE(info, ech->hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  9407. ret = MEMORY_E;
  9408. }
  9409. }
  9410. if (ret == 0) {
  9411. XMEMCPY(aadCopy, aad, aadLen);
  9412. /* seal the payload */
  9413. ret = wc_HpkeSealBase(ech->hpke, ech->ephemeralKey, receiverPubkey,
  9414. info, infoLen, aadCopy, aadLen, ech->innerClientHello,
  9415. ech->innerClientHelloLen - ech->hpke->Nt,
  9416. ech->outerClientPayload);
  9417. XFREE(info, ech->hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  9418. XFREE(aadCopy, ech->hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
  9419. }
  9420. }
  9421. if (receiverPubkey != NULL)
  9422. wc_HpkeFreeKey(ech->hpke, ech->hpke->kem, receiverPubkey,
  9423. ech->hpke->heap);
  9424. return ret;
  9425. }
  9426. #define GREASE_ECH_USE TLSX_GreaseECH_Use
  9427. #define ECH_USE TLSX_ECH_Use
  9428. #define SERVER_ECH_USE TLSX_ServerECH_Use
  9429. #define ECH_WRITE TLSX_ECH_Write
  9430. #define ECH_GET_SIZE TLSX_ECH_GetSize
  9431. #define ECH_PARSE TLSX_ECH_Parse
  9432. #define ECH_FREE TLSX_ECH_Free
  9433. #endif
  9434. /** Releases all extensions in the provided list. */
  9435. void TLSX_FreeAll(TLSX* list, void* heap)
  9436. {
  9437. TLSX* extension;
  9438. while ((extension = list)) {
  9439. list = extension->next;
  9440. switch (extension->type) {
  9441. #ifdef HAVE_SNI
  9442. case TLSX_SERVER_NAME:
  9443. SNI_FREE_ALL((SNI*)extension->data, heap);
  9444. break;
  9445. #endif
  9446. case TLSX_TRUSTED_CA_KEYS:
  9447. TCA_FREE_ALL((TCA*)extension->data, heap);
  9448. break;
  9449. case TLSX_MAX_FRAGMENT_LENGTH:
  9450. MFL_FREE_ALL(extension->data, heap);
  9451. break;
  9452. case TLSX_EXTENDED_MASTER_SECRET:
  9453. case TLSX_TRUNCATED_HMAC:
  9454. /* Nothing to do. */
  9455. break;
  9456. case TLSX_SUPPORTED_GROUPS:
  9457. EC_FREE_ALL((SupportedCurve*)extension->data, heap);
  9458. break;
  9459. case TLSX_EC_POINT_FORMATS:
  9460. PF_FREE_ALL((PointFormat*)extension->data, heap);
  9461. break;
  9462. case TLSX_STATUS_REQUEST:
  9463. CSR_FREE_ALL((CertificateStatusRequest*)extension->data, heap);
  9464. break;
  9465. case TLSX_STATUS_REQUEST_V2:
  9466. CSR2_FREE_ALL((CertificateStatusRequestItemV2*)extension->data,
  9467. heap);
  9468. break;
  9469. case TLSX_RENEGOTIATION_INFO:
  9470. SCR_FREE_ALL(extension->data, heap);
  9471. break;
  9472. case TLSX_SESSION_TICKET:
  9473. WOLF_STK_FREE(extension->data, heap);
  9474. break;
  9475. case TLSX_APPLICATION_LAYER_PROTOCOL:
  9476. ALPN_FREE_ALL((ALPN*)extension->data, heap);
  9477. break;
  9478. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9479. case TLSX_SIGNATURE_ALGORITHMS:
  9480. SA_FREE_ALL((SignatureAlgorithms*)extension->data, heap);
  9481. break;
  9482. #endif
  9483. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  9484. case TLSX_ENCRYPT_THEN_MAC:
  9485. break;
  9486. #endif
  9487. #ifdef WOLFSSL_TLS13
  9488. case TLSX_SUPPORTED_VERSIONS:
  9489. break;
  9490. #ifdef WOLFSSL_SEND_HRR_COOKIE
  9491. case TLSX_COOKIE:
  9492. CKE_FREE_ALL((Cookie*)extension->data, heap);
  9493. break;
  9494. #endif
  9495. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9496. case TLSX_PRE_SHARED_KEY:
  9497. PSK_FREE_ALL((PreSharedKey*)extension->data, heap);
  9498. break;
  9499. case TLSX_PSK_KEY_EXCHANGE_MODES:
  9500. break;
  9501. #endif
  9502. #ifdef WOLFSSL_EARLY_DATA
  9503. case TLSX_EARLY_DATA:
  9504. break;
  9505. #endif
  9506. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  9507. case TLSX_POST_HANDSHAKE_AUTH:
  9508. break;
  9509. #endif
  9510. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9511. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  9512. break;
  9513. #endif
  9514. case TLSX_KEY_SHARE:
  9515. KS_FREE_ALL((KeyShareEntry*)extension->data, heap);
  9516. break;
  9517. #endif
  9518. #ifdef WOLFSSL_SRTP
  9519. case TLSX_USE_SRTP:
  9520. SRTP_FREE((TlsxSrtp*)extension->data, heap);
  9521. break;
  9522. #endif
  9523. #ifdef WOLFSSL_QUIC
  9524. case TLSX_KEY_QUIC_TP_PARAMS:
  9525. FALL_THROUGH;
  9526. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  9527. QTP_FREE((QuicTransportParam*)extension->data, heap);
  9528. break;
  9529. #endif
  9530. #ifdef WOLFSSL_DTLS_CID
  9531. case TLSX_CONNECTION_ID:
  9532. CID_FREE((byte*)extension->data, heap);
  9533. break;
  9534. #endif /* WOLFSSL_DTLS_CID */
  9535. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  9536. case TLSX_ECH:
  9537. ECH_FREE((WOLFSSL_ECH*)extension->data, heap);
  9538. break;
  9539. #endif
  9540. default:
  9541. break;
  9542. }
  9543. XFREE(extension, heap, DYNAMIC_TYPE_TLSX);
  9544. }
  9545. (void)heap;
  9546. }
  9547. /** Checks if the tls extensions are supported based on the protocol version. */
  9548. int TLSX_SupportExtensions(WOLFSSL* ssl) {
  9549. return ssl && (IsTLS(ssl) || ssl->version.major == DTLS_MAJOR);
  9550. }
  9551. /** Tells the buffered size of the extensions in a list. */
  9552. static int TLSX_GetSize(TLSX* list, byte* semaphore, byte msgType,
  9553. word16* pLength)
  9554. {
  9555. int ret = 0;
  9556. TLSX* extension;
  9557. word16 length = 0;
  9558. byte isRequest = (msgType == client_hello ||
  9559. msgType == certificate_request);
  9560. while ((extension = list)) {
  9561. list = extension->next;
  9562. /* only extensions marked as response are sent back to the client. */
  9563. if (!isRequest && !extension->resp)
  9564. continue; /* skip! */
  9565. /* ssl level extensions are expected to override ctx level ones. */
  9566. if (!IS_OFF(semaphore, TLSX_ToSemaphore(extension->type)))
  9567. continue; /* skip! */
  9568. /* extension type + extension data length. */
  9569. length += HELLO_EXT_TYPE_SZ + OPAQUE16_LEN;
  9570. switch (extension->type) {
  9571. #ifdef HAVE_SNI
  9572. case TLSX_SERVER_NAME:
  9573. /* SNI only sends the name on the request. */
  9574. if (isRequest)
  9575. length += SNI_GET_SIZE((SNI*)extension->data);
  9576. break;
  9577. #endif
  9578. case TLSX_TRUSTED_CA_KEYS:
  9579. /* TCA only sends the list on the request. */
  9580. if (isRequest)
  9581. length += TCA_GET_SIZE((TCA*)extension->data);
  9582. break;
  9583. case TLSX_MAX_FRAGMENT_LENGTH:
  9584. length += MFL_GET_SIZE(extension->data);
  9585. break;
  9586. case TLSX_EXTENDED_MASTER_SECRET:
  9587. case TLSX_TRUNCATED_HMAC:
  9588. /* always empty. */
  9589. break;
  9590. case TLSX_SUPPORTED_GROUPS:
  9591. length += EC_GET_SIZE((SupportedCurve*)extension->data);
  9592. break;
  9593. case TLSX_EC_POINT_FORMATS:
  9594. length += PF_GET_SIZE((PointFormat*)extension->data);
  9595. break;
  9596. case TLSX_STATUS_REQUEST:
  9597. length += CSR_GET_SIZE(
  9598. (CertificateStatusRequest*)extension->data, isRequest);
  9599. break;
  9600. case TLSX_STATUS_REQUEST_V2:
  9601. length += CSR2_GET_SIZE(
  9602. (CertificateStatusRequestItemV2*)extension->data,
  9603. isRequest);
  9604. break;
  9605. case TLSX_RENEGOTIATION_INFO:
  9606. length += SCR_GET_SIZE((SecureRenegotiation*)extension->data,
  9607. isRequest);
  9608. break;
  9609. case TLSX_SESSION_TICKET:
  9610. length += WOLF_STK_GET_SIZE((SessionTicket*)extension->data,
  9611. isRequest);
  9612. break;
  9613. case TLSX_APPLICATION_LAYER_PROTOCOL:
  9614. length += ALPN_GET_SIZE((ALPN*)extension->data);
  9615. break;
  9616. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9617. case TLSX_SIGNATURE_ALGORITHMS:
  9618. length += SA_GET_SIZE(extension->data);
  9619. break;
  9620. #endif
  9621. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  9622. case TLSX_ENCRYPT_THEN_MAC:
  9623. ret = ETM_GET_SIZE(msgType, &length);
  9624. break;
  9625. #endif /* HAVE_ENCRYPT_THEN_MAC */
  9626. #ifdef WOLFSSL_TLS13
  9627. case TLSX_SUPPORTED_VERSIONS:
  9628. ret = SV_GET_SIZE(extension->data, msgType, &length);
  9629. break;
  9630. #ifdef WOLFSSL_SEND_HRR_COOKIE
  9631. case TLSX_COOKIE:
  9632. ret = CKE_GET_SIZE((Cookie*)extension->data, msgType, &length);
  9633. break;
  9634. #endif
  9635. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9636. case TLSX_PRE_SHARED_KEY:
  9637. ret = PSK_GET_SIZE((PreSharedKey*)extension->data, msgType,
  9638. &length);
  9639. break;
  9640. case TLSX_PSK_KEY_EXCHANGE_MODES:
  9641. ret = PKM_GET_SIZE((byte)extension->val, msgType, &length);
  9642. break;
  9643. #endif
  9644. #ifdef WOLFSSL_EARLY_DATA
  9645. case TLSX_EARLY_DATA:
  9646. ret = EDI_GET_SIZE(msgType, &length);
  9647. break;
  9648. #endif
  9649. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  9650. case TLSX_POST_HANDSHAKE_AUTH:
  9651. ret = PHA_GET_SIZE(msgType, &length);
  9652. break;
  9653. #endif
  9654. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9655. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  9656. length += SAC_GET_SIZE(extension->data);
  9657. break;
  9658. #endif
  9659. case TLSX_KEY_SHARE:
  9660. length += KS_GET_SIZE((KeyShareEntry*)extension->data, msgType);
  9661. break;
  9662. #endif
  9663. #ifdef WOLFSSL_SRTP
  9664. case TLSX_USE_SRTP:
  9665. length += SRTP_GET_SIZE((TlsxSrtp*)extension->data);
  9666. break;
  9667. #endif
  9668. #ifdef WOLFSSL_QUIC
  9669. case TLSX_KEY_QUIC_TP_PARAMS:
  9670. FALL_THROUGH; /* followed by */
  9671. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  9672. length += QTP_GET_SIZE(extension);
  9673. break;
  9674. #endif
  9675. #ifdef WOLFSSL_DTLS_CID
  9676. case TLSX_CONNECTION_ID:
  9677. length += CID_GET_SIZE((byte*)extension->data);
  9678. break;
  9679. #endif /* WOLFSSL_DTLS_CID */
  9680. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  9681. case TLSX_ECH:
  9682. length += ECH_GET_SIZE((WOLFSSL_ECH*)extension->data);
  9683. break;
  9684. #endif
  9685. default:
  9686. break;
  9687. }
  9688. /* marks the extension as processed so ctx level */
  9689. /* extensions don't overlap with ssl level ones. */
  9690. TURN_ON(semaphore, TLSX_ToSemaphore(extension->type));
  9691. }
  9692. *pLength += length;
  9693. return ret;
  9694. }
  9695. /** Writes the extensions of a list in a buffer. */
  9696. static int TLSX_Write(TLSX* list, byte* output, byte* semaphore,
  9697. byte msgType, word16* pOffset)
  9698. {
  9699. int ret = 0;
  9700. TLSX* extension;
  9701. word16 offset = 0;
  9702. word16 length_offset = 0;
  9703. byte isRequest = (msgType == client_hello ||
  9704. msgType == certificate_request);
  9705. while ((extension = list)) {
  9706. list = extension->next;
  9707. /* only extensions marked as response are written in a response. */
  9708. if (!isRequest && !extension->resp)
  9709. continue; /* skip! */
  9710. /* ssl level extensions are expected to override ctx level ones. */
  9711. if (!IS_OFF(semaphore, TLSX_ToSemaphore(extension->type)))
  9712. continue; /* skip! */
  9713. /* writes extension type. */
  9714. c16toa(extension->type, output + offset);
  9715. offset += HELLO_EXT_TYPE_SZ + OPAQUE16_LEN;
  9716. length_offset = offset;
  9717. /* extension data should be written internally. */
  9718. switch (extension->type) {
  9719. #ifdef HAVE_SNI
  9720. case TLSX_SERVER_NAME:
  9721. if (isRequest) {
  9722. WOLFSSL_MSG("SNI extension to write");
  9723. offset += SNI_WRITE((SNI*)extension->data, output + offset);
  9724. }
  9725. break;
  9726. #endif
  9727. case TLSX_TRUSTED_CA_KEYS:
  9728. WOLFSSL_MSG("Trusted CA Indication extension to write");
  9729. if (isRequest) {
  9730. offset += TCA_WRITE((TCA*)extension->data, output + offset);
  9731. }
  9732. break;
  9733. case TLSX_MAX_FRAGMENT_LENGTH:
  9734. WOLFSSL_MSG("Max Fragment Length extension to write");
  9735. offset += MFL_WRITE((byte*)extension->data, output + offset);
  9736. break;
  9737. case TLSX_EXTENDED_MASTER_SECRET:
  9738. WOLFSSL_MSG("Extended Master Secret");
  9739. /* always empty. */
  9740. break;
  9741. case TLSX_TRUNCATED_HMAC:
  9742. WOLFSSL_MSG("Truncated HMAC extension to write");
  9743. /* always empty. */
  9744. break;
  9745. case TLSX_SUPPORTED_GROUPS:
  9746. WOLFSSL_MSG("Supported Groups extension to write");
  9747. offset += EC_WRITE((SupportedCurve*)extension->data,
  9748. output + offset);
  9749. break;
  9750. case TLSX_EC_POINT_FORMATS:
  9751. WOLFSSL_MSG("Point Formats extension to write");
  9752. offset += PF_WRITE((PointFormat*)extension->data,
  9753. output + offset);
  9754. break;
  9755. case TLSX_STATUS_REQUEST:
  9756. WOLFSSL_MSG("Certificate Status Request extension to write");
  9757. offset += CSR_WRITE((CertificateStatusRequest*)extension->data,
  9758. output + offset, isRequest);
  9759. break;
  9760. case TLSX_STATUS_REQUEST_V2:
  9761. WOLFSSL_MSG("Certificate Status Request v2 extension to write");
  9762. offset += CSR2_WRITE(
  9763. (CertificateStatusRequestItemV2*)extension->data,
  9764. output + offset, isRequest);
  9765. break;
  9766. case TLSX_RENEGOTIATION_INFO:
  9767. WOLFSSL_MSG("Secure Renegotiation extension to write");
  9768. offset += SCR_WRITE((SecureRenegotiation*)extension->data,
  9769. output + offset, isRequest);
  9770. break;
  9771. case TLSX_SESSION_TICKET:
  9772. WOLFSSL_MSG("Session Ticket extension to write");
  9773. offset += WOLF_STK_WRITE((SessionTicket*)extension->data,
  9774. output + offset, isRequest);
  9775. break;
  9776. case TLSX_APPLICATION_LAYER_PROTOCOL:
  9777. WOLFSSL_MSG("ALPN extension to write");
  9778. offset += ALPN_WRITE((ALPN*)extension->data, output + offset);
  9779. break;
  9780. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9781. case TLSX_SIGNATURE_ALGORITHMS:
  9782. WOLFSSL_MSG("Signature Algorithms extension to write");
  9783. offset += SA_WRITE(extension->data, output + offset);
  9784. break;
  9785. #endif
  9786. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  9787. case TLSX_ENCRYPT_THEN_MAC:
  9788. WOLFSSL_MSG("Encrypt-Then-Mac extension to write");
  9789. ret = ETM_WRITE(extension->data, output, msgType, &offset);
  9790. break;
  9791. #endif /* HAVE_ENCRYPT_THEN_MAC */
  9792. #ifdef WOLFSSL_TLS13
  9793. case TLSX_SUPPORTED_VERSIONS:
  9794. WOLFSSL_MSG("Supported Versions extension to write");
  9795. ret = SV_WRITE(extension->data, output + offset, msgType, &offset);
  9796. break;
  9797. #ifdef WOLFSSL_SEND_HRR_COOKIE
  9798. case TLSX_COOKIE:
  9799. WOLFSSL_MSG("Cookie extension to write");
  9800. ret = CKE_WRITE((Cookie*)extension->data, output + offset,
  9801. msgType, &offset);
  9802. break;
  9803. #endif
  9804. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9805. case TLSX_PRE_SHARED_KEY:
  9806. WOLFSSL_MSG("Pre-Shared Key extension to write");
  9807. ret = PSK_WRITE((PreSharedKey*)extension->data, output + offset,
  9808. msgType, &offset);
  9809. break;
  9810. case TLSX_PSK_KEY_EXCHANGE_MODES:
  9811. WOLFSSL_MSG("PSK Key Exchange Modes extension to write");
  9812. ret = PKM_WRITE((byte)extension->val, output + offset, msgType,
  9813. &offset);
  9814. break;
  9815. #endif
  9816. #ifdef WOLFSSL_EARLY_DATA
  9817. case TLSX_EARLY_DATA:
  9818. WOLFSSL_MSG("Early Data extension to write");
  9819. ret = EDI_WRITE(extension->val, output + offset, msgType,
  9820. &offset);
  9821. break;
  9822. #endif
  9823. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  9824. case TLSX_POST_HANDSHAKE_AUTH:
  9825. WOLFSSL_MSG("Post-Handshake Authentication extension to write");
  9826. ret = PHA_WRITE(output + offset, msgType, &offset);
  9827. break;
  9828. #endif
  9829. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9830. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  9831. WOLFSSL_MSG("Signature Algorithms extension to write");
  9832. offset += SAC_WRITE(extension->data, output + offset);
  9833. break;
  9834. #endif
  9835. case TLSX_KEY_SHARE:
  9836. WOLFSSL_MSG("Key Share extension to write");
  9837. offset += KS_WRITE((KeyShareEntry*)extension->data,
  9838. output + offset, msgType);
  9839. break;
  9840. #endif
  9841. #ifdef WOLFSSL_SRTP
  9842. case TLSX_USE_SRTP:
  9843. offset += SRTP_WRITE((TlsxSrtp*)extension->data, output+offset);
  9844. break;
  9845. #endif
  9846. #ifdef WOLFSSL_QUIC
  9847. case TLSX_KEY_QUIC_TP_PARAMS:
  9848. FALL_THROUGH;
  9849. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  9850. WOLFSSL_MSG("QUIC transport parameter to write");
  9851. offset += QTP_WRITE((QuicTransportParam*)extension->data,
  9852. output + offset);
  9853. break;
  9854. #endif
  9855. #ifdef WOLFSSL_DTLS_CID
  9856. case TLSX_CONNECTION_ID:
  9857. offset += CID_WRITE((byte*)extension->data, output+offset);
  9858. break;
  9859. #endif /* WOLFSSL_DTLS_CID */
  9860. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  9861. case TLSX_ECH:
  9862. ret = ECH_WRITE((WOLFSSL_ECH*)extension->data,
  9863. output + offset, &offset);
  9864. break;
  9865. #endif
  9866. default:
  9867. break;
  9868. }
  9869. /* writes extension data length. */
  9870. c16toa(offset - length_offset, output + length_offset - OPAQUE16_LEN);
  9871. /* marks the extension as processed so ctx level */
  9872. /* extensions don't overlap with ssl level ones. */
  9873. TURN_ON(semaphore, TLSX_ToSemaphore(extension->type));
  9874. /* if we encountered an error propagate it */
  9875. if (ret != 0)
  9876. break;
  9877. }
  9878. *pOffset += offset;
  9879. return ret;
  9880. }
  9881. #ifdef HAVE_SUPPORTED_CURVES
  9882. /* Populates the default supported groups / curves */
  9883. static int TLSX_PopulateSupportedGroups(WOLFSSL* ssl, TLSX** extensions)
  9884. {
  9885. int ret = WOLFSSL_SUCCESS;
  9886. #ifdef WOLFSSL_TLS13
  9887. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9888. if (ssl->options.resuming && ssl->session->namedGroup != 0) {
  9889. return TLSX_UseSupportedCurve(extensions, ssl->session->namedGroup,
  9890. ssl->heap);
  9891. }
  9892. #endif
  9893. if (ssl->numGroups != 0) {
  9894. int i;
  9895. for (i = 0; i < ssl->numGroups; i++) {
  9896. ret = TLSX_UseSupportedCurve(extensions, ssl->group[i], ssl->heap);
  9897. if (ret != WOLFSSL_SUCCESS)
  9898. return ret;
  9899. }
  9900. return WOLFSSL_SUCCESS;
  9901. }
  9902. #endif /* WOLFSSL_TLS13 */
  9903. #if defined(HAVE_ECC)
  9904. /* list in order by strength, since not all servers choose by strength */
  9905. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  9906. #ifndef NO_ECC_SECP
  9907. ret = TLSX_UseSupportedCurve(extensions,
  9908. WOLFSSL_ECC_SECP521R1, ssl->heap);
  9909. if (ret != WOLFSSL_SUCCESS) return ret;
  9910. #endif
  9911. #endif
  9912. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  9913. #ifdef HAVE_ECC_BRAINPOOL
  9914. ret = TLSX_UseSupportedCurve(extensions,
  9915. WOLFSSL_ECC_BRAINPOOLP512R1, ssl->heap);
  9916. if (ret != WOLFSSL_SUCCESS) return ret;
  9917. #endif
  9918. #endif
  9919. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  9920. #ifndef NO_ECC_SECP
  9921. ret = TLSX_UseSupportedCurve(extensions,
  9922. WOLFSSL_ECC_SECP384R1, ssl->heap);
  9923. if (ret != WOLFSSL_SUCCESS) return ret;
  9924. #endif
  9925. #ifdef HAVE_ECC_BRAINPOOL
  9926. ret = TLSX_UseSupportedCurve(extensions,
  9927. WOLFSSL_ECC_BRAINPOOLP384R1, ssl->heap);
  9928. if (ret != WOLFSSL_SUCCESS) return ret;
  9929. #endif
  9930. #endif
  9931. #endif /* HAVE_ECC */
  9932. #ifndef HAVE_FIPS
  9933. #if defined(HAVE_CURVE448) && ECC_MIN_KEY_SZ <= 448
  9934. ret = TLSX_UseSupportedCurve(extensions,
  9935. WOLFSSL_ECC_X448, ssl->heap);
  9936. if (ret != WOLFSSL_SUCCESS) return ret;
  9937. #endif
  9938. #endif /* HAVE_FIPS */
  9939. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  9940. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  9941. #ifndef NO_ECC_SECP
  9942. ret = TLSX_UseSupportedCurve(extensions,
  9943. WOLFSSL_ECC_SECP256R1, ssl->heap);
  9944. if (ret != WOLFSSL_SUCCESS) return ret;
  9945. #endif
  9946. #ifdef HAVE_ECC_KOBLITZ
  9947. ret = TLSX_UseSupportedCurve(extensions,
  9948. WOLFSSL_ECC_SECP256K1, ssl->heap);
  9949. if (ret != WOLFSSL_SUCCESS) return ret;
  9950. #endif
  9951. #ifdef HAVE_ECC_BRAINPOOL
  9952. ret = TLSX_UseSupportedCurve(extensions,
  9953. WOLFSSL_ECC_BRAINPOOLP256R1, ssl->heap);
  9954. if (ret != WOLFSSL_SUCCESS) return ret;
  9955. #endif
  9956. #endif
  9957. #endif /* HAVE_ECC */
  9958. #ifndef HAVE_FIPS
  9959. #if defined(HAVE_CURVE25519) && ECC_MIN_KEY_SZ <= 256
  9960. ret = TLSX_UseSupportedCurve(extensions,
  9961. WOLFSSL_ECC_X25519, ssl->heap);
  9962. if (ret != WOLFSSL_SUCCESS) return ret;
  9963. #endif
  9964. #endif /* HAVE_FIPS */
  9965. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  9966. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  9967. #ifndef NO_ECC_SECP
  9968. ret = TLSX_UseSupportedCurve(extensions,
  9969. WOLFSSL_ECC_SECP224R1, ssl->heap);
  9970. if (ret != WOLFSSL_SUCCESS) return ret;
  9971. #endif
  9972. #ifdef HAVE_ECC_KOBLITZ
  9973. ret = TLSX_UseSupportedCurve(extensions,
  9974. WOLFSSL_ECC_SECP224K1, ssl->heap);
  9975. if (ret != WOLFSSL_SUCCESS) return ret;
  9976. #endif
  9977. #endif
  9978. #ifndef HAVE_FIPS
  9979. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  9980. #ifndef NO_ECC_SECP
  9981. ret = TLSX_UseSupportedCurve(extensions,
  9982. WOLFSSL_ECC_SECP192R1, ssl->heap);
  9983. if (ret != WOLFSSL_SUCCESS) return ret;
  9984. #endif
  9985. #ifdef HAVE_ECC_KOBLITZ
  9986. ret = TLSX_UseSupportedCurve(extensions,
  9987. WOLFSSL_ECC_SECP192K1, ssl->heap);
  9988. if (ret != WOLFSSL_SUCCESS) return ret;
  9989. #endif
  9990. #endif
  9991. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  9992. #ifndef NO_ECC_SECP
  9993. ret = TLSX_UseSupportedCurve(extensions,
  9994. WOLFSSL_ECC_SECP160R1, ssl->heap);
  9995. if (ret != WOLFSSL_SUCCESS) return ret;
  9996. #endif
  9997. #ifdef HAVE_ECC_SECPR2
  9998. ret = TLSX_UseSupportedCurve(extensions,
  9999. WOLFSSL_ECC_SECP160R2, ssl->heap);
  10000. if (ret != WOLFSSL_SUCCESS) return ret;
  10001. #endif
  10002. #ifdef HAVE_ECC_KOBLITZ
  10003. ret = TLSX_UseSupportedCurve(extensions,
  10004. WOLFSSL_ECC_SECP160K1, ssl->heap);
  10005. if (ret != WOLFSSL_SUCCESS) return ret;
  10006. #endif
  10007. #endif
  10008. #endif /* HAVE_FIPS */
  10009. #endif /* HAVE_ECC */
  10010. #ifndef NO_DH
  10011. /* Add FFDHE supported groups. */
  10012. #ifdef HAVE_FFDHE_8192
  10013. if (8192/8 >= ssl->options.minDhKeySz &&
  10014. 8192/8 <= ssl->options.maxDhKeySz) {
  10015. ret = TLSX_UseSupportedCurve(extensions,
  10016. WOLFSSL_FFDHE_8192, ssl->heap);
  10017. if (ret != WOLFSSL_SUCCESS)
  10018. return ret;
  10019. }
  10020. #endif
  10021. #ifdef HAVE_FFDHE_6144
  10022. if (6144/8 >= ssl->options.minDhKeySz &&
  10023. 6144/8 <= ssl->options.maxDhKeySz) {
  10024. ret = TLSX_UseSupportedCurve(extensions,
  10025. WOLFSSL_FFDHE_6144, ssl->heap);
  10026. if (ret != WOLFSSL_SUCCESS)
  10027. return ret;
  10028. }
  10029. #endif
  10030. #ifdef HAVE_FFDHE_4096
  10031. if (4096/8 >= ssl->options.minDhKeySz &&
  10032. 4096/8 <= ssl->options.maxDhKeySz) {
  10033. ret = TLSX_UseSupportedCurve(extensions,
  10034. WOLFSSL_FFDHE_4096, ssl->heap);
  10035. if (ret != WOLFSSL_SUCCESS)
  10036. return ret;
  10037. }
  10038. #endif
  10039. #ifdef HAVE_FFDHE_3072
  10040. if (3072/8 >= ssl->options.minDhKeySz &&
  10041. 3072/8 <= ssl->options.maxDhKeySz) {
  10042. ret = TLSX_UseSupportedCurve(extensions,
  10043. WOLFSSL_FFDHE_3072, ssl->heap);
  10044. if (ret != WOLFSSL_SUCCESS)
  10045. return ret;
  10046. }
  10047. #endif
  10048. #ifdef HAVE_FFDHE_2048
  10049. if (2048/8 >= ssl->options.minDhKeySz &&
  10050. 2048/8 <= ssl->options.maxDhKeySz) {
  10051. ret = TLSX_UseSupportedCurve(extensions,
  10052. WOLFSSL_FFDHE_2048, ssl->heap);
  10053. if (ret != WOLFSSL_SUCCESS)
  10054. return ret;
  10055. }
  10056. #endif
  10057. #endif
  10058. #ifdef HAVE_PQC
  10059. #ifdef WOLFSSL_WC_KYBER
  10060. #ifdef WOLFSSL_KYBER512
  10061. if (ret == WOLFSSL_SUCCESS)
  10062. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL1,
  10063. ssl->heap);
  10064. #endif
  10065. #ifdef WOLFSSL_KYBER768
  10066. if (ret == WOLFSSL_SUCCESS)
  10067. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL3,
  10068. ssl->heap);
  10069. #endif
  10070. #ifdef WOLFSSL_KYBER768
  10071. if (ret == WOLFSSL_SUCCESS)
  10072. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL5,
  10073. ssl->heap);
  10074. #endif
  10075. #elif defined(HAVE_LIBOQS)
  10076. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL1, ssl->heap);
  10077. if (ret == WOLFSSL_SUCCESS)
  10078. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL3,
  10079. ssl->heap);
  10080. if (ret == WOLFSSL_SUCCESS)
  10081. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL5,
  10082. ssl->heap);
  10083. if (ret == WOLFSSL_SUCCESS)
  10084. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P256_KYBER_LEVEL1,
  10085. ssl->heap);
  10086. if (ret == WOLFSSL_SUCCESS)
  10087. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P384_KYBER_LEVEL3,
  10088. ssl->heap);
  10089. if (ret == WOLFSSL_SUCCESS)
  10090. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P521_KYBER_LEVEL5,
  10091. ssl->heap);
  10092. #elif defined(HAVE_PQM4)
  10093. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL1, ssl->heap);
  10094. #endif /* HAVE_LIBOQS */
  10095. #endif /* HAVE_PQC */
  10096. (void)ssl;
  10097. (void)extensions;
  10098. return ret;
  10099. }
  10100. #endif /* HAVE_SUPPORTED_CURVES */
  10101. int TLSX_PopulateExtensions(WOLFSSL* ssl, byte isServer)
  10102. {
  10103. int ret = 0;
  10104. byte* public_key = NULL;
  10105. word16 public_key_len = 0;
  10106. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  10107. int usingPSK = 0;
  10108. #endif
  10109. #if defined(HAVE_SUPPORTED_CURVES) && defined(WOLFSSL_TLS13)
  10110. TLSX* extension = NULL;
  10111. word16 namedGroup = WOLFSSL_NAMED_GROUP_INVALID;
  10112. #endif
  10113. /* server will add extension depending on what is parsed from client */
  10114. if (!isServer) {
  10115. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  10116. if (!ssl->options.disallowEncThenMac) {
  10117. ret = TLSX_EncryptThenMac_Use(ssl);
  10118. if (ret != 0)
  10119. return ret;
  10120. }
  10121. #endif
  10122. #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  10123. defined(HAVE_CURVE448)) && defined(HAVE_SUPPORTED_CURVES)
  10124. if (!ssl->options.userCurves && !ssl->ctx->userCurves) {
  10125. if (TLSX_Find(ssl->ctx->extensions,
  10126. TLSX_SUPPORTED_GROUPS) == NULL) {
  10127. ret = TLSX_PopulateSupportedGroups(ssl, &ssl->extensions);
  10128. if (ret != WOLFSSL_SUCCESS)
  10129. return ret;
  10130. }
  10131. }
  10132. if ((!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade) &&
  10133. TLSX_Find(ssl->ctx->extensions, TLSX_EC_POINT_FORMATS) == NULL &&
  10134. TLSX_Find(ssl->extensions, TLSX_EC_POINT_FORMATS) == NULL) {
  10135. ret = TLSX_UsePointFormat(&ssl->extensions,
  10136. WOLFSSL_EC_PF_UNCOMPRESSED, ssl->heap);
  10137. if (ret != WOLFSSL_SUCCESS)
  10138. return ret;
  10139. }
  10140. #endif /* (HAVE_ECC || CURVE25519 || CURVE448) && HAVE_SUPPORTED_CURVES */
  10141. #ifdef WOLFSSL_SRTP
  10142. if (ssl->options.dtls && ssl->dtlsSrtpProfiles != 0) {
  10143. WOLFSSL_MSG("Adding DTLS SRTP extension");
  10144. if ((ret = TLSX_UseSRTP(&ssl->extensions, ssl->dtlsSrtpProfiles,
  10145. ssl->heap)) != 0) {
  10146. return ret;
  10147. }
  10148. }
  10149. #endif
  10150. } /* is not server */
  10151. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10152. WOLFSSL_MSG("Adding signature algorithms extension");
  10153. if ((ret = TLSX_SetSignatureAlgorithms(&ssl->extensions, ssl, ssl->heap))
  10154. != 0) {
  10155. return ret;
  10156. }
  10157. #else
  10158. ret = 0;
  10159. #endif
  10160. #ifdef WOLFSSL_TLS13
  10161. if (!isServer && IsAtLeastTLSv1_3(ssl->version)) {
  10162. /* Add mandatory TLS v1.3 extension: supported version */
  10163. WOLFSSL_MSG("Adding supported versions extension");
  10164. if ((ret = TLSX_SetSupportedVersions(&ssl->extensions, ssl,
  10165. ssl->heap)) != 0) {
  10166. return ret;
  10167. }
  10168. #if !defined(HAVE_ECC) && !defined(HAVE_CURVE25519) && \
  10169. !defined(HAVE_CURVE448) && defined(HAVE_SUPPORTED_CURVES)
  10170. if (TLSX_Find(ssl->ctx->extensions, TLSX_SUPPORTED_GROUPS) == NULL) {
  10171. /* Put in DH groups for TLS 1.3 only. */
  10172. ret = TLSX_PopulateSupportedGroups(ssl, &ssl->extensions);
  10173. if (ret != WOLFSSL_SUCCESS)
  10174. return ret;
  10175. /* ret value will be overwritten in !NO_PSK case */
  10176. #ifdef NO_PSK
  10177. ret = 0;
  10178. #endif
  10179. }
  10180. #endif /* !(HAVE_ECC || CURVE25519 || CURVE448) && HAVE_SUPPORTED_CURVES */
  10181. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10182. if (ssl->certHashSigAlgoSz > 0) {
  10183. WOLFSSL_MSG("Adding signature algorithms cert extension");
  10184. if ((ret = TLSX_SetSignatureAlgorithmsCert(&ssl->extensions,
  10185. ssl, ssl->heap)) != 0) {
  10186. return ret;
  10187. }
  10188. }
  10189. #endif
  10190. #if defined(HAVE_SUPPORTED_CURVES)
  10191. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  10192. if (extension == NULL) {
  10193. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10194. if (ssl->options.resuming && ssl->session->namedGroup != 0)
  10195. namedGroup = ssl->session->namedGroup;
  10196. else
  10197. #endif
  10198. if (ssl->numGroups > 0) {
  10199. int set = 0;
  10200. int i, j;
  10201. /* try to find the highest element in ssl->group[]
  10202. * that is contained in preferredGroup[].
  10203. */
  10204. namedGroup = preferredGroup[0];
  10205. for (i = 0; i < ssl->numGroups && !set; i++) {
  10206. for (j = 0; preferredGroup[j] != WOLFSSL_NAMED_GROUP_INVALID; j++) {
  10207. if (preferredGroup[j] == ssl->group[i]
  10208. #ifdef HAVE_LIBOQS
  10209. && TLSX_KeyShare_IsSupported(preferredGroup[j])
  10210. #endif
  10211. ) {
  10212. namedGroup = ssl->group[i];
  10213. set = 1;
  10214. break;
  10215. }
  10216. }
  10217. }
  10218. if (!set)
  10219. namedGroup = WOLFSSL_NAMED_GROUP_INVALID;
  10220. }
  10221. else {
  10222. /* Choose the most preferred group. */
  10223. namedGroup = preferredGroup[0];
  10224. #ifdef HAVE_LIBOQS
  10225. if (!TLSX_KeyShare_IsSupported(namedGroup)) {
  10226. int i = 1;
  10227. for (;preferredGroup[i] != WOLFSSL_NAMED_GROUP_INVALID;
  10228. i++) {
  10229. if (TLSX_KeyShare_IsSupported(preferredGroup[i]))
  10230. break;
  10231. }
  10232. namedGroup = preferredGroup[i];
  10233. }
  10234. #endif
  10235. }
  10236. }
  10237. else {
  10238. KeyShareEntry* kse = (KeyShareEntry*)extension->data;
  10239. if (kse)
  10240. namedGroup = kse->group;
  10241. }
  10242. if (namedGroup != WOLFSSL_NAMED_GROUP_INVALID) {
  10243. #ifdef HAVE_PQC
  10244. /* For KEMs, the key share has already been generated, but not
  10245. * if we are resuming. */
  10246. if (!WOLFSSL_NAMED_GROUP_IS_PQC(namedGroup)
  10247. #ifdef HAVE_SESSION_TICKET
  10248. || ssl->options.resuming
  10249. #endif /* HAVE_SESSION_TICKET */
  10250. )
  10251. #endif /* HAVE_PQC */
  10252. {
  10253. ret = TLSX_KeyShare_Use(ssl, namedGroup, 0, NULL, NULL,
  10254. &ssl->extensions);
  10255. }
  10256. if (ret != 0)
  10257. return ret;
  10258. }
  10259. #endif /* HAVE_SUPPORTED_CURVES */
  10260. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10261. TLSX_Remove(&ssl->extensions, TLSX_PRE_SHARED_KEY, ssl->heap);
  10262. #endif
  10263. #if defined(HAVE_SESSION_TICKET)
  10264. if (ssl->options.resuming && ssl->session->ticketLen > 0) {
  10265. WOLFSSL_SESSION* sess = ssl->session;
  10266. #ifdef WOLFSSL_32BIT_MILLI_TIME
  10267. word32 now, milli;
  10268. #else
  10269. word64 now, milli;
  10270. #endif
  10271. if (sess->ticketLen > MAX_PSK_ID_LEN) {
  10272. WOLFSSL_MSG("Session ticket length for PSK ext is too large");
  10273. return BUFFER_ERROR;
  10274. }
  10275. /* Determine the MAC algorithm for the cipher suite used. */
  10276. ssl->options.cipherSuite0 = sess->cipherSuite0;
  10277. ssl->options.cipherSuite = sess->cipherSuite;
  10278. ret = SetCipherSpecs(ssl);
  10279. if (ret != 0)
  10280. return ret;
  10281. now = TimeNowInMilliseconds();
  10282. if (now == 0)
  10283. return GETTIME_ERROR;
  10284. #ifdef WOLFSSL_32BIT_MILLI_TIME
  10285. if (now < sess->ticketSeen)
  10286. milli = (0xFFFFFFFFU - sess->ticketSeen) + 1 + now;
  10287. else
  10288. milli = now - sess->ticketSeen;
  10289. milli += sess->ticketAdd;
  10290. /* Pre-shared key is mandatory extension for resumption. */
  10291. ret = TLSX_PreSharedKey_Use(&ssl->extensions, sess->ticket,
  10292. sess->ticketLen, milli, ssl->specs.mac_algorithm,
  10293. ssl->options.cipherSuite0, ssl->options.cipherSuite, 1,
  10294. NULL, ssl->heap);
  10295. #else
  10296. milli = now - sess->ticketSeen + sess->ticketAdd;
  10297. /* Pre-shared key is mandatory extension for resumption. */
  10298. ret = TLSX_PreSharedKey_Use(&ssl->extensions, sess->ticket,
  10299. sess->ticketLen, (word32)milli, ssl->specs.mac_algorithm,
  10300. ssl->options.cipherSuite0, ssl->options.cipherSuite, 1,
  10301. NULL, ssl->heap);
  10302. #endif
  10303. if (ret != 0)
  10304. return ret;
  10305. usingPSK = 1;
  10306. }
  10307. #endif
  10308. #ifndef NO_PSK
  10309. #ifndef WOLFSSL_PSK_ONE_ID
  10310. if (ssl->options.client_psk_cs_cb != NULL) {
  10311. int i;
  10312. const Suites* suites = WOLFSSL_SUITES(ssl);
  10313. for (i = 0; i < suites->suiteSz; i += 2) {
  10314. byte cipherSuite0 = suites->suites[i + 0];
  10315. byte cipherSuite = suites->suites[i + 1];
  10316. unsigned int keySz;
  10317. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  10318. int cnt = 0;
  10319. #endif
  10320. #ifdef HAVE_NULL_CIPHER
  10321. if (cipherSuite0 == ECC_BYTE ||
  10322. cipherSuite0 == ECDHE_PSK_BYTE) {
  10323. if (cipherSuite != TLS_SHA256_SHA256 &&
  10324. cipherSuite != TLS_SHA384_SHA384) {
  10325. continue;
  10326. }
  10327. }
  10328. else
  10329. #endif
  10330. if (cipherSuite0 != TLS13_BYTE)
  10331. continue;
  10332. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  10333. do {
  10334. ssl->arrays->client_identity[0] = cnt;
  10335. #endif
  10336. ssl->arrays->client_identity[MAX_PSK_ID_LEN] = '\0';
  10337. keySz = ssl->options.client_psk_cs_cb(
  10338. ssl, ssl->arrays->server_hint,
  10339. ssl->arrays->client_identity, MAX_PSK_ID_LEN,
  10340. ssl->arrays->psk_key, MAX_PSK_KEY_LEN,
  10341. GetCipherNameInternal(cipherSuite0, cipherSuite));
  10342. if (keySz > 0) {
  10343. ssl->arrays->psk_keySz = keySz;
  10344. ret = TLSX_PreSharedKey_Use(&ssl->extensions,
  10345. (byte*)ssl->arrays->client_identity,
  10346. (word16)XSTRLEN(ssl->arrays->client_identity),
  10347. 0, SuiteMac(WOLFSSL_SUITES(ssl)->suites + i),
  10348. cipherSuite0, cipherSuite, 0, NULL, ssl->heap);
  10349. if (ret != 0)
  10350. return ret;
  10351. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  10352. cnt++;
  10353. #endif
  10354. }
  10355. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  10356. }
  10357. while (keySz > 0);
  10358. #endif
  10359. }
  10360. usingPSK = 1;
  10361. }
  10362. else
  10363. #endif
  10364. if (ssl->options.client_psk_cb != NULL ||
  10365. ssl->options.client_psk_tls13_cb != NULL) {
  10366. /* Default ciphersuite. */
  10367. byte cipherSuite0 = TLS13_BYTE;
  10368. byte cipherSuite = WOLFSSL_DEF_PSK_CIPHER;
  10369. int cipherSuiteFlags = WOLFSSL_CIPHER_SUITE_FLAG_NONE;
  10370. const char* cipherName = NULL;
  10371. if (ssl->options.client_psk_tls13_cb != NULL) {
  10372. ssl->arrays->psk_keySz = ssl->options.client_psk_tls13_cb(
  10373. ssl, ssl->arrays->server_hint,
  10374. ssl->arrays->client_identity, MAX_PSK_ID_LEN,
  10375. ssl->arrays->psk_key, MAX_PSK_KEY_LEN, &cipherName);
  10376. if (GetCipherSuiteFromName(cipherName, &cipherSuite0,
  10377. &cipherSuite, &cipherSuiteFlags) != 0) {
  10378. return PSK_KEY_ERROR;
  10379. }
  10380. }
  10381. else {
  10382. ssl->arrays->psk_keySz = ssl->options.client_psk_cb(ssl,
  10383. ssl->arrays->server_hint, ssl->arrays->client_identity,
  10384. MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN);
  10385. }
  10386. #if defined(OPENSSL_EXTRA)
  10387. /* OpenSSL treats 0 as a PSK key length of 0
  10388. * and meaning no PSK available.
  10389. */
  10390. if (ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN) {
  10391. return PSK_KEY_ERROR;
  10392. }
  10393. if (ssl->arrays->psk_keySz > 0) {
  10394. #else
  10395. if (ssl->arrays->psk_keySz == 0 ||
  10396. ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN) {
  10397. return PSK_KEY_ERROR;
  10398. }
  10399. #endif
  10400. ssl->arrays->client_identity[MAX_PSK_ID_LEN] = '\0';
  10401. ssl->options.cipherSuite0 = cipherSuite0;
  10402. ssl->options.cipherSuite = cipherSuite;
  10403. (void)cipherSuiteFlags;
  10404. ret = SetCipherSpecs(ssl);
  10405. if (ret != 0)
  10406. return ret;
  10407. ret = TLSX_PreSharedKey_Use(&ssl->extensions,
  10408. (byte*)ssl->arrays->client_identity,
  10409. (word16)XSTRLEN(ssl->arrays->client_identity),
  10410. 0, ssl->specs.mac_algorithm,
  10411. cipherSuite0, cipherSuite, 0,
  10412. NULL, ssl->heap);
  10413. if (ret != 0)
  10414. return ret;
  10415. usingPSK = 1;
  10416. #if defined(OPENSSL_EXTRA)
  10417. }
  10418. #endif
  10419. }
  10420. #endif /* !NO_PSK */
  10421. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10422. /* Some servers do not generate session tickets unless
  10423. * the extension is seen in a non-resume client hello.
  10424. * We used to send it only if we were otherwise using PSK.
  10425. * Now always send it. Define NO_TLSX_PSKKEM_PLAIN_ANNOUNCE
  10426. * to revert to the old behaviour. */
  10427. #ifdef NO_TLSX_PSKKEM_PLAIN_ANNOUNCE
  10428. if (usingPSK)
  10429. #endif
  10430. {
  10431. byte modes = 0;
  10432. (void)usingPSK;
  10433. /* Pre-shared key modes: mandatory extension for resumption. */
  10434. #ifdef HAVE_SUPPORTED_CURVES
  10435. if (!ssl->options.onlyPskDheKe)
  10436. #endif
  10437. {
  10438. modes = 1 << PSK_KE;
  10439. }
  10440. #if !defined(NO_DH) || defined(HAVE_ECC) || \
  10441. defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  10442. if (!ssl->options.noPskDheKe) {
  10443. modes |= 1 << PSK_DHE_KE;
  10444. }
  10445. #endif
  10446. ret = TLSX_PskKeyModes_Use(ssl, modes);
  10447. if (ret != 0)
  10448. return ret;
  10449. }
  10450. #endif
  10451. #if defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  10452. if (!isServer && ssl->options.postHandshakeAuth) {
  10453. ret = TLSX_PostHandAuth_Use(ssl);
  10454. if (ret != 0)
  10455. return ret;
  10456. }
  10457. #endif
  10458. #if defined(HAVE_ECH)
  10459. /* GREASE ECH */
  10460. if (ssl->echConfigs == NULL) {
  10461. ret = GREASE_ECH_USE(&(ssl->extensions), ssl->heap, ssl->rng);
  10462. }
  10463. else if (ssl->echConfigs != NULL) {
  10464. ret = ECH_USE(ssl->echConfigs, &(ssl->extensions), ssl->heap,
  10465. ssl->rng);
  10466. }
  10467. #endif
  10468. }
  10469. #if defined(HAVE_ECH)
  10470. else if (IsAtLeastTLSv1_3(ssl->version)) {
  10471. if (ssl->ctx->echConfigs != NULL) {
  10472. ret = SERVER_ECH_USE(&(ssl->extensions), ssl->heap,
  10473. ssl->ctx->echConfigs);
  10474. if (ret == 0)
  10475. TLSX_SetResponse(ssl, TLSX_ECH);
  10476. }
  10477. }
  10478. #endif
  10479. #endif
  10480. (void)isServer;
  10481. (void)public_key;
  10482. (void)public_key_len;
  10483. (void)ssl;
  10484. return ret;
  10485. }
  10486. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
  10487. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  10488. /* because the size of ech depends on the size of other extensions we need to
  10489. * get the size with ech special and process ech last, return status */
  10490. static int TLSX_GetSizeWithEch(WOLFSSL* ssl, byte* semaphore, byte msgType,
  10491. word16* pLength)
  10492. {
  10493. int ret = 0;
  10494. TLSX* echX = NULL;
  10495. TLSX* serverNameX = NULL;
  10496. TLSX** extensions = NULL;
  10497. #ifdef WOLFSSL_SMALL_STACK
  10498. char* tmpServerName = NULL;
  10499. #else
  10500. char tmpServerName[MAX_PUBLIC_NAME_SZ];
  10501. #endif
  10502. /* calculate the rest of the extensions length with inner ech */
  10503. if (ssl->extensions)
  10504. echX = TLSX_Find(ssl->extensions, TLSX_ECH);
  10505. if (echX == NULL && ssl->ctx && ssl->ctx->extensions)
  10506. echX = TLSX_Find(ssl->ctx->extensions, TLSX_ECH);
  10507. /* if type is outer change sni to public name */
  10508. if (echX != NULL && ((WOLFSSL_ECH*)echX->data)->type == ECH_TYPE_OUTER) {
  10509. if (ssl->extensions) {
  10510. serverNameX = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
  10511. if (serverNameX != NULL)
  10512. extensions = &ssl->extensions;
  10513. }
  10514. if (serverNameX == NULL && ssl->ctx && ssl->ctx->extensions) {
  10515. serverNameX = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
  10516. extensions = &ssl->ctx->extensions;
  10517. }
  10518. /* store the inner server name */
  10519. if (serverNameX != NULL) {
  10520. char* hostName = ((SNI*)serverNameX->data)->data.host_name;
  10521. word32 hostNameSz = (word32)XSTRLEN(hostName) + 1;
  10522. #ifdef WOLFSSL_SMALL_STACK
  10523. tmpServerName = (char*)XMALLOC(hostNameSz, ssl->heap,
  10524. DYNAMIC_TYPE_TMP_BUFFER);
  10525. if (tmpServerName == NULL)
  10526. return MEMORY_E;
  10527. #else
  10528. /* truncate if too long */
  10529. if (hostNameSz > MAX_PUBLIC_NAME_SZ)
  10530. hostNameSz = MAX_PUBLIC_NAME_SZ;
  10531. #endif
  10532. XMEMCPY(tmpServerName, hostName, hostNameSz);
  10533. }
  10534. /* remove the inner server name */
  10535. TLSX_Remove(extensions, TLSX_SERVER_NAME, ssl->heap);
  10536. ret = TLSX_UseSNI(extensions, WOLFSSL_SNI_HOST_NAME,
  10537. ((WOLFSSL_ECH*)echX->data)->echConfig->publicName,
  10538. XSTRLEN(((WOLFSSL_ECH*)echX->data)->echConfig->publicName),
  10539. ssl->heap);
  10540. /* set the public name as the server name */
  10541. if (ret == WOLFSSL_SUCCESS)
  10542. ret = 0;
  10543. }
  10544. if (ret == 0 && ssl->extensions)
  10545. ret = TLSX_GetSize(ssl->extensions, semaphore, msgType, pLength);
  10546. if (ret == 0 && ssl->ctx && ssl->ctx->extensions)
  10547. ret = TLSX_GetSize(ssl->ctx->extensions, semaphore, msgType, pLength);
  10548. if (serverNameX != NULL) {
  10549. /* remove the public name SNI */
  10550. TLSX_Remove(extensions, TLSX_SERVER_NAME, ssl->heap);
  10551. ret = TLSX_UseSNI(extensions, WOLFSSL_SNI_HOST_NAME,
  10552. tmpServerName, XSTRLEN(tmpServerName), ssl->heap);
  10553. /* restore the inner server name */
  10554. if (ret == WOLFSSL_SUCCESS)
  10555. ret = 0;
  10556. }
  10557. #ifdef WOLFSSL_SMALL_STACK
  10558. XFREE(tmpServerName, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  10559. #endif
  10560. return ret;
  10561. }
  10562. #endif
  10563. /** Tells the buffered size of extensions to be sent into the client hello. */
  10564. int TLSX_GetRequestSize(WOLFSSL* ssl, byte msgType, word16* pLength)
  10565. {
  10566. int ret = 0;
  10567. word16 length = 0;
  10568. byte semaphore[SEMAPHORE_SIZE] = {0};
  10569. if (!TLSX_SupportExtensions(ssl))
  10570. return 0;
  10571. if (msgType == client_hello) {
  10572. EC_VALIDATE_REQUEST(ssl, semaphore);
  10573. PF_VALIDATE_REQUEST(ssl, semaphore);
  10574. WOLF_STK_VALIDATE_REQUEST(ssl);
  10575. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10576. if (WOLFSSL_SUITES(ssl)->hashSigAlgoSz == 0)
  10577. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  10578. #endif
  10579. #if defined(WOLFSSL_TLS13)
  10580. if (!IsAtLeastTLSv1_2(ssl))
  10581. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10582. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  10583. if (!IsAtLeastTLSv1_3(ssl->version)) {
  10584. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10585. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10586. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10587. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PSK_KEY_EXCHANGE_MODES));
  10588. #endif
  10589. #ifdef WOLFSSL_EARLY_DATA
  10590. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  10591. #endif
  10592. #ifdef WOLFSSL_SEND_HRR_COOKIE
  10593. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  10594. #endif
  10595. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  10596. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_POST_HANDSHAKE_AUTH));
  10597. #endif
  10598. }
  10599. #endif
  10600. #endif /* WOLFSSL_TLS13 */
  10601. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  10602. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  10603. if (!SSL_CM(ssl)->ocspStaplingEnabled) {
  10604. /* mark already sent, so it won't send it */
  10605. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10606. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  10607. }
  10608. #endif
  10609. }
  10610. #ifdef WOLFSSL_TLS13
  10611. #ifndef NO_CERTS
  10612. else if (msgType == certificate_request) {
  10613. /* Don't send out any extension except those that are turned off. */
  10614. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10615. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10616. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  10617. #endif
  10618. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  10619. * TLSX_CERTIFICATE_AUTHORITIES, OID_FILTERS
  10620. * TLSX_STATUS_REQUEST
  10621. */
  10622. }
  10623. #endif
  10624. #if defined(HAVE_ECH)
  10625. if (ssl->options.useEch == 1 && msgType == client_hello) {
  10626. ret = TLSX_GetSizeWithEch(ssl, semaphore, msgType, &length);
  10627. if (ret != 0)
  10628. return ret;
  10629. }
  10630. else
  10631. #endif /* HAVE_ECH */
  10632. #endif /* WOLFSSL_TLS13 */
  10633. {
  10634. if (ssl->extensions) {
  10635. ret = TLSX_GetSize(ssl->extensions, semaphore, msgType, &length);
  10636. if (ret != 0)
  10637. return ret;
  10638. }
  10639. if (ssl->ctx && ssl->ctx->extensions) {
  10640. ret = TLSX_GetSize(ssl->ctx->extensions, semaphore, msgType,
  10641. &length);
  10642. if (ret != 0)
  10643. return ret;
  10644. }
  10645. }
  10646. #ifdef HAVE_EXTENDED_MASTER
  10647. if (msgType == client_hello && ssl->options.haveEMS &&
  10648. (!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade)) {
  10649. length += HELLO_EXT_SZ;
  10650. }
  10651. #endif
  10652. if (length)
  10653. length += OPAQUE16_LEN; /* for total length storage. */
  10654. *pLength += length;
  10655. return ret;
  10656. }
  10657. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  10658. /* return status after writing the extensions with ech written last */
  10659. static int TLSX_WriteWithEch(WOLFSSL* ssl, byte* output, byte* semaphore,
  10660. byte msgType, word16* pOffset)
  10661. {
  10662. int ret = 0;
  10663. TLSX* echX = NULL;
  10664. TLSX* serverNameX = NULL;
  10665. TLSX** extensions = NULL;
  10666. #ifdef WOLFSSL_SMALL_STACK
  10667. char* tmpServerName = NULL;
  10668. #else
  10669. char tmpServerName[MAX_PUBLIC_NAME_SZ];
  10670. #endif
  10671. /* get the echX from either extensions or ctx */
  10672. if (ssl->extensions)
  10673. echX = TLSX_Find(ssl->extensions, TLSX_ECH);
  10674. if (echX == NULL && ssl->ctx && ssl->ctx->extensions) {
  10675. /* if not NULL the semaphore will stop it from being counted */
  10676. if (echX == NULL)
  10677. echX = TLSX_Find(ssl->ctx->extensions, TLSX_ECH);
  10678. }
  10679. /* if type is outer change sni to public name */
  10680. if (echX != NULL && ((WOLFSSL_ECH*)echX->data)->type == ECH_TYPE_OUTER) {
  10681. if (ssl->extensions) {
  10682. serverNameX = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
  10683. if (serverNameX != NULL)
  10684. extensions = &ssl->extensions;
  10685. }
  10686. if (serverNameX == NULL && ssl->ctx && ssl->ctx->extensions) {
  10687. serverNameX = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
  10688. extensions = &ssl->ctx->extensions;
  10689. }
  10690. /* store the inner server name */
  10691. if (serverNameX != NULL) {
  10692. char* hostName = ((SNI*)serverNameX->data)->data.host_name;
  10693. word32 hostNameSz = (word32)XSTRLEN(hostName) + 1;
  10694. #ifdef WOLFSSL_SMALL_STACK
  10695. tmpServerName = (char*)XMALLOC(hostNameSz, ssl->heap,
  10696. DYNAMIC_TYPE_TMP_BUFFER);
  10697. if (tmpServerName == NULL)
  10698. return MEMORY_E;
  10699. #else
  10700. /* truncate if too long */
  10701. if (hostNameSz > MAX_PUBLIC_NAME_SZ)
  10702. hostNameSz = MAX_PUBLIC_NAME_SZ;
  10703. #endif
  10704. XMEMCPY(tmpServerName, hostName, hostNameSz);
  10705. }
  10706. /* remove the inner server name */
  10707. TLSX_Remove(extensions, TLSX_SERVER_NAME, ssl->heap);
  10708. ret = TLSX_UseSNI(extensions, WOLFSSL_SNI_HOST_NAME,
  10709. ((WOLFSSL_ECH*)echX->data)->echConfig->publicName,
  10710. XSTRLEN(((WOLFSSL_ECH*)echX->data)->echConfig->publicName),
  10711. ssl->heap);
  10712. /* set the public name as the server name */
  10713. if (ret == WOLFSSL_SUCCESS)
  10714. ret = 0;
  10715. }
  10716. if (echX != NULL) {
  10717. /* turn ech on so it doesn't write, then write it last */
  10718. TURN_ON(semaphore, TLSX_ToSemaphore(echX->type));
  10719. }
  10720. if (ret == 0 && ssl->extensions) {
  10721. ret = TLSX_Write(ssl->extensions, output + *pOffset, semaphore,
  10722. msgType, pOffset);
  10723. }
  10724. if (ret == 0 && ssl->ctx && ssl->ctx->extensions) {
  10725. ret = TLSX_Write(ssl->ctx->extensions, output + *pOffset, semaphore,
  10726. msgType, pOffset);
  10727. }
  10728. if (echX != NULL) {
  10729. /* turn off and write it last */
  10730. TURN_OFF(semaphore, TLSX_ToSemaphore(echX->type));
  10731. }
  10732. if (ret == 0 && ssl->extensions) {
  10733. ret = TLSX_Write(ssl->extensions, output + *pOffset, semaphore,
  10734. msgType, pOffset);
  10735. }
  10736. if (ret == 0 && ssl->ctx && ssl->ctx->extensions) {
  10737. ret = TLSX_Write(ssl->ctx->extensions, output + *pOffset, semaphore,
  10738. msgType, pOffset);
  10739. }
  10740. if (serverNameX != NULL) {
  10741. /* remove the public name SNI */
  10742. TLSX_Remove(extensions, TLSX_SERVER_NAME, ssl->heap);
  10743. ret = TLSX_UseSNI(extensions, WOLFSSL_SNI_HOST_NAME, tmpServerName,
  10744. XSTRLEN(tmpServerName), ssl->heap);
  10745. /* restore the inner server name */
  10746. if (ret == WOLFSSL_SUCCESS)
  10747. ret = 0;
  10748. }
  10749. #ifdef WOLFSSL_SMALL_STACK
  10750. XFREE(tmpServerName, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  10751. #endif
  10752. return ret;
  10753. }
  10754. #endif
  10755. /** Writes the extensions to be sent into the client hello. */
  10756. int TLSX_WriteRequest(WOLFSSL* ssl, byte* output, byte msgType, word16* pOffset)
  10757. {
  10758. int ret = 0;
  10759. word16 offset = 0;
  10760. byte semaphore[SEMAPHORE_SIZE] = {0};
  10761. if (!TLSX_SupportExtensions(ssl) || output == NULL)
  10762. return 0;
  10763. offset += OPAQUE16_LEN; /* extensions length */
  10764. if (msgType == client_hello) {
  10765. EC_VALIDATE_REQUEST(ssl, semaphore);
  10766. PF_VALIDATE_REQUEST(ssl, semaphore);
  10767. WOLF_STK_VALIDATE_REQUEST(ssl);
  10768. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10769. if (WOLFSSL_SUITES(ssl)->hashSigAlgoSz == 0)
  10770. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  10771. #endif
  10772. #ifdef WOLFSSL_TLS13
  10773. if (!IsAtLeastTLSv1_2(ssl)) {
  10774. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10775. }
  10776. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  10777. if (!IsAtLeastTLSv1_3(ssl->version)) {
  10778. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10779. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10780. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PSK_KEY_EXCHANGE_MODES));
  10781. #endif
  10782. #ifdef WOLFSSL_EARLY_DATA
  10783. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  10784. #endif
  10785. #ifdef WOLFSSL_SEND_HRR_COOKIE
  10786. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  10787. #endif
  10788. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  10789. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_POST_HANDSHAKE_AUTH));
  10790. #endif
  10791. }
  10792. #endif
  10793. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10794. /* Must write Pre-shared Key extension at the end in TLS v1.3.
  10795. * Must not write out Pre-shared Key extension in earlier versions of
  10796. * protocol.
  10797. */
  10798. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10799. #endif
  10800. #endif /* WOLFSSL_TLS13 */
  10801. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  10802. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  10803. /* mark already sent, so it won't send it */
  10804. if (!SSL_CM(ssl)->ocspStaplingEnabled) {
  10805. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10806. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  10807. }
  10808. #endif
  10809. }
  10810. #ifdef WOLFSSL_TLS13
  10811. #ifndef NO_CERTS
  10812. else if (msgType == certificate_request) {
  10813. /* Don't send out any extension except those that are turned off. */
  10814. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10815. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10816. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  10817. #endif
  10818. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  10819. * TLSX_CERTIFICATE_AUTHORITIES, TLSX_OID_FILTERS
  10820. * TLSX_STATUS_REQUEST
  10821. */
  10822. }
  10823. #endif
  10824. #endif
  10825. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  10826. if (ssl->options.useEch == 1 && msgType == client_hello) {
  10827. ret = TLSX_WriteWithEch(ssl, output, semaphore,
  10828. msgType, &offset);
  10829. if (ret != 0)
  10830. return ret;
  10831. }
  10832. else
  10833. #endif
  10834. {
  10835. if (ssl->extensions) {
  10836. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  10837. msgType, &offset);
  10838. if (ret != 0)
  10839. return ret;
  10840. }
  10841. if (ssl->ctx && ssl->ctx->extensions) {
  10842. ret = TLSX_Write(ssl->ctx->extensions, output + offset, semaphore,
  10843. msgType, &offset);
  10844. if (ret != 0)
  10845. return ret;
  10846. }
  10847. }
  10848. #ifdef HAVE_EXTENDED_MASTER
  10849. if (msgType == client_hello && ssl->options.haveEMS &&
  10850. (!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade)) {
  10851. WOLFSSL_MSG("EMS extension to write");
  10852. c16toa(HELLO_EXT_EXTMS, output + offset);
  10853. offset += HELLO_EXT_TYPE_SZ;
  10854. c16toa(0, output + offset);
  10855. offset += HELLO_EXT_SZ_SZ;
  10856. }
  10857. #endif
  10858. #ifdef WOLFSSL_TLS13
  10859. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10860. if (msgType == client_hello && IsAtLeastTLSv1_3(ssl->version)) {
  10861. /* Write out what we can of Pre-shared key extension. */
  10862. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10863. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  10864. client_hello, &offset);
  10865. if (ret != 0)
  10866. return ret;
  10867. }
  10868. #endif
  10869. #endif
  10870. if (offset > OPAQUE16_LEN || msgType != client_hello)
  10871. c16toa(offset - OPAQUE16_LEN, output); /* extensions length */
  10872. *pOffset += offset;
  10873. return ret;
  10874. }
  10875. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
  10876. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_SERVER)
  10877. /** Tells the buffered size of extensions to be sent into the server hello. */
  10878. int TLSX_GetResponseSize(WOLFSSL* ssl, byte msgType, word16* pLength)
  10879. {
  10880. int ret = 0;
  10881. word16 length = 0;
  10882. byte semaphore[SEMAPHORE_SIZE] = {0};
  10883. switch (msgType) {
  10884. #ifndef NO_WOLFSSL_SERVER
  10885. case server_hello:
  10886. PF_VALIDATE_RESPONSE(ssl, semaphore);
  10887. #ifdef WOLFSSL_TLS13
  10888. if (IsAtLeastTLSv1_3(ssl->version)) {
  10889. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10890. TURN_OFF(semaphore,
  10891. TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10892. #if defined(HAVE_SUPPORTED_CURVES)
  10893. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10894. if (!ssl->options.noPskDheKe)
  10895. #endif
  10896. {
  10897. /* Expect KeyShare extension in ServerHello. */
  10898. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10899. }
  10900. #endif
  10901. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10902. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10903. #endif
  10904. #ifdef WOLFSSL_DTLS_CID
  10905. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  10906. #endif
  10907. }
  10908. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  10909. else {
  10910. #ifdef HAVE_SUPPORTED_CURVES
  10911. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10912. #endif
  10913. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10914. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10915. #endif
  10916. }
  10917. #endif
  10918. #endif /* WOLFSSL_TLS13 */
  10919. break;
  10920. #ifdef WOLFSSL_TLS13
  10921. case hello_retry_request:
  10922. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10923. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10924. #ifdef HAVE_SUPPORTED_CURVES
  10925. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10926. if (!ssl->options.noPskDheKe)
  10927. #endif
  10928. {
  10929. /* Expect KeyShare extension in HelloRetryRequest. */
  10930. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10931. }
  10932. #endif
  10933. #ifdef WOLFSSL_SEND_HRR_COOKIE
  10934. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  10935. #endif
  10936. break;
  10937. #endif
  10938. #ifdef WOLFSSL_TLS13
  10939. case encrypted_extensions:
  10940. /* Send out all extension except those that are turned on. */
  10941. #ifdef HAVE_ECC
  10942. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  10943. #endif
  10944. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10945. #ifdef HAVE_SESSION_TICKET
  10946. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SESSION_TICKET));
  10947. #endif
  10948. #ifdef HAVE_SUPPORTED_CURVES
  10949. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10950. #endif
  10951. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10952. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10953. #endif
  10954. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  10955. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10956. #endif
  10957. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  10958. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  10959. #endif
  10960. #if defined(HAVE_SERVER_RENEGOTIATION_INFO)
  10961. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_RENEGOTIATION_INFO));
  10962. #endif
  10963. #ifdef WOLFSSL_DTLS_CID
  10964. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  10965. #endif /* WOLFSSL_DTLS_CID */
  10966. break;
  10967. #ifdef WOLFSSL_EARLY_DATA
  10968. case session_ticket:
  10969. if (ssl->options.tls1_3) {
  10970. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10971. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  10972. }
  10973. break;
  10974. #endif
  10975. #endif
  10976. #endif
  10977. #ifdef WOLFSSL_TLS13
  10978. #ifndef NO_CERTS
  10979. case certificate:
  10980. /* Don't send out any extension except those that are turned off. */
  10981. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10982. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10983. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  10984. * TLSX_SERVER_CERTIFICATE_TYPE
  10985. */
  10986. break;
  10987. #endif
  10988. #endif
  10989. }
  10990. #ifdef HAVE_EXTENDED_MASTER
  10991. if (ssl->options.haveEMS && msgType == server_hello &&
  10992. !IsAtLeastTLSv1_3(ssl->version)) {
  10993. length += HELLO_EXT_SZ;
  10994. }
  10995. #endif
  10996. if (TLSX_SupportExtensions(ssl)) {
  10997. ret = TLSX_GetSize(ssl->extensions, semaphore, msgType, &length);
  10998. if (ret != 0)
  10999. return ret;
  11000. }
  11001. /* All the response data is set at the ssl object only, so no ctx here. */
  11002. if (length || msgType != server_hello)
  11003. length += OPAQUE16_LEN; /* for total length storage. */
  11004. *pLength += length;
  11005. return ret;
  11006. }
  11007. /** Writes the server hello extensions into a buffer. */
  11008. int TLSX_WriteResponse(WOLFSSL *ssl, byte* output, byte msgType, word16* pOffset)
  11009. {
  11010. int ret = 0;
  11011. word16 offset = 0;
  11012. if (TLSX_SupportExtensions(ssl) && output) {
  11013. byte semaphore[SEMAPHORE_SIZE] = {0};
  11014. switch (msgType) {
  11015. #ifndef NO_WOLFSSL_SERVER
  11016. case server_hello:
  11017. PF_VALIDATE_RESPONSE(ssl, semaphore);
  11018. #ifdef WOLFSSL_TLS13
  11019. if (IsAtLeastTLSv1_3(ssl->version)) {
  11020. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  11021. TURN_OFF(semaphore,
  11022. TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  11023. #ifdef HAVE_SUPPORTED_CURVES
  11024. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11025. if (!ssl->options.noPskDheKe)
  11026. #endif
  11027. {
  11028. /* Write out KeyShare in ServerHello. */
  11029. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  11030. }
  11031. #endif
  11032. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11033. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  11034. #endif
  11035. #ifdef WOLFSSL_DTLS_CID
  11036. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  11037. #endif /* WOLFSSL_DTLS_CID */
  11038. }
  11039. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  11040. else {
  11041. #ifdef HAVE_SUPPORTED_CURVES
  11042. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  11043. #endif
  11044. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11045. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  11046. #endif
  11047. }
  11048. #endif
  11049. #endif
  11050. break;
  11051. #ifdef WOLFSSL_TLS13
  11052. case hello_retry_request:
  11053. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  11054. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  11055. #ifdef HAVE_SUPPORTED_CURVES
  11056. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11057. if (!ssl->options.noPskDheKe)
  11058. #endif
  11059. {
  11060. /* Write out KeyShare in HelloRetryRequest. */
  11061. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  11062. }
  11063. #endif
  11064. /* Cookie is written below as last extension. */
  11065. break;
  11066. #endif
  11067. #ifdef WOLFSSL_TLS13
  11068. case encrypted_extensions:
  11069. /* Send out all extension except those that are turned on. */
  11070. #ifdef HAVE_ECC
  11071. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  11072. #endif
  11073. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  11074. #ifdef HAVE_SESSION_TICKET
  11075. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SESSION_TICKET));
  11076. #endif
  11077. #ifdef HAVE_SUPPORTED_CURVES
  11078. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  11079. #endif
  11080. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11081. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  11082. #endif
  11083. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  11084. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  11085. #endif
  11086. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  11087. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  11088. #endif
  11089. #if defined(HAVE_SERVER_RENEGOTIATION_INFO)
  11090. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_RENEGOTIATION_INFO));
  11091. #endif
  11092. #ifdef WOLFSSL_DTLS_CID
  11093. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  11094. #endif /* WOLFSSL_DTLS_CID */
  11095. break;
  11096. #ifdef WOLFSSL_EARLY_DATA
  11097. case session_ticket:
  11098. if (ssl->options.tls1_3) {
  11099. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  11100. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  11101. }
  11102. break;
  11103. #endif
  11104. #endif
  11105. #endif
  11106. #ifdef WOLFSSL_TLS13
  11107. #ifndef NO_CERTS
  11108. case certificate:
  11109. /* Don't send out any extension except those that are turned
  11110. * off. */
  11111. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  11112. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  11113. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  11114. * TLSX_SERVER_CERTIFICATE_TYPE
  11115. */
  11116. break;
  11117. #endif
  11118. #endif
  11119. default:
  11120. break;
  11121. }
  11122. offset += OPAQUE16_LEN; /* extensions length */
  11123. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  11124. msgType, &offset);
  11125. if (ret != 0)
  11126. return ret;
  11127. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  11128. if (msgType == hello_retry_request) {
  11129. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  11130. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  11131. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  11132. msgType, &offset);
  11133. if (ret != 0)
  11134. return ret;
  11135. }
  11136. #endif
  11137. #ifdef HAVE_EXTENDED_MASTER
  11138. if (ssl->options.haveEMS && msgType == server_hello &&
  11139. !IsAtLeastTLSv1_3(ssl->version)) {
  11140. WOLFSSL_MSG("EMS extension to write");
  11141. c16toa(HELLO_EXT_EXTMS, output + offset);
  11142. offset += HELLO_EXT_TYPE_SZ;
  11143. c16toa(0, output + offset);
  11144. offset += HELLO_EXT_SZ_SZ;
  11145. }
  11146. #endif
  11147. if (offset > OPAQUE16_LEN || msgType != server_hello)
  11148. c16toa(offset - OPAQUE16_LEN, output); /* extensions length */
  11149. }
  11150. if (pOffset)
  11151. *pOffset += offset;
  11152. return ret;
  11153. }
  11154. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_SERVER */
  11155. #ifdef WOLFSSL_TLS13
  11156. int TLSX_ParseVersion(WOLFSSL* ssl, const byte* input, word16 length,
  11157. byte msgType, int* found)
  11158. {
  11159. int ret = 0;
  11160. int offset = 0;
  11161. *found = 0;
  11162. while (offset < (int)length) {
  11163. word16 type;
  11164. word16 size;
  11165. if (offset + (2 * OPAQUE16_LEN) > length) {
  11166. ret = BUFFER_ERROR;
  11167. break;
  11168. }
  11169. ato16(input + offset, &type);
  11170. offset += HELLO_EXT_TYPE_SZ;
  11171. ato16(input + offset, &size);
  11172. offset += OPAQUE16_LEN;
  11173. if (offset + size > length) {
  11174. ret = BUFFER_ERROR;
  11175. break;
  11176. }
  11177. if (type == TLSX_SUPPORTED_VERSIONS) {
  11178. *found = 1;
  11179. WOLFSSL_MSG("Supported Versions extension received");
  11180. ret = SV_PARSE(ssl, input + offset, size, msgType, &ssl->version,
  11181. &ssl->options, &ssl->extensions);
  11182. break;
  11183. }
  11184. offset += size;
  11185. }
  11186. return ret;
  11187. }
  11188. #endif
  11189. /** Parses a buffer of TLS extensions. */
  11190. int TLSX_Parse(WOLFSSL* ssl, const byte* input, word16 length, byte msgType,
  11191. Suites *suites)
  11192. {
  11193. int ret = 0;
  11194. word16 offset = 0;
  11195. byte isRequest = (msgType == client_hello ||
  11196. msgType == certificate_request);
  11197. #ifdef HAVE_EXTENDED_MASTER
  11198. byte pendingEMS = 0;
  11199. #endif
  11200. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  11201. int pskDone = 0;
  11202. #endif
  11203. byte seenType[SEMAPHORE_SIZE]; /* Seen known extensions. */
  11204. if (!ssl || !input || (isRequest && !suites))
  11205. return BAD_FUNC_ARG;
  11206. /* No known extensions seen yet. */
  11207. XMEMSET(seenType, 0, sizeof(seenType));
  11208. while (ret == 0 && offset < length) {
  11209. word16 type;
  11210. word16 size;
  11211. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  11212. if (msgType == client_hello && pskDone) {
  11213. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  11214. return PSK_KEY_ERROR;
  11215. }
  11216. #endif
  11217. if (length - offset < HELLO_EXT_TYPE_SZ + OPAQUE16_LEN)
  11218. return BUFFER_ERROR;
  11219. ato16(input + offset, &type);
  11220. offset += HELLO_EXT_TYPE_SZ;
  11221. ato16(input + offset, &size);
  11222. offset += OPAQUE16_LEN;
  11223. /* Check we have a bit for extension type. */
  11224. if ((type <= 62) || (type == TLSX_RENEGOTIATION_INFO)
  11225. #ifdef WOLFSSL_QUIC
  11226. || (type == TLSX_KEY_QUIC_TP_PARAMS_DRAFT)
  11227. #endif
  11228. )
  11229. {
  11230. /* Detect duplicate recognized extensions. */
  11231. if (IS_OFF(seenType, TLSX_ToSemaphore(type))) {
  11232. TURN_ON(seenType, TLSX_ToSemaphore(type));
  11233. }
  11234. else {
  11235. return DUPLICATE_TLS_EXT_E;
  11236. }
  11237. }
  11238. if (length - offset < size)
  11239. return BUFFER_ERROR;
  11240. switch (type) {
  11241. #ifdef HAVE_SNI
  11242. case TLSX_SERVER_NAME:
  11243. WOLFSSL_MSG("SNI extension received");
  11244. #ifdef WOLFSSL_DEBUG_TLS
  11245. WOLFSSL_BUFFER(input + offset, size);
  11246. #endif
  11247. #ifdef WOLFSSL_TLS13
  11248. if (IsAtLeastTLSv1_3(ssl->version)) {
  11249. if (msgType != client_hello &&
  11250. msgType != encrypted_extensions)
  11251. return EXT_NOT_ALLOWED;
  11252. }
  11253. else
  11254. #endif
  11255. {
  11256. if (msgType != client_hello &&
  11257. msgType != server_hello)
  11258. return EXT_NOT_ALLOWED;
  11259. }
  11260. ret = SNI_PARSE(ssl, input + offset, size, isRequest);
  11261. break;
  11262. #endif
  11263. case TLSX_TRUSTED_CA_KEYS:
  11264. WOLFSSL_MSG("Trusted CA extension received");
  11265. #ifdef WOLFSSL_DEBUG_TLS
  11266. WOLFSSL_BUFFER(input + offset, size);
  11267. #endif
  11268. #ifdef WOLFSSL_TLS13
  11269. if (IsAtLeastTLSv1_3(ssl->version)) {
  11270. if (msgType != client_hello &&
  11271. msgType != encrypted_extensions)
  11272. return EXT_NOT_ALLOWED;
  11273. }
  11274. else
  11275. #endif
  11276. {
  11277. if (msgType != client_hello)
  11278. return EXT_NOT_ALLOWED;
  11279. }
  11280. ret = TCA_PARSE(ssl, input + offset, size, isRequest);
  11281. break;
  11282. case TLSX_MAX_FRAGMENT_LENGTH:
  11283. WOLFSSL_MSG("Max Fragment Length extension received");
  11284. #ifdef WOLFSSL_DEBUG_TLS
  11285. WOLFSSL_BUFFER(input + offset, size);
  11286. #endif
  11287. #ifdef WOLFSSL_TLS13
  11288. if (IsAtLeastTLSv1_3(ssl->version)) {
  11289. if (msgType != client_hello &&
  11290. msgType != encrypted_extensions) {
  11291. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11292. return EXT_NOT_ALLOWED;
  11293. }
  11294. }
  11295. else
  11296. #endif
  11297. {
  11298. if (msgType != client_hello &&
  11299. msgType != server_hello) {
  11300. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11301. return EXT_NOT_ALLOWED;
  11302. }
  11303. }
  11304. ret = MFL_PARSE(ssl, input + offset, size, isRequest);
  11305. break;
  11306. case TLSX_TRUNCATED_HMAC:
  11307. WOLFSSL_MSG("Truncated HMAC extension received");
  11308. #ifdef WOLFSSL_DEBUG_TLS
  11309. WOLFSSL_BUFFER(input + offset, size);
  11310. #endif
  11311. #ifdef WOLFSSL_TLS13
  11312. if (IsAtLeastTLSv1_3(ssl->version))
  11313. break;
  11314. #endif
  11315. if (msgType != client_hello)
  11316. return EXT_NOT_ALLOWED;
  11317. ret = THM_PARSE(ssl, input + offset, size, isRequest);
  11318. break;
  11319. case TLSX_SUPPORTED_GROUPS:
  11320. WOLFSSL_MSG("Supported Groups extension received");
  11321. #ifdef WOLFSSL_DEBUG_TLS
  11322. WOLFSSL_BUFFER(input + offset, size);
  11323. #endif
  11324. #ifdef WOLFSSL_TLS13
  11325. if (IsAtLeastTLSv1_3(ssl->version)) {
  11326. if (msgType != client_hello &&
  11327. msgType != encrypted_extensions) {
  11328. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11329. return EXT_NOT_ALLOWED;
  11330. }
  11331. }
  11332. else
  11333. #endif
  11334. {
  11335. if (msgType != client_hello) {
  11336. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11337. return EXT_NOT_ALLOWED;
  11338. }
  11339. }
  11340. ret = EC_PARSE(ssl, input + offset, size, isRequest,
  11341. &ssl->extensions);
  11342. break;
  11343. case TLSX_EC_POINT_FORMATS:
  11344. WOLFSSL_MSG("Point Formats extension received");
  11345. #ifdef WOLFSSL_DEBUG_TLS
  11346. WOLFSSL_BUFFER(input + offset, size);
  11347. #endif
  11348. #ifdef WOLFSSL_TLS13
  11349. if (IsAtLeastTLSv1_3(ssl->version))
  11350. break;
  11351. #endif
  11352. if (msgType != client_hello &&
  11353. msgType != server_hello) {
  11354. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11355. return EXT_NOT_ALLOWED;
  11356. }
  11357. ret = PF_PARSE(ssl, input + offset, size, isRequest);
  11358. break;
  11359. case TLSX_STATUS_REQUEST:
  11360. WOLFSSL_MSG("Certificate Status Request extension received");
  11361. #ifdef WOLFSSL_DEBUG_TLS
  11362. WOLFSSL_BUFFER(input + offset, size);
  11363. #endif
  11364. #ifdef WOLFSSL_TLS13
  11365. if (IsAtLeastTLSv1_3(ssl->version)) {
  11366. if (msgType != client_hello &&
  11367. msgType != certificate_request &&
  11368. msgType != certificate)
  11369. return EXT_NOT_ALLOWED;
  11370. }
  11371. else
  11372. #endif
  11373. {
  11374. if (msgType != client_hello &&
  11375. msgType != server_hello)
  11376. return EXT_NOT_ALLOWED;
  11377. }
  11378. ret = CSR_PARSE(ssl, input + offset, size, isRequest);
  11379. break;
  11380. case TLSX_STATUS_REQUEST_V2:
  11381. WOLFSSL_MSG("Certificate Status Request v2 extension received");
  11382. #ifdef WOLFSSL_DEBUG_TLS
  11383. WOLFSSL_BUFFER(input + offset, size);
  11384. #endif
  11385. #if defined(WOLFSSL_TLS13) && defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  11386. if (IsAtLeastTLSv1_3(ssl->version)) {
  11387. if (msgType != client_hello &&
  11388. msgType != certificate_request &&
  11389. msgType != certificate)
  11390. return EXT_NOT_ALLOWED;
  11391. }
  11392. else
  11393. #endif
  11394. {
  11395. if (msgType != client_hello &&
  11396. msgType != server_hello)
  11397. return EXT_NOT_ALLOWED;
  11398. }
  11399. ret = CSR2_PARSE(ssl, input + offset, size, isRequest);
  11400. break;
  11401. #ifdef HAVE_EXTENDED_MASTER
  11402. case HELLO_EXT_EXTMS:
  11403. WOLFSSL_MSG("Extended Master Secret extension received");
  11404. #ifdef WOLFSSL_DEBUG_TLS
  11405. WOLFSSL_BUFFER(input + offset, size);
  11406. #endif
  11407. #if defined(WOLFSSL_TLS13)
  11408. if (IsAtLeastTLSv1_3(ssl->version))
  11409. break;
  11410. #endif
  11411. if (msgType != client_hello &&
  11412. msgType != server_hello)
  11413. return EXT_NOT_ALLOWED;
  11414. if (size != 0)
  11415. return BUFFER_ERROR;
  11416. #ifndef NO_WOLFSSL_SERVER
  11417. if (isRequest)
  11418. ssl->options.haveEMS = 1;
  11419. #endif
  11420. pendingEMS = 1;
  11421. break;
  11422. #endif
  11423. case TLSX_RENEGOTIATION_INFO:
  11424. WOLFSSL_MSG("Secure Renegotiation extension received");
  11425. #ifdef WOLFSSL_DEBUG_TLS
  11426. WOLFSSL_BUFFER(input + offset, size);
  11427. #endif
  11428. #ifdef WOLFSSL_TLS13
  11429. if (IsAtLeastTLSv1_3(ssl->version))
  11430. break;
  11431. #endif
  11432. if (msgType != client_hello &&
  11433. msgType != server_hello)
  11434. return EXT_NOT_ALLOWED;
  11435. ret = SCR_PARSE(ssl, input + offset, size, isRequest);
  11436. break;
  11437. case TLSX_SESSION_TICKET:
  11438. WOLFSSL_MSG("Session Ticket extension received");
  11439. #ifdef WOLFSSL_DEBUG_TLS
  11440. WOLFSSL_BUFFER(input + offset, size);
  11441. #endif
  11442. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)
  11443. if (IsAtLeastTLSv1_3(ssl->version)) {
  11444. if (msgType != client_hello)
  11445. return EXT_NOT_ALLOWED;
  11446. }
  11447. else
  11448. #endif
  11449. {
  11450. if (msgType != client_hello &&
  11451. msgType != server_hello)
  11452. return EXT_NOT_ALLOWED;
  11453. }
  11454. ret = WOLF_STK_PARSE(ssl, input + offset, size, isRequest);
  11455. break;
  11456. case TLSX_APPLICATION_LAYER_PROTOCOL:
  11457. WOLFSSL_MSG("ALPN extension received");
  11458. #ifdef WOLFSSL_DEBUG_TLS
  11459. WOLFSSL_BUFFER(input + offset, size);
  11460. #endif
  11461. #if defined(WOLFSSL_TLS13) && defined(HAVE_ALPN)
  11462. if (IsAtLeastTLSv1_3(ssl->version)) {
  11463. if (msgType != client_hello &&
  11464. msgType != encrypted_extensions)
  11465. return EXT_NOT_ALLOWED;
  11466. }
  11467. else
  11468. #endif
  11469. {
  11470. if (msgType != client_hello &&
  11471. msgType != server_hello)
  11472. return EXT_NOT_ALLOWED;
  11473. }
  11474. ret = ALPN_PARSE(ssl, input + offset, size, isRequest);
  11475. break;
  11476. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  11477. case TLSX_SIGNATURE_ALGORITHMS:
  11478. WOLFSSL_MSG("Signature Algorithms extension received");
  11479. #ifdef WOLFSSL_DEBUG_TLS
  11480. WOLFSSL_BUFFER(input + offset, size);
  11481. #endif
  11482. if (!IsAtLeastTLSv1_2(ssl))
  11483. break;
  11484. #ifdef WOLFSSL_TLS13
  11485. if (IsAtLeastTLSv1_3(ssl->version)) {
  11486. if (msgType != client_hello &&
  11487. msgType != certificate_request)
  11488. return EXT_NOT_ALLOWED;
  11489. }
  11490. else
  11491. #endif
  11492. {
  11493. if (msgType != client_hello)
  11494. return EXT_NOT_ALLOWED;
  11495. }
  11496. ret = SA_PARSE(ssl, input + offset, size, isRequest, suites);
  11497. break;
  11498. #endif
  11499. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  11500. case TLSX_ENCRYPT_THEN_MAC:
  11501. WOLFSSL_MSG("Encrypt-Then-Mac extension received");
  11502. /* Ignore for TLS 1.3+ */
  11503. if (IsAtLeastTLSv1_3(ssl->version))
  11504. break;
  11505. if (msgType != client_hello &&
  11506. msgType != server_hello)
  11507. return EXT_NOT_ALLOWED;
  11508. ret = ETM_PARSE(ssl, input + offset, size, msgType);
  11509. break;
  11510. #endif /* HAVE_ENCRYPT_THEN_MAC */
  11511. #ifdef WOLFSSL_TLS13
  11512. case TLSX_SUPPORTED_VERSIONS:
  11513. WOLFSSL_MSG("Skipping Supported Versions - already processed");
  11514. #ifdef WOLFSSL_DEBUG_TLS
  11515. WOLFSSL_BUFFER(input + offset, size);
  11516. #endif
  11517. if (msgType != client_hello &&
  11518. msgType != server_hello &&
  11519. msgType != hello_retry_request)
  11520. return EXT_NOT_ALLOWED;
  11521. break;
  11522. #ifdef WOLFSSL_SEND_HRR_COOKIE
  11523. case TLSX_COOKIE:
  11524. WOLFSSL_MSG("Cookie extension received");
  11525. #ifdef WOLFSSL_DEBUG_TLS
  11526. WOLFSSL_BUFFER(input + offset, size);
  11527. #endif
  11528. if (!IsAtLeastTLSv1_3(ssl->version))
  11529. break;
  11530. if (msgType != client_hello &&
  11531. msgType != hello_retry_request) {
  11532. return EXT_NOT_ALLOWED;
  11533. }
  11534. ret = CKE_PARSE(ssl, input + offset, size, msgType);
  11535. break;
  11536. #endif
  11537. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11538. case TLSX_PRE_SHARED_KEY:
  11539. WOLFSSL_MSG("Pre-Shared Key extension received");
  11540. #ifdef WOLFSSL_DEBUG_TLS
  11541. WOLFSSL_BUFFER(input + offset, size);
  11542. #endif
  11543. if (!IsAtLeastTLSv1_3(ssl->version))
  11544. break;
  11545. if (msgType != client_hello &&
  11546. msgType != server_hello) {
  11547. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11548. return EXT_NOT_ALLOWED;
  11549. }
  11550. ret = PSK_PARSE(ssl, input + offset, size, msgType);
  11551. pskDone = 1;
  11552. break;
  11553. case TLSX_PSK_KEY_EXCHANGE_MODES:
  11554. WOLFSSL_MSG("PSK Key Exchange Modes extension received");
  11555. #ifdef WOLFSSL_DEBUG_TLS
  11556. WOLFSSL_BUFFER(input + offset, size);
  11557. #endif
  11558. if (!IsAtLeastTLSv1_3(ssl->version))
  11559. break;
  11560. if (msgType != client_hello) {
  11561. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11562. return EXT_NOT_ALLOWED;
  11563. }
  11564. ret = PKM_PARSE(ssl, input + offset, size, msgType);
  11565. break;
  11566. #endif
  11567. #ifdef WOLFSSL_EARLY_DATA
  11568. case TLSX_EARLY_DATA:
  11569. WOLFSSL_MSG("Early Data extension received");
  11570. #ifdef WOLFSSL_DEBUG_TLS
  11571. WOLFSSL_BUFFER(input + offset, size);
  11572. #endif
  11573. if (!IsAtLeastTLSv1_3(ssl->version))
  11574. break;
  11575. if (msgType != client_hello && msgType != session_ticket &&
  11576. msgType != encrypted_extensions) {
  11577. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11578. return EXT_NOT_ALLOWED;
  11579. }
  11580. ret = EDI_PARSE(ssl, input + offset, size, msgType);
  11581. break;
  11582. #endif
  11583. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  11584. case TLSX_POST_HANDSHAKE_AUTH:
  11585. WOLFSSL_MSG("Post Handshake Authentication extension received");
  11586. #ifdef WOLFSSL_DEBUG_TLS
  11587. WOLFSSL_BUFFER(input + offset, size);
  11588. #endif
  11589. if (!IsAtLeastTLSv1_3(ssl->version))
  11590. break;
  11591. if (msgType != client_hello) {
  11592. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11593. return EXT_NOT_ALLOWED;
  11594. }
  11595. ret = PHA_PARSE(ssl, input + offset, size, msgType);
  11596. break;
  11597. #endif
  11598. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  11599. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  11600. WOLFSSL_MSG("Signature Algorithms extension received");
  11601. #ifdef WOLFSSL_DEBUG_TLS
  11602. WOLFSSL_BUFFER(input + offset, size);
  11603. #endif
  11604. if (!IsAtLeastTLSv1_3(ssl->version))
  11605. break;
  11606. if (msgType != client_hello &&
  11607. msgType != certificate_request) {
  11608. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11609. return EXT_NOT_ALLOWED;
  11610. }
  11611. ret = SAC_PARSE(ssl, input + offset, size, isRequest);
  11612. break;
  11613. #endif
  11614. case TLSX_KEY_SHARE:
  11615. WOLFSSL_MSG("Key Share extension received");
  11616. #ifdef WOLFSSL_DEBUG_TLS
  11617. WOLFSSL_BUFFER(input + offset, size);
  11618. #endif
  11619. #ifdef HAVE_SUPPORTED_CURVES
  11620. if (!IsAtLeastTLSv1_3(ssl->version))
  11621. break;
  11622. if (msgType != client_hello && msgType != server_hello &&
  11623. msgType != hello_retry_request) {
  11624. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11625. return EXT_NOT_ALLOWED;
  11626. }
  11627. #endif
  11628. ret = KS_PARSE(ssl, input + offset, size, msgType);
  11629. break;
  11630. #endif
  11631. #ifdef WOLFSSL_SRTP
  11632. case TLSX_USE_SRTP:
  11633. WOLFSSL_MSG("Use SRTP extension received");
  11634. ret = SRTP_PARSE(ssl, input + offset, size, isRequest);
  11635. break;
  11636. #endif
  11637. #ifdef WOLFSSL_QUIC
  11638. case TLSX_KEY_QUIC_TP_PARAMS:
  11639. FALL_THROUGH;
  11640. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  11641. WOLFSSL_MSG("QUIC transport parameter received");
  11642. #ifdef WOLFSSL_DEBUG_TLS
  11643. WOLFSSL_BUFFER(input + offset, size);
  11644. #endif
  11645. if (IsAtLeastTLSv1_3(ssl->version) &&
  11646. msgType != client_hello &&
  11647. msgType != server_hello &&
  11648. msgType != encrypted_extensions) {
  11649. return EXT_NOT_ALLOWED;
  11650. }
  11651. else if (!IsAtLeastTLSv1_3(ssl->version) &&
  11652. msgType == encrypted_extensions) {
  11653. return EXT_NOT_ALLOWED;
  11654. }
  11655. else if (WOLFSSL_IS_QUIC(ssl)) {
  11656. ret = QTP_PARSE(ssl, input + offset, size, type, msgType);
  11657. }
  11658. else {
  11659. WOLFSSL_MSG("QUIC transport param TLS extension type, but no QUIC");
  11660. return EXT_NOT_ALLOWED; /* be safe, this should not happen */
  11661. }
  11662. break;
  11663. #endif /* WOLFSSL_QUIC */
  11664. #if defined(WOLFSSL_DTLS_CID)
  11665. case TLSX_CONNECTION_ID:
  11666. /* connection ID not supported in DTLSv1.2 */
  11667. if (!IsAtLeastTLSv1_3(ssl->version))
  11668. break;
  11669. if (msgType != client_hello && msgType != server_hello)
  11670. return EXT_NOT_ALLOWED;
  11671. WOLFSSL_MSG("ConnectionID extension received");
  11672. ret = CID_PARSE(ssl, input + offset, size, isRequest);
  11673. break;
  11674. #endif /* defined(WOLFSSL_DTLS_CID) */
  11675. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  11676. case TLSX_ECH:
  11677. ret = ECH_PARSE(ssl, input + offset, size, msgType);
  11678. break;
  11679. #endif
  11680. default:
  11681. WOLFSSL_MSG("Unknown TLS extension type");
  11682. }
  11683. /* offset should be updated here! */
  11684. offset += size;
  11685. }
  11686. #ifdef HAVE_EXTENDED_MASTER
  11687. if (IsAtLeastTLSv1_3(ssl->version) && msgType == hello_retry_request) {
  11688. /* Don't change EMS status until server_hello received.
  11689. * Second ClientHello must have same extensions.
  11690. */
  11691. }
  11692. else if (!isRequest && ssl->options.haveEMS && !pendingEMS)
  11693. ssl->options.haveEMS = 0;
  11694. #endif
  11695. if (ret == 0)
  11696. ret = SNI_VERIFY_PARSE(ssl, isRequest);
  11697. if (ret == 0)
  11698. ret = TCA_VERIFY_PARSE(ssl, isRequest);
  11699. return ret;
  11700. }
  11701. /* undefining semaphore macros */
  11702. #undef IS_OFF
  11703. #undef TURN_ON
  11704. #undef SEMAPHORE_SIZE
  11705. #endif /* HAVE_TLS_EXTENSIONS */
  11706. #ifndef NO_WOLFSSL_CLIENT
  11707. WOLFSSL_METHOD* wolfTLS_client_method(void)
  11708. {
  11709. return wolfTLS_client_method_ex(NULL);
  11710. }
  11711. WOLFSSL_METHOD* wolfTLS_client_method_ex(void* heap)
  11712. {
  11713. WOLFSSL_METHOD* method =
  11714. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11715. heap, DYNAMIC_TYPE_METHOD);
  11716. (void)heap;
  11717. WOLFSSL_ENTER("TLS_client_method_ex");
  11718. if (method) {
  11719. #if defined(WOLFSSL_TLS13)
  11720. InitSSL_Method(method, MakeTLSv1_3());
  11721. #elif !defined(WOLFSSL_NO_TLS12)
  11722. InitSSL_Method(method, MakeTLSv1_2());
  11723. #elif !defined(NO_OLD_TLS)
  11724. InitSSL_Method(method, MakeTLSv1_1());
  11725. #elif defined(WOLFSSL_ALLOW_TLSV10)
  11726. InitSSL_Method(method, MakeTLSv1());
  11727. #else
  11728. #error No TLS version enabled!
  11729. #endif
  11730. method->downgrade = 1;
  11731. method->side = WOLFSSL_CLIENT_END;
  11732. }
  11733. return method;
  11734. }
  11735. #ifndef NO_OLD_TLS
  11736. #ifdef WOLFSSL_ALLOW_TLSV10
  11737. WOLFSSL_METHOD* wolfTLSv1_client_method(void)
  11738. {
  11739. return wolfTLSv1_client_method_ex(NULL);
  11740. }
  11741. WOLFSSL_METHOD* wolfTLSv1_client_method_ex(void* heap)
  11742. {
  11743. WOLFSSL_METHOD* method =
  11744. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11745. heap, DYNAMIC_TYPE_METHOD);
  11746. (void)heap;
  11747. WOLFSSL_ENTER("TLSv1_client_method_ex");
  11748. if (method)
  11749. InitSSL_Method(method, MakeTLSv1());
  11750. return method;
  11751. }
  11752. #endif /* WOLFSSL_ALLOW_TLSV10 */
  11753. WOLFSSL_METHOD* wolfTLSv1_1_client_method(void)
  11754. {
  11755. return wolfTLSv1_1_client_method_ex(NULL);
  11756. }
  11757. WOLFSSL_METHOD* wolfTLSv1_1_client_method_ex(void* heap)
  11758. {
  11759. WOLFSSL_METHOD* method =
  11760. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11761. heap, DYNAMIC_TYPE_METHOD);
  11762. (void)heap;
  11763. WOLFSSL_ENTER("TLSv1_1_client_method_ex");
  11764. if (method)
  11765. InitSSL_Method(method, MakeTLSv1_1());
  11766. return method;
  11767. }
  11768. #endif /* !NO_OLD_TLS */
  11769. #ifndef WOLFSSL_NO_TLS12
  11770. WOLFSSL_ABI
  11771. WOLFSSL_METHOD* wolfTLSv1_2_client_method(void)
  11772. {
  11773. return wolfTLSv1_2_client_method_ex(NULL);
  11774. }
  11775. WOLFSSL_METHOD* wolfTLSv1_2_client_method_ex(void* heap)
  11776. {
  11777. WOLFSSL_METHOD* method =
  11778. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11779. heap, DYNAMIC_TYPE_METHOD);
  11780. (void)heap;
  11781. WOLFSSL_ENTER("TLSv1_2_client_method_ex");
  11782. if (method)
  11783. InitSSL_Method(method, MakeTLSv1_2());
  11784. return method;
  11785. }
  11786. #endif /* WOLFSSL_NO_TLS12 */
  11787. #ifdef WOLFSSL_TLS13
  11788. /* The TLS v1.3 client method data.
  11789. *
  11790. * returns the method data for a TLS v1.3 client.
  11791. */
  11792. WOLFSSL_ABI
  11793. WOLFSSL_METHOD* wolfTLSv1_3_client_method(void)
  11794. {
  11795. return wolfTLSv1_3_client_method_ex(NULL);
  11796. }
  11797. /* The TLS v1.3 client method data.
  11798. *
  11799. * heap The heap used for allocation.
  11800. * returns the method data for a TLS v1.3 client.
  11801. */
  11802. WOLFSSL_METHOD* wolfTLSv1_3_client_method_ex(void* heap)
  11803. {
  11804. WOLFSSL_METHOD* method = (WOLFSSL_METHOD*)
  11805. XMALLOC(sizeof(WOLFSSL_METHOD), heap,
  11806. DYNAMIC_TYPE_METHOD);
  11807. (void)heap;
  11808. WOLFSSL_ENTER("TLSv1_3_client_method_ex");
  11809. if (method)
  11810. InitSSL_Method(method, MakeTLSv1_3());
  11811. return method;
  11812. }
  11813. #endif /* WOLFSSL_TLS13 */
  11814. #ifdef WOLFSSL_DTLS
  11815. WOLFSSL_METHOD* wolfDTLS_client_method(void)
  11816. {
  11817. return wolfDTLS_client_method_ex(NULL);
  11818. }
  11819. WOLFSSL_METHOD* wolfDTLS_client_method_ex(void* heap)
  11820. {
  11821. WOLFSSL_METHOD* method =
  11822. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11823. heap, DYNAMIC_TYPE_METHOD);
  11824. (void)heap;
  11825. WOLFSSL_ENTER("DTLS_client_method_ex");
  11826. if (method) {
  11827. #if defined(WOLFSSL_DTLS13)
  11828. InitSSL_Method(method, MakeDTLSv1_3());
  11829. #elif !defined(WOLFSSL_NO_TLS12)
  11830. InitSSL_Method(method, MakeDTLSv1_2());
  11831. #elif !defined(NO_OLD_TLS)
  11832. InitSSL_Method(method, MakeDTLSv1());
  11833. #else
  11834. #error No DTLS version enabled!
  11835. #endif
  11836. method->downgrade = 1;
  11837. method->side = WOLFSSL_CLIENT_END;
  11838. }
  11839. return method;
  11840. }
  11841. #ifndef NO_OLD_TLS
  11842. WOLFSSL_METHOD* wolfDTLSv1_client_method(void)
  11843. {
  11844. return wolfDTLSv1_client_method_ex(NULL);
  11845. }
  11846. WOLFSSL_METHOD* wolfDTLSv1_client_method_ex(void* heap)
  11847. {
  11848. WOLFSSL_METHOD* method =
  11849. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11850. heap, DYNAMIC_TYPE_METHOD);
  11851. (void)heap;
  11852. WOLFSSL_ENTER("DTLSv1_client_method_ex");
  11853. if (method)
  11854. InitSSL_Method(method, MakeDTLSv1());
  11855. return method;
  11856. }
  11857. #endif /* NO_OLD_TLS */
  11858. #ifndef WOLFSSL_NO_TLS12
  11859. WOLFSSL_METHOD* wolfDTLSv1_2_client_method(void)
  11860. {
  11861. return wolfDTLSv1_2_client_method_ex(NULL);
  11862. }
  11863. WOLFSSL_METHOD* wolfDTLSv1_2_client_method_ex(void* heap)
  11864. {
  11865. WOLFSSL_METHOD* method =
  11866. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11867. heap, DYNAMIC_TYPE_METHOD);
  11868. (void)heap;
  11869. WOLFSSL_ENTER("DTLSv1_2_client_method_ex");
  11870. if (method)
  11871. InitSSL_Method(method, MakeDTLSv1_2());
  11872. (void)heap;
  11873. return method;
  11874. }
  11875. #endif /* !WOLFSSL_NO_TLS12 */
  11876. #endif /* WOLFSSL_DTLS */
  11877. #endif /* NO_WOLFSSL_CLIENT */
  11878. /* EITHER SIDE METHODS */
  11879. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  11880. #ifndef NO_OLD_TLS
  11881. #ifdef WOLFSSL_ALLOW_TLSV10
  11882. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  11883. *
  11884. * Returns a pointer to a WOLFSSL_METHOD struct
  11885. */
  11886. WOLFSSL_METHOD* wolfTLSv1_method(void)
  11887. {
  11888. return wolfTLSv1_method_ex(NULL);
  11889. }
  11890. WOLFSSL_METHOD* wolfTLSv1_method_ex(void* heap)
  11891. {
  11892. WOLFSSL_METHOD* m;
  11893. WOLFSSL_ENTER("TLSv1_method");
  11894. #ifndef NO_WOLFSSL_CLIENT
  11895. m = wolfTLSv1_client_method_ex(heap);
  11896. #else
  11897. m = wolfTLSv1_server_method_ex(heap);
  11898. #endif
  11899. if (m != NULL) {
  11900. m->side = WOLFSSL_NEITHER_END;
  11901. }
  11902. return m;
  11903. }
  11904. #endif /* WOLFSSL_ALLOW_TLSV10 */
  11905. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  11906. *
  11907. * Returns a pointer to a WOLFSSL_METHOD struct
  11908. */
  11909. WOLFSSL_METHOD* wolfTLSv1_1_method(void)
  11910. {
  11911. return wolfTLSv1_1_method_ex(NULL);
  11912. }
  11913. WOLFSSL_METHOD* wolfTLSv1_1_method_ex(void* heap)
  11914. {
  11915. WOLFSSL_METHOD* m;
  11916. WOLFSSL_ENTER("TLSv1_1_method");
  11917. #ifndef NO_WOLFSSL_CLIENT
  11918. m = wolfTLSv1_1_client_method_ex(heap);
  11919. #else
  11920. m = wolfTLSv1_1_server_method_ex(heap);
  11921. #endif
  11922. if (m != NULL) {
  11923. m->side = WOLFSSL_NEITHER_END;
  11924. }
  11925. return m;
  11926. }
  11927. #endif /* !NO_OLD_TLS */
  11928. #ifndef WOLFSSL_NO_TLS12
  11929. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  11930. *
  11931. * Returns a pointer to a WOLFSSL_METHOD struct
  11932. */
  11933. WOLFSSL_METHOD* wolfTLSv1_2_method(void)
  11934. {
  11935. return wolfTLSv1_2_method_ex(NULL);
  11936. }
  11937. WOLFSSL_METHOD* wolfTLSv1_2_method_ex(void* heap)
  11938. {
  11939. WOLFSSL_METHOD* m;
  11940. WOLFSSL_ENTER("TLSv1_2_method");
  11941. #ifndef NO_WOLFSSL_CLIENT
  11942. m = wolfTLSv1_2_client_method_ex(heap);
  11943. #else
  11944. m = wolfTLSv1_2_server_method_ex(heap);
  11945. #endif
  11946. if (m != NULL) {
  11947. m->side = WOLFSSL_NEITHER_END;
  11948. }
  11949. return m;
  11950. }
  11951. #endif /* !WOLFSSL_NO_TLS12 */
  11952. #ifdef WOLFSSL_TLS13
  11953. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  11954. *
  11955. * Returns a pointer to a WOLFSSL_METHOD struct
  11956. */
  11957. WOLFSSL_METHOD* wolfTLSv1_3_method(void)
  11958. {
  11959. return wolfTLSv1_3_method_ex(NULL);
  11960. }
  11961. WOLFSSL_METHOD* wolfTLSv1_3_method_ex(void* heap)
  11962. {
  11963. WOLFSSL_METHOD* m;
  11964. WOLFSSL_ENTER("TLSv1_3_method");
  11965. #ifndef NO_WOLFSSL_CLIENT
  11966. m = wolfTLSv1_3_client_method_ex(heap);
  11967. #else
  11968. m = wolfTLSv1_3_server_method_ex(heap);
  11969. #endif
  11970. if (m != NULL) {
  11971. m->side = WOLFSSL_NEITHER_END;
  11972. }
  11973. return m;
  11974. }
  11975. #endif /* WOLFSSL_TLS13 */
  11976. #ifdef WOLFSSL_DTLS
  11977. WOLFSSL_METHOD* wolfDTLS_method(void)
  11978. {
  11979. return wolfDTLS_method_ex(NULL);
  11980. }
  11981. WOLFSSL_METHOD* wolfDTLS_method_ex(void* heap)
  11982. {
  11983. WOLFSSL_METHOD* m;
  11984. WOLFSSL_ENTER("DTLS_method_ex");
  11985. #ifndef NO_WOLFSSL_CLIENT
  11986. m = wolfDTLS_client_method_ex(heap);
  11987. #else
  11988. m = wolfDTLS_server_method_ex(heap);
  11989. #endif
  11990. if (m != NULL) {
  11991. m->side = WOLFSSL_NEITHER_END;
  11992. }
  11993. return m;
  11994. }
  11995. #ifndef NO_OLD_TLS
  11996. WOLFSSL_METHOD* wolfDTLSv1_method(void)
  11997. {
  11998. return wolfDTLSv1_method_ex(NULL);
  11999. }
  12000. WOLFSSL_METHOD* wolfDTLSv1_method_ex(void* heap)
  12001. {
  12002. WOLFSSL_METHOD* m;
  12003. WOLFSSL_ENTER("DTLSv1_method_ex");
  12004. #ifndef NO_WOLFSSL_CLIENT
  12005. m = wolfDTLSv1_client_method_ex(heap);
  12006. #else
  12007. m = wolfDTLSv1_server_method_ex(heap);
  12008. #endif
  12009. if (m != NULL) {
  12010. m->side = WOLFSSL_NEITHER_END;
  12011. }
  12012. return m;
  12013. }
  12014. #endif /* !NO_OLD_TLS */
  12015. #ifndef WOLFSSL_NO_TLS12
  12016. WOLFSSL_METHOD* wolfDTLSv1_2_method(void)
  12017. {
  12018. return wolfDTLSv1_2_method_ex(NULL);
  12019. }
  12020. WOLFSSL_METHOD* wolfDTLSv1_2_method_ex(void* heap)
  12021. {
  12022. WOLFSSL_METHOD* m;
  12023. WOLFSSL_ENTER("DTLSv1_2_method");
  12024. #ifndef NO_WOLFSSL_CLIENT
  12025. m = wolfDTLSv1_2_client_method_ex(heap);
  12026. #else
  12027. m = wolfDTLSv1_2_server_method_ex(heap);
  12028. #endif
  12029. if (m != NULL) {
  12030. m->side = WOLFSSL_NEITHER_END;
  12031. }
  12032. return m;
  12033. }
  12034. #endif /* !WOLFSSL_NO_TLS12 */
  12035. #endif /* WOLFSSL_DTLS */
  12036. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  12037. #ifndef NO_WOLFSSL_SERVER
  12038. WOLFSSL_METHOD* wolfTLS_server_method(void)
  12039. {
  12040. return wolfTLS_server_method_ex(NULL);
  12041. }
  12042. WOLFSSL_METHOD* wolfTLS_server_method_ex(void* heap)
  12043. {
  12044. WOLFSSL_METHOD* method =
  12045. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12046. heap, DYNAMIC_TYPE_METHOD);
  12047. (void)heap;
  12048. WOLFSSL_ENTER("TLS_server_method_ex");
  12049. if (method) {
  12050. #if defined(WOLFSSL_TLS13)
  12051. InitSSL_Method(method, MakeTLSv1_3());
  12052. #elif !defined(WOLFSSL_NO_TLS12)
  12053. InitSSL_Method(method, MakeTLSv1_2());
  12054. #elif !defined(NO_OLD_TLS)
  12055. InitSSL_Method(method, MakeTLSv1_1());
  12056. #elif defined(WOLFSSL_ALLOW_TLSV10)
  12057. InitSSL_Method(method, MakeTLSv1());
  12058. #else
  12059. #error No TLS version enabled!
  12060. #endif
  12061. method->downgrade = 1;
  12062. method->side = WOLFSSL_SERVER_END;
  12063. }
  12064. return method;
  12065. }
  12066. #ifndef NO_OLD_TLS
  12067. #ifdef WOLFSSL_ALLOW_TLSV10
  12068. WOLFSSL_METHOD* wolfTLSv1_server_method(void)
  12069. {
  12070. return wolfTLSv1_server_method_ex(NULL);
  12071. }
  12072. WOLFSSL_METHOD* wolfTLSv1_server_method_ex(void* heap)
  12073. {
  12074. WOLFSSL_METHOD* method =
  12075. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12076. heap, DYNAMIC_TYPE_METHOD);
  12077. (void)heap;
  12078. WOLFSSL_ENTER("TLSv1_server_method_ex");
  12079. if (method) {
  12080. InitSSL_Method(method, MakeTLSv1());
  12081. method->side = WOLFSSL_SERVER_END;
  12082. }
  12083. return method;
  12084. }
  12085. #endif /* WOLFSSL_ALLOW_TLSV10 */
  12086. WOLFSSL_METHOD* wolfTLSv1_1_server_method(void)
  12087. {
  12088. return wolfTLSv1_1_server_method_ex(NULL);
  12089. }
  12090. WOLFSSL_METHOD* wolfTLSv1_1_server_method_ex(void* heap)
  12091. {
  12092. WOLFSSL_METHOD* method =
  12093. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12094. heap, DYNAMIC_TYPE_METHOD);
  12095. (void)heap;
  12096. WOLFSSL_ENTER("TLSv1_1_server_method_ex");
  12097. if (method) {
  12098. InitSSL_Method(method, MakeTLSv1_1());
  12099. method->side = WOLFSSL_SERVER_END;
  12100. }
  12101. return method;
  12102. }
  12103. #endif /* !NO_OLD_TLS */
  12104. #ifndef WOLFSSL_NO_TLS12
  12105. WOLFSSL_ABI
  12106. WOLFSSL_METHOD* wolfTLSv1_2_server_method(void)
  12107. {
  12108. return wolfTLSv1_2_server_method_ex(NULL);
  12109. }
  12110. WOLFSSL_METHOD* wolfTLSv1_2_server_method_ex(void* heap)
  12111. {
  12112. WOLFSSL_METHOD* method =
  12113. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12114. heap, DYNAMIC_TYPE_METHOD);
  12115. (void)heap;
  12116. WOLFSSL_ENTER("TLSv1_2_server_method_ex");
  12117. if (method) {
  12118. InitSSL_Method(method, MakeTLSv1_2());
  12119. method->side = WOLFSSL_SERVER_END;
  12120. }
  12121. return method;
  12122. }
  12123. #endif /* !WOLFSSL_NO_TLS12 */
  12124. #ifdef WOLFSSL_TLS13
  12125. /* The TLS v1.3 server method data.
  12126. *
  12127. * returns the method data for a TLS v1.3 server.
  12128. */
  12129. WOLFSSL_ABI
  12130. WOLFSSL_METHOD* wolfTLSv1_3_server_method(void)
  12131. {
  12132. return wolfTLSv1_3_server_method_ex(NULL);
  12133. }
  12134. /* The TLS v1.3 server method data.
  12135. *
  12136. * heap The heap used for allocation.
  12137. * returns the method data for a TLS v1.3 server.
  12138. */
  12139. WOLFSSL_METHOD* wolfTLSv1_3_server_method_ex(void* heap)
  12140. {
  12141. WOLFSSL_METHOD* method =
  12142. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12143. heap, DYNAMIC_TYPE_METHOD);
  12144. (void)heap;
  12145. WOLFSSL_ENTER("TLSv1_3_server_method_ex");
  12146. if (method) {
  12147. InitSSL_Method(method, MakeTLSv1_3());
  12148. method->side = WOLFSSL_SERVER_END;
  12149. }
  12150. return method;
  12151. }
  12152. #endif /* WOLFSSL_TLS13 */
  12153. #ifdef WOLFSSL_DTLS
  12154. WOLFSSL_METHOD* wolfDTLS_server_method(void)
  12155. {
  12156. return wolfDTLS_server_method_ex(NULL);
  12157. }
  12158. WOLFSSL_METHOD* wolfDTLS_server_method_ex(void* heap)
  12159. {
  12160. WOLFSSL_METHOD* method =
  12161. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12162. heap, DYNAMIC_TYPE_METHOD);
  12163. (void)heap;
  12164. WOLFSSL_ENTER("DTLS_server_method_ex");
  12165. if (method) {
  12166. #if defined(WOLFSSL_DTLS13)
  12167. InitSSL_Method(method, MakeDTLSv1_3());
  12168. #elif !defined(WOLFSSL_NO_TLS12)
  12169. InitSSL_Method(method, MakeDTLSv1_2());
  12170. #elif !defined(NO_OLD_TLS)
  12171. InitSSL_Method(method, MakeDTLSv1());
  12172. #else
  12173. #error No DTLS version enabled!
  12174. #endif
  12175. method->downgrade = 1;
  12176. method->side = WOLFSSL_SERVER_END;
  12177. }
  12178. return method;
  12179. }
  12180. #ifndef NO_OLD_TLS
  12181. WOLFSSL_METHOD* wolfDTLSv1_server_method(void)
  12182. {
  12183. return wolfDTLSv1_server_method_ex(NULL);
  12184. }
  12185. WOLFSSL_METHOD* wolfDTLSv1_server_method_ex(void* heap)
  12186. {
  12187. WOLFSSL_METHOD* method =
  12188. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12189. heap, DYNAMIC_TYPE_METHOD);
  12190. (void)heap;
  12191. WOLFSSL_ENTER("DTLSv1_server_method_ex");
  12192. if (method) {
  12193. InitSSL_Method(method, MakeDTLSv1());
  12194. method->side = WOLFSSL_SERVER_END;
  12195. }
  12196. return method;
  12197. }
  12198. #endif /* !NO_OLD_TLS */
  12199. #ifndef WOLFSSL_NO_TLS12
  12200. WOLFSSL_METHOD* wolfDTLSv1_2_server_method(void)
  12201. {
  12202. return wolfDTLSv1_2_server_method_ex(NULL);
  12203. }
  12204. WOLFSSL_METHOD* wolfDTLSv1_2_server_method_ex(void* heap)
  12205. {
  12206. WOLFSSL_METHOD* method =
  12207. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  12208. heap, DYNAMIC_TYPE_METHOD);
  12209. WOLFSSL_ENTER("DTLSv1_2_server_method_ex");
  12210. (void)heap;
  12211. if (method) {
  12212. InitSSL_Method(method, MakeDTLSv1_2());
  12213. method->side = WOLFSSL_SERVER_END;
  12214. }
  12215. (void)heap;
  12216. return method;
  12217. }
  12218. #endif /* !WOLFSSL_NO_TLS12 */
  12219. #endif /* WOLFSSL_DTLS */
  12220. #endif /* NO_WOLFSSL_SERVER */
  12221. #endif /* NO_TLS */
  12222. #endif /* WOLFCRYPT_ONLY */