btm_sec.c 255 KB

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