ssl_tls.c 240 KB

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