tls.c 414 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376
  1. /* tls.c
  2. *
  3. * Copyright (C) 2006-2022 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. #ifdef HAVE_LIBOQS
  46. #include <oqs/kem.h>
  47. #elif defined(HAVE_PQM4)
  48. #include "api_kyber.h"
  49. #define PQM4_PUBLIC_KEY_LENGTH CRYPTO_PUBLICKEYBYTES
  50. #define PQM4_PRIVATE_KEY_LENGTH CRYPTO_SECRETKEYBYTES
  51. #define PQM4_SHARED_SECRET_LENGTH CRYPTO_BYTES
  52. #define PQM4_CIPHERTEXT_LENGTH CRYPTO_CIPHERTEXTBYTES
  53. #endif
  54. #endif
  55. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  56. #include <wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h>
  57. #endif
  58. #ifndef NO_TLS
  59. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  60. static int TLSX_KeyShare_IsSupported(int namedGroup);
  61. static void TLSX_KeyShare_FreeAll(KeyShareEntry* list, void* heap);
  62. #endif
  63. #ifdef HAVE_SUPPORTED_CURVES
  64. static int TLSX_PopulateSupportedGroups(WOLFSSL* ssl, TLSX** extensions);
  65. #endif
  66. /* Digest enable checks */
  67. #ifdef NO_OLD_TLS /* TLS 1.2 only */
  68. #if defined(NO_SHA256) && !defined(WOLFSSL_SHA384) && \
  69. !defined(WOLFSSL_SHA512)
  70. #error Must have SHA256, SHA384 or SHA512 enabled for TLS 1.2
  71. #endif
  72. #else /* TLS 1.1 or older */
  73. #if defined(NO_MD5) && defined(NO_SHA)
  74. #error Must have SHA1 and MD5 enabled for old TLS
  75. #endif
  76. #endif
  77. #ifdef WOLFSSL_TLS13
  78. #if !defined(NO_DH) && \
  79. !defined(HAVE_FFDHE_2048) && !defined(HAVE_FFDHE_3072) && \
  80. !defined(HAVE_FFDHE_4096) && !defined(HAVE_FFDHE_6144) && \
  81. !defined(HAVE_FFDHE_8192)
  82. #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
  83. #endif
  84. #if !defined(NO_RSA) && !defined(WC_RSA_PSS)
  85. #error The build option WC_RSA_PSS is required for TLS 1.3 with RSA
  86. #endif
  87. #ifndef HAVE_TLS_EXTENSIONS
  88. #ifndef _MSC_VER
  89. #error "The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3"
  90. #else
  91. #pragma message("Error: The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3")
  92. #endif
  93. #endif
  94. #endif
  95. /* Warn if secrets logging is enabled */
  96. #if defined(SHOW_SECRETS) || defined(WOLFSSL_SSLKEYLOGFILE)
  97. #ifndef _MSC_VER
  98. #warning The SHOW_SECRETS and WOLFSSL_SSLKEYLOGFILE options should only be used for debugging and never in a production environment
  99. #else
  100. #pragma message("Warning: The SHOW_SECRETS and WOLFSSL_SSLKEYLOGFILE options should only be used for debugging and never in a production environment")
  101. #endif
  102. #endif
  103. /* Optional Pre-Master-Secret logging for Wireshark */
  104. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_SSLKEYLOGFILE)
  105. #ifndef WOLFSSL_SSLKEYLOGFILE_OUTPUT
  106. #define WOLFSSL_SSLKEYLOGFILE_OUTPUT "sslkeylog.log"
  107. #endif
  108. #endif
  109. #ifndef WOLFSSL_NO_TLS12
  110. #ifdef WOLFSSL_SHA384
  111. #define HSHASH_SZ WC_SHA384_DIGEST_SIZE
  112. #else
  113. #define HSHASH_SZ FINISHED_SZ
  114. #endif
  115. int BuildTlsHandshakeHash(WOLFSSL* ssl, byte* hash, word32* hashLen)
  116. {
  117. int ret = 0;
  118. word32 hashSz = FINISHED_SZ;
  119. if (ssl == NULL || hash == NULL || hashLen == NULL || *hashLen < HSHASH_SZ)
  120. return BAD_FUNC_ARG;
  121. /* for constant timing perform these even if error */
  122. #ifndef NO_OLD_TLS
  123. ret |= wc_Md5GetHash(&ssl->hsHashes->hashMd5, hash);
  124. ret |= wc_ShaGetHash(&ssl->hsHashes->hashSha, &hash[WC_MD5_DIGEST_SIZE]);
  125. #endif
  126. if (IsAtLeastTLSv1_2(ssl)) {
  127. #ifndef NO_SHA256
  128. if (ssl->specs.mac_algorithm <= sha256_mac ||
  129. ssl->specs.mac_algorithm == blake2b_mac) {
  130. ret |= wc_Sha256GetHash(&ssl->hsHashes->hashSha256, hash);
  131. hashSz = WC_SHA256_DIGEST_SIZE;
  132. }
  133. #endif
  134. #ifdef WOLFSSL_SHA384
  135. if (ssl->specs.mac_algorithm == sha384_mac) {
  136. ret |= wc_Sha384GetHash(&ssl->hsHashes->hashSha384, hash);
  137. hashSz = WC_SHA384_DIGEST_SIZE;
  138. }
  139. #endif
  140. }
  141. *hashLen = hashSz;
  142. #ifdef WOLFSSL_CHECK_MEM_ZERO
  143. wc_MemZero_Add("TLS handshake hash", hash, hashSz);
  144. #endif
  145. if (ret != 0) {
  146. ret = BUILD_MSG_ERROR;
  147. WOLFSSL_ERROR_VERBOSE(ret);
  148. }
  149. return ret;
  150. }
  151. int BuildTlsFinished(WOLFSSL* ssl, Hashes* hashes, const byte* sender)
  152. {
  153. int ret;
  154. const byte* side = NULL;
  155. word32 hashSz = HSHASH_SZ;
  156. #if !defined(WOLFSSL_ASYNC_CRYPT) || defined(WC_ASYNC_NO_HASH)
  157. byte handshake_hash[HSHASH_SZ];
  158. #else
  159. WC_DECLARE_VAR(handshake_hash, byte, HSHASH_SZ, ssl->heap);
  160. if (handshake_hash == NULL)
  161. return MEMORY_E;
  162. #endif
  163. ret = BuildTlsHandshakeHash(ssl, handshake_hash, &hashSz);
  164. if (ret == 0) {
  165. if (XSTRNCMP((const char*)sender, (const char*)kTlsClientStr,
  166. SIZEOF_SENDER) == 0) {
  167. side = kTlsClientFinStr;
  168. }
  169. else if (XSTRNCMP((const char*)sender, (const char*)kTlsServerStr,
  170. SIZEOF_SENDER) == 0) {
  171. side = kTlsServerFinStr;
  172. }
  173. else {
  174. ret = BAD_FUNC_ARG;
  175. WOLFSSL_MSG("Unexpected sender value");
  176. }
  177. }
  178. if (ret == 0) {
  179. #ifdef WOLFSSL_HAVE_PRF
  180. #if !defined(NO_CERTS) && defined(HAVE_PK_CALLBACKS)
  181. if (ssl->ctx->TlsFinishedCb) {
  182. void* ctx = wolfSSL_GetTlsFinishedCtx(ssl);
  183. ret = ssl->ctx->TlsFinishedCb(ssl, side, handshake_hash,
  184. (byte*)hashes, ctx);
  185. }
  186. if (!ssl->ctx->TlsFinishedCb || ret == PROTOCOLCB_UNAVAILABLE)
  187. #endif
  188. {
  189. PRIVATE_KEY_UNLOCK();
  190. ret = wc_PRF_TLS((byte*)hashes, TLS_FINISHED_SZ,
  191. ssl->arrays->masterSecret,
  192. SECRET_LEN, side, FINISHED_LABEL_SZ, handshake_hash, hashSz,
  193. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  194. ssl->heap, ssl->devId);
  195. PRIVATE_KEY_LOCK();
  196. }
  197. ForceZero(handshake_hash, hashSz);
  198. #else
  199. /* Pseudo random function must be enabled in the configuration. */
  200. ret = PRF_MISSING;
  201. WOLFSSL_ERROR_VERBOSE(ret);
  202. WOLFSSL_MSG("Pseudo-random function is not enabled");
  203. (void)side;
  204. (void)hashes;
  205. #endif
  206. }
  207. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  208. WC_FREE_VAR(handshake_hash, ssl->heap);
  209. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  210. wc_MemZero_Check(handshake_hash, HSHASH_SZ);
  211. #endif
  212. return ret;
  213. }
  214. #endif /* !WOLFSSL_NO_TLS12 */
  215. #ifndef NO_OLD_TLS
  216. #ifdef WOLFSSL_ALLOW_TLSV10
  217. ProtocolVersion MakeTLSv1(void)
  218. {
  219. ProtocolVersion pv;
  220. pv.major = SSLv3_MAJOR;
  221. pv.minor = TLSv1_MINOR;
  222. return pv;
  223. }
  224. #endif /* WOLFSSL_ALLOW_TLSV10 */
  225. ProtocolVersion MakeTLSv1_1(void)
  226. {
  227. ProtocolVersion pv;
  228. pv.major = SSLv3_MAJOR;
  229. pv.minor = TLSv1_1_MINOR;
  230. return pv;
  231. }
  232. #endif /* !NO_OLD_TLS */
  233. #ifndef WOLFSSL_NO_TLS12
  234. ProtocolVersion MakeTLSv1_2(void)
  235. {
  236. ProtocolVersion pv;
  237. pv.major = SSLv3_MAJOR;
  238. pv.minor = TLSv1_2_MINOR;
  239. return pv;
  240. }
  241. #endif /* !WOLFSSL_NO_TLS12 */
  242. #ifdef WOLFSSL_TLS13
  243. /* The TLS v1.3 protocol version.
  244. *
  245. * returns the protocol version data for TLS v1.3.
  246. */
  247. ProtocolVersion MakeTLSv1_3(void)
  248. {
  249. ProtocolVersion pv;
  250. pv.major = SSLv3_MAJOR;
  251. pv.minor = TLSv1_3_MINOR;
  252. return pv;
  253. }
  254. #endif
  255. #ifndef WOLFSSL_NO_TLS12
  256. #ifdef HAVE_EXTENDED_MASTER
  257. static const byte ext_master_label[EXT_MASTER_LABEL_SZ + 1] =
  258. "extended master secret";
  259. #endif
  260. static const byte master_label[MASTER_LABEL_SZ + 1] = "master secret";
  261. static const byte key_label [KEY_LABEL_SZ + 1] = "key expansion";
  262. static int _DeriveTlsKeys(byte* key_dig, word32 key_dig_len,
  263. const byte* ms, word32 msLen,
  264. const byte* sr, const byte* cr,
  265. int tls1_2, int hash_type,
  266. void* heap, int devId)
  267. {
  268. int ret;
  269. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  270. WC_DECLARE_VAR(seed, byte, SEED_LEN, heap);
  271. if (seed == NULL)
  272. return MEMORY_E;
  273. #else
  274. byte seed[SEED_LEN];
  275. #endif
  276. XMEMCPY(seed, sr, RAN_LEN);
  277. XMEMCPY(seed + RAN_LEN, cr, RAN_LEN);
  278. #ifdef WOLFSSL_HAVE_PRF
  279. PRIVATE_KEY_UNLOCK();
  280. ret = wc_PRF_TLS(key_dig, key_dig_len, ms, msLen, key_label, KEY_LABEL_SZ,
  281. seed, SEED_LEN, tls1_2, hash_type, heap, devId);
  282. PRIVATE_KEY_LOCK();
  283. #else
  284. /* Pseudo random function must be enabled in the configuration. */
  285. ret = PRF_MISSING;
  286. WOLFSSL_ERROR_VERBOSE(ret);
  287. WOLFSSL_MSG("Pseudo-random function is not enabled");
  288. (void)key_dig;
  289. (void)key_dig_len;
  290. (void)ms;
  291. (void)msLen;
  292. (void)tls1_2;
  293. (void)hash_type;
  294. (void)heap;
  295. (void)devId;
  296. (void)key_label;
  297. (void)master_label;
  298. #ifdef HAVE_EXTENDED_MASTER
  299. (void)ext_master_label;
  300. #endif
  301. #endif
  302. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  303. WC_FREE_VAR(seed, heap);
  304. #endif
  305. return ret;
  306. }
  307. /* External facing wrapper so user can call as well, 0 on success */
  308. int wolfSSL_DeriveTlsKeys(byte* key_dig, word32 key_dig_len,
  309. const byte* ms, word32 msLen,
  310. const byte* sr, const byte* cr,
  311. int tls1_2, int hash_type)
  312. {
  313. return _DeriveTlsKeys(key_dig, key_dig_len, ms, msLen, sr, cr, tls1_2,
  314. hash_type, NULL, INVALID_DEVID);
  315. }
  316. int DeriveTlsKeys(WOLFSSL* ssl)
  317. {
  318. int ret;
  319. int key_dig_len = 2 * ssl->specs.hash_size +
  320. 2 * ssl->specs.key_size +
  321. 2 * ssl->specs.iv_size;
  322. #ifdef WOLFSSL_SMALL_STACK
  323. byte* key_dig;
  324. #else
  325. byte key_dig[MAX_PRF_DIG];
  326. #endif
  327. #ifdef WOLFSSL_SMALL_STACK
  328. key_dig = (byte*)XMALLOC(MAX_PRF_DIG, ssl->heap, DYNAMIC_TYPE_DIGEST);
  329. if (key_dig == NULL) {
  330. return MEMORY_E;
  331. }
  332. #endif
  333. #if !defined(NO_CERTS) && defined(HAVE_PK_CALLBACKS)
  334. ret = PROTOCOLCB_UNAVAILABLE;
  335. if (ssl->ctx->GenSessionKeyCb) {
  336. void* ctx = wolfSSL_GetGenSessionKeyCtx(ssl);
  337. ret = ssl->ctx->GenSessionKeyCb(ssl, ctx);
  338. }
  339. if (!ssl->ctx->GenSessionKeyCb || ret == PROTOCOLCB_UNAVAILABLE)
  340. #endif
  341. ret = _DeriveTlsKeys(key_dig, key_dig_len,
  342. ssl->arrays->masterSecret, SECRET_LEN,
  343. ssl->arrays->serverRandom, ssl->arrays->clientRandom,
  344. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  345. ssl->heap, ssl->devId);
  346. if (ret == 0)
  347. ret = StoreKeys(ssl, key_dig, PROVISION_CLIENT_SERVER);
  348. #ifdef WOLFSSL_SMALL_STACK
  349. XFREE(key_dig, ssl->heap, DYNAMIC_TYPE_DIGEST);
  350. #endif
  351. return ret;
  352. }
  353. static int _MakeTlsMasterSecret(byte* ms, word32 msLen,
  354. const byte* pms, word32 pmsLen,
  355. const byte* cr, const byte* sr,
  356. int tls1_2, int hash_type,
  357. void* heap, int devId)
  358. {
  359. int ret;
  360. #if !defined(WOLFSSL_ASYNC_CRYPT) || defined(WC_ASYNC_NO_HASH)
  361. byte seed[SEED_LEN];
  362. #else
  363. WC_DECLARE_VAR(seed, byte, SEED_LEN, heap);
  364. if (seed == NULL)
  365. return MEMORY_E;
  366. #endif
  367. XMEMCPY(seed, cr, RAN_LEN);
  368. XMEMCPY(seed + RAN_LEN, sr, RAN_LEN);
  369. #ifdef WOLFSSL_HAVE_PRF
  370. PRIVATE_KEY_UNLOCK();
  371. ret = wc_PRF_TLS(ms, msLen, pms, pmsLen, master_label, MASTER_LABEL_SZ,
  372. seed, SEED_LEN, tls1_2, hash_type, heap, devId);
  373. PRIVATE_KEY_LOCK();
  374. #else
  375. /* Pseudo random function must be enabled in the configuration. */
  376. ret = PRF_MISSING;
  377. WOLFSSL_MSG("Pseudo-random function is not enabled");
  378. (void)ms;
  379. (void)msLen;
  380. (void)pms;
  381. (void)pmsLen;
  382. (void)tls1_2;
  383. (void)hash_type;
  384. (void)heap;
  385. (void)devId;
  386. #endif
  387. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  388. WC_FREE_VAR(seed, heap);
  389. #endif
  390. return ret;
  391. }
  392. /* External facing wrapper so user can call as well, 0 on success */
  393. int wolfSSL_MakeTlsMasterSecret(byte* ms, word32 msLen,
  394. const byte* pms, word32 pmsLen,
  395. const byte* cr, const byte* sr,
  396. int tls1_2, int hash_type)
  397. {
  398. return _MakeTlsMasterSecret(ms, msLen, pms, pmsLen, cr, sr, tls1_2,
  399. hash_type, NULL, INVALID_DEVID);
  400. }
  401. #ifdef HAVE_EXTENDED_MASTER
  402. static int _MakeTlsExtendedMasterSecret(byte* ms, word32 msLen,
  403. const byte* pms, word32 pmsLen,
  404. const byte* sHash, word32 sHashLen,
  405. int tls1_2, int hash_type,
  406. void* heap, int devId)
  407. {
  408. int ret;
  409. #ifdef WOLFSSL_HAVE_PRF
  410. PRIVATE_KEY_UNLOCK();
  411. ret = wc_PRF_TLS(ms, msLen, pms, pmsLen, ext_master_label, EXT_MASTER_LABEL_SZ,
  412. sHash, sHashLen, tls1_2, hash_type, heap, devId);
  413. PRIVATE_KEY_LOCK();
  414. #else
  415. /* Pseudo random function must be enabled in the configuration. */
  416. ret = PRF_MISSING;
  417. WOLFSSL_MSG("Pseudo-random function is not enabled");
  418. (void)ms;
  419. (void)msLen;
  420. (void)pms;
  421. (void)pmsLen;
  422. (void)sHash;
  423. (void)sHashLen;
  424. (void)tls1_2;
  425. (void)hash_type;
  426. (void)heap;
  427. (void)devId;
  428. #endif
  429. return ret;
  430. }
  431. /* External facing wrapper so user can call as well, 0 on success */
  432. int wolfSSL_MakeTlsExtendedMasterSecret(byte* ms, word32 msLen,
  433. const byte* pms, word32 pmsLen,
  434. const byte* sHash, word32 sHashLen,
  435. int tls1_2, int hash_type)
  436. {
  437. return _MakeTlsExtendedMasterSecret(ms, msLen, pms, pmsLen, sHash, sHashLen,
  438. tls1_2, hash_type, NULL, INVALID_DEVID);
  439. }
  440. #endif /* HAVE_EXTENDED_MASTER */
  441. int MakeTlsMasterSecret(WOLFSSL* ssl)
  442. {
  443. int ret;
  444. #ifdef HAVE_EXTENDED_MASTER
  445. if (ssl->options.haveEMS) {
  446. word32 hashSz = HSHASH_SZ;
  447. #ifdef WOLFSSL_SMALL_STACK
  448. byte* handshake_hash = (byte*)XMALLOC(HSHASH_SZ, ssl->heap,
  449. DYNAMIC_TYPE_DIGEST);
  450. if (handshake_hash == NULL)
  451. return MEMORY_E;
  452. #else
  453. byte handshake_hash[HSHASH_SZ];
  454. #endif
  455. ret = BuildTlsHandshakeHash(ssl, handshake_hash, &hashSz);
  456. if (ret == 0) {
  457. ret = _MakeTlsExtendedMasterSecret(
  458. ssl->arrays->masterSecret, SECRET_LEN,
  459. ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz,
  460. handshake_hash, hashSz,
  461. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  462. ssl->heap, ssl->devId);
  463. ForceZero(handshake_hash, hashSz);
  464. }
  465. #ifdef WOLFSSL_SMALL_STACK
  466. XFREE(handshake_hash, ssl->heap, DYNAMIC_TYPE_DIGEST);
  467. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  468. wc_MemZero_Check(handshake_hash, HSHASH_SZ);
  469. #endif
  470. }
  471. else
  472. #endif /* HAVE_EXTENDED_MASTER */
  473. {
  474. #if !defined(NO_CERTS) && defined(HAVE_PK_CALLBACKS)
  475. ret = PROTOCOLCB_UNAVAILABLE;
  476. if (ssl->ctx->GenMasterCb) {
  477. void* ctx = wolfSSL_GetGenMasterSecretCtx(ssl);
  478. ret = ssl->ctx->GenMasterCb(ssl, ctx);
  479. }
  480. if (!ssl->ctx->GenMasterCb || ret == PROTOCOLCB_UNAVAILABLE)
  481. #endif
  482. ret = _MakeTlsMasterSecret(ssl->arrays->masterSecret, SECRET_LEN,
  483. ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz,
  484. ssl->arrays->clientRandom, ssl->arrays->serverRandom,
  485. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  486. ssl->heap, ssl->devId);
  487. }
  488. if (ret == 0) {
  489. #ifdef SHOW_SECRETS
  490. /* Wireshark Pre-Master-Secret Format:
  491. * CLIENT_RANDOM <clientrandom> <mastersecret>
  492. */
  493. const char* CLIENT_RANDOM_LABEL = "CLIENT_RANDOM";
  494. int i, pmsPos = 0;
  495. char pmsBuf[13 + 1 + 64 + 1 + 96 + 1 + 1];
  496. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%s ",
  497. CLIENT_RANDOM_LABEL);
  498. pmsPos += XSTRLEN(CLIENT_RANDOM_LABEL) + 1;
  499. for (i = 0; i < RAN_LEN; i++) {
  500. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%02x",
  501. ssl->arrays->clientRandom[i]);
  502. pmsPos += 2;
  503. }
  504. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, " ");
  505. pmsPos += 1;
  506. for (i = 0; i < SECRET_LEN; i++) {
  507. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%02x",
  508. ssl->arrays->masterSecret[i]);
  509. pmsPos += 2;
  510. }
  511. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "\n");
  512. pmsPos += 1;
  513. /* print master secret */
  514. puts(pmsBuf);
  515. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_SSLKEYLOGFILE)
  516. {
  517. FILE* f = XFOPEN(WOLFSSL_SSLKEYLOGFILE_OUTPUT, "a");
  518. if (f != XBADFILE) {
  519. XFWRITE(pmsBuf, 1, pmsPos, f);
  520. XFCLOSE(f);
  521. }
  522. }
  523. #endif
  524. #endif /* SHOW_SECRETS */
  525. ret = DeriveTlsKeys(ssl);
  526. }
  527. return ret;
  528. }
  529. /* Used by EAP-TLS and EAP-TTLS to derive keying material from
  530. * the master_secret. */
  531. int wolfSSL_make_eap_keys(WOLFSSL* ssl, void* msk, unsigned int len,
  532. const char* label)
  533. {
  534. int ret;
  535. #ifdef WOLFSSL_SMALL_STACK
  536. byte* seed;
  537. #else
  538. byte seed[SEED_LEN];
  539. #endif
  540. #ifdef WOLFSSL_SMALL_STACK
  541. seed = (byte*)XMALLOC(SEED_LEN, ssl->heap, DYNAMIC_TYPE_SEED);
  542. if (seed == NULL)
  543. return MEMORY_E;
  544. #endif
  545. /*
  546. * As per RFC-5281, the order of the client and server randoms is reversed
  547. * from that used by the TLS protocol to derive keys.
  548. */
  549. XMEMCPY(seed, ssl->arrays->clientRandom, RAN_LEN);
  550. XMEMCPY(seed + RAN_LEN, ssl->arrays->serverRandom, RAN_LEN);
  551. #ifdef WOLFSSL_HAVE_PRF
  552. PRIVATE_KEY_UNLOCK();
  553. ret = wc_PRF_TLS((byte*)msk, len, ssl->arrays->masterSecret, SECRET_LEN,
  554. (const byte *)label, (word32)XSTRLEN(label), seed, SEED_LEN,
  555. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  556. ssl->heap, ssl->devId);
  557. PRIVATE_KEY_LOCK();
  558. #else
  559. /* Pseudo random function must be enabled in the configuration. */
  560. ret = PRF_MISSING;
  561. WOLFSSL_MSG("Pseudo-random function is not enabled");
  562. (void)msk;
  563. (void)len;
  564. (void)label;
  565. #endif
  566. #ifdef WOLFSSL_SMALL_STACK
  567. XFREE(seed, ssl->heap, DYNAMIC_TYPE_SEED);
  568. #endif
  569. return ret;
  570. }
  571. /* return HMAC digest type in wolfSSL format */
  572. int wolfSSL_GetHmacType(WOLFSSL* ssl)
  573. {
  574. if (ssl == NULL)
  575. return BAD_FUNC_ARG;
  576. switch (ssl->specs.mac_algorithm) {
  577. #ifndef NO_MD5
  578. case md5_mac:
  579. {
  580. return WC_MD5;
  581. }
  582. #endif
  583. #ifndef NO_SHA256
  584. case sha256_mac:
  585. {
  586. return WC_SHA256;
  587. }
  588. #endif
  589. #ifdef WOLFSSL_SHA384
  590. case sha384_mac:
  591. {
  592. return WC_SHA384;
  593. }
  594. #endif
  595. #ifndef NO_SHA
  596. case sha_mac:
  597. {
  598. return WC_SHA;
  599. }
  600. #endif
  601. #ifdef HAVE_BLAKE2
  602. case blake2b_mac:
  603. {
  604. return BLAKE2B_ID;
  605. }
  606. #endif
  607. default:
  608. {
  609. return WOLFSSL_FATAL_ERROR;
  610. }
  611. }
  612. }
  613. int wolfSSL_SetTlsHmacInner(WOLFSSL* ssl, byte* inner, word32 sz, int content,
  614. int verify)
  615. {
  616. if (ssl == NULL || inner == NULL)
  617. return BAD_FUNC_ARG;
  618. XMEMSET(inner, 0, WOLFSSL_TLS_HMAC_INNER_SZ);
  619. WriteSEQ(ssl, verify, inner);
  620. inner[SEQ_SZ] = (byte)content;
  621. inner[SEQ_SZ + ENUM_LEN] = ssl->version.major;
  622. inner[SEQ_SZ + ENUM_LEN + ENUM_LEN] = ssl->version.minor;
  623. c16toa((word16)sz, inner + SEQ_SZ + ENUM_LEN + VERSION_SZ);
  624. return 0;
  625. }
  626. #ifndef WOLFSSL_AEAD_ONLY
  627. #if !defined(WOLFSSL_NO_HASH_RAW) && !defined(HAVE_FIPS) && \
  628. !defined(HAVE_SELFTEST)
  629. /* Update the hash in the HMAC.
  630. *
  631. * hmac HMAC object.
  632. * data Data to be hashed.
  633. * sz Size of data to hash.
  634. * returns 0 on success, otherwise failure.
  635. */
  636. static int Hmac_HashUpdate(Hmac* hmac, const byte* data, word32 sz)
  637. {
  638. int ret = BAD_FUNC_ARG;
  639. switch (hmac->macType) {
  640. #ifndef NO_SHA
  641. case WC_SHA:
  642. ret = wc_ShaUpdate(&hmac->hash.sha, data, sz);
  643. break;
  644. #endif /* !NO_SHA */
  645. #ifndef NO_SHA256
  646. case WC_SHA256:
  647. ret = wc_Sha256Update(&hmac->hash.sha256, data, sz);
  648. break;
  649. #endif /* !NO_SHA256 */
  650. #ifdef WOLFSSL_SHA384
  651. case WC_SHA384:
  652. ret = wc_Sha384Update(&hmac->hash.sha384, data, sz);
  653. break;
  654. #endif /* WOLFSSL_SHA384 */
  655. #ifdef WOLFSSL_SHA512
  656. case WC_SHA512:
  657. ret = wc_Sha512Update(&hmac->hash.sha512, data, sz);
  658. break;
  659. #endif /* WOLFSSL_SHA512 */
  660. default:
  661. break;
  662. }
  663. return ret;
  664. }
  665. /* Finalize the hash but don't put the EOC, padding or length in.
  666. *
  667. * hmac HMAC object.
  668. * hash Hash result.
  669. * returns 0 on success, otherwise failure.
  670. */
  671. static int Hmac_HashFinalRaw(Hmac* hmac, unsigned char* hash)
  672. {
  673. int ret = BAD_FUNC_ARG;
  674. switch (hmac->macType) {
  675. #ifndef NO_SHA
  676. case WC_SHA:
  677. ret = wc_ShaFinalRaw(&hmac->hash.sha, hash);
  678. break;
  679. #endif /* !NO_SHA */
  680. #ifndef NO_SHA256
  681. case WC_SHA256:
  682. ret = wc_Sha256FinalRaw(&hmac->hash.sha256, hash);
  683. break;
  684. #endif /* !NO_SHA256 */
  685. #ifdef WOLFSSL_SHA384
  686. case WC_SHA384:
  687. ret = wc_Sha384FinalRaw(&hmac->hash.sha384, hash);
  688. break;
  689. #endif /* WOLFSSL_SHA384 */
  690. #ifdef WOLFSSL_SHA512
  691. case WC_SHA512:
  692. ret = wc_Sha512FinalRaw(&hmac->hash.sha512, hash);
  693. break;
  694. #endif /* WOLFSSL_SHA512 */
  695. default:
  696. break;
  697. }
  698. return ret;
  699. }
  700. /* Finalize the HMAC by performing outer hash.
  701. *
  702. * hmac HMAC object.
  703. * mac MAC result.
  704. * returns 0 on success, otherwise failure.
  705. */
  706. static int Hmac_OuterHash(Hmac* hmac, unsigned char* mac)
  707. {
  708. int ret = BAD_FUNC_ARG;
  709. wc_HashAlg hash;
  710. enum wc_HashType hashType = (enum wc_HashType)hmac->macType;
  711. int digestSz = wc_HashGetDigestSize(hashType);
  712. int blockSz = wc_HashGetBlockSize(hashType);
  713. if ((digestSz >= 0) && (blockSz >= 0)) {
  714. ret = wc_HashInit(&hash, hashType);
  715. }
  716. if (ret == 0) {
  717. ret = wc_HashUpdate(&hash, hashType, (byte*)hmac->opad,
  718. blockSz);
  719. if (ret == 0)
  720. ret = wc_HashUpdate(&hash, hashType, (byte*)hmac->innerHash,
  721. digestSz);
  722. if (ret == 0)
  723. ret = wc_HashFinal(&hash, hashType, mac);
  724. wc_HashFree(&hash, hashType);
  725. }
  726. return ret;
  727. }
  728. /* Calculate the HMAC of the header + message data.
  729. * Constant time implementation using wc_Sha*FinalRaw().
  730. *
  731. * hmac HMAC object.
  732. * digest MAC result.
  733. * in Message data.
  734. * sz Size of the message data.
  735. * header Constructed record header with length of handshake data.
  736. * returns 0 on success, otherwise failure.
  737. */
  738. static int Hmac_UpdateFinal_CT(Hmac* hmac, byte* digest, const byte* in,
  739. word32 sz, int macLen, byte* header)
  740. {
  741. byte lenBytes[8];
  742. int i, j;
  743. unsigned int k;
  744. int blockBits, blockMask;
  745. int lastBlockLen, extraLen, eocIndex;
  746. int blocks, safeBlocks, lenBlock, eocBlock;
  747. unsigned int maxLen;
  748. int blockSz, padSz;
  749. int ret;
  750. word32 realLen;
  751. byte extraBlock;
  752. switch (hmac->macType) {
  753. #ifndef NO_SHA
  754. case WC_SHA:
  755. blockSz = WC_SHA_BLOCK_SIZE;
  756. blockBits = 6;
  757. padSz = WC_SHA_BLOCK_SIZE - WC_SHA_PAD_SIZE + 1;
  758. break;
  759. #endif /* !NO_SHA */
  760. #ifndef NO_SHA256
  761. case WC_SHA256:
  762. blockSz = WC_SHA256_BLOCK_SIZE;
  763. blockBits = 6;
  764. padSz = WC_SHA256_BLOCK_SIZE - WC_SHA256_PAD_SIZE + 1;
  765. break;
  766. #endif /* !NO_SHA256 */
  767. #ifdef WOLFSSL_SHA384
  768. case WC_SHA384:
  769. blockSz = WC_SHA384_BLOCK_SIZE;
  770. blockBits = 7;
  771. padSz = WC_SHA384_BLOCK_SIZE - WC_SHA384_PAD_SIZE + 1;
  772. break;
  773. #endif /* WOLFSSL_SHA384 */
  774. #ifdef WOLFSSL_SHA512
  775. case WC_SHA512:
  776. blockSz = WC_SHA512_BLOCK_SIZE;
  777. blockBits = 7;
  778. padSz = WC_SHA512_BLOCK_SIZE - WC_SHA512_PAD_SIZE + 1;
  779. break;
  780. #endif /* WOLFSSL_SHA512 */
  781. default:
  782. return BAD_FUNC_ARG;
  783. }
  784. blockMask = blockSz - 1;
  785. /* Size of data to HMAC if padding length byte is zero. */
  786. maxLen = WOLFSSL_TLS_HMAC_INNER_SZ + sz - 1 - macLen;
  787. /* Complete data (including padding) has block for EOC and/or length. */
  788. extraBlock = ctSetLTE((maxLen + padSz) & blockMask, padSz);
  789. /* Total number of blocks for data including padding. */
  790. blocks = ((maxLen + blockSz - 1) >> blockBits) + extraBlock;
  791. /* Up to last 6 blocks can be hashed safely. */
  792. safeBlocks = blocks - 6;
  793. /* Length of message data. */
  794. realLen = maxLen - in[sz - 1];
  795. /* Number of message bytes in last block. */
  796. lastBlockLen = realLen & blockMask;
  797. /* Number of padding bytes in last block. */
  798. extraLen = ((blockSz * 2 - padSz - lastBlockLen) & blockMask) + 1;
  799. /* Number of blocks to create for hash. */
  800. lenBlock = (realLen + extraLen) >> blockBits;
  801. /* Block containing EOC byte. */
  802. eocBlock = realLen >> blockBits;
  803. /* Index of EOC byte in block. */
  804. eocIndex = realLen & blockMask;
  805. /* Add length of hmac's ipad to total length. */
  806. realLen += blockSz;
  807. /* Length as bits - 8 bytes bigendian. */
  808. c32toa(realLen >> ((sizeof(word32) * 8) - 3), lenBytes);
  809. c32toa(realLen << 3, lenBytes + sizeof(word32));
  810. ret = Hmac_HashUpdate(hmac, (unsigned char*)hmac->ipad, blockSz);
  811. if (ret != 0)
  812. return ret;
  813. XMEMSET(hmac->innerHash, 0, macLen);
  814. if (safeBlocks > 0) {
  815. ret = Hmac_HashUpdate(hmac, header, WOLFSSL_TLS_HMAC_INNER_SZ);
  816. if (ret != 0)
  817. return ret;
  818. ret = Hmac_HashUpdate(hmac, in, safeBlocks * blockSz -
  819. WOLFSSL_TLS_HMAC_INNER_SZ);
  820. if (ret != 0)
  821. return ret;
  822. }
  823. else
  824. safeBlocks = 0;
  825. XMEMSET(digest, 0, macLen);
  826. k = safeBlocks * blockSz;
  827. for (i = safeBlocks; i < blocks; i++) {
  828. unsigned char hashBlock[WC_MAX_BLOCK_SIZE];
  829. unsigned char isEocBlock = ctMaskEq(i, eocBlock);
  830. unsigned char isOutBlock = ctMaskEq(i, lenBlock);
  831. for (j = 0; j < blockSz; j++) {
  832. unsigned char atEoc = ctMaskEq(j, eocIndex) & isEocBlock;
  833. unsigned char pastEoc = ctMaskGT(j, eocIndex) & isEocBlock;
  834. unsigned char b = 0;
  835. if (k < WOLFSSL_TLS_HMAC_INNER_SZ)
  836. b = header[k];
  837. else if (k < maxLen)
  838. b = in[k - WOLFSSL_TLS_HMAC_INNER_SZ];
  839. k++;
  840. b = ctMaskSel(atEoc, 0x80, b);
  841. b &= (unsigned char)~(word32)pastEoc;
  842. b &= ((unsigned char)~(word32)isOutBlock) | isEocBlock;
  843. if (j >= blockSz - 8) {
  844. b = ctMaskSel(isOutBlock, lenBytes[j - (blockSz - 8)], b);
  845. }
  846. hashBlock[j] = b;
  847. }
  848. ret = Hmac_HashUpdate(hmac, hashBlock, blockSz);
  849. if (ret != 0)
  850. return ret;
  851. ret = Hmac_HashFinalRaw(hmac, hashBlock);
  852. if (ret != 0)
  853. return ret;
  854. for (j = 0; j < macLen; j++)
  855. ((unsigned char*)hmac->innerHash)[j] |= hashBlock[j] & isOutBlock;
  856. }
  857. ret = Hmac_OuterHash(hmac, digest);
  858. return ret;
  859. }
  860. #endif
  861. #if defined(WOLFSSL_NO_HASH_RAW) || defined(HAVE_FIPS) || \
  862. defined(HAVE_SELFTEST) || defined(HAVE_BLAKE2)
  863. /* Calculate the HMAC of the header + message data.
  864. * Constant time implementation using normal hashing operations.
  865. * Update-Final need to be constant time.
  866. *
  867. * hmac HMAC object.
  868. * digest MAC result.
  869. * in Message data.
  870. * sz Size of the message data.
  871. * header Constructed record header with length of handshake data.
  872. * returns 0 on success, otherwise failure.
  873. */
  874. static int Hmac_UpdateFinal(Hmac* hmac, byte* digest, const byte* in,
  875. word32 sz, byte* header)
  876. {
  877. byte dummy[WC_MAX_BLOCK_SIZE] = {0};
  878. int ret;
  879. word32 msgSz, blockSz, macSz, padSz, maxSz, realSz;
  880. word32 currSz, offset = 0;
  881. int msgBlocks, blocks, blockBits;
  882. int i;
  883. switch (hmac->macType) {
  884. #ifndef NO_SHA
  885. case WC_SHA:
  886. blockSz = WC_SHA_BLOCK_SIZE;
  887. blockBits = 6;
  888. macSz = WC_SHA_DIGEST_SIZE;
  889. padSz = WC_SHA_BLOCK_SIZE - WC_SHA_PAD_SIZE + 1;
  890. break;
  891. #endif /* !NO_SHA */
  892. #ifndef NO_SHA256
  893. case WC_SHA256:
  894. blockSz = WC_SHA256_BLOCK_SIZE;
  895. blockBits = 6;
  896. macSz = WC_SHA256_DIGEST_SIZE;
  897. padSz = WC_SHA256_BLOCK_SIZE - WC_SHA256_PAD_SIZE + 1;
  898. break;
  899. #endif /* !NO_SHA256 */
  900. #ifdef WOLFSSL_SHA384
  901. case WC_SHA384:
  902. blockSz = WC_SHA384_BLOCK_SIZE;
  903. blockBits = 7;
  904. macSz = WC_SHA384_DIGEST_SIZE;
  905. padSz = WC_SHA384_BLOCK_SIZE - WC_SHA384_PAD_SIZE + 1;
  906. break;
  907. #endif /* WOLFSSL_SHA384 */
  908. #ifdef WOLFSSL_SHA512
  909. case WC_SHA512:
  910. blockSz = WC_SHA512_BLOCK_SIZE;
  911. blockBits = 7;
  912. macSz = WC_SHA512_DIGEST_SIZE;
  913. padSz = WC_SHA512_BLOCK_SIZE - WC_SHA512_PAD_SIZE + 1;
  914. break;
  915. #endif /* WOLFSSL_SHA512 */
  916. #ifdef HAVE_BLAKE2
  917. case WC_HASH_TYPE_BLAKE2B:
  918. blockSz = BLAKE2B_BLOCKBYTES;
  919. blockBits = 7;
  920. macSz = BLAKE2B_256;
  921. padSz = 0;
  922. break;
  923. #endif /* HAVE_BLAKE2 */
  924. default:
  925. return BAD_FUNC_ARG;
  926. }
  927. msgSz = sz - (1 + in[sz - 1] + macSz);
  928. /* Make negative result 0 */
  929. msgSz &= ~(0 - (msgSz >> 31));
  930. realSz = WOLFSSL_TLS_HMAC_INNER_SZ + msgSz;
  931. maxSz = WOLFSSL_TLS_HMAC_INNER_SZ + (sz - 1) - macSz;
  932. /* Calculate #blocks processed in HMAC for max and real data. */
  933. blocks = maxSz >> blockBits;
  934. blocks += ((maxSz + padSz) % blockSz) < padSz;
  935. msgBlocks = realSz >> blockBits;
  936. /* #Extra blocks to process. */
  937. blocks -= msgBlocks + ((((realSz + padSz) % blockSz) < padSz) ? 1 : 0);
  938. /* Calculate whole blocks. */
  939. msgBlocks--;
  940. ret = wc_HmacUpdate(hmac, header, WOLFSSL_TLS_HMAC_INNER_SZ);
  941. if (ret == 0) {
  942. /* Fill the rest of the block with any available data. */
  943. currSz = ctMaskLT(msgSz, blockSz) & msgSz;
  944. currSz |= ctMaskGTE(msgSz, blockSz) & blockSz;
  945. currSz -= WOLFSSL_TLS_HMAC_INNER_SZ;
  946. currSz &= ~(0 - (currSz >> 31));
  947. ret = wc_HmacUpdate(hmac, in, currSz);
  948. offset = currSz;
  949. }
  950. if (ret == 0) {
  951. /* Do the hash operations on a block basis. */
  952. for (i = 0; i < msgBlocks; i++, offset += blockSz) {
  953. ret = wc_HmacUpdate(hmac, in + offset, blockSz);
  954. if (ret != 0)
  955. break;
  956. }
  957. }
  958. if (ret == 0)
  959. ret = wc_HmacUpdate(hmac, in + offset, msgSz - offset);
  960. if (ret == 0)
  961. ret = wc_HmacFinal(hmac, digest);
  962. if (ret == 0) {
  963. /* Do the dummy hash operations. Do at least one. */
  964. for (i = 0; i < blocks + 1; i++) {
  965. ret = wc_HmacUpdate(hmac, dummy, blockSz);
  966. if (ret != 0)
  967. break;
  968. }
  969. }
  970. return ret;
  971. }
  972. #endif
  973. int TLS_hmac(WOLFSSL* ssl, byte* digest, const byte* in, word32 sz, int padSz,
  974. int content, int verify, int epochOrder)
  975. {
  976. Hmac hmac;
  977. byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
  978. int ret = 0;
  979. const byte* macSecret = NULL;
  980. word32 hashSz = 0;
  981. if (ssl == NULL)
  982. return BAD_FUNC_ARG;
  983. #ifdef HAVE_TRUNCATED_HMAC
  984. hashSz = ssl->truncated_hmac ? (byte)TRUNCATED_HMAC_SZ
  985. : ssl->specs.hash_size;
  986. #else
  987. hashSz = ssl->specs.hash_size;
  988. #endif
  989. #ifdef HAVE_FUZZER
  990. /* Fuzz "in" buffer with sz to be used in HMAC algorithm */
  991. if (ssl->fuzzerCb) {
  992. if (verify && padSz >= 0) {
  993. ssl->fuzzerCb(ssl, in, sz + hashSz + padSz + 1, FUZZ_HMAC,
  994. ssl->fuzzerCtx);
  995. }
  996. else {
  997. ssl->fuzzerCb(ssl, in, sz, FUZZ_HMAC, ssl->fuzzerCtx);
  998. }
  999. }
  1000. #endif
  1001. if (!ssl->options.dtls)
  1002. wolfSSL_SetTlsHmacInner(ssl, myInner, sz, content, verify);
  1003. else
  1004. wolfSSL_SetTlsHmacInner(ssl, myInner, sz, content, epochOrder);
  1005. ret = wc_HmacInit(&hmac, ssl->heap, ssl->devId);
  1006. if (ret != 0)
  1007. return ret;
  1008. #ifdef WOLFSSL_DTLS
  1009. if (ssl->options.dtls)
  1010. macSecret = wolfSSL_GetDtlsMacSecret(ssl, verify, epochOrder);
  1011. else
  1012. macSecret = wolfSSL_GetMacSecret(ssl, verify);
  1013. #else
  1014. macSecret = wolfSSL_GetMacSecret(ssl, verify);
  1015. #endif
  1016. ret = wc_HmacSetKey(&hmac, wolfSSL_GetHmacType(ssl),
  1017. macSecret,
  1018. ssl->specs.hash_size);
  1019. if (ret == 0) {
  1020. /* Constant time verification required. */
  1021. if (verify && padSz >= 0) {
  1022. #if !defined(WOLFSSL_NO_HASH_RAW) && !defined(HAVE_FIPS) && \
  1023. !defined(HAVE_SELFTEST)
  1024. #ifdef HAVE_BLAKE2
  1025. if (wolfSSL_GetHmacType(ssl) == WC_HASH_TYPE_BLAKE2B) {
  1026. ret = Hmac_UpdateFinal(&hmac, digest, in,
  1027. sz + hashSz + padSz + 1, myInner);
  1028. }
  1029. else
  1030. #endif
  1031. {
  1032. ret = Hmac_UpdateFinal_CT(&hmac, digest, in,
  1033. sz + hashSz + padSz + 1, hashSz, myInner);
  1034. }
  1035. #else
  1036. ret = Hmac_UpdateFinal(&hmac, digest, in, sz + hashSz + padSz + 1,
  1037. myInner);
  1038. #endif
  1039. }
  1040. else {
  1041. ret = wc_HmacUpdate(&hmac, myInner, sizeof(myInner));
  1042. if (ret == 0)
  1043. ret = wc_HmacUpdate(&hmac, in, sz); /* content */
  1044. if (ret == 0)
  1045. ret = wc_HmacFinal(&hmac, digest);
  1046. }
  1047. }
  1048. wc_HmacFree(&hmac);
  1049. return ret;
  1050. }
  1051. #endif /* WOLFSSL_AEAD_ONLY */
  1052. #endif /* !WOLFSSL_NO_TLS12 */
  1053. #ifdef HAVE_TLS_EXTENSIONS
  1054. /**
  1055. * The TLSX semaphore is used to calculate the size of the extensions to be sent
  1056. * from one peer to another.
  1057. */
  1058. /** Supports up to 72 flags. Increase as needed. */
  1059. #define SEMAPHORE_SIZE 9
  1060. /**
  1061. * Converts the extension type (id) to an index in the semaphore.
  1062. *
  1063. * Official reference for TLS extension types:
  1064. * http://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xml
  1065. *
  1066. * Motivation:
  1067. * Previously, we used the extension type itself as the index of that
  1068. * extension in the semaphore as the extension types were declared
  1069. * sequentially, but maintain a semaphore as big as the number of available
  1070. * extensions is no longer an option since the release of renegotiation_info.
  1071. *
  1072. * How to update:
  1073. * Assign extension types that extrapolate the number of available semaphores
  1074. * to the first available index going backwards in the semaphore array.
  1075. * When adding a new extension type that don't extrapolate the number of
  1076. * available semaphores, check for a possible collision with with a
  1077. * 'remapped' extension type.
  1078. */
  1079. static WC_INLINE word16 TLSX_ToSemaphore(word16 type)
  1080. {
  1081. switch (type) {
  1082. case TLSX_RENEGOTIATION_INFO: /* 0xFF01 */
  1083. return 63;
  1084. #ifdef WOLFSSL_QUIC
  1085. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT: /* 0xffa5 */
  1086. return 64;
  1087. #endif
  1088. default:
  1089. if (type > 62) {
  1090. /* This message SHOULD only happens during the adding of
  1091. new TLS extensions in which its IANA number overflows
  1092. the current semaphore's range, or if its number already
  1093. is assigned to be used by another extension.
  1094. Use this check value for the new extension and decrement
  1095. the check value by one. */
  1096. WOLFSSL_MSG("### TLSX semaphore collision or overflow detected!");
  1097. }
  1098. }
  1099. return type;
  1100. }
  1101. /** Checks if a specific light (tls extension) is not set in the semaphore. */
  1102. #define IS_OFF(semaphore, light) \
  1103. (!(((semaphore)[(light) / 8] & (byte) (0x01 << ((light) % 8)))))
  1104. /** Turn on a specific light (tls extension) in the semaphore. */
  1105. /* the semaphore marks the extensions already written to the message */
  1106. #define TURN_ON(semaphore, light) \
  1107. ((semaphore)[(light) / 8] |= (byte) (0x01 << ((light) % 8)))
  1108. /** Turn off a specific light (tls extension) in the semaphore. */
  1109. #define TURN_OFF(semaphore, light) \
  1110. ((semaphore)[(light) / 8] &= (byte) ~(0x01 << ((light) % 8)))
  1111. /** Creates a new extension. */
  1112. static TLSX* TLSX_New(TLSX_Type type, const void* data, void* heap)
  1113. {
  1114. TLSX* extension = (TLSX*)XMALLOC(sizeof(TLSX), heap, DYNAMIC_TYPE_TLSX);
  1115. (void)heap;
  1116. if (extension) {
  1117. extension->type = type;
  1118. extension->data = (void*)data;
  1119. extension->resp = 0;
  1120. extension->next = NULL;
  1121. }
  1122. return extension;
  1123. }
  1124. /**
  1125. * Creates a new extension and pushes it to the provided list.
  1126. * Checks for duplicate extensions, keeps the newest.
  1127. */
  1128. int TLSX_Push(TLSX** list, TLSX_Type type, const void* data, void* heap)
  1129. {
  1130. TLSX* extension = TLSX_New(type, data, heap);
  1131. if (extension == NULL)
  1132. return MEMORY_E;
  1133. /* pushes the new extension on the list. */
  1134. extension->next = *list;
  1135. *list = extension;
  1136. /* remove duplicate extensions, there should be only one of each type. */
  1137. do {
  1138. if (extension->next && extension->next->type == type) {
  1139. TLSX *next = extension->next;
  1140. extension->next = next->next;
  1141. next->next = NULL;
  1142. TLSX_FreeAll(next, heap);
  1143. /* there is no way to occur more than
  1144. * two extensions of the same type.
  1145. */
  1146. break;
  1147. }
  1148. } while ((extension = extension->next));
  1149. return 0;
  1150. }
  1151. #ifdef WOLFSSL_TLS13
  1152. /**
  1153. * Creates a new extension and prepend it to the provided list.
  1154. * Checks for duplicate extensions, keeps the newest.
  1155. */
  1156. static int TLSX_Prepend(TLSX** list, TLSX_Type type, void* data, void* heap)
  1157. {
  1158. TLSX* extension = TLSX_New(type, data, heap);
  1159. TLSX* curr = *list;
  1160. if (extension == NULL)
  1161. return MEMORY_E;
  1162. /* remove duplicate extensions, there should be only one of each type. */
  1163. while (curr && curr->next) {
  1164. if (curr->next->type == type) {
  1165. TLSX *next = curr->next;
  1166. curr->next = next->next;
  1167. next->next = NULL;
  1168. TLSX_FreeAll(next, heap);
  1169. }
  1170. curr = curr->next;
  1171. }
  1172. if (curr)
  1173. curr->next = extension;
  1174. else
  1175. *list = extension;
  1176. return 0;
  1177. }
  1178. #endif /* WOLFSSL_TLS13 */
  1179. #ifndef NO_WOLFSSL_CLIENT
  1180. int TLSX_CheckUnsupportedExtension(WOLFSSL* ssl, TLSX_Type type);
  1181. int TLSX_CheckUnsupportedExtension(WOLFSSL* ssl, TLSX_Type type)
  1182. {
  1183. TLSX *extension = TLSX_Find(ssl->extensions, type);
  1184. if (!extension)
  1185. extension = TLSX_Find(ssl->ctx->extensions, type);
  1186. return extension == NULL;
  1187. }
  1188. int TLSX_HandleUnsupportedExtension(WOLFSSL* ssl);
  1189. int TLSX_HandleUnsupportedExtension(WOLFSSL* ssl)
  1190. {
  1191. SendAlert(ssl, alert_fatal, unsupported_extension);
  1192. WOLFSSL_ERROR_VERBOSE(UNSUPPORTED_EXTENSION);
  1193. return UNSUPPORTED_EXTENSION;
  1194. }
  1195. #else
  1196. #define TLSX_CheckUnsupportedExtension(ssl, type) 0
  1197. #define TLSX_HandleUnsupportedExtension(ssl) 0
  1198. #endif
  1199. /** Mark an extension to be sent back to the client. */
  1200. static void TLSX_SetResponse(WOLFSSL* ssl, TLSX_Type type)
  1201. {
  1202. TLSX *extension = TLSX_Find(ssl->extensions, type);
  1203. if (extension)
  1204. extension->resp = 1;
  1205. }
  1206. /******************************************************************************/
  1207. /* Application-Layer Protocol Negotiation */
  1208. /******************************************************************************/
  1209. #ifdef HAVE_ALPN
  1210. /** Creates a new ALPN object, providing protocol name to use. */
  1211. static ALPN* TLSX_ALPN_New(char *protocol_name, word16 protocol_nameSz,
  1212. void* heap)
  1213. {
  1214. ALPN *alpn;
  1215. WOLFSSL_ENTER("TLSX_ALPN_New");
  1216. if (protocol_name == NULL ||
  1217. protocol_nameSz > WOLFSSL_MAX_ALPN_PROTO_NAME_LEN) {
  1218. WOLFSSL_MSG("Invalid arguments");
  1219. return NULL;
  1220. }
  1221. alpn = (ALPN*)XMALLOC(sizeof(ALPN), heap, DYNAMIC_TYPE_TLSX);
  1222. if (alpn == NULL) {
  1223. WOLFSSL_MSG("Memory failure");
  1224. return NULL;
  1225. }
  1226. alpn->next = NULL;
  1227. alpn->negotiated = 0;
  1228. alpn->options = 0;
  1229. alpn->protocol_name = (char*)XMALLOC(protocol_nameSz + 1,
  1230. heap, DYNAMIC_TYPE_TLSX);
  1231. if (alpn->protocol_name == NULL) {
  1232. WOLFSSL_MSG("Memory failure");
  1233. XFREE(alpn, heap, DYNAMIC_TYPE_TLSX);
  1234. return NULL;
  1235. }
  1236. XMEMCPY(alpn->protocol_name, protocol_name, protocol_nameSz);
  1237. alpn->protocol_name[protocol_nameSz] = 0;
  1238. (void)heap;
  1239. return alpn;
  1240. }
  1241. /** Releases an ALPN object. */
  1242. static void TLSX_ALPN_Free(ALPN *alpn, void* heap)
  1243. {
  1244. (void)heap;
  1245. if (alpn == NULL)
  1246. return;
  1247. XFREE(alpn->protocol_name, heap, DYNAMIC_TYPE_TLSX);
  1248. XFREE(alpn, heap, DYNAMIC_TYPE_TLSX);
  1249. }
  1250. /** Releases all ALPN objects in the provided list. */
  1251. static void TLSX_ALPN_FreeAll(ALPN *list, void* heap)
  1252. {
  1253. ALPN* alpn;
  1254. while ((alpn = list)) {
  1255. list = alpn->next;
  1256. TLSX_ALPN_Free(alpn, heap);
  1257. }
  1258. }
  1259. /** Tells the buffered size of the ALPN objects in a list. */
  1260. static word16 TLSX_ALPN_GetSize(ALPN *list)
  1261. {
  1262. ALPN* alpn;
  1263. word16 length = OPAQUE16_LEN; /* list length */
  1264. while ((alpn = list)) {
  1265. list = alpn->next;
  1266. length++; /* protocol name length is on one byte */
  1267. length += (word16)XSTRLEN(alpn->protocol_name);
  1268. }
  1269. return length;
  1270. }
  1271. /** Writes the ALPN objects of a list in a buffer. */
  1272. static word16 TLSX_ALPN_Write(ALPN *list, byte *output)
  1273. {
  1274. ALPN* alpn;
  1275. word16 length = 0;
  1276. word16 offset = OPAQUE16_LEN; /* list length offset */
  1277. while ((alpn = list)) {
  1278. list = alpn->next;
  1279. length = (word16)XSTRLEN(alpn->protocol_name);
  1280. /* protocol name length */
  1281. output[offset++] = (byte)length;
  1282. /* protocol name value */
  1283. XMEMCPY(output + offset, alpn->protocol_name, length);
  1284. offset += length;
  1285. }
  1286. /* writing list length */
  1287. c16toa(offset - OPAQUE16_LEN, output);
  1288. return offset;
  1289. }
  1290. /** Finds a protocol name in the provided ALPN list */
  1291. static ALPN* TLSX_ALPN_Find(ALPN *list, char *protocol_name, word16 size)
  1292. {
  1293. ALPN *alpn;
  1294. if (list == NULL || protocol_name == NULL)
  1295. return NULL;
  1296. alpn = list;
  1297. while (alpn != NULL && (
  1298. (word16)XSTRLEN(alpn->protocol_name) != size ||
  1299. XSTRNCMP(alpn->protocol_name, protocol_name, size)))
  1300. alpn = alpn->next;
  1301. return alpn;
  1302. }
  1303. /** Set the ALPN matching client and server requirements */
  1304. static int TLSX_SetALPN(TLSX** extensions, const void* data, word16 size,
  1305. void* heap)
  1306. {
  1307. ALPN *alpn;
  1308. int ret;
  1309. if (extensions == NULL || data == NULL)
  1310. return BAD_FUNC_ARG;
  1311. alpn = TLSX_ALPN_New((char *)data, size, heap);
  1312. if (alpn == NULL) {
  1313. WOLFSSL_MSG("Memory failure");
  1314. return MEMORY_E;
  1315. }
  1316. alpn->negotiated = 1;
  1317. ret = TLSX_Push(extensions, TLSX_APPLICATION_LAYER_PROTOCOL, (void*)alpn,
  1318. heap);
  1319. if (ret != 0) {
  1320. TLSX_ALPN_Free(alpn, heap);
  1321. return ret;
  1322. }
  1323. return WOLFSSL_SUCCESS;
  1324. }
  1325. /** Parses a buffer of ALPN extensions and set the first one matching
  1326. * client and server requirements */
  1327. static int TLSX_ALPN_ParseAndSet(WOLFSSL *ssl, const byte *input, word16 length,
  1328. byte isRequest)
  1329. {
  1330. word16 size = 0, offset = 0, idx = 0;
  1331. int r = BUFFER_ERROR;
  1332. byte match = 0;
  1333. TLSX *extension;
  1334. ALPN *alpn = NULL, *list;
  1335. if (OPAQUE16_LEN > length)
  1336. return BUFFER_ERROR;
  1337. ato16(input, &size);
  1338. offset += OPAQUE16_LEN;
  1339. if (size == 0)
  1340. return BUFFER_ERROR;
  1341. extension = TLSX_Find(ssl->extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
  1342. if (extension == NULL)
  1343. extension = TLSX_Find(ssl->ctx->extensions,
  1344. TLSX_APPLICATION_LAYER_PROTOCOL);
  1345. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
  1346. if (ssl->alpnSelect != NULL && ssl->options.side == WOLFSSL_SERVER_END) {
  1347. const byte* out;
  1348. unsigned char outLen;
  1349. if (ssl->alpnSelect(ssl, &out, &outLen, input + offset, size,
  1350. ssl->alpnSelectArg) == 0) {
  1351. WOLFSSL_MSG("ALPN protocol match");
  1352. /* clears out all current ALPN extensions set */
  1353. TLSX_Remove(&ssl->extensions, TLSX_APPLICATION_LAYER_PROTOCOL, ssl->heap);
  1354. extension = NULL;
  1355. if (TLSX_UseALPN(&ssl->extensions, (char*)out, outLen, 0, ssl->heap)
  1356. == WOLFSSL_SUCCESS) {
  1357. extension = TLSX_Find(ssl->extensions,
  1358. TLSX_APPLICATION_LAYER_PROTOCOL);
  1359. }
  1360. }
  1361. }
  1362. #endif
  1363. if (extension == NULL || extension->data == NULL) {
  1364. return isRequest ? 0
  1365. : TLSX_HandleUnsupportedExtension(ssl);
  1366. }
  1367. /* validating alpn list length */
  1368. if (length != OPAQUE16_LEN + size)
  1369. return BUFFER_ERROR;
  1370. list = (ALPN*)extension->data;
  1371. /* keep the list sent by client */
  1372. if (isRequest) {
  1373. if (ssl->alpn_client_list != NULL)
  1374. XFREE(ssl->alpn_client_list, ssl->heap, DYNAMIC_TYPE_ALPN);
  1375. ssl->alpn_client_list = (char *)XMALLOC(size, ssl->heap,
  1376. DYNAMIC_TYPE_ALPN);
  1377. if (ssl->alpn_client_list == NULL)
  1378. return MEMORY_ERROR;
  1379. }
  1380. for (size = 0; offset < length; offset += size) {
  1381. size = input[offset++];
  1382. if (offset + size > length || size == 0)
  1383. return BUFFER_ERROR;
  1384. if (isRequest) {
  1385. XMEMCPY(ssl->alpn_client_list+idx, (char*)input + offset, size);
  1386. idx += size;
  1387. ssl->alpn_client_list[idx++] = ',';
  1388. }
  1389. if (!match) {
  1390. alpn = TLSX_ALPN_Find(list, (char*)input + offset, size);
  1391. if (alpn != NULL) {
  1392. WOLFSSL_MSG("ALPN protocol match");
  1393. match = 1;
  1394. /* skip reading other values if not required */
  1395. if (!isRequest)
  1396. break;
  1397. }
  1398. }
  1399. }
  1400. if (isRequest)
  1401. ssl->alpn_client_list[idx-1] = 0;
  1402. if (!match) {
  1403. WOLFSSL_MSG("No ALPN protocol match");
  1404. /* do nothing if no protocol match between client and server and option
  1405. is set to continue (like OpenSSL) */
  1406. if (list->options & WOLFSSL_ALPN_CONTINUE_ON_MISMATCH) {
  1407. WOLFSSL_MSG("Continue on mismatch");
  1408. return 0;
  1409. }
  1410. SendAlert(ssl, alert_fatal, no_application_protocol);
  1411. WOLFSSL_ERROR_VERBOSE(UNKNOWN_ALPN_PROTOCOL_NAME_E);
  1412. return UNKNOWN_ALPN_PROTOCOL_NAME_E;
  1413. }
  1414. /* set the matching negotiated protocol */
  1415. r = TLSX_SetALPN(&ssl->extensions,
  1416. alpn->protocol_name,
  1417. (word16)XSTRLEN(alpn->protocol_name),
  1418. ssl->heap);
  1419. if (r != WOLFSSL_SUCCESS) {
  1420. WOLFSSL_MSG("TLSX_SetALPN failed");
  1421. return BUFFER_ERROR;
  1422. }
  1423. /* reply to ALPN extension sent from client */
  1424. if (isRequest) {
  1425. #ifndef NO_WOLFSSL_SERVER
  1426. TLSX_SetResponse(ssl, TLSX_APPLICATION_LAYER_PROTOCOL);
  1427. #endif
  1428. }
  1429. return 0;
  1430. }
  1431. /** Add a protocol name to the list of accepted usable ones */
  1432. int TLSX_UseALPN(TLSX** extensions, const void* data, word16 size, byte options,
  1433. void* heap)
  1434. {
  1435. ALPN *alpn;
  1436. TLSX *extension;
  1437. int ret;
  1438. if (extensions == NULL || data == NULL)
  1439. return BAD_FUNC_ARG;
  1440. alpn = TLSX_ALPN_New((char *)data, size, heap);
  1441. if (alpn == NULL) {
  1442. WOLFSSL_MSG("Memory failure");
  1443. return MEMORY_E;
  1444. }
  1445. /* Set Options of ALPN */
  1446. alpn->options = options;
  1447. extension = TLSX_Find(*extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
  1448. if (extension == NULL) {
  1449. ret = TLSX_Push(extensions, TLSX_APPLICATION_LAYER_PROTOCOL,
  1450. (void*)alpn, heap);
  1451. if (ret != 0) {
  1452. TLSX_ALPN_Free(alpn, heap);
  1453. return ret;
  1454. }
  1455. }
  1456. else {
  1457. /* push new ALPN object to extension data. */
  1458. alpn->next = (ALPN*)extension->data;
  1459. extension->data = (void*)alpn;
  1460. }
  1461. return WOLFSSL_SUCCESS;
  1462. }
  1463. /** Get the protocol name set by the server */
  1464. int TLSX_ALPN_GetRequest(TLSX* extensions, void** data, word16 *dataSz)
  1465. {
  1466. TLSX *extension;
  1467. ALPN *alpn;
  1468. if (extensions == NULL || data == NULL || dataSz == NULL)
  1469. return BAD_FUNC_ARG;
  1470. extension = TLSX_Find(extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
  1471. if (extension == NULL) {
  1472. WOLFSSL_MSG("TLS extension not found");
  1473. WOLFSSL_ERROR_VERBOSE(WOLFSSL_ALPN_NOT_FOUND);
  1474. return WOLFSSL_ALPN_NOT_FOUND;
  1475. }
  1476. alpn = (ALPN *)extension->data;
  1477. if (alpn == NULL) {
  1478. WOLFSSL_MSG("ALPN extension not found");
  1479. *data = NULL;
  1480. *dataSz = 0;
  1481. WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR);
  1482. return WOLFSSL_FATAL_ERROR;
  1483. }
  1484. if (alpn->negotiated != 1) {
  1485. /* consider as an error */
  1486. if (alpn->options & WOLFSSL_ALPN_FAILED_ON_MISMATCH) {
  1487. WOLFSSL_MSG("No protocol match with peer -> Failed");
  1488. WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR);
  1489. return WOLFSSL_FATAL_ERROR;
  1490. }
  1491. /* continue without negotiated protocol */
  1492. WOLFSSL_MSG("No protocol match with peer -> Continue");
  1493. WOLFSSL_ERROR_VERBOSE(WOLFSSL_ALPN_NOT_FOUND);
  1494. return WOLFSSL_ALPN_NOT_FOUND;
  1495. }
  1496. if (alpn->next != NULL) {
  1497. WOLFSSL_MSG("Only one protocol name must be accepted");
  1498. WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR);
  1499. return WOLFSSL_FATAL_ERROR;
  1500. }
  1501. *data = alpn->protocol_name;
  1502. *dataSz = (word16)XSTRLEN((char*)*data);
  1503. return WOLFSSL_SUCCESS;
  1504. }
  1505. #define ALPN_FREE_ALL TLSX_ALPN_FreeAll
  1506. #define ALPN_GET_SIZE TLSX_ALPN_GetSize
  1507. #define ALPN_WRITE TLSX_ALPN_Write
  1508. #define ALPN_PARSE TLSX_ALPN_ParseAndSet
  1509. #else /* HAVE_ALPN */
  1510. #define ALPN_FREE_ALL(list, heap)
  1511. #define ALPN_GET_SIZE(list) 0
  1512. #define ALPN_WRITE(a, b) 0
  1513. #define ALPN_PARSE(a, b, c, d) 0
  1514. #endif /* HAVE_ALPN */
  1515. /******************************************************************************/
  1516. /* Server Name Indication */
  1517. /******************************************************************************/
  1518. #ifdef HAVE_SNI
  1519. /** Creates a new SNI object. */
  1520. static SNI* TLSX_SNI_New(byte type, const void* data, word16 size, void* heap)
  1521. {
  1522. SNI* sni = (SNI*)XMALLOC(sizeof(SNI), heap, DYNAMIC_TYPE_TLSX);
  1523. (void)heap;
  1524. if (sni) {
  1525. sni->type = type;
  1526. sni->next = NULL;
  1527. #ifndef NO_WOLFSSL_SERVER
  1528. sni->options = 0;
  1529. sni->status = WOLFSSL_SNI_NO_MATCH;
  1530. #endif
  1531. switch (sni->type) {
  1532. case WOLFSSL_SNI_HOST_NAME:
  1533. sni->data.host_name = (char*)XMALLOC(size + 1, heap,
  1534. DYNAMIC_TYPE_TLSX);
  1535. if (sni->data.host_name) {
  1536. XSTRNCPY(sni->data.host_name, (const char*)data, size);
  1537. sni->data.host_name[size] = '\0';
  1538. } else {
  1539. XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
  1540. sni = NULL;
  1541. }
  1542. break;
  1543. default: /* invalid type */
  1544. XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
  1545. sni = NULL;
  1546. }
  1547. }
  1548. return sni;
  1549. }
  1550. /** Releases a SNI object. */
  1551. static void TLSX_SNI_Free(SNI* sni, void* heap)
  1552. {
  1553. if (sni) {
  1554. switch (sni->type) {
  1555. case WOLFSSL_SNI_HOST_NAME:
  1556. XFREE(sni->data.host_name, heap, DYNAMIC_TYPE_TLSX);
  1557. break;
  1558. }
  1559. XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
  1560. }
  1561. (void)heap;
  1562. }
  1563. /** Releases all SNI objects in the provided list. */
  1564. static void TLSX_SNI_FreeAll(SNI* list, void* heap)
  1565. {
  1566. SNI* sni;
  1567. while ((sni = list)) {
  1568. list = sni->next;
  1569. TLSX_SNI_Free(sni, heap);
  1570. }
  1571. }
  1572. /** Tells the buffered size of the SNI objects in a list. */
  1573. static word16 TLSX_SNI_GetSize(SNI* list)
  1574. {
  1575. SNI* sni;
  1576. word16 length = OPAQUE16_LEN; /* list length */
  1577. while ((sni = list)) {
  1578. list = sni->next;
  1579. length += ENUM_LEN + OPAQUE16_LEN; /* sni type + sni length */
  1580. switch (sni->type) {
  1581. case WOLFSSL_SNI_HOST_NAME:
  1582. length += (word16)XSTRLEN((char*)sni->data.host_name);
  1583. break;
  1584. }
  1585. }
  1586. return length;
  1587. }
  1588. /** Writes the SNI objects of a list in a buffer. */
  1589. static word16 TLSX_SNI_Write(SNI* list, byte* output)
  1590. {
  1591. SNI* sni;
  1592. word16 length = 0;
  1593. word16 offset = OPAQUE16_LEN; /* list length offset */
  1594. while ((sni = list)) {
  1595. list = sni->next;
  1596. output[offset++] = sni->type; /* sni type */
  1597. switch (sni->type) {
  1598. case WOLFSSL_SNI_HOST_NAME:
  1599. length = (word16)XSTRLEN((char*)sni->data.host_name);
  1600. c16toa(length, output + offset); /* sni length */
  1601. offset += OPAQUE16_LEN;
  1602. XMEMCPY(output + offset, sni->data.host_name, length);
  1603. offset += length;
  1604. break;
  1605. }
  1606. }
  1607. c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
  1608. return offset;
  1609. }
  1610. /** Finds a SNI object in the provided list. */
  1611. static SNI* TLSX_SNI_Find(SNI *list, byte type)
  1612. {
  1613. SNI* sni = list;
  1614. while (sni && sni->type != type)
  1615. sni = sni->next;
  1616. return sni;
  1617. }
  1618. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  1619. /** Sets the status of a SNI object. */
  1620. static void TLSX_SNI_SetStatus(TLSX* extensions, byte type, byte status)
  1621. {
  1622. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1623. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1624. if (sni)
  1625. sni->status = status;
  1626. }
  1627. #endif
  1628. /** Gets the status of a SNI object. */
  1629. byte TLSX_SNI_Status(TLSX* extensions, byte type)
  1630. {
  1631. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1632. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1633. if (sni)
  1634. return sni->status;
  1635. return 0;
  1636. }
  1637. /** Parses a buffer of SNI extensions. */
  1638. static int TLSX_SNI_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  1639. byte isRequest)
  1640. {
  1641. #ifndef NO_WOLFSSL_SERVER
  1642. word16 size = 0;
  1643. word16 offset = 0;
  1644. int cacheOnly = 0;
  1645. SNI *sni = NULL;
  1646. byte type;
  1647. int matchStat;
  1648. byte matched;
  1649. #endif
  1650. TLSX *extension = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
  1651. if (!extension)
  1652. extension = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
  1653. if (!isRequest) {
  1654. #ifndef NO_WOLFSSL_CLIENT
  1655. if (!extension || !extension->data)
  1656. return TLSX_HandleUnsupportedExtension(ssl);
  1657. if (length > 0)
  1658. return BUFFER_ERROR; /* SNI response MUST be empty. */
  1659. /* This call enables wolfSSL_SNI_GetRequest() to be called in the
  1660. * client side to fetch the used SNI. It will only work if the SNI
  1661. * was set at the SSL object level. Right now we only support one
  1662. * name type, WOLFSSL_SNI_HOST_NAME, but in the future, the
  1663. * inclusion of other name types will turn this method inaccurate,
  1664. * as the extension response doesn't contains information of which
  1665. * name was accepted.
  1666. */
  1667. TLSX_SNI_SetStatus(ssl->extensions, WOLFSSL_SNI_HOST_NAME,
  1668. WOLFSSL_SNI_REAL_MATCH);
  1669. return 0;
  1670. #endif
  1671. }
  1672. #ifndef NO_WOLFSSL_SERVER
  1673. if (!extension || !extension->data) {
  1674. /* This will keep SNI even though TLSX_UseSNI has not been called.
  1675. * Enable it so that the received sni is available to functions
  1676. * that use a custom callback when SNI is received.
  1677. */
  1678. #ifdef WOLFSSL_ALWAYS_KEEP_SNI
  1679. cacheOnly = 1;
  1680. #endif
  1681. if (ssl->ctx->sniRecvCb) {
  1682. cacheOnly = 1;
  1683. }
  1684. if (cacheOnly) {
  1685. WOLFSSL_MSG("Forcing SSL object to store SNI parameter");
  1686. }
  1687. else {
  1688. /* Skipping, SNI not enabled at server side. */
  1689. return 0;
  1690. }
  1691. }
  1692. if (OPAQUE16_LEN > length)
  1693. return BUFFER_ERROR;
  1694. ato16(input, &size);
  1695. offset += OPAQUE16_LEN;
  1696. /* validating sni list length */
  1697. if (length != OPAQUE16_LEN + size || size == 0)
  1698. return BUFFER_ERROR;
  1699. /* SNI was badly specified and only one type is now recognized and allowed.
  1700. * Only one SNI value per type (RFC6066), so, no loop. */
  1701. type = input[offset++];
  1702. if (type != WOLFSSL_SNI_HOST_NAME)
  1703. return BUFFER_ERROR;
  1704. if (offset + OPAQUE16_LEN > length)
  1705. return BUFFER_ERROR;
  1706. ato16(input + offset, &size);
  1707. offset += OPAQUE16_LEN;
  1708. if (offset + size != length || size == 0)
  1709. return BUFFER_ERROR;
  1710. if (!cacheOnly && !(sni = TLSX_SNI_Find((SNI*)extension->data, type)))
  1711. return 0; /* not using this type of SNI. */
  1712. #ifdef WOLFSSL_TLS13
  1713. /* Don't process the second ClientHello SNI extension if there
  1714. * was problems with the first.
  1715. */
  1716. if (!cacheOnly && sni->status != 0)
  1717. return 0;
  1718. #endif
  1719. matched = cacheOnly || (XSTRLEN(sni->data.host_name) == size &&
  1720. XSTRNCMP(sni->data.host_name, (const char*)input + offset, size) == 0);
  1721. if (matched || sni->options & WOLFSSL_SNI_ANSWER_ON_MISMATCH) {
  1722. int r = TLSX_UseSNI(&ssl->extensions, type, input + offset, size,
  1723. ssl->heap);
  1724. if (r != WOLFSSL_SUCCESS)
  1725. return r; /* throws error. */
  1726. if (cacheOnly) {
  1727. WOLFSSL_MSG("Forcing storage of SNI, Fake match");
  1728. matchStat = WOLFSSL_SNI_FORCE_KEEP;
  1729. }
  1730. else if (matched) {
  1731. WOLFSSL_MSG("SNI did match!");
  1732. matchStat = WOLFSSL_SNI_REAL_MATCH;
  1733. }
  1734. else {
  1735. WOLFSSL_MSG("fake SNI match from ANSWER_ON_MISMATCH");
  1736. matchStat = WOLFSSL_SNI_FAKE_MATCH;
  1737. }
  1738. TLSX_SNI_SetStatus(ssl->extensions, type, (byte)matchStat);
  1739. if (!cacheOnly)
  1740. TLSX_SetResponse(ssl, TLSX_SERVER_NAME);
  1741. }
  1742. else if (!(sni->options & WOLFSSL_SNI_CONTINUE_ON_MISMATCH)) {
  1743. SendAlert(ssl, alert_fatal, unrecognized_name);
  1744. WOLFSSL_ERROR_VERBOSE(UNKNOWN_SNI_HOST_NAME_E);
  1745. return UNKNOWN_SNI_HOST_NAME_E;
  1746. }
  1747. #else
  1748. (void)input;
  1749. #endif
  1750. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1751. (void)length;
  1752. #endif
  1753. return 0;
  1754. }
  1755. static int TLSX_SNI_VerifyParse(WOLFSSL* ssl, byte isRequest)
  1756. {
  1757. (void)ssl;
  1758. if (isRequest) {
  1759. #ifndef NO_WOLFSSL_SERVER
  1760. TLSX* ctx_ext = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
  1761. TLSX* ssl_ext = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
  1762. SNI* ctx_sni = ctx_ext ? (SNI*)ctx_ext->data : NULL;
  1763. SNI* ssl_sni = ssl_ext ? (SNI*)ssl_ext->data : NULL;
  1764. SNI* sni = NULL;
  1765. for (; ctx_sni; ctx_sni = ctx_sni->next) {
  1766. if (ctx_sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) {
  1767. sni = TLSX_SNI_Find(ssl_sni, ctx_sni->type);
  1768. if (sni) {
  1769. if (sni->status != WOLFSSL_SNI_NO_MATCH)
  1770. continue;
  1771. /* if ssl level overrides ctx level, it is ok. */
  1772. if ((sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) == 0)
  1773. continue;
  1774. }
  1775. SendAlert(ssl, alert_fatal, handshake_failure);
  1776. WOLFSSL_ERROR_VERBOSE(SNI_ABSENT_ERROR);
  1777. return SNI_ABSENT_ERROR;
  1778. }
  1779. }
  1780. for (; ssl_sni; ssl_sni = ssl_sni->next) {
  1781. if (ssl_sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) {
  1782. if (ssl_sni->status != WOLFSSL_SNI_NO_MATCH)
  1783. continue;
  1784. SendAlert(ssl, alert_fatal, handshake_failure);
  1785. WOLFSSL_ERROR_VERBOSE(SNI_ABSENT_ERROR);
  1786. return SNI_ABSENT_ERROR;
  1787. }
  1788. }
  1789. #endif /* NO_WOLFSSL_SERVER */
  1790. }
  1791. return 0;
  1792. }
  1793. int TLSX_UseSNI(TLSX** extensions, byte type, const void* data, word16 size,
  1794. void* heap)
  1795. {
  1796. TLSX* extension;
  1797. SNI* sni = NULL;
  1798. if (extensions == NULL || data == NULL)
  1799. return BAD_FUNC_ARG;
  1800. if ((sni = TLSX_SNI_New(type, data, size, heap)) == NULL)
  1801. return MEMORY_E;
  1802. extension = TLSX_Find(*extensions, TLSX_SERVER_NAME);
  1803. if (!extension) {
  1804. int ret = TLSX_Push(extensions, TLSX_SERVER_NAME, (void*)sni, heap);
  1805. if (ret != 0) {
  1806. TLSX_SNI_Free(sni, heap);
  1807. return ret;
  1808. }
  1809. }
  1810. else {
  1811. /* push new SNI object to extension data. */
  1812. sni->next = (SNI*)extension->data;
  1813. extension->data = (void*)sni;
  1814. /* remove duplicate SNI, there should be only one of each type. */
  1815. do {
  1816. if (sni->next && sni->next->type == type) {
  1817. SNI* next = sni->next;
  1818. sni->next = next->next;
  1819. TLSX_SNI_Free(next, heap);
  1820. /* there is no way to occur more than
  1821. * two SNIs of the same type.
  1822. */
  1823. break;
  1824. }
  1825. } while ((sni = sni->next));
  1826. }
  1827. return WOLFSSL_SUCCESS;
  1828. }
  1829. #ifndef NO_WOLFSSL_SERVER
  1830. /** Tells the SNI requested by the client. */
  1831. word16 TLSX_SNI_GetRequest(TLSX* extensions, byte type, void** data)
  1832. {
  1833. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1834. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1835. if (sni && sni->status != WOLFSSL_SNI_NO_MATCH) {
  1836. switch (sni->type) {
  1837. case WOLFSSL_SNI_HOST_NAME:
  1838. if (data) {
  1839. *data = sni->data.host_name;
  1840. return (word16)XSTRLEN((char*)*data);
  1841. }
  1842. }
  1843. }
  1844. return 0;
  1845. }
  1846. /** Sets the options for a SNI object. */
  1847. void TLSX_SNI_SetOptions(TLSX* extensions, byte type, byte options)
  1848. {
  1849. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1850. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1851. if (sni)
  1852. sni->options = options;
  1853. }
  1854. /** Retrieves a SNI request from a client hello buffer. */
  1855. int TLSX_SNI_GetFromBuffer(const byte* clientHello, word32 helloSz,
  1856. byte type, byte* sni, word32* inOutSz)
  1857. {
  1858. word32 offset = 0;
  1859. word32 len32 = 0;
  1860. word16 len16 = 0;
  1861. if (helloSz < RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ + CLIENT_HELLO_FIRST)
  1862. return INCOMPLETE_DATA;
  1863. /* TLS record header */
  1864. if ((enum ContentType) clientHello[offset++] != handshake) {
  1865. /* checking for SSLv2.0 client hello according to: */
  1866. /* http://tools.ietf.org/html/rfc4346#appendix-E.1 */
  1867. if ((enum HandShakeType) clientHello[++offset] == client_hello) {
  1868. offset += ENUM_LEN + VERSION_SZ; /* skip version */
  1869. ato16(clientHello + offset, &len16);
  1870. offset += OPAQUE16_LEN;
  1871. if (len16 % 3) /* cipher_spec_length must be multiple of 3 */
  1872. return BUFFER_ERROR;
  1873. ato16(clientHello + offset, &len16);
  1874. /* Returning SNI_UNSUPPORTED do not increment offset here */
  1875. if (len16 != 0) /* session_id_length must be 0 */
  1876. return BUFFER_ERROR;
  1877. WOLFSSL_ERROR_VERBOSE(SNI_UNSUPPORTED);
  1878. return SNI_UNSUPPORTED;
  1879. }
  1880. return BUFFER_ERROR;
  1881. }
  1882. if (clientHello[offset++] != SSLv3_MAJOR)
  1883. return BUFFER_ERROR;
  1884. if (clientHello[offset++] < TLSv1_MINOR) {
  1885. WOLFSSL_ERROR_VERBOSE(SNI_UNSUPPORTED);
  1886. return SNI_UNSUPPORTED;
  1887. }
  1888. ato16(clientHello + offset, &len16);
  1889. offset += OPAQUE16_LEN;
  1890. if (offset + len16 > helloSz)
  1891. return INCOMPLETE_DATA;
  1892. /* Handshake header */
  1893. if ((enum HandShakeType) clientHello[offset] != client_hello)
  1894. return BUFFER_ERROR;
  1895. c24to32(clientHello + offset + 1, &len32);
  1896. offset += HANDSHAKE_HEADER_SZ;
  1897. if (offset + len32 > helloSz)
  1898. return BUFFER_ERROR;
  1899. /* client hello */
  1900. offset += VERSION_SZ + RAN_LEN; /* version, random */
  1901. if (helloSz < offset + clientHello[offset])
  1902. return BUFFER_ERROR;
  1903. offset += ENUM_LEN + clientHello[offset]; /* skip session id */
  1904. /* cypher suites */
  1905. if (helloSz < offset + OPAQUE16_LEN)
  1906. return BUFFER_ERROR;
  1907. ato16(clientHello + offset, &len16);
  1908. offset += OPAQUE16_LEN;
  1909. if (helloSz < offset + len16)
  1910. return BUFFER_ERROR;
  1911. offset += len16; /* skip cypher suites */
  1912. /* compression methods */
  1913. if (helloSz < offset + 1)
  1914. return BUFFER_ERROR;
  1915. if (helloSz < offset + clientHello[offset])
  1916. return BUFFER_ERROR;
  1917. offset += ENUM_LEN + clientHello[offset]; /* skip compression methods */
  1918. /* extensions */
  1919. if (helloSz < offset + OPAQUE16_LEN)
  1920. return 0; /* no extensions in client hello. */
  1921. ato16(clientHello + offset, &len16);
  1922. offset += OPAQUE16_LEN;
  1923. if (helloSz < offset + len16)
  1924. return BUFFER_ERROR;
  1925. while (len16 >= OPAQUE16_LEN + OPAQUE16_LEN) {
  1926. word16 extType;
  1927. word16 extLen;
  1928. ato16(clientHello + offset, &extType);
  1929. offset += OPAQUE16_LEN;
  1930. ato16(clientHello + offset, &extLen);
  1931. offset += OPAQUE16_LEN;
  1932. if (helloSz < offset + extLen)
  1933. return BUFFER_ERROR;
  1934. if (extType != TLSX_SERVER_NAME) {
  1935. offset += extLen; /* skip extension */
  1936. } else {
  1937. word16 listLen;
  1938. ato16(clientHello + offset, &listLen);
  1939. offset += OPAQUE16_LEN;
  1940. if (helloSz < offset + listLen)
  1941. return BUFFER_ERROR;
  1942. while (listLen > ENUM_LEN + OPAQUE16_LEN) {
  1943. byte sniType = clientHello[offset++];
  1944. word16 sniLen;
  1945. ato16(clientHello + offset, &sniLen);
  1946. offset += OPAQUE16_LEN;
  1947. if (helloSz < offset + sniLen)
  1948. return BUFFER_ERROR;
  1949. if (sniType != type) {
  1950. offset += sniLen;
  1951. listLen -= min(ENUM_LEN + OPAQUE16_LEN + sniLen, listLen);
  1952. continue;
  1953. }
  1954. *inOutSz = min(sniLen, *inOutSz);
  1955. XMEMCPY(sni, clientHello + offset, *inOutSz);
  1956. return WOLFSSL_SUCCESS;
  1957. }
  1958. }
  1959. len16 -= min(2 * OPAQUE16_LEN + extLen, len16);
  1960. }
  1961. return len16 ? BUFFER_ERROR : 0;
  1962. }
  1963. #endif
  1964. #define SNI_FREE_ALL TLSX_SNI_FreeAll
  1965. #define SNI_GET_SIZE TLSX_SNI_GetSize
  1966. #define SNI_WRITE TLSX_SNI_Write
  1967. #define SNI_PARSE TLSX_SNI_Parse
  1968. #define SNI_VERIFY_PARSE TLSX_SNI_VerifyParse
  1969. #else
  1970. #define SNI_FREE_ALL(list, heap)
  1971. #define SNI_GET_SIZE(list) 0
  1972. #define SNI_WRITE(a, b) 0
  1973. #define SNI_PARSE(a, b, c, d) 0
  1974. #define SNI_VERIFY_PARSE(a, b) 0
  1975. #endif /* HAVE_SNI */
  1976. /******************************************************************************/
  1977. /* Trusted CA Key Indication */
  1978. /******************************************************************************/
  1979. #ifdef HAVE_TRUSTED_CA
  1980. /** Creates a new TCA object. */
  1981. static TCA* TLSX_TCA_New(byte type, const byte* id, word16 idSz, void* heap)
  1982. {
  1983. TCA* tca = (TCA*)XMALLOC(sizeof(TCA), heap, DYNAMIC_TYPE_TLSX);
  1984. if (tca) {
  1985. XMEMSET(tca, 0, sizeof(TCA));
  1986. tca->type = type;
  1987. switch (type) {
  1988. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  1989. break;
  1990. #ifndef NO_SHA
  1991. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  1992. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  1993. if (idSz == WC_SHA_DIGEST_SIZE &&
  1994. (tca->id =
  1995. (byte*)XMALLOC(idSz, heap, DYNAMIC_TYPE_TLSX))) {
  1996. XMEMCPY(tca->id, id, idSz);
  1997. tca->idSz = idSz;
  1998. }
  1999. else {
  2000. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2001. tca = NULL;
  2002. }
  2003. break;
  2004. #endif
  2005. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2006. if (idSz > 0 &&
  2007. (tca->id =
  2008. (byte*)XMALLOC(idSz, heap, DYNAMIC_TYPE_TLSX))) {
  2009. XMEMCPY(tca->id, id, idSz);
  2010. tca->idSz = idSz;
  2011. }
  2012. else {
  2013. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2014. tca = NULL;
  2015. }
  2016. break;
  2017. default: /* invalid type */
  2018. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2019. tca = NULL;
  2020. }
  2021. }
  2022. (void)heap;
  2023. return tca;
  2024. }
  2025. /** Releases a TCA object. */
  2026. static void TLSX_TCA_Free(TCA* tca, void* heap)
  2027. {
  2028. (void)heap;
  2029. if (tca) {
  2030. if (tca->id)
  2031. XFREE(tca->id, heap, DYNAMIC_TYPE_TLSX);
  2032. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2033. }
  2034. }
  2035. /** Releases all TCA objects in the provided list. */
  2036. static void TLSX_TCA_FreeAll(TCA* list, void* heap)
  2037. {
  2038. TCA* tca;
  2039. while ((tca = list)) {
  2040. list = tca->next;
  2041. TLSX_TCA_Free(tca, heap);
  2042. }
  2043. }
  2044. /** Tells the buffered size of the TCA objects in a list. */
  2045. static word16 TLSX_TCA_GetSize(TCA* list)
  2046. {
  2047. TCA* tca;
  2048. word16 length = OPAQUE16_LEN; /* list length */
  2049. while ((tca = list)) {
  2050. list = tca->next;
  2051. length += ENUM_LEN; /* tca type */
  2052. switch (tca->type) {
  2053. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2054. break;
  2055. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2056. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2057. length += tca->idSz;
  2058. break;
  2059. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2060. length += OPAQUE16_LEN + tca->idSz;
  2061. break;
  2062. }
  2063. }
  2064. return length;
  2065. }
  2066. /** Writes the TCA objects of a list in a buffer. */
  2067. static word16 TLSX_TCA_Write(TCA* list, byte* output)
  2068. {
  2069. TCA* tca;
  2070. word16 offset = OPAQUE16_LEN; /* list length offset */
  2071. while ((tca = list)) {
  2072. list = tca->next;
  2073. output[offset++] = tca->type; /* tca type */
  2074. switch (tca->type) {
  2075. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2076. break;
  2077. #ifndef NO_SHA
  2078. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2079. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2080. if (tca->id != NULL) {
  2081. XMEMCPY(output + offset, tca->id, tca->idSz);
  2082. offset += tca->idSz;
  2083. }
  2084. else {
  2085. /* ID missing. Set to an empty string. */
  2086. c16toa(0, output + offset);
  2087. offset += OPAQUE16_LEN;
  2088. }
  2089. break;
  2090. #endif
  2091. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2092. if (tca->id != NULL) {
  2093. c16toa(tca->idSz, output + offset); /* tca length */
  2094. offset += OPAQUE16_LEN;
  2095. XMEMCPY(output + offset, tca->id, tca->idSz);
  2096. offset += tca->idSz;
  2097. }
  2098. else {
  2099. /* ID missing. Set to an empty string. */
  2100. c16toa(0, output + offset);
  2101. offset += OPAQUE16_LEN;
  2102. }
  2103. break;
  2104. default:
  2105. /* ID unknown. Set to an empty string. */
  2106. c16toa(0, output + offset);
  2107. offset += OPAQUE16_LEN;
  2108. }
  2109. }
  2110. c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
  2111. return offset;
  2112. }
  2113. #ifndef NO_WOLFSSL_SERVER
  2114. static TCA* TLSX_TCA_Find(TCA *list, byte type, const byte* id, word16 idSz)
  2115. {
  2116. TCA* tca = list;
  2117. while (tca && tca->type != type && type != WOLFSSL_TRUSTED_CA_PRE_AGREED &&
  2118. idSz != tca->idSz && !XMEMCMP(id, tca->id, idSz))
  2119. tca = tca->next;
  2120. return tca;
  2121. }
  2122. #endif /* NO_WOLFSSL_SERVER */
  2123. /** Parses a buffer of TCA extensions. */
  2124. static int TLSX_TCA_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2125. byte isRequest)
  2126. {
  2127. #ifndef NO_WOLFSSL_SERVER
  2128. word16 size = 0;
  2129. word16 offset = 0;
  2130. #endif
  2131. TLSX *extension = TLSX_Find(ssl->extensions, TLSX_TRUSTED_CA_KEYS);
  2132. if (!extension)
  2133. extension = TLSX_Find(ssl->ctx->extensions, TLSX_TRUSTED_CA_KEYS);
  2134. if (!isRequest) {
  2135. #ifndef NO_WOLFSSL_CLIENT
  2136. if (!extension || !extension->data)
  2137. return TLSX_HandleUnsupportedExtension(ssl);
  2138. if (length > 0)
  2139. return BUFFER_ERROR; /* TCA response MUST be empty. */
  2140. /* Set the flag that we're good for keys */
  2141. TLSX_SetResponse(ssl, TLSX_TRUSTED_CA_KEYS);
  2142. return 0;
  2143. #endif
  2144. }
  2145. #ifndef NO_WOLFSSL_SERVER
  2146. if (!extension || !extension->data) {
  2147. /* Skipping, TCA not enabled at server side. */
  2148. return 0;
  2149. }
  2150. if (OPAQUE16_LEN > length)
  2151. return BUFFER_ERROR;
  2152. ato16(input, &size);
  2153. offset += OPAQUE16_LEN;
  2154. /* validating tca list length */
  2155. if (length != OPAQUE16_LEN + size)
  2156. return BUFFER_ERROR;
  2157. for (size = 0; offset < length; offset += size) {
  2158. TCA *tca = NULL;
  2159. byte type;
  2160. const byte* id = NULL;
  2161. word16 idSz = 0;
  2162. if (offset + ENUM_LEN > length)
  2163. return BUFFER_ERROR;
  2164. type = input[offset++];
  2165. switch (type) {
  2166. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2167. break;
  2168. #ifndef NO_SHA
  2169. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2170. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2171. if (offset + WC_SHA_DIGEST_SIZE > length)
  2172. return BUFFER_ERROR;
  2173. idSz = WC_SHA_DIGEST_SIZE;
  2174. id = input + offset;
  2175. offset += idSz;
  2176. break;
  2177. #endif
  2178. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2179. if (offset + OPAQUE16_LEN > length)
  2180. return BUFFER_ERROR;
  2181. ato16(input + offset, &idSz);
  2182. offset += OPAQUE16_LEN;
  2183. if ((offset > length) || (idSz > length - offset))
  2184. return BUFFER_ERROR;
  2185. id = input + offset;
  2186. offset += idSz;
  2187. break;
  2188. default:
  2189. WOLFSSL_ERROR_VERBOSE(TCA_INVALID_ID_TYPE);
  2190. return TCA_INVALID_ID_TYPE;
  2191. }
  2192. /* Find the type/ID in the TCA list. */
  2193. tca = TLSX_TCA_Find((TCA*)extension->data, type, id, idSz);
  2194. if (tca != NULL) {
  2195. /* Found it. Set the response flag and break out of the loop. */
  2196. TLSX_SetResponse(ssl, TLSX_TRUSTED_CA_KEYS);
  2197. break;
  2198. }
  2199. }
  2200. #else
  2201. (void)input;
  2202. #endif
  2203. return 0;
  2204. }
  2205. /* Checks to see if the server sent a response for the TCA. */
  2206. static int TLSX_TCA_VerifyParse(WOLFSSL* ssl, byte isRequest)
  2207. {
  2208. (void)ssl;
  2209. if (!isRequest) {
  2210. #ifndef NO_WOLFSSL_CLIENT
  2211. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_TRUSTED_CA_KEYS);
  2212. if (extension && !extension->resp) {
  2213. SendAlert(ssl, alert_fatal, handshake_failure);
  2214. WOLFSSL_ERROR_VERBOSE(TCA_ABSENT_ERROR);
  2215. return TCA_ABSENT_ERROR;
  2216. }
  2217. #endif /* NO_WOLFSSL_CLIENT */
  2218. }
  2219. return 0;
  2220. }
  2221. int TLSX_UseTrustedCA(TLSX** extensions, byte type,
  2222. const byte* id, word16 idSz, void* heap)
  2223. {
  2224. TLSX* extension;
  2225. TCA* tca = NULL;
  2226. if (extensions == NULL)
  2227. return BAD_FUNC_ARG;
  2228. if ((tca = TLSX_TCA_New(type, id, idSz, heap)) == NULL)
  2229. return MEMORY_E;
  2230. extension = TLSX_Find(*extensions, TLSX_TRUSTED_CA_KEYS);
  2231. if (!extension) {
  2232. int ret = TLSX_Push(extensions, TLSX_TRUSTED_CA_KEYS, (void*)tca, heap);
  2233. if (ret != 0) {
  2234. TLSX_TCA_Free(tca, heap);
  2235. return ret;
  2236. }
  2237. }
  2238. else {
  2239. /* push new TCA object to extension data. */
  2240. tca->next = (TCA*)extension->data;
  2241. extension->data = (void*)tca;
  2242. }
  2243. return WOLFSSL_SUCCESS;
  2244. }
  2245. #define TCA_FREE_ALL TLSX_TCA_FreeAll
  2246. #define TCA_GET_SIZE TLSX_TCA_GetSize
  2247. #define TCA_WRITE TLSX_TCA_Write
  2248. #define TCA_PARSE TLSX_TCA_Parse
  2249. #define TCA_VERIFY_PARSE TLSX_TCA_VerifyParse
  2250. #else /* HAVE_TRUSTED_CA */
  2251. #define TCA_FREE_ALL(list, heap)
  2252. #define TCA_GET_SIZE(list) 0
  2253. #define TCA_WRITE(a, b) 0
  2254. #define TCA_PARSE(a, b, c, d) 0
  2255. #define TCA_VERIFY_PARSE(a, b) 0
  2256. #endif /* HAVE_TRUSTED_CA */
  2257. /******************************************************************************/
  2258. /* Max Fragment Length Negotiation */
  2259. /******************************************************************************/
  2260. #ifdef HAVE_MAX_FRAGMENT
  2261. static word16 TLSX_MFL_Write(byte* data, byte* output)
  2262. {
  2263. output[0] = data[0];
  2264. return ENUM_LEN;
  2265. }
  2266. static int TLSX_MFL_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2267. byte isRequest)
  2268. {
  2269. if (length != ENUM_LEN)
  2270. return BUFFER_ERROR;
  2271. #ifdef WOLFSSL_OLD_UNSUPPORTED_EXTENSION
  2272. (void) isRequest;
  2273. #else
  2274. if (!isRequest)
  2275. if (TLSX_CheckUnsupportedExtension(ssl, TLSX_MAX_FRAGMENT_LENGTH))
  2276. return TLSX_HandleUnsupportedExtension(ssl);
  2277. #endif
  2278. switch (*input) {
  2279. case WOLFSSL_MFL_2_8 : ssl->max_fragment = 256; break;
  2280. case WOLFSSL_MFL_2_9 : ssl->max_fragment = 512; break;
  2281. case WOLFSSL_MFL_2_10: ssl->max_fragment = 1024; break;
  2282. case WOLFSSL_MFL_2_11: ssl->max_fragment = 2048; break;
  2283. case WOLFSSL_MFL_2_12: ssl->max_fragment = 4096; break;
  2284. case WOLFSSL_MFL_2_13: ssl->max_fragment = 8192; break;
  2285. default:
  2286. SendAlert(ssl, alert_fatal, illegal_parameter);
  2287. WOLFSSL_ERROR_VERBOSE(UNKNOWN_MAX_FRAG_LEN_E);
  2288. return UNKNOWN_MAX_FRAG_LEN_E;
  2289. }
  2290. #ifndef NO_WOLFSSL_SERVER
  2291. if (isRequest) {
  2292. int ret = TLSX_UseMaxFragment(&ssl->extensions, *input, ssl->heap);
  2293. if (ret != WOLFSSL_SUCCESS)
  2294. return ret; /* throw error */
  2295. TLSX_SetResponse(ssl, TLSX_MAX_FRAGMENT_LENGTH);
  2296. }
  2297. #endif
  2298. return 0;
  2299. }
  2300. int TLSX_UseMaxFragment(TLSX** extensions, byte mfl, void* heap)
  2301. {
  2302. byte* data = NULL;
  2303. int ret = 0;
  2304. if (extensions == NULL || mfl < WOLFSSL_MFL_MIN || mfl > WOLFSSL_MFL_MAX)
  2305. return BAD_FUNC_ARG;
  2306. data = (byte*)XMALLOC(ENUM_LEN, heap, DYNAMIC_TYPE_TLSX);
  2307. if (data == NULL)
  2308. return MEMORY_E;
  2309. data[0] = mfl;
  2310. ret = TLSX_Push(extensions, TLSX_MAX_FRAGMENT_LENGTH, data, heap);
  2311. if (ret != 0) {
  2312. XFREE(data, heap, DYNAMIC_TYPE_TLSX);
  2313. return ret;
  2314. }
  2315. return WOLFSSL_SUCCESS;
  2316. }
  2317. #define MFL_FREE_ALL(data, heap) XFREE(data, (heap), DYNAMIC_TYPE_TLSX)
  2318. #define MFL_GET_SIZE(data) ENUM_LEN
  2319. #define MFL_WRITE TLSX_MFL_Write
  2320. #define MFL_PARSE TLSX_MFL_Parse
  2321. #else
  2322. #define MFL_FREE_ALL(a, b)
  2323. #define MFL_GET_SIZE(a) 0
  2324. #define MFL_WRITE(a, b) 0
  2325. #define MFL_PARSE(a, b, c, d) 0
  2326. #endif /* HAVE_MAX_FRAGMENT */
  2327. /******************************************************************************/
  2328. /* Truncated HMAC */
  2329. /******************************************************************************/
  2330. #ifdef HAVE_TRUNCATED_HMAC
  2331. static int TLSX_THM_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2332. byte isRequest)
  2333. {
  2334. if (length != 0 || input == NULL)
  2335. return BUFFER_ERROR;
  2336. if (!isRequest) {
  2337. #ifndef WOLFSSL_OLD_UNSUPPORTED_EXTENSION
  2338. if (TLSX_CheckUnsupportedExtension(ssl, TLSX_TRUNCATED_HMAC))
  2339. return TLSX_HandleUnsupportedExtension(ssl);
  2340. #endif
  2341. }
  2342. else {
  2343. #ifndef NO_WOLFSSL_SERVER
  2344. int ret = TLSX_UseTruncatedHMAC(&ssl->extensions, ssl->heap);
  2345. if (ret != WOLFSSL_SUCCESS)
  2346. return ret; /* throw error */
  2347. TLSX_SetResponse(ssl, TLSX_TRUNCATED_HMAC);
  2348. #endif
  2349. }
  2350. ssl->truncated_hmac = 1;
  2351. return 0;
  2352. }
  2353. int TLSX_UseTruncatedHMAC(TLSX** extensions, void* heap)
  2354. {
  2355. int ret = 0;
  2356. if (extensions == NULL)
  2357. return BAD_FUNC_ARG;
  2358. ret = TLSX_Push(extensions, TLSX_TRUNCATED_HMAC, NULL, heap);
  2359. if (ret != 0)
  2360. return ret;
  2361. return WOLFSSL_SUCCESS;
  2362. }
  2363. #define THM_PARSE TLSX_THM_Parse
  2364. #else
  2365. #define THM_PARSE(a, b, c, d) 0
  2366. #endif /* HAVE_TRUNCATED_HMAC */
  2367. /******************************************************************************/
  2368. /* Certificate Status Request */
  2369. /******************************************************************************/
  2370. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  2371. static void TLSX_CSR_Free(CertificateStatusRequest* csr, void* heap)
  2372. {
  2373. switch (csr->status_type) {
  2374. case WOLFSSL_CSR_OCSP:
  2375. FreeOcspRequest(&csr->request.ocsp);
  2376. break;
  2377. }
  2378. XFREE(csr, heap, DYNAMIC_TYPE_TLSX);
  2379. (void)heap;
  2380. }
  2381. static word16 TLSX_CSR_GetSize(CertificateStatusRequest* csr, byte isRequest)
  2382. {
  2383. word16 size = 0;
  2384. /* shut up compiler warnings */
  2385. (void) csr; (void) isRequest;
  2386. #ifndef NO_WOLFSSL_CLIENT
  2387. if (isRequest) {
  2388. switch (csr->status_type) {
  2389. case WOLFSSL_CSR_OCSP:
  2390. size += ENUM_LEN + 2 * OPAQUE16_LEN;
  2391. if (csr->request.ocsp.nonceSz)
  2392. size += OCSP_NONCE_EXT_SZ;
  2393. break;
  2394. }
  2395. }
  2396. #endif
  2397. #if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER)
  2398. if (!isRequest && csr->ssl->options.tls1_3)
  2399. return OPAQUE8_LEN + OPAQUE24_LEN + csr->response.length;
  2400. #endif
  2401. return size;
  2402. }
  2403. static word16 TLSX_CSR_Write(CertificateStatusRequest* csr, byte* output,
  2404. byte isRequest)
  2405. {
  2406. /* shut up compiler warnings */
  2407. (void) csr; (void) output; (void) isRequest;
  2408. #ifndef NO_WOLFSSL_CLIENT
  2409. if (isRequest) {
  2410. word16 offset = 0;
  2411. word16 length = 0;
  2412. /* type */
  2413. output[offset++] = csr->status_type;
  2414. switch (csr->status_type) {
  2415. case WOLFSSL_CSR_OCSP:
  2416. /* responder id list */
  2417. c16toa(0, output + offset);
  2418. offset += OPAQUE16_LEN;
  2419. /* request extensions */
  2420. if (csr->request.ocsp.nonceSz)
  2421. length = (word16)EncodeOcspRequestExtensions(
  2422. &csr->request.ocsp,
  2423. output + offset + OPAQUE16_LEN,
  2424. OCSP_NONCE_EXT_SZ);
  2425. c16toa(length, output + offset);
  2426. offset += OPAQUE16_LEN + length;
  2427. break;
  2428. }
  2429. return offset;
  2430. }
  2431. #endif
  2432. #if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER)
  2433. if (!isRequest && csr->ssl->options.tls1_3) {
  2434. word16 offset = 0;
  2435. output[offset++] = csr->status_type;
  2436. c32to24(csr->response.length, output + offset);
  2437. offset += OPAQUE24_LEN;
  2438. XMEMCPY(output + offset, csr->response.buffer, csr->response.length);
  2439. offset += csr->response.length;
  2440. return offset;
  2441. }
  2442. #endif
  2443. return 0;
  2444. }
  2445. static int TLSX_CSR_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2446. byte isRequest)
  2447. {
  2448. int ret;
  2449. #if !defined(NO_WOLFSSL_SERVER)
  2450. byte status_type;
  2451. word16 size = 0;
  2452. #if defined(WOLFSSL_TLS13)
  2453. DecodedCert* cert;
  2454. #endif
  2455. #endif
  2456. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER) \
  2457. && defined(WOLFSSL_TLS13)
  2458. OcspRequest* request;
  2459. TLSX* extension;
  2460. CertificateStatusRequest* csr;
  2461. #endif
  2462. #if !defined(NO_WOLFSSL_CLIENT) && defined(WOLFSSL_TLS13) \
  2463. || !defined(NO_WOLFSSL_SERVER)
  2464. word32 offset = 0;
  2465. #endif
  2466. #if !defined(NO_WOLFSSL_CLIENT) && defined(WOLFSSL_TLS13)
  2467. word32 resp_length;
  2468. #endif
  2469. /* shut up compiler warnings */
  2470. (void) ssl; (void) input;
  2471. if (!isRequest) {
  2472. #ifndef NO_WOLFSSL_CLIENT
  2473. extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2474. csr = extension ? (CertificateStatusRequest*)extension->data : NULL;
  2475. if (!csr) {
  2476. /* look at context level */
  2477. extension = TLSX_Find(ssl->ctx->extensions, TLSX_STATUS_REQUEST);
  2478. csr = extension ? (CertificateStatusRequest*)extension->data : NULL;
  2479. if (!csr) /* unexpected extension */
  2480. return TLSX_HandleUnsupportedExtension(ssl);
  2481. /* enable extension at ssl level */
  2482. ret = TLSX_UseCertificateStatusRequest(&ssl->extensions,
  2483. csr->status_type, csr->options, ssl,
  2484. ssl->heap, ssl->devId);
  2485. if (ret != WOLFSSL_SUCCESS)
  2486. return ret;
  2487. switch (csr->status_type) {
  2488. case WOLFSSL_CSR_OCSP:
  2489. /* propagate nonce */
  2490. if (csr->request.ocsp.nonceSz) {
  2491. request =
  2492. (OcspRequest*)TLSX_CSR_GetRequest(ssl->extensions);
  2493. if (request) {
  2494. XMEMCPY(request->nonce, csr->request.ocsp.nonce,
  2495. csr->request.ocsp.nonceSz);
  2496. request->nonceSz = csr->request.ocsp.nonceSz;
  2497. }
  2498. }
  2499. break;
  2500. }
  2501. }
  2502. ssl->status_request = 1;
  2503. #ifdef WOLFSSL_TLS13
  2504. if (ssl->options.tls1_3) {
  2505. /* Get the new extension potentially created above. */
  2506. extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2507. csr = extension ? (CertificateStatusRequest*)extension->data : NULL;
  2508. if (csr == NULL)
  2509. return MEMORY_ERROR;
  2510. ret = 0;
  2511. if (OPAQUE8_LEN + OPAQUE24_LEN > length)
  2512. ret = BUFFER_ERROR;
  2513. if (ret == 0 && input[offset++] != WOLFSSL_CSR_OCSP) {
  2514. ret = BAD_CERTIFICATE_STATUS_ERROR;
  2515. WOLFSSL_ERROR_VERBOSE(ret);
  2516. }
  2517. if (ret == 0) {
  2518. c24to32(input + offset, &resp_length);
  2519. offset += OPAQUE24_LEN;
  2520. if (offset + resp_length != length)
  2521. ret = BUFFER_ERROR;
  2522. }
  2523. if (ret == 0) {
  2524. csr->response.buffer = (byte*)(input + offset);
  2525. csr->response.length = resp_length;
  2526. }
  2527. return ret;
  2528. }
  2529. else
  2530. #endif
  2531. {
  2532. /* extension_data MUST be empty. */
  2533. return length ? BUFFER_ERROR : 0;
  2534. }
  2535. #endif
  2536. }
  2537. else {
  2538. #ifndef NO_WOLFSSL_SERVER
  2539. if (length == 0)
  2540. return 0;
  2541. status_type = input[offset++];
  2542. switch (status_type) {
  2543. case WOLFSSL_CSR_OCSP: {
  2544. /* skip responder_id_list */
  2545. if ((int)(length - offset) < OPAQUE16_LEN)
  2546. return BUFFER_ERROR;
  2547. ato16(input + offset, &size);
  2548. offset += OPAQUE16_LEN + size;
  2549. /* skip request_extensions */
  2550. if ((int)(length - offset) < OPAQUE16_LEN)
  2551. return BUFFER_ERROR;
  2552. ato16(input + offset, &size);
  2553. offset += OPAQUE16_LEN + size;
  2554. if (offset > length)
  2555. return BUFFER_ERROR;
  2556. /* is able to send OCSP response? */
  2557. if (SSL_CM(ssl) == NULL || !SSL_CM(ssl)->ocspStaplingEnabled)
  2558. return 0;
  2559. }
  2560. break;
  2561. /* unknown status type */
  2562. default:
  2563. return 0;
  2564. }
  2565. /* if using status_request and already sending it, skip this one */
  2566. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  2567. if (ssl->status_request_v2)
  2568. return 0;
  2569. #endif
  2570. /* accept the first good status_type and return */
  2571. ret = TLSX_UseCertificateStatusRequest(&ssl->extensions, status_type,
  2572. 0, ssl, ssl->heap, ssl->devId);
  2573. if (ret != WOLFSSL_SUCCESS)
  2574. return ret; /* throw error */
  2575. #if defined(WOLFSSL_TLS13)
  2576. if (ssl->options.tls1_3) {
  2577. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), ssl->heap,
  2578. DYNAMIC_TYPE_DCERT);
  2579. if (cert == NULL) {
  2580. return MEMORY_E;
  2581. }
  2582. InitDecodedCert(cert, ssl->buffers.certificate->buffer,
  2583. ssl->buffers.certificate->length, ssl->heap);
  2584. ret = ParseCert(cert, CERT_TYPE, 1, SSL_CM(ssl));
  2585. if (ret != 0 ) {
  2586. XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
  2587. return ret;
  2588. }
  2589. ret = TLSX_CSR_InitRequest(ssl->extensions, cert, ssl->heap);
  2590. if (ret != 0 ) {
  2591. XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
  2592. return ret;
  2593. }
  2594. XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
  2595. extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2596. csr = extension ?
  2597. (CertificateStatusRequest*)extension->data : NULL;
  2598. if (csr == NULL)
  2599. return MEMORY_ERROR;
  2600. request = &csr->request.ocsp;
  2601. ret = CreateOcspResponse(ssl, &request, &csr->response);
  2602. if (ret != 0)
  2603. return ret;
  2604. if (csr->response.buffer)
  2605. TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST);
  2606. }
  2607. else
  2608. #endif
  2609. TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST);
  2610. ssl->status_request = status_type;
  2611. #endif
  2612. }
  2613. return 0;
  2614. }
  2615. int TLSX_CSR_InitRequest(TLSX* extensions, DecodedCert* cert, void* heap)
  2616. {
  2617. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST);
  2618. CertificateStatusRequest* csr = extension ?
  2619. (CertificateStatusRequest*)extension->data : NULL;
  2620. int ret = 0;
  2621. if (csr) {
  2622. switch (csr->status_type) {
  2623. case WOLFSSL_CSR_OCSP: {
  2624. byte nonce[MAX_OCSP_NONCE_SZ];
  2625. int nonceSz = csr->request.ocsp.nonceSz;
  2626. /* preserve nonce */
  2627. XMEMCPY(nonce, csr->request.ocsp.nonce, nonceSz);
  2628. if ((ret = InitOcspRequest(&csr->request.ocsp, cert, 0, heap))
  2629. != 0)
  2630. return ret;
  2631. /* restore nonce */
  2632. XMEMCPY(csr->request.ocsp.nonce, nonce, nonceSz);
  2633. csr->request.ocsp.nonceSz = nonceSz;
  2634. }
  2635. break;
  2636. }
  2637. }
  2638. return ret;
  2639. }
  2640. void* TLSX_CSR_GetRequest(TLSX* extensions)
  2641. {
  2642. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST);
  2643. CertificateStatusRequest* csr = extension ?
  2644. (CertificateStatusRequest*)extension->data : NULL;
  2645. if (csr) {
  2646. switch (csr->status_type) {
  2647. case WOLFSSL_CSR_OCSP:
  2648. return &csr->request.ocsp;
  2649. }
  2650. }
  2651. return NULL;
  2652. }
  2653. int TLSX_CSR_ForceRequest(WOLFSSL* ssl)
  2654. {
  2655. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2656. CertificateStatusRequest* csr = extension ?
  2657. (CertificateStatusRequest*)extension->data : NULL;
  2658. if (csr) {
  2659. switch (csr->status_type) {
  2660. case WOLFSSL_CSR_OCSP:
  2661. if (SSL_CM(ssl)->ocspEnabled) {
  2662. csr->request.ocsp.ssl = ssl;
  2663. return CheckOcspRequest(SSL_CM(ssl)->ocsp,
  2664. &csr->request.ocsp, NULL);
  2665. }
  2666. else {
  2667. WOLFSSL_ERROR_VERBOSE(OCSP_LOOKUP_FAIL);
  2668. return OCSP_LOOKUP_FAIL;
  2669. }
  2670. }
  2671. }
  2672. return 0;
  2673. }
  2674. int TLSX_UseCertificateStatusRequest(TLSX** extensions, byte status_type,
  2675. byte options, WOLFSSL* ssl, void* heap,
  2676. int devId)
  2677. {
  2678. CertificateStatusRequest* csr = NULL;
  2679. int ret = 0;
  2680. if (!extensions || status_type != WOLFSSL_CSR_OCSP)
  2681. return BAD_FUNC_ARG;
  2682. csr = (CertificateStatusRequest*)
  2683. XMALLOC(sizeof(CertificateStatusRequest), heap, DYNAMIC_TYPE_TLSX);
  2684. if (!csr)
  2685. return MEMORY_E;
  2686. ForceZero(csr, sizeof(CertificateStatusRequest));
  2687. csr->status_type = status_type;
  2688. csr->options = options;
  2689. csr->ssl = ssl;
  2690. switch (csr->status_type) {
  2691. case WOLFSSL_CSR_OCSP:
  2692. if (options & WOLFSSL_CSR_OCSP_USE_NONCE) {
  2693. WC_RNG rng;
  2694. #ifndef HAVE_FIPS
  2695. ret = wc_InitRng_ex(&rng, heap, devId);
  2696. #else
  2697. ret = wc_InitRng(&rng);
  2698. (void)devId;
  2699. #endif
  2700. if (ret == 0) {
  2701. if (wc_RNG_GenerateBlock(&rng, csr->request.ocsp.nonce,
  2702. MAX_OCSP_NONCE_SZ) == 0)
  2703. csr->request.ocsp.nonceSz = MAX_OCSP_NONCE_SZ;
  2704. wc_FreeRng(&rng);
  2705. }
  2706. }
  2707. break;
  2708. }
  2709. if ((ret = TLSX_Push(extensions, TLSX_STATUS_REQUEST, csr, heap)) != 0) {
  2710. XFREE(csr, heap, DYNAMIC_TYPE_TLSX);
  2711. return ret;
  2712. }
  2713. return WOLFSSL_SUCCESS;
  2714. }
  2715. #define CSR_FREE_ALL TLSX_CSR_Free
  2716. #define CSR_GET_SIZE TLSX_CSR_GetSize
  2717. #define CSR_WRITE TLSX_CSR_Write
  2718. #define CSR_PARSE TLSX_CSR_Parse
  2719. #else
  2720. #define CSR_FREE_ALL(data, heap)
  2721. #define CSR_GET_SIZE(a, b) 0
  2722. #define CSR_WRITE(a, b, c) 0
  2723. #define CSR_PARSE(a, b, c, d) 0
  2724. #endif /* HAVE_CERTIFICATE_STATUS_REQUEST */
  2725. /******************************************************************************/
  2726. /* Certificate Status Request v2 */
  2727. /******************************************************************************/
  2728. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  2729. static void TLSX_CSR2_FreeAll(CertificateStatusRequestItemV2* csr2, void* heap)
  2730. {
  2731. CertificateStatusRequestItemV2* next;
  2732. for (; csr2; csr2 = next) {
  2733. next = csr2->next;
  2734. switch (csr2->status_type) {
  2735. case WOLFSSL_CSR2_OCSP:
  2736. case WOLFSSL_CSR2_OCSP_MULTI:
  2737. while(csr2->requests--)
  2738. FreeOcspRequest(&csr2->request.ocsp[csr2->requests]);
  2739. break;
  2740. }
  2741. XFREE(csr2, heap, DYNAMIC_TYPE_TLSX);
  2742. }
  2743. (void)heap;
  2744. }
  2745. static word16 TLSX_CSR2_GetSize(CertificateStatusRequestItemV2* csr2,
  2746. byte isRequest)
  2747. {
  2748. word16 size = 0;
  2749. /* shut up compiler warnings */
  2750. (void) csr2; (void) isRequest;
  2751. #ifndef NO_WOLFSSL_CLIENT
  2752. if (isRequest) {
  2753. CertificateStatusRequestItemV2* next;
  2754. for (size = OPAQUE16_LEN; csr2; csr2 = next) {
  2755. next = csr2->next;
  2756. switch (csr2->status_type) {
  2757. case WOLFSSL_CSR2_OCSP:
  2758. case WOLFSSL_CSR2_OCSP_MULTI:
  2759. size += ENUM_LEN + 3 * OPAQUE16_LEN;
  2760. if (csr2->request.ocsp[0].nonceSz)
  2761. size += OCSP_NONCE_EXT_SZ;
  2762. break;
  2763. }
  2764. }
  2765. }
  2766. #endif
  2767. return size;
  2768. }
  2769. static word16 TLSX_CSR2_Write(CertificateStatusRequestItemV2* csr2,
  2770. byte* output, byte isRequest)
  2771. {
  2772. /* shut up compiler warnings */
  2773. (void) csr2; (void) output; (void) isRequest;
  2774. #ifndef NO_WOLFSSL_CLIENT
  2775. if (isRequest) {
  2776. word16 offset;
  2777. word16 length;
  2778. for (offset = OPAQUE16_LEN; csr2 != NULL; csr2 = csr2->next) {
  2779. /* status_type */
  2780. output[offset++] = csr2->status_type;
  2781. /* request */
  2782. switch (csr2->status_type) {
  2783. case WOLFSSL_CSR2_OCSP:
  2784. case WOLFSSL_CSR2_OCSP_MULTI:
  2785. /* request_length */
  2786. length = 2 * OPAQUE16_LEN;
  2787. if (csr2->request.ocsp[0].nonceSz)
  2788. length += OCSP_NONCE_EXT_SZ;
  2789. c16toa(length, output + offset);
  2790. offset += OPAQUE16_LEN;
  2791. /* responder id list */
  2792. c16toa(0, output + offset);
  2793. offset += OPAQUE16_LEN;
  2794. /* request extensions */
  2795. length = 0;
  2796. if (csr2->request.ocsp[0].nonceSz)
  2797. length = (word16)EncodeOcspRequestExtensions(
  2798. &csr2->request.ocsp[0],
  2799. output + offset + OPAQUE16_LEN,
  2800. OCSP_NONCE_EXT_SZ);
  2801. c16toa(length, output + offset);
  2802. offset += OPAQUE16_LEN + length;
  2803. break;
  2804. }
  2805. }
  2806. /* list size */
  2807. c16toa(offset - OPAQUE16_LEN, output);
  2808. return offset;
  2809. }
  2810. #endif
  2811. return 0;
  2812. }
  2813. static int TLSX_CSR2_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2814. byte isRequest)
  2815. {
  2816. int ret;
  2817. /* shut up compiler warnings */
  2818. (void) ssl; (void) input;
  2819. if (!isRequest) {
  2820. #ifndef NO_WOLFSSL_CLIENT
  2821. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST_V2);
  2822. CertificateStatusRequestItemV2* csr2 = extension ?
  2823. (CertificateStatusRequestItemV2*)extension->data : NULL;
  2824. if (!csr2) {
  2825. /* look at context level */
  2826. extension = TLSX_Find(ssl->ctx->extensions, TLSX_STATUS_REQUEST_V2);
  2827. csr2 = extension ?
  2828. (CertificateStatusRequestItemV2*)extension->data : NULL;
  2829. if (!csr2) /* unexpected extension */
  2830. return TLSX_HandleUnsupportedExtension(ssl);
  2831. /* enable extension at ssl level */
  2832. for (; csr2; csr2 = csr2->next) {
  2833. ret = TLSX_UseCertificateStatusRequestV2(&ssl->extensions,
  2834. csr2->status_type, csr2->options, ssl->heap,
  2835. ssl->devId);
  2836. if (ret != WOLFSSL_SUCCESS)
  2837. return ret;
  2838. switch (csr2->status_type) {
  2839. case WOLFSSL_CSR2_OCSP:
  2840. /* followed by */
  2841. case WOLFSSL_CSR2_OCSP_MULTI:
  2842. /* propagate nonce */
  2843. if (csr2->request.ocsp[0].nonceSz) {
  2844. OcspRequest* request =
  2845. (OcspRequest*)TLSX_CSR2_GetRequest(ssl->extensions,
  2846. csr2->status_type, 0);
  2847. if (request) {
  2848. XMEMCPY(request->nonce,
  2849. csr2->request.ocsp[0].nonce,
  2850. csr2->request.ocsp[0].nonceSz);
  2851. request->nonceSz =
  2852. csr2->request.ocsp[0].nonceSz;
  2853. }
  2854. }
  2855. break;
  2856. }
  2857. }
  2858. }
  2859. ssl->status_request_v2 = 1;
  2860. return length ? BUFFER_ERROR : 0; /* extension_data MUST be empty. */
  2861. #endif
  2862. }
  2863. else {
  2864. #ifndef NO_WOLFSSL_SERVER
  2865. byte status_type;
  2866. word16 request_length;
  2867. word16 offset = 0;
  2868. word16 size = 0;
  2869. /* list size */
  2870. if (offset + OPAQUE16_LEN >= length) {
  2871. return BUFFER_E;
  2872. }
  2873. ato16(input + offset, &request_length);
  2874. offset += OPAQUE16_LEN;
  2875. if (length - OPAQUE16_LEN != request_length)
  2876. return BUFFER_ERROR;
  2877. while (length > offset) {
  2878. if ((int)(length - offset) < ENUM_LEN + OPAQUE16_LEN)
  2879. return BUFFER_ERROR;
  2880. status_type = input[offset++];
  2881. ato16(input + offset, &request_length);
  2882. offset += OPAQUE16_LEN;
  2883. if (length - offset < request_length)
  2884. return BUFFER_ERROR;
  2885. switch (status_type) {
  2886. case WOLFSSL_CSR2_OCSP:
  2887. case WOLFSSL_CSR2_OCSP_MULTI:
  2888. /* skip responder_id_list */
  2889. if ((int)(length - offset) < OPAQUE16_LEN)
  2890. return BUFFER_ERROR;
  2891. ato16(input + offset, &size);
  2892. if (length - offset < size)
  2893. return BUFFER_ERROR;
  2894. offset += OPAQUE16_LEN + size;
  2895. /* skip request_extensions */
  2896. if ((int)(length - offset) < OPAQUE16_LEN)
  2897. return BUFFER_ERROR;
  2898. ato16(input + offset, &size);
  2899. if (length - offset < size)
  2900. return BUFFER_ERROR;
  2901. offset += OPAQUE16_LEN + size;
  2902. if (offset > length)
  2903. return BUFFER_ERROR;
  2904. /* is able to send OCSP response? */
  2905. if (SSL_CM(ssl) == NULL
  2906. || !SSL_CM(ssl)->ocspStaplingEnabled)
  2907. continue;
  2908. break;
  2909. default:
  2910. /* unknown status type, skipping! */
  2911. offset += request_length;
  2912. continue;
  2913. }
  2914. /* if using status_request and already sending it, remove it
  2915. * and prefer to use the v2 version */
  2916. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  2917. if (ssl->status_request) {
  2918. ssl->status_request = 0;
  2919. TLSX_Remove(&ssl->extensions, TLSX_STATUS_REQUEST, ssl->heap);
  2920. }
  2921. #endif
  2922. /* TLS 1.3 servers MUST NOT act upon presence or information in
  2923. * this extension (RFC 8448 Section 4.4.2.1).
  2924. */
  2925. if (!IsAtLeastTLSv1_3(ssl->version)) {
  2926. /* accept the first good status_type and return */
  2927. ret = TLSX_UseCertificateStatusRequestV2(&ssl->extensions,
  2928. status_type, 0, ssl->heap, ssl->devId);
  2929. if (ret != WOLFSSL_SUCCESS)
  2930. return ret; /* throw error */
  2931. TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST_V2);
  2932. ssl->status_request_v2 = status_type;
  2933. }
  2934. return 0;
  2935. }
  2936. #endif
  2937. }
  2938. return 0;
  2939. }
  2940. int TLSX_CSR2_InitRequests(TLSX* extensions, DecodedCert* cert, byte isPeer,
  2941. void* heap)
  2942. {
  2943. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST_V2);
  2944. CertificateStatusRequestItemV2* csr2 = extension ?
  2945. (CertificateStatusRequestItemV2*)extension->data : NULL;
  2946. int ret = 0;
  2947. for (; csr2; csr2 = csr2->next) {
  2948. switch (csr2->status_type) {
  2949. case WOLFSSL_CSR2_OCSP:
  2950. if (!isPeer || csr2->requests != 0)
  2951. break;
  2952. FALL_THROUGH; /* followed by */
  2953. case WOLFSSL_CSR2_OCSP_MULTI: {
  2954. if (csr2->requests < 1 + MAX_CHAIN_DEPTH) {
  2955. byte nonce[MAX_OCSP_NONCE_SZ];
  2956. int nonceSz = csr2->request.ocsp[0].nonceSz;
  2957. /* preserve nonce, replicating nonce of ocsp[0] */
  2958. XMEMCPY(nonce, csr2->request.ocsp[0].nonce, nonceSz);
  2959. if ((ret = InitOcspRequest(
  2960. &csr2->request.ocsp[csr2->requests], cert,
  2961. 0, heap)) != 0)
  2962. return ret;
  2963. /* restore nonce */
  2964. XMEMCPY(csr2->request.ocsp[csr2->requests].nonce,
  2965. nonce, nonceSz);
  2966. csr2->request.ocsp[csr2->requests].nonceSz = nonceSz;
  2967. csr2->requests++;
  2968. }
  2969. }
  2970. break;
  2971. }
  2972. }
  2973. (void)cert;
  2974. return ret;
  2975. }
  2976. void* TLSX_CSR2_GetRequest(TLSX* extensions, byte status_type, byte idx)
  2977. {
  2978. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST_V2);
  2979. CertificateStatusRequestItemV2* csr2 = extension ?
  2980. (CertificateStatusRequestItemV2*)extension->data : NULL;
  2981. for (; csr2; csr2 = csr2->next) {
  2982. if (csr2->status_type == status_type) {
  2983. switch (csr2->status_type) {
  2984. case WOLFSSL_CSR2_OCSP:
  2985. /* followed by */
  2986. case WOLFSSL_CSR2_OCSP_MULTI:
  2987. /* requests are initialized in the reverse order */
  2988. return idx < csr2->requests
  2989. ? &csr2->request.ocsp[csr2->requests - idx - 1]
  2990. : NULL;
  2991. }
  2992. }
  2993. }
  2994. return NULL;
  2995. }
  2996. int TLSX_CSR2_ForceRequest(WOLFSSL* ssl)
  2997. {
  2998. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST_V2);
  2999. CertificateStatusRequestItemV2* csr2 = extension ?
  3000. (CertificateStatusRequestItemV2*)extension->data : NULL;
  3001. /* forces only the first one */
  3002. if (csr2) {
  3003. switch (csr2->status_type) {
  3004. case WOLFSSL_CSR2_OCSP:
  3005. /* followed by */
  3006. case WOLFSSL_CSR2_OCSP_MULTI:
  3007. if (SSL_CM(ssl)->ocspEnabled) {
  3008. csr2->request.ocsp[0].ssl = ssl;
  3009. return CheckOcspRequest(SSL_CM(ssl)->ocsp,
  3010. &csr2->request.ocsp[0], NULL);
  3011. }
  3012. else {
  3013. WOLFSSL_ERROR_VERBOSE(OCSP_LOOKUP_FAIL);
  3014. return OCSP_LOOKUP_FAIL;
  3015. }
  3016. }
  3017. }
  3018. return 0;
  3019. }
  3020. int TLSX_UseCertificateStatusRequestV2(TLSX** extensions, byte status_type,
  3021. byte options, void* heap, int devId)
  3022. {
  3023. TLSX* extension = NULL;
  3024. CertificateStatusRequestItemV2* csr2 = NULL;
  3025. int ret = 0;
  3026. if (!extensions)
  3027. return BAD_FUNC_ARG;
  3028. if (status_type != WOLFSSL_CSR2_OCSP
  3029. && status_type != WOLFSSL_CSR2_OCSP_MULTI)
  3030. return BAD_FUNC_ARG;
  3031. csr2 = (CertificateStatusRequestItemV2*)
  3032. XMALLOC(sizeof(CertificateStatusRequestItemV2), heap, DYNAMIC_TYPE_TLSX);
  3033. if (!csr2)
  3034. return MEMORY_E;
  3035. ForceZero(csr2, sizeof(CertificateStatusRequestItemV2));
  3036. csr2->status_type = status_type;
  3037. csr2->options = options;
  3038. csr2->next = NULL;
  3039. switch (csr2->status_type) {
  3040. case WOLFSSL_CSR2_OCSP:
  3041. case WOLFSSL_CSR2_OCSP_MULTI:
  3042. if (options & WOLFSSL_CSR2_OCSP_USE_NONCE) {
  3043. WC_RNG rng;
  3044. #ifndef HAVE_FIPS
  3045. ret = wc_InitRng_ex(&rng, heap, devId);
  3046. #else
  3047. ret = wc_InitRng(&rng);
  3048. (void)devId;
  3049. #endif
  3050. if (ret == 0) {
  3051. if (wc_RNG_GenerateBlock(&rng, csr2->request.ocsp[0].nonce,
  3052. MAX_OCSP_NONCE_SZ) == 0)
  3053. csr2->request.ocsp[0].nonceSz = MAX_OCSP_NONCE_SZ;
  3054. wc_FreeRng(&rng);
  3055. }
  3056. }
  3057. break;
  3058. }
  3059. /* append new item */
  3060. if ((extension = TLSX_Find(*extensions, TLSX_STATUS_REQUEST_V2))) {
  3061. CertificateStatusRequestItemV2* last =
  3062. (CertificateStatusRequestItemV2*)extension->data;
  3063. for (; last->next; last = last->next);
  3064. last->next = csr2;
  3065. }
  3066. else if ((ret = TLSX_Push(extensions, TLSX_STATUS_REQUEST_V2, csr2,heap))) {
  3067. XFREE(csr2, heap, DYNAMIC_TYPE_TLSX);
  3068. return ret;
  3069. }
  3070. return WOLFSSL_SUCCESS;
  3071. }
  3072. #define CSR2_FREE_ALL TLSX_CSR2_FreeAll
  3073. #define CSR2_GET_SIZE TLSX_CSR2_GetSize
  3074. #define CSR2_WRITE TLSX_CSR2_Write
  3075. #define CSR2_PARSE TLSX_CSR2_Parse
  3076. #else
  3077. #define CSR2_FREE_ALL(data, heap)
  3078. #define CSR2_GET_SIZE(a, b) 0
  3079. #define CSR2_WRITE(a, b, c) 0
  3080. #define CSR2_PARSE(a, b, c, d) 0
  3081. #endif /* HAVE_CERTIFICATE_STATUS_REQUEST_V2 */
  3082. /******************************************************************************/
  3083. /* Supported Elliptic Curves */
  3084. /******************************************************************************/
  3085. #ifdef HAVE_SUPPORTED_CURVES
  3086. #if !defined(HAVE_ECC) && !defined(HAVE_CURVE25519) && !defined(HAVE_CURVE448) \
  3087. && !defined(HAVE_FFDHE) && !defined(HAVE_PQC)
  3088. #error Elliptic Curves Extension requires Elliptic Curve Cryptography or liboqs groups. \
  3089. Use --enable-ecc and/or --enable-liboqs in the configure script or \
  3090. define HAVE_ECC. Alternatively use FFDHE for DH ciphersuites.
  3091. #endif
  3092. static int TLSX_SupportedCurve_New(SupportedCurve** curve, word16 name,
  3093. void* heap)
  3094. {
  3095. if (curve == NULL)
  3096. return BAD_FUNC_ARG;
  3097. (void)heap;
  3098. *curve = (SupportedCurve*)XMALLOC(sizeof(SupportedCurve), heap,
  3099. DYNAMIC_TYPE_TLSX);
  3100. if (*curve == NULL)
  3101. return MEMORY_E;
  3102. (*curve)->name = name;
  3103. (*curve)->next = NULL;
  3104. return 0;
  3105. }
  3106. static int TLSX_PointFormat_New(PointFormat** point, byte format, void* heap)
  3107. {
  3108. if (point == NULL)
  3109. return BAD_FUNC_ARG;
  3110. (void)heap;
  3111. *point = (PointFormat*)XMALLOC(sizeof(PointFormat), heap,
  3112. DYNAMIC_TYPE_TLSX);
  3113. if (*point == NULL)
  3114. return MEMORY_E;
  3115. (*point)->format = format;
  3116. (*point)->next = NULL;
  3117. return 0;
  3118. }
  3119. static void TLSX_SupportedCurve_FreeAll(SupportedCurve* list, void* heap)
  3120. {
  3121. SupportedCurve* curve;
  3122. while ((curve = list)) {
  3123. list = curve->next;
  3124. XFREE(curve, heap, DYNAMIC_TYPE_TLSX);
  3125. }
  3126. (void)heap;
  3127. }
  3128. static void TLSX_PointFormat_FreeAll(PointFormat* list, void* heap)
  3129. {
  3130. PointFormat* point;
  3131. while ((point = list)) {
  3132. list = point->next;
  3133. XFREE(point, heap, DYNAMIC_TYPE_TLSX);
  3134. }
  3135. (void)heap;
  3136. }
  3137. static int TLSX_SupportedCurve_Append(SupportedCurve* list, word16 name,
  3138. void* heap)
  3139. {
  3140. int ret = BAD_FUNC_ARG;
  3141. while (list) {
  3142. if (list->name == name) {
  3143. ret = 0; /* curve already in use */
  3144. break;
  3145. }
  3146. if (list->next == NULL) {
  3147. ret = TLSX_SupportedCurve_New(&list->next, name, heap);
  3148. break;
  3149. }
  3150. list = list->next;
  3151. }
  3152. return ret;
  3153. }
  3154. static int TLSX_PointFormat_Append(PointFormat* list, byte format, void* heap)
  3155. {
  3156. int ret = BAD_FUNC_ARG;
  3157. while (list) {
  3158. if (list->format == format) {
  3159. ret = 0; /* format already in use */
  3160. break;
  3161. }
  3162. if (list->next == NULL) {
  3163. ret = TLSX_PointFormat_New(&list->next, format, heap);
  3164. break;
  3165. }
  3166. list = list->next;
  3167. }
  3168. return ret;
  3169. }
  3170. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
  3171. #if defined(HAVE_FFDHE) && (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3172. defined(HAVE_CURVE448))
  3173. static void TLSX_SupportedCurve_ValidateRequest(const WOLFSSL* ssl,
  3174. const byte* semaphore)
  3175. {
  3176. /* If all pre-defined parameter types for key exchange are supported then
  3177. * always send SupportedGroups extension.
  3178. */
  3179. (void)ssl;
  3180. (void)semaphore;
  3181. }
  3182. #else
  3183. static void TLSX_SupportedCurve_ValidateRequest(WOLFSSL* ssl, byte* semaphore)
  3184. {
  3185. word16 i;
  3186. for (i = 0; i < ssl->suites->suiteSz; i += 2) {
  3187. if (ssl->suites->suites[i] == TLS13_BYTE)
  3188. return;
  3189. if ((ssl->suites->suites[i] == ECC_BYTE) ||
  3190. (ssl->suites->suites[i] == ECDHE_PSK_BYTE) ||
  3191. (ssl->suites->suites[i] == CHACHA_BYTE)) {
  3192. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3193. defined(HAVE_CURVE448)
  3194. return;
  3195. #endif
  3196. }
  3197. #ifdef HAVE_FFDHE
  3198. else {
  3199. return;
  3200. }
  3201. #endif
  3202. }
  3203. /* turns semaphore on to avoid sending this extension. */
  3204. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_GROUPS));
  3205. }
  3206. #endif
  3207. /* Only send PointFormats if TLSv13, ECC or CHACHA cipher suite present.
  3208. */
  3209. static void TLSX_PointFormat_ValidateRequest(WOLFSSL* ssl, byte* semaphore)
  3210. {
  3211. word16 i;
  3212. for (i = 0; i < ssl->suites->suiteSz; i += 2) {
  3213. if (ssl->suites->suites[i] == TLS13_BYTE)
  3214. return;
  3215. if ((ssl->suites->suites[i] == ECC_BYTE) ||
  3216. (ssl->suites->suites[i] == ECDHE_PSK_BYTE) ||
  3217. (ssl->suites->suites[i] == CHACHA_BYTE)) {
  3218. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3219. defined(HAVE_CURVE448)
  3220. return;
  3221. #endif
  3222. }
  3223. }
  3224. #ifdef HAVE_FFDHE
  3225. (void)semaphore;
  3226. return;
  3227. #else
  3228. /* turns semaphore on to avoid sending this extension. */
  3229. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  3230. #endif
  3231. }
  3232. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
  3233. #ifndef NO_WOLFSSL_SERVER
  3234. static void TLSX_PointFormat_ValidateResponse(WOLFSSL* ssl, byte* semaphore)
  3235. {
  3236. #if defined(HAVE_FFDHE) || defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3237. defined(HAVE_CURVE448)
  3238. (void)semaphore;
  3239. #endif
  3240. if (ssl->options.cipherSuite0 == TLS13_BYTE)
  3241. return;
  3242. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  3243. if (ssl->options.cipherSuite0 == ECC_BYTE ||
  3244. ssl->options.cipherSuite0 == ECDHE_PSK_BYTE ||
  3245. ssl->options.cipherSuite0 == CHACHA_BYTE) {
  3246. return;
  3247. }
  3248. #endif
  3249. /* turns semaphore on to avoid sending this extension. */
  3250. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  3251. }
  3252. #endif /* !NO_WOLFSSL_SERVER */
  3253. #ifndef NO_WOLFSSL_CLIENT
  3254. static word16 TLSX_SupportedCurve_GetSize(SupportedCurve* list)
  3255. {
  3256. SupportedCurve* curve;
  3257. word16 length = OPAQUE16_LEN; /* list length */
  3258. while ((curve = list)) {
  3259. list = curve->next;
  3260. length += OPAQUE16_LEN; /* curve length */
  3261. }
  3262. return length;
  3263. }
  3264. #endif
  3265. static word16 TLSX_PointFormat_GetSize(PointFormat* list)
  3266. {
  3267. PointFormat* point;
  3268. word16 length = ENUM_LEN; /* list length */
  3269. while ((point = list)) {
  3270. list = point->next;
  3271. length += ENUM_LEN; /* format length */
  3272. }
  3273. return length;
  3274. }
  3275. #ifndef NO_WOLFSSL_CLIENT
  3276. static word16 TLSX_SupportedCurve_Write(SupportedCurve* list, byte* output)
  3277. {
  3278. word16 offset = OPAQUE16_LEN;
  3279. while (list) {
  3280. c16toa(list->name, output + offset);
  3281. offset += OPAQUE16_LEN;
  3282. list = list->next;
  3283. }
  3284. c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
  3285. return offset;
  3286. }
  3287. #endif
  3288. static word16 TLSX_PointFormat_Write(PointFormat* list, byte* output)
  3289. {
  3290. word16 offset = ENUM_LEN;
  3291. while (list) {
  3292. output[offset++] = list->format;
  3293. list = list->next;
  3294. }
  3295. output[0] = (byte)(offset - ENUM_LEN);
  3296. return offset;
  3297. }
  3298. #if !defined(NO_WOLFSSL_SERVER) || (defined(WOLFSSL_TLS13) && \
  3299. !defined(WOLFSSL_NO_SERVER_GROUPS_EXT))
  3300. static int TLSX_SupportedCurve_Parse(WOLFSSL* ssl, const byte* input,
  3301. word16 length, byte isRequest)
  3302. {
  3303. word16 offset;
  3304. word16 name;
  3305. int ret;
  3306. if(!isRequest && !IsAtLeastTLSv1_3(ssl->version)) {
  3307. #ifdef WOLFSSL_ALLOW_SERVER_SC_EXT
  3308. return 0;
  3309. #else
  3310. return BUFFER_ERROR; /* servers doesn't send this extension. */
  3311. #endif
  3312. }
  3313. if (OPAQUE16_LEN > length || length % OPAQUE16_LEN)
  3314. return BUFFER_ERROR;
  3315. ato16(input, &offset);
  3316. /* validating curve list length */
  3317. if (length != OPAQUE16_LEN + offset)
  3318. return BUFFER_ERROR;
  3319. offset = OPAQUE16_LEN;
  3320. if (offset == length)
  3321. return 0;
  3322. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
  3323. if (!isRequest) {
  3324. TLSX* extension;
  3325. SupportedCurve* curve;
  3326. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3327. if (extension != NULL) {
  3328. /* Replace client list with server list of supported groups. */
  3329. curve = (SupportedCurve*)extension->data;
  3330. extension->data = NULL;
  3331. TLSX_SupportedCurve_FreeAll(curve, ssl->heap);
  3332. ato16(input + offset, &name);
  3333. offset += OPAQUE16_LEN;
  3334. ret = TLSX_SupportedCurve_New(&curve, name, ssl->heap);
  3335. if (ret != 0)
  3336. return ret; /* throw error */
  3337. extension->data = (void*)curve;
  3338. }
  3339. }
  3340. #endif
  3341. for (; offset < length; offset += OPAQUE16_LEN) {
  3342. ato16(input + offset, &name);
  3343. ret = TLSX_UseSupportedCurve(&ssl->extensions, name, ssl->heap);
  3344. /* If it is BAD_FUNC_ARG then it is a group we do not support, but
  3345. * that is fine. */
  3346. if (ret != WOLFSSL_SUCCESS && ret != BAD_FUNC_ARG) {
  3347. return ret;
  3348. }
  3349. }
  3350. return 0;
  3351. }
  3352. #endif
  3353. #if !defined(NO_WOLFSSL_SERVER)
  3354. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
  3355. /* Checks the priority of the groups on the server and set the supported groups
  3356. * response if there is a group not advertised by the client that is preferred.
  3357. *
  3358. * ssl SSL/TLS object.
  3359. * returns 0 on success, otherwise an error.
  3360. */
  3361. int TLSX_SupportedCurve_CheckPriority(WOLFSSL* ssl)
  3362. {
  3363. int ret;
  3364. TLSX* extension;
  3365. TLSX* priority = NULL;
  3366. TLSX* ext = NULL;
  3367. word16 name;
  3368. SupportedCurve* curve;
  3369. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3370. /* May be doing PSK with no key exchange. */
  3371. if (extension == NULL)
  3372. return 0;
  3373. ret = TLSX_PopulateSupportedGroups(ssl, &priority);
  3374. if (ret != WOLFSSL_SUCCESS) {
  3375. TLSX_FreeAll(priority, ssl->heap);
  3376. return ret;
  3377. }
  3378. ext = TLSX_Find(priority, TLSX_SUPPORTED_GROUPS);
  3379. if (ext == NULL) {
  3380. WOLFSSL_MSG("Could not find supported groups extension");
  3381. TLSX_FreeAll(priority, ssl->heap);
  3382. return 0;
  3383. }
  3384. curve = (SupportedCurve*)ext->data;
  3385. name = curve->name;
  3386. curve = (SupportedCurve*)extension->data;
  3387. while (curve != NULL) {
  3388. if (curve->name == name)
  3389. break;
  3390. curve = curve->next;
  3391. }
  3392. if (curve == NULL) {
  3393. /* Couldn't find the preferred group in client list. */
  3394. extension->resp = 1;
  3395. /* Send server list back and free client list. */
  3396. curve = (SupportedCurve*)extension->data;
  3397. extension->data = ext->data;
  3398. ext->data = curve;
  3399. }
  3400. TLSX_FreeAll(priority, ssl->heap);
  3401. return 0;
  3402. }
  3403. #endif /* WOLFSSL_TLS13 && !WOLFSSL_NO_SERVER_GROUPS_EXT */
  3404. #if defined(HAVE_FFDHE) && !defined(WOLFSSL_NO_TLS12)
  3405. #ifdef HAVE_PUBLIC_FFDHE
  3406. static int tlsx_ffdhe_find_group(WOLFSSL* ssl, SupportedCurve* clientGroup,
  3407. SupportedCurve* serverGroup)
  3408. {
  3409. int ret = 0;
  3410. SupportedCurve* group;
  3411. const DhParams* params = NULL;
  3412. for (; serverGroup != NULL; serverGroup = serverGroup->next) {
  3413. if (!WOLFSSL_NAMED_GROUP_IS_FFHDE(serverGroup->name))
  3414. continue;
  3415. for (group = clientGroup; group != NULL; group = group->next) {
  3416. if (serverGroup->name != group->name)
  3417. continue;
  3418. switch (serverGroup->name) {
  3419. #ifdef HAVE_FFDHE_2048
  3420. case WOLFSSL_FFDHE_2048:
  3421. params = wc_Dh_ffdhe2048_Get();
  3422. break;
  3423. #endif
  3424. #ifdef HAVE_FFDHE_3072
  3425. case WOLFSSL_FFDHE_3072:
  3426. params = wc_Dh_ffdhe3072_Get();
  3427. break;
  3428. #endif
  3429. #ifdef HAVE_FFDHE_4096
  3430. case WOLFSSL_FFDHE_4096:
  3431. params = wc_Dh_ffdhe4096_Get();
  3432. break;
  3433. #endif
  3434. #ifdef HAVE_FFDHE_6144
  3435. case WOLFSSL_FFDHE_6144:
  3436. params = wc_Dh_ffdhe6144_Get();
  3437. break;
  3438. #endif
  3439. #ifdef HAVE_FFDHE_8192
  3440. case WOLFSSL_FFDHE_8192:
  3441. params = wc_Dh_ffdhe8192_Get();
  3442. break;
  3443. #endif
  3444. default:
  3445. break;
  3446. }
  3447. if (params == NULL) {
  3448. ret = BAD_FUNC_ARG;
  3449. break;
  3450. }
  3451. if (params->p_len >= ssl->options.minDhKeySz &&
  3452. params->p_len <= ssl->options.maxDhKeySz) {
  3453. break;
  3454. }
  3455. }
  3456. if (ret != 0)
  3457. break;
  3458. if ((group != NULL) && (serverGroup->name == group->name))
  3459. break;
  3460. }
  3461. if ((ret == 0) && (serverGroup != NULL) && (params != NULL)) {
  3462. ssl->buffers.serverDH_P.buffer = (unsigned char *)params->p;
  3463. ssl->buffers.serverDH_P.length = params->p_len;
  3464. ssl->buffers.serverDH_G.buffer = (unsigned char *)params->g;
  3465. ssl->buffers.serverDH_G.length = params->g_len;
  3466. ssl->namedGroup = serverGroup->name;
  3467. #if !defined(WOLFSSL_OLD_PRIME_CHECK) && \
  3468. !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  3469. ssl->options.dhDoKeyTest = 0;
  3470. #endif
  3471. ssl->options.haveDH = 1;
  3472. }
  3473. return ret;
  3474. }
  3475. #else
  3476. static int tlsx_ffdhe_find_group(WOLFSSL* ssl, SupportedCurve* clientGroup,
  3477. SupportedCurve* serverGroup)
  3478. {
  3479. int ret = 0;
  3480. SupportedCurve* group;
  3481. word32 p_len;
  3482. for (; serverGroup != NULL; serverGroup = serverGroup->next) {
  3483. if (!WOLFSSL_NAMED_GROUP_IS_FFHDE(serverGroup->name))
  3484. continue;
  3485. for (group = clientGroup; group != NULL; group = group->next) {
  3486. if (serverGroup->name != group->name)
  3487. continue;
  3488. wc_DhGetNamedKeyParamSize(serverGroup->name, &p_len, NULL, NULL);
  3489. if (p_len == 0) {
  3490. ret = BAD_FUNC_ARG;
  3491. break;
  3492. }
  3493. if (p_len >= ssl->options.minDhKeySz &&
  3494. p_len <= ssl->options.maxDhKeySz) {
  3495. break;
  3496. }
  3497. }
  3498. if (ret != 0)
  3499. break;
  3500. if ((group != NULL) && (serverGroup->name == group->name))
  3501. break;
  3502. }
  3503. if ((ret == 0) && (serverGroup != NULL)) {
  3504. word32 pSz, gSz;
  3505. ssl->buffers.serverDH_P.buffer = NULL;
  3506. ssl->buffers.serverDH_G.buffer = NULL;
  3507. ret = wc_DhGetNamedKeyParamSize(serverGroup->name, &pSz, &gSz, NULL);
  3508. if (ret == 0) {
  3509. ssl->buffers.serverDH_P.buffer =
  3510. (byte*)XMALLOC(pSz, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  3511. if (ssl->buffers.serverDH_P.buffer == NULL)
  3512. ret = MEMORY_E;
  3513. else
  3514. ssl->buffers.serverDH_P.length = pSz;
  3515. }
  3516. if (ret == 0) {
  3517. ssl->buffers.serverDH_G.buffer =
  3518. (byte*)XMALLOC(gSz, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  3519. if (ssl->buffers.serverDH_G.buffer == NULL) {
  3520. ret = MEMORY_E;
  3521. } else
  3522. ssl->buffers.serverDH_G.length = gSz;
  3523. }
  3524. if (ret == 0) {
  3525. ret = wc_DhCopyNamedKey(serverGroup->name,
  3526. ssl->buffers.serverDH_P.buffer, &pSz,
  3527. ssl->buffers.serverDH_G.buffer, &gSz,
  3528. NULL, NULL);
  3529. }
  3530. if (ret == 0) {
  3531. ssl->buffers.weOwnDH = 1;
  3532. ssl->namedGroup = serverGroup->name;
  3533. #if !defined(WOLFSSL_OLD_PRIME_CHECK) && \
  3534. !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  3535. ssl->options.dhDoKeyTest = 0;
  3536. #endif
  3537. ssl->options.haveDH = 1;
  3538. }
  3539. else {
  3540. if (ssl->buffers.serverDH_P.buffer != NULL) {
  3541. XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap,
  3542. DYNAMIC_TYPE_PUBLIC_KEY);
  3543. ssl->buffers.serverDH_P.length = 0;
  3544. ssl->buffers.serverDH_P.buffer = NULL;
  3545. }
  3546. if (ssl->buffers.serverDH_G.buffer != NULL) {
  3547. XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap,
  3548. DYNAMIC_TYPE_PUBLIC_KEY);
  3549. ssl->buffers.serverDH_G.length = 0;
  3550. ssl->buffers.serverDH_G.buffer = NULL;
  3551. }
  3552. }
  3553. }
  3554. return ret;
  3555. }
  3556. #endif
  3557. /* Set the highest priority common FFDHE group on the server as compared to
  3558. * client extensions.
  3559. *
  3560. * ssl SSL/TLS object.
  3561. * returns 0 on success, otherwise an error.
  3562. */
  3563. int TLSX_SupportedFFDHE_Set(WOLFSSL* ssl)
  3564. {
  3565. int ret;
  3566. TLSX* priority = NULL;
  3567. TLSX* ext = NULL;
  3568. TLSX* extension;
  3569. SupportedCurve* clientGroup;
  3570. SupportedCurve* serverGroup;
  3571. SupportedCurve* group;
  3572. int found = 0;
  3573. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3574. /* May be doing PSK with no key exchange. */
  3575. if (extension == NULL)
  3576. return 0;
  3577. clientGroup = (SupportedCurve*)extension->data;
  3578. for (group = clientGroup; group != NULL; group = group->next) {
  3579. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(group->name)) {
  3580. found = 1;
  3581. break;
  3582. }
  3583. }
  3584. if (!found)
  3585. return 0;
  3586. if (ssl->buffers.serverDH_P.buffer && ssl->buffers.weOwnDH) {
  3587. XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap,
  3588. DYNAMIC_TYPE_PUBLIC_KEY);
  3589. }
  3590. if (ssl->buffers.serverDH_G.buffer && ssl->buffers.weOwnDH) {
  3591. XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap,
  3592. DYNAMIC_TYPE_PUBLIC_KEY);
  3593. }
  3594. ssl->buffers.serverDH_P.buffer = NULL;
  3595. ssl->buffers.serverDH_G.buffer = NULL;
  3596. ssl->buffers.weOwnDH = 0;
  3597. ssl->options.haveDH = 0;
  3598. ret = TLSX_PopulateSupportedGroups(ssl, &priority);
  3599. if (ret == WOLFSSL_SUCCESS) {
  3600. ext = TLSX_Find(priority, TLSX_SUPPORTED_GROUPS);
  3601. serverGroup = (SupportedCurve*)ext->data;
  3602. ret = tlsx_ffdhe_find_group(ssl, clientGroup, serverGroup);
  3603. }
  3604. TLSX_FreeAll(priority, ssl->heap);
  3605. return ret;
  3606. }
  3607. #endif /* HAVE_FFDHE && !WOLFSSL_NO_TLS12 */
  3608. #endif /* !NO_WOLFSSL_SERVER */
  3609. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
  3610. /* Return the preferred group.
  3611. *
  3612. * ssl SSL/TLS object.
  3613. * checkSupported Whether to check for the first supported group.
  3614. * returns BAD_FUNC_ARG if no group found, otherwise the group.
  3615. */
  3616. int TLSX_SupportedCurve_Preferred(WOLFSSL* ssl, int checkSupported)
  3617. {
  3618. TLSX* extension;
  3619. SupportedCurve* curve;
  3620. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3621. if (extension == NULL)
  3622. return BAD_FUNC_ARG;
  3623. curve = (SupportedCurve*)extension->data;
  3624. while (curve != NULL) {
  3625. if (!checkSupported || TLSX_KeyShare_IsSupported(curve->name))
  3626. return curve->name;
  3627. curve = curve->next;
  3628. }
  3629. return BAD_FUNC_ARG;
  3630. }
  3631. #endif /* HAVE_SUPPORTED_CURVES */
  3632. #ifndef NO_WOLFSSL_SERVER
  3633. static int TLSX_PointFormat_Parse(WOLFSSL* ssl, const byte* input,
  3634. word16 length, byte isRequest)
  3635. {
  3636. int ret;
  3637. /* validating formats list length */
  3638. if (ENUM_LEN > length || length != (word16)ENUM_LEN + input[0])
  3639. return BUFFER_ERROR;
  3640. if (isRequest) {
  3641. /* adding uncompressed point format to response */
  3642. ret = TLSX_UsePointFormat(&ssl->extensions, WOLFSSL_EC_PF_UNCOMPRESSED,
  3643. ssl->heap);
  3644. if (ret != WOLFSSL_SUCCESS)
  3645. return ret; /* throw error */
  3646. TLSX_SetResponse(ssl, TLSX_EC_POINT_FORMATS);
  3647. }
  3648. return 0;
  3649. }
  3650. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  3651. int TLSX_ValidateSupportedCurves(WOLFSSL* ssl, byte first, byte second) {
  3652. TLSX* extension = NULL;
  3653. SupportedCurve* curve = NULL;
  3654. word32 oid = 0;
  3655. word32 defOid = 0;
  3656. word32 defSz = 80; /* Maximum known curve size is 66. */
  3657. word32 nextOid = 0;
  3658. word32 nextSz = 80; /* Maximum known curve size is 66. */
  3659. word32 currOid = ssl->ecdhCurveOID;
  3660. int ephmSuite = 0;
  3661. word16 octets = 0; /* according to 'ecc_set_type ecc_sets[];' */
  3662. int key = 0; /* validate key */
  3663. (void)oid;
  3664. if (first == CHACHA_BYTE) {
  3665. switch (second) {
  3666. case TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
  3667. case TLS_PSK_WITH_CHACHA20_POLY1305_SHA256:
  3668. case TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
  3669. case TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256:
  3670. return 1; /* no suite restriction */
  3671. case TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
  3672. case TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256:
  3673. case TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
  3674. break;
  3675. }
  3676. }
  3677. if (first == ECC_BYTE || first == ECDHE_PSK_BYTE || first == CHACHA_BYTE)
  3678. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3679. if (!extension)
  3680. return 1; /* no suite restriction */
  3681. for (curve = (SupportedCurve*)extension->data;
  3682. curve && !key;
  3683. curve = curve->next) {
  3684. #ifdef OPENSSL_EXTRA
  3685. /* skip if name is not in supported ECC range
  3686. * or disabled by user */
  3687. if (curve->name > WOLFSSL_ECC_MAX ||
  3688. wolfSSL_curve_is_disabled(ssl, curve->name))
  3689. continue;
  3690. #endif
  3691. /* find supported curve */
  3692. switch (curve->name) {
  3693. #ifdef HAVE_ECC
  3694. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  3695. #ifndef NO_ECC_SECP
  3696. case WOLFSSL_ECC_SECP160R1:
  3697. oid = ECC_SECP160R1_OID;
  3698. octets = 20;
  3699. break;
  3700. #endif /* !NO_ECC_SECP */
  3701. #ifdef HAVE_ECC_SECPR2
  3702. case WOLFSSL_ECC_SECP160R2:
  3703. oid = ECC_SECP160R2_OID;
  3704. octets = 20;
  3705. break;
  3706. #endif /* HAVE_ECC_SECPR2 */
  3707. #ifdef HAVE_ECC_KOBLITZ
  3708. case WOLFSSL_ECC_SECP160K1:
  3709. oid = ECC_SECP160K1_OID;
  3710. octets = 20;
  3711. break;
  3712. #endif /* HAVE_ECC_KOBLITZ */
  3713. #endif
  3714. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  3715. #ifndef NO_ECC_SECP
  3716. case WOLFSSL_ECC_SECP192R1:
  3717. oid = ECC_SECP192R1_OID;
  3718. octets = 24;
  3719. break;
  3720. #endif /* !NO_ECC_SECP */
  3721. #ifdef HAVE_ECC_KOBLITZ
  3722. case WOLFSSL_ECC_SECP192K1:
  3723. oid = ECC_SECP192K1_OID;
  3724. octets = 24;
  3725. break;
  3726. #endif /* HAVE_ECC_KOBLITZ */
  3727. #endif
  3728. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  3729. #ifndef NO_ECC_SECP
  3730. case WOLFSSL_ECC_SECP224R1:
  3731. oid = ECC_SECP224R1_OID;
  3732. octets = 28;
  3733. break;
  3734. #endif /* !NO_ECC_SECP */
  3735. #ifdef HAVE_ECC_KOBLITZ
  3736. case WOLFSSL_ECC_SECP224K1:
  3737. oid = ECC_SECP224K1_OID;
  3738. octets = 28;
  3739. break;
  3740. #endif /* HAVE_ECC_KOBLITZ */
  3741. #endif
  3742. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  3743. #ifndef NO_ECC_SECP
  3744. case WOLFSSL_ECC_SECP256R1:
  3745. oid = ECC_SECP256R1_OID;
  3746. octets = 32;
  3747. break;
  3748. #endif /* !NO_ECC_SECP */
  3749. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  3750. #endif
  3751. #if (defined(HAVE_CURVE25519) || defined(HAVE_ED25519)) && ECC_MIN_KEY_SZ <= 256
  3752. case WOLFSSL_ECC_X25519:
  3753. oid = ECC_X25519_OID;
  3754. octets = 32;
  3755. break;
  3756. #endif /* HAVE_CURVE25519 */
  3757. #ifdef HAVE_ECC
  3758. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  3759. #ifdef HAVE_ECC_KOBLITZ
  3760. case WOLFSSL_ECC_SECP256K1:
  3761. oid = ECC_SECP256K1_OID;
  3762. octets = 32;
  3763. break;
  3764. #endif /* HAVE_ECC_KOBLITZ */
  3765. #ifdef HAVE_ECC_BRAINPOOL
  3766. case WOLFSSL_ECC_BRAINPOOLP256R1:
  3767. oid = ECC_BRAINPOOLP256R1_OID;
  3768. octets = 32;
  3769. break;
  3770. #endif /* HAVE_ECC_BRAINPOOL */
  3771. #endif
  3772. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  3773. #ifndef NO_ECC_SECP
  3774. case WOLFSSL_ECC_SECP384R1:
  3775. oid = ECC_SECP384R1_OID;
  3776. octets = 48;
  3777. break;
  3778. #endif /* !NO_ECC_SECP */
  3779. #ifdef HAVE_ECC_BRAINPOOL
  3780. case WOLFSSL_ECC_BRAINPOOLP384R1:
  3781. oid = ECC_BRAINPOOLP384R1_OID;
  3782. octets = 48;
  3783. break;
  3784. #endif /* HAVE_ECC_BRAINPOOL */
  3785. #endif
  3786. #endif
  3787. #if (defined(HAVE_CURVE448) || defined(HAVE_ED448)) && ECC_MIN_KEY_SZ <= 448
  3788. case WOLFSSL_ECC_X448:
  3789. oid = ECC_X448_OID;
  3790. octets = 57;
  3791. break;
  3792. #endif /* HAVE_CURVE448 */
  3793. #ifdef HAVE_ECC
  3794. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  3795. #ifdef HAVE_ECC_BRAINPOOL
  3796. case WOLFSSL_ECC_BRAINPOOLP512R1:
  3797. oid = ECC_BRAINPOOLP512R1_OID;
  3798. octets = 64;
  3799. break;
  3800. #endif /* HAVE_ECC_BRAINPOOL */
  3801. #endif
  3802. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  3803. #ifndef NO_ECC_SECP
  3804. case WOLFSSL_ECC_SECP521R1:
  3805. oid = ECC_SECP521R1_OID;
  3806. octets = 66;
  3807. break;
  3808. #endif /* !NO_ECC_SECP */
  3809. #endif
  3810. #endif
  3811. default: continue; /* unsupported curve */
  3812. }
  3813. #ifdef HAVE_ECC
  3814. /* Set default Oid */
  3815. if (defOid == 0 && ssl->eccTempKeySz <= octets && defSz > octets) {
  3816. defOid = oid;
  3817. defSz = octets;
  3818. }
  3819. /* The eccTempKeySz is the preferred ephemeral key size */
  3820. if (currOid == 0 && ssl->eccTempKeySz == octets)
  3821. currOid = oid;
  3822. if ((nextOid == 0 || nextSz > octets) && ssl->eccTempKeySz <= octets) {
  3823. nextOid = oid;
  3824. nextSz = octets;
  3825. }
  3826. #else
  3827. if (defOid == 0 && defSz > octets) {
  3828. defOid = oid;
  3829. defSz = octets;
  3830. }
  3831. if (currOid == 0)
  3832. currOid = oid;
  3833. if (nextOid == 0 || nextSz > octets) {
  3834. nextOid = oid;
  3835. nextSz = octets;
  3836. }
  3837. #endif
  3838. if (first == ECC_BYTE) {
  3839. switch (second) {
  3840. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
  3841. /* ECDHE_ECDSA */
  3842. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA:
  3843. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA:
  3844. case TLS_ECDHE_ECDSA_WITH_RC4_128_SHA:
  3845. case TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA:
  3846. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256:
  3847. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384:
  3848. case TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
  3849. case TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:
  3850. case TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8:
  3851. case TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8:
  3852. key |= ssl->ecdhCurveOID == oid;
  3853. ephmSuite = 1;
  3854. break;
  3855. #ifdef WOLFSSL_STATIC_DH
  3856. /* ECDH_ECDSA */
  3857. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA:
  3858. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA:
  3859. case TLS_ECDH_ECDSA_WITH_RC4_128_SHA:
  3860. case TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA:
  3861. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256:
  3862. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384:
  3863. case TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256:
  3864. case TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384:
  3865. if (oid == ECC_X25519_OID && defOid == oid) {
  3866. defOid = 0;
  3867. defSz = 80;
  3868. }
  3869. if (oid == ECC_X448_OID && defOid == oid) {
  3870. defOid = 0;
  3871. defSz = 80;
  3872. }
  3873. key |= ssl->pkCurveOID == oid;
  3874. break;
  3875. #endif /* WOLFSSL_STATIC_DH */
  3876. #endif /* HAVE_ECC || HAVE_ED25519 || HAVE_ED448 */
  3877. #ifndef NO_RSA
  3878. /* ECDHE_RSA */
  3879. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA:
  3880. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA:
  3881. case TLS_ECDHE_RSA_WITH_RC4_128_SHA:
  3882. case TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA:
  3883. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256:
  3884. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384:
  3885. case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:
  3886. case TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384:
  3887. key |= ssl->ecdhCurveOID == oid;
  3888. ephmSuite = 1;
  3889. break;
  3890. #if defined(HAVE_ECC) && defined(WOLFSSL_STATIC_DH)
  3891. /* ECDH_RSA */
  3892. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA:
  3893. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA:
  3894. case TLS_ECDH_RSA_WITH_RC4_128_SHA:
  3895. case TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA:
  3896. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256:
  3897. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384:
  3898. case TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256:
  3899. case TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384:
  3900. if (oid == ECC_X25519_OID && defOid == oid) {
  3901. defOid = 0;
  3902. defSz = 80;
  3903. }
  3904. if (oid == ECC_X448_OID && defOid == oid) {
  3905. defOid = 0;
  3906. defSz = 80;
  3907. }
  3908. key |= ssl->pkCurveOID == oid;
  3909. break;
  3910. #endif /* HAVE_ECC && WOLFSSL_STATIC_DH */
  3911. #endif
  3912. default:
  3913. if (oid == ECC_X25519_OID && defOid == oid) {
  3914. defOid = 0;
  3915. defSz = 80;
  3916. }
  3917. if (oid == ECC_X448_OID && defOid == oid) {
  3918. defOid = 0;
  3919. defSz = 80;
  3920. }
  3921. key = 1;
  3922. break;
  3923. }
  3924. }
  3925. /* ChaCha20-Poly1305 ECC cipher suites */
  3926. if (first == CHACHA_BYTE) {
  3927. switch (second) {
  3928. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
  3929. /* ECDHE_ECDSA */
  3930. case TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 :
  3931. case TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 :
  3932. key |= ssl->ecdhCurveOID == oid;
  3933. ephmSuite = 1;
  3934. break;
  3935. #endif /* HAVE_ECC || HAVE_ED25519 || HAVE_ED448 */
  3936. #ifndef NO_RSA
  3937. /* ECDHE_RSA */
  3938. case TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 :
  3939. case TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 :
  3940. key |= ssl->ecdhCurveOID == oid;
  3941. ephmSuite = 1;
  3942. break;
  3943. #endif
  3944. default:
  3945. key = 1;
  3946. break;
  3947. }
  3948. }
  3949. }
  3950. /* Choose the default if it is at the required strength. */
  3951. #ifdef HAVE_ECC
  3952. if (ssl->ecdhCurveOID == 0 && defSz == ssl->eccTempKeySz)
  3953. #else
  3954. if (ssl->ecdhCurveOID == 0)
  3955. #endif
  3956. {
  3957. key = 1;
  3958. ssl->ecdhCurveOID = defOid;
  3959. }
  3960. /* Choose any curve at the required strength. */
  3961. if (ssl->ecdhCurveOID == 0) {
  3962. key = 1;
  3963. ssl->ecdhCurveOID = currOid;
  3964. }
  3965. /* Choose the default if it is at the next highest strength. */
  3966. if (ssl->ecdhCurveOID == 0 && defSz == nextSz)
  3967. ssl->ecdhCurveOID = defOid;
  3968. /* Choose any curve at the next highest strength. */
  3969. if (ssl->ecdhCurveOID == 0)
  3970. ssl->ecdhCurveOID = nextOid;
  3971. /* No curve and ephemeral ECC suite requires a matching curve. */
  3972. if (ssl->ecdhCurveOID == 0 && ephmSuite)
  3973. key = 0;
  3974. return key;
  3975. }
  3976. #endif
  3977. #endif /* NO_WOLFSSL_SERVER */
  3978. int TLSX_UseSupportedCurve(TLSX** extensions, word16 name, void* heap)
  3979. {
  3980. TLSX* extension = NULL;
  3981. SupportedCurve* curve = NULL;
  3982. int ret;
  3983. if (extensions == NULL) {
  3984. return BAD_FUNC_ARG;
  3985. }
  3986. #ifdef WOLFSSL_TLS13
  3987. if (! TLSX_KeyShare_IsSupported(name)) {
  3988. return BAD_FUNC_ARG;
  3989. }
  3990. #endif
  3991. extension = TLSX_Find(*extensions, TLSX_SUPPORTED_GROUPS);
  3992. if (!extension) {
  3993. ret = TLSX_SupportedCurve_New(&curve, name, heap);
  3994. if (ret != 0)
  3995. return ret;
  3996. ret = TLSX_Push(extensions, TLSX_SUPPORTED_GROUPS, curve, heap);
  3997. if (ret != 0) {
  3998. XFREE(curve, heap, DYNAMIC_TYPE_TLSX);
  3999. return ret;
  4000. }
  4001. }
  4002. else {
  4003. ret = TLSX_SupportedCurve_Append((SupportedCurve*)extension->data, name,
  4004. heap);
  4005. if (ret != 0)
  4006. return ret;
  4007. }
  4008. return WOLFSSL_SUCCESS;
  4009. }
  4010. int TLSX_UsePointFormat(TLSX** extensions, byte format, void* heap)
  4011. {
  4012. TLSX* extension = NULL;
  4013. PointFormat* point = NULL;
  4014. int ret = 0;
  4015. if (extensions == NULL)
  4016. return BAD_FUNC_ARG;
  4017. extension = TLSX_Find(*extensions, TLSX_EC_POINT_FORMATS);
  4018. if (!extension) {
  4019. ret = TLSX_PointFormat_New(&point, format, heap);
  4020. if (ret != 0)
  4021. return ret;
  4022. ret = TLSX_Push(extensions, TLSX_EC_POINT_FORMATS, point, heap);
  4023. if (ret != 0) {
  4024. XFREE(point, heap, DYNAMIC_TYPE_TLSX);
  4025. return ret;
  4026. }
  4027. }
  4028. else {
  4029. ret = TLSX_PointFormat_Append((PointFormat*)extension->data, format,
  4030. heap);
  4031. if (ret != 0)
  4032. return ret;
  4033. }
  4034. return WOLFSSL_SUCCESS;
  4035. }
  4036. #define EC_FREE_ALL TLSX_SupportedCurve_FreeAll
  4037. #define EC_VALIDATE_REQUEST TLSX_SupportedCurve_ValidateRequest
  4038. #ifndef NO_WOLFSSL_CLIENT
  4039. #define EC_GET_SIZE TLSX_SupportedCurve_GetSize
  4040. #define EC_WRITE TLSX_SupportedCurve_Write
  4041. #else
  4042. #define EC_GET_SIZE(list) 0
  4043. #define EC_WRITE(a, b) 0
  4044. #endif
  4045. #if !defined(NO_WOLFSSL_SERVER) || (defined(WOLFSSL_TLS13) && \
  4046. !defined(WOLFSSL_NO_SERVER_GROUPS_EXT))
  4047. #define EC_PARSE TLSX_SupportedCurve_Parse
  4048. #else
  4049. #define EC_PARSE(a, b, c, d) 0
  4050. #endif
  4051. #define PF_FREE_ALL TLSX_PointFormat_FreeAll
  4052. #define PF_VALIDATE_REQUEST TLSX_PointFormat_ValidateRequest
  4053. #define PF_VALIDATE_RESPONSE TLSX_PointFormat_ValidateResponse
  4054. #define PF_GET_SIZE TLSX_PointFormat_GetSize
  4055. #define PF_WRITE TLSX_PointFormat_Write
  4056. #ifndef NO_WOLFSSL_SERVER
  4057. #define PF_PARSE TLSX_PointFormat_Parse
  4058. #else
  4059. #define PF_PARSE(a, b, c, d) 0
  4060. #endif
  4061. #else
  4062. #define EC_FREE_ALL(list, heap)
  4063. #define EC_GET_SIZE(list) 0
  4064. #define EC_WRITE(a, b) 0
  4065. #define EC_PARSE(a, b, c, d) 0
  4066. #define EC_VALIDATE_REQUEST(a, b)
  4067. #define PF_FREE_ALL(list, heap)
  4068. #define PF_GET_SIZE(list) 0
  4069. #define PF_WRITE(a, b) 0
  4070. #define PF_PARSE(a, b, c, d) 0
  4071. #define PF_VALIDATE_REQUEST(a, b)
  4072. #define PF_VALIDATE_RESPONSE(a, b)
  4073. #endif /* HAVE_SUPPORTED_CURVES */
  4074. /******************************************************************************/
  4075. /* Renegotiation Indication */
  4076. /******************************************************************************/
  4077. #if defined(HAVE_SECURE_RENEGOTIATION) \
  4078. || defined(HAVE_SERVER_RENEGOTIATION_INFO)
  4079. static byte TLSX_SecureRenegotiation_GetSize(SecureRenegotiation* data,
  4080. int isRequest)
  4081. {
  4082. byte length = OPAQUE8_LEN; /* empty info length */
  4083. /* data will be NULL for HAVE_SERVER_RENEGOTIATION_INFO only */
  4084. if (data && data->enabled && data->verifySet) {
  4085. /* client sends client_verify_data only */
  4086. length += TLS_FINISHED_SZ;
  4087. /* server also sends server_verify_data */
  4088. if (!isRequest)
  4089. length += TLS_FINISHED_SZ;
  4090. }
  4091. return length;
  4092. }
  4093. static word16 TLSX_SecureRenegotiation_Write(SecureRenegotiation* data,
  4094. byte* output, int isRequest)
  4095. {
  4096. word16 offset = OPAQUE8_LEN; /* RenegotiationInfo length */
  4097. if (data && data->enabled && data->verifySet) {
  4098. /* client sends client_verify_data only */
  4099. XMEMCPY(output + offset, data->client_verify_data, TLS_FINISHED_SZ);
  4100. offset += TLS_FINISHED_SZ;
  4101. /* server also sends server_verify_data */
  4102. if (!isRequest) {
  4103. XMEMCPY(output + offset, data->server_verify_data, TLS_FINISHED_SZ);
  4104. offset += TLS_FINISHED_SZ;
  4105. }
  4106. }
  4107. output[0] = (byte)(offset - 1); /* info length - self */
  4108. return offset;
  4109. }
  4110. static int TLSX_SecureRenegotiation_Parse(WOLFSSL* ssl, const byte* input,
  4111. word16 length, byte isRequest)
  4112. {
  4113. int ret = SECURE_RENEGOTIATION_E;
  4114. if (length >= OPAQUE8_LEN) {
  4115. if (isRequest) {
  4116. #ifndef NO_WOLFSSL_SERVER
  4117. if (ssl->secure_renegotiation == NULL) {
  4118. ret = wolfSSL_UseSecureRenegotiation(ssl);
  4119. if (ret == WOLFSSL_SUCCESS)
  4120. ret = 0;
  4121. }
  4122. if (ret != 0 && ret != SECURE_RENEGOTIATION_E) {
  4123. }
  4124. else if (ssl->secure_renegotiation == NULL) {
  4125. }
  4126. else if (!ssl->secure_renegotiation->enabled) {
  4127. if (*input == 0) {
  4128. input++; /* get past size */
  4129. ssl->secure_renegotiation->enabled = 1;
  4130. TLSX_SetResponse(ssl, TLSX_RENEGOTIATION_INFO);
  4131. ret = 0;
  4132. }
  4133. else {
  4134. /* already in error state */
  4135. WOLFSSL_MSG("SCR client verify data present");
  4136. }
  4137. }
  4138. else if (*input == TLS_FINISHED_SZ) {
  4139. if (length < TLS_FINISHED_SZ + 1) {
  4140. WOLFSSL_MSG("SCR malformed buffer");
  4141. ret = BUFFER_E;
  4142. }
  4143. else {
  4144. input++; /* get past size */
  4145. /* validate client verify data */
  4146. if (XMEMCMP(input,
  4147. ssl->secure_renegotiation->client_verify_data,
  4148. TLS_FINISHED_SZ) == 0) {
  4149. WOLFSSL_MSG("SCR client verify data match");
  4150. TLSX_SetResponse(ssl, TLSX_RENEGOTIATION_INFO);
  4151. ret = 0; /* verified */
  4152. }
  4153. else {
  4154. /* already in error state */
  4155. WOLFSSL_MSG("SCR client verify data Failure");
  4156. }
  4157. }
  4158. }
  4159. #endif
  4160. }
  4161. else if (ssl->secure_renegotiation != NULL) {
  4162. #ifndef NO_WOLFSSL_CLIENT
  4163. if (!ssl->secure_renegotiation->enabled) {
  4164. if (*input == 0) {
  4165. ssl->secure_renegotiation->enabled = 1;
  4166. ret = 0;
  4167. }
  4168. }
  4169. else if (*input == 2 * TLS_FINISHED_SZ &&
  4170. length == 2 * TLS_FINISHED_SZ + OPAQUE8_LEN) {
  4171. input++; /* get past size */
  4172. /* validate client and server verify data */
  4173. if (XMEMCMP(input,
  4174. ssl->secure_renegotiation->client_verify_data,
  4175. TLS_FINISHED_SZ) == 0 &&
  4176. XMEMCMP(input + TLS_FINISHED_SZ,
  4177. ssl->secure_renegotiation->server_verify_data,
  4178. TLS_FINISHED_SZ) == 0) {
  4179. WOLFSSL_MSG("SCR client and server verify data match");
  4180. ret = 0; /* verified */
  4181. }
  4182. else {
  4183. /* already in error state */
  4184. WOLFSSL_MSG("SCR client and server verify data Failure");
  4185. }
  4186. }
  4187. #endif
  4188. }
  4189. }
  4190. if (ret != 0) {
  4191. WOLFSSL_ERROR_VERBOSE(ret);
  4192. SendAlert(ssl, alert_fatal, handshake_failure);
  4193. }
  4194. return ret;
  4195. }
  4196. int TLSX_UseSecureRenegotiation(TLSX** extensions, void* heap)
  4197. {
  4198. int ret = 0;
  4199. SecureRenegotiation* data;
  4200. data = (SecureRenegotiation*)XMALLOC(sizeof(SecureRenegotiation), heap,
  4201. DYNAMIC_TYPE_TLSX);
  4202. if (data == NULL)
  4203. return MEMORY_E;
  4204. XMEMSET(data, 0, sizeof(SecureRenegotiation));
  4205. ret = TLSX_Push(extensions, TLSX_RENEGOTIATION_INFO, data, heap);
  4206. if (ret != 0) {
  4207. XFREE(data, heap, DYNAMIC_TYPE_TLSX);
  4208. return ret;
  4209. }
  4210. return WOLFSSL_SUCCESS;
  4211. }
  4212. #ifdef HAVE_SERVER_RENEGOTIATION_INFO
  4213. int TLSX_AddEmptyRenegotiationInfo(TLSX** extensions, void* heap)
  4214. {
  4215. int ret;
  4216. /* send empty renegotiation_info extension */
  4217. TLSX* ext = TLSX_Find(*extensions, TLSX_RENEGOTIATION_INFO);
  4218. if (ext == NULL) {
  4219. ret = TLSX_UseSecureRenegotiation(extensions, heap);
  4220. if (ret != WOLFSSL_SUCCESS)
  4221. return ret;
  4222. ext = TLSX_Find(*extensions, TLSX_RENEGOTIATION_INFO);
  4223. }
  4224. if (ext)
  4225. ext->resp = 1;
  4226. return WOLFSSL_SUCCESS;
  4227. }
  4228. #endif /* HAVE_SERVER_RENEGOTIATION_INFO */
  4229. #define SCR_FREE_ALL(data, heap) XFREE(data, (heap), DYNAMIC_TYPE_TLSX)
  4230. #define SCR_GET_SIZE TLSX_SecureRenegotiation_GetSize
  4231. #define SCR_WRITE TLSX_SecureRenegotiation_Write
  4232. #define SCR_PARSE TLSX_SecureRenegotiation_Parse
  4233. #else
  4234. #define SCR_FREE_ALL(a, heap)
  4235. #define SCR_GET_SIZE(a, b) 0
  4236. #define SCR_WRITE(a, b, c) 0
  4237. #define SCR_PARSE(a, b, c, d) 0
  4238. #endif /* HAVE_SECURE_RENEGOTIATION || HAVE_SERVER_RENEGOTIATION_INFO */
  4239. /******************************************************************************/
  4240. /* Session Tickets */
  4241. /******************************************************************************/
  4242. #ifdef HAVE_SESSION_TICKET
  4243. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
  4244. static void TLSX_SessionTicket_ValidateRequest(WOLFSSL* ssl)
  4245. {
  4246. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_SESSION_TICKET);
  4247. SessionTicket* ticket = extension ?
  4248. (SessionTicket*)extension->data : NULL;
  4249. if (ticket) {
  4250. /* TODO validate ticket timeout here! */
  4251. if (ticket->lifetime == 0xfffffff) {
  4252. /* send empty ticket on timeout */
  4253. TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
  4254. }
  4255. }
  4256. }
  4257. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
  4258. static word16 TLSX_SessionTicket_GetSize(SessionTicket* ticket, int isRequest)
  4259. {
  4260. (void)isRequest;
  4261. return ticket ? ticket->size : 0;
  4262. }
  4263. static word16 TLSX_SessionTicket_Write(SessionTicket* ticket, byte* output,
  4264. int isRequest)
  4265. {
  4266. word16 offset = 0; /* empty ticket */
  4267. if (isRequest && ticket) {
  4268. XMEMCPY(output + offset, ticket->data, ticket->size);
  4269. offset += ticket->size;
  4270. }
  4271. return offset;
  4272. }
  4273. static int TLSX_SessionTicket_Parse(WOLFSSL* ssl, const byte* input,
  4274. word16 length, byte isRequest)
  4275. {
  4276. int ret = 0;
  4277. (void) input; /* avoid unused parameter if NO_WOLFSSL_SERVER defined */
  4278. if (!isRequest) {
  4279. if (TLSX_CheckUnsupportedExtension(ssl, TLSX_SESSION_TICKET))
  4280. return TLSX_HandleUnsupportedExtension(ssl);
  4281. if (length != 0)
  4282. return BUFFER_ERROR;
  4283. #ifndef NO_WOLFSSL_CLIENT
  4284. ssl->expect_session_ticket = 1;
  4285. #endif
  4286. }
  4287. #ifndef NO_WOLFSSL_SERVER
  4288. else {
  4289. /* server side */
  4290. if (ssl->ctx->ticketEncCb == NULL) {
  4291. WOLFSSL_MSG("Client sent session ticket, server has no callback");
  4292. return 0;
  4293. }
  4294. if (length > SESSION_TICKET_LEN) {
  4295. ret = BAD_TICKET_MSG_SZ;
  4296. WOLFSSL_ERROR_VERBOSE(ret);
  4297. } else if (IsAtLeastTLSv1_3(ssl->version)) {
  4298. WOLFSSL_MSG("Process client ticket rejected, TLS 1.3 no support");
  4299. ssl->options.rejectTicket = 1;
  4300. ret = 0; /* not fatal */
  4301. } else if (ssl->options.noTicketTls12) {
  4302. /* ignore ticket request */
  4303. } else if (length == 0) {
  4304. /* blank ticket */
  4305. ret = TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
  4306. if (ret == WOLFSSL_SUCCESS) {
  4307. ret = 0;
  4308. /* send blank ticket */
  4309. TLSX_SetResponse(ssl, TLSX_SESSION_TICKET);
  4310. ssl->options.createTicket = 1; /* will send ticket msg */
  4311. ssl->options.useTicket = 1;
  4312. ssl->options.resuming = 0; /* no standard resumption */
  4313. ssl->arrays->sessionIDSz = 0; /* no echo on blank ticket */
  4314. }
  4315. } else {
  4316. /* got actual ticket from client */
  4317. ret = DoClientTicket(ssl, input, length);
  4318. if (ret == WOLFSSL_TICKET_RET_OK) { /* use ticket to resume */
  4319. WOLFSSL_MSG("Using existing client ticket");
  4320. ssl->options.useTicket = 1;
  4321. ssl->options.resuming = 1;
  4322. /* SERVER: ticket is peer auth. */
  4323. ssl->options.peerAuthGood = 1;
  4324. } else if (ret == WOLFSSL_TICKET_RET_CREATE) {
  4325. WOLFSSL_MSG("Using existing client ticket, creating new one");
  4326. ret = TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
  4327. if (ret == WOLFSSL_SUCCESS) {
  4328. ret = 0;
  4329. TLSX_SetResponse(ssl, TLSX_SESSION_TICKET);
  4330. /* send blank ticket */
  4331. ssl->options.createTicket = 1; /* will send ticket msg */
  4332. ssl->options.useTicket = 1;
  4333. ssl->options.resuming = 1;
  4334. /* SERVER: ticket is peer auth. */
  4335. ssl->options.peerAuthGood = 1;
  4336. }
  4337. } else if (ret == WOLFSSL_TICKET_RET_REJECT) {
  4338. WOLFSSL_MSG("Process client ticket rejected, not using");
  4339. ssl->options.rejectTicket = 1;
  4340. ret = 0; /* not fatal */
  4341. } else if (ret == VERSION_ERROR) {
  4342. WOLFSSL_MSG("Process client ticket rejected, bad TLS version");
  4343. ssl->options.rejectTicket = 1;
  4344. ret = 0; /* not fatal */
  4345. } else if (ret == WOLFSSL_TICKET_RET_FATAL) {
  4346. WOLFSSL_MSG("Process client ticket fatal error, not using");
  4347. } else if (ret < 0) {
  4348. WOLFSSL_MSG("Process client ticket unknown error, not using");
  4349. }
  4350. }
  4351. }
  4352. #endif /* NO_WOLFSSL_SERVER */
  4353. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  4354. (void)ssl;
  4355. #endif
  4356. return ret;
  4357. }
  4358. WOLFSSL_LOCAL SessionTicket* TLSX_SessionTicket_Create(word32 lifetime,
  4359. byte* data, word16 size, void* heap)
  4360. {
  4361. SessionTicket* ticket = (SessionTicket*)XMALLOC(sizeof(SessionTicket),
  4362. heap, DYNAMIC_TYPE_TLSX);
  4363. if (ticket) {
  4364. ticket->data = (byte*)XMALLOC(size, heap, DYNAMIC_TYPE_TLSX);
  4365. if (ticket->data == NULL) {
  4366. XFREE(ticket, heap, DYNAMIC_TYPE_TLSX);
  4367. return NULL;
  4368. }
  4369. XMEMCPY(ticket->data, data, size);
  4370. ticket->size = size;
  4371. ticket->lifetime = lifetime;
  4372. }
  4373. (void)heap;
  4374. return ticket;
  4375. }
  4376. WOLFSSL_LOCAL void TLSX_SessionTicket_Free(SessionTicket* ticket, void* heap)
  4377. {
  4378. if (ticket) {
  4379. XFREE(ticket->data, heap, DYNAMIC_TYPE_TLSX);
  4380. XFREE(ticket, heap, DYNAMIC_TYPE_TLSX);
  4381. }
  4382. (void)heap;
  4383. }
  4384. int TLSX_UseSessionTicket(TLSX** extensions, SessionTicket* ticket, void* heap)
  4385. {
  4386. int ret = 0;
  4387. if (extensions == NULL)
  4388. return BAD_FUNC_ARG;
  4389. /* If the ticket is NULL, the client will request a new ticket from the
  4390. server. Otherwise, the client will use it in the next client hello. */
  4391. if ((ret = TLSX_Push(extensions, TLSX_SESSION_TICKET, (void*)ticket, heap))
  4392. != 0)
  4393. return ret;
  4394. return WOLFSSL_SUCCESS;
  4395. }
  4396. #define WOLF_STK_VALIDATE_REQUEST TLSX_SessionTicket_ValidateRequest
  4397. #define WOLF_STK_GET_SIZE TLSX_SessionTicket_GetSize
  4398. #define WOLF_STK_WRITE TLSX_SessionTicket_Write
  4399. #define WOLF_STK_PARSE TLSX_SessionTicket_Parse
  4400. #define WOLF_STK_FREE(stk, heap) TLSX_SessionTicket_Free((SessionTicket*)(stk),(heap))
  4401. #else
  4402. #define WOLF_STK_FREE(a, b)
  4403. #define WOLF_STK_VALIDATE_REQUEST(a)
  4404. #define WOLF_STK_GET_SIZE(a, b) 0
  4405. #define WOLF_STK_WRITE(a, b, c) 0
  4406. #define WOLF_STK_PARSE(a, b, c, d) 0
  4407. #endif /* HAVE_SESSION_TICKET */
  4408. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  4409. /******************************************************************************/
  4410. /* Encrypt-then-MAC */
  4411. /******************************************************************************/
  4412. #ifndef WOLFSSL_NO_TLS12
  4413. static int TLSX_EncryptThenMac_Use(WOLFSSL* ssl);
  4414. /**
  4415. * Get the size of the Encrypt-Then-MAC extension.
  4416. *
  4417. * msgType Type of message to put extension into.
  4418. * pSz Size of extension data.
  4419. * return SANITY_MSG_E when the message is not allowed to have extension and
  4420. * 0 otherwise.
  4421. */
  4422. static int TLSX_EncryptThenMac_GetSize(byte msgType, word16* pSz)
  4423. {
  4424. (void)pSz;
  4425. if (msgType != client_hello && msgType != server_hello) {
  4426. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4427. return SANITY_MSG_E;
  4428. }
  4429. /* Empty extension */
  4430. return 0;
  4431. }
  4432. /**
  4433. * Write the Encrypt-Then-MAC extension.
  4434. *
  4435. * data Unused
  4436. * output Extension data buffer. Unused.
  4437. * msgType Type of message to put extension into.
  4438. * pSz Size of extension data.
  4439. * return SANITY_MSG_E when the message is not allowed to have extension and
  4440. * 0 otherwise.
  4441. */
  4442. static int TLSX_EncryptThenMac_Write(void* data, byte* output, byte msgType,
  4443. word16* pSz)
  4444. {
  4445. (void)data;
  4446. (void)output;
  4447. (void)pSz;
  4448. if (msgType != client_hello && msgType != server_hello) {
  4449. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4450. return SANITY_MSG_E;
  4451. }
  4452. /* Empty extension */
  4453. return 0;
  4454. }
  4455. /**
  4456. * Parse the Encrypt-Then-MAC extension.
  4457. *
  4458. * ssl SSL object
  4459. * input Extension data buffer.
  4460. * length Length of this extension's data.
  4461. * msgType Type of message to extension appeared in.
  4462. * return SANITY_MSG_E when the message is not allowed to have extension,
  4463. * BUFFER_ERROR when the extension's data is invalid,
  4464. * MEMORY_E when unable to allocate memory and
  4465. * 0 otherwise.
  4466. */
  4467. static int TLSX_EncryptThenMac_Parse(WOLFSSL* ssl, const byte* input,
  4468. word16 length, byte msgType)
  4469. {
  4470. int ret;
  4471. (void)input;
  4472. if (msgType != client_hello && msgType != server_hello) {
  4473. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4474. return SANITY_MSG_E;
  4475. }
  4476. /* Empty extension */
  4477. if (length != 0)
  4478. return BUFFER_ERROR;
  4479. if (msgType == client_hello) {
  4480. /* Check the user hasn't disallowed use of Encrypt-Then-Mac. */
  4481. if (!ssl->options.disallowEncThenMac) {
  4482. ssl->options.encThenMac = 1;
  4483. /* Set the extension reply. */
  4484. ret = TLSX_EncryptThenMac_Use(ssl);
  4485. if (ret != 0)
  4486. return ret;
  4487. }
  4488. return 0;
  4489. }
  4490. /* Server Hello */
  4491. if (ssl->options.disallowEncThenMac) {
  4492. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4493. return SANITY_MSG_E;
  4494. }
  4495. ssl->options.encThenMac = 1;
  4496. return 0;
  4497. }
  4498. /**
  4499. * Add the Encrypt-Then-MAC extension to list.
  4500. *
  4501. * ssl SSL object
  4502. * return MEMORY_E when unable to allocate memory and 0 otherwise.
  4503. */
  4504. static int TLSX_EncryptThenMac_Use(WOLFSSL* ssl)
  4505. {
  4506. int ret = 0;
  4507. TLSX* extension;
  4508. /* Find the Encrypt-Then-Mac extension if it exists. */
  4509. extension = TLSX_Find(ssl->extensions, TLSX_ENCRYPT_THEN_MAC);
  4510. if (extension == NULL) {
  4511. /* Push new Encrypt-Then-Mac extension. */
  4512. ret = TLSX_Push(&ssl->extensions, TLSX_ENCRYPT_THEN_MAC, NULL,
  4513. ssl->heap);
  4514. if (ret != 0)
  4515. return ret;
  4516. }
  4517. return 0;
  4518. }
  4519. /**
  4520. * Set the Encrypt-Then-MAC extension as one to respond too.
  4521. *
  4522. * ssl SSL object
  4523. * return EXT_MISSING when EncryptThenMac extension not in list.
  4524. */
  4525. int TLSX_EncryptThenMac_Respond(WOLFSSL* ssl)
  4526. {
  4527. TLSX* extension;
  4528. extension = TLSX_Find(ssl->extensions, TLSX_ENCRYPT_THEN_MAC);
  4529. if (extension == NULL)
  4530. return EXT_MISSING;
  4531. extension->resp = 1;
  4532. return 0;
  4533. }
  4534. #define ETM_GET_SIZE TLSX_EncryptThenMac_GetSize
  4535. #define ETM_WRITE TLSX_EncryptThenMac_Write
  4536. #define ETM_PARSE TLSX_EncryptThenMac_Parse
  4537. #else
  4538. #define ETM_GET_SIZE(a, b) 0
  4539. #define ETM_WRITE(a, b, c, d) 0
  4540. #define ETM_PARSE(a, b, c, d) 0
  4541. #endif /* !WOLFSSL_NO_TLS12 */
  4542. #endif /* HAVE_ENCRYPT_THEN_MAC && !WOLFSSL_AEAD_ONLY */
  4543. #ifdef WOLFSSL_SRTP
  4544. /******************************************************************************/
  4545. /* DTLS SRTP (Secure Real-time Transport Protocol) */
  4546. /******************************************************************************/
  4547. /* Only support single SRTP profile */
  4548. typedef struct TlsxSrtp {
  4549. word16 profileCount;
  4550. word16 ids; /* selected bits */
  4551. } TlsxSrtp;
  4552. static int TLSX_UseSRTP_GetSize(TlsxSrtp *srtp)
  4553. {
  4554. /* SRTP Profile Len (2)
  4555. * SRTP Profiles (2)
  4556. * MKI (master key id) Length */
  4557. return (OPAQUE16_LEN + (srtp->profileCount * OPAQUE16_LEN) + 1);
  4558. }
  4559. static TlsxSrtp* TLSX_UseSRTP_New(word16 ids, void* heap)
  4560. {
  4561. TlsxSrtp* srtp;
  4562. int i;
  4563. srtp = (TlsxSrtp*)XMALLOC(sizeof(TlsxSrtp), heap, DYNAMIC_TYPE_TLSX);
  4564. if (srtp == NULL) {
  4565. WOLFSSL_MSG("TLSX SRTP Memory failure");
  4566. return NULL;
  4567. }
  4568. /* count and test each bit set */
  4569. srtp->profileCount = 0;
  4570. for (i=0; i<16; i++) {
  4571. if (ids & (1 << i)) {
  4572. srtp->profileCount++;
  4573. }
  4574. }
  4575. srtp->ids = ids;
  4576. return srtp;
  4577. }
  4578. static void TLSX_UseSRTP_Free(TlsxSrtp *srtp, void* heap)
  4579. {
  4580. if (srtp != NULL) {
  4581. XFREE(srtp, heap, DYNAMIC_TYPE_TLSX);
  4582. }
  4583. (void)heap;
  4584. }
  4585. static int TLSX_UseSRTP_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  4586. byte isRequest)
  4587. {
  4588. int ret = BAD_FUNC_ARG;
  4589. word16 profile_len = 0;
  4590. word16 profile_value = 0;
  4591. word16 offset = 0;
  4592. #ifndef NO_WOLFSSL_SERVER
  4593. int i;
  4594. TlsxSrtp* srtp = NULL;
  4595. #endif
  4596. if (length < OPAQUE16_LEN) {
  4597. return BUFFER_ERROR;
  4598. }
  4599. /* reset selected DTLS SRTP profile ID */
  4600. ssl->dtlsSrtpId = 0;
  4601. /* total length, not include itself */
  4602. ato16(input, &profile_len);
  4603. offset += OPAQUE16_LEN;
  4604. if (!isRequest) {
  4605. #ifndef NO_WOLFSSL_CLIENT
  4606. if (length < offset + OPAQUE16_LEN)
  4607. return BUFFER_ERROR;
  4608. ato16(input + offset, &profile_value);
  4609. /* check that the profile received was in the ones we support */
  4610. if (profile_value < 16 &&
  4611. (ssl->dtlsSrtpProfiles & (1 << profile_value))) {
  4612. ssl->dtlsSrtpId = profile_value;
  4613. ret = 0; /* success */
  4614. }
  4615. #endif
  4616. }
  4617. #ifndef NO_WOLFSSL_SERVER
  4618. else {
  4619. /* parse remainder one profile at a time, looking for match in CTX */
  4620. ret = 0;
  4621. for (i=offset; i<length; i+=OPAQUE16_LEN) {
  4622. ato16(input+i, &profile_value);
  4623. /* find first match */
  4624. if (profile_value < 16 &&
  4625. ssl->dtlsSrtpProfiles & (1 << profile_value)) {
  4626. ssl->dtlsSrtpId = profile_value;
  4627. /* make sure we respond with selected SRTP id selected */
  4628. srtp = TLSX_UseSRTP_New((1 << profile_value), ssl->heap);
  4629. if (srtp != NULL) {
  4630. ret = TLSX_Push(&ssl->extensions, TLSX_USE_SRTP,
  4631. (void*)srtp, ssl->heap);
  4632. if (ret == 0) {
  4633. TLSX_SetResponse(ssl, TLSX_USE_SRTP);
  4634. /* successfully set extension */
  4635. }
  4636. }
  4637. else {
  4638. ret = MEMORY_E;
  4639. }
  4640. break;
  4641. }
  4642. }
  4643. }
  4644. if (ret == 0 && ssl->dtlsSrtpId == 0) {
  4645. WOLFSSL_MSG("TLSX_UseSRTP_Parse profile not found!");
  4646. /* not fatal */
  4647. }
  4648. else if (ret != 0) {
  4649. ssl->dtlsSrtpId = 0;
  4650. TLSX_UseSRTP_Free(srtp, ssl->heap);
  4651. }
  4652. #endif
  4653. (void)profile_len;
  4654. return ret;
  4655. }
  4656. static word16 TLSX_UseSRTP_Write(TlsxSrtp* srtp, byte* output)
  4657. {
  4658. word16 offset = 0;
  4659. int i, j;
  4660. c16toa(srtp->profileCount*2, output+offset);
  4661. offset += OPAQUE16_LEN;
  4662. for (i=0; i< srtp->profileCount; i+=2) {
  4663. for (j=0; j<16; j++) {
  4664. if (srtp->ids & (1 << j)) {
  4665. c16toa(j, output+offset);
  4666. offset += OPAQUE16_LEN;
  4667. }
  4668. }
  4669. }
  4670. output[offset++] = 0x00; /* MKI Length */
  4671. return offset;
  4672. }
  4673. static int TLSX_UseSRTP(TLSX** extensions, word16 profiles, void* heap)
  4674. {
  4675. int ret = 0;
  4676. TLSX* extension;
  4677. if (extensions == NULL) {
  4678. return BAD_FUNC_ARG;
  4679. }
  4680. extension = TLSX_Find(*extensions, TLSX_USE_SRTP);
  4681. if (extension == NULL) {
  4682. TlsxSrtp* srtp = TLSX_UseSRTP_New(profiles, heap);
  4683. if (srtp == NULL) {
  4684. return MEMORY_E;
  4685. }
  4686. ret = TLSX_Push(extensions, TLSX_USE_SRTP, (void*)srtp, heap);
  4687. if (ret != 0) {
  4688. TLSX_UseSRTP_Free(srtp, heap);
  4689. }
  4690. }
  4691. return ret;
  4692. }
  4693. #ifndef NO_WOLFSSL_SERVER
  4694. #define SRTP_FREE TLSX_UseSRTP_Free
  4695. #define SRTP_PARSE TLSX_UseSRTP_Parse
  4696. #define SRTP_WRITE TLSX_UseSRTP_Write
  4697. #define SRTP_GET_SIZE TLSX_UseSRTP_GetSize
  4698. #else
  4699. #define SRTP_FREE(a, b)
  4700. #define SRTP_PARSE(a, b, c, d) 0
  4701. #define SRTP_WRITE(a, b) 0
  4702. #define SRTP_GET_SIZE(a) 0
  4703. #endif
  4704. #endif /* WOLFSSL_SRTP */
  4705. /******************************************************************************/
  4706. /* Supported Versions */
  4707. /******************************************************************************/
  4708. #ifdef WOLFSSL_TLS13
  4709. static WC_INLINE int versionIsGreater(byte isDtls, byte a, byte b)
  4710. {
  4711. (void)isDtls;
  4712. #ifdef WOLFSSL_DTLS
  4713. /* DTLS version increases backwards (-1,-2,-3,etc) */
  4714. if (isDtls)
  4715. return a < b;
  4716. #endif /* WOLFSSL_DTLS */
  4717. return a > b;
  4718. }
  4719. static WC_INLINE int versionIsLesser(byte isDtls, byte a, byte b)
  4720. {
  4721. (void)isDtls;
  4722. #ifdef WOLFSSL_DTLS
  4723. /* DTLS version increases backwards (-1,-2,-3,etc) */
  4724. if (isDtls)
  4725. return a > b;
  4726. #endif /* WOLFSSL_DTLS */
  4727. return a < b;
  4728. }
  4729. static WC_INLINE int versionIsAtLeast(byte isDtls, byte a, byte b)
  4730. {
  4731. (void)isDtls;
  4732. #ifdef WOLFSSL_DTLS
  4733. /* DTLS version increases backwards (-1,-2,-3,etc) */
  4734. if (isDtls)
  4735. return a <= b;
  4736. #endif /* WOLFSSL_DTLS */
  4737. return a >= b;
  4738. }
  4739. static WC_INLINE int versionIsLessEqual(byte isDtls, byte a, byte b)
  4740. {
  4741. (void)isDtls;
  4742. #ifdef WOLFSSL_DTLS
  4743. /* DTLS version increases backwards (-1,-2,-3,etc) */
  4744. if (isDtls)
  4745. return a >= b;
  4746. #endif /* WOLFSSL_DTLS */
  4747. return a <= b;
  4748. }
  4749. /* Return the size of the SupportedVersions extension's data.
  4750. *
  4751. * data The SSL/TLS object.
  4752. * msgType The type of the message this extension is being written into.
  4753. * returns the length of data that will be in the extension.
  4754. */
  4755. static int TLSX_SupportedVersions_GetSize(void* data, byte msgType, word16* pSz)
  4756. {
  4757. WOLFSSL* ssl = (WOLFSSL*)data;
  4758. byte tls13Minor, tls12Minor, tls11Minor, isDtls;
  4759. isDtls = !!ssl->options.dtls;
  4760. tls13Minor = (byte)(isDtls ? DTLSv1_3_MINOR : TLSv1_3_MINOR);
  4761. tls12Minor = (byte)(isDtls ? DTLSv1_2_MINOR : TLSv1_2_MINOR);
  4762. tls11Minor = (byte)(isDtls ? DTLS_MINOR : TLSv1_1_MINOR);
  4763. /* unused on some configuration */
  4764. (void)tls12Minor;
  4765. (void)tls13Minor;
  4766. (void)tls11Minor;
  4767. if (msgType == client_hello) {
  4768. /* TLS v1.2 and TLS v1.3 */
  4769. int cnt = 0;
  4770. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls13Minor)
  4771. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4772. defined(WOLFSSL_WPAS_SMALL)
  4773. && (ssl->options.mask & SSL_OP_NO_TLSv1_3) == 0
  4774. #endif
  4775. ) {
  4776. cnt++;
  4777. }
  4778. if (ssl->options.downgrade) {
  4779. #ifndef WOLFSSL_NO_TLS12
  4780. if (versionIsLessEqual(
  4781. isDtls, ssl->options.minDowngrade, tls12Minor)
  4782. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4783. defined(WOLFSSL_WPAS_SMALL)
  4784. && (ssl->options.mask & SSL_OP_NO_TLSv1_2) == 0
  4785. #endif
  4786. ) {
  4787. cnt++;
  4788. }
  4789. #endif
  4790. #ifndef NO_OLD_TLS
  4791. if (versionIsLessEqual(
  4792. isDtls, ssl->options.minDowngrade, tls11Minor)
  4793. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4794. defined(WOLFSSL_WPAS_SMALL)
  4795. && (ssl->options.mask & SSL_OP_NO_TLSv1_1) == 0
  4796. #endif
  4797. ) {
  4798. cnt++;
  4799. }
  4800. #ifdef WOLFSSL_ALLOW_TLSV10
  4801. if (!ssl->options.dtls && (ssl->options.minDowngrade <= TLSv1_MINOR)
  4802. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4803. defined(WOLFSSL_WPAS_SMALL)
  4804. && (ssl->options.mask & SSL_OP_NO_TLSv1) == 0
  4805. #endif
  4806. ) {
  4807. cnt++;
  4808. }
  4809. #endif
  4810. #endif
  4811. }
  4812. *pSz += (word16)(OPAQUE8_LEN + cnt * OPAQUE16_LEN);
  4813. }
  4814. else if (msgType == server_hello || msgType == hello_retry_request) {
  4815. *pSz += OPAQUE16_LEN;
  4816. }
  4817. else {
  4818. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4819. return SANITY_MSG_E;
  4820. }
  4821. return 0;
  4822. }
  4823. /* Writes the SupportedVersions extension into the buffer.
  4824. *
  4825. * data The SSL/TLS object.
  4826. * output The buffer to write the extension into.
  4827. * msgType The type of the message this extension is being written into.
  4828. * returns the length of data that was written.
  4829. */
  4830. static int TLSX_SupportedVersions_Write(void* data, byte* output,
  4831. byte msgType, word16* pSz)
  4832. {
  4833. WOLFSSL* ssl = (WOLFSSL*)data;
  4834. byte major;
  4835. byte* cnt;
  4836. byte tls13minor, tls12minor, tls11minor, isDtls = 0;
  4837. tls13minor = (byte)TLSv1_3_MINOR;
  4838. tls12minor = (byte)TLSv1_2_MINOR;
  4839. tls11minor = (byte)TLSv1_1_MINOR;
  4840. /* unused in some configuration */
  4841. (void)tls11minor;
  4842. (void)tls12minor;
  4843. #ifdef WOLFSSL_DTLS13
  4844. if (ssl->options.dtls) {
  4845. tls13minor = (byte)DTLSv1_3_MINOR;
  4846. tls12minor = (byte)DTLSv1_2_MINOR;
  4847. tls11minor = (byte)DTLS_MINOR;
  4848. isDtls = 1;
  4849. }
  4850. #endif /* WOLFSSL_DTLS13 */
  4851. if (msgType == client_hello) {
  4852. major = ssl->ctx->method->version.major;
  4853. cnt = output++;
  4854. *cnt = 0;
  4855. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls13minor)
  4856. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4857. defined(WOLFSSL_WPAS_SMALL)
  4858. && (ssl->options.mask & SSL_OP_NO_TLSv1_3) == 0
  4859. #endif
  4860. ) {
  4861. *cnt += OPAQUE16_LEN;
  4862. #ifdef WOLFSSL_TLS13_DRAFT
  4863. /* The TLS draft major number. */
  4864. *(output++) = TLS_DRAFT_MAJOR;
  4865. /* Version of draft supported. */
  4866. *(output++) = TLS_DRAFT_MINOR;
  4867. #else
  4868. *(output++) = major;
  4869. *(output++) = tls13minor;
  4870. #endif
  4871. }
  4872. if (ssl->options.downgrade) {
  4873. #ifndef WOLFSSL_NO_TLS12
  4874. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls12minor)
  4875. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4876. defined(WOLFSSL_WPAS_SMALL)
  4877. && (ssl->options.mask & SSL_OP_NO_TLSv1_2) == 0
  4878. #endif
  4879. ) {
  4880. *cnt += OPAQUE16_LEN;
  4881. *(output++) = major;
  4882. *(output++) = tls12minor;
  4883. }
  4884. #endif
  4885. #ifndef NO_OLD_TLS
  4886. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls11minor)
  4887. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4888. defined(WOLFSSL_WPAS_SMALL)
  4889. && (ssl->options.mask & SSL_OP_NO_TLSv1_1) == 0
  4890. #endif
  4891. ) {
  4892. *cnt += OPAQUE16_LEN;
  4893. *(output++) = major;
  4894. *(output++) = tls11minor;
  4895. }
  4896. #ifdef WOLFSSL_ALLOW_TLSV10
  4897. if (!ssl->options.dtls && (ssl->options.minDowngrade <= TLSv1_MINOR)
  4898. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4899. defined(WOLFSSL_WPAS_SMALL)
  4900. && (ssl->options.mask & SSL_OP_NO_TLSv1) == 0
  4901. #endif
  4902. ) {
  4903. *cnt += OPAQUE16_LEN;
  4904. *(output++) = major;
  4905. *(output++) = (byte)TLSv1_MINOR;
  4906. }
  4907. #endif
  4908. #endif
  4909. }
  4910. *pSz += (word16)(OPAQUE8_LEN + *cnt);
  4911. }
  4912. else if (msgType == server_hello || msgType == hello_retry_request) {
  4913. output[0] = ssl->version.major;
  4914. output[1] = ssl->version.minor;
  4915. *pSz += OPAQUE16_LEN;
  4916. }
  4917. else {
  4918. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4919. return SANITY_MSG_E;
  4920. }
  4921. return 0;
  4922. }
  4923. /* Parse the SupportedVersions extension.
  4924. *
  4925. * ssl The SSL/TLS object.
  4926. * input The buffer with the extension data.
  4927. * length The length of the extension data.
  4928. * msgType The type of the message this extension is being parsed from.
  4929. * returns 0 on success, otherwise failure.
  4930. */
  4931. static int TLSX_SupportedVersions_Parse(WOLFSSL* ssl, const byte* input,
  4932. word16 length, byte msgType)
  4933. {
  4934. ProtocolVersion pv = ssl->ctx->method->version;
  4935. int i;
  4936. int len;
  4937. int newMinor = 0;
  4938. int set = 0;
  4939. int ret;
  4940. byte major, minor;
  4941. byte tls13minor, tls12minor;
  4942. byte isDtls;
  4943. tls13minor = TLSv1_3_MINOR;
  4944. tls12minor = TLSv1_2_MINOR;
  4945. isDtls = ssl->options.dtls == 1;
  4946. #ifdef WOLFSSL_DTLS13
  4947. if (ssl->options.dtls) {
  4948. tls13minor = DTLSv1_3_MINOR;
  4949. tls12minor = DTLSv1_2_MINOR;
  4950. }
  4951. #endif /* WOLFSSL_DTLS13 */
  4952. if (msgType == client_hello) {
  4953. /* Must contain a length and at least one version. */
  4954. if (length < OPAQUE8_LEN + OPAQUE16_LEN || (length & 1) != 1)
  4955. return BUFFER_ERROR;
  4956. len = *input;
  4957. /* Protocol version array must fill rest of data. */
  4958. if (length != (word16)OPAQUE8_LEN + len)
  4959. return BUFFER_ERROR;
  4960. input++;
  4961. /* Find first match. */
  4962. for (i = 0; i < len; i += OPAQUE16_LEN) {
  4963. major = input[i];
  4964. minor = input[i + OPAQUE8_LEN];
  4965. #ifdef WOLFSSL_TLS13_DRAFT
  4966. if (major == TLS_DRAFT_MAJOR && minor == TLS_DRAFT_MINOR) {
  4967. major = SSLv3_MAJOR;
  4968. minor = TLSv1_3_MINOR;
  4969. }
  4970. #else
  4971. if (major == TLS_DRAFT_MAJOR)
  4972. continue;
  4973. #endif
  4974. if (major != pv.major)
  4975. continue;
  4976. /* No upgrade allowed. */
  4977. if (versionIsGreater(isDtls, minor, ssl->version.minor))
  4978. continue;
  4979. /* Check downgrade. */
  4980. if (versionIsLesser(isDtls, minor, ssl->version.minor)) {
  4981. if (!ssl->options.downgrade)
  4982. continue;
  4983. if (versionIsLesser(
  4984. isDtls, minor, ssl->options.minDowngrade))
  4985. continue;
  4986. if (newMinor == 0 &&
  4987. versionIsGreater(
  4988. isDtls, minor, ssl->options.oldMinor)) {
  4989. /* Downgrade the version. */
  4990. ssl->version.minor = minor;
  4991. }
  4992. }
  4993. if (versionIsAtLeast(isDtls, minor, tls13minor)) {
  4994. ssl->options.tls1_3 = 1;
  4995. /* TLS v1.3 requires supported version extension */
  4996. if (TLSX_Find(ssl->extensions, TLSX_SUPPORTED_VERSIONS) == NULL) {
  4997. ret = TLSX_Prepend(&ssl->extensions,
  4998. TLSX_SUPPORTED_VERSIONS, ssl, ssl->heap);
  4999. if (ret != 0) {
  5000. return ret;
  5001. }
  5002. TLSX_SetResponse(ssl, TLSX_SUPPORTED_VERSIONS);
  5003. }
  5004. if (versionIsGreater(isDtls, minor, newMinor)) {
  5005. ssl->version.minor = minor;
  5006. newMinor = minor;
  5007. }
  5008. }
  5009. else if (versionIsGreater(
  5010. isDtls, minor, ssl->options.oldMinor))
  5011. ssl->options.oldMinor = minor;
  5012. set = 1;
  5013. }
  5014. if (!set) {
  5015. #ifdef WOLFSSL_MYSQL_COMPATIBLE
  5016. SendAlert(ssl, alert_fatal, wc_protocol_version);
  5017. #else
  5018. SendAlert(ssl, alert_fatal, protocol_version);
  5019. #endif
  5020. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5021. return VERSION_ERROR;
  5022. }
  5023. }
  5024. else if (msgType == server_hello || msgType == hello_retry_request) {
  5025. /* Must contain one version. */
  5026. if (length != OPAQUE16_LEN)
  5027. return BUFFER_ERROR;
  5028. major = input[0];
  5029. minor = input[OPAQUE8_LEN];
  5030. if (major != pv.major) {
  5031. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5032. return VERSION_ERROR;
  5033. }
  5034. /* Can't downgrade with this extension below TLS v1.3. */
  5035. if (versionIsLesser(isDtls, minor, tls13minor)) {
  5036. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5037. return VERSION_ERROR;
  5038. }
  5039. /* Version is TLS v1.2 to handle downgrading from TLS v1.3+. */
  5040. if (ssl->options.downgrade && ssl->version.minor == tls12minor) {
  5041. /* Set minor version back to TLS v1.3+ */
  5042. ssl->version.minor = ssl->ctx->method->version.minor;
  5043. }
  5044. /* No upgrade allowed. */
  5045. if (versionIsLesser(isDtls, ssl->version.minor, minor)) {
  5046. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5047. return VERSION_ERROR;
  5048. }
  5049. /* Check downgrade. */
  5050. if (versionIsGreater(isDtls, ssl->version.minor, minor)) {
  5051. if (!ssl->options.downgrade) {
  5052. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5053. return VERSION_ERROR;
  5054. }
  5055. if (versionIsLesser(
  5056. isDtls, minor, ssl->options.minDowngrade)) {
  5057. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5058. return VERSION_ERROR;
  5059. }
  5060. /* Downgrade the version. */
  5061. ssl->version.minor = minor;
  5062. }
  5063. }
  5064. else {
  5065. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5066. return SANITY_MSG_E;
  5067. }
  5068. return 0;
  5069. }
  5070. /* Sets a new SupportedVersions extension into the extension list.
  5071. *
  5072. * extensions The list of extensions.
  5073. * data The extensions specific data.
  5074. * heap The heap used for allocation.
  5075. * returns 0 on success, otherwise failure.
  5076. */
  5077. static int TLSX_SetSupportedVersions(TLSX** extensions, const void* data,
  5078. void* heap)
  5079. {
  5080. if (extensions == NULL || data == NULL)
  5081. return BAD_FUNC_ARG;
  5082. return TLSX_Push(extensions, TLSX_SUPPORTED_VERSIONS, data, heap);
  5083. }
  5084. #define SV_GET_SIZE TLSX_SupportedVersions_GetSize
  5085. #define SV_WRITE TLSX_SupportedVersions_Write
  5086. #define SV_PARSE TLSX_SupportedVersions_Parse
  5087. #else
  5088. #define SV_GET_SIZE(a, b, c) 0
  5089. #define SV_WRITE(a, b, c, d) 0
  5090. #define SV_PARSE(a, b, c, d) 0
  5091. #endif /* WOLFSSL_TLS13 */
  5092. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  5093. /******************************************************************************/
  5094. /* Cookie */
  5095. /******************************************************************************/
  5096. /* Free the cookie data.
  5097. *
  5098. * cookie Cookie data.
  5099. * heap The heap used for allocation.
  5100. */
  5101. static void TLSX_Cookie_FreeAll(Cookie* cookie, void* heap)
  5102. {
  5103. (void)heap;
  5104. if (cookie != NULL)
  5105. XFREE(cookie, heap, DYNAMIC_TYPE_TLSX);
  5106. }
  5107. /* Get the size of the encoded Cookie extension.
  5108. * In messages: ClientHello and HelloRetryRequest.
  5109. *
  5110. * cookie The cookie to write.
  5111. * msgType The type of the message this extension is being written into.
  5112. * returns the number of bytes of the encoded Cookie extension.
  5113. */
  5114. static int TLSX_Cookie_GetSize(Cookie* cookie, byte msgType, word16* pSz)
  5115. {
  5116. if (msgType == client_hello || msgType == hello_retry_request) {
  5117. *pSz += OPAQUE16_LEN + cookie->len;
  5118. }
  5119. else {
  5120. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5121. return SANITY_MSG_E;
  5122. }
  5123. return 0;
  5124. }
  5125. /* Writes the Cookie extension into the output buffer.
  5126. * Assumes that the the output buffer is big enough to hold data.
  5127. * In messages: ClientHello and HelloRetryRequest.
  5128. *
  5129. * cookie The cookie to write.
  5130. * output The buffer to write into.
  5131. * msgType The type of the message this extension is being written into.
  5132. * returns the number of bytes written into the buffer.
  5133. */
  5134. static int TLSX_Cookie_Write(Cookie* cookie, byte* output, byte msgType,
  5135. word16* pSz)
  5136. {
  5137. if (msgType == client_hello || msgType == hello_retry_request) {
  5138. c16toa(cookie->len, output);
  5139. output += OPAQUE16_LEN;
  5140. XMEMCPY(output, &cookie->data, cookie->len);
  5141. *pSz += OPAQUE16_LEN + cookie->len;
  5142. }
  5143. else {
  5144. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5145. return SANITY_MSG_E;
  5146. }
  5147. return 0;
  5148. }
  5149. /* Parse the Cookie extension.
  5150. * In messages: ClientHello and HelloRetryRequest.
  5151. *
  5152. * ssl The SSL/TLS object.
  5153. * input The extension data.
  5154. * length The length of the extension data.
  5155. * msgType The type of the message this extension is being parsed from.
  5156. * returns 0 on success and other values indicate failure.
  5157. */
  5158. static int TLSX_Cookie_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  5159. byte msgType)
  5160. {
  5161. word16 len;
  5162. word16 idx = 0;
  5163. TLSX* extension;
  5164. Cookie* cookie;
  5165. if (msgType != client_hello && msgType != hello_retry_request) {
  5166. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5167. return SANITY_MSG_E;
  5168. }
  5169. /* Message contains length and Cookie which must be at least one byte
  5170. * in length.
  5171. */
  5172. if (length < OPAQUE16_LEN + 1)
  5173. return BUFFER_E;
  5174. ato16(input + idx, &len);
  5175. idx += OPAQUE16_LEN;
  5176. if (length - idx != len)
  5177. return BUFFER_E;
  5178. if (msgType == hello_retry_request)
  5179. return TLSX_Cookie_Use(ssl, input + idx, len, NULL, 0, 0);
  5180. /* client_hello */
  5181. extension = TLSX_Find(ssl->extensions, TLSX_COOKIE);
  5182. if (extension == NULL) {
  5183. #ifdef WOLFSSL_DTLS13
  5184. if (ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version))
  5185. /* Allow a cookie extension with DTLS 1.3 because it is possible
  5186. * that a different SSL instance sent the cookie but we are now
  5187. * receiving it. */
  5188. return TLSX_Cookie_Use(ssl, input + idx, len, NULL, 0, 0);
  5189. else
  5190. #endif
  5191. {
  5192. WOLFSSL_ERROR_VERBOSE(HRR_COOKIE_ERROR);
  5193. return HRR_COOKIE_ERROR;
  5194. }
  5195. }
  5196. cookie = (Cookie*)extension->data;
  5197. if (cookie->len != len || XMEMCMP(&cookie->data, input + idx, len) != 0) {
  5198. WOLFSSL_ERROR_VERBOSE(HRR_COOKIE_ERROR);
  5199. return HRR_COOKIE_ERROR;
  5200. }
  5201. /* Request seen. */
  5202. extension->resp = 0;
  5203. return 0;
  5204. }
  5205. /* Use the data to create a new Cookie object in the extensions.
  5206. *
  5207. * ssl SSL/TLS object.
  5208. * data Cookie data.
  5209. * len Length of cookie data in bytes.
  5210. * mac MAC data.
  5211. * macSz Length of MAC data in bytes.
  5212. * resp Indicates the extension will go into a response (HelloRetryRequest).
  5213. * returns 0 on success and other values indicate failure.
  5214. */
  5215. int TLSX_Cookie_Use(WOLFSSL* ssl, const byte* data, word16 len, byte* mac,
  5216. byte macSz, int resp)
  5217. {
  5218. int ret = 0;
  5219. TLSX* extension;
  5220. Cookie* cookie;
  5221. /* Find the cookie extension if it exists. */
  5222. extension = TLSX_Find(ssl->extensions, TLSX_COOKIE);
  5223. if (extension == NULL) {
  5224. /* Push new cookie extension. */
  5225. ret = TLSX_Push(&ssl->extensions, TLSX_COOKIE, NULL, ssl->heap);
  5226. if (ret != 0)
  5227. return ret;
  5228. extension = TLSX_Find(ssl->extensions, TLSX_COOKIE);
  5229. if (extension == NULL)
  5230. return MEMORY_E;
  5231. }
  5232. /* The Cookie structure has one byte for cookie data already. */
  5233. cookie = (Cookie*)XMALLOC(sizeof(Cookie) + len + macSz - 1, ssl->heap,
  5234. DYNAMIC_TYPE_TLSX);
  5235. if (cookie == NULL)
  5236. return MEMORY_E;
  5237. cookie->len = len + macSz;
  5238. XMEMCPY(&cookie->data, data, len);
  5239. if (mac != NULL)
  5240. XMEMCPY(&cookie->data + len, mac, macSz);
  5241. if (extension->data != NULL)
  5242. XFREE(extension->data, ssl->heap, DYNAMIC_TYPE_TLSX);
  5243. extension->data = (void*)cookie;
  5244. extension->resp = (byte)resp;
  5245. return 0;
  5246. }
  5247. #define CKE_FREE_ALL TLSX_Cookie_FreeAll
  5248. #define CKE_GET_SIZE TLSX_Cookie_GetSize
  5249. #define CKE_WRITE TLSX_Cookie_Write
  5250. #define CKE_PARSE TLSX_Cookie_Parse
  5251. #else
  5252. #define CKE_FREE_ALL(a, b) 0
  5253. #define CKE_GET_SIZE(a, b, c) 0
  5254. #define CKE_WRITE(a, b, c, d) 0
  5255. #define CKE_PARSE(a, b, c, d) 0
  5256. #endif
  5257. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  5258. /******************************************************************************/
  5259. /* Signature Algorithms */
  5260. /******************************************************************************/
  5261. /* Return the size of the SignatureAlgorithms extension's data.
  5262. *
  5263. * data Unused
  5264. * returns the length of data that will be in the extension.
  5265. */
  5266. static word16 TLSX_SignatureAlgorithms_GetSize(void* data)
  5267. {
  5268. WOLFSSL* ssl = (WOLFSSL*)data;
  5269. return OPAQUE16_LEN + ssl->suites->hashSigAlgoSz;
  5270. }
  5271. /* Creates a bit string of supported hash algorithms with RSA PSS.
  5272. * The bit string is used when determining which signature algorithm to use
  5273. * when creating the CertificateVerify message.
  5274. * Note: Valid data has an even length as each signature algorithm is two bytes.
  5275. *
  5276. * ssl The SSL/TLS object.
  5277. * input The buffer with the list of supported signature algorithms.
  5278. * length The length of the list in bytes.
  5279. * returns 0 on success, BUFFER_ERROR when the length is not even.
  5280. */
  5281. static int TLSX_SignatureAlgorithms_MapPss(WOLFSSL *ssl, const byte* input,
  5282. word16 length)
  5283. {
  5284. word16 i;
  5285. if ((length & 1) == 1)
  5286. return BUFFER_ERROR;
  5287. ssl->pssAlgo = 0;
  5288. for (i = 0; i < length; i += 2) {
  5289. if (input[i] == rsa_pss_sa_algo && input[i + 1] <= sha512_mac)
  5290. ssl->pssAlgo |= 1 << input[i + 1];
  5291. #ifdef WOLFSSL_TLS13
  5292. if (input[i] == rsa_pss_sa_algo && input[i + 1] >= pss_sha256 &&
  5293. input[i + 1] <= pss_sha512) {
  5294. ssl->pssAlgo |= 1 << input[i + 1];
  5295. }
  5296. #endif
  5297. }
  5298. return 0;
  5299. }
  5300. /* Writes the SignatureAlgorithms extension into the buffer.
  5301. *
  5302. * data Unused
  5303. * output The buffer to write the extension into.
  5304. * returns the length of data that was written.
  5305. */
  5306. static word16 TLSX_SignatureAlgorithms_Write(void* data, byte* output)
  5307. {
  5308. WOLFSSL* ssl = (WOLFSSL*)data;
  5309. c16toa(ssl->suites->hashSigAlgoSz, output);
  5310. XMEMCPY(output + OPAQUE16_LEN, ssl->suites->hashSigAlgo,
  5311. ssl->suites->hashSigAlgoSz);
  5312. TLSX_SignatureAlgorithms_MapPss(ssl, output + OPAQUE16_LEN,
  5313. ssl->suites->hashSigAlgoSz);
  5314. return OPAQUE16_LEN + ssl->suites->hashSigAlgoSz;
  5315. }
  5316. /* Parse the SignatureAlgorithms extension.
  5317. *
  5318. * ssl The SSL/TLS object.
  5319. * input The buffer with the extension data.
  5320. * length The length of the extension data.
  5321. * returns 0 on success, otherwise failure.
  5322. */
  5323. static int TLSX_SignatureAlgorithms_Parse(WOLFSSL *ssl, const byte* input,
  5324. word16 length, byte isRequest, Suites* suites)
  5325. {
  5326. word16 len;
  5327. if (!isRequest)
  5328. return BUFFER_ERROR;
  5329. /* Must contain a length and at least algorithm. */
  5330. if (length < OPAQUE16_LEN + OPAQUE16_LEN || (length & 1) != 0)
  5331. return BUFFER_ERROR;
  5332. ato16(input, &len);
  5333. input += OPAQUE16_LEN;
  5334. /* Algorithm array must fill rest of data. */
  5335. if (length != OPAQUE16_LEN + len)
  5336. return BUFFER_ERROR;
  5337. /* Sig Algo list size must be even. */
  5338. if (suites->hashSigAlgoSz % 2 != 0)
  5339. return BUFFER_ERROR;
  5340. /* truncate hashSigAlgo list if too long */
  5341. suites->hashSigAlgoSz = len;
  5342. if (suites->hashSigAlgoSz > WOLFSSL_MAX_SIGALGO) {
  5343. WOLFSSL_MSG("TLSX SigAlgo list exceeds max, truncating");
  5344. suites->hashSigAlgoSz = WOLFSSL_MAX_SIGALGO;
  5345. }
  5346. XMEMCPY(suites->hashSigAlgo, input, suites->hashSigAlgoSz);
  5347. return TLSX_SignatureAlgorithms_MapPss(ssl, input, len);
  5348. }
  5349. /* Sets a new SignatureAlgorithms extension into the extension list.
  5350. *
  5351. * extensions The list of extensions.
  5352. * data The extensions specific data.
  5353. * heap The heap used for allocation.
  5354. * returns 0 on success, otherwise failure.
  5355. */
  5356. static int TLSX_SetSignatureAlgorithms(TLSX** extensions, const void* data,
  5357. void* heap)
  5358. {
  5359. if (extensions == NULL)
  5360. return BAD_FUNC_ARG;
  5361. return TLSX_Push(extensions, TLSX_SIGNATURE_ALGORITHMS, data, heap);
  5362. }
  5363. #define SA_GET_SIZE TLSX_SignatureAlgorithms_GetSize
  5364. #define SA_WRITE TLSX_SignatureAlgorithms_Write
  5365. #define SA_PARSE TLSX_SignatureAlgorithms_Parse
  5366. #endif
  5367. /******************************************************************************/
  5368. /* Signature Algorithms Certificate */
  5369. /******************************************************************************/
  5370. #if defined(WOLFSSL_TLS13) && !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  5371. /* Return the size of the SignatureAlgorithms extension's data.
  5372. *
  5373. * data Unused
  5374. * returns the length of data that will be in the extension.
  5375. */
  5376. static word16 TLSX_SignatureAlgorithmsCert_GetSize(void* data)
  5377. {
  5378. WOLFSSL* ssl = (WOLFSSL*)data;
  5379. return OPAQUE16_LEN + ssl->certHashSigAlgoSz;
  5380. }
  5381. /* Writes the SignatureAlgorithmsCert extension into the buffer.
  5382. *
  5383. * data Unused
  5384. * output The buffer to write the extension into.
  5385. * returns the length of data that was written.
  5386. */
  5387. static word16 TLSX_SignatureAlgorithmsCert_Write(void* data, byte* output)
  5388. {
  5389. WOLFSSL* ssl = (WOLFSSL*)data;
  5390. c16toa(ssl->certHashSigAlgoSz, output);
  5391. XMEMCPY(output + OPAQUE16_LEN, ssl->certHashSigAlgo,
  5392. ssl->certHashSigAlgoSz);
  5393. return OPAQUE16_LEN + ssl->certHashSigAlgoSz;
  5394. }
  5395. /* Parse the SignatureAlgorithmsCert extension.
  5396. *
  5397. * ssl The SSL/TLS object.
  5398. * input The buffer with the extension data.
  5399. * length The length of the extension data.
  5400. * returns 0 on success, otherwise failure.
  5401. */
  5402. static int TLSX_SignatureAlgorithmsCert_Parse(WOLFSSL *ssl, const byte* input,
  5403. word16 length, byte isRequest)
  5404. {
  5405. word16 len;
  5406. if (!isRequest)
  5407. return BUFFER_ERROR;
  5408. /* Must contain a length and at least algorithm. */
  5409. if (length < OPAQUE16_LEN + OPAQUE16_LEN || (length & 1) != 0)
  5410. return BUFFER_ERROR;
  5411. ato16(input, &len);
  5412. input += OPAQUE16_LEN;
  5413. /* Algorithm array must fill rest of data. */
  5414. if (length != OPAQUE16_LEN + len)
  5415. return BUFFER_ERROR;
  5416. /* truncate hashSigAlgo list if too long */
  5417. ssl->certHashSigAlgoSz = len;
  5418. if (ssl->certHashSigAlgoSz > WOLFSSL_MAX_SIGALGO) {
  5419. WOLFSSL_MSG("TLSX SigAlgo list exceeds max, truncating");
  5420. ssl->certHashSigAlgoSz = WOLFSSL_MAX_SIGALGO;
  5421. }
  5422. XMEMCPY(ssl->certHashSigAlgo, input, ssl->certHashSigAlgoSz);
  5423. return 0;
  5424. }
  5425. /* Sets a new SignatureAlgorithmsCert extension into the extension list.
  5426. *
  5427. * extensions The list of extensions.
  5428. * data The extensions specific data.
  5429. * heap The heap used for allocation.
  5430. * returns 0 on success, otherwise failure.
  5431. */
  5432. static int TLSX_SetSignatureAlgorithmsCert(TLSX** extensions, const void* data,
  5433. void* heap)
  5434. {
  5435. if (extensions == NULL)
  5436. return BAD_FUNC_ARG;
  5437. return TLSX_Push(extensions, TLSX_SIGNATURE_ALGORITHMS_CERT, data, heap);
  5438. }
  5439. #define SAC_GET_SIZE TLSX_SignatureAlgorithmsCert_GetSize
  5440. #define SAC_WRITE TLSX_SignatureAlgorithmsCert_Write
  5441. #define SAC_PARSE TLSX_SignatureAlgorithmsCert_Parse
  5442. #endif /* WOLFSSL_TLS13 */
  5443. /******************************************************************************/
  5444. /* Key Share */
  5445. /******************************************************************************/
  5446. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  5447. /* Create a key share entry using named Diffie-Hellman parameters group.
  5448. * Generates a key pair.
  5449. *
  5450. * ssl The SSL/TLS object.
  5451. * kse The key share entry object.
  5452. * returns 0 on success, otherwise failure.
  5453. */
  5454. static int TLSX_KeyShare_GenDhKey(WOLFSSL *ssl, KeyShareEntry* kse)
  5455. {
  5456. int ret = 0;
  5457. #if !defined(NO_DH) && (!defined(NO_CERTS) || !defined(NO_PSK))
  5458. word32 pSz = 0, pvtSz = 0;
  5459. DhKey* dhKey = (DhKey*)kse->key;
  5460. /* Pick the parameters from the named group. */
  5461. #ifdef HAVE_PUBLIC_FFDHE
  5462. const DhParams* params = NULL;
  5463. switch (kse->group) {
  5464. #ifdef HAVE_FFDHE_2048
  5465. case WOLFSSL_FFDHE_2048:
  5466. params = wc_Dh_ffdhe2048_Get();
  5467. kse->keyLen = 29;
  5468. break;
  5469. #endif
  5470. #ifdef HAVE_FFDHE_3072
  5471. case WOLFSSL_FFDHE_3072:
  5472. params = wc_Dh_ffdhe3072_Get();
  5473. kse->keyLen = 34;
  5474. break;
  5475. #endif
  5476. #ifdef HAVE_FFDHE_4096
  5477. case WOLFSSL_FFDHE_4096:
  5478. params = wc_Dh_ffdhe4096_Get();
  5479. kse->keyLen = 39;
  5480. break;
  5481. #endif
  5482. #ifdef HAVE_FFDHE_6144
  5483. case WOLFSSL_FFDHE_6144:
  5484. params = wc_Dh_ffdhe6144_Get();
  5485. kse->keyLen = 46;
  5486. break;
  5487. #endif
  5488. #ifdef HAVE_FFDHE_8192
  5489. case WOLFSSL_FFDHE_8192:
  5490. params = wc_Dh_ffdhe8192_Get();
  5491. kse->keyLen = 52;
  5492. break;
  5493. #endif
  5494. default:
  5495. break;
  5496. }
  5497. if (params == NULL)
  5498. return BAD_FUNC_ARG;
  5499. pSz = params->p_len;
  5500. pvtSz = kse->keyLen;
  5501. #else
  5502. kse->keyLen = wc_DhGetNamedKeyMinSize(kse->group);
  5503. if (kse->keyLen == 0) {
  5504. return BAD_FUNC_ARG;
  5505. }
  5506. ret = wc_DhGetNamedKeyParamSize(kse->group, &pSz, NULL, NULL);
  5507. if (ret != 0) {
  5508. return BAD_FUNC_ARG;
  5509. }
  5510. pvtSz = kse->keyLen;
  5511. #endif
  5512. kse->pubKeyLen = pSz;
  5513. /* Trigger Key Generation */
  5514. if (kse->pubKey == NULL || kse->privKey == NULL) {
  5515. if (kse->key == NULL) {
  5516. kse->key = (DhKey*)XMALLOC(sizeof(DhKey), ssl->heap,
  5517. DYNAMIC_TYPE_DH);
  5518. if (kse->key == NULL)
  5519. return MEMORY_E;
  5520. /* Setup Key */
  5521. ret = wc_InitDhKey_ex((DhKey*)kse->key, ssl->heap, ssl->devId);
  5522. if (ret == 0) {
  5523. dhKey = (DhKey*)kse->key;
  5524. #ifdef HAVE_PUBLIC_FFDHE
  5525. ret = wc_DhSetKey(dhKey, params->p, params->p_len, params->g,
  5526. params->g_len);
  5527. #else
  5528. ret = wc_DhSetNamedKey(dhKey, kse->group);
  5529. #endif
  5530. }
  5531. }
  5532. /* Allocate space for the private and public key */
  5533. if (ret == 0 && kse->pubKey == NULL) {
  5534. kse->pubKey = (byte*)XMALLOC(kse->pubKeyLen, ssl->heap,
  5535. DYNAMIC_TYPE_PUBLIC_KEY);
  5536. if (kse->pubKey == NULL)
  5537. ret = MEMORY_E;
  5538. }
  5539. if (ret == 0 && kse->privKey == NULL) {
  5540. kse->privKey = (byte*)XMALLOC(kse->keyLen, ssl->heap,
  5541. DYNAMIC_TYPE_PRIVATE_KEY);
  5542. if (kse->privKey == NULL)
  5543. ret = MEMORY_E;
  5544. }
  5545. if (ret == 0) {
  5546. #if defined(WOLFSSL_STATIC_EPHEMERAL) && defined(WOLFSSL_DH_EXTRA)
  5547. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_DH, kse->key);
  5548. if (ret == 0) {
  5549. ret = wc_DhExportKeyPair(dhKey,
  5550. (byte*)kse->privKey, &kse->keyLen, /* private */
  5551. kse->pubKey, &kse->pubKeyLen /* public */
  5552. );
  5553. }
  5554. else
  5555. #endif
  5556. {
  5557. /* Generate a new key pair */
  5558. /* For async this is called once and when event is done, the
  5559. * provided buffers will be populated.
  5560. * Final processing is zero pad below. */
  5561. ret = DhGenKeyPair(ssl, dhKey,
  5562. (byte*)kse->privKey, &kse->keyLen, /* private */
  5563. kse->pubKey, &kse->pubKeyLen /* public */
  5564. );
  5565. #ifdef WOLFSSL_ASYNC_CRYPT
  5566. if (ret == WC_PENDING_E) {
  5567. return ret;
  5568. }
  5569. #endif
  5570. }
  5571. }
  5572. }
  5573. if (ret == 0) {
  5574. if (pSz != kse->pubKeyLen) {
  5575. /* Zero pad the front of the public key to match prime "p" size */
  5576. XMEMMOVE(kse->pubKey + pSz - kse->pubKeyLen, kse->pubKey,
  5577. kse->pubKeyLen);
  5578. XMEMSET(kse->pubKey, 0, pSz - kse->pubKeyLen);
  5579. kse->pubKeyLen = pSz;
  5580. }
  5581. if (pvtSz != kse->keyLen) {
  5582. /* Zero pad the front of the private key */
  5583. XMEMMOVE(kse->privKey + pvtSz - kse->keyLen, kse->privKey,
  5584. kse->keyLen);
  5585. XMEMSET(kse->privKey, 0, pvtSz - kse->keyLen);
  5586. kse->keyLen = pvtSz;
  5587. }
  5588. #ifdef WOLFSSL_DEBUG_TLS
  5589. WOLFSSL_MSG("Public DH Key");
  5590. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  5591. #endif
  5592. }
  5593. /* Always release the DH key to free up memory.
  5594. * The DhKey will be setup again in TLSX_KeyShare_ProcessDh */
  5595. if (dhKey != NULL)
  5596. wc_FreeDhKey(dhKey);
  5597. if (kse->key != NULL) {
  5598. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_DH);
  5599. kse->key = NULL;
  5600. }
  5601. if (ret != 0) {
  5602. /* Cleanup on error, otherwise data owned by key share entry */
  5603. if (kse->privKey != NULL) {
  5604. XFREE(kse->privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5605. kse->privKey = NULL;
  5606. }
  5607. if (kse->pubKey != NULL) {
  5608. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5609. kse->pubKey = NULL;
  5610. }
  5611. }
  5612. #else
  5613. (void)ssl;
  5614. (void)kse;
  5615. ret = NOT_COMPILED_IN;
  5616. WOLFSSL_ERROR_VERBOSE(ret);
  5617. #endif
  5618. return ret;
  5619. }
  5620. /* Create a key share entry using X25519 parameters group.
  5621. * Generates a key pair.
  5622. *
  5623. * ssl The SSL/TLS object.
  5624. * kse The key share entry object.
  5625. * returns 0 on success, otherwise failure.
  5626. */
  5627. static int TLSX_KeyShare_GenX25519Key(WOLFSSL *ssl, KeyShareEntry* kse)
  5628. {
  5629. int ret = 0;
  5630. #ifdef HAVE_CURVE25519
  5631. curve25519_key* key = (curve25519_key*)kse->key;
  5632. if (kse->key == NULL) {
  5633. /* Allocate a Curve25519 key to hold private key. */
  5634. kse->key = (curve25519_key*)XMALLOC(sizeof(curve25519_key), ssl->heap,
  5635. DYNAMIC_TYPE_PRIVATE_KEY);
  5636. if (kse->key == NULL) {
  5637. WOLFSSL_MSG("GenX25519Key memory error");
  5638. return MEMORY_E;
  5639. }
  5640. /* Make an Curve25519 key. */
  5641. ret = wc_curve25519_init_ex((curve25519_key*)kse->key, ssl->heap,
  5642. INVALID_DEVID);
  5643. if (ret == 0) {
  5644. /* setting "key" means okay to call wc_curve25519_free */
  5645. key = (curve25519_key*)kse->key;
  5646. #ifdef WOLFSSL_STATIC_EPHEMERAL
  5647. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_CURVE25519, kse->key);
  5648. if (ret != 0)
  5649. #endif
  5650. {
  5651. ret = wc_curve25519_make_key(ssl->rng, CURVE25519_KEYSIZE, key);
  5652. }
  5653. }
  5654. }
  5655. if (ret == 0 && kse->pubKey == NULL) {
  5656. /* Allocate space for the public key. */
  5657. kse->pubKey = (byte*)XMALLOC(CURVE25519_KEYSIZE, ssl->heap,
  5658. DYNAMIC_TYPE_PUBLIC_KEY);
  5659. if (kse->pubKey == NULL) {
  5660. WOLFSSL_MSG("GenX25519Key pub memory error");
  5661. ret = MEMORY_E;
  5662. }
  5663. }
  5664. if (ret == 0) {
  5665. /* Export Curve25519 public key. */
  5666. kse->pubKeyLen = CURVE25519_KEYSIZE;
  5667. if (wc_curve25519_export_public_ex(key, kse->pubKey, &kse->pubKeyLen,
  5668. EC25519_LITTLE_ENDIAN) != 0) {
  5669. ret = ECC_EXPORT_ERROR;
  5670. WOLFSSL_ERROR_VERBOSE(ret);
  5671. }
  5672. kse->pubKeyLen = CURVE25519_KEYSIZE; /* always CURVE25519_KEYSIZE */
  5673. }
  5674. #ifdef WOLFSSL_DEBUG_TLS
  5675. if (ret == 0) {
  5676. WOLFSSL_MSG("Public Curve25519 Key");
  5677. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  5678. }
  5679. #endif
  5680. if (ret != 0) {
  5681. /* Data owned by key share entry otherwise. */
  5682. if (kse->pubKey != NULL) {
  5683. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5684. kse->pubKey = NULL;
  5685. }
  5686. if (key != NULL)
  5687. wc_curve25519_free(key);
  5688. if (kse->key != NULL) {
  5689. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5690. kse->key = NULL;
  5691. }
  5692. }
  5693. #else
  5694. (void)ssl;
  5695. (void)kse;
  5696. ret = NOT_COMPILED_IN;
  5697. WOLFSSL_ERROR_VERBOSE(ret);
  5698. #endif /* HAVE_CURVE25519 */
  5699. return ret;
  5700. }
  5701. /* Create a key share entry using X448 parameters group.
  5702. * Generates a key pair.
  5703. *
  5704. * ssl The SSL/TLS object.
  5705. * kse The key share entry object.
  5706. * returns 0 on success, otherwise failure.
  5707. */
  5708. static int TLSX_KeyShare_GenX448Key(WOLFSSL *ssl, KeyShareEntry* kse)
  5709. {
  5710. int ret = 0;
  5711. #ifdef HAVE_CURVE448
  5712. curve448_key* key = (curve448_key*)kse->key;
  5713. if (kse->key == NULL) {
  5714. /* Allocate a Curve448 key to hold private key. */
  5715. kse->key = (curve448_key*)XMALLOC(sizeof(curve448_key), ssl->heap,
  5716. DYNAMIC_TYPE_PRIVATE_KEY);
  5717. if (kse->key == NULL) {
  5718. WOLFSSL_MSG("GenX448Key memory error");
  5719. return MEMORY_E;
  5720. }
  5721. /* Make an Curve448 key. */
  5722. ret = wc_curve448_init((curve448_key*)kse->key);
  5723. if (ret == 0) {
  5724. key = (curve448_key*)kse->key;
  5725. #ifdef WOLFSSL_STATIC_EPHEMERAL
  5726. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_CURVE448, kse->key);
  5727. if (ret != 0)
  5728. #endif
  5729. {
  5730. ret = wc_curve448_make_key(ssl->rng, CURVE448_KEY_SIZE, key);
  5731. }
  5732. }
  5733. }
  5734. if (ret == 0 && kse->pubKey == NULL) {
  5735. /* Allocate space for the public key. */
  5736. kse->pubKey = (byte*)XMALLOC(CURVE448_KEY_SIZE, ssl->heap,
  5737. DYNAMIC_TYPE_PUBLIC_KEY);
  5738. if (kse->pubKey == NULL) {
  5739. WOLFSSL_MSG("GenX448Key pub memory error");
  5740. ret = MEMORY_E;
  5741. }
  5742. }
  5743. if (ret == 0) {
  5744. /* Export Curve448 public key. */
  5745. kse->pubKeyLen = CURVE448_KEY_SIZE;
  5746. if (wc_curve448_export_public_ex(key, kse->pubKey, &kse->pubKeyLen,
  5747. EC448_LITTLE_ENDIAN) != 0) {
  5748. ret = ECC_EXPORT_ERROR;
  5749. }
  5750. kse->pubKeyLen = CURVE448_KEY_SIZE; /* always CURVE448_KEY_SIZE */
  5751. }
  5752. #ifdef WOLFSSL_DEBUG_TLS
  5753. if (ret == 0) {
  5754. WOLFSSL_MSG("Public Curve448 Key");
  5755. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  5756. }
  5757. #endif
  5758. if (ret != 0) {
  5759. /* Data owned by key share entry otherwise. */
  5760. if (kse->pubKey != NULL) {
  5761. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5762. kse->pubKey = NULL;
  5763. }
  5764. if (key != NULL)
  5765. wc_curve448_free(key);
  5766. if (kse->key != NULL) {
  5767. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5768. kse->key = NULL;
  5769. }
  5770. }
  5771. #else
  5772. (void)ssl;
  5773. (void)kse;
  5774. ret = NOT_COMPILED_IN;
  5775. WOLFSSL_ERROR_VERBOSE(ret);
  5776. #endif /* HAVE_CURVE448 */
  5777. return ret;
  5778. }
  5779. /* Create a key share entry using named elliptic curve parameters group.
  5780. * Generates a key pair.
  5781. *
  5782. * ssl The SSL/TLS object.
  5783. * kse The key share entry object.
  5784. * returns 0 on success, otherwise failure.
  5785. */
  5786. static int TLSX_KeyShare_GenEccKey(WOLFSSL *ssl, KeyShareEntry* kse)
  5787. {
  5788. int ret = 0;
  5789. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT)
  5790. word32 keySize = 0;
  5791. word16 curveId = (word16) ECC_CURVE_INVALID;
  5792. ecc_key* eccKey = (ecc_key*)kse->key;
  5793. /* TODO: [TLS13] The key sizes should come from wolfcrypt. */
  5794. /* Translate named group to a curve id. */
  5795. switch (kse->group) {
  5796. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  5797. #ifndef NO_ECC_SECP
  5798. case WOLFSSL_ECC_SECP256R1:
  5799. curveId = ECC_SECP256R1;
  5800. keySize = 32;
  5801. break;
  5802. #endif /* !NO_ECC_SECP */
  5803. #endif
  5804. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  5805. #ifndef NO_ECC_SECP
  5806. case WOLFSSL_ECC_SECP384R1:
  5807. curveId = ECC_SECP384R1;
  5808. keySize = 48;
  5809. break;
  5810. #endif /* !NO_ECC_SECP */
  5811. #endif
  5812. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  5813. #ifndef NO_ECC_SECP
  5814. case WOLFSSL_ECC_SECP521R1:
  5815. curveId = ECC_SECP521R1;
  5816. keySize = 66;
  5817. break;
  5818. #endif /* !NO_ECC_SECP */
  5819. #endif
  5820. default:
  5821. WOLFSSL_ERROR_VERBOSE(BAD_FUNC_ARG);
  5822. return BAD_FUNC_ARG;
  5823. }
  5824. if (kse->key == NULL) {
  5825. kse->keyLen = keySize;
  5826. kse->pubKeyLen = keySize * 2 + 1;
  5827. #if defined(WOLFSSL_RENESAS_TSIP_TLS) && (WOLFSSL_RENESAS_TSIP_VER >= 115)
  5828. ret = tsip_Tls13GenEccKeyPair(ssl, kse);
  5829. if (ret != CRYPTOCB_UNAVAILABLE) {
  5830. return ret;
  5831. }
  5832. #endif
  5833. /* Allocate an ECC key to hold private key. */
  5834. kse->key = (byte*)XMALLOC(sizeof(ecc_key), ssl->heap, DYNAMIC_TYPE_ECC);
  5835. if (kse->key == NULL) {
  5836. WOLFSSL_MSG("EccTempKey Memory error");
  5837. return MEMORY_E;
  5838. }
  5839. /* Make an ECC key */
  5840. ret = wc_ecc_init_ex((ecc_key*)kse->key, ssl->heap, ssl->devId);
  5841. if (ret == 0) {
  5842. /* setting eccKey means okay to call wc_ecc_free */
  5843. eccKey = (ecc_key*)kse->key;
  5844. #ifdef WOLFSSL_STATIC_EPHEMERAL
  5845. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_ECDH, kse->key);
  5846. if (ret != 0)
  5847. #endif
  5848. {
  5849. /* set curve info for EccMakeKey "peer" info */
  5850. ret = wc_ecc_set_curve(eccKey, kse->keyLen, curveId);
  5851. if (ret == 0) {
  5852. /* Generate ephemeral ECC key */
  5853. /* For async this is called once and when event is done, the
  5854. * provided buffers in key be populated.
  5855. * Final processing is x963 key export below. */
  5856. ret = EccMakeKey(ssl, eccKey, eccKey);
  5857. }
  5858. #ifdef WOLFSSL_ASYNC_CRYPT
  5859. if (ret == WC_PENDING_E)
  5860. return ret;
  5861. #endif
  5862. }
  5863. }
  5864. }
  5865. if (ret == 0 && kse->pubKey == NULL) {
  5866. /* Allocate space for the public key */
  5867. kse->pubKey = (byte*)XMALLOC(kse->pubKeyLen, ssl->heap,
  5868. DYNAMIC_TYPE_PUBLIC_KEY);
  5869. if (kse->pubKey == NULL) {
  5870. WOLFSSL_MSG("Key data Memory error");
  5871. ret = MEMORY_E;
  5872. }
  5873. }
  5874. if (ret == 0) {
  5875. XMEMSET(kse->pubKey, 0, kse->pubKeyLen);
  5876. /* Export public key. */
  5877. PRIVATE_KEY_UNLOCK();
  5878. if (wc_ecc_export_x963(eccKey, kse->pubKey, &kse->pubKeyLen) != 0) {
  5879. ret = ECC_EXPORT_ERROR;
  5880. WOLFSSL_ERROR_VERBOSE(ret);
  5881. }
  5882. PRIVATE_KEY_LOCK();
  5883. }
  5884. #ifdef WOLFSSL_DEBUG_TLS
  5885. if (ret == 0) {
  5886. WOLFSSL_MSG("Public ECC Key");
  5887. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  5888. }
  5889. #endif
  5890. if (ret != 0) {
  5891. /* Cleanup on error, otherwise data owned by key share entry */
  5892. if (kse->pubKey != NULL) {
  5893. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5894. kse->pubKey = NULL;
  5895. }
  5896. if (eccKey != NULL)
  5897. wc_ecc_free(eccKey);
  5898. if (kse->key != NULL) {
  5899. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5900. kse->key = NULL;
  5901. }
  5902. }
  5903. #else
  5904. (void)ssl;
  5905. (void)kse;
  5906. ret = NOT_COMPILED_IN;
  5907. WOLFSSL_ERROR_VERBOSE(ret);
  5908. #endif /* HAVE_ECC && HAVE_ECC_KEY_EXPORT */
  5909. return ret;
  5910. }
  5911. #ifdef HAVE_PQC
  5912. #ifdef HAVE_LIBOQS
  5913. /* Transform a group ID into an OQS Algorithm name as a string. */
  5914. static const char* OQS_ID2name(int id)
  5915. {
  5916. switch (id) {
  5917. case WOLFSSL_KYBER_LEVEL1: return OQS_KEM_alg_kyber_512;
  5918. case WOLFSSL_KYBER_LEVEL3: return OQS_KEM_alg_kyber_768;
  5919. case WOLFSSL_KYBER_LEVEL5: return OQS_KEM_alg_kyber_1024;
  5920. case WOLFSSL_NTRU_HPS_LEVEL1: return OQS_KEM_alg_ntru_hps2048509;
  5921. case WOLFSSL_NTRU_HPS_LEVEL3: return OQS_KEM_alg_ntru_hps2048677;
  5922. case WOLFSSL_NTRU_HPS_LEVEL5: return OQS_KEM_alg_ntru_hps4096821;
  5923. case WOLFSSL_NTRU_HRSS_LEVEL3: return OQS_KEM_alg_ntru_hrss701;
  5924. case WOLFSSL_SABER_LEVEL1: return OQS_KEM_alg_saber_lightsaber;
  5925. case WOLFSSL_SABER_LEVEL3: return OQS_KEM_alg_saber_saber;
  5926. case WOLFSSL_SABER_LEVEL5: return OQS_KEM_alg_saber_firesaber;
  5927. case WOLFSSL_KYBER_90S_LEVEL1: return OQS_KEM_alg_kyber_512_90s;
  5928. case WOLFSSL_KYBER_90S_LEVEL3: return OQS_KEM_alg_kyber_768_90s;
  5929. case WOLFSSL_KYBER_90S_LEVEL5: return OQS_KEM_alg_kyber_1024_90s;
  5930. default: break;
  5931. }
  5932. return NULL;
  5933. }
  5934. #endif /* HAVE_LIBOQS */
  5935. typedef struct PqcHybridMapping {
  5936. int hybrid;
  5937. int ecc;
  5938. int pqc;
  5939. } PqcHybridMapping;
  5940. static const PqcHybridMapping pqc_hybrid_mapping[] = {
  5941. {.hybrid = WOLFSSL_P256_NTRU_HPS_LEVEL1, .ecc = WOLFSSL_ECC_SECP256R1,
  5942. .pqc = WOLFSSL_NTRU_HPS_LEVEL1},
  5943. {.hybrid = WOLFSSL_P384_NTRU_HPS_LEVEL3, .ecc = WOLFSSL_ECC_SECP384R1,
  5944. .pqc = WOLFSSL_NTRU_HPS_LEVEL3},
  5945. {.hybrid = WOLFSSL_P521_NTRU_HPS_LEVEL5, .ecc = WOLFSSL_ECC_SECP521R1,
  5946. .pqc = WOLFSSL_NTRU_HPS_LEVEL5},
  5947. {.hybrid = WOLFSSL_P384_NTRU_HRSS_LEVEL3, .ecc = WOLFSSL_ECC_SECP384R1,
  5948. .pqc = WOLFSSL_NTRU_HRSS_LEVEL3},
  5949. {.hybrid = WOLFSSL_P256_SABER_LEVEL1, .ecc = WOLFSSL_ECC_SECP256R1,
  5950. .pqc = WOLFSSL_SABER_LEVEL1},
  5951. {.hybrid = WOLFSSL_P384_SABER_LEVEL3, .ecc = WOLFSSL_ECC_SECP384R1,
  5952. .pqc = WOLFSSL_SABER_LEVEL3},
  5953. {.hybrid = WOLFSSL_P521_SABER_LEVEL5, .ecc = WOLFSSL_ECC_SECP521R1,
  5954. .pqc = WOLFSSL_SABER_LEVEL5},
  5955. {.hybrid = WOLFSSL_P256_KYBER_LEVEL1, .ecc = WOLFSSL_ECC_SECP256R1,
  5956. .pqc = WOLFSSL_KYBER_LEVEL1},
  5957. {.hybrid = WOLFSSL_P384_KYBER_LEVEL3, .ecc = WOLFSSL_ECC_SECP384R1,
  5958. .pqc = WOLFSSL_KYBER_LEVEL3},
  5959. {.hybrid = WOLFSSL_P521_KYBER_LEVEL5, .ecc = WOLFSSL_ECC_SECP521R1,
  5960. .pqc = WOLFSSL_KYBER_LEVEL5},
  5961. {.hybrid = WOLFSSL_P256_KYBER_90S_LEVEL1, .ecc = WOLFSSL_ECC_SECP256R1,
  5962. .pqc = WOLFSSL_KYBER_90S_LEVEL1},
  5963. {.hybrid = WOLFSSL_P384_KYBER_90S_LEVEL3, .ecc = WOLFSSL_ECC_SECP384R1,
  5964. .pqc = WOLFSSL_KYBER_90S_LEVEL3},
  5965. {.hybrid = WOLFSSL_P521_KYBER_90S_LEVEL5, .ecc = WOLFSSL_ECC_SECP521R1,
  5966. .pqc = WOLFSSL_KYBER_90S_LEVEL5},
  5967. {.hybrid = 0, .ecc = 0, .pqc = 0}
  5968. };
  5969. /* This will map an ecc-pqs hybrid group into its ecc group and pqc kem group.
  5970. * If it cannot find a mapping then *pqc is set to group. ecc is optional. */
  5971. static void findEccPqc(int *ecc, int *pqc, int group)
  5972. {
  5973. int i;
  5974. if (pqc == NULL) {
  5975. return;
  5976. }
  5977. *pqc = 0;
  5978. if (ecc != NULL) {
  5979. *ecc = 0;
  5980. }
  5981. for (i = 0; pqc_hybrid_mapping[i].hybrid != 0; i++) {
  5982. if (pqc_hybrid_mapping[i].hybrid == group) {
  5983. *pqc = pqc_hybrid_mapping[i].pqc;
  5984. if (ecc != NULL) {
  5985. *ecc = pqc_hybrid_mapping[i].ecc;
  5986. }
  5987. break;
  5988. }
  5989. }
  5990. if (*pqc == 0) {
  5991. /* It is not a hybrid, so maybe its simple. */
  5992. *pqc = group;
  5993. }
  5994. }
  5995. /* Create a key share entry using liboqs parameters group.
  5996. * Generates a key pair.
  5997. *
  5998. * ssl The SSL/TLS object.
  5999. * kse The key share entry object.
  6000. * returns 0 on success, otherwise failure.
  6001. */
  6002. #ifdef HAVE_LIBOQS
  6003. static int TLSX_KeyShare_GenPqcKey(WOLFSSL *ssl, KeyShareEntry* kse)
  6004. {
  6005. int ret = 0;
  6006. const char* algName = NULL;
  6007. OQS_KEM* kem = NULL;
  6008. byte* pubKey = NULL;
  6009. byte* privKey = NULL;
  6010. KeyShareEntry *ecc_kse = NULL;
  6011. int oqs_group = 0;
  6012. int ecc_group = 0;
  6013. findEccPqc(&ecc_group, &oqs_group, kse->group);
  6014. algName = OQS_ID2name(oqs_group);
  6015. if (algName == NULL) {
  6016. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  6017. return BAD_FUNC_ARG;
  6018. }
  6019. kem = OQS_KEM_new(algName);
  6020. if (kem == NULL) {
  6021. WOLFSSL_MSG("Error creating OQS KEM, ensure algorithm support"
  6022. "was enabled in liboqs.");
  6023. return BAD_FUNC_ARG;
  6024. }
  6025. ecc_kse = (KeyShareEntry*)XMALLOC(sizeof(*ecc_kse), ssl->heap,
  6026. DYNAMIC_TYPE_TLSX);
  6027. if (ecc_kse == NULL) {
  6028. WOLFSSL_MSG("ecc_kse memory allocation failure");
  6029. ret = MEMORY_ERROR;
  6030. }
  6031. if (ret == 0) {
  6032. XMEMSET(ecc_kse, 0, sizeof(*ecc_kse));
  6033. }
  6034. if (ret == 0 && ecc_group != 0) {
  6035. ecc_kse->group = ecc_group;
  6036. ret = TLSX_KeyShare_GenEccKey(ssl, ecc_kse);
  6037. /* If fail, no error message, TLSX_KeyShare_GenEccKey will do it. */
  6038. }
  6039. if (ret == 0) {
  6040. pubKey = (byte*)XMALLOC(ecc_kse->pubKeyLen + kem->length_public_key,
  6041. ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6042. if (pubKey == NULL) {
  6043. WOLFSSL_MSG("pubkey memory allocation failure");
  6044. ret = MEMORY_ERROR;
  6045. }
  6046. }
  6047. if (ret == 0) {
  6048. privKey = (byte*)XMALLOC(kem->length_secret_key,
  6049. ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6050. if (privKey == NULL) {
  6051. WOLFSSL_MSG("privkey memory allocation failure");
  6052. ret = MEMORY_ERROR;
  6053. }
  6054. }
  6055. if (ret == 0) {
  6056. if (OQS_KEM_keypair(kem, pubKey + ecc_kse->pubKeyLen, privKey) ==
  6057. OQS_SUCCESS) {
  6058. XMEMCPY(pubKey, ecc_kse->pubKey, ecc_kse->pubKeyLen);
  6059. kse->pubKey = pubKey;
  6060. kse->pubKeyLen = ecc_kse->pubKeyLen +
  6061. (word32) kem->length_public_key;
  6062. pubKey = NULL;
  6063. /* Note we are saving the OQS private key and ECC private key
  6064. * separately. That's because the ECC private key is not simply a
  6065. * buffer. Its is an ecc_key struct.
  6066. */
  6067. kse->privKey = privKey;
  6068. privKey = NULL;
  6069. kse->key = ecc_kse->key;
  6070. ecc_kse->key = NULL;
  6071. ret = 0;
  6072. }
  6073. else {
  6074. WOLFSSL_MSG("liboqs keygen failure");
  6075. ret = BAD_FUNC_ARG;
  6076. WOLFSSL_ERROR_VERBOSE(ret);
  6077. }
  6078. }
  6079. #ifdef WOLFSSL_DEBUG_TLS
  6080. WOLFSSL_MSG("Public liboqs Key");
  6081. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  6082. #endif
  6083. OQS_KEM_free(kem);
  6084. TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
  6085. if (pubKey != NULL)
  6086. XFREE(pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6087. if (privKey != NULL)
  6088. XFREE(privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6089. return ret;
  6090. }
  6091. #elif defined(HAVE_PQM4)
  6092. static int TLSX_KeyShare_GenPqcKey(WOLFSSL *ssl, KeyShareEntry* kse)
  6093. {
  6094. /* This assumes KYBER LEVEL 1 (512) implementation is compiled in. */
  6095. int ret = 0;
  6096. byte* pubKey = NULL;
  6097. byte* privKey = NULL;
  6098. KeyShareEntry *ecc_kse = NULL;
  6099. int oqs_group = 0;
  6100. int ecc_group = 0;
  6101. findEccPqc(&ecc_group, &oqs_group, kse->group);
  6102. ecc_kse = (KeyShareEntry*)XMALLOC(sizeof(*ecc_kse), ssl->heap,
  6103. DYNAMIC_TYPE_TLSX);
  6104. if (ecc_kse == NULL) {
  6105. WOLFSSL_MSG("ecc_kse memory allocation failure");
  6106. ret = MEMORY_ERROR;
  6107. }
  6108. if (ret == 0) {
  6109. XMEMSET(ecc_kse, 0, sizeof(*ecc_kse));
  6110. }
  6111. if (ret == 0 && ecc_group != 0) {
  6112. ecc_kse->group = ecc_group;
  6113. ret = TLSX_KeyShare_GenEccKey(ssl, ecc_kse);
  6114. /* If fail, no error message, TLSX_KeyShare_GenEccKey will do it. */
  6115. }
  6116. if (ret == 0) {
  6117. pubKey = (byte*)XMALLOC(ecc_kse->pubKeyLen + PQM4_PUBLIC_KEY_LENGTH,
  6118. ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6119. if (pubKey == NULL) {
  6120. WOLFSSL_MSG("pubkey memory allocation failure");
  6121. ret = MEMORY_ERROR;
  6122. }
  6123. }
  6124. if (ret == 0) {
  6125. privKey = (byte*)XMALLOC(PQM4_PRIVATE_KEY_LENGTH,
  6126. ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6127. if (privKey == NULL) {
  6128. WOLFSSL_MSG("privkey memory allocation failure");
  6129. ret = MEMORY_ERROR;
  6130. }
  6131. }
  6132. if (ret == 0) {
  6133. if (crypto_kem_keypair(pubKey + ecc_kse->pubKeyLen, privKey) == 0) {
  6134. XMEMCPY(pubKey, ecc_kse->pubKey, ecc_kse->pubKeyLen);
  6135. kse->pubKey = pubKey;
  6136. kse->pubKeyLen = ecc_kse->pubKeyLen +
  6137. (word32) PQM4_PUBLIC_KEY_LENGTH;
  6138. pubKey = NULL;
  6139. /* Note we are saving the PQ private key and ECC private key
  6140. * separately. That's because the ECC private key is not simply a
  6141. * buffer. Its is an ecc_key struct.
  6142. */
  6143. kse->privKey = privKey;
  6144. privKey = NULL;
  6145. kse->key = ecc_kse->key;
  6146. ecc_kse->key = NULL;
  6147. ret = 0;
  6148. }
  6149. else {
  6150. WOLFSSL_MSG("liboqs keygen failure");
  6151. ret = BAD_FUNC_ARG;
  6152. WOLFSSL_ERROR_VERBOSE(ret);
  6153. }
  6154. }
  6155. #ifdef WOLFSSL_DEBUG_TLS
  6156. WOLFSSL_MSG("Public PQM4 Key");
  6157. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen );
  6158. #endif
  6159. TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
  6160. if (pubKey != NULL)
  6161. XFREE(pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6162. if (privKey != NULL)
  6163. XFREE(privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6164. return ret;
  6165. }
  6166. #endif /* HAVE_PQM4 */
  6167. #endif /* HAVE_PQC */
  6168. /* Generate a secret/key using the key share entry.
  6169. *
  6170. * ssl The SSL/TLS object.
  6171. * kse The key share entry holding peer data.
  6172. */
  6173. static int TLSX_KeyShare_GenKey(WOLFSSL *ssl, KeyShareEntry *kse)
  6174. {
  6175. int ret;
  6176. /* Named FFDHE groups have a bit set to identify them. */
  6177. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(kse->group))
  6178. ret = TLSX_KeyShare_GenDhKey(ssl, kse);
  6179. else if (kse->group == WOLFSSL_ECC_X25519)
  6180. ret = TLSX_KeyShare_GenX25519Key(ssl, kse);
  6181. else if (kse->group == WOLFSSL_ECC_X448)
  6182. ret = TLSX_KeyShare_GenX448Key(ssl, kse);
  6183. #ifdef HAVE_PQC
  6184. else if (WOLFSSL_NAMED_GROUP_IS_PQC(kse->group))
  6185. ret = TLSX_KeyShare_GenPqcKey(ssl, kse);
  6186. #endif
  6187. else
  6188. ret = TLSX_KeyShare_GenEccKey(ssl, kse);
  6189. #ifdef WOLFSSL_ASYNC_CRYPT
  6190. kse->lastRet = ret;
  6191. #endif
  6192. return ret;
  6193. }
  6194. /* Free the key share dynamic data.
  6195. *
  6196. * list The linked list of key share entry objects.
  6197. * heap The heap used for allocation.
  6198. */
  6199. static void TLSX_KeyShare_FreeAll(KeyShareEntry* list, void* heap)
  6200. {
  6201. KeyShareEntry* current;
  6202. while ((current = list) != NULL) {
  6203. list = current->next;
  6204. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(current->group)) {
  6205. #ifndef NO_DH
  6206. wc_FreeDhKey((DhKey*)current->key);
  6207. #endif
  6208. }
  6209. else if (current->group == WOLFSSL_ECC_X25519) {
  6210. #ifdef HAVE_CURVE25519
  6211. wc_curve25519_free((curve25519_key*)current->key);
  6212. #endif
  6213. }
  6214. else if (current->group == WOLFSSL_ECC_X448) {
  6215. #ifdef HAVE_CURVE448
  6216. wc_curve448_free((curve448_key*)current->key);
  6217. #endif
  6218. }
  6219. #ifdef HAVE_PQC
  6220. else if (WOLFSSL_NAMED_GROUP_IS_PQC(current->group) &&
  6221. current->key != NULL) {
  6222. ForceZero((byte*)current->key, current->keyLen);
  6223. }
  6224. #endif
  6225. else {
  6226. #ifdef HAVE_ECC
  6227. wc_ecc_free((ecc_key*)current->key);
  6228. #endif
  6229. }
  6230. XFREE(current->key, heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6231. #if !defined(NO_DH) && (!defined(NO_CERTS) || !defined(NO_PSK))
  6232. XFREE(current->privKey, heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6233. #endif
  6234. XFREE(current->pubKey, heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6235. XFREE(current->ke, heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6236. XFREE(current, heap, DYNAMIC_TYPE_TLSX);
  6237. }
  6238. (void)heap;
  6239. }
  6240. /* Get the size of the encoded key share extension.
  6241. *
  6242. * list The linked list of key share extensions.
  6243. * msgType The type of the message this extension is being written into.
  6244. * returns the number of bytes of the encoded key share extension.
  6245. */
  6246. static word16 TLSX_KeyShare_GetSize(KeyShareEntry* list, byte msgType)
  6247. {
  6248. word16 len = 0;
  6249. byte isRequest = (msgType == client_hello);
  6250. KeyShareEntry* current;
  6251. /* The named group the server wants to use. */
  6252. if (msgType == hello_retry_request)
  6253. return OPAQUE16_LEN;
  6254. /* List of key exchange groups. */
  6255. if (isRequest)
  6256. len += OPAQUE16_LEN;
  6257. while ((current = list) != NULL) {
  6258. list = current->next;
  6259. if (!isRequest && current->pubKey == NULL)
  6260. continue;
  6261. len += (word16)(KE_GROUP_LEN + OPAQUE16_LEN + current->pubKeyLen);
  6262. }
  6263. return len;
  6264. }
  6265. /* Writes the key share extension into the output buffer.
  6266. * Assumes that the the output buffer is big enough to hold data.
  6267. *
  6268. * list The linked list of key share entries.
  6269. * output The buffer to write into.
  6270. * msgType The type of the message this extension is being written into.
  6271. * returns the number of bytes written into the buffer.
  6272. */
  6273. static word16 TLSX_KeyShare_Write(KeyShareEntry* list, byte* output,
  6274. byte msgType)
  6275. {
  6276. word16 i = 0;
  6277. byte isRequest = (msgType == client_hello);
  6278. KeyShareEntry* current;
  6279. if (msgType == hello_retry_request) {
  6280. c16toa(list->group, output);
  6281. return OPAQUE16_LEN;
  6282. }
  6283. /* ClientHello has a list but ServerHello is only the chosen. */
  6284. if (isRequest)
  6285. i += OPAQUE16_LEN;
  6286. /* Write out all in the list. */
  6287. while ((current = list) != NULL) {
  6288. list = current->next;
  6289. if (!isRequest && current->pubKey == NULL)
  6290. continue;
  6291. c16toa(current->group, &output[i]);
  6292. i += KE_GROUP_LEN;
  6293. c16toa((word16)(current->pubKeyLen), &output[i]);
  6294. i += OPAQUE16_LEN;
  6295. XMEMCPY(&output[i], current->pubKey, current->pubKeyLen);
  6296. i += (word16)current->pubKeyLen;
  6297. }
  6298. /* Write the length of the list if required. */
  6299. if (isRequest)
  6300. c16toa(i - OPAQUE16_LEN, output);
  6301. return i;
  6302. }
  6303. /* Process the DH key share extension on the client side.
  6304. *
  6305. * ssl The SSL/TLS object.
  6306. * keyShareEntry The key share entry object to use to calculate shared secret.
  6307. * returns 0 on success and other values indicate failure.
  6308. */
  6309. static int TLSX_KeyShare_ProcessDh(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6310. {
  6311. int ret = 0;
  6312. #if !defined(NO_DH) && (!defined(NO_CERTS) || !defined(NO_PSK))
  6313. word32 pSz = 0;
  6314. DhKey* dhKey = (DhKey*)keyShareEntry->key;
  6315. #ifdef HAVE_PUBLIC_FFDHE
  6316. const DhParams* params = NULL;
  6317. switch (keyShareEntry->group) {
  6318. #ifdef HAVE_FFDHE_2048
  6319. case WOLFSSL_FFDHE_2048:
  6320. params = wc_Dh_ffdhe2048_Get();
  6321. break;
  6322. #endif
  6323. #ifdef HAVE_FFDHE_3072
  6324. case WOLFSSL_FFDHE_3072:
  6325. params = wc_Dh_ffdhe3072_Get();
  6326. break;
  6327. #endif
  6328. #ifdef HAVE_FFDHE_4096
  6329. case WOLFSSL_FFDHE_4096:
  6330. params = wc_Dh_ffdhe4096_Get();
  6331. break;
  6332. #endif
  6333. #ifdef HAVE_FFDHE_6144
  6334. case WOLFSSL_FFDHE_6144:
  6335. params = wc_Dh_ffdhe6144_Get();
  6336. break;
  6337. #endif
  6338. #ifdef HAVE_FFDHE_8192
  6339. case WOLFSSL_FFDHE_8192:
  6340. params = wc_Dh_ffdhe8192_Get();
  6341. break;
  6342. #endif
  6343. default:
  6344. break;
  6345. }
  6346. if (params == NULL) {
  6347. WOLFSSL_ERROR_VERBOSE(PEER_KEY_ERROR);
  6348. return PEER_KEY_ERROR;
  6349. }
  6350. pSz = params->p_len;
  6351. #else
  6352. ret = wc_DhGetNamedKeyParamSize(keyShareEntry->group, &pSz, NULL, NULL);
  6353. if (ret != 0 || pSz == 0) {
  6354. WOLFSSL_ERROR_VERBOSE(PEER_KEY_ERROR);
  6355. return PEER_KEY_ERROR;
  6356. }
  6357. #endif
  6358. /* if DhKey is not setup, do it now */
  6359. if (keyShareEntry->key == NULL) {
  6360. keyShareEntry->key = (DhKey*)XMALLOC(sizeof(DhKey), ssl->heap,
  6361. DYNAMIC_TYPE_DH);
  6362. if (keyShareEntry->key == NULL)
  6363. return MEMORY_E;
  6364. /* Setup Key */
  6365. ret = wc_InitDhKey_ex((DhKey*)keyShareEntry->key, ssl->heap, ssl->devId);
  6366. if (ret == 0) {
  6367. dhKey = (DhKey*)keyShareEntry->key;
  6368. /* Set key */
  6369. #ifdef HAVE_PUBLIC_FFDHE
  6370. ret = wc_DhSetKey(dhKey, params->p, params->p_len, params->g,
  6371. params->g_len);
  6372. #else
  6373. ret = wc_DhSetNamedKey(dhKey, keyShareEntry->group);
  6374. #endif
  6375. }
  6376. }
  6377. if (ret == 0
  6378. #ifdef WOLFSSL_ASYNC_CRYPT
  6379. && keyShareEntry->lastRet == 0 /* don't enter here if WC_PENDING_E */
  6380. #endif
  6381. ) {
  6382. #ifdef WOLFSSL_DEBUG_TLS
  6383. WOLFSSL_MSG("Peer DH Key");
  6384. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6385. #endif
  6386. ssl->options.dhKeySz = (word16)pSz;
  6387. /* Derive secret from private key and peer's public key. */
  6388. ret = DhAgree(ssl, dhKey,
  6389. (const byte*)keyShareEntry->privKey, keyShareEntry->keyLen, /* our private */
  6390. keyShareEntry->ke, keyShareEntry->keLen, /* peer's public key */
  6391. ssl->arrays->preMasterSecret, &ssl->arrays->preMasterSz, /* secret */
  6392. NULL, 0
  6393. );
  6394. #ifdef WOLFSSL_ASYNC_CRYPT
  6395. if (ret == WC_PENDING_E) {
  6396. return ret;
  6397. }
  6398. #endif
  6399. }
  6400. /* RFC 8446 Section 7.4.1:
  6401. * ... left-padded with zeros up to the size of the prime. ...
  6402. */
  6403. if (ret == 0 && (word32)ssl->options.dhKeySz > ssl->arrays->preMasterSz) {
  6404. word32 diff = (word32)ssl->options.dhKeySz - ssl->arrays->preMasterSz;
  6405. XMEMMOVE(ssl->arrays->preMasterSecret + diff,
  6406. ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz);
  6407. XMEMSET(ssl->arrays->preMasterSecret, 0, diff);
  6408. ssl->arrays->preMasterSz = ssl->options.dhKeySz;
  6409. }
  6410. /* done with key share, release resources */
  6411. if (dhKey)
  6412. wc_FreeDhKey(dhKey);
  6413. if (keyShareEntry->key) {
  6414. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_DH);
  6415. keyShareEntry->key = NULL;
  6416. }
  6417. if (keyShareEntry->privKey != NULL) {
  6418. XFREE(keyShareEntry->privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6419. keyShareEntry->privKey = NULL;
  6420. }
  6421. if (keyShareEntry->pubKey != NULL) {
  6422. XFREE(keyShareEntry->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6423. keyShareEntry->pubKey = NULL;
  6424. }
  6425. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6426. keyShareEntry->ke = NULL;
  6427. #else
  6428. (void)ssl;
  6429. (void)keyShareEntry;
  6430. ret = PEER_KEY_ERROR;
  6431. WOLFSSL_ERROR_VERBOSE(ret);
  6432. #endif
  6433. return ret;
  6434. }
  6435. /* Process the X25519 key share extension on the client side.
  6436. *
  6437. * ssl The SSL/TLS object.
  6438. * keyShareEntry The key share entry object to use to calculate shared secret.
  6439. * returns 0 on success and other values indicate failure.
  6440. */
  6441. static int TLSX_KeyShare_ProcessX25519(WOLFSSL* ssl,
  6442. KeyShareEntry* keyShareEntry)
  6443. {
  6444. int ret;
  6445. #ifdef HAVE_CURVE25519
  6446. curve25519_key* key = (curve25519_key*)keyShareEntry->key;
  6447. curve25519_key* peerX25519Key;
  6448. #ifdef HAVE_ECC
  6449. if (ssl->peerEccKey != NULL) {
  6450. wc_ecc_free(ssl->peerEccKey);
  6451. ssl->peerEccKey = NULL;
  6452. ssl->peerEccKeyPresent = 0;
  6453. }
  6454. #endif
  6455. peerX25519Key = (curve25519_key*)XMALLOC(sizeof(curve25519_key), ssl->heap,
  6456. DYNAMIC_TYPE_TLSX);
  6457. if (peerX25519Key == NULL) {
  6458. WOLFSSL_MSG("PeerEccKey Memory error");
  6459. return MEMORY_ERROR;
  6460. }
  6461. ret = wc_curve25519_init(peerX25519Key);
  6462. if (ret != 0) {
  6463. XFREE(peerX25519Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6464. return ret;
  6465. }
  6466. #ifdef WOLFSSL_DEBUG_TLS
  6467. WOLFSSL_MSG("Peer Curve25519 Key");
  6468. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6469. #endif
  6470. if (wc_curve25519_check_public(keyShareEntry->ke, keyShareEntry->keLen,
  6471. EC25519_LITTLE_ENDIAN) != 0) {
  6472. ret = ECC_PEERKEY_ERROR;
  6473. WOLFSSL_ERROR_VERBOSE(ret);
  6474. }
  6475. if (ret == 0) {
  6476. if (wc_curve25519_import_public_ex(keyShareEntry->ke,
  6477. keyShareEntry->keLen, peerX25519Key,
  6478. EC25519_LITTLE_ENDIAN) != 0) {
  6479. ret = ECC_PEERKEY_ERROR;
  6480. WOLFSSL_ERROR_VERBOSE(ret);
  6481. }
  6482. }
  6483. if (ret == 0) {
  6484. ssl->ecdhCurveOID = ECC_X25519_OID;
  6485. ret = wc_curve25519_shared_secret_ex(key, peerX25519Key,
  6486. ssl->arrays->preMasterSecret,
  6487. &ssl->arrays->preMasterSz,
  6488. EC25519_LITTLE_ENDIAN);
  6489. }
  6490. wc_curve25519_free(peerX25519Key);
  6491. XFREE(peerX25519Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6492. wc_curve25519_free((curve25519_key*)keyShareEntry->key);
  6493. if (keyShareEntry->key != NULL) {
  6494. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6495. keyShareEntry->key = NULL;
  6496. }
  6497. #else
  6498. (void)ssl;
  6499. (void)keyShareEntry;
  6500. ret = PEER_KEY_ERROR;
  6501. WOLFSSL_ERROR_VERBOSE(ret);
  6502. #endif /* HAVE_CURVE25519 */
  6503. return ret;
  6504. }
  6505. /* Process the X448 key share extension on the client side.
  6506. *
  6507. * ssl The SSL/TLS object.
  6508. * keyShareEntry The key share entry object to use to calculate shared secret.
  6509. * returns 0 on success and other values indicate failure.
  6510. */
  6511. static int TLSX_KeyShare_ProcessX448(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6512. {
  6513. int ret;
  6514. #ifdef HAVE_CURVE448
  6515. curve448_key* key = (curve448_key*)keyShareEntry->key;
  6516. curve448_key* peerX448Key;
  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. peerX448Key = (curve448_key*)XMALLOC(sizeof(curve448_key), ssl->heap,
  6525. DYNAMIC_TYPE_TLSX);
  6526. if (peerX448Key == NULL) {
  6527. WOLFSSL_MSG("PeerEccKey Memory error");
  6528. return MEMORY_ERROR;
  6529. }
  6530. ret = wc_curve448_init(peerX448Key);
  6531. if (ret != 0) {
  6532. XFREE(peerX448Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6533. return ret;
  6534. }
  6535. #ifdef WOLFSSL_DEBUG_TLS
  6536. WOLFSSL_MSG("Peer Curve448 Key");
  6537. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6538. #endif
  6539. if (wc_curve448_check_public(keyShareEntry->ke, keyShareEntry->keLen,
  6540. EC448_LITTLE_ENDIAN) != 0) {
  6541. ret = ECC_PEERKEY_ERROR;
  6542. WOLFSSL_ERROR_VERBOSE(ret);
  6543. }
  6544. if (ret == 0) {
  6545. if (wc_curve448_import_public_ex(keyShareEntry->ke,
  6546. keyShareEntry->keLen, peerX448Key,
  6547. EC448_LITTLE_ENDIAN) != 0) {
  6548. ret = ECC_PEERKEY_ERROR;
  6549. WOLFSSL_ERROR_VERBOSE(ret);
  6550. }
  6551. }
  6552. if (ret == 0) {
  6553. ssl->ecdhCurveOID = ECC_X448_OID;
  6554. ret = wc_curve448_shared_secret_ex(key, peerX448Key,
  6555. ssl->arrays->preMasterSecret,
  6556. &ssl->arrays->preMasterSz,
  6557. EC448_LITTLE_ENDIAN);
  6558. }
  6559. wc_curve448_free(peerX448Key);
  6560. XFREE(peerX448Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6561. wc_curve448_free((curve448_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_CURVE448 */
  6572. return ret;
  6573. }
  6574. /* Process the ECC 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_ProcessEcc(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6581. {
  6582. int ret = 0;
  6583. #ifdef HAVE_ECC
  6584. int curveId = ECC_CURVE_INVALID;
  6585. ecc_key* eccKey = (ecc_key*)keyShareEntry->key;
  6586. /* find supported curve */
  6587. switch (keyShareEntry->group) {
  6588. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  6589. #ifndef NO_ECC_SECP
  6590. case WOLFSSL_ECC_SECP256R1:
  6591. curveId = ECC_SECP256R1;
  6592. break;
  6593. #endif /* !NO_ECC_SECP */
  6594. #endif
  6595. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  6596. #ifndef NO_ECC_SECP
  6597. case WOLFSSL_ECC_SECP384R1:
  6598. curveId = ECC_SECP384R1;
  6599. break;
  6600. #endif /* !NO_ECC_SECP */
  6601. #endif
  6602. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  6603. #ifndef NO_ECC_SECP
  6604. case WOLFSSL_ECC_SECP521R1:
  6605. curveId = ECC_SECP521R1;
  6606. break;
  6607. #endif /* !NO_ECC_SECP */
  6608. #endif
  6609. #if defined(HAVE_X448) && ECC_MIN_KEY_SZ <= 448
  6610. case WOLFSSL_ECC_X448:
  6611. curveId = ECC_X448;
  6612. break;
  6613. #endif
  6614. default:
  6615. /* unsupported curve */
  6616. WOLFSSL_ERROR_VERBOSE(ECC_PEERKEY_ERROR);
  6617. return ECC_PEERKEY_ERROR;
  6618. }
  6619. #ifdef WOLFSSL_ASYNC_CRYPT
  6620. if (keyShareEntry->lastRet == 0) /* don't enter here if WC_PENDING_E */
  6621. #endif
  6622. {
  6623. #ifdef WOLFSSL_DEBUG_TLS
  6624. WOLFSSL_MSG("Peer ECC Key");
  6625. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6626. #endif
  6627. if (ssl->peerEccKey != NULL) {
  6628. wc_ecc_free(ssl->peerEccKey);
  6629. XFREE(ssl->peerEccKey, ssl->heap, DYNAMIC_TYPE_ECC);
  6630. ssl->peerEccKeyPresent = 0;
  6631. }
  6632. #if defined(WOLFSSL_RENESAS_TSIP_TLS) && (WOLFSSL_RENESAS_TSIP_VER >= 115)
  6633. ret = tsip_Tls13GenSharedSecret(ssl, keyShareEntry);
  6634. if (ret != CRYPTOCB_UNAVAILABLE) {
  6635. return ret;
  6636. }
  6637. ret = 0;
  6638. #endif
  6639. ssl->peerEccKey = (ecc_key*)XMALLOC(sizeof(ecc_key), ssl->heap,
  6640. DYNAMIC_TYPE_ECC);
  6641. if (ssl->peerEccKey == NULL) {
  6642. WOLFSSL_MSG("PeerEccKey Memory error");
  6643. ret = MEMORY_ERROR;
  6644. }
  6645. if (ret == 0) {
  6646. ret = wc_ecc_init_ex(ssl->peerEccKey, ssl->heap, ssl->devId);
  6647. }
  6648. /* Point is validated by import function. */
  6649. if (ret == 0) {
  6650. ret = wc_ecc_import_x963_ex(keyShareEntry->ke, keyShareEntry->keLen,
  6651. ssl->peerEccKey, curveId);
  6652. if (ret != 0) {
  6653. ret = ECC_PEERKEY_ERROR;
  6654. WOLFSSL_ERROR_VERBOSE(ret);
  6655. }
  6656. }
  6657. if (ret == 0) {
  6658. ssl->ecdhCurveOID = ssl->peerEccKey->dp->oidSum;
  6659. ssl->peerEccKeyPresent = 1;
  6660. }
  6661. }
  6662. if (ret == 0 && eccKey == NULL)
  6663. ret = BAD_FUNC_ARG;
  6664. if (ret == 0) {
  6665. ret = EccSharedSecret(ssl, eccKey, ssl->peerEccKey,
  6666. keyShareEntry->ke, &keyShareEntry->keLen,
  6667. ssl->arrays->preMasterSecret, &ssl->arrays->preMasterSz,
  6668. ssl->options.side
  6669. );
  6670. #ifdef WOLFSSL_ASYNC_CRYPT
  6671. if (ret == WC_PENDING_E)
  6672. return ret;
  6673. #endif
  6674. }
  6675. /* done with key share, release resources */
  6676. if (ssl->peerEccKey != NULL
  6677. #ifdef HAVE_PK_CALLBACKS
  6678. && ssl->ctx->EccSharedSecretCb == NULL
  6679. #endif
  6680. ) {
  6681. wc_ecc_free(ssl->peerEccKey);
  6682. XFREE(ssl->peerEccKey, ssl->heap, DYNAMIC_TYPE_ECC);
  6683. ssl->peerEccKey = NULL;
  6684. ssl->peerEccKeyPresent = 0;
  6685. }
  6686. if (keyShareEntry->key) {
  6687. wc_ecc_free((ecc_key*)keyShareEntry->key);
  6688. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_ECC);
  6689. keyShareEntry->key = NULL;
  6690. }
  6691. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6692. keyShareEntry->ke = NULL;
  6693. #else
  6694. (void)ssl;
  6695. (void)keyShareEntry;
  6696. ret = PEER_KEY_ERROR;
  6697. WOLFSSL_ERROR_VERBOSE(ret);
  6698. #endif /* HAVE_ECC */
  6699. return ret;
  6700. }
  6701. #ifdef HAVE_PQC
  6702. #ifdef HAVE_LIBOQS
  6703. /* Process the liboqs key share extension on the client side.
  6704. *
  6705. * ssl The SSL/TLS object.
  6706. * keyShareEntry The key share entry object to use to calculate shared secret.
  6707. * returns 0 on success and other values indicate failure.
  6708. */
  6709. static int TLSX_KeyShare_ProcessPqc(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6710. {
  6711. int ret = 0;
  6712. const char* algName = NULL;
  6713. OQS_KEM* kem = NULL;
  6714. byte* sharedSecret = NULL;
  6715. word32 sharedSecretLen = 0;
  6716. int oqs_group = 0;
  6717. int ecc_group = 0;
  6718. ecc_key eccpubkey;
  6719. word32 outlen = 0;
  6720. if (keyShareEntry->ke == NULL) {
  6721. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  6722. return BAD_FUNC_ARG;
  6723. }
  6724. if (ssl->options.side == WOLFSSL_SERVER_END) {
  6725. /* I am the server, the shared secret has already been generated and
  6726. * is in keyShareEntry->ke; copy it to the pre-master secret
  6727. * pre-allocated buffer. */
  6728. if (keyShareEntry->keLen > ENCRYPT_LEN) {
  6729. WOLFSSL_MSG("shared secret is too long.");
  6730. WOLFSSL_ERROR_VERBOSE(LENGTH_ERROR);
  6731. return LENGTH_ERROR;
  6732. }
  6733. XMEMCPY(ssl->arrays->preMasterSecret, keyShareEntry->ke, keyShareEntry->keLen);
  6734. ssl->arrays->preMasterSz = keyShareEntry->keLen;
  6735. XFREE(keyShareEntry->ke, sl->heap, DYNAMIC_TYPE_SECRET)
  6736. keyShareEntry->ke = NULL;
  6737. keyShareEntry->keLen = 0;
  6738. return 0;
  6739. }
  6740. /* I am the client, the ciphertext is in keyShareEntry->ke */
  6741. findEccPqc(&ecc_group, &oqs_group, keyShareEntry->group);
  6742. algName = OQS_ID2name(oqs_group);
  6743. if (algName == NULL) {
  6744. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  6745. WOLFSSL_ERROR_VERBOSE(BAD_FUNC_ARG);
  6746. return BAD_FUNC_ARG;
  6747. }
  6748. kem = OQS_KEM_new(algName);
  6749. if (kem == NULL) {
  6750. WOLFSSL_MSG("Error creating OQS KEM, ensure algorithm support"
  6751. "was enabled in liboqs.");
  6752. return MEMORY_E;
  6753. }
  6754. sharedSecretLen = (word32)kem->length_shared_secret;
  6755. switch (ecc_group) {
  6756. case WOLFSSL_ECC_SECP256R1:
  6757. sharedSecretLen += 32;
  6758. outlen = 32;
  6759. break;
  6760. case WOLFSSL_ECC_SECP384R1:
  6761. sharedSecretLen += 48;
  6762. outlen = 48;
  6763. break;
  6764. case WOLFSSL_ECC_SECP521R1:
  6765. sharedSecretLen += 66;
  6766. outlen = 66;
  6767. break;
  6768. default:
  6769. break;
  6770. }
  6771. ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
  6772. if (ret != 0) {
  6773. WOLFSSL_MSG("Memory allocation error.");
  6774. return MEMORY_E;
  6775. }
  6776. sharedSecret = (byte*)XMALLOC(sharedSecretLen, ssl->heap,
  6777. DYNAMIC_TYPE_TLSX);
  6778. if (sharedSecret == NULL) {
  6779. WOLFSSL_MSG("Memory allocation error.");
  6780. ret = MEMORY_E;
  6781. }
  6782. if (ret == 0 && OQS_KEM_decaps(kem, sharedSecret + outlen,
  6783. keyShareEntry->ke + keyShareEntry->keLen -
  6784. kem->length_ciphertext,
  6785. keyShareEntry->privKey) != OQS_SUCCESS) {
  6786. WOLFSSL_MSG("Liboqs decapsulation failure.");
  6787. ret = BAD_FUNC_ARG;
  6788. WOLFSSL_ERROR_VERBOSE(ret);
  6789. }
  6790. if (ecc_group != 0) {
  6791. if (ret == 0) {
  6792. /* Point is validated by import function. */
  6793. ret = wc_ecc_import_x963(keyShareEntry->ke,
  6794. keyShareEntry->keLen -
  6795. (word32)kem->length_ciphertext,
  6796. &eccpubkey);
  6797. if (ret != 0) {
  6798. WOLFSSL_ERROR_VERBOSE(ret);
  6799. WOLFSSL_MSG("ECC Public key import error.");
  6800. }
  6801. }
  6802. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  6803. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  6804. !defined(HAVE_SELFTEST)
  6805. if (ret == 0) {
  6806. ret = wc_ecc_set_rng(keyShareEntry->key, ssl->rng);
  6807. if (ret != 0) {
  6808. WOLFSSL_MSG("Failure to set the ECC private key RNG.");
  6809. }
  6810. }
  6811. #endif
  6812. if (ret == 0) {
  6813. PRIVATE_KEY_UNLOCK();
  6814. ret = wc_ecc_shared_secret(keyShareEntry->key, &eccpubkey, sharedSecret, &outlen);
  6815. PRIVATE_KEY_LOCK();
  6816. if (outlen != sharedSecretLen - kem->length_shared_secret) {
  6817. WOLFSSL_MSG("ECC shared secret derivation error.");
  6818. ret = BAD_FUNC_ARG;
  6819. WOLFSSL_ERROR_VERBOSE(ret);
  6820. }
  6821. }
  6822. }
  6823. if (sharedSecretLen > ENCRYPT_LEN) {
  6824. WOLFSSL_MSG("shared secret is too long.");
  6825. ret = LENGTH_ERROR;
  6826. WOLFSSL_ERROR_VERBOSE(ret);
  6827. }
  6828. if (ret == 0) {
  6829. /* Copy the shared secret to the pre-master secret pre-allocated
  6830. * buffer. */
  6831. XMEMCPY(ssl->arrays->preMasterSecret, sharedSecret, sharedSecretLen);
  6832. ssl->arrays->preMasterSz = (word32) sharedSecretLen;
  6833. }
  6834. if (sharedSecret != NULL) {
  6835. XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_SECRET);
  6836. }
  6837. wc_ecc_free(&eccpubkey);
  6838. OQS_KEM_free(kem);
  6839. return ret;
  6840. }
  6841. #elif defined(HAVE_PQM4)
  6842. static int TLSX_KeyShare_ProcessPqc(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6843. {
  6844. int ret = 0;
  6845. byte* sharedSecret = NULL;
  6846. word32 sharedSecretLen = 0;
  6847. int oqs_group = 0;
  6848. int ecc_group = 0;
  6849. ecc_key eccpubkey;
  6850. word32 outlen = 0;
  6851. if (keyShareEntry->ke == NULL) {
  6852. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  6853. return BAD_FUNC_ARG;
  6854. }
  6855. if (ssl->options.side == WOLFSSL_SERVER_END) {
  6856. /* I am the server, the shared secret has already been generated and
  6857. * is in keyShareEntry->ke; copy it to the pre-master secret
  6858. * pre-allocated buffer. */
  6859. if (keyShareEntry->keLen > ENCRYPT_LEN) {
  6860. WOLFSSL_MSG("shared secret is too long.");
  6861. WOLFSSL_ERROR_VERBOSE(LENGTH_ERROR);
  6862. return LENGTH_ERROR;
  6863. }
  6864. XMEMCPY(ssl->arrays->preMasterSecret, keyShareEntry->ke, keyShareEntry->keLen);
  6865. ssl->arrays->preMasterSz = keyShareEntry->keLen;
  6866. XFREE(keyShareEntry->ke, sl->heap, DYNAMIC_TYPE_SECRET);
  6867. keyShareEntry->ke = NULL;
  6868. keyShareEntry->keLen = 0;
  6869. return 0;
  6870. }
  6871. /* I am the client, the ciphertext is in keyShareEntry->ke */
  6872. findEccPqc(&ecc_group, &oqs_group, keyShareEntry->group);
  6873. sharedSecretLen = (word32)PQM4_SHARED_SECRET_LENGTH;
  6874. switch (ecc_group) {
  6875. case WOLFSSL_ECC_SECP256R1:
  6876. sharedSecretLen += 32;
  6877. outlen = 32;
  6878. break;
  6879. case WOLFSSL_ECC_SECP384R1:
  6880. sharedSecretLen += 48;
  6881. outlen = 48;
  6882. break;
  6883. case WOLFSSL_ECC_SECP521R1:
  6884. sharedSecretLen += 66;
  6885. outlen = 66;
  6886. break;
  6887. default:
  6888. break;
  6889. }
  6890. ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
  6891. if (ret != 0) {
  6892. WOLFSSL_MSG("Memory allocation error.");
  6893. return MEMORY_E;
  6894. }
  6895. sharedSecret = (byte*)XMALLOC(sharedSecretLen, ssl->heap,
  6896. DYNAMIC_TYPE_TLSX);
  6897. if (sharedSecret == NULL) {
  6898. WOLFSSL_MSG("Memory allocation error.");
  6899. ret = MEMORY_E;
  6900. }
  6901. if (ret == 0 && crypto_kem_dec(sharedSecret + outlen,
  6902. keyShareEntry->ke + keyShareEntry->keLen -
  6903. PQM4_CIPHERTEXT_LENGTH,
  6904. keyShareEntry->privKey) != 0) {
  6905. WOLFSSL_MSG("PQM4 decapsulation failure.");
  6906. ret = BAD_FUNC_ARG;
  6907. } else {
  6908. WOLFSSL_MSG("PQM4 decapsulation SUCCESS!!!!!");
  6909. }
  6910. if (ecc_group != 0) {
  6911. if (ret == 0) {
  6912. /* Point is validated by import function. */
  6913. ret = wc_ecc_import_x963(keyShareEntry->ke,
  6914. keyShareEntry->keLen -
  6915. (word32)PQM4_CIPHERTEXT_LENGTH,
  6916. &eccpubkey);
  6917. if (ret != 0) {
  6918. WOLFSSL_MSG("ECC Public key import error.");
  6919. }
  6920. }
  6921. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  6922. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  6923. !defined(HAVE_SELFTEST)
  6924. if (ret == 0) {
  6925. ret = wc_ecc_set_rng(keyShareEntry->key, ssl->rng);
  6926. if (ret != 0) {
  6927. WOLFSSL_MSG("Failure to set the ECC private key RNG.");
  6928. }
  6929. }
  6930. #endif
  6931. if (ret == 0) {
  6932. PRIVATE_KEY_UNLOCK();
  6933. ret = wc_ecc_shared_secret(keyShareEntry->key, &eccpubkey, sharedSecret, &outlen);
  6934. PRIVATE_KEY_LOCK();
  6935. if (outlen != sharedSecretLen - PQM4_SHARED_SECRET_LENGTH) {
  6936. WOLFSSL_MSG("ECC shared secret derivation error.");
  6937. ret = BAD_FUNC_ARG;
  6938. }
  6939. }
  6940. }
  6941. if (sharedSecretLen > ENCRYPT_LEN) {
  6942. WOLFSSL_MSG("shared secret is too long.\n");
  6943. ret = LENGTH_ERROR;
  6944. }
  6945. if (ret == 0) {
  6946. /* Copy the shared secret to the pre-master secret pre-allocated
  6947. * buffer. */
  6948. XMEMCPY(ssl->arrays->preMasterSecret, sharedSecret, sharedSecretLen);
  6949. ssl->arrays->preMasterSz = (word32) sharedSecretLen;
  6950. }
  6951. if (sharedSecret != NULL) {
  6952. XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_SECRET);
  6953. }
  6954. wc_ecc_free(&eccpubkey);
  6955. return ret;
  6956. }
  6957. #endif /* HAVE_PQM4 */
  6958. #endif /* HAVE_PQC */
  6959. /* Process the key share extension on the client side.
  6960. *
  6961. * ssl The SSL/TLS object.
  6962. * keyShareEntry The key share entry object to use to calculate shared secret.
  6963. * returns 0 on success and other values indicate failure.
  6964. */
  6965. static int TLSX_KeyShare_Process(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6966. {
  6967. int ret;
  6968. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  6969. ssl->session->namedGroup = (byte)keyShareEntry->group;
  6970. #endif
  6971. /* reset the pre master secret size */
  6972. if (ssl->arrays->preMasterSz == 0)
  6973. ssl->arrays->preMasterSz = ENCRYPT_LEN;
  6974. /* Use Key Share Data from server. */
  6975. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(keyShareEntry->group))
  6976. ret = TLSX_KeyShare_ProcessDh(ssl, keyShareEntry);
  6977. else if (keyShareEntry->group == WOLFSSL_ECC_X25519)
  6978. ret = TLSX_KeyShare_ProcessX25519(ssl, keyShareEntry);
  6979. else if (keyShareEntry->group == WOLFSSL_ECC_X448)
  6980. ret = TLSX_KeyShare_ProcessX448(ssl, keyShareEntry);
  6981. #ifdef HAVE_PQC
  6982. else if (WOLFSSL_NAMED_GROUP_IS_PQC(keyShareEntry->group))
  6983. ret = TLSX_KeyShare_ProcessPqc(ssl, keyShareEntry);
  6984. #endif
  6985. else
  6986. ret = TLSX_KeyShare_ProcessEcc(ssl, keyShareEntry);
  6987. #ifdef WOLFSSL_DEBUG_TLS
  6988. if (ret == 0) {
  6989. WOLFSSL_MSG("KE Secret");
  6990. WOLFSSL_BUFFER(ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz);
  6991. }
  6992. #endif
  6993. #ifdef WOLFSSL_ASYNC_CRYPT
  6994. keyShareEntry->lastRet = ret;
  6995. #endif
  6996. return ret;
  6997. }
  6998. /* Parse an entry of the KeyShare extension.
  6999. *
  7000. * ssl The SSL/TLS object.
  7001. * input The extension data.
  7002. * length The length of the extension data.
  7003. * kse The new key share entry object.
  7004. * returns a positive number to indicate amount of data parsed and a negative
  7005. * number on error.
  7006. */
  7007. static int TLSX_KeyShareEntry_Parse(WOLFSSL* ssl, const byte* input,
  7008. word16 length, KeyShareEntry **kse)
  7009. {
  7010. int ret;
  7011. word16 group;
  7012. word16 keLen;
  7013. int offset = 0;
  7014. byte* ke;
  7015. if (length < OPAQUE16_LEN + OPAQUE16_LEN)
  7016. return BUFFER_ERROR;
  7017. /* Named group */
  7018. ato16(&input[offset], &group);
  7019. offset += OPAQUE16_LEN;
  7020. /* Key exchange data - public key. */
  7021. ato16(&input[offset], &keLen);
  7022. offset += OPAQUE16_LEN;
  7023. if (keLen == 0)
  7024. return INVALID_PARAMETER;
  7025. if (keLen > length - offset)
  7026. return BUFFER_ERROR;
  7027. #ifdef HAVE_PQC
  7028. if (WOLFSSL_NAMED_GROUP_IS_PQC(group) &&
  7029. ssl->options.side == WOLFSSL_SERVER_END) {
  7030. /* For KEMs, the public key is not stored. Casting away const because
  7031. * we know for KEMs, it will be read-only.*/
  7032. ke = (byte *)&input[offset];
  7033. } else
  7034. #endif
  7035. {
  7036. /* Store a copy in the key share object. */
  7037. ke = (byte*)XMALLOC(keLen, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7038. if (ke == NULL)
  7039. return MEMORY_E;
  7040. XMEMCPY(ke, &input[offset], keLen);
  7041. }
  7042. /* Populate a key share object in the extension. */
  7043. ret = TLSX_KeyShare_Use(ssl, group, keLen, ke, kse);
  7044. if (ret != 0) {
  7045. if (ke != &input[offset]) {
  7046. XFREE(ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7047. }
  7048. return ret;
  7049. }
  7050. /* Total length of the parsed data. */
  7051. return offset + keLen;
  7052. }
  7053. /* Searches the groups sent for the specified named group.
  7054. *
  7055. * ssl SSL/TLS object.
  7056. * name Group name to match.
  7057. * returns 1 when the extension has the group name and 0 otherwise.
  7058. */
  7059. static int TLSX_KeyShare_Find(WOLFSSL* ssl, word16 group)
  7060. {
  7061. TLSX* extension;
  7062. KeyShareEntry* list;
  7063. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7064. if (extension == NULL) {
  7065. extension = TLSX_Find(ssl->ctx->extensions, TLSX_KEY_SHARE);
  7066. if (extension == NULL)
  7067. return 0;
  7068. }
  7069. list = (KeyShareEntry*)extension->data;
  7070. while (list != NULL) {
  7071. if (list->group == group)
  7072. return 1;
  7073. list = list->next;
  7074. }
  7075. return 0;
  7076. }
  7077. /* Searches the supported groups extension for the specified named group.
  7078. *
  7079. * ssl The SSL/TLS object.
  7080. * name The group name to match.
  7081. * returns 1 when the extension has the group name and 0 otherwise.
  7082. */
  7083. static int TLSX_SupportedGroups_Find(WOLFSSL* ssl, word16 name)
  7084. {
  7085. #ifdef HAVE_SUPPORTED_CURVES
  7086. TLSX* extension;
  7087. SupportedCurve* curve = NULL;
  7088. if ((extension = TLSX_Find(ssl->extensions,
  7089. TLSX_SUPPORTED_GROUPS)) == NULL) {
  7090. if ((extension = TLSX_Find(ssl->ctx->extensions,
  7091. TLSX_SUPPORTED_GROUPS)) == NULL) {
  7092. return 0;
  7093. }
  7094. }
  7095. for (curve = (SupportedCurve*)extension->data; curve; curve = curve->next) {
  7096. if (curve->name == name)
  7097. return 1;
  7098. }
  7099. #endif
  7100. (void)ssl;
  7101. (void)name;
  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. static 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. int offset = 0;
  7121. word16 len;
  7122. TLSX* extension;
  7123. /* Add a KeyShare extension if it doesn't exist. */
  7124. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7125. if (extension == NULL) {
  7126. /* Push new KeyShare extension. */
  7127. ret = TLSX_Push(&ssl->extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  7128. if (ret != 0)
  7129. return ret;
  7130. }
  7131. if (length < OPAQUE16_LEN)
  7132. return BUFFER_ERROR;
  7133. /* ClientHello contains zero or more key share entries. */
  7134. ato16(input, &len);
  7135. if (len != length - OPAQUE16_LEN)
  7136. return BUFFER_ERROR;
  7137. offset += OPAQUE16_LEN;
  7138. while (offset < (int)length) {
  7139. ret = TLSX_KeyShareEntry_Parse(ssl, &input[offset],
  7140. length - (word16)offset,
  7141. &keyShareEntry);
  7142. if (ret < 0)
  7143. return ret;
  7144. offset += ret;
  7145. }
  7146. ret = 0;
  7147. }
  7148. else if (msgType == server_hello) {
  7149. int len;
  7150. if (length < OPAQUE16_LEN)
  7151. return BUFFER_ERROR;
  7152. /* The data is the named group the server wants to use. */
  7153. ato16(input, &group);
  7154. /* Check the selected group was supported by ClientHello extensions. */
  7155. if (!TLSX_SupportedGroups_Find(ssl, group)) {
  7156. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7157. return BAD_KEY_SHARE_DATA;
  7158. }
  7159. /* Check if the group was sent. */
  7160. if (!TLSX_KeyShare_Find(ssl, group)) {
  7161. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7162. return BAD_KEY_SHARE_DATA;
  7163. }
  7164. /* ServerHello contains one key share entry. */
  7165. len = TLSX_KeyShareEntry_Parse(ssl, input, length, &keyShareEntry);
  7166. if (len != (int)length)
  7167. return BUFFER_ERROR;
  7168. /* Not in list sent if there isn't a private key. */
  7169. if (keyShareEntry == NULL || (keyShareEntry->key == NULL
  7170. #if !defined(NO_DH) || defined(HAVE_PQC)
  7171. && keyShareEntry->privKey == NULL
  7172. #endif
  7173. )) {
  7174. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7175. return BAD_KEY_SHARE_DATA;
  7176. }
  7177. /* Process the entry to calculate the secret. */
  7178. ret = TLSX_KeyShare_Process(ssl, keyShareEntry);
  7179. if (ret == 0)
  7180. ssl->session->namedGroup = ssl->namedGroup = group;
  7181. }
  7182. else if (msgType == hello_retry_request) {
  7183. if (length != OPAQUE16_LEN)
  7184. return BUFFER_ERROR;
  7185. /* The data is the named group the server wants to use. */
  7186. ato16(input, &group);
  7187. #ifdef WOLFSSL_ASYNC_CRYPT
  7188. /* only perform find and clear TLSX if not returning from async */
  7189. if (ssl->error != WC_PENDING_E)
  7190. #endif
  7191. {
  7192. /* Check the selected group was supported by ClientHello extensions. */
  7193. if (!TLSX_SupportedGroups_Find(ssl, group)) {
  7194. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7195. return BAD_KEY_SHARE_DATA;
  7196. }
  7197. /* Check if the group was sent. */
  7198. if (TLSX_KeyShare_Find(ssl, group)) {
  7199. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7200. return BAD_KEY_SHARE_DATA;
  7201. }
  7202. /* Clear out unusable key shares. */
  7203. ret = TLSX_KeyShare_Empty(ssl);
  7204. if (ret != 0)
  7205. return ret;
  7206. }
  7207. #ifdef HAVE_PQC
  7208. /* For post-quantum groups, do this in TLSX_PopulateExtensions(). */
  7209. if (!WOLFSSL_NAMED_GROUP_IS_PQC(group))
  7210. #endif
  7211. ret = TLSX_KeyShare_Use(ssl, group, 0, NULL, NULL);
  7212. }
  7213. else {
  7214. /* Not a message type that is allowed to have this extension. */
  7215. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  7216. return SANITY_MSG_E;
  7217. }
  7218. return ret;
  7219. }
  7220. /* Create a new key share entry and put it into the list.
  7221. *
  7222. * list The linked list of key share entries.
  7223. * group The named group.
  7224. * heap The memory to allocate with.
  7225. * keyShareEntry The new key share entry object.
  7226. * returns 0 on success and other values indicate failure.
  7227. */
  7228. static int TLSX_KeyShare_New(KeyShareEntry** list, int group, void *heap,
  7229. KeyShareEntry** keyShareEntry)
  7230. {
  7231. KeyShareEntry* kse;
  7232. KeyShareEntry** next;
  7233. kse = (KeyShareEntry*)XMALLOC(sizeof(KeyShareEntry), heap,
  7234. DYNAMIC_TYPE_TLSX);
  7235. if (kse == NULL)
  7236. return MEMORY_E;
  7237. XMEMSET(kse, 0, sizeof(*kse));
  7238. kse->group = (word16)group;
  7239. /* Add it to the back and maintain the links. */
  7240. while (*list != NULL) {
  7241. /* Assign to temporary to work around compiler bug found by customer. */
  7242. next = &((*list)->next);
  7243. list = next;
  7244. }
  7245. *list = kse;
  7246. *keyShareEntry = kse;
  7247. (void)heap;
  7248. return 0;
  7249. }
  7250. #ifdef HAVE_PQC
  7251. #ifdef HAVE_LIBOQS
  7252. static int server_generate_pqc_ciphertext(WOLFSSL* ssl,
  7253. KeyShareEntry* keyShareEntry,
  7254. byte* data, word16 len) {
  7255. /* I am the server. The data parameter is the client's public key. I need
  7256. * to generate the public information (AKA ciphertext) and shared secret
  7257. * here. Note the "public information" is equivalent to a the public key in
  7258. * key exchange parlance. That's why it is being assigned to pubKey.
  7259. */
  7260. const char* algName = NULL;
  7261. OQS_KEM* kem = NULL;
  7262. byte* sharedSecret = NULL;
  7263. byte* ciphertext = NULL;
  7264. int ret = 0;
  7265. int oqs_group = 0;
  7266. int ecc_group = 0;
  7267. KeyShareEntry *ecc_kse = NULL;
  7268. ecc_key eccpubkey;
  7269. word32 outlen = 0;
  7270. findEccPqc(&ecc_group, &oqs_group, keyShareEntry->group);
  7271. algName = OQS_ID2name(oqs_group);
  7272. if (algName == NULL) {
  7273. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  7274. return BAD_FUNC_ARG;
  7275. }
  7276. ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
  7277. if (ret != 0) {
  7278. WOLFSSL_MSG("Could not do ECC public key initialization.");
  7279. return MEMORY_E;
  7280. }
  7281. ecc_kse = (KeyShareEntry*)XMALLOC(sizeof(*ecc_kse), ssl->heap, DYNAMIC_TYPE_TLSX);
  7282. if (ecc_kse == NULL) {
  7283. WOLFSSL_MSG("ecc_kse memory allocation failure");
  7284. ret = MEMORY_ERROR;
  7285. }
  7286. if (ret == 0) {
  7287. XMEMSET(ecc_kse, 0, sizeof(*ecc_kse));
  7288. }
  7289. if (ret == 0 && ecc_group != 0) {
  7290. ecc_kse->group = ecc_group;
  7291. ret = TLSX_KeyShare_GenEccKey(ssl, ecc_kse);
  7292. if (ret != 0) {
  7293. /* No message, TLSX_KeyShare_GenEccKey() will do it. */
  7294. return ret;
  7295. }
  7296. ret = 0;
  7297. }
  7298. if (ret == 0) {
  7299. kem = OQS_KEM_new(algName);
  7300. if (kem == NULL) {
  7301. WOLFSSL_MSG("Error creating OQS KEM, ensure algorithm support "
  7302. "was enabled in liboqs.");
  7303. ret = MEMORY_E;
  7304. }
  7305. }
  7306. if (ret == 0 && len != kem->length_public_key + ecc_kse->pubKeyLen) {
  7307. WOLFSSL_MSG("Invalid public key.");
  7308. WOLFSSL_ERROR_VERBOSE(BAD_FUNC_ARG);
  7309. ret = BAD_FUNC_ARG;
  7310. }
  7311. if (ret == 0) {
  7312. sharedSecret = (byte*)XMALLOC(ecc_kse->keyLen +
  7313. kem->length_shared_secret,
  7314. ssl->heap, DYNAMIC_TYPE_TLSX);
  7315. ciphertext = (byte*)XMALLOC(ecc_kse->pubKeyLen + kem->length_ciphertext,
  7316. ssl->heap, DYNAMIC_TYPE_TLSX);
  7317. if (sharedSecret == NULL || ciphertext == NULL) {
  7318. WOLFSSL_MSG("Ciphertext/shared secret memory allocation failure.");
  7319. ret = MEMORY_E;
  7320. }
  7321. }
  7322. if (ecc_group != 0) {
  7323. if (ret == 0) {
  7324. /* Point is validated by import function. */
  7325. ret = wc_ecc_import_x963(data, len - (word32)kem->length_public_key,
  7326. &eccpubkey);
  7327. if (ret != 0) {
  7328. WOLFSSL_MSG("Bad ECC public key.");
  7329. }
  7330. }
  7331. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  7332. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  7333. !defined(HAVE_SELFTEST)
  7334. if (ret == 0) {
  7335. ret = wc_ecc_set_rng(ecc_kse->key, ssl->rng);
  7336. }
  7337. #endif
  7338. if (ret == 0) {
  7339. outlen = ecc_kse->keyLen;
  7340. PRIVATE_KEY_UNLOCK();
  7341. ret = wc_ecc_shared_secret(ecc_kse->key, &eccpubkey,
  7342. sharedSecret,
  7343. &outlen);
  7344. PRIVATE_KEY_LOCK();
  7345. if (outlen != ecc_kse->keyLen) {
  7346. WOLFSSL_MSG("Data length mismatch.");
  7347. ret = BAD_FUNC_ARG;
  7348. }
  7349. }
  7350. }
  7351. if (ret == 0 &&
  7352. OQS_KEM_encaps(kem, ciphertext + ecc_kse->pubKeyLen,
  7353. sharedSecret + outlen,
  7354. data + ecc_kse->pubKeyLen) != OQS_SUCCESS) {
  7355. WOLFSSL_MSG("OQS Encapsulation failure.");
  7356. ret = BAD_FUNC_ARG;
  7357. }
  7358. if (ret == 0) {
  7359. if (keyShareEntry->ke != NULL) {
  7360. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7361. }
  7362. keyShareEntry->ke = sharedSecret;
  7363. keyShareEntry->keLen = outlen + (word32)kem->length_shared_secret;
  7364. sharedSecret = NULL;
  7365. XMEMCPY(ciphertext, ecc_kse->pubKey, ecc_kse->pubKeyLen);
  7366. keyShareEntry->pubKey = ciphertext;
  7367. keyShareEntry->pubKeyLen = (word32)(ecc_kse->pubKeyLen +
  7368. kem->length_ciphertext);
  7369. ciphertext = NULL;
  7370. /* Set namedGroup so wolfSSL_get_curve_name() can function properly on
  7371. * the server side. */
  7372. ssl->namedGroup = keyShareEntry->group;
  7373. }
  7374. TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
  7375. if (sharedSecret != NULL)
  7376. XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_TLSX);
  7377. if (ciphertext != NULL)
  7378. XFREE(ciphertext, ssl->heap, DYNAMIC_TYPE_TLSX);
  7379. wc_ecc_free(&eccpubkey);
  7380. OQS_KEM_free(kem);
  7381. return ret;
  7382. }
  7383. #elif defined(HAVE_PQM4)
  7384. static int server_generate_pqc_ciphertext(WOLFSSL* ssl,
  7385. KeyShareEntry* keyShareEntry,
  7386. byte* data, word16 len) {
  7387. /* I am the server. The data parameter is the client's public key. I need
  7388. * to generate the public information (AKA ciphertext) and shared secret
  7389. * here. Note the "public information" is equivalent to a the public key in
  7390. * key exchange parlance. That's why it is being assigned to pubKey.
  7391. */
  7392. byte* sharedSecret = NULL;
  7393. byte* ciphertext = NULL;
  7394. int ret = 0;
  7395. int oqs_group = 0;
  7396. int ecc_group = 0;
  7397. KeyShareEntry *ecc_kse = NULL;
  7398. ecc_key eccpubkey;
  7399. word32 outlen = 0;
  7400. findEccPqc(&ecc_group, &oqs_group, keyShareEntry->group);
  7401. ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
  7402. if (ret != 0) {
  7403. WOLFSSL_MSG("Could not do ECC public key initialization.");
  7404. return MEMORY_E;
  7405. }
  7406. ecc_kse = (KeyShareEntry*)XMALLOC(sizeof(*ecc_kse), ssl->heap, DYNAMIC_TYPE_TLSX);
  7407. if (ecc_kse == NULL) {
  7408. WOLFSSL_MSG("ecc_kse memory allocation failure");
  7409. ret = MEMORY_ERROR;
  7410. }
  7411. if (ret == 0) {
  7412. XMEMSET(ecc_kse, 0, sizeof(*ecc_kse));
  7413. }
  7414. if (ret == 0 && ecc_group != 0) {
  7415. ecc_kse->group = ecc_group;
  7416. ret = TLSX_KeyShare_GenEccKey(ssl, ecc_kse);
  7417. if (ret != 0) {
  7418. /* No message, TLSX_KeyShare_GenEccKey() will do it. */
  7419. return ret;
  7420. }
  7421. ret = 0;
  7422. }
  7423. if (ret == 0 && len != PQM4_PUBLIC_KEY_LENGTH + ecc_kse->pubKeyLen) {
  7424. WOLFSSL_MSG("Invalid public key.");
  7425. ret = BAD_FUNC_ARG;
  7426. }
  7427. if (ret == 0) {
  7428. sharedSecret = (byte*)XMALLOC(ecc_kse->keyLen + PQM4_SHARED_SECRET_LENGTH,
  7429. ssl->heap, DYNAMIC_TYPE_TLSX);
  7430. ciphertext = (byte*)XMALLOC(ecc_kse->pubKeyLen + PQM4_CIPHERTEXT_LENGTH,
  7431. ssl->heap, DYNAMIC_TYPE_TLSX);
  7432. if (sharedSecret == NULL || ciphertext == NULL) {
  7433. WOLFSSL_MSG("Ciphertext/shared secret memory allocation failure.");
  7434. ret = MEMORY_E;
  7435. }
  7436. }
  7437. if (ecc_group != 0) {
  7438. if (ret == 0) {
  7439. /* Point is validated by import function. */
  7440. ret = wc_ecc_import_x963(data, len - PQM4_PUBLIC_KEY_LENGTH,
  7441. &eccpubkey);
  7442. if (ret != 0) {
  7443. WOLFSSL_MSG("Bad ECC public key.");
  7444. }
  7445. }
  7446. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  7447. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  7448. !defined(HAVE_SELFTEST)
  7449. if (ret == 0) {
  7450. ret = wc_ecc_set_rng(ecc_kse->key, ssl->rng);
  7451. }
  7452. #endif
  7453. if (ret == 0) {
  7454. outlen = ecc_kse->keyLen;
  7455. PRIVATE_KEY_UNLOCK();
  7456. ret = wc_ecc_shared_secret(ecc_kse->key, &eccpubkey,
  7457. sharedSecret,
  7458. &outlen);
  7459. PRIVATE_KEY_LOCK();
  7460. if (outlen != ecc_kse->keyLen) {
  7461. WOLFSSL_MSG("Data length mismatch.");
  7462. ret = BAD_FUNC_ARG;
  7463. }
  7464. }
  7465. }
  7466. if (ret == 0 &&
  7467. crypto_kem_enc(ciphertext + ecc_kse->pubKeyLen,
  7468. sharedSecret + outlen,
  7469. data + ecc_kse->pubKeyLen) != 0) {
  7470. WOLFSSL_MSG("PQM4 Encapsulation failure.");
  7471. ret = BAD_FUNC_ARG;
  7472. }
  7473. if (ret == 0) {
  7474. if (keyShareEntry->ke != NULL) {
  7475. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7476. }
  7477. keyShareEntry->ke = sharedSecret;
  7478. keyShareEntry->keLen = outlen + (word32)PQM4_SHARED_SECRET_LENGTH;
  7479. sharedSecret = NULL;
  7480. XMEMCPY(ciphertext, ecc_kse->pubKey, ecc_kse->pubKeyLen);
  7481. keyShareEntry->pubKey = ciphertext;
  7482. keyShareEntry->pubKeyLen = (word32)(ecc_kse->pubKeyLen +
  7483. PQM4_CIPHERTEXT_LENGTH);
  7484. ciphertext = NULL;
  7485. /* Set namedGroup so wolfSSL_get_curve_name() can function properly on
  7486. * the server side. */
  7487. ssl->namedGroup = keyShareEntry->group;
  7488. }
  7489. TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
  7490. if (sharedSecret != NULL)
  7491. XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_TLSX);
  7492. if (ciphertext != NULL)
  7493. XFREE(ciphertext, ssl->heap, DYNAMIC_TYPE_TLSX);
  7494. wc_ecc_free(&eccpubkey);
  7495. return ret;
  7496. }
  7497. #endif /* HAVE_PQM4 */
  7498. #endif /* HAVE_PQC */
  7499. /* Use the data to create a new key share object in the extensions.
  7500. *
  7501. * ssl The SSL/TLS object.
  7502. * group The named group.
  7503. * len The length of the public key data.
  7504. * data The public key data.
  7505. * kse The new key share entry object.
  7506. * returns 0 on success and other values indicate failure.
  7507. */
  7508. int TLSX_KeyShare_Use(WOLFSSL* ssl, word16 group, word16 len, byte* data,
  7509. KeyShareEntry **kse)
  7510. {
  7511. int ret = 0;
  7512. TLSX* extension;
  7513. KeyShareEntry* keyShareEntry = NULL;
  7514. /* Find the KeyShare extension if it exists. */
  7515. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7516. if (extension == NULL) {
  7517. /* Push new KeyShare extension. */
  7518. ret = TLSX_Push(&ssl->extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  7519. if (ret != 0)
  7520. return ret;
  7521. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7522. if (extension == NULL)
  7523. return MEMORY_E;
  7524. }
  7525. extension->resp = 0;
  7526. /* Try to find the key share entry with this group. */
  7527. keyShareEntry = (KeyShareEntry*)extension->data;
  7528. while (keyShareEntry != NULL) {
  7529. if (keyShareEntry->group == group)
  7530. break;
  7531. keyShareEntry = keyShareEntry->next;
  7532. }
  7533. /* Create a new key share entry if not found. */
  7534. if (keyShareEntry == NULL) {
  7535. ret = TLSX_KeyShare_New((KeyShareEntry**)&extension->data, group,
  7536. ssl->heap, &keyShareEntry);
  7537. if (ret != 0)
  7538. return ret;
  7539. }
  7540. #ifdef HAVE_PQC
  7541. if (WOLFSSL_NAMED_GROUP_IS_PQC(group) &&
  7542. ssl->options.side == WOLFSSL_SERVER_END) {
  7543. ret = server_generate_pqc_ciphertext(ssl, keyShareEntry, data,
  7544. len);
  7545. if (ret != 0)
  7546. return ret;
  7547. }
  7548. else
  7549. #endif
  7550. if (data != NULL) {
  7551. if (keyShareEntry->ke != NULL) {
  7552. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7553. }
  7554. keyShareEntry->ke = data;
  7555. keyShareEntry->keLen = len;
  7556. }
  7557. else {
  7558. /* Generate a key pair. */
  7559. ret = TLSX_KeyShare_GenKey(ssl, keyShareEntry);
  7560. if (ret != 0)
  7561. return ret;
  7562. }
  7563. if (kse != NULL)
  7564. *kse = keyShareEntry;
  7565. return 0;
  7566. }
  7567. /* Set an empty Key Share extension.
  7568. *
  7569. * ssl The SSL/TLS object.
  7570. * returns 0 on success and other values indicate failure.
  7571. */
  7572. int TLSX_KeyShare_Empty(WOLFSSL* ssl)
  7573. {
  7574. int ret = 0;
  7575. TLSX* extension;
  7576. /* Find the KeyShare extension if it exists. */
  7577. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7578. if (extension == NULL) {
  7579. /* Push new KeyShare extension. */
  7580. ret = TLSX_Push(&ssl->extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  7581. }
  7582. else if (extension->data != NULL) {
  7583. TLSX_KeyShare_FreeAll((KeyShareEntry*)extension->data, ssl->heap);
  7584. extension->data = NULL;
  7585. }
  7586. return ret;
  7587. }
  7588. /* Returns whether this group is supported.
  7589. *
  7590. * namedGroup The named group to check.
  7591. * returns 1 when supported or 0 otherwise.
  7592. */
  7593. static int TLSX_KeyShare_IsSupported(int namedGroup)
  7594. {
  7595. switch (namedGroup) {
  7596. #ifdef HAVE_FFDHE_2048
  7597. case WOLFSSL_FFDHE_2048:
  7598. break;
  7599. #endif
  7600. #ifdef HAVE_FFDHE_3072
  7601. case WOLFSSL_FFDHE_3072:
  7602. break;
  7603. #endif
  7604. #ifdef HAVE_FFDHE_4096
  7605. case WOLFSSL_FFDHE_4096:
  7606. break;
  7607. #endif
  7608. #ifdef HAVE_FFDHE_6144
  7609. case WOLFSSL_FFDHE_6144:
  7610. break;
  7611. #endif
  7612. #ifdef HAVE_FFDHE_8192
  7613. case WOLFSSL_FFDHE_8192:
  7614. break;
  7615. #endif
  7616. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  7617. #ifdef HAVE_ECC_KOBLITZ
  7618. case WOLFSSL_ECC_SECP256K1:
  7619. break;
  7620. #endif
  7621. #ifndef NO_ECC_SECP
  7622. case WOLFSSL_ECC_SECP256R1:
  7623. break;
  7624. #endif /* !NO_ECC_SECP */
  7625. #ifdef HAVE_ECC_BRAINPOOL
  7626. case WOLFSSL_ECC_BRAINPOOLP256R1:
  7627. break;
  7628. #endif
  7629. #endif
  7630. #if defined(HAVE_CURVE25519) && ECC_MIN_KEY_SZ <= 256
  7631. case WOLFSSL_ECC_X25519:
  7632. break;
  7633. #endif
  7634. #if defined(HAVE_CURVE448) && ECC_MIN_KEY_SZ <= 448
  7635. case WOLFSSL_ECC_X448:
  7636. break;
  7637. #endif
  7638. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  7639. #ifndef NO_ECC_SECP
  7640. case WOLFSSL_ECC_SECP384R1:
  7641. break;
  7642. #endif /* !NO_ECC_SECP */
  7643. #ifdef HAVE_ECC_BRAINPOOL
  7644. case WOLFSSL_ECC_BRAINPOOLP384R1:
  7645. break;
  7646. #endif
  7647. #endif
  7648. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  7649. #ifndef NO_ECC_SECP
  7650. case WOLFSSL_ECC_SECP521R1:
  7651. break;
  7652. #endif /* !NO_ECC_SECP */
  7653. #endif
  7654. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  7655. #ifdef HAVE_ECC_KOBLITZ
  7656. case WOLFSSL_ECC_SECP160K1:
  7657. break;
  7658. #endif
  7659. #ifndef NO_ECC_SECP
  7660. case WOLFSSL_ECC_SECP160R1:
  7661. break;
  7662. #endif
  7663. #ifdef HAVE_ECC_SECPR2
  7664. case WOLFSSL_ECC_SECP160R2:
  7665. break;
  7666. #endif
  7667. #endif
  7668. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  7669. #ifdef HAVE_ECC_KOBLITZ
  7670. case WOLFSSL_ECC_SECP192K1:
  7671. break;
  7672. #endif
  7673. #ifndef NO_ECC_SECP
  7674. case WOLFSSL_ECC_SECP192R1:
  7675. break;
  7676. #endif
  7677. #endif
  7678. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  7679. #ifdef HAVE_ECC_KOBLITZ
  7680. case WOLFSSL_ECC_SECP224K1:
  7681. break;
  7682. #endif
  7683. #ifndef NO_ECC_SECP
  7684. case WOLFSSL_ECC_SECP224R1:
  7685. break;
  7686. #endif
  7687. #endif
  7688. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  7689. #ifdef HAVE_ECC_BRAINPOOL
  7690. case WOLFSSL_ECC_BRAINPOOLP512R1:
  7691. break;
  7692. #endif
  7693. #endif
  7694. #ifdef HAVE_PQC
  7695. #ifdef HAVE_LIBOQS
  7696. case WOLFSSL_KYBER_LEVEL1:
  7697. case WOLFSSL_KYBER_LEVEL3:
  7698. case WOLFSSL_KYBER_LEVEL5:
  7699. case WOLFSSL_NTRU_HPS_LEVEL1:
  7700. case WOLFSSL_NTRU_HPS_LEVEL3:
  7701. case WOLFSSL_NTRU_HPS_LEVEL5:
  7702. case WOLFSSL_NTRU_HRSS_LEVEL3:
  7703. case WOLFSSL_SABER_LEVEL1:
  7704. case WOLFSSL_SABER_LEVEL3:
  7705. case WOLFSSL_SABER_LEVEL5:
  7706. case WOLFSSL_KYBER_90S_LEVEL1:
  7707. case WOLFSSL_KYBER_90S_LEVEL3:
  7708. case WOLFSSL_KYBER_90S_LEVEL5:
  7709. case WOLFSSL_P256_NTRU_HPS_LEVEL1:
  7710. case WOLFSSL_P384_NTRU_HPS_LEVEL3:
  7711. case WOLFSSL_P521_NTRU_HPS_LEVEL5:
  7712. case WOLFSSL_P384_NTRU_HRSS_LEVEL3:
  7713. case WOLFSSL_P256_SABER_LEVEL1:
  7714. case WOLFSSL_P384_SABER_LEVEL3:
  7715. case WOLFSSL_P521_SABER_LEVEL5:
  7716. case WOLFSSL_P256_KYBER_LEVEL1:
  7717. case WOLFSSL_P384_KYBER_LEVEL3:
  7718. case WOLFSSL_P521_KYBER_LEVEL5:
  7719. case WOLFSSL_P256_KYBER_90S_LEVEL1:
  7720. case WOLFSSL_P384_KYBER_90S_LEVEL3:
  7721. case WOLFSSL_P521_KYBER_90S_LEVEL5:
  7722. findEccPqc(NULL, &namedGroup, namedGroup);
  7723. if (! OQS_KEM_alg_is_enabled(OQS_ID2name(namedGroup))) {
  7724. return 0;
  7725. }
  7726. break;
  7727. #elif defined(HAVE_PQM4)
  7728. case WOLFSSL_KYBER_LEVEL1:
  7729. break;
  7730. #endif
  7731. #endif /* HAVE_PQC */
  7732. default:
  7733. return 0;
  7734. }
  7735. return 1;
  7736. }
  7737. /* Examines the application specified group ranking and returns the rank of the
  7738. * group.
  7739. * If no group ranking set then all groups are rank 0 (highest).
  7740. *
  7741. * ssl The SSL/TLS object.
  7742. * group The group to check ranking for.
  7743. * returns ranking from 0 to MAX_GROUP_COUNT-1 or -1 when group not in list.
  7744. */
  7745. static int TLSX_KeyShare_GroupRank(WOLFSSL* ssl, int group)
  7746. {
  7747. byte i;
  7748. if (ssl->numGroups == 0) {
  7749. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  7750. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  7751. #ifndef NO_ECC_SECP
  7752. ssl->group[ssl->numGroups++] = WOLFSSL_ECC_SECP256R1;
  7753. #endif
  7754. #endif
  7755. #endif
  7756. #ifndef HAVE_FIPS
  7757. #if defined(HAVE_CURVE25519) && ECC_MIN_KEY_SZ <= 256
  7758. ssl->group[ssl->numGroups++] = WOLFSSL_ECC_X25519;
  7759. #endif
  7760. #endif
  7761. #ifndef HAVE_FIPS
  7762. #if defined(HAVE_CURVE448) && ECC_MIN_KEY_SZ <= 448
  7763. ssl->group[ssl->numGroups++] = WOLFSSL_ECC_X448;
  7764. #endif
  7765. #endif
  7766. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  7767. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  7768. #ifndef NO_ECC_SECP
  7769. ssl->group[ssl->numGroups++] = WOLFSSL_ECC_SECP384R1;
  7770. #endif
  7771. #endif
  7772. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  7773. #ifndef NO_ECC_SECP
  7774. ssl->group[ssl->numGroups++] = WOLFSSL_ECC_SECP521R1;
  7775. #endif
  7776. #endif
  7777. #endif
  7778. /* Add FFDHE supported groups. */
  7779. #ifdef HAVE_FFDHE_2048
  7780. ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_2048;
  7781. #endif
  7782. #ifdef HAVE_FFDHE_3072
  7783. ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_3072;
  7784. #endif
  7785. #ifdef HAVE_FFDHE_4096
  7786. ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_4096;
  7787. #endif
  7788. #ifdef HAVE_FFDHE_6144
  7789. ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_6144;
  7790. #endif
  7791. #ifdef HAVE_FFDHE_8192
  7792. ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_8192;
  7793. #endif
  7794. #ifdef HAVE_PQC
  7795. /* For the liboqs groups we need to do a runtime check because
  7796. * liboqs could be compiled to make an algorithm unavailable.
  7797. */
  7798. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_LEVEL1))
  7799. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_LEVEL1;
  7800. #ifdef HAVE_LIBOQS
  7801. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_LEVEL3))
  7802. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_LEVEL3;
  7803. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_LEVEL5))
  7804. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_LEVEL5;
  7805. if (TLSX_KeyShare_IsSupported(WOLFSSL_NTRU_HPS_LEVEL1))
  7806. ssl->group[ssl->numGroups++] = WOLFSSL_NTRU_HPS_LEVEL1;
  7807. if (TLSX_KeyShare_IsSupported(WOLFSSL_NTRU_HPS_LEVEL3))
  7808. ssl->group[ssl->numGroups++] = WOLFSSL_NTRU_HPS_LEVEL3;
  7809. if (TLSX_KeyShare_IsSupported(WOLFSSL_NTRU_HPS_LEVEL5))
  7810. ssl->group[ssl->numGroups++] = WOLFSSL_NTRU_HPS_LEVEL5;
  7811. if (TLSX_KeyShare_IsSupported(WOLFSSL_NTRU_HRSS_LEVEL3))
  7812. ssl->group[ssl->numGroups++] = WOLFSSL_NTRU_HRSS_LEVEL3;
  7813. if (TLSX_KeyShare_IsSupported(WOLFSSL_SABER_LEVEL1))
  7814. ssl->group[ssl->numGroups++] = WOLFSSL_SABER_LEVEL1;
  7815. if (TLSX_KeyShare_IsSupported(WOLFSSL_SABER_LEVEL3))
  7816. ssl->group[ssl->numGroups++] = WOLFSSL_SABER_LEVEL3;
  7817. if (TLSX_KeyShare_IsSupported(WOLFSSL_SABER_LEVEL5))
  7818. ssl->group[ssl->numGroups++] = WOLFSSL_SABER_LEVEL5;
  7819. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_90S_LEVEL1))
  7820. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_90S_LEVEL1;
  7821. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_90S_LEVEL3))
  7822. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_90S_LEVEL3;
  7823. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_90S_LEVEL5))
  7824. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_90S_LEVEL5;
  7825. if (TLSX_KeyShare_IsSupported(WOLFSSL_P256_NTRU_HPS_LEVEL1))
  7826. ssl->group[ssl->numGroups++] = WOLFSSL_P256_NTRU_HPS_LEVEL1;
  7827. if (TLSX_KeyShare_IsSupported(WOLFSSL_P384_NTRU_HPS_LEVEL3))
  7828. ssl->group[ssl->numGroups++] = WOLFSSL_P384_NTRU_HPS_LEVEL3;
  7829. if (TLSX_KeyShare_IsSupported(WOLFSSL_P521_NTRU_HPS_LEVEL5))
  7830. ssl->group[ssl->numGroups++] = WOLFSSL_P521_NTRU_HPS_LEVEL5;
  7831. if (TLSX_KeyShare_IsSupported(WOLFSSL_P384_NTRU_HRSS_LEVEL3))
  7832. ssl->group[ssl->numGroups++] = WOLFSSL_P384_NTRU_HRSS_LEVEL3;
  7833. if (TLSX_KeyShare_IsSupported(WOLFSSL_P256_SABER_LEVEL1))
  7834. ssl->group[ssl->numGroups++] = WOLFSSL_P256_SABER_LEVEL1;
  7835. if (TLSX_KeyShare_IsSupported(WOLFSSL_P384_SABER_LEVEL3))
  7836. ssl->group[ssl->numGroups++] = WOLFSSL_P384_SABER_LEVEL3;
  7837. if (TLSX_KeyShare_IsSupported(WOLFSSL_P521_SABER_LEVEL5))
  7838. ssl->group[ssl->numGroups++] = WOLFSSL_P521_SABER_LEVEL5;
  7839. if (TLSX_KeyShare_IsSupported(WOLFSSL_P256_KYBER_LEVEL1))
  7840. ssl->group[ssl->numGroups++] = WOLFSSL_P256_KYBER_LEVEL1;
  7841. if (TLSX_KeyShare_IsSupported(WOLFSSL_P384_KYBER_LEVEL3))
  7842. ssl->group[ssl->numGroups++] = WOLFSSL_P384_KYBER_LEVEL3;
  7843. if (TLSX_KeyShare_IsSupported(WOLFSSL_P521_KYBER_LEVEL5))
  7844. ssl->group[ssl->numGroups++] = WOLFSSL_P521_KYBER_LEVEL5;
  7845. if (TLSX_KeyShare_IsSupported(WOLFSSL_P256_KYBER_90S_LEVEL1))
  7846. ssl->group[ssl->numGroups++] = WOLFSSL_P256_KYBER_90S_LEVEL1;
  7847. if (TLSX_KeyShare_IsSupported(WOLFSSL_P384_KYBER_90S_LEVEL3))
  7848. ssl->group[ssl->numGroups++] = WOLFSSL_P384_KYBER_90S_LEVEL3;
  7849. if (TLSX_KeyShare_IsSupported(WOLFSSL_P521_KYBER_90S_LEVEL5))
  7850. ssl->group[ssl->numGroups++] = WOLFSSL_P521_KYBER_90S_LEVEL5;
  7851. #endif /* HAVE_LIBOQS */
  7852. #endif /* HAVE_PQC */
  7853. }
  7854. for (i = 0; i < ssl->numGroups; i++)
  7855. if (ssl->group[i] == (word16)group)
  7856. return i;
  7857. return -1;
  7858. }
  7859. /* Set a key share that is supported by the client into extensions.
  7860. *
  7861. * ssl The SSL/TLS object.
  7862. * returns BAD_KEY_SHARE_DATA if no supported group has a key share,
  7863. * 0 if a supported group has a key share and other values indicate an error.
  7864. */
  7865. static int TLSX_KeyShare_SetSupported(WOLFSSL* ssl)
  7866. {
  7867. int ret;
  7868. #ifdef HAVE_SUPPORTED_CURVES
  7869. TLSX* extension;
  7870. SupportedCurve* curve = NULL;
  7871. SupportedCurve* preferredCurve = NULL;
  7872. int preferredRank = WOLFSSL_MAX_GROUP_COUNT;
  7873. int rank;
  7874. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  7875. if (extension != NULL)
  7876. curve = (SupportedCurve*)extension->data;
  7877. /* Use server's preference order. */
  7878. for (; curve != NULL; curve = curve->next) {
  7879. if (!TLSX_KeyShare_IsSupported(curve->name))
  7880. continue;
  7881. if (wolfSSL_curve_is_disabled(ssl, curve->name))
  7882. continue;
  7883. rank = TLSX_KeyShare_GroupRank(ssl, curve->name);
  7884. if (rank == -1)
  7885. continue;
  7886. if (rank < preferredRank) {
  7887. preferredCurve = curve;
  7888. preferredRank = rank;
  7889. }
  7890. }
  7891. curve = preferredCurve;
  7892. if (curve == NULL) {
  7893. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7894. return BAD_KEY_SHARE_DATA;
  7895. }
  7896. /* Delete the old key share data list. */
  7897. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7898. if (extension != NULL) {
  7899. KeyShareEntry* kse = (KeyShareEntry*)extension->data;
  7900. #ifdef WOLFSSL_ASYNC_CRYPT
  7901. /* for async don't free, call `TLSX_KeyShare_Use` again */
  7902. if (kse && kse->lastRet != WC_PENDING_E)
  7903. #endif
  7904. {
  7905. TLSX_KeyShare_FreeAll(kse, ssl->heap);
  7906. extension->data = NULL;
  7907. }
  7908. }
  7909. /* Add in the chosen group. */
  7910. ret = TLSX_KeyShare_Use(ssl, curve->name, 0, NULL, NULL);
  7911. if (ret != 0 && ret != WC_PENDING_E)
  7912. return ret;
  7913. /* Set extension to be in response. */
  7914. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7915. extension->resp = 1;
  7916. #else
  7917. (void)ssl;
  7918. WOLFSSL_ERROR_VERBOSE(NOT_COMPILED_IN);
  7919. ret = NOT_COMPILED_IN;
  7920. #endif
  7921. return ret;
  7922. }
  7923. /* Ensure there is a key pair that can be used for key exchange.
  7924. *
  7925. * ssl The SSL/TLS object.
  7926. * doHelloRetry If set to non-zero will do hello_retry
  7927. * returns 0 on success and other values indicate failure.
  7928. */
  7929. int TLSX_KeyShare_Establish(WOLFSSL *ssl, int* doHelloRetry)
  7930. {
  7931. int ret;
  7932. TLSX* extension;
  7933. KeyShareEntry* clientKSE = NULL;
  7934. KeyShareEntry* serverKSE;
  7935. KeyShareEntry* list = NULL;
  7936. KeyShareEntry* preferredKSE = NULL;
  7937. int preferredRank = WOLFSSL_MAX_GROUP_COUNT;
  7938. int rank;
  7939. /* Find the KeyShare extension if it exists. */
  7940. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7941. if (extension != NULL)
  7942. list = (KeyShareEntry*)extension->data;
  7943. if (extension && extension->resp == 1) {
  7944. ret = 0;
  7945. #ifdef WOLFSSL_ASYNC_CRYPT
  7946. /* in async case make sure key generation is finalized */
  7947. serverKSE = (KeyShareEntry*)extension->data;
  7948. if (serverKSE->lastRet == WC_PENDING_E) {
  7949. if (ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE)
  7950. *doHelloRetry = 1;
  7951. ret = TLSX_KeyShare_GenKey(ssl, serverKSE);
  7952. }
  7953. #endif
  7954. return ret;
  7955. }
  7956. /* Use server's preference order. */
  7957. for (clientKSE = list; clientKSE != NULL; clientKSE = clientKSE->next) {
  7958. if (clientKSE->ke == NULL)
  7959. continue;
  7960. /* Check consistency now - extensions in any order. */
  7961. if (!TLSX_SupportedGroups_Find(ssl, clientKSE->group))
  7962. continue;
  7963. if (!WOLFSSL_NAMED_GROUP_IS_FFHDE(clientKSE->group)) {
  7964. /* Check max value supported. */
  7965. if (clientKSE->group > WOLFSSL_ECC_MAX) {
  7966. #ifdef HAVE_PQC
  7967. if (!WOLFSSL_NAMED_GROUP_IS_PQC(clientKSE->group))
  7968. #endif
  7969. continue;
  7970. }
  7971. if (wolfSSL_curve_is_disabled(ssl, clientKSE->group))
  7972. continue;
  7973. }
  7974. if (!TLSX_KeyShare_IsSupported(clientKSE->group))
  7975. continue;
  7976. rank = TLSX_KeyShare_GroupRank(ssl, clientKSE->group);
  7977. if (rank == -1)
  7978. continue;
  7979. if (rank < preferredRank) {
  7980. preferredKSE = clientKSE;
  7981. preferredRank = rank;
  7982. }
  7983. }
  7984. clientKSE = preferredKSE;
  7985. /* No supported group found - send HelloRetryRequest. */
  7986. if (clientKSE == NULL) {
  7987. /* Set KEY_SHARE_ERROR to indicate HelloRetryRequest required. */
  7988. *doHelloRetry = 1;
  7989. return TLSX_KeyShare_SetSupported(ssl);
  7990. }
  7991. list = NULL;
  7992. /* Generate a new key pair except in the case of OQS KEM because we
  7993. * are going to encapsulate and that does not require us to generate a
  7994. * key pair.
  7995. */
  7996. ret = TLSX_KeyShare_New(&list, clientKSE->group, ssl->heap, &serverKSE);
  7997. if (ret != 0)
  7998. return ret;
  7999. if (clientKSE->key == NULL) {
  8000. #ifdef HAVE_PQC
  8001. if (WOLFSSL_NAMED_GROUP_IS_PQC(clientKSE->group)) {
  8002. /* Going to need the public key (AKA ciphertext). */
  8003. serverKSE->pubKey = clientKSE->pubKey;
  8004. clientKSE->pubKey = NULL;
  8005. serverKSE->pubKeyLen = clientKSE->pubKeyLen;
  8006. clientKSE->pubKeyLen = 0;
  8007. }
  8008. else
  8009. #endif
  8010. {
  8011. ret = TLSX_KeyShare_GenKey(ssl, serverKSE);
  8012. }
  8013. /* for async do setup of serverKSE below, but return WC_PENDING_E */
  8014. if (ret != 0
  8015. #ifdef WOLFSSL_ASYNC_CRYPT
  8016. && ret != WC_PENDING_E
  8017. #endif
  8018. ) {
  8019. return ret;
  8020. }
  8021. }
  8022. else {
  8023. /* transfer buffers to serverKSE */
  8024. serverKSE->key = clientKSE->key;
  8025. clientKSE->key = NULL;
  8026. serverKSE->keyLen = clientKSE->keyLen;
  8027. serverKSE->pubKey = clientKSE->pubKey;
  8028. clientKSE->pubKey = NULL;
  8029. serverKSE->pubKeyLen = clientKSE->pubKeyLen;
  8030. #ifndef NO_DH
  8031. serverKSE->privKey = clientKSE->privKey;
  8032. clientKSE->privKey = NULL;
  8033. #endif
  8034. }
  8035. serverKSE->ke = clientKSE->ke;
  8036. serverKSE->keLen = clientKSE->keLen;
  8037. clientKSE->ke = NULL;
  8038. clientKSE->keLen = 0;
  8039. TLSX_KeyShare_FreeAll((KeyShareEntry*)extension->data, ssl->heap);
  8040. extension->data = (void *)serverKSE;
  8041. extension->resp = 1;
  8042. return ret;
  8043. }
  8044. /* Derive the shared secret of the key exchange.
  8045. *
  8046. * ssl The SSL/TLS object.
  8047. * returns 0 on success and other values indicate failure.
  8048. */
  8049. int TLSX_KeyShare_DeriveSecret(WOLFSSL *ssl)
  8050. {
  8051. int ret;
  8052. TLSX* extension;
  8053. KeyShareEntry* list = NULL;
  8054. #ifdef WOLFSSL_ASYNC_CRYPT
  8055. ret = wolfSSL_AsyncPop(ssl, NULL);
  8056. /* Check for error */
  8057. if (ret != WC_NOT_PENDING_E && ret < 0) {
  8058. return ret;
  8059. }
  8060. #endif
  8061. /* Find the KeyShare extension if it exists. */
  8062. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  8063. if (extension != NULL)
  8064. list = (KeyShareEntry*)extension->data;
  8065. if (list == NULL)
  8066. return KEY_SHARE_ERROR;
  8067. /* Calculate secret. */
  8068. ret = TLSX_KeyShare_Process(ssl, list);
  8069. return ret;
  8070. }
  8071. #define KS_FREE_ALL TLSX_KeyShare_FreeAll
  8072. #define KS_GET_SIZE TLSX_KeyShare_GetSize
  8073. #define KS_WRITE TLSX_KeyShare_Write
  8074. #define KS_PARSE TLSX_KeyShare_Parse
  8075. #else
  8076. #define KS_FREE_ALL(a, b)
  8077. #define KS_GET_SIZE(a, b) 0
  8078. #define KS_WRITE(a, b, c) 0
  8079. #define KS_PARSE(a, b, c, d) 0
  8080. #endif /* WOLFSSL_TLS13 */
  8081. /******************************************************************************/
  8082. /* Pre-Shared Key */
  8083. /******************************************************************************/
  8084. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  8085. /* Free the pre-shared key dynamic data.
  8086. *
  8087. * list The linked list of key share entry objects.
  8088. * heap The heap used for allocation.
  8089. */
  8090. static void TLSX_PreSharedKey_FreeAll(PreSharedKey* list, void* heap)
  8091. {
  8092. PreSharedKey* current;
  8093. while ((current = list) != NULL) {
  8094. list = current->next;
  8095. XFREE(current->identity, heap, DYNAMIC_TYPE_TLSX);
  8096. XFREE(current, heap, DYNAMIC_TYPE_TLSX);
  8097. }
  8098. (void)heap;
  8099. }
  8100. /* Get the size of the encoded pre shared key extension.
  8101. *
  8102. * list The linked list of pre-shared key extensions.
  8103. * msgType The type of the message this extension is being written into.
  8104. * returns the number of bytes of the encoded pre-shared key extension or
  8105. * SANITY_MSG_E to indicate invalid message type.
  8106. */
  8107. static int TLSX_PreSharedKey_GetSize(PreSharedKey* list, byte msgType,
  8108. word16* pSz)
  8109. {
  8110. if (msgType == client_hello) {
  8111. /* Length of identities + Length of binders. */
  8112. word16 len = OPAQUE16_LEN + OPAQUE16_LEN;
  8113. while (list != NULL) {
  8114. /* Each entry has: identity, ticket age and binder. */
  8115. len += OPAQUE16_LEN + list->identityLen + OPAQUE32_LEN +
  8116. OPAQUE8_LEN + (word16)list->binderLen;
  8117. list = list->next;
  8118. }
  8119. *pSz += len;
  8120. return 0;
  8121. }
  8122. if (msgType == server_hello) {
  8123. *pSz += OPAQUE16_LEN;
  8124. return 0;
  8125. }
  8126. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8127. return SANITY_MSG_E;
  8128. }
  8129. /* The number of bytes to be written for the binders.
  8130. *
  8131. * list The linked list of pre-shared key extensions.
  8132. * msgType The type of the message this extension is being written into.
  8133. * returns the number of bytes of the encoded pre-shared key extension or
  8134. * SANITY_MSG_E to indicate invalid message type.
  8135. */
  8136. int TLSX_PreSharedKey_GetSizeBinders(PreSharedKey* list, byte msgType,
  8137. word16* pSz)
  8138. {
  8139. word16 len;
  8140. if (msgType != client_hello) {
  8141. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8142. return SANITY_MSG_E;
  8143. }
  8144. /* Length of all binders. */
  8145. len = OPAQUE16_LEN;
  8146. while (list != NULL) {
  8147. len += OPAQUE8_LEN + (word16)list->binderLen;
  8148. list = list->next;
  8149. }
  8150. *pSz = len;
  8151. return 0;
  8152. }
  8153. /* Writes the pre-shared key extension into the output buffer - binders only.
  8154. * Assumes that the the output buffer is big enough to hold data.
  8155. *
  8156. * list The linked list of key share entries.
  8157. * output The buffer to write into.
  8158. * msgType The type of the message this extension is being written into.
  8159. * returns the number of bytes written into the buffer.
  8160. */
  8161. int TLSX_PreSharedKey_WriteBinders(PreSharedKey* list, byte* output,
  8162. byte msgType, word16* pSz)
  8163. {
  8164. PreSharedKey* current = list;
  8165. word16 idx = 0;
  8166. word16 lenIdx;
  8167. word16 len;
  8168. if (msgType != client_hello) {
  8169. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8170. return SANITY_MSG_E;
  8171. }
  8172. /* Skip length of all binders. */
  8173. lenIdx = idx;
  8174. idx += OPAQUE16_LEN;
  8175. while (current != NULL) {
  8176. /* Binder data length. */
  8177. output[idx++] = (byte)current->binderLen;
  8178. /* Binder data. */
  8179. XMEMCPY(output + idx, current->binder, current->binderLen);
  8180. idx += (word16)current->binderLen;
  8181. current = current->next;
  8182. }
  8183. /* Length of the binders. */
  8184. len = idx - lenIdx - OPAQUE16_LEN;
  8185. c16toa(len, output + lenIdx);
  8186. *pSz = idx;
  8187. return 0;
  8188. }
  8189. /* Writes the pre-shared key extension into the output buffer.
  8190. * Assumes that the the output buffer is big enough to hold data.
  8191. *
  8192. * list The linked list of key share entries.
  8193. * output The buffer to write into.
  8194. * msgType The type of the message this extension is being written into.
  8195. * returns the number of bytes written into the buffer.
  8196. */
  8197. static int TLSX_PreSharedKey_Write(PreSharedKey* list, byte* output,
  8198. byte msgType, word16* pSz)
  8199. {
  8200. if (msgType == client_hello) {
  8201. PreSharedKey* current = list;
  8202. word16 idx = 0;
  8203. word16 lenIdx;
  8204. word16 len;
  8205. int ret;
  8206. /* Write identites only. Binders after HMACing over this. */
  8207. lenIdx = idx;
  8208. idx += OPAQUE16_LEN;
  8209. while (current != NULL) {
  8210. /* Identity length */
  8211. c16toa(current->identityLen, output + idx);
  8212. idx += OPAQUE16_LEN;
  8213. /* Identity data */
  8214. XMEMCPY(output + idx, current->identity, current->identityLen);
  8215. idx += current->identityLen;
  8216. /* Obfuscated ticket age. */
  8217. c32toa(current->ticketAge, output + idx);
  8218. idx += OPAQUE32_LEN;
  8219. current = current->next;
  8220. }
  8221. /* Length of the identites. */
  8222. len = idx - lenIdx - OPAQUE16_LEN;
  8223. c16toa(len, output + lenIdx);
  8224. /* Don't include binders here.
  8225. * The binders are based on the hash of all the ClientHello data up to
  8226. * and include the identities written above.
  8227. */
  8228. ret = TLSX_PreSharedKey_GetSizeBinders(list, msgType, &len);
  8229. if (ret < 0)
  8230. return ret;
  8231. *pSz += idx + len;
  8232. }
  8233. else if (msgType == server_hello) {
  8234. word16 i;
  8235. /* Find the index of the chosen identity. */
  8236. for (i=0; list != NULL && !list->chosen; i++)
  8237. list = list->next;
  8238. if (list == NULL) {
  8239. WOLFSSL_ERROR_VERBOSE(BUILD_MSG_ERROR);
  8240. return BUILD_MSG_ERROR;
  8241. }
  8242. /* The index of the identity chosen by the server from the list supplied
  8243. * by the client.
  8244. */
  8245. c16toa(i, output);
  8246. *pSz += OPAQUE16_LEN;
  8247. }
  8248. else {
  8249. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8250. return SANITY_MSG_E;
  8251. }
  8252. return 0;
  8253. }
  8254. /* Parse the pre-shared key extension.
  8255. * Different formats in different messages.
  8256. *
  8257. * ssl The SSL/TLS object.
  8258. * input The extension data.
  8259. * length The length of the extension data.
  8260. * msgType The type of the message this extension is being parsed from.
  8261. * returns 0 on success and other values indicate failure.
  8262. */
  8263. static int TLSX_PreSharedKey_Parse(WOLFSSL* ssl, const byte* input,
  8264. word16 length, byte msgType)
  8265. {
  8266. TLSX* extension;
  8267. PreSharedKey* list;
  8268. if (msgType == client_hello) {
  8269. int ret;
  8270. word16 len;
  8271. word16 idx = 0;
  8272. TLSX_Remove(&ssl->extensions, TLSX_PRE_SHARED_KEY, ssl->heap);
  8273. /* Length of identities and of binders. */
  8274. if ((int)(length - idx) < OPAQUE16_LEN + OPAQUE16_LEN)
  8275. return BUFFER_E;
  8276. /* Length of identities. */
  8277. ato16(input + idx, &len);
  8278. idx += OPAQUE16_LEN;
  8279. if (len < MIN_PSK_ID_LEN || length - idx < len)
  8280. return BUFFER_E;
  8281. /* Create a pre-shared key object for each identity. */
  8282. while (len > 0) {
  8283. const byte* identity;
  8284. word16 identityLen;
  8285. word32 age;
  8286. if (len < OPAQUE16_LEN)
  8287. return BUFFER_E;
  8288. /* Length of identity. */
  8289. ato16(input + idx, &identityLen);
  8290. idx += OPAQUE16_LEN;
  8291. if (len < OPAQUE16_LEN + identityLen + OPAQUE32_LEN ||
  8292. identityLen > MAX_PSK_ID_LEN)
  8293. return BUFFER_E;
  8294. /* Cache identity pointer. */
  8295. identity = input + idx;
  8296. idx += identityLen;
  8297. /* Ticket age. */
  8298. ato32(input + idx, &age);
  8299. idx += OPAQUE32_LEN;
  8300. ret = TLSX_PreSharedKey_Use(ssl, identity, identityLen, age, no_mac,
  8301. 0, 0, 1, NULL);
  8302. if (ret != 0)
  8303. return ret;
  8304. /* Done with this identity. */
  8305. len -= OPAQUE16_LEN + identityLen + OPAQUE32_LEN;
  8306. }
  8307. /* Find the list of identities sent to server. */
  8308. extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
  8309. if (extension == NULL)
  8310. return PSK_KEY_ERROR;
  8311. list = (PreSharedKey*)extension->data;
  8312. /* Length of binders. */
  8313. if (idx + OPAQUE16_LEN > length)
  8314. return BUFFER_E;
  8315. ato16(input + idx, &len);
  8316. idx += OPAQUE16_LEN;
  8317. if (len < MIN_PSK_BINDERS_LEN || length - idx < len)
  8318. return BUFFER_E;
  8319. /* Set binder for each identity. */
  8320. while (list != NULL && len > 0) {
  8321. /* Length of binder */
  8322. list->binderLen = input[idx++];
  8323. if (list->binderLen < WC_SHA256_DIGEST_SIZE ||
  8324. list->binderLen > WC_MAX_DIGEST_SIZE)
  8325. return BUFFER_E;
  8326. if (len < OPAQUE8_LEN + list->binderLen)
  8327. return BUFFER_E;
  8328. /* Copy binder into static buffer. */
  8329. XMEMCPY(list->binder, input + idx, list->binderLen);
  8330. idx += (word16)list->binderLen;
  8331. /* Done with binder entry. */
  8332. len -= OPAQUE8_LEN + (word16)list->binderLen;
  8333. /* Next identity. */
  8334. list = list->next;
  8335. }
  8336. if (list != NULL || len != 0)
  8337. return BUFFER_E;
  8338. return 0;
  8339. }
  8340. if (msgType == server_hello) {
  8341. word16 idx;
  8342. /* Index of identity chosen by server. */
  8343. if (length != OPAQUE16_LEN)
  8344. return BUFFER_E;
  8345. ato16(input, &idx);
  8346. #ifdef WOLFSSL_EARLY_DATA
  8347. ssl->options.pskIdIndex = idx + 1;
  8348. #endif
  8349. /* Find the list of identities sent to server. */
  8350. extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
  8351. if (extension == NULL)
  8352. return PSK_KEY_ERROR;
  8353. list = (PreSharedKey*)extension->data;
  8354. /* Mark the identity as chosen. */
  8355. for (; list != NULL && idx > 0; idx--)
  8356. list = list->next;
  8357. if (list == NULL) {
  8358. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  8359. return PSK_KEY_ERROR;
  8360. }
  8361. list->chosen = 1;
  8362. #ifdef HAVE_SESSION_TICKET
  8363. if (list->resumption) {
  8364. /* Check that the session's details are the same as the server's. */
  8365. if (ssl->options.cipherSuite0 != ssl->session->cipherSuite0 ||
  8366. ssl->options.cipherSuite != ssl->session->cipherSuite ||
  8367. ssl->session->version.major != ssl->ctx->method->version.major ||
  8368. ssl->session->version.minor != ssl->ctx->method->version.minor) {
  8369. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  8370. return PSK_KEY_ERROR;
  8371. }
  8372. }
  8373. #endif
  8374. return 0;
  8375. }
  8376. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8377. return SANITY_MSG_E;
  8378. }
  8379. /* Create a new pre-shared key and put it into the list.
  8380. *
  8381. * list The linked list of pre-shared key.
  8382. * identity The identity.
  8383. * len The length of the identity data.
  8384. * heap The memory to allocate with.
  8385. * preSharedKey The new pre-shared key object.
  8386. * returns 0 on success and other values indicate failure.
  8387. */
  8388. static int TLSX_PreSharedKey_New(PreSharedKey** list, const byte* identity,
  8389. word16 len, void *heap,
  8390. PreSharedKey** preSharedKey)
  8391. {
  8392. PreSharedKey* psk;
  8393. PreSharedKey** next;
  8394. psk = (PreSharedKey*)XMALLOC(sizeof(PreSharedKey), heap, DYNAMIC_TYPE_TLSX);
  8395. if (psk == NULL)
  8396. return MEMORY_E;
  8397. XMEMSET(psk, 0, sizeof(*psk));
  8398. /* Make a copy of the identity data. */
  8399. psk->identity = (byte*)XMALLOC(len, heap, DYNAMIC_TYPE_TLSX);
  8400. if (psk->identity == NULL) {
  8401. XFREE(psk, heap, DYNAMIC_TYPE_TLSX);
  8402. return MEMORY_E;
  8403. }
  8404. XMEMCPY(psk->identity, identity, len);
  8405. psk->identityLen = len;
  8406. /* Add it to the end and maintain the links. */
  8407. while (*list != NULL) {
  8408. /* Assign to temporary to work around compiler bug found by customer. */
  8409. next = &((*list)->next);
  8410. list = next;
  8411. }
  8412. *list = psk;
  8413. *preSharedKey = psk;
  8414. (void)heap;
  8415. return 0;
  8416. }
  8417. static WC_INLINE byte GetHmacLength(int hmac)
  8418. {
  8419. switch (hmac) {
  8420. #ifndef NO_SHA256
  8421. case sha256_mac:
  8422. return WC_SHA256_DIGEST_SIZE;
  8423. #endif
  8424. #ifdef WOLFSSL_SHA384
  8425. case sha384_mac:
  8426. return WC_SHA384_DIGEST_SIZE;
  8427. #endif
  8428. #ifdef WOLFSSL_SHA512
  8429. case sha512_mac:
  8430. return WC_SHA512_DIGEST_SIZE;
  8431. #endif
  8432. }
  8433. return 0;
  8434. }
  8435. /* Use the data to create a new pre-shared key object in the extensions.
  8436. *
  8437. * ssl The SSL/TLS object.
  8438. * identity The identity.
  8439. * len The length of the identity data.
  8440. * age The age of the identity.
  8441. * hmac The HMAC algorithm.
  8442. * ciphersuite0 The first byte of the ciphersuite to use.
  8443. * ciphersuite The second byte of the ciphersuite to use.
  8444. * resumption The PSK is for resumption of a session.
  8445. * preSharedKey The new pre-shared key object.
  8446. * returns 0 on success and other values indicate failure.
  8447. */
  8448. int TLSX_PreSharedKey_Use(WOLFSSL* ssl, const byte* identity, word16 len,
  8449. word32 age, byte hmac, byte cipherSuite0,
  8450. byte cipherSuite, byte resumption,
  8451. PreSharedKey **preSharedKey)
  8452. {
  8453. int ret = 0;
  8454. TLSX* extension;
  8455. PreSharedKey* psk = NULL;
  8456. /* Find the pre-shared key extension if it exists. */
  8457. extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
  8458. if (extension == NULL) {
  8459. /* Push new pre-shared key extension. */
  8460. ret = TLSX_Push(&ssl->extensions, TLSX_PRE_SHARED_KEY, NULL, ssl->heap);
  8461. if (ret != 0)
  8462. return ret;
  8463. extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
  8464. if (extension == NULL)
  8465. return MEMORY_E;
  8466. }
  8467. /* Try to find the pre-shared key with this identity. */
  8468. psk = (PreSharedKey*)extension->data;
  8469. while (psk != NULL) {
  8470. if ((psk->identityLen == len) &&
  8471. (XMEMCMP(psk->identity, identity, len) == 0)) {
  8472. break;
  8473. }
  8474. psk = psk->next;
  8475. }
  8476. /* Create a new pre-shared key object if not found. */
  8477. if (psk == NULL) {
  8478. ret = TLSX_PreSharedKey_New((PreSharedKey**)&extension->data, identity,
  8479. len, ssl->heap, &psk);
  8480. if (ret != 0)
  8481. return ret;
  8482. }
  8483. /* Update/set age and HMAC algorithm. */
  8484. psk->ticketAge = age;
  8485. psk->hmac = hmac;
  8486. psk->cipherSuite0 = cipherSuite0;
  8487. psk->cipherSuite = cipherSuite;
  8488. psk->resumption = resumption;
  8489. psk->binderLen = GetHmacLength(psk->hmac);
  8490. if (preSharedKey != NULL)
  8491. *preSharedKey = psk;
  8492. return 0;
  8493. }
  8494. #define PSK_FREE_ALL TLSX_PreSharedKey_FreeAll
  8495. #define PSK_GET_SIZE TLSX_PreSharedKey_GetSize
  8496. #define PSK_WRITE TLSX_PreSharedKey_Write
  8497. #define PSK_PARSE TLSX_PreSharedKey_Parse
  8498. #else
  8499. #define PSK_FREE_ALL(a, b)
  8500. #define PSK_GET_SIZE(a, b, c) 0
  8501. #define PSK_WRITE(a, b, c, d) 0
  8502. #define PSK_PARSE(a, b, c, d) 0
  8503. #endif
  8504. /******************************************************************************/
  8505. /* PSK Key Exchange Modes */
  8506. /******************************************************************************/
  8507. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  8508. /* Get the size of the encoded PSK KE modes extension.
  8509. * Only in ClientHello.
  8510. *
  8511. * modes The PSK KE mode bit string.
  8512. * msgType The type of the message this extension is being written into.
  8513. * returns the number of bytes of the encoded PSK KE mode extension.
  8514. */
  8515. static int TLSX_PskKeModes_GetSize(byte modes, byte msgType, word16* pSz)
  8516. {
  8517. if (msgType == client_hello) {
  8518. /* Format: Len | Modes* */
  8519. word16 len = OPAQUE8_LEN;
  8520. /* Check whether each possible mode is to be written. */
  8521. if (modes & (1 << PSK_KE))
  8522. len += OPAQUE8_LEN;
  8523. if (modes & (1 << PSK_DHE_KE))
  8524. len += OPAQUE8_LEN;
  8525. *pSz += len;
  8526. return 0;
  8527. }
  8528. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8529. return SANITY_MSG_E;
  8530. }
  8531. /* Writes the PSK KE modes extension into the output buffer.
  8532. * Assumes that the the output buffer is big enough to hold data.
  8533. * Only in ClientHello.
  8534. *
  8535. * modes The PSK KE mode bit string.
  8536. * output The buffer to write into.
  8537. * msgType The type of the message this extension is being written into.
  8538. * returns the number of bytes written into the buffer.
  8539. */
  8540. static int TLSX_PskKeModes_Write(byte modes, byte* output, byte msgType,
  8541. word16* pSz)
  8542. {
  8543. if (msgType == client_hello) {
  8544. /* Format: Len | Modes* */
  8545. word16 idx = OPAQUE8_LEN;
  8546. /* Write out each possible mode. */
  8547. if (modes & (1 << PSK_KE))
  8548. output[idx++] = PSK_KE;
  8549. if (modes & (1 << PSK_DHE_KE))
  8550. output[idx++] = PSK_DHE_KE;
  8551. /* Write out length of mode list. */
  8552. output[0] = (byte)(idx - OPAQUE8_LEN);
  8553. *pSz += idx;
  8554. return 0;
  8555. }
  8556. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8557. return SANITY_MSG_E;
  8558. }
  8559. /* Parse the PSK KE modes extension.
  8560. * Only in ClientHello.
  8561. *
  8562. * ssl The SSL/TLS object.
  8563. * input The extension data.
  8564. * length The length of the extension data.
  8565. * msgType The type of the message this extension is being parsed from.
  8566. * returns 0 on success and other values indicate failure.
  8567. */
  8568. static int TLSX_PskKeModes_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  8569. byte msgType)
  8570. {
  8571. int ret;
  8572. if (msgType == client_hello) {
  8573. /* Format: Len | Modes* */
  8574. int idx = 0;
  8575. word16 len;
  8576. byte modes = 0;
  8577. /* Ensure length byte exists. */
  8578. if (length < OPAQUE8_LEN)
  8579. return BUFFER_E;
  8580. /* Get length of mode list and ensure that is the only data. */
  8581. len = input[0];
  8582. if (length - OPAQUE8_LEN != len)
  8583. return BUFFER_E;
  8584. idx = OPAQUE8_LEN;
  8585. /* Set a bit for each recognized modes. */
  8586. while (len > 0) {
  8587. /* Ignore unrecognized modes. */
  8588. if (input[idx] <= PSK_DHE_KE)
  8589. modes |= 1 << input[idx];
  8590. idx++;
  8591. len--;
  8592. }
  8593. ret = TLSX_PskKeModes_Use(ssl, modes);
  8594. if (ret != 0)
  8595. return ret;
  8596. return 0;
  8597. }
  8598. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8599. return SANITY_MSG_E;
  8600. }
  8601. /* Use the data to create a new PSK Key Exchange Modes object in the extensions.
  8602. *
  8603. * ssl The SSL/TLS object.
  8604. * modes The PSK key exchange modes.
  8605. * returns 0 on success and other values indicate failure.
  8606. */
  8607. int TLSX_PskKeModes_Use(WOLFSSL* ssl, byte modes)
  8608. {
  8609. int ret = 0;
  8610. TLSX* extension;
  8611. /* Find the PSK key exchange modes extension if it exists. */
  8612. extension = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES);
  8613. if (extension == NULL) {
  8614. /* Push new PSK key exchange modes extension. */
  8615. ret = TLSX_Push(&ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES, NULL,
  8616. ssl->heap);
  8617. if (ret != 0)
  8618. return ret;
  8619. extension = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES);
  8620. if (extension == NULL)
  8621. return MEMORY_E;
  8622. }
  8623. extension->val = modes;
  8624. return 0;
  8625. }
  8626. #define PKM_GET_SIZE TLSX_PskKeModes_GetSize
  8627. #define PKM_WRITE TLSX_PskKeModes_Write
  8628. #define PKM_PARSE TLSX_PskKeModes_Parse
  8629. #else
  8630. #define PKM_GET_SIZE(a, b, c) 0
  8631. #define PKM_WRITE(a, b, c, d) 0
  8632. #define PKM_PARSE(a, b, c, d) 0
  8633. #endif
  8634. /******************************************************************************/
  8635. /* Post-Handshake Authentication */
  8636. /******************************************************************************/
  8637. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  8638. /* Get the size of the encoded Post-Handshake Authentication extension.
  8639. * Only in ClientHello.
  8640. *
  8641. * msgType The type of the message this extension is being written into.
  8642. * returns the number of bytes of the encoded Post-Handshake Authentication
  8643. * extension.
  8644. */
  8645. static int TLSX_PostHandAuth_GetSize(byte msgType, word16* pSz)
  8646. {
  8647. if (msgType == client_hello) {
  8648. *pSz += 0;
  8649. return 0;
  8650. }
  8651. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8652. return SANITY_MSG_E;
  8653. }
  8654. /* Writes the Post-Handshake Authentication extension into the output buffer.
  8655. * Assumes that the the output buffer is big enough to hold data.
  8656. * Only in ClientHello.
  8657. *
  8658. * output The buffer to write into.
  8659. * msgType The type of the message this extension is being written into.
  8660. * returns the number of bytes written into the buffer.
  8661. */
  8662. static int TLSX_PostHandAuth_Write(byte* output, byte msgType, word16* pSz)
  8663. {
  8664. (void)output;
  8665. if (msgType == client_hello) {
  8666. *pSz += 0;
  8667. return 0;
  8668. }
  8669. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8670. return SANITY_MSG_E;
  8671. }
  8672. /* Parse the Post-Handshake Authentication extension.
  8673. * Only in ClientHello.
  8674. *
  8675. * ssl The SSL/TLS object.
  8676. * input The extension data.
  8677. * length The length of the extension data.
  8678. * msgType The type of the message this extension is being parsed from.
  8679. * returns 0 on success and other values indicate failure.
  8680. */
  8681. static int TLSX_PostHandAuth_Parse(WOLFSSL* ssl, const byte* input,
  8682. word16 length, byte msgType)
  8683. {
  8684. (void)input;
  8685. if (msgType == client_hello) {
  8686. /* Ensure extension is empty. */
  8687. if (length != 0)
  8688. return BUFFER_E;
  8689. ssl->options.postHandshakeAuth = 1;
  8690. return 0;
  8691. }
  8692. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8693. return SANITY_MSG_E;
  8694. }
  8695. /* Create a new Post-handshake authentication object in the extensions.
  8696. *
  8697. * ssl The SSL/TLS object.
  8698. * returns 0 on success and other values indicate failure.
  8699. */
  8700. static int TLSX_PostHandAuth_Use(WOLFSSL* ssl)
  8701. {
  8702. int ret = 0;
  8703. TLSX* extension;
  8704. /* Find the PSK key exchange modes extension if it exists. */
  8705. extension = TLSX_Find(ssl->extensions, TLSX_POST_HANDSHAKE_AUTH);
  8706. if (extension == NULL) {
  8707. /* Push new Post-handshake Authentication extension. */
  8708. ret = TLSX_Push(&ssl->extensions, TLSX_POST_HANDSHAKE_AUTH, NULL,
  8709. ssl->heap);
  8710. if (ret != 0)
  8711. return ret;
  8712. }
  8713. return 0;
  8714. }
  8715. #define PHA_GET_SIZE TLSX_PostHandAuth_GetSize
  8716. #define PHA_WRITE TLSX_PostHandAuth_Write
  8717. #define PHA_PARSE TLSX_PostHandAuth_Parse
  8718. #else
  8719. #define PHA_GET_SIZE(a, b) 0
  8720. #define PHA_WRITE(a, b, c) 0
  8721. #define PHA_PARSE(a, b, c, d) 0
  8722. #endif
  8723. /******************************************************************************/
  8724. /* Early Data Indication */
  8725. /******************************************************************************/
  8726. #ifdef WOLFSSL_EARLY_DATA
  8727. /* Get the size of the encoded Early Data Indication extension.
  8728. * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
  8729. *
  8730. * msgType The type of the message this extension is being written into.
  8731. * returns the number of bytes of the encoded Early Data Indication extension.
  8732. */
  8733. static int TLSX_EarlyData_GetSize(byte msgType, word16* pSz)
  8734. {
  8735. int ret = 0;
  8736. if (msgType == client_hello || msgType == encrypted_extensions)
  8737. *pSz += 0;
  8738. else if (msgType == session_ticket)
  8739. *pSz += OPAQUE32_LEN;
  8740. else {
  8741. ret = SANITY_MSG_E;
  8742. WOLFSSL_ERROR_VERBOSE(ret);
  8743. }
  8744. return ret;
  8745. }
  8746. /* Writes the Early Data Indicator extension into the output buffer.
  8747. * Assumes that the the output buffer is big enough to hold data.
  8748. * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
  8749. *
  8750. * maxSz The maximum early data size.
  8751. * output The buffer to write into.
  8752. * msgType The type of the message this extension is being written into.
  8753. * returns the number of bytes written into the buffer.
  8754. */
  8755. static int TLSX_EarlyData_Write(word32 maxSz, byte* output, byte msgType,
  8756. word16* pSz)
  8757. {
  8758. if (msgType == client_hello || msgType == encrypted_extensions)
  8759. return 0;
  8760. else if (msgType == session_ticket) {
  8761. c32toa(maxSz, output);
  8762. *pSz += OPAQUE32_LEN;
  8763. return 0;
  8764. }
  8765. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8766. return SANITY_MSG_E;
  8767. }
  8768. /* Parse the Early Data Indicator extension.
  8769. * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
  8770. *
  8771. * ssl The SSL/TLS object.
  8772. * input The extension data.
  8773. * length The length of the extension data.
  8774. * msgType The type of the message this extension is being parsed from.
  8775. * returns 0 on success and other values indicate failure.
  8776. */
  8777. static int TLSX_EarlyData_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  8778. byte msgType)
  8779. {
  8780. if (msgType == client_hello) {
  8781. if (length != 0)
  8782. return BUFFER_E;
  8783. if (ssl->earlyData == expecting_early_data) {
  8784. if (ssl->options.maxEarlyDataSz != 0)
  8785. ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_ACCEPTED;
  8786. else
  8787. ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_REJECTED;
  8788. return TLSX_EarlyData_Use(ssl, 0, 0);
  8789. }
  8790. ssl->earlyData = early_data_ext;
  8791. return 0;
  8792. }
  8793. if (msgType == encrypted_extensions) {
  8794. if (length != 0)
  8795. return BUFFER_E;
  8796. /* Ensure the index of PSK identity chosen by server is 0.
  8797. * Index is plus one to handle 'not set' value of 0.
  8798. */
  8799. if (ssl->options.pskIdIndex != 1) {
  8800. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  8801. return PSK_KEY_ERROR;
  8802. }
  8803. if (ssl->options.side == WOLFSSL_CLIENT_END) {
  8804. /* the extension from server comes in */
  8805. ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_ACCEPTED;
  8806. }
  8807. return TLSX_EarlyData_Use(ssl, 1, 1);
  8808. }
  8809. if (msgType == session_ticket) {
  8810. word32 maxSz;
  8811. if (length != OPAQUE32_LEN)
  8812. return BUFFER_E;
  8813. ato32(input, &maxSz);
  8814. ssl->session->maxEarlyDataSz = maxSz;
  8815. return 0;
  8816. }
  8817. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8818. return SANITY_MSG_E;
  8819. }
  8820. /* Use the data to create a new Early Data object in the extensions.
  8821. *
  8822. * ssl The SSL/TLS object.
  8823. * maxSz The maximum early data size.
  8824. * is_response if this extension is part of a response
  8825. * returns 0 on success and other values indicate failure.
  8826. */
  8827. int TLSX_EarlyData_Use(WOLFSSL* ssl, word32 maxSz, int is_response)
  8828. {
  8829. int ret = 0;
  8830. TLSX* extension;
  8831. /* Find the early data extension if it exists. */
  8832. extension = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA);
  8833. if (extension == NULL) {
  8834. /* Push new early data extension. */
  8835. ret = TLSX_Push(&ssl->extensions, TLSX_EARLY_DATA, NULL, ssl->heap);
  8836. if (ret != 0)
  8837. return ret;
  8838. extension = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA);
  8839. if (extension == NULL)
  8840. return MEMORY_E;
  8841. }
  8842. extension->resp = is_response;
  8843. extension->val = maxSz;
  8844. return 0;
  8845. }
  8846. #define EDI_GET_SIZE TLSX_EarlyData_GetSize
  8847. #define EDI_WRITE TLSX_EarlyData_Write
  8848. #define EDI_PARSE TLSX_EarlyData_Parse
  8849. #else
  8850. #define EDI_GET_SIZE(a, b) 0
  8851. #define EDI_WRITE(a, b, c, d) 0
  8852. #define EDI_PARSE(a, b, c, d) 0
  8853. #endif
  8854. /******************************************************************************/
  8855. /* QUIC transport parameter extension */
  8856. /******************************************************************************/
  8857. #ifdef WOLFSSL_QUIC
  8858. static word16 TLSX_QuicTP_GetSize(TLSX* extension)
  8859. {
  8860. const QuicTransportParam *tp = (QuicTransportParam*)extension->data;
  8861. return tp ? tp->len : 0;
  8862. }
  8863. int TLSX_QuicTP_Use(WOLFSSL* ssl, TLSX_Type ext_type, int is_response)
  8864. {
  8865. int ret = 0;
  8866. TLSX* extension;
  8867. WOLFSSL_ENTER("TLSX_QuicTP_Use");
  8868. if (ssl->quic.transport_local == NULL) {
  8869. /* RFC9000, ch 7.3: "An endpoint MUST treat the absence of [...]
  8870. * from either endpoint [...] as a connection error of type
  8871. * TRANSPORT_PARAMETER_ERROR."
  8872. */
  8873. ret = QUIC_TP_MISSING_E;
  8874. goto cleanup;
  8875. }
  8876. extension = TLSX_Find(ssl->extensions, ext_type);
  8877. if (extension == NULL) {
  8878. ret = TLSX_Push(&ssl->extensions, ext_type, NULL, ssl->heap);
  8879. if (ret != 0)
  8880. goto cleanup;
  8881. extension = TLSX_Find(ssl->extensions, ext_type);
  8882. if (extension == NULL) {
  8883. ret = MEMORY_E;
  8884. goto cleanup;
  8885. }
  8886. }
  8887. if (extension->data) {
  8888. QuicTransportParam_free((QuicTransportParam*)extension->data, ssl->heap);
  8889. extension->data = NULL;
  8890. }
  8891. extension->resp = is_response;
  8892. extension->data = (void*)QuicTransportParam_dup(ssl->quic.transport_local, ssl->heap);
  8893. if (!extension->data) {
  8894. ret = MEMORY_E;
  8895. goto cleanup;
  8896. }
  8897. cleanup:
  8898. WOLFSSL_LEAVE("TLSX_QuicTP_Use", ret);
  8899. return ret;
  8900. }
  8901. static word16 TLSX_QuicTP_Write(QuicTransportParam *tp, byte* output)
  8902. {
  8903. word16 len = 0;
  8904. WOLFSSL_ENTER("TLSX_QuicTP_Write");
  8905. if (tp && tp->len) {
  8906. XMEMCPY(output, tp->data, tp->len);
  8907. len = tp->len;
  8908. }
  8909. WOLFSSL_LEAVE("TLSX_QuicTP_Write", len);
  8910. return len;
  8911. }
  8912. static int TLSX_QuicTP_Parse(WOLFSSL *ssl, const byte *input, size_t len, int ext_type, int msgType)
  8913. {
  8914. const QuicTransportParam *tp, **ptp;
  8915. (void)msgType;
  8916. tp = QuicTransportParam_new(input, len, ssl->heap);
  8917. if (!tp) {
  8918. return MEMORY_E;
  8919. }
  8920. ptp = (ext_type == TLSX_KEY_QUIC_TP_PARAMS_DRAFT) ?
  8921. &ssl->quic.transport_peer_draft : &ssl->quic.transport_peer;
  8922. if (*ptp) {
  8923. QTP_FREE(*ptp, ssl->heap);
  8924. }
  8925. *ptp = tp;
  8926. return 0;
  8927. }
  8928. #define QTP_GET_SIZE TLSX_QuicTP_GetSize
  8929. #define QTP_USE TLSX_QuicTP_Use
  8930. #define QTP_WRITE TLSX_QuicTP_Write
  8931. #define QTP_PARSE TLSX_QuicTP_Parse
  8932. #endif /* WOLFSSL_QUIC */
  8933. #if defined(WOLFSSL_DTLS_CID)
  8934. #define CID_GET_SIZE TLSX_ConnectionID_GetSize
  8935. #define CID_WRITE TLSX_ConnectionID_Write
  8936. #define CID_PARSE TLSX_ConnectionID_Parse
  8937. #define CID_FREE TLSX_ConnectionID_Free
  8938. #else
  8939. #define CID_GET_SIZE(a) 0
  8940. #define CID_WRITE(a, b) 0
  8941. #define CID_PARSE(a, b, c, d) 0
  8942. #define CID_FREE(a, b) 0
  8943. #endif /* defined(WOLFSSL_DTLS_CID) */
  8944. /******************************************************************************/
  8945. /* TLS Extensions Framework */
  8946. /******************************************************************************/
  8947. /** Finds an extension in the provided list. */
  8948. TLSX* TLSX_Find(TLSX* list, TLSX_Type type)
  8949. {
  8950. TLSX* extension = list;
  8951. while (extension && extension->type != type)
  8952. extension = extension->next;
  8953. return extension;
  8954. }
  8955. /** Remove an extension. */
  8956. void TLSX_Remove(TLSX** list, TLSX_Type type, void* heap)
  8957. {
  8958. TLSX* extension = *list;
  8959. TLSX** next = list;
  8960. while (extension && extension->type != type) {
  8961. next = &extension->next;
  8962. extension = extension->next;
  8963. }
  8964. if (extension) {
  8965. *next = extension->next;
  8966. extension->next = NULL;
  8967. TLSX_FreeAll(extension, heap);
  8968. }
  8969. }
  8970. /** Releases all extensions in the provided list. */
  8971. void TLSX_FreeAll(TLSX* list, void* heap)
  8972. {
  8973. TLSX* extension;
  8974. while ((extension = list)) {
  8975. list = extension->next;
  8976. switch (extension->type) {
  8977. #ifdef HAVE_SNI
  8978. case TLSX_SERVER_NAME:
  8979. SNI_FREE_ALL((SNI*)extension->data, heap);
  8980. break;
  8981. #endif
  8982. case TLSX_TRUSTED_CA_KEYS:
  8983. TCA_FREE_ALL((TCA*)extension->data, heap);
  8984. break;
  8985. case TLSX_MAX_FRAGMENT_LENGTH:
  8986. MFL_FREE_ALL(extension->data, heap);
  8987. break;
  8988. case TLSX_EXTENDED_MASTER_SECRET:
  8989. case TLSX_TRUNCATED_HMAC:
  8990. /* Nothing to do. */
  8991. break;
  8992. case TLSX_SUPPORTED_GROUPS:
  8993. EC_FREE_ALL((SupportedCurve*)extension->data, heap);
  8994. break;
  8995. case TLSX_EC_POINT_FORMATS:
  8996. PF_FREE_ALL((PointFormat*)extension->data, heap);
  8997. break;
  8998. case TLSX_STATUS_REQUEST:
  8999. CSR_FREE_ALL((CertificateStatusRequest*)extension->data, heap);
  9000. break;
  9001. case TLSX_STATUS_REQUEST_V2:
  9002. CSR2_FREE_ALL((CertificateStatusRequestItemV2*)extension->data,
  9003. heap);
  9004. break;
  9005. case TLSX_RENEGOTIATION_INFO:
  9006. SCR_FREE_ALL(extension->data, heap);
  9007. break;
  9008. case TLSX_SESSION_TICKET:
  9009. WOLF_STK_FREE(extension->data, heap);
  9010. break;
  9011. case TLSX_APPLICATION_LAYER_PROTOCOL:
  9012. ALPN_FREE_ALL((ALPN*)extension->data, heap);
  9013. break;
  9014. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9015. case TLSX_SIGNATURE_ALGORITHMS:
  9016. break;
  9017. #endif
  9018. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  9019. case TLSX_ENCRYPT_THEN_MAC:
  9020. break;
  9021. #endif
  9022. #ifdef WOLFSSL_TLS13
  9023. case TLSX_SUPPORTED_VERSIONS:
  9024. break;
  9025. #ifdef WOLFSSL_SEND_HRR_COOKIE
  9026. case TLSX_COOKIE:
  9027. CKE_FREE_ALL((Cookie*)extension->data, heap);
  9028. break;
  9029. #endif
  9030. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9031. case TLSX_PRE_SHARED_KEY:
  9032. PSK_FREE_ALL((PreSharedKey*)extension->data, heap);
  9033. break;
  9034. case TLSX_PSK_KEY_EXCHANGE_MODES:
  9035. break;
  9036. #endif
  9037. #ifdef WOLFSSL_EARLY_DATA
  9038. case TLSX_EARLY_DATA:
  9039. break;
  9040. #endif
  9041. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  9042. case TLSX_POST_HANDSHAKE_AUTH:
  9043. break;
  9044. #endif
  9045. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9046. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  9047. break;
  9048. #endif
  9049. case TLSX_KEY_SHARE:
  9050. KS_FREE_ALL((KeyShareEntry*)extension->data, heap);
  9051. break;
  9052. #endif
  9053. #ifdef WOLFSSL_SRTP
  9054. case TLSX_USE_SRTP:
  9055. SRTP_FREE((TlsxSrtp*)extension->data, heap);
  9056. break;
  9057. #endif
  9058. #ifdef WOLFSSL_QUIC
  9059. case TLSX_KEY_QUIC_TP_PARAMS:
  9060. FALL_THROUGH;
  9061. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  9062. QTP_FREE((QuicTransportParam*)extension->data, heap);
  9063. break;
  9064. #endif
  9065. #ifdef WOLFSSL_DTLS_CID
  9066. case TLSX_CONNECTION_ID:
  9067. CID_FREE((byte*)extension->data, heap);
  9068. break;
  9069. #endif /* WOLFSSL_DTLS_CID */
  9070. default:
  9071. break;
  9072. }
  9073. XFREE(extension, heap, DYNAMIC_TYPE_TLSX);
  9074. }
  9075. (void)heap;
  9076. }
  9077. /** Checks if the tls extensions are supported based on the protocol version. */
  9078. int TLSX_SupportExtensions(WOLFSSL* ssl) {
  9079. return ssl && (IsTLS(ssl) || ssl->version.major == DTLS_MAJOR);
  9080. }
  9081. /** Tells the buffered size of the extensions in a list. */
  9082. static int TLSX_GetSize(TLSX* list, byte* semaphore, byte msgType,
  9083. word16* pLength)
  9084. {
  9085. int ret = 0;
  9086. TLSX* extension;
  9087. word16 length = 0;
  9088. byte isRequest = (msgType == client_hello ||
  9089. msgType == certificate_request);
  9090. while ((extension = list)) {
  9091. list = extension->next;
  9092. /* only extensions marked as response are sent back to the client. */
  9093. if (!isRequest && !extension->resp)
  9094. continue; /* skip! */
  9095. /* ssl level extensions are expected to override ctx level ones. */
  9096. if (!IS_OFF(semaphore, TLSX_ToSemaphore(extension->type)))
  9097. continue; /* skip! */
  9098. /* extension type + extension data length. */
  9099. length += HELLO_EXT_TYPE_SZ + OPAQUE16_LEN;
  9100. switch (extension->type) {
  9101. #ifdef HAVE_SNI
  9102. case TLSX_SERVER_NAME:
  9103. /* SNI only sends the name on the request. */
  9104. if (isRequest)
  9105. length += SNI_GET_SIZE((SNI*)extension->data);
  9106. break;
  9107. #endif
  9108. case TLSX_TRUSTED_CA_KEYS:
  9109. /* TCA only sends the list on the request. */
  9110. if (isRequest)
  9111. length += TCA_GET_SIZE((TCA*)extension->data);
  9112. break;
  9113. case TLSX_MAX_FRAGMENT_LENGTH:
  9114. length += MFL_GET_SIZE(extension->data);
  9115. break;
  9116. case TLSX_EXTENDED_MASTER_SECRET:
  9117. case TLSX_TRUNCATED_HMAC:
  9118. /* always empty. */
  9119. break;
  9120. case TLSX_SUPPORTED_GROUPS:
  9121. length += EC_GET_SIZE((SupportedCurve*)extension->data);
  9122. break;
  9123. case TLSX_EC_POINT_FORMATS:
  9124. length += PF_GET_SIZE((PointFormat*)extension->data);
  9125. break;
  9126. case TLSX_STATUS_REQUEST:
  9127. length += CSR_GET_SIZE(
  9128. (CertificateStatusRequest*)extension->data, isRequest);
  9129. break;
  9130. case TLSX_STATUS_REQUEST_V2:
  9131. length += CSR2_GET_SIZE(
  9132. (CertificateStatusRequestItemV2*)extension->data,
  9133. isRequest);
  9134. break;
  9135. case TLSX_RENEGOTIATION_INFO:
  9136. length += SCR_GET_SIZE((SecureRenegotiation*)extension->data,
  9137. isRequest);
  9138. break;
  9139. case TLSX_SESSION_TICKET:
  9140. length += WOLF_STK_GET_SIZE((SessionTicket*)extension->data,
  9141. isRequest);
  9142. break;
  9143. case TLSX_APPLICATION_LAYER_PROTOCOL:
  9144. length += ALPN_GET_SIZE((ALPN*)extension->data);
  9145. break;
  9146. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9147. case TLSX_SIGNATURE_ALGORITHMS:
  9148. length += SA_GET_SIZE(extension->data);
  9149. break;
  9150. #endif
  9151. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  9152. case TLSX_ENCRYPT_THEN_MAC:
  9153. ret = ETM_GET_SIZE(msgType, &length);
  9154. break;
  9155. #endif /* HAVE_ENCRYPT_THEN_MAC */
  9156. #ifdef WOLFSSL_TLS13
  9157. case TLSX_SUPPORTED_VERSIONS:
  9158. ret = SV_GET_SIZE(extension->data, msgType, &length);
  9159. break;
  9160. #ifdef WOLFSSL_SEND_HRR_COOKIE
  9161. case TLSX_COOKIE:
  9162. ret = CKE_GET_SIZE((Cookie*)extension->data, msgType, &length);
  9163. break;
  9164. #endif
  9165. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9166. case TLSX_PRE_SHARED_KEY:
  9167. ret = PSK_GET_SIZE((PreSharedKey*)extension->data, msgType,
  9168. &length);
  9169. break;
  9170. case TLSX_PSK_KEY_EXCHANGE_MODES:
  9171. ret = PKM_GET_SIZE((byte)extension->val, msgType, &length);
  9172. break;
  9173. #endif
  9174. #ifdef WOLFSSL_EARLY_DATA
  9175. case TLSX_EARLY_DATA:
  9176. ret = EDI_GET_SIZE(msgType, &length);
  9177. break;
  9178. #endif
  9179. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  9180. case TLSX_POST_HANDSHAKE_AUTH:
  9181. ret = PHA_GET_SIZE(msgType, &length);
  9182. break;
  9183. #endif
  9184. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9185. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  9186. length += SAC_GET_SIZE(extension->data);
  9187. break;
  9188. #endif
  9189. case TLSX_KEY_SHARE:
  9190. length += KS_GET_SIZE((KeyShareEntry*)extension->data, msgType);
  9191. break;
  9192. #endif
  9193. #ifdef WOLFSSL_SRTP
  9194. case TLSX_USE_SRTP:
  9195. length += SRTP_GET_SIZE((TlsxSrtp*)extension->data);
  9196. break;
  9197. #endif
  9198. #ifdef WOLFSSL_QUIC
  9199. case TLSX_KEY_QUIC_TP_PARAMS:
  9200. FALL_THROUGH; /* followed by */
  9201. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  9202. length += QTP_GET_SIZE(extension);
  9203. break;
  9204. #endif
  9205. #ifdef WOLFSSL_DTLS_CID
  9206. case TLSX_CONNECTION_ID:
  9207. length += CID_GET_SIZE((byte*)extension->data);
  9208. break;
  9209. #endif /* WOLFSSL_DTLS_CID */
  9210. default:
  9211. break;
  9212. }
  9213. /* marks the extension as processed so ctx level */
  9214. /* extensions don't overlap with ssl level ones. */
  9215. TURN_ON(semaphore, TLSX_ToSemaphore(extension->type));
  9216. }
  9217. *pLength += length;
  9218. return ret;
  9219. }
  9220. /** Writes the extensions of a list in a buffer. */
  9221. static int TLSX_Write(TLSX* list, byte* output, byte* semaphore,
  9222. byte msgType, word16* pOffset)
  9223. {
  9224. int ret = 0;
  9225. TLSX* extension;
  9226. word16 offset = 0;
  9227. word16 length_offset = 0;
  9228. byte isRequest = (msgType == client_hello ||
  9229. msgType == certificate_request);
  9230. while ((extension = list)) {
  9231. list = extension->next;
  9232. /* only extensions marked as response are written in a response. */
  9233. if (!isRequest && !extension->resp)
  9234. continue; /* skip! */
  9235. /* ssl level extensions are expected to override ctx level ones. */
  9236. if (!IS_OFF(semaphore, TLSX_ToSemaphore(extension->type)))
  9237. continue; /* skip! */
  9238. /* writes extension type. */
  9239. c16toa(extension->type, output + offset);
  9240. offset += HELLO_EXT_TYPE_SZ + OPAQUE16_LEN;
  9241. length_offset = offset;
  9242. /* extension data should be written internally. */
  9243. switch (extension->type) {
  9244. #ifdef HAVE_SNI
  9245. case TLSX_SERVER_NAME:
  9246. if (isRequest) {
  9247. WOLFSSL_MSG("SNI extension to write");
  9248. offset += SNI_WRITE((SNI*)extension->data, output + offset);
  9249. }
  9250. break;
  9251. #endif
  9252. case TLSX_TRUSTED_CA_KEYS:
  9253. WOLFSSL_MSG("Trusted CA Indication extension to write");
  9254. if (isRequest) {
  9255. offset += TCA_WRITE((TCA*)extension->data, output + offset);
  9256. }
  9257. break;
  9258. case TLSX_MAX_FRAGMENT_LENGTH:
  9259. WOLFSSL_MSG("Max Fragment Length extension to write");
  9260. offset += MFL_WRITE((byte*)extension->data, output + offset);
  9261. break;
  9262. case TLSX_EXTENDED_MASTER_SECRET:
  9263. WOLFSSL_MSG("Extended Master Secret");
  9264. /* always empty. */
  9265. break;
  9266. case TLSX_TRUNCATED_HMAC:
  9267. WOLFSSL_MSG("Truncated HMAC extension to write");
  9268. /* always empty. */
  9269. break;
  9270. case TLSX_SUPPORTED_GROUPS:
  9271. WOLFSSL_MSG("Supported Groups extension to write");
  9272. offset += EC_WRITE((SupportedCurve*)extension->data,
  9273. output + offset);
  9274. break;
  9275. case TLSX_EC_POINT_FORMATS:
  9276. WOLFSSL_MSG("Point Formats extension to write");
  9277. offset += PF_WRITE((PointFormat*)extension->data,
  9278. output + offset);
  9279. break;
  9280. case TLSX_STATUS_REQUEST:
  9281. WOLFSSL_MSG("Certificate Status Request extension to write");
  9282. offset += CSR_WRITE((CertificateStatusRequest*)extension->data,
  9283. output + offset, isRequest);
  9284. break;
  9285. case TLSX_STATUS_REQUEST_V2:
  9286. WOLFSSL_MSG("Certificate Status Request v2 extension to write");
  9287. offset += CSR2_WRITE(
  9288. (CertificateStatusRequestItemV2*)extension->data,
  9289. output + offset, isRequest);
  9290. break;
  9291. case TLSX_RENEGOTIATION_INFO:
  9292. WOLFSSL_MSG("Secure Renegotiation extension to write");
  9293. offset += SCR_WRITE((SecureRenegotiation*)extension->data,
  9294. output + offset, isRequest);
  9295. break;
  9296. case TLSX_SESSION_TICKET:
  9297. WOLFSSL_MSG("Session Ticket extension to write");
  9298. offset += WOLF_STK_WRITE((SessionTicket*)extension->data,
  9299. output + offset, isRequest);
  9300. break;
  9301. case TLSX_APPLICATION_LAYER_PROTOCOL:
  9302. WOLFSSL_MSG("ALPN extension to write");
  9303. offset += ALPN_WRITE((ALPN*)extension->data, output + offset);
  9304. break;
  9305. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9306. case TLSX_SIGNATURE_ALGORITHMS:
  9307. WOLFSSL_MSG("Signature Algorithms extension to write");
  9308. offset += SA_WRITE(extension->data, output + offset);
  9309. break;
  9310. #endif
  9311. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  9312. case TLSX_ENCRYPT_THEN_MAC:
  9313. WOLFSSL_MSG("Encrypt-Then-Mac extension to write");
  9314. ret = ETM_WRITE(extension->data, output, msgType, &offset);
  9315. break;
  9316. #endif /* HAVE_ENCRYPT_THEN_MAC */
  9317. #ifdef WOLFSSL_TLS13
  9318. case TLSX_SUPPORTED_VERSIONS:
  9319. WOLFSSL_MSG("Supported Versions extension to write");
  9320. ret = SV_WRITE(extension->data, output + offset, msgType, &offset);
  9321. break;
  9322. #ifdef WOLFSSL_SEND_HRR_COOKIE
  9323. case TLSX_COOKIE:
  9324. WOLFSSL_MSG("Cookie extension to write");
  9325. ret = CKE_WRITE((Cookie*)extension->data, output + offset,
  9326. msgType, &offset);
  9327. break;
  9328. #endif
  9329. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9330. case TLSX_PRE_SHARED_KEY:
  9331. WOLFSSL_MSG("Pre-Shared Key extension to write");
  9332. ret = PSK_WRITE((PreSharedKey*)extension->data, output + offset,
  9333. msgType, &offset);
  9334. break;
  9335. case TLSX_PSK_KEY_EXCHANGE_MODES:
  9336. WOLFSSL_MSG("PSK Key Exchange Modes extension to write");
  9337. ret = PKM_WRITE((byte)extension->val, output + offset, msgType,
  9338. &offset);
  9339. break;
  9340. #endif
  9341. #ifdef WOLFSSL_EARLY_DATA
  9342. case TLSX_EARLY_DATA:
  9343. WOLFSSL_MSG("Early Data extension to write");
  9344. ret = EDI_WRITE(extension->val, output + offset, msgType,
  9345. &offset);
  9346. break;
  9347. #endif
  9348. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  9349. case TLSX_POST_HANDSHAKE_AUTH:
  9350. WOLFSSL_MSG("Post-Handshake Authentication extension to write");
  9351. ret = PHA_WRITE(output + offset, msgType, &offset);
  9352. break;
  9353. #endif
  9354. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9355. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  9356. WOLFSSL_MSG("Signature Algorithms extension to write");
  9357. offset += SAC_WRITE(extension->data, output + offset);
  9358. break;
  9359. #endif
  9360. case TLSX_KEY_SHARE:
  9361. WOLFSSL_MSG("Key Share extension to write");
  9362. offset += KS_WRITE((KeyShareEntry*)extension->data,
  9363. output + offset, msgType);
  9364. break;
  9365. #endif
  9366. #ifdef WOLFSSL_SRTP
  9367. case TLSX_USE_SRTP:
  9368. offset += SRTP_WRITE((TlsxSrtp*)extension->data, output+offset);
  9369. break;
  9370. #endif
  9371. #ifdef WOLFSSL_QUIC
  9372. case TLSX_KEY_QUIC_TP_PARAMS:
  9373. FALL_THROUGH;
  9374. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  9375. WOLFSSL_MSG("QUIC transport parameter to write");
  9376. offset += QTP_WRITE((QuicTransportParam*)extension->data,
  9377. output + offset);
  9378. break;
  9379. #endif
  9380. #ifdef WOLFSSL_DTLS_CID
  9381. case TLSX_CONNECTION_ID:
  9382. offset += CID_WRITE((byte*)extension->data, output+offset);
  9383. break;
  9384. #endif /* WOLFSSL_DTLS_CID */
  9385. default:
  9386. break;
  9387. }
  9388. /* writes extension data length. */
  9389. c16toa(offset - length_offset, output + length_offset - OPAQUE16_LEN);
  9390. /* marks the extension as processed so ctx level */
  9391. /* extensions don't overlap with ssl level ones. */
  9392. TURN_ON(semaphore, TLSX_ToSemaphore(extension->type));
  9393. }
  9394. *pOffset += offset;
  9395. return ret;
  9396. }
  9397. #ifdef HAVE_SUPPORTED_CURVES
  9398. /* Populates the default supported groups / curves */
  9399. static int TLSX_PopulateSupportedGroups(WOLFSSL* ssl, TLSX** extensions)
  9400. {
  9401. int ret = WOLFSSL_SUCCESS;
  9402. #ifdef WOLFSSL_TLS13
  9403. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9404. if (ssl->options.resuming && ssl->session->namedGroup != 0) {
  9405. return TLSX_UseSupportedCurve(extensions, ssl->session->namedGroup,
  9406. ssl->heap);
  9407. }
  9408. #endif
  9409. if (ssl->numGroups != 0) {
  9410. int i;
  9411. for (i = 0; i < ssl->numGroups; i++) {
  9412. ret = TLSX_UseSupportedCurve(extensions, ssl->group[i], ssl->heap);
  9413. if (ret != WOLFSSL_SUCCESS)
  9414. return ret;
  9415. }
  9416. return WOLFSSL_SUCCESS;
  9417. }
  9418. #endif /* WOLFSSL_TLS13 */
  9419. #if defined(HAVE_ECC)
  9420. /* list in order by strength, since not all servers choose by strength */
  9421. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  9422. #ifndef NO_ECC_SECP
  9423. ret = TLSX_UseSupportedCurve(extensions,
  9424. WOLFSSL_ECC_SECP521R1, ssl->heap);
  9425. if (ret != WOLFSSL_SUCCESS) return ret;
  9426. #endif
  9427. #endif
  9428. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  9429. #ifdef HAVE_ECC_BRAINPOOL
  9430. ret = TLSX_UseSupportedCurve(extensions,
  9431. WOLFSSL_ECC_BRAINPOOLP512R1, ssl->heap);
  9432. if (ret != WOLFSSL_SUCCESS) return ret;
  9433. #endif
  9434. #endif
  9435. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  9436. #ifndef NO_ECC_SECP
  9437. ret = TLSX_UseSupportedCurve(extensions,
  9438. WOLFSSL_ECC_SECP384R1, ssl->heap);
  9439. if (ret != WOLFSSL_SUCCESS) return ret;
  9440. #endif
  9441. #ifdef HAVE_ECC_BRAINPOOL
  9442. ret = TLSX_UseSupportedCurve(extensions,
  9443. WOLFSSL_ECC_BRAINPOOLP384R1, ssl->heap);
  9444. if (ret != WOLFSSL_SUCCESS) return ret;
  9445. #endif
  9446. #endif
  9447. #endif /* HAVE_ECC */
  9448. #ifndef HAVE_FIPS
  9449. #if defined(HAVE_CURVE448) && ECC_MIN_KEY_SZ <= 448
  9450. ret = TLSX_UseSupportedCurve(extensions,
  9451. WOLFSSL_ECC_X448, ssl->heap);
  9452. if (ret != WOLFSSL_SUCCESS) return ret;
  9453. #endif
  9454. #endif /* HAVE_FIPS */
  9455. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  9456. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  9457. #ifndef NO_ECC_SECP
  9458. ret = TLSX_UseSupportedCurve(extensions,
  9459. WOLFSSL_ECC_SECP256R1, ssl->heap);
  9460. if (ret != WOLFSSL_SUCCESS) return ret;
  9461. #endif
  9462. #ifdef HAVE_ECC_KOBLITZ
  9463. ret = TLSX_UseSupportedCurve(extensions,
  9464. WOLFSSL_ECC_SECP256K1, ssl->heap);
  9465. if (ret != WOLFSSL_SUCCESS) return ret;
  9466. #endif
  9467. #ifdef HAVE_ECC_BRAINPOOL
  9468. ret = TLSX_UseSupportedCurve(extensions,
  9469. WOLFSSL_ECC_BRAINPOOLP256R1, ssl->heap);
  9470. if (ret != WOLFSSL_SUCCESS) return ret;
  9471. #endif
  9472. #endif
  9473. #endif /* HAVE_ECC */
  9474. #ifndef HAVE_FIPS
  9475. #if defined(HAVE_CURVE25519) && ECC_MIN_KEY_SZ <= 256
  9476. ret = TLSX_UseSupportedCurve(extensions,
  9477. WOLFSSL_ECC_X25519, ssl->heap);
  9478. if (ret != WOLFSSL_SUCCESS) return ret;
  9479. #endif
  9480. #endif /* HAVE_FIPS */
  9481. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  9482. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  9483. #ifndef NO_ECC_SECP
  9484. ret = TLSX_UseSupportedCurve(extensions,
  9485. WOLFSSL_ECC_SECP224R1, ssl->heap);
  9486. if (ret != WOLFSSL_SUCCESS) return ret;
  9487. #endif
  9488. #ifdef HAVE_ECC_KOBLITZ
  9489. ret = TLSX_UseSupportedCurve(extensions,
  9490. WOLFSSL_ECC_SECP224K1, ssl->heap);
  9491. if (ret != WOLFSSL_SUCCESS) return ret;
  9492. #endif
  9493. #endif
  9494. #ifndef HAVE_FIPS
  9495. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  9496. #ifndef NO_ECC_SECP
  9497. ret = TLSX_UseSupportedCurve(extensions,
  9498. WOLFSSL_ECC_SECP192R1, ssl->heap);
  9499. if (ret != WOLFSSL_SUCCESS) return ret;
  9500. #endif
  9501. #ifdef HAVE_ECC_KOBLITZ
  9502. ret = TLSX_UseSupportedCurve(extensions,
  9503. WOLFSSL_ECC_SECP192K1, ssl->heap);
  9504. if (ret != WOLFSSL_SUCCESS) return ret;
  9505. #endif
  9506. #endif
  9507. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  9508. #ifndef NO_ECC_SECP
  9509. ret = TLSX_UseSupportedCurve(extensions,
  9510. WOLFSSL_ECC_SECP160R1, ssl->heap);
  9511. if (ret != WOLFSSL_SUCCESS) return ret;
  9512. #endif
  9513. #ifdef HAVE_ECC_SECPR2
  9514. ret = TLSX_UseSupportedCurve(extensions,
  9515. WOLFSSL_ECC_SECP160R2, ssl->heap);
  9516. if (ret != WOLFSSL_SUCCESS) return ret;
  9517. #endif
  9518. #ifdef HAVE_ECC_KOBLITZ
  9519. ret = TLSX_UseSupportedCurve(extensions,
  9520. WOLFSSL_ECC_SECP160K1, ssl->heap);
  9521. if (ret != WOLFSSL_SUCCESS) return ret;
  9522. #endif
  9523. #endif
  9524. #endif /* HAVE_FIPS */
  9525. #endif /* HAVE_ECC */
  9526. #ifndef NO_DH
  9527. /* Add FFDHE supported groups. */
  9528. #ifdef HAVE_FFDHE_8192
  9529. if (8192/8 >= ssl->options.minDhKeySz &&
  9530. 8192/8 <= ssl->options.maxDhKeySz) {
  9531. ret = TLSX_UseSupportedCurve(extensions,
  9532. WOLFSSL_FFDHE_8192, ssl->heap);
  9533. if (ret != WOLFSSL_SUCCESS)
  9534. return ret;
  9535. }
  9536. #endif
  9537. #ifdef HAVE_FFDHE_6144
  9538. if (6144/8 >= ssl->options.minDhKeySz &&
  9539. 6144/8 <= ssl->options.maxDhKeySz) {
  9540. ret = TLSX_UseSupportedCurve(extensions,
  9541. WOLFSSL_FFDHE_6144, ssl->heap);
  9542. if (ret != WOLFSSL_SUCCESS)
  9543. return ret;
  9544. }
  9545. #endif
  9546. #ifdef HAVE_FFDHE_4096
  9547. if (4096/8 >= ssl->options.minDhKeySz &&
  9548. 4096/8 <= ssl->options.maxDhKeySz) {
  9549. ret = TLSX_UseSupportedCurve(extensions,
  9550. WOLFSSL_FFDHE_4096, ssl->heap);
  9551. if (ret != WOLFSSL_SUCCESS)
  9552. return ret;
  9553. }
  9554. #endif
  9555. #ifdef HAVE_FFDHE_3072
  9556. if (3072/8 >= ssl->options.minDhKeySz &&
  9557. 3072/8 <= ssl->options.maxDhKeySz) {
  9558. ret = TLSX_UseSupportedCurve(extensions,
  9559. WOLFSSL_FFDHE_3072, ssl->heap);
  9560. if (ret != WOLFSSL_SUCCESS)
  9561. return ret;
  9562. }
  9563. #endif
  9564. #ifdef HAVE_FFDHE_2048
  9565. if (2048/8 >= ssl->options.minDhKeySz &&
  9566. 2048/8 <= ssl->options.maxDhKeySz) {
  9567. ret = TLSX_UseSupportedCurve(extensions,
  9568. WOLFSSL_FFDHE_2048, ssl->heap);
  9569. if (ret != WOLFSSL_SUCCESS)
  9570. return ret;
  9571. }
  9572. #endif
  9573. #endif
  9574. #ifdef HAVE_PQC
  9575. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL1, ssl->heap);
  9576. #if HAVE_LIBOQS
  9577. if (ret == WOLFSSL_SUCCESS)
  9578. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL3,
  9579. ssl->heap);
  9580. if (ret == WOLFSSL_SUCCESS)
  9581. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL5,
  9582. ssl->heap);
  9583. if (ret == WOLFSSL_SUCCESS)
  9584. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_NTRU_HPS_LEVEL1,
  9585. ssl->heap);
  9586. if (ret == WOLFSSL_SUCCESS)
  9587. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_NTRU_HPS_LEVEL3,
  9588. ssl->heap);
  9589. if (ret == WOLFSSL_SUCCESS)
  9590. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_NTRU_HPS_LEVEL5,
  9591. ssl->heap);
  9592. if (ret == WOLFSSL_SUCCESS)
  9593. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_NTRU_HRSS_LEVEL3,
  9594. ssl->heap);
  9595. if (ret == WOLFSSL_SUCCESS)
  9596. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_SABER_LEVEL1,
  9597. ssl->heap);
  9598. if (ret == WOLFSSL_SUCCESS)
  9599. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_SABER_LEVEL3,
  9600. ssl->heap);
  9601. if (ret == WOLFSSL_SUCCESS)
  9602. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_SABER_LEVEL5,
  9603. ssl->heap);
  9604. if (ret == WOLFSSL_SUCCESS)
  9605. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_90S_LEVEL1,
  9606. ssl->heap);
  9607. if (ret == WOLFSSL_SUCCESS)
  9608. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_90S_LEVEL3,
  9609. ssl->heap);
  9610. if (ret == WOLFSSL_SUCCESS)
  9611. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_90S_LEVEL5,
  9612. ssl->heap);
  9613. if (ret == WOLFSSL_SUCCESS)
  9614. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P256_NTRU_HPS_LEVEL1,
  9615. ssl->heap);
  9616. if (ret == WOLFSSL_SUCCESS)
  9617. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P384_NTRU_HPS_LEVEL3,
  9618. ssl->heap);
  9619. if (ret == WOLFSSL_SUCCESS)
  9620. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P521_NTRU_HPS_LEVEL5,
  9621. ssl->heap);
  9622. if (ret == WOLFSSL_SUCCESS)
  9623. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P384_NTRU_HRSS_LEVEL3,
  9624. ssl->heap);
  9625. if (ret == WOLFSSL_SUCCESS)
  9626. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P256_SABER_LEVEL1,
  9627. ssl->heap);
  9628. if (ret == WOLFSSL_SUCCESS)
  9629. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P384_SABER_LEVEL3,
  9630. ssl->heap);
  9631. if (ret == WOLFSSL_SUCCESS)
  9632. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P521_SABER_LEVEL5,
  9633. ssl->heap);
  9634. if (ret == WOLFSSL_SUCCESS)
  9635. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P256_KYBER_LEVEL1,
  9636. ssl->heap);
  9637. if (ret == WOLFSSL_SUCCESS)
  9638. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P384_KYBER_LEVEL3,
  9639. ssl->heap);
  9640. if (ret == WOLFSSL_SUCCESS)
  9641. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P521_KYBER_LEVEL5,
  9642. ssl->heap);
  9643. if (ret == WOLFSSL_SUCCESS)
  9644. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P256_KYBER_90S_LEVEL1,
  9645. ssl->heap);
  9646. if (ret == WOLFSSL_SUCCESS)
  9647. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P384_KYBER_90S_LEVEL3,
  9648. ssl->heap);
  9649. if (ret == WOLFSSL_SUCCESS)
  9650. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P521_KYBER_90S_LEVEL5,
  9651. ssl->heap);
  9652. #endif /* HAVE_LIBOQS */
  9653. #endif /* HAVE_PQC */
  9654. (void)ssl;
  9655. (void)extensions;
  9656. return ret;
  9657. }
  9658. #endif /* HAVE_SUPPORTED_CURVES */
  9659. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  9660. static const word16 preferredGroup[] = {
  9661. #if defined(HAVE_ECC) && (!defined(NO_ECC256) || \
  9662. defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP) && ECC_MIN_KEY_SZ <= 256
  9663. WOLFSSL_ECC_SECP256R1,
  9664. #endif
  9665. #if defined(HAVE_CURVE25519) && ECC_MIN_KEY_SZ <= 256
  9666. WOLFSSL_ECC_X25519,
  9667. #endif
  9668. #if defined(HAVE_CURVE448) && ECC_MIN_KEY_SZ <= 448
  9669. WOLFSSL_ECC_X448,
  9670. #endif
  9671. #if defined(HAVE_ECC) && (!defined(NO_ECC384) || \
  9672. defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP) && ECC_MIN_KEY_SZ <= 384
  9673. WOLFSSL_ECC_SECP384R1,
  9674. #endif
  9675. #if defined(HAVE_ECC) && (!defined(NO_ECC521) || \
  9676. defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP) && ECC_MIN_KEY_SZ <= 521
  9677. WOLFSSL_ECC_SECP521R1,
  9678. #endif
  9679. #if defined(HAVE_FFDHE_2048)
  9680. WOLFSSL_FFDHE_2048,
  9681. #endif
  9682. #if defined(HAVE_FFDHE_3072)
  9683. WOLFSSL_FFDHE_3072,
  9684. #endif
  9685. #if defined(HAVE_FFDHE_4096)
  9686. WOLFSSL_FFDHE_4096,
  9687. #endif
  9688. #if defined(HAVE_FFDHE_6144)
  9689. WOLFSSL_FFDHE_6144,
  9690. #endif
  9691. #if defined(HAVE_FFDHE_8192)
  9692. WOLFSSL_FFDHE_8192,
  9693. #endif
  9694. WOLFSSL_NAMED_GROUP_INVALID
  9695. };
  9696. #endif /* WOLFSSL_TLS13 && HAVE_SUPPORTED_CURVES */
  9697. int TLSX_PopulateExtensions(WOLFSSL* ssl, byte isServer)
  9698. {
  9699. int ret = 0;
  9700. byte* public_key = NULL;
  9701. word16 public_key_len = 0;
  9702. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  9703. int usingPSK = 0;
  9704. #endif
  9705. #if defined(HAVE_SUPPORTED_CURVES) && defined(WOLFSSL_TLS13)
  9706. TLSX* extension = NULL;
  9707. word16 namedGroup = WOLFSSL_NAMED_GROUP_INVALID;
  9708. #endif
  9709. /* server will add extension depending on what is parsed from client */
  9710. if (!isServer) {
  9711. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  9712. if (!ssl->options.disallowEncThenMac) {
  9713. ret = TLSX_EncryptThenMac_Use(ssl);
  9714. if (ret != 0)
  9715. return ret;
  9716. }
  9717. #endif
  9718. #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  9719. defined(HAVE_CURVE448)) && defined(HAVE_SUPPORTED_CURVES)
  9720. if (!ssl->options.userCurves && !ssl->ctx->userCurves) {
  9721. if (TLSX_Find(ssl->ctx->extensions,
  9722. TLSX_SUPPORTED_GROUPS) == NULL) {
  9723. ret = TLSX_PopulateSupportedGroups(ssl, &ssl->extensions);
  9724. if (ret != WOLFSSL_SUCCESS)
  9725. return ret;
  9726. }
  9727. }
  9728. if ((!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade) &&
  9729. TLSX_Find(ssl->ctx->extensions, TLSX_EC_POINT_FORMATS) == NULL &&
  9730. TLSX_Find(ssl->extensions, TLSX_EC_POINT_FORMATS) == NULL) {
  9731. ret = TLSX_UsePointFormat(&ssl->extensions,
  9732. WOLFSSL_EC_PF_UNCOMPRESSED, ssl->heap);
  9733. if (ret != WOLFSSL_SUCCESS)
  9734. return ret;
  9735. }
  9736. #endif /* (HAVE_ECC || CURVE25519 || CURVE448) && HAVE_SUPPORTED_CURVES */
  9737. #ifdef WOLFSSL_SRTP
  9738. if (ssl->options.dtls && ssl->dtlsSrtpProfiles != 0) {
  9739. WOLFSSL_MSG("Adding DTLS SRTP extension");
  9740. if ((ret = TLSX_UseSRTP(&ssl->extensions, ssl->dtlsSrtpProfiles,
  9741. ssl->heap)) != 0) {
  9742. return ret;
  9743. }
  9744. }
  9745. #endif
  9746. } /* is not server */
  9747. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9748. WOLFSSL_MSG("Adding signature algorithms extension");
  9749. if ((ret = TLSX_SetSignatureAlgorithms(&ssl->extensions, ssl, ssl->heap))
  9750. != 0) {
  9751. return ret;
  9752. }
  9753. #else
  9754. ret = 0;
  9755. #endif
  9756. #ifdef WOLFSSL_TLS13
  9757. if (!isServer && IsAtLeastTLSv1_3(ssl->version)) {
  9758. /* Add mandatory TLS v1.3 extension: supported version */
  9759. WOLFSSL_MSG("Adding supported versions extension");
  9760. if ((ret = TLSX_SetSupportedVersions(&ssl->extensions, ssl,
  9761. ssl->heap)) != 0) {
  9762. return ret;
  9763. }
  9764. #if !defined(HAVE_ECC) && !defined(HAVE_CURVE25519) && \
  9765. !defined(HAVE_CURVE448) && defined(HAVE_SUPPORTED_CURVES)
  9766. if (TLSX_Find(ssl->ctx->extensions, TLSX_SUPPORTED_GROUPS) == NULL) {
  9767. /* Put in DH groups for TLS 1.3 only. */
  9768. ret = TLSX_PopulateSupportedGroups(ssl, &ssl->extensions);
  9769. if (ret != WOLFSSL_SUCCESS)
  9770. return ret;
  9771. ret = 0;
  9772. }
  9773. #endif /* !(HAVE_ECC || CURVE25519 || CURVE448) && HAVE_SUPPORTED_CURVES */
  9774. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9775. if (ssl->certHashSigAlgoSz > 0) {
  9776. WOLFSSL_MSG("Adding signature algorithms cert extension");
  9777. if ((ret = TLSX_SetSignatureAlgorithmsCert(&ssl->extensions,
  9778. ssl, ssl->heap)) != 0) {
  9779. return ret;
  9780. }
  9781. }
  9782. #endif
  9783. #if defined(HAVE_SUPPORTED_CURVES)
  9784. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  9785. if (extension == NULL) {
  9786. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9787. if (ssl->options.resuming && ssl->session->namedGroup != 0)
  9788. namedGroup = ssl->session->namedGroup;
  9789. else
  9790. #endif
  9791. if (ssl->numGroups > 0) {
  9792. int set = 0;
  9793. int i, j;
  9794. /* try to find the highest element in ssl->group[]
  9795. * that is contained in preferredGroup[].
  9796. */
  9797. namedGroup = preferredGroup[0];
  9798. for (i = 0; i < ssl->numGroups && !set; i++) {
  9799. for (j = 0; preferredGroup[j] != WOLFSSL_NAMED_GROUP_INVALID; j++) {
  9800. if (preferredGroup[j] == ssl->group[i]) {
  9801. namedGroup = ssl->group[i];
  9802. set = 1;
  9803. break;
  9804. }
  9805. }
  9806. }
  9807. }
  9808. else {
  9809. /* Choose the most preferred group. */
  9810. namedGroup = preferredGroup[0];
  9811. }
  9812. }
  9813. else {
  9814. KeyShareEntry* kse = (KeyShareEntry*)extension->data;
  9815. if (kse)
  9816. namedGroup = kse->group;
  9817. }
  9818. if (namedGroup > 0) {
  9819. #ifdef HAVE_PQC
  9820. /* For KEMs, the key share has already been generated. */
  9821. if (!WOLFSSL_NAMED_GROUP_IS_PQC(namedGroup))
  9822. #endif
  9823. ret = TLSX_KeyShare_Use(ssl, namedGroup, 0, NULL, NULL);
  9824. if (ret != 0)
  9825. return ret;
  9826. }
  9827. #endif /* HAVE_SUPPORTED_CURVES */
  9828. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9829. TLSX_Remove(&ssl->extensions, TLSX_PRE_SHARED_KEY, ssl->heap);
  9830. #endif
  9831. #if defined(HAVE_SESSION_TICKET)
  9832. if (ssl->options.resuming && ssl->session->ticketLen > 0) {
  9833. WOLFSSL_SESSION* sess = ssl->session;
  9834. word32 now, milli;
  9835. if (sess->ticketLen > MAX_PSK_ID_LEN) {
  9836. WOLFSSL_MSG("Session ticket length for PSK ext is too large");
  9837. return BUFFER_ERROR;
  9838. }
  9839. /* Determine the MAC algorithm for the cipher suite used. */
  9840. ssl->options.cipherSuite0 = sess->cipherSuite0;
  9841. ssl->options.cipherSuite = sess->cipherSuite;
  9842. ret = SetCipherSpecs(ssl);
  9843. if (ret != 0)
  9844. return ret;
  9845. now = TimeNowInMilliseconds();
  9846. if (now < sess->ticketSeen)
  9847. milli = (0xFFFFFFFFU - sess->ticketSeen) + 1 + now;
  9848. else
  9849. milli = now - sess->ticketSeen;
  9850. milli += sess->ticketAdd;
  9851. /* Pre-shared key is mandatory extension for resumption. */
  9852. ret = TLSX_PreSharedKey_Use(ssl, sess->ticket, sess->ticketLen,
  9853. milli, ssl->specs.mac_algorithm,
  9854. ssl->options.cipherSuite0,
  9855. ssl->options.cipherSuite, 1,
  9856. NULL);
  9857. if (ret != 0)
  9858. return ret;
  9859. usingPSK = 1;
  9860. }
  9861. #endif
  9862. #ifndef NO_PSK
  9863. #ifndef WOLFSSL_PSK_ONE_ID
  9864. if (ssl->options.client_psk_cs_cb != NULL) {
  9865. int i;
  9866. for (i = 0; i < ssl->suites->suiteSz; i += 2) {
  9867. byte cipherSuite0 = ssl->suites->suites[i + 0];
  9868. byte cipherSuite = ssl->suites->suites[i + 1];
  9869. unsigned int keySz;
  9870. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  9871. int cnt = 0;
  9872. #endif
  9873. #ifdef HAVE_NULL_CIPHER
  9874. if (cipherSuite0 == ECC_BYTE ||
  9875. cipherSuite0 == ECDHE_PSK_BYTE) {
  9876. if (cipherSuite != TLS_SHA256_SHA256 &&
  9877. cipherSuite != TLS_SHA384_SHA384) {
  9878. continue;
  9879. }
  9880. }
  9881. else
  9882. #endif
  9883. if (cipherSuite0 != TLS13_BYTE)
  9884. continue;
  9885. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  9886. do {
  9887. ssl->arrays->client_identity[0] = cnt;
  9888. #endif
  9889. ssl->arrays->client_identity[MAX_PSK_ID_LEN] = '\0';
  9890. keySz = ssl->options.client_psk_cs_cb(
  9891. ssl, ssl->arrays->server_hint,
  9892. ssl->arrays->client_identity, MAX_PSK_ID_LEN,
  9893. ssl->arrays->psk_key, MAX_PSK_KEY_LEN,
  9894. GetCipherNameInternal(cipherSuite0, cipherSuite));
  9895. if (keySz > 0) {
  9896. ssl->arrays->psk_keySz = keySz;
  9897. ret = TLSX_PreSharedKey_Use(ssl,
  9898. (byte*)ssl->arrays->client_identity,
  9899. (word16)XSTRLEN(ssl->arrays->client_identity),
  9900. 0, SuiteMac(ssl->suites->suites + i),
  9901. cipherSuite0, cipherSuite, 0, NULL);
  9902. if (ret != 0)
  9903. return ret;
  9904. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  9905. cnt++;
  9906. #endif
  9907. }
  9908. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  9909. }
  9910. while (keySz > 0);
  9911. #endif
  9912. }
  9913. usingPSK = 1;
  9914. }
  9915. else
  9916. #endif
  9917. if (ssl->options.client_psk_cb != NULL ||
  9918. ssl->options.client_psk_tls13_cb != NULL) {
  9919. /* Default ciphersuite. */
  9920. byte cipherSuite0 = TLS13_BYTE;
  9921. byte cipherSuite = WOLFSSL_DEF_PSK_CIPHER;
  9922. int cipherSuiteFlags = WOLFSSL_CIPHER_SUITE_FLAG_NONE;
  9923. const char* cipherName = NULL;
  9924. if (ssl->options.client_psk_tls13_cb != NULL) {
  9925. ssl->arrays->psk_keySz = ssl->options.client_psk_tls13_cb(
  9926. ssl, ssl->arrays->server_hint,
  9927. ssl->arrays->client_identity, MAX_PSK_ID_LEN,
  9928. ssl->arrays->psk_key, MAX_PSK_KEY_LEN, &cipherName);
  9929. if (GetCipherSuiteFromName(cipherName, &cipherSuite0,
  9930. &cipherSuite, &cipherSuiteFlags) != 0) {
  9931. return PSK_KEY_ERROR;
  9932. }
  9933. }
  9934. else {
  9935. ssl->arrays->psk_keySz = ssl->options.client_psk_cb(ssl,
  9936. ssl->arrays->server_hint, ssl->arrays->client_identity,
  9937. MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN);
  9938. }
  9939. #if defined(OPENSSL_EXTRA)
  9940. /* OpenSSL treats 0 as a PSK key length of 0
  9941. * and meaning no PSK available.
  9942. */
  9943. if (ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN) {
  9944. return PSK_KEY_ERROR;
  9945. }
  9946. if (ssl->arrays->psk_keySz > 0) {
  9947. #else
  9948. if (ssl->arrays->psk_keySz == 0 ||
  9949. ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN) {
  9950. return PSK_KEY_ERROR;
  9951. }
  9952. #endif
  9953. ssl->arrays->client_identity[MAX_PSK_ID_LEN] = '\0';
  9954. ssl->options.cipherSuite0 = cipherSuite0;
  9955. ssl->options.cipherSuite = cipherSuite;
  9956. (void)cipherSuiteFlags;
  9957. ret = SetCipherSpecs(ssl);
  9958. if (ret != 0)
  9959. return ret;
  9960. ret = TLSX_PreSharedKey_Use(ssl,
  9961. (byte*)ssl->arrays->client_identity,
  9962. (word16)XSTRLEN(ssl->arrays->client_identity),
  9963. 0, ssl->specs.mac_algorithm,
  9964. cipherSuite0, cipherSuite, 0,
  9965. NULL);
  9966. if (ret != 0)
  9967. return ret;
  9968. usingPSK = 1;
  9969. #if defined(OPENSSL_EXTRA)
  9970. }
  9971. #endif
  9972. }
  9973. #endif /* !NO_PSK */
  9974. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9975. /* Some servers do not generate session tickets unless
  9976. * the extension is seen in a non-resume client hello.
  9977. * We used to send it only if we were otherwise using PSK.
  9978. * Now always send it. Define NO_TLSX_PSKKEM_PLAIN_ANNOUNCE
  9979. * to revert to the old behaviour. */
  9980. #ifdef NO_TLSX_PSKKEM_PLAIN_ANNOUNCE
  9981. if (usingPSK)
  9982. #endif
  9983. {
  9984. byte modes;
  9985. (void)usingPSK;
  9986. /* Pre-shared key modes: mandatory extension for resumption. */
  9987. modes = 1 << PSK_KE;
  9988. #if !defined(NO_DH) || defined(HAVE_ECC) || \
  9989. defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  9990. if (!ssl->options.noPskDheKe)
  9991. modes |= 1 << PSK_DHE_KE;
  9992. #endif
  9993. ret = TLSX_PskKeModes_Use(ssl, modes);
  9994. if (ret != 0)
  9995. return ret;
  9996. }
  9997. #endif
  9998. #if defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  9999. if (!isServer && ssl->options.postHandshakeAuth) {
  10000. ret = TLSX_PostHandAuth_Use(ssl);
  10001. if (ret != 0)
  10002. return ret;
  10003. }
  10004. #endif
  10005. }
  10006. #endif
  10007. (void)isServer;
  10008. (void)public_key;
  10009. (void)public_key_len;
  10010. (void)ssl;
  10011. return ret;
  10012. }
  10013. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
  10014. /** Tells the buffered size of extensions to be sent into the client hello. */
  10015. int TLSX_GetRequestSize(WOLFSSL* ssl, byte msgType, word16* pLength)
  10016. {
  10017. int ret = 0;
  10018. word16 length = 0;
  10019. byte semaphore[SEMAPHORE_SIZE] = {0};
  10020. if (!TLSX_SupportExtensions(ssl))
  10021. return 0;
  10022. if (msgType == client_hello) {
  10023. EC_VALIDATE_REQUEST(ssl, semaphore);
  10024. PF_VALIDATE_REQUEST(ssl, semaphore);
  10025. WOLF_STK_VALIDATE_REQUEST(ssl);
  10026. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10027. if (ssl->suites->hashSigAlgoSz == 0)
  10028. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  10029. #endif
  10030. #if defined(WOLFSSL_TLS13)
  10031. if (!IsAtLeastTLSv1_2(ssl))
  10032. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10033. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  10034. if (!IsAtLeastTLSv1_3(ssl->version)) {
  10035. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10036. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10037. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10038. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PSK_KEY_EXCHANGE_MODES));
  10039. #endif
  10040. #ifdef WOLFSSL_EARLY_DATA
  10041. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  10042. #endif
  10043. #ifdef WOLFSSL_SEND_HRR_COOKIE
  10044. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  10045. #endif
  10046. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  10047. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_POST_HANDSHAKE_AUTH));
  10048. #endif
  10049. }
  10050. #endif
  10051. #endif
  10052. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  10053. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  10054. if (!SSL_CM(ssl)->ocspStaplingEnabled) {
  10055. /* mark already sent, so it won't send it */
  10056. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10057. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  10058. }
  10059. #endif
  10060. }
  10061. #ifdef WOLFSSL_TLS13
  10062. #ifndef NO_CERTS
  10063. else if (msgType == certificate_request) {
  10064. /* Don't send out any extension except those that are turned off. */
  10065. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10066. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10067. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  10068. #endif
  10069. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  10070. * TLSX_CERTIFICATE_AUTHORITIES, OID_FILTERS
  10071. * TLSX_STATUS_REQUEST
  10072. */
  10073. }
  10074. #endif
  10075. #endif
  10076. if (ssl->extensions) {
  10077. ret = TLSX_GetSize(ssl->extensions, semaphore, msgType, &length);
  10078. if (ret != 0)
  10079. return ret;
  10080. }
  10081. if (ssl->ctx && ssl->ctx->extensions) {
  10082. ret = TLSX_GetSize(ssl->ctx->extensions, semaphore, msgType, &length);
  10083. if (ret != 0)
  10084. return ret;
  10085. }
  10086. #ifdef HAVE_EXTENDED_MASTER
  10087. if (msgType == client_hello && ssl->options.haveEMS &&
  10088. (!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade)) {
  10089. length += HELLO_EXT_SZ;
  10090. }
  10091. #endif
  10092. if (length)
  10093. length += OPAQUE16_LEN; /* for total length storage. */
  10094. *pLength += length;
  10095. return ret;
  10096. }
  10097. /** Writes the extensions to be sent into the client hello. */
  10098. int TLSX_WriteRequest(WOLFSSL* ssl, byte* output, byte msgType, word16* pOffset)
  10099. {
  10100. int ret = 0;
  10101. word16 offset = 0;
  10102. byte semaphore[SEMAPHORE_SIZE] = {0};
  10103. if (!TLSX_SupportExtensions(ssl) || output == NULL)
  10104. return 0;
  10105. offset += OPAQUE16_LEN; /* extensions length */
  10106. if (msgType == client_hello) {
  10107. EC_VALIDATE_REQUEST(ssl, semaphore);
  10108. PF_VALIDATE_REQUEST(ssl, semaphore);
  10109. WOLF_STK_VALIDATE_REQUEST(ssl);
  10110. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10111. if (ssl->suites->hashSigAlgoSz == 0)
  10112. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  10113. #endif
  10114. #ifdef WOLFSSL_TLS13
  10115. if (!IsAtLeastTLSv1_2(ssl))
  10116. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10117. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  10118. if (!IsAtLeastTLSv1_3(ssl->version)) {
  10119. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10120. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10121. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PSK_KEY_EXCHANGE_MODES));
  10122. #endif
  10123. #ifdef WOLFSSL_EARLY_DATA
  10124. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  10125. #endif
  10126. #ifdef WOLFSSL_SEND_HRR_COOKIE
  10127. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  10128. #endif
  10129. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  10130. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_POST_HANDSHAKE_AUTH));
  10131. #endif
  10132. }
  10133. #endif
  10134. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10135. /* Must write Pre-shared Key extension at the end in TLS v1.3.
  10136. * Must not write out Pre-shared Key extension in earlier versions of
  10137. * protocol.
  10138. */
  10139. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10140. #endif
  10141. #endif
  10142. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  10143. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  10144. /* mark already sent, so it won't send it */
  10145. if (!SSL_CM(ssl)->ocspStaplingEnabled) {
  10146. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10147. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  10148. }
  10149. #endif
  10150. }
  10151. #ifdef WOLFSSL_TLS13
  10152. #ifndef NO_CERTS
  10153. else if (msgType == certificate_request) {
  10154. /* Don't send out any extension except those that are turned off. */
  10155. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10156. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10157. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  10158. #endif
  10159. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  10160. * TLSX_CERTIFICATE_AUTHORITIES, TLSX_OID_FILTERS
  10161. * TLSX_STATUS_REQUEST
  10162. */
  10163. }
  10164. #endif
  10165. #endif
  10166. if (ssl->extensions) {
  10167. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  10168. msgType, &offset);
  10169. if (ret != 0)
  10170. return ret;
  10171. }
  10172. if (ssl->ctx && ssl->ctx->extensions) {
  10173. ret = TLSX_Write(ssl->ctx->extensions, output + offset, semaphore,
  10174. msgType, &offset);
  10175. if (ret != 0)
  10176. return ret;
  10177. }
  10178. #ifdef HAVE_EXTENDED_MASTER
  10179. if (msgType == client_hello && ssl->options.haveEMS &&
  10180. (!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade)) {
  10181. WOLFSSL_MSG("EMS extension to write");
  10182. c16toa(HELLO_EXT_EXTMS, output + offset);
  10183. offset += HELLO_EXT_TYPE_SZ;
  10184. c16toa(0, output + offset);
  10185. offset += HELLO_EXT_SZ_SZ;
  10186. }
  10187. #endif
  10188. #ifdef WOLFSSL_TLS13
  10189. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10190. if (msgType == client_hello && IsAtLeastTLSv1_3(ssl->version)) {
  10191. /* Write out what we can of Pre-shared key extension. */
  10192. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10193. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  10194. client_hello, &offset);
  10195. if (ret != 0)
  10196. return ret;
  10197. }
  10198. #endif
  10199. #endif
  10200. if (offset > OPAQUE16_LEN || msgType != client_hello)
  10201. c16toa(offset - OPAQUE16_LEN, output); /* extensions length */
  10202. *pOffset += offset;
  10203. return ret;
  10204. }
  10205. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
  10206. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_SERVER)
  10207. /** Tells the buffered size of extensions to be sent into the server hello. */
  10208. int TLSX_GetResponseSize(WOLFSSL* ssl, byte msgType, word16* pLength)
  10209. {
  10210. int ret = 0;
  10211. word16 length = 0;
  10212. byte semaphore[SEMAPHORE_SIZE] = {0};
  10213. switch (msgType) {
  10214. #ifndef NO_WOLFSSL_SERVER
  10215. case server_hello:
  10216. PF_VALIDATE_RESPONSE(ssl, semaphore);
  10217. #ifdef WOLFSSL_TLS13
  10218. if (IsAtLeastTLSv1_3(ssl->version)) {
  10219. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10220. TURN_OFF(semaphore,
  10221. TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10222. #ifdef HAVE_SUPPORTED_CURVES
  10223. if (!ssl->options.noPskDheKe)
  10224. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10225. #endif
  10226. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10227. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10228. #endif
  10229. #ifdef WOLFSSL_DTLS_CID
  10230. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  10231. #endif /* WOLFSSL_DTLS_CID */
  10232. }
  10233. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  10234. else {
  10235. #ifdef HAVE_SUPPORTED_CURVES
  10236. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10237. #endif
  10238. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10239. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10240. #endif
  10241. }
  10242. #endif
  10243. #endif
  10244. break;
  10245. #ifdef WOLFSSL_TLS13
  10246. case hello_retry_request:
  10247. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10248. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10249. #ifdef HAVE_SUPPORTED_CURVES
  10250. if (!ssl->options.noPskDheKe)
  10251. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10252. #endif
  10253. #ifdef WOLFSSL_SEND_HRR_COOKIE
  10254. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  10255. #endif
  10256. break;
  10257. #endif
  10258. #ifdef WOLFSSL_TLS13
  10259. case encrypted_extensions:
  10260. /* Send out all extension except those that are turned on. */
  10261. #ifdef HAVE_ECC
  10262. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  10263. #endif
  10264. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10265. #ifdef HAVE_SESSION_TICKET
  10266. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SESSION_TICKET));
  10267. #endif
  10268. #ifdef HAVE_SUPPORTED_CURVES
  10269. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10270. #endif
  10271. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10272. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10273. #endif
  10274. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  10275. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10276. #endif
  10277. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  10278. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  10279. #endif
  10280. #if defined(HAVE_SERVER_RENEGOTIATION_INFO)
  10281. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_RENEGOTIATION_INFO));
  10282. #endif
  10283. #ifdef WOLFSSL_DTLS_CID
  10284. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  10285. #endif /* WOLFSSL_DTLS_CID */
  10286. break;
  10287. #ifdef WOLFSSL_EARLY_DATA
  10288. case session_ticket:
  10289. if (ssl->options.tls1_3) {
  10290. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10291. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  10292. }
  10293. break;
  10294. #endif
  10295. #endif
  10296. #endif
  10297. #ifdef WOLFSSL_TLS13
  10298. #ifndef NO_CERTS
  10299. case certificate:
  10300. /* Don't send out any extension except those that are turned off. */
  10301. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10302. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10303. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  10304. * TLSX_SERVER_CERTIFICATE_TYPE
  10305. */
  10306. break;
  10307. #endif
  10308. #endif
  10309. }
  10310. #ifdef HAVE_EXTENDED_MASTER
  10311. if (ssl->options.haveEMS && msgType == server_hello &&
  10312. !IsAtLeastTLSv1_3(ssl->version)) {
  10313. length += HELLO_EXT_SZ;
  10314. }
  10315. #endif
  10316. if (TLSX_SupportExtensions(ssl)) {
  10317. ret = TLSX_GetSize(ssl->extensions, semaphore, msgType, &length);
  10318. if (ret != 0)
  10319. return ret;
  10320. }
  10321. /* All the response data is set at the ssl object only, so no ctx here. */
  10322. if (length || msgType != server_hello)
  10323. length += OPAQUE16_LEN; /* for total length storage. */
  10324. *pLength += length;
  10325. return ret;
  10326. }
  10327. /** Writes the server hello extensions into a buffer. */
  10328. int TLSX_WriteResponse(WOLFSSL *ssl, byte* output, byte msgType, word16* pOffset)
  10329. {
  10330. int ret = 0;
  10331. word16 offset = 0;
  10332. if (TLSX_SupportExtensions(ssl) && output) {
  10333. byte semaphore[SEMAPHORE_SIZE] = {0};
  10334. switch (msgType) {
  10335. #ifndef NO_WOLFSSL_SERVER
  10336. case server_hello:
  10337. PF_VALIDATE_RESPONSE(ssl, semaphore);
  10338. #ifdef WOLFSSL_TLS13
  10339. if (IsAtLeastTLSv1_3(ssl->version)) {
  10340. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10341. TURN_OFF(semaphore,
  10342. TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10343. #ifdef HAVE_SUPPORTED_CURVES
  10344. if (!ssl->options.noPskDheKe)
  10345. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10346. #endif
  10347. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10348. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10349. #endif
  10350. #ifdef WOLFSSL_DTLS_CID
  10351. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  10352. #endif /* WOLFSSL_DTLS_CID */
  10353. }
  10354. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  10355. else {
  10356. #ifdef HAVE_SUPPORTED_CURVES
  10357. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10358. #endif
  10359. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10360. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10361. #endif
  10362. }
  10363. #endif
  10364. #endif
  10365. break;
  10366. #ifdef WOLFSSL_TLS13
  10367. case hello_retry_request:
  10368. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10369. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10370. #ifdef HAVE_SUPPORTED_CURVES
  10371. if (!ssl->options.noPskDheKe)
  10372. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10373. #endif
  10374. /* Cookie is written below as last extension. */
  10375. break;
  10376. #endif
  10377. #ifdef WOLFSSL_TLS13
  10378. case encrypted_extensions:
  10379. /* Send out all extension except those that are turned on. */
  10380. #ifdef HAVE_ECC
  10381. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  10382. #endif
  10383. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10384. #ifdef HAVE_SESSION_TICKET
  10385. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SESSION_TICKET));
  10386. #endif
  10387. #ifdef HAVE_SUPPORTED_CURVES
  10388. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10389. #endif
  10390. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10391. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10392. #endif
  10393. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  10394. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10395. #endif
  10396. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  10397. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  10398. #endif
  10399. #if defined(HAVE_SERVER_RENEGOTIATION_INFO)
  10400. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_RENEGOTIATION_INFO));
  10401. #endif
  10402. #ifdef WOLFSSL_DTLS_CID
  10403. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  10404. #endif /* WOLFSSL_DTLS_CID */
  10405. break;
  10406. #ifdef WOLFSSL_EARLY_DATA
  10407. case session_ticket:
  10408. if (ssl->options.tls1_3) {
  10409. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10410. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  10411. }
  10412. break;
  10413. #endif
  10414. #endif
  10415. #endif
  10416. #ifdef WOLFSSL_TLS13
  10417. #ifndef NO_CERTS
  10418. case certificate:
  10419. /* Don't send out any extension except those that are turned
  10420. * off. */
  10421. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10422. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10423. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  10424. * TLSX_SERVER_CERTIFICATE_TYPE
  10425. */
  10426. break;
  10427. #endif
  10428. #endif
  10429. default:
  10430. break;
  10431. }
  10432. offset += OPAQUE16_LEN; /* extensions length */
  10433. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  10434. msgType, &offset);
  10435. if (ret != 0)
  10436. return ret;
  10437. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  10438. if (msgType == hello_retry_request) {
  10439. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10440. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  10441. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  10442. msgType, &offset);
  10443. if (ret != 0)
  10444. return ret;
  10445. }
  10446. #endif
  10447. #ifdef HAVE_EXTENDED_MASTER
  10448. if (ssl->options.haveEMS && msgType == server_hello &&
  10449. !IsAtLeastTLSv1_3(ssl->version)) {
  10450. WOLFSSL_MSG("EMS extension to write");
  10451. c16toa(HELLO_EXT_EXTMS, output + offset);
  10452. offset += HELLO_EXT_TYPE_SZ;
  10453. c16toa(0, output + offset);
  10454. offset += HELLO_EXT_SZ_SZ;
  10455. }
  10456. #endif
  10457. if (offset > OPAQUE16_LEN || msgType != server_hello)
  10458. c16toa(offset - OPAQUE16_LEN, output); /* extensions length */
  10459. }
  10460. if (pOffset)
  10461. *pOffset += offset;
  10462. return ret;
  10463. }
  10464. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_SERVER */
  10465. #ifdef WOLFSSL_TLS13
  10466. int TLSX_ParseVersion(WOLFSSL* ssl, const byte* input, word16 length,
  10467. byte msgType, int* found)
  10468. {
  10469. int ret = 0;
  10470. int offset = 0;
  10471. *found = 0;
  10472. while (offset < (int)length) {
  10473. word16 type;
  10474. word16 size;
  10475. if (offset + (2 * OPAQUE16_LEN) > length) {
  10476. ret = BUFFER_ERROR;
  10477. break;
  10478. }
  10479. ato16(input + offset, &type);
  10480. offset += HELLO_EXT_TYPE_SZ;
  10481. ato16(input + offset, &size);
  10482. offset += OPAQUE16_LEN;
  10483. if (offset + size > length) {
  10484. ret = BUFFER_ERROR;
  10485. break;
  10486. }
  10487. if (type == TLSX_SUPPORTED_VERSIONS) {
  10488. *found = 1;
  10489. WOLFSSL_MSG("Supported Versions extension received");
  10490. ret = SV_PARSE(ssl, input + offset, size, msgType);
  10491. break;
  10492. }
  10493. offset += size;
  10494. }
  10495. return ret;
  10496. }
  10497. #endif
  10498. /** Parses a buffer of TLS extensions. */
  10499. int TLSX_Parse(WOLFSSL* ssl, const byte* input, word16 length, byte msgType,
  10500. Suites *suites)
  10501. {
  10502. int ret = 0;
  10503. word16 offset = 0;
  10504. byte isRequest = (msgType == client_hello ||
  10505. msgType == certificate_request);
  10506. #ifdef HAVE_EXTENDED_MASTER
  10507. byte pendingEMS = 0;
  10508. #endif
  10509. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  10510. int pskDone = 0;
  10511. #endif
  10512. if (!ssl || !input || (isRequest && !suites))
  10513. return BAD_FUNC_ARG;
  10514. while (ret == 0 && offset < length) {
  10515. word16 type;
  10516. word16 size;
  10517. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  10518. if (msgType == client_hello && pskDone) {
  10519. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  10520. return PSK_KEY_ERROR;
  10521. }
  10522. #endif
  10523. if (length - offset < HELLO_EXT_TYPE_SZ + OPAQUE16_LEN)
  10524. return BUFFER_ERROR;
  10525. ato16(input + offset, &type);
  10526. offset += HELLO_EXT_TYPE_SZ;
  10527. ato16(input + offset, &size);
  10528. offset += OPAQUE16_LEN;
  10529. if (length - offset < size)
  10530. return BUFFER_ERROR;
  10531. switch (type) {
  10532. #ifdef HAVE_SNI
  10533. case TLSX_SERVER_NAME:
  10534. WOLFSSL_MSG("SNI extension received");
  10535. #ifdef WOLFSSL_DEBUG_TLS
  10536. WOLFSSL_BUFFER(input + offset, size);
  10537. #endif
  10538. #ifdef WOLFSSL_TLS13
  10539. if (IsAtLeastTLSv1_3(ssl->version)) {
  10540. if (msgType != client_hello &&
  10541. msgType != encrypted_extensions)
  10542. return EXT_NOT_ALLOWED;
  10543. }
  10544. else
  10545. #endif
  10546. {
  10547. if (msgType != client_hello)
  10548. return EXT_NOT_ALLOWED;
  10549. }
  10550. ret = SNI_PARSE(ssl, input + offset, size, isRequest);
  10551. break;
  10552. #endif
  10553. case TLSX_TRUSTED_CA_KEYS:
  10554. WOLFSSL_MSG("Trusted CA extension received");
  10555. #ifdef WOLFSSL_DEBUG_TLS
  10556. WOLFSSL_BUFFER(input + offset, size);
  10557. #endif
  10558. #ifdef WOLFSSL_TLS13
  10559. if (IsAtLeastTLSv1_3(ssl->version)) {
  10560. if (msgType != client_hello &&
  10561. msgType != encrypted_extensions)
  10562. return EXT_NOT_ALLOWED;
  10563. }
  10564. else
  10565. #endif
  10566. {
  10567. if (msgType != client_hello)
  10568. return EXT_NOT_ALLOWED;
  10569. }
  10570. ret = TCA_PARSE(ssl, input + offset, size, isRequest);
  10571. break;
  10572. case TLSX_MAX_FRAGMENT_LENGTH:
  10573. WOLFSSL_MSG("Max Fragment Length extension received");
  10574. #ifdef WOLFSSL_DEBUG_TLS
  10575. WOLFSSL_BUFFER(input + offset, size);
  10576. #endif
  10577. #ifdef WOLFSSL_TLS13
  10578. if (IsAtLeastTLSv1_3(ssl->version)) {
  10579. if (msgType != client_hello &&
  10580. msgType != encrypted_extensions) {
  10581. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  10582. return EXT_NOT_ALLOWED;
  10583. }
  10584. }
  10585. else
  10586. #endif
  10587. {
  10588. if (msgType != client_hello &&
  10589. msgType != server_hello) {
  10590. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  10591. return EXT_NOT_ALLOWED;
  10592. }
  10593. }
  10594. ret = MFL_PARSE(ssl, input + offset, size, isRequest);
  10595. break;
  10596. case TLSX_TRUNCATED_HMAC:
  10597. WOLFSSL_MSG("Truncated HMAC extension received");
  10598. #ifdef WOLFSSL_DEBUG_TLS
  10599. WOLFSSL_BUFFER(input + offset, size);
  10600. #endif
  10601. #ifdef WOLFSSL_TLS13
  10602. if (IsAtLeastTLSv1_3(ssl->version))
  10603. break;
  10604. #endif
  10605. if (msgType != client_hello)
  10606. return EXT_NOT_ALLOWED;
  10607. ret = THM_PARSE(ssl, input + offset, size, isRequest);
  10608. break;
  10609. case TLSX_SUPPORTED_GROUPS:
  10610. WOLFSSL_MSG("Supported Groups extension received");
  10611. #ifdef WOLFSSL_DEBUG_TLS
  10612. WOLFSSL_BUFFER(input + offset, size);
  10613. #endif
  10614. #ifdef WOLFSSL_TLS13
  10615. if (IsAtLeastTLSv1_3(ssl->version)) {
  10616. if (msgType != client_hello &&
  10617. msgType != encrypted_extensions) {
  10618. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  10619. return EXT_NOT_ALLOWED;
  10620. }
  10621. }
  10622. else
  10623. #endif
  10624. {
  10625. if (msgType != client_hello) {
  10626. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  10627. return EXT_NOT_ALLOWED;
  10628. }
  10629. }
  10630. ret = EC_PARSE(ssl, input + offset, size, isRequest);
  10631. break;
  10632. case TLSX_EC_POINT_FORMATS:
  10633. WOLFSSL_MSG("Point Formats extension received");
  10634. #ifdef WOLFSSL_DEBUG_TLS
  10635. WOLFSSL_BUFFER(input + offset, size);
  10636. #endif
  10637. #ifdef WOLFSSL_TLS13
  10638. if (IsAtLeastTLSv1_3(ssl->version))
  10639. break;
  10640. #endif
  10641. if (msgType != client_hello &&
  10642. msgType != server_hello) {
  10643. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  10644. return EXT_NOT_ALLOWED;
  10645. }
  10646. ret = PF_PARSE(ssl, input + offset, size, isRequest);
  10647. break;
  10648. case TLSX_STATUS_REQUEST:
  10649. WOLFSSL_MSG("Certificate Status Request extension received");
  10650. #ifdef WOLFSSL_DEBUG_TLS
  10651. WOLFSSL_BUFFER(input + offset, size);
  10652. #endif
  10653. #ifdef WOLFSSL_TLS13
  10654. if (IsAtLeastTLSv1_3(ssl->version)) {
  10655. if (msgType != client_hello &&
  10656. msgType != certificate_request &&
  10657. msgType != certificate)
  10658. return EXT_NOT_ALLOWED;
  10659. }
  10660. else
  10661. #endif
  10662. {
  10663. if (msgType != client_hello &&
  10664. msgType != server_hello)
  10665. return EXT_NOT_ALLOWED;
  10666. }
  10667. ret = CSR_PARSE(ssl, input + offset, size, isRequest);
  10668. break;
  10669. case TLSX_STATUS_REQUEST_V2:
  10670. WOLFSSL_MSG("Certificate Status Request v2 extension received");
  10671. #ifdef WOLFSSL_DEBUG_TLS
  10672. WOLFSSL_BUFFER(input + offset, size);
  10673. #endif
  10674. #if defined(WOLFSSL_TLS13) && defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  10675. if (IsAtLeastTLSv1_3(ssl->version)) {
  10676. if (msgType != client_hello &&
  10677. msgType != certificate_request &&
  10678. msgType != certificate)
  10679. return EXT_NOT_ALLOWED;
  10680. }
  10681. else
  10682. #endif
  10683. {
  10684. if (msgType != client_hello &&
  10685. msgType != server_hello)
  10686. return EXT_NOT_ALLOWED;
  10687. }
  10688. ret = CSR2_PARSE(ssl, input + offset, size, isRequest);
  10689. break;
  10690. #ifdef HAVE_EXTENDED_MASTER
  10691. case HELLO_EXT_EXTMS:
  10692. WOLFSSL_MSG("Extended Master Secret extension received");
  10693. #ifdef WOLFSSL_DEBUG_TLS
  10694. WOLFSSL_BUFFER(input + offset, size);
  10695. #endif
  10696. #if defined(WOLFSSL_TLS13)
  10697. if (IsAtLeastTLSv1_3(ssl->version))
  10698. break;
  10699. #endif
  10700. if (msgType != client_hello &&
  10701. msgType != server_hello)
  10702. return EXT_NOT_ALLOWED;
  10703. if (size != 0)
  10704. return BUFFER_ERROR;
  10705. #ifndef NO_WOLFSSL_SERVER
  10706. if (isRequest)
  10707. ssl->options.haveEMS = 1;
  10708. #endif
  10709. pendingEMS = 1;
  10710. break;
  10711. #endif
  10712. case TLSX_RENEGOTIATION_INFO:
  10713. WOLFSSL_MSG("Secure Renegotiation extension received");
  10714. #ifdef WOLFSSL_DEBUG_TLS
  10715. WOLFSSL_BUFFER(input + offset, size);
  10716. #endif
  10717. #ifdef WOLFSSL_TLS13
  10718. if (IsAtLeastTLSv1_3(ssl->version))
  10719. break;
  10720. #endif
  10721. if (msgType != client_hello &&
  10722. msgType != server_hello)
  10723. return EXT_NOT_ALLOWED;
  10724. ret = SCR_PARSE(ssl, input + offset, size, isRequest);
  10725. break;
  10726. case TLSX_SESSION_TICKET:
  10727. WOLFSSL_MSG("Session Ticket extension received");
  10728. #ifdef WOLFSSL_DEBUG_TLS
  10729. WOLFSSL_BUFFER(input + offset, size);
  10730. #endif
  10731. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)
  10732. if (IsAtLeastTLSv1_3(ssl->version)) {
  10733. if (msgType != client_hello)
  10734. return EXT_NOT_ALLOWED;
  10735. }
  10736. else
  10737. #endif
  10738. {
  10739. if (msgType != client_hello &&
  10740. msgType != server_hello)
  10741. return EXT_NOT_ALLOWED;
  10742. }
  10743. ret = WOLF_STK_PARSE(ssl, input + offset, size, isRequest);
  10744. break;
  10745. case TLSX_APPLICATION_LAYER_PROTOCOL:
  10746. WOLFSSL_MSG("ALPN extension received");
  10747. #ifdef WOLFSSL_DEBUG_TLS
  10748. WOLFSSL_BUFFER(input + offset, size);
  10749. #endif
  10750. #if defined(WOLFSSL_TLS13) && defined(HAVE_ALPN)
  10751. if (IsAtLeastTLSv1_3(ssl->version)) {
  10752. if (msgType != client_hello &&
  10753. msgType != encrypted_extensions)
  10754. return EXT_NOT_ALLOWED;
  10755. }
  10756. else
  10757. #endif
  10758. {
  10759. if (msgType != client_hello &&
  10760. msgType != server_hello)
  10761. return EXT_NOT_ALLOWED;
  10762. }
  10763. ret = ALPN_PARSE(ssl, input + offset, size, isRequest);
  10764. break;
  10765. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10766. case TLSX_SIGNATURE_ALGORITHMS:
  10767. WOLFSSL_MSG("Signature Algorithms extension received");
  10768. #ifdef WOLFSSL_DEBUG_TLS
  10769. WOLFSSL_BUFFER(input + offset, size);
  10770. #endif
  10771. if (!IsAtLeastTLSv1_2(ssl))
  10772. break;
  10773. #ifdef WOLFSSL_TLS13
  10774. if (IsAtLeastTLSv1_3(ssl->version)) {
  10775. if (msgType != client_hello &&
  10776. msgType != certificate_request)
  10777. return EXT_NOT_ALLOWED;
  10778. }
  10779. else
  10780. #endif
  10781. {
  10782. if (msgType != client_hello)
  10783. return EXT_NOT_ALLOWED;
  10784. }
  10785. ret = SA_PARSE(ssl, input + offset, size, isRequest, suites);
  10786. break;
  10787. #endif
  10788. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  10789. case TLSX_ENCRYPT_THEN_MAC:
  10790. WOLFSSL_MSG("Encrypt-Then-Mac extension received");
  10791. /* Ignore for TLS 1.3+ */
  10792. if (IsAtLeastTLSv1_3(ssl->version))
  10793. break;
  10794. if (msgType != client_hello &&
  10795. msgType != server_hello)
  10796. return EXT_NOT_ALLOWED;
  10797. ret = ETM_PARSE(ssl, input + offset, size, msgType);
  10798. break;
  10799. #endif /* HAVE_ENCRYPT_THEN_MAC */
  10800. #ifdef WOLFSSL_TLS13
  10801. case TLSX_SUPPORTED_VERSIONS:
  10802. WOLFSSL_MSG("Skipping Supported Versions - already processed");
  10803. #ifdef WOLFSSL_DEBUG_TLS
  10804. WOLFSSL_BUFFER(input + offset, size);
  10805. #endif
  10806. if (msgType != client_hello &&
  10807. msgType != server_hello &&
  10808. msgType != hello_retry_request)
  10809. return EXT_NOT_ALLOWED;
  10810. break;
  10811. #ifdef WOLFSSL_SEND_HRR_COOKIE
  10812. case TLSX_COOKIE:
  10813. WOLFSSL_MSG("Cookie extension received");
  10814. #ifdef WOLFSSL_DEBUG_TLS
  10815. WOLFSSL_BUFFER(input + offset, size);
  10816. #endif
  10817. if (!IsAtLeastTLSv1_3(ssl->version))
  10818. break;
  10819. if (msgType != client_hello &&
  10820. msgType != hello_retry_request) {
  10821. return EXT_NOT_ALLOWED;
  10822. }
  10823. ret = CKE_PARSE(ssl, input + offset, size, msgType);
  10824. break;
  10825. #endif
  10826. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10827. case TLSX_PRE_SHARED_KEY:
  10828. WOLFSSL_MSG("Pre-Shared Key extension received");
  10829. #ifdef WOLFSSL_DEBUG_TLS
  10830. WOLFSSL_BUFFER(input + offset, size);
  10831. #endif
  10832. if (!IsAtLeastTLSv1_3(ssl->version))
  10833. break;
  10834. if (msgType != client_hello &&
  10835. msgType != server_hello) {
  10836. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  10837. return EXT_NOT_ALLOWED;
  10838. }
  10839. ret = PSK_PARSE(ssl, input + offset, size, msgType);
  10840. pskDone = 1;
  10841. break;
  10842. case TLSX_PSK_KEY_EXCHANGE_MODES:
  10843. WOLFSSL_MSG("PSK Key Exchange Modes extension received");
  10844. #ifdef WOLFSSL_DEBUG_TLS
  10845. WOLFSSL_BUFFER(input + offset, size);
  10846. #endif
  10847. if (!IsAtLeastTLSv1_3(ssl->version))
  10848. break;
  10849. if (msgType != client_hello) {
  10850. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  10851. return EXT_NOT_ALLOWED;
  10852. }
  10853. ret = PKM_PARSE(ssl, input + offset, size, msgType);
  10854. break;
  10855. #endif
  10856. #ifdef WOLFSSL_EARLY_DATA
  10857. case TLSX_EARLY_DATA:
  10858. WOLFSSL_MSG("Early Data extension received");
  10859. #ifdef WOLFSSL_DEBUG_TLS
  10860. WOLFSSL_BUFFER(input + offset, size);
  10861. #endif
  10862. if (!IsAtLeastTLSv1_3(ssl->version))
  10863. break;
  10864. if (msgType != client_hello && msgType != session_ticket &&
  10865. msgType != encrypted_extensions) {
  10866. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  10867. return EXT_NOT_ALLOWED;
  10868. }
  10869. ret = EDI_PARSE(ssl, input + offset, size, msgType);
  10870. break;
  10871. #endif
  10872. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  10873. case TLSX_POST_HANDSHAKE_AUTH:
  10874. WOLFSSL_MSG("Post Handshake Authentication extension received");
  10875. #ifdef WOLFSSL_DEBUG_TLS
  10876. WOLFSSL_BUFFER(input + offset, size);
  10877. #endif
  10878. if (!IsAtLeastTLSv1_3(ssl->version))
  10879. break;
  10880. if (msgType != client_hello) {
  10881. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  10882. return EXT_NOT_ALLOWED;
  10883. }
  10884. ret = PHA_PARSE(ssl, input + offset, size, msgType);
  10885. break;
  10886. #endif
  10887. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10888. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  10889. WOLFSSL_MSG("Signature Algorithms extension received");
  10890. #ifdef WOLFSSL_DEBUG_TLS
  10891. WOLFSSL_BUFFER(input + offset, size);
  10892. #endif
  10893. if (!IsAtLeastTLSv1_3(ssl->version))
  10894. break;
  10895. if (msgType != client_hello &&
  10896. msgType != certificate_request) {
  10897. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  10898. return EXT_NOT_ALLOWED;
  10899. }
  10900. ret = SAC_PARSE(ssl, input + offset, size, isRequest);
  10901. break;
  10902. #endif
  10903. case TLSX_KEY_SHARE:
  10904. WOLFSSL_MSG("Key Share extension received");
  10905. #ifdef WOLFSSL_DEBUG_TLS
  10906. WOLFSSL_BUFFER(input + offset, size);
  10907. #endif
  10908. #ifdef HAVE_SUPPORTED_CURVES
  10909. if (!IsAtLeastTLSv1_3(ssl->version))
  10910. break;
  10911. if (msgType != client_hello && msgType != server_hello &&
  10912. msgType != hello_retry_request) {
  10913. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  10914. return EXT_NOT_ALLOWED;
  10915. }
  10916. #endif
  10917. ret = KS_PARSE(ssl, input + offset, size, msgType);
  10918. break;
  10919. #endif
  10920. #ifdef WOLFSSL_SRTP
  10921. case TLSX_USE_SRTP:
  10922. WOLFSSL_MSG("Use SRTP extension received");
  10923. ret = SRTP_PARSE(ssl, input + offset, size, isRequest);
  10924. break;
  10925. #endif
  10926. #ifdef WOLFSSL_QUIC
  10927. case TLSX_KEY_QUIC_TP_PARAMS:
  10928. FALL_THROUGH;
  10929. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  10930. WOLFSSL_MSG("QUIC transport parameter received");
  10931. #ifdef WOLFSSL_DEBUG_TLS
  10932. WOLFSSL_BUFFER(input + offset, size);
  10933. #endif
  10934. if (IsAtLeastTLSv1_3(ssl->version) &&
  10935. msgType != client_hello &&
  10936. msgType != server_hello &&
  10937. msgType != encrypted_extensions) {
  10938. return EXT_NOT_ALLOWED;
  10939. }
  10940. else if (!IsAtLeastTLSv1_3(ssl->version) &&
  10941. msgType == encrypted_extensions) {
  10942. return EXT_NOT_ALLOWED;
  10943. }
  10944. else if (WOLFSSL_IS_QUIC(ssl)) {
  10945. ret = QTP_PARSE(ssl, input + offset, size, type, msgType);
  10946. }
  10947. else {
  10948. WOLFSSL_MSG("QUIC transport param TLS extension type, but no QUIC");
  10949. return EXT_NOT_ALLOWED; /* be safe, this should not happen */
  10950. }
  10951. break;
  10952. #endif /* WOLFSSL_QUIC */
  10953. #if defined(WOLFSSL_DTLS_CID)
  10954. case TLSX_CONNECTION_ID:
  10955. /* connection ID not supported in DTLSv1.2 */
  10956. if (!IsAtLeastTLSv1_3(ssl->version))
  10957. break;
  10958. if (msgType != client_hello && msgType != server_hello)
  10959. return EXT_NOT_ALLOWED;
  10960. WOLFSSL_MSG("ConnectionID extension received");
  10961. ret = CID_PARSE(ssl, input + offset, size, isRequest);
  10962. break;
  10963. #endif /* defined(WOLFSSL_DTLS_CID) */
  10964. default:
  10965. WOLFSSL_MSG("Unknown TLS extension type");
  10966. }
  10967. /* offset should be updated here! */
  10968. offset += size;
  10969. }
  10970. #ifdef HAVE_EXTENDED_MASTER
  10971. if (IsAtLeastTLSv1_3(ssl->version) && msgType == hello_retry_request) {
  10972. /* Don't change EMS status until server_hello received.
  10973. * Second ClientHello must have same extensions.
  10974. */
  10975. }
  10976. else if (!isRequest && ssl->options.haveEMS && !pendingEMS)
  10977. ssl->options.haveEMS = 0;
  10978. #endif
  10979. if (ret == 0)
  10980. ret = SNI_VERIFY_PARSE(ssl, isRequest);
  10981. if (ret == 0)
  10982. ret = TCA_VERIFY_PARSE(ssl, isRequest);
  10983. return ret;
  10984. }
  10985. /* undefining semaphore macros */
  10986. #undef IS_OFF
  10987. #undef TURN_ON
  10988. #undef SEMAPHORE_SIZE
  10989. #endif /* HAVE_TLS_EXTENSIONS */
  10990. #ifndef NO_WOLFSSL_CLIENT
  10991. WOLFSSL_METHOD* wolfTLS_client_method(void)
  10992. {
  10993. return wolfTLS_client_method_ex(NULL);
  10994. }
  10995. WOLFSSL_METHOD* wolfTLS_client_method_ex(void* heap)
  10996. {
  10997. WOLFSSL_METHOD* method =
  10998. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  10999. heap, DYNAMIC_TYPE_METHOD);
  11000. (void)heap;
  11001. WOLFSSL_ENTER("TLS_client_method_ex");
  11002. if (method) {
  11003. #if defined(WOLFSSL_TLS13)
  11004. InitSSL_Method(method, MakeTLSv1_3());
  11005. #elif !defined(WOLFSSL_NO_TLS12)
  11006. InitSSL_Method(method, MakeTLSv1_2());
  11007. #elif !defined(NO_OLD_TLS)
  11008. InitSSL_Method(method, MakeTLSv1_1());
  11009. #elif defined(WOLFSSL_ALLOW_TLSV10)
  11010. InitSSL_Method(method, MakeTLSv1());
  11011. #else
  11012. #error No TLS version enabled!
  11013. #endif
  11014. method->downgrade = 1;
  11015. method->side = WOLFSSL_CLIENT_END;
  11016. }
  11017. return method;
  11018. }
  11019. #ifndef NO_OLD_TLS
  11020. #ifdef WOLFSSL_ALLOW_TLSV10
  11021. WOLFSSL_METHOD* wolfTLSv1_client_method(void)
  11022. {
  11023. return wolfTLSv1_client_method_ex(NULL);
  11024. }
  11025. WOLFSSL_METHOD* wolfTLSv1_client_method_ex(void* heap)
  11026. {
  11027. WOLFSSL_METHOD* method =
  11028. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11029. heap, DYNAMIC_TYPE_METHOD);
  11030. (void)heap;
  11031. WOLFSSL_ENTER("TLSv1_client_method_ex");
  11032. if (method)
  11033. InitSSL_Method(method, MakeTLSv1());
  11034. return method;
  11035. }
  11036. #endif /* WOLFSSL_ALLOW_TLSV10 */
  11037. WOLFSSL_METHOD* wolfTLSv1_1_client_method(void)
  11038. {
  11039. return wolfTLSv1_1_client_method_ex(NULL);
  11040. }
  11041. WOLFSSL_METHOD* wolfTLSv1_1_client_method_ex(void* heap)
  11042. {
  11043. WOLFSSL_METHOD* method =
  11044. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11045. heap, DYNAMIC_TYPE_METHOD);
  11046. (void)heap;
  11047. WOLFSSL_ENTER("TLSv1_1_client_method_ex");
  11048. if (method)
  11049. InitSSL_Method(method, MakeTLSv1_1());
  11050. return method;
  11051. }
  11052. #endif /* !NO_OLD_TLS */
  11053. #ifndef WOLFSSL_NO_TLS12
  11054. WOLFSSL_ABI
  11055. WOLFSSL_METHOD* wolfTLSv1_2_client_method(void)
  11056. {
  11057. return wolfTLSv1_2_client_method_ex(NULL);
  11058. }
  11059. WOLFSSL_METHOD* wolfTLSv1_2_client_method_ex(void* heap)
  11060. {
  11061. WOLFSSL_METHOD* method =
  11062. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11063. heap, DYNAMIC_TYPE_METHOD);
  11064. (void)heap;
  11065. WOLFSSL_ENTER("TLSv1_2_client_method_ex");
  11066. if (method)
  11067. InitSSL_Method(method, MakeTLSv1_2());
  11068. return method;
  11069. }
  11070. #endif /* WOLFSSL_NO_TLS12 */
  11071. #ifdef WOLFSSL_TLS13
  11072. /* The TLS v1.3 client method data.
  11073. *
  11074. * returns the method data for a TLS v1.3 client.
  11075. */
  11076. WOLFSSL_ABI
  11077. WOLFSSL_METHOD* wolfTLSv1_3_client_method(void)
  11078. {
  11079. return wolfTLSv1_3_client_method_ex(NULL);
  11080. }
  11081. /* The TLS v1.3 client method data.
  11082. *
  11083. * heap The heap used for allocation.
  11084. * returns the method data for a TLS v1.3 client.
  11085. */
  11086. WOLFSSL_METHOD* wolfTLSv1_3_client_method_ex(void* heap)
  11087. {
  11088. WOLFSSL_METHOD* method = (WOLFSSL_METHOD*)
  11089. XMALLOC(sizeof(WOLFSSL_METHOD), heap,
  11090. DYNAMIC_TYPE_METHOD);
  11091. (void)heap;
  11092. WOLFSSL_ENTER("TLSv1_3_client_method_ex");
  11093. if (method)
  11094. InitSSL_Method(method, MakeTLSv1_3());
  11095. return method;
  11096. }
  11097. #endif /* WOLFSSL_TLS13 */
  11098. #ifdef WOLFSSL_DTLS
  11099. WOLFSSL_METHOD* wolfDTLS_client_method(void)
  11100. {
  11101. return wolfDTLS_client_method_ex(NULL);
  11102. }
  11103. WOLFSSL_METHOD* wolfDTLS_client_method_ex(void* heap)
  11104. {
  11105. WOLFSSL_METHOD* method =
  11106. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11107. heap, DYNAMIC_TYPE_METHOD);
  11108. (void)heap;
  11109. WOLFSSL_ENTER("DTLS_client_method_ex");
  11110. if (method) {
  11111. #if defined(WOLFSSL_DTLS13)
  11112. InitSSL_Method(method, MakeDTLSv1_3());
  11113. #elif !defined(WOLFSSL_NO_TLS12)
  11114. InitSSL_Method(method, MakeDTLSv1_2());
  11115. #elif !defined(NO_OLD_TLS)
  11116. InitSSL_Method(method, MakeDTLSv1());
  11117. #else
  11118. #error No DTLS version enabled!
  11119. #endif
  11120. method->downgrade = 1;
  11121. method->side = WOLFSSL_CLIENT_END;
  11122. }
  11123. return method;
  11124. }
  11125. #ifndef NO_OLD_TLS
  11126. WOLFSSL_METHOD* wolfDTLSv1_client_method(void)
  11127. {
  11128. return wolfDTLSv1_client_method_ex(NULL);
  11129. }
  11130. WOLFSSL_METHOD* wolfDTLSv1_client_method_ex(void* heap)
  11131. {
  11132. WOLFSSL_METHOD* method =
  11133. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11134. heap, DYNAMIC_TYPE_METHOD);
  11135. (void)heap;
  11136. WOLFSSL_ENTER("DTLSv1_client_method_ex");
  11137. if (method)
  11138. InitSSL_Method(method, MakeDTLSv1());
  11139. return method;
  11140. }
  11141. #endif /* NO_OLD_TLS */
  11142. #ifndef WOLFSSL_NO_TLS12
  11143. WOLFSSL_METHOD* wolfDTLSv1_2_client_method(void)
  11144. {
  11145. return wolfDTLSv1_2_client_method_ex(NULL);
  11146. }
  11147. WOLFSSL_METHOD* wolfDTLSv1_2_client_method_ex(void* heap)
  11148. {
  11149. WOLFSSL_METHOD* method =
  11150. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11151. heap, DYNAMIC_TYPE_METHOD);
  11152. (void)heap;
  11153. WOLFSSL_ENTER("DTLSv1_2_client_method_ex");
  11154. if (method)
  11155. InitSSL_Method(method, MakeDTLSv1_2());
  11156. (void)heap;
  11157. return method;
  11158. }
  11159. #endif /* !WOLFSSL_NO_TLS12 */
  11160. #endif /* WOLFSSL_DTLS */
  11161. #endif /* NO_WOLFSSL_CLIENT */
  11162. /* EITHER SIDE METHODS */
  11163. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  11164. #ifndef NO_OLD_TLS
  11165. #ifdef WOLFSSL_ALLOW_TLSV10
  11166. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  11167. *
  11168. * Returns a pointer to a WOLFSSL_METHOD struct
  11169. */
  11170. WOLFSSL_METHOD* wolfTLSv1_method(void)
  11171. {
  11172. return wolfTLSv1_method_ex(NULL);
  11173. }
  11174. WOLFSSL_METHOD* wolfTLSv1_method_ex(void* heap)
  11175. {
  11176. WOLFSSL_METHOD* m;
  11177. WOLFSSL_ENTER("TLSv1_method");
  11178. #ifndef NO_WOLFSSL_CLIENT
  11179. m = wolfTLSv1_client_method_ex(heap);
  11180. #else
  11181. m = wolfTLSv1_server_method_ex(heap);
  11182. #endif
  11183. if (m != NULL) {
  11184. m->side = WOLFSSL_NEITHER_END;
  11185. }
  11186. return m;
  11187. }
  11188. #endif /* WOLFSSL_ALLOW_TLSV10 */
  11189. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  11190. *
  11191. * Returns a pointer to a WOLFSSL_METHOD struct
  11192. */
  11193. WOLFSSL_METHOD* wolfTLSv1_1_method(void)
  11194. {
  11195. return wolfTLSv1_1_method_ex(NULL);
  11196. }
  11197. WOLFSSL_METHOD* wolfTLSv1_1_method_ex(void* heap)
  11198. {
  11199. WOLFSSL_METHOD* m;
  11200. WOLFSSL_ENTER("TLSv1_1_method");
  11201. #ifndef NO_WOLFSSL_CLIENT
  11202. m = wolfTLSv1_1_client_method_ex(heap);
  11203. #else
  11204. m = wolfTLSv1_1_server_method_ex(heap);
  11205. #endif
  11206. if (m != NULL) {
  11207. m->side = WOLFSSL_NEITHER_END;
  11208. }
  11209. return m;
  11210. }
  11211. #endif /* !NO_OLD_TLS */
  11212. #ifndef WOLFSSL_NO_TLS12
  11213. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  11214. *
  11215. * Returns a pointer to a WOLFSSL_METHOD struct
  11216. */
  11217. WOLFSSL_METHOD* wolfTLSv1_2_method(void)
  11218. {
  11219. return wolfTLSv1_2_method_ex(NULL);
  11220. }
  11221. WOLFSSL_METHOD* wolfTLSv1_2_method_ex(void* heap)
  11222. {
  11223. WOLFSSL_METHOD* m;
  11224. WOLFSSL_ENTER("TLSv1_2_method");
  11225. #ifndef NO_WOLFSSL_CLIENT
  11226. m = wolfTLSv1_2_client_method_ex(heap);
  11227. #else
  11228. m = wolfTLSv1_2_server_method_ex(heap);
  11229. #endif
  11230. if (m != NULL) {
  11231. m->side = WOLFSSL_NEITHER_END;
  11232. }
  11233. return m;
  11234. }
  11235. #endif /* !WOLFSSL_NO_TLS12 */
  11236. #ifdef WOLFSSL_TLS13
  11237. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  11238. *
  11239. * Returns a pointer to a WOLFSSL_METHOD struct
  11240. */
  11241. WOLFSSL_METHOD* wolfTLSv1_3_method(void)
  11242. {
  11243. return wolfTLSv1_3_method_ex(NULL);
  11244. }
  11245. WOLFSSL_METHOD* wolfTLSv1_3_method_ex(void* heap)
  11246. {
  11247. WOLFSSL_METHOD* m;
  11248. WOLFSSL_ENTER("TLSv1_3_method");
  11249. #ifndef NO_WOLFSSL_CLIENT
  11250. m = wolfTLSv1_3_client_method_ex(heap);
  11251. #else
  11252. m = wolfTLSv1_3_server_method_ex(heap);
  11253. #endif
  11254. if (m != NULL) {
  11255. m->side = WOLFSSL_NEITHER_END;
  11256. }
  11257. return m;
  11258. }
  11259. #endif /* WOLFSSL_TLS13 */
  11260. #ifdef WOLFSSL_DTLS
  11261. WOLFSSL_METHOD* wolfDTLS_method(void)
  11262. {
  11263. return wolfDTLS_method_ex(NULL);
  11264. }
  11265. WOLFSSL_METHOD* wolfDTLS_method_ex(void* heap)
  11266. {
  11267. WOLFSSL_METHOD* m;
  11268. WOLFSSL_ENTER("DTLS_method_ex");
  11269. #ifndef NO_WOLFSSL_CLIENT
  11270. m = wolfDTLS_client_method_ex(heap);
  11271. #else
  11272. m = wolfDTLS_server_method_ex(heap);
  11273. #endif
  11274. if (m != NULL) {
  11275. m->side = WOLFSSL_NEITHER_END;
  11276. }
  11277. return m;
  11278. }
  11279. #ifndef NO_OLD_TLS
  11280. WOLFSSL_METHOD* wolfDTLSv1_method(void)
  11281. {
  11282. return wolfDTLSv1_method_ex(NULL);
  11283. }
  11284. WOLFSSL_METHOD* wolfDTLSv1_method_ex(void* heap)
  11285. {
  11286. WOLFSSL_METHOD* m;
  11287. WOLFSSL_ENTER("DTLSv1_method_ex");
  11288. #ifndef NO_WOLFSSL_CLIENT
  11289. m = wolfDTLSv1_client_method_ex(heap);
  11290. #else
  11291. m = wolfDTLSv1_server_method_ex(heap);
  11292. #endif
  11293. if (m != NULL) {
  11294. m->side = WOLFSSL_NEITHER_END;
  11295. }
  11296. return m;
  11297. }
  11298. #endif /* !NO_OLD_TLS */
  11299. #ifndef WOLFSSL_NO_TLS12
  11300. WOLFSSL_METHOD* wolfDTLSv1_2_method(void)
  11301. {
  11302. return wolfDTLSv1_2_method_ex(NULL);
  11303. }
  11304. WOLFSSL_METHOD* wolfDTLSv1_2_method_ex(void* heap)
  11305. {
  11306. WOLFSSL_METHOD* m;
  11307. WOLFSSL_ENTER("DTLSv1_2_method");
  11308. #ifndef NO_WOLFSSL_CLIENT
  11309. m = wolfDTLSv1_2_client_method_ex(heap);
  11310. #else
  11311. m = wolfDTLSv1_2_server_method_ex(heap);
  11312. #endif
  11313. if (m != NULL) {
  11314. m->side = WOLFSSL_NEITHER_END;
  11315. }
  11316. return m;
  11317. }
  11318. #endif /* !WOLFSSL_NO_TLS12 */
  11319. #endif /* WOLFSSL_DTLS */
  11320. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  11321. #ifndef NO_WOLFSSL_SERVER
  11322. WOLFSSL_METHOD* wolfTLS_server_method(void)
  11323. {
  11324. return wolfTLS_server_method_ex(NULL);
  11325. }
  11326. WOLFSSL_METHOD* wolfTLS_server_method_ex(void* heap)
  11327. {
  11328. WOLFSSL_METHOD* method =
  11329. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11330. heap, DYNAMIC_TYPE_METHOD);
  11331. (void)heap;
  11332. WOLFSSL_ENTER("TLS_server_method_ex");
  11333. if (method) {
  11334. #if defined(WOLFSSL_TLS13)
  11335. InitSSL_Method(method, MakeTLSv1_3());
  11336. #elif !defined(WOLFSSL_NO_TLS12)
  11337. InitSSL_Method(method, MakeTLSv1_2());
  11338. #elif !defined(NO_OLD_TLS)
  11339. InitSSL_Method(method, MakeTLSv1_1());
  11340. #elif defined(WOLFSSL_ALLOW_TLSV10)
  11341. InitSSL_Method(method, MakeTLSv1());
  11342. #else
  11343. #error No TLS version enabled!
  11344. #endif
  11345. method->downgrade = 1;
  11346. method->side = WOLFSSL_SERVER_END;
  11347. }
  11348. return method;
  11349. }
  11350. #ifndef NO_OLD_TLS
  11351. #ifdef WOLFSSL_ALLOW_TLSV10
  11352. WOLFSSL_METHOD* wolfTLSv1_server_method(void)
  11353. {
  11354. return wolfTLSv1_server_method_ex(NULL);
  11355. }
  11356. WOLFSSL_METHOD* wolfTLSv1_server_method_ex(void* heap)
  11357. {
  11358. WOLFSSL_METHOD* method =
  11359. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11360. heap, DYNAMIC_TYPE_METHOD);
  11361. (void)heap;
  11362. WOLFSSL_ENTER("TLSv1_server_method_ex");
  11363. if (method) {
  11364. InitSSL_Method(method, MakeTLSv1());
  11365. method->side = WOLFSSL_SERVER_END;
  11366. }
  11367. return method;
  11368. }
  11369. #endif /* WOLFSSL_ALLOW_TLSV10 */
  11370. WOLFSSL_METHOD* wolfTLSv1_1_server_method(void)
  11371. {
  11372. return wolfTLSv1_1_server_method_ex(NULL);
  11373. }
  11374. WOLFSSL_METHOD* wolfTLSv1_1_server_method_ex(void* heap)
  11375. {
  11376. WOLFSSL_METHOD* method =
  11377. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11378. heap, DYNAMIC_TYPE_METHOD);
  11379. (void)heap;
  11380. WOLFSSL_ENTER("TLSv1_1_server_method_ex");
  11381. if (method) {
  11382. InitSSL_Method(method, MakeTLSv1_1());
  11383. method->side = WOLFSSL_SERVER_END;
  11384. }
  11385. return method;
  11386. }
  11387. #endif /* !NO_OLD_TLS */
  11388. #ifndef WOLFSSL_NO_TLS12
  11389. WOLFSSL_ABI
  11390. WOLFSSL_METHOD* wolfTLSv1_2_server_method(void)
  11391. {
  11392. return wolfTLSv1_2_server_method_ex(NULL);
  11393. }
  11394. WOLFSSL_METHOD* wolfTLSv1_2_server_method_ex(void* heap)
  11395. {
  11396. WOLFSSL_METHOD* method =
  11397. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11398. heap, DYNAMIC_TYPE_METHOD);
  11399. (void)heap;
  11400. WOLFSSL_ENTER("TLSv1_2_server_method_ex");
  11401. if (method) {
  11402. InitSSL_Method(method, MakeTLSv1_2());
  11403. method->side = WOLFSSL_SERVER_END;
  11404. }
  11405. return method;
  11406. }
  11407. #endif /* !WOLFSSL_NO_TLS12 */
  11408. #ifdef WOLFSSL_TLS13
  11409. /* The TLS v1.3 server method data.
  11410. *
  11411. * returns the method data for a TLS v1.3 server.
  11412. */
  11413. WOLFSSL_ABI
  11414. WOLFSSL_METHOD* wolfTLSv1_3_server_method(void)
  11415. {
  11416. return wolfTLSv1_3_server_method_ex(NULL);
  11417. }
  11418. /* The TLS v1.3 server method data.
  11419. *
  11420. * heap The heap used for allocation.
  11421. * returns the method data for a TLS v1.3 server.
  11422. */
  11423. WOLFSSL_METHOD* wolfTLSv1_3_server_method_ex(void* heap)
  11424. {
  11425. WOLFSSL_METHOD* method =
  11426. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11427. heap, DYNAMIC_TYPE_METHOD);
  11428. (void)heap;
  11429. WOLFSSL_ENTER("TLSv1_3_server_method_ex");
  11430. if (method) {
  11431. InitSSL_Method(method, MakeTLSv1_3());
  11432. method->side = WOLFSSL_SERVER_END;
  11433. }
  11434. return method;
  11435. }
  11436. #endif /* WOLFSSL_TLS13 */
  11437. #ifdef WOLFSSL_DTLS
  11438. WOLFSSL_METHOD* wolfDTLS_server_method(void)
  11439. {
  11440. return wolfDTLS_server_method_ex(NULL);
  11441. }
  11442. WOLFSSL_METHOD* wolfDTLS_server_method_ex(void* heap)
  11443. {
  11444. WOLFSSL_METHOD* method =
  11445. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11446. heap, DYNAMIC_TYPE_METHOD);
  11447. (void)heap;
  11448. WOLFSSL_ENTER("DTLS_server_method_ex");
  11449. if (method) {
  11450. #if defined(WOLFSSL_DTLS13)
  11451. InitSSL_Method(method, MakeDTLSv1_3());
  11452. #elif !defined(WOLFSSL_NO_TLS12)
  11453. InitSSL_Method(method, MakeDTLSv1_2());
  11454. #elif !defined(NO_OLD_TLS)
  11455. InitSSL_Method(method, MakeDTLSv1());
  11456. #else
  11457. #error No DTLS version enabled!
  11458. #endif
  11459. method->downgrade = 1;
  11460. method->side = WOLFSSL_SERVER_END;
  11461. }
  11462. return method;
  11463. }
  11464. #ifndef NO_OLD_TLS
  11465. WOLFSSL_METHOD* wolfDTLSv1_server_method(void)
  11466. {
  11467. return wolfDTLSv1_server_method_ex(NULL);
  11468. }
  11469. WOLFSSL_METHOD* wolfDTLSv1_server_method_ex(void* heap)
  11470. {
  11471. WOLFSSL_METHOD* method =
  11472. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11473. heap, DYNAMIC_TYPE_METHOD);
  11474. (void)heap;
  11475. WOLFSSL_ENTER("DTLSv1_server_method_ex");
  11476. if (method) {
  11477. InitSSL_Method(method, MakeDTLSv1());
  11478. method->side = WOLFSSL_SERVER_END;
  11479. }
  11480. return method;
  11481. }
  11482. #endif /* !NO_OLD_TLS */
  11483. #ifndef WOLFSSL_NO_TLS12
  11484. WOLFSSL_METHOD* wolfDTLSv1_2_server_method(void)
  11485. {
  11486. return wolfDTLSv1_2_server_method_ex(NULL);
  11487. }
  11488. WOLFSSL_METHOD* wolfDTLSv1_2_server_method_ex(void* heap)
  11489. {
  11490. WOLFSSL_METHOD* method =
  11491. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11492. heap, DYNAMIC_TYPE_METHOD);
  11493. WOLFSSL_ENTER("DTLSv1_2_server_method_ex");
  11494. (void)heap;
  11495. if (method) {
  11496. InitSSL_Method(method, MakeDTLSv1_2());
  11497. method->side = WOLFSSL_SERVER_END;
  11498. }
  11499. (void)heap;
  11500. return method;
  11501. }
  11502. #endif /* !WOLFSSL_NO_TLS12 */
  11503. #endif /* WOLFSSL_DTLS */
  11504. #endif /* NO_WOLFSSL_SERVER */
  11505. #endif /* NO_TLS */
  11506. #endif /* WOLFCRYPT_ONLY */