ssl_tls.c 238 KB

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