ssl_tls.c 239 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702
  1. /*
  2. * SSLv3/TLSv1 shared functions
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /*
  20. * The SSL 3.0 specification was drafted by Netscape in 1996,
  21. * and became an IETF standard in 1999.
  22. *
  23. * http://wp.netscape.com/eng/ssl3/
  24. * http://www.ietf.org/rfc/rfc2246.txt
  25. * http://www.ietf.org/rfc/rfc4346.txt
  26. */
  27. #include "common.h"
  28. #if defined(MBEDTLS_SSL_TLS_C)
  29. #if defined(MBEDTLS_PLATFORM_C)
  30. #include "mbedtls/platform.h"
  31. #else
  32. #include <stdlib.h>
  33. #define mbedtls_calloc calloc
  34. #define mbedtls_free free
  35. #endif
  36. #include "mbedtls/ssl.h"
  37. #include "mbedtls/ssl_internal.h"
  38. #include "mbedtls/debug.h"
  39. #include "mbedtls/error.h"
  40. #include "mbedtls/platform_util.h"
  41. #include "mbedtls/version.h"
  42. #include "mbedtls/constant_time.h"
  43. #include <string.h>
  44. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  45. #include "mbedtls/psa_util.h"
  46. #include "psa/crypto.h"
  47. #endif
  48. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  49. #include "mbedtls/oid.h"
  50. #endif
  51. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  52. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  53. /* Top-level Connection ID API */
  54. int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
  55. size_t len,
  56. int ignore_other_cid )
  57. {
  58. if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
  59. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  60. if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
  61. ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
  62. {
  63. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  64. }
  65. conf->ignore_unexpected_cid = ignore_other_cid;
  66. conf->cid_len = len;
  67. return( 0 );
  68. }
  69. int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
  70. int enable,
  71. unsigned char const *own_cid,
  72. size_t own_cid_len )
  73. {
  74. if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  75. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  76. ssl->negotiate_cid = enable;
  77. if( enable == MBEDTLS_SSL_CID_DISABLED )
  78. {
  79. MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
  80. return( 0 );
  81. }
  82. MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
  83. MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
  84. if( own_cid_len != ssl->conf->cid_len )
  85. {
  86. MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
  87. (unsigned) own_cid_len,
  88. (unsigned) ssl->conf->cid_len ) );
  89. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  90. }
  91. memcpy( ssl->own_cid, own_cid, own_cid_len );
  92. /* Truncation is not an issue here because
  93. * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
  94. ssl->own_cid_len = (uint8_t) own_cid_len;
  95. return( 0 );
  96. }
  97. int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
  98. int *enabled,
  99. unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
  100. size_t *peer_cid_len )
  101. {
  102. *enabled = MBEDTLS_SSL_CID_DISABLED;
  103. if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
  104. ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
  105. {
  106. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  107. }
  108. /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
  109. * were used, but client and server requested the empty CID.
  110. * This is indistinguishable from not using the CID extension
  111. * in the first place. */
  112. if( ssl->transform_in->in_cid_len == 0 &&
  113. ssl->transform_in->out_cid_len == 0 )
  114. {
  115. return( 0 );
  116. }
  117. if( peer_cid_len != NULL )
  118. {
  119. *peer_cid_len = ssl->transform_in->out_cid_len;
  120. if( peer_cid != NULL )
  121. {
  122. memcpy( peer_cid, ssl->transform_in->out_cid,
  123. ssl->transform_in->out_cid_len );
  124. }
  125. }
  126. *enabled = MBEDTLS_SSL_CID_ENABLED;
  127. return( 0 );
  128. }
  129. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  130. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  131. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  132. /*
  133. * Convert max_fragment_length codes to length.
  134. * RFC 6066 says:
  135. * enum{
  136. * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
  137. * } MaxFragmentLength;
  138. * and we add 0 -> extension unused
  139. */
  140. static unsigned int ssl_mfl_code_to_length( int mfl )
  141. {
  142. switch( mfl )
  143. {
  144. case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
  145. return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
  146. case MBEDTLS_SSL_MAX_FRAG_LEN_512:
  147. return 512;
  148. case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
  149. return 1024;
  150. case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
  151. return 2048;
  152. case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
  153. return 4096;
  154. default:
  155. return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
  156. }
  157. }
  158. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  159. int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
  160. const mbedtls_ssl_session *src )
  161. {
  162. mbedtls_ssl_session_free( dst );
  163. memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
  164. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
  165. dst->ticket = NULL;
  166. #endif
  167. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  168. #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  169. if( src->peer_cert != NULL )
  170. {
  171. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  172. dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
  173. if( dst->peer_cert == NULL )
  174. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  175. mbedtls_x509_crt_init( dst->peer_cert );
  176. if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
  177. src->peer_cert->raw.len ) ) != 0 )
  178. {
  179. mbedtls_free( dst->peer_cert );
  180. dst->peer_cert = NULL;
  181. return( ret );
  182. }
  183. }
  184. #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  185. if( src->peer_cert_digest != NULL )
  186. {
  187. dst->peer_cert_digest =
  188. mbedtls_calloc( 1, src->peer_cert_digest_len );
  189. if( dst->peer_cert_digest == NULL )
  190. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  191. memcpy( dst->peer_cert_digest, src->peer_cert_digest,
  192. src->peer_cert_digest_len );
  193. dst->peer_cert_digest_type = src->peer_cert_digest_type;
  194. dst->peer_cert_digest_len = src->peer_cert_digest_len;
  195. }
  196. #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  197. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  198. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
  199. if( src->ticket != NULL )
  200. {
  201. dst->ticket = mbedtls_calloc( 1, src->ticket_len );
  202. if( dst->ticket == NULL )
  203. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  204. memcpy( dst->ticket, src->ticket, src->ticket_len );
  205. }
  206. #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
  207. return( 0 );
  208. }
  209. #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
  210. static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
  211. {
  212. unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
  213. if( resized_buffer == NULL )
  214. return -1;
  215. /* We want to copy len_new bytes when downsizing the buffer, and
  216. * len_old bytes when upsizing, so we choose the smaller of two sizes,
  217. * to fit one buffer into another. Size checks, ensuring that no data is
  218. * lost, are done outside of this function. */
  219. memcpy( resized_buffer, *buffer,
  220. ( len_new < *len_old ) ? len_new : *len_old );
  221. mbedtls_platform_zeroize( *buffer, *len_old );
  222. mbedtls_free( *buffer );
  223. *buffer = resized_buffer;
  224. *len_old = len_new;
  225. return 0;
  226. }
  227. static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing,
  228. size_t in_buf_new_len,
  229. size_t out_buf_new_len )
  230. {
  231. int modified = 0;
  232. size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
  233. size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
  234. if( ssl->in_buf != NULL )
  235. {
  236. written_in = ssl->in_msg - ssl->in_buf;
  237. iv_offset_in = ssl->in_iv - ssl->in_buf;
  238. len_offset_in = ssl->in_len - ssl->in_buf;
  239. if( downsizing ?
  240. ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
  241. ssl->in_buf_len < in_buf_new_len )
  242. {
  243. if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 )
  244. {
  245. MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
  246. }
  247. else
  248. {
  249. MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
  250. in_buf_new_len ) );
  251. modified = 1;
  252. }
  253. }
  254. }
  255. if( ssl->out_buf != NULL )
  256. {
  257. written_out = ssl->out_msg - ssl->out_buf;
  258. iv_offset_out = ssl->out_iv - ssl->out_buf;
  259. len_offset_out = ssl->out_len - ssl->out_buf;
  260. if( downsizing ?
  261. ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
  262. ssl->out_buf_len < out_buf_new_len )
  263. {
  264. if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 )
  265. {
  266. MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
  267. }
  268. else
  269. {
  270. MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
  271. out_buf_new_len ) );
  272. modified = 1;
  273. }
  274. }
  275. }
  276. if( modified )
  277. {
  278. /* Update pointers here to avoid doing it twice. */
  279. mbedtls_ssl_reset_in_out_pointers( ssl );
  280. /* Fields below might not be properly updated with record
  281. * splitting or with CID, so they are manually updated here. */
  282. ssl->out_msg = ssl->out_buf + written_out;
  283. ssl->out_len = ssl->out_buf + len_offset_out;
  284. ssl->out_iv = ssl->out_buf + iv_offset_out;
  285. ssl->in_msg = ssl->in_buf + written_in;
  286. ssl->in_len = ssl->in_buf + len_offset_in;
  287. ssl->in_iv = ssl->in_buf + iv_offset_in;
  288. }
  289. }
  290. #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
  291. /*
  292. * Key material generation
  293. */
  294. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  295. static int ssl3_prf( const unsigned char *secret, size_t slen,
  296. const char *label,
  297. const unsigned char *random, size_t rlen,
  298. unsigned char *dstbuf, size_t dlen )
  299. {
  300. int ret = 0;
  301. size_t i;
  302. mbedtls_md5_context md5;
  303. mbedtls_sha1_context sha1;
  304. unsigned char padding[16];
  305. unsigned char sha1sum[20];
  306. ((void)label);
  307. mbedtls_md5_init( &md5 );
  308. mbedtls_sha1_init( &sha1 );
  309. /*
  310. * SSLv3:
  311. * block =
  312. * MD5( secret + SHA1( 'A' + secret + random ) ) +
  313. * MD5( secret + SHA1( 'BB' + secret + random ) ) +
  314. * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
  315. * ...
  316. */
  317. for( i = 0; i < dlen / 16; i++ )
  318. {
  319. memset( padding, (unsigned char) ('A' + i), 1 + i );
  320. if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
  321. goto exit;
  322. if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
  323. goto exit;
  324. if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
  325. goto exit;
  326. if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
  327. goto exit;
  328. if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
  329. goto exit;
  330. if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
  331. goto exit;
  332. if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
  333. goto exit;
  334. if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
  335. goto exit;
  336. if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
  337. goto exit;
  338. }
  339. exit:
  340. mbedtls_md5_free( &md5 );
  341. mbedtls_sha1_free( &sha1 );
  342. mbedtls_platform_zeroize( padding, sizeof( padding ) );
  343. mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
  344. return( ret );
  345. }
  346. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  347. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
  348. static int tls1_prf( const unsigned char *secret, size_t slen,
  349. const char *label,
  350. const unsigned char *random, size_t rlen,
  351. unsigned char *dstbuf, size_t dlen )
  352. {
  353. size_t nb, hs;
  354. size_t i, j, k;
  355. const unsigned char *S1, *S2;
  356. unsigned char *tmp;
  357. size_t tmp_len = 0;
  358. unsigned char h_i[20];
  359. const mbedtls_md_info_t *md_info;
  360. mbedtls_md_context_t md_ctx;
  361. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  362. mbedtls_md_init( &md_ctx );
  363. tmp_len = 20 + strlen( label ) + rlen;
  364. tmp = mbedtls_calloc( 1, tmp_len );
  365. if( tmp == NULL )
  366. {
  367. ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
  368. goto exit;
  369. }
  370. hs = ( slen + 1 ) / 2;
  371. S1 = secret;
  372. S2 = secret + slen - hs;
  373. nb = strlen( label );
  374. memcpy( tmp + 20, label, nb );
  375. memcpy( tmp + 20 + nb, random, rlen );
  376. nb += rlen;
  377. /*
  378. * First compute P_md5(secret,label+random)[0..dlen]
  379. */
  380. if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
  381. {
  382. ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  383. goto exit;
  384. }
  385. if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
  386. {
  387. goto exit;
  388. }
  389. ret = mbedtls_md_hmac_starts( &md_ctx, S1, hs );
  390. if( ret != 0 )
  391. goto exit;
  392. ret = mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
  393. if( ret != 0 )
  394. goto exit;
  395. ret = mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
  396. if( ret != 0 )
  397. goto exit;
  398. for( i = 0; i < dlen; i += 16 )
  399. {
  400. ret = mbedtls_md_hmac_reset ( &md_ctx );
  401. if( ret != 0 )
  402. goto exit;
  403. ret = mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
  404. if( ret != 0 )
  405. goto exit;
  406. ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
  407. if( ret != 0 )
  408. goto exit;
  409. ret = mbedtls_md_hmac_reset ( &md_ctx );
  410. if( ret != 0 )
  411. goto exit;
  412. ret = mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
  413. if( ret != 0 )
  414. goto exit;
  415. ret = mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
  416. if( ret != 0 )
  417. goto exit;
  418. k = ( i + 16 > dlen ) ? dlen % 16 : 16;
  419. for( j = 0; j < k; j++ )
  420. dstbuf[i + j] = h_i[j];
  421. }
  422. mbedtls_md_free( &md_ctx );
  423. /*
  424. * XOR out with P_sha1(secret,label+random)[0..dlen]
  425. */
  426. if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
  427. {
  428. ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  429. goto exit;
  430. }
  431. if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
  432. {
  433. goto exit;
  434. }
  435. ret = mbedtls_md_hmac_starts( &md_ctx, S2, hs );
  436. if( ret != 0 )
  437. goto exit;
  438. ret = mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
  439. if( ret != 0 )
  440. goto exit;
  441. ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
  442. if( ret != 0 )
  443. goto exit;
  444. for( i = 0; i < dlen; i += 20 )
  445. {
  446. ret = mbedtls_md_hmac_reset ( &md_ctx );
  447. if( ret != 0 )
  448. goto exit;
  449. ret = mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
  450. if( ret != 0 )
  451. goto exit;
  452. ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
  453. if( ret != 0 )
  454. goto exit;
  455. ret = mbedtls_md_hmac_reset ( &md_ctx );
  456. if( ret != 0 )
  457. goto exit;
  458. ret = mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
  459. if( ret != 0 )
  460. goto exit;
  461. ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
  462. if( ret != 0 )
  463. goto exit;
  464. k = ( i + 20 > dlen ) ? dlen % 20 : 20;
  465. for( j = 0; j < k; j++ )
  466. dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
  467. }
  468. exit:
  469. mbedtls_md_free( &md_ctx );
  470. mbedtls_platform_zeroize( tmp, tmp_len );
  471. mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
  472. mbedtls_free( tmp );
  473. return( ret );
  474. }
  475. #endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
  476. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  477. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  478. static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
  479. psa_key_id_t key,
  480. psa_algorithm_t alg,
  481. const unsigned char* seed, size_t seed_length,
  482. const unsigned char* label, size_t label_length,
  483. size_t capacity )
  484. {
  485. psa_status_t status;
  486. status = psa_key_derivation_setup( derivation, alg );
  487. if( status != PSA_SUCCESS )
  488. return( status );
  489. if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
  490. {
  491. status = psa_key_derivation_input_bytes( derivation,
  492. PSA_KEY_DERIVATION_INPUT_SEED,
  493. seed, seed_length );
  494. if( status != PSA_SUCCESS )
  495. return( status );
  496. if( mbedtls_svc_key_id_is_null( key ) )
  497. {
  498. status = psa_key_derivation_input_bytes(
  499. derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
  500. NULL, 0 );
  501. }
  502. else
  503. {
  504. status = psa_key_derivation_input_key(
  505. derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key );
  506. }
  507. if( status != PSA_SUCCESS )
  508. return( status );
  509. status = psa_key_derivation_input_bytes( derivation,
  510. PSA_KEY_DERIVATION_INPUT_LABEL,
  511. label, label_length );
  512. if( status != PSA_SUCCESS )
  513. return( status );
  514. }
  515. else
  516. {
  517. return( PSA_ERROR_NOT_SUPPORTED );
  518. }
  519. status = psa_key_derivation_set_capacity( derivation, capacity );
  520. if( status != PSA_SUCCESS )
  521. return( status );
  522. return( PSA_SUCCESS );
  523. }
  524. static int tls_prf_generic( mbedtls_md_type_t md_type,
  525. const unsigned char *secret, size_t slen,
  526. const char *label,
  527. const unsigned char *random, size_t rlen,
  528. unsigned char *dstbuf, size_t dlen )
  529. {
  530. psa_status_t status;
  531. psa_algorithm_t alg;
  532. psa_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
  533. psa_key_derivation_operation_t derivation =
  534. PSA_KEY_DERIVATION_OPERATION_INIT;
  535. if( md_type == MBEDTLS_MD_SHA384 )
  536. alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
  537. else
  538. alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
  539. /* Normally a "secret" should be long enough to be impossible to
  540. * find by brute force, and in particular should not be empty. But
  541. * this PRF is also used to derive an IV, in particular in EAP-TLS,
  542. * and for this use case it makes sense to have a 0-length "secret".
  543. * Since the key API doesn't allow importing a key of length 0,
  544. * keep master_key=0, which setup_psa_key_derivation() understands
  545. * to mean a 0-length "secret" input. */
  546. if( slen != 0 )
  547. {
  548. psa_key_attributes_t key_attributes = psa_key_attributes_init();
  549. psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
  550. psa_set_key_algorithm( &key_attributes, alg );
  551. psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
  552. status = psa_import_key( &key_attributes, secret, slen, &master_key );
  553. if( status != PSA_SUCCESS )
  554. return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
  555. }
  556. status = setup_psa_key_derivation( &derivation,
  557. master_key, alg,
  558. random, rlen,
  559. (unsigned char const *) label,
  560. (size_t) strlen( label ),
  561. dlen );
  562. if( status != PSA_SUCCESS )
  563. {
  564. psa_key_derivation_abort( &derivation );
  565. psa_destroy_key( master_key );
  566. return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
  567. }
  568. status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
  569. if( status != PSA_SUCCESS )
  570. {
  571. psa_key_derivation_abort( &derivation );
  572. psa_destroy_key( master_key );
  573. return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
  574. }
  575. status = psa_key_derivation_abort( &derivation );
  576. if( status != PSA_SUCCESS )
  577. {
  578. psa_destroy_key( master_key );
  579. return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
  580. }
  581. if( ! mbedtls_svc_key_id_is_null( master_key ) )
  582. status = psa_destroy_key( master_key );
  583. if( status != PSA_SUCCESS )
  584. return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
  585. return( 0 );
  586. }
  587. #else /* MBEDTLS_USE_PSA_CRYPTO */
  588. static int tls_prf_generic( mbedtls_md_type_t md_type,
  589. const unsigned char *secret, size_t slen,
  590. const char *label,
  591. const unsigned char *random, size_t rlen,
  592. unsigned char *dstbuf, size_t dlen )
  593. {
  594. size_t nb;
  595. size_t i, j, k, md_len;
  596. unsigned char *tmp;
  597. size_t tmp_len = 0;
  598. unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
  599. const mbedtls_md_info_t *md_info;
  600. mbedtls_md_context_t md_ctx;
  601. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  602. mbedtls_md_init( &md_ctx );
  603. if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
  604. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  605. md_len = mbedtls_md_get_size( md_info );
  606. tmp_len = md_len + strlen( label ) + rlen;
  607. tmp = mbedtls_calloc( 1, tmp_len );
  608. if( tmp == NULL )
  609. {
  610. ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
  611. goto exit;
  612. }
  613. nb = strlen( label );
  614. memcpy( tmp + md_len, label, nb );
  615. memcpy( tmp + md_len + nb, random, rlen );
  616. nb += rlen;
  617. /*
  618. * Compute P_<hash>(secret, label + random)[0..dlen]
  619. */
  620. if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
  621. goto exit;
  622. ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen );
  623. if( ret != 0 )
  624. goto exit;
  625. ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
  626. if( ret != 0 )
  627. goto exit;
  628. ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
  629. if( ret != 0 )
  630. goto exit;
  631. for( i = 0; i < dlen; i += md_len )
  632. {
  633. ret = mbedtls_md_hmac_reset ( &md_ctx );
  634. if( ret != 0 )
  635. goto exit;
  636. ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
  637. if( ret != 0 )
  638. goto exit;
  639. ret = mbedtls_md_hmac_finish( &md_ctx, h_i );
  640. if( ret != 0 )
  641. goto exit;
  642. ret = mbedtls_md_hmac_reset ( &md_ctx );
  643. if( ret != 0 )
  644. goto exit;
  645. ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
  646. if( ret != 0 )
  647. goto exit;
  648. ret = mbedtls_md_hmac_finish( &md_ctx, tmp );
  649. if( ret != 0 )
  650. goto exit;
  651. k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
  652. for( j = 0; j < k; j++ )
  653. dstbuf[i + j] = h_i[j];
  654. }
  655. exit:
  656. mbedtls_md_free( &md_ctx );
  657. mbedtls_platform_zeroize( tmp, tmp_len );
  658. mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
  659. mbedtls_free( tmp );
  660. return( ret );
  661. }
  662. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  663. #if defined(MBEDTLS_SHA256_C)
  664. static int tls_prf_sha256( const unsigned char *secret, size_t slen,
  665. const char *label,
  666. const unsigned char *random, size_t rlen,
  667. unsigned char *dstbuf, size_t dlen )
  668. {
  669. return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
  670. label, random, rlen, dstbuf, dlen ) );
  671. }
  672. #endif /* MBEDTLS_SHA256_C */
  673. #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
  674. static int tls_prf_sha384( const unsigned char *secret, size_t slen,
  675. const char *label,
  676. const unsigned char *random, size_t rlen,
  677. unsigned char *dstbuf, size_t dlen )
  678. {
  679. return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
  680. label, random, rlen, dstbuf, dlen ) );
  681. }
  682. #endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
  683. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  684. static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
  685. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  686. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  687. static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
  688. #endif
  689. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  690. static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
  691. static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
  692. #endif
  693. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
  694. static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char*, size_t * );
  695. static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
  696. #endif
  697. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  698. #if defined(MBEDTLS_SHA256_C)
  699. static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
  700. static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * );
  701. static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
  702. #endif
  703. #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
  704. static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
  705. static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * );
  706. static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
  707. #endif
  708. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  709. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
  710. defined(MBEDTLS_USE_PSA_CRYPTO)
  711. static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
  712. {
  713. if( ssl->conf->f_psk != NULL )
  714. {
  715. /* If we've used a callback to select the PSK,
  716. * the static configuration is irrelevant. */
  717. if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
  718. return( 1 );
  719. return( 0 );
  720. }
  721. if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) )
  722. return( 1 );
  723. return( 0 );
  724. }
  725. #endif /* MBEDTLS_USE_PSA_CRYPTO &&
  726. MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
  727. #if defined(MBEDTLS_SSL_EXPORT_KEYS)
  728. static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
  729. {
  730. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  731. if( tls_prf == ssl3_prf )
  732. {
  733. return( MBEDTLS_SSL_TLS_PRF_SSL3 );
  734. }
  735. else
  736. #endif
  737. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
  738. if( tls_prf == tls1_prf )
  739. {
  740. return( MBEDTLS_SSL_TLS_PRF_TLS1 );
  741. }
  742. else
  743. #endif
  744. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  745. #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
  746. if( tls_prf == tls_prf_sha384 )
  747. {
  748. return( MBEDTLS_SSL_TLS_PRF_SHA384 );
  749. }
  750. else
  751. #endif
  752. #if defined(MBEDTLS_SHA256_C)
  753. if( tls_prf == tls_prf_sha256 )
  754. {
  755. return( MBEDTLS_SSL_TLS_PRF_SHA256 );
  756. }
  757. else
  758. #endif
  759. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  760. return( MBEDTLS_SSL_TLS_PRF_NONE );
  761. }
  762. #endif /* MBEDTLS_SSL_EXPORT_KEYS */
  763. int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
  764. const unsigned char *secret, size_t slen,
  765. const char *label,
  766. const unsigned char *random, size_t rlen,
  767. unsigned char *dstbuf, size_t dlen )
  768. {
  769. mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
  770. switch( prf )
  771. {
  772. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  773. case MBEDTLS_SSL_TLS_PRF_SSL3:
  774. tls_prf = ssl3_prf;
  775. break;
  776. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  777. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
  778. case MBEDTLS_SSL_TLS_PRF_TLS1:
  779. tls_prf = tls1_prf;
  780. break;
  781. #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
  782. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  783. #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
  784. case MBEDTLS_SSL_TLS_PRF_SHA384:
  785. tls_prf = tls_prf_sha384;
  786. break;
  787. #endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
  788. #if defined(MBEDTLS_SHA256_C)
  789. case MBEDTLS_SSL_TLS_PRF_SHA256:
  790. tls_prf = tls_prf_sha256;
  791. break;
  792. #endif /* MBEDTLS_SHA256_C */
  793. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  794. default:
  795. return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
  796. }
  797. return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
  798. }
  799. /* Type for the TLS PRF */
  800. typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
  801. const unsigned char *, size_t,
  802. unsigned char *, size_t);
  803. /*
  804. * Populate a transform structure with session keys and all the other
  805. * necessary information.
  806. *
  807. * Parameters:
  808. * - [in/out]: transform: structure to populate
  809. * [in] must be just initialised with mbedtls_ssl_transform_init()
  810. * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
  811. * - [in] ciphersuite
  812. * - [in] master
  813. * - [in] encrypt_then_mac
  814. * - [in] trunc_hmac
  815. * - [in] compression
  816. * - [in] tls_prf: pointer to PRF to use for key derivation
  817. * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
  818. * - [in] minor_ver: SSL/TLS minor version
  819. * - [in] endpoint: client or server
  820. * - [in] ssl: optionally used for:
  821. * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context (non-const)
  822. * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
  823. * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
  824. */
  825. static int ssl_populate_transform( mbedtls_ssl_transform *transform,
  826. int ciphersuite,
  827. const unsigned char master[48],
  828. #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
  829. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  830. int encrypt_then_mac,
  831. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  832. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  833. int trunc_hmac,
  834. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  835. #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
  836. #if defined(MBEDTLS_ZLIB_SUPPORT)
  837. int compression,
  838. #endif
  839. ssl_tls_prf_t tls_prf,
  840. const unsigned char randbytes[64],
  841. int minor_ver,
  842. unsigned endpoint,
  843. #if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
  844. const
  845. #endif
  846. mbedtls_ssl_context *ssl )
  847. {
  848. int ret = 0;
  849. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  850. int psa_fallthrough;
  851. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  852. unsigned char keyblk[256];
  853. unsigned char *key1;
  854. unsigned char *key2;
  855. unsigned char *mac_enc;
  856. unsigned char *mac_dec;
  857. size_t mac_key_len = 0;
  858. size_t iv_copy_len;
  859. unsigned keylen;
  860. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  861. const mbedtls_cipher_info_t *cipher_info;
  862. const mbedtls_md_info_t *md_info;
  863. #if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
  864. !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
  865. !defined(MBEDTLS_DEBUG_C)
  866. ssl = NULL; /* make sure we don't use it except for those cases */
  867. (void) ssl;
  868. #endif
  869. /*
  870. * Some data just needs copying into the structure
  871. */
  872. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
  873. defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
  874. transform->encrypt_then_mac = encrypt_then_mac;
  875. #endif
  876. transform->minor_ver = minor_ver;
  877. #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
  878. memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
  879. #endif
  880. /*
  881. * Get various info structures
  882. */
  883. ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
  884. if( ciphersuite_info == NULL )
  885. {
  886. MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
  887. ciphersuite ) );
  888. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  889. }
  890. cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
  891. if( cipher_info == NULL )
  892. {
  893. MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
  894. ciphersuite_info->cipher ) );
  895. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  896. }
  897. md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
  898. if( md_info == NULL )
  899. {
  900. MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found",
  901. (unsigned) ciphersuite_info->mac ) );
  902. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  903. }
  904. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  905. /* Copy own and peer's CID if the use of the CID
  906. * extension has been negotiated. */
  907. if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
  908. {
  909. MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
  910. transform->in_cid_len = ssl->own_cid_len;
  911. memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
  912. MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
  913. transform->in_cid_len );
  914. transform->out_cid_len = ssl->handshake->peer_cid_len;
  915. memcpy( transform->out_cid, ssl->handshake->peer_cid,
  916. ssl->handshake->peer_cid_len );
  917. MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
  918. transform->out_cid_len );
  919. }
  920. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  921. /*
  922. * Compute key block using the PRF
  923. */
  924. ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
  925. if( ret != 0 )
  926. {
  927. MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
  928. return( ret );
  929. }
  930. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
  931. mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
  932. MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
  933. MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
  934. MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
  935. /*
  936. * Determine the appropriate key, IV and MAC length.
  937. */
  938. keylen = cipher_info->key_bitlen / 8;
  939. #if defined(MBEDTLS_GCM_C) || \
  940. defined(MBEDTLS_CCM_C) || \
  941. defined(MBEDTLS_CHACHAPOLY_C)
  942. if( cipher_info->mode == MBEDTLS_MODE_GCM ||
  943. cipher_info->mode == MBEDTLS_MODE_CCM ||
  944. cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
  945. {
  946. size_t explicit_ivlen;
  947. transform->maclen = 0;
  948. mac_key_len = 0;
  949. transform->taglen =
  950. ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
  951. /* All modes haves 96-bit IVs, but the length of the static parts vary
  952. * with mode and version:
  953. * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
  954. * (to be concatenated with a dynamically chosen IV of 8 Bytes)
  955. * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
  956. * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
  957. * sequence number).
  958. */
  959. transform->ivlen = 12;
  960. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  961. if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
  962. {
  963. transform->fixed_ivlen = 12;
  964. }
  965. else
  966. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  967. {
  968. if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
  969. transform->fixed_ivlen = 12;
  970. else
  971. transform->fixed_ivlen = 4;
  972. }
  973. /* Minimum length of encrypted record */
  974. explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
  975. transform->minlen = explicit_ivlen + transform->taglen;
  976. }
  977. else
  978. #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
  979. #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
  980. if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
  981. cipher_info->mode == MBEDTLS_MODE_CBC )
  982. {
  983. /* Initialize HMAC contexts */
  984. if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
  985. ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
  986. {
  987. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
  988. goto end;
  989. }
  990. /* Get MAC length */
  991. mac_key_len = mbedtls_md_get_size( md_info );
  992. transform->maclen = mac_key_len;
  993. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  994. /*
  995. * If HMAC is to be truncated, we shall keep the leftmost bytes,
  996. * (rfc 6066 page 13 or rfc 2104 section 4),
  997. * so we only need to adjust the length here.
  998. */
  999. if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
  1000. {
  1001. transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
  1002. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
  1003. /* Fall back to old, non-compliant version of the truncated
  1004. * HMAC implementation which also truncates the key
  1005. * (Mbed TLS versions from 1.3 to 2.6.0) */
  1006. mac_key_len = transform->maclen;
  1007. #endif
  1008. }
  1009. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  1010. /* IV length */
  1011. transform->ivlen = cipher_info->iv_size;
  1012. /* Minimum length */
  1013. if( cipher_info->mode == MBEDTLS_MODE_STREAM )
  1014. transform->minlen = transform->maclen;
  1015. else
  1016. {
  1017. /*
  1018. * GenericBlockCipher:
  1019. * 1. if EtM is in use: one block plus MAC
  1020. * otherwise: * first multiple of blocklen greater than maclen
  1021. * 2. IV except for SSL3 and TLS 1.0
  1022. */
  1023. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  1024. if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
  1025. {
  1026. transform->minlen = transform->maclen
  1027. + cipher_info->block_size;
  1028. }
  1029. else
  1030. #endif
  1031. {
  1032. transform->minlen = transform->maclen
  1033. + cipher_info->block_size
  1034. - transform->maclen % cipher_info->block_size;
  1035. }
  1036. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
  1037. if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
  1038. minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
  1039. ; /* No need to adjust minlen */
  1040. else
  1041. #endif
  1042. #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
  1043. if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
  1044. minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  1045. {
  1046. transform->minlen += transform->ivlen;
  1047. }
  1048. else
  1049. #endif
  1050. {
  1051. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  1052. ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  1053. goto end;
  1054. }
  1055. }
  1056. }
  1057. else
  1058. #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
  1059. {
  1060. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  1061. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  1062. }
  1063. MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
  1064. (unsigned) keylen,
  1065. (unsigned) transform->minlen,
  1066. (unsigned) transform->ivlen,
  1067. (unsigned) transform->maclen ) );
  1068. /*
  1069. * Finally setup the cipher contexts, IVs and MAC secrets.
  1070. */
  1071. #if defined(MBEDTLS_SSL_CLI_C)
  1072. if( endpoint == MBEDTLS_SSL_IS_CLIENT )
  1073. {
  1074. key1 = keyblk + mac_key_len * 2;
  1075. key2 = keyblk + mac_key_len * 2 + keylen;
  1076. mac_enc = keyblk;
  1077. mac_dec = keyblk + mac_key_len;
  1078. /*
  1079. * This is not used in TLS v1.1.
  1080. */
  1081. iv_copy_len = ( transform->fixed_ivlen ) ?
  1082. transform->fixed_ivlen : transform->ivlen;
  1083. memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
  1084. memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
  1085. iv_copy_len );
  1086. }
  1087. else
  1088. #endif /* MBEDTLS_SSL_CLI_C */
  1089. #if defined(MBEDTLS_SSL_SRV_C)
  1090. if( endpoint == MBEDTLS_SSL_IS_SERVER )
  1091. {
  1092. key1 = keyblk + mac_key_len * 2 + keylen;
  1093. key2 = keyblk + mac_key_len * 2;
  1094. mac_enc = keyblk + mac_key_len;
  1095. mac_dec = keyblk;
  1096. /*
  1097. * This is not used in TLS v1.1.
  1098. */
  1099. iv_copy_len = ( transform->fixed_ivlen ) ?
  1100. transform->fixed_ivlen : transform->ivlen;
  1101. memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
  1102. memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
  1103. iv_copy_len );
  1104. }
  1105. else
  1106. #endif /* MBEDTLS_SSL_SRV_C */
  1107. {
  1108. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  1109. ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  1110. goto end;
  1111. }
  1112. #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
  1113. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  1114. if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
  1115. {
  1116. if( mac_key_len > sizeof( transform->mac_enc ) )
  1117. {
  1118. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  1119. ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  1120. goto end;
  1121. }
  1122. memcpy( transform->mac_enc, mac_enc, mac_key_len );
  1123. memcpy( transform->mac_dec, mac_dec, mac_key_len );
  1124. }
  1125. else
  1126. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  1127. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  1128. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  1129. if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
  1130. {
  1131. /* For HMAC-based ciphersuites, initialize the HMAC transforms.
  1132. For AEAD-based ciphersuites, there is nothing to do here. */
  1133. if( mac_key_len != 0 )
  1134. {
  1135. ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc,
  1136. mac_enc, mac_key_len );
  1137. if( ret != 0 )
  1138. goto end;
  1139. ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec,
  1140. mac_dec, mac_key_len );
  1141. if( ret != 0 )
  1142. goto end;
  1143. }
  1144. }
  1145. else
  1146. #endif
  1147. {
  1148. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  1149. ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  1150. goto end;
  1151. }
  1152. #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
  1153. #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
  1154. if( mbedtls_ssl_hw_record_init != NULL )
  1155. {
  1156. ret = 0;
  1157. MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
  1158. if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
  1159. transform->iv_enc, transform->iv_dec,
  1160. iv_copy_len,
  1161. mac_enc, mac_dec,
  1162. mac_key_len ) ) != 0 )
  1163. {
  1164. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
  1165. ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
  1166. goto end;
  1167. }
  1168. }
  1169. #else
  1170. ((void) mac_dec);
  1171. ((void) mac_enc);
  1172. #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
  1173. #if defined(MBEDTLS_SSL_EXPORT_KEYS)
  1174. if( ssl->conf->f_export_keys != NULL )
  1175. {
  1176. ssl->conf->f_export_keys( ssl->conf->p_export_keys,
  1177. master, keyblk,
  1178. mac_key_len, keylen,
  1179. iv_copy_len );
  1180. }
  1181. if( ssl->conf->f_export_keys_ext != NULL )
  1182. {
  1183. ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
  1184. master, keyblk,
  1185. mac_key_len, keylen,
  1186. iv_copy_len,
  1187. randbytes + 32,
  1188. randbytes,
  1189. tls_prf_get_type( tls_prf ) );
  1190. }
  1191. #endif
  1192. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1193. /* Only use PSA-based ciphers for TLS-1.2.
  1194. * That's relevant at least for TLS-1.0, where
  1195. * we assume that mbedtls_cipher_crypt() updates
  1196. * the structure field for the IV, which the PSA-based
  1197. * implementation currently doesn't. */
  1198. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  1199. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  1200. {
  1201. ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
  1202. cipher_info, transform->taglen );
  1203. if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
  1204. {
  1205. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
  1206. goto end;
  1207. }
  1208. if( ret == 0 )
  1209. {
  1210. MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
  1211. psa_fallthrough = 0;
  1212. }
  1213. else
  1214. {
  1215. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
  1216. psa_fallthrough = 1;
  1217. }
  1218. }
  1219. else
  1220. psa_fallthrough = 1;
  1221. #else
  1222. psa_fallthrough = 1;
  1223. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  1224. if( psa_fallthrough == 1 )
  1225. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1226. if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
  1227. cipher_info ) ) != 0 )
  1228. {
  1229. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
  1230. goto end;
  1231. }
  1232. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1233. /* Only use PSA-based ciphers for TLS-1.2.
  1234. * That's relevant at least for TLS-1.0, where
  1235. * we assume that mbedtls_cipher_crypt() updates
  1236. * the structure field for the IV, which the PSA-based
  1237. * implementation currently doesn't. */
  1238. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  1239. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  1240. {
  1241. ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
  1242. cipher_info, transform->taglen );
  1243. if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
  1244. {
  1245. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
  1246. goto end;
  1247. }
  1248. if( ret == 0 )
  1249. {
  1250. MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
  1251. psa_fallthrough = 0;
  1252. }
  1253. else
  1254. {
  1255. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
  1256. psa_fallthrough = 1;
  1257. }
  1258. }
  1259. else
  1260. psa_fallthrough = 1;
  1261. #else
  1262. psa_fallthrough = 1;
  1263. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  1264. if( psa_fallthrough == 1 )
  1265. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1266. if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
  1267. cipher_info ) ) != 0 )
  1268. {
  1269. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
  1270. goto end;
  1271. }
  1272. if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
  1273. cipher_info->key_bitlen,
  1274. MBEDTLS_ENCRYPT ) ) != 0 )
  1275. {
  1276. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
  1277. goto end;
  1278. }
  1279. if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
  1280. cipher_info->key_bitlen,
  1281. MBEDTLS_DECRYPT ) ) != 0 )
  1282. {
  1283. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
  1284. goto end;
  1285. }
  1286. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1287. if( cipher_info->mode == MBEDTLS_MODE_CBC )
  1288. {
  1289. if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
  1290. MBEDTLS_PADDING_NONE ) ) != 0 )
  1291. {
  1292. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
  1293. goto end;
  1294. }
  1295. if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
  1296. MBEDTLS_PADDING_NONE ) ) != 0 )
  1297. {
  1298. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
  1299. goto end;
  1300. }
  1301. }
  1302. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  1303. /* Initialize Zlib contexts */
  1304. #if defined(MBEDTLS_ZLIB_SUPPORT)
  1305. if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
  1306. {
  1307. MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
  1308. memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
  1309. memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
  1310. if( deflateInit( &transform->ctx_deflate,
  1311. Z_DEFAULT_COMPRESSION ) != Z_OK ||
  1312. inflateInit( &transform->ctx_inflate ) != Z_OK )
  1313. {
  1314. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
  1315. ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
  1316. goto end;
  1317. }
  1318. }
  1319. #endif /* MBEDTLS_ZLIB_SUPPORT */
  1320. end:
  1321. mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
  1322. return( ret );
  1323. }
  1324. /*
  1325. * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
  1326. *
  1327. * Inputs:
  1328. * - SSL/TLS minor version
  1329. * - hash associated with the ciphersuite (only used by TLS 1.2)
  1330. *
  1331. * Outputs:
  1332. * - the tls_prf, calc_verify and calc_finished members of handshake structure
  1333. */
  1334. static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
  1335. int minor_ver,
  1336. mbedtls_md_type_t hash )
  1337. {
  1338. #if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || \
  1339. !( defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384) )
  1340. (void) hash;
  1341. #endif
  1342. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  1343. if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
  1344. {
  1345. handshake->tls_prf = ssl3_prf;
  1346. handshake->calc_verify = ssl_calc_verify_ssl;
  1347. handshake->calc_finished = ssl_calc_finished_ssl;
  1348. }
  1349. else
  1350. #endif
  1351. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
  1352. if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
  1353. {
  1354. handshake->tls_prf = tls1_prf;
  1355. handshake->calc_verify = ssl_calc_verify_tls;
  1356. handshake->calc_finished = ssl_calc_finished_tls;
  1357. }
  1358. else
  1359. #endif
  1360. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  1361. #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
  1362. if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
  1363. hash == MBEDTLS_MD_SHA384 )
  1364. {
  1365. handshake->tls_prf = tls_prf_sha384;
  1366. handshake->calc_verify = ssl_calc_verify_tls_sha384;
  1367. handshake->calc_finished = ssl_calc_finished_tls_sha384;
  1368. }
  1369. else
  1370. #endif
  1371. #if defined(MBEDTLS_SHA256_C)
  1372. if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  1373. {
  1374. handshake->tls_prf = tls_prf_sha256;
  1375. handshake->calc_verify = ssl_calc_verify_tls_sha256;
  1376. handshake->calc_finished = ssl_calc_finished_tls_sha256;
  1377. }
  1378. else
  1379. #endif
  1380. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  1381. {
  1382. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  1383. }
  1384. return( 0 );
  1385. }
  1386. /*
  1387. * Compute master secret if needed
  1388. *
  1389. * Parameters:
  1390. * [in/out] handshake
  1391. * [in] resume, premaster, extended_ms, calc_verify, tls_prf
  1392. * (PSA-PSK) ciphersuite_info, psk_opaque
  1393. * [out] premaster (cleared)
  1394. * [out] master
  1395. * [in] ssl: optionally used for debugging, EMS and PSA-PSK
  1396. * debug: conf->f_dbg, conf->p_dbg
  1397. * EMS: passed to calc_verify (debug + (SSL3) session_negotiate)
  1398. * PSA-PSA: minor_ver, conf
  1399. */
  1400. static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
  1401. unsigned char *master,
  1402. const mbedtls_ssl_context *ssl )
  1403. {
  1404. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1405. /* cf. RFC 5246, Section 8.1:
  1406. * "The master secret is always exactly 48 bytes in length." */
  1407. size_t const master_secret_len = 48;
  1408. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  1409. unsigned char session_hash[48];
  1410. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  1411. /* The label for the KDF used for key expansion.
  1412. * This is either "master secret" or "extended master secret"
  1413. * depending on whether the Extended Master Secret extension
  1414. * is used. */
  1415. char const *lbl = "master secret";
  1416. /* The salt for the KDF used for key expansion.
  1417. * - If the Extended Master Secret extension is not used,
  1418. * this is ClientHello.Random + ServerHello.Random
  1419. * (see Sect. 8.1 in RFC 5246).
  1420. * - If the Extended Master Secret extension is used,
  1421. * this is the transcript of the handshake so far.
  1422. * (see Sect. 4 in RFC 7627). */
  1423. unsigned char const *salt = handshake->randbytes;
  1424. size_t salt_len = 64;
  1425. #if !defined(MBEDTLS_DEBUG_C) && \
  1426. !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
  1427. !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
  1428. defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
  1429. ssl = NULL; /* make sure we don't use it except for those cases */
  1430. (void) ssl;
  1431. #endif
  1432. if( handshake->resume != 0 )
  1433. {
  1434. MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
  1435. return( 0 );
  1436. }
  1437. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  1438. if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
  1439. {
  1440. lbl = "extended master secret";
  1441. salt = session_hash;
  1442. handshake->calc_verify( ssl, session_hash, &salt_len );
  1443. MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
  1444. session_hash, salt_len );
  1445. }
  1446. #endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
  1447. #if defined(MBEDTLS_USE_PSA_CRYPTO) && \
  1448. defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
  1449. if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
  1450. ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
  1451. ssl_use_opaque_psk( ssl ) == 1 )
  1452. {
  1453. /* Perform PSK-to-MS expansion in a single step. */
  1454. psa_status_t status;
  1455. psa_algorithm_t alg;
  1456. psa_key_id_t psk;
  1457. psa_key_derivation_operation_t derivation =
  1458. PSA_KEY_DERIVATION_OPERATION_INIT;
  1459. mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
  1460. MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
  1461. psk = mbedtls_ssl_get_opaque_psk( ssl );
  1462. if( hash_alg == MBEDTLS_MD_SHA384 )
  1463. alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
  1464. else
  1465. alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
  1466. status = setup_psa_key_derivation( &derivation, psk, alg,
  1467. salt, salt_len,
  1468. (unsigned char const *) lbl,
  1469. (size_t) strlen( lbl ),
  1470. master_secret_len );
  1471. if( status != PSA_SUCCESS )
  1472. {
  1473. psa_key_derivation_abort( &derivation );
  1474. return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
  1475. }
  1476. status = psa_key_derivation_output_bytes( &derivation,
  1477. master,
  1478. master_secret_len );
  1479. if( status != PSA_SUCCESS )
  1480. {
  1481. psa_key_derivation_abort( &derivation );
  1482. return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
  1483. }
  1484. status = psa_key_derivation_abort( &derivation );
  1485. if( status != PSA_SUCCESS )
  1486. return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
  1487. }
  1488. else
  1489. #endif
  1490. {
  1491. ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
  1492. lbl, salt, salt_len,
  1493. master,
  1494. master_secret_len );
  1495. if( ret != 0 )
  1496. {
  1497. MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
  1498. return( ret );
  1499. }
  1500. MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
  1501. handshake->premaster,
  1502. handshake->pmslen );
  1503. mbedtls_platform_zeroize( handshake->premaster,
  1504. sizeof(handshake->premaster) );
  1505. }
  1506. return( 0 );
  1507. }
  1508. int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
  1509. {
  1510. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1511. const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
  1512. ssl->handshake->ciphersuite_info;
  1513. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
  1514. /* Set PRF, calc_verify and calc_finished function pointers */
  1515. ret = ssl_set_handshake_prfs( ssl->handshake,
  1516. ssl->minor_ver,
  1517. ciphersuite_info->mac );
  1518. if( ret != 0 )
  1519. {
  1520. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
  1521. return( ret );
  1522. }
  1523. /* Compute master secret if needed */
  1524. ret = ssl_compute_master( ssl->handshake,
  1525. ssl->session_negotiate->master,
  1526. ssl );
  1527. if( ret != 0 )
  1528. {
  1529. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
  1530. return( ret );
  1531. }
  1532. /* Swap the client and server random values:
  1533. * - MS derivation wanted client+server (RFC 5246 8.1)
  1534. * - key derivation wants server+client (RFC 5246 6.3) */
  1535. {
  1536. unsigned char tmp[64];
  1537. memcpy( tmp, ssl->handshake->randbytes, 64 );
  1538. memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
  1539. memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
  1540. mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
  1541. }
  1542. /* Populate transform structure */
  1543. ret = ssl_populate_transform( ssl->transform_negotiate,
  1544. ssl->session_negotiate->ciphersuite,
  1545. ssl->session_negotiate->master,
  1546. #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
  1547. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  1548. ssl->session_negotiate->encrypt_then_mac,
  1549. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  1550. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  1551. ssl->session_negotiate->trunc_hmac,
  1552. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  1553. #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
  1554. #if defined(MBEDTLS_ZLIB_SUPPORT)
  1555. ssl->session_negotiate->compression,
  1556. #endif
  1557. ssl->handshake->tls_prf,
  1558. ssl->handshake->randbytes,
  1559. ssl->minor_ver,
  1560. ssl->conf->endpoint,
  1561. ssl );
  1562. if( ret != 0 )
  1563. {
  1564. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
  1565. return( ret );
  1566. }
  1567. /* We no longer need Server/ClientHello.random values */
  1568. mbedtls_platform_zeroize( ssl->handshake->randbytes,
  1569. sizeof( ssl->handshake->randbytes ) );
  1570. /* Allocate compression buffer */
  1571. #if defined(MBEDTLS_ZLIB_SUPPORT)
  1572. if( ssl->session_negotiate->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
  1573. ssl->compress_buf == NULL )
  1574. {
  1575. MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
  1576. ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
  1577. if( ssl->compress_buf == NULL )
  1578. {
  1579. MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
  1580. MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
  1581. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  1582. }
  1583. }
  1584. #endif
  1585. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
  1586. return( 0 );
  1587. }
  1588. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  1589. void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
  1590. unsigned char *hash,
  1591. size_t *hlen )
  1592. {
  1593. mbedtls_md5_context md5;
  1594. mbedtls_sha1_context sha1;
  1595. unsigned char pad_1[48];
  1596. unsigned char pad_2[48];
  1597. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
  1598. mbedtls_md5_init( &md5 );
  1599. mbedtls_sha1_init( &sha1 );
  1600. mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
  1601. mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
  1602. memset( pad_1, 0x36, 48 );
  1603. memset( pad_2, 0x5C, 48 );
  1604. mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
  1605. mbedtls_md5_update_ret( &md5, pad_1, 48 );
  1606. mbedtls_md5_finish_ret( &md5, hash );
  1607. mbedtls_md5_starts_ret( &md5 );
  1608. mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
  1609. mbedtls_md5_update_ret( &md5, pad_2, 48 );
  1610. mbedtls_md5_update_ret( &md5, hash, 16 );
  1611. mbedtls_md5_finish_ret( &md5, hash );
  1612. mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
  1613. mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
  1614. mbedtls_sha1_finish_ret( &sha1, hash + 16 );
  1615. mbedtls_sha1_starts_ret( &sha1 );
  1616. mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
  1617. mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
  1618. mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
  1619. mbedtls_sha1_finish_ret( &sha1, hash + 16 );
  1620. *hlen = 36;
  1621. MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
  1622. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
  1623. mbedtls_md5_free( &md5 );
  1624. mbedtls_sha1_free( &sha1 );
  1625. return;
  1626. }
  1627. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  1628. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
  1629. void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
  1630. unsigned char *hash,
  1631. size_t *hlen )
  1632. {
  1633. mbedtls_md5_context md5;
  1634. mbedtls_sha1_context sha1;
  1635. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
  1636. mbedtls_md5_init( &md5 );
  1637. mbedtls_sha1_init( &sha1 );
  1638. mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
  1639. mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
  1640. mbedtls_md5_finish_ret( &md5, hash );
  1641. mbedtls_sha1_finish_ret( &sha1, hash + 16 );
  1642. *hlen = 36;
  1643. MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
  1644. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
  1645. mbedtls_md5_free( &md5 );
  1646. mbedtls_sha1_free( &sha1 );
  1647. return;
  1648. }
  1649. #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
  1650. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  1651. #if defined(MBEDTLS_SHA256_C)
  1652. void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
  1653. unsigned char *hash,
  1654. size_t *hlen )
  1655. {
  1656. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1657. size_t hash_size;
  1658. psa_status_t status;
  1659. psa_hash_operation_t sha256_psa = psa_hash_operation_init();
  1660. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
  1661. status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
  1662. if( status != PSA_SUCCESS )
  1663. {
  1664. MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
  1665. return;
  1666. }
  1667. status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
  1668. if( status != PSA_SUCCESS )
  1669. {
  1670. MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
  1671. return;
  1672. }
  1673. *hlen = 32;
  1674. MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
  1675. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
  1676. #else
  1677. mbedtls_sha256_context sha256;
  1678. mbedtls_sha256_init( &sha256 );
  1679. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
  1680. mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
  1681. mbedtls_sha256_finish_ret( &sha256, hash );
  1682. *hlen = 32;
  1683. MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
  1684. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
  1685. mbedtls_sha256_free( &sha256 );
  1686. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1687. return;
  1688. }
  1689. #endif /* MBEDTLS_SHA256_C */
  1690. #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
  1691. void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
  1692. unsigned char *hash,
  1693. size_t *hlen )
  1694. {
  1695. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1696. size_t hash_size;
  1697. psa_status_t status;
  1698. psa_hash_operation_t sha384_psa = psa_hash_operation_init();
  1699. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
  1700. status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
  1701. if( status != PSA_SUCCESS )
  1702. {
  1703. MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
  1704. return;
  1705. }
  1706. status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
  1707. if( status != PSA_SUCCESS )
  1708. {
  1709. MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
  1710. return;
  1711. }
  1712. *hlen = 48;
  1713. MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
  1714. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
  1715. #else
  1716. mbedtls_sha512_context sha512;
  1717. mbedtls_sha512_init( &sha512 );
  1718. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
  1719. mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
  1720. mbedtls_sha512_finish_ret( &sha512, hash );
  1721. *hlen = 48;
  1722. MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
  1723. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
  1724. mbedtls_sha512_free( &sha512 );
  1725. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1726. return;
  1727. }
  1728. #endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
  1729. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  1730. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  1731. int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
  1732. {
  1733. unsigned char *p = ssl->handshake->premaster;
  1734. unsigned char *end = p + sizeof( ssl->handshake->premaster );
  1735. const unsigned char *psk = NULL;
  1736. size_t psk_len = 0;
  1737. if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len )
  1738. == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
  1739. {
  1740. /*
  1741. * This should never happen because the existence of a PSK is always
  1742. * checked before calling this function
  1743. */
  1744. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  1745. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  1746. }
  1747. /*
  1748. * PMS = struct {
  1749. * opaque other_secret<0..2^16-1>;
  1750. * opaque psk<0..2^16-1>;
  1751. * };
  1752. * with "other_secret" depending on the particular key exchange
  1753. */
  1754. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
  1755. if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
  1756. {
  1757. if( end - p < 2 )
  1758. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  1759. MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
  1760. p += 2;
  1761. if( end < p || (size_t)( end - p ) < psk_len )
  1762. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  1763. memset( p, 0, psk_len );
  1764. p += psk_len;
  1765. }
  1766. else
  1767. #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
  1768. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  1769. if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
  1770. {
  1771. /*
  1772. * other_secret already set by the ClientKeyExchange message,
  1773. * and is 48 bytes long
  1774. */
  1775. if( end - p < 2 )
  1776. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  1777. *p++ = 0;
  1778. *p++ = 48;
  1779. p += 48;
  1780. }
  1781. else
  1782. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
  1783. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  1784. if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
  1785. {
  1786. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1787. size_t len;
  1788. /* Write length only when we know the actual value */
  1789. if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
  1790. p + 2, end - ( p + 2 ), &len,
  1791. ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  1792. {
  1793. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
  1794. return( ret );
  1795. }
  1796. MBEDTLS_PUT_UINT16_BE( len, p, 0 );
  1797. p += 2 + len;
  1798. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
  1799. }
  1800. else
  1801. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  1802. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  1803. if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
  1804. {
  1805. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1806. size_t zlen;
  1807. if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
  1808. p + 2, end - ( p + 2 ),
  1809. ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  1810. {
  1811. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
  1812. return( ret );
  1813. }
  1814. MBEDTLS_PUT_UINT16_BE( zlen, p, 0 );
  1815. p += 2 + zlen;
  1816. MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
  1817. MBEDTLS_DEBUG_ECDH_Z );
  1818. }
  1819. else
  1820. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  1821. {
  1822. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  1823. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  1824. }
  1825. /* opaque psk<0..2^16-1>; */
  1826. if( end - p < 2 )
  1827. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  1828. MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 );
  1829. p += 2;
  1830. if( end < p || (size_t)( end - p ) < psk_len )
  1831. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  1832. memcpy( p, psk, psk_len );
  1833. p += psk_len;
  1834. ssl->handshake->pmslen = p - ssl->handshake->premaster;
  1835. return( 0 );
  1836. }
  1837. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  1838. #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
  1839. static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
  1840. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1841. int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
  1842. {
  1843. /* If renegotiation is not enforced, retransmit until we would reach max
  1844. * timeout if we were using the usual handshake doubling scheme */
  1845. if( ssl->conf->renego_max_records < 0 )
  1846. {
  1847. uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
  1848. unsigned char doublings = 1;
  1849. while( ratio != 0 )
  1850. {
  1851. ++doublings;
  1852. ratio >>= 1;
  1853. }
  1854. if( ++ssl->renego_records_seen > doublings )
  1855. {
  1856. MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
  1857. return( 0 );
  1858. }
  1859. }
  1860. return( ssl_write_hello_request( ssl ) );
  1861. }
  1862. #endif
  1863. #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
  1864. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1865. static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
  1866. {
  1867. #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  1868. if( session->peer_cert != NULL )
  1869. {
  1870. mbedtls_x509_crt_free( session->peer_cert );
  1871. mbedtls_free( session->peer_cert );
  1872. session->peer_cert = NULL;
  1873. }
  1874. #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  1875. if( session->peer_cert_digest != NULL )
  1876. {
  1877. /* Zeroization is not necessary. */
  1878. mbedtls_free( session->peer_cert_digest );
  1879. session->peer_cert_digest = NULL;
  1880. session->peer_cert_digest_type = MBEDTLS_MD_NONE;
  1881. session->peer_cert_digest_len = 0;
  1882. }
  1883. #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  1884. }
  1885. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  1886. /*
  1887. * Handshake functions
  1888. */
  1889. #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  1890. /* No certificate support -> dummy functions */
  1891. int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
  1892. {
  1893. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  1894. ssl->handshake->ciphersuite_info;
  1895. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
  1896. if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
  1897. {
  1898. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
  1899. ssl->state++;
  1900. return( 0 );
  1901. }
  1902. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  1903. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  1904. }
  1905. int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
  1906. {
  1907. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  1908. ssl->handshake->ciphersuite_info;
  1909. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
  1910. if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
  1911. {
  1912. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
  1913. ssl->state++;
  1914. return( 0 );
  1915. }
  1916. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  1917. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  1918. }
  1919. #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  1920. /* Some certificate support -> implement write and parse */
  1921. int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
  1922. {
  1923. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  1924. size_t i, n;
  1925. const mbedtls_x509_crt *crt;
  1926. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  1927. ssl->handshake->ciphersuite_info;
  1928. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
  1929. if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
  1930. {
  1931. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
  1932. ssl->state++;
  1933. return( 0 );
  1934. }
  1935. #if defined(MBEDTLS_SSL_CLI_C)
  1936. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
  1937. {
  1938. if( ssl->client_auth == 0 )
  1939. {
  1940. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
  1941. ssl->state++;
  1942. return( 0 );
  1943. }
  1944. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  1945. /*
  1946. * If using SSLv3 and got no cert, send an Alert message
  1947. * (otherwise an empty Certificate message will be sent).
  1948. */
  1949. if( mbedtls_ssl_own_cert( ssl ) == NULL &&
  1950. ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
  1951. {
  1952. ssl->out_msglen = 2;
  1953. ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
  1954. ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
  1955. ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
  1956. MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
  1957. goto write_msg;
  1958. }
  1959. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  1960. }
  1961. #endif /* MBEDTLS_SSL_CLI_C */
  1962. #if defined(MBEDTLS_SSL_SRV_C)
  1963. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
  1964. {
  1965. if( mbedtls_ssl_own_cert( ssl ) == NULL )
  1966. {
  1967. MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
  1968. return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
  1969. }
  1970. }
  1971. #endif
  1972. MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
  1973. /*
  1974. * 0 . 0 handshake type
  1975. * 1 . 3 handshake length
  1976. * 4 . 6 length of all certs
  1977. * 7 . 9 length of cert. 1
  1978. * 10 . n-1 peer certificate
  1979. * n . n+2 length of cert. 2
  1980. * n+3 . ... upper level cert, etc.
  1981. */
  1982. i = 7;
  1983. crt = mbedtls_ssl_own_cert( ssl );
  1984. while( crt != NULL )
  1985. {
  1986. n = crt->raw.len;
  1987. if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
  1988. {
  1989. MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET
  1990. " > %" MBEDTLS_PRINTF_SIZET,
  1991. i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
  1992. return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
  1993. }
  1994. ssl->out_msg[i ] = MBEDTLS_BYTE_2( n );
  1995. ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n );
  1996. ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n );
  1997. i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
  1998. i += n; crt = crt->next;
  1999. }
  2000. ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 );
  2001. ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 );
  2002. ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 );
  2003. ssl->out_msglen = i;
  2004. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  2005. ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
  2006. #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
  2007. write_msg:
  2008. #endif
  2009. ssl->state++;
  2010. if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
  2011. {
  2012. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
  2013. return( ret );
  2014. }
  2015. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
  2016. return( ret );
  2017. }
  2018. #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
  2019. #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  2020. static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
  2021. unsigned char *crt_buf,
  2022. size_t crt_buf_len )
  2023. {
  2024. mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
  2025. if( peer_crt == NULL )
  2026. return( -1 );
  2027. if( peer_crt->raw.len != crt_buf_len )
  2028. return( -1 );
  2029. return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
  2030. }
  2031. #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  2032. static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
  2033. unsigned char *crt_buf,
  2034. size_t crt_buf_len )
  2035. {
  2036. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2037. unsigned char const * const peer_cert_digest =
  2038. ssl->session->peer_cert_digest;
  2039. mbedtls_md_type_t const peer_cert_digest_type =
  2040. ssl->session->peer_cert_digest_type;
  2041. mbedtls_md_info_t const * const digest_info =
  2042. mbedtls_md_info_from_type( peer_cert_digest_type );
  2043. unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
  2044. size_t digest_len;
  2045. if( peer_cert_digest == NULL || digest_info == NULL )
  2046. return( -1 );
  2047. digest_len = mbedtls_md_get_size( digest_info );
  2048. if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
  2049. return( -1 );
  2050. ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
  2051. if( ret != 0 )
  2052. return( -1 );
  2053. return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
  2054. }
  2055. #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  2056. #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
  2057. /*
  2058. * Once the certificate message is read, parse it into a cert chain and
  2059. * perform basic checks, but leave actual verification to the caller
  2060. */
  2061. static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
  2062. mbedtls_x509_crt *chain )
  2063. {
  2064. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2065. #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
  2066. int crt_cnt=0;
  2067. #endif
  2068. size_t i, n;
  2069. uint8_t alert;
  2070. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  2071. {
  2072. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
  2073. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2074. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  2075. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  2076. }
  2077. if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
  2078. ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
  2079. {
  2080. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
  2081. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2082. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  2083. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
  2084. }
  2085. i = mbedtls_ssl_hs_hdr_len( ssl );
  2086. /*
  2087. * Same message structure as in mbedtls_ssl_write_certificate()
  2088. */
  2089. n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
  2090. if( ssl->in_msg[i] != 0 ||
  2091. ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
  2092. {
  2093. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
  2094. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2095. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  2096. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
  2097. }
  2098. /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
  2099. i += 3;
  2100. /* Iterate through and parse the CRTs in the provided chain. */
  2101. while( i < ssl->in_hslen )
  2102. {
  2103. /* Check that there's room for the next CRT's length fields. */
  2104. if ( i + 3 > ssl->in_hslen ) {
  2105. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
  2106. mbedtls_ssl_send_alert_message( ssl,
  2107. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2108. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  2109. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
  2110. }
  2111. /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
  2112. * anything beyond 2**16 ~ 64K. */
  2113. if( ssl->in_msg[i] != 0 )
  2114. {
  2115. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
  2116. mbedtls_ssl_send_alert_message( ssl,
  2117. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2118. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  2119. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
  2120. }
  2121. /* Read length of the next CRT in the chain. */
  2122. n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
  2123. | (unsigned int) ssl->in_msg[i + 2];
  2124. i += 3;
  2125. if( n < 128 || i + n > ssl->in_hslen )
  2126. {
  2127. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
  2128. mbedtls_ssl_send_alert_message( ssl,
  2129. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2130. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  2131. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
  2132. }
  2133. /* Check if we're handling the first CRT in the chain. */
  2134. #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
  2135. if( crt_cnt++ == 0 &&
  2136. ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
  2137. ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
  2138. {
  2139. /* During client-side renegotiation, check that the server's
  2140. * end-CRTs hasn't changed compared to the initial handshake,
  2141. * mitigating the triple handshake attack. On success, reuse
  2142. * the original end-CRT instead of parsing it again. */
  2143. MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
  2144. if( ssl_check_peer_crt_unchanged( ssl,
  2145. &ssl->in_msg[i],
  2146. n ) != 0 )
  2147. {
  2148. MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
  2149. mbedtls_ssl_send_alert_message( ssl,
  2150. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2151. MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
  2152. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
  2153. }
  2154. /* Now we can safely free the original chain. */
  2155. ssl_clear_peer_cert( ssl->session );
  2156. }
  2157. #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
  2158. /* Parse the next certificate in the chain. */
  2159. #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  2160. ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
  2161. #else
  2162. /* If we don't need to store the CRT chain permanently, parse
  2163. * it in-place from the input buffer instead of making a copy. */
  2164. ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
  2165. #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  2166. switch( ret )
  2167. {
  2168. case 0: /*ok*/
  2169. case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
  2170. /* Ignore certificate with an unknown algorithm: maybe a
  2171. prior certificate was already trusted. */
  2172. break;
  2173. case MBEDTLS_ERR_X509_ALLOC_FAILED:
  2174. alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
  2175. goto crt_parse_der_failed;
  2176. case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
  2177. alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
  2178. goto crt_parse_der_failed;
  2179. default:
  2180. alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
  2181. crt_parse_der_failed:
  2182. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
  2183. MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
  2184. return( ret );
  2185. }
  2186. i += n;
  2187. }
  2188. MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
  2189. return( 0 );
  2190. }
  2191. #if defined(MBEDTLS_SSL_SRV_C)
  2192. static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
  2193. {
  2194. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
  2195. return( -1 );
  2196. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  2197. /*
  2198. * Check if the client sent an empty certificate
  2199. */
  2200. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
  2201. {
  2202. if( ssl->in_msglen == 2 &&
  2203. ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
  2204. ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
  2205. ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
  2206. {
  2207. MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
  2208. return( 0 );
  2209. }
  2210. return( -1 );
  2211. }
  2212. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  2213. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  2214. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2215. if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
  2216. ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
  2217. ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
  2218. memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
  2219. {
  2220. MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
  2221. return( 0 );
  2222. }
  2223. return( -1 );
  2224. #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
  2225. MBEDTLS_SSL_PROTO_TLS1_2 */
  2226. }
  2227. #endif /* MBEDTLS_SSL_SRV_C */
  2228. /* Check if a certificate message is expected.
  2229. * Return either
  2230. * - SSL_CERTIFICATE_EXPECTED, or
  2231. * - SSL_CERTIFICATE_SKIP
  2232. * indicating whether a Certificate message is expected or not.
  2233. */
  2234. #define SSL_CERTIFICATE_EXPECTED 0
  2235. #define SSL_CERTIFICATE_SKIP 1
  2236. static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
  2237. int authmode )
  2238. {
  2239. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2240. ssl->handshake->ciphersuite_info;
  2241. if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
  2242. return( SSL_CERTIFICATE_SKIP );
  2243. #if defined(MBEDTLS_SSL_SRV_C)
  2244. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
  2245. {
  2246. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
  2247. return( SSL_CERTIFICATE_SKIP );
  2248. if( authmode == MBEDTLS_SSL_VERIFY_NONE )
  2249. {
  2250. ssl->session_negotiate->verify_result =
  2251. MBEDTLS_X509_BADCERT_SKIP_VERIFY;
  2252. return( SSL_CERTIFICATE_SKIP );
  2253. }
  2254. }
  2255. #else
  2256. ((void) authmode);
  2257. #endif /* MBEDTLS_SSL_SRV_C */
  2258. return( SSL_CERTIFICATE_EXPECTED );
  2259. }
  2260. static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
  2261. int authmode,
  2262. mbedtls_x509_crt *chain,
  2263. void *rs_ctx )
  2264. {
  2265. int ret = 0;
  2266. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2267. ssl->handshake->ciphersuite_info;
  2268. int have_ca_chain = 0;
  2269. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
  2270. void *p_vrfy;
  2271. if( authmode == MBEDTLS_SSL_VERIFY_NONE )
  2272. return( 0 );
  2273. if( ssl->f_vrfy != NULL )
  2274. {
  2275. MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
  2276. f_vrfy = ssl->f_vrfy;
  2277. p_vrfy = ssl->p_vrfy;
  2278. }
  2279. else
  2280. {
  2281. MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
  2282. f_vrfy = ssl->conf->f_vrfy;
  2283. p_vrfy = ssl->conf->p_vrfy;
  2284. }
  2285. /*
  2286. * Main check: verify certificate
  2287. */
  2288. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  2289. if( ssl->conf->f_ca_cb != NULL )
  2290. {
  2291. ((void) rs_ctx);
  2292. have_ca_chain = 1;
  2293. MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
  2294. ret = mbedtls_x509_crt_verify_with_ca_cb(
  2295. chain,
  2296. ssl->conf->f_ca_cb,
  2297. ssl->conf->p_ca_cb,
  2298. ssl->conf->cert_profile,
  2299. ssl->hostname,
  2300. &ssl->session_negotiate->verify_result,
  2301. f_vrfy, p_vrfy );
  2302. }
  2303. else
  2304. #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
  2305. {
  2306. mbedtls_x509_crt *ca_chain;
  2307. mbedtls_x509_crl *ca_crl;
  2308. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  2309. if( ssl->handshake->sni_ca_chain != NULL )
  2310. {
  2311. ca_chain = ssl->handshake->sni_ca_chain;
  2312. ca_crl = ssl->handshake->sni_ca_crl;
  2313. }
  2314. else
  2315. #endif
  2316. {
  2317. ca_chain = ssl->conf->ca_chain;
  2318. ca_crl = ssl->conf->ca_crl;
  2319. }
  2320. if( ca_chain != NULL )
  2321. have_ca_chain = 1;
  2322. ret = mbedtls_x509_crt_verify_restartable(
  2323. chain,
  2324. ca_chain, ca_crl,
  2325. ssl->conf->cert_profile,
  2326. ssl->hostname,
  2327. &ssl->session_negotiate->verify_result,
  2328. f_vrfy, p_vrfy, rs_ctx );
  2329. }
  2330. if( ret != 0 )
  2331. {
  2332. MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
  2333. }
  2334. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2335. if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
  2336. return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
  2337. #endif
  2338. /*
  2339. * Secondary checks: always done, but change 'ret' only if it was 0
  2340. */
  2341. #if defined(MBEDTLS_ECP_C)
  2342. {
  2343. const mbedtls_pk_context *pk = &chain->pk;
  2344. /* If certificate uses an EC key, make sure the curve is OK */
  2345. if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
  2346. mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
  2347. {
  2348. ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
  2349. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
  2350. if( ret == 0 )
  2351. ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
  2352. }
  2353. }
  2354. #endif /* MBEDTLS_ECP_C */
  2355. if( mbedtls_ssl_check_cert_usage( chain,
  2356. ciphersuite_info,
  2357. ! ssl->conf->endpoint,
  2358. &ssl->session_negotiate->verify_result ) != 0 )
  2359. {
  2360. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
  2361. if( ret == 0 )
  2362. ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
  2363. }
  2364. /* mbedtls_x509_crt_verify_with_profile is supposed to report a
  2365. * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
  2366. * with details encoded in the verification flags. All other kinds
  2367. * of error codes, including those from the user provided f_vrfy
  2368. * functions, are treated as fatal and lead to a failure of
  2369. * ssl_parse_certificate even if verification was optional. */
  2370. if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
  2371. ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
  2372. ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
  2373. {
  2374. ret = 0;
  2375. }
  2376. if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
  2377. {
  2378. MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
  2379. ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
  2380. }
  2381. if( 1 == ssl->conf->ignore_cert_flag )
  2382. {
  2383. ret = 0;
  2384. MBEDTLS_SSL_DEBUG_MSG(2, ("<= quectel ignore cert flag"));
  2385. }
  2386. if( ret != 0 )
  2387. {
  2388. uint8_t alert;
  2389. /* The certificate may have been rejected for several reasons.
  2390. Pick one and send the corresponding alert. Which alert to send
  2391. may be a subject of debate in some cases. */
  2392. if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
  2393. alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
  2394. else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
  2395. alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
  2396. else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
  2397. alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
  2398. else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
  2399. alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
  2400. else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
  2401. alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
  2402. else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
  2403. alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
  2404. else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
  2405. alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
  2406. else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
  2407. alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
  2408. else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
  2409. alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
  2410. else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
  2411. alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
  2412. else
  2413. alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
  2414. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2415. alert );
  2416. }
  2417. #if defined(MBEDTLS_DEBUG_C)
  2418. if( ssl->session_negotiate->verify_result != 0 )
  2419. {
  2420. MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
  2421. (unsigned int) ssl->session_negotiate->verify_result ) );
  2422. }
  2423. else
  2424. {
  2425. MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
  2426. }
  2427. #endif /* MBEDTLS_DEBUG_C */
  2428. return( ret );
  2429. }
  2430. #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  2431. static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
  2432. unsigned char *start, size_t len )
  2433. {
  2434. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2435. /* Remember digest of the peer's end-CRT. */
  2436. ssl->session_negotiate->peer_cert_digest =
  2437. mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
  2438. if( ssl->session_negotiate->peer_cert_digest == NULL )
  2439. {
  2440. MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
  2441. MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
  2442. mbedtls_ssl_send_alert_message( ssl,
  2443. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2444. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
  2445. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  2446. }
  2447. ret = mbedtls_md( mbedtls_md_info_from_type(
  2448. MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
  2449. start, len,
  2450. ssl->session_negotiate->peer_cert_digest );
  2451. ssl->session_negotiate->peer_cert_digest_type =
  2452. MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
  2453. ssl->session_negotiate->peer_cert_digest_len =
  2454. MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
  2455. return( ret );
  2456. }
  2457. static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
  2458. unsigned char *start, size_t len )
  2459. {
  2460. unsigned char *end = start + len;
  2461. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2462. /* Make a copy of the peer's raw public key. */
  2463. mbedtls_pk_init( &ssl->handshake->peer_pubkey );
  2464. ret = mbedtls_pk_parse_subpubkey( &start, end,
  2465. &ssl->handshake->peer_pubkey );
  2466. if( ret != 0 )
  2467. {
  2468. /* We should have parsed the public key before. */
  2469. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2470. }
  2471. return( 0 );
  2472. }
  2473. #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  2474. #ifdef CONFIG_QUEC_PROJECT_FEATURE_SSL
  2475. void quec_mbedtls_ssl_set_ignore_cert_flag(mbedtls_ssl_config *conf, unsigned int ignore_cert_flag)
  2476. {
  2477. conf->ignore_cert_flag = ignore_cert_flag;
  2478. }
  2479. #endif
  2480. int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
  2481. {
  2482. int ret = 0;
  2483. int crt_expected;
  2484. #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  2485. const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
  2486. ? ssl->handshake->sni_authmode
  2487. : ssl->conf->authmode;
  2488. #else
  2489. const int authmode = ssl->conf->authmode;
  2490. #endif
  2491. void *rs_ctx = NULL;
  2492. mbedtls_x509_crt *chain = NULL;
  2493. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
  2494. crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
  2495. if( crt_expected == SSL_CERTIFICATE_SKIP )
  2496. {
  2497. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
  2498. goto exit;
  2499. }
  2500. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2501. if( ssl->handshake->ecrs_enabled &&
  2502. ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
  2503. {
  2504. chain = ssl->handshake->ecrs_peer_cert;
  2505. ssl->handshake->ecrs_peer_cert = NULL;
  2506. goto crt_verify;
  2507. }
  2508. #endif
  2509. if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
  2510. {
  2511. /* mbedtls_ssl_read_record may have sent an alert already. We
  2512. let it decide whether to alert. */
  2513. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  2514. goto exit;
  2515. }
  2516. #if defined(MBEDTLS_SSL_SRV_C)
  2517. if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
  2518. {
  2519. ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
  2520. if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
  2521. ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
  2522. goto exit;
  2523. }
  2524. #endif /* MBEDTLS_SSL_SRV_C */
  2525. /* Clear existing peer CRT structure in case we tried to
  2526. * reuse a session but it failed, and allocate a new one. */
  2527. ssl_clear_peer_cert( ssl->session_negotiate );
  2528. chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
  2529. if( chain == NULL )
  2530. {
  2531. MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
  2532. sizeof( mbedtls_x509_crt ) ) );
  2533. mbedtls_ssl_send_alert_message( ssl,
  2534. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2535. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
  2536. ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
  2537. goto exit;
  2538. }
  2539. mbedtls_x509_crt_init( chain );
  2540. ret = ssl_parse_certificate_chain( ssl, chain );
  2541. if( ret != 0 )
  2542. goto exit;
  2543. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2544. if( ssl->handshake->ecrs_enabled)
  2545. ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
  2546. crt_verify:
  2547. if( ssl->handshake->ecrs_enabled)
  2548. rs_ctx = &ssl->handshake->ecrs_ctx;
  2549. #endif
  2550. ret = ssl_parse_certificate_verify( ssl, authmode,
  2551. chain, rs_ctx );
  2552. if( ret != 0 )
  2553. goto exit;
  2554. #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  2555. {
  2556. unsigned char *crt_start, *pk_start;
  2557. size_t crt_len, pk_len;
  2558. /* We parse the CRT chain without copying, so
  2559. * these pointers point into the input buffer,
  2560. * and are hence still valid after freeing the
  2561. * CRT chain. */
  2562. crt_start = chain->raw.p;
  2563. crt_len = chain->raw.len;
  2564. pk_start = chain->pk_raw.p;
  2565. pk_len = chain->pk_raw.len;
  2566. /* Free the CRT structures before computing
  2567. * digest and copying the peer's public key. */
  2568. mbedtls_x509_crt_free( chain );
  2569. mbedtls_free( chain );
  2570. chain = NULL;
  2571. ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
  2572. if( ret != 0 )
  2573. goto exit;
  2574. ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
  2575. if( ret != 0 )
  2576. goto exit;
  2577. }
  2578. #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  2579. /* Pass ownership to session structure. */
  2580. ssl->session_negotiate->peer_cert = chain;
  2581. chain = NULL;
  2582. #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  2583. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
  2584. exit:
  2585. if( ret == 0 )
  2586. ssl->state++;
  2587. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2588. if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
  2589. {
  2590. ssl->handshake->ecrs_peer_cert = chain;
  2591. chain = NULL;
  2592. }
  2593. #endif
  2594. if( chain != NULL )
  2595. {
  2596. mbedtls_x509_crt_free( chain );
  2597. mbedtls_free( chain );
  2598. }
  2599. return( ret );
  2600. }
  2601. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  2602. void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
  2603. const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
  2604. {
  2605. ((void) ciphersuite_info);
  2606. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  2607. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  2608. if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
  2609. ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
  2610. else
  2611. #endif
  2612. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2613. #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
  2614. if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
  2615. ssl->handshake->update_checksum = ssl_update_checksum_sha384;
  2616. else
  2617. #endif
  2618. #if defined(MBEDTLS_SHA256_C)
  2619. if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
  2620. ssl->handshake->update_checksum = ssl_update_checksum_sha256;
  2621. else
  2622. #endif
  2623. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  2624. {
  2625. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2626. return;
  2627. }
  2628. }
  2629. void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
  2630. {
  2631. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  2632. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  2633. mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
  2634. mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
  2635. #endif
  2636. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2637. #if defined(MBEDTLS_SHA256_C)
  2638. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2639. psa_hash_abort( &ssl->handshake->fin_sha256_psa );
  2640. psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
  2641. #else
  2642. mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
  2643. #endif
  2644. #endif
  2645. #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
  2646. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2647. psa_hash_abort( &ssl->handshake->fin_sha384_psa );
  2648. psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
  2649. #else
  2650. mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
  2651. #endif
  2652. #endif
  2653. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  2654. }
  2655. static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
  2656. const unsigned char *buf, size_t len )
  2657. {
  2658. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  2659. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  2660. mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
  2661. mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
  2662. #endif
  2663. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2664. #if defined(MBEDTLS_SHA256_C)
  2665. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2666. psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
  2667. #else
  2668. mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
  2669. #endif
  2670. #endif
  2671. #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
  2672. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2673. psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
  2674. #else
  2675. mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
  2676. #endif
  2677. #endif
  2678. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  2679. }
  2680. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  2681. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  2682. static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
  2683. const unsigned char *buf, size_t len )
  2684. {
  2685. mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
  2686. mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
  2687. }
  2688. #endif
  2689. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2690. #if defined(MBEDTLS_SHA256_C)
  2691. static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
  2692. const unsigned char *buf, size_t len )
  2693. {
  2694. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2695. psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
  2696. #else
  2697. mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
  2698. #endif
  2699. }
  2700. #endif
  2701. #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
  2702. static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
  2703. const unsigned char *buf, size_t len )
  2704. {
  2705. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2706. psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
  2707. #else
  2708. mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
  2709. #endif
  2710. }
  2711. #endif
  2712. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  2713. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  2714. static void ssl_calc_finished_ssl(
  2715. mbedtls_ssl_context *ssl, unsigned char *buf, int from )
  2716. {
  2717. const char *sender;
  2718. mbedtls_md5_context md5;
  2719. mbedtls_sha1_context sha1;
  2720. unsigned char padbuf[48];
  2721. unsigned char md5sum[16];
  2722. unsigned char sha1sum[20];
  2723. mbedtls_ssl_session *session = ssl->session_negotiate;
  2724. if( !session )
  2725. session = ssl->session;
  2726. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
  2727. mbedtls_md5_init( &md5 );
  2728. mbedtls_sha1_init( &sha1 );
  2729. mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
  2730. mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
  2731. /*
  2732. * SSLv3:
  2733. * hash =
  2734. * MD5( master + pad2 +
  2735. * MD5( handshake + sender + master + pad1 ) )
  2736. * + SHA1( master + pad2 +
  2737. * SHA1( handshake + sender + master + pad1 ) )
  2738. */
  2739. #if !defined(MBEDTLS_MD5_ALT)
  2740. MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
  2741. md5.state, sizeof( md5.state ) );
  2742. #endif
  2743. #if !defined(MBEDTLS_SHA1_ALT)
  2744. MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
  2745. sha1.state, sizeof( sha1.state ) );
  2746. #endif
  2747. sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
  2748. : "SRVR";
  2749. memset( padbuf, 0x36, 48 );
  2750. mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
  2751. mbedtls_md5_update_ret( &md5, session->master, 48 );
  2752. mbedtls_md5_update_ret( &md5, padbuf, 48 );
  2753. mbedtls_md5_finish_ret( &md5, md5sum );
  2754. mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
  2755. mbedtls_sha1_update_ret( &sha1, session->master, 48 );
  2756. mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
  2757. mbedtls_sha1_finish_ret( &sha1, sha1sum );
  2758. memset( padbuf, 0x5C, 48 );
  2759. mbedtls_md5_starts_ret( &md5 );
  2760. mbedtls_md5_update_ret( &md5, session->master, 48 );
  2761. mbedtls_md5_update_ret( &md5, padbuf, 48 );
  2762. mbedtls_md5_update_ret( &md5, md5sum, 16 );
  2763. mbedtls_md5_finish_ret( &md5, buf );
  2764. mbedtls_sha1_starts_ret( &sha1 );
  2765. mbedtls_sha1_update_ret( &sha1, session->master, 48 );
  2766. mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
  2767. mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
  2768. mbedtls_sha1_finish_ret( &sha1, buf + 16 );
  2769. MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
  2770. mbedtls_md5_free( &md5 );
  2771. mbedtls_sha1_free( &sha1 );
  2772. mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
  2773. mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
  2774. mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
  2775. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
  2776. }
  2777. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  2778. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
  2779. static void ssl_calc_finished_tls(
  2780. mbedtls_ssl_context *ssl, unsigned char *buf, int from )
  2781. {
  2782. int len = 12;
  2783. const char *sender;
  2784. mbedtls_md5_context md5;
  2785. mbedtls_sha1_context sha1;
  2786. unsigned char padbuf[36];
  2787. mbedtls_ssl_session *session = ssl->session_negotiate;
  2788. if( !session )
  2789. session = ssl->session;
  2790. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
  2791. mbedtls_md5_init( &md5 );
  2792. mbedtls_sha1_init( &sha1 );
  2793. mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
  2794. mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
  2795. /*
  2796. * TLSv1:
  2797. * hash = PRF( master, finished_label,
  2798. * MD5( handshake ) + SHA1( handshake ) )[0..11]
  2799. */
  2800. #if !defined(MBEDTLS_MD5_ALT)
  2801. MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
  2802. md5.state, sizeof( md5.state ) );
  2803. #endif
  2804. #if !defined(MBEDTLS_SHA1_ALT)
  2805. MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
  2806. sha1.state, sizeof( sha1.state ) );
  2807. #endif
  2808. sender = ( from == MBEDTLS_SSL_IS_CLIENT )
  2809. ? "client finished"
  2810. : "server finished";
  2811. mbedtls_md5_finish_ret( &md5, padbuf );
  2812. mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
  2813. ssl->handshake->tls_prf( session->master, 48, sender,
  2814. padbuf, 36, buf, len );
  2815. MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
  2816. mbedtls_md5_free( &md5 );
  2817. mbedtls_sha1_free( &sha1 );
  2818. mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
  2819. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
  2820. }
  2821. #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
  2822. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2823. #if defined(MBEDTLS_SHA256_C)
  2824. static void ssl_calc_finished_tls_sha256(
  2825. mbedtls_ssl_context *ssl, unsigned char *buf, int from )
  2826. {
  2827. int len = 12;
  2828. const char *sender;
  2829. unsigned char padbuf[32];
  2830. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2831. size_t hash_size;
  2832. psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
  2833. psa_status_t status;
  2834. #else
  2835. mbedtls_sha256_context sha256;
  2836. #endif
  2837. mbedtls_ssl_session *session = ssl->session_negotiate;
  2838. if( !session )
  2839. session = ssl->session;
  2840. sender = ( from == MBEDTLS_SSL_IS_CLIENT )
  2841. ? "client finished"
  2842. : "server finished";
  2843. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2844. sha256_psa = psa_hash_operation_init();
  2845. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
  2846. status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
  2847. if( status != PSA_SUCCESS )
  2848. {
  2849. MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
  2850. return;
  2851. }
  2852. status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
  2853. if( status != PSA_SUCCESS )
  2854. {
  2855. MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
  2856. return;
  2857. }
  2858. MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
  2859. #else
  2860. mbedtls_sha256_init( &sha256 );
  2861. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
  2862. mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
  2863. /*
  2864. * TLSv1.2:
  2865. * hash = PRF( master, finished_label,
  2866. * Hash( handshake ) )[0.11]
  2867. */
  2868. #if !defined(MBEDTLS_SHA256_ALT)
  2869. MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
  2870. sha256.state, sizeof( sha256.state ) );
  2871. #endif
  2872. mbedtls_sha256_finish_ret( &sha256, padbuf );
  2873. mbedtls_sha256_free( &sha256 );
  2874. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  2875. ssl->handshake->tls_prf( session->master, 48, sender,
  2876. padbuf, 32, buf, len );
  2877. MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
  2878. mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
  2879. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
  2880. }
  2881. #endif /* MBEDTLS_SHA256_C */
  2882. #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
  2883. static void ssl_calc_finished_tls_sha384(
  2884. mbedtls_ssl_context *ssl, unsigned char *buf, int from )
  2885. {
  2886. int len = 12;
  2887. const char *sender;
  2888. unsigned char padbuf[48];
  2889. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2890. size_t hash_size;
  2891. psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
  2892. psa_status_t status;
  2893. #else
  2894. mbedtls_sha512_context sha512;
  2895. #endif
  2896. mbedtls_ssl_session *session = ssl->session_negotiate;
  2897. if( !session )
  2898. session = ssl->session;
  2899. sender = ( from == MBEDTLS_SSL_IS_CLIENT )
  2900. ? "client finished"
  2901. : "server finished";
  2902. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2903. sha384_psa = psa_hash_operation_init();
  2904. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
  2905. status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
  2906. if( status != PSA_SUCCESS )
  2907. {
  2908. MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
  2909. return;
  2910. }
  2911. status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
  2912. if( status != PSA_SUCCESS )
  2913. {
  2914. MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
  2915. return;
  2916. }
  2917. MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
  2918. #else
  2919. mbedtls_sha512_init( &sha512 );
  2920. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
  2921. mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
  2922. /*
  2923. * TLSv1.2:
  2924. * hash = PRF( master, finished_label,
  2925. * Hash( handshake ) )[0.11]
  2926. */
  2927. #if !defined(MBEDTLS_SHA512_ALT)
  2928. MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
  2929. sha512.state, sizeof( sha512.state ) );
  2930. #endif
  2931. /* mbedtls_sha512_finish_ret's output parameter is declared as a
  2932. * 64-byte buffer, but sice we're using SHA-384, we know that the
  2933. * output fits in 48 bytes. This is correct C, but GCC 11.1 warns
  2934. * about it.
  2935. */
  2936. #if defined(__GNUC__) && __GNUC__ >= 11
  2937. #pragma GCC diagnostic push
  2938. #pragma GCC diagnostic ignored "-Wstringop-overflow"
  2939. #endif
  2940. mbedtls_sha512_finish_ret( &sha512, padbuf );
  2941. #if defined(__GNUC__) && __GNUC__ >= 11
  2942. #pragma GCC diagnostic pop
  2943. #endif
  2944. mbedtls_sha512_free( &sha512 );
  2945. #endif
  2946. ssl->handshake->tls_prf( session->master, 48, sender,
  2947. padbuf, 48, buf, len );
  2948. MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
  2949. mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
  2950. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
  2951. }
  2952. #endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
  2953. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  2954. void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
  2955. {
  2956. MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
  2957. /*
  2958. * Free our handshake params
  2959. */
  2960. mbedtls_ssl_handshake_free( ssl );
  2961. mbedtls_free( ssl->handshake );
  2962. ssl->handshake = NULL;
  2963. /*
  2964. * Free the previous transform and swith in the current one
  2965. */
  2966. if( ssl->transform )
  2967. {
  2968. mbedtls_ssl_transform_free( ssl->transform );
  2969. mbedtls_free( ssl->transform );
  2970. }
  2971. ssl->transform = ssl->transform_negotiate;
  2972. ssl->transform_negotiate = NULL;
  2973. MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
  2974. }
  2975. void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
  2976. {
  2977. int resume = ssl->handshake->resume;
  2978. MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
  2979. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  2980. if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
  2981. {
  2982. ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
  2983. ssl->renego_records_seen = 0;
  2984. }
  2985. #endif
  2986. /*
  2987. * Free the previous session and switch in the current one
  2988. */
  2989. if( ssl->session )
  2990. {
  2991. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  2992. /* RFC 7366 3.1: keep the EtM state */
  2993. ssl->session_negotiate->encrypt_then_mac =
  2994. ssl->session->encrypt_then_mac;
  2995. #endif
  2996. mbedtls_ssl_session_free( ssl->session );
  2997. mbedtls_free( ssl->session );
  2998. }
  2999. ssl->session = ssl->session_negotiate;
  3000. ssl->session_negotiate = NULL;
  3001. /*
  3002. * Add cache entry
  3003. */
  3004. if( ssl->conf->f_set_cache != NULL &&
  3005. ssl->session->id_len != 0 &&
  3006. resume == 0 )
  3007. {
  3008. if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
  3009. MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
  3010. }
  3011. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  3012. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  3013. ssl->handshake->flight != NULL )
  3014. {
  3015. /* Cancel handshake timer */
  3016. mbedtls_ssl_set_timer( ssl, 0 );
  3017. /* Keep last flight around in case we need to resend it:
  3018. * we need the handshake and transform structures for that */
  3019. MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
  3020. }
  3021. else
  3022. #endif
  3023. mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
  3024. ssl->state++;
  3025. MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
  3026. }
  3027. int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
  3028. {
  3029. int ret, hash_len;
  3030. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
  3031. mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
  3032. ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
  3033. /*
  3034. * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
  3035. * may define some other value. Currently (early 2016), no defined
  3036. * ciphersuite does this (and this is unlikely to change as activity has
  3037. * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
  3038. */
  3039. hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
  3040. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  3041. ssl->verify_data_len = hash_len;
  3042. memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
  3043. #endif
  3044. ssl->out_msglen = 4 + hash_len;
  3045. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  3046. ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
  3047. /*
  3048. * In case of session resuming, invert the client and server
  3049. * ChangeCipherSpec messages order.
  3050. */
  3051. if( ssl->handshake->resume != 0 )
  3052. {
  3053. #if defined(MBEDTLS_SSL_CLI_C)
  3054. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
  3055. ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
  3056. #endif
  3057. #if defined(MBEDTLS_SSL_SRV_C)
  3058. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
  3059. ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
  3060. #endif
  3061. }
  3062. else
  3063. ssl->state++;
  3064. /*
  3065. * Switch to our negotiated transform and session parameters for outbound
  3066. * data.
  3067. */
  3068. MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
  3069. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  3070. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  3071. {
  3072. unsigned char i;
  3073. /* Remember current epoch settings for resending */
  3074. ssl->handshake->alt_transform_out = ssl->transform_out;
  3075. memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
  3076. /* Set sequence_number to zero */
  3077. memset( ssl->cur_out_ctr + 2, 0, 6 );
  3078. /* Increment epoch */
  3079. for( i = 2; i > 0; i-- )
  3080. if( ++ssl->cur_out_ctr[i - 1] != 0 )
  3081. break;
  3082. /* The loop goes to its end iff the counter is wrapping */
  3083. if( i == 0 )
  3084. {
  3085. MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
  3086. return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
  3087. }
  3088. }
  3089. else
  3090. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  3091. memset( ssl->cur_out_ctr, 0, 8 );
  3092. ssl->transform_out = ssl->transform_negotiate;
  3093. ssl->session_out = ssl->session_negotiate;
  3094. #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
  3095. if( mbedtls_ssl_hw_record_activate != NULL )
  3096. {
  3097. if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
  3098. {
  3099. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
  3100. return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
  3101. }
  3102. }
  3103. #endif
  3104. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  3105. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  3106. mbedtls_ssl_send_flight_completed( ssl );
  3107. #endif
  3108. if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
  3109. {
  3110. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
  3111. return( ret );
  3112. }
  3113. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  3114. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  3115. ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
  3116. {
  3117. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
  3118. return( ret );
  3119. }
  3120. #endif
  3121. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
  3122. return( 0 );
  3123. }
  3124. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  3125. #define SSL_MAX_HASH_LEN 36
  3126. #else
  3127. #define SSL_MAX_HASH_LEN 12
  3128. #endif
  3129. int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
  3130. {
  3131. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  3132. unsigned int hash_len;
  3133. unsigned char buf[SSL_MAX_HASH_LEN];
  3134. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
  3135. /* There is currently no ciphersuite using another length with TLS 1.2 */
  3136. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  3137. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
  3138. hash_len = 36;
  3139. else
  3140. #endif
  3141. hash_len = 12;
  3142. ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
  3143. if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
  3144. {
  3145. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  3146. goto exit;
  3147. }
  3148. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  3149. {
  3150. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
  3151. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  3152. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  3153. ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
  3154. goto exit;
  3155. }
  3156. if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
  3157. ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
  3158. {
  3159. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
  3160. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  3161. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  3162. ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
  3163. goto exit;
  3164. }
  3165. if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
  3166. buf, hash_len ) != 0 )
  3167. {
  3168. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
  3169. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  3170. MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
  3171. ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
  3172. goto exit;
  3173. }
  3174. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  3175. ssl->verify_data_len = hash_len;
  3176. memcpy( ssl->peer_verify_data, buf, hash_len );
  3177. #endif
  3178. if( ssl->handshake->resume != 0 )
  3179. {
  3180. #if defined(MBEDTLS_SSL_CLI_C)
  3181. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
  3182. ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
  3183. #endif
  3184. #if defined(MBEDTLS_SSL_SRV_C)
  3185. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
  3186. ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
  3187. #endif
  3188. }
  3189. else
  3190. ssl->state++;
  3191. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  3192. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  3193. mbedtls_ssl_recv_flight_completed( ssl );
  3194. #endif
  3195. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
  3196. exit:
  3197. mbedtls_platform_zeroize( buf, hash_len );
  3198. return( ret );
  3199. }
  3200. static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
  3201. {
  3202. memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
  3203. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  3204. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  3205. mbedtls_md5_init( &handshake->fin_md5 );
  3206. mbedtls_sha1_init( &handshake->fin_sha1 );
  3207. mbedtls_md5_starts_ret( &handshake->fin_md5 );
  3208. mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
  3209. #endif
  3210. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  3211. #if defined(MBEDTLS_SHA256_C)
  3212. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  3213. handshake->fin_sha256_psa = psa_hash_operation_init();
  3214. psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
  3215. #else
  3216. mbedtls_sha256_init( &handshake->fin_sha256 );
  3217. mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
  3218. #endif
  3219. #endif
  3220. #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
  3221. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  3222. handshake->fin_sha384_psa = psa_hash_operation_init();
  3223. psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
  3224. #else
  3225. mbedtls_sha512_init( &handshake->fin_sha512 );
  3226. mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
  3227. #endif
  3228. #endif
  3229. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  3230. handshake->update_checksum = ssl_update_checksum_start;
  3231. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  3232. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  3233. mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
  3234. #endif
  3235. #if defined(MBEDTLS_DHM_C)
  3236. mbedtls_dhm_init( &handshake->dhm_ctx );
  3237. #endif
  3238. #if defined(MBEDTLS_ECDH_C)
  3239. mbedtls_ecdh_init( &handshake->ecdh_ctx );
  3240. #endif
  3241. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  3242. mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
  3243. #if defined(MBEDTLS_SSL_CLI_C)
  3244. handshake->ecjpake_cache = NULL;
  3245. handshake->ecjpake_cache_len = 0;
  3246. #endif
  3247. #endif
  3248. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  3249. mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
  3250. #endif
  3251. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  3252. handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
  3253. #endif
  3254. #if defined(MBEDTLS_X509_CRT_PARSE_C) && \
  3255. !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  3256. mbedtls_pk_init( &handshake->peer_pubkey );
  3257. #endif
  3258. }
  3259. void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
  3260. {
  3261. memset( transform, 0, sizeof(mbedtls_ssl_transform) );
  3262. mbedtls_cipher_init( &transform->cipher_ctx_enc );
  3263. mbedtls_cipher_init( &transform->cipher_ctx_dec );
  3264. #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
  3265. mbedtls_md_init( &transform->md_ctx_enc );
  3266. mbedtls_md_init( &transform->md_ctx_dec );
  3267. #endif
  3268. }
  3269. void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
  3270. {
  3271. memset( session, 0, sizeof(mbedtls_ssl_session) );
  3272. }
  3273. static int ssl_handshake_init( mbedtls_ssl_context *ssl )
  3274. {
  3275. /* Clear old handshake information if present */
  3276. if( ssl->transform_negotiate )
  3277. mbedtls_ssl_transform_free( ssl->transform_negotiate );
  3278. if( ssl->session_negotiate )
  3279. mbedtls_ssl_session_free( ssl->session_negotiate );
  3280. if( ssl->handshake )
  3281. mbedtls_ssl_handshake_free( ssl );
  3282. /*
  3283. * Either the pointers are now NULL or cleared properly and can be freed.
  3284. * Now allocate missing structures.
  3285. */
  3286. if( ssl->transform_negotiate == NULL )
  3287. {
  3288. ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
  3289. }
  3290. if( ssl->session_negotiate == NULL )
  3291. {
  3292. ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
  3293. }
  3294. if( ssl->handshake == NULL )
  3295. {
  3296. ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
  3297. }
  3298. #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
  3299. /* If the buffers are too small - reallocate */
  3300. handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
  3301. MBEDTLS_SSL_OUT_BUFFER_LEN );
  3302. #endif
  3303. /* All pointers should exist and can be directly freed without issue */
  3304. if( ssl->handshake == NULL ||
  3305. ssl->transform_negotiate == NULL ||
  3306. ssl->session_negotiate == NULL )
  3307. {
  3308. MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
  3309. mbedtls_free( ssl->handshake );
  3310. mbedtls_free( ssl->transform_negotiate );
  3311. mbedtls_free( ssl->session_negotiate );
  3312. ssl->handshake = NULL;
  3313. ssl->transform_negotiate = NULL;
  3314. ssl->session_negotiate = NULL;
  3315. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  3316. }
  3317. /* Initialize structures */
  3318. mbedtls_ssl_session_init( ssl->session_negotiate );
  3319. mbedtls_ssl_transform_init( ssl->transform_negotiate );
  3320. ssl_handshake_params_init( ssl->handshake );
  3321. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  3322. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  3323. {
  3324. ssl->handshake->alt_transform_out = ssl->transform_out;
  3325. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
  3326. ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
  3327. else
  3328. ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
  3329. mbedtls_ssl_set_timer( ssl, 0 );
  3330. }
  3331. #endif
  3332. return( 0 );
  3333. }
  3334. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
  3335. /* Dummy cookie callbacks for defaults */
  3336. static int ssl_cookie_write_dummy( void *ctx,
  3337. unsigned char **p, unsigned char *end,
  3338. const unsigned char *cli_id, size_t cli_id_len )
  3339. {
  3340. ((void) ctx);
  3341. ((void) p);
  3342. ((void) end);
  3343. ((void) cli_id);
  3344. ((void) cli_id_len);
  3345. return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
  3346. }
  3347. static int ssl_cookie_check_dummy( void *ctx,
  3348. const unsigned char *cookie, size_t cookie_len,
  3349. const unsigned char *cli_id, size_t cli_id_len )
  3350. {
  3351. ((void) ctx);
  3352. ((void) cookie);
  3353. ((void) cookie_len);
  3354. ((void) cli_id);
  3355. ((void) cli_id_len);
  3356. return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
  3357. }
  3358. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
  3359. /*
  3360. * Initialize an SSL context
  3361. */
  3362. void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
  3363. {
  3364. memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
  3365. }
  3366. /*
  3367. * Setup an SSL context
  3368. */
  3369. int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
  3370. const mbedtls_ssl_config *conf )
  3371. {
  3372. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  3373. size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
  3374. size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
  3375. ssl->conf = conf;
  3376. /*
  3377. * Prepare base structures
  3378. */
  3379. /* Set to NULL in case of an error condition */
  3380. ssl->out_buf = NULL;
  3381. #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
  3382. ssl->in_buf_len = in_buf_len;
  3383. #endif
  3384. ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
  3385. if( ssl->in_buf == NULL )
  3386. {
  3387. MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) );
  3388. ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
  3389. goto error;
  3390. }
  3391. #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
  3392. ssl->out_buf_len = out_buf_len;
  3393. #endif
  3394. ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
  3395. if( ssl->out_buf == NULL )
  3396. {
  3397. MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) );
  3398. ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
  3399. goto error;
  3400. }
  3401. mbedtls_ssl_reset_in_out_pointers( ssl );
  3402. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  3403. memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) );
  3404. #endif
  3405. if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
  3406. goto error;
  3407. return( 0 );
  3408. error:
  3409. mbedtls_free( ssl->in_buf );
  3410. mbedtls_free( ssl->out_buf );
  3411. ssl->conf = NULL;
  3412. #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
  3413. ssl->in_buf_len = 0;
  3414. ssl->out_buf_len = 0;
  3415. #endif
  3416. ssl->in_buf = NULL;
  3417. ssl->out_buf = NULL;
  3418. ssl->in_hdr = NULL;
  3419. ssl->in_ctr = NULL;
  3420. ssl->in_len = NULL;
  3421. ssl->in_iv = NULL;
  3422. ssl->in_msg = NULL;
  3423. ssl->out_hdr = NULL;
  3424. ssl->out_ctr = NULL;
  3425. ssl->out_len = NULL;
  3426. ssl->out_iv = NULL;
  3427. ssl->out_msg = NULL;
  3428. return( ret );
  3429. }
  3430. /*
  3431. * Reset an initialized and used SSL context for re-use while retaining
  3432. * all application-set variables, function pointers and data.
  3433. *
  3434. * If partial is non-zero, keep data in the input buffer and client ID.
  3435. * (Use when a DTLS client reconnects from the same port.)
  3436. */
  3437. int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
  3438. {
  3439. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  3440. #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
  3441. size_t in_buf_len = ssl->in_buf_len;
  3442. size_t out_buf_len = ssl->out_buf_len;
  3443. #else
  3444. size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
  3445. size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
  3446. #endif
  3447. #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
  3448. !defined(MBEDTLS_SSL_SRV_C)
  3449. ((void) partial);
  3450. #endif
  3451. ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
  3452. /* Cancel any possibly running timer */
  3453. mbedtls_ssl_set_timer( ssl, 0 );
  3454. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  3455. ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
  3456. ssl->renego_records_seen = 0;
  3457. ssl->verify_data_len = 0;
  3458. memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
  3459. memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
  3460. #endif
  3461. ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
  3462. ssl->in_offt = NULL;
  3463. mbedtls_ssl_reset_in_out_pointers( ssl );
  3464. ssl->in_msgtype = 0;
  3465. ssl->in_msglen = 0;
  3466. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  3467. ssl->next_record_offset = 0;
  3468. ssl->in_epoch = 0;
  3469. #endif
  3470. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  3471. mbedtls_ssl_dtls_replay_reset( ssl );
  3472. #endif
  3473. ssl->in_hslen = 0;
  3474. ssl->nb_zero = 0;
  3475. ssl->keep_current_message = 0;
  3476. ssl->out_msgtype = 0;
  3477. ssl->out_msglen = 0;
  3478. ssl->out_left = 0;
  3479. #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
  3480. if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
  3481. ssl->split_done = 0;
  3482. #endif
  3483. memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
  3484. ssl->transform_in = NULL;
  3485. ssl->transform_out = NULL;
  3486. ssl->session_in = NULL;
  3487. ssl->session_out = NULL;
  3488. memset( ssl->out_buf, 0, out_buf_len );
  3489. #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
  3490. if( partial == 0 )
  3491. #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
  3492. {
  3493. ssl->in_left = 0;
  3494. memset( ssl->in_buf, 0, in_buf_len );
  3495. }
  3496. #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
  3497. if( mbedtls_ssl_hw_record_reset != NULL )
  3498. {
  3499. MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
  3500. if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
  3501. {
  3502. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
  3503. return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
  3504. }
  3505. }
  3506. #endif
  3507. if( ssl->transform )
  3508. {
  3509. mbedtls_ssl_transform_free( ssl->transform );
  3510. mbedtls_free( ssl->transform );
  3511. ssl->transform = NULL;
  3512. }
  3513. if( ssl->session )
  3514. {
  3515. mbedtls_ssl_session_free( ssl->session );
  3516. mbedtls_free( ssl->session );
  3517. ssl->session = NULL;
  3518. }
  3519. #if defined(MBEDTLS_SSL_ALPN)
  3520. ssl->alpn_chosen = NULL;
  3521. #endif
  3522. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
  3523. #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
  3524. if( partial == 0 )
  3525. #endif
  3526. {
  3527. mbedtls_free( ssl->cli_id );
  3528. ssl->cli_id = NULL;
  3529. ssl->cli_id_len = 0;
  3530. }
  3531. #endif
  3532. if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
  3533. return( ret );
  3534. return( 0 );
  3535. }
  3536. /*
  3537. * Reset an initialized and used SSL context for re-use while retaining
  3538. * all application-set variables, function pointers and data.
  3539. */
  3540. int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
  3541. {
  3542. return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
  3543. }
  3544. /*
  3545. * SSL set accessors
  3546. */
  3547. void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
  3548. {
  3549. conf->endpoint = endpoint;
  3550. }
  3551. void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
  3552. {
  3553. conf->transport = transport;
  3554. }
  3555. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  3556. void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
  3557. {
  3558. conf->anti_replay = mode;
  3559. }
  3560. #endif
  3561. #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
  3562. void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
  3563. {
  3564. conf->badmac_limit = limit;
  3565. }
  3566. #endif
  3567. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  3568. void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
  3569. unsigned allow_packing )
  3570. {
  3571. ssl->disable_datagram_packing = !allow_packing;
  3572. }
  3573. void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
  3574. uint32_t min, uint32_t max )
  3575. {
  3576. conf->hs_timeout_min = min;
  3577. conf->hs_timeout_max = max;
  3578. }
  3579. #endif
  3580. void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
  3581. {
  3582. conf->authmode = authmode;
  3583. }
  3584. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  3585. void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
  3586. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  3587. void *p_vrfy )
  3588. {
  3589. conf->f_vrfy = f_vrfy;
  3590. conf->p_vrfy = p_vrfy;
  3591. }
  3592. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  3593. void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
  3594. int (*f_rng)(void *, unsigned char *, size_t),
  3595. void *p_rng )
  3596. {
  3597. conf->f_rng = f_rng;
  3598. conf->p_rng = p_rng;
  3599. }
  3600. void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
  3601. void (*f_dbg)(void *, int, const char *, int, const char *),
  3602. void *p_dbg )
  3603. {
  3604. conf->f_dbg = f_dbg;
  3605. conf->p_dbg = p_dbg;
  3606. }
  3607. void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
  3608. void *p_bio,
  3609. mbedtls_ssl_send_t *f_send,
  3610. mbedtls_ssl_recv_t *f_recv,
  3611. mbedtls_ssl_recv_timeout_t *f_recv_timeout )
  3612. {
  3613. ssl->p_bio = p_bio;
  3614. ssl->f_send = f_send;
  3615. ssl->f_recv = f_recv;
  3616. ssl->f_recv_timeout = f_recv_timeout;
  3617. }
  3618. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  3619. void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
  3620. {
  3621. ssl->mtu = mtu;
  3622. }
  3623. #endif
  3624. void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
  3625. {
  3626. conf->read_timeout = timeout;
  3627. }
  3628. void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
  3629. void *p_timer,
  3630. mbedtls_ssl_set_timer_t *f_set_timer,
  3631. mbedtls_ssl_get_timer_t *f_get_timer )
  3632. {
  3633. ssl->p_timer = p_timer;
  3634. ssl->f_set_timer = f_set_timer;
  3635. ssl->f_get_timer = f_get_timer;
  3636. /* Make sure we start with no timer running */
  3637. mbedtls_ssl_set_timer( ssl, 0 );
  3638. }
  3639. #if defined(MBEDTLS_SSL_SRV_C)
  3640. void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
  3641. void *p_cache,
  3642. int (*f_get_cache)(void *, mbedtls_ssl_session *),
  3643. int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
  3644. {
  3645. conf->p_cache = p_cache;
  3646. conf->f_get_cache = f_get_cache;
  3647. conf->f_set_cache = f_set_cache;
  3648. }
  3649. #endif /* MBEDTLS_SSL_SRV_C */
  3650. #if defined(MBEDTLS_SSL_CLI_C)
  3651. int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
  3652. {
  3653. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  3654. if( ssl == NULL ||
  3655. session == NULL ||
  3656. ssl->session_negotiate == NULL ||
  3657. ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
  3658. {
  3659. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  3660. }
  3661. if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
  3662. session ) ) != 0 )
  3663. return( ret );
  3664. ssl->handshake->resume = 1;
  3665. return( 0 );
  3666. }
  3667. #endif /* MBEDTLS_SSL_CLI_C */
  3668. void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
  3669. const int *ciphersuites )
  3670. {
  3671. conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
  3672. conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
  3673. conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
  3674. conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
  3675. }
  3676. void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
  3677. const int *ciphersuites,
  3678. int major, int minor )
  3679. {
  3680. if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
  3681. return;
  3682. if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
  3683. return;
  3684. conf->ciphersuite_list[minor] = ciphersuites;
  3685. }
  3686. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  3687. void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
  3688. const mbedtls_x509_crt_profile *profile )
  3689. {
  3690. conf->cert_profile = profile;
  3691. }
  3692. /* Append a new keycert entry to a (possibly empty) list */
  3693. static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
  3694. mbedtls_x509_crt *cert,
  3695. mbedtls_pk_context *key )
  3696. {
  3697. mbedtls_ssl_key_cert *new_cert;
  3698. new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
  3699. if( new_cert == NULL )
  3700. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  3701. new_cert->cert = cert;
  3702. new_cert->key = key;
  3703. new_cert->next = NULL;
  3704. /* Update head is the list was null, else add to the end */
  3705. if( *head == NULL )
  3706. {
  3707. *head = new_cert;
  3708. }
  3709. else
  3710. {
  3711. mbedtls_ssl_key_cert *cur = *head;
  3712. while( cur->next != NULL )
  3713. cur = cur->next;
  3714. cur->next = new_cert;
  3715. }
  3716. return( 0 );
  3717. }
  3718. int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
  3719. mbedtls_x509_crt *own_cert,
  3720. mbedtls_pk_context *pk_key )
  3721. {
  3722. return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
  3723. }
  3724. void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
  3725. mbedtls_x509_crt *ca_chain,
  3726. mbedtls_x509_crl *ca_crl )
  3727. {
  3728. conf->ca_chain = ca_chain;
  3729. conf->ca_crl = ca_crl;
  3730. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  3731. /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
  3732. * cannot be used together. */
  3733. conf->f_ca_cb = NULL;
  3734. conf->p_ca_cb = NULL;
  3735. #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
  3736. }
  3737. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  3738. void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
  3739. mbedtls_x509_crt_ca_cb_t f_ca_cb,
  3740. void *p_ca_cb )
  3741. {
  3742. conf->f_ca_cb = f_ca_cb;
  3743. conf->p_ca_cb = p_ca_cb;
  3744. /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
  3745. * cannot be used together. */
  3746. conf->ca_chain = NULL;
  3747. conf->ca_crl = NULL;
  3748. }
  3749. #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
  3750. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  3751. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  3752. int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
  3753. mbedtls_x509_crt *own_cert,
  3754. mbedtls_pk_context *pk_key )
  3755. {
  3756. return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
  3757. own_cert, pk_key ) );
  3758. }
  3759. void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
  3760. mbedtls_x509_crt *ca_chain,
  3761. mbedtls_x509_crl *ca_crl )
  3762. {
  3763. ssl->handshake->sni_ca_chain = ca_chain;
  3764. ssl->handshake->sni_ca_crl = ca_crl;
  3765. }
  3766. void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
  3767. int authmode )
  3768. {
  3769. ssl->handshake->sni_authmode = authmode;
  3770. }
  3771. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  3772. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  3773. void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
  3774. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  3775. void *p_vrfy )
  3776. {
  3777. ssl->f_vrfy = f_vrfy;
  3778. ssl->p_vrfy = p_vrfy;
  3779. }
  3780. #endif
  3781. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  3782. /*
  3783. * Set EC J-PAKE password for current handshake
  3784. */
  3785. int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
  3786. const unsigned char *pw,
  3787. size_t pw_len )
  3788. {
  3789. mbedtls_ecjpake_role role;
  3790. if( ssl->handshake == NULL || ssl->conf == NULL )
  3791. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  3792. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
  3793. role = MBEDTLS_ECJPAKE_SERVER;
  3794. else
  3795. role = MBEDTLS_ECJPAKE_CLIENT;
  3796. return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
  3797. role,
  3798. MBEDTLS_MD_SHA256,
  3799. MBEDTLS_ECP_DP_SECP256R1,
  3800. pw, pw_len ) );
  3801. }
  3802. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  3803. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  3804. static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
  3805. {
  3806. /* Remove reference to existing PSK, if any. */
  3807. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  3808. if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
  3809. {
  3810. /* The maintenance of the PSK key slot is the
  3811. * user's responsibility. */
  3812. conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
  3813. }
  3814. /* This and the following branch should never
  3815. * be taken simultaenously as we maintain the
  3816. * invariant that raw and opaque PSKs are never
  3817. * configured simultaneously. As a safeguard,
  3818. * though, `else` is omitted here. */
  3819. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  3820. if( conf->psk != NULL )
  3821. {
  3822. mbedtls_platform_zeroize( conf->psk, conf->psk_len );
  3823. mbedtls_free( conf->psk );
  3824. conf->psk = NULL;
  3825. conf->psk_len = 0;
  3826. }
  3827. /* Remove reference to PSK identity, if any. */
  3828. if( conf->psk_identity != NULL )
  3829. {
  3830. mbedtls_free( conf->psk_identity );
  3831. conf->psk_identity = NULL;
  3832. conf->psk_identity_len = 0;
  3833. }
  3834. }
  3835. /* This function assumes that PSK identity in the SSL config is unset.
  3836. * It checks that the provided identity is well-formed and attempts
  3837. * to make a copy of it in the SSL config.
  3838. * On failure, the PSK identity in the config remains unset. */
  3839. static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
  3840. unsigned char const *psk_identity,
  3841. size_t psk_identity_len )
  3842. {
  3843. /* Identity len will be encoded on two bytes */
  3844. if( psk_identity == NULL ||
  3845. ( psk_identity_len >> 16 ) != 0 ||
  3846. psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
  3847. {
  3848. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  3849. }
  3850. conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
  3851. if( conf->psk_identity == NULL )
  3852. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  3853. conf->psk_identity_len = psk_identity_len;
  3854. memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
  3855. return( 0 );
  3856. }
  3857. int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
  3858. const unsigned char *psk, size_t psk_len,
  3859. const unsigned char *psk_identity, size_t psk_identity_len )
  3860. {
  3861. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  3862. /* Remove opaque/raw PSK + PSK Identity */
  3863. ssl_conf_remove_psk( conf );
  3864. /* Check and set raw PSK */
  3865. if( psk == NULL )
  3866. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  3867. if( psk_len == 0 )
  3868. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  3869. if( psk_len > MBEDTLS_PSK_MAX_LEN )
  3870. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  3871. if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
  3872. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  3873. conf->psk_len = psk_len;
  3874. memcpy( conf->psk, psk, conf->psk_len );
  3875. /* Check and set PSK Identity */
  3876. ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
  3877. if( ret != 0 )
  3878. ssl_conf_remove_psk( conf );
  3879. return( ret );
  3880. }
  3881. static void ssl_remove_psk( mbedtls_ssl_context *ssl )
  3882. {
  3883. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  3884. if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
  3885. {
  3886. ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
  3887. }
  3888. else
  3889. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  3890. if( ssl->handshake->psk != NULL )
  3891. {
  3892. mbedtls_platform_zeroize( ssl->handshake->psk,
  3893. ssl->handshake->psk_len );
  3894. mbedtls_free( ssl->handshake->psk );
  3895. ssl->handshake->psk_len = 0;
  3896. }
  3897. }
  3898. int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
  3899. const unsigned char *psk, size_t psk_len )
  3900. {
  3901. if( psk == NULL || ssl->handshake == NULL )
  3902. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  3903. if( psk_len > MBEDTLS_PSK_MAX_LEN )
  3904. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  3905. ssl_remove_psk( ssl );
  3906. if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
  3907. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  3908. ssl->handshake->psk_len = psk_len;
  3909. memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
  3910. return( 0 );
  3911. }
  3912. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  3913. int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
  3914. psa_key_id_t psk,
  3915. const unsigned char *psk_identity,
  3916. size_t psk_identity_len )
  3917. {
  3918. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  3919. /* Clear opaque/raw PSK + PSK Identity, if present. */
  3920. ssl_conf_remove_psk( conf );
  3921. /* Check and set opaque PSK */
  3922. if( mbedtls_svc_key_id_is_null( psk ) )
  3923. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  3924. conf->psk_opaque = psk;
  3925. /* Check and set PSK Identity */
  3926. ret = ssl_conf_set_psk_identity( conf, psk_identity,
  3927. psk_identity_len );
  3928. if( ret != 0 )
  3929. ssl_conf_remove_psk( conf );
  3930. return( ret );
  3931. }
  3932. int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
  3933. psa_key_id_t psk )
  3934. {
  3935. if( ( mbedtls_svc_key_id_is_null( psk ) ) ||
  3936. ( ssl->handshake == NULL ) )
  3937. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  3938. ssl_remove_psk( ssl );
  3939. ssl->handshake->psk_opaque = psk;
  3940. return( 0 );
  3941. }
  3942. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  3943. void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
  3944. int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
  3945. size_t),
  3946. void *p_psk )
  3947. {
  3948. conf->f_psk = f_psk;
  3949. conf->p_psk = p_psk;
  3950. }
  3951. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  3952. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
  3953. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  3954. int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
  3955. {
  3956. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  3957. if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
  3958. ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
  3959. {
  3960. mbedtls_mpi_free( &conf->dhm_P );
  3961. mbedtls_mpi_free( &conf->dhm_G );
  3962. return( ret );
  3963. }
  3964. return( 0 );
  3965. }
  3966. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  3967. int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
  3968. const unsigned char *dhm_P, size_t P_len,
  3969. const unsigned char *dhm_G, size_t G_len )
  3970. {
  3971. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  3972. if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
  3973. ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
  3974. {
  3975. mbedtls_mpi_free( &conf->dhm_P );
  3976. mbedtls_mpi_free( &conf->dhm_G );
  3977. return( ret );
  3978. }
  3979. return( 0 );
  3980. }
  3981. int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
  3982. {
  3983. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  3984. if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
  3985. ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
  3986. {
  3987. mbedtls_mpi_free( &conf->dhm_P );
  3988. mbedtls_mpi_free( &conf->dhm_G );
  3989. return( ret );
  3990. }
  3991. return( 0 );
  3992. }
  3993. #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
  3994. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
  3995. /*
  3996. * Set the minimum length for Diffie-Hellman parameters
  3997. */
  3998. void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
  3999. unsigned int bitlen )
  4000. {
  4001. conf->dhm_min_bitlen = bitlen;
  4002. }
  4003. #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
  4004. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  4005. /*
  4006. * Set allowed/preferred hashes for handshake signatures
  4007. */
  4008. void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
  4009. const int *hashes )
  4010. {
  4011. conf->sig_hashes = hashes;
  4012. }
  4013. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  4014. #if defined(MBEDTLS_ECP_C)
  4015. /*
  4016. * Set the allowed elliptic curves
  4017. */
  4018. void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
  4019. const mbedtls_ecp_group_id *curve_list )
  4020. {
  4021. conf->curve_list = curve_list;
  4022. }
  4023. #endif /* MBEDTLS_ECP_C */
  4024. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  4025. int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
  4026. {
  4027. /* Initialize to suppress unnecessary compiler warning */
  4028. size_t hostname_len = 0;
  4029. /* Check if new hostname is valid before
  4030. * making any change to current one */
  4031. if( hostname != NULL )
  4032. {
  4033. hostname_len = strlen( hostname );
  4034. if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
  4035. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  4036. }
  4037. /* Now it's clear that we will overwrite the old hostname,
  4038. * so we can free it safely */
  4039. if( ssl->hostname != NULL )
  4040. {
  4041. mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
  4042. mbedtls_free( ssl->hostname );
  4043. }
  4044. /* Passing NULL as hostname shall clear the old one */
  4045. if( hostname == NULL )
  4046. {
  4047. ssl->hostname = NULL;
  4048. }
  4049. else
  4050. {
  4051. ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
  4052. if( ssl->hostname == NULL )
  4053. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  4054. memcpy( ssl->hostname, hostname, hostname_len );
  4055. ssl->hostname[hostname_len] = '\0';
  4056. }
  4057. return( 0 );
  4058. }
  4059. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  4060. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  4061. void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
  4062. int (*f_sni)(void *, mbedtls_ssl_context *,
  4063. const unsigned char *, size_t),
  4064. void *p_sni )
  4065. {
  4066. conf->f_sni = f_sni;
  4067. conf->p_sni = p_sni;
  4068. }
  4069. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  4070. #if defined(MBEDTLS_SSL_ALPN)
  4071. int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
  4072. {
  4073. size_t cur_len, tot_len;
  4074. const char **p;
  4075. /*
  4076. * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
  4077. * MUST NOT be truncated."
  4078. * We check lengths now rather than later.
  4079. */
  4080. tot_len = 0;
  4081. for( p = protos; *p != NULL; p++ )
  4082. {
  4083. cur_len = strlen( *p );
  4084. tot_len += cur_len;
  4085. if( ( cur_len == 0 ) ||
  4086. ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
  4087. ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
  4088. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  4089. }
  4090. conf->alpn_list = protos;
  4091. return( 0 );
  4092. }
  4093. const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
  4094. {
  4095. return( ssl->alpn_chosen );
  4096. }
  4097. #endif /* MBEDTLS_SSL_ALPN */
  4098. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  4099. void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf,
  4100. int support_mki_value )
  4101. {
  4102. conf->dtls_srtp_mki_support = support_mki_value;
  4103. }
  4104. int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl,
  4105. unsigned char *mki_value,
  4106. uint16_t mki_len )
  4107. {
  4108. if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH )
  4109. {
  4110. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  4111. }
  4112. if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED )
  4113. {
  4114. return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
  4115. }
  4116. memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len );
  4117. ssl->dtls_srtp_info.mki_len = mki_len;
  4118. return( 0 );
  4119. }
  4120. int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf,
  4121. const mbedtls_ssl_srtp_profile *profiles )
  4122. {
  4123. const mbedtls_ssl_srtp_profile *p;
  4124. size_t list_size = 0;
  4125. /* check the profiles list: all entry must be valid,
  4126. * its size cannot be more than the total number of supported profiles, currently 4 */
  4127. for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
  4128. list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
  4129. p++ )
  4130. {
  4131. if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET )
  4132. {
  4133. list_size++;
  4134. }
  4135. else
  4136. {
  4137. /* unsupported value, stop parsing and set the size to an error value */
  4138. list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
  4139. }
  4140. }
  4141. if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH )
  4142. {
  4143. conf->dtls_srtp_profile_list = NULL;
  4144. conf->dtls_srtp_profile_list_len = 0;
  4145. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  4146. }
  4147. conf->dtls_srtp_profile_list = profiles;
  4148. conf->dtls_srtp_profile_list_len = list_size;
  4149. return( 0 );
  4150. }
  4151. void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl,
  4152. mbedtls_dtls_srtp_info *dtls_srtp_info )
  4153. {
  4154. dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
  4155. /* do not copy the mki value if there is no chosen profile */
  4156. if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
  4157. {
  4158. dtls_srtp_info->mki_len = 0;
  4159. }
  4160. else
  4161. {
  4162. dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
  4163. memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
  4164. ssl->dtls_srtp_info.mki_len );
  4165. }
  4166. }
  4167. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  4168. void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
  4169. {
  4170. conf->max_major_ver = major;
  4171. conf->max_minor_ver = minor;
  4172. }
  4173. void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
  4174. {
  4175. conf->min_major_ver = major;
  4176. conf->min_minor_ver = minor;
  4177. }
  4178. #if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
  4179. void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
  4180. {
  4181. conf->fallback = fallback;
  4182. }
  4183. #endif
  4184. #if defined(MBEDTLS_SSL_SRV_C)
  4185. void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
  4186. char cert_req_ca_list )
  4187. {
  4188. conf->cert_req_ca_list = cert_req_ca_list;
  4189. }
  4190. #endif
  4191. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  4192. void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
  4193. {
  4194. conf->encrypt_then_mac = etm;
  4195. }
  4196. #endif
  4197. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  4198. void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
  4199. {
  4200. conf->extended_ms = ems;
  4201. }
  4202. #endif
  4203. #if defined(MBEDTLS_ARC4_C)
  4204. void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
  4205. {
  4206. conf->arc4_disabled = arc4;
  4207. }
  4208. #endif
  4209. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  4210. int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
  4211. {
  4212. if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
  4213. ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
  4214. {
  4215. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  4216. }
  4217. conf->mfl_code = mfl_code;
  4218. return( 0 );
  4219. }
  4220. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  4221. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  4222. void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
  4223. {
  4224. conf->trunc_hmac = truncate;
  4225. }
  4226. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  4227. #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
  4228. void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
  4229. {
  4230. conf->cbc_record_splitting = split;
  4231. }
  4232. #endif
  4233. void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
  4234. {
  4235. conf->allow_legacy_renegotiation = allow_legacy;
  4236. }
  4237. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  4238. void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
  4239. {
  4240. conf->disable_renegotiation = renegotiation;
  4241. }
  4242. void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
  4243. {
  4244. conf->renego_max_records = max_records;
  4245. }
  4246. void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
  4247. const unsigned char period[8] )
  4248. {
  4249. memcpy( conf->renego_period, period, 8 );
  4250. }
  4251. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  4252. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  4253. #if defined(MBEDTLS_SSL_CLI_C)
  4254. void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
  4255. {
  4256. conf->session_tickets = use_tickets;
  4257. }
  4258. #endif
  4259. #if defined(MBEDTLS_SSL_SRV_C)
  4260. void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
  4261. mbedtls_ssl_ticket_write_t *f_ticket_write,
  4262. mbedtls_ssl_ticket_parse_t *f_ticket_parse,
  4263. void *p_ticket )
  4264. {
  4265. conf->f_ticket_write = f_ticket_write;
  4266. conf->f_ticket_parse = f_ticket_parse;
  4267. conf->p_ticket = p_ticket;
  4268. }
  4269. #endif
  4270. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  4271. #if defined(MBEDTLS_SSL_EXPORT_KEYS)
  4272. void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
  4273. mbedtls_ssl_export_keys_t *f_export_keys,
  4274. void *p_export_keys )
  4275. {
  4276. conf->f_export_keys = f_export_keys;
  4277. conf->p_export_keys = p_export_keys;
  4278. }
  4279. void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
  4280. mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
  4281. void *p_export_keys )
  4282. {
  4283. conf->f_export_keys_ext = f_export_keys_ext;
  4284. conf->p_export_keys = p_export_keys;
  4285. }
  4286. #endif
  4287. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  4288. void mbedtls_ssl_conf_async_private_cb(
  4289. mbedtls_ssl_config *conf,
  4290. mbedtls_ssl_async_sign_t *f_async_sign,
  4291. mbedtls_ssl_async_decrypt_t *f_async_decrypt,
  4292. mbedtls_ssl_async_resume_t *f_async_resume,
  4293. mbedtls_ssl_async_cancel_t *f_async_cancel,
  4294. void *async_config_data )
  4295. {
  4296. conf->f_async_sign_start = f_async_sign;
  4297. conf->f_async_decrypt_start = f_async_decrypt;
  4298. conf->f_async_resume = f_async_resume;
  4299. conf->f_async_cancel = f_async_cancel;
  4300. conf->p_async_config_data = async_config_data;
  4301. }
  4302. void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
  4303. {
  4304. return( conf->p_async_config_data );
  4305. }
  4306. void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
  4307. {
  4308. if( ssl->handshake == NULL )
  4309. return( NULL );
  4310. else
  4311. return( ssl->handshake->user_async_ctx );
  4312. }
  4313. void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
  4314. void *ctx )
  4315. {
  4316. if( ssl->handshake != NULL )
  4317. ssl->handshake->user_async_ctx = ctx;
  4318. }
  4319. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  4320. /*
  4321. * SSL get accessors
  4322. */
  4323. uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
  4324. {
  4325. if( ssl->session != NULL )
  4326. return( ssl->session->verify_result );
  4327. if( ssl->session_negotiate != NULL )
  4328. return( ssl->session_negotiate->verify_result );
  4329. return( 0xFFFFFFFF );
  4330. }
  4331. const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
  4332. {
  4333. if( ssl == NULL || ssl->session == NULL )
  4334. return( NULL );
  4335. return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
  4336. }
  4337. const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
  4338. {
  4339. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  4340. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  4341. {
  4342. switch( ssl->minor_ver )
  4343. {
  4344. case MBEDTLS_SSL_MINOR_VERSION_2:
  4345. return( "DTLSv1.0" );
  4346. case MBEDTLS_SSL_MINOR_VERSION_3:
  4347. return( "DTLSv1.2" );
  4348. default:
  4349. return( "unknown (DTLS)" );
  4350. }
  4351. }
  4352. #endif
  4353. switch( ssl->minor_ver )
  4354. {
  4355. case MBEDTLS_SSL_MINOR_VERSION_0:
  4356. return( "SSLv3.0" );
  4357. case MBEDTLS_SSL_MINOR_VERSION_1:
  4358. return( "TLSv1.0" );
  4359. case MBEDTLS_SSL_MINOR_VERSION_2:
  4360. return( "TLSv1.1" );
  4361. case MBEDTLS_SSL_MINOR_VERSION_3:
  4362. return( "TLSv1.2" );
  4363. default:
  4364. return( "unknown" );
  4365. }
  4366. }
  4367. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  4368. size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
  4369. {
  4370. size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
  4371. size_t read_mfl;
  4372. /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
  4373. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
  4374. ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
  4375. {
  4376. return ssl_mfl_code_to_length( ssl->conf->mfl_code );
  4377. }
  4378. /* Check if a smaller max length was negotiated */
  4379. if( ssl->session_out != NULL )
  4380. {
  4381. read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
  4382. if( read_mfl < max_len )
  4383. {
  4384. max_len = read_mfl;
  4385. }
  4386. }
  4387. // During a handshake, use the value being negotiated
  4388. if( ssl->session_negotiate != NULL )
  4389. {
  4390. read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
  4391. if( read_mfl < max_len )
  4392. {
  4393. max_len = read_mfl;
  4394. }
  4395. }
  4396. return( max_len );
  4397. }
  4398. size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
  4399. {
  4400. size_t max_len;
  4401. /*
  4402. * Assume mfl_code is correct since it was checked when set
  4403. */
  4404. max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
  4405. /* Check if a smaller max length was negotiated */
  4406. if( ssl->session_out != NULL &&
  4407. ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
  4408. {
  4409. max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
  4410. }
  4411. /* During a handshake, use the value being negotiated */
  4412. if( ssl->session_negotiate != NULL &&
  4413. ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
  4414. {
  4415. max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
  4416. }
  4417. return( max_len );
  4418. }
  4419. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  4420. size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
  4421. {
  4422. return mbedtls_ssl_get_output_max_frag_len( ssl );
  4423. }
  4424. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  4425. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  4426. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  4427. size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
  4428. {
  4429. /* Return unlimited mtu for client hello messages to avoid fragmentation. */
  4430. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
  4431. ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
  4432. ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
  4433. return ( 0 );
  4434. if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
  4435. return( ssl->mtu );
  4436. if( ssl->mtu == 0 )
  4437. return( ssl->handshake->mtu );
  4438. return( ssl->mtu < ssl->handshake->mtu ?
  4439. ssl->mtu : ssl->handshake->mtu );
  4440. }
  4441. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  4442. int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
  4443. {
  4444. size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
  4445. #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
  4446. !defined(MBEDTLS_SSL_PROTO_DTLS)
  4447. (void) ssl;
  4448. #endif
  4449. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  4450. const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
  4451. if( max_len > mfl )
  4452. max_len = mfl;
  4453. #endif
  4454. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  4455. if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
  4456. {
  4457. const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
  4458. const int ret = mbedtls_ssl_get_record_expansion( ssl );
  4459. const size_t overhead = (size_t) ret;
  4460. if( ret < 0 )
  4461. return( ret );
  4462. if( mtu <= overhead )
  4463. {
  4464. MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
  4465. return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
  4466. }
  4467. if( max_len > mtu - overhead )
  4468. max_len = mtu - overhead;
  4469. }
  4470. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  4471. #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
  4472. !defined(MBEDTLS_SSL_PROTO_DTLS)
  4473. ((void) ssl);
  4474. #endif
  4475. return( (int) max_len );
  4476. }
  4477. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  4478. const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
  4479. {
  4480. if( ssl == NULL || ssl->session == NULL )
  4481. return( NULL );
  4482. #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  4483. return( ssl->session->peer_cert );
  4484. #else
  4485. return( NULL );
  4486. #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  4487. }
  4488. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  4489. #if defined(MBEDTLS_SSL_CLI_C)
  4490. int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
  4491. mbedtls_ssl_session *dst )
  4492. {
  4493. if( ssl == NULL ||
  4494. dst == NULL ||
  4495. ssl->session == NULL ||
  4496. ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
  4497. {
  4498. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  4499. }
  4500. return( mbedtls_ssl_session_copy( dst, ssl->session ) );
  4501. }
  4502. #endif /* MBEDTLS_SSL_CLI_C */
  4503. const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
  4504. {
  4505. if( ssl == NULL )
  4506. return( NULL );
  4507. return( ssl->session );
  4508. }
  4509. /*
  4510. * Define ticket header determining Mbed TLS version
  4511. * and structure of the ticket.
  4512. */
  4513. /*
  4514. * Define bitflag determining compile-time settings influencing
  4515. * structure of serialized SSL sessions.
  4516. */
  4517. #if defined(MBEDTLS_HAVE_TIME)
  4518. #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
  4519. #else
  4520. #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
  4521. #endif /* MBEDTLS_HAVE_TIME */
  4522. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  4523. #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
  4524. #else
  4525. #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
  4526. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  4527. #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
  4528. #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
  4529. #else
  4530. #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
  4531. #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
  4532. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  4533. #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
  4534. #else
  4535. #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
  4536. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  4537. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  4538. #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
  4539. #else
  4540. #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
  4541. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  4542. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  4543. #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
  4544. #else
  4545. #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
  4546. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  4547. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  4548. #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
  4549. #else
  4550. #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
  4551. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  4552. #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
  4553. #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
  4554. #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
  4555. #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
  4556. #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
  4557. #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
  4558. #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
  4559. #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
  4560. ( (uint16_t) ( \
  4561. ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
  4562. ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
  4563. ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
  4564. ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
  4565. ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
  4566. ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
  4567. ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
  4568. static unsigned char ssl_serialized_session_header[] = {
  4569. MBEDTLS_VERSION_MAJOR,
  4570. MBEDTLS_VERSION_MINOR,
  4571. MBEDTLS_VERSION_PATCH,
  4572. MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
  4573. MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
  4574. };
  4575. /*
  4576. * Serialize a session in the following format:
  4577. * (in the presentation language of TLS, RFC 8446 section 3)
  4578. *
  4579. * opaque mbedtls_version[3]; // major, minor, patch
  4580. * opaque session_format[2]; // version-specific 16-bit field determining
  4581. * // the format of the remaining
  4582. * // serialized data.
  4583. *
  4584. * Note: When updating the format, remember to keep
  4585. * these version+format bytes.
  4586. *
  4587. * // In this version, `session_format` determines
  4588. * // the setting of those compile-time
  4589. * // configuration options which influence
  4590. * // the structure of mbedtls_ssl_session.
  4591. * uint64 start_time;
  4592. * uint8 ciphersuite[2]; // defined by the standard
  4593. * uint8 compression; // 0 or 1
  4594. * uint8 session_id_len; // at most 32
  4595. * opaque session_id[32];
  4596. * opaque master[48]; // fixed length in the standard
  4597. * uint32 verify_result;
  4598. * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
  4599. * opaque ticket<0..2^24-1>; // length 0 means no ticket
  4600. * uint32 ticket_lifetime;
  4601. * uint8 mfl_code; // up to 255 according to standard
  4602. * uint8 trunc_hmac; // 0 or 1
  4603. * uint8 encrypt_then_mac; // 0 or 1
  4604. *
  4605. * The order is the same as in the definition of the structure, except
  4606. * verify_result is put before peer_cert so that all mandatory fields come
  4607. * together in one block.
  4608. */
  4609. static int ssl_session_save( const mbedtls_ssl_session *session,
  4610. unsigned char omit_header,
  4611. unsigned char *buf,
  4612. size_t buf_len,
  4613. size_t *olen )
  4614. {
  4615. unsigned char *p = buf;
  4616. size_t used = 0;
  4617. #if defined(MBEDTLS_HAVE_TIME)
  4618. uint64_t start;
  4619. #endif
  4620. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  4621. #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  4622. size_t cert_len;
  4623. #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  4624. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  4625. if( !omit_header )
  4626. {
  4627. /*
  4628. * Add version identifier
  4629. */
  4630. used += sizeof( ssl_serialized_session_header );
  4631. if( used <= buf_len )
  4632. {
  4633. memcpy( p, ssl_serialized_session_header,
  4634. sizeof( ssl_serialized_session_header ) );
  4635. p += sizeof( ssl_serialized_session_header );
  4636. }
  4637. }
  4638. /*
  4639. * Time
  4640. */
  4641. #if defined(MBEDTLS_HAVE_TIME)
  4642. used += 8;
  4643. if( used <= buf_len )
  4644. {
  4645. start = (uint64_t) session->start;
  4646. MBEDTLS_PUT_UINT64_BE( start, p, 0 );
  4647. p += 8;
  4648. }
  4649. #endif /* MBEDTLS_HAVE_TIME */
  4650. /*
  4651. * Basic mandatory fields
  4652. */
  4653. used += 2 /* ciphersuite */
  4654. + 1 /* compression */
  4655. + 1 /* id_len */
  4656. + sizeof( session->id )
  4657. + sizeof( session->master )
  4658. + 4; /* verify_result */
  4659. if( used <= buf_len )
  4660. {
  4661. MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 );
  4662. p += 2;
  4663. *p++ = MBEDTLS_BYTE_0( session->compression );
  4664. *p++ = MBEDTLS_BYTE_0( session->id_len );
  4665. memcpy( p, session->id, 32 );
  4666. p += 32;
  4667. memcpy( p, session->master, 48 );
  4668. p += 48;
  4669. MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 );
  4670. p += 4;
  4671. }
  4672. /*
  4673. * Peer's end-entity certificate
  4674. */
  4675. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  4676. #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  4677. if( session->peer_cert == NULL )
  4678. cert_len = 0;
  4679. else
  4680. cert_len = session->peer_cert->raw.len;
  4681. used += 3 + cert_len;
  4682. if( used <= buf_len )
  4683. {
  4684. *p++ = MBEDTLS_BYTE_2( cert_len );
  4685. *p++ = MBEDTLS_BYTE_1( cert_len );
  4686. *p++ = MBEDTLS_BYTE_0( cert_len );
  4687. if( session->peer_cert != NULL )
  4688. {
  4689. memcpy( p, session->peer_cert->raw.p, cert_len );
  4690. p += cert_len;
  4691. }
  4692. }
  4693. #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  4694. if( session->peer_cert_digest != NULL )
  4695. {
  4696. used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
  4697. if( used <= buf_len )
  4698. {
  4699. *p++ = (unsigned char) session->peer_cert_digest_type;
  4700. *p++ = (unsigned char) session->peer_cert_digest_len;
  4701. memcpy( p, session->peer_cert_digest,
  4702. session->peer_cert_digest_len );
  4703. p += session->peer_cert_digest_len;
  4704. }
  4705. }
  4706. else
  4707. {
  4708. used += 2;
  4709. if( used <= buf_len )
  4710. {
  4711. *p++ = (unsigned char) MBEDTLS_MD_NONE;
  4712. *p++ = 0;
  4713. }
  4714. }
  4715. #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  4716. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  4717. /*
  4718. * Session ticket if any, plus associated data
  4719. */
  4720. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
  4721. used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
  4722. if( used <= buf_len )
  4723. {
  4724. *p++ = MBEDTLS_BYTE_2( session->ticket_len );
  4725. *p++ = MBEDTLS_BYTE_1( session->ticket_len );
  4726. *p++ = MBEDTLS_BYTE_0( session->ticket_len );
  4727. if( session->ticket != NULL )
  4728. {
  4729. memcpy( p, session->ticket, session->ticket_len );
  4730. p += session->ticket_len;
  4731. }
  4732. MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 );
  4733. p += 4;
  4734. }
  4735. #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
  4736. /*
  4737. * Misc extension-related info
  4738. */
  4739. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  4740. used += 1;
  4741. if( used <= buf_len )
  4742. *p++ = session->mfl_code;
  4743. #endif
  4744. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  4745. used += 1;
  4746. if( used <= buf_len )
  4747. *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
  4748. #endif
  4749. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  4750. used += 1;
  4751. if( used <= buf_len )
  4752. *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac );
  4753. #endif
  4754. /* Done */
  4755. *olen = used;
  4756. if( used > buf_len )
  4757. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  4758. return( 0 );
  4759. }
  4760. /*
  4761. * Public wrapper for ssl_session_save()
  4762. */
  4763. int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
  4764. unsigned char *buf,
  4765. size_t buf_len,
  4766. size_t *olen )
  4767. {
  4768. return( ssl_session_save( session, 0, buf, buf_len, olen ) );
  4769. }
  4770. /*
  4771. * Deserialize session, see mbedtls_ssl_session_save() for format.
  4772. *
  4773. * This internal version is wrapped by a public function that cleans up in
  4774. * case of error, and has an extra option omit_header.
  4775. */
  4776. static int ssl_session_load( mbedtls_ssl_session *session,
  4777. unsigned char omit_header,
  4778. const unsigned char *buf,
  4779. size_t len )
  4780. {
  4781. const unsigned char *p = buf;
  4782. const unsigned char * const end = buf + len;
  4783. #if defined(MBEDTLS_HAVE_TIME)
  4784. uint64_t start;
  4785. #endif
  4786. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  4787. #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  4788. size_t cert_len;
  4789. #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  4790. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  4791. if( !omit_header )
  4792. {
  4793. /*
  4794. * Check version identifier
  4795. */
  4796. if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
  4797. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  4798. if( memcmp( p, ssl_serialized_session_header,
  4799. sizeof( ssl_serialized_session_header ) ) != 0 )
  4800. {
  4801. return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
  4802. }
  4803. p += sizeof( ssl_serialized_session_header );
  4804. }
  4805. /*
  4806. * Time
  4807. */
  4808. #if defined(MBEDTLS_HAVE_TIME)
  4809. if( 8 > (size_t)( end - p ) )
  4810. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  4811. start = ( (uint64_t) p[0] << 56 ) |
  4812. ( (uint64_t) p[1] << 48 ) |
  4813. ( (uint64_t) p[2] << 40 ) |
  4814. ( (uint64_t) p[3] << 32 ) |
  4815. ( (uint64_t) p[4] << 24 ) |
  4816. ( (uint64_t) p[5] << 16 ) |
  4817. ( (uint64_t) p[6] << 8 ) |
  4818. ( (uint64_t) p[7] );
  4819. p += 8;
  4820. session->start = (time_t) start;
  4821. #endif /* MBEDTLS_HAVE_TIME */
  4822. /*
  4823. * Basic mandatory fields
  4824. */
  4825. if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
  4826. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  4827. session->ciphersuite = ( p[0] << 8 ) | p[1];
  4828. p += 2;
  4829. session->compression = *p++;
  4830. session->id_len = *p++;
  4831. memcpy( session->id, p, 32 );
  4832. p += 32;
  4833. memcpy( session->master, p, 48 );
  4834. p += 48;
  4835. session->verify_result = ( (uint32_t) p[0] << 24 ) |
  4836. ( (uint32_t) p[1] << 16 ) |
  4837. ( (uint32_t) p[2] << 8 ) |
  4838. ( (uint32_t) p[3] );
  4839. p += 4;
  4840. /* Immediately clear invalid pointer values that have been read, in case
  4841. * we exit early before we replaced them with valid ones. */
  4842. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  4843. #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  4844. session->peer_cert = NULL;
  4845. #else
  4846. session->peer_cert_digest = NULL;
  4847. #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  4848. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  4849. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
  4850. session->ticket = NULL;
  4851. #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
  4852. /*
  4853. * Peer certificate
  4854. */
  4855. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  4856. #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  4857. /* Deserialize CRT from the end of the ticket. */
  4858. if( 3 > (size_t)( end - p ) )
  4859. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  4860. cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
  4861. p += 3;
  4862. if( cert_len != 0 )
  4863. {
  4864. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  4865. if( cert_len > (size_t)( end - p ) )
  4866. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  4867. session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
  4868. if( session->peer_cert == NULL )
  4869. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  4870. mbedtls_x509_crt_init( session->peer_cert );
  4871. if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
  4872. p, cert_len ) ) != 0 )
  4873. {
  4874. mbedtls_x509_crt_free( session->peer_cert );
  4875. mbedtls_free( session->peer_cert );
  4876. session->peer_cert = NULL;
  4877. return( ret );
  4878. }
  4879. p += cert_len;
  4880. }
  4881. #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  4882. /* Deserialize CRT digest from the end of the ticket. */
  4883. if( 2 > (size_t)( end - p ) )
  4884. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  4885. session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
  4886. session->peer_cert_digest_len = (size_t) *p++;
  4887. if( session->peer_cert_digest_len != 0 )
  4888. {
  4889. const mbedtls_md_info_t *md_info =
  4890. mbedtls_md_info_from_type( session->peer_cert_digest_type );
  4891. if( md_info == NULL )
  4892. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  4893. if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
  4894. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  4895. if( session->peer_cert_digest_len > (size_t)( end - p ) )
  4896. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  4897. session->peer_cert_digest =
  4898. mbedtls_calloc( 1, session->peer_cert_digest_len );
  4899. if( session->peer_cert_digest == NULL )
  4900. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  4901. memcpy( session->peer_cert_digest, p,
  4902. session->peer_cert_digest_len );
  4903. p += session->peer_cert_digest_len;
  4904. }
  4905. #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  4906. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  4907. /*
  4908. * Session ticket and associated data
  4909. */
  4910. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
  4911. if( 3 > (size_t)( end - p ) )
  4912. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  4913. session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
  4914. p += 3;
  4915. if( session->ticket_len != 0 )
  4916. {
  4917. if( session->ticket_len > (size_t)( end - p ) )
  4918. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  4919. session->ticket = mbedtls_calloc( 1, session->ticket_len );
  4920. if( session->ticket == NULL )
  4921. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  4922. memcpy( session->ticket, p, session->ticket_len );
  4923. p += session->ticket_len;
  4924. }
  4925. if( 4 > (size_t)( end - p ) )
  4926. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  4927. session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
  4928. ( (uint32_t) p[1] << 16 ) |
  4929. ( (uint32_t) p[2] << 8 ) |
  4930. ( (uint32_t) p[3] );
  4931. p += 4;
  4932. #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
  4933. /*
  4934. * Misc extension-related info
  4935. */
  4936. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  4937. if( 1 > (size_t)( end - p ) )
  4938. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  4939. session->mfl_code = *p++;
  4940. #endif
  4941. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  4942. if( 1 > (size_t)( end - p ) )
  4943. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  4944. session->trunc_hmac = *p++;
  4945. #endif
  4946. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  4947. if( 1 > (size_t)( end - p ) )
  4948. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  4949. session->encrypt_then_mac = *p++;
  4950. #endif
  4951. /* Done, should have consumed entire buffer */
  4952. if( p != end )
  4953. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  4954. return( 0 );
  4955. }
  4956. /*
  4957. * Deserialize session: public wrapper for error cleaning
  4958. */
  4959. int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
  4960. const unsigned char *buf,
  4961. size_t len )
  4962. {
  4963. int ret = ssl_session_load( session, 0, buf, len );
  4964. if( ret != 0 )
  4965. mbedtls_ssl_session_free( session );
  4966. return( ret );
  4967. }
  4968. /*
  4969. * Perform a single step of the SSL handshake
  4970. */
  4971. int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
  4972. {
  4973. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  4974. if( ssl == NULL || ssl->conf == NULL )
  4975. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  4976. #if defined(MBEDTLS_SSL_CLI_C)
  4977. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
  4978. ret = mbedtls_ssl_handshake_client_step( ssl );
  4979. #endif
  4980. #if defined(MBEDTLS_SSL_SRV_C)
  4981. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
  4982. ret = mbedtls_ssl_handshake_server_step( ssl );
  4983. #endif
  4984. return( ret );
  4985. }
  4986. /*
  4987. * Perform the SSL handshake
  4988. */
  4989. int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
  4990. {
  4991. int ret = 0;
  4992. /* Sanity checks */
  4993. if( ssl == NULL || ssl->conf == NULL )
  4994. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  4995. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  4996. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  4997. ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) )
  4998. {
  4999. MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
  5000. "mbedtls_ssl_set_timer_cb() for DTLS" ) );
  5001. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5002. }
  5003. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  5004. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
  5005. /* Main handshake loop */
  5006. while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
  5007. {
  5008. ret = mbedtls_ssl_handshake_step( ssl );
  5009. if( ret != 0 )
  5010. break;
  5011. }
  5012. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
  5013. return( ret );
  5014. }
  5015. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  5016. #if defined(MBEDTLS_SSL_SRV_C)
  5017. /*
  5018. * Write HelloRequest to request renegotiation on server
  5019. */
  5020. static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
  5021. {
  5022. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  5023. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
  5024. ssl->out_msglen = 4;
  5025. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  5026. ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
  5027. if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
  5028. {
  5029. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
  5030. return( ret );
  5031. }
  5032. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
  5033. return( 0 );
  5034. }
  5035. #endif /* MBEDTLS_SSL_SRV_C */
  5036. /*
  5037. * Actually renegotiate current connection, triggered by either:
  5038. * - any side: calling mbedtls_ssl_renegotiate(),
  5039. * - client: receiving a HelloRequest during mbedtls_ssl_read(),
  5040. * - server: receiving any handshake message on server during mbedtls_ssl_read() after
  5041. * the initial handshake is completed.
  5042. * If the handshake doesn't complete due to waiting for I/O, it will continue
  5043. * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
  5044. */
  5045. int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
  5046. {
  5047. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  5048. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
  5049. if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
  5050. return( ret );
  5051. /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
  5052. * the ServerHello will have message_seq = 1" */
  5053. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  5054. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  5055. ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
  5056. {
  5057. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
  5058. ssl->handshake->out_msg_seq = 1;
  5059. else
  5060. ssl->handshake->in_msg_seq = 1;
  5061. }
  5062. #endif
  5063. ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
  5064. ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
  5065. if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
  5066. {
  5067. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
  5068. return( ret );
  5069. }
  5070. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
  5071. return( 0 );
  5072. }
  5073. /*
  5074. * Renegotiate current connection on client,
  5075. * or request renegotiation on server
  5076. */
  5077. int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
  5078. {
  5079. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  5080. if( ssl == NULL || ssl->conf == NULL )
  5081. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5082. #if defined(MBEDTLS_SSL_SRV_C)
  5083. /* On server, just send the request */
  5084. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
  5085. {
  5086. if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
  5087. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5088. ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
  5089. /* Did we already try/start sending HelloRequest? */
  5090. if( ssl->out_left != 0 )
  5091. return( mbedtls_ssl_flush_output( ssl ) );
  5092. return( ssl_write_hello_request( ssl ) );
  5093. }
  5094. #endif /* MBEDTLS_SSL_SRV_C */
  5095. #if defined(MBEDTLS_SSL_CLI_C)
  5096. /*
  5097. * On client, either start the renegotiation process or,
  5098. * if already in progress, continue the handshake
  5099. */
  5100. if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
  5101. {
  5102. if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
  5103. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5104. if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
  5105. {
  5106. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
  5107. return( ret );
  5108. }
  5109. }
  5110. else
  5111. {
  5112. if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
  5113. {
  5114. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
  5115. return( ret );
  5116. }
  5117. }
  5118. #endif /* MBEDTLS_SSL_CLI_C */
  5119. return( ret );
  5120. }
  5121. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  5122. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  5123. static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
  5124. {
  5125. mbedtls_ssl_key_cert *cur = key_cert, *next;
  5126. while( cur != NULL )
  5127. {
  5128. next = cur->next;
  5129. mbedtls_free( cur );
  5130. cur = next;
  5131. }
  5132. }
  5133. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  5134. void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
  5135. {
  5136. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  5137. if( handshake == NULL )
  5138. return;
  5139. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  5140. if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
  5141. {
  5142. ssl->conf->f_async_cancel( ssl );
  5143. handshake->async_in_progress = 0;
  5144. }
  5145. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  5146. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  5147. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  5148. mbedtls_md5_free( &handshake->fin_md5 );
  5149. mbedtls_sha1_free( &handshake->fin_sha1 );
  5150. #endif
  5151. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  5152. #if defined(MBEDTLS_SHA256_C)
  5153. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  5154. psa_hash_abort( &handshake->fin_sha256_psa );
  5155. #else
  5156. mbedtls_sha256_free( &handshake->fin_sha256 );
  5157. #endif
  5158. #endif
  5159. #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
  5160. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  5161. psa_hash_abort( &handshake->fin_sha384_psa );
  5162. #else
  5163. mbedtls_sha512_free( &handshake->fin_sha512 );
  5164. #endif
  5165. #endif
  5166. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  5167. #if defined(MBEDTLS_DHM_C)
  5168. mbedtls_dhm_free( &handshake->dhm_ctx );
  5169. #endif
  5170. #if defined(MBEDTLS_ECDH_C)
  5171. mbedtls_ecdh_free( &handshake->ecdh_ctx );
  5172. #endif
  5173. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  5174. mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
  5175. #if defined(MBEDTLS_SSL_CLI_C)
  5176. mbedtls_free( handshake->ecjpake_cache );
  5177. handshake->ecjpake_cache = NULL;
  5178. handshake->ecjpake_cache_len = 0;
  5179. #endif
  5180. #endif
  5181. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  5182. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  5183. /* explicit void pointer cast for buggy MS compiler */
  5184. mbedtls_free( (void *) handshake->curves );
  5185. #endif
  5186. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  5187. if( handshake->psk != NULL )
  5188. {
  5189. mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
  5190. mbedtls_free( handshake->psk );
  5191. }
  5192. #endif
  5193. #if defined(MBEDTLS_X509_CRT_PARSE_C) && \
  5194. defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  5195. /*
  5196. * Free only the linked list wrapper, not the keys themselves
  5197. * since the belong to the SNI callback
  5198. */
  5199. if( handshake->sni_key_cert != NULL )
  5200. {
  5201. mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
  5202. while( cur != NULL )
  5203. {
  5204. next = cur->next;
  5205. mbedtls_free( cur );
  5206. cur = next;
  5207. }
  5208. }
  5209. #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
  5210. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  5211. mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
  5212. if( handshake->ecrs_peer_cert != NULL )
  5213. {
  5214. mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
  5215. mbedtls_free( handshake->ecrs_peer_cert );
  5216. }
  5217. #endif
  5218. #if defined(MBEDTLS_X509_CRT_PARSE_C) && \
  5219. !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  5220. mbedtls_pk_free( &handshake->peer_pubkey );
  5221. #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  5222. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  5223. mbedtls_free( handshake->verify_cookie );
  5224. mbedtls_ssl_flight_free( handshake->flight );
  5225. mbedtls_ssl_buffering_free( ssl );
  5226. #endif
  5227. #if defined(MBEDTLS_ECDH_C) && \
  5228. defined(MBEDTLS_USE_PSA_CRYPTO)
  5229. psa_destroy_key( handshake->ecdh_psa_privkey );
  5230. #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
  5231. mbedtls_platform_zeroize( handshake,
  5232. sizeof( mbedtls_ssl_handshake_params ) );
  5233. #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
  5234. /* If the buffers are too big - reallocate. Because of the way Mbed TLS
  5235. * processes datagrams and the fact that a datagram is allowed to have
  5236. * several records in it, it is possible that the I/O buffers are not
  5237. * empty at this stage */
  5238. handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ),
  5239. mbedtls_ssl_get_output_buflen( ssl ) );
  5240. #endif
  5241. }
  5242. void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
  5243. {
  5244. if( session == NULL )
  5245. return;
  5246. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  5247. ssl_clear_peer_cert( session );
  5248. #endif
  5249. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
  5250. mbedtls_free( session->ticket );
  5251. #endif
  5252. mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
  5253. }
  5254. #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
  5255. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  5256. #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
  5257. #else
  5258. #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
  5259. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  5260. #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
  5261. #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
  5262. #else
  5263. #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
  5264. #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
  5265. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  5266. #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
  5267. #else
  5268. #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
  5269. #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
  5270. #if defined(MBEDTLS_SSL_ALPN)
  5271. #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
  5272. #else
  5273. #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
  5274. #endif /* MBEDTLS_SSL_ALPN */
  5275. #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
  5276. #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
  5277. #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
  5278. #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
  5279. #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
  5280. ( (uint32_t) ( \
  5281. ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
  5282. ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
  5283. ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
  5284. ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
  5285. 0u ) )
  5286. static unsigned char ssl_serialized_context_header[] = {
  5287. MBEDTLS_VERSION_MAJOR,
  5288. MBEDTLS_VERSION_MINOR,
  5289. MBEDTLS_VERSION_PATCH,
  5290. MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
  5291. MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ),
  5292. MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
  5293. MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
  5294. MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ),
  5295. };
  5296. /*
  5297. * Serialize a full SSL context
  5298. *
  5299. * The format of the serialized data is:
  5300. * (in the presentation language of TLS, RFC 8446 section 3)
  5301. *
  5302. * // header
  5303. * opaque mbedtls_version[3]; // major, minor, patch
  5304. * opaque context_format[5]; // version-specific field determining
  5305. * // the format of the remaining
  5306. * // serialized data.
  5307. * Note: When updating the format, remember to keep these
  5308. * version+format bytes. (We may make their size part of the API.)
  5309. *
  5310. * // session sub-structure
  5311. * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
  5312. * // transform sub-structure
  5313. * uint8 random[64]; // ServerHello.random+ClientHello.random
  5314. * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
  5315. * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
  5316. * // fields from ssl_context
  5317. * uint32 badmac_seen; // DTLS: number of records with failing MAC
  5318. * uint64 in_window_top; // DTLS: last validated record seq_num
  5319. * uint64 in_window; // DTLS: bitmask for replay protection
  5320. * uint8 disable_datagram_packing; // DTLS: only one record per datagram
  5321. * uint64 cur_out_ctr; // Record layer: outgoing sequence number
  5322. * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
  5323. * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
  5324. *
  5325. * Note that many fields of the ssl_context or sub-structures are not
  5326. * serialized, as they fall in one of the following categories:
  5327. *
  5328. * 1. forced value (eg in_left must be 0)
  5329. * 2. pointer to dynamically-allocated memory (eg session, transform)
  5330. * 3. value can be re-derived from other data (eg session keys from MS)
  5331. * 4. value was temporary (eg content of input buffer)
  5332. * 5. value will be provided by the user again (eg I/O callbacks and context)
  5333. */
  5334. int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
  5335. unsigned char *buf,
  5336. size_t buf_len,
  5337. size_t *olen )
  5338. {
  5339. unsigned char *p = buf;
  5340. size_t used = 0;
  5341. size_t session_len;
  5342. int ret = 0;
  5343. /*
  5344. * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
  5345. * this function's documentation.
  5346. *
  5347. * These are due to assumptions/limitations in the implementation. Some of
  5348. * them are likely to stay (no handshake in progress) some might go away
  5349. * (only DTLS) but are currently used to simplify the implementation.
  5350. */
  5351. /* The initial handshake must be over */
  5352. if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
  5353. {
  5354. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
  5355. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5356. }
  5357. if( ssl->handshake != NULL )
  5358. {
  5359. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
  5360. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5361. }
  5362. /* Double-check that sub-structures are indeed ready */
  5363. if( ssl->transform == NULL || ssl->session == NULL )
  5364. {
  5365. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
  5366. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5367. }
  5368. /* There must be no pending incoming or outgoing data */
  5369. if( mbedtls_ssl_check_pending( ssl ) != 0 )
  5370. {
  5371. MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
  5372. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5373. }
  5374. if( ssl->out_left != 0 )
  5375. {
  5376. MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
  5377. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5378. }
  5379. /* Protocol must be DLTS, not TLS */
  5380. if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  5381. {
  5382. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
  5383. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5384. }
  5385. /* Version must be 1.2 */
  5386. if( ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3 )
  5387. {
  5388. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
  5389. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5390. }
  5391. if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
  5392. {
  5393. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
  5394. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5395. }
  5396. /* We must be using an AEAD ciphersuite */
  5397. if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
  5398. {
  5399. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
  5400. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5401. }
  5402. /* Renegotiation must not be enabled */
  5403. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  5404. if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
  5405. {
  5406. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
  5407. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5408. }
  5409. #endif
  5410. /*
  5411. * Version and format identifier
  5412. */
  5413. used += sizeof( ssl_serialized_context_header );
  5414. if( used <= buf_len )
  5415. {
  5416. memcpy( p, ssl_serialized_context_header,
  5417. sizeof( ssl_serialized_context_header ) );
  5418. p += sizeof( ssl_serialized_context_header );
  5419. }
  5420. /*
  5421. * Session (length + data)
  5422. */
  5423. ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
  5424. if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
  5425. return( ret );
  5426. used += 4 + session_len;
  5427. if( used <= buf_len )
  5428. {
  5429. MBEDTLS_PUT_UINT32_BE( session_len, p, 0 );
  5430. p += 4;
  5431. ret = ssl_session_save( ssl->session, 1,
  5432. p, session_len, &session_len );
  5433. if( ret != 0 )
  5434. return( ret );
  5435. p += session_len;
  5436. }
  5437. /*
  5438. * Transform
  5439. */
  5440. used += sizeof( ssl->transform->randbytes );
  5441. if( used <= buf_len )
  5442. {
  5443. memcpy( p, ssl->transform->randbytes,
  5444. sizeof( ssl->transform->randbytes ) );
  5445. p += sizeof( ssl->transform->randbytes );
  5446. }
  5447. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  5448. used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
  5449. if( used <= buf_len )
  5450. {
  5451. *p++ = ssl->transform->in_cid_len;
  5452. memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
  5453. p += ssl->transform->in_cid_len;
  5454. *p++ = ssl->transform->out_cid_len;
  5455. memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
  5456. p += ssl->transform->out_cid_len;
  5457. }
  5458. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  5459. /*
  5460. * Saved fields from top-level ssl_context structure
  5461. */
  5462. #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
  5463. used += 4;
  5464. if( used <= buf_len )
  5465. {
  5466. MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 );
  5467. p += 4;
  5468. }
  5469. #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
  5470. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  5471. used += 16;
  5472. if( used <= buf_len )
  5473. {
  5474. MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 );
  5475. p += 8;
  5476. MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 );
  5477. p += 8;
  5478. }
  5479. #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
  5480. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  5481. used += 1;
  5482. if( used <= buf_len )
  5483. {
  5484. *p++ = ssl->disable_datagram_packing;
  5485. }
  5486. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  5487. used += 8;
  5488. if( used <= buf_len )
  5489. {
  5490. memcpy( p, ssl->cur_out_ctr, 8 );
  5491. p += 8;
  5492. }
  5493. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  5494. used += 2;
  5495. if( used <= buf_len )
  5496. {
  5497. MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 );
  5498. p += 2;
  5499. }
  5500. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  5501. #if defined(MBEDTLS_SSL_ALPN)
  5502. {
  5503. const uint8_t alpn_len = ssl->alpn_chosen
  5504. ? (uint8_t) strlen( ssl->alpn_chosen )
  5505. : 0;
  5506. used += 1 + alpn_len;
  5507. if( used <= buf_len )
  5508. {
  5509. *p++ = alpn_len;
  5510. if( ssl->alpn_chosen != NULL )
  5511. {
  5512. memcpy( p, ssl->alpn_chosen, alpn_len );
  5513. p += alpn_len;
  5514. }
  5515. }
  5516. }
  5517. #endif /* MBEDTLS_SSL_ALPN */
  5518. /*
  5519. * Done
  5520. */
  5521. *olen = used;
  5522. if( used > buf_len )
  5523. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  5524. MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
  5525. return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
  5526. }
  5527. /*
  5528. * Helper to get TLS 1.2 PRF from ciphersuite
  5529. * (Duplicates bits of logic from ssl_set_handshake_prfs().)
  5530. */
  5531. typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
  5532. const char *label,
  5533. const unsigned char *random, size_t rlen,
  5534. unsigned char *dstbuf, size_t dlen );
  5535. static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
  5536. {
  5537. #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
  5538. const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
  5539. mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
  5540. if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
  5541. return( tls_prf_sha384 );
  5542. #else
  5543. (void) ciphersuite_id;
  5544. #endif
  5545. return( tls_prf_sha256 );
  5546. }
  5547. /*
  5548. * Deserialize context, see mbedtls_ssl_context_save() for format.
  5549. *
  5550. * This internal version is wrapped by a public function that cleans up in
  5551. * case of error.
  5552. */
  5553. static int ssl_context_load( mbedtls_ssl_context *ssl,
  5554. const unsigned char *buf,
  5555. size_t len )
  5556. {
  5557. const unsigned char *p = buf;
  5558. const unsigned char * const end = buf + len;
  5559. size_t session_len;
  5560. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  5561. /*
  5562. * The context should have been freshly setup or reset.
  5563. * Give the user an error in case of obvious misuse.
  5564. * (Checking session is useful because it won't be NULL if we're
  5565. * renegotiating, or if the user mistakenly loaded a session first.)
  5566. */
  5567. if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
  5568. ssl->session != NULL )
  5569. {
  5570. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5571. }
  5572. /*
  5573. * We can't check that the config matches the initial one, but we can at
  5574. * least check it matches the requirements for serializing.
  5575. */
  5576. if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
  5577. ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
  5578. ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 ||
  5579. ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ||
  5580. ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 ||
  5581. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  5582. ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
  5583. #endif
  5584. 0 )
  5585. {
  5586. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5587. }
  5588. MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
  5589. /*
  5590. * Check version identifier
  5591. */
  5592. if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
  5593. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5594. if( memcmp( p, ssl_serialized_context_header,
  5595. sizeof( ssl_serialized_context_header ) ) != 0 )
  5596. {
  5597. return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
  5598. }
  5599. p += sizeof( ssl_serialized_context_header );
  5600. /*
  5601. * Session
  5602. */
  5603. if( (size_t)( end - p ) < 4 )
  5604. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5605. session_len = ( (size_t) p[0] << 24 ) |
  5606. ( (size_t) p[1] << 16 ) |
  5607. ( (size_t) p[2] << 8 ) |
  5608. ( (size_t) p[3] );
  5609. p += 4;
  5610. /* This has been allocated by ssl_handshake_init(), called by
  5611. * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
  5612. ssl->session = ssl->session_negotiate;
  5613. ssl->session_in = ssl->session;
  5614. ssl->session_out = ssl->session;
  5615. ssl->session_negotiate = NULL;
  5616. if( (size_t)( end - p ) < session_len )
  5617. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5618. ret = ssl_session_load( ssl->session, 1, p, session_len );
  5619. if( ret != 0 )
  5620. {
  5621. mbedtls_ssl_session_free( ssl->session );
  5622. return( ret );
  5623. }
  5624. p += session_len;
  5625. /*
  5626. * Transform
  5627. */
  5628. /* This has been allocated by ssl_handshake_init(), called by
  5629. * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
  5630. ssl->transform = ssl->transform_negotiate;
  5631. ssl->transform_in = ssl->transform;
  5632. ssl->transform_out = ssl->transform;
  5633. ssl->transform_negotiate = NULL;
  5634. /* Read random bytes and populate structure */
  5635. if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
  5636. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5637. ret = ssl_populate_transform( ssl->transform,
  5638. ssl->session->ciphersuite,
  5639. ssl->session->master,
  5640. #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
  5641. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  5642. ssl->session->encrypt_then_mac,
  5643. #endif
  5644. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  5645. ssl->session->trunc_hmac,
  5646. #endif
  5647. #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
  5648. #if defined(MBEDTLS_ZLIB_SUPPORT)
  5649. ssl->session->compression,
  5650. #endif
  5651. ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
  5652. p, /* currently pointing to randbytes */
  5653. MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
  5654. ssl->conf->endpoint,
  5655. ssl );
  5656. if( ret != 0 )
  5657. return( ret );
  5658. p += sizeof( ssl->transform->randbytes );
  5659. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  5660. /* Read connection IDs and store them */
  5661. if( (size_t)( end - p ) < 1 )
  5662. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5663. ssl->transform->in_cid_len = *p++;
  5664. if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
  5665. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5666. memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
  5667. p += ssl->transform->in_cid_len;
  5668. ssl->transform->out_cid_len = *p++;
  5669. if( (size_t)( end - p ) < ssl->transform->out_cid_len )
  5670. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5671. memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
  5672. p += ssl->transform->out_cid_len;
  5673. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  5674. /*
  5675. * Saved fields from top-level ssl_context structure
  5676. */
  5677. #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
  5678. if( (size_t)( end - p ) < 4 )
  5679. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5680. ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
  5681. ( (uint32_t) p[1] << 16 ) |
  5682. ( (uint32_t) p[2] << 8 ) |
  5683. ( (uint32_t) p[3] );
  5684. p += 4;
  5685. #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
  5686. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  5687. if( (size_t)( end - p ) < 16 )
  5688. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5689. ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
  5690. ( (uint64_t) p[1] << 48 ) |
  5691. ( (uint64_t) p[2] << 40 ) |
  5692. ( (uint64_t) p[3] << 32 ) |
  5693. ( (uint64_t) p[4] << 24 ) |
  5694. ( (uint64_t) p[5] << 16 ) |
  5695. ( (uint64_t) p[6] << 8 ) |
  5696. ( (uint64_t) p[7] );
  5697. p += 8;
  5698. ssl->in_window = ( (uint64_t) p[0] << 56 ) |
  5699. ( (uint64_t) p[1] << 48 ) |
  5700. ( (uint64_t) p[2] << 40 ) |
  5701. ( (uint64_t) p[3] << 32 ) |
  5702. ( (uint64_t) p[4] << 24 ) |
  5703. ( (uint64_t) p[5] << 16 ) |
  5704. ( (uint64_t) p[6] << 8 ) |
  5705. ( (uint64_t) p[7] );
  5706. p += 8;
  5707. #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
  5708. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  5709. if( (size_t)( end - p ) < 1 )
  5710. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5711. ssl->disable_datagram_packing = *p++;
  5712. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  5713. if( (size_t)( end - p ) < 8 )
  5714. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5715. memcpy( ssl->cur_out_ctr, p, 8 );
  5716. p += 8;
  5717. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  5718. if( (size_t)( end - p ) < 2 )
  5719. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5720. ssl->mtu = ( p[0] << 8 ) | p[1];
  5721. p += 2;
  5722. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  5723. #if defined(MBEDTLS_SSL_ALPN)
  5724. {
  5725. uint8_t alpn_len;
  5726. const char **cur;
  5727. if( (size_t)( end - p ) < 1 )
  5728. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5729. alpn_len = *p++;
  5730. if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
  5731. {
  5732. /* alpn_chosen should point to an item in the configured list */
  5733. for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
  5734. {
  5735. if( strlen( *cur ) == alpn_len &&
  5736. memcmp( p, cur, alpn_len ) == 0 )
  5737. {
  5738. ssl->alpn_chosen = *cur;
  5739. break;
  5740. }
  5741. }
  5742. }
  5743. /* can only happen on conf mismatch */
  5744. if( alpn_len != 0 && ssl->alpn_chosen == NULL )
  5745. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5746. p += alpn_len;
  5747. }
  5748. #endif /* MBEDTLS_SSL_ALPN */
  5749. /*
  5750. * Forced fields from top-level ssl_context structure
  5751. *
  5752. * Most of them already set to the correct value by mbedtls_ssl_init() and
  5753. * mbedtls_ssl_reset(), so we only need to set the remaining ones.
  5754. */
  5755. ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
  5756. ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
  5757. ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
  5758. /* Adjust pointers for header fields of outgoing records to
  5759. * the given transform, accounting for explicit IV and CID. */
  5760. mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
  5761. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  5762. ssl->in_epoch = 1;
  5763. #endif
  5764. /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
  5765. * which we don't want - otherwise we'd end up freeing the wrong transform
  5766. * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
  5767. * inappropriately. */
  5768. if( ssl->handshake != NULL )
  5769. {
  5770. mbedtls_ssl_handshake_free( ssl );
  5771. mbedtls_free( ssl->handshake );
  5772. ssl->handshake = NULL;
  5773. }
  5774. /*
  5775. * Done - should have consumed entire buffer
  5776. */
  5777. if( p != end )
  5778. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  5779. return( 0 );
  5780. }
  5781. /*
  5782. * Deserialize context: public wrapper for error cleaning
  5783. */
  5784. int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
  5785. const unsigned char *buf,
  5786. size_t len )
  5787. {
  5788. int ret = ssl_context_load( context, buf, len );
  5789. if( ret != 0 )
  5790. mbedtls_ssl_free( context );
  5791. return( ret );
  5792. }
  5793. #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
  5794. /*
  5795. * Free an SSL context
  5796. */
  5797. void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
  5798. {
  5799. if( ssl == NULL )
  5800. return;
  5801. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
  5802. if( ssl->out_buf != NULL )
  5803. {
  5804. #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
  5805. size_t out_buf_len = ssl->out_buf_len;
  5806. #else
  5807. size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
  5808. #endif
  5809. mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
  5810. mbedtls_free( ssl->out_buf );
  5811. ssl->out_buf = NULL;
  5812. }
  5813. if( ssl->in_buf != NULL )
  5814. {
  5815. #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
  5816. size_t in_buf_len = ssl->in_buf_len;
  5817. #else
  5818. size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
  5819. #endif
  5820. mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
  5821. mbedtls_free( ssl->in_buf );
  5822. ssl->in_buf = NULL;
  5823. }
  5824. #if defined(MBEDTLS_ZLIB_SUPPORT)
  5825. if( ssl->compress_buf != NULL )
  5826. {
  5827. mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
  5828. mbedtls_free( ssl->compress_buf );
  5829. }
  5830. #endif
  5831. if( ssl->transform )
  5832. {
  5833. mbedtls_ssl_transform_free( ssl->transform );
  5834. mbedtls_free( ssl->transform );
  5835. }
  5836. if( ssl->handshake )
  5837. {
  5838. mbedtls_ssl_handshake_free( ssl );
  5839. mbedtls_ssl_transform_free( ssl->transform_negotiate );
  5840. mbedtls_ssl_session_free( ssl->session_negotiate );
  5841. mbedtls_free( ssl->handshake );
  5842. mbedtls_free( ssl->transform_negotiate );
  5843. mbedtls_free( ssl->session_negotiate );
  5844. }
  5845. if( ssl->session )
  5846. {
  5847. mbedtls_ssl_session_free( ssl->session );
  5848. mbedtls_free( ssl->session );
  5849. }
  5850. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  5851. if( ssl->hostname != NULL )
  5852. {
  5853. mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
  5854. mbedtls_free( ssl->hostname );
  5855. }
  5856. #endif
  5857. #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
  5858. if( mbedtls_ssl_hw_record_finish != NULL )
  5859. {
  5860. MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
  5861. mbedtls_ssl_hw_record_finish( ssl );
  5862. }
  5863. #endif
  5864. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
  5865. mbedtls_free( ssl->cli_id );
  5866. #endif
  5867. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
  5868. /* Actually clear after last debug message */
  5869. mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
  5870. }
  5871. /*
  5872. * Initialze mbedtls_ssl_config
  5873. */
  5874. void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
  5875. {
  5876. memset( conf, 0, sizeof( mbedtls_ssl_config ) );
  5877. }
  5878. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  5879. static int ssl_preset_default_hashes[] = {
  5880. #if defined(MBEDTLS_SHA512_C)
  5881. MBEDTLS_MD_SHA512,
  5882. #endif
  5883. #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
  5884. MBEDTLS_MD_SHA384,
  5885. #endif
  5886. #if defined(MBEDTLS_SHA256_C)
  5887. MBEDTLS_MD_SHA256,
  5888. MBEDTLS_MD_SHA224,
  5889. #endif
  5890. #if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
  5891. MBEDTLS_MD_SHA1,
  5892. #endif
  5893. MBEDTLS_MD_NONE
  5894. };
  5895. #endif
  5896. static int ssl_preset_suiteb_ciphersuites[] = {
  5897. MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  5898. MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
  5899. 0
  5900. };
  5901. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  5902. static int ssl_preset_suiteb_hashes[] = {
  5903. MBEDTLS_MD_SHA256,
  5904. MBEDTLS_MD_SHA384,
  5905. MBEDTLS_MD_NONE
  5906. };
  5907. #endif
  5908. #if defined(MBEDTLS_ECP_C)
  5909. static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
  5910. #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
  5911. MBEDTLS_ECP_DP_SECP256R1,
  5912. #endif
  5913. #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
  5914. MBEDTLS_ECP_DP_SECP384R1,
  5915. #endif
  5916. MBEDTLS_ECP_DP_NONE
  5917. };
  5918. #endif
  5919. /*
  5920. * Load default in mbedtls_ssl_config
  5921. */
  5922. int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
  5923. int endpoint, int transport, int preset )
  5924. {
  5925. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
  5926. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  5927. #endif
  5928. /* Use the functions here so that they are covered in tests,
  5929. * but otherwise access member directly for efficiency */
  5930. mbedtls_ssl_conf_endpoint( conf, endpoint );
  5931. mbedtls_ssl_conf_transport( conf, transport );
  5932. /*
  5933. * Things that are common to all presets
  5934. */
  5935. #if defined(MBEDTLS_SSL_CLI_C)
  5936. if( endpoint == MBEDTLS_SSL_IS_CLIENT )
  5937. {
  5938. conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
  5939. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  5940. conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
  5941. #endif
  5942. }
  5943. #endif
  5944. #if defined(MBEDTLS_ARC4_C)
  5945. conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
  5946. #endif
  5947. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  5948. conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
  5949. #endif
  5950. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  5951. conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
  5952. #endif
  5953. #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
  5954. conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
  5955. #endif
  5956. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
  5957. conf->f_cookie_write = ssl_cookie_write_dummy;
  5958. conf->f_cookie_check = ssl_cookie_check_dummy;
  5959. #endif
  5960. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  5961. conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
  5962. #endif
  5963. #if defined(MBEDTLS_SSL_SRV_C)
  5964. conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
  5965. #endif
  5966. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  5967. conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
  5968. conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
  5969. #endif
  5970. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  5971. conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
  5972. memset( conf->renego_period, 0x00, 2 );
  5973. memset( conf->renego_period + 2, 0xFF, 6 );
  5974. #endif
  5975. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
  5976. if( endpoint == MBEDTLS_SSL_IS_SERVER )
  5977. {
  5978. const unsigned char dhm_p[] =
  5979. MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
  5980. const unsigned char dhm_g[] =
  5981. MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
  5982. if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
  5983. dhm_p, sizeof( dhm_p ),
  5984. dhm_g, sizeof( dhm_g ) ) ) != 0 )
  5985. {
  5986. return( ret );
  5987. }
  5988. }
  5989. #endif
  5990. /*
  5991. * Preset-specific defaults
  5992. */
  5993. switch( preset )
  5994. {
  5995. /*
  5996. * NSA Suite B
  5997. */
  5998. case MBEDTLS_SSL_PRESET_SUITEB:
  5999. conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
  6000. conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
  6001. conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
  6002. conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
  6003. conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
  6004. conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
  6005. conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
  6006. conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
  6007. ssl_preset_suiteb_ciphersuites;
  6008. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  6009. conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
  6010. #endif
  6011. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  6012. conf->sig_hashes = ssl_preset_suiteb_hashes;
  6013. #endif
  6014. #if defined(MBEDTLS_ECP_C)
  6015. conf->curve_list = ssl_preset_suiteb_curves;
  6016. #endif
  6017. break;
  6018. /*
  6019. * Default
  6020. */
  6021. default:
  6022. conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
  6023. MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
  6024. MBEDTLS_SSL_MIN_MAJOR_VERSION :
  6025. MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
  6026. conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
  6027. MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
  6028. MBEDTLS_SSL_MIN_MINOR_VERSION :
  6029. MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
  6030. conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
  6031. conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
  6032. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  6033. if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  6034. conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
  6035. #endif
  6036. conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
  6037. conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
  6038. conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
  6039. conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
  6040. mbedtls_ssl_list_ciphersuites();
  6041. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  6042. conf->cert_profile = &mbedtls_x509_crt_profile_default;
  6043. #endif
  6044. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  6045. conf->sig_hashes = ssl_preset_default_hashes;
  6046. #endif
  6047. #if defined(MBEDTLS_ECP_C)
  6048. conf->curve_list = mbedtls_ecp_grp_id_list();
  6049. #endif
  6050. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
  6051. conf->dhm_min_bitlen = 1024;
  6052. #endif
  6053. }
  6054. return( 0 );
  6055. }
  6056. /*
  6057. * Free mbedtls_ssl_config
  6058. */
  6059. void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
  6060. {
  6061. #if defined(MBEDTLS_DHM_C)
  6062. mbedtls_mpi_free( &conf->dhm_P );
  6063. mbedtls_mpi_free( &conf->dhm_G );
  6064. #endif
  6065. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  6066. if( conf->psk != NULL )
  6067. {
  6068. mbedtls_platform_zeroize( conf->psk, conf->psk_len );
  6069. mbedtls_free( conf->psk );
  6070. conf->psk = NULL;
  6071. conf->psk_len = 0;
  6072. }
  6073. if( conf->psk_identity != NULL )
  6074. {
  6075. mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
  6076. mbedtls_free( conf->psk_identity );
  6077. conf->psk_identity = NULL;
  6078. conf->psk_identity_len = 0;
  6079. }
  6080. #endif
  6081. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  6082. ssl_key_cert_free( conf->key_cert );
  6083. #endif
  6084. mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
  6085. }
  6086. #if defined(MBEDTLS_PK_C) && \
  6087. ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
  6088. /*
  6089. * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
  6090. */
  6091. unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
  6092. {
  6093. #if defined(MBEDTLS_RSA_C)
  6094. if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
  6095. return( MBEDTLS_SSL_SIG_RSA );
  6096. #endif
  6097. #if defined(MBEDTLS_ECDSA_C)
  6098. if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
  6099. return( MBEDTLS_SSL_SIG_ECDSA );
  6100. #endif
  6101. return( MBEDTLS_SSL_SIG_ANON );
  6102. }
  6103. unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
  6104. {
  6105. switch( type ) {
  6106. case MBEDTLS_PK_RSA:
  6107. return( MBEDTLS_SSL_SIG_RSA );
  6108. case MBEDTLS_PK_ECDSA:
  6109. case MBEDTLS_PK_ECKEY:
  6110. return( MBEDTLS_SSL_SIG_ECDSA );
  6111. default:
  6112. return( MBEDTLS_SSL_SIG_ANON );
  6113. }
  6114. }
  6115. mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
  6116. {
  6117. switch( sig )
  6118. {
  6119. #if defined(MBEDTLS_RSA_C)
  6120. case MBEDTLS_SSL_SIG_RSA:
  6121. return( MBEDTLS_PK_RSA );
  6122. #endif
  6123. #if defined(MBEDTLS_ECDSA_C)
  6124. case MBEDTLS_SSL_SIG_ECDSA:
  6125. return( MBEDTLS_PK_ECDSA );
  6126. #endif
  6127. default:
  6128. return( MBEDTLS_PK_NONE );
  6129. }
  6130. }
  6131. #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
  6132. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  6133. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  6134. /* Find an entry in a signature-hash set matching a given hash algorithm. */
  6135. mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
  6136. mbedtls_pk_type_t sig_alg )
  6137. {
  6138. switch( sig_alg )
  6139. {
  6140. case MBEDTLS_PK_RSA:
  6141. return( set->rsa );
  6142. case MBEDTLS_PK_ECDSA:
  6143. return( set->ecdsa );
  6144. default:
  6145. return( MBEDTLS_MD_NONE );
  6146. }
  6147. }
  6148. /* Add a signature-hash-pair to a signature-hash set */
  6149. void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
  6150. mbedtls_pk_type_t sig_alg,
  6151. mbedtls_md_type_t md_alg )
  6152. {
  6153. switch( sig_alg )
  6154. {
  6155. case MBEDTLS_PK_RSA:
  6156. if( set->rsa == MBEDTLS_MD_NONE )
  6157. set->rsa = md_alg;
  6158. break;
  6159. case MBEDTLS_PK_ECDSA:
  6160. if( set->ecdsa == MBEDTLS_MD_NONE )
  6161. set->ecdsa = md_alg;
  6162. break;
  6163. default:
  6164. break;
  6165. }
  6166. }
  6167. /* Allow exactly one hash algorithm for each signature. */
  6168. void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
  6169. mbedtls_md_type_t md_alg )
  6170. {
  6171. set->rsa = md_alg;
  6172. set->ecdsa = md_alg;
  6173. }
  6174. #endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
  6175. MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  6176. /*
  6177. * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
  6178. */
  6179. mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
  6180. {
  6181. switch( hash )
  6182. {
  6183. #if defined(MBEDTLS_MD5_C)
  6184. case MBEDTLS_SSL_HASH_MD5:
  6185. return( MBEDTLS_MD_MD5 );
  6186. #endif
  6187. #if defined(MBEDTLS_SHA1_C)
  6188. case MBEDTLS_SSL_HASH_SHA1:
  6189. return( MBEDTLS_MD_SHA1 );
  6190. #endif
  6191. #if defined(MBEDTLS_SHA256_C)
  6192. case MBEDTLS_SSL_HASH_SHA224:
  6193. return( MBEDTLS_MD_SHA224 );
  6194. case MBEDTLS_SSL_HASH_SHA256:
  6195. return( MBEDTLS_MD_SHA256 );
  6196. #endif
  6197. #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
  6198. case MBEDTLS_SSL_HASH_SHA384:
  6199. return( MBEDTLS_MD_SHA384 );
  6200. #endif
  6201. #if defined(MBEDTLS_SHA512_C)
  6202. case MBEDTLS_SSL_HASH_SHA512:
  6203. return( MBEDTLS_MD_SHA512 );
  6204. #endif
  6205. default:
  6206. return( MBEDTLS_MD_NONE );
  6207. }
  6208. }
  6209. /*
  6210. * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
  6211. */
  6212. unsigned char mbedtls_ssl_hash_from_md_alg( int md )
  6213. {
  6214. switch( md )
  6215. {
  6216. #if defined(MBEDTLS_MD5_C)
  6217. case MBEDTLS_MD_MD5:
  6218. return( MBEDTLS_SSL_HASH_MD5 );
  6219. #endif
  6220. #if defined(MBEDTLS_SHA1_C)
  6221. case MBEDTLS_MD_SHA1:
  6222. return( MBEDTLS_SSL_HASH_SHA1 );
  6223. #endif
  6224. #if defined(MBEDTLS_SHA256_C)
  6225. case MBEDTLS_MD_SHA224:
  6226. return( MBEDTLS_SSL_HASH_SHA224 );
  6227. case MBEDTLS_MD_SHA256:
  6228. return( MBEDTLS_SSL_HASH_SHA256 );
  6229. #endif
  6230. #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
  6231. case MBEDTLS_MD_SHA384:
  6232. return( MBEDTLS_SSL_HASH_SHA384 );
  6233. #endif
  6234. #if defined(MBEDTLS_SHA512_C)
  6235. case MBEDTLS_MD_SHA512:
  6236. return( MBEDTLS_SSL_HASH_SHA512 );
  6237. #endif
  6238. default:
  6239. return( MBEDTLS_SSL_HASH_NONE );
  6240. }
  6241. }
  6242. #if defined(MBEDTLS_ECP_C)
  6243. /*
  6244. * Check if a curve proposed by the peer is in our list.
  6245. * Return 0 if we're willing to use it, -1 otherwise.
  6246. */
  6247. int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
  6248. {
  6249. const mbedtls_ecp_group_id *gid;
  6250. if( ssl->conf->curve_list == NULL )
  6251. return( -1 );
  6252. for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
  6253. if( *gid == grp_id )
  6254. return( 0 );
  6255. return( -1 );
  6256. }
  6257. #endif /* MBEDTLS_ECP_C */
  6258. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  6259. /*
  6260. * Check if a hash proposed by the peer is in our list.
  6261. * Return 0 if we're willing to use it, -1 otherwise.
  6262. */
  6263. int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
  6264. mbedtls_md_type_t md )
  6265. {
  6266. const int *cur;
  6267. if( ssl->conf->sig_hashes == NULL )
  6268. return( -1 );
  6269. for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
  6270. if( *cur == (int) md )
  6271. return( 0 );
  6272. return( -1 );
  6273. }
  6274. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  6275. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  6276. int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
  6277. const mbedtls_ssl_ciphersuite_t *ciphersuite,
  6278. int cert_endpoint,
  6279. uint32_t *flags )
  6280. {
  6281. int ret = 0;
  6282. #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
  6283. int usage = 0;
  6284. #endif
  6285. #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
  6286. const char *ext_oid;
  6287. size_t ext_len;
  6288. #endif
  6289. #if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
  6290. !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
  6291. ((void) cert);
  6292. ((void) cert_endpoint);
  6293. ((void) flags);
  6294. #endif
  6295. #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
  6296. if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
  6297. {
  6298. /* Server part of the key exchange */
  6299. switch( ciphersuite->key_exchange )
  6300. {
  6301. case MBEDTLS_KEY_EXCHANGE_RSA:
  6302. case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
  6303. usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
  6304. break;
  6305. case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
  6306. case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
  6307. case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
  6308. usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
  6309. break;
  6310. case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
  6311. case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
  6312. usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
  6313. break;
  6314. /* Don't use default: we want warnings when adding new values */
  6315. case MBEDTLS_KEY_EXCHANGE_NONE:
  6316. case MBEDTLS_KEY_EXCHANGE_PSK:
  6317. case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
  6318. case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
  6319. case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
  6320. usage = 0;
  6321. }
  6322. }
  6323. else
  6324. {
  6325. /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
  6326. usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
  6327. }
  6328. if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
  6329. {
  6330. *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
  6331. ret = -1;
  6332. }
  6333. #else
  6334. ((void) ciphersuite);
  6335. #endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
  6336. #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
  6337. if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
  6338. {
  6339. ext_oid = MBEDTLS_OID_SERVER_AUTH;
  6340. ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
  6341. }
  6342. else
  6343. {
  6344. ext_oid = MBEDTLS_OID_CLIENT_AUTH;
  6345. ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
  6346. }
  6347. if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
  6348. {
  6349. *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
  6350. ret = -1;
  6351. }
  6352. #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
  6353. return( ret );
  6354. }
  6355. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  6356. int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
  6357. {
  6358. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  6359. if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
  6360. return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
  6361. switch( md )
  6362. {
  6363. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
  6364. #if defined(MBEDTLS_MD5_C)
  6365. case MBEDTLS_SSL_HASH_MD5:
  6366. return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
  6367. #endif
  6368. #if defined(MBEDTLS_SHA1_C)
  6369. case MBEDTLS_SSL_HASH_SHA1:
  6370. ssl->handshake->calc_verify = ssl_calc_verify_tls;
  6371. break;
  6372. #endif
  6373. #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
  6374. #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
  6375. case MBEDTLS_SSL_HASH_SHA384:
  6376. ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
  6377. break;
  6378. #endif
  6379. #if defined(MBEDTLS_SHA256_C)
  6380. case MBEDTLS_SSL_HASH_SHA256:
  6381. ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
  6382. break;
  6383. #endif
  6384. default:
  6385. return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
  6386. }
  6387. return 0;
  6388. #else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
  6389. (void) ssl;
  6390. (void) md;
  6391. return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
  6392. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  6393. }
  6394. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  6395. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  6396. int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
  6397. unsigned char *output,
  6398. unsigned char *data, size_t data_len )
  6399. {
  6400. int ret = 0;
  6401. mbedtls_md5_context mbedtls_md5;
  6402. mbedtls_sha1_context mbedtls_sha1;
  6403. mbedtls_md5_init( &mbedtls_md5 );
  6404. mbedtls_sha1_init( &mbedtls_sha1 );
  6405. /*
  6406. * digitally-signed struct {
  6407. * opaque md5_hash[16];
  6408. * opaque sha_hash[20];
  6409. * };
  6410. *
  6411. * md5_hash
  6412. * MD5(ClientHello.random + ServerHello.random
  6413. * + ServerParams);
  6414. * sha_hash
  6415. * SHA(ClientHello.random + ServerHello.random
  6416. * + ServerParams);
  6417. */
  6418. if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
  6419. {
  6420. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
  6421. goto exit;
  6422. }
  6423. if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
  6424. ssl->handshake->randbytes, 64 ) ) != 0 )
  6425. {
  6426. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
  6427. goto exit;
  6428. }
  6429. if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
  6430. {
  6431. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
  6432. goto exit;
  6433. }
  6434. if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
  6435. {
  6436. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
  6437. goto exit;
  6438. }
  6439. if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
  6440. {
  6441. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
  6442. goto exit;
  6443. }
  6444. if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
  6445. ssl->handshake->randbytes, 64 ) ) != 0 )
  6446. {
  6447. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
  6448. goto exit;
  6449. }
  6450. if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
  6451. data_len ) ) != 0 )
  6452. {
  6453. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
  6454. goto exit;
  6455. }
  6456. if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
  6457. output + 16 ) ) != 0 )
  6458. {
  6459. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
  6460. goto exit;
  6461. }
  6462. exit:
  6463. mbedtls_md5_free( &mbedtls_md5 );
  6464. mbedtls_sha1_free( &mbedtls_sha1 );
  6465. if( ret != 0 )
  6466. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  6467. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
  6468. return( ret );
  6469. }
  6470. #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
  6471. MBEDTLS_SSL_PROTO_TLS1_1 */
  6472. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  6473. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  6474. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  6475. int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
  6476. unsigned char *hash, size_t *hashlen,
  6477. unsigned char *data, size_t data_len,
  6478. mbedtls_md_type_t md_alg )
  6479. {
  6480. psa_status_t status;
  6481. psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
  6482. psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
  6483. MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
  6484. if( ( status = psa_hash_setup( &hash_operation,
  6485. hash_alg ) ) != PSA_SUCCESS )
  6486. {
  6487. MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
  6488. goto exit;
  6489. }
  6490. if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
  6491. 64 ) ) != PSA_SUCCESS )
  6492. {
  6493. MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
  6494. goto exit;
  6495. }
  6496. if( ( status = psa_hash_update( &hash_operation,
  6497. data, data_len ) ) != PSA_SUCCESS )
  6498. {
  6499. MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
  6500. goto exit;
  6501. }
  6502. if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE,
  6503. hashlen ) ) != PSA_SUCCESS )
  6504. {
  6505. MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
  6506. goto exit;
  6507. }
  6508. exit:
  6509. if( status != PSA_SUCCESS )
  6510. {
  6511. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  6512. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
  6513. switch( status )
  6514. {
  6515. case PSA_ERROR_NOT_SUPPORTED:
  6516. return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
  6517. case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
  6518. case PSA_ERROR_BUFFER_TOO_SMALL:
  6519. return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  6520. case PSA_ERROR_INSUFFICIENT_MEMORY:
  6521. return( MBEDTLS_ERR_MD_ALLOC_FAILED );
  6522. default:
  6523. return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
  6524. }
  6525. }
  6526. return( 0 );
  6527. }
  6528. #else
  6529. int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
  6530. unsigned char *hash, size_t *hashlen,
  6531. unsigned char *data, size_t data_len,
  6532. mbedtls_md_type_t md_alg )
  6533. {
  6534. int ret = 0;
  6535. mbedtls_md_context_t ctx;
  6536. const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
  6537. *hashlen = mbedtls_md_get_size( md_info );
  6538. MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
  6539. mbedtls_md_init( &ctx );
  6540. /*
  6541. * digitally-signed struct {
  6542. * opaque client_random[32];
  6543. * opaque server_random[32];
  6544. * ServerDHParams params;
  6545. * };
  6546. */
  6547. if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
  6548. {
  6549. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
  6550. goto exit;
  6551. }
  6552. if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
  6553. {
  6554. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
  6555. goto exit;
  6556. }
  6557. if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
  6558. {
  6559. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
  6560. goto exit;
  6561. }
  6562. if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
  6563. {
  6564. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
  6565. goto exit;
  6566. }
  6567. if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
  6568. {
  6569. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
  6570. goto exit;
  6571. }
  6572. exit:
  6573. mbedtls_md_free( &ctx );
  6574. if( ret != 0 )
  6575. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  6576. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
  6577. return( ret );
  6578. }
  6579. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  6580. #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
  6581. MBEDTLS_SSL_PROTO_TLS1_2 */
  6582. #endif /* MBEDTLS_SSL_TLS_C */