ssl_tls.c 269 KB

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