ssl_tls.c 261 KB

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