tls.c 354 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807
  1. /* tls.c
  2. *
  3. * Copyright (C) 2006-2020 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/hmac.h>
  30. #ifdef NO_INLINE
  31. #include <wolfssl/wolfcrypt/misc.h>
  32. #else
  33. #define WOLFSSL_MISC_INCLUDED
  34. #include <wolfcrypt/src/misc.c>
  35. #endif
  36. #ifdef HAVE_CURVE25519
  37. #include <wolfssl/wolfcrypt/curve25519.h>
  38. #endif
  39. #ifdef HAVE_CURVE448
  40. #include <wolfssl/wolfcrypt/curve448.h>
  41. #endif
  42. #ifdef HAVE_NTRU
  43. #include "libntruencrypt/ntru_crypto.h"
  44. #include <wolfssl/wolfcrypt/random.h>
  45. #endif
  46. #ifdef HAVE_QSH
  47. static int TLSX_AddQSHKey(QSHKey** list, QSHKey* key);
  48. static byte* TLSX_QSHKeyFind_Pub(QSHKey* qsh, word16* pubLen, word16 name);
  49. #if defined(HAVE_NTRU)
  50. static int TLSX_CreateNtruKey(WOLFSSL* ssl, int type);
  51. #endif
  52. #endif /* HAVE_QSH */
  53. #if (!defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_TLS13) && \
  54. !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)) || \
  55. (defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES))
  56. static int TLSX_KeyShare_IsSupported(int namedGroup);
  57. #endif
  58. #if ((!defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_TLS13) && \
  59. !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)) || \
  60. (defined(WOLFSSL_TLS13) && !defined(HAVE_ECC) && !defined(HAVE_CURVE25519) \
  61. && !defined(HAVE_CURVE448) && defined(HAVE_SUPPORTED_CURVES)) || \
  62. ((defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  63. defined(HAVE_CURVE448)) && defined(HAVE_SUPPORTED_CURVES))) && \
  64. defined(HAVE_TLS_EXTENSIONS)
  65. static int TLSX_PopulateSupportedGroups(WOLFSSL* ssl, TLSX** extensions);
  66. #endif
  67. #ifndef NO_TLS
  68. /* Digest enable checks */
  69. #ifdef NO_OLD_TLS /* TLS 1.2 only */
  70. #if defined(NO_SHA256) && !defined(WOLFSSL_SHA384) && \
  71. !defined(WOLFSSL_SHA512)
  72. #error Must have SHA256, SHA384 or SHA512 enabled for TLS 1.2
  73. #endif
  74. #else /* TLS 1.1 or older */
  75. #if defined(NO_MD5) && defined(NO_SHA)
  76. #error Must have SHA1 and MD5 enabled for old TLS
  77. #endif
  78. #endif
  79. #ifdef WOLFSSL_TLS13
  80. #if !defined(NO_DH) && \
  81. !defined(HAVE_FFDHE_2048) && !defined(HAVE_FFDHE_3072) && \
  82. !defined(HAVE_FFDHE_4096) && !defined(HAVE_FFDHE_6144) && \
  83. !defined(HAVE_FFDHE_8192)
  84. #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
  85. #endif
  86. #if !defined(NO_RSA) && !defined(WC_RSA_PSS)
  87. #error The build option WC_RSA_PSS is required for TLS 1.3 with RSA
  88. #endif
  89. #ifndef HAVE_TLS_EXTENSIONS
  90. #ifndef _MSC_VER
  91. #error "The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3"
  92. #else
  93. #pragma message("Error: The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3")
  94. #endif
  95. #endif
  96. #endif
  97. /* Warn if secrets logging is enabled */
  98. #if defined(SHOW_SECRETS) || defined(WOLFSSL_SSLKEYLOGFILE)
  99. #ifndef _MSC_VER
  100. #warning The SHOW_SECRETS and WOLFSSL_SSLKEYLOGFILE options should only be used for debugging and never in a production environment
  101. #else
  102. #pragma message("Warning: The SHOW_SECRETS and WOLFSSL_SSLKEYLOGFILE options should only be used for debugging and never in a production environment")
  103. #endif
  104. #endif
  105. /* Optional Pre-Master-Secret logging for Wireshark */
  106. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_SSLKEYLOGFILE)
  107. #ifndef WOLFSSL_SSLKEYLOGFILE_OUTPUT
  108. #define WOLFSSL_SSLKEYLOGFILE_OUTPUT "sslkeylog.log"
  109. #endif
  110. #endif
  111. #ifndef WOLFSSL_NO_TLS12
  112. #ifdef WOLFSSL_SHA384
  113. #define HSHASH_SZ WC_SHA384_DIGEST_SIZE
  114. #else
  115. #define HSHASH_SZ FINISHED_SZ
  116. #endif
  117. #ifdef WOLFSSL_RENESAS_TSIP_TLS
  118. #if (WOLFSSL_RENESAS_TSIP_VER >=109)
  119. int tsip_generateMasterSecretEx(
  120. byte cipherSuiteFirst,
  121. byte cipherSuite,
  122. const byte* pr, /* pre-master */
  123. const byte* cr, /* client random */
  124. const byte* sr, /* server random */
  125. byte* ms);
  126. #elif (WOLFSSL_RENESAS_TSIP_VER >=106)
  127. int tsip_generateMasterSecret(
  128. const byte* pre,
  129. const byte* cr,
  130. const byte* sr,
  131. byte* ms);
  132. #endif
  133. int tsip_useable(const WOLFSSL *ssl);
  134. int tsip_generateSeesionKey(WOLFSSL *ssl);
  135. int tsip_generateVerifyData(
  136. const byte* ms,
  137. const byte* side,
  138. const byte* handshake_hash,
  139. byte* hashes);
  140. #endif /*WOLFSSL_RENESAS_TSIP_TLS*/
  141. int BuildTlsHandshakeHash(WOLFSSL* ssl, byte* hash, word32* hashLen)
  142. {
  143. int ret = 0;
  144. word32 hashSz = FINISHED_SZ;
  145. if (ssl == NULL || hash == NULL || hashLen == NULL || *hashLen < HSHASH_SZ)
  146. return BAD_FUNC_ARG;
  147. /* for constant timing perform these even if error */
  148. #ifndef NO_OLD_TLS
  149. ret |= wc_Md5GetHash(&ssl->hsHashes->hashMd5, hash);
  150. ret |= wc_ShaGetHash(&ssl->hsHashes->hashSha, &hash[WC_MD5_DIGEST_SIZE]);
  151. #endif
  152. if (IsAtLeastTLSv1_2(ssl)) {
  153. #ifndef NO_SHA256
  154. if (ssl->specs.mac_algorithm <= sha256_mac ||
  155. ssl->specs.mac_algorithm == blake2b_mac) {
  156. ret |= wc_Sha256GetHash(&ssl->hsHashes->hashSha256, hash);
  157. hashSz = WC_SHA256_DIGEST_SIZE;
  158. }
  159. #endif
  160. #ifdef WOLFSSL_SHA384
  161. if (ssl->specs.mac_algorithm == sha384_mac) {
  162. ret |= wc_Sha384GetHash(&ssl->hsHashes->hashSha384, hash);
  163. hashSz = WC_SHA384_DIGEST_SIZE;
  164. }
  165. #endif
  166. }
  167. *hashLen = hashSz;
  168. if (ret != 0)
  169. ret = BUILD_MSG_ERROR;
  170. return ret;
  171. }
  172. int BuildTlsFinished(WOLFSSL* ssl, Hashes* hashes, const byte* sender)
  173. {
  174. int ret;
  175. const byte* side;
  176. word32 hashSz = HSHASH_SZ;
  177. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  178. DECLARE_VAR(handshake_hash, byte, HSHASH_SZ, ssl->heap);
  179. if (handshake_hash == NULL)
  180. return MEMORY_E;
  181. #else
  182. byte handshake_hash[HSHASH_SZ];
  183. #endif
  184. ret = BuildTlsHandshakeHash(ssl, handshake_hash, &hashSz);
  185. if (ret == 0) {
  186. if (XSTRNCMP((const char*)sender, (const char*)client, SIZEOF_SENDER) == 0)
  187. side = tls_client;
  188. else
  189. side = tls_server;
  190. #ifdef WOLFSSL_HAVE_PRF
  191. #if defined(WOLFSSL_RENESAS_TSIP_TLS) && \
  192. !defined(NO_WOLFSSL_RENESAS_TSIP_TLS_SESSION)
  193. if (tsip_useable(ssl)) {
  194. ret = tsip_generateVerifyData(ssl->arrays->tsip_masterSecret,
  195. side, handshake_hash, (byte*)hashes /* out */);
  196. } else
  197. #endif
  198. ret = wc_PRF_TLS((byte*)hashes, TLS_FINISHED_SZ, ssl->arrays->masterSecret,
  199. SECRET_LEN, side, FINISHED_LABEL_SZ, handshake_hash, hashSz,
  200. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  201. ssl->heap, ssl->devId);
  202. #else
  203. /* Pseudo random function must be enabled in the configuration. */
  204. ret = PRF_MISSING;
  205. WOLFSSL_MSG("Pseudo-random function is not enabled");
  206. (void)side;
  207. (void)hashes;
  208. #endif
  209. }
  210. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  211. FREE_VAR(handshake_hash, ssl->heap);
  212. #endif
  213. return ret;
  214. }
  215. #endif /* !WOLFSSL_NO_TLS12 */
  216. #ifndef NO_OLD_TLS
  217. #ifdef WOLFSSL_ALLOW_TLSV10
  218. ProtocolVersion MakeTLSv1(void)
  219. {
  220. ProtocolVersion pv;
  221. pv.major = SSLv3_MAJOR;
  222. pv.minor = TLSv1_MINOR;
  223. return pv;
  224. }
  225. #endif /* WOLFSSL_ALLOW_TLSV10 */
  226. ProtocolVersion MakeTLSv1_1(void)
  227. {
  228. ProtocolVersion pv;
  229. pv.major = SSLv3_MAJOR;
  230. pv.minor = TLSv1_1_MINOR;
  231. return pv;
  232. }
  233. #endif /* !NO_OLD_TLS */
  234. #ifndef WOLFSSL_NO_TLS12
  235. ProtocolVersion MakeTLSv1_2(void)
  236. {
  237. ProtocolVersion pv;
  238. pv.major = SSLv3_MAJOR;
  239. pv.minor = TLSv1_2_MINOR;
  240. return pv;
  241. }
  242. #endif /* !WOLFSSL_NO_TLS12 */
  243. #ifdef WOLFSSL_TLS13
  244. /* The TLS v1.3 protocol version.
  245. *
  246. * returns the protocol version data for TLS v1.3.
  247. */
  248. ProtocolVersion MakeTLSv1_3(void)
  249. {
  250. ProtocolVersion pv;
  251. pv.major = SSLv3_MAJOR;
  252. pv.minor = TLSv1_3_MINOR;
  253. return pv;
  254. }
  255. #endif
  256. #ifndef WOLFSSL_NO_TLS12
  257. #ifdef HAVE_EXTENDED_MASTER
  258. static const byte ext_master_label[EXT_MASTER_LABEL_SZ + 1] =
  259. "extended master secret";
  260. #endif
  261. static const byte master_label[MASTER_LABEL_SZ + 1] = "master secret";
  262. static const byte key_label [KEY_LABEL_SZ + 1] = "key expansion";
  263. static int _DeriveTlsKeys(byte* key_dig, word32 key_dig_len,
  264. const byte* ms, word32 msLen,
  265. const byte* sr, const byte* cr,
  266. int tls1_2, int hash_type,
  267. void* heap, int devId)
  268. {
  269. int ret;
  270. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  271. DECLARE_VAR(seed, byte, SEED_LEN, heap);
  272. if (seed == NULL)
  273. return MEMORY_E;
  274. #else
  275. byte seed[SEED_LEN];
  276. #endif
  277. XMEMCPY(seed, sr, RAN_LEN);
  278. XMEMCPY(seed + RAN_LEN, cr, RAN_LEN);
  279. #ifdef WOLFSSL_HAVE_PRF
  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. #else
  283. /* Pseudo random function must be enabled in the configuration. */
  284. ret = PRF_MISSING;
  285. WOLFSSL_MSG("Pseudo-random function is not enabled");
  286. (void)key_dig;
  287. (void)key_dig_len;
  288. (void)ms;
  289. (void)msLen;
  290. (void)tls1_2;
  291. (void)hash_type;
  292. (void)heap;
  293. (void)devId;
  294. (void)key_label;
  295. (void)master_label;
  296. #ifdef HAVE_EXTENDED_MASTER
  297. (void)ext_master_label;
  298. #endif
  299. #endif
  300. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  301. FREE_VAR(seed, heap);
  302. #endif
  303. return ret;
  304. }
  305. /* External facing wrapper so user can call as well, 0 on success */
  306. int wolfSSL_DeriveTlsKeys(byte* key_dig, word32 key_dig_len,
  307. const byte* ms, word32 msLen,
  308. const byte* sr, const byte* cr,
  309. int tls1_2, int hash_type)
  310. {
  311. return _DeriveTlsKeys(key_dig, key_dig_len, ms, msLen, sr, cr, tls1_2,
  312. hash_type, NULL, INVALID_DEVID);
  313. }
  314. int DeriveTlsKeys(WOLFSSL* ssl)
  315. {
  316. int ret;
  317. int key_dig_len = 2 * ssl->specs.hash_size +
  318. 2 * ssl->specs.key_size +
  319. 2 * ssl->specs.iv_size;
  320. #ifdef WOLFSSL_SMALL_STACK
  321. byte* key_dig;
  322. #else
  323. byte key_dig[MAX_PRF_DIG];
  324. #endif
  325. #ifdef WOLFSSL_SMALL_STACK
  326. key_dig = (byte*)XMALLOC(MAX_PRF_DIG, ssl->heap, DYNAMIC_TYPE_DIGEST);
  327. if (key_dig == NULL) {
  328. return MEMORY_E;
  329. }
  330. #endif
  331. #if defined(WOLFSSL_RENESAS_TSIP_TLS) && \
  332. !defined(NO_WOLFSSL_RENESAS_TSIP_TLS_SESSION)
  333. if (tsip_useable(ssl))
  334. ret = tsip_generateSeesionKey(ssl);
  335. else {
  336. #endif
  337. ret = _DeriveTlsKeys(key_dig, key_dig_len,
  338. ssl->arrays->masterSecret, SECRET_LEN,
  339. ssl->arrays->serverRandom, ssl->arrays->clientRandom,
  340. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  341. ssl->heap, ssl->devId);
  342. if (ret == 0)
  343. ret = StoreKeys(ssl, key_dig, PROVISION_CLIENT_SERVER);
  344. #if defined(WOLFSSL_RENESAS_TSIP_TLS) && \
  345. !defined(NO_WOLFSSL_RENESAS_TSIP_TLS_SESSION)
  346. }
  347. #endif
  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. DECLARE_VAR(seed, byte, SEED_LEN, heap);
  362. if (seed == NULL)
  363. return MEMORY_E;
  364. #else
  365. byte seed[SEED_LEN];
  366. #endif
  367. XMEMCPY(seed, cr, RAN_LEN);
  368. XMEMCPY(seed + RAN_LEN, sr, RAN_LEN);
  369. #ifdef WOLFSSL_HAVE_PRF
  370. ret = wc_PRF_TLS(ms, msLen, pms, pmsLen, master_label, MASTER_LABEL_SZ,
  371. seed, SEED_LEN, tls1_2, hash_type, heap, devId);
  372. #else
  373. /* Pseudo random function must be enabled in the configuration. */
  374. ret = PRF_MISSING;
  375. WOLFSSL_MSG("Pseudo-random function is not enabled");
  376. (void)ms;
  377. (void)msLen;
  378. (void)pms;
  379. (void)pmsLen;
  380. (void)tls1_2;
  381. (void)hash_type;
  382. (void)heap;
  383. (void)devId;
  384. #endif
  385. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  386. FREE_VAR(seed, heap);
  387. #endif
  388. return ret;
  389. }
  390. /* External facing wrapper so user can call as well, 0 on success */
  391. int wolfSSL_MakeTlsMasterSecret(byte* ms, word32 msLen,
  392. const byte* pms, word32 pmsLen,
  393. const byte* cr, const byte* sr,
  394. int tls1_2, int hash_type)
  395. {
  396. return _MakeTlsMasterSecret(ms, msLen, pms, pmsLen, cr, sr, tls1_2,
  397. hash_type, NULL, INVALID_DEVID);
  398. }
  399. #ifdef HAVE_EXTENDED_MASTER
  400. static int _MakeTlsExtendedMasterSecret(byte* ms, word32 msLen,
  401. const byte* pms, word32 pmsLen,
  402. const byte* sHash, word32 sHashLen,
  403. int tls1_2, int hash_type,
  404. void* heap, int devId)
  405. {
  406. int ret;
  407. #ifdef WOLFSSL_HAVE_PRF
  408. ret = wc_PRF_TLS(ms, msLen, pms, pmsLen, ext_master_label, EXT_MASTER_LABEL_SZ,
  409. sHash, sHashLen, tls1_2, hash_type, heap, devId);
  410. #else
  411. /* Pseudo random function must be enabled in the configuration. */
  412. ret = PRF_MISSING;
  413. WOLFSSL_MSG("Pseudo-random function is not enabled");
  414. (void)ms;
  415. (void)msLen;
  416. (void)pms;
  417. (void)pmsLen;
  418. (void)sHash;
  419. (void)sHashLen;
  420. (void)tls1_2;
  421. (void)hash_type;
  422. (void)heap;
  423. (void)devId;
  424. #endif
  425. return ret;
  426. }
  427. /* External facing wrapper so user can call as well, 0 on success */
  428. int wolfSSL_MakeTlsExtendedMasterSecret(byte* ms, word32 msLen,
  429. const byte* pms, word32 pmsLen,
  430. const byte* sHash, word32 sHashLen,
  431. int tls1_2, int hash_type)
  432. {
  433. return _MakeTlsExtendedMasterSecret(ms, msLen, pms, pmsLen, sHash, sHashLen,
  434. tls1_2, hash_type, NULL, INVALID_DEVID);
  435. }
  436. #endif /* HAVE_EXTENDED_MASTER */
  437. int MakeTlsMasterSecret(WOLFSSL* ssl)
  438. {
  439. int ret;
  440. #ifdef HAVE_EXTENDED_MASTER
  441. if (ssl->options.haveEMS) {
  442. word32 hashSz = HSHASH_SZ;
  443. #ifdef WOLFSSL_SMALL_STACK
  444. byte* handshake_hash = (byte*)XMALLOC(HSHASH_SZ, ssl->heap,
  445. DYNAMIC_TYPE_DIGEST);
  446. if (handshake_hash == NULL)
  447. return MEMORY_E;
  448. #else
  449. byte handshake_hash[HSHASH_SZ];
  450. #endif
  451. ret = BuildTlsHandshakeHash(ssl, handshake_hash, &hashSz);
  452. if (ret == 0) {
  453. ret = _MakeTlsExtendedMasterSecret(
  454. ssl->arrays->masterSecret, SECRET_LEN,
  455. ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz,
  456. handshake_hash, hashSz,
  457. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  458. ssl->heap, ssl->devId);
  459. }
  460. #ifdef WOLFSSL_SMALL_STACK
  461. XFREE(handshake_hash, ssl->heap, DYNAMIC_TYPE_DIGEST);
  462. #endif
  463. }
  464. else
  465. #endif /* HAVE_EXTENDED_MASTER */
  466. {
  467. #if defined(WOLFSSL_RENESAS_TSIP_TLS) && \
  468. !defined(NO_WOLFSSL_RENESAS_TSIP_TLS_SESSION)
  469. if (tsip_useable(ssl)) {
  470. #if (WOLFSSL_RENESAS_TSIP_VER>=109)
  471. ret = tsip_generateMasterSecretEx(
  472. ssl->options.cipherSuite0,
  473. ssl->options.cipherSuite,
  474. &ssl->arrays->preMasterSecret[VERSION_SZ],
  475. ssl->arrays->clientRandom,
  476. ssl->arrays->serverRandom,
  477. ssl->arrays->tsip_masterSecret);
  478. #elif (WOLFSSL_RENESAS_TSIP_VER>=106)
  479. ret = tsip_generateMasterSecret(
  480. &ssl->arrays->preMasterSecret[VERSION_SZ],
  481. ssl->arrays->clientRandom,
  482. ssl->arrays->serverRandom,
  483. ssl->arrays->tsip_masterSecret);
  484. #endif
  485. } else
  486. #endif
  487. ret = _MakeTlsMasterSecret(ssl->arrays->masterSecret, SECRET_LEN,
  488. ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz,
  489. ssl->arrays->clientRandom, ssl->arrays->serverRandom,
  490. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  491. ssl->heap, ssl->devId);
  492. }
  493. if (ret == 0) {
  494. #ifdef SHOW_SECRETS
  495. /* Wireshark Pre-Master-Secret Format:
  496. * CLIENT_RANDOM <clientrandom> <mastersecret>
  497. */
  498. const char* CLIENT_RANDOM_LABEL = "CLIENT_RANDOM";
  499. int i, pmsPos = 0;
  500. char pmsBuf[13 + 1 + 64 + 1 + 96 + 1 + 1];
  501. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%s ",
  502. CLIENT_RANDOM_LABEL);
  503. pmsPos += XSTRLEN(CLIENT_RANDOM_LABEL) + 1;
  504. for (i = 0; i < RAN_LEN; i++) {
  505. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%02x",
  506. ssl->arrays->clientRandom[i]);
  507. pmsPos += 2;
  508. }
  509. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, " ");
  510. pmsPos += 1;
  511. for (i = 0; i < SECRET_LEN; i++) {
  512. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%02x",
  513. ssl->arrays->masterSecret[i]);
  514. pmsPos += 2;
  515. }
  516. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "\n");
  517. pmsPos += 1;
  518. /* print master secret */
  519. puts(pmsBuf);
  520. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_SSLKEYLOGFILE)
  521. {
  522. FILE* f = XFOPEN(WOLFSSL_SSLKEYLOGFILE_OUTPUT, "a");
  523. if (f != XBADFILE) {
  524. XFWRITE(pmsBuf, 1, pmsPos, f);
  525. XFCLOSE(f);
  526. }
  527. }
  528. #endif
  529. #endif /* SHOW_SECRETS */
  530. ret = DeriveTlsKeys(ssl);
  531. }
  532. return ret;
  533. }
  534. /* Used by EAP-TLS and EAP-TTLS to derive keying material from
  535. * the master_secret. */
  536. int wolfSSL_make_eap_keys(WOLFSSL* ssl, void* msk, unsigned int len,
  537. const char* label)
  538. {
  539. int ret;
  540. #ifdef WOLFSSL_SMALL_STACK
  541. byte* seed;
  542. #else
  543. byte seed[SEED_LEN];
  544. #endif
  545. #ifdef WOLFSSL_SMALL_STACK
  546. seed = (byte*)XMALLOC(SEED_LEN, ssl->heap, DYNAMIC_TYPE_SEED);
  547. if (seed == NULL)
  548. return MEMORY_E;
  549. #endif
  550. /*
  551. * As per RFC-5281, the order of the client and server randoms is reversed
  552. * from that used by the TLS protocol to derive keys.
  553. */
  554. XMEMCPY(seed, ssl->arrays->clientRandom, RAN_LEN);
  555. XMEMCPY(seed + RAN_LEN, ssl->arrays->serverRandom, RAN_LEN);
  556. #ifdef WOLFSSL_HAVE_PRF
  557. ret = wc_PRF_TLS((byte*)msk, len, ssl->arrays->masterSecret, SECRET_LEN,
  558. (const byte *)label, (word32)XSTRLEN(label), seed, SEED_LEN,
  559. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  560. ssl->heap, ssl->devId);
  561. #else
  562. /* Pseudo random function must be enabled in the configuration. */
  563. ret = PRF_MISSING;
  564. WOLFSSL_MSG("Pseudo-random function is not enabled");
  565. (void)msk;
  566. (void)len;
  567. (void)label;
  568. #endif
  569. #ifdef WOLFSSL_SMALL_STACK
  570. XFREE(seed, ssl->heap, DYNAMIC_TYPE_SEED);
  571. #endif
  572. return ret;
  573. }
  574. /* return HMAC digest type in wolfSSL format */
  575. int wolfSSL_GetHmacType(WOLFSSL* ssl)
  576. {
  577. if (ssl == NULL)
  578. return BAD_FUNC_ARG;
  579. switch (ssl->specs.mac_algorithm) {
  580. #ifndef NO_MD5
  581. case md5_mac:
  582. {
  583. return WC_MD5;
  584. }
  585. #endif
  586. #ifndef NO_SHA256
  587. case sha256_mac:
  588. {
  589. return WC_SHA256;
  590. }
  591. #endif
  592. #ifdef WOLFSSL_SHA384
  593. case sha384_mac:
  594. {
  595. return WC_SHA384;
  596. }
  597. #endif
  598. #ifndef NO_SHA
  599. case sha_mac:
  600. {
  601. return WC_SHA;
  602. }
  603. #endif
  604. #ifdef HAVE_BLAKE2
  605. case blake2b_mac:
  606. {
  607. return BLAKE2B_ID;
  608. }
  609. #endif
  610. default:
  611. {
  612. return WOLFSSL_FATAL_ERROR;
  613. }
  614. }
  615. }
  616. int wolfSSL_SetTlsHmacInner(WOLFSSL* ssl, byte* inner, word32 sz, int content,
  617. int verify)
  618. {
  619. if (ssl == NULL || inner == NULL)
  620. return BAD_FUNC_ARG;
  621. XMEMSET(inner, 0, WOLFSSL_TLS_HMAC_INNER_SZ);
  622. WriteSEQ(ssl, verify, inner);
  623. inner[SEQ_SZ] = (byte)content;
  624. inner[SEQ_SZ + ENUM_LEN] = ssl->version.major;
  625. inner[SEQ_SZ + ENUM_LEN + ENUM_LEN] = ssl->version.minor;
  626. c16toa((word16)sz, inner + SEQ_SZ + ENUM_LEN + VERSION_SZ);
  627. return 0;
  628. }
  629. #ifndef WOLFSSL_AEAD_ONLY
  630. #if !defined(WOLFSSL_NO_HASH_RAW) && !defined(HAVE_FIPS) && \
  631. !defined(HAVE_SELFTEST)
  632. /* Update the hash in the HMAC.
  633. *
  634. * hmac HMAC object.
  635. * data Data to be hashed.
  636. * sz Size of data to hash.
  637. * returns 0 on success, otherwise failure.
  638. */
  639. static int Hmac_HashUpdate(Hmac* hmac, const byte* data, word32 sz)
  640. {
  641. int ret = BAD_FUNC_ARG;
  642. switch (hmac->macType) {
  643. #ifndef NO_SHA
  644. case WC_SHA:
  645. ret = wc_ShaUpdate(&hmac->hash.sha, data, sz);
  646. break;
  647. #endif /* !NO_SHA */
  648. #ifndef NO_SHA256
  649. case WC_SHA256:
  650. ret = wc_Sha256Update(&hmac->hash.sha256, data, sz);
  651. break;
  652. #endif /* !NO_SHA256 */
  653. #ifdef WOLFSSL_SHA384
  654. case WC_SHA384:
  655. ret = wc_Sha384Update(&hmac->hash.sha384, data, sz);
  656. break;
  657. #endif /* WOLFSSL_SHA384 */
  658. #ifdef WOLFSSL_SHA512
  659. case WC_SHA512:
  660. ret = wc_Sha512Update(&hmac->hash.sha512, data, sz);
  661. break;
  662. #endif /* WOLFSSL_SHA512 */
  663. }
  664. return ret;
  665. }
  666. /* Finalize the hash but don't put the EOC, padding or length in.
  667. *
  668. * hmac HMAC object.
  669. * hash Hash result.
  670. * returns 0 on success, otherwise failure.
  671. */
  672. static int Hmac_HashFinalRaw(Hmac* hmac, unsigned char* hash)
  673. {
  674. int ret = BAD_FUNC_ARG;
  675. switch (hmac->macType) {
  676. #ifndef NO_SHA
  677. case WC_SHA:
  678. ret = wc_ShaFinalRaw(&hmac->hash.sha, hash);
  679. break;
  680. #endif /* !NO_SHA */
  681. #ifndef NO_SHA256
  682. case WC_SHA256:
  683. ret = wc_Sha256FinalRaw(&hmac->hash.sha256, hash);
  684. break;
  685. #endif /* !NO_SHA256 */
  686. #ifdef WOLFSSL_SHA384
  687. case WC_SHA384:
  688. ret = wc_Sha384FinalRaw(&hmac->hash.sha384, hash);
  689. break;
  690. #endif /* WOLFSSL_SHA384 */
  691. #ifdef WOLFSSL_SHA512
  692. case WC_SHA512:
  693. ret = wc_Sha512FinalRaw(&hmac->hash.sha512, hash);
  694. break;
  695. #endif /* WOLFSSL_SHA512 */
  696. }
  697. return ret;
  698. }
  699. /* Finalize the HMAC by performing outer hash.
  700. *
  701. * hmac HMAC object.
  702. * mac MAC result.
  703. * returns 0 on success, otherwise failure.
  704. */
  705. static int Hmac_OuterHash(Hmac* hmac, unsigned char* mac)
  706. {
  707. int ret = BAD_FUNC_ARG;
  708. wc_HashAlg hash;
  709. enum wc_HashType hashType = (enum wc_HashType)hmac->macType;
  710. int digestSz = wc_HashGetDigestSize(hashType);
  711. int blockSz = wc_HashGetBlockSize(hashType);
  712. if ((digestSz >= 0) && (blockSz >= 0)) {
  713. ret = wc_HashInit(&hash, hashType);
  714. }
  715. if (ret == 0) {
  716. ret = wc_HashUpdate(&hash, hashType, (byte*)hmac->opad,
  717. blockSz);
  718. if (ret == 0)
  719. ret = wc_HashUpdate(&hash, hashType, (byte*)hmac->innerHash,
  720. digestSz);
  721. if (ret == 0)
  722. ret = wc_HashFinal(&hash, hashType, mac);
  723. wc_HashFree(&hash, hashType);
  724. }
  725. return ret;
  726. }
  727. /* Calculate the HMAC of the header + message data.
  728. * Constant time implementation using wc_Sha*FinalRaw().
  729. *
  730. * hmac HMAC object.
  731. * digest MAC result.
  732. * in Message data.
  733. * sz Size of the message data.
  734. * header Constructed record header with length of handshake data.
  735. * returns 0 on success, otherwise failure.
  736. */
  737. static int Hmac_UpdateFinal_CT(Hmac* hmac, byte* digest, const byte* in,
  738. word32 sz, byte* header)
  739. {
  740. byte lenBytes[8];
  741. int i, j, k;
  742. int blockBits, blockMask;
  743. int lastBlockLen, macLen, extraLen, eocIndex;
  744. int blocks, safeBlocks, lenBlock, eocBlock;
  745. int maxLen;
  746. int blockSz, padSz;
  747. int ret;
  748. word32 realLen;
  749. byte extraBlock;
  750. switch (hmac->macType) {
  751. #ifndef NO_SHA
  752. case WC_SHA:
  753. blockSz = WC_SHA_BLOCK_SIZE;
  754. blockBits = 6;
  755. macLen = WC_SHA_DIGEST_SIZE;
  756. padSz = WC_SHA_BLOCK_SIZE - WC_SHA_PAD_SIZE + 1;
  757. break;
  758. #endif /* !NO_SHA */
  759. #ifndef NO_SHA256
  760. case WC_SHA256:
  761. blockSz = WC_SHA256_BLOCK_SIZE;
  762. blockBits = 6;
  763. macLen = WC_SHA256_DIGEST_SIZE;
  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. macLen = WC_SHA384_DIGEST_SIZE;
  772. padSz = WC_SHA384_BLOCK_SIZE - WC_SHA384_PAD_SIZE + 1;
  773. break;
  774. #endif /* WOLFSSL_SHA384 */
  775. #ifdef WOLFSSL_SHA512
  776. case WC_SHA512:
  777. blockSz = WC_SHA512_BLOCK_SIZE;
  778. blockBits = 7;
  779. macLen = WC_SHA512_DIGEST_SIZE;
  780. padSz = WC_SHA512_BLOCK_SIZE - WC_SHA512_PAD_SIZE + 1;
  781. break;
  782. #endif /* WOLFSSL_SHA512 */
  783. default:
  784. return BAD_FUNC_ARG;
  785. }
  786. blockMask = blockSz - 1;
  787. /* Size of data to HMAC if padding length byte is zero. */
  788. maxLen = WOLFSSL_TLS_HMAC_INNER_SZ + sz - 1 - macLen;
  789. /* Complete data (including padding) has block for EOC and/or length. */
  790. extraBlock = ctSetLTE((maxLen + padSz) & blockMask, padSz);
  791. /* Total number of blocks for data including padding. */
  792. blocks = ((maxLen + blockSz - 1) >> blockBits) + extraBlock;
  793. /* Up to last 6 blocks can be hashed safely. */
  794. safeBlocks = blocks - 6;
  795. /* Length of message data. */
  796. realLen = maxLen - in[sz - 1];
  797. /* Number of message bytes in last block. */
  798. lastBlockLen = realLen & blockMask;
  799. /* Number of padding bytes in last block. */
  800. extraLen = ((blockSz * 2 - padSz - lastBlockLen) & blockMask) + 1;
  801. /* Number of blocks to create for hash. */
  802. lenBlock = (realLen + extraLen) >> blockBits;
  803. /* Block containing EOC byte. */
  804. eocBlock = realLen >> blockBits;
  805. /* Index of EOC byte in block. */
  806. eocIndex = realLen & blockMask;
  807. /* Add length of hmac's ipad to total length. */
  808. realLen += blockSz;
  809. /* Length as bits - 8 bytes bigendian. */
  810. c32toa(realLen >> ((sizeof(word32) * 8) - 3), lenBytes);
  811. c32toa(realLen << 3, lenBytes + sizeof(word32));
  812. ret = Hmac_HashUpdate(hmac, (unsigned char*)hmac->ipad, blockSz);
  813. if (ret != 0)
  814. return ret;
  815. XMEMSET(hmac->innerHash, 0, macLen);
  816. if (safeBlocks > 0) {
  817. ret = Hmac_HashUpdate(hmac, header, WOLFSSL_TLS_HMAC_INNER_SZ);
  818. if (ret != 0)
  819. return ret;
  820. ret = Hmac_HashUpdate(hmac, in, safeBlocks * blockSz -
  821. WOLFSSL_TLS_HMAC_INNER_SZ);
  822. if (ret != 0)
  823. return ret;
  824. }
  825. else
  826. safeBlocks = 0;
  827. XMEMSET(digest, 0, macLen);
  828. k = safeBlocks * blockSz;
  829. for (i = safeBlocks; i < blocks; i++) {
  830. unsigned char hashBlock[WC_MAX_BLOCK_SIZE];
  831. unsigned char isEocBlock = ctMaskEq(i, eocBlock);
  832. unsigned char isOutBlock = ctMaskEq(i, lenBlock);
  833. for (j = 0; j < blockSz; j++, k++) {
  834. unsigned char atEoc = ctMaskEq(j, eocIndex) & isEocBlock;
  835. unsigned char pastEoc = ctMaskGT(j, eocIndex) & isEocBlock;
  836. unsigned char b = 0;
  837. if (k < WOLFSSL_TLS_HMAC_INNER_SZ)
  838. b = header[k];
  839. else if (k < maxLen)
  840. b = in[k - WOLFSSL_TLS_HMAC_INNER_SZ];
  841. b = ctMaskSel(atEoc, 0x80, b);
  842. b &= (unsigned char)~(word32)pastEoc;
  843. b &= ((unsigned char)~(word32)isOutBlock) | isEocBlock;
  844. if (j >= blockSz - 8) {
  845. b = ctMaskSel(isOutBlock, lenBytes[j - (blockSz - 8)], b);
  846. }
  847. hashBlock[j] = b;
  848. }
  849. ret = Hmac_HashUpdate(hmac, hashBlock, blockSz);
  850. if (ret != 0)
  851. return ret;
  852. ret = Hmac_HashFinalRaw(hmac, hashBlock);
  853. if (ret != 0)
  854. return ret;
  855. for (j = 0; j < macLen; j++)
  856. ((unsigned char*)hmac->innerHash)[j] |= hashBlock[j] & isOutBlock;
  857. }
  858. ret = Hmac_OuterHash(hmac, digest);
  859. return ret;
  860. }
  861. #endif
  862. #if defined(WOLFSSL_NO_HASH_RAW) || defined(HAVE_FIPS) || \
  863. defined(HAVE_SELFTEST) || defined(HAVE_BLAKE2)
  864. /* Calculate the HMAC of the header + message data.
  865. * Constant time implementation using normal hashing operations.
  866. * Update-Final need to be constant time.
  867. *
  868. * hmac HMAC object.
  869. * digest MAC result.
  870. * in Message data.
  871. * sz Size of the message data.
  872. * header Constructed record header with length of handshake data.
  873. * returns 0 on success, otherwise failure.
  874. */
  875. static int Hmac_UpdateFinal(Hmac* hmac, byte* digest, const byte* in,
  876. word32 sz, byte* header)
  877. {
  878. byte dummy[WC_MAX_BLOCK_SIZE] = {0};
  879. int ret;
  880. word32 msgSz, blockSz, macSz, padSz, maxSz, realSz;
  881. word32 currSz, offset = 0;
  882. int msgBlocks, blocks, blockBits;
  883. int i;
  884. switch (hmac->macType) {
  885. #ifndef NO_SHA
  886. case WC_SHA:
  887. blockSz = WC_SHA_BLOCK_SIZE;
  888. blockBits = 6;
  889. macSz = WC_SHA_DIGEST_SIZE;
  890. padSz = WC_SHA_BLOCK_SIZE - WC_SHA_PAD_SIZE + 1;
  891. break;
  892. #endif /* !NO_SHA */
  893. #ifndef NO_SHA256
  894. case WC_SHA256:
  895. blockSz = WC_SHA256_BLOCK_SIZE;
  896. blockBits = 6;
  897. macSz = WC_SHA256_DIGEST_SIZE;
  898. padSz = WC_SHA256_BLOCK_SIZE - WC_SHA256_PAD_SIZE + 1;
  899. break;
  900. #endif /* !NO_SHA256 */
  901. #ifdef WOLFSSL_SHA384
  902. case WC_SHA384:
  903. blockSz = WC_SHA384_BLOCK_SIZE;
  904. blockBits = 7;
  905. macSz = WC_SHA384_DIGEST_SIZE;
  906. padSz = WC_SHA384_BLOCK_SIZE - WC_SHA384_PAD_SIZE + 1;
  907. break;
  908. #endif /* WOLFSSL_SHA384 */
  909. #ifdef WOLFSSL_SHA512
  910. case WC_SHA512:
  911. blockSz = WC_SHA512_BLOCK_SIZE;
  912. blockBits = 7;
  913. macSz = WC_SHA512_DIGEST_SIZE;
  914. padSz = WC_SHA512_BLOCK_SIZE - WC_SHA512_PAD_SIZE + 1;
  915. break;
  916. #endif /* WOLFSSL_SHA512 */
  917. #ifdef HAVE_BLAKE2
  918. case WC_HASH_TYPE_BLAKE2B:
  919. blockSz = BLAKE2B_BLOCKBYTES;
  920. blockBits = 7;
  921. macSz = BLAKE2B_256;
  922. padSz = 0;
  923. break;
  924. #endif /* HAVE_BLAK2 */
  925. default:
  926. return BAD_FUNC_ARG;
  927. }
  928. msgSz = sz - (1 + in[sz - 1] + macSz);
  929. /* Make negative result 0 */
  930. msgSz &= ~(0 - (msgSz >> 31));
  931. realSz = WOLFSSL_TLS_HMAC_INNER_SZ + msgSz;
  932. maxSz = WOLFSSL_TLS_HMAC_INNER_SZ + (sz - 1) - macSz;
  933. /* Calculate #blocks processed in HMAC for max and real data. */
  934. blocks = maxSz >> blockBits;
  935. blocks += ((maxSz + padSz) % blockSz) < padSz;
  936. msgBlocks = realSz >> blockBits;
  937. /* #Extra blocks to process. */
  938. blocks -= msgBlocks + (((realSz + padSz) % blockSz) < padSz);
  939. /* Calculate whole blocks. */
  940. msgBlocks--;
  941. ret = wc_HmacUpdate(hmac, header, WOLFSSL_TLS_HMAC_INNER_SZ);
  942. if (ret == 0) {
  943. /* Fill the rest of the block with any available data. */
  944. currSz = ctMaskLT(msgSz, blockSz) & msgSz;
  945. currSz |= ctMaskGTE(msgSz, blockSz) & blockSz;
  946. currSz -= WOLFSSL_TLS_HMAC_INNER_SZ;
  947. currSz &= ~(0 - (currSz >> 31));
  948. ret = wc_HmacUpdate(hmac, in, currSz);
  949. offset = currSz;
  950. }
  951. if (ret == 0) {
  952. /* Do the hash operations on a block basis. */
  953. for (i = 0; i < msgBlocks; i++, offset += blockSz) {
  954. ret = wc_HmacUpdate(hmac, in + offset, blockSz);
  955. if (ret != 0)
  956. break;
  957. }
  958. }
  959. if (ret == 0)
  960. ret = wc_HmacUpdate(hmac, in + offset, msgSz - offset);
  961. if (ret == 0)
  962. ret = wc_HmacFinal(hmac, digest);
  963. if (ret == 0) {
  964. /* Do the dummy hash operations. Do at least one. */
  965. for (i = 0; i < blocks + 1; i++) {
  966. ret = wc_HmacUpdate(hmac, dummy, blockSz);
  967. if (ret != 0)
  968. break;
  969. }
  970. }
  971. return ret;
  972. }
  973. #endif
  974. int TLS_hmac(WOLFSSL* ssl, byte* digest, const byte* in, word32 sz, int padSz,
  975. int content, int verify, int epochOrder)
  976. {
  977. Hmac hmac;
  978. byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
  979. int ret = 0;
  980. const byte* macSecret = NULL;
  981. word32 hashSz = 0;
  982. if (ssl == NULL)
  983. return BAD_FUNC_ARG;
  984. #ifdef HAVE_TRUNCATED_HMAC
  985. hashSz = ssl->truncated_hmac ? (byte)TRUNCATED_HMAC_SZ
  986. : ssl->specs.hash_size;
  987. #else
  988. hashSz = ssl->specs.hash_size;
  989. #endif
  990. #ifdef HAVE_FUZZER
  991. /* Fuzz "in" buffer with sz to be used in HMAC algorithm */
  992. if (ssl->fuzzerCb) {
  993. if (verify && padSz >= 0) {
  994. ssl->fuzzerCb(ssl, in, sz + hashSz + padSz + 1, FUZZ_HMAC,
  995. ssl->fuzzerCtx);
  996. }
  997. else {
  998. ssl->fuzzerCb(ssl, in, sz, FUZZ_HMAC, ssl->fuzzerCtx);
  999. }
  1000. }
  1001. #endif
  1002. if (!ssl->options.dtls)
  1003. wolfSSL_SetTlsHmacInner(ssl, myInner, sz, content, verify);
  1004. else
  1005. wolfSSL_SetTlsHmacInner(ssl, myInner, sz, content, epochOrder);
  1006. #if defined(WOLFSSL_RENESAS_TSIP_TLS) && \
  1007. !defined(NO_WOLFSSL_RENESAS_TSIP_TLS_SESSION)
  1008. if (tsip_useable(ssl)) {
  1009. if (ssl->specs.hash_size == WC_SHA_DIGEST_SIZE)
  1010. ret = tsip_Sha1Hmac(ssl, myInner, WOLFSSL_TLS_HMAC_INNER_SZ,
  1011. in, sz, digest, verify);
  1012. else if (ssl->specs.hash_size == WC_SHA256_DIGEST_SIZE)
  1013. ret = tsip_Sha256Hmac(ssl, myInner, WOLFSSL_TLS_HMAC_INNER_SZ,
  1014. in, sz, digest, verify);
  1015. else
  1016. ret = TSIP_MAC_DIGSZ_E;
  1017. return ret;
  1018. }
  1019. #endif
  1020. ret = wc_HmacInit(&hmac, ssl->heap, ssl->devId);
  1021. if (ret != 0)
  1022. return ret;
  1023. #ifdef WOLFSSL_DTLS
  1024. if (ssl->options.dtls)
  1025. macSecret = wolfSSL_GetDtlsMacSecret(ssl, verify, epochOrder);
  1026. else
  1027. macSecret = wolfSSL_GetMacSecret(ssl, verify);
  1028. #else
  1029. macSecret = wolfSSL_GetMacSecret(ssl, verify);
  1030. #endif
  1031. ret = wc_HmacSetKey(&hmac, wolfSSL_GetHmacType(ssl),
  1032. macSecret,
  1033. ssl->specs.hash_size);
  1034. if (ret == 0) {
  1035. /* Constant time verification required. */
  1036. if (verify && padSz >= 0) {
  1037. #if !defined(WOLFSSL_NO_HASH_RAW) && !defined(HAVE_FIPS) && \
  1038. !defined(HAVE_SELFTEST)
  1039. #ifdef HAVE_BLAKE2
  1040. if (wolfSSL_GetHmacType(ssl) == WC_HASH_TYPE_BLAKE2B) {
  1041. ret = Hmac_UpdateFinal(&hmac, digest, in,
  1042. sz + hashSz + padSz + 1, myInner);
  1043. }
  1044. else
  1045. #endif
  1046. {
  1047. ret = Hmac_UpdateFinal_CT(&hmac, digest, in,
  1048. sz + hashSz + padSz + 1, myInner);
  1049. }
  1050. #else
  1051. ret = Hmac_UpdateFinal(&hmac, digest, in, sz + hashSz + padSz + 1,
  1052. myInner);
  1053. #endif
  1054. }
  1055. else {
  1056. ret = wc_HmacUpdate(&hmac, myInner, sizeof(myInner));
  1057. if (ret == 0)
  1058. ret = wc_HmacUpdate(&hmac, in, sz); /* content */
  1059. if (ret == 0)
  1060. ret = wc_HmacFinal(&hmac, digest);
  1061. }
  1062. }
  1063. wc_HmacFree(&hmac);
  1064. return ret;
  1065. }
  1066. #endif /* WOLFSSL_AEAD_ONLY */
  1067. #endif /* !WOLFSSL_NO_TLS12 */
  1068. #ifdef HAVE_TLS_EXTENSIONS
  1069. /**
  1070. * The TLSX semaphore is used to calculate the size of the extensions to be sent
  1071. * from one peer to another.
  1072. */
  1073. /** Supports up to 64 flags. Increase as needed. */
  1074. #define SEMAPHORE_SIZE 8
  1075. /**
  1076. * Converts the extension type (id) to an index in the semaphore.
  1077. *
  1078. * Official reference for TLS extension types:
  1079. * http://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xml
  1080. *
  1081. * Motivation:
  1082. * Previously, we used the extension type itself as the index of that
  1083. * extension in the semaphore as the extension types were declared
  1084. * sequentially, but maintain a semaphore as big as the number of available
  1085. * extensions is no longer an option since the release of renegotiation_info.
  1086. *
  1087. * How to update:
  1088. * Assign extension types that extrapolate the number of available semaphores
  1089. * to the first available index going backwards in the semaphore array.
  1090. * When adding a new extension type that don't extrapolate the number of
  1091. * available semaphores, check for a possible collision with with a
  1092. * 'remapped' extension type.
  1093. */
  1094. static WC_INLINE word16 TLSX_ToSemaphore(word16 type)
  1095. {
  1096. switch (type) {
  1097. case TLSX_RENEGOTIATION_INFO: /* 0xFF01 */
  1098. return 63;
  1099. default:
  1100. if (type > 62) {
  1101. /* This message SHOULD only happens during the adding of
  1102. new TLS extensions in which its IANA number overflows
  1103. the current semaphore's range, or if its number already
  1104. is assigned to be used by another extension.
  1105. Use this check value for the new extension and decrement
  1106. the check value by one. */
  1107. WOLFSSL_MSG("### TLSX semaphore collision or overflow detected!");
  1108. }
  1109. }
  1110. return type;
  1111. }
  1112. /** Checks if a specific light (tls extension) is not set in the semaphore. */
  1113. #define IS_OFF(semaphore, light) \
  1114. (!(((semaphore)[(light) / 8] & (byte) (0x01 << ((light) % 8)))))
  1115. /** Turn on a specific light (tls extension) in the semaphore. */
  1116. /* the semaphore marks the extensions already written to the message */
  1117. #define TURN_ON(semaphore, light) \
  1118. ((semaphore)[(light) / 8] |= (byte) (0x01 << ((light) % 8)))
  1119. /** Turn off a specific light (tls extension) in the semaphore. */
  1120. #define TURN_OFF(semaphore, light) \
  1121. ((semaphore)[(light) / 8] &= (byte) ~(0x01 << ((light) % 8)))
  1122. /** Creates a new extension. */
  1123. static TLSX* TLSX_New(TLSX_Type type, void* data, void* heap)
  1124. {
  1125. TLSX* extension = (TLSX*)XMALLOC(sizeof(TLSX), heap, DYNAMIC_TYPE_TLSX);
  1126. (void)heap;
  1127. if (extension) {
  1128. extension->type = type;
  1129. extension->data = data;
  1130. extension->resp = 0;
  1131. extension->next = NULL;
  1132. }
  1133. return extension;
  1134. }
  1135. /**
  1136. * Creates a new extension and pushes it to the provided list.
  1137. * Checks for duplicate extensions, keeps the newest.
  1138. */
  1139. static int TLSX_Push(TLSX** list, TLSX_Type type, void* data, void* heap)
  1140. {
  1141. TLSX* extension = TLSX_New(type, data, heap);
  1142. if (extension == NULL)
  1143. return MEMORY_E;
  1144. /* pushes the new extension on the list. */
  1145. extension->next = *list;
  1146. *list = extension;
  1147. /* remove duplicate extensions, there should be only one of each type. */
  1148. do {
  1149. if (extension->next && extension->next->type == type) {
  1150. TLSX *next = extension->next;
  1151. extension->next = next->next;
  1152. next->next = NULL;
  1153. TLSX_FreeAll(next, heap);
  1154. /* there is no way to occur more than
  1155. * two extensions of the same type.
  1156. */
  1157. break;
  1158. }
  1159. } while ((extension = extension->next));
  1160. return 0;
  1161. }
  1162. #ifdef WOLFSSL_TLS13
  1163. /**
  1164. * Creates a new extension and prepend it to the provided list.
  1165. * Checks for duplicate extensions, keeps the newest.
  1166. */
  1167. static int TLSX_Prepend(TLSX** list, TLSX_Type type, void* data, void* heap)
  1168. {
  1169. TLSX* extension = TLSX_New(type, data, heap);
  1170. TLSX* curr = *list;
  1171. if (extension == NULL)
  1172. return MEMORY_E;
  1173. /* remove duplicate extensions, there should be only one of each type. */
  1174. while (curr && curr->next) {
  1175. if (curr->next->type == type) {
  1176. TLSX *next = curr->next;
  1177. curr->next = next->next;
  1178. next->next = NULL;
  1179. TLSX_FreeAll(next, heap);
  1180. }
  1181. curr = curr->next;
  1182. }
  1183. if (curr)
  1184. curr->next = extension;
  1185. else
  1186. *list = extension;
  1187. return 0;
  1188. }
  1189. #endif /* WOLFSSL_TLS13 */
  1190. #ifndef NO_WOLFSSL_CLIENT
  1191. int TLSX_CheckUnsupportedExtension(WOLFSSL* ssl, TLSX_Type type);
  1192. int TLSX_CheckUnsupportedExtension(WOLFSSL* ssl, TLSX_Type type)
  1193. {
  1194. TLSX *extension = TLSX_Find(ssl->extensions, type);
  1195. if (!extension)
  1196. extension = TLSX_Find(ssl->ctx->extensions, type);
  1197. return extension == NULL;
  1198. }
  1199. int TLSX_HandleUnsupportedExtension(WOLFSSL* ssl);
  1200. int TLSX_HandleUnsupportedExtension(WOLFSSL* ssl)
  1201. {
  1202. SendAlert(ssl, alert_fatal, unsupported_extension);
  1203. return UNSUPPORTED_EXTENSION;
  1204. }
  1205. #else
  1206. #define TLSX_CheckUnsupportedExtension(ssl, type) 0
  1207. #define TLSX_HandleUnsupportedExtension(ssl) 0
  1208. #endif
  1209. /** Mark an extension to be sent back to the client. */
  1210. void TLSX_SetResponse(WOLFSSL* ssl, TLSX_Type type);
  1211. void TLSX_SetResponse(WOLFSSL* ssl, TLSX_Type type)
  1212. {
  1213. TLSX *extension = TLSX_Find(ssl->extensions, type);
  1214. if (extension)
  1215. extension->resp = 1;
  1216. }
  1217. /******************************************************************************/
  1218. /* Application-Layer Protocol Negotiation */
  1219. /******************************************************************************/
  1220. #ifdef HAVE_ALPN
  1221. /** Creates a new ALPN object, providing protocol name to use. */
  1222. static ALPN* TLSX_ALPN_New(char *protocol_name, word16 protocol_nameSz,
  1223. void* heap)
  1224. {
  1225. ALPN *alpn;
  1226. WOLFSSL_ENTER("TLSX_ALPN_New");
  1227. if (protocol_name == NULL ||
  1228. protocol_nameSz > WOLFSSL_MAX_ALPN_PROTO_NAME_LEN) {
  1229. WOLFSSL_MSG("Invalid arguments");
  1230. return NULL;
  1231. }
  1232. alpn = (ALPN*)XMALLOC(sizeof(ALPN), heap, DYNAMIC_TYPE_TLSX);
  1233. if (alpn == NULL) {
  1234. WOLFSSL_MSG("Memory failure");
  1235. return NULL;
  1236. }
  1237. alpn->next = NULL;
  1238. alpn->negotiated = 0;
  1239. alpn->options = 0;
  1240. alpn->protocol_name = (char*)XMALLOC(protocol_nameSz + 1,
  1241. heap, DYNAMIC_TYPE_TLSX);
  1242. if (alpn->protocol_name == NULL) {
  1243. WOLFSSL_MSG("Memory failure");
  1244. XFREE(alpn, heap, DYNAMIC_TYPE_TLSX);
  1245. return NULL;
  1246. }
  1247. XMEMCPY(alpn->protocol_name, protocol_name, protocol_nameSz);
  1248. alpn->protocol_name[protocol_nameSz] = 0;
  1249. (void)heap;
  1250. return alpn;
  1251. }
  1252. /** Releases an ALPN object. */
  1253. static void TLSX_ALPN_Free(ALPN *alpn, void* heap)
  1254. {
  1255. (void)heap;
  1256. if (alpn == NULL)
  1257. return;
  1258. XFREE(alpn->protocol_name, heap, DYNAMIC_TYPE_TLSX);
  1259. XFREE(alpn, heap, DYNAMIC_TYPE_TLSX);
  1260. }
  1261. /** Releases all ALPN objects in the provided list. */
  1262. static void TLSX_ALPN_FreeAll(ALPN *list, void* heap)
  1263. {
  1264. ALPN* alpn;
  1265. while ((alpn = list)) {
  1266. list = alpn->next;
  1267. TLSX_ALPN_Free(alpn, heap);
  1268. }
  1269. }
  1270. /** Tells the buffered size of the ALPN objects in a list. */
  1271. static word16 TLSX_ALPN_GetSize(ALPN *list)
  1272. {
  1273. ALPN* alpn;
  1274. word16 length = OPAQUE16_LEN; /* list length */
  1275. while ((alpn = list)) {
  1276. list = alpn->next;
  1277. length++; /* protocol name length is on one byte */
  1278. length += (word16)XSTRLEN(alpn->protocol_name);
  1279. }
  1280. return length;
  1281. }
  1282. /** Writes the ALPN objects of a list in a buffer. */
  1283. static word16 TLSX_ALPN_Write(ALPN *list, byte *output)
  1284. {
  1285. ALPN* alpn;
  1286. word16 length = 0;
  1287. word16 offset = OPAQUE16_LEN; /* list length offset */
  1288. while ((alpn = list)) {
  1289. list = alpn->next;
  1290. length = (word16)XSTRLEN(alpn->protocol_name);
  1291. /* protocol name length */
  1292. output[offset++] = (byte)length;
  1293. /* protocol name value */
  1294. XMEMCPY(output + offset, alpn->protocol_name, length);
  1295. offset += length;
  1296. }
  1297. /* writing list length */
  1298. c16toa(offset - OPAQUE16_LEN, output);
  1299. return offset;
  1300. }
  1301. /** Finds a protocol name in the provided ALPN list */
  1302. static ALPN* TLSX_ALPN_Find(ALPN *list, char *protocol_name, word16 size)
  1303. {
  1304. ALPN *alpn;
  1305. if (list == NULL || protocol_name == NULL)
  1306. return NULL;
  1307. alpn = list;
  1308. while (alpn != NULL && (
  1309. (word16)XSTRLEN(alpn->protocol_name) != size ||
  1310. XSTRNCMP(alpn->protocol_name, protocol_name, size)))
  1311. alpn = alpn->next;
  1312. return alpn;
  1313. }
  1314. /** Set the ALPN matching client and server requirements */
  1315. static int TLSX_SetALPN(TLSX** extensions, const void* data, word16 size,
  1316. void* heap)
  1317. {
  1318. ALPN *alpn;
  1319. int ret;
  1320. if (extensions == NULL || data == NULL)
  1321. return BAD_FUNC_ARG;
  1322. alpn = TLSX_ALPN_New((char *)data, size, heap);
  1323. if (alpn == NULL) {
  1324. WOLFSSL_MSG("Memory failure");
  1325. return MEMORY_E;
  1326. }
  1327. alpn->negotiated = 1;
  1328. ret = TLSX_Push(extensions, TLSX_APPLICATION_LAYER_PROTOCOL, (void*)alpn,
  1329. heap);
  1330. if (ret != 0) {
  1331. TLSX_ALPN_Free(alpn, heap);
  1332. return ret;
  1333. }
  1334. return WOLFSSL_SUCCESS;
  1335. }
  1336. /** Parses a buffer of ALPN extensions and set the first one matching
  1337. * client and server requirements */
  1338. static int TLSX_ALPN_ParseAndSet(WOLFSSL *ssl, byte *input, word16 length,
  1339. byte isRequest)
  1340. {
  1341. word16 size = 0, offset = 0, idx = 0;
  1342. int r = BUFFER_ERROR;
  1343. byte match = 0;
  1344. TLSX *extension;
  1345. ALPN *alpn = NULL, *list;
  1346. if (OPAQUE16_LEN > length)
  1347. return BUFFER_ERROR;
  1348. ato16(input, &size);
  1349. offset += OPAQUE16_LEN;
  1350. if (size == 0)
  1351. return BUFFER_ERROR;
  1352. extension = TLSX_Find(ssl->extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
  1353. if (extension == NULL)
  1354. extension = TLSX_Find(ssl->ctx->extensions,
  1355. TLSX_APPLICATION_LAYER_PROTOCOL);
  1356. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
  1357. if (ssl->alpnSelect != NULL) {
  1358. const byte* out;
  1359. unsigned char outLen;
  1360. if (ssl->alpnSelect(ssl, &out, &outLen, input + offset, size,
  1361. ssl->alpnSelectArg) == 0) {
  1362. WOLFSSL_MSG("ALPN protocol match");
  1363. if (TLSX_UseALPN(&ssl->extensions, (char*)out, outLen, 0, ssl->heap)
  1364. == WOLFSSL_SUCCESS) {
  1365. if (extension == NULL) {
  1366. extension = TLSX_Find(ssl->extensions,
  1367. TLSX_APPLICATION_LAYER_PROTOCOL);
  1368. }
  1369. }
  1370. }
  1371. }
  1372. #endif
  1373. if (extension == NULL || extension->data == NULL) {
  1374. return isRequest ? 0
  1375. : TLSX_HandleUnsupportedExtension(ssl);
  1376. }
  1377. /* validating alpn list length */
  1378. if (length != OPAQUE16_LEN + size)
  1379. return BUFFER_ERROR;
  1380. list = (ALPN*)extension->data;
  1381. /* keep the list sent by client */
  1382. if (isRequest) {
  1383. if (ssl->alpn_client_list != NULL)
  1384. XFREE(ssl->alpn_client_list, ssl->heap, DYNAMIC_TYPE_ALPN);
  1385. ssl->alpn_client_list = (char *)XMALLOC(size, ssl->heap,
  1386. DYNAMIC_TYPE_ALPN);
  1387. if (ssl->alpn_client_list == NULL)
  1388. return MEMORY_ERROR;
  1389. }
  1390. for (size = 0; offset < length; offset += size) {
  1391. size = input[offset++];
  1392. if (offset + size > length || size == 0)
  1393. return BUFFER_ERROR;
  1394. if (isRequest) {
  1395. XMEMCPY(ssl->alpn_client_list+idx, (char*)input + offset, size);
  1396. idx += size;
  1397. ssl->alpn_client_list[idx++] = ',';
  1398. }
  1399. if (!match) {
  1400. alpn = TLSX_ALPN_Find(list, (char*)input + offset, size);
  1401. if (alpn != NULL) {
  1402. WOLFSSL_MSG("ALPN protocol match");
  1403. match = 1;
  1404. /* skip reading other values if not required */
  1405. if (!isRequest)
  1406. break;
  1407. }
  1408. }
  1409. }
  1410. if (isRequest)
  1411. ssl->alpn_client_list[idx-1] = 0;
  1412. if (!match) {
  1413. WOLFSSL_MSG("No ALPN protocol match");
  1414. /* do nothing if no protocol match between client and server and option
  1415. is set to continue (like OpenSSL) */
  1416. if (list->options & WOLFSSL_ALPN_CONTINUE_ON_MISMATCH) {
  1417. WOLFSSL_MSG("Continue on mismatch");
  1418. return 0;
  1419. }
  1420. SendAlert(ssl, alert_fatal, no_application_protocol);
  1421. return UNKNOWN_ALPN_PROTOCOL_NAME_E;
  1422. }
  1423. /* set the matching negotiated protocol */
  1424. r = TLSX_SetALPN(&ssl->extensions,
  1425. alpn->protocol_name,
  1426. (word16)XSTRLEN(alpn->protocol_name),
  1427. ssl->heap);
  1428. if (r != WOLFSSL_SUCCESS) {
  1429. WOLFSSL_MSG("TLSX_UseALPN failed");
  1430. return BUFFER_ERROR;
  1431. }
  1432. /* reply to ALPN extension sent from client */
  1433. if (isRequest) {
  1434. #ifndef NO_WOLFSSL_SERVER
  1435. TLSX_SetResponse(ssl, TLSX_APPLICATION_LAYER_PROTOCOL);
  1436. #endif
  1437. }
  1438. return 0;
  1439. }
  1440. /** Add a protocol name to the list of accepted usable ones */
  1441. int TLSX_UseALPN(TLSX** extensions, const void* data, word16 size, byte options,
  1442. void* heap)
  1443. {
  1444. ALPN *alpn;
  1445. TLSX *extension;
  1446. int ret;
  1447. if (extensions == NULL || data == NULL)
  1448. return BAD_FUNC_ARG;
  1449. alpn = TLSX_ALPN_New((char *)data, size, heap);
  1450. if (alpn == NULL) {
  1451. WOLFSSL_MSG("Memory failure");
  1452. return MEMORY_E;
  1453. }
  1454. /* Set Options of ALPN */
  1455. alpn->options = options;
  1456. extension = TLSX_Find(*extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
  1457. if (extension == NULL) {
  1458. ret = TLSX_Push(extensions, TLSX_APPLICATION_LAYER_PROTOCOL,
  1459. (void*)alpn, heap);
  1460. if (ret != 0) {
  1461. TLSX_ALPN_Free(alpn, heap);
  1462. return ret;
  1463. }
  1464. }
  1465. else {
  1466. /* push new ALPN object to extension data. */
  1467. alpn->next = (ALPN*)extension->data;
  1468. extension->data = (void*)alpn;
  1469. }
  1470. return WOLFSSL_SUCCESS;
  1471. }
  1472. /** Get the protocol name set by the server */
  1473. int TLSX_ALPN_GetRequest(TLSX* extensions, void** data, word16 *dataSz)
  1474. {
  1475. TLSX *extension;
  1476. ALPN *alpn;
  1477. if (extensions == NULL || data == NULL || dataSz == NULL)
  1478. return BAD_FUNC_ARG;
  1479. extension = TLSX_Find(extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
  1480. if (extension == NULL) {
  1481. WOLFSSL_MSG("TLS extension not found");
  1482. return WOLFSSL_ALPN_NOT_FOUND;
  1483. }
  1484. alpn = (ALPN *)extension->data;
  1485. if (alpn == NULL) {
  1486. WOLFSSL_MSG("ALPN extension not found");
  1487. *data = NULL;
  1488. *dataSz = 0;
  1489. return WOLFSSL_FATAL_ERROR;
  1490. }
  1491. if (alpn->negotiated != 1) {
  1492. /* consider as an error */
  1493. if (alpn->options & WOLFSSL_ALPN_FAILED_ON_MISMATCH) {
  1494. WOLFSSL_MSG("No protocol match with peer -> Failed");
  1495. return WOLFSSL_FATAL_ERROR;
  1496. }
  1497. /* continue without negotiated protocol */
  1498. WOLFSSL_MSG("No protocol match with peer -> Continue");
  1499. return WOLFSSL_ALPN_NOT_FOUND;
  1500. }
  1501. if (alpn->next != NULL) {
  1502. WOLFSSL_MSG("Only one protocol name must be accepted");
  1503. return WOLFSSL_FATAL_ERROR;
  1504. }
  1505. *data = alpn->protocol_name;
  1506. *dataSz = (word16)XSTRLEN((char*)*data);
  1507. return WOLFSSL_SUCCESS;
  1508. }
  1509. #define ALPN_FREE_ALL TLSX_ALPN_FreeAll
  1510. #define ALPN_GET_SIZE TLSX_ALPN_GetSize
  1511. #define ALPN_WRITE TLSX_ALPN_Write
  1512. #define ALPN_PARSE TLSX_ALPN_ParseAndSet
  1513. #else /* HAVE_ALPN */
  1514. #define ALPN_FREE_ALL(list, heap)
  1515. #define ALPN_GET_SIZE(list) 0
  1516. #define ALPN_WRITE(a, b) 0
  1517. #define ALPN_PARSE(a, b, c, d) 0
  1518. #endif /* HAVE_ALPN */
  1519. /******************************************************************************/
  1520. /* Server Name Indication */
  1521. /******************************************************************************/
  1522. #ifdef HAVE_SNI
  1523. /** Creates a new SNI object. */
  1524. static SNI* TLSX_SNI_New(byte type, const void* data, word16 size, void* heap)
  1525. {
  1526. SNI* sni = (SNI*)XMALLOC(sizeof(SNI), heap, DYNAMIC_TYPE_TLSX);
  1527. (void)heap;
  1528. if (sni) {
  1529. sni->type = type;
  1530. sni->next = NULL;
  1531. #ifndef NO_WOLFSSL_SERVER
  1532. sni->options = 0;
  1533. sni->status = WOLFSSL_SNI_NO_MATCH;
  1534. #endif
  1535. switch (sni->type) {
  1536. case WOLFSSL_SNI_HOST_NAME:
  1537. sni->data.host_name = (char*)XMALLOC(size + 1, heap,
  1538. DYNAMIC_TYPE_TLSX);
  1539. if (sni->data.host_name) {
  1540. XSTRNCPY(sni->data.host_name, (const char*)data, size);
  1541. sni->data.host_name[size] = '\0';
  1542. } else {
  1543. XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
  1544. sni = NULL;
  1545. }
  1546. break;
  1547. default: /* invalid type */
  1548. XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
  1549. sni = NULL;
  1550. }
  1551. }
  1552. return sni;
  1553. }
  1554. /** Releases a SNI object. */
  1555. static void TLSX_SNI_Free(SNI* sni, void* heap)
  1556. {
  1557. if (sni) {
  1558. switch (sni->type) {
  1559. case WOLFSSL_SNI_HOST_NAME:
  1560. XFREE(sni->data.host_name, heap, DYNAMIC_TYPE_TLSX);
  1561. break;
  1562. }
  1563. XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
  1564. }
  1565. (void)heap;
  1566. }
  1567. /** Releases all SNI objects in the provided list. */
  1568. static void TLSX_SNI_FreeAll(SNI* list, void* heap)
  1569. {
  1570. SNI* sni;
  1571. while ((sni = list)) {
  1572. list = sni->next;
  1573. TLSX_SNI_Free(sni, heap);
  1574. }
  1575. }
  1576. /** Tells the buffered size of the SNI objects in a list. */
  1577. static word16 TLSX_SNI_GetSize(SNI* list)
  1578. {
  1579. SNI* sni;
  1580. word16 length = OPAQUE16_LEN; /* list length */
  1581. while ((sni = list)) {
  1582. list = sni->next;
  1583. length += ENUM_LEN + OPAQUE16_LEN; /* sni type + sni length */
  1584. switch (sni->type) {
  1585. case WOLFSSL_SNI_HOST_NAME:
  1586. length += (word16)XSTRLEN((char*)sni->data.host_name);
  1587. break;
  1588. }
  1589. }
  1590. return length;
  1591. }
  1592. /** Writes the SNI objects of a list in a buffer. */
  1593. static word16 TLSX_SNI_Write(SNI* list, byte* output)
  1594. {
  1595. SNI* sni;
  1596. word16 length = 0;
  1597. word16 offset = OPAQUE16_LEN; /* list length offset */
  1598. while ((sni = list)) {
  1599. list = sni->next;
  1600. output[offset++] = sni->type; /* sni type */
  1601. switch (sni->type) {
  1602. case WOLFSSL_SNI_HOST_NAME:
  1603. length = (word16)XSTRLEN((char*)sni->data.host_name);
  1604. c16toa(length, output + offset); /* sni length */
  1605. offset += OPAQUE16_LEN;
  1606. XMEMCPY(output + offset, sni->data.host_name, length);
  1607. offset += length;
  1608. break;
  1609. }
  1610. }
  1611. c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
  1612. return offset;
  1613. }
  1614. /** Finds a SNI object in the provided list. */
  1615. static SNI* TLSX_SNI_Find(SNI *list, byte type)
  1616. {
  1617. SNI* sni = list;
  1618. while (sni && sni->type != type)
  1619. sni = sni->next;
  1620. return sni;
  1621. }
  1622. /** Sets the status of a SNI object. */
  1623. static void TLSX_SNI_SetStatus(TLSX* extensions, byte type, byte status)
  1624. {
  1625. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1626. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1627. if (sni)
  1628. sni->status = status;
  1629. }
  1630. /** Gets the status of a SNI object. */
  1631. byte TLSX_SNI_Status(TLSX* extensions, byte type)
  1632. {
  1633. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1634. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1635. if (sni)
  1636. return sni->status;
  1637. return 0;
  1638. }
  1639. /** Parses a buffer of SNI extensions. */
  1640. static int TLSX_SNI_Parse(WOLFSSL* ssl, byte* input, word16 length,
  1641. byte isRequest)
  1642. {
  1643. #ifndef NO_WOLFSSL_SERVER
  1644. word16 size = 0;
  1645. word16 offset = 0;
  1646. int cacheOnly = 0;
  1647. SNI *sni = NULL;
  1648. byte type;
  1649. int matchStat;
  1650. byte matched;
  1651. #endif
  1652. TLSX *extension = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
  1653. if (!extension)
  1654. extension = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
  1655. if (!isRequest) {
  1656. #ifndef NO_WOLFSSL_CLIENT
  1657. if (!extension || !extension->data)
  1658. return TLSX_HandleUnsupportedExtension(ssl);
  1659. if (length > 0)
  1660. return BUFFER_ERROR; /* SNI response MUST be empty. */
  1661. /* This call enables wolfSSL_SNI_GetRequest() to be called in the
  1662. * client side to fetch the used SNI. It will only work if the SNI
  1663. * was set at the SSL object level. Right now we only support one
  1664. * name type, WOLFSSL_SNI_HOST_NAME, but in the future, the
  1665. * inclusion of other name types will turn this method inaccurate,
  1666. * as the extension response doesn't contains information of which
  1667. * name was accepted.
  1668. */
  1669. TLSX_SNI_SetStatus(ssl->extensions, WOLFSSL_SNI_HOST_NAME,
  1670. WOLFSSL_SNI_REAL_MATCH);
  1671. return 0;
  1672. #endif
  1673. }
  1674. #ifndef NO_WOLFSSL_SERVER
  1675. if (!extension || !extension->data) {
  1676. #if defined(WOLFSSL_ALWAYS_KEEP_SNI) && !defined(NO_WOLFSSL_SERVER)
  1677. /* This will keep SNI even though TLSX_UseSNI has not been called.
  1678. * Enable it so that the received sni is available to functions
  1679. * that use a custom callback when SNI is received.
  1680. */
  1681. cacheOnly = 1;
  1682. WOLFSSL_MSG("Forcing SSL object to store SNI parameter");
  1683. #else
  1684. /* Skipping, SNI not enabled at server side. */
  1685. return 0;
  1686. #endif
  1687. }
  1688. if (OPAQUE16_LEN > length)
  1689. return BUFFER_ERROR;
  1690. ato16(input, &size);
  1691. offset += OPAQUE16_LEN;
  1692. /* validating sni list length */
  1693. if (length != OPAQUE16_LEN + size || size == 0)
  1694. return BUFFER_ERROR;
  1695. /* SNI was badly specified and only one type is now recognized and allowed.
  1696. * Only one SNI value per type (RFC6066), so, no loop. */
  1697. type = input[offset++];
  1698. if (type != WOLFSSL_SNI_HOST_NAME)
  1699. return BUFFER_ERROR;
  1700. if (offset + OPAQUE16_LEN > length)
  1701. return BUFFER_ERROR;
  1702. ato16(input + offset, &size);
  1703. offset += OPAQUE16_LEN;
  1704. if (offset + size != length || size == 0)
  1705. return BUFFER_ERROR;
  1706. if (!cacheOnly && !(sni = TLSX_SNI_Find((SNI*)extension->data, type)))
  1707. return 0; /* not using this type of SNI. */
  1708. #ifdef WOLFSSL_TLS13
  1709. /* Don't process the second ClientHello SNI extension if there
  1710. * was problems with the first.
  1711. */
  1712. if (!cacheOnly && sni->status != 0)
  1713. return 0;
  1714. #endif
  1715. matched = cacheOnly || (XSTRLEN(sni->data.host_name) == size &&
  1716. XSTRNCMP(sni->data.host_name, (const char*)input + offset, size) == 0);
  1717. if (matched || sni->options & WOLFSSL_SNI_ANSWER_ON_MISMATCH) {
  1718. int r = TLSX_UseSNI(&ssl->extensions, type, input + offset, size,
  1719. ssl->heap);
  1720. if (r != WOLFSSL_SUCCESS)
  1721. return r; /* throws error. */
  1722. if (cacheOnly) {
  1723. WOLFSSL_MSG("Forcing storage of SNI, Fake match");
  1724. matchStat = WOLFSSL_SNI_FORCE_KEEP;
  1725. }
  1726. else if (matched) {
  1727. WOLFSSL_MSG("SNI did match!");
  1728. matchStat = WOLFSSL_SNI_REAL_MATCH;
  1729. }
  1730. else {
  1731. WOLFSSL_MSG("fake SNI match from ANSWER_ON_MISMATCH");
  1732. matchStat = WOLFSSL_SNI_FAKE_MATCH;
  1733. }
  1734. TLSX_SNI_SetStatus(ssl->extensions, type, (byte)matchStat);
  1735. if(!cacheOnly)
  1736. TLSX_SetResponse(ssl, TLSX_SERVER_NAME);
  1737. }
  1738. else if (!(sni->options & WOLFSSL_SNI_CONTINUE_ON_MISMATCH)) {
  1739. SendAlert(ssl, alert_fatal, unrecognized_name);
  1740. return UNKNOWN_SNI_HOST_NAME_E;
  1741. }
  1742. #else
  1743. (void)input;
  1744. #endif
  1745. return 0;
  1746. }
  1747. static int TLSX_SNI_VerifyParse(WOLFSSL* ssl, byte isRequest)
  1748. {
  1749. (void)ssl;
  1750. if (isRequest) {
  1751. #ifndef NO_WOLFSSL_SERVER
  1752. TLSX* ctx_ext = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
  1753. TLSX* ssl_ext = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
  1754. SNI* ctx_sni = ctx_ext ? (SNI*)ctx_ext->data : NULL;
  1755. SNI* ssl_sni = ssl_ext ? (SNI*)ssl_ext->data : NULL;
  1756. SNI* sni = NULL;
  1757. for (; ctx_sni; ctx_sni = ctx_sni->next) {
  1758. if (ctx_sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) {
  1759. sni = TLSX_SNI_Find(ssl_sni, ctx_sni->type);
  1760. if (sni) {
  1761. if (sni->status != WOLFSSL_SNI_NO_MATCH)
  1762. continue;
  1763. /* if ssl level overrides ctx level, it is ok. */
  1764. if ((sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) == 0)
  1765. continue;
  1766. }
  1767. SendAlert(ssl, alert_fatal, handshake_failure);
  1768. return SNI_ABSENT_ERROR;
  1769. }
  1770. }
  1771. for (; ssl_sni; ssl_sni = ssl_sni->next) {
  1772. if (ssl_sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) {
  1773. if (ssl_sni->status != WOLFSSL_SNI_NO_MATCH)
  1774. continue;
  1775. SendAlert(ssl, alert_fatal, handshake_failure);
  1776. return SNI_ABSENT_ERROR;
  1777. }
  1778. }
  1779. #endif /* NO_WOLFSSL_SERVER */
  1780. }
  1781. return 0;
  1782. }
  1783. int TLSX_UseSNI(TLSX** extensions, byte type, const void* data, word16 size,
  1784. void* heap)
  1785. {
  1786. TLSX* extension;
  1787. SNI* sni = NULL;
  1788. if (extensions == NULL || data == NULL)
  1789. return BAD_FUNC_ARG;
  1790. if ((sni = TLSX_SNI_New(type, data, size, heap)) == NULL)
  1791. return MEMORY_E;
  1792. extension = TLSX_Find(*extensions, TLSX_SERVER_NAME);
  1793. if (!extension) {
  1794. int ret = TLSX_Push(extensions, TLSX_SERVER_NAME, (void*)sni, heap);
  1795. if (ret != 0) {
  1796. TLSX_SNI_Free(sni, heap);
  1797. return ret;
  1798. }
  1799. }
  1800. else {
  1801. /* push new SNI object to extension data. */
  1802. sni->next = (SNI*)extension->data;
  1803. extension->data = (void*)sni;
  1804. /* remove duplicate SNI, there should be only one of each type. */
  1805. do {
  1806. if (sni->next && sni->next->type == type) {
  1807. SNI* next = sni->next;
  1808. sni->next = next->next;
  1809. TLSX_SNI_Free(next, heap);
  1810. /* there is no way to occur more than
  1811. * two SNIs of the same type.
  1812. */
  1813. break;
  1814. }
  1815. } while ((sni = sni->next));
  1816. }
  1817. return WOLFSSL_SUCCESS;
  1818. }
  1819. #ifndef NO_WOLFSSL_SERVER
  1820. /** Tells the SNI requested by the client. */
  1821. word16 TLSX_SNI_GetRequest(TLSX* extensions, byte type, void** data)
  1822. {
  1823. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1824. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1825. if (sni && sni->status != WOLFSSL_SNI_NO_MATCH) {
  1826. switch (sni->type) {
  1827. case WOLFSSL_SNI_HOST_NAME:
  1828. if (data) {
  1829. *data = sni->data.host_name;
  1830. return (word16)XSTRLEN((char*)*data);
  1831. }
  1832. }
  1833. }
  1834. return 0;
  1835. }
  1836. /** Sets the options for a SNI object. */
  1837. void TLSX_SNI_SetOptions(TLSX* extensions, byte type, byte options)
  1838. {
  1839. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1840. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1841. if (sni)
  1842. sni->options = options;
  1843. }
  1844. /** Retrieves a SNI request from a client hello buffer. */
  1845. int TLSX_SNI_GetFromBuffer(const byte* clientHello, word32 helloSz,
  1846. byte type, byte* sni, word32* inOutSz)
  1847. {
  1848. word32 offset = 0;
  1849. word32 len32 = 0;
  1850. word16 len16 = 0;
  1851. if (helloSz < RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ + CLIENT_HELLO_FIRST)
  1852. return INCOMPLETE_DATA;
  1853. /* TLS record header */
  1854. if ((enum ContentType) clientHello[offset++] != handshake) {
  1855. /* checking for SSLv2.0 client hello according to: */
  1856. /* http://tools.ietf.org/html/rfc4346#appendix-E.1 */
  1857. if ((enum HandShakeType) clientHello[++offset] == client_hello) {
  1858. offset += ENUM_LEN + VERSION_SZ; /* skip version */
  1859. ato16(clientHello + offset, &len16);
  1860. offset += OPAQUE16_LEN;
  1861. if (len16 % 3) /* cipher_spec_length must be multiple of 3 */
  1862. return BUFFER_ERROR;
  1863. ato16(clientHello + offset, &len16);
  1864. /* Returning SNI_UNSUPPORTED do not increment offset here */
  1865. if (len16 != 0) /* session_id_length must be 0 */
  1866. return BUFFER_ERROR;
  1867. return SNI_UNSUPPORTED;
  1868. }
  1869. return BUFFER_ERROR;
  1870. }
  1871. if (clientHello[offset++] != SSLv3_MAJOR)
  1872. return BUFFER_ERROR;
  1873. if (clientHello[offset++] < TLSv1_MINOR)
  1874. return SNI_UNSUPPORTED;
  1875. ato16(clientHello + offset, &len16);
  1876. offset += OPAQUE16_LEN;
  1877. if (offset + len16 > helloSz)
  1878. return INCOMPLETE_DATA;
  1879. /* Handshake header */
  1880. if ((enum HandShakeType) clientHello[offset] != client_hello)
  1881. return BUFFER_ERROR;
  1882. c24to32(clientHello + offset + 1, &len32);
  1883. offset += HANDSHAKE_HEADER_SZ;
  1884. if (offset + len32 > helloSz)
  1885. return BUFFER_ERROR;
  1886. /* client hello */
  1887. offset += VERSION_SZ + RAN_LEN; /* version, random */
  1888. if (helloSz < offset + clientHello[offset])
  1889. return BUFFER_ERROR;
  1890. offset += ENUM_LEN + clientHello[offset]; /* skip session id */
  1891. /* cypher suites */
  1892. if (helloSz < offset + OPAQUE16_LEN)
  1893. return BUFFER_ERROR;
  1894. ato16(clientHello + offset, &len16);
  1895. offset += OPAQUE16_LEN;
  1896. if (helloSz < offset + len16)
  1897. return BUFFER_ERROR;
  1898. offset += len16; /* skip cypher suites */
  1899. /* compression methods */
  1900. if (helloSz < offset + 1)
  1901. return BUFFER_ERROR;
  1902. if (helloSz < offset + clientHello[offset])
  1903. return BUFFER_ERROR;
  1904. offset += ENUM_LEN + clientHello[offset]; /* skip compression methods */
  1905. /* extensions */
  1906. if (helloSz < offset + OPAQUE16_LEN)
  1907. return 0; /* no extensions in client hello. */
  1908. ato16(clientHello + offset, &len16);
  1909. offset += OPAQUE16_LEN;
  1910. if (helloSz < offset + len16)
  1911. return BUFFER_ERROR;
  1912. while (len16 >= OPAQUE16_LEN + OPAQUE16_LEN) {
  1913. word16 extType;
  1914. word16 extLen;
  1915. ato16(clientHello + offset, &extType);
  1916. offset += OPAQUE16_LEN;
  1917. ato16(clientHello + offset, &extLen);
  1918. offset += OPAQUE16_LEN;
  1919. if (helloSz < offset + extLen)
  1920. return BUFFER_ERROR;
  1921. if (extType != TLSX_SERVER_NAME) {
  1922. offset += extLen; /* skip extension */
  1923. } else {
  1924. word16 listLen;
  1925. ato16(clientHello + offset, &listLen);
  1926. offset += OPAQUE16_LEN;
  1927. if (helloSz < offset + listLen)
  1928. return BUFFER_ERROR;
  1929. while (listLen > ENUM_LEN + OPAQUE16_LEN) {
  1930. byte sniType = clientHello[offset++];
  1931. word16 sniLen;
  1932. ato16(clientHello + offset, &sniLen);
  1933. offset += OPAQUE16_LEN;
  1934. if (helloSz < offset + sniLen)
  1935. return BUFFER_ERROR;
  1936. if (sniType != type) {
  1937. offset += sniLen;
  1938. listLen -= min(ENUM_LEN + OPAQUE16_LEN + sniLen, listLen);
  1939. continue;
  1940. }
  1941. *inOutSz = min(sniLen, *inOutSz);
  1942. XMEMCPY(sni, clientHello + offset, *inOutSz);
  1943. return WOLFSSL_SUCCESS;
  1944. }
  1945. }
  1946. len16 -= min(2 * OPAQUE16_LEN + extLen, len16);
  1947. }
  1948. return len16 ? BUFFER_ERROR : 0;
  1949. }
  1950. #endif
  1951. #define SNI_FREE_ALL TLSX_SNI_FreeAll
  1952. #define SNI_GET_SIZE TLSX_SNI_GetSize
  1953. #define SNI_WRITE TLSX_SNI_Write
  1954. #define SNI_PARSE TLSX_SNI_Parse
  1955. #define SNI_VERIFY_PARSE TLSX_SNI_VerifyParse
  1956. #else
  1957. #define SNI_FREE_ALL(list, heap)
  1958. #define SNI_GET_SIZE(list) 0
  1959. #define SNI_WRITE(a, b) 0
  1960. #define SNI_PARSE(a, b, c, d) 0
  1961. #define SNI_VERIFY_PARSE(a, b) 0
  1962. #endif /* HAVE_SNI */
  1963. /******************************************************************************/
  1964. /* Trusted CA Key Indication */
  1965. /******************************************************************************/
  1966. #ifdef HAVE_TRUSTED_CA
  1967. /** Creates a new TCA object. */
  1968. static TCA* TLSX_TCA_New(byte type, const byte* id, word16 idSz, void* heap)
  1969. {
  1970. TCA* tca = (TCA*)XMALLOC(sizeof(TCA), heap, DYNAMIC_TYPE_TLSX);
  1971. if (tca) {
  1972. XMEMSET(tca, 0, sizeof(TCA));
  1973. tca->type = type;
  1974. switch (type) {
  1975. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  1976. break;
  1977. #ifndef NO_SHA
  1978. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  1979. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  1980. if (idSz == WC_SHA_DIGEST_SIZE &&
  1981. (tca->id =
  1982. (byte*)XMALLOC(idSz, heap, DYNAMIC_TYPE_TLSX))) {
  1983. XMEMCPY(tca->id, id, idSz);
  1984. tca->idSz = idSz;
  1985. }
  1986. else {
  1987. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  1988. tca = NULL;
  1989. }
  1990. break;
  1991. #endif
  1992. case WOLFSSL_TRUSTED_CA_X509_NAME:
  1993. if (idSz > 0 &&
  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. default: /* invalid type */
  2005. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2006. tca = NULL;
  2007. }
  2008. }
  2009. (void)heap;
  2010. return tca;
  2011. }
  2012. /** Releases a TCA object. */
  2013. static void TLSX_TCA_Free(TCA* tca, void* heap)
  2014. {
  2015. (void)heap;
  2016. if (tca) {
  2017. if (tca->id)
  2018. XFREE(tca->id, heap, DYNAMIC_TYPE_TLSX);
  2019. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2020. }
  2021. }
  2022. /** Releases all TCA objects in the provided list. */
  2023. static void TLSX_TCA_FreeAll(TCA* list, void* heap)
  2024. {
  2025. TCA* tca;
  2026. while ((tca = list)) {
  2027. list = tca->next;
  2028. TLSX_TCA_Free(tca, heap);
  2029. }
  2030. }
  2031. /** Tells the buffered size of the TCA objects in a list. */
  2032. static word16 TLSX_TCA_GetSize(TCA* list)
  2033. {
  2034. TCA* tca;
  2035. word16 length = OPAQUE16_LEN; /* list length */
  2036. while ((tca = list)) {
  2037. list = tca->next;
  2038. length += ENUM_LEN; /* tca type */
  2039. switch (tca->type) {
  2040. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2041. break;
  2042. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2043. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2044. length += tca->idSz;
  2045. break;
  2046. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2047. length += OPAQUE16_LEN + tca->idSz;
  2048. break;
  2049. }
  2050. }
  2051. return length;
  2052. }
  2053. /** Writes the TCA objects of a list in a buffer. */
  2054. static word16 TLSX_TCA_Write(TCA* list, byte* output)
  2055. {
  2056. TCA* tca;
  2057. word16 offset = OPAQUE16_LEN; /* list length offset */
  2058. while ((tca = list)) {
  2059. list = tca->next;
  2060. output[offset++] = tca->type; /* tca type */
  2061. switch (tca->type) {
  2062. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2063. break;
  2064. #ifndef NO_SHA
  2065. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2066. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2067. if (tca->id != NULL) {
  2068. XMEMCPY(output + offset, tca->id, tca->idSz);
  2069. offset += tca->idSz;
  2070. }
  2071. else {
  2072. /* ID missing. Set to an empty string. */
  2073. c16toa(0, output + offset);
  2074. offset += OPAQUE16_LEN;
  2075. }
  2076. break;
  2077. #endif
  2078. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2079. if (tca->id != NULL) {
  2080. c16toa(tca->idSz, output + offset); /* tca length */
  2081. offset += OPAQUE16_LEN;
  2082. XMEMCPY(output + offset, tca->id, tca->idSz);
  2083. offset += tca->idSz;
  2084. }
  2085. else {
  2086. /* ID missing. Set to an empty string. */
  2087. c16toa(0, output + offset);
  2088. offset += OPAQUE16_LEN;
  2089. }
  2090. break;
  2091. default:
  2092. /* ID unknown. Set to an empty string. */
  2093. c16toa(0, output + offset);
  2094. offset += OPAQUE16_LEN;
  2095. }
  2096. }
  2097. c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
  2098. return offset;
  2099. }
  2100. #ifndef NO_WOLFSSL_SERVER
  2101. static TCA* TLSX_TCA_Find(TCA *list, byte type, const byte* id, word16 idSz)
  2102. {
  2103. TCA* tca = list;
  2104. while (tca && tca->type != type && type != WOLFSSL_TRUSTED_CA_PRE_AGREED &&
  2105. idSz != tca->idSz && !XMEMCMP(id, tca->id, idSz))
  2106. tca = tca->next;
  2107. return tca;
  2108. }
  2109. #endif /* NO_WOLFSSL_SERVER */
  2110. /** Parses a buffer of TCA extensions. */
  2111. static int TLSX_TCA_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2112. byte isRequest)
  2113. {
  2114. #ifndef NO_WOLFSSL_SERVER
  2115. word16 size = 0;
  2116. word16 offset = 0;
  2117. #endif
  2118. TLSX *extension = TLSX_Find(ssl->extensions, TLSX_TRUSTED_CA_KEYS);
  2119. if (!extension)
  2120. extension = TLSX_Find(ssl->ctx->extensions, TLSX_TRUSTED_CA_KEYS);
  2121. if (!isRequest) {
  2122. #ifndef NO_WOLFSSL_CLIENT
  2123. if (!extension || !extension->data)
  2124. return TLSX_HandleUnsupportedExtension(ssl);
  2125. if (length > 0)
  2126. return BUFFER_ERROR; /* TCA response MUST be empty. */
  2127. /* Set the flag that we're good for keys */
  2128. TLSX_SetResponse(ssl, TLSX_TRUSTED_CA_KEYS);
  2129. return 0;
  2130. #endif
  2131. }
  2132. #ifndef NO_WOLFSSL_SERVER
  2133. if (!extension || !extension->data) {
  2134. /* Skipping, TCA not enabled at server side. */
  2135. return 0;
  2136. }
  2137. if (OPAQUE16_LEN > length)
  2138. return BUFFER_ERROR;
  2139. ato16(input, &size);
  2140. offset += OPAQUE16_LEN;
  2141. /* validating tca list length */
  2142. if (length != OPAQUE16_LEN + size)
  2143. return BUFFER_ERROR;
  2144. for (size = 0; offset < length; offset += size) {
  2145. TCA *tca = NULL;
  2146. byte type;
  2147. const byte* id = NULL;
  2148. word16 idSz = 0;
  2149. if (offset + ENUM_LEN > length)
  2150. return BUFFER_ERROR;
  2151. type = input[offset++];
  2152. switch (type) {
  2153. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2154. break;
  2155. #ifndef NO_SHA
  2156. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2157. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2158. if (offset + WC_SHA_DIGEST_SIZE > length)
  2159. return BUFFER_ERROR;
  2160. idSz = WC_SHA_DIGEST_SIZE;
  2161. id = input + offset;
  2162. offset += idSz;
  2163. break;
  2164. #endif
  2165. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2166. if (offset + OPAQUE16_LEN > length)
  2167. return BUFFER_ERROR;
  2168. ato16(input + offset, &idSz);
  2169. offset += OPAQUE16_LEN;
  2170. if ((offset > length) || (idSz > length - offset))
  2171. return BUFFER_ERROR;
  2172. id = input + offset;
  2173. offset += idSz;
  2174. break;
  2175. default:
  2176. return TCA_INVALID_ID_TYPE;
  2177. }
  2178. /* Find the type/ID in the TCA list. */
  2179. tca = TLSX_TCA_Find((TCA*)extension->data, type, id, idSz);
  2180. if (tca != NULL) {
  2181. /* Found it. Set the response flag and break out of the loop. */
  2182. TLSX_SetResponse(ssl, TLSX_TRUSTED_CA_KEYS);
  2183. break;
  2184. }
  2185. }
  2186. #else
  2187. (void)input;
  2188. #endif
  2189. return 0;
  2190. }
  2191. /* Checks to see if the server sent a response for the TCA. */
  2192. static int TLSX_TCA_VerifyParse(WOLFSSL* ssl, byte isRequest)
  2193. {
  2194. (void)ssl;
  2195. if (!isRequest) {
  2196. #ifndef NO_WOLFSSL_CLIENT
  2197. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_TRUSTED_CA_KEYS);
  2198. if (extension && !extension->resp) {
  2199. SendAlert(ssl, alert_fatal, handshake_failure);
  2200. return TCA_ABSENT_ERROR;
  2201. }
  2202. #endif /* NO_WOLFSSL_CLIENT */
  2203. }
  2204. return 0;
  2205. }
  2206. int TLSX_UseTrustedCA(TLSX** extensions, byte type,
  2207. const byte* id, word16 idSz, void* heap)
  2208. {
  2209. TLSX* extension;
  2210. TCA* tca = NULL;
  2211. if (extensions == NULL)
  2212. return BAD_FUNC_ARG;
  2213. if ((tca = TLSX_TCA_New(type, id, idSz, heap)) == NULL)
  2214. return MEMORY_E;
  2215. extension = TLSX_Find(*extensions, TLSX_TRUSTED_CA_KEYS);
  2216. if (!extension) {
  2217. int ret = TLSX_Push(extensions, TLSX_TRUSTED_CA_KEYS, (void*)tca, heap);
  2218. if (ret != 0) {
  2219. TLSX_TCA_Free(tca, heap);
  2220. return ret;
  2221. }
  2222. }
  2223. else {
  2224. /* push new TCA object to extension data. */
  2225. tca->next = (TCA*)extension->data;
  2226. extension->data = (void*)tca;
  2227. }
  2228. return WOLFSSL_SUCCESS;
  2229. }
  2230. #define TCA_FREE_ALL TLSX_TCA_FreeAll
  2231. #define TCA_GET_SIZE TLSX_TCA_GetSize
  2232. #define TCA_WRITE TLSX_TCA_Write
  2233. #define TCA_PARSE TLSX_TCA_Parse
  2234. #define TCA_VERIFY_PARSE TLSX_TCA_VerifyParse
  2235. #else /* HAVE_TRUSTED_CA */
  2236. #define TCA_FREE_ALL(list, heap)
  2237. #define TCA_GET_SIZE(list) 0
  2238. #define TCA_WRITE(a, b) 0
  2239. #define TCA_PARSE(a, b, c, d) 0
  2240. #define TCA_VERIFY_PARSE(a, b) 0
  2241. #endif /* HAVE_TRUSTED_CA */
  2242. /******************************************************************************/
  2243. /* Max Fragment Length Negotiation */
  2244. /******************************************************************************/
  2245. #ifdef HAVE_MAX_FRAGMENT
  2246. static word16 TLSX_MFL_Write(byte* data, byte* output)
  2247. {
  2248. output[0] = data[0];
  2249. return ENUM_LEN;
  2250. }
  2251. static int TLSX_MFL_Parse(WOLFSSL* ssl, byte* input, word16 length,
  2252. byte isRequest)
  2253. {
  2254. if (length != ENUM_LEN)
  2255. return BUFFER_ERROR;
  2256. #ifdef WOLFSSL_OLD_UNSUPPORTED_EXTENSION
  2257. (void) isRequest;
  2258. #else
  2259. if (!isRequest)
  2260. if (TLSX_CheckUnsupportedExtension(ssl, TLSX_MAX_FRAGMENT_LENGTH))
  2261. return TLSX_HandleUnsupportedExtension(ssl);
  2262. #endif
  2263. switch (*input) {
  2264. case WOLFSSL_MFL_2_8 : ssl->max_fragment = 256; break;
  2265. case WOLFSSL_MFL_2_9 : ssl->max_fragment = 512; break;
  2266. case WOLFSSL_MFL_2_10: ssl->max_fragment = 1024; break;
  2267. case WOLFSSL_MFL_2_11: ssl->max_fragment = 2048; break;
  2268. case WOLFSSL_MFL_2_12: ssl->max_fragment = 4096; break;
  2269. case WOLFSSL_MFL_2_13: ssl->max_fragment = 8192; break;
  2270. default:
  2271. SendAlert(ssl, alert_fatal, illegal_parameter);
  2272. return UNKNOWN_MAX_FRAG_LEN_E;
  2273. }
  2274. #ifndef NO_WOLFSSL_SERVER
  2275. if (isRequest) {
  2276. int ret = TLSX_UseMaxFragment(&ssl->extensions, *input, ssl->heap);
  2277. if (ret != WOLFSSL_SUCCESS)
  2278. return ret; /* throw error */
  2279. TLSX_SetResponse(ssl, TLSX_MAX_FRAGMENT_LENGTH);
  2280. }
  2281. #endif
  2282. return 0;
  2283. }
  2284. int TLSX_UseMaxFragment(TLSX** extensions, byte mfl, void* heap)
  2285. {
  2286. byte* data = NULL;
  2287. int ret = 0;
  2288. if (extensions == NULL || mfl < WOLFSSL_MFL_MIN || mfl > WOLFSSL_MFL_MAX)
  2289. return BAD_FUNC_ARG;
  2290. data = (byte*)XMALLOC(ENUM_LEN, heap, DYNAMIC_TYPE_TLSX);
  2291. if (data == NULL)
  2292. return MEMORY_E;
  2293. data[0] = mfl;
  2294. ret = TLSX_Push(extensions, TLSX_MAX_FRAGMENT_LENGTH, data, heap);
  2295. if (ret != 0) {
  2296. XFREE(data, heap, DYNAMIC_TYPE_TLSX);
  2297. return ret;
  2298. }
  2299. return WOLFSSL_SUCCESS;
  2300. }
  2301. #define MFL_FREE_ALL(data, heap) XFREE(data, (heap), DYNAMIC_TYPE_TLSX)
  2302. #define MFL_GET_SIZE(data) ENUM_LEN
  2303. #define MFL_WRITE TLSX_MFL_Write
  2304. #define MFL_PARSE TLSX_MFL_Parse
  2305. #else
  2306. #define MFL_FREE_ALL(a, b)
  2307. #define MFL_GET_SIZE(a) 0
  2308. #define MFL_WRITE(a, b) 0
  2309. #define MFL_PARSE(a, b, c, d) 0
  2310. #endif /* HAVE_MAX_FRAGMENT */
  2311. /******************************************************************************/
  2312. /* Truncated HMAC */
  2313. /******************************************************************************/
  2314. #ifdef HAVE_TRUNCATED_HMAC
  2315. static int TLSX_THM_Parse(WOLFSSL* ssl, byte* input, word16 length,
  2316. byte isRequest)
  2317. {
  2318. if (length != 0 || input == NULL)
  2319. return BUFFER_ERROR;
  2320. if (!isRequest) {
  2321. #ifndef WOLFSSL_OLD_UNSUPPORTED_EXTENSION
  2322. if (TLSX_CheckUnsupportedExtension(ssl, TLSX_TRUNCATED_HMAC))
  2323. return TLSX_HandleUnsupportedExtension(ssl);
  2324. #endif
  2325. }
  2326. else {
  2327. #ifndef NO_WOLFSSL_SERVER
  2328. int ret = TLSX_UseTruncatedHMAC(&ssl->extensions, ssl->heap);
  2329. if (ret != WOLFSSL_SUCCESS)
  2330. return ret; /* throw error */
  2331. TLSX_SetResponse(ssl, TLSX_TRUNCATED_HMAC);
  2332. #endif
  2333. }
  2334. ssl->truncated_hmac = 1;
  2335. return 0;
  2336. }
  2337. int TLSX_UseTruncatedHMAC(TLSX** extensions, void* heap)
  2338. {
  2339. int ret = 0;
  2340. if (extensions == NULL)
  2341. return BAD_FUNC_ARG;
  2342. ret = TLSX_Push(extensions, TLSX_TRUNCATED_HMAC, NULL, heap);
  2343. if (ret != 0)
  2344. return ret;
  2345. return WOLFSSL_SUCCESS;
  2346. }
  2347. #define THM_PARSE TLSX_THM_Parse
  2348. #else
  2349. #define THM_PARSE(a, b, c, d) 0
  2350. #endif /* HAVE_TRUNCATED_HMAC */
  2351. /******************************************************************************/
  2352. /* Certificate Status Request */
  2353. /******************************************************************************/
  2354. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  2355. static void TLSX_CSR_Free(CertificateStatusRequest* csr, void* heap)
  2356. {
  2357. switch (csr->status_type) {
  2358. case WOLFSSL_CSR_OCSP:
  2359. FreeOcspRequest(&csr->request.ocsp);
  2360. break;
  2361. }
  2362. XFREE(csr, heap, DYNAMIC_TYPE_TLSX);
  2363. (void)heap;
  2364. }
  2365. static word16 TLSX_CSR_GetSize(CertificateStatusRequest* csr, byte isRequest)
  2366. {
  2367. word16 size = 0;
  2368. /* shut up compiler warnings */
  2369. (void) csr; (void) isRequest;
  2370. #ifndef NO_WOLFSSL_CLIENT
  2371. if (isRequest) {
  2372. switch (csr->status_type) {
  2373. case WOLFSSL_CSR_OCSP:
  2374. size += ENUM_LEN + 2 * OPAQUE16_LEN;
  2375. if (csr->request.ocsp.nonceSz)
  2376. size += OCSP_NONCE_EXT_SZ;
  2377. break;
  2378. }
  2379. }
  2380. #endif
  2381. #if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER)
  2382. if (!isRequest && csr->ssl->options.tls1_3)
  2383. return OPAQUE8_LEN + OPAQUE24_LEN + csr->response.length;
  2384. #endif
  2385. return size;
  2386. }
  2387. static word16 TLSX_CSR_Write(CertificateStatusRequest* csr, byte* output,
  2388. byte isRequest)
  2389. {
  2390. /* shut up compiler warnings */
  2391. (void) csr; (void) output; (void) isRequest;
  2392. #ifndef NO_WOLFSSL_CLIENT
  2393. if (isRequest) {
  2394. word16 offset = 0;
  2395. word16 length = 0;
  2396. /* type */
  2397. output[offset++] = csr->status_type;
  2398. switch (csr->status_type) {
  2399. case WOLFSSL_CSR_OCSP:
  2400. /* responder id list */
  2401. c16toa(0, output + offset);
  2402. offset += OPAQUE16_LEN;
  2403. /* request extensions */
  2404. if (csr->request.ocsp.nonceSz)
  2405. length = (word16)EncodeOcspRequestExtensions(
  2406. &csr->request.ocsp,
  2407. output + offset + OPAQUE16_LEN,
  2408. OCSP_NONCE_EXT_SZ);
  2409. c16toa(length, output + offset);
  2410. offset += OPAQUE16_LEN + length;
  2411. break;
  2412. }
  2413. return offset;
  2414. }
  2415. #endif
  2416. #if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER)
  2417. if (!isRequest && csr->ssl->options.tls1_3) {
  2418. word16 offset = 0;
  2419. output[offset++] = csr->status_type;
  2420. c32to24(csr->response.length, output + offset);
  2421. offset += OPAQUE24_LEN;
  2422. XMEMCPY(output + offset, csr->response.buffer, csr->response.length);
  2423. offset += csr->response.length;
  2424. return offset;
  2425. }
  2426. #endif
  2427. return 0;
  2428. }
  2429. static int TLSX_CSR_Parse(WOLFSSL* ssl, byte* input, word16 length,
  2430. byte isRequest)
  2431. {
  2432. int ret;
  2433. /* shut up compiler warnings */
  2434. (void) ssl; (void) input;
  2435. if (!isRequest) {
  2436. #ifndef NO_WOLFSSL_CLIENT
  2437. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2438. CertificateStatusRequest* csr = extension ?
  2439. (CertificateStatusRequest*)extension->data : NULL;
  2440. if (!csr) {
  2441. /* look at context level */
  2442. extension = TLSX_Find(ssl->ctx->extensions, TLSX_STATUS_REQUEST);
  2443. csr = extension ? (CertificateStatusRequest*)extension->data : NULL;
  2444. if (!csr) /* unexpected extension */
  2445. return TLSX_HandleUnsupportedExtension(ssl);
  2446. /* enable extension at ssl level */
  2447. ret = TLSX_UseCertificateStatusRequest(&ssl->extensions,
  2448. csr->status_type, csr->options, ssl,
  2449. ssl->heap, ssl->devId);
  2450. if (ret != WOLFSSL_SUCCESS)
  2451. return ret;
  2452. switch (csr->status_type) {
  2453. case WOLFSSL_CSR_OCSP:
  2454. /* propagate nonce */
  2455. if (csr->request.ocsp.nonceSz) {
  2456. OcspRequest* request =
  2457. (OcspRequest*)TLSX_CSR_GetRequest(ssl->extensions);
  2458. if (request) {
  2459. XMEMCPY(request->nonce, csr->request.ocsp.nonce,
  2460. csr->request.ocsp.nonceSz);
  2461. request->nonceSz = csr->request.ocsp.nonceSz;
  2462. }
  2463. }
  2464. break;
  2465. }
  2466. }
  2467. ssl->status_request = 1;
  2468. #ifdef WOLFSSL_TLS13
  2469. if (ssl->options.tls1_3) {
  2470. word32 resp_length;
  2471. word32 offset = 0;
  2472. /* Get the new extension potentially created above. */
  2473. extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2474. csr = extension ? (CertificateStatusRequest*)extension->data : NULL;
  2475. if (csr == NULL)
  2476. return MEMORY_ERROR;
  2477. ret = 0;
  2478. if (OPAQUE8_LEN + OPAQUE24_LEN > length)
  2479. ret = BUFFER_ERROR;
  2480. if (ret == 0 && input[offset++] != WOLFSSL_CSR_OCSP)
  2481. ret = BAD_CERTIFICATE_STATUS_ERROR;
  2482. if (ret == 0) {
  2483. c24to32(input + offset, &resp_length);
  2484. offset += OPAQUE24_LEN;
  2485. if (offset + resp_length != length)
  2486. ret = BUFFER_ERROR;
  2487. }
  2488. #if !defined(NO_WOLFSSL_SERVER)
  2489. if (ret == 0) {
  2490. csr->response.buffer = input + offset;
  2491. csr->response.length = resp_length;
  2492. }
  2493. #endif
  2494. return ret;
  2495. }
  2496. else
  2497. #endif
  2498. {
  2499. /* extension_data MUST be empty. */
  2500. return length ? BUFFER_ERROR : 0;
  2501. }
  2502. #endif
  2503. }
  2504. else {
  2505. #ifndef NO_WOLFSSL_SERVER
  2506. byte status_type;
  2507. word16 offset = 0;
  2508. word16 size = 0;
  2509. if (length == 0)
  2510. return 0;
  2511. status_type = input[offset++];
  2512. switch (status_type) {
  2513. case WOLFSSL_CSR_OCSP: {
  2514. /* skip responder_id_list */
  2515. if (length - offset < OPAQUE16_LEN)
  2516. return BUFFER_ERROR;
  2517. ato16(input + offset, &size);
  2518. offset += OPAQUE16_LEN + size;
  2519. /* skip request_extensions */
  2520. if (length - offset < OPAQUE16_LEN)
  2521. return BUFFER_ERROR;
  2522. ato16(input + offset, &size);
  2523. offset += OPAQUE16_LEN + size;
  2524. if (offset > length)
  2525. return BUFFER_ERROR;
  2526. /* is able to send OCSP response? */
  2527. if (ssl->ctx->cm == NULL || !ssl->ctx->cm->ocspStaplingEnabled)
  2528. return 0;
  2529. }
  2530. break;
  2531. /* unknown status type */
  2532. default:
  2533. return 0;
  2534. }
  2535. /* if using status_request and already sending it, skip this one */
  2536. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  2537. if (ssl->status_request_v2)
  2538. return 0;
  2539. #endif
  2540. /* accept the first good status_type and return */
  2541. ret = TLSX_UseCertificateStatusRequest(&ssl->extensions, status_type,
  2542. 0, ssl, ssl->heap, ssl->devId);
  2543. if (ret != WOLFSSL_SUCCESS)
  2544. return ret; /* throw error */
  2545. #if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER)
  2546. if (ssl->options.tls1_3) {
  2547. OcspRequest* request;
  2548. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2549. CertificateStatusRequest* csr = extension ?
  2550. (CertificateStatusRequest*)extension->data : NULL;
  2551. if (csr == NULL)
  2552. return MEMORY_ERROR;
  2553. request = &csr->request.ocsp;
  2554. ret = CreateOcspResponse(ssl, &request, &csr->response);
  2555. if (ret != 0)
  2556. return ret;
  2557. if (csr->response.buffer)
  2558. TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST);
  2559. }
  2560. else
  2561. #endif
  2562. TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST);
  2563. ssl->status_request = status_type;
  2564. #endif
  2565. }
  2566. return 0;
  2567. }
  2568. int TLSX_CSR_InitRequest(TLSX* extensions, DecodedCert* cert, void* heap)
  2569. {
  2570. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST);
  2571. CertificateStatusRequest* csr = extension ?
  2572. (CertificateStatusRequest*)extension->data : NULL;
  2573. int ret = 0;
  2574. if (csr) {
  2575. switch (csr->status_type) {
  2576. case WOLFSSL_CSR_OCSP: {
  2577. byte nonce[MAX_OCSP_NONCE_SZ];
  2578. int nonceSz = csr->request.ocsp.nonceSz;
  2579. /* preserve nonce */
  2580. XMEMCPY(nonce, csr->request.ocsp.nonce, nonceSz);
  2581. if ((ret = InitOcspRequest(&csr->request.ocsp, cert, 0, heap))
  2582. != 0)
  2583. return ret;
  2584. /* restore nonce */
  2585. XMEMCPY(csr->request.ocsp.nonce, nonce, nonceSz);
  2586. csr->request.ocsp.nonceSz = nonceSz;
  2587. }
  2588. break;
  2589. }
  2590. }
  2591. return ret;
  2592. }
  2593. void* TLSX_CSR_GetRequest(TLSX* extensions)
  2594. {
  2595. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST);
  2596. CertificateStatusRequest* csr = extension ?
  2597. (CertificateStatusRequest*)extension->data : NULL;
  2598. if (csr) {
  2599. switch (csr->status_type) {
  2600. case WOLFSSL_CSR_OCSP:
  2601. return &csr->request.ocsp;
  2602. break;
  2603. }
  2604. }
  2605. return NULL;
  2606. }
  2607. int TLSX_CSR_ForceRequest(WOLFSSL* ssl)
  2608. {
  2609. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2610. CertificateStatusRequest* csr = extension ?
  2611. (CertificateStatusRequest*)extension->data : NULL;
  2612. if (csr) {
  2613. switch (csr->status_type) {
  2614. case WOLFSSL_CSR_OCSP:
  2615. if (ssl->ctx->cm->ocspEnabled) {
  2616. csr->request.ocsp.ssl = ssl;
  2617. return CheckOcspRequest(ssl->ctx->cm->ocsp,
  2618. &csr->request.ocsp, NULL);
  2619. }
  2620. else
  2621. return OCSP_LOOKUP_FAIL;
  2622. }
  2623. }
  2624. return 0;
  2625. }
  2626. int TLSX_UseCertificateStatusRequest(TLSX** extensions, byte status_type,
  2627. byte options, WOLFSSL* ssl, void* heap,
  2628. int devId)
  2629. {
  2630. CertificateStatusRequest* csr = NULL;
  2631. int ret = 0;
  2632. if (!extensions || status_type != WOLFSSL_CSR_OCSP)
  2633. return BAD_FUNC_ARG;
  2634. csr = (CertificateStatusRequest*)
  2635. XMALLOC(sizeof(CertificateStatusRequest), heap, DYNAMIC_TYPE_TLSX);
  2636. if (!csr)
  2637. return MEMORY_E;
  2638. ForceZero(csr, sizeof(CertificateStatusRequest));
  2639. csr->status_type = status_type;
  2640. csr->options = options;
  2641. csr->ssl = ssl;
  2642. switch (csr->status_type) {
  2643. case WOLFSSL_CSR_OCSP:
  2644. if (options & WOLFSSL_CSR_OCSP_USE_NONCE) {
  2645. WC_RNG rng;
  2646. #ifndef HAVE_FIPS
  2647. ret = wc_InitRng_ex(&rng, heap, devId);
  2648. #else
  2649. ret = wc_InitRng(&rng);
  2650. (void)devId;
  2651. #endif
  2652. if (ret == 0) {
  2653. if (wc_RNG_GenerateBlock(&rng, csr->request.ocsp.nonce,
  2654. MAX_OCSP_NONCE_SZ) == 0)
  2655. csr->request.ocsp.nonceSz = MAX_OCSP_NONCE_SZ;
  2656. wc_FreeRng(&rng);
  2657. }
  2658. }
  2659. break;
  2660. }
  2661. if ((ret = TLSX_Push(extensions, TLSX_STATUS_REQUEST, csr, heap)) != 0) {
  2662. XFREE(csr, heap, DYNAMIC_TYPE_TLSX);
  2663. return ret;
  2664. }
  2665. return WOLFSSL_SUCCESS;
  2666. }
  2667. #define CSR_FREE_ALL TLSX_CSR_Free
  2668. #define CSR_GET_SIZE TLSX_CSR_GetSize
  2669. #define CSR_WRITE TLSX_CSR_Write
  2670. #define CSR_PARSE TLSX_CSR_Parse
  2671. #else
  2672. #define CSR_FREE_ALL(data, heap)
  2673. #define CSR_GET_SIZE(a, b) 0
  2674. #define CSR_WRITE(a, b, c) 0
  2675. #define CSR_PARSE(a, b, c, d) 0
  2676. #endif /* HAVE_CERTIFICATE_STATUS_REQUEST */
  2677. /******************************************************************************/
  2678. /* Certificate Status Request v2 */
  2679. /******************************************************************************/
  2680. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  2681. static void TLSX_CSR2_FreeAll(CertificateStatusRequestItemV2* csr2, void* heap)
  2682. {
  2683. CertificateStatusRequestItemV2* next;
  2684. for (; csr2; csr2 = next) {
  2685. next = csr2->next;
  2686. switch (csr2->status_type) {
  2687. case WOLFSSL_CSR2_OCSP:
  2688. case WOLFSSL_CSR2_OCSP_MULTI:
  2689. while(csr2->requests--)
  2690. FreeOcspRequest(&csr2->request.ocsp[csr2->requests]);
  2691. break;
  2692. }
  2693. XFREE(csr2, heap, DYNAMIC_TYPE_TLSX);
  2694. }
  2695. (void)heap;
  2696. }
  2697. static word16 TLSX_CSR2_GetSize(CertificateStatusRequestItemV2* csr2,
  2698. byte isRequest)
  2699. {
  2700. word16 size = 0;
  2701. /* shut up compiler warnings */
  2702. (void) csr2; (void) isRequest;
  2703. #ifndef NO_WOLFSSL_CLIENT
  2704. if (isRequest) {
  2705. CertificateStatusRequestItemV2* next;
  2706. for (size = OPAQUE16_LEN; csr2; csr2 = next) {
  2707. next = csr2->next;
  2708. switch (csr2->status_type) {
  2709. case WOLFSSL_CSR2_OCSP:
  2710. case WOLFSSL_CSR2_OCSP_MULTI:
  2711. size += ENUM_LEN + 3 * OPAQUE16_LEN;
  2712. if (csr2->request.ocsp[0].nonceSz)
  2713. size += OCSP_NONCE_EXT_SZ;
  2714. break;
  2715. }
  2716. }
  2717. }
  2718. #endif
  2719. return size;
  2720. }
  2721. static word16 TLSX_CSR2_Write(CertificateStatusRequestItemV2* csr2,
  2722. byte* output, byte isRequest)
  2723. {
  2724. /* shut up compiler warnings */
  2725. (void) csr2; (void) output; (void) isRequest;
  2726. #ifndef NO_WOLFSSL_CLIENT
  2727. if (isRequest) {
  2728. word16 offset;
  2729. word16 length;
  2730. for (offset = OPAQUE16_LEN; csr2 != NULL; csr2 = csr2->next) {
  2731. /* status_type */
  2732. output[offset++] = csr2->status_type;
  2733. /* request */
  2734. switch (csr2->status_type) {
  2735. case WOLFSSL_CSR2_OCSP:
  2736. case WOLFSSL_CSR2_OCSP_MULTI:
  2737. /* request_length */
  2738. length = 2 * OPAQUE16_LEN;
  2739. if (csr2->request.ocsp[0].nonceSz)
  2740. length += OCSP_NONCE_EXT_SZ;
  2741. c16toa(length, output + offset);
  2742. offset += OPAQUE16_LEN;
  2743. /* responder id list */
  2744. c16toa(0, output + offset);
  2745. offset += OPAQUE16_LEN;
  2746. /* request extensions */
  2747. length = 0;
  2748. if (csr2->request.ocsp[0].nonceSz)
  2749. length = (word16)EncodeOcspRequestExtensions(
  2750. &csr2->request.ocsp[0],
  2751. output + offset + OPAQUE16_LEN,
  2752. OCSP_NONCE_EXT_SZ);
  2753. c16toa(length, output + offset);
  2754. offset += OPAQUE16_LEN + length;
  2755. break;
  2756. }
  2757. }
  2758. /* list size */
  2759. c16toa(offset - OPAQUE16_LEN, output);
  2760. return offset;
  2761. }
  2762. #endif
  2763. return 0;
  2764. }
  2765. static int TLSX_CSR2_Parse(WOLFSSL* ssl, byte* input, word16 length,
  2766. byte isRequest)
  2767. {
  2768. int ret;
  2769. /* shut up compiler warnings */
  2770. (void) ssl; (void) input;
  2771. if (!isRequest) {
  2772. #ifndef NO_WOLFSSL_CLIENT
  2773. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST_V2);
  2774. CertificateStatusRequestItemV2* csr2 = extension ?
  2775. (CertificateStatusRequestItemV2*)extension->data : NULL;
  2776. if (!csr2) {
  2777. /* look at context level */
  2778. extension = TLSX_Find(ssl->ctx->extensions, TLSX_STATUS_REQUEST_V2);
  2779. csr2 = extension ?
  2780. (CertificateStatusRequestItemV2*)extension->data : NULL;
  2781. if (!csr2) /* unexpected extension */
  2782. return TLSX_HandleUnsupportedExtension(ssl);
  2783. /* enable extension at ssl level */
  2784. for (; csr2; csr2 = csr2->next) {
  2785. ret = TLSX_UseCertificateStatusRequestV2(&ssl->extensions,
  2786. csr2->status_type, csr2->options, ssl->heap,
  2787. ssl->devId);
  2788. if (ret != WOLFSSL_SUCCESS)
  2789. return ret;
  2790. switch (csr2->status_type) {
  2791. case WOLFSSL_CSR2_OCSP:
  2792. /* followed by */
  2793. case WOLFSSL_CSR2_OCSP_MULTI:
  2794. /* propagate nonce */
  2795. if (csr2->request.ocsp[0].nonceSz) {
  2796. OcspRequest* request =
  2797. (OcspRequest*)TLSX_CSR2_GetRequest(ssl->extensions,
  2798. csr2->status_type, 0);
  2799. if (request) {
  2800. XMEMCPY(request->nonce,
  2801. csr2->request.ocsp[0].nonce,
  2802. csr2->request.ocsp[0].nonceSz);
  2803. request->nonceSz =
  2804. csr2->request.ocsp[0].nonceSz;
  2805. }
  2806. }
  2807. break;
  2808. }
  2809. }
  2810. }
  2811. ssl->status_request_v2 = 1;
  2812. return length ? BUFFER_ERROR : 0; /* extension_data MUST be empty. */
  2813. #endif
  2814. }
  2815. else {
  2816. #ifndef NO_WOLFSSL_SERVER
  2817. byte status_type;
  2818. word16 request_length;
  2819. word16 offset = 0;
  2820. word16 size = 0;
  2821. /* list size */
  2822. if (offset + OPAQUE16_LEN >= length) {
  2823. return BUFFER_E;
  2824. }
  2825. ato16(input + offset, &request_length);
  2826. offset += OPAQUE16_LEN;
  2827. if (length - OPAQUE16_LEN != request_length)
  2828. return BUFFER_ERROR;
  2829. while (length > offset) {
  2830. if (length - offset < ENUM_LEN + OPAQUE16_LEN)
  2831. return BUFFER_ERROR;
  2832. status_type = input[offset++];
  2833. ato16(input + offset, &request_length);
  2834. offset += OPAQUE16_LEN;
  2835. if (length - offset < request_length)
  2836. return BUFFER_ERROR;
  2837. switch (status_type) {
  2838. case WOLFSSL_CSR2_OCSP:
  2839. case WOLFSSL_CSR2_OCSP_MULTI:
  2840. /* skip responder_id_list */
  2841. if (length - offset < OPAQUE16_LEN)
  2842. return BUFFER_ERROR;
  2843. ato16(input + offset, &size);
  2844. if (length - offset < size)
  2845. return BUFFER_ERROR;
  2846. offset += OPAQUE16_LEN + size;
  2847. /* skip request_extensions */
  2848. if (length - offset < OPAQUE16_LEN)
  2849. return BUFFER_ERROR;
  2850. ato16(input + offset, &size);
  2851. if (length - offset < size)
  2852. return BUFFER_ERROR;
  2853. offset += OPAQUE16_LEN + size;
  2854. if (offset > length)
  2855. return BUFFER_ERROR;
  2856. /* is able to send OCSP response? */
  2857. if (ssl->ctx->cm == NULL
  2858. || !ssl->ctx->cm->ocspStaplingEnabled)
  2859. continue;
  2860. break;
  2861. default:
  2862. /* unknown status type, skipping! */
  2863. offset += request_length;
  2864. continue;
  2865. }
  2866. /* if using status_request and already sending it, skip this one */
  2867. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  2868. if (ssl->status_request)
  2869. return 0;
  2870. #endif
  2871. /* TLS 1.3 servers MUST NOT act upon presence or information in
  2872. * this extension (RFC 8448 Section 4.4.2.1).
  2873. */
  2874. if (!IsAtLeastTLSv1_3(ssl->version)) {
  2875. /* accept the first good status_type and return */
  2876. ret = TLSX_UseCertificateStatusRequestV2(&ssl->extensions,
  2877. status_type, 0, ssl->heap, ssl->devId);
  2878. if (ret != WOLFSSL_SUCCESS)
  2879. return ret; /* throw error */
  2880. TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST_V2);
  2881. ssl->status_request_v2 = status_type;
  2882. }
  2883. return 0;
  2884. }
  2885. #endif
  2886. }
  2887. return 0;
  2888. }
  2889. int TLSX_CSR2_InitRequests(TLSX* extensions, DecodedCert* cert, byte isPeer,
  2890. void* heap)
  2891. {
  2892. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST_V2);
  2893. CertificateStatusRequestItemV2* csr2 = extension ?
  2894. (CertificateStatusRequestItemV2*)extension->data : NULL;
  2895. int ret = 0;
  2896. for (; csr2; csr2 = csr2->next) {
  2897. switch (csr2->status_type) {
  2898. case WOLFSSL_CSR2_OCSP:
  2899. if (!isPeer || csr2->requests != 0)
  2900. break;
  2901. FALL_THROUGH; /* followed by */
  2902. case WOLFSSL_CSR2_OCSP_MULTI: {
  2903. if (csr2->requests < 1 + MAX_CHAIN_DEPTH) {
  2904. byte nonce[MAX_OCSP_NONCE_SZ];
  2905. int nonceSz = csr2->request.ocsp[0].nonceSz;
  2906. /* preserve nonce, replicating nonce of ocsp[0] */
  2907. XMEMCPY(nonce, csr2->request.ocsp[0].nonce, nonceSz);
  2908. if ((ret = InitOcspRequest(
  2909. &csr2->request.ocsp[csr2->requests], cert,
  2910. 0, heap)) != 0)
  2911. return ret;
  2912. /* restore nonce */
  2913. XMEMCPY(csr2->request.ocsp[csr2->requests].nonce,
  2914. nonce, nonceSz);
  2915. csr2->request.ocsp[csr2->requests].nonceSz = nonceSz;
  2916. csr2->requests++;
  2917. }
  2918. }
  2919. break;
  2920. }
  2921. }
  2922. (void)cert;
  2923. return ret;
  2924. }
  2925. void* TLSX_CSR2_GetRequest(TLSX* extensions, byte status_type, byte idx)
  2926. {
  2927. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST_V2);
  2928. CertificateStatusRequestItemV2* csr2 = extension ?
  2929. (CertificateStatusRequestItemV2*)extension->data : NULL;
  2930. for (; csr2; csr2 = csr2->next) {
  2931. if (csr2->status_type == status_type) {
  2932. switch (csr2->status_type) {
  2933. case WOLFSSL_CSR2_OCSP:
  2934. /* followed by */
  2935. case WOLFSSL_CSR2_OCSP_MULTI:
  2936. /* requests are initialized in the reverse order */
  2937. return idx < csr2->requests
  2938. ? &csr2->request.ocsp[csr2->requests - idx - 1]
  2939. : NULL;
  2940. break;
  2941. }
  2942. }
  2943. }
  2944. return NULL;
  2945. }
  2946. int TLSX_CSR2_ForceRequest(WOLFSSL* ssl)
  2947. {
  2948. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST_V2);
  2949. CertificateStatusRequestItemV2* csr2 = extension ?
  2950. (CertificateStatusRequestItemV2*)extension->data : NULL;
  2951. /* forces only the first one */
  2952. if (csr2) {
  2953. switch (csr2->status_type) {
  2954. case WOLFSSL_CSR2_OCSP:
  2955. /* followed by */
  2956. case WOLFSSL_CSR2_OCSP_MULTI:
  2957. if (ssl->ctx->cm->ocspEnabled) {
  2958. csr2->request.ocsp[0].ssl = ssl;
  2959. return CheckOcspRequest(ssl->ctx->cm->ocsp,
  2960. &csr2->request.ocsp[0], NULL);
  2961. }
  2962. else
  2963. return OCSP_LOOKUP_FAIL;
  2964. }
  2965. }
  2966. return 0;
  2967. }
  2968. int TLSX_UseCertificateStatusRequestV2(TLSX** extensions, byte status_type,
  2969. byte options, void* heap, int devId)
  2970. {
  2971. TLSX* extension = NULL;
  2972. CertificateStatusRequestItemV2* csr2 = NULL;
  2973. int ret = 0;
  2974. if (!extensions)
  2975. return BAD_FUNC_ARG;
  2976. if (status_type != WOLFSSL_CSR2_OCSP
  2977. && status_type != WOLFSSL_CSR2_OCSP_MULTI)
  2978. return BAD_FUNC_ARG;
  2979. csr2 = (CertificateStatusRequestItemV2*)
  2980. XMALLOC(sizeof(CertificateStatusRequestItemV2), heap, DYNAMIC_TYPE_TLSX);
  2981. if (!csr2)
  2982. return MEMORY_E;
  2983. ForceZero(csr2, sizeof(CertificateStatusRequestItemV2));
  2984. csr2->status_type = status_type;
  2985. csr2->options = options;
  2986. csr2->next = NULL;
  2987. switch (csr2->status_type) {
  2988. case WOLFSSL_CSR2_OCSP:
  2989. case WOLFSSL_CSR2_OCSP_MULTI:
  2990. if (options & WOLFSSL_CSR2_OCSP_USE_NONCE) {
  2991. WC_RNG rng;
  2992. #ifndef HAVE_FIPS
  2993. ret = wc_InitRng_ex(&rng, heap, devId);
  2994. #else
  2995. ret = wc_InitRng(&rng);
  2996. (void)devId;
  2997. #endif
  2998. if (ret == 0) {
  2999. if (wc_RNG_GenerateBlock(&rng, csr2->request.ocsp[0].nonce,
  3000. MAX_OCSP_NONCE_SZ) == 0)
  3001. csr2->request.ocsp[0].nonceSz = MAX_OCSP_NONCE_SZ;
  3002. wc_FreeRng(&rng);
  3003. }
  3004. }
  3005. break;
  3006. }
  3007. /* append new item */
  3008. if ((extension = TLSX_Find(*extensions, TLSX_STATUS_REQUEST_V2))) {
  3009. CertificateStatusRequestItemV2* last =
  3010. (CertificateStatusRequestItemV2*)extension->data;
  3011. for (; last->next; last = last->next);
  3012. last->next = csr2;
  3013. }
  3014. else if ((ret = TLSX_Push(extensions, TLSX_STATUS_REQUEST_V2, csr2,heap))) {
  3015. XFREE(csr2, heap, DYNAMIC_TYPE_TLSX);
  3016. return ret;
  3017. }
  3018. return WOLFSSL_SUCCESS;
  3019. }
  3020. #define CSR2_FREE_ALL TLSX_CSR2_FreeAll
  3021. #define CSR2_GET_SIZE TLSX_CSR2_GetSize
  3022. #define CSR2_WRITE TLSX_CSR2_Write
  3023. #define CSR2_PARSE TLSX_CSR2_Parse
  3024. #else
  3025. #define CSR2_FREE_ALL(data, heap)
  3026. #define CSR2_GET_SIZE(a, b) 0
  3027. #define CSR2_WRITE(a, b, c) 0
  3028. #define CSR2_PARSE(a, b, c, d) 0
  3029. #endif /* HAVE_CERTIFICATE_STATUS_REQUEST_V2 */
  3030. /******************************************************************************/
  3031. /* Supported Elliptic Curves */
  3032. /******************************************************************************/
  3033. #ifdef HAVE_SUPPORTED_CURVES
  3034. #if !defined(HAVE_ECC) && !defined(HAVE_CURVE25519) && !defined(HAVE_CURVE448) \
  3035. && !defined(HAVE_FFDHE)
  3036. #error Elliptic Curves Extension requires Elliptic Curve Cryptography. \
  3037. Use --enable-ecc in the configure script or define HAVE_ECC. \
  3038. Alternatively use FFDHE for DH ciphersuites.
  3039. #endif
  3040. static int TLSX_SupportedCurve_New(SupportedCurve** curve, word16 name,
  3041. void* heap)
  3042. {
  3043. if (curve == NULL)
  3044. return BAD_FUNC_ARG;
  3045. (void)heap;
  3046. *curve = (SupportedCurve*)XMALLOC(sizeof(SupportedCurve), heap,
  3047. DYNAMIC_TYPE_TLSX);
  3048. if (*curve == NULL)
  3049. return MEMORY_E;
  3050. (*curve)->name = name;
  3051. (*curve)->next = NULL;
  3052. return 0;
  3053. }
  3054. static int TLSX_PointFormat_New(PointFormat** point, byte format, void* heap)
  3055. {
  3056. if (point == NULL)
  3057. return BAD_FUNC_ARG;
  3058. (void)heap;
  3059. *point = (PointFormat*)XMALLOC(sizeof(PointFormat), heap,
  3060. DYNAMIC_TYPE_TLSX);
  3061. if (*point == NULL)
  3062. return MEMORY_E;
  3063. (*point)->format = format;
  3064. (*point)->next = NULL;
  3065. return 0;
  3066. }
  3067. static void TLSX_SupportedCurve_FreeAll(SupportedCurve* list, void* heap)
  3068. {
  3069. SupportedCurve* curve;
  3070. while ((curve = list)) {
  3071. list = curve->next;
  3072. XFREE(curve, heap, DYNAMIC_TYPE_TLSX);
  3073. }
  3074. (void)heap;
  3075. }
  3076. static void TLSX_PointFormat_FreeAll(PointFormat* list, void* heap)
  3077. {
  3078. PointFormat* point;
  3079. while ((point = list)) {
  3080. list = point->next;
  3081. XFREE(point, heap, DYNAMIC_TYPE_TLSX);
  3082. }
  3083. (void)heap;
  3084. }
  3085. static int TLSX_SupportedCurve_Append(SupportedCurve* list, word16 name,
  3086. void* heap)
  3087. {
  3088. int ret = BAD_FUNC_ARG;
  3089. while (list) {
  3090. if (list->name == name) {
  3091. ret = 0; /* curve already in use */
  3092. break;
  3093. }
  3094. if (list->next == NULL) {
  3095. ret = TLSX_SupportedCurve_New(&list->next, name, heap);
  3096. break;
  3097. }
  3098. list = list->next;
  3099. }
  3100. return ret;
  3101. }
  3102. static int TLSX_PointFormat_Append(PointFormat* list, byte format, void* heap)
  3103. {
  3104. int ret = BAD_FUNC_ARG;
  3105. while (list) {
  3106. if (list->format == format) {
  3107. ret = 0; /* format already in use */
  3108. break;
  3109. }
  3110. if (list->next == NULL) {
  3111. ret = TLSX_PointFormat_New(&list->next, format, heap);
  3112. break;
  3113. }
  3114. list = list->next;
  3115. }
  3116. return ret;
  3117. }
  3118. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
  3119. static void TLSX_SupportedCurve_ValidateRequest(WOLFSSL* ssl, byte* semaphore)
  3120. {
  3121. word16 i;
  3122. for (i = 0; i < ssl->suites->suiteSz; i+= 2) {
  3123. if (ssl->suites->suites[i] == TLS13_BYTE)
  3124. return;
  3125. if (ssl->suites->suites[i] == ECC_BYTE ||
  3126. ssl->suites->suites[i] == CHACHA_BYTE) {
  3127. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3128. defined(HAVE_CURVE448)
  3129. return;
  3130. #endif
  3131. }
  3132. else {
  3133. #ifdef HAVE_FFDHE
  3134. return;
  3135. #endif
  3136. }
  3137. }
  3138. /* turns semaphore on to avoid sending this extension. */
  3139. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_GROUPS));
  3140. }
  3141. static void TLSX_PointFormat_ValidateRequest(WOLFSSL* ssl, byte* semaphore)
  3142. {
  3143. word16 i;
  3144. for (i = 0; i < ssl->suites->suiteSz; i+= 2) {
  3145. if (ssl->suites->suites[i] == TLS13_BYTE)
  3146. return;
  3147. if (ssl->suites->suites[i] == ECC_BYTE ||
  3148. ssl->suites->suites[i] == CHACHA_BYTE) {
  3149. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3150. defined(HAVE_CURVE448)
  3151. return;
  3152. #endif
  3153. }
  3154. }
  3155. #ifdef HAVE_FFDHE
  3156. return;
  3157. #endif
  3158. /* turns semaphore on to avoid sending this extension. */
  3159. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  3160. }
  3161. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
  3162. #ifndef NO_WOLFSSL_SERVER
  3163. static void TLSX_PointFormat_ValidateResponse(WOLFSSL* ssl, byte* semaphore)
  3164. {
  3165. #if defined(HAVE_FFDHE) || defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3166. defined(HAVE_CURVE448)
  3167. (void)semaphore;
  3168. #endif
  3169. if (ssl->options.cipherSuite0 == TLS13_BYTE)
  3170. return;
  3171. if (ssl->options.cipherSuite0 == ECC_BYTE ||
  3172. ssl->options.cipherSuite0 == CHACHA_BYTE) {
  3173. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  3174. return;
  3175. #endif
  3176. }
  3177. else {
  3178. #ifdef HAVE_FFDHE
  3179. return;
  3180. #endif
  3181. }
  3182. #if !defined(HAVE_FFDHE) || (!defined(HAVE_ECC) && !defined(HAVE_CURVE25519) \
  3183. && !defined(HAVE_CURVE448))
  3184. /* turns semaphore on to avoid sending this extension. */
  3185. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  3186. #endif
  3187. }
  3188. #endif /* !NO_WOLFSSL_SERVER */
  3189. #ifndef NO_WOLFSSL_CLIENT
  3190. static word16 TLSX_SupportedCurve_GetSize(SupportedCurve* list)
  3191. {
  3192. SupportedCurve* curve;
  3193. word16 length = OPAQUE16_LEN; /* list length */
  3194. while ((curve = list)) {
  3195. list = curve->next;
  3196. length += OPAQUE16_LEN; /* curve length */
  3197. }
  3198. return length;
  3199. }
  3200. #endif
  3201. static word16 TLSX_PointFormat_GetSize(PointFormat* list)
  3202. {
  3203. PointFormat* point;
  3204. word16 length = ENUM_LEN; /* list length */
  3205. while ((point = list)) {
  3206. list = point->next;
  3207. length += ENUM_LEN; /* format length */
  3208. }
  3209. return length;
  3210. }
  3211. #ifndef NO_WOLFSSL_CLIENT
  3212. static word16 TLSX_SupportedCurve_Write(SupportedCurve* list, byte* output)
  3213. {
  3214. word16 offset = OPAQUE16_LEN;
  3215. while (list) {
  3216. c16toa(list->name, output + offset);
  3217. offset += OPAQUE16_LEN;
  3218. list = list->next;
  3219. }
  3220. c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
  3221. return offset;
  3222. }
  3223. #endif
  3224. static word16 TLSX_PointFormat_Write(PointFormat* list, byte* output)
  3225. {
  3226. word16 offset = ENUM_LEN;
  3227. while (list) {
  3228. output[offset++] = list->format;
  3229. list = list->next;
  3230. }
  3231. output[0] = (byte)(offset - ENUM_LEN);
  3232. return offset;
  3233. }
  3234. #if !defined(NO_WOLFSSL_SERVER) || (defined(WOLFSSL_TLS13) && \
  3235. !defined(WOLFSSL_NO_SERVER_GROUPS_EXT))
  3236. static int TLSX_SupportedCurve_Parse(WOLFSSL* ssl, byte* input, word16 length,
  3237. byte isRequest)
  3238. {
  3239. word16 offset;
  3240. word16 name;
  3241. int ret;
  3242. if(!isRequest && !IsAtLeastTLSv1_3(ssl->version)) {
  3243. #ifdef WOLFSSL_ALLOW_SERVER_SC_EXT
  3244. return 0;
  3245. #else
  3246. return BUFFER_ERROR; /* servers doesn't send this extension. */
  3247. #endif
  3248. }
  3249. if (OPAQUE16_LEN > length || length % OPAQUE16_LEN)
  3250. return BUFFER_ERROR;
  3251. ato16(input, &offset);
  3252. /* validating curve list length */
  3253. if (length != OPAQUE16_LEN + offset)
  3254. return BUFFER_ERROR;
  3255. offset = OPAQUE16_LEN;
  3256. if (offset == length)
  3257. return 0;
  3258. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
  3259. if (!isRequest) {
  3260. TLSX* extension;
  3261. SupportedCurve* curve;
  3262. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3263. if (extension != NULL) {
  3264. /* Replace client list with server list of supported groups. */
  3265. curve = (SupportedCurve*)extension->data;
  3266. extension->data = NULL;
  3267. TLSX_SupportedCurve_FreeAll(curve, ssl->heap);
  3268. ato16(input + offset, &name);
  3269. offset += OPAQUE16_LEN;
  3270. ret = TLSX_SupportedCurve_New(&curve, name, ssl->heap);
  3271. if (ret != 0)
  3272. return ret; /* throw error */
  3273. extension->data = (void*)curve;
  3274. }
  3275. }
  3276. #endif
  3277. for (; offset < length; offset += OPAQUE16_LEN) {
  3278. ato16(input + offset, &name);
  3279. ret = TLSX_UseSupportedCurve(&ssl->extensions, name, ssl->heap);
  3280. if (ret != WOLFSSL_SUCCESS)
  3281. return ret; /* throw error */
  3282. }
  3283. return 0;
  3284. }
  3285. #endif
  3286. #if !defined(NO_WOLFSSL_SERVER)
  3287. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
  3288. /* Checks the priority of the groups on the server and set the supported groups
  3289. * response if there is a group not advertised by the client that is preferred.
  3290. *
  3291. * ssl SSL/TLS object.
  3292. * returns 0 on success, otherwise an error.
  3293. */
  3294. int TLSX_SupportedCurve_CheckPriority(WOLFSSL* ssl)
  3295. {
  3296. int ret;
  3297. TLSX* extension;
  3298. TLSX* priority = NULL;
  3299. TLSX* ext = NULL;
  3300. word16 name;
  3301. SupportedCurve* curve;
  3302. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3303. /* May be doing PSK with no key exchange. */
  3304. if (extension == NULL)
  3305. return 0;
  3306. if ((ret = TLSX_PopulateSupportedGroups(ssl, &priority)) != WOLFSSL_SUCCESS)
  3307. return ret;
  3308. ext = TLSX_Find(priority, TLSX_SUPPORTED_GROUPS);
  3309. curve = (SupportedCurve*)ext->data;
  3310. name = curve->name;
  3311. curve = (SupportedCurve*)extension->data;
  3312. while (curve != NULL) {
  3313. if (curve->name == name)
  3314. break;
  3315. curve = curve->next;
  3316. }
  3317. if (curve == NULL) {
  3318. /* Couldn't find the preferred group in client list. */
  3319. extension->resp = 1;
  3320. /* Send server list back and free client list. */
  3321. curve = (SupportedCurve*)extension->data;
  3322. extension->data = ext->data;
  3323. ext->data = curve;
  3324. }
  3325. TLSX_FreeAll(priority, ssl->heap);
  3326. return 0;
  3327. }
  3328. #endif /* WOLFSSL_TLS13 && !WOLFSSL_NO_SERVER_GROUPS_EXT */
  3329. #if defined(HAVE_FFDHE) && !defined(WOLFSSL_NO_TLS12)
  3330. /* Set the highest priority common FFDHE group on the server as compared to
  3331. * client extensions.
  3332. *
  3333. * ssl SSL/TLS object.
  3334. * returns 0 on success, otherwise an error.
  3335. */
  3336. int TLSX_SupportedFFDHE_Set(WOLFSSL* ssl)
  3337. {
  3338. int ret = 0;
  3339. TLSX* extension;
  3340. TLSX* priority = NULL;
  3341. TLSX* ext = NULL;
  3342. SupportedCurve* serverGroup;
  3343. SupportedCurve* clientGroup;
  3344. SupportedCurve* group;
  3345. const DhParams* params = NULL;
  3346. int found = 0;
  3347. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3348. /* May be doing PSK with no key exchange. */
  3349. if (extension == NULL)
  3350. return 0;
  3351. clientGroup = (SupportedCurve*)extension->data;
  3352. for (group = clientGroup; group != NULL; group = group->next) {
  3353. if (group->name >= MIN_FFHDE_GROUP && group->name <= MAX_FFHDE_GROUP) {
  3354. found = 1;
  3355. break;
  3356. }
  3357. }
  3358. if (!found)
  3359. return 0;
  3360. if (ssl->buffers.serverDH_P.buffer && ssl->buffers.weOwnDH) {
  3361. XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap,
  3362. DYNAMIC_TYPE_PUBLIC_KEY);
  3363. }
  3364. if (ssl->buffers.serverDH_G.buffer && ssl->buffers.weOwnDH) {
  3365. XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap,
  3366. DYNAMIC_TYPE_PUBLIC_KEY);
  3367. }
  3368. ssl->buffers.serverDH_P.buffer = NULL;
  3369. ssl->buffers.serverDH_G.buffer = NULL;
  3370. ssl->buffers.weOwnDH = 0;
  3371. ssl->options.haveDH = 0;
  3372. if ((ret = TLSX_PopulateSupportedGroups(ssl, &priority)) != WOLFSSL_SUCCESS) {
  3373. TLSX_FreeAll(priority, ssl->heap);
  3374. return ret;
  3375. }
  3376. ret = 0;
  3377. ext = TLSX_Find(priority, TLSX_SUPPORTED_GROUPS);
  3378. serverGroup = (SupportedCurve*)ext->data;
  3379. for (; serverGroup != NULL; serverGroup = serverGroup->next) {
  3380. if ((serverGroup->name & NAMED_DH_MASK) != NAMED_DH_MASK)
  3381. continue;
  3382. for (group = clientGroup; group != NULL; group = group->next) {
  3383. if (serverGroup->name != group->name)
  3384. continue;
  3385. switch (serverGroup->name) {
  3386. #ifdef HAVE_FFDHE_2048
  3387. case WOLFSSL_FFDHE_2048:
  3388. params = wc_Dh_ffdhe2048_Get();
  3389. break;
  3390. #endif
  3391. #ifdef HAVE_FFDHE_3072
  3392. case WOLFSSL_FFDHE_3072:
  3393. params = wc_Dh_ffdhe3072_Get();
  3394. break;
  3395. #endif
  3396. #ifdef HAVE_FFDHE_4096
  3397. case WOLFSSL_FFDHE_4096:
  3398. params = wc_Dh_ffdhe4096_Get();
  3399. break;
  3400. #endif
  3401. #ifdef HAVE_FFDHE_6144
  3402. case WOLFSSL_FFDHE_6144:
  3403. params = wc_Dh_ffdhe6144_Get();
  3404. break;
  3405. #endif
  3406. #ifdef HAVE_FFDHE_8192
  3407. case WOLFSSL_FFDHE_8192:
  3408. params = wc_Dh_ffdhe8192_Get();
  3409. break;
  3410. #endif
  3411. }
  3412. if (params == NULL)
  3413. return BAD_FUNC_ARG;
  3414. if (params->p_len >= ssl->options.minDhKeySz &&
  3415. params->p_len <= ssl->options.maxDhKeySz) {
  3416. break;
  3417. }
  3418. }
  3419. if (group != NULL && serverGroup->name == group->name)
  3420. break;
  3421. }
  3422. if (serverGroup) {
  3423. ssl->buffers.serverDH_P.buffer = (unsigned char *)params->p;
  3424. ssl->buffers.serverDH_P.length = params->p_len;
  3425. ssl->buffers.serverDH_G.buffer = (unsigned char *)params->g;
  3426. ssl->buffers.serverDH_G.length = params->g_len;
  3427. ssl->namedGroup = serverGroup->name;
  3428. #if !defined(WOLFSSL_OLD_PRIME_CHECK) && \
  3429. !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  3430. ssl->options.dhDoKeyTest = 0;
  3431. #endif
  3432. ssl->options.haveDH = 1;
  3433. }
  3434. TLSX_FreeAll(priority, ssl->heap);
  3435. return ret;
  3436. }
  3437. #endif /* HAVE_FFDHE && !WOLFSSL_NO_TLS12 */
  3438. #endif /* !NO_WOLFSSL_SERVER */
  3439. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
  3440. /* Return the preferred group.
  3441. *
  3442. * ssl SSL/TLS object.
  3443. * checkSupported Whether to check for the first supported group.
  3444. * returns BAD_FUNC_ARG if no group found, otherwise the group.
  3445. */
  3446. int TLSX_SupportedCurve_Preferred(WOLFSSL* ssl, int checkSupported)
  3447. {
  3448. TLSX* extension;
  3449. SupportedCurve* curve;
  3450. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3451. if (extension == NULL)
  3452. return BAD_FUNC_ARG;
  3453. curve = (SupportedCurve*)extension->data;
  3454. while (curve != NULL) {
  3455. if (!checkSupported || TLSX_KeyShare_IsSupported(curve->name))
  3456. return curve->name;
  3457. curve = curve->next;
  3458. }
  3459. return BAD_FUNC_ARG;
  3460. }
  3461. #endif /* HAVE_SUPPORTED_CURVES */
  3462. #ifndef NO_WOLFSSL_SERVER
  3463. static int TLSX_PointFormat_Parse(WOLFSSL* ssl, byte* input, word16 length,
  3464. byte isRequest)
  3465. {
  3466. int ret;
  3467. /* validating formats list length */
  3468. if (ENUM_LEN > length || length != (word16)ENUM_LEN + input[0])
  3469. return BUFFER_ERROR;
  3470. if (isRequest) {
  3471. /* adding uncompressed point format to response */
  3472. ret = TLSX_UsePointFormat(&ssl->extensions, WOLFSSL_EC_PF_UNCOMPRESSED,
  3473. ssl->heap);
  3474. if (ret != WOLFSSL_SUCCESS)
  3475. return ret; /* throw error */
  3476. TLSX_SetResponse(ssl, TLSX_EC_POINT_FORMATS);
  3477. }
  3478. return 0;
  3479. }
  3480. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  3481. int TLSX_ValidateSupportedCurves(WOLFSSL* ssl, byte first, byte second) {
  3482. TLSX* extension = NULL;
  3483. SupportedCurve* curve = NULL;
  3484. word32 oid = 0;
  3485. word32 pkOid = 0;
  3486. word32 defOid = 0;
  3487. word32 defSz = 80; /* Maximum known curve size is 66. */
  3488. word32 nextOid = 0;
  3489. word32 nextSz = 80; /* Maximum known curve size is 66. */
  3490. word32 currOid = ssl->ecdhCurveOID;
  3491. int ephmSuite = 0;
  3492. word16 octets = 0; /* according to 'ecc_set_type ecc_sets[];' */
  3493. int sig = 0; /* validate signature */
  3494. int key = 0; /* validate key */
  3495. (void)oid;
  3496. if (first == ECC_BYTE || first == CHACHA_BYTE)
  3497. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3498. if (!extension)
  3499. return 1; /* no suite restriction */
  3500. for (curve = (SupportedCurve*)extension->data;
  3501. curve && !(sig && key);
  3502. curve = curve->next) {
  3503. #ifdef OPENSSL_EXTRA
  3504. /* skip if name is not in supported ECC range */
  3505. if (curve->name > WOLFSSL_ECC_X448)
  3506. continue;
  3507. /* skip if curve is disabled by user */
  3508. if (ssl->ctx->disabledCurves & (1 << curve->name))
  3509. continue;
  3510. #endif
  3511. /* find supported curve */
  3512. switch (curve->name) {
  3513. #ifdef HAVE_ECC
  3514. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  3515. #ifndef NO_ECC_SECP
  3516. case WOLFSSL_ECC_SECP160R1:
  3517. pkOid = oid = ECC_SECP160R1_OID;
  3518. octets = 20;
  3519. break;
  3520. #endif /* !NO_ECC_SECP */
  3521. #ifdef HAVE_ECC_SECPR2
  3522. case WOLFSSL_ECC_SECP160R2:
  3523. pkOid = oid = ECC_SECP160R2_OID;
  3524. octets = 20;
  3525. break;
  3526. #endif /* HAVE_ECC_SECPR2 */
  3527. #ifdef HAVE_ECC_KOBLITZ
  3528. case WOLFSSL_ECC_SECP160K1:
  3529. pkOid = oid = ECC_SECP160K1_OID;
  3530. octets = 20;
  3531. break;
  3532. #endif /* HAVE_ECC_KOBLITZ */
  3533. #endif
  3534. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  3535. #ifndef NO_ECC_SECP
  3536. case WOLFSSL_ECC_SECP192R1:
  3537. pkOid = oid = ECC_SECP192R1_OID;
  3538. octets = 24;
  3539. break;
  3540. #endif /* !NO_ECC_SECP */
  3541. #ifdef HAVE_ECC_KOBLITZ
  3542. case WOLFSSL_ECC_SECP192K1:
  3543. pkOid = oid = ECC_SECP192K1_OID;
  3544. octets = 24;
  3545. break;
  3546. #endif /* HAVE_ECC_KOBLITZ */
  3547. #endif
  3548. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  3549. #ifndef NO_ECC_SECP
  3550. case WOLFSSL_ECC_SECP224R1:
  3551. pkOid = oid = ECC_SECP224R1_OID;
  3552. octets = 28;
  3553. break;
  3554. #endif /* !NO_ECC_SECP */
  3555. #ifdef HAVE_ECC_KOBLITZ
  3556. case WOLFSSL_ECC_SECP224K1:
  3557. pkOid = oid = ECC_SECP224K1_OID;
  3558. octets = 28;
  3559. break;
  3560. #endif /* HAVE_ECC_KOBLITZ */
  3561. #endif
  3562. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  3563. #ifndef NO_ECC_SECP
  3564. case WOLFSSL_ECC_SECP256R1:
  3565. pkOid = oid = ECC_SECP256R1_OID;
  3566. octets = 32;
  3567. break;
  3568. #endif /* !NO_ECC_SECP */
  3569. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  3570. #endif
  3571. #ifdef HAVE_CURVE25519
  3572. case WOLFSSL_ECC_X25519:
  3573. oid = ECC_X25519_OID;
  3574. #ifdef HAVE_ED25519
  3575. pkOid = ECC_ED25519_OID;
  3576. #else
  3577. pkOid = ECC_X25519_OID;
  3578. #endif
  3579. octets = 32;
  3580. break;
  3581. #endif /* HAVE_CURVE25519 */
  3582. #ifdef HAVE_ECC
  3583. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  3584. #ifdef HAVE_ECC_KOBLITZ
  3585. case WOLFSSL_ECC_SECP256K1:
  3586. pkOid = oid = ECC_SECP256K1_OID;
  3587. octets = 32;
  3588. break;
  3589. #endif /* HAVE_ECC_KOBLITZ */
  3590. #ifdef HAVE_ECC_BRAINPOOL
  3591. case WOLFSSL_ECC_BRAINPOOLP256R1:
  3592. pkOid = oid = ECC_BRAINPOOLP256R1_OID;
  3593. octets = 32;
  3594. break;
  3595. #endif /* HAVE_ECC_BRAINPOOL */
  3596. #endif
  3597. #endif
  3598. #ifdef HAVE_CURVE448
  3599. case WOLFSSL_ECC_X448:
  3600. oid = ECC_X448_OID;
  3601. #ifdef HAVE_ED448
  3602. pkOid = ECC_ED448_OID;
  3603. #else
  3604. pkOid = ECC_X448_OID;
  3605. #endif
  3606. octets = 57;
  3607. break;
  3608. #endif /* HAVE_CURVE448 */
  3609. #ifdef HAVE_ECC
  3610. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  3611. #ifndef NO_ECC_SECP
  3612. case WOLFSSL_ECC_SECP384R1:
  3613. pkOid = oid = ECC_SECP384R1_OID;
  3614. octets = 48;
  3615. break;
  3616. #endif /* !NO_ECC_SECP */
  3617. #ifdef HAVE_ECC_BRAINPOOL
  3618. case WOLFSSL_ECC_BRAINPOOLP384R1:
  3619. pkOid = oid = ECC_BRAINPOOLP384R1_OID;
  3620. octets = 48;
  3621. break;
  3622. #endif /* HAVE_ECC_BRAINPOOL */
  3623. #endif
  3624. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  3625. #ifdef HAVE_ECC_BRAINPOOL
  3626. case WOLFSSL_ECC_BRAINPOOLP512R1:
  3627. pkOid = oid = ECC_BRAINPOOLP512R1_OID;
  3628. octets = 64;
  3629. break;
  3630. #endif /* HAVE_ECC_BRAINPOOL */
  3631. #endif
  3632. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  3633. #ifndef NO_ECC_SECP
  3634. case WOLFSSL_ECC_SECP521R1:
  3635. pkOid = oid = ECC_SECP521R1_OID;
  3636. octets = 66;
  3637. break;
  3638. #endif /* !NO_ECC_SECP */
  3639. #endif
  3640. #endif
  3641. default: continue; /* unsupported curve */
  3642. }
  3643. #ifdef HAVE_ECC
  3644. /* Set default Oid */
  3645. if (defOid == 0 && ssl->eccTempKeySz <= octets && defSz > octets) {
  3646. defOid = oid;
  3647. defSz = octets;
  3648. }
  3649. /* The eccTempKeySz is the preferred ephemeral key size */
  3650. if (currOid == 0 && ssl->eccTempKeySz == octets)
  3651. currOid = oid;
  3652. if ((nextOid == 0 || nextSz > octets) && ssl->eccTempKeySz <= octets) {
  3653. nextOid = oid;
  3654. nextSz = octets;
  3655. }
  3656. #else
  3657. if (defOid == 0 && defSz > octets) {
  3658. defOid = oid;
  3659. defSz = octets;
  3660. }
  3661. if (currOid == 0)
  3662. currOid = oid;
  3663. if (nextOid == 0 || nextSz > octets) {
  3664. nextOid = oid;
  3665. nextSz = octets;
  3666. }
  3667. #endif
  3668. if (first == ECC_BYTE) {
  3669. switch (second) {
  3670. /* ECDHE_ECDSA */
  3671. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA:
  3672. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA:
  3673. case TLS_ECDHE_ECDSA_WITH_RC4_128_SHA:
  3674. case TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA:
  3675. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256:
  3676. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384:
  3677. case TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
  3678. case TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:
  3679. case TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8:
  3680. case TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8:
  3681. sig |= ssl->pkCurveOID == pkOid;
  3682. key |= ssl->ecdhCurveOID == oid;
  3683. ephmSuite = 1;
  3684. break;
  3685. #ifdef WOLFSSL_STATIC_DH
  3686. /* ECDH_ECDSA */
  3687. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA:
  3688. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA:
  3689. case TLS_ECDH_ECDSA_WITH_RC4_128_SHA:
  3690. case TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA:
  3691. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256:
  3692. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384:
  3693. case TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256:
  3694. case TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384:
  3695. if (oid == ECC_X25519_OID && defOid == oid) {
  3696. defOid = 0;
  3697. defSz = 80;
  3698. }
  3699. if (oid == ECC_X448_OID && defOid == oid) {
  3700. defOid = 0;
  3701. defSz = 80;
  3702. }
  3703. sig |= ssl->pkCurveOID == pkOid;
  3704. key |= ssl->pkCurveOID == oid;
  3705. break;
  3706. #endif /* WOLFSSL_STATIC_DH */
  3707. #ifndef NO_RSA
  3708. /* ECDHE_RSA */
  3709. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA:
  3710. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA:
  3711. case TLS_ECDHE_RSA_WITH_RC4_128_SHA:
  3712. case TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA:
  3713. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256:
  3714. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384:
  3715. case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:
  3716. case TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384:
  3717. sig = 1;
  3718. key |= ssl->ecdhCurveOID == oid;
  3719. ephmSuite = 1;
  3720. break;
  3721. #ifdef WOLFSSL_STATIC_DH
  3722. /* ECDH_RSA */
  3723. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA:
  3724. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA:
  3725. case TLS_ECDH_RSA_WITH_RC4_128_SHA:
  3726. case TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA:
  3727. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256:
  3728. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384:
  3729. case TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256:
  3730. case TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384:
  3731. if (oid == ECC_X25519_OID && defOid == oid) {
  3732. defOid = 0;
  3733. defSz = 80;
  3734. }
  3735. if (oid == ECC_X448_OID && defOid == oid) {
  3736. defOid = 0;
  3737. defSz = 80;
  3738. }
  3739. sig = 1;
  3740. key |= ssl->pkCurveOID == pkOid;
  3741. break;
  3742. #endif /* WOLFSSL_STATIC_DH */
  3743. #endif
  3744. default:
  3745. if (oid == ECC_X25519_OID && defOid == oid) {
  3746. defOid = 0;
  3747. defSz = 80;
  3748. }
  3749. if (oid == ECC_X448_OID && defOid == oid) {
  3750. defOid = 0;
  3751. defSz = 80;
  3752. }
  3753. if (oid != ECC_X25519_OID && oid != ECC_X448_OID) {
  3754. sig = 1;
  3755. }
  3756. key = 1;
  3757. break;
  3758. }
  3759. }
  3760. /* ChaCha20-Poly1305 ECC cipher suites */
  3761. if (first == CHACHA_BYTE) {
  3762. switch (second) {
  3763. /* ECDHE_ECDSA */
  3764. case TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 :
  3765. case TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 :
  3766. sig |= ssl->pkCurveOID == pkOid;
  3767. key |= ssl->ecdhCurveOID == oid;
  3768. ephmSuite = 1;
  3769. break;
  3770. #ifndef NO_RSA
  3771. /* ECDHE_RSA */
  3772. case TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 :
  3773. case TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 :
  3774. sig = 1;
  3775. key |= ssl->ecdhCurveOID == oid;
  3776. ephmSuite = 1;
  3777. break;
  3778. #endif
  3779. default:
  3780. sig = 1;
  3781. key = 1;
  3782. break;
  3783. }
  3784. }
  3785. }
  3786. /* Choose the default if it is at the required strength. */
  3787. #ifdef HAVE_ECC
  3788. if (ssl->ecdhCurveOID == 0 && defSz == ssl->eccTempKeySz)
  3789. #else
  3790. if (ssl->ecdhCurveOID == 0)
  3791. #endif
  3792. {
  3793. key = 1;
  3794. ssl->ecdhCurveOID = defOid;
  3795. }
  3796. /* Choose any curve at the required strength. */
  3797. if (ssl->ecdhCurveOID == 0) {
  3798. key = 1;
  3799. ssl->ecdhCurveOID = currOid;
  3800. }
  3801. /* Choose the default if it is at the next highest strength. */
  3802. if (ssl->ecdhCurveOID == 0 && defSz == nextSz)
  3803. ssl->ecdhCurveOID = defOid;
  3804. /* Choose any curve at the next highest strength. */
  3805. if (ssl->ecdhCurveOID == 0)
  3806. ssl->ecdhCurveOID = nextOid;
  3807. /* No curve and ephemeral ECC suite requires a matching curve. */
  3808. if (ssl->ecdhCurveOID == 0 && ephmSuite)
  3809. key = 0;
  3810. return sig && key;
  3811. }
  3812. #endif
  3813. #endif /* NO_WOLFSSL_SERVER */
  3814. int TLSX_UseSupportedCurve(TLSX** extensions, word16 name, void* heap)
  3815. {
  3816. TLSX* extension = NULL;
  3817. SupportedCurve* curve = NULL;
  3818. int ret;
  3819. if (extensions == NULL)
  3820. return BAD_FUNC_ARG;
  3821. extension = TLSX_Find(*extensions, TLSX_SUPPORTED_GROUPS);
  3822. if (!extension) {
  3823. ret = TLSX_SupportedCurve_New(&curve, name, heap);
  3824. if (ret != 0)
  3825. return ret;
  3826. ret = TLSX_Push(extensions, TLSX_SUPPORTED_GROUPS, curve, heap);
  3827. if (ret != 0) {
  3828. XFREE(curve, heap, DYNAMIC_TYPE_TLSX);
  3829. return ret;
  3830. }
  3831. }
  3832. else {
  3833. ret = TLSX_SupportedCurve_Append((SupportedCurve*)extension->data, name,
  3834. heap);
  3835. if (ret != 0)
  3836. return ret;
  3837. }
  3838. return WOLFSSL_SUCCESS;
  3839. }
  3840. int TLSX_UsePointFormat(TLSX** extensions, byte format, void* heap)
  3841. {
  3842. TLSX* extension = NULL;
  3843. PointFormat* point = NULL;
  3844. int ret = 0;
  3845. if (extensions == NULL)
  3846. return BAD_FUNC_ARG;
  3847. extension = TLSX_Find(*extensions, TLSX_EC_POINT_FORMATS);
  3848. if (!extension) {
  3849. ret = TLSX_PointFormat_New(&point, format, heap);
  3850. if (ret != 0)
  3851. return ret;
  3852. ret = TLSX_Push(extensions, TLSX_EC_POINT_FORMATS, point, heap);
  3853. if (ret != 0) {
  3854. XFREE(point, heap, DYNAMIC_TYPE_TLSX);
  3855. return ret;
  3856. }
  3857. }
  3858. else {
  3859. ret = TLSX_PointFormat_Append((PointFormat*)extension->data, format,
  3860. heap);
  3861. if (ret != 0)
  3862. return ret;
  3863. }
  3864. return WOLFSSL_SUCCESS;
  3865. }
  3866. #define EC_FREE_ALL TLSX_SupportedCurve_FreeAll
  3867. #define EC_VALIDATE_REQUEST TLSX_SupportedCurve_ValidateRequest
  3868. #ifndef NO_WOLFSSL_CLIENT
  3869. #define EC_GET_SIZE TLSX_SupportedCurve_GetSize
  3870. #define EC_WRITE TLSX_SupportedCurve_Write
  3871. #else
  3872. #define EC_GET_SIZE(list) 0
  3873. #define EC_WRITE(a, b) 0
  3874. #endif
  3875. #if !defined(NO_WOLFSSL_SERVER) || (defined(WOLFSSL_TLS13) && \
  3876. !defined(WOLFSSL_NO_SERVER_GROUPS_EXT))
  3877. #define EC_PARSE TLSX_SupportedCurve_Parse
  3878. #else
  3879. #define EC_PARSE(a, b, c, d) 0
  3880. #endif
  3881. #define PF_FREE_ALL TLSX_PointFormat_FreeAll
  3882. #define PF_VALIDATE_REQUEST TLSX_PointFormat_ValidateRequest
  3883. #define PF_VALIDATE_RESPONSE TLSX_PointFormat_ValidateResponse
  3884. #define PF_GET_SIZE TLSX_PointFormat_GetSize
  3885. #define PF_WRITE TLSX_PointFormat_Write
  3886. #ifndef NO_WOLFSSL_SERVER
  3887. #define PF_PARSE TLSX_PointFormat_Parse
  3888. #else
  3889. #define PF_PARSE(a, b, c, d) 0
  3890. #endif
  3891. #else
  3892. #define EC_FREE_ALL(list, heap)
  3893. #define EC_GET_SIZE(list) 0
  3894. #define EC_WRITE(a, b) 0
  3895. #define EC_PARSE(a, b, c, d) 0
  3896. #define EC_VALIDATE_REQUEST(a, b)
  3897. #define PF_FREE_ALL(list, heap)
  3898. #define PF_GET_SIZE(list) 0
  3899. #define PF_WRITE(a, b) 0
  3900. #define PF_PARSE(a, b, c, d) 0
  3901. #define PF_VALIDATE_REQUEST(a, b)
  3902. #define PF_VALIDATE_RESPONSE(a, b)
  3903. #endif /* HAVE_SUPPORTED_CURVES */
  3904. /******************************************************************************/
  3905. /* Renegotiation Indication */
  3906. /******************************************************************************/
  3907. #if defined(HAVE_SECURE_RENEGOTIATION) \
  3908. || defined(HAVE_SERVER_RENEGOTIATION_INFO)
  3909. static byte TLSX_SecureRenegotiation_GetSize(SecureRenegotiation* data,
  3910. int isRequest)
  3911. {
  3912. byte length = OPAQUE8_LEN; /* empty info length */
  3913. /* data will be NULL for HAVE_SERVER_RENEGOTIATION_INFO only */
  3914. if (data && data->enabled && data->verifySet) {
  3915. /* client sends client_verify_data only */
  3916. length += TLS_FINISHED_SZ;
  3917. /* server also sends server_verify_data */
  3918. if (!isRequest)
  3919. length += TLS_FINISHED_SZ;
  3920. }
  3921. return length;
  3922. }
  3923. static word16 TLSX_SecureRenegotiation_Write(SecureRenegotiation* data,
  3924. byte* output, int isRequest)
  3925. {
  3926. word16 offset = OPAQUE8_LEN; /* RenegotiationInfo length */
  3927. if (data && data->enabled && data->verifySet) {
  3928. /* client sends client_verify_data only */
  3929. XMEMCPY(output + offset, data->client_verify_data, TLS_FINISHED_SZ);
  3930. offset += TLS_FINISHED_SZ;
  3931. /* server also sends server_verify_data */
  3932. if (!isRequest) {
  3933. XMEMCPY(output + offset, data->server_verify_data, TLS_FINISHED_SZ);
  3934. offset += TLS_FINISHED_SZ;
  3935. }
  3936. }
  3937. output[0] = (byte)(offset - 1); /* info length - self */
  3938. return offset;
  3939. }
  3940. static int TLSX_SecureRenegotiation_Parse(WOLFSSL* ssl, byte* input,
  3941. word16 length, byte isRequest)
  3942. {
  3943. int ret = SECURE_RENEGOTIATION_E;
  3944. if (length >= OPAQUE8_LEN) {
  3945. if (isRequest) {
  3946. #ifndef NO_WOLFSSL_SERVER
  3947. if (ssl->secure_renegotiation == NULL) {
  3948. ret = wolfSSL_UseSecureRenegotiation(ssl);
  3949. if (ret == WOLFSSL_SUCCESS)
  3950. ret = 0;
  3951. }
  3952. if (ret != 0 && ret != SECURE_RENEGOTIATION_E) {
  3953. }
  3954. else if (!ssl->secure_renegotiation->enabled) {
  3955. if (*input == 0) {
  3956. input++; /* get past size */
  3957. ssl->secure_renegotiation->enabled = 1;
  3958. TLSX_SetResponse(ssl, TLSX_RENEGOTIATION_INFO);
  3959. ret = 0;
  3960. }
  3961. else {
  3962. /* already in error state */
  3963. WOLFSSL_MSG("SCR client verify data present");
  3964. }
  3965. }
  3966. else if (*input == TLS_FINISHED_SZ) {
  3967. if (length < TLS_FINISHED_SZ + 1) {
  3968. WOLFSSL_MSG("SCR malformed buffer");
  3969. ret = BUFFER_E;
  3970. }
  3971. else {
  3972. input++; /* get past size */
  3973. /* validate client verify data */
  3974. if (XMEMCMP(input,
  3975. ssl->secure_renegotiation->client_verify_data,
  3976. TLS_FINISHED_SZ) == 0) {
  3977. WOLFSSL_MSG("SCR client verify data match");
  3978. TLSX_SetResponse(ssl, TLSX_RENEGOTIATION_INFO);
  3979. ret = 0; /* verified */
  3980. } else {
  3981. /* already in error state */
  3982. WOLFSSL_MSG("SCR client verify data Failure");
  3983. }
  3984. }
  3985. }
  3986. #endif
  3987. }
  3988. else if (ssl->secure_renegotiation != NULL) {
  3989. #ifndef NO_WOLFSSL_CLIENT
  3990. if (!ssl->secure_renegotiation->enabled) {
  3991. if (*input == 0) {
  3992. ssl->secure_renegotiation->enabled = 1;
  3993. ret = 0;
  3994. }
  3995. }
  3996. else if (*input == 2 * TLS_FINISHED_SZ &&
  3997. length == 2 * TLS_FINISHED_SZ + OPAQUE8_LEN) {
  3998. input++; /* get past size */
  3999. /* validate client and server verify data */
  4000. if (XMEMCMP(input,
  4001. ssl->secure_renegotiation->client_verify_data,
  4002. TLS_FINISHED_SZ) == 0 &&
  4003. XMEMCMP(input + TLS_FINISHED_SZ,
  4004. ssl->secure_renegotiation->server_verify_data,
  4005. TLS_FINISHED_SZ) == 0) {
  4006. WOLFSSL_MSG("SCR client and server verify data match");
  4007. ret = 0; /* verified */
  4008. } else {
  4009. /* already in error state */
  4010. WOLFSSL_MSG("SCR client and server verify data Failure");
  4011. }
  4012. }
  4013. #endif
  4014. }
  4015. }
  4016. if (ret != 0) {
  4017. SendAlert(ssl, alert_fatal, handshake_failure);
  4018. }
  4019. return ret;
  4020. }
  4021. int TLSX_UseSecureRenegotiation(TLSX** extensions, void* heap)
  4022. {
  4023. int ret = 0;
  4024. SecureRenegotiation* data;
  4025. data = (SecureRenegotiation*)XMALLOC(sizeof(SecureRenegotiation), heap,
  4026. DYNAMIC_TYPE_TLSX);
  4027. if (data == NULL)
  4028. return MEMORY_E;
  4029. XMEMSET(data, 0, sizeof(SecureRenegotiation));
  4030. ret = TLSX_Push(extensions, TLSX_RENEGOTIATION_INFO, data, heap);
  4031. if (ret != 0) {
  4032. XFREE(data, heap, DYNAMIC_TYPE_TLSX);
  4033. return ret;
  4034. }
  4035. return WOLFSSL_SUCCESS;
  4036. }
  4037. #ifdef HAVE_SERVER_RENEGOTIATION_INFO
  4038. int TLSX_AddEmptyRenegotiationInfo(TLSX** extensions, void* heap)
  4039. {
  4040. int ret;
  4041. /* send empty renegotiation_info extension */
  4042. TLSX* ext = TLSX_Find(*extensions, TLSX_RENEGOTIATION_INFO);
  4043. if (ext == NULL) {
  4044. ret = TLSX_UseSecureRenegotiation(extensions, heap);
  4045. if (ret != WOLFSSL_SUCCESS)
  4046. return ret;
  4047. ext = TLSX_Find(*extensions, TLSX_RENEGOTIATION_INFO);
  4048. }
  4049. if (ext)
  4050. ext->resp = 1;
  4051. return WOLFSSL_SUCCESS;
  4052. }
  4053. #endif /* HAVE_SERVER_RENEGOTIATION_INFO */
  4054. #define SCR_FREE_ALL(data, heap) XFREE(data, (heap), DYNAMIC_TYPE_TLSX)
  4055. #define SCR_GET_SIZE TLSX_SecureRenegotiation_GetSize
  4056. #define SCR_WRITE TLSX_SecureRenegotiation_Write
  4057. #define SCR_PARSE TLSX_SecureRenegotiation_Parse
  4058. #else
  4059. #define SCR_FREE_ALL(a, heap)
  4060. #define SCR_GET_SIZE(a, b) 0
  4061. #define SCR_WRITE(a, b, c) 0
  4062. #define SCR_PARSE(a, b, c, d) 0
  4063. #endif /* HAVE_SECURE_RENEGOTIATION */
  4064. /******************************************************************************/
  4065. /* Session Tickets */
  4066. /******************************************************************************/
  4067. #ifdef HAVE_SESSION_TICKET
  4068. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
  4069. static void TLSX_SessionTicket_ValidateRequest(WOLFSSL* ssl)
  4070. {
  4071. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_SESSION_TICKET);
  4072. SessionTicket* ticket = extension ?
  4073. (SessionTicket*)extension->data : NULL;
  4074. if (ticket) {
  4075. /* TODO validate ticket timeout here! */
  4076. if (ticket->lifetime == 0xfffffff) {
  4077. /* send empty ticket on timeout */
  4078. TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
  4079. }
  4080. }
  4081. }
  4082. #endif /* WLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
  4083. static word16 TLSX_SessionTicket_GetSize(SessionTicket* ticket, int isRequest)
  4084. {
  4085. (void)isRequest;
  4086. return ticket ? ticket->size : 0;
  4087. }
  4088. static word16 TLSX_SessionTicket_Write(SessionTicket* ticket, byte* output,
  4089. int isRequest)
  4090. {
  4091. word16 offset = 0; /* empty ticket */
  4092. if (isRequest && ticket) {
  4093. XMEMCPY(output + offset, ticket->data, ticket->size);
  4094. offset += ticket->size;
  4095. }
  4096. return offset;
  4097. }
  4098. static int TLSX_SessionTicket_Parse(WOLFSSL* ssl, byte* input, word16 length,
  4099. byte isRequest)
  4100. {
  4101. int ret = 0;
  4102. (void) input; /* avoid unused parameter if NO_WOLFSSL_SERVER defined */
  4103. if (!isRequest) {
  4104. if (TLSX_CheckUnsupportedExtension(ssl, TLSX_SESSION_TICKET))
  4105. return TLSX_HandleUnsupportedExtension(ssl);
  4106. if (length != 0)
  4107. return BUFFER_ERROR;
  4108. #ifndef NO_WOLFSSL_CLIENT
  4109. ssl->expect_session_ticket = 1;
  4110. #endif
  4111. }
  4112. #ifndef NO_WOLFSSL_SERVER
  4113. else {
  4114. /* server side */
  4115. if (ssl->ctx->ticketEncCb == NULL) {
  4116. WOLFSSL_MSG("Client sent session ticket, server has no callback");
  4117. return 0;
  4118. }
  4119. if (length == 0) {
  4120. /* blank ticket */
  4121. ret = TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
  4122. if (ret == WOLFSSL_SUCCESS) {
  4123. ret = 0;
  4124. TLSX_SetResponse(ssl, TLSX_SESSION_TICKET); /* send blank ticket */
  4125. ssl->options.createTicket = 1; /* will send ticket msg */
  4126. ssl->options.useTicket = 1;
  4127. ssl->options.resuming = 0; /* no standard resumption */
  4128. ssl->arrays->sessionIDSz = 0; /* no echo on blank ticket */
  4129. }
  4130. } else {
  4131. /* got actual ticket from client */
  4132. ret = DoClientTicket(ssl, input, length);
  4133. if (ret == WOLFSSL_TICKET_RET_OK) { /* use ticket to resume */
  4134. WOLFSSL_MSG("Using existing client ticket");
  4135. ssl->options.useTicket = 1;
  4136. ssl->options.resuming = 1;
  4137. } else if (ret == WOLFSSL_TICKET_RET_CREATE) {
  4138. WOLFSSL_MSG("Using existing client ticket, creating new one");
  4139. ret = TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
  4140. if (ret == WOLFSSL_SUCCESS) {
  4141. ret = 0;
  4142. TLSX_SetResponse(ssl, TLSX_SESSION_TICKET);
  4143. /* send blank ticket */
  4144. ssl->options.createTicket = 1; /* will send ticket msg */
  4145. ssl->options.useTicket = 1;
  4146. ssl->options.resuming = 1;
  4147. }
  4148. } else if (ret == WOLFSSL_TICKET_RET_REJECT) {
  4149. WOLFSSL_MSG("Process client ticket rejected, not using");
  4150. ssl->options.rejectTicket = 1;
  4151. ret = 0; /* not fatal */
  4152. } else if (ret == WOLFSSL_TICKET_RET_FATAL || ret < 0) {
  4153. WOLFSSL_MSG("Process client ticket fatal error, not using");
  4154. }
  4155. }
  4156. }
  4157. #endif /* NO_WOLFSSL_SERVER */
  4158. return ret;
  4159. }
  4160. WOLFSSL_LOCAL SessionTicket* TLSX_SessionTicket_Create(word32 lifetime,
  4161. byte* data, word16 size, void* heap)
  4162. {
  4163. SessionTicket* ticket = (SessionTicket*)XMALLOC(sizeof(SessionTicket),
  4164. heap, DYNAMIC_TYPE_TLSX);
  4165. if (ticket) {
  4166. ticket->data = (byte*)XMALLOC(size, heap, DYNAMIC_TYPE_TLSX);
  4167. if (ticket->data == NULL) {
  4168. XFREE(ticket, heap, DYNAMIC_TYPE_TLSX);
  4169. return NULL;
  4170. }
  4171. XMEMCPY(ticket->data, data, size);
  4172. ticket->size = size;
  4173. ticket->lifetime = lifetime;
  4174. }
  4175. (void)heap;
  4176. return ticket;
  4177. }
  4178. WOLFSSL_LOCAL void TLSX_SessionTicket_Free(SessionTicket* ticket, void* heap)
  4179. {
  4180. if (ticket) {
  4181. XFREE(ticket->data, heap, DYNAMIC_TYPE_TLSX);
  4182. XFREE(ticket, heap, DYNAMIC_TYPE_TLSX);
  4183. }
  4184. (void)heap;
  4185. }
  4186. int TLSX_UseSessionTicket(TLSX** extensions, SessionTicket* ticket, void* heap)
  4187. {
  4188. int ret = 0;
  4189. if (extensions == NULL)
  4190. return BAD_FUNC_ARG;
  4191. /* If the ticket is NULL, the client will request a new ticket from the
  4192. server. Otherwise, the client will use it in the next client hello. */
  4193. if ((ret = TLSX_Push(extensions, TLSX_SESSION_TICKET, (void*)ticket, heap))
  4194. != 0)
  4195. return ret;
  4196. return WOLFSSL_SUCCESS;
  4197. }
  4198. #define WOLF_STK_VALIDATE_REQUEST TLSX_SessionTicket_ValidateRequest
  4199. #define WOLF_STK_GET_SIZE TLSX_SessionTicket_GetSize
  4200. #define WOLF_STK_WRITE TLSX_SessionTicket_Write
  4201. #define WOLF_STK_PARSE TLSX_SessionTicket_Parse
  4202. #define WOLF_STK_FREE(stk, heap) TLSX_SessionTicket_Free((SessionTicket*)stk,(heap))
  4203. #else
  4204. #define WOLF_STK_FREE(a, b)
  4205. #define WOLF_STK_VALIDATE_REQUEST(a)
  4206. #define WOLF_STK_GET_SIZE(a, b) 0
  4207. #define WOLF_STK_WRITE(a, b, c) 0
  4208. #define WOLF_STK_PARSE(a, b, c, d) 0
  4209. #endif /* HAVE_SESSION_TICKET */
  4210. /******************************************************************************/
  4211. /* Quantum-Safe-Hybrid */
  4212. /******************************************************************************/
  4213. #ifdef HAVE_QSH
  4214. #if defined(HAVE_NTRU)
  4215. static WC_RNG* gRng;
  4216. static wolfSSL_Mutex* gRngMutex;
  4217. #endif
  4218. static void TLSX_QSH_FreeAll(QSHScheme* list, void* heap)
  4219. {
  4220. QSHScheme* current;
  4221. while ((current = list)) {
  4222. list = current->next;
  4223. XFREE(current, heap, DYNAMIC_TYPE_TLSX);
  4224. }
  4225. (void)heap;
  4226. }
  4227. static int TLSX_QSH_Append(QSHScheme** list, word16 name, byte* pub,
  4228. word16 pubLen)
  4229. {
  4230. QSHScheme* temp;
  4231. if (list == NULL)
  4232. return BAD_FUNC_ARG;
  4233. if ((temp = (QSHScheme*)XMALLOC(sizeof(QSHScheme), NULL,
  4234. DYNAMIC_TYPE_TLSX)) == NULL)
  4235. return MEMORY_E;
  4236. temp->name = name;
  4237. temp->PK = pub;
  4238. temp->PKLen = pubLen;
  4239. temp->next = *list;
  4240. *list = temp;
  4241. return 0;
  4242. }
  4243. /* request for server's public key : 02 indicates 0-2 requested */
  4244. static byte TLSX_QSH_SerPKReq(byte* output, byte isRequest)
  4245. {
  4246. if (isRequest) {
  4247. /* only request one public key from the server */
  4248. output[0] = 0x01;
  4249. return OPAQUE8_LEN;
  4250. }
  4251. else {
  4252. return 0;
  4253. }
  4254. }
  4255. #ifndef NO_WOLFSSL_CLIENT
  4256. /* check for TLS_QSH suite */
  4257. static void TLSX_QSH_ValidateRequest(WOLFSSL* ssl, byte* semaphore)
  4258. {
  4259. int i;
  4260. for (i = 0; i < ssl->suites->suiteSz; i+= 2)
  4261. if (ssl->suites->suites[i] == QSH_BYTE)
  4262. return;
  4263. /* No QSH suite found */
  4264. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_QUANTUM_SAFE_HYBRID));
  4265. }
  4266. /* return the size of the QSH hello extension
  4267. list the list of QSHScheme structs containing id and key
  4268. isRequest if 1 then is being sent to the server
  4269. */
  4270. word16 TLSX_QSH_GetSize(QSHScheme* list, byte isRequest)
  4271. {
  4272. QSHScheme* temp = list;
  4273. word16 length = 0;
  4274. /* account for size of scheme list and public key list */
  4275. if (isRequest)
  4276. length = OPAQUE16_LEN;
  4277. length += OPAQUE24_LEN;
  4278. /* for each non null element in list add size */
  4279. while ((temp)) {
  4280. /* add public key info Scheme | Key Length | Key */
  4281. length += OPAQUE16_LEN;
  4282. length += OPAQUE16_LEN;
  4283. length += temp->PKLen;
  4284. /* if client add name size for scheme list
  4285. advance to next QSHScheme struct in list */
  4286. if (isRequest)
  4287. length += OPAQUE16_LEN;
  4288. temp = temp->next;
  4289. }
  4290. /* add length for request server public keys */
  4291. if (isRequest)
  4292. length += OPAQUE8_LEN;
  4293. return length;
  4294. }
  4295. /* write out a list of QSHScheme IDs */
  4296. static word16 TLSX_QSH_Write(QSHScheme* list, byte* output)
  4297. {
  4298. QSHScheme* current = list;
  4299. word16 length = 0;
  4300. length += OPAQUE16_LEN;
  4301. while (current) {
  4302. c16toa(current->name, output + length);
  4303. length += OPAQUE16_LEN;
  4304. current = (QSHScheme*)current->next;
  4305. }
  4306. c16toa(length - OPAQUE16_LEN, output); /* writing list length */
  4307. return length;
  4308. }
  4309. /* write public key list in extension */
  4310. static word16 TLSX_QSHPK_WriteR(QSHScheme* format, byte* output)
  4311. {
  4312. word32 offset = 0;
  4313. word16 public_len = 0;
  4314. if (!format)
  4315. return offset;
  4316. /* write scheme ID */
  4317. c16toa(format->name, output + offset);
  4318. offset += OPAQUE16_LEN;
  4319. /* write public key matching scheme */
  4320. public_len = format->PKLen;
  4321. c16toa(public_len, output + offset);
  4322. offset += OPAQUE16_LEN;
  4323. if (format->PK) {
  4324. XMEMCPY(output+offset, format->PK, public_len);
  4325. }
  4326. return public_len + offset;
  4327. }
  4328. word16 TLSX_QSHPK_Write(QSHScheme* list, byte* output)
  4329. {
  4330. QSHScheme* current = list;
  4331. word32 length = 0;
  4332. word24 toWire;
  4333. length += OPAQUE24_LEN;
  4334. while (current) {
  4335. length += TLSX_QSHPK_WriteR(current, output + length);
  4336. current = (QSHScheme*)current->next;
  4337. }
  4338. /* length of public keys sent */
  4339. c32to24(length - OPAQUE24_LEN, toWire);
  4340. output[0] = toWire[0];
  4341. output[1] = toWire[1];
  4342. output[2] = toWire[2];
  4343. return length;
  4344. }
  4345. #endif /* NO_WOLFSSL_CLIENT */
  4346. #ifndef NO_WOLFSSL_SERVER
  4347. static void TLSX_QSHAgreement(TLSX** extensions, void* heap)
  4348. {
  4349. TLSX* extension = TLSX_Find(*extensions, TLSX_QUANTUM_SAFE_HYBRID);
  4350. QSHScheme* format = NULL;
  4351. QSHScheme* del = NULL;
  4352. QSHScheme* prev = NULL;
  4353. if (extension == NULL)
  4354. return;
  4355. format = (QSHScheme*)extension->data;
  4356. while (format) {
  4357. if (format->PKLen == 0) {
  4358. /* case of head */
  4359. if (format == extension->data) {
  4360. extension->data = format->next;
  4361. }
  4362. if (prev)
  4363. prev->next = format->next;
  4364. del = format;
  4365. format = format->next;
  4366. XFREE(del, heap, DYNAMIC_TYPE_TMP_BUFFER);
  4367. del = NULL;
  4368. } else {
  4369. prev = format;
  4370. format = format->next;
  4371. }
  4372. }
  4373. (void)heap;
  4374. }
  4375. /* Parse in hello extension
  4376. input the byte stream to process
  4377. length length of total extension found
  4378. isRequest set to 1 if being sent to the server
  4379. */
  4380. static int TLSX_QSH_Parse(WOLFSSL* ssl, byte* input, word16 length,
  4381. byte isRequest)
  4382. {
  4383. byte numKeys = 0;
  4384. word16 offset = 0;
  4385. word16 schemSz = 0;
  4386. word16 offset_len = 0;
  4387. word32 offset_pk = 0;
  4388. word16 name = 0;
  4389. word16 PKLen = 0;
  4390. byte* PK = NULL;
  4391. int r;
  4392. if (OPAQUE16_LEN > length)
  4393. return BUFFER_ERROR;
  4394. if (isRequest) {
  4395. ato16(input, &schemSz);
  4396. /* list of public keys available for QSH schemes */
  4397. offset_len = schemSz + OPAQUE16_LEN;
  4398. }
  4399. offset_pk = ((input[offset_len] << 16) & 0xFF00000) |
  4400. (((input[offset_len + 1]) << 8) & 0xFF00) |
  4401. (input[offset_len + 2] & 0xFF);
  4402. offset_len += OPAQUE24_LEN;
  4403. /* check buffer size */
  4404. if (offset_pk > length)
  4405. return BUFFER_ERROR;
  4406. /* set maximum number of keys the client will accept */
  4407. if (!isRequest)
  4408. numKeys = (ssl->maxRequest < 1)? 1 : ssl->maxRequest;
  4409. /* hello extension read list of scheme ids */
  4410. if (isRequest) {
  4411. /* read in request for public keys */
  4412. ssl->minRequest = (input[length -1] >> 4) & 0xFF;
  4413. ssl->maxRequest = input[length -1] & 0x0F;
  4414. /* choose the min between min requested by client and 1 */
  4415. numKeys = (ssl->minRequest > 1) ? ssl->minRequest : 1;
  4416. if (ssl->minRequest > ssl->maxRequest)
  4417. return BAD_FUNC_ARG;
  4418. offset += OPAQUE16_LEN;
  4419. schemSz += offset;
  4420. /* check buffer size */
  4421. if (schemSz > length)
  4422. return BUFFER_ERROR;
  4423. while ((offset < schemSz) && numKeys) {
  4424. /* Scheme ID list */
  4425. ato16(input + offset, &name);
  4426. offset += OPAQUE16_LEN;
  4427. /* validate we have scheme id */
  4428. if (ssl->user_set_QSHSchemes &&
  4429. !TLSX_ValidateQSHScheme(&ssl->extensions, name)) {
  4430. continue;
  4431. }
  4432. /* server create keys on demand */
  4433. if ((r = TLSX_CreateNtruKey(ssl, name)) != 0) {
  4434. WOLFSSL_MSG("Error creating ntru keys");
  4435. return r;
  4436. }
  4437. /* peer sent an agreed upon scheme */
  4438. r = TLSX_UseQSHScheme(&ssl->extensions, name, NULL, 0, ssl->heap);
  4439. if (r != WOLFSSL_SUCCESS) return r; /* throw error */
  4440. numKeys--;
  4441. }
  4442. /* choose the min between min requested by client and 1 */
  4443. numKeys = (ssl->minRequest > 1) ? ssl->minRequest : 1;
  4444. }
  4445. /* QSHPK struct */
  4446. offset_pk += offset_len;
  4447. while ((offset_len < offset_pk) && numKeys) {
  4448. QSHKey * temp;
  4449. if ((temp = (QSHKey*)XMALLOC(sizeof(QSHKey), ssl->heap,
  4450. DYNAMIC_TYPE_TLSX)) == NULL)
  4451. return MEMORY_E;
  4452. /* initialize */
  4453. temp->next = NULL;
  4454. temp->pub.buffer = NULL;
  4455. temp->pub.length = 0;
  4456. temp->pri.buffer = NULL;
  4457. temp->pri.length = 0;
  4458. /* scheme id */
  4459. ato16(input + offset_len, &(temp->name));
  4460. offset_len += OPAQUE16_LEN;
  4461. /* public key length */
  4462. ato16(input + offset_len, &PKLen);
  4463. temp->pub.length = PKLen;
  4464. offset_len += OPAQUE16_LEN;
  4465. if (isRequest) {
  4466. /* validate we have scheme id */
  4467. if (ssl->user_set_QSHSchemes &&
  4468. (!TLSX_ValidateQSHScheme(&ssl->extensions, temp->name))) {
  4469. offset_len += PKLen;
  4470. XFREE(temp, ssl->heap, DYNAMIC_TYPE_TLSX);
  4471. continue;
  4472. }
  4473. }
  4474. /* read in public key */
  4475. if (PKLen > 0) {
  4476. temp->pub.buffer = (byte*)XMALLOC(temp->pub.length,
  4477. ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  4478. XMEMCPY(temp->pub.buffer, input + offset_len, temp->pub.length);
  4479. offset_len += PKLen;
  4480. }
  4481. else {
  4482. PK = NULL;
  4483. }
  4484. /* use own key when adding to extensions list for sending reply */
  4485. PKLen = 0;
  4486. PK = TLSX_QSHKeyFind_Pub(ssl->QSH_Key, &PKLen, temp->name);
  4487. r = TLSX_UseQSHScheme(&ssl->extensions, temp->name, PK, PKLen,
  4488. ssl->heap);
  4489. /* store peers key */
  4490. ssl->peerQSHKeyPresent = 1;
  4491. if (TLSX_AddQSHKey(&ssl->peerQSHKey, temp) != 0)
  4492. return MEMORY_E;
  4493. if (temp->pub.length == 0) {
  4494. XFREE(temp, ssl->heap, DYNAMIC_TYPE_TLSX);
  4495. }
  4496. if (r != WOLFSSL_SUCCESS) {return r;} /* throw error */
  4497. numKeys--;
  4498. }
  4499. /* reply to a QSH extension sent from client */
  4500. if (isRequest) {
  4501. TLSX_SetResponse(ssl, TLSX_QUANTUM_SAFE_HYBRID);
  4502. /* only use schemes we have key generated for -- free the rest */
  4503. TLSX_QSHAgreement(&ssl->extensions, ssl->heap);
  4504. }
  4505. return 0;
  4506. }
  4507. /* Used for parsing in QSHCipher structs on Key Exchange */
  4508. int TLSX_QSHCipher_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  4509. byte isServer)
  4510. {
  4511. QSHKey* key;
  4512. word16 Max_Secret_Len = 48;
  4513. word16 offset = 0;
  4514. word16 offset_len = 0;
  4515. word32 offset_pk = 0;
  4516. word16 name = 0;
  4517. word16 secretLen = 0;
  4518. byte* secret = NULL;
  4519. word16 buffLen = 0;
  4520. byte buff[145]; /* size enough for 3 secrets */
  4521. buffer* buf;
  4522. /* pointer to location where secret should be stored */
  4523. if (isServer) {
  4524. buf = ssl->QSH_secret->CliSi;
  4525. }
  4526. else {
  4527. buf = ssl->QSH_secret->SerSi;
  4528. }
  4529. offset_pk = ((input[offset_len] << 16) & 0xFF0000) |
  4530. (((input[offset_len + 1]) << 8) & 0xFF00) |
  4531. (input[offset_len + 2] & 0xFF);
  4532. offset_len += OPAQUE24_LEN;
  4533. /* validating extension list length -- check if trying to read over edge
  4534. of buffer */
  4535. if (length < (offset_pk + OPAQUE24_LEN)) {
  4536. return BUFFER_ERROR;
  4537. }
  4538. /* QSHCipherList struct */
  4539. offset_pk += offset_len;
  4540. while (offset_len < offset_pk) {
  4541. /* scheme id */
  4542. ato16(input + offset_len, &name);
  4543. offset_len += OPAQUE16_LEN;
  4544. /* public key length */
  4545. ato16(input + offset_len, &secretLen);
  4546. offset_len += OPAQUE16_LEN;
  4547. /* read in public key */
  4548. if (secretLen > 0) {
  4549. secret = (byte*)(input + offset_len);
  4550. offset_len += secretLen;
  4551. }
  4552. else {
  4553. secret = NULL;
  4554. }
  4555. /* no secret sent */
  4556. if (secret == NULL)
  4557. continue;
  4558. /* find corresponding key */
  4559. key = ssl->QSH_Key;
  4560. while (key) {
  4561. if (key->name == name)
  4562. break;
  4563. else
  4564. key = (QSHKey*)key->next;
  4565. }
  4566. /* if we do not have the key than there was a big issue negotiation */
  4567. if (key == NULL) {
  4568. WOLFSSL_MSG("key was null for decryption!!!\n");
  4569. return MEMORY_E;
  4570. }
  4571. /* Decrypt sent secret */
  4572. buffLen = Max_Secret_Len;
  4573. QSH_Decrypt(key, secret, secretLen, buff + offset, &buffLen);
  4574. offset += buffLen;
  4575. }
  4576. /* allocate memory for buffer */
  4577. buf->length = offset;
  4578. buf->buffer = (byte*)XMALLOC(offset, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  4579. if (buf->buffer == NULL)
  4580. return MEMORY_E;
  4581. /* store secrets */
  4582. XMEMCPY(buf->buffer, buff, offset);
  4583. ForceZero(buff, offset);
  4584. return offset_len;
  4585. }
  4586. /* return 1 on success */
  4587. int TLSX_ValidateQSHScheme(TLSX** extensions, word16 theirs) {
  4588. TLSX* extension = TLSX_Find(*extensions, TLSX_QUANTUM_SAFE_HYBRID);
  4589. QSHScheme* format = NULL;
  4590. /* if no extension is sent then do not use QSH */
  4591. if (!extension) {
  4592. WOLFSSL_MSG("No QSH Extension");
  4593. return 0;
  4594. }
  4595. for (format = (QSHScheme*)extension->data; format; format = format->next) {
  4596. if (format->name == theirs) {
  4597. WOLFSSL_MSG("Found Matching QSH Scheme");
  4598. return 1; /* have QSH */
  4599. }
  4600. }
  4601. return 0;
  4602. }
  4603. #endif /* NO_WOLFSSL_SERVER */
  4604. /* test if the QSH Scheme is implemented
  4605. return 1 if yes 0 if no */
  4606. static int TLSX_HaveQSHScheme(word16 name)
  4607. {
  4608. switch(name) {
  4609. #ifdef HAVE_NTRU
  4610. case WOLFSSL_NTRU_EESS439:
  4611. case WOLFSSL_NTRU_EESS593:
  4612. case WOLFSSL_NTRU_EESS743:
  4613. return 1;
  4614. #endif
  4615. case WOLFSSL_LWE_XXX:
  4616. case WOLFSSL_HFE_XXX:
  4617. return 0; /* not supported yet */
  4618. default:
  4619. return 0;
  4620. }
  4621. }
  4622. /* Add a QSHScheme struct to list of usable ones */
  4623. int TLSX_UseQSHScheme(TLSX** extensions, word16 name, byte* pKey, word16 pkeySz,
  4624. void* heap)
  4625. {
  4626. TLSX* extension = NULL;
  4627. QSHScheme* format = NULL;
  4628. int ret = 0;
  4629. /* sanity check */
  4630. if (extensions == NULL || (pKey == NULL && pkeySz != 0))
  4631. return BAD_FUNC_ARG;
  4632. extension = TLSX_Find(*extensions, TLSX_QUANTUM_SAFE_HYBRID);
  4633. /* if scheme is implemented than add */
  4634. if (TLSX_HaveQSHScheme(name)) {
  4635. if ((ret = TLSX_QSH_Append(&format, name, pKey, pkeySz)) != 0)
  4636. return ret;
  4637. extension = TLSX_Find(*extensions, TLSX_QUANTUM_SAFE_HYBRID);
  4638. if (!extension) {
  4639. if ((ret = TLSX_Push(extensions, TLSX_QUANTUM_SAFE_HYBRID, format,
  4640. heap)) != 0) {
  4641. XFREE(format, 0, DYNAMIC_TYPE_TLSX);
  4642. return ret;
  4643. }
  4644. }
  4645. else {
  4646. /* push new QSH object to extension data. */
  4647. format->next = (QSHScheme*)extension->data;
  4648. extension->data = (void*)format;
  4649. /* look for another format of the same name to remove (replacement) */
  4650. do {
  4651. if (format->next && (format->next->name == name)) {
  4652. QSHScheme* next = format->next;
  4653. format->next = next->next;
  4654. XFREE(next, 0, DYNAMIC_TYPE_TLSX);
  4655. break;
  4656. }
  4657. } while ((format = format->next));
  4658. }
  4659. }
  4660. return WOLFSSL_SUCCESS;
  4661. }
  4662. #define QSH_FREE_ALL TLSX_QSH_FreeAll
  4663. #define QSH_VALIDATE_REQUEST TLSX_QSH_ValidateRequest
  4664. #ifndef NO_WOLFSSL_CLIENT
  4665. #define QSH_GET_SIZE TLSX_QSH_GetSize
  4666. #define QSH_WRITE TLSX_QSH_Write
  4667. #else
  4668. #define QSH_GET_SIZE(list, a) 0
  4669. #define QSH_WRITE(a, b) 0
  4670. #endif
  4671. #ifndef NO_WOLFSSL_SERVER
  4672. #define QSH_PARSE TLSX_QSH_Parse
  4673. #else
  4674. #define QSH_PARSE(a, b, c, d) 0
  4675. #endif
  4676. #define QSHPK_WRITE TLSX_QSHPK_Write
  4677. #define QSH_SERREQ TLSX_QSH_SerPKReq
  4678. #else
  4679. #define QSH_FREE_ALL(list, heap)
  4680. #define QSH_GET_SIZE(list, a) 0
  4681. #define QSH_WRITE(a, b) 0
  4682. #define QSH_PARSE(a, b, c, d) 0
  4683. #define QSHPK_WRITE(a, b) 0
  4684. #define QSH_SERREQ(a, b) 0
  4685. #define QSH_VALIDATE_REQUEST(a, b)
  4686. #endif /* HAVE_QSH */
  4687. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  4688. /******************************************************************************/
  4689. /* Encrypt-then-MAC */
  4690. /******************************************************************************/
  4691. #ifndef WOLFSSL_NO_TLS12
  4692. static int TLSX_EncryptThenMac_Use(WOLFSSL* ssl);
  4693. /**
  4694. * Get the size of the Encrypt-Then-MAC extension.
  4695. *
  4696. * msgType Type of message to put extension into.
  4697. * pSz Size of extension data.
  4698. * return SANITY_MSG_E when the message is not allowed to have extension and
  4699. * 0 otherwise.
  4700. */
  4701. static int TLSX_EncryptThenMac_GetSize(byte msgType, word16* pSz)
  4702. {
  4703. (void)pSz;
  4704. if (msgType != client_hello && msgType != server_hello) {
  4705. return SANITY_MSG_E;
  4706. }
  4707. /* Empty extension */
  4708. return 0;
  4709. }
  4710. /**
  4711. * Write the Encrypt-Then-MAC extension.
  4712. *
  4713. * data Unused
  4714. * output Extension data buffer. Unused.
  4715. * msgType Type of message to put extension into.
  4716. * pSz Size of extension data.
  4717. * return SANITY_MSG_E when the message is not allowed to have extension and
  4718. * 0 otherwise.
  4719. */
  4720. static int TLSX_EncryptThenMac_Write(void* data, byte* output, byte msgType,
  4721. word16* pSz)
  4722. {
  4723. (void)data;
  4724. (void)output;
  4725. (void)pSz;
  4726. if (msgType != client_hello && msgType != server_hello) {
  4727. return SANITY_MSG_E;
  4728. }
  4729. /* Empty extension */
  4730. return 0;
  4731. }
  4732. /**
  4733. * Parse the Encrypt-Then-MAC extension.
  4734. *
  4735. * ssl SSL object
  4736. * input Extension data buffer.
  4737. * length Length of this extension's data.
  4738. * msgType Type of message to extension appeared in.
  4739. * return SANITY_MSG_E when the message is not allowed to have extension,
  4740. * BUFFER_ERROR when the extension's data is invalid,
  4741. * MEMORY_E when unable to allocate memory and
  4742. * 0 otherwise.
  4743. */
  4744. static int TLSX_EncryptThenMac_Parse(WOLFSSL* ssl, byte* input, word16 length,
  4745. byte msgType)
  4746. {
  4747. int ret;
  4748. (void)input;
  4749. if (msgType != client_hello && msgType != server_hello) {
  4750. return SANITY_MSG_E;
  4751. }
  4752. /* Empty extension */
  4753. if (length != 0)
  4754. return BUFFER_ERROR;
  4755. if (msgType == client_hello) {
  4756. /* Check the user hasn't disallowed use of Encrypt-Then-Mac. */
  4757. if (!ssl->options.disallowEncThenMac) {
  4758. ssl->options.encThenMac = 1;
  4759. /* Set the extension reply. */
  4760. ret = TLSX_EncryptThenMac_Use(ssl);
  4761. if (ret != 0)
  4762. return ret;
  4763. TLSX_SetResponse(ssl, TLSX_ENCRYPT_THEN_MAC);
  4764. }
  4765. return 0;
  4766. }
  4767. /* Server Hello */
  4768. if (ssl->options.disallowEncThenMac)
  4769. return SANITY_MSG_E;
  4770. ssl->options.encThenMac = 1;
  4771. return 0;
  4772. }
  4773. /**
  4774. * Add the Encrypt-Then-MAC extension to list.
  4775. *
  4776. * ssl SSL object
  4777. * return MEMORY_E when unable to allocate memory and 0 otherwise.
  4778. */
  4779. static int TLSX_EncryptThenMac_Use(WOLFSSL* ssl)
  4780. {
  4781. int ret = 0;
  4782. TLSX* extension;
  4783. /* Find the Encrypt-Then-Mac extension if it exists. */
  4784. extension = TLSX_Find(ssl->extensions, TLSX_ENCRYPT_THEN_MAC);
  4785. if (extension == NULL) {
  4786. /* Push new Encrypt-Then-Mac extension. */
  4787. ret = TLSX_Push(&ssl->extensions, TLSX_ENCRYPT_THEN_MAC, NULL,
  4788. ssl->heap);
  4789. if (ret != 0)
  4790. return ret;
  4791. }
  4792. return 0;
  4793. }
  4794. #define ETM_GET_SIZE TLSX_EncryptThenMac_GetSize
  4795. #define ETM_WRITE TLSX_EncryptThenMac_Write
  4796. #define ETM_PARSE TLSX_EncryptThenMac_Parse
  4797. #else
  4798. #define ETM_GET_SIZE(a, b) 0
  4799. #define ETM_WRITE(a, b, c, d) 0
  4800. #define ETM_PARSE(a, b, c, d) 0
  4801. #endif /* !WOLFSSL_NO_TLS12 */
  4802. #endif /* HAVE_ENCRYPT_THEN_MAC && !WOLFSSL_AEAD_ONLY */
  4803. /******************************************************************************/
  4804. /* Supported Versions */
  4805. /******************************************************************************/
  4806. #ifdef WOLFSSL_TLS13
  4807. /* Return the size of the SupportedVersions extension's data.
  4808. *
  4809. * data The SSL/TLS object.
  4810. * msgType The type of the message this extension is being written into.
  4811. * returns the length of data that will be in the extension.
  4812. */
  4813. static int TLSX_SupportedVersions_GetSize(void* data, byte msgType, word16* pSz)
  4814. {
  4815. WOLFSSL* ssl = (WOLFSSL*)data;
  4816. if (msgType == client_hello) {
  4817. /* TLS v1.2 and TLS v1.3 */
  4818. int cnt = 0;
  4819. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  4820. if ((ssl->options.mask & SSL_OP_NO_TLSv1_3) == 0)
  4821. #endif
  4822. cnt++;
  4823. if (ssl->options.downgrade) {
  4824. #ifndef WOLFSSL_NO_TLS12
  4825. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  4826. if ((ssl->options.mask & SSL_OP_NO_TLSv1_2) == 0)
  4827. #endif
  4828. cnt++;
  4829. #endif
  4830. #ifndef NO_OLD_TLS
  4831. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  4832. if ((ssl->options.mask & SSL_OP_NO_TLSv1_1) == 0)
  4833. #endif
  4834. cnt++;
  4835. #ifdef WOLFSSL_ALLOW_TLSV10
  4836. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  4837. if ((ssl->options.mask & SSL_OP_NO_TLSv1) == 0)
  4838. #endif
  4839. cnt++;
  4840. #endif
  4841. #endif
  4842. }
  4843. *pSz += (word16)(OPAQUE8_LEN + cnt * OPAQUE16_LEN);
  4844. }
  4845. else if (msgType == server_hello || msgType == hello_retry_request)
  4846. *pSz += OPAQUE16_LEN;
  4847. else
  4848. return SANITY_MSG_E;
  4849. return 0;
  4850. }
  4851. /* Writes the SupportedVersions extension into the buffer.
  4852. *
  4853. * data The SSL/TLS object.
  4854. * output The buffer to write the extension into.
  4855. * msgType The type of the message this extension is being written into.
  4856. * returns the length of data that was written.
  4857. */
  4858. static int TLSX_SupportedVersions_Write(void* data, byte* output,
  4859. byte msgType, word16* pSz)
  4860. {
  4861. WOLFSSL* ssl = (WOLFSSL*)data;
  4862. byte major;
  4863. byte* cnt;
  4864. if (msgType == client_hello) {
  4865. major = ssl->ctx->method->version.major;
  4866. cnt = output++;
  4867. *cnt = 0;
  4868. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  4869. if ((ssl->options.mask & SSL_OP_NO_TLSv1_3) == 0)
  4870. #endif
  4871. {
  4872. *cnt += OPAQUE16_LEN;
  4873. #ifdef WOLFSSL_TLS13_DRAFT
  4874. /* The TLS draft major number. */
  4875. *(output++) = TLS_DRAFT_MAJOR;
  4876. /* Version of draft supported. */
  4877. *(output++) = TLS_DRAFT_MINOR;
  4878. #else
  4879. *(output++) = major;
  4880. *(output++) = (byte)TLSv1_3_MINOR;
  4881. #endif
  4882. }
  4883. if (ssl->options.downgrade) {
  4884. #ifndef WOLFSSL_NO_TLS12
  4885. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  4886. if ((ssl->options.mask & SSL_OP_NO_TLSv1_2) == 0)
  4887. #endif
  4888. {
  4889. *cnt += OPAQUE16_LEN;
  4890. *(output++) = major;
  4891. *(output++) = (byte)TLSv1_2_MINOR;
  4892. }
  4893. #endif
  4894. #ifndef NO_OLD_TLS
  4895. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  4896. if ((ssl->options.mask & SSL_OP_NO_TLSv1_1) == 0)
  4897. #endif
  4898. {
  4899. *cnt += OPAQUE16_LEN;
  4900. *(output++) = major;
  4901. *(output++) = (byte)TLSv1_1_MINOR;
  4902. }
  4903. #ifdef WOLFSSL_ALLOW_TLSV10
  4904. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  4905. if ((ssl->options.mask & SSL_OP_NO_TLSv1) == 0)
  4906. #endif
  4907. {
  4908. *cnt += OPAQUE16_LEN;
  4909. *(output++) = major;
  4910. *(output++) = (byte)TLSv1_MINOR;
  4911. }
  4912. #endif
  4913. #endif
  4914. }
  4915. *pSz += (word16)(OPAQUE8_LEN + *cnt);
  4916. }
  4917. else if (msgType == server_hello || msgType == hello_retry_request) {
  4918. output[0] = ssl->version.major;
  4919. output[1] = ssl->version.minor;
  4920. *pSz += OPAQUE16_LEN;
  4921. }
  4922. else
  4923. return SANITY_MSG_E;
  4924. return 0;
  4925. }
  4926. /* Parse the SupportedVersions extension.
  4927. *
  4928. * ssl The SSL/TLS object.
  4929. * input The buffer with the extension data.
  4930. * length The length of the extension data.
  4931. * msgType The type of the message this extension is being parsed from.
  4932. * returns 0 on success, otherwise failure.
  4933. */
  4934. static int TLSX_SupportedVersions_Parse(WOLFSSL* ssl, byte* input,
  4935. word16 length, byte msgType)
  4936. {
  4937. ProtocolVersion pv = ssl->ctx->method->version;
  4938. int i;
  4939. int len;
  4940. byte major, minor;
  4941. int newMinor = 0;
  4942. int set = 0;
  4943. int ret;
  4944. if (msgType == client_hello) {
  4945. /* Must contain a length and at least one version. */
  4946. if (length < OPAQUE8_LEN + OPAQUE16_LEN || (length & 1) != 1)
  4947. return BUFFER_ERROR;
  4948. len = *input;
  4949. /* Protocol version array must fill rest of data. */
  4950. if (length != (word16)OPAQUE8_LEN + len)
  4951. return BUFFER_ERROR;
  4952. input++;
  4953. /* Find first match. */
  4954. for (i = 0; i < len; i += OPAQUE16_LEN) {
  4955. major = input[i];
  4956. minor = input[i + OPAQUE8_LEN];
  4957. #ifdef WOLFSSL_TLS13_DRAFT
  4958. if (major == TLS_DRAFT_MAJOR && minor == TLS_DRAFT_MINOR) {
  4959. major = SSLv3_MAJOR;
  4960. minor = TLSv1_3_MINOR;
  4961. }
  4962. #else
  4963. if (major == TLS_DRAFT_MAJOR)
  4964. continue;
  4965. #endif
  4966. if (major != pv.major)
  4967. continue;
  4968. /* No upgrade allowed. */
  4969. if (minor > ssl->version.minor)
  4970. continue;
  4971. /* Check downgrade. */
  4972. if (minor < ssl->version.minor) {
  4973. if (!ssl->options.downgrade)
  4974. continue;
  4975. if (minor < ssl->options.minDowngrade)
  4976. continue;
  4977. if (newMinor == 0 && minor > ssl->options.oldMinor) {
  4978. /* Downgrade the version. */
  4979. ssl->version.minor = minor;
  4980. }
  4981. }
  4982. if (minor >= TLSv1_3_MINOR) {
  4983. if (!ssl->options.tls1_3) {
  4984. ssl->options.tls1_3 = 1;
  4985. ret = TLSX_Prepend(&ssl->extensions,
  4986. TLSX_SUPPORTED_VERSIONS, ssl, ssl->heap);
  4987. if (ret != 0) {
  4988. return ret;
  4989. }
  4990. TLSX_SetResponse(ssl, TLSX_SUPPORTED_VERSIONS);
  4991. }
  4992. if (minor > newMinor) {
  4993. ssl->version.minor = minor;
  4994. newMinor = minor;
  4995. }
  4996. }
  4997. else if (minor > ssl->options.oldMinor)
  4998. ssl->options.oldMinor = minor;
  4999. set = 1;
  5000. }
  5001. if (!set) {
  5002. #ifdef WOLFSSL_MYSQL_COMPATIBLE
  5003. SendAlert(ssl, alert_fatal, wc_protocol_version);
  5004. #else
  5005. SendAlert(ssl, alert_fatal, protocol_version);
  5006. #endif
  5007. return VERSION_ERROR;
  5008. }
  5009. }
  5010. else if (msgType == server_hello || msgType == hello_retry_request) {
  5011. /* Must contain one version. */
  5012. if (length != OPAQUE16_LEN)
  5013. return BUFFER_ERROR;
  5014. major = input[0];
  5015. minor = input[OPAQUE8_LEN];
  5016. if (major != pv.major)
  5017. return VERSION_ERROR;
  5018. /* Can't downgrade with this extension below TLS v1.3. */
  5019. if (minor < TLSv1_3_MINOR)
  5020. return VERSION_ERROR;
  5021. /* Version is TLS v1.2 to handle downgrading from TLS v1.3+. */
  5022. if (ssl->options.downgrade && ssl->version.minor == TLSv1_2_MINOR) {
  5023. /* Set minor version back to TLS v1.3+ */
  5024. ssl->version.minor = ssl->ctx->method->version.minor;
  5025. }
  5026. /* No upgrade allowed. */
  5027. if (ssl->version.minor < minor)
  5028. return VERSION_ERROR;
  5029. /* Check downgrade. */
  5030. if (ssl->version.minor > minor) {
  5031. if (!ssl->options.downgrade)
  5032. return VERSION_ERROR;
  5033. if (minor < ssl->options.minDowngrade)
  5034. return VERSION_ERROR;
  5035. /* Downgrade the version. */
  5036. ssl->version.minor = minor;
  5037. }
  5038. }
  5039. else
  5040. return SANITY_MSG_E;
  5041. return 0;
  5042. }
  5043. /* Sets a new SupportedVersions extension into the extension list.
  5044. *
  5045. * extensions The list of extensions.
  5046. * data The extensions specific data.
  5047. * heap The heap used for allocation.
  5048. * returns 0 on success, otherwise failure.
  5049. */
  5050. static int TLSX_SetSupportedVersions(TLSX** extensions, const void* data,
  5051. void* heap)
  5052. {
  5053. if (extensions == NULL || data == NULL)
  5054. return BAD_FUNC_ARG;
  5055. return TLSX_Push(extensions, TLSX_SUPPORTED_VERSIONS, (void *)data, heap);
  5056. }
  5057. #define SV_GET_SIZE TLSX_SupportedVersions_GetSize
  5058. #define SV_WRITE TLSX_SupportedVersions_Write
  5059. #define SV_PARSE TLSX_SupportedVersions_Parse
  5060. #else
  5061. #define SV_GET_SIZE(a, b, c) 0
  5062. #define SV_WRITE(a, b, c, d) 0
  5063. #define SV_PARSE(a, b, c, d) 0
  5064. #endif /* WOLFSSL_TLS13 */
  5065. #if defined(WOLFSSL_TLS13)
  5066. /******************************************************************************/
  5067. /* Cookie */
  5068. /******************************************************************************/
  5069. /* Free the cookie data.
  5070. *
  5071. * cookie Cookie data.
  5072. * heap The heap used for allocation.
  5073. */
  5074. static void TLSX_Cookie_FreeAll(Cookie* cookie, void* heap)
  5075. {
  5076. (void)heap;
  5077. if (cookie != NULL)
  5078. XFREE(cookie, heap, DYNAMIC_TYPE_TLSX);
  5079. }
  5080. /* Get the size of the encoded Cookie extension.
  5081. * In messages: ClientHello and HelloRetryRequest.
  5082. *
  5083. * cookie The cookie to write.
  5084. * msgType The type of the message this extension is being written into.
  5085. * returns the number of bytes of the encoded Cookie extension.
  5086. */
  5087. static int TLSX_Cookie_GetSize(Cookie* cookie, byte msgType, word16* pSz)
  5088. {
  5089. if (msgType == client_hello || msgType == hello_retry_request)
  5090. *pSz += OPAQUE16_LEN + cookie->len;
  5091. else
  5092. return SANITY_MSG_E;
  5093. return 0;
  5094. }
  5095. /* Writes the Cookie extension into the output buffer.
  5096. * Assumes that the the output buffer is big enough to hold data.
  5097. * In messages: ClientHello and HelloRetryRequest.
  5098. *
  5099. * cookie The cookie to write.
  5100. * output The buffer to write into.
  5101. * msgType The type of the message this extension is being written into.
  5102. * returns the number of bytes written into the buffer.
  5103. */
  5104. static int TLSX_Cookie_Write(Cookie* cookie, byte* output, byte msgType,
  5105. word16* pSz)
  5106. {
  5107. if (msgType == client_hello || msgType == hello_retry_request) {
  5108. c16toa(cookie->len, output);
  5109. output += OPAQUE16_LEN;
  5110. XMEMCPY(output, &cookie->data, cookie->len);
  5111. *pSz += OPAQUE16_LEN + cookie->len;
  5112. }
  5113. else
  5114. return SANITY_MSG_E;
  5115. return 0;
  5116. }
  5117. /* Parse the Cookie extension.
  5118. * In messages: ClientHello and HelloRetryRequest.
  5119. *
  5120. * ssl The SSL/TLS object.
  5121. * input The extension data.
  5122. * length The length of the extension data.
  5123. * msgType The type of the message this extension is being parsed from.
  5124. * returns 0 on success and other values indicate failure.
  5125. */
  5126. static int TLSX_Cookie_Parse(WOLFSSL* ssl, byte* input, word16 length,
  5127. byte msgType)
  5128. {
  5129. word16 len;
  5130. word16 idx = 0;
  5131. TLSX* extension;
  5132. Cookie* cookie;
  5133. if (msgType != client_hello && msgType != hello_retry_request)
  5134. return SANITY_MSG_E;
  5135. /* Message contains length and Cookie which must be at least one byte
  5136. * in length.
  5137. */
  5138. if (length < OPAQUE16_LEN + 1)
  5139. return BUFFER_E;
  5140. ato16(input + idx, &len);
  5141. idx += OPAQUE16_LEN;
  5142. if (length - idx != len)
  5143. return BUFFER_E;
  5144. if (msgType == hello_retry_request)
  5145. return TLSX_Cookie_Use(ssl, input + idx, len, NULL, 0, 0);
  5146. /* client_hello */
  5147. extension = TLSX_Find(ssl->extensions, TLSX_COOKIE);
  5148. if (extension == NULL)
  5149. return HRR_COOKIE_ERROR;
  5150. cookie = (Cookie*)extension->data;
  5151. if (cookie->len != len || XMEMCMP(&cookie->data, input + idx, len) != 0)
  5152. return HRR_COOKIE_ERROR;
  5153. /* Request seen. */
  5154. extension->resp = 0;
  5155. return 0;
  5156. }
  5157. /* Use the data to create a new Cookie object in the extensions.
  5158. *
  5159. * ssl SSL/TLS object.
  5160. * data Cookie data.
  5161. * len Length of cookie data in bytes.
  5162. * mac MAC data.
  5163. * macSz Length of MAC data in bytes.
  5164. * resp Indicates the extension will go into a response (HelloRetryRequest).
  5165. * returns 0 on success and other values indicate failure.
  5166. */
  5167. int TLSX_Cookie_Use(WOLFSSL* ssl, byte* data, word16 len, byte* mac,
  5168. byte macSz, int resp)
  5169. {
  5170. int ret = 0;
  5171. TLSX* extension;
  5172. Cookie* cookie;
  5173. /* Find the cookie extension if it exists. */
  5174. extension = TLSX_Find(ssl->extensions, TLSX_COOKIE);
  5175. if (extension == NULL) {
  5176. /* Push new cookie extension. */
  5177. ret = TLSX_Push(&ssl->extensions, TLSX_COOKIE, NULL, ssl->heap);
  5178. if (ret != 0)
  5179. return ret;
  5180. extension = TLSX_Find(ssl->extensions, TLSX_COOKIE);
  5181. if (extension == NULL)
  5182. return MEMORY_E;
  5183. }
  5184. /* The Cookie structure has one byte for cookie data already. */
  5185. cookie = (Cookie*)XMALLOC(sizeof(Cookie) + len + macSz - 1, ssl->heap,
  5186. DYNAMIC_TYPE_TLSX);
  5187. if (cookie == NULL)
  5188. return MEMORY_E;
  5189. cookie->len = len + macSz;
  5190. XMEMCPY(&cookie->data, data, len);
  5191. if (mac != NULL)
  5192. XMEMCPY(&cookie->data + len, mac, macSz);
  5193. extension->data = (void*)cookie;
  5194. extension->resp = (byte)resp;
  5195. return 0;
  5196. }
  5197. #define CKE_FREE_ALL TLSX_Cookie_FreeAll
  5198. #define CKE_GET_SIZE TLSX_Cookie_GetSize
  5199. #define CKE_WRITE TLSX_Cookie_Write
  5200. #define CKE_PARSE TLSX_Cookie_Parse
  5201. #else
  5202. #define CKE_FREE_ALL(a, b) 0
  5203. #define CKE_GET_SIZE(a, b, c) 0
  5204. #define CKE_WRITE(a, b, c, d) 0
  5205. #define CKE_PARSE(a, b, c, d) 0
  5206. #endif
  5207. #if !defined(WOLFSSL_NO_SIGALG)
  5208. /******************************************************************************/
  5209. /* Signature Algorithms */
  5210. /******************************************************************************/
  5211. /* Return the size of the SignatureAlgorithms extension's data.
  5212. *
  5213. * data Unused
  5214. * returns the length of data that will be in the extension.
  5215. */
  5216. static word16 TLSX_SignatureAlgorithms_GetSize(void* data)
  5217. {
  5218. WOLFSSL* ssl = (WOLFSSL*)data;
  5219. return OPAQUE16_LEN + ssl->suites->hashSigAlgoSz;
  5220. }
  5221. /* Creates a bit string of supported hash algorithms with RSA PSS.
  5222. * The bit string is used when determining which signature algorithm to use
  5223. * when creating the CertificateVerify message.
  5224. * Note: Valid data has an even length as each signature algorithm is two bytes.
  5225. *
  5226. * ssl The SSL/TLS object.
  5227. * input The buffer with the list of supported signature algorithms.
  5228. * length The length of the list in bytes.
  5229. * returns 0 on success, BUFFER_ERROR when the length is not even.
  5230. */
  5231. static int TLSX_SignatureAlgorithms_MapPss(WOLFSSL *ssl, byte* input,
  5232. word16 length)
  5233. {
  5234. word16 i;
  5235. if ((length & 1) == 1)
  5236. return BUFFER_ERROR;
  5237. ssl->pssAlgo = 0;
  5238. for (i = 0; i < length; i += 2) {
  5239. if (input[i] == rsa_pss_sa_algo && input[i + 1] <= sha512_mac)
  5240. ssl->pssAlgo |= 1 << input[i + 1];
  5241. #ifdef WOLFSSL_TLS13
  5242. if (input[i] == rsa_pss_sa_algo && input[i + 1] >= pss_sha256 &&
  5243. input[i + 1] <= pss_sha512) {
  5244. ssl->pssAlgo |= 1 << input[i + 1];
  5245. }
  5246. #endif
  5247. }
  5248. return 0;
  5249. }
  5250. /* Writes the SignatureAlgorithms extension into the buffer.
  5251. *
  5252. * data Unused
  5253. * output The buffer to write the extension into.
  5254. * returns the length of data that was written.
  5255. */
  5256. static word16 TLSX_SignatureAlgorithms_Write(void* data, byte* output)
  5257. {
  5258. WOLFSSL* ssl = (WOLFSSL*)data;
  5259. c16toa(ssl->suites->hashSigAlgoSz, output);
  5260. XMEMCPY(output + OPAQUE16_LEN, ssl->suites->hashSigAlgo,
  5261. ssl->suites->hashSigAlgoSz);
  5262. TLSX_SignatureAlgorithms_MapPss(ssl, output + OPAQUE16_LEN,
  5263. ssl->suites->hashSigAlgoSz);
  5264. return OPAQUE16_LEN + ssl->suites->hashSigAlgoSz;
  5265. }
  5266. /* Parse the SignatureAlgorithms extension.
  5267. *
  5268. * ssl The SSL/TLS object.
  5269. * input The buffer with the extension data.
  5270. * length The length of the extension data.
  5271. * returns 0 on success, otherwise failure.
  5272. */
  5273. static int TLSX_SignatureAlgorithms_Parse(WOLFSSL *ssl, byte* input,
  5274. word16 length, byte isRequest, Suites* suites)
  5275. {
  5276. word16 len;
  5277. if (!isRequest)
  5278. return BUFFER_ERROR;
  5279. /* Must contain a length and at least algorithm. */
  5280. if (length < OPAQUE16_LEN + OPAQUE16_LEN || (length & 1) != 0)
  5281. return BUFFER_ERROR;
  5282. ato16(input, &len);
  5283. input += OPAQUE16_LEN;
  5284. /* Algorithm array must fill rest of data. */
  5285. if (length != OPAQUE16_LEN + len)
  5286. return BUFFER_ERROR;
  5287. /* Sig Algo list size must be even. */
  5288. if (suites->hashSigAlgoSz % 2 != 0)
  5289. return BUFFER_ERROR;
  5290. /* truncate hashSigAlgo list if too long */
  5291. suites->hashSigAlgoSz = len;
  5292. if (suites->hashSigAlgoSz > WOLFSSL_MAX_SIGALGO) {
  5293. WOLFSSL_MSG("TLSX SigAlgo list exceeds max, truncating");
  5294. suites->hashSigAlgoSz = WOLFSSL_MAX_SIGALGO;
  5295. }
  5296. XMEMCPY(suites->hashSigAlgo, input, suites->hashSigAlgoSz);
  5297. return TLSX_SignatureAlgorithms_MapPss(ssl, input, len);
  5298. }
  5299. /* Sets a new SignatureAlgorithms extension into the extension list.
  5300. *
  5301. * extensions The list of extensions.
  5302. * data The extensions specific data.
  5303. * heap The heap used for allocation.
  5304. * returns 0 on success, otherwise failure.
  5305. */
  5306. static int TLSX_SetSignatureAlgorithms(TLSX** extensions, const void* data,
  5307. void* heap)
  5308. {
  5309. if (extensions == NULL)
  5310. return BAD_FUNC_ARG;
  5311. return TLSX_Push(extensions, TLSX_SIGNATURE_ALGORITHMS, (void *)data, heap);
  5312. }
  5313. #define SA_GET_SIZE TLSX_SignatureAlgorithms_GetSize
  5314. #define SA_WRITE TLSX_SignatureAlgorithms_Write
  5315. #define SA_PARSE TLSX_SignatureAlgorithms_Parse
  5316. #endif
  5317. /******************************************************************************/
  5318. /* Signature Algorithms Certificate */
  5319. /******************************************************************************/
  5320. #ifdef WOLFSSL_TLS13
  5321. /* Return the size of the SignatureAlgorithms extension's data.
  5322. *
  5323. * data Unused
  5324. * returns the length of data that will be in the extension.
  5325. */
  5326. static word16 TLSX_SignatureAlgorithmsCert_GetSize(void* data)
  5327. {
  5328. WOLFSSL* ssl = (WOLFSSL*)data;
  5329. return OPAQUE16_LEN + ssl->certHashSigAlgoSz;
  5330. }
  5331. /* Writes the SignatureAlgorithmsCert extension into the buffer.
  5332. *
  5333. * data Unused
  5334. * output The buffer to write the extension into.
  5335. * returns the length of data that was written.
  5336. */
  5337. static word16 TLSX_SignatureAlgorithmsCert_Write(void* data, byte* output)
  5338. {
  5339. WOLFSSL* ssl = (WOLFSSL*)data;
  5340. c16toa(ssl->certHashSigAlgoSz, output);
  5341. XMEMCPY(output + OPAQUE16_LEN, ssl->certHashSigAlgo,
  5342. ssl->certHashSigAlgoSz);
  5343. return OPAQUE16_LEN + ssl->certHashSigAlgoSz;
  5344. }
  5345. /* Parse the SignatureAlgorithmsCert extension.
  5346. *
  5347. * ssl The SSL/TLS object.
  5348. * input The buffer with the extension data.
  5349. * length The length of the extension data.
  5350. * returns 0 on success, otherwise failure.
  5351. */
  5352. static int TLSX_SignatureAlgorithmsCert_Parse(WOLFSSL *ssl, byte* input,
  5353. word16 length, byte isRequest)
  5354. {
  5355. word16 len;
  5356. if (!isRequest)
  5357. return BUFFER_ERROR;
  5358. /* Must contain a length and at least algorithm. */
  5359. if (length < OPAQUE16_LEN + OPAQUE16_LEN || (length & 1) != 0)
  5360. return BUFFER_ERROR;
  5361. ato16(input, &len);
  5362. input += OPAQUE16_LEN;
  5363. /* Algorithm array must fill rest of data. */
  5364. if (length != OPAQUE16_LEN + len)
  5365. return BUFFER_ERROR;
  5366. /* truncate hashSigAlgo list if too long */
  5367. ssl->certHashSigAlgoSz = len;
  5368. if (ssl->certHashSigAlgoSz > WOLFSSL_MAX_SIGALGO) {
  5369. WOLFSSL_MSG("TLSX SigAlgo list exceeds max, truncating");
  5370. ssl->certHashSigAlgoSz = WOLFSSL_MAX_SIGALGO;
  5371. }
  5372. XMEMCPY(ssl->certHashSigAlgo, input, ssl->certHashSigAlgoSz);
  5373. return 0;
  5374. }
  5375. /* Sets a new SignatureAlgorithmsCert extension into the extension list.
  5376. *
  5377. * extensions The list of extensions.
  5378. * data The extensions specific data.
  5379. * heap The heap used for allocation.
  5380. * returns 0 on success, otherwise failure.
  5381. */
  5382. static int TLSX_SetSignatureAlgorithmsCert(TLSX** extensions, const void* data,
  5383. void* heap)
  5384. {
  5385. if (extensions == NULL)
  5386. return BAD_FUNC_ARG;
  5387. return TLSX_Push(extensions, TLSX_SIGNATURE_ALGORITHMS_CERT, (void *)data,
  5388. heap);
  5389. }
  5390. #define SAC_GET_SIZE TLSX_SignatureAlgorithmsCert_GetSize
  5391. #define SAC_WRITE TLSX_SignatureAlgorithmsCert_Write
  5392. #define SAC_PARSE TLSX_SignatureAlgorithmsCert_Parse
  5393. #endif /* WOLFSSL_TLS13 */
  5394. /******************************************************************************/
  5395. /* Key Share */
  5396. /******************************************************************************/
  5397. #ifdef WOLFSSL_TLS13
  5398. /* Create a key share entry using named Diffie-Hellman parameters group.
  5399. * Generates a key pair.
  5400. *
  5401. * ssl The SSL/TLS object.
  5402. * kse The key share entry object.
  5403. * returns 0 on success, otherwise failure.
  5404. */
  5405. static int TLSX_KeyShare_GenDhKey(WOLFSSL *ssl, KeyShareEntry* kse)
  5406. {
  5407. int ret;
  5408. #ifndef NO_DH
  5409. byte* keyData;
  5410. void* key = NULL;
  5411. word32 keySz;
  5412. word32 dataSz;
  5413. const DhParams* params;
  5414. #ifdef WOLFSSL_SMALL_STACK
  5415. DhKey* dhKey = NULL;
  5416. #else
  5417. DhKey dhKey[1];
  5418. #endif
  5419. /* TODO: [TLS13] The key size should come from wolfcrypt. */
  5420. /* Pick the parameters from the named group. */
  5421. switch (kse->group) {
  5422. #ifdef HAVE_FFDHE_2048
  5423. case WOLFSSL_FFDHE_2048:
  5424. params = wc_Dh_ffdhe2048_Get();
  5425. keySz = 29;
  5426. break;
  5427. #endif
  5428. #ifdef HAVE_FFDHE_3072
  5429. case WOLFSSL_FFDHE_3072:
  5430. params = wc_Dh_ffdhe3072_Get();
  5431. keySz = 34;
  5432. break;
  5433. #endif
  5434. #ifdef HAVE_FFDHE_4096
  5435. case WOLFSSL_FFDHE_4096:
  5436. params = wc_Dh_ffdhe4096_Get();
  5437. keySz = 39;
  5438. break;
  5439. #endif
  5440. #ifdef HAVE_FFDHE_6144
  5441. case WOLFSSL_FFDHE_6144:
  5442. params = wc_Dh_ffdhe6144_Get();
  5443. keySz = 46;
  5444. break;
  5445. #endif
  5446. #ifdef HAVE_FFDHE_8192
  5447. case WOLFSSL_FFDHE_8192:
  5448. params = wc_Dh_ffdhe8192_Get();
  5449. keySz = 52;
  5450. break;
  5451. #endif
  5452. default:
  5453. return BAD_FUNC_ARG;
  5454. }
  5455. #ifdef WOLFSSL_SMALL_STACK
  5456. dhKey = (DhKey*)XMALLOC(sizeof(DhKey), ssl->heap, DYNAMIC_TYPE_DH);
  5457. if (dhKey == NULL)
  5458. return MEMORY_E;
  5459. #endif
  5460. ret = wc_InitDhKey_ex(dhKey, ssl->heap, ssl->devId);
  5461. if (ret != 0) {
  5462. #ifdef WOLFSSL_SMALL_STACK
  5463. XFREE(dhKey, ssl->heap, DYNAMIC_TYPE_DH);
  5464. #endif
  5465. return ret;
  5466. }
  5467. /* Allocate space for the public key */
  5468. dataSz = params->p_len;
  5469. keyData = (byte*)XMALLOC(dataSz, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5470. if (keyData == NULL) {
  5471. ret = MEMORY_E;
  5472. goto end;
  5473. }
  5474. /* Allocate space for the private key */
  5475. key = (byte*)XMALLOC(keySz, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5476. if (key == NULL) {
  5477. ret = MEMORY_E;
  5478. goto end;
  5479. }
  5480. /* Set key */
  5481. ret = wc_DhSetKey(dhKey,
  5482. (byte*)params->p, params->p_len,
  5483. (byte*)params->g, params->g_len);
  5484. if (ret != 0)
  5485. goto end;
  5486. #if defined(WOLFSSL_STATIC_EPHEMERAL) && defined(WOLFSSL_DH_EXTRA)
  5487. if (ssl->staticKE.key && ssl->staticKE.keyAlgo == WC_PK_TYPE_DH) {
  5488. DerBuffer* keyDer = ssl->staticKE.key;
  5489. word32 idx = 0;
  5490. WOLFSSL_MSG("Using static DH key");
  5491. ret = wc_DhKeyDecode(keyDer->buffer, &idx, dhKey, keyDer->length);
  5492. if (ret == 0) {
  5493. ret = wc_DhExportKeyPair(dhKey, (byte*)key, &keySz, keyData, &dataSz);
  5494. }
  5495. }
  5496. else
  5497. #endif
  5498. {
  5499. /* Generate a new key pair */
  5500. ret = wc_DhGenerateKeyPair(dhKey, ssl->rng, (byte*)key, &keySz, keyData,
  5501. &dataSz);
  5502. #ifdef WOLFSSL_ASYNC_CRYPT
  5503. /* TODO: Make this function non-blocking */
  5504. if (ret == WC_PENDING_E) {
  5505. ret = wc_AsyncWait(ret, &dhKey->asyncDev, WC_ASYNC_FLAG_NONE);
  5506. }
  5507. #endif
  5508. }
  5509. if (ret != 0)
  5510. goto end;
  5511. if (params->p_len != dataSz) {
  5512. /* Zero pad the front of the public key to match prime "p" size */
  5513. XMEMMOVE(keyData + params->p_len - dataSz, keyData, dataSz);
  5514. XMEMSET(keyData, 0, params->p_len - dataSz);
  5515. }
  5516. kse->pubKey = keyData;
  5517. kse->pubKeyLen = params->p_len;
  5518. kse->key = key;
  5519. kse->keyLen = keySz;
  5520. #ifdef WOLFSSL_DEBUG_TLS
  5521. WOLFSSL_MSG("Public DH Key");
  5522. WOLFSSL_BUFFER(keyData, params->p_len);
  5523. #endif
  5524. end:
  5525. wc_FreeDhKey(dhKey);
  5526. #ifdef WOLFSSL_SMALL_STACK
  5527. XFREE(dhKey, ssl->heap, DYNAMIC_TYPE_DH);
  5528. #endif
  5529. if (ret != 0) {
  5530. /* Data owned by key share entry otherwise. */
  5531. if (keyData != NULL)
  5532. XFREE(keyData, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5533. if (key != NULL)
  5534. XFREE(key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5535. }
  5536. #else
  5537. (void)ssl;
  5538. (void)kse;
  5539. ret = NOT_COMPILED_IN;
  5540. #endif
  5541. return ret;
  5542. }
  5543. /* Create a key share entry using X25519 parameters group.
  5544. * Generates a key pair.
  5545. *
  5546. * ssl The SSL/TLS object.
  5547. * kse The key share entry object.
  5548. * returns 0 on success, otherwise failure.
  5549. */
  5550. static int TLSX_KeyShare_GenX25519Key(WOLFSSL *ssl, KeyShareEntry* kse)
  5551. {
  5552. int ret;
  5553. #ifdef HAVE_CURVE25519
  5554. byte* keyData = NULL;
  5555. word32 dataSize = CURVE25519_KEYSIZE;
  5556. curve25519_key* key;
  5557. /* Allocate an ECC key to hold private key. */
  5558. key = (curve25519_key*)XMALLOC(sizeof(curve25519_key), ssl->heap,
  5559. DYNAMIC_TYPE_PRIVATE_KEY);
  5560. if (key == NULL) {
  5561. WOLFSSL_MSG("EccTempKey Memory error");
  5562. return MEMORY_E;
  5563. }
  5564. /* Make an ECC key. */
  5565. ret = wc_curve25519_init(key);
  5566. if (ret != 0)
  5567. goto end;
  5568. ret = wc_curve25519_make_key(ssl->rng, CURVE25519_KEYSIZE, key);
  5569. if (ret != 0)
  5570. goto end;
  5571. /* Allocate space for the public key. */
  5572. keyData = (byte*)XMALLOC(CURVE25519_KEYSIZE, ssl->heap,
  5573. DYNAMIC_TYPE_PUBLIC_KEY);
  5574. if (keyData == NULL) {
  5575. WOLFSSL_MSG("Key data Memory error");
  5576. ret = MEMORY_E;
  5577. goto end;
  5578. }
  5579. /* Export public key. */
  5580. if (wc_curve25519_export_public_ex(key, keyData, &dataSize,
  5581. EC25519_LITTLE_ENDIAN) != 0) {
  5582. ret = ECC_EXPORT_ERROR;
  5583. goto end;
  5584. }
  5585. kse->pubKey = keyData;
  5586. kse->pubKeyLen = CURVE25519_KEYSIZE;
  5587. kse->key = key;
  5588. #ifdef WOLFSSL_DEBUG_TLS
  5589. WOLFSSL_MSG("Public Curve25519 Key");
  5590. WOLFSSL_BUFFER(keyData, dataSize);
  5591. #endif
  5592. end:
  5593. if (ret != 0) {
  5594. /* Data owned by key share entry otherwise. */
  5595. if (keyData != NULL)
  5596. XFREE(keyData, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5597. wc_curve25519_free(key);
  5598. XFREE(key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5599. }
  5600. #else
  5601. (void)ssl;
  5602. (void)kse;
  5603. ret = NOT_COMPILED_IN;
  5604. #endif /* HAVE_CURVE25519 */
  5605. return ret;
  5606. }
  5607. /* Create a key share entry using X448 parameters group.
  5608. * Generates a key pair.
  5609. *
  5610. * ssl The SSL/TLS object.
  5611. * kse The key share entry object.
  5612. * returns 0 on success, otherwise failure.
  5613. */
  5614. static int TLSX_KeyShare_GenX448Key(WOLFSSL *ssl, KeyShareEntry* kse)
  5615. {
  5616. int ret;
  5617. #ifdef HAVE_CURVE448
  5618. byte* keyData = NULL;
  5619. word32 dataSize = CURVE448_KEY_SIZE;
  5620. curve448_key* key;
  5621. /* Allocate an ECC key to hold private key. */
  5622. key = (curve448_key*)XMALLOC(sizeof(curve448_key), ssl->heap,
  5623. DYNAMIC_TYPE_PRIVATE_KEY);
  5624. if (key == NULL) {
  5625. WOLFSSL_MSG("EccTempKey Memory error");
  5626. return MEMORY_E;
  5627. }
  5628. /* Make an ECC key. */
  5629. ret = wc_curve448_init(key);
  5630. if (ret != 0)
  5631. goto end;
  5632. ret = wc_curve448_make_key(ssl->rng, CURVE448_KEY_SIZE, key);
  5633. if (ret != 0)
  5634. goto end;
  5635. /* Allocate space for the public key. */
  5636. keyData = (byte*)XMALLOC(CURVE448_KEY_SIZE, ssl->heap,
  5637. DYNAMIC_TYPE_PUBLIC_KEY);
  5638. if (keyData == NULL) {
  5639. WOLFSSL_MSG("Key data Memory error");
  5640. ret = MEMORY_E;
  5641. goto end;
  5642. }
  5643. /* Export public key. */
  5644. if (wc_curve448_export_public_ex(key, keyData, &dataSize,
  5645. EC448_LITTLE_ENDIAN) != 0) {
  5646. ret = ECC_EXPORT_ERROR;
  5647. goto end;
  5648. }
  5649. kse->pubKey = keyData;
  5650. kse->pubKeyLen = CURVE448_KEY_SIZE;
  5651. kse->key = key;
  5652. #ifdef WOLFSSL_DEBUG_TLS
  5653. WOLFSSL_MSG("Public Curve448 Key");
  5654. WOLFSSL_BUFFER(keyData, dataSize);
  5655. #endif
  5656. end:
  5657. if (ret != 0) {
  5658. /* Data owned by key share entry otherwise. */
  5659. if (keyData != NULL)
  5660. XFREE(keyData, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5661. wc_curve448_free(key);
  5662. XFREE(key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5663. }
  5664. #else
  5665. (void)ssl;
  5666. (void)kse;
  5667. ret = NOT_COMPILED_IN;
  5668. #endif /* HAVE_CURVE448 */
  5669. return ret;
  5670. }
  5671. /* Create a key share entry using named elliptic curve parameters group.
  5672. * Generates a key pair.
  5673. *
  5674. * ssl The SSL/TLS object.
  5675. * kse The key share entry object.
  5676. * returns 0 on success, otherwise failure.
  5677. */
  5678. static int TLSX_KeyShare_GenEccKey(WOLFSSL *ssl, KeyShareEntry* kse)
  5679. {
  5680. int ret;
  5681. #ifdef HAVE_ECC
  5682. byte* keyData = NULL;
  5683. word32 dataSize;
  5684. byte* keyPtr = NULL;
  5685. word32 keySize;
  5686. ecc_key* eccKey;
  5687. word16 curveId;
  5688. /* TODO: [TLS13] The key sizes should come from wolfcrypt. */
  5689. /* Translate named group to a curve id. */
  5690. switch (kse->group) {
  5691. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  5692. #ifndef NO_ECC_SECP
  5693. case WOLFSSL_ECC_SECP256R1:
  5694. curveId = ECC_SECP256R1;
  5695. keySize = 32;
  5696. dataSize = keySize * 2 + 1;
  5697. break;
  5698. #endif /* !NO_ECC_SECP */
  5699. #endif
  5700. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  5701. #ifndef NO_ECC_SECP
  5702. case WOLFSSL_ECC_SECP384R1:
  5703. curveId = ECC_SECP384R1;
  5704. keySize = 48;
  5705. dataSize = keySize * 2 + 1;
  5706. break;
  5707. #endif /* !NO_ECC_SECP */
  5708. #endif
  5709. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  5710. #ifndef NO_ECC_SECP
  5711. case WOLFSSL_ECC_SECP521R1:
  5712. curveId = ECC_SECP521R1;
  5713. keySize = 66;
  5714. dataSize = keySize * 2 + 1;
  5715. break;
  5716. #endif /* !NO_ECC_SECP */
  5717. #endif
  5718. #ifdef HAVE_X448
  5719. case WOLFSSL_ECC_X448:
  5720. curveId = ECC_X448;
  5721. dataSize = keySize = 56;
  5722. break;
  5723. #endif
  5724. default:
  5725. return BAD_FUNC_ARG;
  5726. }
  5727. /* Allocate an ECC key to hold private key. */
  5728. keyPtr = (byte*)XMALLOC(sizeof(ecc_key), ssl->heap,
  5729. DYNAMIC_TYPE_PRIVATE_KEY);
  5730. if (keyPtr == NULL) {
  5731. WOLFSSL_MSG("EccTempKey Memory error");
  5732. return MEMORY_E;
  5733. }
  5734. eccKey = (ecc_key*)keyPtr;
  5735. /* Make an ECC key. */
  5736. ret = wc_ecc_init_ex(eccKey, ssl->heap, ssl->devId);
  5737. if (ret != 0)
  5738. goto end;
  5739. #ifdef WOLFSSL_STATIC_EPHEMERAL
  5740. if (ssl->staticKE.key && ssl->staticKE.keyAlgo == WC_PK_TYPE_ECDH) {
  5741. DerBuffer* keyDer = ssl->staticKE.key;
  5742. word32 idx = 0;
  5743. WOLFSSL_MSG("Using static ECDH key");
  5744. ret = wc_EccPrivateKeyDecode(keyDer->buffer, &idx, eccKey, keyDer->length);
  5745. }
  5746. else
  5747. #endif
  5748. {
  5749. /* Generate ephemeral ECC key */
  5750. ret = wc_ecc_make_key_ex(ssl->rng, keySize, eccKey, curveId);
  5751. #ifdef WOLFSSL_ASYNC_CRYPT
  5752. /* TODO: Make this function non-blocking */
  5753. if (ret == WC_PENDING_E) {
  5754. ret = wc_AsyncWait(ret, &eccKey->asyncDev, WC_ASYNC_FLAG_NONE);
  5755. }
  5756. #endif
  5757. }
  5758. if (ret != 0)
  5759. goto end;
  5760. /* Allocate space for the public key. */
  5761. keyData = (byte*)XMALLOC(dataSize, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5762. if (keyData == NULL) {
  5763. WOLFSSL_MSG("Key data Memory error");
  5764. ret = MEMORY_E;
  5765. goto end;
  5766. }
  5767. /* Export public key. */
  5768. if (wc_ecc_export_x963(eccKey, keyData, &dataSize) != 0) {
  5769. ret = ECC_EXPORT_ERROR;
  5770. goto end;
  5771. }
  5772. kse->pubKey = keyData;
  5773. kse->pubKeyLen = dataSize;
  5774. kse->key = keyPtr;
  5775. #ifdef WOLFSSL_DEBUG_TLS
  5776. WOLFSSL_MSG("Public ECC Key");
  5777. WOLFSSL_BUFFER(keyData, dataSize);
  5778. #endif
  5779. end:
  5780. if (ret != 0) {
  5781. /* Data owned by key share entry otherwise. */
  5782. if (keyPtr != NULL)
  5783. XFREE(keyPtr, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5784. if (keyData != NULL)
  5785. XFREE(keyData, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5786. }
  5787. #else
  5788. (void)ssl;
  5789. (void)kse;
  5790. ret = NOT_COMPILED_IN;
  5791. #endif /* HAVE_ECC */
  5792. return ret;
  5793. }
  5794. /* Generate a secret/key using the key share entry.
  5795. *
  5796. * ssl The SSL/TLS object.
  5797. * kse The key share entry holding peer data.
  5798. */
  5799. static int TLSX_KeyShare_GenKey(WOLFSSL *ssl, KeyShareEntry *kse)
  5800. {
  5801. /* Named FFHE groups have a bit set to identify them. */
  5802. if ((kse->group & NAMED_DH_MASK) == NAMED_DH_MASK)
  5803. return TLSX_KeyShare_GenDhKey(ssl, kse);
  5804. if (kse->group == WOLFSSL_ECC_X25519)
  5805. return TLSX_KeyShare_GenX25519Key(ssl, kse);
  5806. if (kse->group == WOLFSSL_ECC_X448)
  5807. return TLSX_KeyShare_GenX448Key(ssl, kse);
  5808. return TLSX_KeyShare_GenEccKey(ssl, kse);
  5809. }
  5810. /* Free the key share dynamic data.
  5811. *
  5812. * list The linked list of key share entry objects.
  5813. * heap The heap used for allocation.
  5814. */
  5815. static void TLSX_KeyShare_FreeAll(KeyShareEntry* list, void* heap)
  5816. {
  5817. KeyShareEntry* current;
  5818. while ((current = list) != NULL) {
  5819. list = current->next;
  5820. if ((current->group & NAMED_DH_MASK) == 0) {
  5821. if (current->group == WOLFSSL_ECC_X25519) {
  5822. #ifdef HAVE_CURVE25519
  5823. wc_curve25519_free((curve25519_key*)current->key);
  5824. #endif
  5825. }
  5826. else if (current->group == WOLFSSL_ECC_X448) {
  5827. #ifdef HAVE_CURVE448
  5828. wc_curve448_free((curve448_key*)current->key);
  5829. #endif
  5830. }
  5831. else {
  5832. #ifdef HAVE_ECC
  5833. wc_ecc_free((ecc_key*)(current->key));
  5834. #endif
  5835. }
  5836. }
  5837. if (current->key != NULL)
  5838. XFREE(current->key, heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5839. XFREE(current->pubKey, heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5840. XFREE(current->ke, heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5841. XFREE(current, heap, DYNAMIC_TYPE_TLSX);
  5842. }
  5843. (void)heap;
  5844. }
  5845. /* Get the size of the encoded key share extension.
  5846. *
  5847. * list The linked list of key share extensions.
  5848. * msgType The type of the message this extension is being written into.
  5849. * returns the number of bytes of the encoded key share extension.
  5850. */
  5851. static word16 TLSX_KeyShare_GetSize(KeyShareEntry* list, byte msgType)
  5852. {
  5853. word16 len = 0;
  5854. byte isRequest = (msgType == client_hello);
  5855. KeyShareEntry* current;
  5856. /* The named group the server wants to use. */
  5857. if (msgType == hello_retry_request)
  5858. return OPAQUE16_LEN;
  5859. /* List of key exchange groups. */
  5860. if (isRequest)
  5861. len += OPAQUE16_LEN;
  5862. while ((current = list) != NULL) {
  5863. list = current->next;
  5864. if (!isRequest && current->key == NULL)
  5865. continue;
  5866. len += KE_GROUP_LEN + OPAQUE16_LEN + current->pubKeyLen;
  5867. }
  5868. return len;
  5869. }
  5870. /* Writes the key share extension into the output buffer.
  5871. * Assumes that the the output buffer is big enough to hold data.
  5872. *
  5873. * list The linked list of key share entries.
  5874. * output The buffer to write into.
  5875. * msgType The type of the message this extension is being written into.
  5876. * returns the number of bytes written into the buffer.
  5877. */
  5878. static word16 TLSX_KeyShare_Write(KeyShareEntry* list, byte* output,
  5879. byte msgType)
  5880. {
  5881. word16 i = 0;
  5882. byte isRequest = (msgType == client_hello);
  5883. KeyShareEntry* current;
  5884. if (msgType == hello_retry_request) {
  5885. c16toa(list->group, output);
  5886. return OPAQUE16_LEN;
  5887. }
  5888. /* ClientHello has a list but ServerHello is only the chosen. */
  5889. if (isRequest)
  5890. i += OPAQUE16_LEN;
  5891. /* Write out all in the list. */
  5892. while ((current = list) != NULL) {
  5893. list = current->next;
  5894. if (!isRequest && current->key == NULL)
  5895. continue;
  5896. c16toa(current->group, &output[i]);
  5897. i += KE_GROUP_LEN;
  5898. c16toa((word16)(current->pubKeyLen), &output[i]);
  5899. i += OPAQUE16_LEN;
  5900. XMEMCPY(&output[i], current->pubKey, current->pubKeyLen);
  5901. i += (word16)current->pubKeyLen;
  5902. }
  5903. /* Write the length of the list if required. */
  5904. if (isRequest)
  5905. c16toa(i - OPAQUE16_LEN, output);
  5906. return i;
  5907. }
  5908. /* Process the DH key share extension on the client side.
  5909. *
  5910. * ssl The SSL/TLS object.
  5911. * keyShareEntry The key share entry object to use to calculate shared secret.
  5912. * returns 0 on success and other values indicate failure.
  5913. */
  5914. static int TLSX_KeyShare_ProcessDh(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  5915. {
  5916. #ifndef NO_DH
  5917. int ret;
  5918. const DhParams* params;
  5919. #ifdef WOLFSSL_SMALL_STACK
  5920. DhKey* dhKey = NULL;
  5921. #else
  5922. DhKey dhKey[1];
  5923. #endif
  5924. switch (keyShareEntry->group) {
  5925. #ifdef HAVE_FFDHE_2048
  5926. case WOLFSSL_FFDHE_2048:
  5927. params = wc_Dh_ffdhe2048_Get();
  5928. break;
  5929. #endif
  5930. #ifdef HAVE_FFDHE_3072
  5931. case WOLFSSL_FFDHE_3072:
  5932. params = wc_Dh_ffdhe3072_Get();
  5933. break;
  5934. #endif
  5935. #ifdef HAVE_FFDHE_4096
  5936. case WOLFSSL_FFDHE_4096:
  5937. params = wc_Dh_ffdhe4096_Get();
  5938. break;
  5939. #endif
  5940. #ifdef HAVE_FFDHE_6144
  5941. case WOLFSSL_FFDHE_6144:
  5942. params = wc_Dh_ffdhe6144_Get();
  5943. break;
  5944. #endif
  5945. #ifdef HAVE_FFDHE_8192
  5946. case WOLFSSL_FFDHE_8192:
  5947. params = wc_Dh_ffdhe8192_Get();
  5948. break;
  5949. #endif
  5950. default:
  5951. return PEER_KEY_ERROR;
  5952. }
  5953. #ifdef WOLFSSL_DEBUG_TLS
  5954. WOLFSSL_MSG("Peer DH Key");
  5955. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  5956. #endif
  5957. #ifdef WOLFSSL_SMALL_STACK
  5958. dhKey = (DhKey*)XMALLOC(sizeof(DhKey), ssl->heap, DYNAMIC_TYPE_DH);
  5959. if (dhKey == NULL)
  5960. return MEMORY_E;
  5961. #endif
  5962. ret = wc_InitDhKey_ex(dhKey, ssl->heap, ssl->devId);
  5963. if (ret != 0) {
  5964. #ifdef WOLFSSL_SMALL_STACK
  5965. XFREE(dhKey, ssl->heap, DYNAMIC_TYPE_DH);
  5966. #endif
  5967. return ret;
  5968. }
  5969. /* Set key */
  5970. ret = wc_DhSetKey(dhKey, (byte*)params->p, params->p_len, (byte*)params->g,
  5971. params->g_len);
  5972. if (ret != 0) {
  5973. wc_FreeDhKey(dhKey);
  5974. #ifdef WOLFSSL_SMALL_STACK
  5975. XFREE(dhKey, ssl->heap, DYNAMIC_TYPE_DH);
  5976. #endif
  5977. return ret;
  5978. }
  5979. ret = wc_DhCheckPubKey(dhKey, keyShareEntry->ke, keyShareEntry->keLen);
  5980. if (ret != 0) {
  5981. wc_FreeDhKey(dhKey);
  5982. #ifdef WOLFSSL_SMALL_STACK
  5983. XFREE(dhKey, ssl->heap, DYNAMIC_TYPE_DH);
  5984. #endif
  5985. return PEER_KEY_ERROR;
  5986. }
  5987. /* Derive secret from private key and peer's public key. */
  5988. ret = wc_DhAgree(dhKey,
  5989. ssl->arrays->preMasterSecret, &ssl->arrays->preMasterSz,
  5990. (const byte*)keyShareEntry->key, keyShareEntry->keyLen,
  5991. keyShareEntry->ke, keyShareEntry->keLen);
  5992. #ifdef WOLFSSL_ASYNC_CRYPT
  5993. /* TODO: Make this function non-blocking */
  5994. if (ret == WC_PENDING_E) {
  5995. ret = wc_AsyncWait(ret, &dhKey->asyncDev, WC_ASYNC_FLAG_NONE);
  5996. }
  5997. #endif
  5998. /* RFC 8446 Section 7.4.1:
  5999. * ... left-padded with zeros up to the size of the prime. ...
  6000. */
  6001. if (params->p_len > ssl->arrays->preMasterSz) {
  6002. word32 diff = params->p_len - ssl->arrays->preMasterSz;
  6003. XMEMMOVE(ssl->arrays->preMasterSecret + diff,
  6004. ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz);
  6005. XMEMSET(ssl->arrays->preMasterSecret, 0, diff);
  6006. ssl->arrays->preMasterSz = params->p_len;
  6007. }
  6008. ssl->options.dhKeySz = params->p_len;
  6009. wc_FreeDhKey(dhKey);
  6010. #ifdef WOLFSSL_SMALL_STACK
  6011. XFREE(dhKey, ssl->heap, DYNAMIC_TYPE_DH);
  6012. #endif
  6013. if (keyShareEntry->key != NULL) {
  6014. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6015. keyShareEntry->key = NULL;
  6016. }
  6017. XFREE(keyShareEntry->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6018. keyShareEntry->pubKey = NULL;
  6019. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6020. keyShareEntry->ke = NULL;
  6021. return ret;
  6022. #else
  6023. (void)ssl;
  6024. (void)keyShareEntry;
  6025. return PEER_KEY_ERROR;
  6026. #endif
  6027. }
  6028. /* Process the X25519 key share extension on the client side.
  6029. *
  6030. * ssl The SSL/TLS object.
  6031. * keyShareEntry The key share entry object to use to calculate shared secret.
  6032. * returns 0 on success and other values indicate failure.
  6033. */
  6034. static int TLSX_KeyShare_ProcessX25519(WOLFSSL* ssl,
  6035. KeyShareEntry* keyShareEntry)
  6036. {
  6037. int ret;
  6038. #ifdef HAVE_CURVE25519
  6039. curve25519_key* key = (curve25519_key*)keyShareEntry->key;
  6040. curve25519_key* peerX25519Key;
  6041. #ifdef HAVE_ECC
  6042. if (ssl->peerEccKey != NULL) {
  6043. wc_ecc_free(ssl->peerEccKey);
  6044. ssl->peerEccKey = NULL;
  6045. }
  6046. #endif
  6047. peerX25519Key = (curve25519_key*)XMALLOC(sizeof(curve25519_key), ssl->heap,
  6048. DYNAMIC_TYPE_TLSX);
  6049. if (peerX25519Key == NULL) {
  6050. WOLFSSL_MSG("PeerEccKey Memory error");
  6051. return MEMORY_ERROR;
  6052. }
  6053. ret = wc_curve25519_init(peerX25519Key);
  6054. if (ret != 0) {
  6055. XFREE(peerX25519Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6056. return ret;
  6057. }
  6058. #ifdef WOLFSSL_DEBUG_TLS
  6059. WOLFSSL_MSG("Peer Curve25519 Key");
  6060. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6061. #endif
  6062. if (wc_curve25519_check_public(keyShareEntry->ke, keyShareEntry->keLen,
  6063. EC25519_LITTLE_ENDIAN) != 0) {
  6064. ret = ECC_PEERKEY_ERROR;
  6065. }
  6066. if (ret == 0) {
  6067. if (wc_curve25519_import_public_ex(keyShareEntry->ke,
  6068. keyShareEntry->keLen, peerX25519Key,
  6069. EC25519_LITTLE_ENDIAN) != 0) {
  6070. ret = ECC_PEERKEY_ERROR;
  6071. }
  6072. }
  6073. if (ret == 0) {
  6074. ssl->ecdhCurveOID = ECC_X25519_OID;
  6075. ret = wc_curve25519_shared_secret_ex(key, peerX25519Key,
  6076. ssl->arrays->preMasterSecret,
  6077. &ssl->arrays->preMasterSz,
  6078. EC25519_LITTLE_ENDIAN);
  6079. }
  6080. wc_curve25519_free(peerX25519Key);
  6081. XFREE(peerX25519Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6082. wc_curve25519_free((curve25519_key*)keyShareEntry->key);
  6083. if (keyShareEntry->key != NULL) {
  6084. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6085. keyShareEntry->key = NULL;
  6086. }
  6087. #else
  6088. (void)ssl;
  6089. (void)keyShareEntry;
  6090. ret = PEER_KEY_ERROR;
  6091. #endif /* HAVE_CURVE25519 */
  6092. return ret;
  6093. }
  6094. /* Process the X448 key share extension on the client side.
  6095. *
  6096. * ssl The SSL/TLS object.
  6097. * keyShareEntry The key share entry object to use to calculate shared secret.
  6098. * returns 0 on success and other values indicate failure.
  6099. */
  6100. static int TLSX_KeyShare_ProcessX448(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6101. {
  6102. int ret;
  6103. #ifdef HAVE_CURVE448
  6104. curve448_key* key = (curve448_key*)keyShareEntry->key;
  6105. curve448_key* peerX448Key;
  6106. #ifdef HAVE_ECC
  6107. if (ssl->peerEccKey != NULL) {
  6108. wc_ecc_free(ssl->peerEccKey);
  6109. ssl->peerEccKey = NULL;
  6110. }
  6111. #endif
  6112. peerX448Key = (curve448_key*)XMALLOC(sizeof(curve448_key), ssl->heap,
  6113. DYNAMIC_TYPE_TLSX);
  6114. if (peerX448Key == NULL) {
  6115. WOLFSSL_MSG("PeerEccKey Memory error");
  6116. return MEMORY_ERROR;
  6117. }
  6118. ret = wc_curve448_init(peerX448Key);
  6119. if (ret != 0) {
  6120. XFREE(peerX448Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6121. return ret;
  6122. }
  6123. #ifdef WOLFSSL_DEBUG_TLS
  6124. WOLFSSL_MSG("Peer Curve448 Key");
  6125. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6126. #endif
  6127. if (wc_curve448_check_public(keyShareEntry->ke, keyShareEntry->keLen,
  6128. EC448_LITTLE_ENDIAN) != 0) {
  6129. ret = ECC_PEERKEY_ERROR;
  6130. }
  6131. if (ret == 0) {
  6132. if (wc_curve448_import_public_ex(keyShareEntry->ke,
  6133. keyShareEntry->keLen, peerX448Key,
  6134. EC448_LITTLE_ENDIAN) != 0) {
  6135. ret = ECC_PEERKEY_ERROR;
  6136. }
  6137. }
  6138. if (ret == 0) {
  6139. ssl->ecdhCurveOID = ECC_X448_OID;
  6140. ret = wc_curve448_shared_secret_ex(key, peerX448Key,
  6141. ssl->arrays->preMasterSecret,
  6142. &ssl->arrays->preMasterSz,
  6143. EC448_LITTLE_ENDIAN);
  6144. }
  6145. wc_curve448_free(peerX448Key);
  6146. XFREE(peerX448Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6147. wc_curve448_free((curve448_key*)keyShareEntry->key);
  6148. if (keyShareEntry->key != NULL) {
  6149. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6150. keyShareEntry->key = NULL;
  6151. }
  6152. #else
  6153. (void)ssl;
  6154. (void)keyShareEntry;
  6155. ret = PEER_KEY_ERROR;
  6156. #endif /* HAVE_CURVE448 */
  6157. return ret;
  6158. }
  6159. /* Process the ECC key share extension on the client side.
  6160. *
  6161. * ssl The SSL/TLS object.
  6162. * keyShareEntry The key share entry object to use to calculate shared secret.
  6163. * returns 0 on success and other values indicate failure.
  6164. */
  6165. static int TLSX_KeyShare_ProcessEcc(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6166. {
  6167. int ret;
  6168. #ifdef HAVE_ECC
  6169. int curveId;
  6170. ecc_key* keyShareKey = (ecc_key*)keyShareEntry->key;
  6171. if (ssl->peerEccKey != NULL)
  6172. wc_ecc_free(ssl->peerEccKey);
  6173. ssl->peerEccKey = (ecc_key*)XMALLOC(sizeof(ecc_key), ssl->heap,
  6174. DYNAMIC_TYPE_ECC);
  6175. if (ssl->peerEccKey == NULL) {
  6176. WOLFSSL_MSG("PeerEccKey Memory error");
  6177. return MEMORY_ERROR;
  6178. }
  6179. ret = wc_ecc_init_ex(ssl->peerEccKey, ssl->heap, ssl->devId);
  6180. if (ret != 0)
  6181. return ret;
  6182. /* find supported curve */
  6183. switch (keyShareEntry->group) {
  6184. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  6185. #ifndef NO_ECC_SECP
  6186. case WOLFSSL_ECC_SECP256R1:
  6187. curveId = ECC_SECP256R1;
  6188. break;
  6189. #endif /* !NO_ECC_SECP */
  6190. #endif
  6191. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  6192. #ifndef NO_ECC_SECP
  6193. case WOLFSSL_ECC_SECP384R1:
  6194. curveId = ECC_SECP384R1;
  6195. break;
  6196. #endif /* !NO_ECC_SECP */
  6197. #endif
  6198. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  6199. #ifndef NO_ECC_SECP
  6200. case WOLFSSL_ECC_SECP521R1:
  6201. curveId = ECC_SECP521R1;
  6202. break;
  6203. #endif /* !NO_ECC_SECP */
  6204. #endif
  6205. #ifdef HAVE_X448
  6206. case WOLFSSL_ECC_X448:
  6207. curveId = ECC_X448;
  6208. break;
  6209. #endif
  6210. default:
  6211. /* unsupported curve */
  6212. return ECC_PEERKEY_ERROR;
  6213. }
  6214. #ifdef WOLFSSL_DEBUG_TLS
  6215. WOLFSSL_MSG("Peer ECC Key");
  6216. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6217. #endif
  6218. /* Point is validated by import function. */
  6219. if (wc_ecc_import_x963_ex(keyShareEntry->ke, keyShareEntry->keLen,
  6220. ssl->peerEccKey, curveId) != 0) {
  6221. return ECC_PEERKEY_ERROR;
  6222. }
  6223. ssl->ecdhCurveOID = ssl->peerEccKey->dp->oidSum;
  6224. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  6225. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))) && \
  6226. !defined(HAVE_SELFTEST)
  6227. ret = wc_ecc_set_rng(keyShareKey, ssl->rng);
  6228. if (ret != 0) {
  6229. return ret;
  6230. }
  6231. #endif
  6232. do {
  6233. #if defined(WOLFSSL_ASYNC_CRYPT)
  6234. ret = wc_AsyncWait(ret, &keyShareKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  6235. #endif
  6236. if (ret >= 0)
  6237. ret = wc_ecc_shared_secret(keyShareKey, ssl->peerEccKey,
  6238. ssl->arrays->preMasterSecret, &ssl->arrays->preMasterSz);
  6239. } while (ret == WC_PENDING_E);
  6240. #if 0
  6241. /* TODO: Switch to support async here and use: */
  6242. ret = EccSharedSecret(ssl, keyShareEntry->key, ssl->peerEccKey,
  6243. keyShareEntry->ke, &keyShareEntry->keLen,
  6244. ssl->arrays->preMasterSecret, &ssl->arrays->preMasterSz,
  6245. ssl->options.side
  6246. );
  6247. #endif
  6248. wc_ecc_free(ssl->peerEccKey);
  6249. XFREE(ssl->peerEccKey, ssl->heap, DYNAMIC_TYPE_ECC);
  6250. ssl->peerEccKey = NULL;
  6251. wc_ecc_free((ecc_key*)(keyShareEntry->key));
  6252. if (keyShareEntry->key != NULL) {
  6253. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6254. keyShareEntry->key = NULL;
  6255. }
  6256. #else
  6257. (void)ssl;
  6258. (void)keyShareEntry;
  6259. ret = PEER_KEY_ERROR;
  6260. #endif /* HAVE_ECC */
  6261. return ret;
  6262. }
  6263. /* Process the key share extension on the client side.
  6264. *
  6265. * ssl The SSL/TLS object.
  6266. * keyShareEntry The key share entry object to use to calculate shared secret.
  6267. * returns 0 on success and other values indicate failure.
  6268. */
  6269. static int TLSX_KeyShare_Process(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6270. {
  6271. int ret;
  6272. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  6273. ssl->session.namedGroup = (byte)keyShareEntry->group;
  6274. #endif
  6275. /* Use Key Share Data from server. */
  6276. if (keyShareEntry->group & NAMED_DH_MASK)
  6277. ret = TLSX_KeyShare_ProcessDh(ssl, keyShareEntry);
  6278. else if (keyShareEntry->group == WOLFSSL_ECC_X25519)
  6279. ret = TLSX_KeyShare_ProcessX25519(ssl, keyShareEntry);
  6280. else if (keyShareEntry->group == WOLFSSL_ECC_X448)
  6281. ret = TLSX_KeyShare_ProcessX448(ssl, keyShareEntry);
  6282. else
  6283. ret = TLSX_KeyShare_ProcessEcc(ssl, keyShareEntry);
  6284. #ifdef WOLFSSL_DEBUG_TLS
  6285. WOLFSSL_MSG("KE Secret");
  6286. WOLFSSL_BUFFER(ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz);
  6287. #endif
  6288. return ret;
  6289. }
  6290. /* Parse an entry of the KeyShare extension.
  6291. *
  6292. * ssl The SSL/TLS object.
  6293. * input The extension data.
  6294. * length The length of the extension data.
  6295. * kse The new key share entry object.
  6296. * returns a positive number to indicate amount of data parsed and a negative
  6297. * number on error.
  6298. */
  6299. static int TLSX_KeyShareEntry_Parse(WOLFSSL* ssl, byte* input, word16 length,
  6300. KeyShareEntry **kse)
  6301. {
  6302. int ret;
  6303. word16 group;
  6304. word16 keLen;
  6305. int offset = 0;
  6306. byte* ke;
  6307. if (length < OPAQUE16_LEN + OPAQUE16_LEN)
  6308. return BUFFER_ERROR;
  6309. /* Named group */
  6310. ato16(&input[offset], &group);
  6311. offset += OPAQUE16_LEN;
  6312. /* Key exchange data - public key. */
  6313. ato16(&input[offset], &keLen);
  6314. offset += OPAQUE16_LEN;
  6315. if (keLen == 0)
  6316. return INVALID_PARAMETER;
  6317. if (keLen > length - offset)
  6318. return BUFFER_ERROR;
  6319. /* Store a copy in the key share object. */
  6320. ke = (byte*)XMALLOC(keLen, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6321. if (ke == NULL)
  6322. return MEMORY_E;
  6323. XMEMCPY(ke, &input[offset], keLen);
  6324. /* Populate a key share object in the extension. */
  6325. ret = TLSX_KeyShare_Use(ssl, group, keLen, ke, kse);
  6326. if (ret != 0) {
  6327. XFREE(ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6328. return ret;
  6329. }
  6330. /* Total length of the parsed data. */
  6331. return offset + keLen;
  6332. }
  6333. /* Searches the groups sent for the specified named group.
  6334. *
  6335. * ssl SSL/TLS object.
  6336. * name Group name to match.
  6337. * returns 1 when the extension has the group name and 0 otherwise.
  6338. */
  6339. static int TLSX_KeyShare_Find(WOLFSSL* ssl, word16 group)
  6340. {
  6341. TLSX* extension;
  6342. KeyShareEntry* list;
  6343. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  6344. if (extension == NULL) {
  6345. extension = TLSX_Find(ssl->ctx->extensions, TLSX_KEY_SHARE);
  6346. if (extension == NULL)
  6347. return 0;
  6348. }
  6349. list = (KeyShareEntry*)extension->data;
  6350. while (list != NULL) {
  6351. if (list->group == group)
  6352. return 1;
  6353. list = list->next;
  6354. }
  6355. return 0;
  6356. }
  6357. /* Searches the supported groups extension for the specified named group.
  6358. *
  6359. * ssl The SSL/TLS object.
  6360. * name The group name to match.
  6361. * returns 1 when the extension has the group name and 0 otherwise.
  6362. */
  6363. static int TLSX_SupportedGroups_Find(WOLFSSL* ssl, word16 name)
  6364. {
  6365. #ifdef HAVE_SUPPORTED_CURVES
  6366. TLSX* extension;
  6367. SupportedCurve* curve = NULL;
  6368. if ((extension = TLSX_Find(ssl->extensions,
  6369. TLSX_SUPPORTED_GROUPS)) == NULL) {
  6370. if ((extension = TLSX_Find(ssl->ctx->extensions,
  6371. TLSX_SUPPORTED_GROUPS)) == NULL) {
  6372. return 0;
  6373. }
  6374. }
  6375. for (curve = (SupportedCurve*)extension->data; curve; curve = curve->next) {
  6376. if (curve->name == name)
  6377. return 1;
  6378. }
  6379. #endif
  6380. (void)ssl;
  6381. (void)name;
  6382. return 0;
  6383. }
  6384. /* Parse the KeyShare extension.
  6385. * Different formats in different messages.
  6386. *
  6387. * ssl The SSL/TLS object.
  6388. * input The extension data.
  6389. * length The length of the extension data.
  6390. * msgType The type of the message this extension is being parsed from.
  6391. * returns 0 on success and other values indicate failure.
  6392. */
  6393. static int TLSX_KeyShare_Parse(WOLFSSL* ssl, byte* input, word16 length,
  6394. byte msgType)
  6395. {
  6396. int ret;
  6397. KeyShareEntry *keyShareEntry = NULL;
  6398. word16 group;
  6399. if (msgType == client_hello) {
  6400. int offset = 0;
  6401. word16 len;
  6402. TLSX* extension;
  6403. /* Add a KeyShare extension if it doesn't exist. */
  6404. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  6405. if (extension == NULL) {
  6406. /* Push new KeyShare extension. */
  6407. ret = TLSX_Push(&ssl->extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  6408. if (ret != 0)
  6409. return ret;
  6410. }
  6411. if (length < OPAQUE16_LEN)
  6412. return BUFFER_ERROR;
  6413. /* ClientHello contains zero or more key share entries. */
  6414. ato16(input, &len);
  6415. if (len != length - OPAQUE16_LEN)
  6416. return BUFFER_ERROR;
  6417. offset += OPAQUE16_LEN;
  6418. while (offset < (int)length) {
  6419. ret = TLSX_KeyShareEntry_Parse(ssl, &input[offset], length - offset,
  6420. &keyShareEntry);
  6421. if (ret < 0)
  6422. return ret;
  6423. offset += ret;
  6424. }
  6425. ret = 0;
  6426. }
  6427. else if (msgType == server_hello) {
  6428. int len;
  6429. if (length < OPAQUE16_LEN)
  6430. return BUFFER_ERROR;
  6431. /* The data is the named group the server wants to use. */
  6432. ato16(input, &group);
  6433. /* Check the selected group was supported by ClientHello extensions. */
  6434. if (!TLSX_SupportedGroups_Find(ssl, group))
  6435. return BAD_KEY_SHARE_DATA;
  6436. /* Check if the group was sent. */
  6437. if (!TLSX_KeyShare_Find(ssl, group))
  6438. return BAD_KEY_SHARE_DATA;
  6439. /* ServerHello contains one key share entry. */
  6440. len = TLSX_KeyShareEntry_Parse(ssl, input, length, &keyShareEntry);
  6441. if (len != (int)length)
  6442. return BUFFER_ERROR;
  6443. /* Not in list sent if there isn't a private key. */
  6444. if (keyShareEntry == NULL || keyShareEntry->key == NULL)
  6445. return BAD_KEY_SHARE_DATA;
  6446. /* Process the entry to calculate the secret. */
  6447. ret = TLSX_KeyShare_Process(ssl, keyShareEntry);
  6448. if (ret == 0)
  6449. ssl->session.namedGroup = ssl->namedGroup = group;
  6450. }
  6451. else if (msgType == hello_retry_request) {
  6452. if (length != OPAQUE16_LEN)
  6453. return BUFFER_ERROR;
  6454. /* The data is the named group the server wants to use. */
  6455. ato16(input, &group);
  6456. /* Check the selected group was supported by ClientHello extensions. */
  6457. if (!TLSX_SupportedGroups_Find(ssl, group))
  6458. return BAD_KEY_SHARE_DATA;
  6459. /* Check if the group was sent. */
  6460. if (TLSX_KeyShare_Find(ssl, group))
  6461. return BAD_KEY_SHARE_DATA;
  6462. /* Clear out unusable key shares. */
  6463. ret = TLSX_KeyShare_Empty(ssl);
  6464. if (ret != 0)
  6465. return ret;
  6466. /* Try to use the server's group. */
  6467. ret = TLSX_KeyShare_Use(ssl, group, 0, NULL, NULL);
  6468. }
  6469. else {
  6470. /* Not a message type that is allowed to have this extension. */
  6471. return SANITY_MSG_E;
  6472. }
  6473. return ret;
  6474. }
  6475. /* Create a new key share entry and put it into the list.
  6476. *
  6477. * list The linked list of key share entries.
  6478. * group The named group.
  6479. * heap The memory to allocate with.
  6480. * keyShareEntry The new key share entry object.
  6481. * returns 0 on success and other values indicate failure.
  6482. */
  6483. static int TLSX_KeyShare_New(KeyShareEntry** list, int group, void *heap,
  6484. KeyShareEntry** keyShareEntry)
  6485. {
  6486. KeyShareEntry* kse;
  6487. KeyShareEntry** next;
  6488. kse = (KeyShareEntry*)XMALLOC(sizeof(KeyShareEntry), heap,
  6489. DYNAMIC_TYPE_TLSX);
  6490. if (kse == NULL)
  6491. return MEMORY_E;
  6492. XMEMSET(kse, 0, sizeof(*kse));
  6493. kse->group = (word16)group;
  6494. /* Add it to the back and maintain the links. */
  6495. while (*list != NULL) {
  6496. /* Assign to temporary to work around compiler bug found by customer. */
  6497. next = &((*list)->next);
  6498. list = next;
  6499. }
  6500. *list = kse;
  6501. *keyShareEntry = kse;
  6502. (void)heap;
  6503. return 0;
  6504. }
  6505. /* Use the data to create a new key share object in the extensions.
  6506. *
  6507. * ssl The SSL/TLS object.
  6508. * group The named group.
  6509. * len The length of the public key data.
  6510. * data The public key data.
  6511. * kse The new key share entry object.
  6512. * returns 0 on success and other values indicate failure.
  6513. */
  6514. int TLSX_KeyShare_Use(WOLFSSL* ssl, word16 group, word16 len, byte* data,
  6515. KeyShareEntry **kse)
  6516. {
  6517. int ret = 0;
  6518. TLSX* extension;
  6519. KeyShareEntry* keyShareEntry = NULL;
  6520. /* Find the KeyShare extension if it exists. */
  6521. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  6522. if (extension == NULL) {
  6523. /* Push new KeyShare extension. */
  6524. ret = TLSX_Push(&ssl->extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  6525. if (ret != 0)
  6526. return ret;
  6527. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  6528. if (extension == NULL)
  6529. return MEMORY_E;
  6530. }
  6531. extension->resp = 0;
  6532. /* Try to find the key share entry with this group. */
  6533. keyShareEntry = (KeyShareEntry*)extension->data;
  6534. while (keyShareEntry != NULL) {
  6535. if (keyShareEntry->group == group)
  6536. break;
  6537. keyShareEntry = keyShareEntry->next;
  6538. }
  6539. /* Create a new key share entry if not found. */
  6540. if (keyShareEntry == NULL) {
  6541. ret = TLSX_KeyShare_New((KeyShareEntry**)&extension->data, group,
  6542. ssl->heap, &keyShareEntry);
  6543. if (ret != 0)
  6544. return ret;
  6545. }
  6546. if (data != NULL) {
  6547. if (keyShareEntry->ke != NULL) {
  6548. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6549. }
  6550. keyShareEntry->ke = data;
  6551. keyShareEntry->keLen = len;
  6552. }
  6553. else {
  6554. /* Generate a key pair. */
  6555. ret = TLSX_KeyShare_GenKey(ssl, keyShareEntry);
  6556. if (ret != 0)
  6557. return ret;
  6558. }
  6559. if (kse != NULL)
  6560. *kse = keyShareEntry;
  6561. return 0;
  6562. }
  6563. /* Set an empty Key Share extension.
  6564. *
  6565. * ssl The SSL/TLS object.
  6566. * returns 0 on success and other values indicate failure.
  6567. */
  6568. int TLSX_KeyShare_Empty(WOLFSSL* ssl)
  6569. {
  6570. int ret = 0;
  6571. TLSX* extension;
  6572. /* Find the KeyShare extension if it exists. */
  6573. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  6574. if (extension == NULL) {
  6575. /* Push new KeyShare extension. */
  6576. ret = TLSX_Push(&ssl->extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  6577. }
  6578. else if (extension->data != NULL) {
  6579. TLSX_KeyShare_FreeAll((KeyShareEntry*)extension->data, ssl->heap);
  6580. extension->data = NULL;
  6581. }
  6582. return ret;
  6583. }
  6584. /* Returns whether this group is supported.
  6585. *
  6586. * namedGroup The named group to check.
  6587. * returns 1 when supported or 0 otherwise.
  6588. */
  6589. static int TLSX_KeyShare_IsSupported(int namedGroup)
  6590. {
  6591. switch (namedGroup) {
  6592. #ifdef HAVE_FFDHE_2048
  6593. case WOLFSSL_FFDHE_2048:
  6594. break;
  6595. #endif
  6596. #ifdef HAVE_FFDHE_3072
  6597. case WOLFSSL_FFDHE_3072:
  6598. break;
  6599. #endif
  6600. #ifdef HAVE_FFDHE_4096
  6601. case WOLFSSL_FFDHE_4096:
  6602. break;
  6603. #endif
  6604. #ifdef HAVE_FFDHE_6144
  6605. case WOLFSSL_FFDHE_6144:
  6606. break;
  6607. #endif
  6608. #ifdef HAVE_FFDHE_8192
  6609. case WOLFSSL_FFDHE_8192:
  6610. break;
  6611. #endif
  6612. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  6613. #ifndef NO_ECC_SECP
  6614. case WOLFSSL_ECC_SECP256R1:
  6615. break;
  6616. #endif /* !NO_ECC_SECP */
  6617. #endif
  6618. #ifdef HAVE_CURVE25519
  6619. case WOLFSSL_ECC_X25519:
  6620. break;
  6621. #endif
  6622. #ifdef HAVE_CURVE448
  6623. case WOLFSSL_ECC_X448:
  6624. break;
  6625. #endif
  6626. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  6627. #ifndef NO_ECC_SECP
  6628. case WOLFSSL_ECC_SECP384R1:
  6629. break;
  6630. #endif /* !NO_ECC_SECP */
  6631. #endif
  6632. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  6633. #ifndef NO_ECC_SECP
  6634. case WOLFSSL_ECC_SECP521R1:
  6635. break;
  6636. #endif /* !NO_ECC_SECP */
  6637. #endif
  6638. default:
  6639. return 0;
  6640. }
  6641. return 1;
  6642. }
  6643. /* Examines the application specified group ranking and returns the rank of the
  6644. * group.
  6645. * If no group ranking set then all groups are rank 0 (highest).
  6646. *
  6647. * ssl The SSL/TLS object.
  6648. * group The group to check ranking for.
  6649. * returns ranking from 0 to MAX_GROUP_COUNT-1 or -1 when group not in list.
  6650. */
  6651. static int TLSX_KeyShare_GroupRank(WOLFSSL* ssl, int group)
  6652. {
  6653. byte i;
  6654. if (ssl->numGroups == 0) {
  6655. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  6656. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  6657. #ifndef NO_ECC_SECP
  6658. ssl->group[ssl->numGroups++] = WOLFSSL_ECC_SECP256R1;
  6659. #endif
  6660. #endif
  6661. #endif
  6662. #ifndef HAVE_FIPS
  6663. #if defined(HAVE_CURVE25519)
  6664. ssl->group[ssl->numGroups++] = WOLFSSL_ECC_X25519;
  6665. #endif
  6666. #endif
  6667. #ifndef HAVE_FIPS
  6668. #if defined(HAVE_CURVE448)
  6669. ssl->group[ssl->numGroups++] = WOLFSSL_ECC_X448;
  6670. #endif
  6671. #endif
  6672. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  6673. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  6674. #ifndef NO_ECC_SECP
  6675. ssl->group[ssl->numGroups++] = WOLFSSL_ECC_SECP384R1;
  6676. #endif
  6677. #endif
  6678. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  6679. #ifndef NO_ECC_SECP
  6680. ssl->group[ssl->numGroups++] = WOLFSSL_ECC_SECP521R1;
  6681. #endif
  6682. #endif
  6683. #endif
  6684. /* Add FFDHE supported groups. */
  6685. #ifdef HAVE_FFDHE_2048
  6686. ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_2048;
  6687. #endif
  6688. #ifdef HAVE_FFDHE_3072
  6689. ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_3072;
  6690. #endif
  6691. #ifdef HAVE_FFDHE_4096
  6692. ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_4096;
  6693. #endif
  6694. #ifdef HAVE_FFDHE_6144
  6695. ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_6144;
  6696. #endif
  6697. #ifdef HAVE_FFDHE_8192
  6698. ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_8192;
  6699. #endif
  6700. }
  6701. for (i = 0; i < ssl->numGroups; i++)
  6702. if (ssl->group[i] == (word16)group)
  6703. return i;
  6704. return -1;
  6705. }
  6706. /* Set a key share that is supported by the client into extensions.
  6707. *
  6708. * ssl The SSL/TLS object.
  6709. * returns BAD_KEY_SHARE_DATA if no supported group has a key share,
  6710. * 0 if a supported group has a key share and other values indicate an error.
  6711. */
  6712. static int TLSX_KeyShare_SetSupported(WOLFSSL* ssl)
  6713. {
  6714. int ret;
  6715. #ifdef HAVE_SUPPORTED_CURVES
  6716. TLSX* extension;
  6717. SupportedCurve* curve = NULL;
  6718. SupportedCurve* preferredCurve = NULL;
  6719. int preferredRank = WOLFSSL_MAX_GROUP_COUNT;
  6720. int rank;
  6721. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  6722. if (extension != NULL)
  6723. curve = (SupportedCurve*)extension->data;
  6724. /* Use server's preference order. */
  6725. for (; curve != NULL; curve = curve->next) {
  6726. if (!TLSX_KeyShare_IsSupported(curve->name))
  6727. continue;
  6728. rank = TLSX_KeyShare_GroupRank(ssl, curve->name);
  6729. if (rank == -1)
  6730. continue;
  6731. if (rank < preferredRank) {
  6732. preferredCurve = curve;
  6733. preferredRank = rank;
  6734. }
  6735. }
  6736. curve = preferredCurve;
  6737. if (curve == NULL)
  6738. return BAD_KEY_SHARE_DATA;
  6739. /* Delete the old key share data list. */
  6740. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  6741. if (extension != NULL) {
  6742. TLSX_KeyShare_FreeAll((KeyShareEntry*)extension->data, ssl->heap);
  6743. extension->data = NULL;
  6744. }
  6745. /* Add in the chosen group. */
  6746. ret = TLSX_KeyShare_Use(ssl, curve->name, 0, NULL, NULL);
  6747. if (ret != 0)
  6748. return ret;
  6749. /* Set extension to be in response. */
  6750. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  6751. extension->resp = 1;
  6752. #else
  6753. (void)ssl;
  6754. ret = NOT_COMPILED_IN;
  6755. #endif
  6756. return ret;
  6757. }
  6758. /* Ensure there is a key pair that can be used for key exchange.
  6759. *
  6760. * ssl The SSL/TLS object.
  6761. * returns 0 on success and other values indicate failure.
  6762. */
  6763. int TLSX_KeyShare_Establish(WOLFSSL *ssl)
  6764. {
  6765. int ret;
  6766. TLSX* extension;
  6767. KeyShareEntry* clientKSE = NULL;
  6768. KeyShareEntry* serverKSE;
  6769. KeyShareEntry* list = NULL;
  6770. KeyShareEntry* preferredKSE = NULL;
  6771. int preferredRank = WOLFSSL_MAX_GROUP_COUNT;
  6772. int rank;
  6773. /* Find the KeyShare extension if it exists. */
  6774. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  6775. if (extension != NULL)
  6776. list = (KeyShareEntry*)extension->data;
  6777. if (extension && extension->resp == 1)
  6778. return 0;
  6779. /* Use server's preference order. */
  6780. for (clientKSE = list; clientKSE != NULL; clientKSE = clientKSE->next) {
  6781. if (clientKSE->ke == NULL)
  6782. continue;
  6783. /* Check consistency now - extensions in any order. */
  6784. if (!TLSX_SupportedGroups_Find(ssl, clientKSE->group))
  6785. return BAD_KEY_SHARE_DATA;
  6786. if ((clientKSE->group & NAMED_DH_MASK) == 0) {
  6787. /* Check max value supported. */
  6788. if (clientKSE->group > WOLFSSL_ECC_MAX) {
  6789. continue;
  6790. }
  6791. #ifdef OPENSSL_EXTRA
  6792. /* Check if server supports group. */
  6793. if (ssl->ctx->disabledCurves & ((word32)1 << clientKSE->group))
  6794. continue;
  6795. #endif
  6796. }
  6797. if (!TLSX_KeyShare_IsSupported(clientKSE->group))
  6798. continue;
  6799. rank = TLSX_KeyShare_GroupRank(ssl, clientKSE->group);
  6800. if (rank == -1)
  6801. continue;
  6802. if (rank < preferredRank) {
  6803. preferredKSE = clientKSE;
  6804. preferredRank = rank;
  6805. }
  6806. }
  6807. clientKSE = preferredKSE;
  6808. /* No supported group found - send HelloRetryRequest. */
  6809. if (clientKSE == NULL) {
  6810. ret = TLSX_KeyShare_SetSupported(ssl);
  6811. /* Return KEY_SHARE_ERROR to indicate HelloRetryRequest required. */
  6812. if (ret == 0)
  6813. return KEY_SHARE_ERROR;
  6814. return ret;
  6815. }
  6816. list = NULL;
  6817. /* Generate a new key pair. */
  6818. ret = TLSX_KeyShare_New(&list, clientKSE->group, ssl->heap, &serverKSE);
  6819. if (ret != 0)
  6820. return ret;
  6821. if (clientKSE->key == NULL) {
  6822. ret = TLSX_KeyShare_GenKey(ssl, serverKSE);
  6823. if (ret != 0)
  6824. return ret;
  6825. }
  6826. else {
  6827. serverKSE->key = clientKSE->key;
  6828. serverKSE->keyLen = clientKSE->keyLen;
  6829. serverKSE->pubKey = clientKSE->pubKey;
  6830. serverKSE->pubKeyLen = clientKSE->pubKeyLen;
  6831. clientKSE->key = NULL;
  6832. clientKSE->pubKey = NULL;
  6833. }
  6834. serverKSE->ke = clientKSE->ke;
  6835. serverKSE->keLen = clientKSE->keLen;
  6836. clientKSE->ke = NULL;
  6837. clientKSE->keLen = 0;
  6838. TLSX_KeyShare_FreeAll((KeyShareEntry*)extension->data, ssl->heap);
  6839. extension->data = (void *)serverKSE;
  6840. extension->resp = 1;
  6841. return 0;
  6842. }
  6843. /* Derive the shared secret of the key exchange.
  6844. *
  6845. * ssl The SSL/TLS object.
  6846. * returns 0 on success and other values indicate failure.
  6847. */
  6848. int TLSX_KeyShare_DeriveSecret(WOLFSSL *ssl)
  6849. {
  6850. int ret;
  6851. TLSX* extension;
  6852. KeyShareEntry* list = NULL;
  6853. /* Find the KeyShare extension if it exists. */
  6854. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  6855. if (extension != NULL)
  6856. list = (KeyShareEntry*)extension->data;
  6857. if (list == NULL)
  6858. return KEY_SHARE_ERROR;
  6859. /* Calculate secret. */
  6860. ret = TLSX_KeyShare_Process(ssl, list);
  6861. if (ret != 0)
  6862. return ret;
  6863. return ret;
  6864. }
  6865. #define KS_FREE_ALL TLSX_KeyShare_FreeAll
  6866. #define KS_GET_SIZE TLSX_KeyShare_GetSize
  6867. #define KS_WRITE TLSX_KeyShare_Write
  6868. #define KS_PARSE TLSX_KeyShare_Parse
  6869. #else
  6870. #define KS_FREE_ALL(a, b)
  6871. #define KS_GET_SIZE(a, b) 0
  6872. #define KS_WRITE(a, b, c) 0
  6873. #define KS_PARSE(a, b, c, d) 0
  6874. #endif /* WOLFSSL_TLS13 */
  6875. /******************************************************************************/
  6876. /* Pre-Shared Key */
  6877. /******************************************************************************/
  6878. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  6879. /* Free the pre-shared key dynamic data.
  6880. *
  6881. * list The linked list of key share entry objects.
  6882. * heap The heap used for allocation.
  6883. */
  6884. static void TLSX_PreSharedKey_FreeAll(PreSharedKey* list, void* heap)
  6885. {
  6886. PreSharedKey* current;
  6887. while ((current = list) != NULL) {
  6888. list = current->next;
  6889. XFREE(current->identity, heap, DYNAMIC_TYPE_TLSX);
  6890. XFREE(current, heap, DYNAMIC_TYPE_TLSX);
  6891. }
  6892. (void)heap;
  6893. }
  6894. /* Get the size of the encoded pre shared key extension.
  6895. *
  6896. * list The linked list of pre-shared key extensions.
  6897. * msgType The type of the message this extension is being written into.
  6898. * returns the number of bytes of the encoded pre-shared key extension or
  6899. * SANITY_MSG_E to indicate invalid message type.
  6900. */
  6901. static int TLSX_PreSharedKey_GetSize(PreSharedKey* list, byte msgType,
  6902. word16* pSz)
  6903. {
  6904. if (msgType == client_hello) {
  6905. /* Length of identities + Length of binders. */
  6906. word16 len = OPAQUE16_LEN + OPAQUE16_LEN;
  6907. while (list != NULL) {
  6908. /* Each entry has: identity, ticket age and binder. */
  6909. len += OPAQUE16_LEN + list->identityLen + OPAQUE32_LEN +
  6910. OPAQUE8_LEN + list->binderLen;
  6911. list = list->next;
  6912. }
  6913. *pSz += len;
  6914. return 0;
  6915. }
  6916. if (msgType == server_hello) {
  6917. *pSz += OPAQUE16_LEN;
  6918. return 0;
  6919. }
  6920. return SANITY_MSG_E;
  6921. }
  6922. /* The number of bytes to be written for the binders.
  6923. *
  6924. * list The linked list of pre-shared key extensions.
  6925. * msgType The type of the message this extension is being written into.
  6926. * returns the number of bytes of the encoded pre-shared key extension or
  6927. * SANITY_MSG_E to indicate invalid message type.
  6928. */
  6929. int TLSX_PreSharedKey_GetSizeBinders(PreSharedKey* list, byte msgType,
  6930. word16* pSz)
  6931. {
  6932. word16 len;
  6933. if (msgType != client_hello)
  6934. return SANITY_MSG_E;
  6935. /* Length of all binders. */
  6936. len = OPAQUE16_LEN;
  6937. while (list != NULL) {
  6938. len += OPAQUE8_LEN + list->binderLen;
  6939. list = list->next;
  6940. }
  6941. *pSz = len;
  6942. return 0;
  6943. }
  6944. /* Writes the pre-shared key extension into the output buffer - binders only.
  6945. * Assumes that the the output buffer is big enough to hold data.
  6946. *
  6947. * list The linked list of key share entries.
  6948. * output The buffer to write into.
  6949. * msgType The type of the message this extension is being written into.
  6950. * returns the number of bytes written into the buffer.
  6951. */
  6952. int TLSX_PreSharedKey_WriteBinders(PreSharedKey* list, byte* output,
  6953. byte msgType, word16* pSz)
  6954. {
  6955. PreSharedKey* current = list;
  6956. word16 idx = 0;
  6957. word16 lenIdx;
  6958. word16 len;
  6959. if (msgType != client_hello)
  6960. return SANITY_MSG_E;
  6961. /* Skip length of all binders. */
  6962. lenIdx = idx;
  6963. idx += OPAQUE16_LEN;
  6964. while (current != NULL) {
  6965. /* Binder data length. */
  6966. output[idx++] = current->binderLen;
  6967. /* Binder data. */
  6968. XMEMCPY(output + idx, current->binder, current->binderLen);
  6969. idx += current->binderLen;
  6970. current = current->next;
  6971. }
  6972. /* Length of the binders. */
  6973. len = idx - lenIdx - OPAQUE16_LEN;
  6974. c16toa(len, output + lenIdx);
  6975. *pSz = idx;
  6976. return 0;
  6977. }
  6978. /* Writes the pre-shared key extension into the output buffer.
  6979. * Assumes that the the output buffer is big enough to hold data.
  6980. *
  6981. * list The linked list of key share entries.
  6982. * output The buffer to write into.
  6983. * msgType The type of the message this extension is being written into.
  6984. * returns the number of bytes written into the buffer.
  6985. */
  6986. static int TLSX_PreSharedKey_Write(PreSharedKey* list, byte* output,
  6987. byte msgType, word16* pSz)
  6988. {
  6989. if (msgType == client_hello) {
  6990. PreSharedKey* current = list;
  6991. word16 idx = 0;
  6992. word16 lenIdx;
  6993. word16 len;
  6994. int ret;
  6995. /* Write identites only. Binders after HMACing over this. */
  6996. lenIdx = idx;
  6997. idx += OPAQUE16_LEN;
  6998. while (current != NULL) {
  6999. /* Identity length */
  7000. c16toa(current->identityLen, output + idx);
  7001. idx += OPAQUE16_LEN;
  7002. /* Identity data */
  7003. XMEMCPY(output + idx, current->identity, current->identityLen);
  7004. idx += current->identityLen;
  7005. /* Obfuscated ticket age. */
  7006. c32toa(current->ticketAge, output + idx);
  7007. idx += OPAQUE32_LEN;
  7008. current = current->next;
  7009. }
  7010. /* Length of the identites. */
  7011. len = idx - lenIdx - OPAQUE16_LEN;
  7012. c16toa(len, output + lenIdx);
  7013. /* Don't include binders here.
  7014. * The binders are based on the hash of all the ClientHello data up to
  7015. * and include the identities written above.
  7016. */
  7017. ret = TLSX_PreSharedKey_GetSizeBinders(list, msgType, &len);
  7018. if (ret < 0)
  7019. return ret;
  7020. *pSz += idx + len;
  7021. }
  7022. else if (msgType == server_hello) {
  7023. word16 i;
  7024. /* Find the index of the chosen identity. */
  7025. for (i=0; list != NULL && !list->chosen; i++)
  7026. list = list->next;
  7027. if (list == NULL)
  7028. return BUILD_MSG_ERROR;
  7029. /* The index of the identity chosen by the server from the list supplied
  7030. * by the client.
  7031. */
  7032. c16toa(i, output);
  7033. *pSz += OPAQUE16_LEN;
  7034. }
  7035. else
  7036. return SANITY_MSG_E;
  7037. return 0;
  7038. }
  7039. /* Parse the pre-shared key extension.
  7040. * Different formats in different messages.
  7041. *
  7042. * ssl The SSL/TLS object.
  7043. * input The extension data.
  7044. * length The length of the extension data.
  7045. * msgType The type of the message this extension is being parsed from.
  7046. * returns 0 on success and other values indicate failure.
  7047. */
  7048. static int TLSX_PreSharedKey_Parse(WOLFSSL* ssl, byte* input, word16 length,
  7049. byte msgType)
  7050. {
  7051. TLSX* extension;
  7052. PreSharedKey* list;
  7053. if (msgType == client_hello) {
  7054. int ret;
  7055. word16 len;
  7056. word16 idx = 0;
  7057. TLSX_Remove(&ssl->extensions, TLSX_PRE_SHARED_KEY, ssl->heap);
  7058. /* Length of identities and of binders. */
  7059. if (length - idx < OPAQUE16_LEN + OPAQUE16_LEN)
  7060. return BUFFER_E;
  7061. /* Length of identities. */
  7062. ato16(input + idx, &len);
  7063. idx += OPAQUE16_LEN;
  7064. if (len < MIN_PSK_ID_LEN || length - idx < len)
  7065. return BUFFER_E;
  7066. /* Create a pre-shared key object for each identity. */
  7067. while (len > 0) {
  7068. byte* identity;
  7069. word16 identityLen;
  7070. word32 age;
  7071. if (len < OPAQUE16_LEN)
  7072. return BUFFER_E;
  7073. /* Length of identity. */
  7074. ato16(input + idx, &identityLen);
  7075. idx += OPAQUE16_LEN;
  7076. if (len < OPAQUE16_LEN + identityLen + OPAQUE32_LEN ||
  7077. identityLen > MAX_PSK_ID_LEN)
  7078. return BUFFER_E;
  7079. /* Cache identity pointer. */
  7080. identity = input + idx;
  7081. idx += identityLen;
  7082. /* Ticket age. */
  7083. ato32(input + idx, &age);
  7084. idx += OPAQUE32_LEN;
  7085. ret = TLSX_PreSharedKey_Use(ssl, identity, identityLen, age, no_mac,
  7086. 0, 0, 1, NULL);
  7087. if (ret != 0)
  7088. return ret;
  7089. /* Done with this identity. */
  7090. len -= OPAQUE16_LEN + identityLen + OPAQUE32_LEN;
  7091. }
  7092. /* Find the list of identities sent to server. */
  7093. extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
  7094. if (extension == NULL)
  7095. return PSK_KEY_ERROR;
  7096. list = (PreSharedKey*)extension->data;
  7097. /* Length of binders. */
  7098. if (idx + OPAQUE16_LEN > length)
  7099. return BUFFER_E;
  7100. ato16(input + idx, &len);
  7101. idx += OPAQUE16_LEN;
  7102. if (len < MIN_PSK_BINDERS_LEN || length - idx < len)
  7103. return BUFFER_E;
  7104. /* Set binder for each identity. */
  7105. while (list != NULL && len > 0) {
  7106. /* Length of binder */
  7107. list->binderLen = input[idx++];
  7108. if (list->binderLen < WC_SHA256_DIGEST_SIZE ||
  7109. list->binderLen > WC_MAX_DIGEST_SIZE)
  7110. return BUFFER_E;
  7111. if (len < OPAQUE8_LEN + list->binderLen)
  7112. return BUFFER_E;
  7113. /* Copy binder into static buffer. */
  7114. XMEMCPY(list->binder, input + idx, list->binderLen);
  7115. idx += list->binderLen;
  7116. /* Done with binder entry. */
  7117. len -= OPAQUE8_LEN + list->binderLen;
  7118. /* Next identity. */
  7119. list = list->next;
  7120. }
  7121. if (list != NULL || len != 0)
  7122. return BUFFER_E;
  7123. return 0;
  7124. }
  7125. if (msgType == server_hello) {
  7126. word16 idx;
  7127. /* Index of identity chosen by server. */
  7128. if (length != OPAQUE16_LEN)
  7129. return BUFFER_E;
  7130. ato16(input, &idx);
  7131. #ifdef WOLFSSL_EARLY_DATA
  7132. ssl->options.pskIdIndex = idx + 1;
  7133. #endif
  7134. /* Find the list of identities sent to server. */
  7135. extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
  7136. if (extension == NULL)
  7137. return PSK_KEY_ERROR;
  7138. list = (PreSharedKey*)extension->data;
  7139. /* Mark the identity as chosen. */
  7140. for (; list != NULL && idx > 0; idx--)
  7141. list = list->next;
  7142. if (list == NULL)
  7143. return PSK_KEY_ERROR;
  7144. list->chosen = 1;
  7145. #ifdef HAVE_SESSION_TICKET
  7146. if (list->resumption) {
  7147. /* Check that the session's details are the same as the server's. */
  7148. if (ssl->options.cipherSuite0 != ssl->session.cipherSuite0 ||
  7149. ssl->options.cipherSuite != ssl->session.cipherSuite ||
  7150. ssl->session.version.major != ssl->ctx->method->version.major ||
  7151. ssl->session.version.minor != ssl->ctx->method->version.minor) {
  7152. return PSK_KEY_ERROR;
  7153. }
  7154. }
  7155. #endif
  7156. return 0;
  7157. }
  7158. return SANITY_MSG_E;
  7159. }
  7160. /* Create a new pre-shared key and put it into the list.
  7161. *
  7162. * list The linked list of pre-shared key.
  7163. * identity The identity.
  7164. * len The length of the identity data.
  7165. * heap The memory to allocate with.
  7166. * preSharedKey The new pre-shared key object.
  7167. * returns 0 on success and other values indicate failure.
  7168. */
  7169. static int TLSX_PreSharedKey_New(PreSharedKey** list, byte* identity,
  7170. word16 len, void *heap,
  7171. PreSharedKey** preSharedKey)
  7172. {
  7173. PreSharedKey* psk;
  7174. PreSharedKey** next;
  7175. psk = (PreSharedKey*)XMALLOC(sizeof(PreSharedKey), heap, DYNAMIC_TYPE_TLSX);
  7176. if (psk == NULL)
  7177. return MEMORY_E;
  7178. XMEMSET(psk, 0, sizeof(*psk));
  7179. /* Make a copy of the identity data. */
  7180. psk->identity = (byte*)XMALLOC(len, heap, DYNAMIC_TYPE_TLSX);
  7181. if (psk->identity == NULL) {
  7182. XFREE(psk, heap, DYNAMIC_TYPE_TLSX);
  7183. return MEMORY_E;
  7184. }
  7185. XMEMCPY(psk->identity, identity, len);
  7186. psk->identityLen = len;
  7187. /* Add it to the end and maintain the links. */
  7188. while (*list != NULL) {
  7189. /* Assign to temporary to work around compiler bug found by customer. */
  7190. next = &((*list)->next);
  7191. list = next;
  7192. }
  7193. *list = psk;
  7194. *preSharedKey = psk;
  7195. (void)heap;
  7196. return 0;
  7197. }
  7198. static WC_INLINE byte GetHmacLength(int hmac)
  7199. {
  7200. switch (hmac) {
  7201. #ifndef NO_SHA256
  7202. case sha256_mac:
  7203. return WC_SHA256_DIGEST_SIZE;
  7204. #endif
  7205. #ifdef WOLFSSL_SHA384
  7206. case sha384_mac:
  7207. return WC_SHA384_DIGEST_SIZE;
  7208. #endif
  7209. #ifdef WOLFSSL_SHA512
  7210. case sha512_mac:
  7211. return WC_SHA512_DIGEST_SIZE;
  7212. #endif
  7213. }
  7214. return 0;
  7215. }
  7216. /* Use the data to create a new pre-shared key object in the extensions.
  7217. *
  7218. * ssl The SSL/TLS object.
  7219. * identity The identity.
  7220. * len The length of the identity data.
  7221. * age The age of the identity.
  7222. * hmac The HMAC algorithm.
  7223. * ciphersuite0 The first byte of the ciphersuite to use.
  7224. * ciphersuite The second byte of the ciphersuite to use.
  7225. * resumption The PSK is for resumption of a session.
  7226. * preSharedKey The new pre-shared key object.
  7227. * returns 0 on success and other values indicate failure.
  7228. */
  7229. int TLSX_PreSharedKey_Use(WOLFSSL* ssl, byte* identity, word16 len, word32 age,
  7230. byte hmac, byte cipherSuite0,
  7231. byte cipherSuite, byte resumption,
  7232. PreSharedKey **preSharedKey)
  7233. {
  7234. int ret = 0;
  7235. TLSX* extension;
  7236. PreSharedKey* psk = NULL;
  7237. /* Find the pre-shared key extension if it exists. */
  7238. extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
  7239. if (extension == NULL) {
  7240. /* Push new pre-shared key extension. */
  7241. ret = TLSX_Push(&ssl->extensions, TLSX_PRE_SHARED_KEY, NULL, ssl->heap);
  7242. if (ret != 0)
  7243. return ret;
  7244. extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
  7245. if (extension == NULL)
  7246. return MEMORY_E;
  7247. }
  7248. /* Try to find the pre-shared key with this identity. */
  7249. psk = (PreSharedKey*)extension->data;
  7250. while (psk != NULL) {
  7251. if ((psk->identityLen == len) &&
  7252. (XMEMCMP(psk->identity, identity, len) == 0)) {
  7253. break;
  7254. }
  7255. psk = psk->next;
  7256. }
  7257. /* Create a new pre-shared key object if not found. */
  7258. if (psk == NULL) {
  7259. ret = TLSX_PreSharedKey_New((PreSharedKey**)&extension->data, identity,
  7260. len, ssl->heap, &psk);
  7261. if (ret != 0)
  7262. return ret;
  7263. }
  7264. /* Update/set age and HMAC algorithm. */
  7265. psk->ticketAge = age;
  7266. psk->hmac = hmac;
  7267. psk->cipherSuite0 = cipherSuite0;
  7268. psk->cipherSuite = cipherSuite;
  7269. psk->resumption = resumption;
  7270. psk->binderLen = GetHmacLength(psk->hmac);
  7271. if (preSharedKey != NULL)
  7272. *preSharedKey = psk;
  7273. return 0;
  7274. }
  7275. #define PSK_FREE_ALL TLSX_PreSharedKey_FreeAll
  7276. #define PSK_GET_SIZE TLSX_PreSharedKey_GetSize
  7277. #define PSK_WRITE TLSX_PreSharedKey_Write
  7278. #define PSK_PARSE TLSX_PreSharedKey_Parse
  7279. #else
  7280. #define PSK_FREE_ALL(a, b)
  7281. #define PSK_GET_SIZE(a, b, c) 0
  7282. #define PSK_WRITE(a, b, c, d) 0
  7283. #define PSK_PARSE(a, b, c, d) 0
  7284. #endif
  7285. /******************************************************************************/
  7286. /* PSK Key Exchange Modes */
  7287. /******************************************************************************/
  7288. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  7289. /* Get the size of the encoded PSK KE modes extension.
  7290. * Only in ClientHello.
  7291. *
  7292. * modes The PSK KE mode bit string.
  7293. * msgType The type of the message this extension is being written into.
  7294. * returns the number of bytes of the encoded PSK KE mode extension.
  7295. */
  7296. static int TLSX_PskKeModes_GetSize(byte modes, byte msgType, word16* pSz)
  7297. {
  7298. if (msgType == client_hello) {
  7299. /* Format: Len | Modes* */
  7300. word16 len = OPAQUE8_LEN;
  7301. /* Check whether each possible mode is to be written. */
  7302. if (modes & (1 << PSK_KE))
  7303. len += OPAQUE8_LEN;
  7304. if (modes & (1 << PSK_DHE_KE))
  7305. len += OPAQUE8_LEN;
  7306. *pSz += len;
  7307. return 0;
  7308. }
  7309. return SANITY_MSG_E;
  7310. }
  7311. /* Writes the PSK KE modes extension into the output buffer.
  7312. * Assumes that the the output buffer is big enough to hold data.
  7313. * Only in ClientHello.
  7314. *
  7315. * modes The PSK KE mode bit string.
  7316. * output The buffer to write into.
  7317. * msgType The type of the message this extension is being written into.
  7318. * returns the number of bytes written into the buffer.
  7319. */
  7320. static int TLSX_PskKeModes_Write(byte modes, byte* output, byte msgType,
  7321. word16* pSz)
  7322. {
  7323. if (msgType == client_hello) {
  7324. /* Format: Len | Modes* */
  7325. int idx = OPAQUE8_LEN;
  7326. /* Write out each possible mode. */
  7327. if (modes & (1 << PSK_KE))
  7328. output[idx++] = PSK_KE;
  7329. if (modes & (1 << PSK_DHE_KE))
  7330. output[idx++] = PSK_DHE_KE;
  7331. /* Write out length of mode list. */
  7332. output[0] = idx - OPAQUE8_LEN;
  7333. *pSz += idx;
  7334. return 0;
  7335. }
  7336. return SANITY_MSG_E;
  7337. }
  7338. /* Parse the PSK KE modes extension.
  7339. * Only in ClientHello.
  7340. *
  7341. * ssl The SSL/TLS object.
  7342. * input The extension data.
  7343. * length The length of the extension data.
  7344. * msgType The type of the message this extension is being parsed from.
  7345. * returns 0 on success and other values indicate failure.
  7346. */
  7347. static int TLSX_PskKeModes_Parse(WOLFSSL* ssl, byte* input, word16 length,
  7348. byte msgType)
  7349. {
  7350. int ret;
  7351. if (msgType == client_hello) {
  7352. /* Format: Len | Modes* */
  7353. int idx = 0;
  7354. word16 len;
  7355. byte modes = 0;
  7356. /* Ensure length byte exists. */
  7357. if (length < OPAQUE8_LEN)
  7358. return BUFFER_E;
  7359. /* Get length of mode list and ensure that is the only data. */
  7360. len = input[0];
  7361. if (length - OPAQUE8_LEN != len)
  7362. return BUFFER_E;
  7363. idx = OPAQUE8_LEN;
  7364. /* Set a bit for each recognized modes. */
  7365. while (len > 0) {
  7366. /* Ignore unrecognized modes. */
  7367. if (input[idx] <= PSK_DHE_KE)
  7368. modes |= 1 << input[idx];
  7369. idx++;
  7370. len--;
  7371. }
  7372. ret = TLSX_PskKeModes_Use(ssl, modes);
  7373. if (ret != 0)
  7374. return ret;
  7375. return 0;
  7376. }
  7377. return SANITY_MSG_E;
  7378. }
  7379. /* Use the data to create a new PSK Key Exchange Modes object in the extensions.
  7380. *
  7381. * ssl The SSL/TLS object.
  7382. * modes The PSK key exchange modes.
  7383. * returns 0 on success and other values indicate failure.
  7384. */
  7385. int TLSX_PskKeModes_Use(WOLFSSL* ssl, byte modes)
  7386. {
  7387. int ret = 0;
  7388. TLSX* extension;
  7389. /* Find the PSK key exchange modes extension if it exists. */
  7390. extension = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES);
  7391. if (extension == NULL) {
  7392. /* Push new PSK key exchange modes extension. */
  7393. ret = TLSX_Push(&ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES, NULL,
  7394. ssl->heap);
  7395. if (ret != 0)
  7396. return ret;
  7397. extension = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES);
  7398. if (extension == NULL)
  7399. return MEMORY_E;
  7400. }
  7401. extension->val = modes;
  7402. return 0;
  7403. }
  7404. #define PKM_GET_SIZE TLSX_PskKeModes_GetSize
  7405. #define PKM_WRITE TLSX_PskKeModes_Write
  7406. #define PKM_PARSE TLSX_PskKeModes_Parse
  7407. #else
  7408. #define PKM_GET_SIZE(a, b, c) 0
  7409. #define PKM_WRITE(a, b, c, d) 0
  7410. #define PKM_PARSE(a, b, c, d) 0
  7411. #endif
  7412. /******************************************************************************/
  7413. /* Post-Handshake Authentication */
  7414. /******************************************************************************/
  7415. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  7416. /* Get the size of the encoded Post-Handshake Authentication extension.
  7417. * Only in ClientHello.
  7418. *
  7419. * msgType The type of the message this extension is being written into.
  7420. * returns the number of bytes of the encoded Post-Handshake Authentication
  7421. * extension.
  7422. */
  7423. static int TLSX_PostHandAuth_GetSize(byte msgType, word16* pSz)
  7424. {
  7425. if (msgType == client_hello) {
  7426. *pSz += 0;
  7427. return 0;
  7428. }
  7429. return SANITY_MSG_E;
  7430. }
  7431. /* Writes the Post-Handshake Authentication extension into the output buffer.
  7432. * Assumes that the the output buffer is big enough to hold data.
  7433. * Only in ClientHello.
  7434. *
  7435. * output The buffer to write into.
  7436. * msgType The type of the message this extension is being written into.
  7437. * returns the number of bytes written into the buffer.
  7438. */
  7439. static int TLSX_PostHandAuth_Write(byte* output, byte msgType, word16* pSz)
  7440. {
  7441. (void)output;
  7442. if (msgType == client_hello) {
  7443. *pSz += 0;
  7444. return 0;
  7445. }
  7446. return SANITY_MSG_E;
  7447. }
  7448. /* Parse the Post-Handshake Authentication extension.
  7449. * Only in ClientHello.
  7450. *
  7451. * ssl The SSL/TLS object.
  7452. * input The extension data.
  7453. * length The length of the extension data.
  7454. * msgType The type of the message this extension is being parsed from.
  7455. * returns 0 on success and other values indicate failure.
  7456. */
  7457. static int TLSX_PostHandAuth_Parse(WOLFSSL* ssl, byte* input, word16 length,
  7458. byte msgType)
  7459. {
  7460. (void)input;
  7461. if (msgType == client_hello) {
  7462. /* Ensure extension is empty. */
  7463. if (length != 0)
  7464. return BUFFER_E;
  7465. ssl->options.postHandshakeAuth = 1;
  7466. return 0;
  7467. }
  7468. return SANITY_MSG_E;
  7469. }
  7470. /* Create a new Post-handshake authentication object in the extensions.
  7471. *
  7472. * ssl The SSL/TLS object.
  7473. * returns 0 on success and other values indicate failure.
  7474. */
  7475. static int TLSX_PostHandAuth_Use(WOLFSSL* ssl)
  7476. {
  7477. int ret = 0;
  7478. TLSX* extension;
  7479. /* Find the PSK key exchange modes extension if it exists. */
  7480. extension = TLSX_Find(ssl->extensions, TLSX_POST_HANDSHAKE_AUTH);
  7481. if (extension == NULL) {
  7482. /* Push new Post-handshake Authentication extension. */
  7483. ret = TLSX_Push(&ssl->extensions, TLSX_POST_HANDSHAKE_AUTH, NULL,
  7484. ssl->heap);
  7485. if (ret != 0)
  7486. return ret;
  7487. }
  7488. return 0;
  7489. }
  7490. #define PHA_GET_SIZE TLSX_PostHandAuth_GetSize
  7491. #define PHA_WRITE TLSX_PostHandAuth_Write
  7492. #define PHA_PARSE TLSX_PostHandAuth_Parse
  7493. #else
  7494. #define PHA_GET_SIZE(a, b) 0
  7495. #define PHA_WRITE(a, b, c) 0
  7496. #define PHA_PARSE(a, b, c, d) 0
  7497. #endif
  7498. /******************************************************************************/
  7499. /* Early Data Indication */
  7500. /******************************************************************************/
  7501. #ifdef WOLFSSL_EARLY_DATA
  7502. /* Get the size of the encoded Early Data Indication extension.
  7503. * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
  7504. *
  7505. * msgType The type of the message this extension is being written into.
  7506. * returns the number of bytes of the encoded Early Data Indication extension.
  7507. */
  7508. static int TLSX_EarlyData_GetSize(byte msgType, word16* pSz)
  7509. {
  7510. int ret = 0;
  7511. if (msgType == client_hello || msgType == encrypted_extensions)
  7512. *pSz += 0;
  7513. else if (msgType == session_ticket)
  7514. *pSz += OPAQUE32_LEN;
  7515. else
  7516. ret = SANITY_MSG_E;
  7517. return ret;
  7518. }
  7519. /* Writes the Early Data Indicator extension into the output buffer.
  7520. * Assumes that the the output buffer is big enough to hold data.
  7521. * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
  7522. *
  7523. * max The maximum early data size.
  7524. * output The buffer to write into.
  7525. * msgType The type of the message this extension is being written into.
  7526. * returns the number of bytes written into the buffer.
  7527. */
  7528. static int TLSX_EarlyData_Write(word32 max, byte* output, byte msgType,
  7529. word16* pSz)
  7530. {
  7531. if (msgType == client_hello || msgType == encrypted_extensions)
  7532. return 0;
  7533. else if (msgType == session_ticket) {
  7534. c32toa(max, output);
  7535. *pSz += OPAQUE32_LEN;
  7536. return 0;
  7537. }
  7538. return SANITY_MSG_E;
  7539. }
  7540. /* Parse the Early Data Indicator extension.
  7541. * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
  7542. *
  7543. * ssl The SSL/TLS object.
  7544. * input The extension data.
  7545. * length The length of the extension data.
  7546. * msgType The type of the message this extension is being parsed from.
  7547. * returns 0 on success and other values indicate failure.
  7548. */
  7549. static int TLSX_EarlyData_Parse(WOLFSSL* ssl, byte* input, word16 length,
  7550. byte msgType)
  7551. {
  7552. if (msgType == client_hello) {
  7553. if (length != 0)
  7554. return BUFFER_E;
  7555. if (ssl->earlyData == expecting_early_data)
  7556. return TLSX_EarlyData_Use(ssl, 0);
  7557. ssl->earlyData = early_data_ext;
  7558. return 0;
  7559. }
  7560. if (msgType == encrypted_extensions) {
  7561. if (length != 0)
  7562. return BUFFER_E;
  7563. /* Ensure the index of PSK identity chosen by server is 0.
  7564. * Index is plus one to handle 'not set' value of 0.
  7565. */
  7566. if (ssl->options.pskIdIndex != 1)
  7567. return PSK_KEY_ERROR;
  7568. return TLSX_EarlyData_Use(ssl, 1);
  7569. }
  7570. if (msgType == session_ticket) {
  7571. word32 maxSz;
  7572. if (length != OPAQUE32_LEN)
  7573. return BUFFER_E;
  7574. ato32(input, &maxSz);
  7575. ssl->session.maxEarlyDataSz = maxSz;
  7576. return 0;
  7577. }
  7578. return SANITY_MSG_E;
  7579. }
  7580. /* Use the data to create a new Early Data object in the extensions.
  7581. *
  7582. * ssl The SSL/TLS object.
  7583. * max The maximum early data size.
  7584. * returns 0 on success and other values indicate failure.
  7585. */
  7586. int TLSX_EarlyData_Use(WOLFSSL* ssl, word32 max)
  7587. {
  7588. int ret = 0;
  7589. TLSX* extension;
  7590. /* Find the early data extension if it exists. */
  7591. extension = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA);
  7592. if (extension == NULL) {
  7593. /* Push new early data extension. */
  7594. ret = TLSX_Push(&ssl->extensions, TLSX_EARLY_DATA, NULL, ssl->heap);
  7595. if (ret != 0)
  7596. return ret;
  7597. extension = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA);
  7598. if (extension == NULL)
  7599. return MEMORY_E;
  7600. }
  7601. extension->resp = 1;
  7602. extension->val = max;
  7603. return 0;
  7604. }
  7605. #define EDI_GET_SIZE TLSX_EarlyData_GetSize
  7606. #define EDI_WRITE TLSX_EarlyData_Write
  7607. #define EDI_PARSE TLSX_EarlyData_Parse
  7608. #else
  7609. #define EDI_GET_SIZE(a, b) 0
  7610. #define EDI_WRITE(a, b, c, d) 0
  7611. #define EDI_PARSE(a, b, c, d) 0
  7612. #endif
  7613. /******************************************************************************/
  7614. /* TLS Extensions Framework */
  7615. /******************************************************************************/
  7616. /** Finds an extension in the provided list. */
  7617. TLSX* TLSX_Find(TLSX* list, TLSX_Type type)
  7618. {
  7619. TLSX* extension = list;
  7620. while (extension && extension->type != type)
  7621. extension = extension->next;
  7622. return extension;
  7623. }
  7624. /** Remove an extension. */
  7625. void TLSX_Remove(TLSX** list, TLSX_Type type, void* heap)
  7626. {
  7627. TLSX* extension = *list;
  7628. TLSX** next = list;
  7629. while (extension && extension->type != type) {
  7630. next = &extension->next;
  7631. extension = extension->next;
  7632. }
  7633. if (extension) {
  7634. *next = extension->next;
  7635. extension->next = NULL;
  7636. TLSX_FreeAll(extension, heap);
  7637. }
  7638. }
  7639. /** Releases all extensions in the provided list. */
  7640. void TLSX_FreeAll(TLSX* list, void* heap)
  7641. {
  7642. TLSX* extension;
  7643. while ((extension = list)) {
  7644. list = extension->next;
  7645. switch (extension->type) {
  7646. case TLSX_SERVER_NAME:
  7647. SNI_FREE_ALL((SNI*)extension->data, heap);
  7648. break;
  7649. case TLSX_TRUSTED_CA_KEYS:
  7650. TCA_FREE_ALL((TCA*)extension->data, heap);
  7651. break;
  7652. case TLSX_MAX_FRAGMENT_LENGTH:
  7653. MFL_FREE_ALL(extension->data, heap);
  7654. break;
  7655. case TLSX_EXTENDED_MASTER_SECRET:
  7656. case TLSX_TRUNCATED_HMAC:
  7657. /* Nothing to do. */
  7658. break;
  7659. case TLSX_SUPPORTED_GROUPS:
  7660. EC_FREE_ALL((SupportedCurve*)extension->data, heap);
  7661. break;
  7662. case TLSX_EC_POINT_FORMATS:
  7663. PF_FREE_ALL((PointFormat*)extension->data, heap);
  7664. break;
  7665. case TLSX_STATUS_REQUEST:
  7666. CSR_FREE_ALL((CertificateStatusRequest*)extension->data, heap);
  7667. break;
  7668. case TLSX_STATUS_REQUEST_V2:
  7669. CSR2_FREE_ALL((CertificateStatusRequestItemV2*)extension->data,
  7670. heap);
  7671. break;
  7672. case TLSX_RENEGOTIATION_INFO:
  7673. SCR_FREE_ALL(extension->data, heap);
  7674. break;
  7675. case TLSX_SESSION_TICKET:
  7676. WOLF_STK_FREE(extension->data, heap);
  7677. break;
  7678. case TLSX_QUANTUM_SAFE_HYBRID:
  7679. QSH_FREE_ALL((QSHScheme*)extension->data, heap);
  7680. break;
  7681. case TLSX_APPLICATION_LAYER_PROTOCOL:
  7682. ALPN_FREE_ALL((ALPN*)extension->data, heap);
  7683. break;
  7684. #if !defined(WOLFSSL_NO_SIGALG)
  7685. case TLSX_SIGNATURE_ALGORITHMS:
  7686. break;
  7687. #endif
  7688. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  7689. case TLSX_ENCRYPT_THEN_MAC:
  7690. break;
  7691. #endif
  7692. #ifdef WOLFSSL_TLS13
  7693. case TLSX_SUPPORTED_VERSIONS:
  7694. break;
  7695. case TLSX_COOKIE:
  7696. CKE_FREE_ALL((Cookie*)extension->data, heap);
  7697. break;
  7698. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  7699. case TLSX_PRE_SHARED_KEY:
  7700. PSK_FREE_ALL((PreSharedKey*)extension->data, heap);
  7701. break;
  7702. case TLSX_PSK_KEY_EXCHANGE_MODES:
  7703. break;
  7704. #endif
  7705. #ifdef WOLFSSL_EARLY_DATA
  7706. case TLSX_EARLY_DATA:
  7707. break;
  7708. #endif
  7709. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  7710. case TLSX_POST_HANDSHAKE_AUTH:
  7711. break;
  7712. #endif
  7713. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  7714. break;
  7715. case TLSX_KEY_SHARE:
  7716. KS_FREE_ALL((KeyShareEntry*)extension->data, heap);
  7717. break;
  7718. #endif
  7719. }
  7720. XFREE(extension, heap, DYNAMIC_TYPE_TLSX);
  7721. }
  7722. (void)heap;
  7723. }
  7724. /** Checks if the tls extensions are supported based on the protocol version. */
  7725. int TLSX_SupportExtensions(WOLFSSL* ssl) {
  7726. return ssl && (IsTLS(ssl) || ssl->version.major == DTLS_MAJOR);
  7727. }
  7728. /** Tells the buffered size of the extensions in a list. */
  7729. static int TLSX_GetSize(TLSX* list, byte* semaphore, byte msgType,
  7730. word16* pLength)
  7731. {
  7732. int ret = 0;
  7733. TLSX* extension;
  7734. word16 length = 0;
  7735. byte isRequest = (msgType == client_hello ||
  7736. msgType == certificate_request);
  7737. while ((extension = list)) {
  7738. list = extension->next;
  7739. /* only extensions marked as response are sent back to the client. */
  7740. if (!isRequest && !extension->resp)
  7741. continue; /* skip! */
  7742. /* ssl level extensions are expected to override ctx level ones. */
  7743. if (!IS_OFF(semaphore, TLSX_ToSemaphore(extension->type)))
  7744. continue; /* skip! */
  7745. /* extension type + extension data length. */
  7746. length += HELLO_EXT_TYPE_SZ + OPAQUE16_LEN;
  7747. switch (extension->type) {
  7748. case TLSX_SERVER_NAME:
  7749. /* SNI only sends the name on the request. */
  7750. if (isRequest)
  7751. length += SNI_GET_SIZE((SNI*)extension->data);
  7752. break;
  7753. case TLSX_TRUSTED_CA_KEYS:
  7754. /* TCA only sends the list on the request. */
  7755. if (isRequest)
  7756. length += TCA_GET_SIZE((TCA*)extension->data);
  7757. break;
  7758. case TLSX_MAX_FRAGMENT_LENGTH:
  7759. length += MFL_GET_SIZE(extension->data);
  7760. break;
  7761. case TLSX_EXTENDED_MASTER_SECRET:
  7762. case TLSX_TRUNCATED_HMAC:
  7763. /* always empty. */
  7764. break;
  7765. case TLSX_SUPPORTED_GROUPS:
  7766. length += EC_GET_SIZE((SupportedCurve*)extension->data);
  7767. break;
  7768. case TLSX_EC_POINT_FORMATS:
  7769. length += PF_GET_SIZE((PointFormat*)extension->data);
  7770. break;
  7771. case TLSX_STATUS_REQUEST:
  7772. length += CSR_GET_SIZE(
  7773. (CertificateStatusRequest*)extension->data, isRequest);
  7774. break;
  7775. case TLSX_STATUS_REQUEST_V2:
  7776. length += CSR2_GET_SIZE(
  7777. (CertificateStatusRequestItemV2*)extension->data,
  7778. isRequest);
  7779. break;
  7780. case TLSX_RENEGOTIATION_INFO:
  7781. length += SCR_GET_SIZE((SecureRenegotiation*)extension->data,
  7782. isRequest);
  7783. break;
  7784. case TLSX_SESSION_TICKET:
  7785. length += WOLF_STK_GET_SIZE((SessionTicket*)extension->data,
  7786. isRequest);
  7787. break;
  7788. case TLSX_QUANTUM_SAFE_HYBRID:
  7789. length += QSH_GET_SIZE((QSHScheme*)extension->data, isRequest);
  7790. break;
  7791. case TLSX_APPLICATION_LAYER_PROTOCOL:
  7792. length += ALPN_GET_SIZE((ALPN*)extension->data);
  7793. break;
  7794. #if !defined(WOLFSSL_NO_SIGALG)
  7795. case TLSX_SIGNATURE_ALGORITHMS:
  7796. length += SA_GET_SIZE(extension->data);
  7797. break;
  7798. #endif
  7799. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  7800. case TLSX_ENCRYPT_THEN_MAC:
  7801. ret = ETM_GET_SIZE(msgType, &length);
  7802. break;
  7803. #endif /* HAVE_ENCRYPT_THEN_MAC */
  7804. #ifdef WOLFSSL_TLS13
  7805. case TLSX_SUPPORTED_VERSIONS:
  7806. ret = SV_GET_SIZE(extension->data, msgType, &length);
  7807. break;
  7808. case TLSX_COOKIE:
  7809. ret = CKE_GET_SIZE((Cookie*)extension->data, msgType, &length);
  7810. break;
  7811. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  7812. case TLSX_PRE_SHARED_KEY:
  7813. ret = PSK_GET_SIZE((PreSharedKey*)extension->data, msgType,
  7814. &length);
  7815. break;
  7816. case TLSX_PSK_KEY_EXCHANGE_MODES:
  7817. ret = PKM_GET_SIZE(extension->val, msgType, &length);
  7818. break;
  7819. #endif
  7820. #ifdef WOLFSSL_EARLY_DATA
  7821. case TLSX_EARLY_DATA:
  7822. ret = EDI_GET_SIZE(msgType, &length);
  7823. break;
  7824. #endif
  7825. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  7826. case TLSX_POST_HANDSHAKE_AUTH:
  7827. ret = PHA_GET_SIZE(msgType, &length);
  7828. break;
  7829. #endif
  7830. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  7831. length += SAC_GET_SIZE(extension->data);
  7832. break;
  7833. case TLSX_KEY_SHARE:
  7834. length += KS_GET_SIZE((KeyShareEntry*)extension->data, msgType);
  7835. break;
  7836. #endif
  7837. }
  7838. /* marks the extension as processed so ctx level */
  7839. /* extensions don't overlap with ssl level ones. */
  7840. TURN_ON(semaphore, TLSX_ToSemaphore(extension->type));
  7841. }
  7842. *pLength += length;
  7843. return ret;
  7844. }
  7845. /** Writes the extensions of a list in a buffer. */
  7846. static int TLSX_Write(TLSX* list, byte* output, byte* semaphore,
  7847. byte msgType, word16* pOffset)
  7848. {
  7849. int ret = 0;
  7850. TLSX* extension;
  7851. word16 offset = 0;
  7852. word16 length_offset = 0;
  7853. byte isRequest = (msgType == client_hello ||
  7854. msgType == certificate_request);
  7855. while ((extension = list)) {
  7856. list = extension->next;
  7857. /* only extensions marked as response are written in a response. */
  7858. if (!isRequest && !extension->resp)
  7859. continue; /* skip! */
  7860. /* ssl level extensions are expected to override ctx level ones. */
  7861. if (!IS_OFF(semaphore, TLSX_ToSemaphore(extension->type)))
  7862. continue; /* skip! */
  7863. /* writes extension type. */
  7864. c16toa(extension->type, output + offset);
  7865. offset += HELLO_EXT_TYPE_SZ + OPAQUE16_LEN;
  7866. length_offset = offset;
  7867. /* extension data should be written internally. */
  7868. switch (extension->type) {
  7869. case TLSX_SERVER_NAME:
  7870. if (isRequest) {
  7871. WOLFSSL_MSG("SNI extension to write");
  7872. offset += SNI_WRITE((SNI*)extension->data, output + offset);
  7873. }
  7874. break;
  7875. case TLSX_TRUSTED_CA_KEYS:
  7876. WOLFSSL_MSG("Trusted CA Indication extension to write");
  7877. if (isRequest) {
  7878. offset += TCA_WRITE((TCA*)extension->data, output + offset);
  7879. }
  7880. break;
  7881. case TLSX_MAX_FRAGMENT_LENGTH:
  7882. WOLFSSL_MSG("Max Fragment Length extension to write");
  7883. offset += MFL_WRITE((byte*)extension->data, output + offset);
  7884. break;
  7885. case TLSX_EXTENDED_MASTER_SECRET:
  7886. WOLFSSL_MSG("Extended Master Secret");
  7887. /* always empty. */
  7888. break;
  7889. case TLSX_TRUNCATED_HMAC:
  7890. WOLFSSL_MSG("Truncated HMAC extension to write");
  7891. /* always empty. */
  7892. break;
  7893. case TLSX_SUPPORTED_GROUPS:
  7894. WOLFSSL_MSG("Supported Groups extension to write");
  7895. offset += EC_WRITE((SupportedCurve*)extension->data,
  7896. output + offset);
  7897. break;
  7898. case TLSX_EC_POINT_FORMATS:
  7899. WOLFSSL_MSG("Point Formats extension to write");
  7900. offset += PF_WRITE((PointFormat*)extension->data,
  7901. output + offset);
  7902. break;
  7903. case TLSX_STATUS_REQUEST:
  7904. WOLFSSL_MSG("Certificate Status Request extension to write");
  7905. offset += CSR_WRITE((CertificateStatusRequest*)extension->data,
  7906. output + offset, isRequest);
  7907. break;
  7908. case TLSX_STATUS_REQUEST_V2:
  7909. WOLFSSL_MSG("Certificate Status Request v2 extension to write");
  7910. offset += CSR2_WRITE(
  7911. (CertificateStatusRequestItemV2*)extension->data,
  7912. output + offset, isRequest);
  7913. break;
  7914. case TLSX_RENEGOTIATION_INFO:
  7915. WOLFSSL_MSG("Secure Renegotiation extension to write");
  7916. offset += SCR_WRITE((SecureRenegotiation*)extension->data,
  7917. output + offset, isRequest);
  7918. break;
  7919. case TLSX_SESSION_TICKET:
  7920. WOLFSSL_MSG("Session Ticket extension to write");
  7921. offset += WOLF_STK_WRITE((SessionTicket*)extension->data,
  7922. output + offset, isRequest);
  7923. break;
  7924. case TLSX_QUANTUM_SAFE_HYBRID:
  7925. WOLFSSL_MSG("Quantum-Safe-Hybrid extension to write");
  7926. if (isRequest) {
  7927. offset += QSH_WRITE((QSHScheme*)extension->data, output + offset);
  7928. }
  7929. offset += QSHPK_WRITE((QSHScheme*)extension->data, output + offset);
  7930. offset += QSH_SERREQ(output + offset, isRequest);
  7931. break;
  7932. case TLSX_APPLICATION_LAYER_PROTOCOL:
  7933. WOLFSSL_MSG("ALPN extension to write");
  7934. offset += ALPN_WRITE((ALPN*)extension->data, output + offset);
  7935. break;
  7936. #if !defined(WOLFSSL_NO_SIGALG)
  7937. case TLSX_SIGNATURE_ALGORITHMS:
  7938. WOLFSSL_MSG("Signature Algorithms extension to write");
  7939. offset += SA_WRITE(extension->data, output + offset);
  7940. break;
  7941. #endif
  7942. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  7943. case TLSX_ENCRYPT_THEN_MAC:
  7944. WOLFSSL_MSG("Encrypt-Then-Mac extension to write");
  7945. ret = ETM_WRITE(extension->data, output, msgType, &offset);
  7946. break;
  7947. #endif /* HAVE_ENCRYPT_THEN_MAC */
  7948. #ifdef WOLFSSL_TLS13
  7949. case TLSX_SUPPORTED_VERSIONS:
  7950. WOLFSSL_MSG("Supported Versions extension to write");
  7951. ret = SV_WRITE(extension->data, output + offset, msgType, &offset);
  7952. break;
  7953. case TLSX_COOKIE:
  7954. WOLFSSL_MSG("Cookie extension to write");
  7955. ret = CKE_WRITE((Cookie*)extension->data, output + offset,
  7956. msgType, &offset);
  7957. break;
  7958. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  7959. case TLSX_PRE_SHARED_KEY:
  7960. WOLFSSL_MSG("Pre-Shared Key extension to write");
  7961. ret = PSK_WRITE((PreSharedKey*)extension->data, output + offset,
  7962. msgType, &offset);
  7963. break;
  7964. case TLSX_PSK_KEY_EXCHANGE_MODES:
  7965. WOLFSSL_MSG("PSK Key Exchange Modes extension to write");
  7966. ret = PKM_WRITE(extension->val, output + offset, msgType,
  7967. &offset);
  7968. break;
  7969. #endif
  7970. #ifdef WOLFSSL_EARLY_DATA
  7971. case TLSX_EARLY_DATA:
  7972. WOLFSSL_MSG("Early Data extension to write");
  7973. ret = EDI_WRITE(extension->val, output + offset, msgType,
  7974. &offset);
  7975. break;
  7976. #endif
  7977. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  7978. case TLSX_POST_HANDSHAKE_AUTH:
  7979. WOLFSSL_MSG("Post-Handshake Authentication extension to write");
  7980. ret = PHA_WRITE(output + offset, msgType, &offset);
  7981. break;
  7982. #endif
  7983. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  7984. WOLFSSL_MSG("Signature Algorithms extension to write");
  7985. offset += SAC_WRITE(extension->data, output + offset);
  7986. break;
  7987. case TLSX_KEY_SHARE:
  7988. WOLFSSL_MSG("Key Share extension to write");
  7989. offset += KS_WRITE((KeyShareEntry*)extension->data,
  7990. output + offset, msgType);
  7991. break;
  7992. #endif
  7993. }
  7994. /* writes extension data length. */
  7995. c16toa(offset - length_offset, output + length_offset - OPAQUE16_LEN);
  7996. /* marks the extension as processed so ctx level */
  7997. /* extensions don't overlap with ssl level ones. */
  7998. TURN_ON(semaphore, TLSX_ToSemaphore(extension->type));
  7999. }
  8000. *pOffset += offset;
  8001. return ret;
  8002. }
  8003. #if defined(HAVE_NTRU) && defined(HAVE_QSH)
  8004. static word32 GetEntropy(unsigned char* out, word32 num_bytes)
  8005. {
  8006. int ret = 0;
  8007. if (gRng == NULL) {
  8008. if ((gRng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL,
  8009. DYNAMIC_TYPE_TLSX)) == NULL)
  8010. return DRBG_OUT_OF_MEMORY;
  8011. wc_InitRng(gRng);
  8012. }
  8013. if (gRngMutex == NULL) {
  8014. if ((gRngMutex = (wolfSSL_Mutex*)XMALLOC(sizeof(wolfSSL_Mutex), NULL,
  8015. DYNAMIC_TYPE_TLSX)) == NULL)
  8016. return DRBG_OUT_OF_MEMORY;
  8017. wc_InitMutex(gRngMutex);
  8018. }
  8019. ret |= wc_LockMutex(gRngMutex);
  8020. ret |= wc_RNG_GenerateBlock(gRng, out, num_bytes);
  8021. ret |= wc_UnLockMutex(gRngMutex);
  8022. if (ret != 0)
  8023. return DRBG_ENTROPY_FAIL;
  8024. return DRBG_OK;
  8025. }
  8026. #endif
  8027. #ifdef HAVE_QSH
  8028. static int TLSX_CreateQSHKey(WOLFSSL* ssl, int type)
  8029. {
  8030. int ret = -1;
  8031. (void)ssl;
  8032. switch (type) {
  8033. #ifdef HAVE_NTRU
  8034. case WOLFSSL_NTRU_EESS439:
  8035. case WOLFSSL_NTRU_EESS593:
  8036. case WOLFSSL_NTRU_EESS743:
  8037. ret = TLSX_CreateNtruKey(ssl, type);
  8038. break;
  8039. #endif
  8040. default:
  8041. WOLFSSL_MSG("Unknown type for creating NTRU key");
  8042. break;
  8043. }
  8044. return ret;
  8045. }
  8046. static int TLSX_AddQSHKey(QSHKey** list, QSHKey* key)
  8047. {
  8048. QSHKey* current;
  8049. if (key == NULL)
  8050. return BAD_FUNC_ARG;
  8051. /* if no public key stored in key then do not add */
  8052. if (key->pub.length == 0 || key->pub.buffer == NULL)
  8053. return 0;
  8054. /* first element to be added to the list */
  8055. current = *list;
  8056. if (current == NULL) {
  8057. *list = key;
  8058. return 0;
  8059. }
  8060. while (current->next) {
  8061. /* can only have one of the key in the list */
  8062. if (current->name == key->name)
  8063. return -1;
  8064. current = (QSHKey*)current->next;
  8065. }
  8066. current->next = (struct QSHKey*)key;
  8067. return 0;
  8068. }
  8069. #if defined(HAVE_NTRU)
  8070. int TLSX_CreateNtruKey(WOLFSSL* ssl, int type)
  8071. {
  8072. int ret = -1;
  8073. int ntruType;
  8074. /* variable declarations for NTRU*/
  8075. QSHKey* temp = NULL;
  8076. byte public_key[1027];
  8077. word16 public_key_len = sizeof(public_key);
  8078. byte private_key[1120];
  8079. word16 private_key_len = sizeof(private_key);
  8080. DRBG_HANDLE drbg;
  8081. if (ssl == NULL)
  8082. return BAD_FUNC_ARG;
  8083. switch (type) {
  8084. case WOLFSSL_NTRU_EESS439:
  8085. ntruType = NTRU_EES439EP1;
  8086. break;
  8087. case WOLFSSL_NTRU_EESS593:
  8088. ntruType = NTRU_EES593EP1;
  8089. break;
  8090. case WOLFSSL_NTRU_EESS743:
  8091. ntruType = NTRU_EES743EP1;
  8092. break;
  8093. default:
  8094. WOLFSSL_MSG("Unknown type for creating NTRU key");
  8095. return -1;
  8096. }
  8097. ret = ntru_crypto_drbg_external_instantiate(GetEntropy, &drbg);
  8098. if (ret != DRBG_OK) {
  8099. WOLFSSL_MSG("NTRU drbg instantiate failed\n");
  8100. return ret;
  8101. }
  8102. if ((ret = ntru_crypto_ntru_encrypt_keygen(drbg, ntruType,
  8103. &public_key_len, NULL, &private_key_len, NULL)) != NTRU_OK)
  8104. return ret;
  8105. if ((ret = ntru_crypto_ntru_encrypt_keygen(drbg, ntruType,
  8106. &public_key_len, public_key, &private_key_len, private_key)) != NTRU_OK)
  8107. return ret;
  8108. ret = ntru_crypto_drbg_uninstantiate(drbg);
  8109. if (ret != NTRU_OK) {
  8110. WOLFSSL_MSG("NTRU drbg uninstantiate failed\n");
  8111. return ret;
  8112. }
  8113. if ((temp = (QSHKey*)XMALLOC(sizeof(QSHKey), ssl->heap,
  8114. DYNAMIC_TYPE_TLSX)) == NULL)
  8115. return MEMORY_E;
  8116. temp->name = type;
  8117. temp->pub.length = public_key_len;
  8118. temp->pub.buffer = (byte*)XMALLOC(public_key_len, ssl->heap,
  8119. DYNAMIC_TYPE_PUBLIC_KEY);
  8120. XMEMCPY(temp->pub.buffer, public_key, public_key_len);
  8121. temp->pri.length = private_key_len;
  8122. temp->pri.buffer = (byte*)XMALLOC(private_key_len, ssl->heap,
  8123. DYNAMIC_TYPE_ARRAYS);
  8124. XMEMCPY(temp->pri.buffer, private_key, private_key_len);
  8125. temp->next = NULL;
  8126. TLSX_AddQSHKey(&ssl->QSH_Key, temp);
  8127. (void)ssl;
  8128. (void)type;
  8129. return ret;
  8130. }
  8131. #endif
  8132. /*
  8133. Used to find a public key from the list of keys
  8134. pubLen length of array
  8135. name input the name of the scheme looking for ie WOLFSSL_NTRU_ESSXXX
  8136. returns a pointer to public key byte* or NULL if not found
  8137. */
  8138. static byte* TLSX_QSHKeyFind_Pub(QSHKey* qsh, word16* pubLen, word16 name)
  8139. {
  8140. QSHKey* current = qsh;
  8141. if (qsh == NULL || pubLen == NULL)
  8142. return NULL;
  8143. *pubLen = 0;
  8144. while(current) {
  8145. if (current->name == name) {
  8146. *pubLen = current->pub.length;
  8147. return current->pub.buffer;
  8148. }
  8149. current = (QSHKey*)current->next;
  8150. }
  8151. return NULL;
  8152. }
  8153. #endif /* HAVE_QSH */
  8154. #if (!defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_TLS13) && \
  8155. !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)) || \
  8156. (defined(WOLFSSL_TLS13) && !defined(HAVE_ECC) && !defined(HAVE_CURVE25519) \
  8157. && !defined(HAVE_CURVE448) && defined(HAVE_SUPPORTED_CURVES)) || \
  8158. ((defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  8159. defined(HAVE_CURVE448)) && defined(HAVE_SUPPORTED_CURVES))
  8160. /* Populates the default supported groups / curves */
  8161. static int TLSX_PopulateSupportedGroups(WOLFSSL* ssl, TLSX** extensions)
  8162. {
  8163. int ret = WOLFSSL_SUCCESS;
  8164. #ifdef WOLFSSL_TLS13
  8165. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  8166. if (ssl->options.resuming && ssl->session.namedGroup != 0) {
  8167. return TLSX_UseSupportedCurve(extensions, ssl->session.namedGroup,
  8168. ssl->heap);
  8169. }
  8170. #endif
  8171. #ifdef HAVE_SUPPORTED_CURVES
  8172. if (ssl->numGroups != 0) {
  8173. int i;
  8174. for (i = 0; i < ssl->numGroups; i++) {
  8175. ret = TLSX_UseSupportedCurve(extensions, ssl->group[i], ssl->heap);
  8176. if (ret != WOLFSSL_SUCCESS)
  8177. return ret;
  8178. }
  8179. return WOLFSSL_SUCCESS;
  8180. }
  8181. #endif /* HAVE_SUPPORTED_CURVES */
  8182. #endif /* WOLFSSL_TLS13 */
  8183. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  8184. /* list in order by strength, since not all servers choose by strength */
  8185. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  8186. #ifndef NO_ECC_SECP
  8187. ret = TLSX_UseSupportedCurve(extensions,
  8188. WOLFSSL_ECC_SECP521R1, ssl->heap);
  8189. if (ret != WOLFSSL_SUCCESS) return ret;
  8190. #endif
  8191. #endif
  8192. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  8193. #ifdef HAVE_ECC_BRAINPOOL
  8194. ret = TLSX_UseSupportedCurve(extensions,
  8195. WOLFSSL_ECC_BRAINPOOLP512R1, ssl->heap);
  8196. if (ret != WOLFSSL_SUCCESS) return ret;
  8197. #endif
  8198. #endif
  8199. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  8200. #ifndef NO_ECC_SECP
  8201. ret = TLSX_UseSupportedCurve(extensions,
  8202. WOLFSSL_ECC_SECP384R1, ssl->heap);
  8203. if (ret != WOLFSSL_SUCCESS) return ret;
  8204. #endif
  8205. #ifdef HAVE_ECC_BRAINPOOL
  8206. ret = TLSX_UseSupportedCurve(extensions,
  8207. WOLFSSL_ECC_BRAINPOOLP384R1, ssl->heap);
  8208. if (ret != WOLFSSL_SUCCESS) return ret;
  8209. #endif
  8210. #endif
  8211. #endif /* HAVE_ECC && HAVE_SUPPORTED_CURVES */
  8212. #ifndef HAVE_FIPS
  8213. #if defined(HAVE_CURVE448)
  8214. ret = TLSX_UseSupportedCurve(extensions,
  8215. WOLFSSL_ECC_X448, ssl->heap);
  8216. if (ret != WOLFSSL_SUCCESS) return ret;
  8217. #endif
  8218. #endif /* HAVE_FIPS */
  8219. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  8220. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  8221. #ifndef NO_ECC_SECP
  8222. ret = TLSX_UseSupportedCurve(extensions,
  8223. WOLFSSL_ECC_SECP256R1, ssl->heap);
  8224. if (ret != WOLFSSL_SUCCESS) return ret;
  8225. #endif
  8226. #ifdef HAVE_ECC_KOBLITZ
  8227. ret = TLSX_UseSupportedCurve(extensions,
  8228. WOLFSSL_ECC_SECP256K1, ssl->heap);
  8229. if (ret != WOLFSSL_SUCCESS) return ret;
  8230. #endif
  8231. #ifdef HAVE_ECC_BRAINPOOL
  8232. ret = TLSX_UseSupportedCurve(extensions,
  8233. WOLFSSL_ECC_BRAINPOOLP256R1, ssl->heap);
  8234. if (ret != WOLFSSL_SUCCESS) return ret;
  8235. #endif
  8236. #endif
  8237. #endif /* HAVE_ECC && HAVE_SUPPORTED_CURVES */
  8238. #ifndef HAVE_FIPS
  8239. #if defined(HAVE_CURVE25519)
  8240. ret = TLSX_UseSupportedCurve(extensions,
  8241. WOLFSSL_ECC_X25519, ssl->heap);
  8242. if (ret != WOLFSSL_SUCCESS) return ret;
  8243. #endif
  8244. #endif /* HAVE_FIPS */
  8245. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  8246. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  8247. #ifndef NO_ECC_SECP
  8248. ret = TLSX_UseSupportedCurve(extensions,
  8249. WOLFSSL_ECC_SECP224R1, ssl->heap);
  8250. if (ret != WOLFSSL_SUCCESS) return ret;
  8251. #endif
  8252. #ifdef HAVE_ECC_KOBLITZ
  8253. ret = TLSX_UseSupportedCurve(extensions,
  8254. WOLFSSL_ECC_SECP224K1, ssl->heap);
  8255. if (ret != WOLFSSL_SUCCESS) return ret;
  8256. #endif
  8257. #endif
  8258. #ifndef HAVE_FIPS
  8259. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  8260. #ifndef NO_ECC_SECP
  8261. ret = TLSX_UseSupportedCurve(extensions,
  8262. WOLFSSL_ECC_SECP192R1, ssl->heap);
  8263. if (ret != WOLFSSL_SUCCESS) return ret;
  8264. #endif
  8265. #ifdef HAVE_ECC_KOBLITZ
  8266. ret = TLSX_UseSupportedCurve(extensions,
  8267. WOLFSSL_ECC_SECP192K1, ssl->heap);
  8268. if (ret != WOLFSSL_SUCCESS) return ret;
  8269. #endif
  8270. #endif
  8271. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  8272. #ifndef NO_ECC_SECP
  8273. ret = TLSX_UseSupportedCurve(extensions,
  8274. WOLFSSL_ECC_SECP160R1, ssl->heap);
  8275. if (ret != WOLFSSL_SUCCESS) return ret;
  8276. #endif
  8277. #ifdef HAVE_ECC_SECPR2
  8278. ret = TLSX_UseSupportedCurve(extensions,
  8279. WOLFSSL_ECC_SECP160R2, ssl->heap);
  8280. if (ret != WOLFSSL_SUCCESS) return ret;
  8281. #endif
  8282. #ifdef HAVE_ECC_KOBLITZ
  8283. ret = TLSX_UseSupportedCurve(extensions,
  8284. WOLFSSL_ECC_SECP160K1, ssl->heap);
  8285. if (ret != WOLFSSL_SUCCESS) return ret;
  8286. #endif
  8287. #endif
  8288. #endif /* HAVE_FIPS */
  8289. #endif /* HAVE_ECC && HAVE_SUPPORTED_CURVES */
  8290. /* Add FFDHE supported groups. */
  8291. #ifdef HAVE_FFDHE_8192
  8292. if (8192/8 >= ssl->options.minDhKeySz &&
  8293. 8192/8 <= ssl->options.maxDhKeySz) {
  8294. ret = TLSX_UseSupportedCurve(extensions,
  8295. WOLFSSL_FFDHE_8192, ssl->heap);
  8296. if (ret != WOLFSSL_SUCCESS)
  8297. return ret;
  8298. }
  8299. #endif
  8300. #ifdef HAVE_FFDHE_6144
  8301. if (6144/8 >= ssl->options.minDhKeySz &&
  8302. 6144/8 <= ssl->options.maxDhKeySz) {
  8303. ret = TLSX_UseSupportedCurve(extensions,
  8304. WOLFSSL_FFDHE_6144, ssl->heap);
  8305. if (ret != WOLFSSL_SUCCESS)
  8306. return ret;
  8307. }
  8308. #endif
  8309. #ifdef HAVE_FFDHE_4096
  8310. if (4096/8 >= ssl->options.minDhKeySz &&
  8311. 4096/8 <= ssl->options.maxDhKeySz) {
  8312. ret = TLSX_UseSupportedCurve(extensions,
  8313. WOLFSSL_FFDHE_4096, ssl->heap);
  8314. if (ret != WOLFSSL_SUCCESS)
  8315. return ret;
  8316. }
  8317. #endif
  8318. #ifdef HAVE_FFDHE_3072
  8319. if (3072/8 >= ssl->options.minDhKeySz &&
  8320. 3072/8 <= ssl->options.maxDhKeySz) {
  8321. ret = TLSX_UseSupportedCurve(extensions,
  8322. WOLFSSL_FFDHE_3072, ssl->heap);
  8323. if (ret != WOLFSSL_SUCCESS)
  8324. return ret;
  8325. }
  8326. #endif
  8327. #ifdef HAVE_FFDHE_2048
  8328. if (2048/8 >= ssl->options.minDhKeySz &&
  8329. 2048/8 <= ssl->options.maxDhKeySz) {
  8330. ret = TLSX_UseSupportedCurve(extensions,
  8331. WOLFSSL_FFDHE_2048, ssl->heap);
  8332. if (ret != WOLFSSL_SUCCESS)
  8333. return ret;
  8334. }
  8335. #endif
  8336. (void)ssl;
  8337. (void)extensions;
  8338. return ret;
  8339. }
  8340. #endif
  8341. int TLSX_PopulateExtensions(WOLFSSL* ssl, byte isServer)
  8342. {
  8343. int ret = 0;
  8344. byte* public_key = NULL;
  8345. word16 public_key_len = 0;
  8346. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  8347. int usingPSK = 0;
  8348. #endif
  8349. #ifdef HAVE_QSH
  8350. TLSX* extension;
  8351. QSHScheme* qsh;
  8352. QSHScheme* next;
  8353. /* add supported QSHSchemes */
  8354. WOLFSSL_MSG("Adding supported QSH Schemes");
  8355. #endif
  8356. /* server will add extension depending on what is parsed from client */
  8357. if (!isServer) {
  8358. #ifdef HAVE_QSH
  8359. /* test if user has set a specific scheme already */
  8360. if (!ssl->user_set_QSHSchemes) {
  8361. if (ssl->sendQSHKeys && ssl->QSH_Key == NULL) {
  8362. if ((ret = TLSX_CreateQSHKey(ssl, WOLFSSL_NTRU_EESS743)) != 0) {
  8363. WOLFSSL_MSG("Error creating ntru keys");
  8364. return ret;
  8365. }
  8366. if ((ret = TLSX_CreateQSHKey(ssl, WOLFSSL_NTRU_EESS593)) != 0) {
  8367. WOLFSSL_MSG("Error creating ntru keys");
  8368. return ret;
  8369. }
  8370. if ((ret = TLSX_CreateQSHKey(ssl, WOLFSSL_NTRU_EESS439)) != 0) {
  8371. WOLFSSL_MSG("Error creating ntru keys");
  8372. return ret;
  8373. }
  8374. /* add NTRU 256 */
  8375. public_key = TLSX_QSHKeyFind_Pub(ssl->QSH_Key,
  8376. &public_key_len, WOLFSSL_NTRU_EESS743);
  8377. }
  8378. if (TLSX_UseQSHScheme(&ssl->extensions, WOLFSSL_NTRU_EESS743,
  8379. public_key, public_key_len, ssl->heap)
  8380. != WOLFSSL_SUCCESS)
  8381. ret = -1;
  8382. /* add NTRU 196 */
  8383. if (ssl->sendQSHKeys) {
  8384. public_key = TLSX_QSHKeyFind_Pub(ssl->QSH_Key,
  8385. &public_key_len, WOLFSSL_NTRU_EESS593);
  8386. }
  8387. if (TLSX_UseQSHScheme(&ssl->extensions, WOLFSSL_NTRU_EESS593,
  8388. public_key, public_key_len, ssl->heap)
  8389. != WOLFSSL_SUCCESS)
  8390. ret = -1;
  8391. /* add NTRU 128 */
  8392. if (ssl->sendQSHKeys) {
  8393. public_key = TLSX_QSHKeyFind_Pub(ssl->QSH_Key,
  8394. &public_key_len, WOLFSSL_NTRU_EESS439);
  8395. }
  8396. if (TLSX_UseQSHScheme(&ssl->extensions, WOLFSSL_NTRU_EESS439,
  8397. public_key, public_key_len, ssl->heap)
  8398. != WOLFSSL_SUCCESS)
  8399. ret = -1;
  8400. }
  8401. else if (ssl->sendQSHKeys && ssl->QSH_Key == NULL) {
  8402. /* for each scheme make a client key */
  8403. extension = TLSX_Find(ssl->extensions, TLSX_QUANTUM_SAFE_HYBRID);
  8404. if (extension) {
  8405. qsh = (QSHScheme*)extension->data;
  8406. while (qsh) {
  8407. if ((ret = TLSX_CreateQSHKey(ssl, qsh->name)) != 0)
  8408. return ret;
  8409. /* get next now because qsh could be freed */
  8410. next = qsh->next;
  8411. /* find the public key created and add to extension*/
  8412. public_key = TLSX_QSHKeyFind_Pub(ssl->QSH_Key,
  8413. &public_key_len, qsh->name);
  8414. if (TLSX_UseQSHScheme(&ssl->extensions, qsh->name,
  8415. public_key, public_key_len,
  8416. ssl->heap) != WOLFSSL_SUCCESS)
  8417. ret = -1;
  8418. qsh = next;
  8419. }
  8420. }
  8421. }
  8422. #endif
  8423. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  8424. if (!ssl->options.disallowEncThenMac) {
  8425. ret = TLSX_EncryptThenMac_Use(ssl);
  8426. if (ret != 0)
  8427. return ret;
  8428. }
  8429. #endif
  8430. #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  8431. defined(HAVE_CURVE448)) && defined(HAVE_SUPPORTED_CURVES)
  8432. if (!ssl->options.userCurves && !ssl->ctx->userCurves) {
  8433. if (TLSX_Find(ssl->ctx->extensions,
  8434. TLSX_SUPPORTED_GROUPS) == NULL) {
  8435. ret = TLSX_PopulateSupportedGroups(ssl, &ssl->extensions);
  8436. if (ret != WOLFSSL_SUCCESS)
  8437. return ret;
  8438. }
  8439. }
  8440. if ((!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade) &&
  8441. TLSX_Find(ssl->ctx->extensions, TLSX_EC_POINT_FORMATS) == NULL &&
  8442. TLSX_Find(ssl->extensions, TLSX_EC_POINT_FORMATS) == NULL) {
  8443. ret = TLSX_UsePointFormat(&ssl->extensions,
  8444. WOLFSSL_EC_PF_UNCOMPRESSED, ssl->heap);
  8445. if (ret != WOLFSSL_SUCCESS)
  8446. return ret;
  8447. }
  8448. #endif /* (HAVE_ECC || CURVE25519 || CURVE448) && HAVE_SUPPORTED_CURVES */
  8449. } /* is not server */
  8450. #if !defined(WOLFSSL_NO_SIGALG)
  8451. WOLFSSL_MSG("Adding signature algorithms extension");
  8452. if ((ret = TLSX_SetSignatureAlgorithms(&ssl->extensions, ssl, ssl->heap))
  8453. != 0) {
  8454. return ret;
  8455. }
  8456. #else
  8457. ret = 0;
  8458. #endif
  8459. #ifdef WOLFSSL_TLS13
  8460. if (!isServer && IsAtLeastTLSv1_3(ssl->version)) {
  8461. /* Add mandatory TLS v1.3 extension: supported version */
  8462. WOLFSSL_MSG("Adding supported versions extension");
  8463. if ((ret = TLSX_SetSupportedVersions(&ssl->extensions, ssl,
  8464. ssl->heap)) != 0) {
  8465. return ret;
  8466. }
  8467. #if !defined(HAVE_ECC) && !defined(HAVE_CURVE25519) && \
  8468. !defined(HAVE_CURVE448) && defined(HAVE_SUPPORTED_CURVES)
  8469. if (TLSX_Find(ssl->ctx->extensions, TLSX_SUPPORTED_GROUPS) == NULL) {
  8470. /* Put in DH groups for TLS 1.3 only. */
  8471. ret = TLSX_PopulateSupportedGroups(ssl, &ssl->extensions);
  8472. if (ret != WOLFSSL_SUCCESS)
  8473. return ret;
  8474. ret = 0;
  8475. }
  8476. #endif /* (HAVE_ECC || CURVE25519 || CURVE448) && HAVE_SUPPORTED_CURVES */
  8477. if (ssl->certHashSigAlgoSz > 0) {
  8478. WOLFSSL_MSG("Adding signature algorithms cert extension");
  8479. if ((ret = TLSX_SetSignatureAlgorithmsCert(&ssl->extensions,
  8480. ssl, ssl->heap)) != 0) {
  8481. return ret;
  8482. }
  8483. }
  8484. if (TLSX_Find(ssl->extensions, TLSX_KEY_SHARE) == NULL) {
  8485. word16 namedGroup;
  8486. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  8487. if (ssl->options.resuming && ssl->session.namedGroup != 0)
  8488. namedGroup = ssl->session.namedGroup;
  8489. else
  8490. #endif
  8491. {
  8492. #if defined(HAVE_ECC) && (!defined(NO_ECC256) || \
  8493. defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP)
  8494. namedGroup = WOLFSSL_ECC_SECP256R1;
  8495. #elif defined(HAVE_CURVE25519)
  8496. namedGroup = WOLFSSL_ECC_X25519;
  8497. #elif defined(HAVE_CURVE448)
  8498. namedGroup = WOLFSSL_ECC_X448;
  8499. #elif defined(HAVE_ECC) && (!defined(NO_ECC384) || \
  8500. defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP)
  8501. namedGroup = WOLFSSL_ECC_SECP384R1;
  8502. #elif defined(HAVE_ECC) && (!defined(NO_ECC521) || \
  8503. defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP)
  8504. namedGroup = WOLFSSL_ECC_SECP521R1;
  8505. #elif defined(HAVE_FFDHE_2048)
  8506. namedGroup = WOLFSSL_FFDHE_2048;
  8507. #elif defined(HAVE_FFDHE_3072)
  8508. namedGroup = WOLFSSL_FFDHE_3072;
  8509. #elif defined(HAVE_FFDHE_4096)
  8510. namedGroup = WOLFSSL_FFDHE_4096;
  8511. #elif defined(HAVE_FFDHE_6144)
  8512. namedGroup = WOLFSSL_FFDHE_6144;
  8513. #elif defined(HAVE_FFDHE_8192)
  8514. namedGroup = WOLFSSL_FFDHE_8192;
  8515. #else
  8516. return KEY_SHARE_ERROR;
  8517. #endif
  8518. }
  8519. ret = TLSX_KeyShare_Use(ssl, namedGroup, 0, NULL, NULL);
  8520. if (ret != 0)
  8521. return ret;
  8522. }
  8523. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  8524. TLSX_Remove(&ssl->extensions, TLSX_PRE_SHARED_KEY, ssl->heap);
  8525. #endif
  8526. #if defined(HAVE_SESSION_TICKET)
  8527. if (ssl->options.resuming && ssl->session.ticketLen > 0) {
  8528. WOLFSSL_SESSION* sess = &ssl->session;
  8529. word32 milli;
  8530. if (sess->ticketLen > MAX_PSK_ID_LEN) {
  8531. WOLFSSL_MSG("Session ticket length for PSK ext is too large");
  8532. return BUFFER_ERROR;
  8533. }
  8534. /* Determine the MAC algorithm for the cipher suite used. */
  8535. ssl->options.cipherSuite0 = sess->cipherSuite0;
  8536. ssl->options.cipherSuite = sess->cipherSuite;
  8537. ret = SetCipherSpecs(ssl);
  8538. if (ret != 0)
  8539. return ret;
  8540. milli = TimeNowInMilliseconds() - sess->ticketSeen +
  8541. sess->ticketAdd;
  8542. /* Pre-shared key is mandatory extension for resumption. */
  8543. ret = TLSX_PreSharedKey_Use(ssl, sess->ticket, sess->ticketLen,
  8544. milli, ssl->specs.mac_algorithm,
  8545. ssl->options.cipherSuite0,
  8546. ssl->options.cipherSuite, 1,
  8547. NULL);
  8548. if (ret != 0)
  8549. return ret;
  8550. usingPSK = 1;
  8551. }
  8552. #endif
  8553. #ifndef NO_PSK
  8554. if (ssl->options.client_psk_cb != NULL ||
  8555. ssl->options.client_psk_tls13_cb != NULL) {
  8556. /* Default ciphersuite. */
  8557. byte cipherSuite0 = TLS13_BYTE;
  8558. byte cipherSuite = WOLFSSL_DEF_PSK_CIPHER;
  8559. const char* cipherName = NULL;
  8560. if (ssl->options.client_psk_tls13_cb != NULL) {
  8561. ssl->arrays->psk_keySz = ssl->options.client_psk_tls13_cb(
  8562. ssl, ssl->arrays->server_hint,
  8563. ssl->arrays->client_identity, MAX_PSK_ID_LEN,
  8564. ssl->arrays->psk_key, MAX_PSK_KEY_LEN, &cipherName);
  8565. if (GetCipherSuiteFromName(cipherName, &cipherSuite0,
  8566. &cipherSuite) != 0) {
  8567. return PSK_KEY_ERROR;
  8568. }
  8569. }
  8570. else {
  8571. ssl->arrays->psk_keySz = ssl->options.client_psk_cb(ssl,
  8572. ssl->arrays->server_hint, ssl->arrays->client_identity,
  8573. MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN);
  8574. }
  8575. if (ssl->arrays->psk_keySz == 0 ||
  8576. ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN) {
  8577. return PSK_KEY_ERROR;
  8578. }
  8579. ssl->arrays->client_identity[MAX_PSK_ID_LEN] = '\0';
  8580. /* TODO: Callback should be able to change ciphersuite. */
  8581. ssl->options.cipherSuite0 = cipherSuite0;
  8582. ssl->options.cipherSuite = cipherSuite;
  8583. ret = SetCipherSpecs(ssl);
  8584. if (ret != 0)
  8585. return ret;
  8586. ret = TLSX_PreSharedKey_Use(ssl,
  8587. (byte*)ssl->arrays->client_identity,
  8588. (word16)XSTRLEN(ssl->arrays->client_identity),
  8589. 0, ssl->specs.mac_algorithm,
  8590. cipherSuite0, cipherSuite, 0,
  8591. NULL);
  8592. if (ret != 0)
  8593. return ret;
  8594. usingPSK = 1;
  8595. }
  8596. #endif
  8597. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  8598. if (usingPSK) {
  8599. byte modes;
  8600. /* Pre-shared key modes: mandatory extension for resumption. */
  8601. modes = 1 << PSK_KE;
  8602. #if !defined(NO_DH) || defined(HAVE_ECC) || \
  8603. defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  8604. if (!ssl->options.noPskDheKe)
  8605. modes |= 1 << PSK_DHE_KE;
  8606. #endif
  8607. ret = TLSX_PskKeModes_Use(ssl, modes);
  8608. if (ret != 0)
  8609. return ret;
  8610. }
  8611. #endif
  8612. #if defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  8613. if (!isServer && ssl->options.postHandshakeAuth) {
  8614. ret = TLSX_PostHandAuth_Use(ssl);
  8615. if (ret != 0)
  8616. return ret;
  8617. }
  8618. #endif
  8619. }
  8620. #endif
  8621. (void)isServer;
  8622. (void)public_key;
  8623. (void)public_key_len;
  8624. (void)ssl;
  8625. return ret;
  8626. }
  8627. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
  8628. /** Tells the buffered size of extensions to be sent into the client hello. */
  8629. int TLSX_GetRequestSize(WOLFSSL* ssl, byte msgType, word16* pLength)
  8630. {
  8631. int ret = 0;
  8632. word16 length = 0;
  8633. byte semaphore[SEMAPHORE_SIZE] = {0};
  8634. if (!TLSX_SupportExtensions(ssl))
  8635. return 0;
  8636. if (msgType == client_hello) {
  8637. EC_VALIDATE_REQUEST(ssl, semaphore);
  8638. PF_VALIDATE_REQUEST(ssl, semaphore);
  8639. QSH_VALIDATE_REQUEST(ssl, semaphore);
  8640. WOLF_STK_VALIDATE_REQUEST(ssl);
  8641. #if !defined(WOLFSSL_NO_SIGALG)
  8642. if (ssl->suites->hashSigAlgoSz == 0)
  8643. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  8644. #endif
  8645. #if defined(WOLFSSL_TLS13)
  8646. if (!IsAtLeastTLSv1_2(ssl))
  8647. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  8648. if (!IsAtLeastTLSv1_3(ssl->version)) {
  8649. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  8650. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  8651. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  8652. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PSK_KEY_EXCHANGE_MODES));
  8653. #endif
  8654. #ifdef WOLFSSL_EARLY_DATA
  8655. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  8656. #endif
  8657. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  8658. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  8659. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_POST_HANDSHAKE_AUTH));
  8660. #endif
  8661. }
  8662. #endif
  8663. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  8664. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  8665. if (!ssl->ctx->cm->ocspStaplingEnabled) {
  8666. /* mark already sent, so it won't send it */
  8667. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  8668. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  8669. }
  8670. #endif
  8671. }
  8672. #ifdef WOLFSSL_TLS13
  8673. #ifndef NO_CERTS
  8674. else if (msgType == certificate_request) {
  8675. /* Don't send out any extension except those that are turned off. */
  8676. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  8677. #if !defined(WOLFSSL_NO_SIGALG)
  8678. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  8679. #endif
  8680. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  8681. * TLSX_CERTIFICATE_AUTHORITIES, OID_FILTERS
  8682. * TLSX_STATUS_REQUEST
  8683. */
  8684. }
  8685. #endif
  8686. #endif
  8687. if (ssl->extensions) {
  8688. ret = TLSX_GetSize(ssl->extensions, semaphore, msgType, &length);
  8689. if (ret != 0)
  8690. return ret;
  8691. }
  8692. if (ssl->ctx && ssl->ctx->extensions) {
  8693. ret = TLSX_GetSize(ssl->ctx->extensions, semaphore, msgType, &length);
  8694. if (ret != 0)
  8695. return ret;
  8696. }
  8697. #ifdef HAVE_EXTENDED_MASTER
  8698. if (msgType == client_hello && ssl->options.haveEMS &&
  8699. (!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade)) {
  8700. length += HELLO_EXT_SZ;
  8701. }
  8702. #endif
  8703. if (length)
  8704. length += OPAQUE16_LEN; /* for total length storage. */
  8705. *pLength += length;
  8706. return ret;
  8707. }
  8708. /** Writes the extensions to be sent into the client hello. */
  8709. int TLSX_WriteRequest(WOLFSSL* ssl, byte* output, byte msgType, word16* pOffset)
  8710. {
  8711. int ret = 0;
  8712. word16 offset = 0;
  8713. byte semaphore[SEMAPHORE_SIZE] = {0};
  8714. if (!TLSX_SupportExtensions(ssl) || output == NULL)
  8715. return 0;
  8716. offset += OPAQUE16_LEN; /* extensions length */
  8717. if (msgType == client_hello) {
  8718. EC_VALIDATE_REQUEST(ssl, semaphore);
  8719. PF_VALIDATE_REQUEST(ssl, semaphore);
  8720. WOLF_STK_VALIDATE_REQUEST(ssl);
  8721. QSH_VALIDATE_REQUEST(ssl, semaphore);
  8722. #if !defined(WOLFSSL_NO_SIGALG)
  8723. if (ssl->suites->hashSigAlgoSz == 0)
  8724. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  8725. #endif
  8726. #ifdef WOLFSSL_TLS13
  8727. if (!IsAtLeastTLSv1_2(ssl))
  8728. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  8729. if (!IsAtLeastTLSv1_3(ssl->version)) {
  8730. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  8731. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  8732. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PSK_KEY_EXCHANGE_MODES));
  8733. #endif
  8734. #ifdef WOLFSSL_EARLY_DATA
  8735. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  8736. #endif
  8737. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  8738. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  8739. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_POST_HANDSHAKE_AUTH));
  8740. #endif
  8741. }
  8742. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  8743. /* Must write Pre-shared Key extension at the end in TLS v1.3.
  8744. * Must not write out Pre-shared Key extension in earlier versions of
  8745. * protocol.
  8746. */
  8747. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  8748. #endif
  8749. #endif
  8750. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  8751. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  8752. /* mark already sent, so it won't send it */
  8753. if (!ssl->ctx->cm->ocspStaplingEnabled) {
  8754. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  8755. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  8756. }
  8757. #endif
  8758. }
  8759. #ifdef WOLFSSL_TLS13
  8760. #ifndef NO_CERTS
  8761. else if (msgType == certificate_request) {
  8762. /* Don't send out any extension except those that are turned off. */
  8763. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  8764. #if !defined(WOLFSSL_NO_SIGALG)
  8765. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  8766. #endif
  8767. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  8768. * TLSX_CERTIFICATE_AUTHORITIES, TLSX_OID_FILTERS
  8769. * TLSX_STATUS_REQUEST
  8770. */
  8771. }
  8772. #endif
  8773. #endif
  8774. if (ssl->extensions) {
  8775. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  8776. msgType, &offset);
  8777. if (ret != 0)
  8778. return ret;
  8779. }
  8780. if (ssl->ctx && ssl->ctx->extensions) {
  8781. ret = TLSX_Write(ssl->ctx->extensions, output + offset, semaphore,
  8782. msgType, &offset);
  8783. if (ret != 0)
  8784. return ret;
  8785. }
  8786. #ifdef HAVE_EXTENDED_MASTER
  8787. if (msgType == client_hello && ssl->options.haveEMS &&
  8788. (!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade)) {
  8789. WOLFSSL_MSG("EMS extension to write");
  8790. c16toa(HELLO_EXT_EXTMS, output + offset);
  8791. offset += HELLO_EXT_TYPE_SZ;
  8792. c16toa(0, output + offset);
  8793. offset += HELLO_EXT_SZ_SZ;
  8794. }
  8795. #endif
  8796. #ifdef WOLFSSL_TLS13
  8797. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  8798. if (msgType == client_hello && IsAtLeastTLSv1_3(ssl->version)) {
  8799. /* Write out what we can of Pre-shared key extension. */
  8800. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  8801. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  8802. client_hello, &offset);
  8803. if (ret != 0)
  8804. return ret;
  8805. }
  8806. #endif
  8807. #endif
  8808. if (offset > OPAQUE16_LEN || msgType != client_hello)
  8809. c16toa(offset - OPAQUE16_LEN, output); /* extensions length */
  8810. *pOffset += offset;
  8811. return ret;
  8812. }
  8813. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
  8814. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_SERVER)
  8815. /** Tells the buffered size of extensions to be sent into the server hello. */
  8816. int TLSX_GetResponseSize(WOLFSSL* ssl, byte msgType, word16* pLength)
  8817. {
  8818. int ret = 0;
  8819. word16 length = 0;
  8820. byte semaphore[SEMAPHORE_SIZE] = {0};
  8821. switch (msgType) {
  8822. #ifndef NO_WOLFSSL_SERVER
  8823. case server_hello:
  8824. PF_VALIDATE_RESPONSE(ssl, semaphore);
  8825. #ifdef WOLFSSL_TLS13
  8826. if (IsAtLeastTLSv1_3(ssl->version)) {
  8827. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  8828. TURN_OFF(semaphore,
  8829. TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  8830. if (!ssl->options.noPskDheKe)
  8831. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  8832. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  8833. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  8834. #endif
  8835. }
  8836. else {
  8837. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  8838. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  8839. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  8840. #endif
  8841. }
  8842. #endif
  8843. break;
  8844. #ifdef WOLFSSL_TLS13
  8845. case hello_retry_request:
  8846. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  8847. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  8848. if (!ssl->options.noPskDheKe)
  8849. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  8850. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  8851. break;
  8852. #endif
  8853. #ifdef WOLFSSL_TLS13
  8854. case encrypted_extensions:
  8855. /* Send out all extension except those that are turned on. */
  8856. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  8857. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  8858. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SESSION_TICKET));
  8859. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  8860. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  8861. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  8862. #endif
  8863. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  8864. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  8865. #endif
  8866. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  8867. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  8868. #endif
  8869. #if defined(HAVE_SECURE_RENEGOTIATION)
  8870. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_RENEGOTIATION_INFO));
  8871. #endif
  8872. break;
  8873. #ifdef WOLFSSL_EARLY_DATA
  8874. case session_ticket:
  8875. if (ssl->options.tls1_3) {
  8876. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  8877. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  8878. }
  8879. break;
  8880. #endif
  8881. #endif
  8882. #endif
  8883. #ifdef WOLFSSL_TLS13
  8884. #ifndef NO_CERTS
  8885. case certificate:
  8886. /* Don't send out any extension except those that are turned off. */
  8887. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  8888. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  8889. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  8890. * TLSX_SERVER_CERTIFICATE_TYPE
  8891. */
  8892. break;
  8893. #endif
  8894. #endif
  8895. }
  8896. #ifdef HAVE_QSH
  8897. /* change response if not using TLS_QSH */
  8898. if (!ssl->options.haveQSH) {
  8899. TLSX* ext = TLSX_Find(ssl->extensions, TLSX_QUANTUM_SAFE_HYBRID);
  8900. if (ext)
  8901. ext->resp = 0;
  8902. }
  8903. #endif
  8904. #ifdef HAVE_EXTENDED_MASTER
  8905. if (ssl->options.haveEMS && msgType == server_hello &&
  8906. !IsAtLeastTLSv1_3(ssl->version)) {
  8907. length += HELLO_EXT_SZ;
  8908. }
  8909. #endif
  8910. if (TLSX_SupportExtensions(ssl)) {
  8911. ret = TLSX_GetSize(ssl->extensions, semaphore, msgType, &length);
  8912. if (ret != 0)
  8913. return ret;
  8914. }
  8915. /* All the response data is set at the ssl object only, so no ctx here. */
  8916. if (length || msgType != server_hello)
  8917. length += OPAQUE16_LEN; /* for total length storage. */
  8918. *pLength += length;
  8919. return ret;
  8920. }
  8921. /** Writes the server hello extensions into a buffer. */
  8922. int TLSX_WriteResponse(WOLFSSL *ssl, byte* output, byte msgType, word16* pOffset)
  8923. {
  8924. int ret = 0;
  8925. word16 offset = 0;
  8926. if (TLSX_SupportExtensions(ssl) && output) {
  8927. byte semaphore[SEMAPHORE_SIZE] = {0};
  8928. switch (msgType) {
  8929. #ifndef NO_WOLFSSL_SERVER
  8930. case server_hello:
  8931. PF_VALIDATE_RESPONSE(ssl, semaphore);
  8932. #ifdef WOLFSSL_TLS13
  8933. if (IsAtLeastTLSv1_3(ssl->version)) {
  8934. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  8935. TURN_OFF(semaphore,
  8936. TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  8937. if (!ssl->options.noPskDheKe)
  8938. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  8939. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  8940. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  8941. #endif
  8942. }
  8943. else {
  8944. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  8945. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  8946. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  8947. #endif
  8948. }
  8949. #endif
  8950. break;
  8951. #ifdef WOLFSSL_TLS13
  8952. case hello_retry_request:
  8953. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  8954. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  8955. if (!ssl->options.noPskDheKe)
  8956. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  8957. /* Cookie is written below as last extension. */
  8958. break;
  8959. #endif
  8960. #ifdef WOLFSSL_TLS13
  8961. case encrypted_extensions:
  8962. /* Send out all extension except those that are turned on. */
  8963. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  8964. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  8965. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SESSION_TICKET));
  8966. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  8967. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  8968. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  8969. #endif
  8970. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  8971. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  8972. #endif
  8973. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  8974. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  8975. #endif
  8976. #if defined(HAVE_SECURE_RENEGOTIATION)
  8977. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_RENEGOTIATION_INFO));
  8978. #endif
  8979. break;
  8980. #ifdef WOLFSSL_EARLY_DATA
  8981. case session_ticket:
  8982. if (ssl->options.tls1_3) {
  8983. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  8984. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  8985. }
  8986. break;
  8987. #endif
  8988. #endif
  8989. #endif
  8990. #ifdef WOLFSSL_TLS13
  8991. #ifndef NO_CERTS
  8992. case certificate:
  8993. /* Don't send out any extension except those that are turned
  8994. * off. */
  8995. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  8996. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  8997. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  8998. * TLSX_SERVER_CERTIFICATE_TYPE
  8999. */
  9000. break;
  9001. #endif
  9002. #endif
  9003. }
  9004. offset += OPAQUE16_LEN; /* extensions length */
  9005. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  9006. msgType, &offset);
  9007. if (ret != 0)
  9008. return ret;
  9009. #ifdef WOLFSSL_TLS13
  9010. if (msgType == hello_retry_request) {
  9011. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  9012. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  9013. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  9014. msgType, &offset);
  9015. if (ret != 0)
  9016. return ret;
  9017. }
  9018. #endif
  9019. #ifdef HAVE_EXTENDED_MASTER
  9020. if (ssl->options.haveEMS && msgType == server_hello &&
  9021. !IsAtLeastTLSv1_3(ssl->version)) {
  9022. WOLFSSL_MSG("EMS extension to write");
  9023. c16toa(HELLO_EXT_EXTMS, output + offset);
  9024. offset += HELLO_EXT_TYPE_SZ;
  9025. c16toa(0, output + offset);
  9026. offset += HELLO_EXT_SZ_SZ;
  9027. }
  9028. #endif
  9029. if (offset > OPAQUE16_LEN || msgType != server_hello)
  9030. c16toa(offset - OPAQUE16_LEN, output); /* extensions length */
  9031. }
  9032. if (pOffset)
  9033. *pOffset += offset;
  9034. return ret;
  9035. }
  9036. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_SERVER */
  9037. #ifdef WOLFSSL_TLS13
  9038. int TLSX_ParseVersion(WOLFSSL* ssl, byte* input, word16 length, byte msgType,
  9039. int* found)
  9040. {
  9041. int ret = 0;
  9042. int offset = 0;
  9043. *found = 0;
  9044. while (offset < (int)length) {
  9045. word16 type;
  9046. word16 size;
  9047. if (offset + (2 * OPAQUE16_LEN) > length) {
  9048. ret = BUFFER_ERROR;
  9049. break;
  9050. }
  9051. ato16(input + offset, &type);
  9052. offset += HELLO_EXT_TYPE_SZ;
  9053. ato16(input + offset, &size);
  9054. offset += OPAQUE16_LEN;
  9055. if (offset + size > length) {
  9056. ret = BUFFER_ERROR;
  9057. break;
  9058. }
  9059. if (type == TLSX_SUPPORTED_VERSIONS) {
  9060. *found = 1;
  9061. WOLFSSL_MSG("Supported Versions extension received");
  9062. ret = SV_PARSE(ssl, input + offset, size, msgType);
  9063. break;
  9064. }
  9065. offset += size;
  9066. }
  9067. return ret;
  9068. }
  9069. #endif
  9070. /** Parses a buffer of TLS extensions. */
  9071. int TLSX_Parse(WOLFSSL* ssl, byte* input, word16 length, byte msgType,
  9072. Suites *suites)
  9073. {
  9074. int ret = 0;
  9075. word16 offset = 0;
  9076. byte isRequest = (msgType == client_hello ||
  9077. msgType == certificate_request);
  9078. #ifdef HAVE_EXTENDED_MASTER
  9079. byte pendingEMS = 0;
  9080. #endif
  9081. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  9082. int pskDone = 0;
  9083. #endif
  9084. if (!ssl || !input || (isRequest && !suites))
  9085. return BAD_FUNC_ARG;
  9086. while (ret == 0 && offset < length) {
  9087. word16 type;
  9088. word16 size;
  9089. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  9090. if (msgType == client_hello && pskDone)
  9091. return PSK_KEY_ERROR;
  9092. #endif
  9093. if (length - offset < HELLO_EXT_TYPE_SZ + OPAQUE16_LEN)
  9094. return BUFFER_ERROR;
  9095. ato16(input + offset, &type);
  9096. offset += HELLO_EXT_TYPE_SZ;
  9097. ato16(input + offset, &size);
  9098. offset += OPAQUE16_LEN;
  9099. if (offset + size > length)
  9100. return BUFFER_ERROR;
  9101. switch (type) {
  9102. case TLSX_SERVER_NAME:
  9103. WOLFSSL_MSG("SNI extension received");
  9104. #ifdef WOLFSSL_DEBUG_TLS
  9105. WOLFSSL_BUFFER(input + offset, size);
  9106. #endif
  9107. #ifdef WOLFSSL_TLS13
  9108. if (IsAtLeastTLSv1_3(ssl->version) &&
  9109. msgType != client_hello &&
  9110. msgType != server_hello &&
  9111. msgType != encrypted_extensions) {
  9112. return EXT_NOT_ALLOWED;
  9113. }
  9114. else if (!IsAtLeastTLSv1_3(ssl->version) &&
  9115. msgType == encrypted_extensions) {
  9116. return EXT_NOT_ALLOWED;
  9117. }
  9118. #endif
  9119. ret = SNI_PARSE(ssl, input + offset, size, isRequest);
  9120. break;
  9121. case TLSX_TRUSTED_CA_KEYS:
  9122. WOLFSSL_MSG("Trusted CA extension received");
  9123. #ifdef WOLFSSL_DEBUG_TLS
  9124. WOLFSSL_BUFFER(input + offset, size);
  9125. #endif
  9126. #ifdef WOLFSSL_TLS13
  9127. if (IsAtLeastTLSv1_3(ssl->version) &&
  9128. msgType != client_hello &&
  9129. msgType != encrypted_extensions) {
  9130. return EXT_NOT_ALLOWED;
  9131. }
  9132. #endif
  9133. ret = TCA_PARSE(ssl, input + offset, size, isRequest);
  9134. break;
  9135. case TLSX_MAX_FRAGMENT_LENGTH:
  9136. WOLFSSL_MSG("Max Fragment Length extension received");
  9137. #ifdef WOLFSSL_DEBUG_TLS
  9138. WOLFSSL_BUFFER(input + offset, size);
  9139. #endif
  9140. #ifdef WOLFSSL_TLS13
  9141. if (IsAtLeastTLSv1_3(ssl->version) &&
  9142. msgType != client_hello &&
  9143. msgType != encrypted_extensions) {
  9144. return EXT_NOT_ALLOWED;
  9145. }
  9146. else if (!IsAtLeastTLSv1_3(ssl->version) &&
  9147. msgType == encrypted_extensions) {
  9148. return EXT_NOT_ALLOWED;
  9149. }
  9150. #endif
  9151. ret = MFL_PARSE(ssl, input + offset, size, isRequest);
  9152. break;
  9153. case TLSX_TRUNCATED_HMAC:
  9154. WOLFSSL_MSG("Truncated HMAC extension received");
  9155. #ifdef WOLFSSL_DEBUG_TLS
  9156. WOLFSSL_BUFFER(input + offset, size);
  9157. #endif
  9158. #ifdef WOLFSSL_TLS13
  9159. if (IsAtLeastTLSv1_3(ssl->version))
  9160. break;
  9161. #endif
  9162. ret = THM_PARSE(ssl, input + offset, size, isRequest);
  9163. break;
  9164. case TLSX_SUPPORTED_GROUPS:
  9165. WOLFSSL_MSG("Supported Groups extension received");
  9166. #ifdef WOLFSSL_DEBUG_TLS
  9167. WOLFSSL_BUFFER(input + offset, size);
  9168. #endif
  9169. #ifdef WOLFSSL_TLS13
  9170. if (IsAtLeastTLSv1_3(ssl->version) &&
  9171. msgType != client_hello &&
  9172. msgType != server_hello &&
  9173. msgType != encrypted_extensions) {
  9174. return EXT_NOT_ALLOWED;
  9175. }
  9176. else if (!IsAtLeastTLSv1_3(ssl->version) &&
  9177. msgType == encrypted_extensions) {
  9178. return EXT_NOT_ALLOWED;
  9179. }
  9180. #endif
  9181. ret = EC_PARSE(ssl, input + offset, size, isRequest);
  9182. break;
  9183. case TLSX_EC_POINT_FORMATS:
  9184. WOLFSSL_MSG("Point Formats extension received");
  9185. #ifdef WOLFSSL_DEBUG_TLS
  9186. WOLFSSL_BUFFER(input + offset, size);
  9187. #endif
  9188. #ifdef WOLFSSL_TLS13
  9189. if (IsAtLeastTLSv1_3(ssl->version))
  9190. break;
  9191. #endif
  9192. ret = PF_PARSE(ssl, input + offset, size, isRequest);
  9193. break;
  9194. case TLSX_STATUS_REQUEST:
  9195. WOLFSSL_MSG("Certificate Status Request extension received");
  9196. #ifdef WOLFSSL_DEBUG_TLS
  9197. WOLFSSL_BUFFER(input + offset, size);
  9198. #endif
  9199. #ifdef WOLFSSL_TLS13
  9200. if (IsAtLeastTLSv1_3(ssl->version) &&
  9201. msgType != client_hello &&
  9202. msgType != certificate_request &&
  9203. msgType != certificate) {
  9204. break;
  9205. }
  9206. #endif
  9207. ret = CSR_PARSE(ssl, input + offset, size, isRequest);
  9208. break;
  9209. case TLSX_STATUS_REQUEST_V2:
  9210. WOLFSSL_MSG("Certificate Status Request v2 extension received");
  9211. #ifdef WOLFSSL_DEBUG_TLS
  9212. WOLFSSL_BUFFER(input + offset, size);
  9213. #endif
  9214. #ifdef WOLFSSL_TLS13
  9215. if (IsAtLeastTLSv1_3(ssl->version) &&
  9216. msgType != client_hello &&
  9217. msgType != certificate_request &&
  9218. msgType != certificate) {
  9219. return EXT_NOT_ALLOWED;
  9220. }
  9221. #endif
  9222. ret = CSR2_PARSE(ssl, input + offset, size, isRequest);
  9223. break;
  9224. #ifdef HAVE_EXTENDED_MASTER
  9225. case HELLO_EXT_EXTMS:
  9226. WOLFSSL_MSG("Extended Master Secret extension received");
  9227. #ifdef WOLFSSL_DEBUG_TLS
  9228. WOLFSSL_BUFFER(input + offset, size);
  9229. #endif
  9230. #ifdef WOLFSSL_TLS13
  9231. if (IsAtLeastTLSv1_3(ssl->version))
  9232. break;
  9233. #endif
  9234. if (size != 0)
  9235. return BUFFER_ERROR;
  9236. #ifndef NO_WOLFSSL_SERVER
  9237. if (isRequest)
  9238. ssl->options.haveEMS = 1;
  9239. #endif
  9240. pendingEMS = 1;
  9241. break;
  9242. #endif
  9243. case TLSX_RENEGOTIATION_INFO:
  9244. WOLFSSL_MSG("Secure Renegotiation extension received");
  9245. #ifdef WOLFSSL_DEBUG_TLS
  9246. WOLFSSL_BUFFER(input + offset, size);
  9247. #endif
  9248. #ifdef WOLFSSL_TLS13
  9249. if (IsAtLeastTLSv1_3(ssl->version))
  9250. break;
  9251. #endif
  9252. ret = SCR_PARSE(ssl, input + offset, size, isRequest);
  9253. break;
  9254. case TLSX_SESSION_TICKET:
  9255. WOLFSSL_MSG("Session Ticket extension received");
  9256. #ifdef WOLFSSL_DEBUG_TLS
  9257. WOLFSSL_BUFFER(input + offset, size);
  9258. #endif
  9259. #ifdef WOLFSSL_TLS13
  9260. if (IsAtLeastTLSv1_3(ssl->version) &&
  9261. msgType != client_hello) {
  9262. return EXT_NOT_ALLOWED;
  9263. }
  9264. #endif
  9265. ret = WOLF_STK_PARSE(ssl, input + offset, size, isRequest);
  9266. break;
  9267. case TLSX_QUANTUM_SAFE_HYBRID:
  9268. WOLFSSL_MSG("Quantum-Safe-Hybrid extension received");
  9269. #ifdef WOLFSSL_DEBUG_TLS
  9270. WOLFSSL_BUFFER(input + offset, size);
  9271. #endif
  9272. #ifdef WOLFSSL_TLS13
  9273. if (IsAtLeastTLSv1_3(ssl->version))
  9274. break;
  9275. #endif
  9276. ret = QSH_PARSE(ssl, input + offset, size, isRequest);
  9277. break;
  9278. case TLSX_APPLICATION_LAYER_PROTOCOL:
  9279. WOLFSSL_MSG("ALPN extension received");
  9280. #ifdef WOLFSSL_DEBUG_TLS
  9281. WOLFSSL_BUFFER(input + offset, size);
  9282. #endif
  9283. #ifdef WOLFSSL_TLS13
  9284. if (IsAtLeastTLSv1_3(ssl->version) &&
  9285. msgType != client_hello &&
  9286. msgType != server_hello &&
  9287. msgType != encrypted_extensions) {
  9288. return EXT_NOT_ALLOWED;
  9289. }
  9290. else if (!IsAtLeastTLSv1_3(ssl->version) &&
  9291. msgType == encrypted_extensions) {
  9292. return EXT_NOT_ALLOWED;
  9293. }
  9294. #endif
  9295. ret = ALPN_PARSE(ssl, input + offset, size, isRequest);
  9296. break;
  9297. #if !defined(WOLFSSL_NO_SIGALG)
  9298. case TLSX_SIGNATURE_ALGORITHMS:
  9299. WOLFSSL_MSG("Signature Algorithms extension received");
  9300. #ifdef WOLFSSL_DEBUG_TLS
  9301. WOLFSSL_BUFFER(input + offset, size);
  9302. #endif
  9303. if (!IsAtLeastTLSv1_2(ssl))
  9304. break;
  9305. #ifdef WOLFSSL_TLS13
  9306. if (IsAtLeastTLSv1_3(ssl->version) &&
  9307. msgType != client_hello &&
  9308. msgType != certificate_request) {
  9309. return EXT_NOT_ALLOWED;
  9310. }
  9311. #endif
  9312. ret = SA_PARSE(ssl, input + offset, size, isRequest, suites);
  9313. break;
  9314. #endif
  9315. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  9316. case TLSX_ENCRYPT_THEN_MAC:
  9317. WOLFSSL_MSG("Encrypt-Then-Mac extension received");
  9318. /* Ignore for TLS 1.3+ */
  9319. if (IsAtLeastTLSv1_3(ssl->version))
  9320. break;
  9321. ret = ETM_PARSE(ssl, input + offset, size, msgType);
  9322. break;
  9323. #endif /* HAVE_ENCRYPT_THEN_MAC */
  9324. #ifdef WOLFSSL_TLS13
  9325. case TLSX_SUPPORTED_VERSIONS:
  9326. WOLFSSL_MSG("Skipping Supported Versions - already processed");
  9327. #ifdef WOLFSSL_DEBUG_TLS
  9328. WOLFSSL_BUFFER(input + offset, size);
  9329. #endif
  9330. break;
  9331. case TLSX_COOKIE:
  9332. WOLFSSL_MSG("Cookie extension received");
  9333. #ifdef WOLFSSL_DEBUG_TLS
  9334. WOLFSSL_BUFFER(input + offset, size);
  9335. #endif
  9336. if (!IsAtLeastTLSv1_3(ssl->version))
  9337. break;
  9338. if (msgType != client_hello &&
  9339. msgType != hello_retry_request) {
  9340. return EXT_NOT_ALLOWED;
  9341. }
  9342. ret = CKE_PARSE(ssl, input + offset, size, msgType);
  9343. break;
  9344. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9345. case TLSX_PRE_SHARED_KEY:
  9346. WOLFSSL_MSG("Pre-Shared Key extension received");
  9347. #ifdef WOLFSSL_DEBUG_TLS
  9348. WOLFSSL_BUFFER(input + offset, size);
  9349. #endif
  9350. if (!IsAtLeastTLSv1_3(ssl->version))
  9351. break;
  9352. if (msgType != client_hello && msgType != server_hello)
  9353. return EXT_NOT_ALLOWED;
  9354. ret = PSK_PARSE(ssl, input + offset, size, msgType);
  9355. pskDone = 1;
  9356. break;
  9357. case TLSX_PSK_KEY_EXCHANGE_MODES:
  9358. WOLFSSL_MSG("PSK Key Exchange Modes extension received");
  9359. #ifdef WOLFSSL_DEBUG_TLS
  9360. WOLFSSL_BUFFER(input + offset, size);
  9361. #endif
  9362. if (!IsAtLeastTLSv1_3(ssl->version))
  9363. break;
  9364. if (msgType != client_hello)
  9365. return EXT_NOT_ALLOWED;
  9366. ret = PKM_PARSE(ssl, input + offset, size, msgType);
  9367. break;
  9368. #endif
  9369. #ifdef WOLFSSL_EARLY_DATA
  9370. case TLSX_EARLY_DATA:
  9371. WOLFSSL_MSG("Early Data extension received");
  9372. #ifdef WOLFSSL_DEBUG_TLS
  9373. WOLFSSL_BUFFER(input + offset, size);
  9374. #endif
  9375. if (!IsAtLeastTLSv1_3(ssl->version))
  9376. break;
  9377. if (msgType != client_hello && msgType != session_ticket &&
  9378. msgType != encrypted_extensions) {
  9379. return EXT_NOT_ALLOWED;
  9380. }
  9381. if (!IsAtLeastTLSv1_3(ssl->version) &&
  9382. (msgType == session_ticket ||
  9383. msgType == encrypted_extensions)) {
  9384. return EXT_NOT_ALLOWED;
  9385. }
  9386. ret = EDI_PARSE(ssl, input + offset, size, msgType);
  9387. break;
  9388. #endif
  9389. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  9390. case TLSX_POST_HANDSHAKE_AUTH:
  9391. WOLFSSL_MSG("Post Handshake Authentication extension received");
  9392. #ifdef WOLFSSL_DEBUG_TLS
  9393. WOLFSSL_BUFFER(input + offset, size);
  9394. #endif
  9395. if (!IsAtLeastTLSv1_3(ssl->version))
  9396. break;
  9397. if (msgType != client_hello)
  9398. return EXT_NOT_ALLOWED;
  9399. ret = PHA_PARSE(ssl, input + offset, size, msgType);
  9400. break;
  9401. #endif
  9402. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  9403. WOLFSSL_MSG("Signature Algorithms extension received");
  9404. #ifdef WOLFSSL_DEBUG_TLS
  9405. WOLFSSL_BUFFER(input + offset, size);
  9406. #endif
  9407. if (!IsAtLeastTLSv1_3(ssl->version))
  9408. break;
  9409. if (msgType != client_hello &&
  9410. msgType != certificate_request) {
  9411. return EXT_NOT_ALLOWED;
  9412. }
  9413. if (!IsAtLeastTLSv1_3(ssl->version) &&
  9414. msgType == certificate_request) {
  9415. return EXT_NOT_ALLOWED;
  9416. }
  9417. ret = SAC_PARSE(ssl, input + offset, size, isRequest);
  9418. break;
  9419. case TLSX_KEY_SHARE:
  9420. WOLFSSL_MSG("Key Share extension received");
  9421. #ifdef WOLFSSL_DEBUG_TLS
  9422. WOLFSSL_BUFFER(input + offset, size);
  9423. #endif
  9424. if (!IsAtLeastTLSv1_3(ssl->version))
  9425. break;
  9426. if (msgType != client_hello && msgType != server_hello &&
  9427. msgType != hello_retry_request) {
  9428. return EXT_NOT_ALLOWED;
  9429. }
  9430. ret = KS_PARSE(ssl, input + offset, size, msgType);
  9431. break;
  9432. #endif
  9433. default:
  9434. WOLFSSL_MSG("Unknown TLS extension type");
  9435. }
  9436. /* offset should be updated here! */
  9437. offset += size;
  9438. }
  9439. #ifdef HAVE_EXTENDED_MASTER
  9440. if (!isRequest && ssl->options.haveEMS && !pendingEMS)
  9441. ssl->options.haveEMS = 0;
  9442. #endif
  9443. if (ret == 0)
  9444. ret = SNI_VERIFY_PARSE(ssl, isRequest);
  9445. if (ret == 0)
  9446. ret = TCA_VERIFY_PARSE(ssl, isRequest);
  9447. return ret;
  9448. }
  9449. /* undefining semaphore macros */
  9450. #undef IS_OFF
  9451. #undef TURN_ON
  9452. #undef SEMAPHORE_SIZE
  9453. #endif /* HAVE_TLS_EXTENSIONS */
  9454. #ifndef NO_WOLFSSL_CLIENT
  9455. WOLFSSL_METHOD* wolfTLS_client_method(void)
  9456. {
  9457. return wolfTLS_client_method_ex(NULL);
  9458. }
  9459. WOLFSSL_METHOD* wolfTLS_client_method_ex(void* heap)
  9460. {
  9461. WOLFSSL_METHOD* method =
  9462. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  9463. heap, DYNAMIC_TYPE_METHOD);
  9464. (void)heap;
  9465. WOLFSSL_ENTER("TLS_client_method_ex");
  9466. if (method) {
  9467. #if defined(WOLFSSL_TLS13)
  9468. InitSSL_Method(method, MakeTLSv1_3());
  9469. #elif !defined(WOLFSSL_NO_TLS12)
  9470. InitSSL_Method(method, MakeTLSv1_2());
  9471. #elif !defined(NO_OLD_TLS)
  9472. InitSSL_Method(method, MakeTLSv1_1());
  9473. #elif defined(WOLFSSL_ALLOW_TLSV10)
  9474. InitSSL_Method(method, MakeTLSv1());
  9475. #else
  9476. #error No TLS version enabled!
  9477. #endif
  9478. method->downgrade = 1;
  9479. method->side = WOLFSSL_CLIENT_END;
  9480. }
  9481. return method;
  9482. }
  9483. #ifndef NO_OLD_TLS
  9484. #ifdef WOLFSSL_ALLOW_TLSV10
  9485. WOLFSSL_METHOD* wolfTLSv1_client_method(void)
  9486. {
  9487. return wolfTLSv1_client_method_ex(NULL);
  9488. }
  9489. WOLFSSL_METHOD* wolfTLSv1_client_method_ex(void* heap)
  9490. {
  9491. WOLFSSL_METHOD* method =
  9492. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  9493. heap, DYNAMIC_TYPE_METHOD);
  9494. (void)heap;
  9495. WOLFSSL_ENTER("TLSv1_client_method_ex");
  9496. if (method)
  9497. InitSSL_Method(method, MakeTLSv1());
  9498. return method;
  9499. }
  9500. #endif /* WOLFSSL_ALLOW_TLSV10 */
  9501. WOLFSSL_METHOD* wolfTLSv1_1_client_method(void)
  9502. {
  9503. return wolfTLSv1_1_client_method_ex(NULL);
  9504. }
  9505. WOLFSSL_METHOD* wolfTLSv1_1_client_method_ex(void* heap)
  9506. {
  9507. WOLFSSL_METHOD* method =
  9508. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  9509. heap, DYNAMIC_TYPE_METHOD);
  9510. (void)heap;
  9511. WOLFSSL_ENTER("TLSv1_1_client_method_ex");
  9512. if (method)
  9513. InitSSL_Method(method, MakeTLSv1_1());
  9514. return method;
  9515. }
  9516. #endif /* !NO_OLD_TLS */
  9517. #ifndef WOLFSSL_NO_TLS12
  9518. WOLFSSL_ABI
  9519. WOLFSSL_METHOD* wolfTLSv1_2_client_method(void)
  9520. {
  9521. return wolfTLSv1_2_client_method_ex(NULL);
  9522. }
  9523. WOLFSSL_METHOD* wolfTLSv1_2_client_method_ex(void* heap)
  9524. {
  9525. WOLFSSL_METHOD* method =
  9526. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  9527. heap, DYNAMIC_TYPE_METHOD);
  9528. (void)heap;
  9529. WOLFSSL_ENTER("TLSv1_2_client_method_ex");
  9530. if (method)
  9531. InitSSL_Method(method, MakeTLSv1_2());
  9532. return method;
  9533. }
  9534. #endif /* WOLFSSL_NO_TLS12 */
  9535. #ifdef WOLFSSL_TLS13
  9536. /* The TLS v1.3 client method data.
  9537. *
  9538. * returns the method data for a TLS v1.3 client.
  9539. */
  9540. WOLFSSL_ABI
  9541. WOLFSSL_METHOD* wolfTLSv1_3_client_method(void)
  9542. {
  9543. return wolfTLSv1_3_client_method_ex(NULL);
  9544. }
  9545. /* The TLS v1.3 client method data.
  9546. *
  9547. * heap The heap used for allocation.
  9548. * returns the method data for a TLS v1.3 client.
  9549. */
  9550. WOLFSSL_METHOD* wolfTLSv1_3_client_method_ex(void* heap)
  9551. {
  9552. WOLFSSL_METHOD* method = (WOLFSSL_METHOD*)
  9553. XMALLOC(sizeof(WOLFSSL_METHOD), heap,
  9554. DYNAMIC_TYPE_METHOD);
  9555. (void)heap;
  9556. WOLFSSL_ENTER("TLSv1_3_client_method_ex");
  9557. if (method)
  9558. InitSSL_Method(method, MakeTLSv1_3());
  9559. return method;
  9560. }
  9561. #endif /* WOLFSSL_TLS13 */
  9562. #ifdef WOLFSSL_DTLS
  9563. WOLFSSL_METHOD* wolfDTLS_client_method(void)
  9564. {
  9565. return wolfDTLS_client_method_ex(NULL);
  9566. }
  9567. WOLFSSL_METHOD* wolfDTLS_client_method_ex(void* heap)
  9568. {
  9569. WOLFSSL_METHOD* method =
  9570. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  9571. heap, DYNAMIC_TYPE_METHOD);
  9572. (void)heap;
  9573. WOLFSSL_ENTER("DTLS_client_method_ex");
  9574. if (method) {
  9575. #if !defined(WOLFSSL_NO_TLS12)
  9576. InitSSL_Method(method, MakeDTLSv1_2());
  9577. #elif !defined(NO_OLD_TLS)
  9578. InitSSL_Method(method, MakeDTLSv1());
  9579. #else
  9580. #error No DTLS version enabled!
  9581. #endif
  9582. method->downgrade = 1;
  9583. method->side = WOLFSSL_CLIENT_END;
  9584. }
  9585. return method;
  9586. }
  9587. #ifndef NO_OLD_TLS
  9588. WOLFSSL_METHOD* wolfDTLSv1_client_method(void)
  9589. {
  9590. return wolfDTLSv1_client_method_ex(NULL);
  9591. }
  9592. WOLFSSL_METHOD* wolfDTLSv1_client_method_ex(void* heap)
  9593. {
  9594. WOLFSSL_METHOD* method =
  9595. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  9596. heap, DYNAMIC_TYPE_METHOD);
  9597. (void)heap;
  9598. WOLFSSL_ENTER("DTLSv1_client_method_ex");
  9599. if (method)
  9600. InitSSL_Method(method, MakeDTLSv1());
  9601. return method;
  9602. }
  9603. #endif /* NO_OLD_TLS */
  9604. #ifndef WOLFSSL_NO_TLS12
  9605. WOLFSSL_METHOD* wolfDTLSv1_2_client_method(void)
  9606. {
  9607. return wolfDTLSv1_2_client_method_ex(NULL);
  9608. }
  9609. WOLFSSL_METHOD* wolfDTLSv1_2_client_method_ex(void* heap)
  9610. {
  9611. WOLFSSL_METHOD* method =
  9612. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  9613. heap, DYNAMIC_TYPE_METHOD);
  9614. (void)heap;
  9615. WOLFSSL_ENTER("DTLSv1_2_client_method_ex");
  9616. if (method)
  9617. InitSSL_Method(method, MakeDTLSv1_2());
  9618. (void)heap;
  9619. return method;
  9620. }
  9621. #endif /* !WOLFSSL_NO_TLS12 */
  9622. #endif /* WOLFSSL_DTLS */
  9623. #endif /* NO_WOLFSSL_CLIENT */
  9624. /* EITHER SIDE METHODS */
  9625. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  9626. #ifndef NO_OLD_TLS
  9627. #ifdef WOLFSSL_ALLOW_TLSV10
  9628. /* Gets a WOLFSL_METHOD type that is not set as client or server
  9629. *
  9630. * Returns a pointer to a WOLFSSL_METHOD struct
  9631. */
  9632. WOLFSSL_METHOD* wolfTLSv1_method(void)
  9633. {
  9634. return wolfTLSv1_method_ex(NULL);
  9635. }
  9636. WOLFSSL_METHOD* wolfTLSv1_method_ex(void* heap)
  9637. {
  9638. WOLFSSL_METHOD* m;
  9639. WOLFSSL_ENTER("TLSv1_method");
  9640. #ifndef NO_WOLFSSL_CLIENT
  9641. m = wolfTLSv1_client_method_ex(heap);
  9642. #else
  9643. m = wolfTLSv1_server_method_ex(heap);
  9644. #endif
  9645. if (m != NULL) {
  9646. m->side = WOLFSSL_NEITHER_END;
  9647. }
  9648. return m;
  9649. }
  9650. #endif /* WOLFSSL_ALLOW_TLSV10 */
  9651. /* Gets a WOLFSL_METHOD type that is not set as client or server
  9652. *
  9653. * Returns a pointer to a WOLFSSL_METHOD struct
  9654. */
  9655. WOLFSSL_METHOD* wolfTLSv1_1_method(void)
  9656. {
  9657. return wolfTLSv1_1_method_ex(NULL);
  9658. }
  9659. WOLFSSL_METHOD* wolfTLSv1_1_method_ex(void* heap)
  9660. {
  9661. WOLFSSL_METHOD* m;
  9662. WOLFSSL_ENTER("TLSv1_1_method");
  9663. #ifndef NO_WOLFSSL_CLIENT
  9664. m = wolfTLSv1_1_client_method_ex(heap);
  9665. #else
  9666. m = wolfTLSv1_1_server_method_ex(heap);
  9667. #endif
  9668. if (m != NULL) {
  9669. m->side = WOLFSSL_NEITHER_END;
  9670. }
  9671. return m;
  9672. }
  9673. #endif /* !NO_OLD_TLS */
  9674. #ifndef WOLFSSL_NO_TLS12
  9675. /* Gets a WOLFSL_METHOD type that is not set as client or server
  9676. *
  9677. * Returns a pointer to a WOLFSSL_METHOD struct
  9678. */
  9679. WOLFSSL_METHOD* wolfTLSv1_2_method(void)
  9680. {
  9681. return wolfTLSv1_2_method_ex(NULL);
  9682. }
  9683. WOLFSSL_METHOD* wolfTLSv1_2_method_ex(void* heap)
  9684. {
  9685. WOLFSSL_METHOD* m;
  9686. WOLFSSL_ENTER("TLSv1_2_method");
  9687. #ifndef NO_WOLFSSL_CLIENT
  9688. m = wolfTLSv1_2_client_method_ex(heap);
  9689. #else
  9690. m = wolfTLSv1_2_server_method_ex(heap);
  9691. #endif
  9692. if (m != NULL) {
  9693. m->side = WOLFSSL_NEITHER_END;
  9694. }
  9695. return m;
  9696. }
  9697. #endif /* !WOLFSSL_NO_TLS12 */
  9698. #ifdef WOLFSSL_TLS13
  9699. /* Gets a WOLFSL_METHOD type that is not set as client or server
  9700. *
  9701. * Returns a pointer to a WOLFSSL_METHOD struct
  9702. */
  9703. WOLFSSL_METHOD* wolfTLSv1_3_method(void)
  9704. {
  9705. return wolfTLSv1_3_method_ex(NULL);
  9706. }
  9707. WOLFSSL_METHOD* wolfTLSv1_3_method_ex(void* heap)
  9708. {
  9709. WOLFSSL_METHOD* m;
  9710. WOLFSSL_ENTER("TLSv1_3_method");
  9711. #ifndef NO_WOLFSSL_CLIENT
  9712. m = wolfTLSv1_3_client_method_ex(heap);
  9713. #else
  9714. m = wolfTLSv1_3_server_method_ex(heap);
  9715. #endif
  9716. if (m != NULL) {
  9717. m->side = WOLFSSL_NEITHER_END;
  9718. }
  9719. return m;
  9720. }
  9721. #endif /* WOLFSSL_TLS13 */
  9722. #ifdef WOLFSSL_DTLS
  9723. WOLFSSL_METHOD* wolfDTLS_method(void)
  9724. {
  9725. return wolfDTLS_method_ex(NULL);
  9726. }
  9727. WOLFSSL_METHOD* wolfDTLS_method_ex(void* heap)
  9728. {
  9729. WOLFSSL_METHOD* m;
  9730. WOLFSSL_ENTER("DTLS_method_ex");
  9731. #ifndef NO_WOLFSSL_CLIENT
  9732. m = wolfDTLS_client_method_ex(heap);
  9733. #else
  9734. m = wolfDTLS_server_method_ex(heap);
  9735. #endif
  9736. if (m != NULL) {
  9737. m->side = WOLFSSL_NEITHER_END;
  9738. }
  9739. return m;
  9740. }
  9741. #ifndef NO_OLD_TLS
  9742. WOLFSSL_METHOD* wolfDTLSv1_method(void)
  9743. {
  9744. return wolfDTLSv1_method_ex(NULL);
  9745. }
  9746. WOLFSSL_METHOD* wolfDTLSv1_method_ex(void* heap)
  9747. {
  9748. WOLFSSL_METHOD* m;
  9749. WOLFSSL_ENTER("DTLSv1_method_ex");
  9750. #ifndef NO_WOLFSSL_CLIENT
  9751. m = wolfDTLSv1_client_method_ex(heap);
  9752. #else
  9753. m = wolfDTLSv1_server_method_ex(heap);
  9754. #endif
  9755. if (m != NULL) {
  9756. m->side = WOLFSSL_NEITHER_END;
  9757. }
  9758. return m;
  9759. }
  9760. #endif /* !NO_OLD_TLS */
  9761. #ifndef WOLFSSL_NO_TLS12
  9762. WOLFSSL_METHOD* wolfDTLSv1_2_method(void)
  9763. {
  9764. return wolfDTLSv1_2_method_ex(NULL);
  9765. }
  9766. WOLFSSL_METHOD* wolfDTLSv1_2_method_ex(void* heap)
  9767. {
  9768. WOLFSSL_METHOD* m;
  9769. WOLFSSL_ENTER("DTLSv1_2_method");
  9770. #ifndef NO_WOLFSSL_CLIENT
  9771. m = wolfDTLSv1_2_client_method_ex(heap);
  9772. #else
  9773. m = wolfDTLSv1_2_server_method_ex(heap);
  9774. #endif
  9775. if (m != NULL) {
  9776. m->side = WOLFSSL_NEITHER_END;
  9777. }
  9778. return m;
  9779. }
  9780. #endif /* !WOLFSSL_NO_TLS12 */
  9781. #endif /* WOLFSSL_DTLS */
  9782. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  9783. #ifndef NO_WOLFSSL_SERVER
  9784. WOLFSSL_METHOD* wolfTLS_server_method(void)
  9785. {
  9786. return wolfTLS_server_method_ex(NULL);
  9787. }
  9788. WOLFSSL_METHOD* wolfTLS_server_method_ex(void* heap)
  9789. {
  9790. WOLFSSL_METHOD* method =
  9791. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  9792. heap, DYNAMIC_TYPE_METHOD);
  9793. (void)heap;
  9794. WOLFSSL_ENTER("TLS_server_method_ex");
  9795. if (method) {
  9796. #if defined(WOLFSSL_TLS13)
  9797. InitSSL_Method(method, MakeTLSv1_3());
  9798. #elif !defined(WOLFSSL_NO_TLS12)
  9799. InitSSL_Method(method, MakeTLSv1_2());
  9800. #elif !defined(NO_OLD_TLS)
  9801. InitSSL_Method(method, MakeTLSv1_1());
  9802. #elif defined(WOLFSSL_ALLOW_TLSV10)
  9803. InitSSL_Method(method, MakeTLSv1());
  9804. #else
  9805. #error No TLS version enabled!
  9806. #endif
  9807. method->downgrade = 1;
  9808. method->side = WOLFSSL_SERVER_END;
  9809. }
  9810. return method;
  9811. }
  9812. #ifndef NO_OLD_TLS
  9813. #ifdef WOLFSSL_ALLOW_TLSV10
  9814. WOLFSSL_METHOD* wolfTLSv1_server_method(void)
  9815. {
  9816. return wolfTLSv1_server_method_ex(NULL);
  9817. }
  9818. WOLFSSL_METHOD* wolfTLSv1_server_method_ex(void* heap)
  9819. {
  9820. WOLFSSL_METHOD* method =
  9821. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  9822. heap, DYNAMIC_TYPE_METHOD);
  9823. (void)heap;
  9824. WOLFSSL_ENTER("TLSv1_server_method_ex");
  9825. if (method) {
  9826. InitSSL_Method(method, MakeTLSv1());
  9827. method->side = WOLFSSL_SERVER_END;
  9828. }
  9829. return method;
  9830. }
  9831. #endif /* WOLFSSL_ALLOW_TLSV10 */
  9832. WOLFSSL_METHOD* wolfTLSv1_1_server_method(void)
  9833. {
  9834. return wolfTLSv1_1_server_method_ex(NULL);
  9835. }
  9836. WOLFSSL_METHOD* wolfTLSv1_1_server_method_ex(void* heap)
  9837. {
  9838. WOLFSSL_METHOD* method =
  9839. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  9840. heap, DYNAMIC_TYPE_METHOD);
  9841. (void)heap;
  9842. WOLFSSL_ENTER("TLSv1_1_server_method_ex");
  9843. if (method) {
  9844. InitSSL_Method(method, MakeTLSv1_1());
  9845. method->side = WOLFSSL_SERVER_END;
  9846. }
  9847. return method;
  9848. }
  9849. #endif /* !NO_OLD_TLS */
  9850. #ifndef WOLFSSL_NO_TLS12
  9851. WOLFSSL_ABI
  9852. WOLFSSL_METHOD* wolfTLSv1_2_server_method(void)
  9853. {
  9854. return wolfTLSv1_2_server_method_ex(NULL);
  9855. }
  9856. WOLFSSL_METHOD* wolfTLSv1_2_server_method_ex(void* heap)
  9857. {
  9858. WOLFSSL_METHOD* method =
  9859. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  9860. heap, DYNAMIC_TYPE_METHOD);
  9861. (void)heap;
  9862. WOLFSSL_ENTER("TLSv1_2_server_method_ex");
  9863. if (method) {
  9864. InitSSL_Method(method, MakeTLSv1_2());
  9865. method->side = WOLFSSL_SERVER_END;
  9866. }
  9867. return method;
  9868. }
  9869. #endif /* !WOLFSSL_NO_TLS12 */
  9870. #ifdef WOLFSSL_TLS13
  9871. /* The TLS v1.3 server method data.
  9872. *
  9873. * returns the method data for a TLS v1.3 server.
  9874. */
  9875. WOLFSSL_ABI
  9876. WOLFSSL_METHOD* wolfTLSv1_3_server_method(void)
  9877. {
  9878. return wolfTLSv1_3_server_method_ex(NULL);
  9879. }
  9880. /* The TLS v1.3 server method data.
  9881. *
  9882. * heap The heap used for allocation.
  9883. * returns the method data for a TLS v1.3 server.
  9884. */
  9885. WOLFSSL_METHOD* wolfTLSv1_3_server_method_ex(void* heap)
  9886. {
  9887. WOLFSSL_METHOD* method =
  9888. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  9889. heap, DYNAMIC_TYPE_METHOD);
  9890. (void)heap;
  9891. WOLFSSL_ENTER("TLSv1_3_server_method_ex");
  9892. if (method) {
  9893. InitSSL_Method(method, MakeTLSv1_3());
  9894. method->side = WOLFSSL_SERVER_END;
  9895. }
  9896. return method;
  9897. }
  9898. #endif /* WOLFSSL_TLS13 */
  9899. #ifdef WOLFSSL_DTLS
  9900. WOLFSSL_METHOD* wolfDTLS_server_method(void)
  9901. {
  9902. return wolfDTLS_server_method_ex(NULL);
  9903. }
  9904. WOLFSSL_METHOD* wolfDTLS_server_method_ex(void* heap)
  9905. {
  9906. WOLFSSL_METHOD* method =
  9907. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  9908. heap, DYNAMIC_TYPE_METHOD);
  9909. (void)heap;
  9910. WOLFSSL_ENTER("DTLS_server_method_ex");
  9911. if (method) {
  9912. #if !defined(WOLFSSL_NO_TLS12)
  9913. InitSSL_Method(method, MakeDTLSv1_2());
  9914. #elif !defined(NO_OLD_TLS)
  9915. InitSSL_Method(method, MakeDTLSv1());
  9916. #else
  9917. #error No DTLS version enabled!
  9918. #endif
  9919. method->downgrade = 1;
  9920. method->side = WOLFSSL_SERVER_END;
  9921. }
  9922. return method;
  9923. }
  9924. #ifndef NO_OLD_TLS
  9925. WOLFSSL_METHOD* wolfDTLSv1_server_method(void)
  9926. {
  9927. return wolfDTLSv1_server_method_ex(NULL);
  9928. }
  9929. WOLFSSL_METHOD* wolfDTLSv1_server_method_ex(void* heap)
  9930. {
  9931. WOLFSSL_METHOD* method =
  9932. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  9933. heap, DYNAMIC_TYPE_METHOD);
  9934. (void)heap;
  9935. WOLFSSL_ENTER("DTLSv1_server_method_ex");
  9936. if (method) {
  9937. InitSSL_Method(method, MakeDTLSv1());
  9938. method->side = WOLFSSL_SERVER_END;
  9939. }
  9940. return method;
  9941. }
  9942. #endif /* !NO_OLD_TLS */
  9943. #ifndef WOLFSSL_NO_TLS12
  9944. WOLFSSL_METHOD* wolfDTLSv1_2_server_method(void)
  9945. {
  9946. return wolfDTLSv1_2_server_method_ex(NULL);
  9947. }
  9948. WOLFSSL_METHOD* wolfDTLSv1_2_server_method_ex(void* heap)
  9949. {
  9950. WOLFSSL_METHOD* method =
  9951. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  9952. heap, DYNAMIC_TYPE_METHOD);
  9953. WOLFSSL_ENTER("DTLSv1_2_server_method_ex");
  9954. (void)heap;
  9955. if (method) {
  9956. InitSSL_Method(method, MakeDTLSv1_2());
  9957. method->side = WOLFSSL_SERVER_END;
  9958. }
  9959. (void)heap;
  9960. return method;
  9961. }
  9962. #endif /* !WOLFSSL_NO_TLS12 */
  9963. #endif /* WOLFSSL_DTLS */
  9964. #endif /* NO_WOLFSSL_SERVER */
  9965. #endif /* NO_TLS */
  9966. #endif /* WOLFCRYPT_ONLY */