btm_sec.c 245 KB

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