ssl_tls.c 238 KB

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