btm_sec.c 257 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 1999-2012 Broadcom Corporation
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. /******************************************************************************
  19. *
  20. * This file contains functions for the Bluetooth Security Manager
  21. *
  22. ******************************************************************************/
  23. //#define LOG_TAG "bt_btm_sec"
  24. #include <stdarg.h>
  25. #include <string.h>
  26. #include "stack/bt_types.h"
  27. #include "device/controller.h"
  28. #include "stack/hcimsgs.h"
  29. #include "stack/btu.h"
  30. #include "btm_int.h"
  31. #include "l2c_int.h"
  32. #include "osi/fixed_queue.h"
  33. #include "osi/alarm.h"
  34. #include "stack/btm_ble_api.h"
  35. #if (BT_USE_TRACES == TRUE && BT_TRACE_VERBOSE == FALSE)
  36. /* needed for sprintf() */
  37. #include <stdio.h>
  38. #endif
  39. #if BLE_INCLUDED == TRUE
  40. #include "gatt_int.h"
  41. #endif
  42. #define BTM_SEC_MAX_COLLISION_DELAY (5000)
  43. #ifdef APPL_AUTH_WRITE_EXCEPTION
  44. BOOLEAN (APPL_AUTH_WRITE_EXCEPTION)(BD_ADDR bd_addr);
  45. #endif
  46. /********************************************************************************
  47. ** L O C A L F U N C T I O N P R O T O T Y P E S *
  48. *********************************************************************************/
  49. #if (SMP_INCLUDED == TRUE)
  50. static tBTM_SEC_SERV_REC *btm_sec_find_next_serv (tBTM_SEC_SERV_REC *p_cur);
  51. static tBTM_SEC_SERV_REC *btm_sec_find_mx_serv (UINT8 is_originator, UINT16 psm,
  52. UINT32 mx_proto_id,
  53. UINT32 mx_chan_id);
  54. static tBTM_STATUS btm_sec_execute_procedure (tBTM_SEC_DEV_REC *p_dev_rec);
  55. static BOOLEAN btm_sec_start_get_name (tBTM_SEC_DEV_REC *p_dev_rec);
  56. static BOOLEAN btm_sec_start_authentication (tBTM_SEC_DEV_REC *p_dev_rec);
  57. static BOOLEAN btm_sec_start_encryption (tBTM_SEC_DEV_REC *p_dev_rec);
  58. static void btm_sec_collision_timeout (TIMER_LIST_ENT *p_tle);
  59. static void btm_restore_mode(void);
  60. static void btm_sec_pairing_timeout (TIMER_LIST_ENT *p_tle);
  61. static tBTM_STATUS btm_sec_dd_create_conn (tBTM_SEC_DEV_REC *p_dev_rec);
  62. static void btm_sec_change_pairing_state (tBTM_PAIRING_STATE new_state);
  63. #endif ///SMP_INCLUDED == TRUE
  64. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  65. static char *btm_pair_state_descr (tBTM_PAIRING_STATE state);
  66. #endif
  67. #if (SMP_INCLUDED == TRUE)
  68. static void btm_sec_check_pending_reqs(void);
  69. static BOOLEAN btm_sec_queue_mx_request (BD_ADDR bd_addr, UINT16 psm, BOOLEAN is_orig,
  70. UINT32 mx_proto_id, UINT32 mx_chan_id,
  71. tBTM_SEC_CALLBACK *p_callback, void *p_ref_data);
  72. static void btm_sec_bond_cancel_complete (void);
  73. static void btm_send_link_key_notif (tBTM_SEC_DEV_REC *p_dev_rec);
  74. static BOOLEAN btm_sec_check_prefetch_pin (tBTM_SEC_DEV_REC *p_dev_rec);
  75. static UINT8 btm_sec_start_authorization (tBTM_SEC_DEV_REC *p_dev_rec);
  76. #endif ///SMP_INCLUDED == TRUE
  77. BOOLEAN btm_sec_are_all_trusted(UINT32 p_mask[]);
  78. #if (SMP_INCLUDED == TRUE)
  79. static tBTM_STATUS btm_sec_send_hci_disconnect (tBTM_SEC_DEV_REC *p_dev_rec, UINT8 reason, UINT16 conn_handle);
  80. #endif ///SMP_INCLUDED == TRUE
  81. UINT8 btm_sec_start_role_switch (tBTM_SEC_DEV_REC *p_dev_rec);
  82. tBTM_SEC_DEV_REC *btm_sec_find_dev_by_sec_state (UINT8 state);
  83. static BOOLEAN btm_sec_set_security_level ( CONNECTION_TYPE conn_type, const char *p_name, UINT8 service_id,
  84. UINT16 sec_level, UINT16 psm, UINT32 mx_proto_id,
  85. UINT32 mx_chan_id);
  86. #if (SMP_INCLUDED == TRUE)
  87. static BOOLEAN btm_dev_authenticated(tBTM_SEC_DEV_REC *p_dev_rec);
  88. static BOOLEAN btm_dev_encrypted(tBTM_SEC_DEV_REC *p_dev_rec);
  89. static BOOLEAN btm_dev_authorized(tBTM_SEC_DEV_REC *p_dev_rec);
  90. static BOOLEAN btm_serv_trusted(tBTM_SEC_DEV_REC *p_dev_rec, tBTM_SEC_SERV_REC *p_serv_rec);
  91. static BOOLEAN btm_sec_is_serv_level0 (UINT16 psm);
  92. static UINT16 btm_sec_set_serv_level4_flags (UINT16 cur_security, BOOLEAN is_originator);
  93. static BOOLEAN btm_sec_queue_encrypt_request (BD_ADDR bd_addr, tBT_TRANSPORT transport,
  94. tBTM_SEC_CALLBACK *p_callback, void *p_ref_data);
  95. static void btm_sec_check_pending_enc_req (tBTM_SEC_DEV_REC *p_dev_rec, tBT_TRANSPORT transport,
  96. UINT8 encr_enable);
  97. static BOOLEAN btm_sec_use_smp_br_chnl(tBTM_SEC_DEV_REC *p_dev_rec);
  98. static BOOLEAN btm_sec_is_master(tBTM_SEC_DEV_REC *p_dev_rec);
  99. #endif ///SMP_INCLUDED == TRUE
  100. /* TRUE - authenticated link key is possible */
  101. static const BOOLEAN btm_sec_io_map [BTM_IO_CAP_MAX][BTM_IO_CAP_MAX] = {
  102. /* OUT, IO, IN, NONE */
  103. /* OUT */ {FALSE, FALSE, TRUE, FALSE},
  104. /* IO */ {FALSE, TRUE, TRUE, FALSE},
  105. /* IN */ {TRUE, TRUE, TRUE, FALSE},
  106. /* NONE */ {FALSE, FALSE, FALSE, FALSE}
  107. };
  108. /* BTM_IO_CAP_OUT 0 DisplayOnly */
  109. /* BTM_IO_CAP_IO 1 DisplayYesNo */
  110. /* BTM_IO_CAP_IN 2 KeyboardOnly */
  111. /* BTM_IO_CAP_NONE 3 NoInputNoOutput */
  112. /*******************************************************************************
  113. **
  114. ** Function btm_dev_authenticated
  115. **
  116. ** Description check device is authenticated
  117. **
  118. ** Returns BOOLEAN TRUE or FALSE
  119. **
  120. *******************************************************************************/
  121. #if (SMP_INCLUDED == TRUE)
  122. static BOOLEAN btm_dev_authenticated (tBTM_SEC_DEV_REC *p_dev_rec)
  123. {
  124. if (p_dev_rec->sec_flags & BTM_SEC_AUTHENTICATED) {
  125. return (TRUE);
  126. }
  127. return (FALSE);
  128. }
  129. /*******************************************************************************
  130. **
  131. ** Function btm_dev_encrypted
  132. **
  133. ** Description check device is encrypted
  134. **
  135. ** Returns BOOLEAN TRUE or FALSE
  136. **
  137. *******************************************************************************/
  138. static BOOLEAN btm_dev_encrypted (tBTM_SEC_DEV_REC *p_dev_rec)
  139. {
  140. if (p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED) {
  141. return (TRUE);
  142. }
  143. return (FALSE);
  144. }
  145. /*******************************************************************************
  146. **
  147. ** Function btm_dev_authorized
  148. **
  149. ** Description check device is authorized
  150. **
  151. ** Returns BOOLEAN TRUE or FALSE
  152. **
  153. *******************************************************************************/
  154. static BOOLEAN btm_dev_authorized (tBTM_SEC_DEV_REC *p_dev_rec)
  155. {
  156. if (p_dev_rec->sec_flags & BTM_SEC_AUTHORIZED) {
  157. return (TRUE);
  158. }
  159. return (FALSE);
  160. }
  161. /*******************************************************************************
  162. **
  163. ** Function btm_dev_16_digit_authenticated
  164. **
  165. ** Description check device is authenticated by using 16 digit pin or MITM
  166. **
  167. ** Returns BOOLEAN TRUE or FALSE
  168. **
  169. *******************************************************************************/
  170. static BOOLEAN btm_dev_16_digit_authenticated(tBTM_SEC_DEV_REC *p_dev_rec)
  171. {
  172. // BTM_SEC_16_DIGIT_PIN_AUTHED is set if MITM or 16 digit pin is used
  173. if (p_dev_rec->sec_flags & BTM_SEC_16_DIGIT_PIN_AUTHED) {
  174. return (TRUE);
  175. }
  176. return (FALSE);
  177. }
  178. #endif ///SMP_INCLUDED == TRUE
  179. /*******************************************************************************
  180. **
  181. ** Function btm_serv_trusted
  182. **
  183. ** Description check service is trusted
  184. **
  185. ** Returns BOOLEAN TRUE or FALSE
  186. **
  187. *******************************************************************************/
  188. #if (SMP_INCLUDED == TRUE)
  189. static BOOLEAN btm_serv_trusted(tBTM_SEC_DEV_REC *p_dev_rec, tBTM_SEC_SERV_REC *p_serv_rec)
  190. {
  191. if (BTM_SEC_IS_SERVICE_TRUSTED(p_dev_rec->trusted_mask, p_serv_rec->service_id)) {
  192. return (TRUE);
  193. }
  194. return (FALSE);
  195. }
  196. #endif ///SMP_INCLUDED == TRUE
  197. /*******************************************************************************
  198. **
  199. ** Function BTM_SecRegister
  200. **
  201. ** Description Application manager calls this function to register for
  202. ** security services. There can be one and only one application
  203. ** saving link keys. BTM allows only first registration.
  204. **
  205. ** Returns TRUE if registered OK, else FALSE
  206. **
  207. *******************************************************************************/
  208. BOOLEAN BTM_SecRegister(tBTM_APPL_INFO *p_cb_info)
  209. {
  210. #if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
  211. BT_OCTET16 temp_value = {0};
  212. #endif
  213. BTM_TRACE_EVENT("%s application registered\n", __func__);
  214. #if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
  215. BTM_TRACE_DEBUG("%s p_cb_info->p_le_callback == 0x%p\n", __func__, p_cb_info->p_le_callback);
  216. if (p_cb_info->p_le_callback) {
  217. BTM_TRACE_EVENT("%s SMP_Register( btm_proc_smp_cback )\n", __func__);
  218. SMP_Register(btm_proc_smp_cback);
  219. /* if no IR is loaded, need to regenerate all the keys */
  220. if (memcmp(btm_cb.devcb.id_keys.ir, &temp_value, sizeof(BT_OCTET16)) == 0) {
  221. btm_ble_reset_id();
  222. }
  223. #if (CONTROLLER_RPA_LIST_ENABLE == TRUE)
  224. else {
  225. btm_ble_add_default_entry_to_resolving_list();
  226. }
  227. #endif
  228. } else {
  229. BTM_TRACE_WARNING("%s p_cb_info->p_le_callback == NULL\n", __func__);
  230. }
  231. #endif
  232. btm_cb.api = *p_cb_info;
  233. #if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
  234. BTM_TRACE_DEBUG("%s btm_cb.api.p_le_callback = 0x%p\n", __func__, btm_cb.api.p_le_callback);
  235. #endif
  236. BTM_TRACE_EVENT("%s application registered\n", __func__);
  237. return (TRUE);
  238. }
  239. /*******************************************************************************
  240. **
  241. ** Function BTM_SecRegisterLinkKeyNotificationCallback
  242. **
  243. ** Description Application manager calls this function to register for
  244. ** link key notification. When there is nobody registered
  245. ** we should avoid changing link key
  246. **
  247. ** Returns TRUE if registered OK, else FALSE
  248. **
  249. *******************************************************************************/
  250. BOOLEAN BTM_SecRegisterLinkKeyNotificationCallback (tBTM_LINK_KEY_CALLBACK *p_callback)
  251. {
  252. btm_cb.api.p_link_key_callback = p_callback;
  253. return TRUE;
  254. }
  255. /*******************************************************************************
  256. **
  257. ** Function BTM_SecAddRmtNameNotifyCallback
  258. **
  259. ** Description Any profile can register to be notified when name of the
  260. ** remote device is resolved.
  261. **
  262. ** Returns TRUE if registered OK, else FALSE
  263. **
  264. *******************************************************************************/
  265. #if (SMP_INCLUDED == TRUE) || (CLASSIC_BT_INCLUDED == TRUE)
  266. BOOLEAN BTM_SecAddRmtNameNotifyCallback (tBTM_RMT_NAME_CALLBACK *p_callback)
  267. {
  268. int i;
  269. for (i = 0; i < BTM_SEC_MAX_RMT_NAME_CALLBACKS; i++) {
  270. if (btm_cb.p_rmt_name_callback[i] == NULL) {
  271. btm_cb.p_rmt_name_callback[i] = p_callback;
  272. return (TRUE);
  273. }
  274. }
  275. return (FALSE);
  276. }
  277. #endif ///SMP_INCLUDED == TRUE
  278. /*******************************************************************************
  279. **
  280. ** Function BTM_SecDeleteRmtNameNotifyCallback
  281. **
  282. ** Description Any profile can deregister notification when a new Link Key
  283. ** is generated per connection.
  284. **
  285. ** Returns TRUE if OK, else FALSE
  286. **
  287. *******************************************************************************/
  288. #if (SMP_INCLUDED == TRUE) || (CLASSIC_BT_INCLUDED == TRUE)
  289. BOOLEAN BTM_SecDeleteRmtNameNotifyCallback (tBTM_RMT_NAME_CALLBACK *p_callback)
  290. {
  291. int i;
  292. for (i = 0; i < BTM_SEC_MAX_RMT_NAME_CALLBACKS; i++) {
  293. if (btm_cb.p_rmt_name_callback[i] == p_callback) {
  294. btm_cb.p_rmt_name_callback[i] = NULL;
  295. return (TRUE);
  296. }
  297. }
  298. return (FALSE);
  299. }
  300. #endif ///SMP_INCLUDED == TRUE
  301. /*******************************************************************************
  302. **
  303. ** Function BTM_GetSecurityFlags
  304. **
  305. ** Description Get security flags for the device
  306. **
  307. ** Returns BOOLEAN TRUE or FALSE is device found
  308. **
  309. *******************************************************************************/
  310. BOOLEAN BTM_GetSecurityFlags (BD_ADDR bd_addr, UINT8 *p_sec_flags)
  311. {
  312. tBTM_SEC_DEV_REC *p_dev_rec;
  313. if ((p_dev_rec = btm_find_dev (bd_addr)) != NULL) {
  314. *p_sec_flags = (UINT8) p_dev_rec->sec_flags;
  315. return (TRUE);
  316. }
  317. BTM_TRACE_ERROR ("BTM_GetSecurityFlags false");
  318. return (FALSE);
  319. }
  320. /*******************************************************************************
  321. **
  322. ** Function BTM_GetSecurityFlagsByTransport
  323. **
  324. ** Description Get security flags for the device on a particular transport
  325. **
  326. ** Returns BOOLEAN TRUE or FALSE is device found
  327. **
  328. *******************************************************************************/
  329. BOOLEAN BTM_GetSecurityFlagsByTransport (BD_ADDR bd_addr, UINT8 *p_sec_flags,
  330. tBT_TRANSPORT transport)
  331. {
  332. tBTM_SEC_DEV_REC *p_dev_rec;
  333. if ((p_dev_rec = btm_find_dev (bd_addr)) != NULL) {
  334. if (transport == BT_TRANSPORT_BR_EDR) {
  335. *p_sec_flags = (UINT8) p_dev_rec->sec_flags;
  336. } else {
  337. *p_sec_flags = (UINT8) (p_dev_rec->sec_flags >> 8);
  338. }
  339. return (TRUE);
  340. }
  341. BTM_TRACE_ERROR ("BTM_GetSecurityFlags false\n");
  342. return (FALSE);
  343. }
  344. #if (CLASSIC_BT_INCLUDED == TRUE)
  345. /*******************************************************************************
  346. **
  347. ** Function BTM_SetPinType
  348. **
  349. ** Description Set PIN type for the device.
  350. **
  351. ** Returns void
  352. **
  353. *******************************************************************************/
  354. void BTM_SetPinType (UINT8 pin_type, PIN_CODE pin_code, UINT8 pin_code_len)
  355. {
  356. BTM_TRACE_API ("BTM_SetPinType: pin type %d [variable-0, fixed-1], code %s, length %d\n",
  357. pin_type, (char *) pin_code, pin_code_len);
  358. /* If device is not up security mode will be set as a part of startup */
  359. if ( (btm_cb.cfg.pin_type != pin_type)
  360. && controller_get_interface()->get_is_ready() ) {
  361. btsnd_hcic_write_pin_type (pin_type);
  362. }
  363. btm_cb.cfg.pin_type = pin_type;
  364. btm_cb.cfg.pin_code_len = pin_code_len;
  365. memcpy (btm_cb.cfg.pin_code, pin_code, pin_code_len);
  366. }
  367. #endif ///CLASSIC_BT_INCLUDED == TRUE
  368. /*******************************************************************************
  369. **
  370. ** Function BTM_SetPairableMode
  371. **
  372. ** Description Enable or disable pairing
  373. **
  374. ** Parameters allow_pairing - (TRUE or FALSE) whether or not the device
  375. ** allows pairing.
  376. ** connect_only_paired - (TRUE or FALSE) whether or not to
  377. ** only allow paired devices to connect.
  378. **
  379. ** Returns void
  380. **
  381. *******************************************************************************/
  382. void BTM_SetPairableMode (BOOLEAN allow_pairing, BOOLEAN connect_only_paired)
  383. {
  384. BTM_TRACE_API ("BTM_SetPairableMode() allow_pairing: %u connect_only_paired: %u\n", allow_pairing, connect_only_paired);
  385. btm_cb.pairing_disabled = !allow_pairing;
  386. btm_cb.connect_only_paired = connect_only_paired;
  387. }
  388. /*******************************************************************************
  389. **
  390. ** Function BTM_SetSecureConnectionsOnly
  391. **
  392. ** Description Enable or disable default treatment for Mode 4 Level 0 services
  393. **
  394. ** Parameter secure_connections_only_mode - (TRUE or FALSE) whether or not the device
  395. ** TRUE means that the device should treat Mode 4 Level 0 services as
  396. ** services of other levels. (Secure_connections_only_mode)
  397. ** FALSE means that the device should provide default treatment for
  398. ** Mode 4 Level 0 services.
  399. **
  400. ** Returns void
  401. **
  402. *******************************************************************************/
  403. void BTM_SetSecureConnectionsOnly (BOOLEAN secure_connections_only_mode)
  404. {
  405. BTM_TRACE_API("%s: Mode : %u\n", __FUNCTION__,
  406. secure_connections_only_mode);
  407. btm_cb.devcb.secure_connections_only = secure_connections_only_mode;
  408. btm_cb.security_mode = BTM_SEC_MODE_SC;
  409. }
  410. #define BTM_NO_AVAIL_SEC_SERVICES ((UINT16) 0xffff)
  411. /*******************************************************************************
  412. **
  413. ** Function BTM_SetSecurityLevel
  414. **
  415. ** Description Register service security level with Security Manager
  416. **
  417. ** Parameters: is_originator - TRUE if originating the connection, FALSE if not
  418. ** p_name - Name of the service relevant only if
  419. ** authorization will show this name to user. ignored
  420. ** if BTM_SEC_SERVICE_NAME_LEN is 0.
  421. ** service_id - service ID for the service passed to authorization callback
  422. ** sec_level - bit mask of the security features
  423. ** psm - L2CAP PSM
  424. ** mx_proto_id - protocol ID of multiplexing proto below
  425. ** mx_chan_id - channel ID of multiplexing proto below
  426. **
  427. ** Returns TRUE if registered OK, else FALSE
  428. **
  429. *******************************************************************************/
  430. BOOLEAN BTM_SetSecurityLevel (BOOLEAN is_originator, const char *p_name, UINT8 service_id,
  431. UINT16 sec_level, UINT16 psm, UINT32 mx_proto_id,
  432. UINT32 mx_chan_id)
  433. {
  434. #if (L2CAP_UCD_INCLUDED == TRUE)
  435. CONNECTION_TYPE conn_type;
  436. if (is_originator) {
  437. conn_type = CONN_ORIENT_ORIG;
  438. } else {
  439. conn_type = CONN_ORIENT_TERM;
  440. }
  441. return (btm_sec_set_security_level (conn_type, p_name, service_id,
  442. sec_level, psm, mx_proto_id, mx_chan_id));
  443. #else
  444. return (btm_sec_set_security_level (is_originator, p_name, service_id,
  445. sec_level, psm, mx_proto_id, mx_chan_id));
  446. #endif
  447. }
  448. /*******************************************************************************
  449. **
  450. ** Function btm_sec_set_security_level
  451. **
  452. ** Description Register service security level with Security Manager
  453. **
  454. ** Parameters: conn_type - TRUE if originating the connection, FALSE if not
  455. ** p_name - Name of the service relevant only if
  456. ** authorization will show this name to user. ignored
  457. ** if BTM_SEC_SERVICE_NAME_LEN is 0.
  458. ** service_id - service ID for the service passed to authorization callback
  459. ** sec_level - bit mask of the security features
  460. ** psm - L2CAP PSM
  461. ** mx_proto_id - protocol ID of multiplexing proto below
  462. ** mx_chan_id - channel ID of multiplexing proto below
  463. **
  464. ** Returns TRUE if registered OK, else FALSE
  465. **
  466. *******************************************************************************/
  467. static BOOLEAN btm_sec_set_security_level (CONNECTION_TYPE conn_type, const char *p_name, UINT8 service_id,
  468. UINT16 sec_level, UINT16 psm, UINT32 mx_proto_id,
  469. UINT32 mx_chan_id)
  470. {
  471. #if (SMP_INCLUDED == TRUE)
  472. tBTM_SEC_SERV_REC *p_srec;
  473. UINT16 index;
  474. UINT16 first_unused_record = BTM_NO_AVAIL_SEC_SERVICES;
  475. BOOLEAN record_allocated = FALSE;
  476. BOOLEAN is_originator;
  477. #if (L2CAP_UCD_INCLUDED == TRUE)
  478. BOOLEAN is_ucd;
  479. if (conn_type & CONNECTION_TYPE_ORIG_MASK) {
  480. is_originator = TRUE;
  481. } else {
  482. is_originator = FALSE;
  483. }
  484. if (conn_type & CONNECTION_TYPE_CONNLESS_MASK ) {
  485. is_ucd = TRUE;
  486. } else {
  487. is_ucd = FALSE;
  488. }
  489. #else
  490. is_originator = conn_type;
  491. #endif
  492. BTM_TRACE_API("%s : sec: 0x%x\n", __func__, sec_level);
  493. /* See if the record can be reused (same service name, psm, mx_proto_id,
  494. service_id, and mx_chan_id), or obtain the next unused record */
  495. p_srec = &btm_cb.sec_serv_rec[0];
  496. for (index = 0; index < BTM_SEC_MAX_SERVICE_RECORDS; index++, p_srec++) {
  497. /* Check if there is already a record for this service */
  498. if (p_srec->security_flags & BTM_SEC_IN_USE) {
  499. #if BTM_SEC_SERVICE_NAME_LEN > 0
  500. if (p_srec->psm == psm &&
  501. p_srec->mx_proto_id == mx_proto_id &&
  502. service_id == p_srec->service_id &&
  503. (!strncmp (p_name, (char *) p_srec->orig_service_name,
  504. BTM_SEC_SERVICE_NAME_LEN) ||
  505. !strncmp (p_name, (char *) p_srec->term_service_name,
  506. BTM_SEC_SERVICE_NAME_LEN)))
  507. #else
  508. if (p_srec->psm == psm &&
  509. p_srec->mx_proto_id == mx_proto_id &&
  510. service_id == p_srec->service_id)
  511. #endif
  512. {
  513. record_allocated = TRUE;
  514. break;
  515. }
  516. }
  517. /* Mark the first available service record */
  518. else if (!record_allocated) {
  519. memset (p_srec, 0, sizeof(tBTM_SEC_SERV_REC));
  520. record_allocated = TRUE;
  521. first_unused_record = index;
  522. }
  523. }
  524. if (!record_allocated) {
  525. BTM_TRACE_WARNING("BTM_SEC_REG: Out of Service Records (%d)\n", BTM_SEC_MAX_SERVICE_RECORDS);
  526. return (record_allocated);
  527. }
  528. /* Process the request if service record is valid */
  529. /* If a duplicate service wasn't found, use the first available */
  530. if (index >= BTM_SEC_MAX_SERVICE_RECORDS) {
  531. index = first_unused_record;
  532. p_srec = &btm_cb.sec_serv_rec[index];
  533. }
  534. p_srec->psm = psm;
  535. p_srec->service_id = service_id;
  536. p_srec->mx_proto_id = mx_proto_id;
  537. if (is_originator) {
  538. p_srec->orig_mx_chan_id = mx_chan_id;
  539. #if BTM_SEC_SERVICE_NAME_LEN > 0
  540. BCM_STRNCPY_S ((char *)p_srec->orig_service_name, p_name, BTM_SEC_SERVICE_NAME_LEN);
  541. p_srec->orig_service_name[BTM_SEC_SERVICE_NAME_LEN] = '\0';
  542. #endif
  543. /* clear out the old setting, just in case it exists */
  544. #if (L2CAP_UCD_INCLUDED == TRUE)
  545. if ( is_ucd ) {
  546. p_srec->ucd_security_flags &=
  547. ~(BTM_SEC_OUT_AUTHORIZE | BTM_SEC_OUT_ENCRYPT | BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_OUT_MITM |
  548. BTM_SEC_FORCE_MASTER | BTM_SEC_ATTEMPT_MASTER | BTM_SEC_FORCE_SLAVE | BTM_SEC_ATTEMPT_SLAVE);
  549. } else
  550. #endif
  551. {
  552. p_srec->security_flags &=
  553. ~(BTM_SEC_OUT_AUTHORIZE | BTM_SEC_OUT_ENCRYPT | BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_OUT_MITM |
  554. BTM_SEC_FORCE_MASTER | BTM_SEC_ATTEMPT_MASTER | BTM_SEC_FORCE_SLAVE | BTM_SEC_ATTEMPT_SLAVE);
  555. }
  556. /* Parameter validation. Originator should not set requirements for incoming connections */
  557. sec_level &= ~(BTM_SEC_IN_AUTHORIZE | BTM_SEC_IN_ENCRYPT | BTM_SEC_IN_AUTHENTICATE
  558. | BTM_SEC_IN_MITM | BTM_SEC_IN_MIN_16_DIGIT_PIN );
  559. if (btm_cb.security_mode == BTM_SEC_MODE_SP ||
  560. btm_cb.security_mode == BTM_SEC_MODE_SP_DEBUG ||
  561. btm_cb.security_mode == BTM_SEC_MODE_SC) {
  562. if (sec_level & BTM_SEC_OUT_AUTHENTICATE) {
  563. sec_level |= BTM_SEC_OUT_MITM;
  564. }
  565. }
  566. /* Make sure the authenticate bit is set, when encrypt bit is set */
  567. if (sec_level & BTM_SEC_OUT_ENCRYPT) {
  568. sec_level |= BTM_SEC_OUT_AUTHENTICATE;
  569. }
  570. /* outgoing connections usually set the security level right before
  571. * the connection is initiated.
  572. * set it to be the outgoing service */
  573. #if (L2CAP_UCD_INCLUDED == TRUE)
  574. if ( is_ucd == FALSE )
  575. #endif
  576. {
  577. btm_cb.p_out_serv = p_srec;
  578. }
  579. } else {
  580. p_srec->term_mx_chan_id = mx_chan_id;
  581. #if BTM_SEC_SERVICE_NAME_LEN > 0
  582. BCM_STRNCPY_S ((char *)p_srec->term_service_name, p_name, BTM_SEC_SERVICE_NAME_LEN);
  583. p_srec->term_service_name[BTM_SEC_SERVICE_NAME_LEN] = '\0';
  584. #endif
  585. /* clear out the old setting, just in case it exists */
  586. #if (L2CAP_UCD_INCLUDED == TRUE)
  587. if ( is_ucd ) {
  588. p_srec->ucd_security_flags &=
  589. ~(BTM_SEC_IN_AUTHORIZE | BTM_SEC_IN_ENCRYPT | BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_MITM |
  590. BTM_SEC_FORCE_MASTER | BTM_SEC_ATTEMPT_MASTER | BTM_SEC_FORCE_SLAVE | BTM_SEC_ATTEMPT_SLAVE
  591. | BTM_SEC_IN_MIN_16_DIGIT_PIN);
  592. } else
  593. #endif
  594. {
  595. p_srec->security_flags &=
  596. ~(BTM_SEC_IN_AUTHORIZE | BTM_SEC_IN_ENCRYPT | BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_MITM |
  597. BTM_SEC_FORCE_MASTER | BTM_SEC_ATTEMPT_MASTER | BTM_SEC_FORCE_SLAVE | BTM_SEC_ATTEMPT_SLAVE
  598. | BTM_SEC_IN_MIN_16_DIGIT_PIN);
  599. }
  600. /* Parameter validation. Acceptor should not set requirements for outgoing connections */
  601. sec_level &= ~(BTM_SEC_OUT_AUTHORIZE | BTM_SEC_OUT_ENCRYPT | BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_OUT_MITM);
  602. if (btm_cb.security_mode == BTM_SEC_MODE_SP ||
  603. btm_cb.security_mode == BTM_SEC_MODE_SP_DEBUG ||
  604. btm_cb.security_mode == BTM_SEC_MODE_SC) {
  605. if (sec_level & BTM_SEC_IN_AUTHENTICATE) {
  606. sec_level |= BTM_SEC_IN_MITM;
  607. }
  608. }
  609. /* Make sure the authenticate bit is set, when encrypt bit is set */
  610. if (sec_level & BTM_SEC_IN_ENCRYPT) {
  611. sec_level |= BTM_SEC_IN_AUTHENTICATE;
  612. }
  613. }
  614. #if (L2CAP_UCD_INCLUDED == TRUE)
  615. if ( is_ucd ) {
  616. p_srec->security_flags |= (UINT16)(BTM_SEC_IN_USE);
  617. p_srec->ucd_security_flags |= (UINT16)(sec_level | BTM_SEC_IN_USE);
  618. } else {
  619. p_srec->security_flags |= (UINT16)(sec_level | BTM_SEC_IN_USE);
  620. }
  621. BTM_TRACE_API("BTM_SEC_REG[%d]: id %d, conn_type 0x%x, psm 0x%04x, proto_id %d, chan_id %d\n",
  622. index, service_id, conn_type, psm, mx_proto_id, mx_chan_id);
  623. BTM_TRACE_API(" : security_flags: 0x%04x, ucd_security_flags: 0x%04x\n",
  624. p_srec->security_flags, p_srec->ucd_security_flags);
  625. #if BTM_SEC_SERVICE_NAME_LEN > 0
  626. BTM_TRACE_API(" : service name [%s] (up to %d chars saved)\n",
  627. p_name, BTM_SEC_SERVICE_NAME_LEN);
  628. #endif
  629. #else
  630. p_srec->security_flags |= (UINT16)(sec_level | BTM_SEC_IN_USE);
  631. BTM_TRACE_API("BTM_SEC_REG[%d]: id %d, is_orig %d, psm 0x%04x, proto_id %d, chan_id %d\n",
  632. index, service_id, is_originator, psm, mx_proto_id, mx_chan_id);
  633. #if BTM_SEC_SERVICE_NAME_LEN > 0
  634. BTM_TRACE_API(" : sec: 0x%x, service name [%s] (up to %d chars saved)\n",
  635. p_srec->security_flags, p_name, BTM_SEC_SERVICE_NAME_LEN);
  636. #endif
  637. #endif
  638. return (record_allocated);
  639. #else
  640. return FALSE;
  641. #endif ///SMP_INCLUDED == TRUE
  642. }
  643. /*******************************************************************************
  644. **
  645. ** Function BTM_SecClrService
  646. **
  647. ** Description Removes specified service record(s) from the security database.
  648. ** All service records with the specified name are removed.
  649. ** Typically used only by devices with limited RAM so that it can
  650. ** reuse an old security service record.
  651. **
  652. ** Note: Unpredictable results may occur if a service is cleared
  653. ** that is still in use by an application/profile.
  654. **
  655. ** Parameters Service ID - Id of the service to remove. ('0' removes all service
  656. ** records (except SDP).
  657. **
  658. ** Returns Number of records that were freed.
  659. **
  660. *******************************************************************************/
  661. #if (SDP_INCLUDED == TRUE)
  662. UINT8 BTM_SecClrService (UINT8 service_id)
  663. {
  664. tBTM_SEC_SERV_REC *p_srec = &btm_cb.sec_serv_rec[0];
  665. UINT8 num_freed = 0;
  666. int i;
  667. for (i = 0; i < BTM_SEC_MAX_SERVICE_RECORDS; i++, p_srec++) {
  668. /* Delete services with specified name (if in use and not SDP) */
  669. if ((p_srec->security_flags & BTM_SEC_IN_USE) && (p_srec->psm != BT_PSM_SDP) &&
  670. (!service_id || (service_id == p_srec->service_id))) {
  671. BTM_TRACE_API("BTM_SEC_CLR[%d]: id %d\n", i, service_id);
  672. p_srec->security_flags = 0;
  673. #if (L2CAP_UCD_INCLUDED == TRUE)
  674. p_srec->ucd_security_flags = 0;
  675. #endif
  676. num_freed++;
  677. }
  678. }
  679. return (num_freed);
  680. }
  681. #endif ///SDP_INCLUDED == TRUE
  682. /*******************************************************************************
  683. **
  684. ** Function btm_sec_clr_service_by_psm
  685. **
  686. ** Description Removes specified service record from the security database.
  687. ** All service records with the specified psm are removed.
  688. ** Typically used by L2CAP to free up the service record used
  689. ** by dynamic PSM clients when the channel is closed.
  690. ** The given psm must be a virtual psm.
  691. **
  692. ** Parameters Service ID - Id of the service to remove. ('0' removes all service
  693. ** records (except SDP).
  694. **
  695. ** Returns Number of records that were freed.
  696. **
  697. *******************************************************************************/
  698. #if (SDP_INCLUDED== TRUE)
  699. UINT8 btm_sec_clr_service_by_psm (UINT16 psm)
  700. {
  701. tBTM_SEC_SERV_REC *p_srec = &btm_cb.sec_serv_rec[0];
  702. UINT8 num_freed = 0;
  703. int i;
  704. for (i = 0; i < BTM_SEC_MAX_SERVICE_RECORDS; i++, p_srec++) {
  705. /* Delete services with specified name (if in use and not SDP) */
  706. if ((p_srec->security_flags & BTM_SEC_IN_USE) && (p_srec->psm == psm) ) {
  707. BTM_TRACE_API("BTM_SEC_CLR[%d]: id %d\n", i, p_srec->service_id);
  708. p_srec->security_flags = 0;
  709. num_freed++;
  710. }
  711. }
  712. BTM_TRACE_API("btm_sec_clr_service_by_psm psm:0x%x num_freed:%d\n", psm, num_freed);
  713. return (num_freed);
  714. }
  715. #endif ///SDP_INCLUDED== TRUE
  716. /*******************************************************************************
  717. **
  718. ** Function btm_sec_clr_temp_auth_service
  719. **
  720. ** Description Removes specified device record's temporary authorization
  721. ** flag from the security database.
  722. **
  723. ** Parameters Device address to be cleared
  724. **
  725. ** Returns void.
  726. **
  727. *******************************************************************************/
  728. void btm_sec_clr_temp_auth_service (BD_ADDR bda)
  729. {
  730. tBTM_SEC_DEV_REC *p_dev_rec;
  731. if ((p_dev_rec = btm_find_dev (bda)) == NULL) {
  732. BTM_TRACE_WARNING ("btm_sec_clr_temp_auth_service() - no dev CB\n");
  733. return;
  734. }
  735. /* Reset the temporary authorized flag so that next time (untrusted) service is accessed autorization will take place */
  736. if (p_dev_rec->last_author_service_id != BTM_SEC_NO_LAST_SERVICE_ID && p_dev_rec->p_cur_service) {
  737. BTM_TRACE_DEBUG ("btm_sec_clr_auth_service_by_psm [clearing device: %02x:%02x:%02x:%02x:%02x:%02x]\n",
  738. bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
  739. p_dev_rec->last_author_service_id = BTM_SEC_NO_LAST_SERVICE_ID;
  740. }
  741. }
  742. /*******************************************************************************
  743. **
  744. ** Function BTM_PINCodeReply
  745. **
  746. ** Description This function is called after Security Manager submitted
  747. ** PIN code request to the UI.
  748. **
  749. ** Parameters: bd_addr - Address of the device for which PIN was requested
  750. ** res - result of the operation BTM_SUCCESS if success
  751. ** pin_len - length in bytes of the PIN Code
  752. ** p_pin - pointer to array with the PIN Code
  753. ** trusted_mask - bitwise OR of trusted services (array of UINT32)
  754. **
  755. *******************************************************************************/
  756. #if (SMP_INCLUDED == TRUE)
  757. #if (CLASSIC_BT_INCLUDED == TRUE)
  758. void BTM_PINCodeReply (BD_ADDR bd_addr, UINT8 res, UINT8 pin_len, UINT8 *p_pin, UINT32 trusted_mask[])
  759. {
  760. tBTM_SEC_DEV_REC *p_dev_rec;
  761. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  762. BTM_TRACE_API ("BTM_PINCodeReply(): PairState: %s PairFlags: 0x%02x PinLen:%d Result:%d\n",
  763. btm_pair_state_descr(btm_cb.pairing_state), btm_cb.pairing_flags, pin_len, res);
  764. #endif ///BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE
  765. /* If timeout already expired or has been canceled, ignore the reply */
  766. if (btm_cb.pairing_state != BTM_PAIR_STATE_WAIT_LOCAL_PIN) {
  767. BTM_TRACE_WARNING ("BTM_PINCodeReply() - Wrong State: %d\n", btm_cb.pairing_state);
  768. return;
  769. }
  770. if (memcmp (bd_addr, btm_cb.pairing_bda, BD_ADDR_LEN) != 0) {
  771. BTM_TRACE_ERROR ("BTM_PINCodeReply() - Wrong BD Addr\n");
  772. return;
  773. }
  774. if ((p_dev_rec = btm_find_dev (bd_addr)) == NULL) {
  775. BTM_TRACE_ERROR ("BTM_PINCodeReply() - no dev CB\n");
  776. return;
  777. }
  778. if ( (pin_len > PIN_CODE_LEN) || (pin_len == 0) || (p_pin == NULL) ) {
  779. res = BTM_ILLEGAL_VALUE;
  780. }
  781. if (res != BTM_SUCCESS) {
  782. /* if peer started dd OR we started dd and pre-fetch pin was not used send negative reply */
  783. if ((btm_cb.pairing_flags & BTM_PAIR_FLAGS_PEER_STARTED_DD) ||
  784. ((btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD) &&
  785. (btm_cb.pairing_flags & BTM_PAIR_FLAGS_DISC_WHEN_DONE)) ) {
  786. /* use BTM_PAIR_STATE_WAIT_AUTH_COMPLETE to report authentication failed event */
  787. btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_AUTH_COMPLETE);
  788. btm_cb.acl_disc_reason = HCI_ERR_HOST_REJECT_SECURITY;
  789. btsnd_hcic_pin_code_neg_reply (bd_addr);
  790. } else {
  791. p_dev_rec->security_required = BTM_SEC_NONE;
  792. btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
  793. }
  794. return;
  795. }
  796. if (trusted_mask) {
  797. BTM_SEC_COPY_TRUSTED_DEVICE(trusted_mask, p_dev_rec->trusted_mask);
  798. }
  799. p_dev_rec->sec_flags |= BTM_SEC_LINK_KEY_AUTHED;
  800. if (pin_len >= 16) {
  801. p_dev_rec->sec_flags |= BTM_SEC_16_DIGIT_PIN_AUTHED;
  802. }
  803. if ( (btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD)
  804. && (p_dev_rec->hci_handle == BTM_SEC_INVALID_HANDLE)
  805. && (btm_cb.security_mode_changed == FALSE) ) {
  806. /* This is start of the dedicated bonding if local device is 2.0 */
  807. btm_cb.pin_code_len = pin_len;
  808. p_dev_rec->pin_code_length = pin_len;
  809. memcpy (btm_cb.pin_code, p_pin, pin_len);
  810. btm_cb.security_mode_changed = TRUE;
  811. #ifdef APPL_AUTH_WRITE_EXCEPTION
  812. if (!(APPL_AUTH_WRITE_EXCEPTION)(p_dev_rec->bd_addr))
  813. #endif
  814. {
  815. btsnd_hcic_write_auth_enable (TRUE);
  816. }
  817. btm_cb.acl_disc_reason = 0xff ;
  818. /* if we rejected incoming connection request, we have to wait HCI_Connection_Complete event */
  819. /* before originating */
  820. if (btm_cb.pairing_flags & BTM_PAIR_FLAGS_REJECTED_CONNECT) {
  821. BTM_TRACE_WARNING ("BTM_PINCodeReply(): waiting HCI_Connection_Complete after rejected incoming connection\n");
  822. /* we change state little bit early so btm_sec_connected() will originate connection */
  823. /* when existing ACL link is down completely */
  824. btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_PIN_REQ);
  825. }
  826. /* if we already accepted incoming connection from pairing device */
  827. else if (p_dev_rec->sm4 & BTM_SM4_CONN_PEND) {
  828. BTM_TRACE_WARNING ("BTM_PINCodeReply(): link is connecting so wait pin code request from peer\n");
  829. btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_PIN_REQ);
  830. } else if (btm_sec_dd_create_conn(p_dev_rec) != BTM_CMD_STARTED) {
  831. btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
  832. p_dev_rec->sec_flags &= ~BTM_SEC_LINK_KEY_AUTHED;
  833. if (btm_cb.api.p_auth_complete_callback) {
  834. (*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr, p_dev_rec->dev_class,
  835. p_dev_rec->sec_bd_name, HCI_ERR_AUTH_FAILURE);
  836. }
  837. }
  838. return;
  839. }
  840. btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_AUTH_COMPLETE);
  841. btm_cb.acl_disc_reason = HCI_SUCCESS;
  842. #ifdef PORCHE_PAIRING_CONFLICT
  843. BTM_TRACE_EVENT("BTM_PINCodeReply(): Saving pin_len: %d btm_cb.pin_code_len: %d\n", pin_len, btm_cb.pin_code_len);
  844. /* if this was not pre-fetched, save the PIN */
  845. if (btm_cb.pin_code_len == 0) {
  846. memcpy (btm_cb.pin_code, p_pin, pin_len);
  847. }
  848. btm_cb.pin_code_len_saved = pin_len;
  849. #endif
  850. btsnd_hcic_pin_code_req_reply (bd_addr, pin_len, p_pin);
  851. }
  852. #endif ///CLASSIC_BT_INCLUDED == TRUE
  853. #endif ///SMP_INCLUDED == TRUE
  854. /*******************************************************************************
  855. **
  856. ** Function btm_sec_bond_by_transport
  857. **
  858. ** Description this is the bond function that will start either SSP or SMP.
  859. **
  860. ** Parameters: bd_addr - Address of the device to bond
  861. ** pin_len - length in bytes of the PIN Code
  862. ** p_pin - pointer to array with the PIN Code
  863. ** trusted_mask - bitwise OR of trusted services (array of UINT32)
  864. **
  865. ** Note: After 2.1 parameters are not used and preserved here not to change API
  866. *******************************************************************************/
  867. #if (SMP_INCLUDED == TRUE)
  868. tBTM_STATUS btm_sec_bond_by_transport (BD_ADDR bd_addr, tBT_TRANSPORT transport,
  869. UINT8 pin_len, UINT8 *p_pin, UINT32 trusted_mask[])
  870. {
  871. tBTM_SEC_DEV_REC *p_dev_rec;
  872. tBTM_STATUS status;
  873. #if (!CONFIG_BT_STACK_NO_LOG)
  874. UINT8 *p_features;
  875. #endif
  876. UINT8 ii;
  877. tACL_CONN *p = btm_bda_to_acl(bd_addr, transport);
  878. BTM_TRACE_API ("btm_sec_bond_by_transport BDA: %02x:%02x:%02x:%02x:%02x:%02x\n",
  879. bd_addr[0], bd_addr[1], bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]);
  880. BTM_TRACE_DEBUG("btm_sec_bond_by_transport: Transport used %d\n" , transport);
  881. /* Other security process is in progress */
  882. if (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE) {
  883. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  884. BTM_TRACE_ERROR ("BTM_SecBond: already busy in state: %s\n", btm_pair_state_descr(btm_cb.pairing_state));
  885. #endif ///BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE
  886. return (BTM_WRONG_MODE);
  887. }
  888. if ((p_dev_rec = btm_find_or_alloc_dev (bd_addr)) == NULL) {
  889. return (BTM_NO_RESOURCES);
  890. }
  891. BTM_TRACE_DEBUG ("before update sec_flags=0x%x\n", p_dev_rec->sec_flags);
  892. /* Finished if connection is active and already paired */
  893. if ( ((p_dev_rec->hci_handle != BTM_SEC_INVALID_HANDLE) && transport == BT_TRANSPORT_BR_EDR
  894. && (p_dev_rec->sec_flags & BTM_SEC_AUTHENTICATED))
  895. #if (BLE_INCLUDED == TRUE)
  896. || ((p_dev_rec->ble_hci_handle != BTM_SEC_INVALID_HANDLE) && transport == BT_TRANSPORT_LE
  897. && (p_dev_rec->sec_flags & BTM_SEC_LE_AUTHENTICATED))
  898. #endif
  899. ) {
  900. BTM_TRACE_WARNING("BTM_SecBond -> Already Paired\n");
  901. return (BTM_SUCCESS);
  902. }
  903. /* Tell controller to get rid of the link key if it has one stored */
  904. if ((BTM_DeleteStoredLinkKey (bd_addr, NULL)) != BTM_SUCCESS) {
  905. return (BTM_NO_RESOURCES);
  906. }
  907. #if (CLASSIC_BT_INCLUDED == TRUE)
  908. /* Save the PIN code if we got a valid one */
  909. if (p_pin && (pin_len <= PIN_CODE_LEN) && (pin_len != 0)) {
  910. btm_cb.pin_code_len = pin_len;
  911. p_dev_rec->pin_code_length = pin_len;
  912. memcpy (btm_cb.pin_code, p_pin, PIN_CODE_LEN);
  913. }
  914. #endif ///CLASSIC_BT_INCLUDED == TRUE
  915. memcpy (btm_cb.pairing_bda, bd_addr, BD_ADDR_LEN);
  916. btm_cb.pairing_flags = BTM_PAIR_FLAGS_WE_STARTED_DD;
  917. p_dev_rec->security_required = BTM_SEC_OUT_AUTHENTICATE;
  918. p_dev_rec->is_originator = TRUE;
  919. if (trusted_mask) {
  920. BTM_SEC_COPY_TRUSTED_DEVICE(trusted_mask, p_dev_rec->trusted_mask);
  921. }
  922. #if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
  923. if (transport == BT_TRANSPORT_LE) {
  924. btm_ble_init_pseudo_addr (p_dev_rec, bd_addr);
  925. p_dev_rec->sec_flags &= ~ BTM_SEC_LE_MASK;
  926. if (SMP_Pair(bd_addr) == SMP_STARTED) {
  927. btm_cb.pairing_flags |= BTM_PAIR_FLAGS_LE_ACTIVE;
  928. p_dev_rec->sec_state = BTM_SEC_STATE_AUTHENTICATING;
  929. btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_AUTH_COMPLETE);
  930. return BTM_CMD_STARTED;
  931. }
  932. btm_cb.pairing_flags = 0;
  933. return (BTM_NO_RESOURCES);
  934. }
  935. #endif
  936. p_dev_rec->sec_flags &= ~(BTM_SEC_LINK_KEY_KNOWN | BTM_SEC_AUTHENTICATED | BTM_SEC_ENCRYPTED
  937. | BTM_SEC_ROLE_SWITCHED | BTM_SEC_LINK_KEY_AUTHED);
  938. BTM_TRACE_DEBUG ("after update sec_flags=0x%x\n", p_dev_rec->sec_flags);
  939. #if (CLASSIC_BT_INCLUDED == TRUE)
  940. if (!controller_get_interface()->supports_simple_pairing()) {
  941. /* The special case when we authenticate keyboard. Set pin type to fixed */
  942. /* It would be probably better to do it from the application, but it is */
  943. /* complicated */
  944. if (((p_dev_rec->dev_class[1] & BTM_COD_MAJOR_CLASS_MASK) == BTM_COD_MAJOR_PERIPHERAL)
  945. && (p_dev_rec->dev_class[2] & BTM_COD_MINOR_KEYBOARD)
  946. && (btm_cb.cfg.pin_type != HCI_PIN_TYPE_FIXED)) {
  947. btm_cb.pin_type_changed = TRUE;
  948. btsnd_hcic_write_pin_type (HCI_PIN_TYPE_FIXED);
  949. }
  950. }
  951. #endif ///CLASSIC_BT_INCLUDED == TRUE
  952. for (ii = 0; ii <= HCI_EXT_FEATURES_PAGE_MAX; ii++) {
  953. #if (!CONFIG_BT_STACK_NO_LOG)
  954. p_features = p_dev_rec->features[ii];
  955. #endif
  956. BTM_TRACE_EVENT(" remote_features page[%1d] = %02x-%02x-%02x-%02x\n",
  957. ii, p_features[0], p_features[1], p_features[2], p_features[3]);
  958. BTM_TRACE_EVENT(" %02x-%02x-%02x-%02x\n",
  959. p_features[4], p_features[5], p_features[6], p_features[7]);
  960. }
  961. BTM_TRACE_EVENT ("BTM_SecBond: Remote sm4: 0x%x HCI Handle: 0x%04x\n", p_dev_rec->sm4, p_dev_rec->hci_handle);
  962. #if BTM_SEC_FORCE_RNR_FOR_DBOND == TRUE
  963. p_dev_rec->sec_flags &= ~BTM_SEC_NAME_KNOWN;
  964. #endif
  965. /* If connection already exists... */
  966. if (p && p->hci_handle != BTM_SEC_INVALID_HANDLE) {
  967. if (!btm_sec_start_authentication (p_dev_rec)) {
  968. return (BTM_NO_RESOURCES);
  969. }
  970. btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_PIN_REQ);
  971. /* Mark lcb as bonding */
  972. l2cu_update_lcb_4_bonding (bd_addr, TRUE);
  973. return (BTM_CMD_STARTED);
  974. }
  975. BTM_TRACE_DEBUG ("sec mode: %d sm4:x%x\n", btm_cb.security_mode, p_dev_rec->sm4);
  976. if (!controller_get_interface()->supports_simple_pairing()
  977. || (p_dev_rec->sm4 == BTM_SM4_KNOWN)) {
  978. if ( btm_sec_check_prefetch_pin (p_dev_rec) ) {
  979. return (BTM_CMD_STARTED);
  980. }
  981. }
  982. if ((btm_cb.security_mode == BTM_SEC_MODE_SP ||
  983. btm_cb.security_mode == BTM_SEC_MODE_SP_DEBUG ||
  984. btm_cb.security_mode == BTM_SEC_MODE_SC) &&
  985. BTM_SEC_IS_SM4_UNKNOWN(p_dev_rec->sm4)) {
  986. /* local is 2.1 and peer is unknown */
  987. if ((p_dev_rec->sm4 & BTM_SM4_CONN_PEND) == 0) {
  988. /* we are not accepting connection request from peer
  989. * -> RNR (to learn if peer is 2.1)
  990. * RNR when no ACL causes HCI_RMT_HOST_SUP_FEAT_NOTIFY_EVT */
  991. btm_sec_change_pairing_state (BTM_PAIR_STATE_GET_REM_NAME);
  992. BTM_ReadRemoteDeviceName(bd_addr, NULL, BT_TRANSPORT_BR_EDR);
  993. } else {
  994. /* We are accepting connection request from peer */
  995. btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_PIN_REQ);
  996. }
  997. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  998. BTM_TRACE_DEBUG ("State:%s sm4: 0x%x sec_state:%d\n",
  999. btm_pair_state_descr (btm_cb.pairing_state), p_dev_rec->sm4, p_dev_rec->sec_state);
  1000. #endif ///BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE
  1001. return BTM_CMD_STARTED;
  1002. }
  1003. /* both local and peer are 2.1 */
  1004. status = btm_sec_dd_create_conn(p_dev_rec);
  1005. if (status != BTM_CMD_STARTED) {
  1006. btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
  1007. }
  1008. return status;
  1009. }
  1010. /*******************************************************************************
  1011. **
  1012. ** Function BTM_SecBondByTransport
  1013. **
  1014. ** Description This function is called to perform bonding with peer device.
  1015. ** If the connection is already up, but not secure, pairing
  1016. ** is attempted. If already paired BTM_SUCCESS is returned.
  1017. **
  1018. ** Parameters: bd_addr - Address of the device to bond
  1019. ** transport - doing SSP over BR/EDR or SMP over LE
  1020. ** pin_len - length in bytes of the PIN Code
  1021. ** p_pin - pointer to array with the PIN Code
  1022. ** trusted_mask - bitwise OR of trusted services (array of UINT32)
  1023. **
  1024. ** Note: After 2.1 parameters are not used and preserved here not to change API
  1025. *******************************************************************************/
  1026. tBTM_STATUS BTM_SecBondByTransport (BD_ADDR bd_addr, tBT_TRANSPORT transport,
  1027. UINT8 pin_len, UINT8 *p_pin, UINT32 trusted_mask[])
  1028. {
  1029. #if (BLE_INCLUDED == TRUE)
  1030. tBT_DEVICE_TYPE dev_type;
  1031. tBLE_ADDR_TYPE addr_type = 0;
  1032. BTM_ReadDevInfo(bd_addr, &dev_type, &addr_type);
  1033. /* LE device, do SMP pairing */
  1034. if ((transport == BT_TRANSPORT_LE && (dev_type & BT_DEVICE_TYPE_BLE) == 0) ||
  1035. (transport == BT_TRANSPORT_BR_EDR && (dev_type & BT_DEVICE_TYPE_BREDR) == 0)) {
  1036. return BTM_ILLEGAL_ACTION;
  1037. }
  1038. #endif ///BLE_INCLUDED == TRUE
  1039. return btm_sec_bond_by_transport(bd_addr, transport, pin_len, p_pin, trusted_mask);
  1040. }
  1041. #endif ///SMP_INCLUDED == TRUE
  1042. /*******************************************************************************
  1043. **
  1044. ** Function BTM_SecBond
  1045. **
  1046. ** Description This function is called to perform bonding with peer device.
  1047. ** If the connection is already up, but not secure, pairing
  1048. ** is attempted. If already paired BTM_SUCCESS is returned.
  1049. **
  1050. ** Parameters: bd_addr - Address of the device to bond
  1051. ** pin_len - length in bytes of the PIN Code
  1052. ** p_pin - pointer to array with the PIN Code
  1053. ** trusted_mask - bitwise OR of trusted services (array of UINT32)
  1054. **
  1055. ** Note: After 2.1 parameters are not used and preserved here not to change API
  1056. *******************************************************************************/
  1057. #if (SMP_INCLUDED == TRUE)
  1058. tBTM_STATUS BTM_SecBond (BD_ADDR bd_addr, UINT8 pin_len, UINT8 *p_pin, UINT32 trusted_mask[])
  1059. {
  1060. tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
  1061. #if (BLE_INCLUDED == TRUE)
  1062. if (BTM_UseLeLink(bd_addr)) {
  1063. transport = BT_TRANSPORT_LE;
  1064. }
  1065. #endif ///BLE_INCLUDED == TRUE
  1066. return btm_sec_bond_by_transport(bd_addr, transport, pin_len, p_pin, trusted_mask);
  1067. }
  1068. /*******************************************************************************
  1069. **
  1070. ** Function BTM_SecBondCancel
  1071. **
  1072. ** Description This function is called to cancel ongoing bonding process
  1073. ** with peer device.
  1074. **
  1075. ** Parameters: bd_addr - Address of the peer device
  1076. ** transport - FALSE for BR/EDR link; TRUE for LE link
  1077. **
  1078. *******************************************************************************/
  1079. tBTM_STATUS BTM_SecBondCancel (BD_ADDR bd_addr)
  1080. {
  1081. tBTM_SEC_DEV_REC *p_dev_rec;
  1082. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  1083. BTM_TRACE_API ("BTM_SecBondCancel() State: %s flags:0x%x\n",
  1084. btm_pair_state_descr (btm_cb.pairing_state), btm_cb.pairing_flags);
  1085. #endif ///BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE
  1086. if (((p_dev_rec = btm_find_dev (bd_addr)) == NULL)
  1087. || (memcmp (btm_cb.pairing_bda, bd_addr, BD_ADDR_LEN) != 0) ) {
  1088. return BTM_UNKNOWN_ADDR;
  1089. }
  1090. #if SMP_INCLUDED == TRUE
  1091. if (btm_cb.pairing_flags & BTM_PAIR_FLAGS_LE_ACTIVE) {
  1092. if (p_dev_rec->sec_state == BTM_SEC_STATE_AUTHENTICATING) {
  1093. BTM_TRACE_DEBUG ("Cancel LE pairing\n");
  1094. if (SMP_PairCancel(bd_addr)) {
  1095. return BTM_CMD_STARTED;
  1096. }
  1097. }
  1098. return BTM_WRONG_MODE;
  1099. }
  1100. #endif
  1101. BTM_TRACE_DEBUG ("hci_handle:0x%x sec_state:%d\n", p_dev_rec->hci_handle, p_dev_rec->sec_state );
  1102. if (BTM_PAIR_STATE_WAIT_LOCAL_PIN == btm_cb.pairing_state &&
  1103. BTM_PAIR_FLAGS_WE_STARTED_DD & btm_cb.pairing_flags) {
  1104. /* pre-fetching pin for dedicated bonding */
  1105. btm_sec_bond_cancel_complete();
  1106. return BTM_SUCCESS;
  1107. }
  1108. /* If this BDA is in a bonding procedure */
  1109. if ( (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE)
  1110. && (btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD)) {
  1111. /* If the HCI link is up */
  1112. if (p_dev_rec->hci_handle != BTM_SEC_INVALID_HANDLE) {
  1113. /* If some other thread disconnecting, we do not send second command */
  1114. if ((p_dev_rec->sec_state == BTM_SEC_STATE_DISCONNECTING) ||
  1115. (p_dev_rec->sec_state == BTM_SEC_STATE_DISCONNECTING_BOTH)) {
  1116. return (BTM_CMD_STARTED);
  1117. }
  1118. /* If the HCI link was set up by Bonding process */
  1119. if (btm_cb.pairing_flags & BTM_PAIR_FLAGS_DISC_WHEN_DONE) {
  1120. return btm_sec_send_hci_disconnect(p_dev_rec, HCI_ERR_PEER_USER, p_dev_rec->hci_handle);
  1121. } else {
  1122. l2cu_update_lcb_4_bonding(bd_addr, FALSE);
  1123. }
  1124. return BTM_NOT_AUTHORIZED;
  1125. } else { /*HCI link is not up */
  1126. /* If the HCI link creation was started by Bonding process */
  1127. if (btm_cb.pairing_flags & BTM_PAIR_FLAGS_DISC_WHEN_DONE) {
  1128. if (btsnd_hcic_create_conn_cancel(bd_addr)) {
  1129. return BTM_CMD_STARTED;
  1130. }
  1131. return BTM_NO_RESOURCES;
  1132. }
  1133. if (btm_cb.pairing_state == BTM_PAIR_STATE_GET_REM_NAME) {
  1134. BTM_CancelRemoteDeviceName();
  1135. btm_cb.pairing_flags |= BTM_PAIR_FLAGS_WE_CANCEL_DD;
  1136. return BTM_CMD_STARTED;
  1137. }
  1138. return BTM_NOT_AUTHORIZED;
  1139. }
  1140. }
  1141. return BTM_WRONG_MODE;
  1142. }
  1143. /*******************************************************************************
  1144. **
  1145. ** Function BTM_SecGetDeviceLinkKey
  1146. **
  1147. ** Description This function is called to obtain link key for the device
  1148. ** it returns BTM_SUCCESS if link key is available, or
  1149. ** BTM_UNKNOWN_ADDR if Security Manager does not know about
  1150. ** the device or device record does not contain link key info
  1151. **
  1152. ** Parameters: bd_addr - Address of the device
  1153. ** link_key - Link Key is copied into this array
  1154. **
  1155. *******************************************************************************/
  1156. tBTM_STATUS BTM_SecGetDeviceLinkKey (BD_ADDR bd_addr, LINK_KEY link_key)
  1157. {
  1158. tBTM_SEC_DEV_REC *p_dev_rec;
  1159. if (((p_dev_rec = btm_find_dev (bd_addr)) != NULL)
  1160. && (p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_KNOWN)) {
  1161. memcpy (link_key, p_dev_rec->link_key, LINK_KEY_LEN);
  1162. return (BTM_SUCCESS);
  1163. }
  1164. return (BTM_UNKNOWN_ADDR);
  1165. }
  1166. #endif ///SMP_INCLUDED == TRUE
  1167. /*******************************************************************************
  1168. **
  1169. ** Function BTM_SecGetDeviceLinkKeyType
  1170. **
  1171. ** Description This function is called to obtain link key type for the
  1172. ** device.
  1173. ** it returns BTM_SUCCESS if link key is available, or
  1174. ** BTM_UNKNOWN_ADDR if Security Manager does not know about
  1175. ** the device or device record does not contain link key info
  1176. **
  1177. ** Returns BTM_LKEY_TYPE_IGNORE if link key is unknown, link type
  1178. ** otherwise.
  1179. **
  1180. *******************************************************************************/
  1181. tBTM_LINK_KEY_TYPE BTM_SecGetDeviceLinkKeyType (BD_ADDR bd_addr)
  1182. {
  1183. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
  1184. if ((p_dev_rec != NULL) && (p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_KNOWN)) {
  1185. return p_dev_rec->link_key_type;
  1186. }
  1187. return BTM_LKEY_TYPE_IGNORE;
  1188. }
  1189. /*******************************************************************************
  1190. **
  1191. ** Function BTM_SetEncryption
  1192. **
  1193. ** Description This function is called to ensure that connection is
  1194. ** encrypted. Should be called only on an open connection.
  1195. ** Typically only needed for connections that first want to
  1196. ** bring up unencrypted links, then later encrypt them.
  1197. **
  1198. ** Parameters: bd_addr - Address of the peer device
  1199. ** p_callback - Pointer to callback function called if
  1200. ** this function returns PENDING after required
  1201. ** procedures are completed. Can be set to NULL
  1202. ** if status is not desired.
  1203. ** p_ref_data - pointer to any data the caller wishes to receive
  1204. ** in the callback function upon completion.
  1205. * can be set to NULL if not used.
  1206. ** transport - TRUE to encryption the link over LE transport
  1207. ** or FALSE for BR/EDR transport
  1208. **
  1209. ** Returns BTM_SUCCESS - already encrypted
  1210. ** BTM_PENDING - command will be returned in the callback
  1211. ** BTM_WRONG_MODE- connection not up.
  1212. ** BTM_BUSY - security procedures are currently active
  1213. ** BTM_MODE_UNSUPPORTED - if security manager not linked in.
  1214. **
  1215. *******************************************************************************/
  1216. #if (SMP_INCLUDED == TRUE)
  1217. tBTM_STATUS BTM_SetEncryption (BD_ADDR bd_addr, tBT_TRANSPORT transport, tBTM_SEC_CBACK *p_callback,
  1218. void *p_ref_data)
  1219. {
  1220. tBTM_STATUS rc = 0;
  1221. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
  1222. if (!p_dev_rec ||
  1223. (transport == BT_TRANSPORT_BR_EDR && p_dev_rec->hci_handle == BTM_SEC_INVALID_HANDLE)
  1224. #if BLE_INCLUDED == TRUE
  1225. || (transport == BT_TRANSPORT_LE && p_dev_rec->ble_hci_handle == BTM_SEC_INVALID_HANDLE)
  1226. #endif
  1227. ) {
  1228. /* Connection should be up and runnning */
  1229. BTM_TRACE_WARNING ("Security Manager: BTM_SetEncryption not connected\n");
  1230. if (p_callback) {
  1231. (*p_callback) (bd_addr, transport, p_ref_data, BTM_WRONG_MODE);
  1232. }
  1233. return (BTM_WRONG_MODE);
  1234. }
  1235. if ((transport == BT_TRANSPORT_BR_EDR &&
  1236. (p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED))
  1237. #if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
  1238. || (transport == BT_TRANSPORT_LE &&
  1239. (p_dev_rec->sec_flags & BTM_SEC_LE_ENCRYPTED))
  1240. #endif
  1241. ) {
  1242. BTM_TRACE_EVENT ("Security Manager: BTM_SetEncryption already encrypted\n");
  1243. if (p_callback) {
  1244. (*p_callback) (bd_addr, transport, p_ref_data, BTM_SUCCESS);
  1245. }
  1246. return (BTM_SUCCESS);
  1247. }
  1248. p_dev_rec->enc_init_by_we = TRUE;
  1249. /* enqueue security request if security is active */
  1250. if (p_dev_rec->p_callback || (p_dev_rec->sec_state != BTM_SEC_STATE_IDLE)) {
  1251. BTM_TRACE_WARNING ("Security Manager: BTM_SetEncryption busy, enqueue request\n");
  1252. if (btm_sec_queue_encrypt_request(bd_addr, transport, p_callback, p_ref_data)) {
  1253. return BTM_CMD_STARTED;
  1254. } else {
  1255. if (p_callback) {
  1256. (*p_callback) (bd_addr, transport, p_ref_data, BTM_NO_RESOURCES);
  1257. }
  1258. return BTM_NO_RESOURCES;
  1259. }
  1260. }
  1261. p_dev_rec->p_callback = p_callback;
  1262. p_dev_rec->p_ref_data = p_ref_data;
  1263. p_dev_rec->security_required |= (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_ENCRYPT);
  1264. p_dev_rec->is_originator = FALSE;
  1265. BTM_TRACE_API ("Security Manager: BTM_SetEncryption Handle:%d State:%d Flags:0x%x Required:0x%x\n",
  1266. p_dev_rec->hci_handle, p_dev_rec->sec_state, p_dev_rec->sec_flags,
  1267. p_dev_rec->security_required);
  1268. #if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
  1269. if (transport == BT_TRANSPORT_LE) {
  1270. tACL_CONN *p = btm_bda_to_acl(bd_addr, transport);
  1271. if (p) {
  1272. rc = btm_ble_set_encryption(bd_addr, p_ref_data, p->link_role);
  1273. } else {
  1274. rc = BTM_WRONG_MODE;
  1275. BTM_TRACE_WARNING("%s: cannot call btm_ble_set_encryption, p is NULL\n", __FUNCTION__);
  1276. }
  1277. } else
  1278. #endif
  1279. {
  1280. rc = btm_sec_execute_procedure (p_dev_rec);
  1281. }
  1282. if (rc != BTM_CMD_STARTED && rc != BTM_BUSY) {
  1283. if (p_callback) {
  1284. p_dev_rec->p_callback = NULL;
  1285. (*p_callback) (bd_addr, transport, p_dev_rec->p_ref_data, rc);
  1286. }
  1287. }
  1288. return (rc);
  1289. }
  1290. /*******************************************************************************
  1291. * disconnect the ACL link, if it's not done yet.
  1292. *******************************************************************************/
  1293. static tBTM_STATUS btm_sec_send_hci_disconnect (tBTM_SEC_DEV_REC *p_dev_rec, UINT8 reason, UINT16 conn_handle)
  1294. {
  1295. UINT8 old_state = p_dev_rec->sec_state;
  1296. tBTM_STATUS status = BTM_CMD_STARTED;
  1297. BTM_TRACE_EVENT ("btm_sec_send_hci_disconnect: handle:0x%x, reason=0x%x\n",
  1298. conn_handle, reason);
  1299. /* send HCI_Disconnect on a transport only once */
  1300. switch (old_state) {
  1301. case BTM_SEC_STATE_DISCONNECTING:
  1302. if (conn_handle == p_dev_rec->hci_handle) {
  1303. return status;
  1304. }
  1305. p_dev_rec->sec_state = BTM_SEC_STATE_DISCONNECTING_BOTH;
  1306. break;
  1307. #if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
  1308. case BTM_SEC_STATE_DISCONNECTING_BLE:
  1309. if (conn_handle == p_dev_rec->ble_hci_handle) {
  1310. return status;
  1311. }
  1312. p_dev_rec->sec_state = BTM_SEC_STATE_DISCONNECTING_BOTH;
  1313. break;
  1314. case BTM_SEC_STATE_DISCONNECTING_BOTH:
  1315. return status;
  1316. #endif
  1317. default:
  1318. p_dev_rec->sec_state = (conn_handle == p_dev_rec->hci_handle) ?
  1319. BTM_SEC_STATE_DISCONNECTING : BTM_SEC_STATE_DISCONNECTING_BLE;
  1320. break;
  1321. }
  1322. /* If a role switch is in progress, delay the HCI Disconnect to avoid controller problem */
  1323. if (p_dev_rec->rs_disc_pending == BTM_SEC_RS_PENDING && p_dev_rec->hci_handle == conn_handle) {
  1324. BTM_TRACE_DEBUG("RS in progress - Set DISC Pending flag in btm_sec_send_hci_disconnect to delay disconnect\n");
  1325. p_dev_rec->rs_disc_pending = BTM_SEC_DISC_PENDING;
  1326. status = BTM_SUCCESS;
  1327. }
  1328. /* Tear down the HCI link */
  1329. else if (!btsnd_hcic_disconnect (conn_handle, reason)) {
  1330. /* could not send disconnect. restore old state */
  1331. p_dev_rec->sec_state = old_state;
  1332. status = BTM_NO_RESOURCES;
  1333. }
  1334. return status;
  1335. }
  1336. #endif ///SMP_INCLUDED == TRUE
  1337. /*******************************************************************************
  1338. **
  1339. ** Function BTM_ConfirmReqReply
  1340. **
  1341. ** Description This function is called to confirm the numeric value for
  1342. ** Simple Pairing in response to BTM_SP_CFM_REQ_EVT
  1343. **
  1344. ** Parameters: res - result of the operation BTM_SUCCESS if success
  1345. ** bd_addr - Address of the peer device
  1346. **
  1347. *******************************************************************************/
  1348. #if (SMP_INCLUDED == TRUE)
  1349. void BTM_ConfirmReqReply(tBTM_STATUS res, BD_ADDR bd_addr)
  1350. {
  1351. tBTM_SEC_DEV_REC *p_dev_rec;
  1352. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  1353. BTM_TRACE_EVENT ("BTM_ConfirmReqReply() State: %s Res: %u",
  1354. btm_pair_state_descr(btm_cb.pairing_state), res);
  1355. #endif ///BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE
  1356. /* If timeout already expired or has been canceled, ignore the reply */
  1357. if ( (btm_cb.pairing_state != BTM_PAIR_STATE_WAIT_NUMERIC_CONFIRM)
  1358. || (memcmp (btm_cb.pairing_bda, bd_addr, BD_ADDR_LEN) != 0) ) {
  1359. return;
  1360. }
  1361. btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_AUTH_COMPLETE);
  1362. if ( (res == BTM_SUCCESS) || (res == BTM_SUCCESS_NO_SECURITY) ) {
  1363. btm_cb.acl_disc_reason = HCI_SUCCESS;
  1364. if (res == BTM_SUCCESS) {
  1365. if ((p_dev_rec = btm_find_dev (bd_addr)) != NULL) {
  1366. p_dev_rec->sec_flags |= BTM_SEC_LINK_KEY_AUTHED;
  1367. p_dev_rec->sec_flags |= BTM_SEC_16_DIGIT_PIN_AUTHED;
  1368. }
  1369. }
  1370. btsnd_hcic_user_conf_reply (bd_addr, TRUE);
  1371. } else {
  1372. /* Report authentication failed event from state BTM_PAIR_STATE_WAIT_AUTH_COMPLETE */
  1373. btm_cb.acl_disc_reason = HCI_ERR_HOST_REJECT_SECURITY;
  1374. btsnd_hcic_user_conf_reply (bd_addr, FALSE);
  1375. }
  1376. }
  1377. #endif ///SMP_INCLUDED == TRUE
  1378. /*******************************************************************************
  1379. **
  1380. ** Function BTM_PasskeyReqReply
  1381. **
  1382. ** Description This function is called to provide the passkey for
  1383. ** Simple Pairing in response to BTM_SP_KEY_REQ_EVT
  1384. **
  1385. ** Parameters: res - result of the operation BTM_SUCCESS if success
  1386. ** bd_addr - Address of the peer device
  1387. ** passkey - numeric value in the range of
  1388. ** BTM_MIN_PASSKEY_VAL(0) - BTM_MAX_PASSKEY_VAL(999999(0xF423F)).
  1389. **
  1390. *******************************************************************************/
  1391. #if (BT_SSP_INCLUDED == TRUE && SMP_INCLUDED == TRUE)
  1392. void BTM_PasskeyReqReply(tBTM_STATUS res, BD_ADDR bd_addr, UINT32 passkey)
  1393. {
  1394. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  1395. BTM_TRACE_API ("BTM_PasskeyReqReply: State: %s res:%d\n",
  1396. btm_pair_state_descr(btm_cb.pairing_state), res);
  1397. #endif ///BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE
  1398. if ( (btm_cb.pairing_state == BTM_PAIR_STATE_IDLE)
  1399. || (memcmp (btm_cb.pairing_bda, bd_addr, BD_ADDR_LEN) != 0) ) {
  1400. return;
  1401. }
  1402. /* If timeout already expired or has been canceled, ignore the reply */
  1403. if ( (btm_cb.pairing_state == BTM_PAIR_STATE_WAIT_AUTH_COMPLETE) && (res != BTM_SUCCESS) ) {
  1404. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
  1405. if (p_dev_rec != NULL) {
  1406. btm_cb.acl_disc_reason = HCI_ERR_HOST_REJECT_SECURITY;
  1407. if (p_dev_rec->hci_handle != BTM_SEC_INVALID_HANDLE) {
  1408. btm_sec_send_hci_disconnect (p_dev_rec, HCI_ERR_AUTH_FAILURE, p_dev_rec->hci_handle);
  1409. } else {
  1410. BTM_SecBondCancel(bd_addr);
  1411. }
  1412. p_dev_rec->sec_flags &= ~(BTM_SEC_LINK_KEY_AUTHED | BTM_SEC_LINK_KEY_KNOWN);
  1413. btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
  1414. return;
  1415. }
  1416. } else if (btm_cb.pairing_state != BTM_PAIR_STATE_KEY_ENTRY) {
  1417. return;
  1418. }
  1419. if (passkey > BTM_MAX_PASSKEY_VAL) {
  1420. res = BTM_ILLEGAL_VALUE;
  1421. }
  1422. btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_AUTH_COMPLETE);
  1423. if (res != BTM_SUCCESS) {
  1424. /* use BTM_PAIR_STATE_WAIT_AUTH_COMPLETE to report authentication failed event */
  1425. btm_cb.acl_disc_reason = HCI_ERR_HOST_REJECT_SECURITY;
  1426. btsnd_hcic_user_passkey_neg_reply (bd_addr);
  1427. } else {
  1428. btm_cb.acl_disc_reason = HCI_SUCCESS;
  1429. btsnd_hcic_user_passkey_reply (bd_addr, passkey);
  1430. }
  1431. }
  1432. #endif ///BT_SSP_INCLUDED == TRUE && SMP_INCLUDED == TRUE
  1433. /*******************************************************************************
  1434. **
  1435. ** Function BTM_SendKeypressNotif
  1436. **
  1437. ** Description This function is used during the passkey entry model
  1438. ** by a device with KeyboardOnly IO capabilities
  1439. ** (very likely to be a HID Device).
  1440. ** It is called by a HID Device to inform the remote device when
  1441. ** a key has been entered or erased.
  1442. **
  1443. ** Parameters: bd_addr - Address of the peer device
  1444. ** type - notification type
  1445. **
  1446. *******************************************************************************/
  1447. #if (BT_SSP_INCLUDED == TRUE && SMP_INCLUDED == TRUE)
  1448. void BTM_SendKeypressNotif(BD_ADDR bd_addr, tBTM_SP_KEY_TYPE type)
  1449. {
  1450. /* This API only make sense between PASSKEY_REQ and SP complete */
  1451. if (btm_cb.pairing_state == BTM_PAIR_STATE_KEY_ENTRY) {
  1452. btsnd_hcic_send_keypress_notif (bd_addr, type);
  1453. }
  1454. }
  1455. #endif ///BT_SSP_INCLUDED == TRUE && SMP_INCLUDED == TRUE
  1456. #if BTM_OOB_INCLUDED == TRUE && SMP_INCLUDED == TRUE
  1457. /*******************************************************************************
  1458. **
  1459. ** Function BTM_IoCapRsp
  1460. **
  1461. ** Description This function is called in response to BTM_SP_IO_REQ_EVT
  1462. ** When the event data io_req.oob_data is set to BTM_OOB_UNKNOWN
  1463. ** by the tBTM_SP_CALLBACK implementation, this function is
  1464. ** called to provide the actual response
  1465. **
  1466. ** Parameters: bd_addr - Address of the peer device
  1467. ** io_cap - The IO capability of local device.
  1468. ** oob - BTM_OOB_NONE or BTM_OOB_PRESENT.
  1469. ** auth_req- MITM protection required or not.
  1470. **
  1471. *******************************************************************************/
  1472. void BTM_IoCapRsp(BD_ADDR bd_addr, tBTM_IO_CAP io_cap, tBTM_OOB_DATA oob, tBTM_AUTH_REQ auth_req)
  1473. {
  1474. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  1475. BTM_TRACE_EVENT ("BTM_IoCapRsp: state: %s oob: %d io_cap: %d\n",
  1476. btm_pair_state_descr(btm_cb.pairing_state), oob, io_cap);
  1477. #endif ///BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE
  1478. if ( (btm_cb.pairing_state != BTM_PAIR_STATE_WAIT_LOCAL_IOCAPS)
  1479. || (memcmp (btm_cb.pairing_bda, bd_addr, BD_ADDR_LEN) != 0) ) {
  1480. return;
  1481. }
  1482. if (oob < BTM_OOB_UNKNOWN && io_cap < BTM_IO_CAP_MAX) {
  1483. btm_cb.devcb.loc_auth_req = auth_req;
  1484. btm_cb.devcb.loc_io_caps = io_cap;
  1485. if (btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD) {
  1486. auth_req = (BTM_AUTH_DD_BOND | (auth_req & BTM_AUTH_YN_BIT));
  1487. }
  1488. btsnd_hcic_io_cap_req_reply (bd_addr, io_cap, oob, auth_req);
  1489. }
  1490. }
  1491. /*******************************************************************************
  1492. **
  1493. ** Function BTM_ReadLocalOobData
  1494. **
  1495. ** Description This function is called to read the local OOB data from
  1496. ** LM
  1497. **
  1498. *******************************************************************************/
  1499. tBTM_STATUS BTM_ReadLocalOobData(void)
  1500. {
  1501. tBTM_STATUS status = BTM_SUCCESS;
  1502. if (btsnd_hcic_read_local_oob_data() == FALSE) {
  1503. status = BTM_NO_RESOURCES;
  1504. }
  1505. return status;
  1506. }
  1507. /*******************************************************************************
  1508. **
  1509. ** Function BTM_RemoteOobDataReply
  1510. **
  1511. ** Description This function is called to provide the remote OOB data for
  1512. ** Simple Pairing in response to BTM_SP_RMT_OOB_EVT
  1513. **
  1514. ** Parameters: bd_addr - Address of the peer device
  1515. ** c - simple pairing Hash C.
  1516. ** r - simple pairing Randomizer C.
  1517. **
  1518. *******************************************************************************/
  1519. void BTM_RemoteOobDataReply(tBTM_STATUS res, BD_ADDR bd_addr, BT_OCTET16 c, BT_OCTET16 r)
  1520. {
  1521. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  1522. BTM_TRACE_EVENT ("%s() - State: %s res: %d\n", __func__,
  1523. btm_pair_state_descr(btm_cb.pairing_state), res);
  1524. #endif ///BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE
  1525. /* If timeout already expired or has been canceled, ignore the reply */
  1526. if (btm_cb.pairing_state != BTM_PAIR_STATE_WAIT_LOCAL_OOB_RSP) {
  1527. return;
  1528. }
  1529. btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_AUTH_COMPLETE);
  1530. if (res != BTM_SUCCESS) {
  1531. /* use BTM_PAIR_STATE_WAIT_AUTH_COMPLETE to report authentication failed event */
  1532. btm_cb.acl_disc_reason = HCI_ERR_HOST_REJECT_SECURITY;
  1533. btsnd_hcic_rem_oob_neg_reply (bd_addr);
  1534. } else {
  1535. btm_cb.acl_disc_reason = HCI_SUCCESS;
  1536. btsnd_hcic_rem_oob_reply (bd_addr, c, r);
  1537. }
  1538. }
  1539. /*******************************************************************************
  1540. **
  1541. ** Function BTM_BuildOobData
  1542. **
  1543. ** Description This function is called to build the OOB data payload to
  1544. ** be sent over OOB (non-Bluetooth) link
  1545. **
  1546. ** Parameters: p_data - the location for OOB data
  1547. ** max_len - p_data size.
  1548. ** c - simple pairing Hash C.
  1549. ** r - simple pairing Randomizer C.
  1550. ** name_len- 0, local device name would not be included.
  1551. ** otherwise, the local device name is included for
  1552. ** up to this specified length
  1553. **
  1554. ** Returns Number of bytes in p_data.
  1555. **
  1556. *******************************************************************************/
  1557. UINT16 BTM_BuildOobData(UINT8 *p_data, UINT16 max_len, BT_OCTET16 c,
  1558. BT_OCTET16 r, UINT8 name_len)
  1559. {
  1560. UINT8 *p = p_data;
  1561. UINT16 len = 0;
  1562. #if BTM_MAX_LOC_BD_NAME_LEN > 0
  1563. UINT16 name_size;
  1564. UINT8 name_type = BTM_EIR_SHORTENED_LOCAL_NAME_TYPE;
  1565. #endif
  1566. if (p_data && max_len >= BTM_OOB_MANDATORY_SIZE) {
  1567. /* add mandatory part */
  1568. UINT16_TO_STREAM(p, len);
  1569. BDADDR_TO_STREAM(p, controller_get_interface()->get_address()->address);
  1570. len = BTM_OOB_MANDATORY_SIZE;
  1571. max_len -= len;
  1572. /* now optional part */
  1573. /* add Hash C */
  1574. UINT16 delta = BTM_OOB_HASH_C_SIZE + 2;
  1575. if (max_len >= delta) {
  1576. *p++ = BTM_OOB_HASH_C_SIZE + 1;
  1577. *p++ = BTM_EIR_OOB_SSP_HASH_C_TYPE;
  1578. ARRAY_TO_STREAM(p, c, BTM_OOB_HASH_C_SIZE);
  1579. len += delta;
  1580. max_len -= delta;
  1581. }
  1582. /* add Rand R */
  1583. delta = BTM_OOB_RAND_R_SIZE + 2;
  1584. if (max_len >= delta) {
  1585. *p++ = BTM_OOB_RAND_R_SIZE + 1;
  1586. *p++ = BTM_EIR_OOB_SSP_RAND_R_TYPE;
  1587. ARRAY_TO_STREAM(p, r, BTM_OOB_RAND_R_SIZE);
  1588. len += delta;
  1589. max_len -= delta;
  1590. }
  1591. /* add class of device */
  1592. delta = BTM_OOB_COD_SIZE + 2;
  1593. if (max_len >= delta) {
  1594. *p++ = BTM_OOB_COD_SIZE + 1;
  1595. *p++ = BTM_EIR_OOB_COD_TYPE;
  1596. DEVCLASS_TO_STREAM(p, btm_cb.devcb.dev_class);
  1597. len += delta;
  1598. max_len -= delta;
  1599. }
  1600. #if BTM_MAX_LOC_BD_NAME_LEN > 0
  1601. name_size = name_len;
  1602. if (name_size > strlen(btm_cb.cfg.bd_name)) {
  1603. name_type = BTM_EIR_COMPLETE_LOCAL_NAME_TYPE;
  1604. name_size = (UINT16)strlen(btm_cb.cfg.bd_name);
  1605. }
  1606. delta = name_size + 2;
  1607. if (max_len >= delta) {
  1608. *p++ = name_size + 1;
  1609. *p++ = name_type;
  1610. ARRAY_TO_STREAM (p, btm_cb.cfg.bd_name, name_size);
  1611. len += delta;
  1612. max_len -= delta;
  1613. }
  1614. #endif
  1615. /* update len */
  1616. p = p_data;
  1617. UINT16_TO_STREAM(p, len);
  1618. }
  1619. return len;
  1620. }
  1621. /*******************************************************************************
  1622. **
  1623. ** Function BTM_ReadOobData
  1624. **
  1625. ** Description This function is called to parse the OOB data payload
  1626. ** received over OOB (non-Bluetooth) link
  1627. **
  1628. ** Parameters: p_data - the location for OOB data
  1629. ** eir_tag - The associated EIR tag to read the data.
  1630. ** *p_len(output) - the length of the data with the given tag.
  1631. **
  1632. ** Returns the beginning of the data with the given tag.
  1633. ** NULL, if the tag is not found.
  1634. **
  1635. *******************************************************************************/
  1636. UINT8 *BTM_ReadOobData(UINT8 *p_data, UINT8 eir_tag, UINT8 *p_len)
  1637. {
  1638. UINT8 *p = p_data;
  1639. UINT16 max_len;
  1640. UINT8 len, type;
  1641. UINT8 *p_ret = NULL;
  1642. UINT8 ret_len = 0;
  1643. if (p_data) {
  1644. STREAM_TO_UINT16(max_len, p);
  1645. if (max_len >= BTM_OOB_MANDATORY_SIZE) {
  1646. if (BTM_EIR_OOB_BD_ADDR_TYPE == eir_tag) {
  1647. p_ret = p; /* the location for bd_addr */
  1648. ret_len = BTM_OOB_BD_ADDR_SIZE;
  1649. } else {
  1650. p += BD_ADDR_LEN;
  1651. max_len -= BTM_OOB_MANDATORY_SIZE;
  1652. /* now the optional data in EIR format */
  1653. while (max_len > 0) {
  1654. len = *p++; /* tag data len + 1 */
  1655. type = *p++;
  1656. if (eir_tag == type) {
  1657. p_ret = p;
  1658. ret_len = len - 1;
  1659. break;
  1660. }
  1661. /* the data size of this tag is len + 1 (tag data len + 2) */
  1662. if (max_len > len) {
  1663. max_len -= len;
  1664. max_len--;
  1665. len--;
  1666. p += len;
  1667. } else {
  1668. max_len = 0;
  1669. }
  1670. }
  1671. }
  1672. }
  1673. }
  1674. if (p_len) {
  1675. *p_len = ret_len;
  1676. }
  1677. return p_ret;
  1678. }
  1679. #endif ///BTM_OOB_INCLUDED == TRUE && SMP_INCLUDED == TRUE
  1680. #if (CLASSIC_BT_INCLUDED == TRUE)
  1681. /*******************************************************************************
  1682. **
  1683. ** Function BTM_BothEndsSupportSecureConnections
  1684. **
  1685. ** Description This function is called to check if both the local device and the peer device
  1686. ** specified by bd_addr support BR/EDR Secure Connections.
  1687. **
  1688. ** Parameters: bd_addr - address of the peer
  1689. **
  1690. ** Returns TRUE if BR/EDR Secure Connections are supported by both local
  1691. ** and the remote device.
  1692. ** else FALSE.
  1693. **
  1694. *******************************************************************************/
  1695. BOOLEAN BTM_BothEndsSupportSecureConnections(BD_ADDR bd_addr)
  1696. {
  1697. return ((controller_get_interface()->supports_secure_connections()) &&
  1698. (BTM_PeerSupportsSecureConnections(bd_addr)));
  1699. }
  1700. /*******************************************************************************
  1701. **
  1702. ** Function BTM_PeerSupportsSecureConnections
  1703. **
  1704. ** Description This function is called to check if the peer supports
  1705. ** BR/EDR Secure Connections.
  1706. **
  1707. ** Parameters: bd_addr - address of the peer
  1708. **
  1709. ** Returns TRUE if BR/EDR Secure Connections are supported by the peer,
  1710. ** else FALSE.
  1711. **
  1712. *******************************************************************************/
  1713. BOOLEAN BTM_PeerSupportsSecureConnections(BD_ADDR bd_addr)
  1714. {
  1715. tBTM_SEC_DEV_REC *p_dev_rec;
  1716. if ((p_dev_rec = btm_find_dev(bd_addr)) == NULL) {
  1717. BTM_TRACE_WARNING("%s: unknown BDA: %08x%04x\n", __FUNCTION__,
  1718. (bd_addr[0] << 24) + (bd_addr[1] << 16) + (bd_addr[2] << 8) + bd_addr[3],
  1719. (bd_addr[4] << 8) + bd_addr[5]);
  1720. return FALSE;
  1721. }
  1722. return (p_dev_rec->remote_supports_secure_connections);
  1723. }
  1724. /*******************************************************************************
  1725. **
  1726. ** Function BTM_SetOutService
  1727. **
  1728. ** Description This function is called to set the service for
  1729. ** outgoing connections.
  1730. **
  1731. ** If the profile/application calls BTM_SetSecurityLevel
  1732. ** before initiating a connection, this function does not
  1733. ** need to be called.
  1734. **
  1735. ** Returns void
  1736. **
  1737. *******************************************************************************/
  1738. void BTM_SetOutService(BD_ADDR bd_addr, UINT8 service_id, UINT32 mx_chan_id)
  1739. {
  1740. tBTM_SEC_DEV_REC *p_dev_rec;
  1741. tBTM_SEC_SERV_REC *p_serv_rec = &btm_cb.sec_serv_rec[0];
  1742. btm_cb.p_out_serv = p_serv_rec;
  1743. p_dev_rec = btm_find_dev (bd_addr);
  1744. for (int i = 0; i < BTM_SEC_MAX_SERVICE_RECORDS; i++, p_serv_rec++) {
  1745. if ((p_serv_rec->security_flags & BTM_SEC_IN_USE)
  1746. && (p_serv_rec->service_id == service_id)
  1747. && (p_serv_rec->orig_mx_chan_id == mx_chan_id)) {
  1748. BTM_TRACE_API("BTM_SetOutService p_out_serv id %d, psm 0x%04x, proto_id %d, chan_id %d\n",
  1749. p_serv_rec->service_id, p_serv_rec->psm, p_serv_rec->mx_proto_id, p_serv_rec->orig_mx_chan_id);
  1750. btm_cb.p_out_serv = p_serv_rec;
  1751. if (p_dev_rec) {
  1752. p_dev_rec->p_cur_service = p_serv_rec;
  1753. }
  1754. break;
  1755. }
  1756. }
  1757. }
  1758. #endif ///CLASSIC_BT_INCLUDED == TRUE
  1759. /************************************************************************
  1760. ** I N T E R N A L F U N C T I O N S
  1761. *************************************************************************/
  1762. /*******************************************************************************
  1763. **
  1764. ** Function btm_sec_is_upgrade_possible
  1765. **
  1766. ** Description This function returns TRUE if the existing link key
  1767. ** can be upgraded or if the link key does not exist.
  1768. **
  1769. ** Returns BOOLEAN
  1770. **
  1771. *******************************************************************************/
  1772. #if (SMP_INCLUDED == TRUE)
  1773. static BOOLEAN btm_sec_is_upgrade_possible(tBTM_SEC_DEV_REC *p_dev_rec, BOOLEAN is_originator)
  1774. {
  1775. UINT16 mtm_check = is_originator ? BTM_SEC_OUT_MITM : BTM_SEC_IN_MITM;
  1776. BOOLEAN is_possible = TRUE;
  1777. if (p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_KNOWN) {
  1778. is_possible = FALSE;
  1779. if (p_dev_rec->p_cur_service) {
  1780. BTM_TRACE_DEBUG ("%s() id: %d, link_key_typet: %d, rmt_io_caps: %d, chk flags: 0x%x, flags: 0x%x\n",
  1781. __func__, p_dev_rec->p_cur_service->service_id, p_dev_rec->link_key_type,
  1782. p_dev_rec->rmt_io_caps, mtm_check, p_dev_rec->p_cur_service->security_flags);
  1783. } else {
  1784. BTM_TRACE_DEBUG ("%s() link_key_typet: %d, rmt_io_caps: %d, chk flags: 0x%x\n",
  1785. __func__, p_dev_rec->link_key_type, p_dev_rec->rmt_io_caps, mtm_check);
  1786. }
  1787. /* Already have a link key to the connected peer. Is the link key secure enough?
  1788. ** Is a link key upgrade even possible?
  1789. */
  1790. if ((p_dev_rec->security_required & mtm_check) /* needs MITM */
  1791. && ((p_dev_rec->link_key_type == BTM_LKEY_TYPE_UNAUTH_COMB) ||
  1792. (p_dev_rec->link_key_type == BTM_LKEY_TYPE_UNAUTH_COMB_P_256))
  1793. /* has unauthenticated
  1794. link key */
  1795. && (p_dev_rec->rmt_io_caps < BTM_IO_CAP_MAX) /* a valid peer IO cap */
  1796. && (btm_sec_io_map[p_dev_rec->rmt_io_caps][btm_cb.devcb.loc_io_caps]))
  1797. /* authenticated
  1798. link key is possible */
  1799. {
  1800. /* upgrade is possible: check if the application wants the upgrade.
  1801. * If the application is configured to use a global MITM flag,
  1802. * it probably would not want to upgrade the link key based on the security level database */
  1803. is_possible = TRUE;
  1804. }
  1805. }
  1806. BTM_TRACE_DEBUG ("%s() is_possible: %d sec_flags: 0x%x\n", __func__, is_possible, p_dev_rec->sec_flags);
  1807. return is_possible;
  1808. }
  1809. #endif ///SMP_INCLUDED == TRUE
  1810. /*******************************************************************************
  1811. **
  1812. ** Function btm_sec_check_upgrade
  1813. **
  1814. ** Description This function is called to check if the existing link key
  1815. ** needs to be upgraded.
  1816. **
  1817. ** Returns void
  1818. **
  1819. *******************************************************************************/
  1820. #if (SMP_INCLUDED == TRUE)
  1821. static void btm_sec_check_upgrade(tBTM_SEC_DEV_REC *p_dev_rec, BOOLEAN is_originator)
  1822. {
  1823. BTM_TRACE_DEBUG ("%s()\n", __func__);
  1824. /* Only check if link key already exists */
  1825. if (!(p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_KNOWN)) {
  1826. return;
  1827. }
  1828. if (btm_sec_is_upgrade_possible (p_dev_rec, is_originator) == TRUE) {
  1829. BTM_TRACE_DEBUG ("need upgrade!! sec_flags:0x%x\n", p_dev_rec->sec_flags);
  1830. /* upgrade is possible: check if the application wants the upgrade.
  1831. * If the application is configured to use a global MITM flag,
  1832. * it probably would not want to upgrade the link key based on the security level database */
  1833. tBTM_SP_UPGRADE evt_data;
  1834. memcpy (evt_data.bd_addr, p_dev_rec->bd_addr, BD_ADDR_LEN);
  1835. evt_data.upgrade = TRUE;
  1836. if (btm_cb.api.p_sp_callback) {
  1837. (*btm_cb.api.p_sp_callback) (BTM_SP_UPGRADE_EVT, (tBTM_SP_EVT_DATA *)&evt_data);
  1838. }
  1839. BTM_TRACE_DEBUG ("evt_data.upgrade:0x%x\n", evt_data.upgrade);
  1840. if (evt_data.upgrade) {
  1841. /* if the application confirms the upgrade, set the upgrade bit */
  1842. p_dev_rec->sm4 |= BTM_SM4_UPGRADE;
  1843. /* Clear the link key known to go through authentication/pairing again */
  1844. p_dev_rec->sec_flags &= ~(BTM_SEC_LINK_KEY_KNOWN | BTM_SEC_LINK_KEY_AUTHED);
  1845. p_dev_rec->sec_flags &= ~BTM_SEC_AUTHENTICATED;
  1846. BTM_TRACE_DEBUG ("sec_flags:0x%x\n", p_dev_rec->sec_flags);
  1847. }
  1848. }
  1849. }
  1850. #endif ///SMP_INCLUDED == TRUE
  1851. /*******************************************************************************
  1852. **
  1853. ** Function btm_sec_l2cap_access_req
  1854. **
  1855. ** Description This function is called by the L2CAP to grant permission to
  1856. ** establish L2CAP connection to or from the peer device.
  1857. **
  1858. ** Parameters: bd_addr - Address of the peer device
  1859. ** psm - L2CAP PSM
  1860. ** is_originator - TRUE if protocol above L2CAP originates
  1861. ** connection
  1862. ** p_callback - Pointer to callback function called if
  1863. ** this function returns PENDING after required
  1864. ** procedures are complete. MUST NOT BE NULL.
  1865. **
  1866. ** Returns tBTM_STATUS
  1867. **
  1868. *******************************************************************************/
  1869. tBTM_STATUS btm_sec_l2cap_access_req (BD_ADDR bd_addr, UINT16 psm, UINT16 handle,
  1870. CONNECTION_TYPE conn_type,
  1871. tBTM_SEC_CALLBACK *p_callback,
  1872. void *p_ref_data)
  1873. {
  1874. #if (SMP_INCLUDED == TRUE)
  1875. tBTM_SEC_DEV_REC *p_dev_rec;
  1876. tBTM_SEC_SERV_REC *p_serv_rec;
  1877. UINT16 security_required;
  1878. UINT16 old_security_required;
  1879. BOOLEAN old_is_originator;
  1880. tBTM_STATUS rc = BTM_SUCCESS;
  1881. BOOLEAN chk_acp_auth_done = FALSE;
  1882. BOOLEAN is_originator;
  1883. BOOLEAN transport = FALSE; /* should check PSM range in LE connection oriented L2CAP connection */
  1884. #if (L2CAP_UCD_INCLUDED == TRUE)
  1885. if (conn_type & CONNECTION_TYPE_ORIG_MASK) {
  1886. is_originator = TRUE;
  1887. } else {
  1888. is_originator = FALSE;
  1889. }
  1890. BTM_TRACE_DEBUG ("%s() conn_type: 0x%x, %p\n", __func__, conn_type, p_ref_data);
  1891. #else
  1892. is_originator = conn_type;
  1893. BTM_TRACE_DEBUG ("%s() is_originator:%d, %p\n", __func__, is_originator, p_ref_data);
  1894. #endif
  1895. /* Find or get oldest record */
  1896. p_dev_rec = btm_find_or_alloc_dev (bd_addr);
  1897. p_dev_rec->hci_handle = handle;
  1898. /* Find the service record for the PSM */
  1899. p_serv_rec = btm_sec_find_first_serv (conn_type, psm);
  1900. /* If there is no application registered with this PSM do not allow connection */
  1901. if (!p_serv_rec) {
  1902. BTM_TRACE_WARNING ("%s() PSM: %d no application registerd\n", __func__, psm);
  1903. (*p_callback) (bd_addr, transport, p_ref_data, BTM_MODE_UNSUPPORTED);
  1904. return (BTM_MODE_UNSUPPORTED);
  1905. }
  1906. /* Services level0 by default have no security */
  1907. if ((btm_sec_is_serv_level0(psm)) && (!btm_cb.devcb.secure_connections_only)) {
  1908. (*p_callback) (bd_addr, transport, p_ref_data, BTM_SUCCESS_NO_SECURITY);
  1909. return (BTM_SUCCESS);
  1910. }
  1911. #if (L2CAP_UCD_INCLUDED == TRUE)
  1912. if ( conn_type & CONNECTION_TYPE_CONNLESS_MASK ) {
  1913. if (btm_cb.security_mode == BTM_SEC_MODE_SC) {
  1914. security_required = btm_sec_set_serv_level4_flags (p_serv_rec->ucd_security_flags,
  1915. is_originator);
  1916. } else {
  1917. security_required = p_serv_rec->ucd_security_flags;
  1918. }
  1919. rc = BTM_CMD_STARTED;
  1920. if (is_originator) {
  1921. if (((security_required & BTM_SEC_OUT_FLAGS) == 0) ||
  1922. ((((security_required & BTM_SEC_OUT_FLAGS) == BTM_SEC_OUT_AUTHENTICATE) && (p_dev_rec->sec_flags & BTM_SEC_AUTHENTICATED))) ||
  1923. ((((security_required & BTM_SEC_OUT_FLAGS) == (BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_OUT_ENCRYPT)) && (p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED))) ||
  1924. ((((security_required & BTM_SEC_OUT_FLAGS) == BTM_SEC_OUT_FLAGS) && (p_dev_rec->sec_flags & BTM_SEC_AUTHORIZED))) ) {
  1925. rc = BTM_SUCCESS;
  1926. }
  1927. } else {
  1928. if (((security_required & BTM_SEC_IN_FLAGS) == 0) ||
  1929. ((((security_required & BTM_SEC_IN_FLAGS) == BTM_SEC_IN_AUTHENTICATE) && (p_dev_rec->sec_flags & BTM_SEC_AUTHENTICATED))) ||
  1930. ((((security_required & BTM_SEC_IN_FLAGS) == (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_ENCRYPT)) && (p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED))) ||
  1931. ((((security_required & BTM_SEC_IN_FLAGS) == BTM_SEC_IN_FLAGS) && (p_dev_rec->sec_flags & BTM_SEC_AUTHORIZED))) ) {
  1932. // Check for 16 digits (or MITM)
  1933. if (((security_required & BTM_SEC_IN_MIN_16_DIGIT_PIN) == 0) ||
  1934. (((security_required & BTM_SEC_IN_MIN_16_DIGIT_PIN) == BTM_SEC_IN_MIN_16_DIGIT_PIN) &&
  1935. btm_dev_16_digit_authenticated(p_dev_rec))) {
  1936. rc = BTM_SUCCESS;
  1937. }
  1938. }
  1939. }
  1940. if ((rc == BTM_SUCCESS) && (security_required & BTM_SEC_MODE4_LEVEL4) &&
  1941. (p_dev_rec->link_key_type != BTM_LKEY_TYPE_AUTH_COMB_P_256)) {
  1942. rc = BTM_CMD_STARTED;
  1943. }
  1944. if (rc == BTM_SUCCESS) {
  1945. if (p_callback) {
  1946. (*p_callback) (bd_addr, transport, (void *)p_ref_data, BTM_SUCCESS);
  1947. }
  1948. return (BTM_SUCCESS);
  1949. }
  1950. } else
  1951. #endif
  1952. {
  1953. if (btm_cb.security_mode == BTM_SEC_MODE_SC) {
  1954. security_required = btm_sec_set_serv_level4_flags (p_serv_rec->security_flags,
  1955. is_originator);
  1956. } else {
  1957. security_required = p_serv_rec->security_flags;
  1958. }
  1959. }
  1960. BTM_TRACE_DEBUG("%s: security_required 0x%04x, is_originator 0x%02x, psm 0x%04x\n",
  1961. __FUNCTION__, security_required, is_originator, psm);
  1962. if ((!is_originator) && (security_required & BTM_SEC_MODE4_LEVEL4)) {
  1963. BOOLEAN local_supports_sc = controller_get_interface()->supports_secure_connections();
  1964. /* acceptor receives L2CAP Channel Connect Request for Secure Connections Only service */
  1965. if (!(local_supports_sc) || !(p_dev_rec->remote_supports_secure_connections)) {
  1966. BTM_TRACE_DEBUG("%s: SC only service, local_support_for_sc %d\n"
  1967. "rmt_support_for_sc : %d -> fail pairing\n", __FUNCTION__,
  1968. local_supports_sc,
  1969. p_dev_rec->remote_supports_secure_connections);
  1970. if (p_callback) {
  1971. (*p_callback) (bd_addr, transport, (void *)p_ref_data,
  1972. BTM_MODE4_LEVEL4_NOT_SUPPORTED);
  1973. }
  1974. return (BTM_MODE4_LEVEL4_NOT_SUPPORTED);
  1975. }
  1976. }
  1977. /* there are some devices (moto KRZR) which connects to several services at the same time */
  1978. /* we will process one after another */
  1979. if ( (p_dev_rec->p_callback) || (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE) ) {
  1980. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  1981. BTM_TRACE_EVENT ("%s() - busy - PSM:%d delayed state: %s mode:%d, sm4:0x%x\n", __func__,
  1982. psm, btm_pair_state_descr(btm_cb.pairing_state), btm_cb.security_mode, p_dev_rec->sm4);
  1983. #endif ///BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE
  1984. BTM_TRACE_EVENT ("security_flags:x%x, sec_flags:x%x\n", security_required, p_dev_rec->sec_flags);
  1985. rc = BTM_CMD_STARTED;
  1986. if ((btm_cb.security_mode == BTM_SEC_MODE_UNDEFINED ||
  1987. btm_cb.security_mode == BTM_SEC_MODE_NONE ||
  1988. btm_cb.security_mode == BTM_SEC_MODE_SERVICE ||
  1989. btm_cb.security_mode == BTM_SEC_MODE_LINK) ||
  1990. (BTM_SM4_KNOWN == p_dev_rec->sm4) || (BTM_SEC_IS_SM4(p_dev_rec->sm4) &&
  1991. (btm_sec_is_upgrade_possible(p_dev_rec, is_originator) == FALSE))) {
  1992. /* legacy mode - local is legacy or local is lisbon/peer is legacy
  1993. * or SM4 with no possibility of link key upgrade */
  1994. if (is_originator) {
  1995. if (((security_required & BTM_SEC_OUT_FLAGS) == 0) ||
  1996. ((((security_required & BTM_SEC_OUT_FLAGS) == BTM_SEC_OUT_AUTHENTICATE) && btm_dev_authenticated(p_dev_rec))) ||
  1997. ((((security_required & BTM_SEC_OUT_FLAGS) == (BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_OUT_ENCRYPT)) && btm_dev_encrypted(p_dev_rec))) ||
  1998. ((((security_required & BTM_SEC_OUT_FLAGS) == BTM_SEC_OUT_FLAGS) && btm_dev_authorized(p_dev_rec) && btm_dev_encrypted(p_dev_rec))) ) {
  1999. rc = BTM_SUCCESS;
  2000. }
  2001. } else {
  2002. if (((security_required & BTM_SEC_IN_FLAGS) == 0) ||
  2003. (((security_required & BTM_SEC_IN_FLAGS) == BTM_SEC_IN_AUTHENTICATE) && btm_dev_authenticated(p_dev_rec)) ||
  2004. (((security_required & BTM_SEC_IN_FLAGS) == (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_ENCRYPT)) && btm_dev_encrypted(p_dev_rec)) ||
  2005. (((security_required & BTM_SEC_IN_FLAGS) == BTM_SEC_IN_AUTHORIZE) && (btm_dev_authorized(p_dev_rec) || btm_serv_trusted(p_dev_rec, p_serv_rec))) ||
  2006. (((security_required & BTM_SEC_IN_FLAGS) == (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_AUTHORIZE)) && ((btm_dev_authorized(p_dev_rec) || btm_serv_trusted(p_dev_rec, p_serv_rec)) && btm_dev_authenticated(p_dev_rec))) ||
  2007. (((security_required & BTM_SEC_IN_FLAGS) == (BTM_SEC_IN_ENCRYPT | BTM_SEC_IN_AUTHORIZE)) && ((btm_dev_authorized(p_dev_rec) || btm_serv_trusted(p_dev_rec, p_serv_rec)) && btm_dev_encrypted(p_dev_rec))) ||
  2008. (((security_required & BTM_SEC_IN_FLAGS) == BTM_SEC_IN_FLAGS) && btm_dev_encrypted(p_dev_rec) && (btm_dev_authorized(p_dev_rec) || btm_serv_trusted(p_dev_rec, p_serv_rec)))) {
  2009. // Check for 16 digits (or MITM)
  2010. if (((security_required & BTM_SEC_IN_MIN_16_DIGIT_PIN) == 0) ||
  2011. (((security_required & BTM_SEC_IN_MIN_16_DIGIT_PIN) == BTM_SEC_IN_MIN_16_DIGIT_PIN) && btm_dev_16_digit_authenticated(p_dev_rec))) {
  2012. rc = BTM_SUCCESS;
  2013. }
  2014. }
  2015. }
  2016. if ((rc == BTM_SUCCESS) && (security_required & BTM_SEC_MODE4_LEVEL4) &&
  2017. (p_dev_rec->link_key_type != BTM_LKEY_TYPE_AUTH_COMB_P_256)) {
  2018. rc = BTM_CMD_STARTED;
  2019. }
  2020. if (rc == BTM_SUCCESS) {
  2021. if (p_callback) {
  2022. (*p_callback) (bd_addr, transport, (void *)p_ref_data, BTM_SUCCESS);
  2023. }
  2024. return (BTM_SUCCESS);
  2025. }
  2026. }
  2027. btm_cb.sec_req_pending = TRUE;
  2028. return (BTM_CMD_STARTED);
  2029. }
  2030. /* Save pointer to service record */
  2031. p_dev_rec->p_cur_service = p_serv_rec;
  2032. /* Modify security_required in btm_sec_l2cap_access_req for Lisbon */
  2033. if (btm_cb.security_mode == BTM_SEC_MODE_SP ||
  2034. btm_cb.security_mode == BTM_SEC_MODE_SP_DEBUG ||
  2035. btm_cb.security_mode == BTM_SEC_MODE_SC) {
  2036. if (BTM_SEC_IS_SM4(p_dev_rec->sm4)) {
  2037. if (is_originator) {
  2038. /* SM4 to SM4 -> always authenticate & encrypt */
  2039. security_required |= (BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_OUT_ENCRYPT);
  2040. } else { /* acceptor */
  2041. /* SM4 to SM4: the acceptor needs to make sure the authentication is already done */
  2042. chk_acp_auth_done = TRUE;
  2043. /* SM4 to SM4 -> always authenticate & encrypt */
  2044. security_required |= (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_ENCRYPT);
  2045. }
  2046. } else if (!(BTM_SM4_KNOWN & p_dev_rec->sm4)) {
  2047. /* the remote features are not known yet */
  2048. BTM_TRACE_ERROR("%s: (%s) remote features unknown!!sec_flags:0x%02x\n", __FUNCTION__,
  2049. (is_originator) ? "initiator" : "acceptor", p_dev_rec->sec_flags);
  2050. p_dev_rec->sm4 |= BTM_SM4_REQ_PEND;
  2051. return (BTM_CMD_STARTED);
  2052. }
  2053. }
  2054. BTM_TRACE_DEBUG ("%s() sm4:0x%x, sec_flags:0x%x, security_required:0x%x chk:%d\n", __func__,
  2055. p_dev_rec->sm4, p_dev_rec->sec_flags, security_required, chk_acp_auth_done);
  2056. old_security_required = p_dev_rec->security_required;
  2057. old_is_originator = p_dev_rec->is_originator;
  2058. p_dev_rec->security_required = security_required;
  2059. p_dev_rec->p_ref_data = p_ref_data;
  2060. p_dev_rec->is_originator = is_originator;
  2061. #if (L2CAP_UCD_INCLUDED == TRUE)
  2062. if ( conn_type & CONNECTION_TYPE_CONNLESS_MASK ) {
  2063. p_dev_rec->is_ucd = TRUE;
  2064. } else {
  2065. p_dev_rec->is_ucd = FALSE;
  2066. }
  2067. #endif
  2068. /* If there are multiple service records used through the same PSM */
  2069. /* leave security decision for the multiplexor on the top */
  2070. #if (L2CAP_UCD_INCLUDED == TRUE)
  2071. if (((btm_sec_find_next_serv (p_serv_rec)) != NULL)
  2072. && (!( conn_type & CONNECTION_TYPE_CONNLESS_MASK ))) /* if not UCD */
  2073. #else
  2074. if ((btm_sec_find_next_serv (p_serv_rec)) != NULL)
  2075. #endif
  2076. {
  2077. BTM_TRACE_DEBUG ("no next_serv sm4:0x%x, chk:%d\n", p_dev_rec->sm4, chk_acp_auth_done);
  2078. if (!BTM_SEC_IS_SM4(p_dev_rec->sm4)) {
  2079. BTM_TRACE_EVENT ("Security Manager: l2cap_access_req PSM:%d postponed for multiplexer\n", psm);
  2080. /* pre-Lisbon: restore the old settings */
  2081. p_dev_rec->security_required = old_security_required;
  2082. p_dev_rec->is_originator = old_is_originator;
  2083. (*p_callback) (bd_addr, transport, p_ref_data, BTM_SUCCESS);
  2084. return (BTM_SUCCESS);
  2085. }
  2086. }
  2087. /* if the originator is using dynamic PSM in legacy mode, do not start any security process now
  2088. * The layer above L2CAP needs to carry out the security requirement after L2CAP connect
  2089. * response is received */
  2090. if (is_originator &&
  2091. ((btm_cb.security_mode == BTM_SEC_MODE_UNDEFINED ||
  2092. btm_cb.security_mode == BTM_SEC_MODE_NONE ||
  2093. btm_cb.security_mode == BTM_SEC_MODE_SERVICE ||
  2094. btm_cb.security_mode == BTM_SEC_MODE_LINK) ||
  2095. !BTM_SEC_IS_SM4(p_dev_rec->sm4)) && (psm >= 0x1001)) {
  2096. BTM_TRACE_EVENT ("dynamic PSM:0x%x in legacy mode - postponed for upper layer\n", psm);
  2097. /* restore the old settings */
  2098. p_dev_rec->security_required = old_security_required;
  2099. p_dev_rec->is_originator = old_is_originator;
  2100. (*p_callback) (bd_addr, transport, p_ref_data, BTM_SUCCESS);
  2101. return (BTM_SUCCESS);
  2102. }
  2103. if (chk_acp_auth_done) {
  2104. BTM_TRACE_DEBUG ("(SM4 to SM4) btm_sec_l2cap_access_req rspd. authenticated: x%x, enc: x%x\n",
  2105. (p_dev_rec->sec_flags & BTM_SEC_AUTHENTICATED), (p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED));
  2106. /* SM4, but we do not know for sure which level of security we need.
  2107. * as long as we have a link key, it's OK */
  2108. if ((0 == (p_dev_rec->sec_flags & BTM_SEC_AUTHENTICATED))
  2109. || (0 == (p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED))) {
  2110. rc = BTM_DELAY_CHECK;
  2111. /*
  2112. 2046 may report HCI_Encryption_Change and L2C Connection Request out of sequence
  2113. because of data path issues. Delay this disconnect a little bit
  2114. */
  2115. BTM_TRACE_API("%s peer should have initiated security process by now (SM4 to SM4)\n", __func__);
  2116. p_dev_rec->p_callback = p_callback;
  2117. p_dev_rec->sec_state = BTM_SEC_STATE_DELAY_FOR_ENC;
  2118. (*p_callback) (bd_addr, transport, p_ref_data, rc);
  2119. return BTM_SUCCESS;
  2120. }
  2121. }
  2122. p_dev_rec->p_callback = p_callback;
  2123. if (p_dev_rec->last_author_service_id == BTM_SEC_NO_LAST_SERVICE_ID
  2124. || p_dev_rec->last_author_service_id != p_dev_rec->p_cur_service->service_id) {
  2125. /* Although authentication and encryption are per connection
  2126. ** authorization is per access request. For example when serial connection
  2127. ** is up and authorized and client requests to read file (access to other
  2128. ** scn), we need to request user's permission again.
  2129. */
  2130. p_dev_rec->sec_flags &= ~BTM_SEC_AUTHORIZED;
  2131. }
  2132. if (BTM_SEC_IS_SM4(p_dev_rec->sm4)) {
  2133. if ((p_dev_rec->security_required & BTM_SEC_MODE4_LEVEL4) &&
  2134. (p_dev_rec->link_key_type != BTM_LKEY_TYPE_AUTH_COMB_P_256)) {
  2135. /* BTM_LKEY_TYPE_AUTH_COMB_P_256 is the only acceptable key in this case */
  2136. if ((p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_KNOWN) != 0) {
  2137. p_dev_rec->sm4 |= BTM_SM4_UPGRADE;
  2138. }
  2139. p_dev_rec->sec_flags &= ~(BTM_SEC_LINK_KEY_KNOWN | BTM_SEC_LINK_KEY_AUTHED |
  2140. BTM_SEC_AUTHENTICATED);
  2141. BTM_TRACE_DEBUG ("%s: sec_flags:0x%x", __FUNCTION__, p_dev_rec->sec_flags);
  2142. } else {
  2143. /* If we already have a link key to the connected peer, is it secure enough? */
  2144. btm_sec_check_upgrade(p_dev_rec, is_originator);
  2145. }
  2146. }
  2147. BTM_TRACE_EVENT ("%s() PSM:%d Handle:%d State:%d Flags: 0x%x Required: 0x%x Service ID:%d\n",
  2148. __func__, psm, handle, p_dev_rec->sec_state, p_dev_rec->sec_flags,
  2149. p_dev_rec->security_required, p_dev_rec->p_cur_service->service_id);
  2150. if ((rc = btm_sec_execute_procedure (p_dev_rec)) != BTM_CMD_STARTED) {
  2151. p_dev_rec->p_callback = NULL;
  2152. (*p_callback) (bd_addr, transport, p_dev_rec->p_ref_data, (UINT8)rc);
  2153. }
  2154. return (rc);
  2155. #else
  2156. return BTM_MODE_UNSUPPORTED;
  2157. #endif ///SMP_INCLUDED == TRUE
  2158. }
  2159. /*******************************************************************************
  2160. **
  2161. ** Function btm_sec_mx_access_request
  2162. **
  2163. ** Description This function is called by all Multiplexing Protocols during
  2164. ** establishing connection to or from peer device to grant
  2165. ** permission to establish application connection.
  2166. **
  2167. ** Parameters: bd_addr - Address of the peer device
  2168. ** psm - L2CAP PSM
  2169. ** is_originator - TRUE if protocol above L2CAP originates
  2170. ** connection
  2171. ** mx_proto_id - protocol ID of the multiplexer
  2172. ** mx_chan_id - multiplexer channel to reach application
  2173. ** p_callback - Pointer to callback function called if
  2174. ** this function returns PENDING after required
  2175. ** procedures are completed
  2176. ** p_ref_data - Pointer to any reference data needed by the
  2177. ** the callback function.
  2178. **
  2179. ** Returns BTM_CMD_STARTED
  2180. **
  2181. *******************************************************************************/
  2182. tBTM_STATUS btm_sec_mx_access_request (BD_ADDR bd_addr, UINT16 psm, BOOLEAN is_originator,
  2183. UINT32 mx_proto_id, UINT32 mx_chan_id,
  2184. tBTM_SEC_CALLBACK *p_callback, void *p_ref_data)
  2185. {
  2186. #if (SMP_INCLUDED == TRUE)
  2187. tBTM_SEC_DEV_REC *p_dev_rec;
  2188. tBTM_SEC_SERV_REC *p_serv_rec;
  2189. tBTM_STATUS rc;
  2190. UINT16 security_required;
  2191. BOOLEAN transport = FALSE;/* should check PSM range in LE connection oriented L2CAP connection */
  2192. BTM_TRACE_DEBUG ("%s() is_originator: %d\n", __func__, is_originator);
  2193. /* Find or get oldest record */
  2194. p_dev_rec = btm_find_or_alloc_dev (bd_addr);
  2195. /* Find the service record for the PSM */
  2196. p_serv_rec = btm_sec_find_mx_serv (is_originator, psm, mx_proto_id, mx_chan_id);
  2197. /* If there is no application registered with this PSM do not allow connection */
  2198. if (!p_serv_rec) {
  2199. if (p_callback) {
  2200. (*p_callback) (bd_addr, transport, p_ref_data, BTM_MODE_UNSUPPORTED);
  2201. }
  2202. BTM_TRACE_ERROR ("Security Manager: MX service not found PSM:%d Proto:%d SCN:%d\n",
  2203. psm, mx_proto_id, mx_chan_id);
  2204. return BTM_NO_RESOURCES;
  2205. }
  2206. if ((btm_cb.security_mode == BTM_SEC_MODE_SC) && (!btm_sec_is_serv_level0(psm))) {
  2207. security_required = btm_sec_set_serv_level4_flags (p_serv_rec->security_flags,
  2208. is_originator);
  2209. } else {
  2210. security_required = p_serv_rec->security_flags;
  2211. }
  2212. /* there are some devices (moto phone) which connects to several services at the same time */
  2213. /* we will process one after another */
  2214. if ( (p_dev_rec->p_callback) || (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE) ) {
  2215. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  2216. BTM_TRACE_EVENT ("%s() service PSM:%d Proto:%d SCN:%d delayed state: %s\n", __func__,
  2217. psm, mx_proto_id, mx_chan_id, btm_pair_state_descr(btm_cb.pairing_state));
  2218. #endif ///BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE
  2219. rc = BTM_CMD_STARTED;
  2220. if ((btm_cb.security_mode == BTM_SEC_MODE_UNDEFINED ||
  2221. btm_cb.security_mode == BTM_SEC_MODE_NONE ||
  2222. btm_cb.security_mode == BTM_SEC_MODE_SERVICE ||
  2223. btm_cb.security_mode == BTM_SEC_MODE_LINK) ||
  2224. (BTM_SM4_KNOWN == p_dev_rec->sm4) || (BTM_SEC_IS_SM4(p_dev_rec->sm4) &&
  2225. (btm_sec_is_upgrade_possible(p_dev_rec, is_originator) == FALSE))) {
  2226. /* legacy mode - local is legacy or local is lisbon/peer is legacy
  2227. * or SM4 with no possibility of link key upgrade */
  2228. if (is_originator) {
  2229. if (((security_required & BTM_SEC_OUT_FLAGS) == 0) ||
  2230. ((((security_required & BTM_SEC_OUT_FLAGS) == BTM_SEC_OUT_AUTHENTICATE) && btm_dev_authenticated(p_dev_rec))) ||
  2231. ((((security_required & BTM_SEC_OUT_FLAGS) == (BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_OUT_ENCRYPT)) && btm_dev_encrypted(p_dev_rec)))
  2232. ) {
  2233. rc = BTM_SUCCESS;
  2234. }
  2235. } else {
  2236. if (((security_required & BTM_SEC_IN_FLAGS) == 0) ||
  2237. ((((security_required & BTM_SEC_IN_FLAGS) == BTM_SEC_IN_AUTHENTICATE) && btm_dev_authenticated(p_dev_rec))) ||
  2238. (((security_required & BTM_SEC_IN_FLAGS) == BTM_SEC_IN_AUTHORIZE) && (btm_dev_authorized(p_dev_rec) || btm_serv_trusted(p_dev_rec, p_serv_rec))) ||
  2239. (((security_required & BTM_SEC_IN_FLAGS) == (BTM_SEC_IN_AUTHORIZE | BTM_SEC_IN_AUTHENTICATE)) && ((btm_dev_authorized(p_dev_rec) || btm_serv_trusted(p_dev_rec, p_serv_rec)) && btm_dev_authenticated(p_dev_rec))) ||
  2240. (((security_required & BTM_SEC_IN_FLAGS) == (BTM_SEC_IN_AUTHORIZE | BTM_SEC_IN_ENCRYPT)) && ((btm_dev_authorized(p_dev_rec) || btm_serv_trusted(p_dev_rec, p_serv_rec)) && btm_dev_encrypted(p_dev_rec))) ||
  2241. ((((security_required & BTM_SEC_IN_FLAGS) == (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_ENCRYPT)) && btm_dev_encrypted(p_dev_rec)))
  2242. ) {
  2243. // Check for 16 digits (or MITM)
  2244. if (((security_required & BTM_SEC_IN_MIN_16_DIGIT_PIN) == 0) ||
  2245. (((security_required & BTM_SEC_IN_MIN_16_DIGIT_PIN) == BTM_SEC_IN_MIN_16_DIGIT_PIN) && btm_dev_16_digit_authenticated(p_dev_rec))) {
  2246. rc = BTM_SUCCESS;
  2247. }
  2248. }
  2249. }
  2250. if ((rc == BTM_SUCCESS) && (security_required & BTM_SEC_MODE4_LEVEL4) &&
  2251. (p_dev_rec->link_key_type != BTM_LKEY_TYPE_AUTH_COMB_P_256)) {
  2252. rc = BTM_CMD_STARTED;
  2253. }
  2254. }
  2255. if (rc == BTM_SUCCESS) {
  2256. BTM_TRACE_EVENT("%s: allow to bypass, checking authorization\n", __FUNCTION__);
  2257. /* the security in BTM_SEC_IN_FLAGS is fullfilled so far, check the requirements in */
  2258. /* btm_sec_execute_procedure */
  2259. if ((is_originator && (p_serv_rec->security_flags & BTM_SEC_OUT_AUTHORIZE)) ||
  2260. (!is_originator && (p_serv_rec->security_flags & BTM_SEC_IN_AUTHORIZE))) {
  2261. BTM_TRACE_EVENT("%s: still need authorization\n", __FUNCTION__);
  2262. rc = BTM_CMD_STARTED;
  2263. }
  2264. }
  2265. /* Check whether there is a pending security procedure, if so we should always queue */
  2266. /* the new security request */
  2267. if (p_dev_rec->sec_state != BTM_SEC_STATE_IDLE) {
  2268. BTM_TRACE_EVENT("%s: There is a pending security procedure\n", __FUNCTION__);
  2269. rc = BTM_CMD_STARTED;
  2270. }
  2271. if (rc == BTM_CMD_STARTED) {
  2272. BTM_TRACE_EVENT("%s: call btm_sec_queue_mx_request\n", __FUNCTION__);
  2273. btm_sec_queue_mx_request (bd_addr, psm, is_originator, mx_proto_id,
  2274. mx_chan_id, p_callback, p_ref_data);
  2275. } else { /* rc == BTM_SUCCESS */
  2276. /* access granted */
  2277. if (p_callback) {
  2278. (*p_callback) (bd_addr, transport, p_ref_data, (UINT8)rc);
  2279. }
  2280. }
  2281. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  2282. BTM_TRACE_EVENT("%s: return with rc = 0x%02x in delayed state %s\n", __FUNCTION__, rc,
  2283. btm_pair_state_descr(btm_cb.pairing_state));
  2284. #endif ///BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE
  2285. return rc;
  2286. }
  2287. if ((!is_originator) && ((security_required & BTM_SEC_MODE4_LEVEL4) ||
  2288. (btm_cb.security_mode == BTM_SEC_MODE_SC))) {
  2289. BOOLEAN local_supports_sc = controller_get_interface()->supports_secure_connections();
  2290. /* acceptor receives service connection establishment Request for */
  2291. /* Secure Connections Only service */
  2292. if (!(local_supports_sc) || !(p_dev_rec->remote_supports_secure_connections)) {
  2293. BTM_TRACE_DEBUG("%s: SC only service,local_support_for_sc %d,\n"
  2294. "remote_support_for_sc %d: fail pairing\n", __FUNCTION__,
  2295. local_supports_sc, p_dev_rec->remote_supports_secure_connections);
  2296. if (p_callback) {
  2297. (*p_callback) (bd_addr, transport, (void *)p_ref_data,
  2298. BTM_MODE4_LEVEL4_NOT_SUPPORTED);
  2299. }
  2300. return (BTM_MODE4_LEVEL4_NOT_SUPPORTED);
  2301. }
  2302. }
  2303. p_dev_rec->p_cur_service = p_serv_rec;
  2304. p_dev_rec->security_required = security_required;
  2305. if (btm_cb.security_mode == BTM_SEC_MODE_SP ||
  2306. btm_cb.security_mode == BTM_SEC_MODE_SP_DEBUG ||
  2307. btm_cb.security_mode == BTM_SEC_MODE_SC) {
  2308. if (BTM_SEC_IS_SM4(p_dev_rec->sm4)) {
  2309. if ((p_dev_rec->security_required & BTM_SEC_MODE4_LEVEL4) &&
  2310. (p_dev_rec->link_key_type != BTM_LKEY_TYPE_AUTH_COMB_P_256)) {
  2311. /* BTM_LKEY_TYPE_AUTH_COMB_P_256 is the only acceptable key in this case */
  2312. if ((p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_KNOWN) != 0) {
  2313. p_dev_rec->sm4 |= BTM_SM4_UPGRADE;
  2314. }
  2315. p_dev_rec->sec_flags &= ~(BTM_SEC_LINK_KEY_KNOWN | BTM_SEC_LINK_KEY_AUTHED |
  2316. BTM_SEC_AUTHENTICATED);
  2317. BTM_TRACE_DEBUG("%s: sec_flags:0x%x\n", __FUNCTION__, p_dev_rec->sec_flags);
  2318. } else {
  2319. /* If we already have a link key, check if that link key is good enough */
  2320. btm_sec_check_upgrade(p_dev_rec, is_originator);
  2321. }
  2322. }
  2323. }
  2324. p_dev_rec->is_originator = is_originator;
  2325. p_dev_rec->p_callback = p_callback;
  2326. p_dev_rec->p_ref_data = p_ref_data;
  2327. /* Although authentication and encryption are per connection */
  2328. /* authorization is per access request. For example when serial connection */
  2329. /* is up and authorized and client requests to read file (access to other */
  2330. /* scn, we need to request user's permission again. */
  2331. p_dev_rec->sec_flags &= ~(BTM_SEC_AUTHORIZED);
  2332. BTM_TRACE_EVENT ("%s() proto_id:%d chan_id:%d State:%d Flags:0x%x Required:0x%x Service ID:%d\n",
  2333. __func__, mx_proto_id, mx_chan_id, p_dev_rec->sec_state, p_dev_rec->sec_flags,
  2334. p_dev_rec->security_required, p_dev_rec->p_cur_service->service_id);
  2335. if ((rc = btm_sec_execute_procedure (p_dev_rec)) != BTM_CMD_STARTED) {
  2336. if (p_callback) {
  2337. p_dev_rec->p_callback = NULL;
  2338. (*p_callback) (bd_addr, transport, p_ref_data, (UINT8)rc);
  2339. }
  2340. }
  2341. return rc;
  2342. #else
  2343. return BTM_MODE_UNSUPPORTED;
  2344. #endif ///SMP_INCLUDED == TRUE
  2345. }
  2346. /*******************************************************************************
  2347. **
  2348. ** Function btm_sec_conn_req
  2349. **
  2350. ** Description This function is when the peer device is requesting
  2351. ** connection
  2352. **
  2353. ** Returns void
  2354. **
  2355. *******************************************************************************/
  2356. #if (SMP_INCLUDED == TRUE)
  2357. void btm_sec_conn_req (UINT8 *bda, UINT8 *dc)
  2358. {
  2359. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bda);
  2360. /* Some device may request a connection before we are done with the HCI_Reset sequence */
  2361. if (!controller_get_interface()->get_is_ready()) {
  2362. BTM_TRACE_ERROR ("Security Manager: connect request when device not ready\n");
  2363. btsnd_hcic_reject_conn (bda, HCI_ERR_HOST_REJECT_DEVICE);
  2364. return;
  2365. }
  2366. /* Security guys wants us not to allow connection from not paired devices */
  2367. /* Check if connection is allowed for only paired devices */
  2368. if (btm_cb.connect_only_paired) {
  2369. if (!p_dev_rec || !(p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_AUTHED)) {
  2370. BTM_TRACE_ERROR ("Security Manager: connect request from non-paired device\n");
  2371. btsnd_hcic_reject_conn (bda, HCI_ERR_HOST_REJECT_DEVICE);
  2372. return;
  2373. }
  2374. }
  2375. #if BTM_ALLOW_CONN_IF_NONDISCOVER == FALSE
  2376. /* If non-discoverable, only allow known devices to connect */
  2377. if (btm_cb.btm_inq_vars.discoverable_mode == BTM_NON_DISCOVERABLE) {
  2378. if (!p_dev_rec) {
  2379. BTM_TRACE_ERROR ("Security Manager: connect request from not paired device\n");
  2380. btsnd_hcic_reject_conn (bda, HCI_ERR_HOST_REJECT_DEVICE);
  2381. return;
  2382. }
  2383. }
  2384. #endif
  2385. if ((btm_cb.pairing_state != BTM_PAIR_STATE_IDLE)
  2386. && (btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD)
  2387. && (!memcmp (btm_cb.pairing_bda, bda, BD_ADDR_LEN))) {
  2388. BTM_TRACE_ERROR ("Security Manager: reject connect request from bonding device\n");
  2389. /* incoming connection from bonding device is rejected */
  2390. btm_cb.pairing_flags |= BTM_PAIR_FLAGS_REJECTED_CONNECT;
  2391. btsnd_hcic_reject_conn (bda, HCI_ERR_HOST_REJECT_DEVICE);
  2392. return;
  2393. }
  2394. /* Host is not interested or approved connection. Save BDA and DC and */
  2395. /* pass request to L2CAP */
  2396. memcpy (btm_cb.connecting_bda, bda, BD_ADDR_LEN);
  2397. memcpy (btm_cb.connecting_dc, dc, DEV_CLASS_LEN);
  2398. if (l2c_link_hci_conn_req (bda)) {
  2399. if (!p_dev_rec) {
  2400. /* accept the connection -> allocate a device record */
  2401. p_dev_rec = btm_sec_alloc_dev (bda);
  2402. }
  2403. if (p_dev_rec) {
  2404. p_dev_rec->sm4 |= BTM_SM4_CONN_PEND;
  2405. }
  2406. }
  2407. }
  2408. #endif ///SMP_INCLUDED == TRUE
  2409. /*******************************************************************************
  2410. **
  2411. ** Function btm_sec_bond_cancel_complete
  2412. **
  2413. ** Description This function is called to report bond cancel complete
  2414. ** event.
  2415. **
  2416. ** Returns void
  2417. **
  2418. *******************************************************************************/
  2419. #if (SMP_INCLUDED == TRUE)
  2420. static void btm_sec_bond_cancel_complete (void)
  2421. {
  2422. tBTM_SEC_DEV_REC *p_dev_rec;
  2423. if ((btm_cb.pairing_flags & BTM_PAIR_FLAGS_DISC_WHEN_DONE) ||
  2424. (BTM_PAIR_STATE_WAIT_LOCAL_PIN == btm_cb.pairing_state &&
  2425. BTM_PAIR_FLAGS_WE_STARTED_DD & btm_cb.pairing_flags) ||
  2426. (btm_cb.pairing_state == BTM_PAIR_STATE_GET_REM_NAME &&
  2427. BTM_PAIR_FLAGS_WE_CANCEL_DD & btm_cb.pairing_flags)) {
  2428. /* for dedicated bonding in legacy mode, authentication happens at "link level"
  2429. * btm_sec_connected is called with failed status.
  2430. * In theory, the code that handles is_pairing_device/TRUE should clean out security related code.
  2431. * However, this function may clean out the security related flags and btm_sec_connected would not know
  2432. * this function also needs to do proper clean up.
  2433. */
  2434. if ((p_dev_rec = btm_find_dev (btm_cb.pairing_bda)) != NULL) {
  2435. p_dev_rec->security_required = BTM_SEC_NONE;
  2436. }
  2437. btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
  2438. /* Notify application that the cancel succeeded */
  2439. if (btm_cb.api.p_bond_cancel_cmpl_callback) {
  2440. btm_cb.api.p_bond_cancel_cmpl_callback(BTM_SUCCESS);
  2441. }
  2442. }
  2443. }
  2444. #endif ///SMP_INCLUDED == TRUE
  2445. /*******************************************************************************
  2446. **
  2447. ** Function btm_create_conn_cancel_complete
  2448. **
  2449. ** Description This function is called when the command complete message
  2450. ** is received from the HCI for the create connection cancel
  2451. ** command.
  2452. **
  2453. ** Returns void
  2454. **
  2455. *******************************************************************************/
  2456. void btm_create_conn_cancel_complete (UINT8 *p)
  2457. {
  2458. UINT8 status;
  2459. STREAM_TO_UINT8 (status, p);
  2460. //BTM_TRACE_EVENT ("btm_create_conn_cancel_complete(): in State: %s status:%d\n",
  2461. // btm_pair_state_descr(btm_cb.pairing_state), status);
  2462. /* if the create conn cancel cmd was issued by the bond cancel,
  2463. ** the application needs to be notified that bond cancel succeeded
  2464. */
  2465. switch (status) {
  2466. case HCI_SUCCESS:
  2467. #if (SMP_INCLUDED == TRUE)
  2468. btm_sec_bond_cancel_complete();
  2469. #endif ///SMP_INCLUDED == TRUE
  2470. break;
  2471. case HCI_ERR_CONNECTION_EXISTS:
  2472. case HCI_ERR_NO_CONNECTION:
  2473. default:
  2474. /* Notify application of the error */
  2475. if (btm_cb.api.p_bond_cancel_cmpl_callback) {
  2476. btm_cb.api.p_bond_cancel_cmpl_callback(BTM_ERR_PROCESSING);
  2477. }
  2478. break;
  2479. }
  2480. }
  2481. /*******************************************************************************
  2482. **
  2483. ** Function btm_sec_check_pending_reqs
  2484. **
  2485. ** Description This function is called at the end of the security procedure
  2486. ** to let L2CAP and RFCOMM know to re-submit any pending requests
  2487. **
  2488. ** Returns void
  2489. **
  2490. *******************************************************************************/
  2491. #if (SMP_INCLUDED == TRUE)
  2492. void btm_sec_check_pending_reqs (void)
  2493. {
  2494. tBTM_SEC_QUEUE_ENTRY *p_e;
  2495. fixed_queue_t *bq;
  2496. if (btm_cb.pairing_state == BTM_PAIR_STATE_IDLE) {
  2497. /* First, resubmit L2CAP requests */
  2498. if (btm_cb.sec_req_pending) {
  2499. btm_cb.sec_req_pending = FALSE;
  2500. #if (CLASSIC_BT_INCLUDED == TRUE)
  2501. l2cu_resubmit_pending_sec_req (NULL);
  2502. #endif ///SMP_INCLUDED == TRUE
  2503. }
  2504. /* Now, re-submit anything in the mux queue */
  2505. bq = btm_cb.sec_pending_q;
  2506. btm_cb.sec_pending_q = fixed_queue_new(QUEUE_SIZE_MAX);
  2507. while ((p_e = (tBTM_SEC_QUEUE_ENTRY *)fixed_queue_dequeue(bq, 0)) != NULL) {
  2508. /* Check that the ACL is still up before starting security procedures */
  2509. if (btm_bda_to_acl(p_e->bd_addr, p_e->transport) != NULL) {
  2510. if (p_e->psm != 0) {
  2511. BTM_TRACE_EVENT("%s PSM:0x%04x Is_Orig:%u mx_proto_id:%u mx_chan_id:%u\n",
  2512. __FUNCTION__, p_e->psm, p_e->is_orig,
  2513. p_e->mx_proto_id, p_e->mx_chan_id);
  2514. btm_sec_mx_access_request (p_e->bd_addr, p_e->psm, p_e->is_orig,
  2515. p_e->mx_proto_id, p_e->mx_chan_id,
  2516. p_e->p_callback, p_e->p_ref_data);
  2517. } else {
  2518. BTM_SetEncryption(p_e->bd_addr, p_e->transport, p_e->p_callback,
  2519. p_e->p_ref_data);
  2520. }
  2521. }
  2522. osi_free (p_e);
  2523. }
  2524. fixed_queue_free(bq, NULL);
  2525. }
  2526. }
  2527. #endif ///SMP_INCLUDED == TRUE
  2528. /*******************************************************************************
  2529. **
  2530. ** Function btm_sec_init
  2531. **
  2532. ** Description This function is on the SEC startup
  2533. **
  2534. ** Returns void
  2535. **
  2536. *******************************************************************************/
  2537. #if (SMP_INCLUDED == TRUE)
  2538. void btm_sec_init (UINT8 sec_mode)
  2539. {
  2540. btm_cb.security_mode = sec_mode;
  2541. memset (btm_cb.pairing_bda, 0xff, BD_ADDR_LEN);
  2542. btm_cb.max_collision_delay = BTM_SEC_MAX_COLLISION_DELAY;
  2543. }
  2544. #endif ///SMP_INCLUDED == TRUE
  2545. /*******************************************************************************
  2546. **
  2547. ** Function btm_sec_device_down
  2548. **
  2549. ** Description This function should be called when device is disabled or
  2550. ** turned off
  2551. **
  2552. ** Returns void
  2553. **
  2554. *******************************************************************************/
  2555. #if (SMP_INCLUDED == TRUE)
  2556. void btm_sec_device_down (void)
  2557. {
  2558. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  2559. BTM_TRACE_EVENT ("%s() State: %s\n", __func__, btm_pair_state_descr(btm_cb.pairing_state));
  2560. #endif ///BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE
  2561. btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
  2562. }
  2563. #endif ///SMP_INCLUDED == TRUE
  2564. /*******************************************************************************
  2565. **
  2566. ** Function btm_sec_dev_reset
  2567. **
  2568. ** Description This function should be called after device reset
  2569. **
  2570. ** Returns void
  2571. **
  2572. *******************************************************************************/
  2573. void btm_sec_dev_reset (void)
  2574. {
  2575. if (controller_get_interface()->supports_simple_pairing()) {
  2576. /* set the default IO capabilities */
  2577. btm_cb.devcb.loc_io_caps = BTM_LOCAL_IO_CAPS;
  2578. /* add mx service to use no security */
  2579. BTM_SetSecurityLevel(FALSE, "RFC_MUX\n", BTM_SEC_SERVICE_RFC_MUX,
  2580. BTM_SEC_NONE, BT_PSM_RFCOMM, BTM_SEC_PROTO_RFCOMM, 0);
  2581. } else {
  2582. btm_cb.security_mode = BTM_SEC_MODE_SERVICE;
  2583. }
  2584. BTM_TRACE_DEBUG ("btm_sec_dev_reset sec mode: %d\n", btm_cb.security_mode);
  2585. }
  2586. /*******************************************************************************
  2587. **
  2588. ** Function btm_sec_abort_access_req
  2589. **
  2590. ** Description This function is called by the L2CAP or RFCOMM to abort
  2591. ** the pending operation.
  2592. **
  2593. ** Parameters: bd_addr - Address of the peer device
  2594. **
  2595. ** Returns void
  2596. **
  2597. *******************************************************************************/
  2598. void btm_sec_abort_access_req (BD_ADDR bd_addr)
  2599. {
  2600. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
  2601. if (!p_dev_rec) {
  2602. return;
  2603. }
  2604. if ((p_dev_rec->sec_state != BTM_SEC_STATE_AUTHORIZING)
  2605. && (p_dev_rec->sec_state != BTM_SEC_STATE_AUTHENTICATING)) {
  2606. return;
  2607. }
  2608. p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
  2609. p_dev_rec->p_callback = NULL;
  2610. }
  2611. /*******************************************************************************
  2612. **
  2613. ** Function btm_sec_dd_create_conn
  2614. **
  2615. ** Description This function is called to create the ACL connection for
  2616. ** the dedicated boding process
  2617. **
  2618. ** Returns void
  2619. **
  2620. *******************************************************************************/
  2621. #if (SMP_INCLUDED == TRUE)
  2622. static tBTM_STATUS btm_sec_dd_create_conn (tBTM_SEC_DEV_REC *p_dev_rec)
  2623. {
  2624. tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(p_dev_rec->bd_addr, BT_TRANSPORT_BR_EDR);
  2625. if (p_lcb && (p_lcb->link_state == LST_CONNECTED || p_lcb->link_state == LST_CONNECTING)) {
  2626. BTM_TRACE_WARNING("%s Connection already exists\n", __func__);
  2627. return BTM_CMD_STARTED;
  2628. }
  2629. /* Make sure an L2cap link control block is available */
  2630. if (!p_lcb && (p_lcb = l2cu_allocate_lcb (p_dev_rec->bd_addr, TRUE, BT_TRANSPORT_BR_EDR)) == NULL) {
  2631. BTM_TRACE_WARNING ("Security Manager: failed allocate LCB [%02x%02x%02x%02x%02x%02x]\n",
  2632. p_dev_rec->bd_addr[0], p_dev_rec->bd_addr[1], p_dev_rec->bd_addr[2],
  2633. p_dev_rec->bd_addr[3], p_dev_rec->bd_addr[4], p_dev_rec->bd_addr[5]);
  2634. return (BTM_NO_RESOURCES);
  2635. }
  2636. /* set up the control block to indicated dedicated bonding */
  2637. btm_cb.pairing_flags |= BTM_PAIR_FLAGS_DISC_WHEN_DONE;
  2638. if (l2cu_create_conn(p_lcb, BT_TRANSPORT_BR_EDR) == FALSE) {
  2639. BTM_TRACE_WARNING ("Security Manager: failed create [%02x%02x%02x%02x%02x%02x]\n",
  2640. p_dev_rec->bd_addr[0], p_dev_rec->bd_addr[1], p_dev_rec->bd_addr[2],
  2641. p_dev_rec->bd_addr[3], p_dev_rec->bd_addr[4], p_dev_rec->bd_addr[5]);
  2642. l2cu_release_lcb(p_lcb);
  2643. return (BTM_NO_RESOURCES);
  2644. }
  2645. btm_acl_update_busy_level (BTM_BLI_PAGE_EVT);
  2646. BTM_TRACE_DEBUG ("Security Manager: btm_sec_dd_create_conn [%02x%02x%02x%02x%02x%02x]\n",
  2647. p_dev_rec->bd_addr[0], p_dev_rec->bd_addr[1], p_dev_rec->bd_addr[2],
  2648. p_dev_rec->bd_addr[3], p_dev_rec->bd_addr[4], p_dev_rec->bd_addr[5]);
  2649. btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_PIN_REQ);
  2650. return (BTM_CMD_STARTED);
  2651. }
  2652. #endif ///SMP_INCLUDED == TRUE
  2653. /*******************************************************************************
  2654. **
  2655. ** Function btm_sec_rmt_name_request_complete
  2656. **
  2657. ** Description This function is called when remote name was obtained from
  2658. ** the peer device
  2659. **
  2660. ** Returns void
  2661. **
  2662. *******************************************************************************/
  2663. #if (SMP_INCLUDED == TRUE)
  2664. void btm_sec_rmt_name_request_complete (UINT8 *p_bd_addr, UINT8 *p_bd_name, UINT8 status)
  2665. {
  2666. tBTM_SEC_DEV_REC *p_dev_rec = NULL;
  2667. int i;
  2668. DEV_CLASS dev_class;
  2669. UINT8 old_sec_state;
  2670. UINT8 res;
  2671. BTM_TRACE_EVENT ("btm_sec_rmt_name_request_complete\n");
  2672. if (((p_bd_addr == NULL) && !BTM_ACL_IS_CONNECTED(btm_cb.connecting_bda))
  2673. || ((p_bd_addr != NULL) && !BTM_ACL_IS_CONNECTED(p_bd_addr))) {
  2674. btm_acl_resubmit_page();
  2675. }
  2676. /* If remote name request failed, p_bd_addr is null and we need to search */
  2677. /* based on state assuming that we are doing 1 at a time */
  2678. if (p_bd_addr) {
  2679. p_dev_rec = btm_find_dev (p_bd_addr);
  2680. } else {
  2681. list_node_t *p_node = NULL;
  2682. for (p_node = list_begin(btm_cb.p_sec_dev_rec_list); p_node; p_node = list_next(p_node)) {
  2683. p_dev_rec = list_node(p_node);
  2684. if ((p_dev_rec->sec_flags & BTM_SEC_IN_USE)
  2685. && (p_dev_rec->sec_state == BTM_SEC_STATE_GETTING_NAME)) {
  2686. p_bd_addr = p_dev_rec->bd_addr;
  2687. break;
  2688. }
  2689. }
  2690. if (!p_bd_addr) {
  2691. p_dev_rec = NULL;
  2692. }
  2693. }
  2694. /* Commenting out trace due to obf/compilation problems.
  2695. */
  2696. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  2697. if (!p_bd_name) {
  2698. p_bd_name = (UINT8 *)"";
  2699. }
  2700. if (p_dev_rec) {
  2701. BTM_TRACE_EVENT ("Security Manager: rmt_name_complete PairState: %s RemName: %s status: %d State:%d p_dev_rec: %p \n",
  2702. btm_pair_state_descr (btm_cb.pairing_state), p_bd_name,
  2703. status, p_dev_rec->sec_state, p_dev_rec);
  2704. } else {
  2705. BTM_TRACE_EVENT ("Security Manager: rmt_name_complete PairState: %s RemName: %s status: %d\n",
  2706. btm_pair_state_descr (btm_cb.pairing_state), p_bd_name,
  2707. status);
  2708. }
  2709. #endif
  2710. if (p_dev_rec) {
  2711. old_sec_state = p_dev_rec->sec_state;
  2712. if (status == HCI_SUCCESS) {
  2713. BCM_STRNCPY_S ((char *)p_dev_rec->sec_bd_name, (char *)p_bd_name, BTM_MAX_REM_BD_NAME_LEN);
  2714. p_dev_rec->sec_bd_name[BTM_MAX_REM_BD_NAME_LEN] = '\0';
  2715. p_dev_rec->sec_flags |= BTM_SEC_NAME_KNOWN;
  2716. BTM_TRACE_EVENT ("setting BTM_SEC_NAME_KNOWN sec_flags:0x%x\n", p_dev_rec->sec_flags);
  2717. } else {
  2718. /* Notify all clients waiting for name to be resolved even if it failed so clients can continue */
  2719. p_dev_rec->sec_bd_name[0] = '\0';
  2720. }
  2721. if (p_dev_rec->sec_state == BTM_SEC_STATE_GETTING_NAME) {
  2722. p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
  2723. }
  2724. /* Notify all clients waiting for name to be resolved */
  2725. for (i = 0; i < BTM_SEC_MAX_RMT_NAME_CALLBACKS; i++) {
  2726. if (btm_cb.p_rmt_name_callback[i] && p_bd_addr) {
  2727. (*btm_cb.p_rmt_name_callback[i])(p_bd_addr, p_dev_rec->dev_class,
  2728. p_dev_rec->sec_bd_name);
  2729. }
  2730. }
  2731. } else {
  2732. dev_class[0] = 0;
  2733. dev_class[1] = 0;
  2734. dev_class[2] = 0;
  2735. /* Notify all clients waiting for name to be resolved even if not found so clients can continue */
  2736. for (i = 0; i < BTM_SEC_MAX_RMT_NAME_CALLBACKS; i++) {
  2737. if (btm_cb.p_rmt_name_callback[i] && p_bd_addr) {
  2738. (*btm_cb.p_rmt_name_callback[i])(p_bd_addr, dev_class, (UINT8 *)"");
  2739. }
  2740. }
  2741. return;
  2742. }
  2743. /* If we were delaying asking UI for a PIN because name was not resolved, ask now */
  2744. if ( (btm_cb.pairing_state == BTM_PAIR_STATE_WAIT_LOCAL_PIN) && p_bd_addr
  2745. && (memcmp (btm_cb.pairing_bda, p_bd_addr, BD_ADDR_LEN) == 0) ) {
  2746. BTM_TRACE_EVENT ("btm_sec_rmt_name_request_complete() delayed pin now being requested flags:0x%x, (p_pin_callback=%p)\n", btm_cb.pairing_flags, btm_cb.api.p_pin_callback);
  2747. if (((btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD) == 0) &&
  2748. ((btm_cb.pairing_flags & BTM_PAIR_FLAGS_PIN_REQD) == 0) &&
  2749. btm_cb.api.p_pin_callback) {
  2750. BTM_TRACE_EVENT ("btm_sec_rmt_name_request_complete() calling pin_callback\n");
  2751. btm_cb.pairing_flags |= BTM_PAIR_FLAGS_PIN_REQD;
  2752. (*btm_cb.api.p_pin_callback) (p_dev_rec->bd_addr, p_dev_rec->dev_class, p_bd_name,
  2753. (p_dev_rec->p_cur_service == NULL) ? FALSE
  2754. : (p_dev_rec->p_cur_service->security_flags & BTM_SEC_IN_MIN_16_DIGIT_PIN));
  2755. }
  2756. /* Set the same state again to force the timer to be restarted */
  2757. btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_LOCAL_PIN);
  2758. return;
  2759. }
  2760. /* Check if we were delaying bonding because name was not resolved */
  2761. if ( btm_cb.pairing_state == BTM_PAIR_STATE_GET_REM_NAME) {
  2762. if (p_bd_addr && memcmp (btm_cb.pairing_bda, p_bd_addr, BD_ADDR_LEN) == 0) {
  2763. BTM_TRACE_EVENT ("btm_sec_rmt_name_request_complete() continue bonding sm4: 0x%04x, status:0x%x\n", p_dev_rec->sm4, status);
  2764. if (btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_CANCEL_DD) {
  2765. btm_sec_bond_cancel_complete();
  2766. return;
  2767. }
  2768. if (status != HCI_SUCCESS) {
  2769. btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
  2770. if (btm_cb.api.p_auth_complete_callback)
  2771. (*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr, p_dev_rec->dev_class,
  2772. p_dev_rec->sec_bd_name, status);
  2773. return;
  2774. }
  2775. /* if peer is very old legacy devices, HCI_RMT_HOST_SUP_FEAT_NOTIFY_EVT is not reported */
  2776. if (BTM_SEC_IS_SM4_UNKNOWN(p_dev_rec->sm4)) {
  2777. /* set the KNOWN flag only if BTM_PAIR_FLAGS_REJECTED_CONNECT is not set.*/
  2778. /* If it is set, there may be a race condition */
  2779. BTM_TRACE_DEBUG ("btm_sec_rmt_name_request_complete IS_SM4_UNKNOWN Flags:0x%04x\n",
  2780. btm_cb.pairing_flags);
  2781. if ((btm_cb.pairing_flags & BTM_PAIR_FLAGS_REJECTED_CONNECT) == 0) {
  2782. p_dev_rec->sm4 |= BTM_SM4_KNOWN;
  2783. }
  2784. }
  2785. BTM_TRACE_DEBUG("%s, SM4 Value: %x, Legacy:%d,IS SM4:%d, Unknown:%d\n", __FUNCTION__,
  2786. p_dev_rec->sm4, BTM_SEC_IS_SM4_LEGACY(p_dev_rec->sm4),
  2787. BTM_SEC_IS_SM4(p_dev_rec->sm4), BTM_SEC_IS_SM4_UNKNOWN(p_dev_rec->sm4));
  2788. /* BT 2.1 or carkit, bring up the connection to force the peer to request PIN.
  2789. ** Else prefetch (btm_sec_check_prefetch_pin will do the prefetching if needed)
  2790. */
  2791. if ((p_dev_rec->sm4 != BTM_SM4_KNOWN) || !btm_sec_check_prefetch_pin(p_dev_rec)) {
  2792. /* if we rejected incoming connection request, we have to wait HCI_Connection_Complete event */
  2793. /* before originating */
  2794. if (btm_cb.pairing_flags & BTM_PAIR_FLAGS_REJECTED_CONNECT) {
  2795. BTM_TRACE_WARNING ("btm_sec_rmt_name_request_complete: waiting HCI_Connection_Complete after rejecting connection\n");
  2796. }
  2797. /* Both we and the peer are 2.1 - continue to create connection */
  2798. else if (btm_sec_dd_create_conn(p_dev_rec) != BTM_CMD_STARTED) {
  2799. BTM_TRACE_WARNING ("btm_sec_rmt_name_request_complete: failed to start connection\n");
  2800. btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
  2801. if (btm_cb.api.p_auth_complete_callback) {
  2802. (*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr, p_dev_rec->dev_class,
  2803. p_dev_rec->sec_bd_name, HCI_ERR_MEMORY_FULL);
  2804. }
  2805. }
  2806. }
  2807. return;
  2808. } else {
  2809. BTM_TRACE_WARNING ("btm_sec_rmt_name_request_complete: wrong BDA, retry with pairing BDA\n");
  2810. BTM_ReadRemoteDeviceName (btm_cb.pairing_bda, NULL, BT_TRANSPORT_BR_EDR);
  2811. return;
  2812. }
  2813. }
  2814. /* check if we were delaying link_key_callback because name was not resolved */
  2815. if (p_dev_rec->link_key_not_sent) {
  2816. /* If HCI connection complete has not arrived, wait for it */
  2817. if (p_dev_rec->hci_handle == BTM_SEC_INVALID_HANDLE) {
  2818. return;
  2819. }
  2820. p_dev_rec->link_key_not_sent = FALSE;
  2821. btm_send_link_key_notif(p_dev_rec);
  2822. /* If its not us who perform authentication, we should tell stackserver */
  2823. /* that some authentication has been completed */
  2824. /* This is required when different entities receive link notification and auth complete */
  2825. if (!(p_dev_rec->security_required & BTM_SEC_OUT_AUTHENTICATE)) {
  2826. if (btm_cb.api.p_auth_complete_callback) {
  2827. res = (*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr,
  2828. p_dev_rec->dev_class,
  2829. p_dev_rec->sec_bd_name, HCI_SUCCESS);
  2830. if (res == BTM_SEC_DEV_REC_REMOVED) {
  2831. p_dev_rec = NULL;
  2832. }
  2833. }
  2834. }
  2835. }
  2836. if(!p_dev_rec) {
  2837. return;
  2838. }
  2839. /* If this is a bonding procedure can disconnect the link now */
  2840. if ((btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD)
  2841. && (p_dev_rec->sec_flags & BTM_SEC_AUTHENTICATED)) {
  2842. BTM_TRACE_WARNING ("btm_sec_rmt_name_request_complete (none/ce)\n");
  2843. p_dev_rec->security_required &= ~(BTM_SEC_OUT_AUTHENTICATE);
  2844. l2cu_start_post_bond_timer(p_dev_rec->hci_handle);
  2845. return;
  2846. }
  2847. if (old_sec_state != BTM_SEC_STATE_GETTING_NAME) {
  2848. return;
  2849. }
  2850. /* If get name failed, notify the waiting layer */
  2851. if (status != HCI_SUCCESS) {
  2852. btm_sec_dev_rec_cback_event (p_dev_rec, BTM_ERR_PROCESSING, FALSE);
  2853. return;
  2854. }
  2855. if (p_dev_rec->sm4 & BTM_SM4_REQ_PEND) {
  2856. BTM_TRACE_EVENT ("waiting for remote features!!\n");
  2857. return;
  2858. }
  2859. /* Remote Name succeeded, execute the next security procedure, if any */
  2860. status = (UINT8)btm_sec_execute_procedure (p_dev_rec);
  2861. /* If result is pending reply from the user or from the device is pending */
  2862. if (status == BTM_CMD_STARTED) {
  2863. return;
  2864. }
  2865. /* There is no next procedure or start of procedure failed, notify the waiting layer */
  2866. btm_sec_dev_rec_cback_event (p_dev_rec, status, FALSE);
  2867. }
  2868. #endif ///SMP_INCLUDED == TRUE
  2869. /*******************************************************************************
  2870. **
  2871. ** Function btm_sec_rmt_host_support_feat_evt
  2872. **
  2873. ** Description This function is called when the
  2874. ** HCI_RMT_HOST_SUP_FEAT_NOTIFY_EVT is received
  2875. **
  2876. ** Returns void
  2877. **
  2878. *******************************************************************************/
  2879. void btm_sec_rmt_host_support_feat_evt (UINT8 *p)
  2880. {
  2881. tBTM_SEC_DEV_REC *p_dev_rec;
  2882. BD_ADDR bd_addr; /* peer address */
  2883. BD_FEATURES features;
  2884. STREAM_TO_BDADDR (bd_addr, p);
  2885. p_dev_rec = btm_find_or_alloc_dev (bd_addr);
  2886. BTM_TRACE_EVENT ("btm_sec_rmt_host_support_feat_evt sm4: 0x%x p[0]: 0x%x\n", p_dev_rec->sm4, p[0]);
  2887. if (BTM_SEC_IS_SM4_UNKNOWN(p_dev_rec->sm4)) {
  2888. p_dev_rec->sm4 = BTM_SM4_KNOWN;
  2889. STREAM_TO_ARRAY(features, p, HCI_FEATURE_BYTES_PER_PAGE);
  2890. if (HCI_SSP_HOST_SUPPORTED(features)) {
  2891. p_dev_rec->sm4 = BTM_SM4_TRUE;
  2892. }
  2893. BTM_TRACE_EVENT ("btm_sec_rmt_host_support_feat_evt sm4: 0x%x features[0]: 0x%x\n", p_dev_rec->sm4, features[0]);
  2894. }
  2895. }
  2896. /*******************************************************************************
  2897. **
  2898. ** Function btm_io_capabilities_req
  2899. **
  2900. ** Description This function is called when LM request for the IO
  2901. ** capability of the local device and
  2902. ** if the OOB data is present for the device in the event
  2903. **
  2904. ** Returns void
  2905. **
  2906. *******************************************************************************/
  2907. #if (SMP_INCLUDED == TRUE)
  2908. void btm_io_capabilities_req (UINT8 *p)
  2909. {
  2910. tBTM_SP_IO_REQ evt_data;
  2911. UINT8 err_code = 0;
  2912. tBTM_SEC_DEV_REC *p_dev_rec;
  2913. BOOLEAN is_orig = TRUE;
  2914. UINT8 callback_rc = BTM_SUCCESS;
  2915. STREAM_TO_BDADDR (evt_data.bd_addr, p);
  2916. /* setup the default response according to compile options */
  2917. /* assume that the local IO capability does not change
  2918. * loc_io_caps is initialized with the default value */
  2919. evt_data.io_cap = btm_cb.devcb.loc_io_caps;
  2920. evt_data.oob_data = BTM_OOB_NONE;
  2921. evt_data.auth_req = BTM_DEFAULT_AUTH_REQ;
  2922. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  2923. BTM_TRACE_EVENT("%s: State: %s\n", __FUNCTION__, btm_pair_state_descr(btm_cb.pairing_state));
  2924. #endif ///BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE
  2925. p_dev_rec = btm_find_or_alloc_dev (evt_data.bd_addr);
  2926. BTM_TRACE_DEBUG("%s:Security mode: %d, Num Read Remote Feat pages: %d\n", __FUNCTION__,
  2927. btm_cb.security_mode, p_dev_rec->num_read_pages);
  2928. if ((btm_cb.security_mode == BTM_SEC_MODE_SC) && (p_dev_rec->num_read_pages == 0)) {
  2929. BTM_TRACE_EVENT("%s: Device security mode is SC only.\n"
  2930. "To continue need to know remote features.\n", __FUNCTION__);
  2931. p_dev_rec->remote_features_needed = TRUE;
  2932. return;
  2933. }
  2934. p_dev_rec->sm4 |= BTM_SM4_TRUE;
  2935. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  2936. BTM_TRACE_EVENT("%s: State: %s Flags: 0x%04x p_cur_service: %p\n",
  2937. __FUNCTION__, btm_pair_state_descr(btm_cb.pairing_state),
  2938. btm_cb.pairing_flags, p_dev_rec->p_cur_service);
  2939. #endif ///BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE
  2940. if (p_dev_rec->p_cur_service) {
  2941. BTM_TRACE_EVENT("%s: cur_service psm: 0x%04x, security_flags: 0x%04x\n",
  2942. __FUNCTION__, p_dev_rec->p_cur_service->psm,
  2943. p_dev_rec->p_cur_service->security_flags);
  2944. }
  2945. switch (btm_cb.pairing_state) {
  2946. /* initiator connecting */
  2947. case BTM_PAIR_STATE_IDLE:
  2948. //TODO: Handle Idle pairing state
  2949. //security_required = p_dev_rec->security_required;
  2950. break;
  2951. /* received IO capability response already->acceptor */
  2952. case BTM_PAIR_STATE_INCOMING_SSP:
  2953. is_orig = FALSE;
  2954. if (btm_cb.pairing_flags & BTM_PAIR_FLAGS_PEER_STARTED_DD) {
  2955. /* acceptor in dedicated bonding */
  2956. evt_data.auth_req = BTM_DEFAULT_DD_AUTH_REQ;
  2957. }
  2958. break;
  2959. /* initiator, at this point it is expected to be dedicated bonding
  2960. initiated by local device */
  2961. case BTM_PAIR_STATE_WAIT_PIN_REQ:
  2962. if (!memcmp (evt_data.bd_addr, btm_cb.pairing_bda, BD_ADDR_LEN)) {
  2963. evt_data.auth_req = BTM_DEFAULT_DD_AUTH_REQ;
  2964. } else {
  2965. err_code = HCI_ERR_HOST_BUSY_PAIRING;
  2966. }
  2967. break;
  2968. /* any other state is unexpected */
  2969. default:
  2970. err_code = HCI_ERR_HOST_BUSY_PAIRING;
  2971. BTM_TRACE_ERROR("%s: Unexpected Pairing state received %d\n", __FUNCTION__,
  2972. btm_cb.pairing_state);
  2973. break;
  2974. }
  2975. if (btm_cb.pairing_disabled) {
  2976. /* pairing is not allowed */
  2977. BTM_TRACE_DEBUG("%s: Pairing is not allowed -> fail pairing.\n", __FUNCTION__);
  2978. err_code = HCI_ERR_PAIRING_NOT_ALLOWED;
  2979. } else if (btm_cb.security_mode == BTM_SEC_MODE_SC) {
  2980. BOOLEAN local_supports_sc = controller_get_interface()->supports_secure_connections();
  2981. /* device in Secure Connections Only mode */
  2982. if (!(local_supports_sc) || !(p_dev_rec->remote_supports_secure_connections)) {
  2983. BTM_TRACE_DEBUG("%s: SC only service, local_support_for_sc %d,\n"
  2984. " remote_support_for_sc 0x%02x -> fail pairing\n", __FUNCTION__,
  2985. local_supports_sc, p_dev_rec->remote_supports_secure_connections);
  2986. err_code = HCI_ERR_PAIRING_NOT_ALLOWED;
  2987. }
  2988. }
  2989. if (err_code != 0) {
  2990. /* coverity[uninit_use_in_call]
  2991. Event uninit_use_in_call: Using uninitialized element of array "evt_data.bd_addr" in call to function "memcmp"
  2992. False-positive: evt_data.bd_addr is set at the beginning with: STREAM_TO_BDADDR (evt_data.bd_addr, p);
  2993. */
  2994. btsnd_hcic_io_cap_req_neg_reply(evt_data.bd_addr, err_code);
  2995. return;
  2996. }
  2997. evt_data.is_orig = is_orig;
  2998. if (is_orig) {
  2999. /* local device initiated the pairing non-bonding -> use p_cur_service */
  3000. if (!(btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD) &&
  3001. p_dev_rec->p_cur_service &&
  3002. (p_dev_rec->p_cur_service->security_flags & BTM_SEC_OUT_AUTHENTICATE)) {
  3003. if (btm_cb.security_mode == BTM_SEC_MODE_SC) {
  3004. /* SC only mode device requires MITM protection */
  3005. evt_data.auth_req = BTM_AUTH_SP_YES;
  3006. } else {
  3007. evt_data.auth_req = (p_dev_rec->p_cur_service->security_flags &
  3008. BTM_SEC_OUT_MITM) ? BTM_AUTH_SP_YES : BTM_AUTH_SP_NO;
  3009. }
  3010. }
  3011. }
  3012. /* Notify L2CAP to increase timeout */
  3013. l2c_pin_code_request (evt_data.bd_addr);
  3014. memcpy (btm_cb.pairing_bda, evt_data.bd_addr, BD_ADDR_LEN);
  3015. /* coverity[uninit_use_in_call]
  3016. Event uninit_use_in_call: Using uninitialized element of array "evt_data.bd_addr" in call to function "memcmp"
  3017. False-positive: False-positive: evt_data.bd_addr is set at the beginning with: STREAM_TO_BDADDR (evt_data.bd_addr, p);
  3018. */
  3019. if (!memcmp (evt_data.bd_addr, btm_cb.connecting_bda, BD_ADDR_LEN)) {
  3020. memcpy (p_dev_rec->dev_class, btm_cb.connecting_dc, DEV_CLASS_LEN);
  3021. }
  3022. btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_LOCAL_IOCAPS);
  3023. callback_rc = BTM_SUCCESS;
  3024. if (p_dev_rec->sm4 & BTM_SM4_UPGRADE) {
  3025. p_dev_rec->sm4 &= ~BTM_SM4_UPGRADE;
  3026. /* link key upgrade: always use SPGB_YES - assuming we want to save the link key */
  3027. evt_data.auth_req = BTM_AUTH_SPGB_YES;
  3028. } else if (btm_cb.api.p_sp_callback) {
  3029. /* the callback function implementation may change the IO capability... */
  3030. callback_rc = (*btm_cb.api.p_sp_callback) (BTM_SP_IO_REQ_EVT, (tBTM_SP_EVT_DATA *)&evt_data);
  3031. }
  3032. #if BTM_OOB_INCLUDED == TRUE
  3033. if ((callback_rc == BTM_SUCCESS) || (BTM_OOB_UNKNOWN != evt_data.oob_data))
  3034. #else
  3035. if (callback_rc == BTM_SUCCESS)
  3036. #endif
  3037. {
  3038. if ((btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD)) {
  3039. evt_data.auth_req = (BTM_AUTH_DD_BOND | (evt_data.auth_req & BTM_AUTH_YN_BIT));
  3040. }
  3041. if (btm_cb.security_mode == BTM_SEC_MODE_SC) {
  3042. /* At this moment we know that both sides are SC capable, device in */
  3043. /* SC only mode requires MITM for any service so let's set MITM bit */
  3044. evt_data.auth_req |= BTM_AUTH_YN_BIT;
  3045. BTM_TRACE_DEBUG("%s: for device in \"SC only\" mode set auth_req to 0x%02x\n",
  3046. __FUNCTION__, evt_data.auth_req);
  3047. }
  3048. /* if the user does not indicate "reply later" by setting the oob_data to unknown */
  3049. /* send the response right now. Save the current IO capability in the control block */
  3050. btm_cb.devcb.loc_auth_req = evt_data.auth_req;
  3051. btm_cb.devcb.loc_io_caps = evt_data.io_cap;
  3052. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  3053. BTM_TRACE_EVENT("%s: State: %s IO_CAP:%d oob_data:%d auth_req:%d",
  3054. __FUNCTION__, btm_pair_state_descr(btm_cb.pairing_state), evt_data.io_cap,
  3055. evt_data.oob_data, evt_data.auth_req);
  3056. #endif ///BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE
  3057. btsnd_hcic_io_cap_req_reply(evt_data.bd_addr, evt_data.io_cap,
  3058. evt_data.oob_data, evt_data.auth_req);
  3059. }
  3060. }
  3061. /*******************************************************************************
  3062. **
  3063. ** Function btm_io_capabilities_rsp
  3064. **
  3065. ** Description This function is called when the IO capability of the
  3066. ** specified device is received
  3067. **
  3068. ** Returns void
  3069. **
  3070. *******************************************************************************/
  3071. void btm_io_capabilities_rsp (UINT8 *p)
  3072. {
  3073. tBTM_SEC_DEV_REC *p_dev_rec;
  3074. tBTM_SP_IO_RSP evt_data;
  3075. STREAM_TO_BDADDR (evt_data.bd_addr, p);
  3076. STREAM_TO_UINT8 (evt_data.io_cap, p);
  3077. STREAM_TO_UINT8 (evt_data.oob_data, p);
  3078. STREAM_TO_UINT8 (evt_data.auth_req, p);
  3079. /* Allocate a new device record or reuse the oldest one */
  3080. p_dev_rec = btm_find_or_alloc_dev (evt_data.bd_addr);
  3081. /* If no security is in progress, this indicates incoming security */
  3082. if (btm_cb.pairing_state == BTM_PAIR_STATE_IDLE) {
  3083. memcpy (btm_cb.pairing_bda, evt_data.bd_addr, BD_ADDR_LEN);
  3084. btm_sec_change_pairing_state (BTM_PAIR_STATE_INCOMING_SSP);
  3085. /* Make sure we reset the trusted mask to help against attacks */
  3086. BTM_SEC_CLR_TRUSTED_DEVICE(p_dev_rec->trusted_mask);
  3087. /* work around for FW bug */
  3088. btm_inq_stop_on_ssp();
  3089. }
  3090. /* Notify L2CAP to increase timeout */
  3091. l2c_pin_code_request (evt_data.bd_addr);
  3092. /* We must have a device record here.
  3093. * Use the connecting device's CoD for the connection */
  3094. /* coverity[uninit_use_in_call]
  3095. Event uninit_use_in_call: Using uninitialized element of array "evt_data.bd_addr" in call to function "memcmp"
  3096. FALSE-POSITIVE error from Coverity test-tool. evt_data.bd_addr is set at the beginning with: STREAM_TO_BDADDR (evt_data.bd_addr, p);
  3097. */
  3098. if (!memcmp (evt_data.bd_addr, btm_cb.connecting_bda, BD_ADDR_LEN)) {
  3099. memcpy (p_dev_rec->dev_class, btm_cb.connecting_dc, DEV_CLASS_LEN);
  3100. }
  3101. /* peer sets dedicated bonding bit and we did not initiate dedicated bonding */
  3102. if (btm_cb.pairing_state == BTM_PAIR_STATE_INCOMING_SSP /* peer initiated bonding */
  3103. && (evt_data.auth_req & BTM_AUTH_DD_BOND) ) { /* and dedicated bonding bit is set */
  3104. btm_cb.pairing_flags |= BTM_PAIR_FLAGS_PEER_STARTED_DD;
  3105. }
  3106. /* save the IO capability in the device record */
  3107. p_dev_rec->rmt_io_caps = evt_data.io_cap;
  3108. p_dev_rec->rmt_auth_req = evt_data.auth_req;
  3109. if (btm_cb.api.p_sp_callback) {
  3110. (*btm_cb.api.p_sp_callback) (BTM_SP_IO_RSP_EVT, (tBTM_SP_EVT_DATA *)&evt_data);
  3111. }
  3112. }
  3113. /*******************************************************************************
  3114. **
  3115. ** Function btm_proc_sp_req_evt
  3116. **
  3117. ** Description This function is called to process/report
  3118. ** HCI_USER_CONFIRMATION_REQUEST_EVT
  3119. ** or HCI_USER_PASSKEY_REQUEST_EVT
  3120. ** or HCI_USER_PASSKEY_NOTIFY_EVT
  3121. **
  3122. ** Returns void
  3123. **
  3124. *******************************************************************************/
  3125. void btm_proc_sp_req_evt (tBTM_SP_EVT event, UINT8 *p)
  3126. {
  3127. tBTM_STATUS status = BTM_ERR_PROCESSING;
  3128. tBTM_SP_EVT_DATA evt_data;
  3129. UINT8 *p_bda = evt_data.cfm_req.bd_addr;
  3130. tBTM_SEC_DEV_REC *p_dev_rec;
  3131. /* All events start with bd_addr */
  3132. STREAM_TO_BDADDR (p_bda, p);
  3133. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  3134. BTM_TRACE_EVENT ("btm_proc_sp_req_evt() BDA: %08x%04x event: 0x%x, State: %s\n",
  3135. (p_bda[0] << 24) + (p_bda[1] << 16) + (p_bda[2] << 8) + p_bda[3], (p_bda[4] << 8) + p_bda[5],
  3136. event, btm_pair_state_descr(btm_cb.pairing_state));
  3137. #endif ///BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE
  3138. if ( ((p_dev_rec = btm_find_dev (p_bda)) != NULL)
  3139. && (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE)
  3140. && (memcmp (btm_cb.pairing_bda, p_bda, BD_ADDR_LEN) == 0) ) {
  3141. memcpy (evt_data.cfm_req.bd_addr, p_dev_rec->bd_addr, BD_ADDR_LEN);
  3142. memcpy (evt_data.cfm_req.dev_class, p_dev_rec->dev_class, DEV_CLASS_LEN);
  3143. BCM_STRCPY_S ((char *)evt_data.cfm_req.bd_name,(char *)p_dev_rec->sec_bd_name);
  3144. switch (event) {
  3145. case BTM_SP_CFM_REQ_EVT:
  3146. /* Numeric confirmation. Need user to conf the passkey */
  3147. btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_NUMERIC_CONFIRM);
  3148. /* The device record must be allocated in the "IO cap exchange" step */
  3149. STREAM_TO_UINT32 (evt_data.cfm_req.num_val, p);
  3150. evt_data.cfm_req.just_works = TRUE;
  3151. /* process user confirm req in association with the auth_req param */
  3152. // #if (BTM_LOCAL_IO_CAPS == BTM_IO_CAP_IO)
  3153. if ( (p_dev_rec->rmt_io_caps == BTM_IO_CAP_IO)
  3154. && (btm_cb.devcb.loc_io_caps == BTM_IO_CAP_IO)
  3155. && ((p_dev_rec->rmt_auth_req & BTM_AUTH_SP_YES) || (btm_cb.devcb.loc_auth_req & BTM_AUTH_SP_YES)) ) {
  3156. /* Both devices are DisplayYesNo and one or both devices want to authenticate
  3157. -> use authenticated link key */
  3158. evt_data.cfm_req.just_works = FALSE;
  3159. }
  3160. // #endif
  3161. BTM_TRACE_DEBUG ("btm_proc_sp_req_evt() just_works:%d, io loc:%d, rmt:%d, auth loc:%d, rmt:%d\n",
  3162. evt_data.cfm_req.just_works, btm_cb.devcb.loc_io_caps, p_dev_rec->rmt_io_caps,
  3163. btm_cb.devcb.loc_auth_req, p_dev_rec->rmt_auth_req);
  3164. evt_data.cfm_req.loc_auth_req = btm_cb.devcb.loc_auth_req;
  3165. evt_data.cfm_req.rmt_auth_req = p_dev_rec->rmt_auth_req;
  3166. evt_data.cfm_req.loc_io_caps = btm_cb.devcb.loc_io_caps;
  3167. evt_data.cfm_req.rmt_io_caps = p_dev_rec->rmt_io_caps;
  3168. break;
  3169. case BTM_SP_KEY_NOTIF_EVT:
  3170. /* Passkey notification (other side is a keyboard) */
  3171. STREAM_TO_UINT32 (evt_data.key_notif.passkey, p);
  3172. BTM_TRACE_DEBUG ("BTM_SP_KEY_NOTIF_EVT: passkey: %u\n", evt_data.key_notif.passkey);
  3173. btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_AUTH_COMPLETE);
  3174. break;
  3175. #if (BT_SSP_INCLUDED == TRUE)
  3176. case BTM_SP_KEY_REQ_EVT:
  3177. /* HCI_USER_PASSKEY_REQUEST_EVT */
  3178. btm_sec_change_pairing_state (BTM_PAIR_STATE_KEY_ENTRY);
  3179. break;
  3180. #endif
  3181. }
  3182. if (btm_cb.api.p_sp_callback) {
  3183. status = (*btm_cb.api.p_sp_callback) (event, (tBTM_SP_EVT_DATA *)&evt_data);
  3184. if (status != BTM_NOT_AUTHORIZED) {
  3185. return;
  3186. }
  3187. /* else BTM_NOT_AUTHORIZED means when the app wants to reject the req right now */
  3188. } else if ( (event == BTM_SP_CFM_REQ_EVT) && (evt_data.cfm_req.just_works == TRUE) ) {
  3189. /* automatically reply with just works if no sp_cback */
  3190. status = BTM_SUCCESS;
  3191. }
  3192. if (event == BTM_SP_CFM_REQ_EVT) {
  3193. BTM_TRACE_DEBUG ("calling BTM_ConfirmReqReply with status: %d\n", status);
  3194. BTM_ConfirmReqReply (status, p_bda);
  3195. }
  3196. #if (BT_SSP_INCLUDED == TRUE)
  3197. else if (event == BTM_SP_KEY_REQ_EVT) {
  3198. BTM_PasskeyReqReply(status, p_bda, 0);
  3199. }
  3200. #endif
  3201. return;
  3202. }
  3203. /* Something bad. we can only fail this connection */
  3204. btm_cb.acl_disc_reason = HCI_ERR_HOST_REJECT_SECURITY;
  3205. if (BTM_SP_CFM_REQ_EVT == event) {
  3206. btsnd_hcic_user_conf_reply (p_bda, FALSE);
  3207. } else if (BTM_SP_KEY_NOTIF_EVT == event) {
  3208. /* do nothing -> it very unlikely to happen.
  3209. This event is most likely to be received by a HID host when it first connects to a HID device.
  3210. Usually the Host initiated the connection in this case.
  3211. On Mobile platforms, if there's a security process happening,
  3212. the host probably can not initiate another connection.
  3213. BTW (PC) is another story. */
  3214. if (NULL != (p_dev_rec = btm_find_dev (p_bda)) ) {
  3215. btm_sec_disconnect (p_dev_rec->hci_handle, HCI_ERR_AUTH_FAILURE);
  3216. }
  3217. }
  3218. #if (BT_SSP_INCLUDED == TRUE)
  3219. else {
  3220. btsnd_hcic_user_passkey_neg_reply(p_bda);
  3221. }
  3222. #endif
  3223. }
  3224. /*******************************************************************************
  3225. **
  3226. ** Function btm_keypress_notif_evt
  3227. **
  3228. ** Description This function is called when a key press notification is
  3229. ** received
  3230. **
  3231. ** Returns void
  3232. **
  3233. *******************************************************************************/
  3234. void btm_keypress_notif_evt (UINT8 *p)
  3235. {
  3236. tBTM_SP_KEYPRESS evt_data;
  3237. UINT8 *p_bda;
  3238. /* parse & report BTM_SP_KEYPRESS_EVT */
  3239. if (btm_cb.api.p_sp_callback) {
  3240. p_bda = evt_data.bd_addr;
  3241. STREAM_TO_BDADDR (p_bda, p);
  3242. evt_data.notif_type = *p;
  3243. (*btm_cb.api.p_sp_callback) (BTM_SP_KEYPRESS_EVT, (tBTM_SP_EVT_DATA *)&evt_data);
  3244. }
  3245. }
  3246. /*******************************************************************************
  3247. **
  3248. ** Function btm_simple_pair_complete
  3249. **
  3250. ** Description This function is called when simple pairing process is
  3251. ** complete
  3252. **
  3253. ** Returns void
  3254. **
  3255. *******************************************************************************/
  3256. void btm_simple_pair_complete (UINT8 *p)
  3257. {
  3258. tBTM_SP_COMPLT evt_data;
  3259. tBTM_SEC_DEV_REC *p_dev_rec;
  3260. UINT8 status;
  3261. BOOLEAN disc = FALSE;
  3262. status = *p++;
  3263. STREAM_TO_BDADDR (evt_data.bd_addr, p);
  3264. if ((p_dev_rec = btm_find_dev (evt_data.bd_addr)) == NULL) {
  3265. BTM_TRACE_ERROR ("btm_simple_pair_complete() with unknown BDA: %08x%04x\n",
  3266. (evt_data.bd_addr[0] << 24) + (evt_data.bd_addr[1] << 16) + (evt_data.bd_addr[2] << 8) + evt_data.bd_addr[3],
  3267. (evt_data.bd_addr[4] << 8) + evt_data.bd_addr[5]);
  3268. return;
  3269. }
  3270. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  3271. BTM_TRACE_EVENT ("btm_simple_pair_complete() Pair State: %s Status:%d sec_state: %u\n",
  3272. btm_pair_state_descr(btm_cb.pairing_state), status, p_dev_rec->sec_state);
  3273. #endif ///BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE
  3274. evt_data.status = BTM_ERR_PROCESSING;
  3275. if (status == HCI_SUCCESS) {
  3276. evt_data.status = BTM_SUCCESS;
  3277. p_dev_rec->sec_flags |= BTM_SEC_AUTHENTICATED;
  3278. } else {
  3279. if (status == HCI_ERR_PAIRING_NOT_ALLOWED) {
  3280. /* The test spec wants the peer device to get this failure code. */
  3281. btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_DISCONNECT);
  3282. /* Change the timer to 1 second */
  3283. btu_start_timer (&btm_cb.pairing_tle, BTU_TTYPE_USER_FUNC, BT_1SEC_TIMEOUT);
  3284. } else if (memcmp (btm_cb.pairing_bda, evt_data.bd_addr, BD_ADDR_LEN) == 0) {
  3285. /* stop the timer */
  3286. btu_stop_timer (&btm_cb.pairing_tle);
  3287. if (p_dev_rec->sec_state != BTM_SEC_STATE_AUTHENTICATING) {
  3288. /* the initiating side: will receive auth complete event. disconnect ACL at that time */
  3289. disc = TRUE;
  3290. }
  3291. } else {
  3292. disc = TRUE;
  3293. }
  3294. }
  3295. /* Let the pairing state stay active, p_auth_complete_callback will report the failure */
  3296. memcpy (evt_data.bd_addr, p_dev_rec->bd_addr, BD_ADDR_LEN);
  3297. memcpy (evt_data.dev_class, p_dev_rec->dev_class, DEV_CLASS_LEN);
  3298. if (btm_cb.api.p_sp_callback) {
  3299. (*btm_cb.api.p_sp_callback) (BTM_SP_COMPLT_EVT, (tBTM_SP_EVT_DATA *)&evt_data);
  3300. }
  3301. if (disc) {
  3302. /* simple pairing failed */
  3303. /* Avoid sending disconnect on HCI_ERR_PEER_USER */
  3304. if ((status != HCI_ERR_PEER_USER) && (status != HCI_ERR_CONN_CAUSE_LOCAL_HOST)) {
  3305. btm_sec_send_hci_disconnect (p_dev_rec, HCI_ERR_AUTH_FAILURE, p_dev_rec->hci_handle);
  3306. }
  3307. }
  3308. }
  3309. #endif ///SMP_INCLUDED == TRUE
  3310. #if BTM_OOB_INCLUDED == TRUE && SMP_INCLUDED == TRUE
  3311. /*******************************************************************************
  3312. **
  3313. ** Function btm_rem_oob_req
  3314. **
  3315. ** Description This function is called to process/report
  3316. ** HCI_REMOTE_OOB_DATA_REQUEST_EVT
  3317. **
  3318. ** Returns void
  3319. **
  3320. *******************************************************************************/
  3321. void btm_rem_oob_req (UINT8 *p)
  3322. {
  3323. UINT8 *p_bda;
  3324. tBTM_SP_RMT_OOB evt_data;
  3325. tBTM_SEC_DEV_REC *p_dev_rec;
  3326. BT_OCTET16 c;
  3327. BT_OCTET16 r;
  3328. p_bda = evt_data.bd_addr;
  3329. STREAM_TO_BDADDR (p_bda, p);
  3330. BTM_TRACE_EVENT ("btm_rem_oob_req() BDA: %02x:%02x:%02x:%02x:%02x:%02x\n",
  3331. p_bda[0], p_bda[1], p_bda[2], p_bda[3], p_bda[4], p_bda[5]);
  3332. if ( (NULL != (p_dev_rec = btm_find_dev (p_bda))) &&
  3333. btm_cb.api.p_sp_callback) {
  3334. memcpy (evt_data.bd_addr, p_dev_rec->bd_addr, BD_ADDR_LEN);
  3335. memcpy (evt_data.dev_class, p_dev_rec->dev_class, DEV_CLASS_LEN);
  3336. BCM_STRNCPY_S((char *)evt_data.bd_name, (char *)p_dev_rec->sec_bd_name, BTM_MAX_REM_BD_NAME_LEN);
  3337. evt_data.bd_name[BTM_MAX_REM_BD_NAME_LEN] = '\0';
  3338. btm_sec_change_pairing_state(BTM_PAIR_STATE_WAIT_LOCAL_OOB_RSP);
  3339. if ((*btm_cb.api.p_sp_callback) (BTM_SP_RMT_OOB_EVT, (tBTM_SP_EVT_DATA *)&evt_data) == BTM_NOT_AUTHORIZED) {
  3340. BTM_RemoteOobDataReply(TRUE, p_bda, c, r);
  3341. }
  3342. return;
  3343. }
  3344. /* something bad. we can only fail this connection */
  3345. btm_cb.acl_disc_reason = HCI_ERR_HOST_REJECT_SECURITY;
  3346. btsnd_hcic_rem_oob_neg_reply (p_bda);
  3347. }
  3348. /*******************************************************************************
  3349. **
  3350. ** Function btm_read_local_oob_complete
  3351. **
  3352. ** Description This function is called when read local oob data is
  3353. ** completed by the LM
  3354. **
  3355. ** Returns void
  3356. **
  3357. *******************************************************************************/
  3358. void btm_read_local_oob_complete (UINT8 *p)
  3359. {
  3360. tBTM_SP_LOC_OOB evt_data;
  3361. UINT8 status = *p++;
  3362. BTM_TRACE_EVENT ("btm_read_local_oob_complete:%d\n", status);
  3363. if (status == HCI_SUCCESS) {
  3364. evt_data.status = BTM_SUCCESS;
  3365. STREAM_TO_ARRAY16(evt_data.c, p);
  3366. STREAM_TO_ARRAY16(evt_data.r, p);
  3367. } else {
  3368. evt_data.status = BTM_ERR_PROCESSING;
  3369. }
  3370. if (btm_cb.api.p_sp_callback) {
  3371. (*btm_cb.api.p_sp_callback) (BTM_SP_LOC_OOB_EVT, (tBTM_SP_EVT_DATA *)&evt_data);
  3372. }
  3373. }
  3374. #endif /* BTM_OOB_INCLUDED */
  3375. /*******************************************************************************
  3376. **
  3377. ** Function btm_sec_auth_collision
  3378. **
  3379. ** Description This function is called when authentication or encryption
  3380. ** needs to be retried at a later time.
  3381. **
  3382. ** Returns void
  3383. **
  3384. *******************************************************************************/
  3385. #if (SMP_INCLUDED == TRUE)
  3386. static void btm_sec_auth_collision (UINT16 handle)
  3387. {
  3388. tBTM_SEC_DEV_REC *p_dev_rec;
  3389. if (!btm_cb.collision_start_time) {
  3390. btm_cb.collision_start_time = osi_time_get_os_boottime_ms();
  3391. }
  3392. if ((osi_time_get_os_boottime_ms() - btm_cb.collision_start_time) < btm_cb.max_collision_delay)
  3393. {
  3394. if (handle == BTM_SEC_INVALID_HANDLE)
  3395. {
  3396. if ((p_dev_rec = btm_sec_find_dev_by_sec_state (BTM_SEC_STATE_AUTHENTICATING)) == NULL) {
  3397. p_dev_rec = btm_sec_find_dev_by_sec_state (BTM_SEC_STATE_ENCRYPTING);
  3398. }
  3399. } else {
  3400. p_dev_rec = btm_find_dev_by_handle (handle);
  3401. }
  3402. if (p_dev_rec != NULL) {
  3403. BTM_TRACE_DEBUG ("btm_sec_auth_collision: state %d (retrying in a moment...)\n", p_dev_rec->sec_state);
  3404. /* We will restart authentication after timeout */
  3405. if (p_dev_rec->sec_state == BTM_SEC_STATE_AUTHENTICATING || p_dev_rec->sec_state == BTM_SEC_STATE_ENCRYPTING) {
  3406. p_dev_rec->sec_state = 0;
  3407. }
  3408. btm_cb.p_collided_dev_rec = p_dev_rec;
  3409. btm_cb.sec_collision_tle.param = (UINT32) btm_sec_collision_timeout;
  3410. btu_start_timer (&btm_cb.sec_collision_tle, BTU_TTYPE_USER_FUNC, BT_1SEC_TIMEOUT);
  3411. }
  3412. }
  3413. }
  3414. #endif ///SMP_INCLUDED == TRUE
  3415. /*******************************************************************************
  3416. **
  3417. ** Function btm_sec_auth_complete
  3418. **
  3419. ** Description This function is when authentication of the connection is
  3420. ** completed by the LM
  3421. **
  3422. ** Returns void
  3423. **
  3424. *******************************************************************************/
  3425. #if (SMP_INCLUDED == TRUE)
  3426. void btm_sec_auth_complete (UINT16 handle, UINT8 status)
  3427. {
  3428. UINT8 res;
  3429. UINT8 old_sm4;
  3430. tBTM_PAIRING_STATE old_state = btm_cb.pairing_state;
  3431. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev_by_handle (handle);
  3432. BOOLEAN are_bonding = FALSE;
  3433. /* Commenting out trace due to obf/compilation problems.
  3434. */
  3435. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  3436. if (p_dev_rec) {
  3437. BTM_TRACE_EVENT ("Security Manager: auth_complete PairState: %s handle:%u status:%d dev->sec_state: %u Bda:%08x, RName:%s\n",
  3438. btm_pair_state_descr (btm_cb.pairing_state),
  3439. handle, status,
  3440. p_dev_rec->sec_state,
  3441. (p_dev_rec->bd_addr[2] << 24) + (p_dev_rec->bd_addr[3] << 16) + (p_dev_rec->bd_addr[4] << 8) + p_dev_rec->bd_addr[5],
  3442. p_dev_rec->sec_bd_name);
  3443. } else {
  3444. BTM_TRACE_EVENT ("Security Manager: auth_complete PairState: %s handle:%u status:%d\n",
  3445. btm_pair_state_descr (btm_cb.pairing_state),
  3446. handle, status);
  3447. }
  3448. #endif
  3449. /* For transaction collision we need to wait and repeat. There is no need */
  3450. /* for random timeout because only slave should receive the result */
  3451. if ((status == HCI_ERR_LMP_ERR_TRANS_COLLISION) || (status == HCI_ERR_DIFF_TRANSACTION_COLLISION)) {
  3452. btm_sec_auth_collision(handle);
  3453. return;
  3454. }
  3455. btm_cb.collision_start_time = 0;
  3456. btm_restore_mode();
  3457. /* Check if connection was made just to do bonding. If we authenticate
  3458. the connection that is up, this is the last event received.
  3459. */
  3460. if (p_dev_rec
  3461. && (btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD)
  3462. && !(btm_cb.pairing_flags & BTM_PAIR_FLAGS_DISC_WHEN_DONE)) {
  3463. p_dev_rec->security_required &= ~BTM_SEC_OUT_AUTHENTICATE;
  3464. l2cu_start_post_bond_timer (p_dev_rec->hci_handle);
  3465. }
  3466. if (!p_dev_rec) {
  3467. return;
  3468. }
  3469. /* keep the old sm4 flag and clear the retry bit in control block */
  3470. old_sm4 = p_dev_rec->sm4;
  3471. p_dev_rec->sm4 &= ~BTM_SM4_RETRY;
  3472. if ( (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE)
  3473. && (btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD)
  3474. && (memcmp (p_dev_rec->bd_addr, btm_cb.pairing_bda, BD_ADDR_LEN) == 0) ) {
  3475. are_bonding = TRUE;
  3476. }
  3477. if ( (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE)
  3478. && (memcmp (p_dev_rec->bd_addr, btm_cb.pairing_bda, BD_ADDR_LEN) == 0) ) {
  3479. btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
  3480. }
  3481. if (p_dev_rec->sec_state != BTM_SEC_STATE_AUTHENTICATING) {
  3482. if ( (btm_cb.api.p_auth_complete_callback && status != HCI_SUCCESS)
  3483. && (old_state != BTM_PAIR_STATE_IDLE) ) {
  3484. (*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr,
  3485. p_dev_rec->dev_class,
  3486. p_dev_rec->sec_bd_name, status);
  3487. }
  3488. return;
  3489. }
  3490. /* There can be a race condition, when we are starting authentication and
  3491. ** the peer device is doing encryption.
  3492. ** If first we receive encryption change up, then initiated authentication
  3493. ** can not be performed. According to the spec we can not do authentication
  3494. ** on the encrypted link, so device is correct.
  3495. */
  3496. if ((status == HCI_ERR_COMMAND_DISALLOWED)
  3497. && ((p_dev_rec->sec_flags & (BTM_SEC_AUTHENTICATED | BTM_SEC_ENCRYPTED)) ==
  3498. (BTM_SEC_AUTHENTICATED | BTM_SEC_ENCRYPTED))) {
  3499. status = HCI_SUCCESS;
  3500. }
  3501. /* Currently we do not notify user if it is a keyboard which connects */
  3502. /* User probably Disabled the keyboard while it was asleep. Let her try */
  3503. if (btm_cb.api.p_auth_complete_callback) {
  3504. /* report the authentication status */
  3505. if (old_state != BTM_PAIR_STATE_IDLE) {
  3506. res = (*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr,
  3507. p_dev_rec->dev_class,
  3508. p_dev_rec->sec_bd_name, status);
  3509. if (res == BTM_SEC_DEV_REC_REMOVED) {
  3510. p_dev_rec = NULL;
  3511. }
  3512. }
  3513. }
  3514. if(!p_dev_rec) {
  3515. return;
  3516. }
  3517. p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
  3518. #if (CLASSIC_BT_INCLUDED == TRUE)
  3519. btm_sec_update_legacy_auth_state(btm_bda_to_acl(p_dev_rec->bd_addr, BT_TRANSPORT_BR_EDR), BTM_ACL_LEGACY_AUTH_SELF);
  3520. #endif
  3521. /* If this is a bonding procedure can disconnect the link now */
  3522. if (are_bonding) {
  3523. p_dev_rec->security_required &= ~BTM_SEC_OUT_AUTHENTICATE;
  3524. if (status != HCI_SUCCESS) {
  3525. if (((status != HCI_ERR_PEER_USER) && (status != HCI_ERR_CONN_CAUSE_LOCAL_HOST))) {
  3526. btm_sec_send_hci_disconnect (p_dev_rec, HCI_ERR_PEER_USER, p_dev_rec->hci_handle);
  3527. }
  3528. } else {
  3529. BTM_TRACE_DEBUG ("TRYING TO DECIDE IF CAN USE SMP_BR_CHNL\n");
  3530. if (p_dev_rec->new_encryption_key_is_p256 && (btm_sec_use_smp_br_chnl(p_dev_rec))
  3531. /* no LE keys are available, do deriving */
  3532. && (!(p_dev_rec->sec_flags & BTM_SEC_LE_LINK_KEY_KNOWN) ||
  3533. /* or BR key is higher security than existing LE keys */
  3534. (!(p_dev_rec->sec_flags & BTM_SEC_LE_LINK_KEY_AUTHED) &&
  3535. (p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_AUTHED)))) {
  3536. BTM_TRACE_DEBUG ("link encrypted afer dedic bonding can use SMP_BR_CHNL\n");
  3537. if (btm_sec_is_master(p_dev_rec)) {
  3538. // Encryption is required to start SM over BR/EDR
  3539. // indicate that this is encryption after authentication
  3540. BTM_SetEncryption(p_dev_rec->bd_addr, BT_TRANSPORT_BR_EDR, NULL, NULL);
  3541. }
  3542. }
  3543. l2cu_start_post_bond_timer (p_dev_rec->hci_handle);
  3544. }
  3545. return;
  3546. }
  3547. /* If authentication failed, notify the waiting layer */
  3548. if (status != HCI_SUCCESS) {
  3549. if ((old_sm4 & BTM_SM4_RETRY) == 0) {
  3550. /* allow retry only once */
  3551. if (status == HCI_ERR_LMP_ERR_TRANS_COLLISION) {
  3552. /* not retried yet. set the retry bit */
  3553. p_dev_rec->sm4 |= BTM_SM4_RETRY;
  3554. BTM_TRACE_DEBUG ("Collision retry sm4:x%x sec_flags:0x%x\n", p_dev_rec->sm4, p_dev_rec->sec_flags);
  3555. }
  3556. /* this retry for missing key is for Lisbon or later only.
  3557. * Legacy device do not need this. the controller will drive the retry automatically */
  3558. else if (HCI_ERR_KEY_MISSING == status && BTM_SEC_IS_SM4(p_dev_rec->sm4)) {
  3559. /* not retried yet. set the retry bit */
  3560. p_dev_rec->sm4 |= BTM_SM4_RETRY;
  3561. p_dev_rec->sec_flags &= ~BTM_SEC_LINK_KEY_KNOWN;
  3562. BTM_TRACE_DEBUG ("Retry for missing key sm4:x%x sec_flags:0x%x\n", p_dev_rec->sm4, p_dev_rec->sec_flags);
  3563. /* With BRCM controller, we do not need to delete the stored link key in controller.
  3564. If the stack may sit on top of other controller, we may need this
  3565. BTM_DeleteStoredLinkKey (bd_addr, NULL); */
  3566. }
  3567. if (p_dev_rec->sm4 & BTM_SM4_RETRY) {
  3568. btm_sec_execute_procedure (p_dev_rec);
  3569. return;
  3570. }
  3571. }
  3572. btm_sec_dev_rec_cback_event (p_dev_rec, BTM_ERR_PROCESSING, FALSE);
  3573. if (btm_cb.pairing_flags & BTM_PAIR_FLAGS_DISC_WHEN_DONE) {
  3574. btm_sec_send_hci_disconnect (p_dev_rec, HCI_ERR_AUTH_FAILURE, p_dev_rec->hci_handle);
  3575. }
  3576. return;
  3577. }
  3578. p_dev_rec->sec_flags |= BTM_SEC_AUTHENTICATED;
  3579. if (p_dev_rec->pin_code_length >= 16 ||
  3580. p_dev_rec->link_key_type == BTM_LKEY_TYPE_AUTH_COMB ||
  3581. p_dev_rec->link_key_type == BTM_LKEY_TYPE_AUTH_COMB_P_256) {
  3582. // If we have MITM protection we have a higher level of security than
  3583. // provided by 16 digits PIN
  3584. p_dev_rec->sec_flags |= BTM_SEC_16_DIGIT_PIN_AUTHED;
  3585. }
  3586. /* Authentication succeeded, execute the next security procedure, if any */
  3587. status = btm_sec_execute_procedure (p_dev_rec);
  3588. /* If there is no next procedure, or procedure failed to start, notify the caller */
  3589. if (status != BTM_CMD_STARTED) {
  3590. btm_sec_dev_rec_cback_event (p_dev_rec, status, FALSE);
  3591. }
  3592. }
  3593. #endif ///SMP_INCLUDED == TRUE
  3594. /*******************************************************************************
  3595. **
  3596. ** Function btm_sec_encrypt_change
  3597. **
  3598. ** Description This function is when encryption of the connection is
  3599. ** completed by the LM
  3600. **
  3601. ** Returns void
  3602. **
  3603. *******************************************************************************/
  3604. #if (SMP_INCLUDED == TRUE)
  3605. void btm_sec_encrypt_change (UINT16 handle, UINT8 status, UINT8 encr_enable)
  3606. {
  3607. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev_by_handle (handle);
  3608. #if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
  3609. tACL_CONN *p_acl = NULL;
  3610. #endif
  3611. BTM_TRACE_EVENT ("Security Manager: encrypt_change status:%d State:%d, encr_enable = %d\n",
  3612. status, (p_dev_rec) ? p_dev_rec->sec_state : 0, encr_enable);
  3613. BTM_TRACE_DEBUG ("before update p_dev_rec->sec_flags=0x%x\n", (p_dev_rec) ? p_dev_rec->sec_flags : 0 );
  3614. /* For transaction collision we need to wait and repeat. There is no need */
  3615. /* for random timeout because only slave should receive the result */
  3616. if ((status == HCI_ERR_LMP_ERR_TRANS_COLLISION) ||
  3617. (status == HCI_ERR_DIFF_TRANSACTION_COLLISION)) {
  3618. btm_sec_auth_collision(handle);
  3619. return;
  3620. }
  3621. btm_cb.collision_start_time = 0;
  3622. if (!p_dev_rec) {
  3623. return;
  3624. }
  3625. if ((status == HCI_SUCCESS) && encr_enable) {
  3626. if (p_dev_rec->hci_handle == handle) {
  3627. p_dev_rec->sec_flags |= (BTM_SEC_AUTHENTICATED | BTM_SEC_ENCRYPTED);
  3628. if (p_dev_rec->pin_code_length >= 16 ||
  3629. p_dev_rec->link_key_type == BTM_LKEY_TYPE_AUTH_COMB ||
  3630. p_dev_rec->link_key_type == BTM_LKEY_TYPE_AUTH_COMB_P_256) {
  3631. p_dev_rec->sec_flags |= BTM_SEC_16_DIGIT_PIN_AUTHED;
  3632. }
  3633. } else {
  3634. p_dev_rec->sec_flags |= BTM_SEC_LE_ENCRYPTED;
  3635. }
  3636. }
  3637. /* It is possible that we decrypted the link to perform role switch */
  3638. /* mark link not to be encrypted, so that when we execute security next time it will kick in again */
  3639. if ((status == HCI_SUCCESS) && !encr_enable) {
  3640. if (p_dev_rec->hci_handle == handle) {
  3641. p_dev_rec->sec_flags &= ~BTM_SEC_ENCRYPTED;
  3642. } else {
  3643. p_dev_rec->sec_flags &= ~BTM_SEC_LE_ENCRYPTED;
  3644. }
  3645. }
  3646. BTM_TRACE_DEBUG ("after update p_dev_rec->sec_flags=0x%x\n", p_dev_rec->sec_flags );
  3647. #if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
  3648. p_acl = btm_handle_to_acl(handle);
  3649. if (p_acl != NULL) {
  3650. btm_sec_check_pending_enc_req(p_dev_rec, p_acl->transport, encr_enable);
  3651. }
  3652. if (p_acl && p_acl->transport == BT_TRANSPORT_LE) {
  3653. if (status == HCI_ERR_KEY_MISSING || status == HCI_ERR_AUTH_FAILURE ||
  3654. status == HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE) {
  3655. p_dev_rec->sec_flags &= ~ (BTM_SEC_LE_LINK_KEY_KNOWN);
  3656. p_dev_rec->ble.key_type = BTM_LE_KEY_NONE;
  3657. }
  3658. btm_ble_link_encrypted(p_dev_rec->ble.pseudo_addr, encr_enable);
  3659. return;
  3660. } else {
  3661. /* BR/EDR connection, update the encryption key size to be 16 as always */
  3662. p_dev_rec->enc_key_size = 16;
  3663. }
  3664. BTM_TRACE_DEBUG ("in %s new_encr_key_256 is %d\n",
  3665. __func__, p_dev_rec->new_encryption_key_is_p256);
  3666. if ((status == HCI_SUCCESS) && encr_enable && (p_dev_rec->hci_handle == handle)) {
  3667. if (p_dev_rec->new_encryption_key_is_p256) {
  3668. if (btm_sec_use_smp_br_chnl(p_dev_rec) &&
  3669. btm_sec_is_master(p_dev_rec) &&
  3670. /* if LE key is not known, do deriving */
  3671. (!(p_dev_rec->sec_flags & BTM_SEC_LE_LINK_KEY_KNOWN) ||
  3672. /* or BR key is higher security than existing LE keys */
  3673. (!(p_dev_rec->sec_flags & BTM_SEC_LE_LINK_KEY_AUTHED)
  3674. && (p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_AUTHED)))) {
  3675. /* BR/EDR is encrypted with LK that can be used to derive LE LTK */
  3676. p_dev_rec->new_encryption_key_is_p256 = FALSE;
  3677. if (p_dev_rec->no_smp_on_br) {
  3678. BTM_TRACE_DEBUG ("%s NO SM over BR/EDR\n", __func__);
  3679. } else {
  3680. #if (CLASSIC_BT_INCLUDED == TRUE)
  3681. BTM_TRACE_DEBUG ("%s start SM over BR/EDR\n", __func__);
  3682. SMP_BR_PairWith(p_dev_rec->bd_addr);
  3683. #endif ///CLASSIC_BT_INCLUDED == TRUE
  3684. }
  3685. }
  3686. } else {
  3687. // BR/EDR is successfully encrypted. Correct LK type if needed
  3688. // (BR/EDR LK derived from LE LTK was used for encryption)
  3689. if ((encr_enable == 1) && /* encryption is ON for SSP */
  3690. /* LK type is for BR/EDR SC */
  3691. (p_dev_rec->link_key_type == BTM_LKEY_TYPE_UNAUTH_COMB_P_256 ||
  3692. p_dev_rec->link_key_type == BTM_LKEY_TYPE_AUTH_COMB_P_256)) {
  3693. if (p_dev_rec->link_key_type == BTM_LKEY_TYPE_UNAUTH_COMB_P_256) {
  3694. p_dev_rec->link_key_type = BTM_LKEY_TYPE_UNAUTH_COMB;
  3695. } else { /* BTM_LKEY_TYPE_AUTH_COMB_P_256 */
  3696. p_dev_rec->link_key_type = BTM_LKEY_TYPE_AUTH_COMB;
  3697. }
  3698. BTM_TRACE_DEBUG("updated link key type to %d\n", p_dev_rec->link_key_type);
  3699. btm_send_link_key_notif(p_dev_rec);
  3700. }
  3701. }
  3702. }
  3703. #else
  3704. btm_sec_check_pending_enc_req (p_dev_rec, BT_TRANSPORT_BR_EDR, encr_enable);
  3705. #endif /* BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE */
  3706. /* If this encryption was started by peer do not need to do anything */
  3707. if (p_dev_rec->sec_state != BTM_SEC_STATE_ENCRYPTING) {
  3708. if (BTM_SEC_STATE_DELAY_FOR_ENC == p_dev_rec->sec_state) {
  3709. p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
  3710. p_dev_rec->p_callback = NULL;
  3711. #if (CLASSIC_BT_INCLUDED == TRUE)
  3712. l2cu_resubmit_pending_sec_req (p_dev_rec->bd_addr);
  3713. #endif ///CLASSIC_BT_INCLUDED == TRUE
  3714. }
  3715. return;
  3716. }
  3717. p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
  3718. /* If encryption setup failed, notify the waiting layer */
  3719. if (status != HCI_SUCCESS) {
  3720. btm_sec_dev_rec_cback_event (p_dev_rec, BTM_ERR_PROCESSING, FALSE);
  3721. return;
  3722. }
  3723. /* Encryption setup succeeded, execute the next security procedure, if any */
  3724. status = (UINT8)btm_sec_execute_procedure (p_dev_rec);
  3725. /* If there is no next procedure, or procedure failed to start, notify the caller */
  3726. if (status != BTM_CMD_STARTED) {
  3727. btm_sec_dev_rec_cback_event (p_dev_rec, status, FALSE);
  3728. }
  3729. }
  3730. #endif ///SMP_INCLUDED == TRUE
  3731. /*******************************************************************************
  3732. **
  3733. ** Function btm_sec_connect_after_reject_timeout
  3734. **
  3735. ** Description Connection for bonding could not start because of the collision
  3736. ** Initiate outgoing connection
  3737. **
  3738. ** Returns Pointer to the TLE struct
  3739. **
  3740. *******************************************************************************/
  3741. #if (SMP_INCLUDED == TRUE)
  3742. static void btm_sec_connect_after_reject_timeout (TIMER_LIST_ENT *p_tle)
  3743. {
  3744. tBTM_SEC_DEV_REC *p_dev_rec = btm_cb.p_collided_dev_rec;
  3745. UNUSED(p_tle);
  3746. BTM_TRACE_EVENT ("btm_sec_connect_after_reject_timeout()\n");
  3747. btm_cb.sec_collision_tle.param = 0;
  3748. btm_cb.p_collided_dev_rec = 0;
  3749. if (btm_sec_dd_create_conn(p_dev_rec) != BTM_CMD_STARTED) {
  3750. BTM_TRACE_WARNING ("Security Manager: btm_sec_connect_after_reject_timeout: failed to start connection\n");
  3751. btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
  3752. if (btm_cb.api.p_auth_complete_callback) {
  3753. (*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr, p_dev_rec->dev_class,
  3754. p_dev_rec->sec_bd_name, HCI_ERR_MEMORY_FULL);
  3755. }
  3756. }
  3757. }
  3758. #endif ///SMP_INCLUDED == TRUE
  3759. /*******************************************************************************
  3760. **
  3761. ** Function btm_sec_connected
  3762. **
  3763. ** Description This function is when a connection to the peer device is
  3764. ** establsihed
  3765. **
  3766. ** Returns void
  3767. **
  3768. *******************************************************************************/
  3769. #if (SMP_INCLUDED == TRUE)
  3770. void btm_sec_connected (UINT8 *bda, UINT16 handle, UINT8 status, UINT8 enc_mode)
  3771. {
  3772. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bda);
  3773. UINT8 res;
  3774. UINT8 sec_dev_rec_status;
  3775. BOOLEAN is_pairing_device = FALSE;
  3776. tACL_CONN *p_acl_cb;
  3777. UINT8 bit_shift = 0;
  3778. btm_acl_resubmit_page();
  3779. /* Commenting out trace due to obf/compilation problems.
  3780. */
  3781. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  3782. if (p_dev_rec) {
  3783. BTM_TRACE_EVENT ("Security Manager: btm_sec_connected in state: %s handle:%d status:%d enc_mode:%d bda:%x RName:%s\n",
  3784. btm_pair_state_descr(btm_cb.pairing_state), handle, status, enc_mode,
  3785. (bda[2] << 24) + (bda[3] << 16) + (bda[4] << 8) + bda[5],
  3786. p_dev_rec->sec_bd_name);
  3787. } else {
  3788. BTM_TRACE_EVENT ("Security Manager: btm_sec_connected in state: %s handle:%d status:%d enc_mode:%d bda:%x \n",
  3789. btm_pair_state_descr(btm_cb.pairing_state), handle, status, enc_mode,
  3790. (bda[2] << 24) + (bda[3] << 16) + (bda[4] << 8) + bda[5]);
  3791. }
  3792. #endif
  3793. if (!p_dev_rec) {
  3794. /* There is no device record for new connection. Allocate one */
  3795. if (status == HCI_SUCCESS) {
  3796. p_dev_rec = btm_sec_alloc_dev (bda);
  3797. } else {
  3798. /* If the device matches with stored paring address
  3799. * reset the paring state to idle */
  3800. if ((btm_cb.pairing_state != BTM_PAIR_STATE_IDLE) &&
  3801. (memcmp (btm_cb.pairing_bda, bda, BD_ADDR_LEN) == 0)) {
  3802. btm_sec_change_pairing_state(BTM_PAIR_STATE_IDLE);
  3803. }
  3804. /* can not find the device record and the status is error,
  3805. * just ignore it */
  3806. return;
  3807. }
  3808. } else { /* Update the timestamp for this device */
  3809. #if BLE_INCLUDED == TRUE
  3810. bit_shift = (handle == p_dev_rec->ble_hci_handle) ? 8 : 0;
  3811. #endif
  3812. p_dev_rec->timestamp = btm_cb.dev_rec_count++;
  3813. if (p_dev_rec->sm4 & BTM_SM4_CONN_PEND) {
  3814. /* tell L2CAP it's a bonding connection. */
  3815. if ( (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE)
  3816. && (memcmp (btm_cb.pairing_bda, p_dev_rec->bd_addr, BD_ADDR_LEN) == 0)
  3817. && (btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD) ) {
  3818. /* if incoming connection failed while pairing, then try to connect and continue */
  3819. /* Motorola S9 disconnects without asking pin code */
  3820. if ((status != HCI_SUCCESS) && (btm_cb.pairing_state == BTM_PAIR_STATE_WAIT_PIN_REQ)) {
  3821. BTM_TRACE_WARNING ("Security Manager: btm_sec_connected: incoming connection failed without asking PIN\n");
  3822. p_dev_rec->sm4 &= ~BTM_SM4_CONN_PEND;
  3823. if (p_dev_rec->sec_flags & BTM_SEC_NAME_KNOWN) {
  3824. /* Start timer with 0 to initiate connection with new LCB */
  3825. /* because L2CAP will delete current LCB with this event */
  3826. btm_cb.p_collided_dev_rec = p_dev_rec;
  3827. btm_cb.sec_collision_tle.param = (UINT32) btm_sec_connect_after_reject_timeout;
  3828. btu_start_timer (&btm_cb.sec_collision_tle, BTU_TTYPE_USER_FUNC, 0);
  3829. } else {
  3830. btm_sec_change_pairing_state (BTM_PAIR_STATE_GET_REM_NAME);
  3831. BTM_ReadRemoteDeviceName(p_dev_rec->bd_addr, NULL, BT_TRANSPORT_BR_EDR);
  3832. }
  3833. #if BTM_DISC_DURING_RS == TRUE
  3834. p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING; /* reset flag */
  3835. #endif
  3836. return;
  3837. } else {
  3838. l2cu_update_lcb_4_bonding(p_dev_rec->bd_addr, TRUE);
  3839. }
  3840. }
  3841. /* always clear the pending flag */
  3842. p_dev_rec->sm4 &= ~BTM_SM4_CONN_PEND;
  3843. }
  3844. }
  3845. #if BLE_INCLUDED == TRUE
  3846. p_dev_rec->device_type |= BT_DEVICE_TYPE_BREDR;
  3847. #endif
  3848. #if BTM_DISC_DURING_RS == TRUE
  3849. p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING; /* reset flag */
  3850. #endif
  3851. p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING; /* reset flag */
  3852. if ( (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE)
  3853. && (memcmp (btm_cb.pairing_bda, bda, BD_ADDR_LEN) == 0) ) {
  3854. /* if we rejected incoming connection from bonding device */
  3855. if ((status == HCI_ERR_HOST_REJECT_DEVICE)
  3856. && (btm_cb.pairing_flags & BTM_PAIR_FLAGS_REJECTED_CONNECT)) {
  3857. BTM_TRACE_WARNING ("Security Manager: btm_sec_connected: HCI_Conn_Comp Flags:0x%04x, sm4: 0x%x\n",
  3858. btm_cb.pairing_flags, p_dev_rec->sm4);
  3859. btm_cb.pairing_flags &= ~BTM_PAIR_FLAGS_REJECTED_CONNECT;
  3860. if (BTM_SEC_IS_SM4_UNKNOWN(p_dev_rec->sm4)) {
  3861. /* Try again: RNR when no ACL causes HCI_RMT_HOST_SUP_FEAT_NOTIFY_EVT */
  3862. btm_sec_change_pairing_state (BTM_PAIR_STATE_GET_REM_NAME);
  3863. BTM_ReadRemoteDeviceName(bda, NULL, BT_TRANSPORT_BR_EDR);
  3864. return;
  3865. }
  3866. /* if we already have pin code */
  3867. if (btm_cb.pairing_state != BTM_PAIR_STATE_WAIT_LOCAL_PIN) {
  3868. /* Start timer with 0 to initiate connection with new LCB */
  3869. /* because L2CAP will delete current LCB with this event */
  3870. btm_cb.p_collided_dev_rec = p_dev_rec;
  3871. btm_cb.sec_collision_tle.param = (UINT32) btm_sec_connect_after_reject_timeout;
  3872. btu_start_timer (&btm_cb.sec_collision_tle, BTU_TTYPE_USER_FUNC, 0);
  3873. }
  3874. return;
  3875. }
  3876. /* wait for incoming connection without resetting pairing state */
  3877. else if (status == HCI_ERR_CONNECTION_EXISTS) {
  3878. BTM_TRACE_WARNING ("Security Manager: btm_sec_connected: Wait for incoming connection\n");
  3879. return;
  3880. }
  3881. is_pairing_device = TRUE;
  3882. }
  3883. /* If connection was made to do bonding restore link security if changed */
  3884. btm_restore_mode();
  3885. /* if connection fails during pin request, notify application */
  3886. if (status != HCI_SUCCESS) {
  3887. /* If connection failed because of during pairing, need to tell user */
  3888. if (is_pairing_device) {
  3889. p_dev_rec->security_required &= ~BTM_SEC_OUT_AUTHENTICATE;
  3890. p_dev_rec->sec_flags &= ~((BTM_SEC_LINK_KEY_KNOWN | BTM_SEC_LINK_KEY_AUTHED) << bit_shift);
  3891. BTM_TRACE_DEBUG ("security_required:%x \n", p_dev_rec->security_required );
  3892. btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
  3893. /* We need to notify host that the key is not known any more */
  3894. if (btm_cb.api.p_auth_complete_callback) {
  3895. sec_dev_rec_status = (*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr,
  3896. p_dev_rec->dev_class,
  3897. p_dev_rec->sec_bd_name, status);
  3898. if (sec_dev_rec_status == BTM_SEC_DEV_REC_REMOVED) {
  3899. p_dev_rec = NULL;
  3900. }
  3901. }
  3902. }
  3903. /*
  3904. Do not send authentication failure, if following conditions hold good
  3905. 1. BTM Sec Pairing state is idle
  3906. 2. Link key for the remote device is present.
  3907. 3. Remote is SSP capable.
  3908. */
  3909. else if ((p_dev_rec->link_key_type <= BTM_LKEY_TYPE_REMOTE_UNIT) &&
  3910. (((status == HCI_ERR_AUTH_FAILURE) ||
  3911. (status == HCI_ERR_KEY_MISSING) ||
  3912. (status == HCI_ERR_HOST_REJECT_SECURITY) ||
  3913. (status == HCI_ERR_PAIRING_NOT_ALLOWED) ||
  3914. (status == HCI_ERR_UNIT_KEY_USED) ||
  3915. (status == HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED) ||
  3916. (status == HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE) ||
  3917. (status == HCI_ERR_REPEATED_ATTEMPTS)))) {
  3918. p_dev_rec->security_required &= ~BTM_SEC_OUT_AUTHENTICATE;
  3919. p_dev_rec->sec_flags &= ~ (BTM_SEC_LE_LINK_KEY_KNOWN << bit_shift);
  3920. #ifdef BRCM_NOT_4_BTE
  3921. /* If we rejected pairing, pass this special result code */
  3922. if (btm_cb.acl_disc_reason == HCI_ERR_HOST_REJECT_SECURITY) {
  3923. status = HCI_ERR_HOST_REJECT_SECURITY;
  3924. }
  3925. #endif
  3926. /* We need to notify host that the key is not known any more */
  3927. if (btm_cb.api.p_auth_complete_callback) {
  3928. sec_dev_rec_status = (*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr,
  3929. p_dev_rec->dev_class,
  3930. p_dev_rec->sec_bd_name, status);
  3931. if (sec_dev_rec_status == BTM_SEC_DEV_REC_REMOVED) {
  3932. p_dev_rec = NULL;
  3933. }
  3934. }
  3935. }
  3936. if (status == HCI_ERR_CONNECTION_TOUT || status == HCI_ERR_LMP_RESPONSE_TIMEOUT ||
  3937. status == HCI_ERR_UNSPECIFIED || status == HCI_ERR_PAGE_TIMEOUT) {
  3938. btm_sec_dev_rec_cback_event (p_dev_rec, BTM_DEVICE_TIMEOUT, FALSE);
  3939. } else {
  3940. btm_sec_dev_rec_cback_event (p_dev_rec, BTM_ERR_PROCESSING, FALSE);
  3941. }
  3942. return;
  3943. }
  3944. /* If initiated dedicated bonding, return the link key now, and initiate disconnect */
  3945. /* If dedicated bonding, and we now have a link key, we are all done */
  3946. if ( is_pairing_device
  3947. && (p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_KNOWN) ) {
  3948. if (p_dev_rec->link_key_not_sent) {
  3949. p_dev_rec->link_key_not_sent = FALSE;
  3950. btm_send_link_key_notif(p_dev_rec);
  3951. }
  3952. p_dev_rec->security_required &= ~BTM_SEC_OUT_AUTHENTICATE;
  3953. /* remember flag before it is initialized */
  3954. if (btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD) {
  3955. res = TRUE;
  3956. } else {
  3957. res = FALSE;
  3958. }
  3959. if (btm_cb.api.p_auth_complete_callback) {
  3960. sec_dev_rec_status = (*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr,
  3961. p_dev_rec->dev_class,
  3962. p_dev_rec->sec_bd_name, HCI_SUCCESS);
  3963. if (sec_dev_rec_status == BTM_SEC_DEV_REC_REMOVED) {
  3964. p_dev_rec = NULL;
  3965. }
  3966. }
  3967. btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
  3968. if ( res ) {
  3969. /* Let l2cap start bond timer */
  3970. l2cu_update_lcb_4_bonding (p_dev_rec->bd_addr, TRUE);
  3971. }
  3972. return;
  3973. }
  3974. p_dev_rec->hci_handle = handle;
  3975. /* role may not be correct here, it will be updated by l2cap, but we need to */
  3976. /* notify btm_acl that link is up, so starting of rmt name request will not */
  3977. /* set paging flag up */
  3978. p_acl_cb = btm_bda_to_acl(bda, BT_TRANSPORT_BR_EDR);
  3979. if (p_acl_cb) {
  3980. /* whatever is in btm_establish_continue() without reporting the BTM_BL_CONN_EVT event */
  3981. #if (!defined(BTM_BYPASS_EXTRA_ACL_SETUP) || BTM_BYPASS_EXTRA_ACL_SETUP == FALSE)
  3982. /* For now there are a some devices that do not like sending */
  3983. /* commands events and data at the same time. */
  3984. /* Set the packet types to the default allowed by the device */
  3985. btm_set_packet_types (p_acl_cb, btm_cb.btm_acl_pkt_types_supported);
  3986. if (btm_cb.btm_def_link_policy) {
  3987. BTM_SetLinkPolicy (p_acl_cb->remote_addr, &btm_cb.btm_def_link_policy);
  3988. }
  3989. #endif
  3990. }
  3991. btm_acl_created (bda, p_dev_rec->dev_class, p_dev_rec->sec_bd_name, handle, HCI_ROLE_SLAVE, BT_TRANSPORT_BR_EDR);
  3992. /* Initialize security flags. We need to do that because some */
  3993. /* authorization complete could have come after the connection is dropped */
  3994. /* and that would set wrong flag that link has been authorized already */
  3995. p_dev_rec->sec_flags &= ~((BTM_SEC_AUTHORIZED | BTM_SEC_AUTHENTICATED |
  3996. BTM_SEC_ENCRYPTED | BTM_SEC_ROLE_SWITCHED) << bit_shift);
  3997. if (enc_mode != HCI_ENCRYPT_MODE_DISABLED) {
  3998. p_dev_rec->sec_flags |= ((BTM_SEC_AUTHENTICATED | BTM_SEC_ENCRYPTED) << bit_shift);
  3999. }
  4000. if (btm_cb.security_mode == BTM_SEC_MODE_LINK) {
  4001. p_dev_rec->sec_flags |= (BTM_SEC_AUTHENTICATED << bit_shift);
  4002. }
  4003. if (p_dev_rec->pin_code_length >= 16 ||
  4004. p_dev_rec->link_key_type == BTM_LKEY_TYPE_AUTH_COMB ||
  4005. p_dev_rec->link_key_type == BTM_LKEY_TYPE_AUTH_COMB_P_256) {
  4006. p_dev_rec->sec_flags |= (BTM_SEC_16_DIGIT_PIN_AUTHED << bit_shift);
  4007. }
  4008. p_dev_rec->link_key_changed = FALSE;
  4009. /* After connection is established we perform security if we do not know */
  4010. /* the name, or if we are originator because some procedure can have */
  4011. /* been scheduled while connection was down */
  4012. BTM_TRACE_DEBUG ("is_originator:%d \n", p_dev_rec->is_originator);
  4013. if (!(p_dev_rec->sec_flags & BTM_SEC_NAME_KNOWN) || p_dev_rec->is_originator) {
  4014. if ((res = btm_sec_execute_procedure (p_dev_rec)) != BTM_CMD_STARTED) {
  4015. btm_sec_dev_rec_cback_event (p_dev_rec, res, FALSE);
  4016. }
  4017. }
  4018. return;
  4019. }
  4020. /*******************************************************************************
  4021. **
  4022. ** Function btm_sec_disconnect
  4023. **
  4024. ** Description This function is called to disconnect HCI link
  4025. **
  4026. ** Returns btm status
  4027. **
  4028. *******************************************************************************/
  4029. tBTM_STATUS btm_sec_disconnect (UINT16 handle, UINT8 reason)
  4030. {
  4031. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev_by_handle (handle);
  4032. /* In some weird race condition we may not have a record */
  4033. if (!p_dev_rec) {
  4034. btsnd_hcic_disconnect (handle, reason);
  4035. return (BTM_SUCCESS);
  4036. }
  4037. /* If we are in the process of bonding we need to tell client that auth failed */
  4038. if ( (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE)
  4039. && (memcmp (btm_cb.pairing_bda, p_dev_rec->bd_addr, BD_ADDR_LEN) == 0)
  4040. && (btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD) ) {
  4041. /* we are currently doing bonding. Link will be disconnected when done */
  4042. btm_cb.pairing_flags |= BTM_PAIR_FLAGS_DISC_WHEN_DONE;
  4043. return (BTM_BUSY);
  4044. }
  4045. return (btm_sec_send_hci_disconnect(p_dev_rec, reason, handle));
  4046. }
  4047. /*******************************************************************************
  4048. **
  4049. ** Function btm_sec_disconnected
  4050. **
  4051. ** Description This function is when a connection to the peer device is
  4052. ** dropped
  4053. **
  4054. ** Returns void
  4055. **
  4056. *******************************************************************************/
  4057. void btm_sec_disconnected (UINT16 handle, UINT8 reason)
  4058. {
  4059. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev_by_handle (handle);
  4060. UINT8 old_pairing_flags = btm_cb.pairing_flags;
  4061. int result = HCI_ERR_AUTH_FAILURE;
  4062. tBTM_SEC_CALLBACK *p_callback = NULL;
  4063. tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
  4064. /* If page was delayed for disc complete, can do it now */
  4065. btm_cb.discing = FALSE;
  4066. #if (CLASSIC_BT_INCLUDED == TRUE)
  4067. btm_acl_resubmit_page();
  4068. #endif
  4069. if (!p_dev_rec) {
  4070. return;
  4071. }
  4072. p_dev_rec->enc_init_by_we = FALSE;
  4073. transport = (handle == p_dev_rec->hci_handle) ? BT_TRANSPORT_BR_EDR : BT_TRANSPORT_LE;
  4074. p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING; /* reset flag */
  4075. #if BTM_DISC_DURING_RS == TRUE
  4076. p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING; /* reset flag */
  4077. #endif
  4078. /* clear unused flags */
  4079. p_dev_rec->sm4 &= BTM_SM4_TRUE;
  4080. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  4081. uint8_t *bd_addr = (uint8_t *)p_dev_rec->bd_addr;
  4082. BTM_TRACE_EVENT("%s sec_req:x%x state:%s reason:%d bd_addr:%02x:%02x:%02x:%02x:%02x:%02x"
  4083. " remote_name:%s\n", __func__, p_dev_rec->security_required, btm_pair_state_descr(btm_cb.pairing_state),
  4084. reason, bd_addr[0], bd_addr[1], bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5], p_dev_rec->sec_bd_name);
  4085. #endif ///BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE
  4086. BTM_TRACE_EVENT("%s before update sec_flags=0x%x\n", __func__, p_dev_rec->sec_flags);
  4087. #if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
  4088. btm_ble_update_mode_operation(HCI_ROLE_UNKNOWN, p_dev_rec->bd_addr, HCI_SUCCESS);
  4089. /* see sec_flags processing in btm_acl_removed */
  4090. if (transport == BT_TRANSPORT_LE) {
  4091. p_dev_rec->ble_hci_handle = BTM_SEC_INVALID_HANDLE;
  4092. p_dev_rec->sec_flags &= ~(BTM_SEC_LE_AUTHENTICATED | BTM_SEC_LE_ENCRYPTED);
  4093. p_dev_rec->enc_key_size = 0;
  4094. } else
  4095. #endif
  4096. {
  4097. p_dev_rec->hci_handle = BTM_SEC_INVALID_HANDLE;
  4098. p_dev_rec->sec_flags &= ~(BTM_SEC_AUTHORIZED | BTM_SEC_AUTHENTICATED | BTM_SEC_ENCRYPTED
  4099. | BTM_SEC_ROLE_SWITCHED | BTM_SEC_16_DIGIT_PIN_AUTHED);
  4100. }
  4101. #if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
  4102. if (p_dev_rec->sec_state == BTM_SEC_STATE_DISCONNECTING_BOTH) {
  4103. p_dev_rec->sec_state = (transport == BT_TRANSPORT_LE) ?
  4104. BTM_SEC_STATE_DISCONNECTING : BTM_SEC_STATE_DISCONNECTING_BLE;
  4105. return;
  4106. }
  4107. #endif
  4108. p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
  4109. p_dev_rec->security_required = BTM_SEC_NONE;
  4110. p_callback = p_dev_rec->p_callback;
  4111. /* if security is pending, send callback to clean up the security state */
  4112. if (p_callback) {
  4113. p_dev_rec->p_callback = NULL; /* when the peer device time out the authentication before
  4114. we do, this call back must be reset here */
  4115. (*p_callback) (p_dev_rec->bd_addr, transport, p_dev_rec->p_ref_data, BTM_ERR_PROCESSING);
  4116. }
  4117. BTM_TRACE_EVENT("%s after update sec_flags=0x%x\n", __func__, p_dev_rec->sec_flags);
  4118. /* If we are in the process of bonding we need to tell client that auth failed */
  4119. if ( (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE)
  4120. && (memcmp (btm_cb.pairing_bda, p_dev_rec->bd_addr, BD_ADDR_LEN) == 0)) {
  4121. btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
  4122. p_dev_rec->sec_flags &= ~BTM_SEC_LINK_KEY_KNOWN;
  4123. if (btm_cb.api.p_auth_complete_callback) {
  4124. /* If the disconnection reason is REPEATED_ATTEMPTS,
  4125. send this error message to complete callback function
  4126. to display the error message of Repeated attempts.
  4127. All others, send HCI_ERR_AUTH_FAILURE. */
  4128. if (reason == HCI_ERR_REPEATED_ATTEMPTS) {
  4129. result = HCI_ERR_REPEATED_ATTEMPTS;
  4130. } else if (old_pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD) {
  4131. result = HCI_ERR_HOST_REJECT_SECURITY;
  4132. }
  4133. (*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr, p_dev_rec->dev_class,
  4134. p_dev_rec->sec_bd_name, result);
  4135. }
  4136. }
  4137. }
  4138. /*******************************************************************************
  4139. **
  4140. ** Function btm_sec_link_key_notification
  4141. **
  4142. ** Description This function is called when a new connection link key is
  4143. ** generated
  4144. **
  4145. ** Returns Pointer to the record or NULL
  4146. **
  4147. *******************************************************************************/
  4148. void btm_sec_link_key_notification (UINT8 *p_bda, UINT8 *p_link_key, UINT8 key_type)
  4149. {
  4150. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_or_alloc_dev (p_bda);
  4151. BOOLEAN we_are_bonding = FALSE;
  4152. BOOLEAN ltk_derived_lk = FALSE;
  4153. UINT8 res;
  4154. BTM_TRACE_EVENT ("btm_sec_link_key_notification() BDA:%04x%08x, TYPE: %d\n",
  4155. (p_bda[0] << 8) + p_bda[1], (p_bda[2] << 24) + (p_bda[3] << 16) + (p_bda[4] << 8) + p_bda[5],
  4156. key_type);
  4157. if ((key_type >= BTM_LTK_DERIVED_LKEY_OFFSET + BTM_LKEY_TYPE_COMBINATION) &&
  4158. (key_type <= BTM_LTK_DERIVED_LKEY_OFFSET + BTM_LKEY_TYPE_AUTH_COMB_P_256)) {
  4159. ltk_derived_lk = TRUE;
  4160. key_type -= BTM_LTK_DERIVED_LKEY_OFFSET;
  4161. }
  4162. /* If connection was made to do bonding restore link security if changed */
  4163. btm_restore_mode();
  4164. /* Store the previous state of secure connection as current state. Since
  4165. * this is the first encounter with the remote device, whatever the remote
  4166. * device's SC state is, it cannot lower the SC level from this. */
  4167. p_dev_rec->remote_secure_connection_previous_state = p_dev_rec->remote_supports_secure_connections;
  4168. if (p_dev_rec->remote_supports_secure_connections) {
  4169. BTM_TRACE_EVENT ("Remote device supports Secure Connection");
  4170. } else {
  4171. BTM_TRACE_EVENT ("Remote device does not support Secure Connection");
  4172. }
  4173. if (key_type != BTM_LKEY_TYPE_CHANGED_COMB) {
  4174. p_dev_rec->link_key_type = key_type;
  4175. }
  4176. p_dev_rec->sec_flags |= BTM_SEC_LINK_KEY_KNOWN;
  4177. #if (CLASSIC_BT_INCLUDED == TRUE)
  4178. btm_sec_update_legacy_auth_state(btm_bda_to_acl(p_dev_rec->bd_addr, BT_TRANSPORT_BR_EDR), BTM_ACL_LEGACY_AUTH_NONE);
  4179. #endif
  4180. /*
  4181. * Until this point in time, we do not know if MITM was enabled, hence we
  4182. * add the extended security flag here.
  4183. */
  4184. if (p_dev_rec->pin_code_length >= 16 ||
  4185. p_dev_rec->link_key_type == BTM_LKEY_TYPE_AUTH_COMB ||
  4186. p_dev_rec->link_key_type == BTM_LKEY_TYPE_AUTH_COMB_P_256) {
  4187. p_dev_rec->sec_flags |= BTM_SEC_16_DIGIT_PIN_AUTHED;
  4188. }
  4189. #if (BLE_INCLUDED == TRUE)
  4190. /* BR/EDR connection, update the encryption key size to be 16 as always */
  4191. p_dev_rec->enc_key_size = 16;
  4192. #endif
  4193. memcpy (p_dev_rec->link_key, p_link_key, LINK_KEY_LEN);
  4194. if ( (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE)
  4195. && (memcmp (btm_cb.pairing_bda, p_bda, BD_ADDR_LEN) == 0) ) {
  4196. if (btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD) {
  4197. we_are_bonding = TRUE;
  4198. } else {
  4199. btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
  4200. }
  4201. }
  4202. /* save LTK derived LK no matter what */
  4203. if (ltk_derived_lk) {
  4204. if (btm_cb.api.p_link_key_callback) {
  4205. BTM_TRACE_DEBUG ("%s() Save LTK derived LK (key_type = %d)\n",
  4206. __FUNCTION__, p_dev_rec->link_key_type);
  4207. (*btm_cb.api.p_link_key_callback) (p_bda, p_dev_rec->dev_class,
  4208. p_dev_rec->sec_bd_name,
  4209. p_link_key, p_dev_rec->link_key_type,
  4210. p_dev_rec->remote_supports_secure_connections);
  4211. }
  4212. } else {
  4213. if ((p_dev_rec->link_key_type == BTM_LKEY_TYPE_UNAUTH_COMB_P_256) ||
  4214. (p_dev_rec->link_key_type == BTM_LKEY_TYPE_AUTH_COMB_P_256)) {
  4215. p_dev_rec->new_encryption_key_is_p256 = TRUE;
  4216. BTM_TRACE_DEBUG ("%s set new_encr_key_256 to %d\n",
  4217. __func__, p_dev_rec->new_encryption_key_is_p256);
  4218. }
  4219. }
  4220. /* If name is not known at this point delay calling callback until the name is */
  4221. /* resolved. Unless it is a HID Device and we really need to send all link keys. */
  4222. if ((!(p_dev_rec->sec_flags & BTM_SEC_NAME_KNOWN)
  4223. && ((p_dev_rec->dev_class[1] & BTM_COD_MAJOR_CLASS_MASK) != BTM_COD_MAJOR_PERIPHERAL))
  4224. && !ltk_derived_lk) {
  4225. BTM_TRACE_EVENT ("btm_sec_link_key_notification() Delayed BDA: %08x%04x Type:%d\n",
  4226. (p_bda[0] << 24) + (p_bda[1] << 16) + (p_bda[2] << 8) + p_bda[3],
  4227. (p_bda[4] << 8) + p_bda[5], key_type);
  4228. p_dev_rec->link_key_not_sent = TRUE;
  4229. /* If it is for bonding nothing else will follow, so we need to start name resolution */
  4230. if (we_are_bonding) {
  4231. if (!(btsnd_hcic_rmt_name_req (p_bda, HCI_PAGE_SCAN_REP_MODE_R1, HCI_MANDATARY_PAGE_SCAN_MODE, 0))) {
  4232. btm_inq_rmt_name_failed();
  4233. }
  4234. }
  4235. BTM_TRACE_EVENT ("rmt_io_caps:%d, sec_flags:x%x, dev_class[1]:x%02x\n", p_dev_rec->rmt_io_caps, p_dev_rec->sec_flags, p_dev_rec->dev_class[1])
  4236. return;
  4237. }
  4238. /* If its not us who perform authentication, we should tell stackserver */
  4239. /* that some authentication has been completed */
  4240. /* This is required when different entities receive link notification and auth complete */
  4241. if (!(p_dev_rec->security_required & BTM_SEC_OUT_AUTHENTICATE)
  4242. /* for derived key, always send authentication callback for BR channel */
  4243. || ltk_derived_lk) {
  4244. if (btm_cb.api.p_auth_complete_callback) {
  4245. res = (*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr, p_dev_rec->dev_class,
  4246. p_dev_rec->sec_bd_name, HCI_SUCCESS);
  4247. if (res == BTM_SEC_DEV_REC_REMOVED) {
  4248. p_dev_rec = NULL;
  4249. }
  4250. }
  4251. }
  4252. if(!p_dev_rec) {
  4253. return;
  4254. }
  4255. /* We will save link key only if the user authorized it - BTE report link key in all cases */
  4256. #ifdef BRCM_NONE_BTE
  4257. if (p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_AUTHED)
  4258. #endif
  4259. {
  4260. if (btm_cb.api.p_link_key_callback) {
  4261. if (ltk_derived_lk) {
  4262. BTM_TRACE_DEBUG ("btm_sec_link_key_notification() LTK derived LK is saved already"
  4263. " (key_type = %d)\n", p_dev_rec->link_key_type);
  4264. } else {
  4265. (*btm_cb.api.p_link_key_callback) (p_bda, p_dev_rec->dev_class,
  4266. p_dev_rec->sec_bd_name,
  4267. p_link_key, p_dev_rec->link_key_type,
  4268. p_dev_rec->remote_supports_secure_connections);
  4269. }
  4270. }
  4271. }
  4272. }
  4273. /*******************************************************************************
  4274. **
  4275. ** Function btm_sec_link_key_request
  4276. **
  4277. ** Description This function is called when controller requests link key
  4278. **
  4279. ** Returns Pointer to the record or NULL
  4280. **
  4281. *******************************************************************************/
  4282. void btm_sec_link_key_request (UINT8 *p_bda)
  4283. {
  4284. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_or_alloc_dev (p_bda);
  4285. BTM_TRACE_EVENT ("btm_sec_link_key_request() BDA: %02x:%02x:%02x:%02x:%02x:%02x\n",
  4286. p_bda[0], p_bda[1], p_bda[2], p_bda[3], p_bda[4], p_bda[5]);
  4287. if ( (btm_cb.pairing_state == BTM_PAIR_STATE_WAIT_PIN_REQ) &&
  4288. (btm_cb.collision_start_time != 0) &&
  4289. (memcmp (btm_cb.p_collided_dev_rec->bd_addr, p_bda, BD_ADDR_LEN) == 0) ) {
  4290. BTM_TRACE_EVENT ("btm_sec_link_key_request() rejecting link key req "
  4291. "State: %d START_TIMEOUT : %d\n",
  4292. btm_cb.pairing_state, btm_cb.collision_start_time);
  4293. btsnd_hcic_link_key_neg_reply (p_bda);
  4294. return;
  4295. }
  4296. if (p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_KNOWN) {
  4297. btsnd_hcic_link_key_req_reply (p_bda, p_dev_rec->link_key);
  4298. return;
  4299. }
  4300. /* Notify L2CAP to increase timeout */
  4301. l2c_pin_code_request (p_bda);
  4302. /* The link key is not in the database and it is not known to the manager */
  4303. btsnd_hcic_link_key_neg_reply (p_bda);
  4304. }
  4305. #endif ///SMP_INCLUDED == TRUE
  4306. /*******************************************************************************
  4307. **
  4308. ** Function btm_sec_pairing_timeout
  4309. **
  4310. ** Description This function is called when host does not provide PIN
  4311. ** within requested time
  4312. **
  4313. ** Returns Pointer to the TLE struct
  4314. **
  4315. *******************************************************************************/
  4316. #if (SMP_INCLUDED == TRUE)
  4317. static void btm_sec_pairing_timeout (TIMER_LIST_ENT *p_tle)
  4318. {
  4319. tBTM_CB *p_cb = &btm_cb;
  4320. tBTM_SEC_DEV_REC *p_dev_rec;
  4321. #if BTM_OOB_INCLUDED == TRUE
  4322. #if (BTM_LOCAL_IO_CAPS == BTM_IO_CAP_NONE)
  4323. tBTM_AUTH_REQ auth_req = BTM_AUTH_AP_NO;
  4324. #else
  4325. tBTM_AUTH_REQ auth_req = BTM_AUTH_AP_YES;
  4326. #endif
  4327. #endif
  4328. UINT8 name[2];
  4329. UNUSED(p_tle);
  4330. p_cb->pairing_tle.param = 0;
  4331. /* Coverity: FALSE-POSITIVE error from Coverity tool. Please do NOT remove following comment. */
  4332. /* coverity[UNUSED_VALUE] pointer p_dev_rec is actually used several times... This is a Coverity false-positive, i.e. a fake issue.
  4333. */
  4334. p_dev_rec = btm_find_dev (p_cb->pairing_bda);
  4335. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  4336. BTM_TRACE_EVENT ("btm_sec_pairing_timeout() State: %s Flags: %u\n",
  4337. btm_pair_state_descr(p_cb->pairing_state), p_cb->pairing_flags);
  4338. #endif ///BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE
  4339. switch (p_cb->pairing_state) {
  4340. case BTM_PAIR_STATE_WAIT_PIN_REQ:
  4341. btm_sec_bond_cancel_complete();
  4342. break;
  4343. case BTM_PAIR_STATE_WAIT_LOCAL_PIN:
  4344. if ( (btm_cb.pairing_flags & BTM_PAIR_FLAGS_PRE_FETCH_PIN) == 0) {
  4345. btsnd_hcic_pin_code_neg_reply (p_cb->pairing_bda);
  4346. }
  4347. btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
  4348. /* We need to notify the UI that no longer need the PIN */
  4349. if (btm_cb.api.p_auth_complete_callback) {
  4350. if (p_dev_rec == NULL) {
  4351. name[0] = '\0';
  4352. (*btm_cb.api.p_auth_complete_callback) (p_cb->pairing_bda,
  4353. NULL,
  4354. name, HCI_ERR_CONNECTION_TOUT);
  4355. } else {
  4356. (*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr,
  4357. p_dev_rec->dev_class,
  4358. p_dev_rec->sec_bd_name, HCI_ERR_CONNECTION_TOUT);
  4359. }
  4360. }
  4361. break;
  4362. case BTM_PAIR_STATE_WAIT_NUMERIC_CONFIRM:
  4363. btsnd_hcic_user_conf_reply (p_cb->pairing_bda, FALSE);
  4364. /* btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE); */
  4365. break;
  4366. #if (BT_SSP_INCLUDED == TRUE)
  4367. case BTM_PAIR_STATE_KEY_ENTRY:
  4368. btsnd_hcic_user_passkey_neg_reply(p_cb->pairing_bda);
  4369. /* btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE); */
  4370. break;
  4371. #endif /* !BTM_IO_CAP_NONE */
  4372. #if BTM_OOB_INCLUDED == TRUE
  4373. case BTM_PAIR_STATE_WAIT_LOCAL_IOCAPS:
  4374. if (btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD) {
  4375. auth_req |= BTM_AUTH_DD_BOND;
  4376. }
  4377. btsnd_hcic_io_cap_req_reply (p_cb->pairing_bda, btm_cb.devcb.loc_io_caps,
  4378. BTM_OOB_NONE, auth_req);
  4379. btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
  4380. break;
  4381. case BTM_PAIR_STATE_WAIT_LOCAL_OOB_RSP:
  4382. btsnd_hcic_rem_oob_neg_reply (p_cb->pairing_bda);
  4383. btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
  4384. break;
  4385. #endif /* BTM_OOB_INCLUDED */
  4386. case BTM_PAIR_STATE_WAIT_DISCONNECT:
  4387. /* simple pairing failed. Started a 1-sec timer at simple pairing complete.
  4388. * now it's time to tear down the ACL link*/
  4389. if (p_dev_rec == NULL) {
  4390. BTM_TRACE_ERROR ("btm_sec_pairing_timeout() BTM_PAIR_STATE_WAIT_DISCONNECT unknown BDA: %08x%04x\n",
  4391. (p_cb->pairing_bda[0] << 24) + (p_cb->pairing_bda[1] << 16) + (p_cb->pairing_bda[2] << 8) + p_cb->pairing_bda[3],
  4392. (p_cb->pairing_bda[4] << 8) + p_cb->pairing_bda[5]);
  4393. break;
  4394. }
  4395. btm_sec_send_hci_disconnect (p_dev_rec, HCI_ERR_AUTH_FAILURE, p_dev_rec->hci_handle);
  4396. btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
  4397. break;
  4398. case BTM_PAIR_STATE_WAIT_AUTH_COMPLETE:
  4399. case BTM_PAIR_STATE_GET_REM_NAME:
  4400. /* We need to notify the UI that timeout has happened while waiting for authentication*/
  4401. btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
  4402. if (btm_cb.api.p_auth_complete_callback) {
  4403. if (p_dev_rec == NULL) {
  4404. name[0] = '\0';
  4405. (*btm_cb.api.p_auth_complete_callback) (p_cb->pairing_bda,
  4406. NULL,
  4407. name, HCI_ERR_CONNECTION_TOUT);
  4408. } else {
  4409. (*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr,
  4410. p_dev_rec->dev_class,
  4411. p_dev_rec->sec_bd_name, HCI_ERR_CONNECTION_TOUT);
  4412. }
  4413. }
  4414. break;
  4415. default:
  4416. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  4417. BTM_TRACE_WARNING ("btm_sec_pairing_timeout() not processed state: %s\n", btm_pair_state_descr(btm_cb.pairing_state));
  4418. #endif ///BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE
  4419. btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
  4420. break;
  4421. }
  4422. }
  4423. #if (CLASSIC_BT_INCLUDED == TRUE)
  4424. /*******************************************************************************
  4425. **
  4426. ** Function btm_sec_pin_code_request
  4427. **
  4428. ** Description This function is called when controller requests PIN code
  4429. **
  4430. ** Returns Pointer to the record or NULL
  4431. **
  4432. *******************************************************************************/
  4433. void btm_sec_pin_code_request (UINT8 *p_bda)
  4434. {
  4435. tBTM_SEC_DEV_REC *p_dev_rec;
  4436. tBTM_CB *p_cb = &btm_cb;
  4437. #ifdef PORCHE_PAIRING_CONFLICT
  4438. UINT8 default_pin_code_len = 4;
  4439. PIN_CODE default_pin_code = {0x30, 0x30, 0x30, 0x30};
  4440. #endif
  4441. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  4442. BTM_TRACE_EVENT ("btm_sec_pin_code_request() State: %s, BDA:%04x%08x\n",
  4443. btm_pair_state_descr(btm_cb.pairing_state),
  4444. (p_bda[0] << 8) + p_bda[1], (p_bda[2] << 24) + (p_bda[3] << 16) + (p_bda[4] << 8) + p_bda[5] );
  4445. #endif ///BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE
  4446. if (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE) {
  4447. if ( (memcmp (p_bda, btm_cb.pairing_bda, BD_ADDR_LEN) == 0) &&
  4448. (btm_cb.pairing_state == BTM_PAIR_STATE_WAIT_AUTH_COMPLETE) ) {
  4449. /* fake this out - porshe carkit issue - */
  4450. // btm_cb.pairing_state = BTM_PAIR_STATE_IDLE;
  4451. if (! btm_cb.pin_code_len_saved) {
  4452. btsnd_hcic_pin_code_neg_reply (p_bda);
  4453. return;
  4454. } else {
  4455. btsnd_hcic_pin_code_req_reply (p_bda, btm_cb.pin_code_len_saved, p_cb->pin_code);
  4456. return;
  4457. }
  4458. } else if ((btm_cb.pairing_state != BTM_PAIR_STATE_WAIT_PIN_REQ)
  4459. || memcmp (p_bda, btm_cb.pairing_bda, BD_ADDR_LEN) != 0) {
  4460. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  4461. BTM_TRACE_WARNING ("btm_sec_pin_code_request() rejected - state: %s\n",
  4462. btm_pair_state_descr(btm_cb.pairing_state));
  4463. #endif ///BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE
  4464. #ifdef PORCHE_PAIRING_CONFLICT
  4465. /* reply pin code again due to counter in_rand when local initiates pairing */
  4466. BTM_TRACE_EVENT ("btm_sec_pin_code_request from remote dev. for local initiated pairing\n");
  4467. if (! btm_cb.pin_code_len_saved) {
  4468. btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_AUTH_COMPLETE);
  4469. btsnd_hcic_pin_code_req_reply (p_bda, default_pin_code_len, default_pin_code);
  4470. } else {
  4471. btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_AUTH_COMPLETE);
  4472. btsnd_hcic_pin_code_req_reply (p_bda, btm_cb.pin_code_len_saved, p_cb->pin_code);
  4473. }
  4474. #else
  4475. btsnd_hcic_pin_code_neg_reply (p_bda);
  4476. #endif
  4477. return;
  4478. }
  4479. }
  4480. p_dev_rec = btm_find_or_alloc_dev (p_bda);
  4481. /* received PIN code request. must be non-sm4 */
  4482. p_dev_rec->sm4 = BTM_SM4_KNOWN;
  4483. if (btm_cb.pairing_state == BTM_PAIR_STATE_IDLE) {
  4484. memcpy (btm_cb.pairing_bda, p_bda, BD_ADDR_LEN);
  4485. btm_cb.pairing_flags = BTM_PAIR_FLAGS_PEER_STARTED_DD;
  4486. /* Make sure we reset the trusted mask to help against attacks */
  4487. BTM_SEC_CLR_TRUSTED_DEVICE(p_dev_rec->trusted_mask);
  4488. }
  4489. if (!p_cb->pairing_disabled && (p_cb->cfg.pin_type == HCI_PIN_TYPE_FIXED)) {
  4490. BTM_TRACE_EVENT ("btm_sec_pin_code_request fixed pin replying\n");
  4491. btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_AUTH_COMPLETE);
  4492. btsnd_hcic_pin_code_req_reply (p_bda, p_cb->cfg.pin_code_len, p_cb->cfg.pin_code);
  4493. return;
  4494. }
  4495. /* Use the connecting device's CoD for the connection */
  4496. if ( (!memcmp (p_bda, p_cb->connecting_bda, BD_ADDR_LEN))
  4497. && (p_cb->connecting_dc[0] || p_cb->connecting_dc[1] || p_cb->connecting_dc[2]) ) {
  4498. memcpy (p_dev_rec->dev_class, p_cb->connecting_dc, DEV_CLASS_LEN);
  4499. }
  4500. /* We could have started connection after asking user for the PIN code */
  4501. if (btm_cb.pin_code_len != 0) {
  4502. BTM_TRACE_EVENT ("btm_sec_pin_code_request bonding sending reply\n");
  4503. btsnd_hcic_pin_code_req_reply (p_bda, btm_cb.pin_code_len, p_cb->pin_code);
  4504. #ifdef PORCHE_PAIRING_CONFLICT
  4505. btm_cb.pin_code_len_saved = btm_cb.pin_code_len;
  4506. #endif
  4507. /* Mark that we forwarded received from the user PIN code */
  4508. btm_cb.pin_code_len = 0;
  4509. /* We can change mode back right away, that other connection being established */
  4510. /* is not forced to be secure - found a FW issue, so we can not do this
  4511. btm_restore_mode(); */
  4512. btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_AUTH_COMPLETE);
  4513. }
  4514. /* If pairing disabled OR (no PIN callback and not bonding) */
  4515. /* OR we could not allocate entry in the database reject pairing request */
  4516. else if (p_cb->pairing_disabled
  4517. || (p_cb->api.p_pin_callback == NULL)
  4518. /* OR Microsoft keyboard can for some reason try to establish connection */
  4519. /* the only thing we can do here is to shut it up. Normally we will be originator */
  4520. /* for keyboard bonding */
  4521. || (!p_dev_rec->is_originator
  4522. && ((p_dev_rec->dev_class[1] & BTM_COD_MAJOR_CLASS_MASK) == BTM_COD_MAJOR_PERIPHERAL)
  4523. && (p_dev_rec->dev_class[2] & BTM_COD_MINOR_KEYBOARD)) ) {
  4524. BTM_TRACE_WARNING("btm_sec_pin_code_request(): Pairing disabled:%d; PIN callback:%p, Dev Rec:%p!\n",
  4525. p_cb->pairing_disabled, p_cb->api.p_pin_callback, p_dev_rec);
  4526. btsnd_hcic_pin_code_neg_reply (p_bda);
  4527. }
  4528. /* Notify upper layer of PIN request and start expiration timer */
  4529. else {
  4530. btm_cb.pin_code_len_saved = 0;
  4531. btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_LOCAL_PIN);
  4532. /* Pin code request can not come at the same time as connection request */
  4533. memcpy (p_cb->connecting_bda, p_bda, BD_ADDR_LEN);
  4534. memcpy (p_cb->connecting_dc, p_dev_rec->dev_class, DEV_CLASS_LEN);
  4535. BTM_TRACE_EVENT ("btm_sec_pin_code_request going for callback\n");
  4536. btm_cb.pairing_flags |= BTM_PAIR_FLAGS_PIN_REQD;
  4537. if (p_cb->api.p_pin_callback) {
  4538. (*p_cb->api.p_pin_callback) (p_bda, p_dev_rec->dev_class, p_dev_rec->sec_bd_name,
  4539. (p_dev_rec->p_cur_service == NULL) ? FALSE
  4540. : (p_dev_rec->p_cur_service->security_flags
  4541. & BTM_SEC_IN_MIN_16_DIGIT_PIN));
  4542. }
  4543. }
  4544. return;
  4545. }
  4546. #endif ///CLASSIC_BT_INCLUDED == TRUE
  4547. #endif ///SMP_INCLUDED == TRUE
  4548. /*******************************************************************************
  4549. **
  4550. ** Function btm_sec_update_clock_offset
  4551. **
  4552. ** Description This function is called to update clock offset
  4553. **
  4554. ** Returns void
  4555. **
  4556. *******************************************************************************/
  4557. void btm_sec_update_clock_offset (UINT16 handle, UINT16 clock_offset)
  4558. {
  4559. tBTM_SEC_DEV_REC *p_dev_rec;
  4560. tBTM_INQ_INFO *p_inq_info;
  4561. if ((p_dev_rec = btm_find_dev_by_handle (handle)) == NULL) {
  4562. return;
  4563. }
  4564. p_dev_rec->clock_offset = clock_offset | BTM_CLOCK_OFFSET_VALID;
  4565. if ((p_inq_info = BTM_InqDbRead(p_dev_rec->bd_addr)) == NULL) {
  4566. return;
  4567. }
  4568. p_inq_info->results.clock_offset = clock_offset | BTM_CLOCK_OFFSET_VALID;
  4569. }
  4570. /******************************************************************
  4571. ** S T A T I C F U N C T I O N S
  4572. *******************************************************************/
  4573. /*******************************************************************************
  4574. **
  4575. ** Function btm_sec_execute_procedure
  4576. **
  4577. ** Description This function is called to start required security
  4578. ** procedure. There is a case when multiplexing protocol
  4579. ** calls this function on the originating side, connection to
  4580. ** the peer will not be established. This function in this
  4581. ** case performs only authorization.
  4582. **
  4583. ** Returns BTM_SUCCESS - permission is granted
  4584. ** BTM_CMD_STARTED - in process
  4585. ** BTM_NO_RESOURCES - permission declined
  4586. **
  4587. *******************************************************************************/
  4588. #if (SMP_INCLUDED == TRUE)
  4589. static tBTM_STATUS btm_sec_execute_procedure (tBTM_SEC_DEV_REC *p_dev_rec)
  4590. {
  4591. BTM_TRACE_EVENT ("btm_sec_execute_procedure: Required:0x%x Flags:0x%x State:%d\n",
  4592. p_dev_rec->security_required, p_dev_rec->sec_flags, p_dev_rec->sec_state);
  4593. /* There is a chance that we are getting name. Wait until done. */
  4594. if (p_dev_rec->sec_state != 0) {
  4595. return (BTM_CMD_STARTED);
  4596. }
  4597. /* If any security is required, get the name first */
  4598. if (!(p_dev_rec->sec_flags & BTM_SEC_NAME_KNOWN)
  4599. && (p_dev_rec->hci_handle != BTM_SEC_INVALID_HANDLE)) {
  4600. BTM_TRACE_EVENT ("Security Manager: Start get name\n");
  4601. if (!btm_sec_start_get_name (p_dev_rec)) {
  4602. return (BTM_NO_RESOURCES);
  4603. }
  4604. return (BTM_CMD_STARTED);
  4605. }
  4606. #if (CLASSIC_BT_INCLUDED == TRUE)
  4607. tACL_CONN *p_acl_cb = btm_handle_to_acl(p_dev_rec->hci_handle);
  4608. /*
  4609. * To prevent a remote device from doing a Bluetooth Impersonation Attack, a suggested fix by SIG is:
  4610. *
  4611. * "Hosts performing legacy (non-mutual) authentication must ensure a remote device is authenticated
  4612. * prior to proceeding with encryption establishment, regardless of role."
  4613. *
  4614. * As an implementation, we enforce mutual authentication when devices use Legacy Authentication.
  4615. */
  4616. if ((p_acl_cb != NULL) && (BTM_BothEndsSupportSecureConnections(p_acl_cb->remote_addr) == 0) &&
  4617. ((p_acl_cb->legacy_auth_state & BTM_ACL_LEGACY_AUTH_SELF) == 0)) {
  4618. p_dev_rec->sec_flags &= ~BTM_SEC_AUTHENTICATED;
  4619. }
  4620. #endif
  4621. /* If connection is not authenticated and authentication is required */
  4622. /* start authentication and return PENDING to the caller */
  4623. if ((((!(p_dev_rec->sec_flags & BTM_SEC_AUTHENTICATED))
  4624. && (( p_dev_rec->is_originator && (p_dev_rec->security_required & BTM_SEC_OUT_AUTHENTICATE))
  4625. || (!p_dev_rec->is_originator && (p_dev_rec->security_required & BTM_SEC_IN_AUTHENTICATE))))
  4626. || (!(p_dev_rec->sec_flags & BTM_SEC_16_DIGIT_PIN_AUTHED)
  4627. && (!p_dev_rec->is_originator
  4628. && (p_dev_rec->security_required & BTM_SEC_IN_MIN_16_DIGIT_PIN))))
  4629. && (p_dev_rec->hci_handle != BTM_SEC_INVALID_HANDLE)) {
  4630. /*
  4631. * We rely on BTM_SEC_16_DIGIT_PIN_AUTHED being set if MITM is in use,
  4632. * as 16 DIGIT is only needed if MITM is not used. Unfortunately, the
  4633. * BTM_SEC_AUTHENTICATED is used for both MITM and non-MITM
  4634. * authenticated connections, hence we cannot distinguish here.
  4635. */
  4636. #if (L2CAP_UCD_INCLUDED == TRUE)
  4637. /* if incoming UCD packet, discard it */
  4638. if ( !p_dev_rec->is_originator && (p_dev_rec->is_ucd == TRUE )) {
  4639. return (BTM_FAILED_ON_SECURITY);
  4640. }
  4641. #endif
  4642. BTM_TRACE_EVENT ("Security Manager: Start authentication\n");
  4643. /*
  4644. * If we do have a link-key, but we end up here because we need an
  4645. * upgrade, then clear the link-key known and authenticated flag before
  4646. * restarting authentication.
  4647. * WARNING: If the controller has link-key, it is optional and
  4648. * recommended for the controller to send a Link_Key_Request.
  4649. * In case we need an upgrade, the only alternative would be to delete
  4650. * the existing link-key. That could lead to very bad user experience
  4651. * or even IOP issues, if a reconnect causes a new connection that
  4652. * requires an upgrade.
  4653. */
  4654. if ((p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_KNOWN)
  4655. && (!(p_dev_rec->sec_flags & BTM_SEC_16_DIGIT_PIN_AUTHED)
  4656. && (!p_dev_rec->is_originator && (p_dev_rec->security_required
  4657. & BTM_SEC_IN_MIN_16_DIGIT_PIN)))) {
  4658. p_dev_rec->sec_flags &= ~(BTM_SEC_LINK_KEY_KNOWN | BTM_SEC_LINK_KEY_AUTHED
  4659. | BTM_SEC_AUTHENTICATED);
  4660. }
  4661. if (!btm_sec_start_authentication (p_dev_rec)) {
  4662. return (BTM_NO_RESOURCES);
  4663. }
  4664. return (BTM_CMD_STARTED);
  4665. }
  4666. /* If connection is not encrypted and encryption is required */
  4667. /* start encryption and return PENDING to the caller */
  4668. if (!(p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED)
  4669. && (( p_dev_rec->is_originator && (p_dev_rec->security_required & BTM_SEC_OUT_ENCRYPT))
  4670. || (!p_dev_rec->is_originator && (p_dev_rec->security_required & BTM_SEC_IN_ENCRYPT)))
  4671. && (p_dev_rec->hci_handle != BTM_SEC_INVALID_HANDLE)) {
  4672. #if (L2CAP_UCD_INCLUDED == TRUE)
  4673. /* if incoming UCD packet, discard it */
  4674. if ( !p_dev_rec->is_originator && (p_dev_rec->is_ucd == TRUE )) {
  4675. return (BTM_FAILED_ON_SECURITY);
  4676. }
  4677. #endif
  4678. BTM_TRACE_EVENT ("Security Manager: Start encryption\n");
  4679. if (!btm_sec_start_encryption (p_dev_rec)) {
  4680. return (BTM_NO_RESOURCES);
  4681. }
  4682. return (BTM_CMD_STARTED);
  4683. }
  4684. if ((p_dev_rec->security_required & BTM_SEC_MODE4_LEVEL4) &&
  4685. (p_dev_rec->link_key_type != BTM_LKEY_TYPE_AUTH_COMB_P_256)) {
  4686. BTM_TRACE_EVENT("%s: Security Manager: SC only service, but link key type is 0x%02x -"
  4687. "security failure\n", __FUNCTION__, p_dev_rec->link_key_type);
  4688. return (BTM_FAILED_ON_SECURITY);
  4689. }
  4690. /* If connection is not authorized and authorization is required */
  4691. /* start authorization and return PENDING to the caller */
  4692. if (!(p_dev_rec->sec_flags & BTM_SEC_AUTHORIZED)
  4693. && (( p_dev_rec->is_originator && (p_dev_rec->security_required & BTM_SEC_OUT_AUTHORIZE))
  4694. || (!p_dev_rec->is_originator && (p_dev_rec->security_required & BTM_SEC_IN_AUTHORIZE)))) {
  4695. BTM_TRACE_EVENT ("service id:%d, is trusted:%d\n",
  4696. p_dev_rec->p_cur_service->service_id,
  4697. (BTM_SEC_IS_SERVICE_TRUSTED(p_dev_rec->trusted_mask,
  4698. p_dev_rec->p_cur_service->service_id)));
  4699. if ((btm_sec_are_all_trusted(p_dev_rec->trusted_mask) == FALSE) &&
  4700. (p_dev_rec->p_cur_service->service_id < BTM_SEC_MAX_SERVICES) &&
  4701. (BTM_SEC_IS_SERVICE_TRUSTED(p_dev_rec->trusted_mask,
  4702. p_dev_rec->p_cur_service->service_id) == FALSE)) {
  4703. BTM_TRACE_EVENT ("Security Manager: Start authorization\n");
  4704. return (btm_sec_start_authorization (p_dev_rec));
  4705. }
  4706. }
  4707. /* All required security procedures already established */
  4708. p_dev_rec->security_required &= ~(BTM_SEC_OUT_AUTHORIZE | BTM_SEC_IN_AUTHORIZE |
  4709. BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_IN_AUTHENTICATE |
  4710. BTM_SEC_OUT_ENCRYPT | BTM_SEC_IN_ENCRYPT |
  4711. BTM_SEC_FORCE_MASTER | BTM_SEC_ATTEMPT_MASTER |
  4712. BTM_SEC_FORCE_SLAVE | BTM_SEC_ATTEMPT_SLAVE);
  4713. BTM_TRACE_EVENT ("Security Manager: trusted:0x%04x%04x\n", p_dev_rec->trusted_mask[1], p_dev_rec->trusted_mask[0]);
  4714. BTM_TRACE_EVENT ("Security Manager: access granted\n");
  4715. return (BTM_SUCCESS);
  4716. }
  4717. /*******************************************************************************
  4718. **
  4719. ** Function btm_sec_start_get_name
  4720. **
  4721. ** Description This function is called to start get name procedure
  4722. **
  4723. ** Returns TRUE if started
  4724. **
  4725. *******************************************************************************/
  4726. static BOOLEAN btm_sec_start_get_name (tBTM_SEC_DEV_REC *p_dev_rec)
  4727. {
  4728. UINT8 tempstate = p_dev_rec->sec_state;
  4729. p_dev_rec->sec_state = BTM_SEC_STATE_GETTING_NAME;
  4730. /* Device should be connected, no need to provide correct page params */
  4731. /* 0 and NULL are as timeout and callback params because they are not used in security get name case */
  4732. if ((btm_initiate_rem_name (p_dev_rec->bd_addr, NULL, BTM_RMT_NAME_SEC,
  4733. 0, NULL)) != BTM_CMD_STARTED) {
  4734. p_dev_rec->sec_state = tempstate;
  4735. return (FALSE);
  4736. }
  4737. return (TRUE);
  4738. }
  4739. /*******************************************************************************
  4740. **
  4741. ** Function btm_sec_start_authentication
  4742. **
  4743. ** Description This function is called to start authentication
  4744. **
  4745. ** Returns TRUE if started
  4746. **
  4747. *******************************************************************************/
  4748. static BOOLEAN btm_sec_start_authentication (tBTM_SEC_DEV_REC *p_dev_rec)
  4749. {
  4750. p_dev_rec->sec_state = BTM_SEC_STATE_AUTHENTICATING;
  4751. return (btsnd_hcic_auth_request (p_dev_rec->hci_handle));
  4752. }
  4753. /*******************************************************************************
  4754. **
  4755. ** Function btm_sec_start_encryption
  4756. **
  4757. ** Description This function is called to start encryption
  4758. **
  4759. ** Returns TRUE if started
  4760. **
  4761. *******************************************************************************/
  4762. static BOOLEAN btm_sec_start_encryption (tBTM_SEC_DEV_REC *p_dev_rec)
  4763. {
  4764. if (!btsnd_hcic_set_conn_encrypt (p_dev_rec->hci_handle, TRUE)) {
  4765. return (FALSE);
  4766. }
  4767. p_dev_rec->sec_state = BTM_SEC_STATE_ENCRYPTING;
  4768. return (TRUE);
  4769. }
  4770. /*******************************************************************************
  4771. **
  4772. ** Function btm_sec_start_authorization
  4773. **
  4774. ** Description This function is called to start authorization
  4775. **
  4776. ** Returns TRUE if started
  4777. **
  4778. *******************************************************************************/
  4779. static UINT8 btm_sec_start_authorization (tBTM_SEC_DEV_REC *p_dev_rec)
  4780. {
  4781. UINT8 result;
  4782. UINT8 *p_service_name = NULL;
  4783. UINT8 service_id;
  4784. if ((p_dev_rec->sec_flags & BTM_SEC_NAME_KNOWN)
  4785. || (p_dev_rec->hci_handle == BTM_SEC_INVALID_HANDLE)) {
  4786. if (!btm_cb.api.p_authorize_callback) {
  4787. return (BTM_MODE_UNSUPPORTED);
  4788. }
  4789. if (p_dev_rec->p_cur_service) {
  4790. #if BTM_SEC_SERVICE_NAME_LEN > 0
  4791. if (p_dev_rec->is_originator) {
  4792. p_service_name = p_dev_rec->p_cur_service->orig_service_name;
  4793. } else {
  4794. p_service_name = p_dev_rec->p_cur_service->term_service_name;
  4795. }
  4796. #endif
  4797. service_id = p_dev_rec->p_cur_service->service_id;
  4798. } else {
  4799. service_id = 0;
  4800. }
  4801. /* Send authorization request if not already sent during this service connection */
  4802. if (p_dev_rec->last_author_service_id == BTM_SEC_NO_LAST_SERVICE_ID
  4803. || p_dev_rec->last_author_service_id != service_id) {
  4804. p_dev_rec->sec_state = BTM_SEC_STATE_AUTHORIZING;
  4805. result = (*btm_cb.api.p_authorize_callback) (p_dev_rec->bd_addr,
  4806. p_dev_rec->dev_class,
  4807. p_dev_rec->sec_bd_name,
  4808. p_service_name,
  4809. service_id,
  4810. p_dev_rec->is_originator);
  4811. }
  4812. else { /* Already authorized once for this L2CAP bringup */
  4813. BTM_TRACE_DEBUG ("btm_sec_start_authorization: (Ignoring extra Authorization prompt for service %d)\n", service_id);
  4814. return (BTM_SUCCESS);
  4815. }
  4816. if (result == BTM_SUCCESS) {
  4817. p_dev_rec->sec_flags |= BTM_SEC_AUTHORIZED;
  4818. /* Save the currently authorized service in case we are asked again by another multiplexer layer */
  4819. if (!p_dev_rec->is_originator) {
  4820. p_dev_rec->last_author_service_id = service_id;
  4821. }
  4822. p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
  4823. }
  4824. return (result);
  4825. }
  4826. btm_sec_start_get_name (p_dev_rec);
  4827. return (BTM_CMD_STARTED);
  4828. }
  4829. #endif ///SMP_INCLUDED == TRUE
  4830. /*******************************************************************************
  4831. **
  4832. ** Function btm_sec_are_all_trusted
  4833. **
  4834. ** Description This function is called check if all services are trusted
  4835. **
  4836. ** Returns TRUE if all are trusted, otherwise FALSE
  4837. **
  4838. *******************************************************************************/
  4839. BOOLEAN btm_sec_are_all_trusted(UINT32 p_mask[])
  4840. {
  4841. UINT32 trusted_inx;
  4842. for (trusted_inx = 0; trusted_inx < BTM_SEC_SERVICE_ARRAY_SIZE; trusted_inx++) {
  4843. if (p_mask[trusted_inx] != BTM_SEC_TRUST_ALL) {
  4844. return (FALSE);
  4845. }
  4846. }
  4847. return (TRUE);
  4848. }
  4849. /*******************************************************************************
  4850. **
  4851. ** Function btm_sec_find_first_serv
  4852. **
  4853. ** Description Look for the first record in the service database
  4854. ** with specified PSM
  4855. **
  4856. ** Returns Pointer to the record or NULL
  4857. **
  4858. *******************************************************************************/
  4859. #if (SMP_INCLUDED == TRUE)
  4860. tBTM_SEC_SERV_REC *btm_sec_find_first_serv (CONNECTION_TYPE conn_type, UINT16 psm)
  4861. {
  4862. tBTM_SEC_SERV_REC *p_serv_rec = &btm_cb.sec_serv_rec[0];
  4863. int i;
  4864. BOOLEAN is_originator;
  4865. #if (L2CAP_UCD_INCLUDED == TRUE)
  4866. if ( conn_type & CONNECTION_TYPE_ORIG_MASK ) {
  4867. is_originator = TRUE;
  4868. } else {
  4869. is_originator = FALSE;
  4870. }
  4871. #else
  4872. is_originator = conn_type;
  4873. #endif
  4874. if (is_originator && btm_cb.p_out_serv && btm_cb.p_out_serv->psm == psm) {
  4875. /* If this is outgoing connection and the PSM matches p_out_serv,
  4876. * use it as the current service */
  4877. return btm_cb.p_out_serv;
  4878. }
  4879. /* otherwise, just find the first record with the specified PSM */
  4880. for (i = 0; i < BTM_SEC_MAX_SERVICE_RECORDS; i++, p_serv_rec++) {
  4881. if ( (p_serv_rec->security_flags & BTM_SEC_IN_USE) && (p_serv_rec->psm == psm) ) {
  4882. return (p_serv_rec);
  4883. }
  4884. }
  4885. return (NULL);
  4886. }
  4887. /*******************************************************************************
  4888. **
  4889. ** Function btm_sec_find_next_serv
  4890. **
  4891. ** Description Look for the next record in the service database
  4892. ** with specified PSM
  4893. **
  4894. ** Returns Pointer to the record or NULL
  4895. **
  4896. *******************************************************************************/
  4897. static tBTM_SEC_SERV_REC *btm_sec_find_next_serv (tBTM_SEC_SERV_REC *p_cur)
  4898. {
  4899. tBTM_SEC_SERV_REC *p_serv_rec = &btm_cb.sec_serv_rec[0];
  4900. int i;
  4901. for (i = 0; i < BTM_SEC_MAX_SERVICE_RECORDS; i++, p_serv_rec++) {
  4902. if ((p_serv_rec->security_flags & BTM_SEC_IN_USE)
  4903. && (p_serv_rec->psm == p_cur->psm) ) {
  4904. if (p_cur != p_serv_rec) {
  4905. return (p_serv_rec);
  4906. }
  4907. }
  4908. }
  4909. return (NULL);
  4910. }
  4911. /*******************************************************************************
  4912. **
  4913. ** Function btm_sec_find_mx_serv
  4914. **
  4915. ** Description Look for the record in the service database with specified
  4916. ** PSM and multiplexor channel information
  4917. **
  4918. ** Returns Pointer to the record or NULL
  4919. **
  4920. *******************************************************************************/
  4921. static tBTM_SEC_SERV_REC *btm_sec_find_mx_serv (UINT8 is_originator, UINT16 psm,
  4922. UINT32 mx_proto_id, UINT32 mx_chan_id)
  4923. {
  4924. tBTM_SEC_SERV_REC *p_out_serv = btm_cb.p_out_serv;
  4925. tBTM_SEC_SERV_REC *p_serv_rec = &btm_cb.sec_serv_rec[0];
  4926. int i;
  4927. BTM_TRACE_DEBUG ("%s()\n", __func__);
  4928. if (is_originator && p_out_serv && p_out_serv->psm == psm
  4929. && p_out_serv->mx_proto_id == mx_proto_id
  4930. && p_out_serv->orig_mx_chan_id == mx_chan_id) {
  4931. /* If this is outgoing connection and the parameters match p_out_serv,
  4932. * use it as the current service */
  4933. return btm_cb.p_out_serv;
  4934. }
  4935. /* otherwise, the old way */
  4936. for (i = 0; i < BTM_SEC_MAX_SERVICE_RECORDS; i++, p_serv_rec++) {
  4937. if ((p_serv_rec->security_flags & BTM_SEC_IN_USE)
  4938. && (p_serv_rec->psm == psm)
  4939. && (p_serv_rec->mx_proto_id == mx_proto_id)
  4940. && (( is_originator && (p_serv_rec->orig_mx_chan_id == mx_chan_id))
  4941. || (!is_originator && (p_serv_rec->term_mx_chan_id == mx_chan_id)))) {
  4942. return (p_serv_rec);
  4943. }
  4944. }
  4945. return (NULL);
  4946. }
  4947. #endif ///SMP_INCLUDED == TRUE
  4948. /*******************************************************************************
  4949. **
  4950. ** Function btm_sec_collision_timeout
  4951. **
  4952. ** Description Encryption could not start because of the collision
  4953. ** try to do it again
  4954. **
  4955. ** Returns Pointer to the TLE struct
  4956. **
  4957. *******************************************************************************/
  4958. #if (SMP_INCLUDED == TRUE)
  4959. static void btm_sec_collision_timeout (TIMER_LIST_ENT *p_tle)
  4960. {
  4961. UNUSED(p_tle);
  4962. BTM_TRACE_EVENT ("%s()\n", __func__);
  4963. btm_cb.sec_collision_tle.param = 0;
  4964. tBTM_STATUS status = btm_sec_execute_procedure (btm_cb.p_collided_dev_rec);
  4965. /* If result is pending reply from the user or from the device is pending */
  4966. if (status != BTM_CMD_STARTED) {
  4967. /* There is no next procedure or start of procedure failed, notify the waiting layer */
  4968. btm_sec_dev_rec_cback_event (btm_cb.p_collided_dev_rec, status, FALSE);
  4969. }
  4970. }
  4971. /*******************************************************************************
  4972. **
  4973. ** Function btm_send_link_key_notif
  4974. **
  4975. ** Description This function is called when controller requests link key
  4976. **
  4977. ** Returns Pointer to the record or NULL
  4978. **
  4979. *******************************************************************************/
  4980. static void btm_send_link_key_notif (tBTM_SEC_DEV_REC *p_dev_rec)
  4981. {
  4982. if (btm_cb.api.p_link_key_callback) {
  4983. (*btm_cb.api.p_link_key_callback) (p_dev_rec->bd_addr, p_dev_rec->dev_class,
  4984. p_dev_rec->sec_bd_name, p_dev_rec->link_key,
  4985. p_dev_rec->link_key_type,
  4986. p_dev_rec->remote_supports_secure_connections);
  4987. }
  4988. }
  4989. #endif ///SMP_INCLUDED == TRUE
  4990. /*******************************************************************************
  4991. **
  4992. ** Function BTM_ReadTrustedMask
  4993. **
  4994. ** Description Get trusted mask for the peer device
  4995. **
  4996. ** Parameters: bd_addr - Address of the device
  4997. **
  4998. ** Returns NULL, if the device record is not found.
  4999. ** otherwise, the trusted mask
  5000. **
  5001. *******************************************************************************/
  5002. UINT32 *BTM_ReadTrustedMask (BD_ADDR bd_addr)
  5003. {
  5004. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
  5005. if (p_dev_rec != NULL) {
  5006. return (p_dev_rec->trusted_mask);
  5007. }
  5008. return NULL;
  5009. }
  5010. /*******************************************************************************
  5011. **
  5012. ** Function btm_restore_mode
  5013. **
  5014. ** Description This function returns the security mode to previous setting
  5015. ** if it was changed during bonding.
  5016. **
  5017. **
  5018. ** Parameters: void
  5019. **
  5020. *******************************************************************************/
  5021. #if (SMP_INCLUDED == TRUE)
  5022. static void btm_restore_mode(void)
  5023. {
  5024. if (btm_cb.security_mode_changed) {
  5025. btm_cb.security_mode_changed = FALSE;
  5026. BTM_TRACE_DEBUG("%s() Auth enable -> %d\n", __func__, (btm_cb.security_mode == BTM_SEC_MODE_LINK));
  5027. btsnd_hcic_write_auth_enable ((UINT8)(btm_cb.security_mode == BTM_SEC_MODE_LINK));
  5028. }
  5029. #if (CLASSIC_BT_INCLUDED == TRUE)
  5030. if (btm_cb.pin_type_changed) {
  5031. btm_cb.pin_type_changed = FALSE;
  5032. btsnd_hcic_write_pin_type (btm_cb.cfg.pin_type);
  5033. }
  5034. #endif ///CLASSIC_BT_INCLUDED == TRUE
  5035. }
  5036. #endif ///SMP_INCLUDED == TRUE
  5037. /*******************************************************************************
  5038. **
  5039. ** Function btm_sec_find_dev_by_sec_state
  5040. **
  5041. ** Description Look for the record in the device database for the device
  5042. ** which is being authenticated or encrypted
  5043. **
  5044. ** Returns Pointer to the record or NULL
  5045. **
  5046. *******************************************************************************/
  5047. tBTM_SEC_DEV_REC *btm_sec_find_dev_by_sec_state (UINT8 state)
  5048. {
  5049. #if (SMP_INCLUDED == TRUE)
  5050. tBTM_SEC_DEV_REC *p_dev_rec = NULL;
  5051. list_node_t *p_node = NULL;
  5052. for (p_node = list_begin(btm_cb.p_sec_dev_rec_list); p_node; p_node = list_next(p_node)) {
  5053. p_dev_rec = list_node(p_node);
  5054. if ((p_dev_rec->sec_flags & BTM_SEC_IN_USE)
  5055. && (p_dev_rec->sec_state == state)) {
  5056. return (p_dev_rec);
  5057. }
  5058. }
  5059. #endif ///SMP_INCLUDED == TRUE
  5060. return (NULL);
  5061. }
  5062. /*******************************************************************************
  5063. **
  5064. ** Function btm_sec_change_pairing_state
  5065. **
  5066. ** Description This function is called to change pairing state
  5067. **
  5068. *******************************************************************************/
  5069. #if (SMP_INCLUDED == TRUE)
  5070. static void btm_sec_change_pairing_state (tBTM_PAIRING_STATE new_state)
  5071. {
  5072. tBTM_PAIRING_STATE old_state = btm_cb.pairing_state;
  5073. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  5074. BTM_TRACE_EVENT ("%s() Old: %s\n", __func__, btm_pair_state_descr(btm_cb.pairing_state));
  5075. BTM_TRACE_EVENT ("%s() New: %s pairing_flags:0x%x\n\n", __func__,
  5076. btm_pair_state_descr(new_state), btm_cb.pairing_flags);
  5077. #endif ///BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE
  5078. btm_cb.pairing_state = new_state;
  5079. if (new_state == BTM_PAIR_STATE_IDLE) {
  5080. btu_stop_timer (&btm_cb.pairing_tle);
  5081. btm_cb.pairing_flags = 0;
  5082. #if (CLASSIC_BT_INCLUDED == TRUE)
  5083. btm_cb.pin_code_len = 0;
  5084. #endif ///CLASSIC_BT_INCLUDED == TRUE
  5085. /* Make sure the the lcb shows we are not bonding */
  5086. l2cu_update_lcb_4_bonding (btm_cb.pairing_bda, FALSE);
  5087. btm_restore_mode();
  5088. btm_sec_check_pending_reqs();
  5089. btm_inq_clear_ssp();
  5090. memset (btm_cb.pairing_bda, 0xFF, BD_ADDR_LEN);
  5091. } else {
  5092. /* If transitionng out of idle, mark the lcb as bonding */
  5093. if (old_state == BTM_PAIR_STATE_IDLE) {
  5094. l2cu_update_lcb_4_bonding (btm_cb.pairing_bda, TRUE);
  5095. }
  5096. btm_cb.pairing_tle.param = (TIMER_PARAM_TYPE)btm_sec_pairing_timeout;
  5097. btu_start_timer (&btm_cb.pairing_tle, BTU_TTYPE_USER_FUNC, BTM_SEC_TIMEOUT_VALUE);
  5098. }
  5099. }
  5100. #endif ///SMP_INCLUDED == TRUE
  5101. /*******************************************************************************
  5102. **
  5103. ** Function btm_pair_state_descr
  5104. **
  5105. ** Description Return state description for tracing
  5106. **
  5107. *******************************************************************************/
  5108. #if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
  5109. static char *btm_pair_state_descr (tBTM_PAIRING_STATE state)
  5110. {
  5111. #if (BT_TRACE_VERBOSE == TRUE)
  5112. switch (state) {
  5113. case BTM_PAIR_STATE_IDLE: return ("IDLE");
  5114. case BTM_PAIR_STATE_GET_REM_NAME: return ("GET_REM_NAME");
  5115. case BTM_PAIR_STATE_WAIT_PIN_REQ: return ("WAIT_PIN_REQ");
  5116. case BTM_PAIR_STATE_WAIT_LOCAL_PIN: return ("WAIT_LOCAL_PIN");
  5117. case BTM_PAIR_STATE_WAIT_NUMERIC_CONFIRM: return ("WAIT_NUM_CONFIRM");
  5118. case BTM_PAIR_STATE_KEY_ENTRY: return ("KEY_ENTRY");
  5119. case BTM_PAIR_STATE_WAIT_LOCAL_OOB_RSP: return ("WAIT_LOCAL_OOB_RSP");
  5120. case BTM_PAIR_STATE_WAIT_LOCAL_IOCAPS: return ("WAIT_LOCAL_IOCAPS");
  5121. case BTM_PAIR_STATE_INCOMING_SSP: return ("INCOMING_SSP");
  5122. case BTM_PAIR_STATE_WAIT_AUTH_COMPLETE: return ("WAIT_AUTH_COMPLETE");
  5123. case BTM_PAIR_STATE_WAIT_DISCONNECT: return ("WAIT_DISCONNECT");
  5124. }
  5125. return ("???");
  5126. #else
  5127. sprintf(btm_cb.state_temp_buffer, "%d", state);
  5128. return (btm_cb.state_temp_buffer);
  5129. #endif
  5130. }
  5131. #endif
  5132. /*******************************************************************************
  5133. **
  5134. ** Function btm_sec_dev_rec_cback_event
  5135. **
  5136. ** Description This function calls the callback function with the given
  5137. ** result and clear the callback function.
  5138. **
  5139. ** Parameters: void
  5140. **
  5141. *******************************************************************************/
  5142. void btm_sec_dev_rec_cback_event (tBTM_SEC_DEV_REC *p_dev_rec, UINT8 res, BOOLEAN is_le_transport)
  5143. {
  5144. tBTM_SEC_CALLBACK *p_callback;
  5145. if (p_dev_rec && p_dev_rec->p_callback) {
  5146. p_callback = p_dev_rec->p_callback;
  5147. p_dev_rec->p_callback = NULL;
  5148. #if BLE_INCLUDED == TRUE
  5149. if (is_le_transport) {
  5150. (*p_callback) (p_dev_rec->ble.pseudo_addr, BT_TRANSPORT_LE, p_dev_rec->p_ref_data, res);
  5151. } else
  5152. #endif
  5153. {
  5154. (*p_callback) (p_dev_rec->bd_addr, BT_TRANSPORT_BR_EDR, p_dev_rec->p_ref_data, res);
  5155. }
  5156. }
  5157. #if (SMP_INCLUDED == TRUE)
  5158. btm_sec_check_pending_reqs();
  5159. #endif ///SMP_INCLUDED == TRUE
  5160. }
  5161. /*******************************************************************************
  5162. **
  5163. ** Function btm_sec_queue_mx_request
  5164. **
  5165. ** Description Return state description for tracing
  5166. **
  5167. *******************************************************************************/
  5168. #if (SMP_INCLUDED == TRUE)
  5169. static BOOLEAN btm_sec_queue_mx_request (BD_ADDR bd_addr, UINT16 psm, BOOLEAN is_orig,
  5170. UINT32 mx_proto_id, UINT32 mx_chan_id,
  5171. tBTM_SEC_CALLBACK *p_callback, void *p_ref_data)
  5172. {
  5173. tBTM_SEC_QUEUE_ENTRY *p_e = (tBTM_SEC_QUEUE_ENTRY *)osi_malloc (sizeof(tBTM_SEC_QUEUE_ENTRY));
  5174. if (p_e) {
  5175. p_e->psm = psm;
  5176. p_e->is_orig = is_orig;
  5177. p_e->p_callback = p_callback;
  5178. p_e->p_ref_data = p_ref_data;
  5179. p_e->mx_proto_id = mx_proto_id;
  5180. p_e->mx_chan_id = mx_chan_id;
  5181. p_e->transport = BT_TRANSPORT_BR_EDR;
  5182. memcpy (p_e->bd_addr, bd_addr, BD_ADDR_LEN);
  5183. BTM_TRACE_EVENT ("%s() PSM: 0x%04x Is_Orig: %u mx_proto_id: %u mx_chan_id: %u\n",
  5184. __func__, psm, is_orig, mx_proto_id, mx_chan_id);
  5185. fixed_queue_enqueue(btm_cb.sec_pending_q, p_e, FIXED_QUEUE_MAX_TIMEOUT);
  5186. return (TRUE);
  5187. }
  5188. return (FALSE);
  5189. }
  5190. static BOOLEAN btm_sec_check_prefetch_pin (tBTM_SEC_DEV_REC *p_dev_rec)
  5191. {
  5192. BOOLEAN rv = FALSE;
  5193. #if (CLASSIC_BT_INCLUDED == TRUE)
  5194. UINT8 major = (UINT8)(p_dev_rec->dev_class[1] & BTM_COD_MAJOR_CLASS_MASK);
  5195. UINT8 minor = (UINT8)(p_dev_rec->dev_class[2] & BTM_COD_MINOR_CLASS_MASK);
  5196. rv = TRUE;
  5197. if ((major == BTM_COD_MAJOR_AUDIO)
  5198. && ((minor == BTM_COD_MINOR_CONFM_HANDSFREE) || (minor == BTM_COD_MINOR_CAR_AUDIO)) ) {
  5199. BTM_TRACE_EVENT ("%s() Skipping pre-fetch PIN for carkit COD Major: 0x%02x Minor: 0x%02x\n",
  5200. __func__, major, minor);
  5201. if (btm_cb.security_mode_changed == FALSE) {
  5202. btm_cb.security_mode_changed = TRUE;
  5203. #ifdef APPL_AUTH_WRITE_EXCEPTION
  5204. if (!(APPL_AUTH_WRITE_EXCEPTION)(p_dev_rec->bd_addr))
  5205. #endif
  5206. {
  5207. btsnd_hcic_write_auth_enable (TRUE);
  5208. }
  5209. }
  5210. } else {
  5211. btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_LOCAL_PIN);
  5212. /* If we got a PIN, use that, else try to get one */
  5213. if (btm_cb.pin_code_len) {
  5214. BTM_PINCodeReply (p_dev_rec->bd_addr, BTM_SUCCESS, btm_cb.pin_code_len, btm_cb.pin_code, p_dev_rec->trusted_mask);
  5215. } else {
  5216. /* pin was not supplied - pre-fetch pin code now */
  5217. if (btm_cb.api.p_pin_callback && ((btm_cb.pairing_flags & BTM_PAIR_FLAGS_PIN_REQD) == 0)) {
  5218. BTM_TRACE_DEBUG("%s() PIN code callback called\n", __func__);
  5219. if (btm_bda_to_acl(p_dev_rec->bd_addr, BT_TRANSPORT_BR_EDR) == NULL) {
  5220. btm_cb.pairing_flags |= BTM_PAIR_FLAGS_PIN_REQD;
  5221. }
  5222. (btm_cb.api.p_pin_callback) (p_dev_rec->bd_addr, p_dev_rec->dev_class,
  5223. p_dev_rec->sec_bd_name, (p_dev_rec->p_cur_service == NULL) ? FALSE
  5224. : (p_dev_rec->p_cur_service->security_flags
  5225. & BTM_SEC_IN_MIN_16_DIGIT_PIN));
  5226. }
  5227. }
  5228. rv = TRUE;
  5229. }
  5230. #endif ///CLASSIC_BT_INCLUDED == TRUE
  5231. #
  5232. return rv;
  5233. }
  5234. /*******************************************************************************
  5235. **
  5236. ** Function btm_sec_auth_payload_tout
  5237. **
  5238. ** Description Processes the HCI Autheniticated Payload Timeout Event
  5239. ** indicating that a packet containing a valid MIC on the
  5240. ** connection handle was not received within the programmed
  5241. ** timeout value. (Spec Default is 30 secs, but can be
  5242. ** changed via the BTM_SecSetAuthPayloadTimeout() function.
  5243. **
  5244. *******************************************************************************/
  5245. void btm_sec_auth_payload_tout (UINT8 *p, UINT16 hci_evt_len)
  5246. {
  5247. UINT16 handle;
  5248. STREAM_TO_UINT16 (handle, p);
  5249. handle = HCID_GET_HANDLE (handle);
  5250. /* Will be exposed to upper layers in the future if/when determined necessary */
  5251. BTM_TRACE_ERROR ("%s on handle 0x%02x\n", __func__, handle);
  5252. }
  5253. /*******************************************************************************
  5254. **
  5255. ** Function btm_sec_queue_encrypt_request
  5256. **
  5257. ** Description encqueue encryption request when device has active security
  5258. ** process pending.
  5259. **
  5260. *******************************************************************************/
  5261. static BOOLEAN btm_sec_queue_encrypt_request (BD_ADDR bd_addr, tBT_TRANSPORT transport,
  5262. tBTM_SEC_CALLBACK *p_callback, void *p_ref_data)
  5263. {
  5264. tBTM_SEC_QUEUE_ENTRY *p_e;
  5265. p_e = (tBTM_SEC_QUEUE_ENTRY *)osi_malloc(sizeof(tBTM_SEC_QUEUE_ENTRY) + 1);
  5266. if (p_e) {
  5267. p_e->psm = 0; /* if PSM 0, encryption request */
  5268. p_e->p_callback = p_callback;
  5269. p_e->p_ref_data = (void *)(p_e + 1);
  5270. *(UINT8 *)p_e->p_ref_data = *(UINT8 *)(p_ref_data);
  5271. p_e->transport = transport;
  5272. memcpy(p_e->bd_addr, bd_addr, BD_ADDR_LEN);
  5273. fixed_queue_enqueue(btm_cb.sec_pending_q, p_e, FIXED_QUEUE_MAX_TIMEOUT);
  5274. return TRUE;
  5275. }
  5276. return FALSE;
  5277. }
  5278. /*******************************************************************************
  5279. **
  5280. ** Function btm_sec_set_peer_sec_caps
  5281. **
  5282. ** Description This function is called to set sm4 and rmt_sec_caps fields
  5283. ** based on the available peer device features.
  5284. **
  5285. ** Returns void
  5286. **
  5287. *******************************************************************************/
  5288. void btm_sec_set_peer_sec_caps(tACL_CONN *p_acl_cb, tBTM_SEC_DEV_REC *p_dev_rec)
  5289. {
  5290. BD_ADDR rem_bd_addr;
  5291. UINT8 *p_rem_bd_addr;
  5292. if ((btm_cb.security_mode == BTM_SEC_MODE_SP ||
  5293. btm_cb.security_mode == BTM_SEC_MODE_SP_DEBUG ||
  5294. btm_cb.security_mode == BTM_SEC_MODE_SC) &&
  5295. HCI_SSP_HOST_SUPPORTED(p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_1])) {
  5296. p_dev_rec->sm4 = BTM_SM4_TRUE;
  5297. p_dev_rec->remote_supports_secure_connections =
  5298. (HCI_SC_HOST_SUPPORTED(p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_1]));
  5299. } else {
  5300. p_dev_rec->sm4 = BTM_SM4_KNOWN;
  5301. p_dev_rec->remote_supports_secure_connections = FALSE;
  5302. }
  5303. BTM_TRACE_API("%s: sm4: 0x%02x, rmt_support_for_secure_connections %d\n", __FUNCTION__,
  5304. p_dev_rec->sm4, p_dev_rec->remote_supports_secure_connections);
  5305. /* Store previous state of remote device to check if peer device downgraded
  5306. * it's secure connection state. */
  5307. #if (CLASSIC_BT_INCLUDED == TRUE)
  5308. if (p_dev_rec->remote_supports_secure_connections >= p_dev_rec->remote_secure_connection_previous_state) {
  5309. p_dev_rec->remote_secure_connection_previous_state = p_dev_rec->remote_supports_secure_connections;
  5310. } else {
  5311. BTM_TRACE_ERROR("Remote Device downgraded security from SC, deleting Link Key");
  5312. /* Mark in ACL packet that secure connection is downgraded. */
  5313. p_acl_cb->sc_downgrade = 1;
  5314. p_dev_rec->remote_secure_connection_previous_state = 0;
  5315. /* As peer device downgraded it's security, peer device is a suspicious
  5316. * device. Hence remove pairing information by removing link key
  5317. * information. */
  5318. memset(p_dev_rec->link_key, 0, LINK_KEY_LEN);
  5319. p_dev_rec->sec_flags &= ~(BTM_SEC_AUTHORIZED | BTM_SEC_AUTHENTICATED
  5320. | BTM_SEC_ENCRYPTED | BTM_SEC_NAME_KNOWN
  5321. | BTM_SEC_LINK_KEY_KNOWN | BTM_SEC_LINK_KEY_AUTHED
  5322. | BTM_SEC_ROLE_SWITCHED | BTM_SEC_16_DIGIT_PIN_AUTHED);
  5323. return;
  5324. }
  5325. #endif
  5326. if (p_dev_rec->remote_features_needed) {
  5327. BTM_TRACE_EVENT("%s: Now device in SC Only mode, waiting for peer remote features!\n",
  5328. __FUNCTION__);
  5329. p_rem_bd_addr = (UINT8 *) rem_bd_addr;
  5330. BDADDR_TO_STREAM(p_rem_bd_addr, p_dev_rec->bd_addr);
  5331. p_rem_bd_addr = (UINT8 *) rem_bd_addr;
  5332. btm_io_capabilities_req(p_rem_bd_addr);
  5333. p_dev_rec->remote_features_needed = FALSE;
  5334. }
  5335. }
  5336. /*******************************************************************************
  5337. **
  5338. ** Function btm_sec_is_serv_level0
  5339. **
  5340. ** Description This function is called to check if the service corresponding
  5341. ** to PSM is security mode 4 level 0 service.
  5342. **
  5343. ** Returns TRUE if the service is security mode 4 level 0 service
  5344. **
  5345. *******************************************************************************/
  5346. static BOOLEAN btm_sec_is_serv_level0(UINT16 psm)
  5347. {
  5348. if (psm == BT_PSM_SDP) {
  5349. BTM_TRACE_DEBUG("%s: PSM: 0x%04x -> mode 4 level 0 service\n", __FUNCTION__, psm);
  5350. return TRUE;
  5351. }
  5352. return FALSE;
  5353. }
  5354. /*******************************************************************************
  5355. **
  5356. ** Function btm_sec_check_pending_enc_req
  5357. **
  5358. ** Description This function is called to send pending encryption callback if
  5359. ** waiting
  5360. **
  5361. ** Returns void
  5362. **
  5363. *******************************************************************************/
  5364. static void btm_sec_check_pending_enc_req (tBTM_SEC_DEV_REC *p_dev_rec, tBT_TRANSPORT transport,
  5365. UINT8 encr_enable)
  5366. {
  5367. if (fixed_queue_is_empty(btm_cb.sec_pending_q)) {
  5368. return;
  5369. }
  5370. UINT8 res = encr_enable ? BTM_SUCCESS : BTM_ERR_PROCESSING;
  5371. list_t *list = fixed_queue_get_list(btm_cb.sec_pending_q);
  5372. for (const list_node_t *node = list_begin(list); node != list_end(list); ) {
  5373. tBTM_SEC_QUEUE_ENTRY *p_e = (tBTM_SEC_QUEUE_ENTRY *)list_node(node);
  5374. node = list_next(node);
  5375. if (memcmp(p_e->bd_addr, p_dev_rec->bd_addr, BD_ADDR_LEN) == 0 && p_e->psm == 0
  5376. #if BLE_INCLUDED == TRUE
  5377. && p_e->transport == transport
  5378. #endif
  5379. ) {
  5380. #if BLE_INCLUDED == TRUE
  5381. UINT8 sec_act = *(UINT8 *)(p_e->p_ref_data);
  5382. #endif
  5383. if (encr_enable == 0 || transport == BT_TRANSPORT_BR_EDR
  5384. #if BLE_INCLUDED == TRUE
  5385. || (sec_act == BTM_BLE_SEC_ENCRYPT || sec_act == BTM_BLE_SEC_ENCRYPT_NO_MITM)
  5386. || (sec_act == BTM_BLE_SEC_ENCRYPT_MITM && p_dev_rec->sec_flags
  5387. & BTM_SEC_LE_AUTHENTICATED)
  5388. #endif
  5389. ) {
  5390. if (p_e->p_callback) {
  5391. (*p_e->p_callback) (p_dev_rec->bd_addr, transport, p_e->p_ref_data, res);
  5392. }
  5393. fixed_queue_try_remove_from_queue(btm_cb.sec_pending_q, (void *)p_e);
  5394. }
  5395. }
  5396. }
  5397. }
  5398. #endif ///SMP_INCLUDED == TRUE
  5399. /*******************************************************************************
  5400. **
  5401. ** Function btm_sec_set_serv_level4_flags
  5402. **
  5403. ** Description This function is called to set security mode 4 level 4 flags.
  5404. **
  5405. ** Returns service security requirements updated to include secure
  5406. ** connections only mode.
  5407. **
  5408. *******************************************************************************/
  5409. #if (SMP_INCLUDED == TRUE)
  5410. static UINT16 btm_sec_set_serv_level4_flags(UINT16 cur_security, BOOLEAN is_originator)
  5411. {
  5412. UINT16 sec_level4_flags = is_originator ? BTM_SEC_OUT_LEVEL4_FLAGS : BTM_SEC_IN_LEVEL4_FLAGS;
  5413. return cur_security | sec_level4_flags;
  5414. }
  5415. #endif ///SMP_INCLUDED == TRUE
  5416. /*******************************************************************************
  5417. **
  5418. ** Function btm_sec_clear_ble_keys
  5419. **
  5420. ** Description This function is called to clear out the BLE keys.
  5421. ** Typically when devices are removed in BTM_SecDeleteDevice,
  5422. ** or when a new BT Link key is generated.
  5423. **
  5424. ** Returns void
  5425. **
  5426. *******************************************************************************/
  5427. #if (BLE_INCLUDED == TRUE)
  5428. void btm_sec_clear_ble_keys (tBTM_SEC_DEV_REC *p_dev_rec)
  5429. {
  5430. BTM_TRACE_DEBUG ("%s() Clearing BLE Keys\n", __func__);
  5431. #if (SMP_INCLUDED== TRUE)
  5432. p_dev_rec->ble.key_type = BTM_LE_KEY_NONE;
  5433. memset (&p_dev_rec->ble.keys, 0, sizeof(tBTM_SEC_BLE_KEYS));
  5434. #if (BLE_PRIVACY_SPT == TRUE)
  5435. btm_ble_resolving_list_remove_dev(p_dev_rec);
  5436. #endif
  5437. #endif
  5438. }
  5439. #endif ///BLE_INCLUDED == TRUE
  5440. /*******************************************************************************
  5441. **
  5442. ** Function btm_sec_is_a_bonded_dev
  5443. **
  5444. ** Description Is the specified device is a bonded device
  5445. **
  5446. ** Returns TRUE - dev is bonded
  5447. **
  5448. *******************************************************************************/
  5449. BOOLEAN btm_sec_is_a_bonded_dev (BD_ADDR bda)
  5450. {
  5451. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bda);
  5452. BOOLEAN is_bonded = FALSE;
  5453. if (p_dev_rec &&
  5454. #if (SMP_INCLUDED == TRUE && BLE_INCLUDED == TRUE)
  5455. ((p_dev_rec->ble.key_type && (p_dev_rec->sec_flags & BTM_SEC_LE_LINK_KEY_KNOWN)) ||
  5456. #else
  5457. (
  5458. #endif
  5459. (p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_KNOWN))) {
  5460. is_bonded = TRUE;
  5461. }
  5462. BTM_TRACE_DEBUG ("%s() is_bonded=%d\n", __func__, is_bonded);
  5463. return (is_bonded);
  5464. }
  5465. /*******************************************************************************
  5466. **
  5467. ** Function btm_sec_is_le_capable_dev
  5468. **
  5469. ** Description Is the specified device is dual mode or LE only device
  5470. **
  5471. ** Returns TRUE - dev is a dual mode
  5472. **
  5473. *******************************************************************************/
  5474. BOOLEAN btm_sec_is_le_capable_dev (BD_ADDR bda)
  5475. {
  5476. BOOLEAN le_capable = FALSE;
  5477. #if (BLE_INCLUDED== TRUE)
  5478. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bda);
  5479. if (p_dev_rec && (p_dev_rec->device_type & BT_DEVICE_TYPE_BLE) == BT_DEVICE_TYPE_BLE) {
  5480. le_capable = TRUE;
  5481. }
  5482. #endif
  5483. return le_capable;
  5484. }
  5485. /*******************************************************************************
  5486. **
  5487. ** Function btm_sec_find_bonded_dev
  5488. **
  5489. ** Description Find a bonded device starting from the specified index
  5490. **
  5491. ** Returns TRUE - found a bonded device
  5492. **
  5493. *******************************************************************************/
  5494. #if (BLE_INCLUDED == TRUE)
  5495. BOOLEAN btm_sec_find_bonded_dev (UINT8 start_idx, UINT16 *p_found_handle, tBTM_SEC_DEV_REC **p_rec)
  5496. {
  5497. BOOLEAN found = FALSE;
  5498. #if (SMP_INCLUDED== TRUE)
  5499. tBTM_SEC_DEV_REC *p_dev_rec;
  5500. list_node_t *p_node = NULL;
  5501. for (p_node = list_begin(btm_cb.p_sec_dev_rec_list); p_node; p_node = list_next(p_node)) {
  5502. p_dev_rec = list_node(p_node);
  5503. if (p_dev_rec->ble.key_type || (p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_KNOWN)) {
  5504. *p_found_handle = p_dev_rec->hci_handle;
  5505. *p_rec = p_dev_rec;
  5506. break;
  5507. }
  5508. }
  5509. BTM_TRACE_DEBUG ("%s() found=%d\n", __func__, found);
  5510. #endif
  5511. return (found);
  5512. }
  5513. #endif ///BLE_INCLUDED == TRUE
  5514. /*******************************************************************************
  5515. **
  5516. ** Function btm_sec_use_smp_br_chnl
  5517. **
  5518. ** Description The function checks if SMP BR connection can be used with
  5519. ** the peer.
  5520. ** Is called when authentication for dedicated bonding is
  5521. ** successfully completed.
  5522. **
  5523. ** Returns TRUE - if SMP BR connection can be used (the link key is
  5524. ** generated from P-256 and the peer supports Security
  5525. ** Manager over BR).
  5526. **
  5527. *******************************************************************************/
  5528. #if (SMP_INCLUDED == TRUE)
  5529. static BOOLEAN btm_sec_use_smp_br_chnl(tBTM_SEC_DEV_REC *p_dev_rec)
  5530. {
  5531. UINT32 ext_feat;
  5532. UINT8 chnl_mask[L2CAP_FIXED_CHNL_ARRAY_SIZE];
  5533. BTM_TRACE_DEBUG ("%s() link_key_type = 0x%x\n", __func__,
  5534. p_dev_rec->link_key_type);
  5535. if ((p_dev_rec->link_key_type != BTM_LKEY_TYPE_UNAUTH_COMB_P_256) &&
  5536. (p_dev_rec->link_key_type != BTM_LKEY_TYPE_AUTH_COMB_P_256)) {
  5537. return FALSE;
  5538. }
  5539. if (!L2CA_GetPeerFeatures (p_dev_rec->bd_addr, &ext_feat, chnl_mask)) {
  5540. return FALSE;
  5541. }
  5542. if (!(chnl_mask[0] & L2CAP_FIXED_CHNL_SMP_BR_BIT)) {
  5543. return FALSE;
  5544. }
  5545. return TRUE;
  5546. }
  5547. /*******************************************************************************
  5548. **
  5549. ** Function btm_sec_is_master
  5550. **
  5551. ** Description The function checks if the device is BR/EDR master after
  5552. ** pairing is completed.
  5553. **
  5554. ** Returns TRUE - if the device is master.
  5555. **
  5556. *******************************************************************************/
  5557. static BOOLEAN btm_sec_is_master(tBTM_SEC_DEV_REC *p_dev_rec)
  5558. {
  5559. tACL_CONN *p = btm_bda_to_acl(p_dev_rec->bd_addr, BT_TRANSPORT_BR_EDR);
  5560. return (p && (p->link_role == BTM_ROLE_MASTER));
  5561. }
  5562. #if (CLASSIC_BT_INCLUDED == TRUE)
  5563. /*******************************************************************************
  5564. **
  5565. ** Function btm_sec_legacy_authentication_mutual
  5566. **
  5567. ** Description This function is called when legacy authentication is used
  5568. ** and only remote device has completed the authentication
  5569. **
  5570. ** Returns TRUE if aunthentication command sent successfully
  5571. **
  5572. *******************************************************************************/
  5573. BOOLEAN btm_sec_legacy_authentication_mutual (tBTM_SEC_DEV_REC *p_dev_rec)
  5574. {
  5575. return (btm_sec_start_authentication (p_dev_rec));
  5576. }
  5577. /*******************************************************************************
  5578. **
  5579. ** Function btm_sec_update_legacy_auth_state
  5580. **
  5581. ** Description This function updates the legacy authentication state
  5582. **
  5583. ** Returns void
  5584. **
  5585. *******************************************************************************/
  5586. void btm_sec_update_legacy_auth_state(tACL_CONN *p_acl_cb, UINT8 legacy_auth_state)
  5587. {
  5588. if (p_acl_cb) {
  5589. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev_by_handle (p_acl_cb->hci_handle);
  5590. if (p_dev_rec) {
  5591. if ((BTM_BothEndsSupportSecureConnections(p_dev_rec->bd_addr) == 0) &&
  5592. (legacy_auth_state != BTM_ACL_LEGACY_AUTH_NONE)) {
  5593. p_acl_cb->legacy_auth_state |= legacy_auth_state;
  5594. } else {
  5595. p_acl_cb->legacy_auth_state = BTM_ACL_LEGACY_AUTH_NONE;
  5596. }
  5597. }
  5598. }
  5599. }
  5600. /*******************************************************************************
  5601. **
  5602. ** Function btm_sec_handle_remote_legacy_auth_cmp
  5603. **
  5604. ** Description This function updates the legacy authneticaiton state
  5605. ** to indicate that remote device has completed the authentication
  5606. **
  5607. ** Returns void
  5608. **
  5609. *******************************************************************************/
  5610. void btm_sec_handle_remote_legacy_auth_cmp(UINT16 handle)
  5611. {
  5612. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev_by_handle (handle);
  5613. tACL_CONN *p_acl_cb = btm_bda_to_acl(p_dev_rec->bd_addr, BT_TRANSPORT_BR_EDR);
  5614. btm_sec_update_legacy_auth_state(p_acl_cb, BTM_ACL_LEGACY_AUTH_REMOTE);
  5615. }
  5616. #endif /// (CLASSIC_BT_INCLUDED == TRUE)
  5617. #endif ///SMP_INCLUDED == TRUE
  5618. /******************************************************************************
  5619. **
  5620. ** Function btm_sec_dev_authorization
  5621. **
  5622. ** Description This function is used to authorize a specified device(BLE)
  5623. **
  5624. ******************************************************************************
  5625. */
  5626. #if (BLE_INCLUDED == TRUE)
  5627. BOOLEAN btm_sec_dev_authorization(BD_ADDR bd_addr, BOOLEAN authorized)
  5628. {
  5629. #if (SMP_INCLUDED == TRUE)
  5630. UINT8 sec_flag = 0;
  5631. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev(bd_addr);
  5632. if (p_dev_rec) {
  5633. sec_flag = (UINT8)(p_dev_rec->sec_flags >> 8);
  5634. if (!(sec_flag & BTM_SEC_LINK_KEY_AUTHED)) {
  5635. BTM_TRACE_ERROR("Authorized should after successful Authentication(MITM protection)\n");
  5636. return FALSE;
  5637. }
  5638. if (authorized) {
  5639. p_dev_rec->sec_flags |= BTM_SEC_LE_AUTHORIZATION;
  5640. } else {
  5641. p_dev_rec->sec_flags &= ~(BTM_SEC_LE_AUTHORIZATION);
  5642. }
  5643. } else {
  5644. BTM_TRACE_ERROR("%s, can't find device\n", __func__);
  5645. return FALSE;
  5646. }
  5647. return TRUE;
  5648. #endif ///SMP_INCLUDED == TRUE
  5649. return FALSE;
  5650. }
  5651. #endif /// BLE_INCLUDE == TRUE