ssl_tls.c 274 KB

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