btm_sec.c 248 KB

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