ZDSecMgr.c 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602
  1. /******************************************************************************
  2. Filename: ZDSecMgr.c
  3. Revised: $Date: 2012-02-16 13:22:48 -0800 (Thu, 16 Feb 2012) $
  4. Revision: $Revision: 29339 $
  5. Description: The ZigBee Device Security Manager.
  6. Copyright 2005-2012 Texas Instruments Incorporated. All rights reserved.
  7. IMPORTANT: Your use of this Software is limited to those specific rights
  8. granted under the terms of a software license agreement between the user
  9. who downloaded the software, his/her employer (which must be your employer)
  10. and Texas Instruments Incorporated (the "License"). You may not use this
  11. Software unless you agree to abide by the terms of the License. The License
  12. limits your use, and you acknowledge, that the Software may not be modified,
  13. copied or distributed unless embedded on a Texas Instruments microcontroller
  14. or used solely and exclusively in conjunction with a Texas Instruments radio
  15. frequency transceiver, which is integrated into your product. Other than for
  16. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  17. works of, modify, distribute, perform, display or sell this Software and/or
  18. its documentation for any purpose.
  19. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  20. PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  21. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  22. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  23. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  24. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  25. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  26. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  27. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  28. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  29. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  30. Should you have any questions regarding your right to use this Software,
  31. contact Texas Instruments Incorporated at www.TI.com.
  32. ******************************************************************************/
  33. #ifdef __cplusplus
  34. extern "C"
  35. {
  36. #endif
  37. /******************************************************************************
  38. * INCLUDES
  39. */
  40. #include "ZComdef.h"
  41. #include "OSAL.h"
  42. #include "OSAL_NV.h"
  43. #include "ZGlobals.h"
  44. #include "ssp.h"
  45. #include "nwk_globals.h"
  46. #include "nwk.h"
  47. #include "NLMEDE.h"
  48. #include "AddrMgr.h"
  49. #include "AssocList.h"
  50. #include "APSMEDE.h"
  51. #include "ZDConfig.h"
  52. #include "ZDSecMgr.h"
  53. /******************************************************************************
  54. * CONSTANTS
  55. */
  56. // maximum number of devices managed by this Security Manager
  57. #if !defined ( ZDSECMGR_DEVICE_MAX )
  58. #define ZDSECMGR_DEVICE_MAX 3
  59. #endif
  60. // total number of preconfigured devices (EXT address, MASTER key)
  61. //devtag.pro.security
  62. //#define ZDSECMGR_PRECONFIG_MAX ZDSECMGR_DEVICE_MAX
  63. #define ZDSECMGR_PRECONFIG_MAX 0
  64. // maximum number of MASTER keys this device may hold
  65. #define ZDSECMGR_MASTERKEY_MAX ZDSECMGR_DEVICE_MAX
  66. // maximum number of LINK keys this device may store
  67. #define ZDSECMGR_ENTRY_MAX ZDSECMGR_DEVICE_MAX
  68. // total number of devices under control - authentication, SKKE, etc.
  69. #define ZDSECMGR_CTRL_MAX ZDSECMGR_DEVICE_MAX
  70. // total number of stored devices
  71. #if !defined ( ZDSECMGR_STORED_DEVICES )
  72. #define ZDSECMGR_STORED_DEVICES 3
  73. #endif
  74. // Total number of preconfigured trust center link key
  75. #if !defined ( ZDSECMGR_TC_DEVICE_MAX )
  76. #define ZDSECMGR_TC_DEVICE_MAX 1
  77. #endif
  78. #if ( ZDSECMGR_TC_DEVICE_MAX < 1 ) || ( ZDSECMGR_TC_DEVICE_MAX > 255 )
  79. #error "ZDSECMGR_TC_DEVICE_MAX shall be between 1 and 255 !"
  80. #endif
  81. #define ZDSECMGR_CTRL_NONE 0
  82. #define ZDSECMGR_CTRL_INIT 1
  83. #define ZDSECMGR_CTRL_TK_MASTER 2
  84. #define ZDSECMGR_CTRL_SKKE_INIT 3
  85. #define ZDSECMGR_CTRL_SKKE_WAIT 4
  86. #define ZDSECMGR_CTRL_SKKE_DONE 5
  87. #define ZDSECMGR_CTRL_SKKE_FAIL 6
  88. #define ZDSECMGR_CTRL_TK_NWK 7
  89. #define ZDSECMGR_CTRL_BASE_CNTR 1
  90. #define ZDSECMGR_CTRL_SKKE_INIT_CNTR 1
  91. #define ZDSECMGR_CTRL_TK_NWK_CNTR 1
  92. // set SKA slot maximum
  93. #define ZDSECMGR_SKA_SLOT_MAX 1
  94. // APSME Stub Implementations
  95. #define ZDSecMgrMasterKeyGet APSME_MasterKeyGet
  96. #define ZDSecMgrLinkKeySet APSME_LinkKeySet
  97. #define ZDSecMgrLinkKeyNVIdGet APSME_LinkKeyNVIdGet
  98. #define ZDSecMgrKeyFwdToChild APSME_KeyFwdToChild
  99. #define ZDSecMgrIsLinkKeyValid APSME_IsLinkKeyValid
  100. /******************************************************************************
  101. * TYPEDEFS
  102. */
  103. typedef struct
  104. {
  105. uint8 extAddr[Z_EXTADDR_LEN];
  106. uint8 key[SEC_KEY_LEN];
  107. } ZDSecMgrPreConfigData_t;
  108. typedef struct
  109. {
  110. uint16 ami;
  111. uint8 key[SEC_KEY_LEN];
  112. } ZDSecMgrMasterKeyData_t;
  113. typedef struct
  114. {
  115. uint16 ami;
  116. uint16 keyNvId; // index to the Link Key table in NV
  117. ZDSecMgr_Authentication_Option authenticateOption;
  118. } ZDSecMgrEntry_t;
  119. typedef struct
  120. {
  121. ZDSecMgrEntry_t* entry;
  122. uint16 parentAddr;
  123. uint8 secure;
  124. uint8 state;
  125. uint8 cntr;
  126. } ZDSecMgrCtrl_t;
  127. typedef struct
  128. {
  129. uint16 nwkAddr;
  130. uint8* extAddr;
  131. uint16 parentAddr;
  132. uint8 secure;
  133. uint8 devStatus;
  134. ZDSecMgrCtrl_t* ctrl;
  135. } ZDSecMgrDevice_t;
  136. /******************************************************************************
  137. * LOCAL VARIABLES
  138. */
  139. #if 0 // Taken out because the following functionality is only used for test
  140. // purpose. A more efficient (above) way is used. It can be put
  141. // back in if customers request for a white/black list feature.
  142. uint8 ZDSecMgrStoredDeviceList[ZDSECMGR_STORED_DEVICES][Z_EXTADDR_LEN] =
  143. {
  144. { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  145. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
  146. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  147. };
  148. #endif
  149. uint8 ZDSecMgrTCExtAddr[Z_EXTADDR_LEN]=
  150. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  151. // Key data is put in CONST area for security reasons
  152. CONST uint8 ZDSecMgrTCMasterKey[SEC_KEY_LEN] =
  153. {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  154. 0x89,0x67,0x45,0x23,0x01,0xEF,0xCD,0xAB};
  155. uint8 ZDSecMgrTCAuthenticated = FALSE;
  156. //devtag.pro.security - remove this
  157. #if ( ZDSECMGR_PRECONFIG_MAX != 0 )
  158. const ZDSecMgrPreConfigData_t ZDSecMgrPreConfigData[ZDSECMGR_PRECONFIG_MAX] =
  159. {
  160. //---------------------------------------------------------------------------
  161. // DEVICE A
  162. //---------------------------------------------------------------------------
  163. {
  164. // extAddr
  165. {0x7C,0x01,0x12,0x13,0x14,0x15,0x16,0x17},
  166. // key
  167. {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  168. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
  169. },
  170. //---------------------------------------------------------------------------
  171. // DEVICE B
  172. //---------------------------------------------------------------------------
  173. {
  174. // extAddr
  175. {0x84,0x03,0x00,0x00,0x00,0x4B,0x12,0x00},
  176. // key
  177. {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  178. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
  179. },
  180. //---------------------------------------------------------------------------
  181. // DEVICE C
  182. //---------------------------------------------------------------------------
  183. {
  184. // extAddr
  185. {0x3E,0x01,0x12,0x13,0x14,0x15,0x16,0x17},
  186. // key
  187. {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  188. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
  189. },
  190. };
  191. #endif // ( ZDSECMGR_PRECONFIG_MAX != 0 )
  192. ZDSecMgrEntry_t* ZDSecMgrEntries = NULL;
  193. ZDSecMgrCtrl_t* ZDSecMgrCtrlData = NULL;
  194. void ZDSecMgrAddrMgrUpdate( uint16 ami, uint16 nwkAddr );
  195. void ZDSecMgrAddrMgrCB( uint8 update, AddrMgrEntry_t* newEntry, AddrMgrEntry_t* oldEntry );
  196. uint8 ZDSecMgrPermitJoiningEnabled;
  197. uint8 ZDSecMgrPermitJoiningTimed;
  198. APSME_TCLinkKey_t TrustCenterLinkKey;
  199. APSME_ApsLinkKeyFrmCntr_t ApsLinkKeyFrmCntr[ZDSECMGR_ENTRY_MAX];
  200. APSME_TCLinkKeyFrmCntr_t TCLinkKeyFrmCntr[ZDSECMGR_TC_DEVICE_MAX];
  201. /******************************************************************************
  202. * PRIVATE FUNCTIONS
  203. *
  204. * ZDSecMgrMasterKeyInit
  205. * ZDSecMgrAddrStore
  206. * ZDSecMgrExtAddrStore
  207. * ZDSecMgrExtAddrLookup
  208. * ZDSecMgrMasterKeyLookup
  209. * ZDSecMgrMasterKeyStore
  210. * ZDSecMgrEntryInit
  211. * ZDSecMgrEntryLookup
  212. * ZDSecMgrEntryLookupAMI
  213. * ZDSecMgrEntryLookupExt
  214. * ZDSecMgrEntryLookupExtGetIndex
  215. * ZDSecMgrEntryFree
  216. * ZDSecMgrEntryNew
  217. * ZDSecMgrCtrlInit
  218. * ZDSecMgrCtrlRelease
  219. * ZDSecMgrCtrlLookup
  220. * ZDSecMgrCtrlSet
  221. * ZDSecMgrCtrlAdd
  222. * ZDSecMgrCtrlTerm
  223. * ZDSecMgrCtrlReset
  224. * ZDSecMgrMasterKeyLoad
  225. * ZDSecMgrAppKeyGet
  226. * ZDSecMgrAppKeyReq
  227. * ZDSecMgrEstablishKey
  228. * ZDSecMgrSendMasterKey
  229. * ZDSecMgrSendNwkKey
  230. * ZDSecMgrDeviceEntryRemove
  231. * ZDSecMgrDeviceEntryAdd
  232. * ZDSecMgrDeviceCtrlHandler
  233. * ZDSecMgrDeviceCtrlSetup
  234. * ZDSecMgrDeviceCtrlUpdate
  235. * ZDSecMgrDeviceRemove
  236. * ZDSecMgrDeviceValidateSKKE
  237. * ZDSecMgrDeviceValidateRM
  238. * ZDSecMgrDeviceValidateCM
  239. * ZDSecMgrDeviceValidate
  240. * ZDSecMgrDeviceJoin
  241. * ZDSecMgrDeviceJoinDirect
  242. * ZDSecMgrDeviceJoinFwd
  243. * ZDSecMgrDeviceNew
  244. * ZDSecMgrAssocDeviceAuth
  245. * ZDSecMgrAuthInitiate
  246. * ZDSecMgrAuthNwkKey
  247. * APSME_TCLinkKeyInit
  248. * APSME_IsDefaultTCLK
  249. */
  250. //-----------------------------------------------------------------------------
  251. // master key data
  252. //-----------------------------------------------------------------------------
  253. void ZDSecMgrMasterKeyInit( void );
  254. //-----------------------------------------------------------------------------
  255. // address management
  256. //-----------------------------------------------------------------------------
  257. ZStatus_t ZDSecMgrAddrStore( uint16 nwkAddr, uint8* extAddr, uint16* ami );
  258. ZStatus_t ZDSecMgrExtAddrStore( uint16 nwkAddr, uint8* extAddr, uint16* ami );
  259. ZStatus_t ZDSecMgrExtAddrLookup( uint8* extAddr, uint16* ami );
  260. //-----------------------------------------------------------------------------
  261. // Trust Center management
  262. //-----------------------------------------------------------------------------
  263. uint8 ZDSecMgrTCExtAddrCheck( uint8* extAddr );
  264. void ZDSecMgrTCDataLoad( uint8* extAddr );
  265. //-----------------------------------------------------------------------------
  266. // MASTER key data
  267. //-----------------------------------------------------------------------------
  268. ZStatus_t ZDSecMgrMasterKeyLookup( uint16 ami, uint16* pKeyNvId );
  269. ZStatus_t ZDSecMgrMasterKeyStore( uint16 ami, uint8* key );
  270. //-----------------------------------------------------------------------------
  271. // entry data
  272. //-----------------------------------------------------------------------------
  273. void ZDSecMgrEntryInit(uint8 state);
  274. ZStatus_t ZDSecMgrEntryLookup( uint16 nwkAddr, ZDSecMgrEntry_t** entry );
  275. ZStatus_t ZDSecMgrEntryLookupAMI( uint16 ami, ZDSecMgrEntry_t** entry );
  276. ZStatus_t ZDSecMgrEntryLookupExt( uint8* extAddr, ZDSecMgrEntry_t** entry );
  277. ZStatus_t ZDSecMgrEntryLookupExtGetIndex( uint8* extAddr, ZDSecMgrEntry_t** entry, uint16* entryIndex );
  278. ZStatus_t ZDSecMgrEntryLookupAMIGetIndex( uint16 ami, uint16* entryIndex );
  279. void ZDSecMgrEntryFree( ZDSecMgrEntry_t* entry );
  280. ZStatus_t ZDSecMgrEntryNew( ZDSecMgrEntry_t** entry );
  281. ZStatus_t ZDSecMgrAuthenticationSet( uint8* extAddr, ZDSecMgr_Authentication_Option option );
  282. void ZDSecMgrApsLinkKeyInit(void);
  283. #if defined ( NV_RESTORE )
  284. static void ZDSecMgrWriteNV(void);
  285. static void ZDSecMgrRestoreFromNV(void);
  286. static void ZDSecMgrUpdateNV( uint16 index );
  287. #endif
  288. //-----------------------------------------------------------------------------
  289. // control data
  290. //-----------------------------------------------------------------------------
  291. void ZDSecMgrCtrlInit( void );
  292. void ZDSecMgrCtrlRelease( ZDSecMgrCtrl_t* ctrl );
  293. void ZDSecMgrCtrlLookup( ZDSecMgrEntry_t* entry, ZDSecMgrCtrl_t** ctrl );
  294. void ZDSecMgrCtrlSet( ZDSecMgrDevice_t* device,
  295. ZDSecMgrEntry_t* entry,
  296. ZDSecMgrCtrl_t* ctrl );
  297. ZStatus_t ZDSecMgrCtrlAdd( ZDSecMgrDevice_t* device, ZDSecMgrEntry_t* entry );
  298. void ZDSecMgrCtrlTerm( ZDSecMgrEntry_t* entry );
  299. ZStatus_t ZDSecMgrCtrlReset( ZDSecMgrDevice_t* device,
  300. ZDSecMgrEntry_t* entry );
  301. //-----------------------------------------------------------------------------
  302. // key support
  303. //-----------------------------------------------------------------------------
  304. ZStatus_t ZDSecMgrMasterKeyLoad( uint8* extAddr, uint8* key );
  305. ZStatus_t ZDSecMgrAppKeyGet( uint16 initNwkAddr,
  306. uint8* initExtAddr,
  307. uint16 partNwkAddr,
  308. uint8* partExtAddr,
  309. uint8** key,
  310. uint8* keyType );
  311. void ZDSecMgrAppKeyReq( ZDO_RequestKeyInd_t* ind );
  312. ZStatus_t ZDSecMgrEstablishKey( ZDSecMgrDevice_t* device );
  313. ZStatus_t ZDSecMgrSendMasterKey( ZDSecMgrDevice_t* device );
  314. ZStatus_t ZDSecMgrSendNwkKey( ZDSecMgrDevice_t* device );
  315. void ZDSecMgrNwkKeyInit(uint8 setDefault);
  316. //-----------------------------------------------------------------------------
  317. // device entry
  318. //-----------------------------------------------------------------------------
  319. void ZDSecMgrDeviceEntryRemove( ZDSecMgrEntry_t* entry );
  320. ZStatus_t ZDSecMgrDeviceEntryAdd( ZDSecMgrDevice_t* device, uint16 ami );
  321. //-----------------------------------------------------------------------------
  322. // device control
  323. //-----------------------------------------------------------------------------
  324. void ZDSecMgrDeviceCtrlHandler( ZDSecMgrDevice_t* device );
  325. void ZDSecMgrDeviceCtrlSetup( ZDSecMgrDevice_t* device );
  326. void ZDSecMgrDeviceCtrlUpdate( uint8* extAddr, uint8 state );
  327. //-----------------------------------------------------------------------------
  328. // device management
  329. //-----------------------------------------------------------------------------
  330. void ZDSecMgrDeviceRemove( ZDSecMgrDevice_t* device );
  331. ZStatus_t ZDSecMgrDeviceValidateSKKE( ZDSecMgrDevice_t* device );
  332. ZStatus_t ZDSecMgrDeviceValidateRM( ZDSecMgrDevice_t* device );
  333. ZStatus_t ZDSecMgrDeviceValidateCM( ZDSecMgrDevice_t* device );
  334. ZStatus_t ZDSecMgrDeviceValidate( ZDSecMgrDevice_t* device );
  335. ZStatus_t ZDSecMgrDeviceJoin( ZDSecMgrDevice_t* device );
  336. ZStatus_t ZDSecMgrDeviceJoinDirect( ZDSecMgrDevice_t* device );
  337. ZStatus_t ZDSecMgrDeviceJoinFwd( ZDSecMgrDevice_t* device );
  338. ZStatus_t ZDSecMgrDeviceNew( ZDSecMgrDevice_t* device );
  339. //-----------------------------------------------------------------------------
  340. // association management
  341. //-----------------------------------------------------------------------------
  342. void ZDSecMgrAssocDeviceAuth( associated_devices_t* assoc );
  343. //-----------------------------------------------------------------------------
  344. // authentication management
  345. //-----------------------------------------------------------------------------
  346. void ZDSecMgrAuthInitiate( uint8* responder );
  347. void ZDSecMgrAuthNwkKey( void );
  348. //-----------------------------------------------------------------------------
  349. // APSME function
  350. //-----------------------------------------------------------------------------
  351. void APSME_TCLinkKeyInit( uint8 setDefault );
  352. uint8 APSME_IsDefaultTCLK( uint8 *extAddr );
  353. /******************************************************************************
  354. * @fn ZDSecMgrMasterKeyInit ]
  355. *
  356. * @brief Initialize master key data in NV
  357. *
  358. * @param none
  359. *
  360. * @return none
  361. */
  362. void ZDSecMgrMasterKeyInit( void )
  363. {
  364. uint16 index;
  365. ZDSecMgrMasterKeyData_t masterKeyData;
  366. masterKeyData.ami = INVALID_NODE_ADDR;
  367. osal_memset( &masterKeyData.key, 0x00, SEC_KEY_LEN );
  368. for ( index = 0; index < ZDSECMGR_MASTERKEY_MAX; index++ )
  369. {
  370. if ( osal_nv_item_init( (ZCD_NV_MASTER_KEY_DATA_START + index),
  371. sizeof(ZDSecMgrMasterKeyData_t), &masterKeyData) == SUCCESS)
  372. {
  373. // the item already exists in NV just needs to be set to default values
  374. osal_nv_write( (ZCD_NV_MASTER_KEY_DATA_START + index), 0,
  375. sizeof(ZDSecMgrMasterKeyData_t), &masterKeyData );
  376. }
  377. }
  378. }
  379. /******************************************************************************
  380. * @fn ZDSecMgrAddrStore
  381. *
  382. * @brief Store device addresses.
  383. *
  384. * @param nwkAddr - [in] NWK address
  385. * @param extAddr - [in] EXT address
  386. * @param ami - [out] Address Manager index
  387. *
  388. * @return ZStatus_t
  389. */
  390. ZStatus_t ZDSecMgrAddrStore( uint16 nwkAddr, uint8* extAddr, uint16* ami )
  391. {
  392. ZStatus_t status;
  393. AddrMgrEntry_t entry;
  394. // add entry
  395. entry.user = ADDRMGR_USER_SECURITY;
  396. entry.nwkAddr = nwkAddr;
  397. AddrMgrExtAddrSet( entry.extAddr, extAddr );
  398. if ( AddrMgrEntryUpdate( &entry ) == TRUE )
  399. {
  400. // return successful results
  401. *ami = entry.index;
  402. status = ZSuccess;
  403. }
  404. else
  405. {
  406. // return failed results
  407. *ami = entry.index;
  408. status = ZNwkUnknownDevice;
  409. }
  410. return status;
  411. }
  412. /******************************************************************************
  413. * @fn ZDSecMgrExtAddrStore
  414. *
  415. * @brief Store EXT address.
  416. *
  417. * @param extAddr - [in] EXT address
  418. * @param ami - [out] Address Manager index
  419. *
  420. * @return ZStatus_t
  421. */
  422. ZStatus_t ZDSecMgrExtAddrStore( uint16 nwkAddr, uint8* extAddr, uint16* ami )
  423. {
  424. ZStatus_t status;
  425. AddrMgrEntry_t entry;
  426. // add entry
  427. entry.user = ADDRMGR_USER_SECURITY;
  428. entry.nwkAddr = nwkAddr;
  429. AddrMgrExtAddrSet( entry.extAddr, extAddr );
  430. if ( AddrMgrEntryUpdate( &entry ) == TRUE )
  431. {
  432. // return successful results
  433. *ami = entry.index;
  434. status = ZSuccess;
  435. }
  436. else
  437. {
  438. // return failed results
  439. *ami = entry.index;
  440. status = ZNwkUnknownDevice;
  441. }
  442. return status;
  443. }
  444. /******************************************************************************
  445. * @fn ZDSecMgrExtAddrLookup
  446. *
  447. * @brief Lookup index for specified EXT address.
  448. *
  449. * @param extAddr - [in] EXT address
  450. * @param ami - [out] Address Manager index
  451. *
  452. * @return ZStatus_t
  453. */
  454. ZStatus_t ZDSecMgrExtAddrLookup( uint8* extAddr, uint16* ami )
  455. {
  456. ZStatus_t status;
  457. AddrMgrEntry_t entry;
  458. // lookup entry
  459. entry.user = ADDRMGR_USER_SECURITY;
  460. AddrMgrExtAddrSet( entry.extAddr, extAddr );
  461. if ( AddrMgrEntryLookupExt( &entry ) == TRUE )
  462. {
  463. // return successful results
  464. *ami = entry.index;
  465. status = ZSuccess;
  466. }
  467. else
  468. {
  469. // return failed results
  470. *ami = entry.index;
  471. status = ZNwkUnknownDevice;
  472. }
  473. return status;
  474. }
  475. /******************************************************************************
  476. * @fn ZDSecMgrAddrClear
  477. *
  478. * @brief Clear security bit from Address Manager for specific device.
  479. *
  480. * @param extAddr - [in] EXT address
  481. *
  482. * @return ZStatus_t
  483. */
  484. ZStatus_t ZDSecMgrAddrClear( uint8* extAddr )
  485. {
  486. ZStatus_t status;
  487. uint16 entryAmi;
  488. // get Address Manager Index
  489. status = ZDSecMgrExtAddrLookup( extAddr, &entryAmi );
  490. if ( status == ZSuccess )
  491. {
  492. AddrMgrEntry_t addrEntry;
  493. // Clear SECURITY User bit from the address manager
  494. addrEntry.user = ADDRMGR_USER_SECURITY;
  495. addrEntry.index = entryAmi;
  496. if ( AddrMgrEntryRelease( &addrEntry ) != TRUE )
  497. {
  498. // return failure results
  499. status = ZFailure;
  500. }
  501. }
  502. return status;
  503. }
  504. /******************************************************************************
  505. * @fn ZDSecMgrMasterKeyLookup
  506. *
  507. * @brief Lookup MASTER key for specified address index.
  508. *
  509. * @param ami - [in] Address Manager index
  510. * @param pKeyNvId - [out] MASTER key NV ID
  511. *
  512. * @return ZStatus_t
  513. */
  514. ZStatus_t ZDSecMgrMasterKeyLookup( uint16 ami, uint16* pKeyNvId )
  515. {
  516. uint16 index;
  517. ZDSecMgrMasterKeyData_t masterKeyData;
  518. for ( index = 0; index < ZDSECMGR_MASTERKEY_MAX ; index++ )
  519. {
  520. // Read entry index of the Master key table from NV
  521. osal_nv_read( (ZCD_NV_MASTER_KEY_DATA_START + index), 0,
  522. sizeof(ZDSecMgrMasterKeyData_t), &masterKeyData );
  523. if ( masterKeyData.ami == ami )
  524. {
  525. // return successful results
  526. *pKeyNvId = ZCD_NV_MASTER_KEY_DATA_START + index;
  527. // clear copy of key in RAM
  528. osal_memset(&masterKeyData, 0x00, sizeof(ZDSecMgrMasterKeyData_t));
  529. return ZSuccess;
  530. }
  531. }
  532. *pKeyNvId = SEC_NO_KEY_NV_ID;
  533. // clear copy of key in RAM
  534. osal_memset(&masterKeyData, 0x00, sizeof(ZDSecMgrMasterKeyData_t));
  535. return ZNwkUnknownDevice;
  536. }
  537. /******************************************************************************
  538. * @fn ZDSecMgrMasterKeyStore
  539. *
  540. * @brief Store MASTER key for specified address index.
  541. *
  542. * @param ami - [in] Address Manager index
  543. * @param key - [in] valid key to store
  544. *
  545. * @return ZStatus_t
  546. */
  547. ZStatus_t ZDSecMgrMasterKeyStore( uint16 ami, uint8* key )
  548. {
  549. uint16 index;
  550. ZDSecMgrMasterKeyData_t masterKeyData;
  551. for ( index = 0; index < ZDSECMGR_MASTERKEY_MAX ; index++ )
  552. {
  553. // Read entry index of the Master key table from NV
  554. osal_nv_read( (ZCD_NV_MASTER_KEY_DATA_START + index), 0,
  555. sizeof(ZDSecMgrMasterKeyData_t), &masterKeyData );
  556. if ( masterKeyData.ami == INVALID_NODE_ADDR )
  557. {
  558. // store EXT address index
  559. masterKeyData.ami = ami;
  560. if ( key != NULL )
  561. {
  562. osal_memcpy( masterKeyData.key, key, SEC_KEY_LEN );
  563. }
  564. else
  565. {
  566. osal_memset( masterKeyData.key, 0, SEC_KEY_LEN );
  567. }
  568. // set new values in NV
  569. osal_nv_write( (ZCD_NV_MASTER_KEY_DATA_START + index), 0,
  570. sizeof(ZDSecMgrMasterKeyData_t), &masterKeyData );
  571. // clear copy of key in RAM
  572. osal_memset( &masterKeyData, 0x00, sizeof(ZDSecMgrMasterKeyData_t) );
  573. // return successful results
  574. return ZSuccess;
  575. }
  576. }
  577. // clear copy of key in RAM
  578. osal_memset( &masterKeyData, 0x00, sizeof(ZDSecMgrMasterKeyData_t) );
  579. return ZNwkUnknownDevice;
  580. }
  581. /******************************************************************************
  582. * @fn ZDSecMgrEntryInit
  583. *
  584. * @brief Initialize entry sub module
  585. *
  586. * @param state - device initialization state
  587. *
  588. * @return none
  589. */
  590. void ZDSecMgrEntryInit(uint8 state)
  591. {
  592. if (ZDSecMgrEntries == NULL)
  593. {
  594. uint16 index;
  595. if ((ZDSecMgrEntries = osal_mem_alloc(sizeof(ZDSecMgrEntry_t) * ZDSECMGR_ENTRY_MAX)) == NULL)
  596. {
  597. return;
  598. }
  599. for (index = 0; index < ZDSECMGR_ENTRY_MAX; index++)
  600. {
  601. ZDSecMgrEntries[index].ami = INVALID_NODE_ADDR;
  602. ZDSecMgrEntries[index].keyNvId = SEC_NO_KEY_NV_ID;
  603. }
  604. }
  605. #if defined NV_RESTORE
  606. if (state == ZDO_INITDEV_RESTORED_NETWORK_STATE)
  607. {
  608. ZDSecMgrRestoreFromNV();
  609. }
  610. #else
  611. (void)state;
  612. #endif
  613. }
  614. /******************************************************************************
  615. * @fn ZDSecMgrEntryLookup
  616. *
  617. * @brief Lookup entry index using specified NWK address.
  618. *
  619. * @param nwkAddr - [in] NWK address
  620. * @param entry - [out] valid entry
  621. *
  622. * @return ZStatus_t
  623. */
  624. ZStatus_t ZDSecMgrEntryLookup( uint16 nwkAddr, ZDSecMgrEntry_t** entry )
  625. {
  626. uint16 index;
  627. AddrMgrEntry_t addrMgrEntry;
  628. // initialize results
  629. *entry = NULL;
  630. // verify data is available
  631. if ( ZDSecMgrEntries != NULL )
  632. {
  633. addrMgrEntry.user = ADDRMGR_USER_SECURITY;
  634. addrMgrEntry.nwkAddr = nwkAddr;
  635. if ( AddrMgrEntryLookupNwk( &addrMgrEntry ) == TRUE )
  636. {
  637. for ( index = 0; index < ZDSECMGR_ENTRY_MAX ; index++ )
  638. {
  639. if ( addrMgrEntry.index == ZDSecMgrEntries[index].ami )
  640. {
  641. // return successful results
  642. *entry = &ZDSecMgrEntries[index];
  643. // break from loop
  644. return ZSuccess;
  645. }
  646. }
  647. }
  648. }
  649. return ZNwkUnknownDevice;
  650. }
  651. /******************************************************************************
  652. * @fn ZDSecMgrEntryLookupAMI
  653. *
  654. * @brief Lookup entry using specified address index
  655. *
  656. * @param ami - [in] Address Manager index
  657. * @param entry - [out] valid entry
  658. *
  659. * @return ZStatus_t
  660. */
  661. ZStatus_t ZDSecMgrEntryLookupAMI( uint16 ami, ZDSecMgrEntry_t** entry )
  662. {
  663. uint16 index;
  664. // initialize results
  665. *entry = NULL;
  666. // verify data is available
  667. if ( ZDSecMgrEntries != NULL )
  668. {
  669. for ( index = 0; index < ZDSECMGR_ENTRY_MAX ; index++ )
  670. {
  671. if ( ZDSecMgrEntries[index].ami == ami )
  672. {
  673. // return successful results
  674. *entry = &ZDSecMgrEntries[index];
  675. // break from loop
  676. return ZSuccess;
  677. }
  678. }
  679. }
  680. return ZNwkUnknownDevice;
  681. }
  682. /******************************************************************************
  683. * @fn ZDSecMgrEntryLookupExt
  684. *
  685. * @brief Lookup entry index using specified EXT address.
  686. *
  687. * @param extAddr - [in] EXT address
  688. * @param entry - [out] valid entry
  689. *
  690. * @return ZStatus_t
  691. */
  692. ZStatus_t ZDSecMgrEntryLookupExt( uint8* extAddr, ZDSecMgrEntry_t** entry )
  693. {
  694. ZStatus_t status;
  695. uint16 ami;
  696. // initialize results
  697. *entry = NULL;
  698. status = ZNwkUnknownDevice;
  699. // lookup address index
  700. if ( ZDSecMgrExtAddrLookup( extAddr, &ami ) == ZSuccess )
  701. {
  702. status = ZDSecMgrEntryLookupAMI( ami, entry );
  703. }
  704. return status;
  705. }
  706. /******************************************************************************
  707. * @fn ZDSecMgrEntryLookupExtGetIndex
  708. *
  709. * @brief Lookup entry index using specified EXT address.
  710. *
  711. * @param extAddr - [in] EXT address
  712. * @param entryIndex - [out] valid index to the entry table
  713. *
  714. * @return ZStatus_t
  715. */
  716. ZStatus_t ZDSecMgrEntryLookupExtGetIndex( uint8* extAddr, ZDSecMgrEntry_t** entry, uint16* entryIndex )
  717. {
  718. uint16 ami;
  719. uint16 index;
  720. // lookup address index
  721. if ( ZDSecMgrExtAddrLookup( extAddr, &ami ) == ZSuccess )
  722. {
  723. // verify data is available
  724. if ( ZDSecMgrEntries != NULL )
  725. {
  726. for ( index = 0; index < ZDSECMGR_ENTRY_MAX ; index++ )
  727. {
  728. if ( ZDSecMgrEntries[index].ami == ami )
  729. {
  730. // return successful results
  731. *entry = &ZDSecMgrEntries[index];
  732. *entryIndex = index;
  733. // break from loop
  734. return ZSuccess;
  735. }
  736. }
  737. }
  738. }
  739. return ZNwkUnknownDevice;
  740. }
  741. /******************************************************************************
  742. * @fn ZDSecMgrEntryLookupAMIGetIndex
  743. *
  744. * @brief Lookup entry using specified address index
  745. *
  746. * @param ami - [in] Address Manager index
  747. * @param entryIndex - [out] valid index to the entry table
  748. *
  749. * @return ZStatus_t
  750. */
  751. ZStatus_t ZDSecMgrEntryLookupAMIGetIndex( uint16 ami, uint16* entryIndex )
  752. {
  753. uint16 index;
  754. // verify data is available
  755. if ( ZDSecMgrEntries != NULL )
  756. {
  757. for ( index = 0; index < ZDSECMGR_ENTRY_MAX ; index++ )
  758. {
  759. if ( ZDSecMgrEntries[index].ami == ami )
  760. {
  761. // return successful results
  762. *entryIndex = index;
  763. // break from loop
  764. return ZSuccess;
  765. }
  766. }
  767. }
  768. return ZNwkUnknownDevice;
  769. }
  770. /******************************************************************************
  771. * @fn ZDSecMgrEntryFree
  772. *
  773. * @brief Free entry.
  774. *
  775. * @param entry - [in] valid entry
  776. *
  777. * @return ZStatus_t
  778. */
  779. void ZDSecMgrEntryFree( ZDSecMgrEntry_t* entry )
  780. {
  781. APSME_LinkKeyData_t *pApsLinkKey = NULL;
  782. #if defined ( NV_RESTORE )
  783. ZStatus_t status;
  784. uint16 entryIndex;
  785. status = ZDSecMgrEntryLookupAMIGetIndex( entry->ami, &entryIndex );
  786. #endif
  787. pApsLinkKey = (APSME_LinkKeyData_t *)osal_mem_alloc(sizeof(APSME_LinkKeyData_t));
  788. if (pApsLinkKey != NULL)
  789. {
  790. osal_memset( pApsLinkKey, 0x00, sizeof(APSME_LinkKeyData_t) );
  791. // Clear the APS Link key in NV
  792. osal_nv_write( entry->keyNvId, 0,
  793. sizeof(APSME_LinkKeyData_t), pApsLinkKey);
  794. // set entry to invalid Key
  795. entry->keyNvId = SEC_NO_KEY_NV_ID;
  796. osal_mem_free(pApsLinkKey);
  797. }
  798. // marking the entry as INVALID_NODE_ADDR
  799. entry->ami = INVALID_NODE_ADDR;
  800. // set to default value
  801. entry->authenticateOption = ZDSecMgr_Not_Authenticated;
  802. #if defined ( NV_RESTORE )
  803. if ( status == ZSuccess )
  804. {
  805. ZDSecMgrUpdateNV(entryIndex);
  806. }
  807. #endif
  808. }
  809. /******************************************************************************
  810. * @fn ZDSecMgrEntryNew
  811. *
  812. * @brief Get a new entry.
  813. *
  814. * @param entry - [out] valid entry
  815. *
  816. * @return ZStatus_t
  817. */
  818. ZStatus_t ZDSecMgrEntryNew( ZDSecMgrEntry_t** entry )
  819. {
  820. uint16 index;
  821. // initialize results
  822. *entry = NULL;
  823. // verify data is available
  824. if ( ZDSecMgrEntries != NULL )
  825. {
  826. // find available entry
  827. for ( index = 0; index < ZDSECMGR_ENTRY_MAX ; index++ )
  828. {
  829. if ( ZDSecMgrEntries[index].ami == INVALID_NODE_ADDR )
  830. {
  831. // return successful result
  832. *entry = &ZDSecMgrEntries[index];
  833. // Set the authentication option to default
  834. ZDSecMgrEntries[index].authenticateOption = ZDSecMgr_Not_Authenticated;
  835. // break from loop
  836. return ZSuccess;
  837. }
  838. }
  839. }
  840. return ZNwkUnknownDevice;
  841. }
  842. /******************************************************************************
  843. * @fn ZDSecMgrCtrlInit
  844. *
  845. * @brief Initialize control sub module
  846. *
  847. * @param none
  848. *
  849. * @return none
  850. */
  851. void ZDSecMgrCtrlInit( void )
  852. {
  853. uint16 size;
  854. uint16 index;
  855. // allocate entry data
  856. size = (short)( sizeof(ZDSecMgrCtrl_t) * ZDSECMGR_CTRL_MAX );
  857. ZDSecMgrCtrlData = osal_mem_alloc( size );
  858. // initialize data
  859. if ( ZDSecMgrCtrlData != NULL )
  860. {
  861. for( index = 0; index < ZDSECMGR_CTRL_MAX; index++ )
  862. {
  863. ZDSecMgrCtrlData[index].state = ZDSECMGR_CTRL_NONE;
  864. }
  865. }
  866. }
  867. /******************************************************************************
  868. * @fn ZDSecMgrCtrlRelease
  869. *
  870. * @brief Release control data.
  871. *
  872. * @param ctrl - [in] valid control data
  873. *
  874. * @return none
  875. */
  876. void ZDSecMgrCtrlRelease( ZDSecMgrCtrl_t* ctrl )
  877. {
  878. // should always be enough entry control data
  879. ctrl->state = ZDSECMGR_CTRL_NONE;
  880. }
  881. /******************************************************************************
  882. * @fn ZDSecMgrCtrlLookup
  883. *
  884. * @brief Lookup control data.
  885. *
  886. * @param entry - [in] valid entry data
  887. * @param ctrl - [out] control data - NULL if not found
  888. *
  889. * @return none
  890. */
  891. void ZDSecMgrCtrlLookup( ZDSecMgrEntry_t* entry, ZDSecMgrCtrl_t** ctrl )
  892. {
  893. uint16 index;
  894. // initialize search results
  895. *ctrl = NULL;
  896. // verify data is available
  897. if ( ZDSecMgrCtrlData != NULL )
  898. {
  899. for ( index = 0; index < ZDSECMGR_CTRL_MAX; index++ )
  900. {
  901. // make sure control data is in use
  902. if ( ZDSecMgrCtrlData[index].state != ZDSECMGR_CTRL_NONE )
  903. {
  904. // check for entry match
  905. if ( ZDSecMgrCtrlData[index].entry == entry )
  906. {
  907. // return this control data
  908. *ctrl = &ZDSecMgrCtrlData[index];
  909. // break from loop
  910. return;
  911. }
  912. }
  913. }
  914. }
  915. }
  916. /******************************************************************************
  917. * @fn ZDSecMgrCtrlSet
  918. *
  919. * @brief Set control data.
  920. *
  921. * @param device - [in] valid device data
  922. * @param entry - [in] valid entry data
  923. * @param ctrl - [in] valid control data
  924. *
  925. * @return none
  926. */
  927. void ZDSecMgrCtrlSet( ZDSecMgrDevice_t* device,
  928. ZDSecMgrEntry_t* entry,
  929. ZDSecMgrCtrl_t* ctrl )
  930. {
  931. // set control date
  932. ctrl->parentAddr = device->parentAddr;
  933. ctrl->secure = device->secure;
  934. ctrl->entry = entry;
  935. ctrl->state = ZDSECMGR_CTRL_INIT;
  936. ctrl->cntr = 0;
  937. // set device pointer
  938. device->ctrl = ctrl;
  939. }
  940. /******************************************************************************
  941. * @fn ZDSecMgrCtrlAdd
  942. *
  943. * @brief Add control data.
  944. *
  945. * @param device - [in] valid device data
  946. * @param entry - [in] valid entry data
  947. *
  948. * @return ZStatus_t
  949. */
  950. ZStatus_t ZDSecMgrCtrlAdd( ZDSecMgrDevice_t* device, ZDSecMgrEntry_t* entry )
  951. {
  952. uint16 index;
  953. // verify data is available
  954. if ( ZDSecMgrCtrlData != NULL )
  955. {
  956. // look for an empty slot
  957. for ( index = 0; index < ZDSECMGR_CTRL_MAX; index++ )
  958. {
  959. if ( ZDSecMgrCtrlData[index].state == ZDSECMGR_CTRL_NONE )
  960. {
  961. // return successful results
  962. ZDSecMgrCtrlSet( device, entry, &ZDSecMgrCtrlData[index] );
  963. // break from loop
  964. return ZSuccess;
  965. }
  966. }
  967. }
  968. return ZNwkUnknownDevice;
  969. }
  970. /******************************************************************************
  971. * @fn ZDSecMgrCtrlTerm
  972. *
  973. * @brief Terminate device control.
  974. *
  975. * @param entry - [in] valid entry data
  976. *
  977. * @return none
  978. */
  979. void ZDSecMgrCtrlTerm( ZDSecMgrEntry_t* entry )
  980. {
  981. ZDSecMgrCtrl_t* ctrl;
  982. // remove device from control data
  983. ZDSecMgrCtrlLookup ( entry, &ctrl );
  984. if ( ctrl != NULL )
  985. {
  986. ZDSecMgrCtrlRelease ( ctrl );
  987. }
  988. }
  989. /******************************************************************************
  990. * @fn ZDSecMgrCtrlReset
  991. *
  992. * @brief Reset control data.
  993. *
  994. * @param device - [in] valid device data
  995. * @param entry - [in] valid entry data
  996. *
  997. * @return ZStatus_t
  998. */
  999. ZStatus_t ZDSecMgrCtrlReset( ZDSecMgrDevice_t* device, ZDSecMgrEntry_t* entry )
  1000. {
  1001. ZStatus_t status;
  1002. ZDSecMgrCtrl_t* ctrl;
  1003. // initialize results
  1004. status = ZNwkUnknownDevice;
  1005. // look for a match for the entry
  1006. ZDSecMgrCtrlLookup( entry, &ctrl );
  1007. if ( ctrl != NULL )
  1008. {
  1009. ZDSecMgrCtrlSet( device, entry, ctrl );
  1010. status = ZSuccess;
  1011. }
  1012. else
  1013. {
  1014. status = ZDSecMgrCtrlAdd( device, entry );
  1015. }
  1016. return status;
  1017. }
  1018. /******************************************************************************
  1019. * @fn ZDSecMgrMasterKeyLoad
  1020. *
  1021. * @brief Load the MASTER key for device with specified EXT
  1022. * address.
  1023. *
  1024. * @param extAddr - [in] EXT address of device
  1025. * @param key - [in] MASTER key shared with device
  1026. *
  1027. * @return ZStatus_t
  1028. */
  1029. ZStatus_t ZDSecMgrMasterKeyLoad( uint8* extAddr, uint8* key )
  1030. {
  1031. ZStatus_t status;
  1032. uint16 ami;
  1033. uint16 keyNvId;
  1034. // set status based on policy
  1035. status = ZDSecMgrExtAddrLookup( extAddr, &ami );
  1036. if ( status == ZSuccess )
  1037. {
  1038. // get the address NV ID
  1039. if ( ZDSecMgrMasterKeyLookup( ami, &keyNvId ) == ZSuccess )
  1040. {
  1041. // overwrite old key in NV
  1042. osal_nv_write( keyNvId, osal_offsetof(ZDSecMgrMasterKeyData_t, key),
  1043. SEC_KEY_LEN, key );
  1044. }
  1045. else
  1046. {
  1047. // store new key -- NULL will zero key
  1048. status = ZDSecMgrMasterKeyStore( ami, key );
  1049. }
  1050. }
  1051. return status;
  1052. }
  1053. /******************************************************************************
  1054. * @fn ZDSecMgrAppKeyGet
  1055. *
  1056. * @brief get an APP key - option APP(MASTER or LINK) key
  1057. *
  1058. * @param initNwkAddr - [in] NWK address of initiator device
  1059. * @param initExtAddr - [in] EXT address of initiator device
  1060. * @param partNwkAddr - [in] NWK address of partner device
  1061. * @param partExtAddr - [in] EXT address of partner device
  1062. * @param key - [out] APP(MASTER or LINK) key
  1063. * @param keyType - [out] APP(MASTER or LINK) key type
  1064. *
  1065. * @return ZStatus_t
  1066. */
  1067. uint8 ZDSecMgrAppKeyType = KEY_TYPE_APP_LINK; // Set the default key type
  1068. // to KEY_TYPE_APP_LINK since
  1069. // only specific requirement
  1070. // right now comes from SE profile
  1071. ZStatus_t ZDSecMgrAppKeyGet( uint16 initNwkAddr,
  1072. uint8* initExtAddr,
  1073. uint16 partNwkAddr,
  1074. uint8* partExtAddr,
  1075. uint8** key,
  1076. uint8* keyType )
  1077. {
  1078. // Intentionally unreferenced parameters
  1079. (void)initNwkAddr;
  1080. (void)initExtAddr;
  1081. (void)partNwkAddr;
  1082. (void)partExtAddr;
  1083. //---------------------------------------------------------------------------
  1084. // note:
  1085. // should use a robust mechanism to generate keys, for example
  1086. // combine EXT addresses and call a hash function
  1087. //---------------------------------------------------------------------------
  1088. SSP_GetTrueRand( SEC_KEY_LEN, *key );
  1089. *keyType = ZDSecMgrAppKeyType;
  1090. return ZSuccess;
  1091. }
  1092. /******************************************************************************
  1093. * @fn ZDSecMgrAppKeyReq
  1094. *
  1095. * @brief Process request for APP key between two devices.
  1096. *
  1097. * @param device - [in] ZDO_RequestKeyInd_t, request info
  1098. *
  1099. * @return none
  1100. */
  1101. void ZDSecMgrAppKeyReq( ZDO_RequestKeyInd_t* ind )
  1102. {
  1103. APSME_TransportKeyReq_t req;
  1104. uint8 initExtAddr[Z_EXTADDR_LEN];
  1105. uint16 partNwkAddr;
  1106. uint8 key[SEC_KEY_LEN];
  1107. // validate initiator and partner
  1108. if ( ( APSME_LookupNwkAddr( ind->partExtAddr, &partNwkAddr ) == TRUE ) &&
  1109. ( APSME_LookupExtAddr( ind->srcAddr, initExtAddr ) == TRUE ) )
  1110. {
  1111. // point the key to some memory
  1112. req.key = key;
  1113. // get an APP key - option APP (MASTER or LINK) key
  1114. if ( ZDSecMgrAppKeyGet( ind->srcAddr,
  1115. initExtAddr,
  1116. partNwkAddr,
  1117. ind->partExtAddr,
  1118. &req.key,
  1119. &req.keyType ) == ZSuccess )
  1120. {
  1121. // always secure
  1122. req.nwkSecure = TRUE;
  1123. req.apsSecure = TRUE;
  1124. req.tunnel = NULL;
  1125. // send key to initiator device
  1126. req.dstAddr = ind->srcAddr;
  1127. req.extAddr = ind->partExtAddr;
  1128. req.initiator = TRUE;
  1129. APSME_TransportKeyReq( &req );
  1130. // send key to partner device
  1131. req.dstAddr = partNwkAddr;
  1132. req.extAddr = initExtAddr;
  1133. req.initiator = FALSE;
  1134. APSME_TransportKeyReq( &req );
  1135. // clear copy of key in RAM
  1136. osal_memset( key, 0x00, SEC_KEY_LEN);
  1137. }
  1138. }
  1139. }
  1140. /******************************************************************************
  1141. * @fn ZDSecMgrEstablishKey
  1142. *
  1143. * @brief Start SKKE with device joining network.
  1144. *
  1145. * @param device - [in] ZDSecMgrDevice_t, device info
  1146. *
  1147. * @return ZStatus_t
  1148. */
  1149. ZStatus_t ZDSecMgrEstablishKey( ZDSecMgrDevice_t* device )
  1150. {
  1151. ZStatus_t status;
  1152. APSME_EstablishKeyReq_t req;
  1153. req.respExtAddr = device->extAddr;
  1154. req.method = APSME_SKKE_METHOD;
  1155. if ( device->parentAddr == NLME_GetShortAddr() )
  1156. {
  1157. req.dstAddr = device->nwkAddr;
  1158. //devtag.0604.todo - remove obsolete
  1159. req.apsSecure = FALSE;
  1160. req.nwkSecure = FALSE;
  1161. }
  1162. else
  1163. {
  1164. req.dstAddr = device->parentAddr;
  1165. //devtag.0604.todo - remove obsolete
  1166. req.apsSecure = TRUE;
  1167. req.nwkSecure = TRUE;
  1168. }
  1169. status = APSME_EstablishKeyReq( &req );
  1170. return status;
  1171. }
  1172. /******************************************************************************
  1173. * @fn ZDSecMgrSendMasterKey
  1174. *
  1175. * @brief Send MASTER key to device joining network.
  1176. *
  1177. * @param device - [in] ZDSecMgrDevice_t, device info
  1178. *
  1179. * @return ZStatus_t
  1180. */
  1181. ZStatus_t ZDSecMgrSendMasterKey( ZDSecMgrDevice_t* device )
  1182. {
  1183. ZStatus_t status;
  1184. APSME_TransportKeyReq_t req;
  1185. uint16 keyNvId;
  1186. uint8 masterKey[SEC_KEY_LEN];
  1187. req.keyType = KEY_TYPE_TC_MASTER;
  1188. req.extAddr = device->extAddr;
  1189. req.tunnel = NULL;
  1190. if ( ZDSecMgrMasterKeyLookup( device->ctrl->entry->ami, &keyNvId ) == ZSuccess )
  1191. {
  1192. osal_nv_read( keyNvId, osal_offsetof(ZDSecMgrMasterKeyData_t, key),
  1193. SEC_KEY_LEN, masterKey );
  1194. }
  1195. else
  1196. {
  1197. // in case read from NV fails
  1198. osal_memset( masterKey, 0x00, SEC_KEY_LEN);
  1199. }
  1200. req.key = masterKey;
  1201. //check if using secure hop to to parent
  1202. if ( device->parentAddr != NLME_GetShortAddr() )
  1203. {
  1204. //send to parent with security
  1205. req.dstAddr = device->parentAddr;
  1206. req.nwkSecure = TRUE;
  1207. req.apsSecure = TRUE;
  1208. }
  1209. else
  1210. {
  1211. //direct with no security
  1212. req.dstAddr = device->nwkAddr;
  1213. req.nwkSecure = FALSE;
  1214. req.apsSecure = FALSE;
  1215. }
  1216. status = APSME_TransportKeyReq( &req );
  1217. // clear copy of key in RAM
  1218. osal_memset( masterKey, 0x00, SEC_KEY_LEN);
  1219. return status;
  1220. }
  1221. /******************************************************************************
  1222. * @fn ZDSecMgrSendNwkKey
  1223. *
  1224. * @brief Send NWK key to device joining network.
  1225. *
  1226. * @param device - [in] ZDSecMgrDevice_t, device info
  1227. *
  1228. * @return ZStatus_t
  1229. */
  1230. ZStatus_t ZDSecMgrSendNwkKey( ZDSecMgrDevice_t* device )
  1231. {
  1232. ZStatus_t status;
  1233. APSME_TransportKeyReq_t req;
  1234. APSDE_FrameTunnel_t tunnel;
  1235. nwkKeyDesc tmpKey;
  1236. req.dstAddr = device->nwkAddr;
  1237. req.extAddr = device->extAddr;
  1238. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  1239. {
  1240. req.keyType = KEY_TYPE_NWK_HIGH;
  1241. }
  1242. else
  1243. {
  1244. req.keyType = KEY_TYPE_NWK;
  1245. }
  1246. // get the Active Key into a local variable
  1247. if( NLME_ReadNwkKeyInfo( 0, sizeof(tmpKey), &tmpKey,
  1248. ZCD_NV_NWK_ACTIVE_KEY_INFO ) != SUCCESS )
  1249. {
  1250. // set key data to all 0s if NV read fails
  1251. osal_memset(&tmpKey, 0x00, sizeof(tmpKey));
  1252. }
  1253. if ( (ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH)
  1254. || (ZG_CHECK_SECURITY_MODE == ZG_SECURITY_SE_STANDARD) )
  1255. {
  1256. // set values
  1257. req.keySeqNum = tmpKey.keySeqNum;
  1258. req.key = tmpKey.key;
  1259. //devtag.pro.security.todo - make sure that if there is no link key the NWK
  1260. //key isn't used to secure the frame at the APS layer -- since the receiving
  1261. //device may not have a NWK key yet
  1262. req.apsSecure = TRUE;
  1263. // check if using secure hop to to parent
  1264. if ( device->parentAddr == NLME_GetShortAddr() )
  1265. {
  1266. req.nwkSecure = FALSE;
  1267. req.tunnel = NULL;
  1268. }
  1269. else
  1270. {
  1271. req.nwkSecure = TRUE;
  1272. req.tunnel = &tunnel;
  1273. req.tunnel->tna = device->parentAddr;
  1274. req.tunnel->dea = device->extAddr;
  1275. }
  1276. }
  1277. else
  1278. {
  1279. // default values
  1280. //devtag.0604.verify
  1281. req.nwkSecure = TRUE;
  1282. req.apsSecure = FALSE;
  1283. req.tunnel = NULL;
  1284. if ( device->parentAddr != NLME_GetShortAddr() )
  1285. {
  1286. req.dstAddr = device->parentAddr;
  1287. }
  1288. // special cases
  1289. //devtag.0604.todo - modify to preconfig flag
  1290. if ( device->secure == FALSE )
  1291. {
  1292. req.keySeqNum = tmpKey.keySeqNum;
  1293. req.key = tmpKey.key;
  1294. // check if using secure hop to to parent
  1295. if ( device->parentAddr == NLME_GetShortAddr() )
  1296. {
  1297. req.nwkSecure = FALSE;
  1298. }
  1299. }
  1300. else
  1301. {
  1302. req.key = NULL;
  1303. req.keySeqNum = 0;
  1304. }
  1305. }
  1306. status = APSME_TransportKeyReq( &req );
  1307. // clear copy of key in RAM before return
  1308. osal_memset( &tmpKey, 0x00, sizeof(nwkKeyDesc) );
  1309. return status;
  1310. }
  1311. /******************************************************************************
  1312. * @fn ZDSecMgrDeviceEntryRemove
  1313. *
  1314. * @brief Remove device entry.
  1315. *
  1316. * @param entry - [in] valid entry
  1317. *
  1318. * @return none
  1319. */
  1320. void ZDSecMgrDeviceEntryRemove( ZDSecMgrEntry_t* entry )
  1321. {
  1322. // terminate device control
  1323. if ( ( ZG_BUILD_COORDINATOR_TYPE ) && ( ZG_DEVICE_COORDINATOR_TYPE ) )
  1324. {
  1325. ZDSecMgrCtrlTerm( entry );
  1326. }
  1327. // remove device from entry data
  1328. ZDSecMgrEntryFree( entry );
  1329. }
  1330. /******************************************************************************
  1331. * @fn ZDSecMgrDeviceRemoveByExtAddr
  1332. *
  1333. * @brief Remove device entry by its ext address.
  1334. *
  1335. * @param pAddr - pointer to the extended address
  1336. *
  1337. * @return ZStatus_t
  1338. */
  1339. ZStatus_t ZDSecMgrDeviceRemoveByExtAddr( uint8 *pAddr )
  1340. {
  1341. ZDSecMgrEntry_t *pEntry;
  1342. uint8 retValue;
  1343. retValue = (uint8)ZDSecMgrEntryLookupExt( pAddr, &pEntry );
  1344. if( retValue == ZSuccess )
  1345. {
  1346. ZDSecMgrDeviceEntryRemove( pEntry );
  1347. }
  1348. return retValue;
  1349. }
  1350. /******************************************************************************
  1351. * @fn ZDSecMgrDeviceEntryAdd
  1352. *
  1353. * @brief Add entry.
  1354. *
  1355. * @param device - [in] ZDSecMgrDevice_t, device info
  1356. * @param ami - [in] Address Manager index
  1357. *
  1358. * @return ZStatus_t
  1359. */
  1360. void ZDSecMgrAddrMgrUpdate( uint16 ami, uint16 nwkAddr )
  1361. {
  1362. AddrMgrEntry_t entry;
  1363. // get the ami data
  1364. entry.user = ADDRMGR_USER_SECURITY;
  1365. entry.index = ami;
  1366. AddrMgrEntryGet( &entry );
  1367. // check if NWK address is same
  1368. if ( entry.nwkAddr != nwkAddr )
  1369. {
  1370. // update NWK address
  1371. entry.nwkAddr = nwkAddr;
  1372. AddrMgrEntryUpdate( &entry );
  1373. }
  1374. }
  1375. ZStatus_t ZDSecMgrDeviceEntryAdd( ZDSecMgrDevice_t* device, uint16 ami )
  1376. {
  1377. ZStatus_t status;
  1378. ZDSecMgrEntry_t* entry;
  1379. // initialize as unknown until completion
  1380. status = ZNwkUnknownDevice;
  1381. device->ctrl = NULL;
  1382. // make sure not already registered
  1383. if ( ZDSecMgrEntryLookup( device->nwkAddr, &entry ) == ZSuccess )
  1384. {
  1385. // verify that address index is same
  1386. if ( entry->ami != ami )
  1387. {
  1388. // remove conflicting entry
  1389. ZDSecMgrDeviceEntryRemove( entry );
  1390. if ( ZDSecMgrEntryLookupAMI( ami, &entry ) == ZSuccess )
  1391. {
  1392. // update NWK address
  1393. ZDSecMgrAddrMgrUpdate( ami, device->nwkAddr );
  1394. }
  1395. }
  1396. }
  1397. else if ( ZDSecMgrEntryLookupAMI( ami, &entry ) == ZSuccess )
  1398. {
  1399. // update NWK address
  1400. ZDSecMgrAddrMgrUpdate( ami, device->nwkAddr );
  1401. }
  1402. // check if a new entry needs to be created
  1403. if ( entry == NULL )
  1404. {
  1405. // get new entry
  1406. if ( ZDSecMgrEntryNew( &entry ) == ZSuccess )
  1407. {
  1408. // reset entry lkd
  1409. // finish setting up entry
  1410. entry->ami = ami;
  1411. // update NWK address
  1412. ZDSecMgrAddrMgrUpdate( ami, device->nwkAddr );
  1413. // enter new device into device control
  1414. if ( ( ZG_BUILD_COORDINATOR_TYPE ) && ( ZG_DEVICE_COORDINATOR_TYPE ) )
  1415. {
  1416. status = ZDSecMgrCtrlAdd( device, entry );
  1417. }
  1418. else
  1419. {
  1420. status = ZSuccess;
  1421. }
  1422. }
  1423. }
  1424. else
  1425. {
  1426. // reset entry lkd
  1427. // reset entry in entry control
  1428. if ( ( ZG_BUILD_COORDINATOR_TYPE ) && ( ZG_DEVICE_COORDINATOR_TYPE ) )
  1429. {
  1430. status = ZDSecMgrCtrlReset( device, entry );
  1431. }
  1432. else
  1433. {
  1434. status = ZSuccess;
  1435. }
  1436. }
  1437. return status;
  1438. }
  1439. /******************************************************************************
  1440. * @fn ZDSecMgrDeviceCtrlHandler
  1441. *
  1442. * @brief Device control handler.
  1443. *
  1444. * @param device - [in] ZDSecMgrDevice_t, device info
  1445. *
  1446. * @return none
  1447. */
  1448. void ZDSecMgrDeviceCtrlHandler( ZDSecMgrDevice_t* device )
  1449. {
  1450. uint8 state;
  1451. uint8 cntr;
  1452. state = device->ctrl->state;
  1453. cntr = ZDSECMGR_CTRL_BASE_CNTR;
  1454. switch ( state )
  1455. {
  1456. case ZDSECMGR_CTRL_TK_MASTER:
  1457. if ( ZDSecMgrSendMasterKey( device ) == ZSuccess )
  1458. {
  1459. state = ZDSECMGR_CTRL_SKKE_INIT;
  1460. cntr = ZDSECMGR_CTRL_SKKE_INIT_CNTR;
  1461. }
  1462. break;
  1463. case ZDSECMGR_CTRL_SKKE_INIT:
  1464. if ( ZDSecMgrEstablishKey( device ) == ZSuccess )
  1465. {
  1466. state = ZDSECMGR_CTRL_SKKE_WAIT;
  1467. }
  1468. break;
  1469. case ZDSECMGR_CTRL_SKKE_WAIT:
  1470. // continue to wait for SKA control timeout
  1471. break;
  1472. case ZDSECMGR_CTRL_TK_NWK:
  1473. if ( ZDSecMgrSendNwkKey( device ) == ZSuccess )
  1474. {
  1475. state = ZDSECMGR_CTRL_NONE;
  1476. }
  1477. break;
  1478. default:
  1479. state = ZDSECMGR_CTRL_NONE;
  1480. break;
  1481. }
  1482. if ( state != ZDSECMGR_CTRL_NONE )
  1483. {
  1484. device->ctrl->state = state;
  1485. device->ctrl->cntr = cntr;
  1486. osal_start_timerEx(ZDAppTaskID, ZDO_SECMGR_EVENT, 100 );
  1487. }
  1488. else
  1489. {
  1490. ZDSecMgrCtrlRelease( device->ctrl );
  1491. }
  1492. }
  1493. /******************************************************************************
  1494. * @fn ZDSecMgrDeviceCtrlSetup
  1495. *
  1496. * @brief Setup device control.
  1497. *
  1498. * @param device - [in] ZDSecMgrDevice_t, device info
  1499. *
  1500. * @return ZStatus_t
  1501. */
  1502. void ZDSecMgrDeviceCtrlSetup( ZDSecMgrDevice_t* device )
  1503. {
  1504. if ( device->ctrl != NULL )
  1505. {
  1506. if ( device->secure == FALSE )
  1507. {
  1508. // send the master key data to the joining device
  1509. device->ctrl->state = ZDSECMGR_CTRL_TK_MASTER;
  1510. }
  1511. else
  1512. {
  1513. // start SKKE
  1514. device->ctrl->state = ZDSECMGR_CTRL_SKKE_INIT;
  1515. }
  1516. ZDSecMgrDeviceCtrlHandler( device );
  1517. }
  1518. }
  1519. /******************************************************************************
  1520. * @fn ZDSecMgrDeviceCtrlUpdate
  1521. *
  1522. * @brief Update control data.
  1523. *
  1524. * @param extAddr - [in] EXT address
  1525. * @param state - [in] new control state
  1526. *
  1527. * @return none
  1528. */
  1529. void ZDSecMgrDeviceCtrlUpdate( uint8* extAddr, uint8 state )
  1530. {
  1531. ZDSecMgrEntry_t* entry;
  1532. ZDSecMgrCtrl_t* ctrl;
  1533. // lookup device entry data
  1534. (void)ZDSecMgrEntryLookupExt( extAddr, &entry );
  1535. if ( entry != NULL )
  1536. {
  1537. // lookup device control data
  1538. ZDSecMgrCtrlLookup( entry, &ctrl );
  1539. // make sure control data is valid
  1540. if ( ctrl != NULL )
  1541. {
  1542. // possible state transitions
  1543. if ( ctrl->state == ZDSECMGR_CTRL_SKKE_WAIT )
  1544. {
  1545. if ( state == ZDSECMGR_CTRL_SKKE_DONE )
  1546. {
  1547. // send the network key
  1548. ctrl->state = ZDSECMGR_CTRL_TK_NWK;
  1549. ctrl->cntr = ZDSECMGR_CTRL_TK_NWK_CNTR;
  1550. }
  1551. else if ( state == ZDSECMGR_CTRL_SKKE_FAIL )
  1552. {
  1553. // force default timeout in order to cleanup control logic
  1554. ctrl->state = ZDSECMGR_CTRL_SKKE_FAIL;
  1555. ctrl->cntr = ZDSECMGR_CTRL_BASE_CNTR;
  1556. }
  1557. }
  1558. // timer should be active
  1559. }
  1560. }
  1561. }
  1562. void APSME_SKA_TimerExpired( uint8 initiator, uint8* partExtAddr );
  1563. void APSME_SKA_TimerExpired( uint8 initiator, uint8* partExtAddr )
  1564. {
  1565. if ( ( ZG_BUILD_COORDINATOR_TYPE ) && ( ZG_DEVICE_COORDINATOR_TYPE ) )
  1566. {
  1567. if ( initiator == TRUE )
  1568. {
  1569. ZDSecMgrDeviceCtrlUpdate( partExtAddr, ZDSECMGR_CTRL_SKKE_FAIL );
  1570. }
  1571. }
  1572. }
  1573. /******************************************************************************
  1574. * @fn ZDSecMgrDeviceRemove
  1575. *
  1576. * @brief Remove device from network.
  1577. *
  1578. * @param device - [in] ZDSecMgrDevice_t, device info
  1579. *
  1580. * @return none
  1581. */
  1582. void ZDSecMgrDeviceRemove( ZDSecMgrDevice_t* device )
  1583. {
  1584. APSME_RemoveDeviceReq_t remDevReq;
  1585. NLME_LeaveReq_t leaveReq;
  1586. associated_devices_t* assoc;
  1587. // check if parent, remove the device
  1588. if ( device->parentAddr == NLME_GetShortAddr() )
  1589. {
  1590. // this is the parent of the device
  1591. leaveReq.extAddr = device->extAddr;
  1592. leaveReq.removeChildren = FALSE;
  1593. leaveReq.rejoin = FALSE;
  1594. // find child association
  1595. assoc = AssocGetWithExt( device->extAddr );
  1596. if ( ( assoc != NULL ) &&
  1597. ( assoc->nodeRelation >= CHILD_RFD ) &&
  1598. ( assoc->nodeRelation <= CHILD_FFD_RX_IDLE ) )
  1599. {
  1600. // check if associated device is authenticated
  1601. if ( assoc->devStatus & DEV_SEC_AUTH_STATUS )
  1602. {
  1603. leaveReq.silent = FALSE;
  1604. }
  1605. else
  1606. {
  1607. leaveReq.silent = TRUE;
  1608. }
  1609. NLME_LeaveReq( &leaveReq );
  1610. }
  1611. }
  1612. else
  1613. {
  1614. // this is not the parent of the device
  1615. remDevReq.parentAddr = device->parentAddr;
  1616. remDevReq.childExtAddr = device->extAddr;
  1617. APSME_RemoveDeviceReq( &remDevReq );
  1618. }
  1619. }
  1620. /******************************************************************************
  1621. * @fn ZDSecMgrDeviceValidateSKKE
  1622. *
  1623. * @brief Decide whether device is allowed for SKKE.
  1624. *
  1625. * @param device - [in] ZDSecMgrDevice_t, device info
  1626. *
  1627. * @return ZStatus_t
  1628. */
  1629. ZStatus_t ZDSecMgrDeviceValidateSKKE( ZDSecMgrDevice_t* device )
  1630. {
  1631. ZStatus_t status;
  1632. uint16 ami;
  1633. uint16 keyNvId;
  1634. // get EXT address
  1635. status = ZDSecMgrExtAddrLookup( device->extAddr, &ami );
  1636. if ( status == ZSuccess )
  1637. {
  1638. // get MASTER key
  1639. status = ZDSecMgrMasterKeyLookup( ami, &keyNvId );
  1640. if ( status == ZSuccess )
  1641. {
  1642. status = ZDSecMgrDeviceEntryAdd( device, ami );
  1643. }
  1644. }
  1645. return status;
  1646. }
  1647. /******************************************************************************
  1648. * @fn ZDSecMgrDeviceValidateRM (RESIDENTIAL MODE)
  1649. *
  1650. * @brief Decide whether device is allowed.
  1651. *
  1652. * @param device - [in] ZDSecMgrDevice_t, device info
  1653. *
  1654. * @return ZStatus_t
  1655. */
  1656. ZStatus_t ZDSecMgrDeviceValidateRM( ZDSecMgrDevice_t* device )
  1657. {
  1658. ZStatus_t status;
  1659. status = ZSuccess;
  1660. (void)device; // Intentionally unreferenced parameter
  1661. // For test purpose, turning off the zgSecurePermitJoin flag will force
  1662. // the trust center to reject any newly joining devices by sending
  1663. // Remove-device to the parents.
  1664. if ( zgSecurePermitJoin == FALSE )
  1665. {
  1666. status = ZNwkUnknownDevice;
  1667. }
  1668. #if 0 // Taken out because the following functionality is only used for test
  1669. // purpose. A more efficient (above) way is used. It can be put
  1670. // back in if customers request for a white/black list feature.
  1671. // ZDSecMgrStoredDeviceList[] is defined in ZDSecMgr.c
  1672. // The following code processes the device black list (stored device list)
  1673. // If the joining device is not part of the forbidden device list
  1674. // Return ZSuccess. Otherwise, return ZNwkUnknownDevice. The trust center
  1675. // will send Remove-device and ban the device from joining.
  1676. uint8 index;
  1677. uint8* restricted;
  1678. // Look through the stored device list - used for restricted devices
  1679. for ( index = 0; index < ZDSECMGR_STORED_DEVICES; index++ )
  1680. {
  1681. restricted = ZDSecMgrStoredDeviceList[index];
  1682. if ( AddrMgrExtAddrEqual( restricted, device->extAddr ) == TRUE )
  1683. {
  1684. // return as unknown device in regards to validation
  1685. status = ZNwkUnknownDevice;
  1686. // break from loop
  1687. index = ZDSECMGR_STORED_DEVICES;
  1688. }
  1689. }
  1690. #endif
  1691. return status;
  1692. }
  1693. /******************************************************************************
  1694. * @fn ZDSecMgrDeviceValidateCM (COMMERCIAL MODE)
  1695. *
  1696. * @brief Decide whether device is allowed.
  1697. *
  1698. * @param device - [in] ZDSecMgrDevice_t, device info
  1699. *
  1700. * @return ZStatus_t
  1701. */
  1702. //devtag.pro.security
  1703. ZStatus_t ZDSecMgrDeviceValidateCM( ZDSecMgrDevice_t* device )
  1704. {
  1705. ZStatus_t status;
  1706. uint16 ami;
  1707. uint8 key[SEC_KEY_LEN];
  1708. // implement EXT address and MASTER key policy here -- the total number of
  1709. // Security Manager entries should never exceed the number of EXT addresses
  1710. // and MASTER keys available
  1711. // set status based on policy
  1712. //status = ZNwkUnknownDevice;
  1713. // set status based on policy
  1714. status = ZSuccess; // ZNwkUnknownDevice;
  1715. // get key based on policy
  1716. osal_memcpy( key, ZDSecMgrTCMasterKey, SEC_KEY_LEN);
  1717. // if policy, store new EXT address
  1718. status = ZDSecMgrAddrStore( device->nwkAddr, device->extAddr, &ami );
  1719. // set the key
  1720. ZDSecMgrMasterKeyLoad( device->extAddr, key );
  1721. // if EXT address and MASTER key available -- add device
  1722. if ( status == ZSuccess )
  1723. {
  1724. // add device to internal data - with control
  1725. status = ZDSecMgrDeviceEntryAdd( device, ami );
  1726. }
  1727. // remove copy of key in RAM
  1728. osal_memset( key, 0x00, SEC_KEY_LEN );
  1729. return status;
  1730. }
  1731. /******************************************************************************
  1732. * @fn ZDSecMgrDeviceValidate
  1733. *
  1734. * @brief Decide whether device is allowed.
  1735. *
  1736. * @param device - [in] ZDSecMgrDevice_t, device info
  1737. *
  1738. * @return ZStatus_t
  1739. */
  1740. ZStatus_t ZDSecMgrDeviceValidate( ZDSecMgrDevice_t* device )
  1741. {
  1742. ZStatus_t status;
  1743. if ( ZDSecMgrPermitJoiningEnabled == TRUE )
  1744. {
  1745. // device may be joining with a secure flag but it is ultimately the Trust
  1746. // Center that decides -- check if expected pre configured device --
  1747. // override settings
  1748. if ( zgPreConfigKeys == TRUE )
  1749. {
  1750. device->secure = TRUE;
  1751. }
  1752. else
  1753. {
  1754. device->secure = FALSE;
  1755. }
  1756. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  1757. {
  1758. status = ZDSecMgrDeviceValidateCM( device );
  1759. }
  1760. else // ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_RESIDENTIAL )
  1761. {
  1762. status = ZDSecMgrDeviceValidateRM( device );
  1763. }
  1764. }
  1765. else
  1766. {
  1767. status = ZNwkUnknownDevice;
  1768. }
  1769. return status;
  1770. }
  1771. /******************************************************************************
  1772. * @fn ZDSecMgrDeviceJoin
  1773. *
  1774. * @brief Try to join this device.
  1775. *
  1776. * @param device - [in] ZDSecMgrDevice_t, device info
  1777. *
  1778. * @return ZStatus_t
  1779. */
  1780. ZStatus_t ZDSecMgrDeviceJoin( ZDSecMgrDevice_t* device )
  1781. {
  1782. ZStatus_t status;
  1783. uint16 ami;
  1784. // attempt to validate device
  1785. status = ZDSecMgrDeviceValidate( device );
  1786. if ( status == ZSuccess )
  1787. {
  1788. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  1789. {
  1790. ZDSecMgrDeviceCtrlSetup( device );
  1791. }
  1792. else // ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_RESIDENTIAL )
  1793. {
  1794. // Add the device to the address manager
  1795. ZDSecMgrAddrStore( device->nwkAddr, device->extAddr, &ami );
  1796. //send the nwk key data to the joining device
  1797. status = ZDSecMgrSendNwkKey( device );
  1798. }
  1799. }
  1800. else
  1801. {
  1802. // not allowed, remove the device
  1803. ZDSecMgrDeviceRemove( device );
  1804. }
  1805. return status;
  1806. }
  1807. /******************************************************************************
  1808. * @fn ZDSecMgrDeviceJoinDirect
  1809. *
  1810. * @brief Try to join this device as a direct child.
  1811. *
  1812. * @param device - [in] ZDSecMgrDevice_t, device info
  1813. *
  1814. * @return ZStatus_t
  1815. */
  1816. ZStatus_t ZDSecMgrDeviceJoinDirect( ZDSecMgrDevice_t* device )
  1817. {
  1818. ZStatus_t status;
  1819. status = ZDSecMgrDeviceJoin( device );
  1820. if ( status == ZSuccess )
  1821. {
  1822. // set association status to authenticated
  1823. ZDSecMgrAssocDeviceAuth( AssocGetWithShort( device->nwkAddr ) );
  1824. }
  1825. return status;
  1826. }
  1827. /******************************************************************************
  1828. * @fn ZDSecMgrDeviceJoinFwd
  1829. *
  1830. * @brief Forward join to Trust Center.
  1831. *
  1832. * @param device - [in] ZDSecMgrDevice_t, device info
  1833. *
  1834. * @return ZStatus_t
  1835. */
  1836. ZStatus_t ZDSecMgrDeviceJoinFwd( ZDSecMgrDevice_t* device )
  1837. {
  1838. ZStatus_t status;
  1839. APSME_UpdateDeviceReq_t req;
  1840. // forward any joining device to the Trust Center -- the Trust Center will
  1841. // decide if the device is allowed to join
  1842. status = ZSuccess;
  1843. // forward authorization to the Trust Center
  1844. req.dstAddr = APSME_TRUSTCENTER_NWKADDR;
  1845. req.devAddr = device->nwkAddr;
  1846. req.devExtAddr = device->extAddr;
  1847. // set security status, option for router to reject if policy set
  1848. if ( (device->devStatus & DEV_HIGH_SEC_STATUS) )
  1849. {
  1850. if ( device->devStatus & DEV_REJOIN_STATUS )
  1851. {
  1852. if ( device->secure == TRUE )
  1853. {
  1854. req.status = APSME_UD_HIGH_SECURED_REJOIN;
  1855. }
  1856. else
  1857. {
  1858. req.status = APSME_UD_HIGH_UNSECURED_REJOIN;
  1859. }
  1860. }
  1861. else
  1862. {
  1863. req.status = APSME_UD_HIGH_UNSECURED_JOIN;
  1864. }
  1865. }
  1866. else
  1867. {
  1868. if ( device->devStatus & DEV_REJOIN_STATUS )
  1869. {
  1870. if ( device->secure == TRUE )
  1871. {
  1872. req.status = APSME_UD_STANDARD_SECURED_REJOIN;
  1873. }
  1874. else
  1875. {
  1876. req.status = APSME_UD_STANDARD_UNSECURED_REJOIN;
  1877. }
  1878. }
  1879. else
  1880. {
  1881. req.status = APSME_UD_STANDARD_UNSECURED_JOIN;
  1882. }
  1883. }
  1884. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  1885. {
  1886. req.apsSecure = TRUE;
  1887. }
  1888. else
  1889. {
  1890. req.apsSecure = FALSE;
  1891. }
  1892. // send and APSME_UPDATE_DEVICE request to the trust center
  1893. status = APSME_UpdateDeviceReq( &req );
  1894. return status;
  1895. }
  1896. /******************************************************************************
  1897. * @fn ZDSecMgrDeviceNew
  1898. *
  1899. * @brief Process a new device.
  1900. *
  1901. * @param device - [in] ZDSecMgrDevice_t, device info
  1902. *
  1903. * @return ZStatus_t
  1904. */
  1905. ZStatus_t ZDSecMgrDeviceNew( ZDSecMgrDevice_t* joiner )
  1906. {
  1907. ZStatus_t status;
  1908. if ( ( ZG_BUILD_COORDINATOR_TYPE ) && ( ZG_DEVICE_COORDINATOR_TYPE ) )
  1909. {
  1910. // try to join this device
  1911. status = ZDSecMgrDeviceJoinDirect( joiner );
  1912. }
  1913. else
  1914. {
  1915. status = ZDSecMgrDeviceJoinFwd( joiner );
  1916. }
  1917. return status;
  1918. }
  1919. /******************************************************************************
  1920. * @fn ZDSecMgrAssocDeviceAuth
  1921. *
  1922. * @brief Set associated device status to authenticated
  1923. *
  1924. * @param assoc - [in, out] associated_devices_t
  1925. *
  1926. * @return none
  1927. */
  1928. void ZDSecMgrAssocDeviceAuth( associated_devices_t* assoc )
  1929. {
  1930. if ( assoc != NULL )
  1931. {
  1932. assoc->devStatus |= DEV_SEC_AUTH_STATUS;
  1933. }
  1934. }
  1935. /******************************************************************************
  1936. * @fn ZDSecMgrAuthInitiate
  1937. *
  1938. * @brief Initiate entity authentication
  1939. *
  1940. * @param responder - [in] responder EXT address
  1941. *
  1942. * @return none
  1943. */
  1944. void ZDSecMgrAuthInitiate( uint8* responder )
  1945. {
  1946. APSME_AuthenticateReq_t req;
  1947. // make sure NWK address is available
  1948. if ( APSME_LookupNwkAddr( responder, &req.nwkAddr ) )
  1949. {
  1950. // set request fields
  1951. req.extAddr = responder;
  1952. req.action = APSME_EA_INITIATE;
  1953. req.challenge = NULL;
  1954. // start EA processing
  1955. APSME_AuthenticateReq( &req );
  1956. }
  1957. }
  1958. /******************************************************************************
  1959. * @fn ZDSecMgrAuthNwkKey
  1960. *
  1961. * @brief Handle next step in authentication process
  1962. *
  1963. * @param none
  1964. *
  1965. * @return none
  1966. */
  1967. void ZDSecMgrAuthNwkKey()
  1968. {
  1969. if ( devState == DEV_END_DEVICE_UNAUTH )
  1970. {
  1971. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  1972. {
  1973. uint8 parent[Z_EXTADDR_LEN];
  1974. // get parent's EXT address
  1975. NLME_GetCoordExtAddr( parent );
  1976. // begin entity authentication with parent
  1977. ZDSecMgrAuthInitiate( parent );
  1978. }
  1979. else
  1980. {
  1981. // inform ZDO that device has been authenticated
  1982. osal_set_event ( ZDAppTaskID, ZDO_DEVICE_AUTH );
  1983. }
  1984. }
  1985. }
  1986. /******************************************************************************
  1987. * PUBLIC FUNCTIONS
  1988. */
  1989. /******************************************************************************
  1990. * @fn ZDSecMgrInit
  1991. *
  1992. * @brief Initialize ZigBee Device Security Manager.
  1993. *
  1994. * @param state - device initialization state
  1995. *
  1996. * @return none
  1997. */
  1998. #if ( ADDRMGR_CALLBACK_ENABLED == 1 )
  1999. void ZDSecMgrAddrMgrCB( uint8 update, AddrMgrEntry_t* newEntry, AddrMgrEntry_t* oldEntry );
  2000. void ZDSecMgrAddrMgrCB( uint8 update,
  2001. AddrMgrEntry_t* newEntry,
  2002. AddrMgrEntry_t* oldEntry )
  2003. {
  2004. (void)update;
  2005. (void)newEntry;
  2006. (void)oldEntry;
  2007. }
  2008. #endif // ( ADDRMGR_CALLBACK_ENABLED == 1 )
  2009. void ZDSecMgrInit(uint8 state)
  2010. {
  2011. if ( (ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH)
  2012. || (ZG_CHECK_SECURITY_MODE == ZG_SECURITY_SE_STANDARD) )
  2013. {
  2014. // initialize sub modules
  2015. ZDSecMgrEntryInit(state);
  2016. if ( ( ZG_BUILD_COORDINATOR_TYPE ) && ( ZG_DEVICE_COORDINATOR_TYPE ) )
  2017. {
  2018. ZDSecMgrCtrlInit();
  2019. }
  2020. // register with Address Manager
  2021. #if ( ADDRMGR_CALLBACK_ENABLED == 1 )
  2022. AddrMgrRegister( ADDRMGR_REG_SECURITY, ZDSecMgrAddrMgrCB );
  2023. #endif
  2024. }
  2025. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  2026. {
  2027. // configure SKA slot data
  2028. APSME_SKA_SlotInit( ZDSECMGR_SKA_SLOT_MAX );
  2029. }
  2030. if ( ZG_SECURE_ENABLED )
  2031. {
  2032. if ( ( ZG_BUILD_COORDINATOR_TYPE ) && ( ZG_DEVICE_COORDINATOR_TYPE ) )
  2033. {
  2034. // setup joining permissions
  2035. ZDSecMgrPermitJoiningEnabled = TRUE;
  2036. ZDSecMgrPermitJoiningTimed = FALSE;
  2037. }
  2038. }
  2039. // configure security based on security mode and type of device
  2040. ZDSecMgrConfig();
  2041. }
  2042. /******************************************************************************
  2043. * @fn ZDSecMgrConfig
  2044. *
  2045. * @brief Configure ZigBee Device Security Manager.
  2046. *
  2047. * @param none
  2048. *
  2049. * @return none
  2050. */
  2051. void ZDSecMgrConfig( void )
  2052. {
  2053. if ( ZG_SECURE_ENABLED )
  2054. {
  2055. SSP_Init();
  2056. if ( (ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH)
  2057. || (ZG_CHECK_SECURITY_MODE == ZG_SECURITY_SE_STANDARD) )
  2058. {
  2059. if ( ( ZG_BUILD_COORDINATOR_TYPE ) && ( ZG_DEVICE_COORDINATOR_TYPE ) )
  2060. {
  2061. // COMMERCIAL MODE - COORDINATOR DEVICE
  2062. APSME_SecurityCM_CD();
  2063. }
  2064. else if ( ZSTACK_ROUTER_BUILD )
  2065. {
  2066. // COMMERCIAL MODE - ROUTER DEVICE
  2067. APSME_SecurityCM_RD();
  2068. }
  2069. else
  2070. {
  2071. // COMMERCIAL MODE - END DEVICE
  2072. APSME_SecurityCM_ED();
  2073. }
  2074. }
  2075. else // ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_RESIDENTIAL )
  2076. {
  2077. if ( ( ZG_BUILD_COORDINATOR_TYPE ) && ( ZG_DEVICE_COORDINATOR_TYPE ) )
  2078. {
  2079. // RESIDENTIAL MODE - COORDINATOR DEVICE
  2080. APSME_SecurityRM_CD();
  2081. }
  2082. else if ( ZSTACK_ROUTER_BUILD )
  2083. {
  2084. // RESIDENTIAL MODE - ROUTER DEVICE
  2085. APSME_SecurityRM_RD();
  2086. }
  2087. else
  2088. {
  2089. // RESIDENTIAL MODE - END DEVICE
  2090. APSME_SecurityRM_ED();
  2091. }
  2092. }
  2093. }
  2094. else
  2095. {
  2096. // NO SECURITY
  2097. APSME_SecurityNM();
  2098. }
  2099. }
  2100. /******************************************************************************
  2101. * @fn ZDSecMgrPermitJoining
  2102. *
  2103. * @brief Process request to change joining permissions.
  2104. *
  2105. * @param duration - [in] timed duration for join in seconds
  2106. * - 0x00 not allowed
  2107. * - 0xFF allowed without timeout
  2108. *
  2109. * @return uint8 - success(TRUE:FALSE)
  2110. */
  2111. uint8 ZDSecMgrPermitJoining( uint8 duration )
  2112. {
  2113. uint8 accept;
  2114. ZDSecMgrPermitJoiningTimed = FALSE;
  2115. if ( duration > 0 )
  2116. {
  2117. ZDSecMgrPermitJoiningEnabled = TRUE;
  2118. if ( duration != 0xFF )
  2119. {
  2120. ZDSecMgrPermitJoiningTimed = TRUE;
  2121. }
  2122. }
  2123. else
  2124. {
  2125. ZDSecMgrPermitJoiningEnabled = FALSE;
  2126. }
  2127. accept = TRUE;
  2128. return accept;
  2129. }
  2130. /******************************************************************************
  2131. * @fn ZDSecMgrPermitJoiningTimeout
  2132. *
  2133. * @brief Process permit joining timeout
  2134. *
  2135. * @param none
  2136. *
  2137. * @return none
  2138. */
  2139. void ZDSecMgrPermitJoiningTimeout( void )
  2140. {
  2141. if ( ZDSecMgrPermitJoiningTimed == TRUE )
  2142. {
  2143. ZDSecMgrPermitJoiningEnabled = FALSE;
  2144. ZDSecMgrPermitJoiningTimed = FALSE;
  2145. }
  2146. }
  2147. /******************************************************************************
  2148. * @fn ZDSecMgrNewDeviceEvent
  2149. *
  2150. * @brief Process a the new device event, if found reset new device
  2151. * event/timer.
  2152. *
  2153. * @param none
  2154. *
  2155. * @return uint8 - found(TRUE:FALSE)
  2156. */
  2157. uint8 ZDSecMgrNewDeviceEvent( void )
  2158. {
  2159. uint8 found;
  2160. ZDSecMgrDevice_t device;
  2161. AddrMgrEntry_t addrEntry;
  2162. associated_devices_t* assoc;
  2163. ZStatus_t status;
  2164. // initialize return results
  2165. found = FALSE;
  2166. // look for device in the security init state
  2167. assoc = AssocMatchDeviceStatus( DEV_SEC_INIT_STATUS );
  2168. if ( assoc != NULL )
  2169. {
  2170. // device found
  2171. found = TRUE;
  2172. // check for preconfigured security
  2173. if ( zgPreConfigKeys == TRUE )
  2174. {
  2175. // set association status to authenticated
  2176. ZDSecMgrAssocDeviceAuth( assoc );
  2177. }
  2178. // set up device info
  2179. addrEntry.user = ADDRMGR_USER_DEFAULT;
  2180. addrEntry.index = assoc->addrIdx;
  2181. AddrMgrEntryGet( &addrEntry );
  2182. device.nwkAddr = assoc->shortAddr;
  2183. device.extAddr = addrEntry.extAddr;
  2184. device.parentAddr = NLME_GetShortAddr();
  2185. device.secure = FALSE;
  2186. device.devStatus = assoc->devStatus;
  2187. // process new device
  2188. status = ZDSecMgrDeviceNew( &device );
  2189. if ( status == ZSuccess )
  2190. {
  2191. assoc->devStatus &= ~DEV_SEC_INIT_STATUS;
  2192. }
  2193. else if ( status == ZNwkUnknownDevice )
  2194. {
  2195. AssocRemove( addrEntry.extAddr );
  2196. }
  2197. }
  2198. return found;
  2199. }
  2200. /******************************************************************************
  2201. * @fn ZDSecMgrEvent
  2202. *
  2203. * @brief Handle ZDO Security Manager event/timer(ZDO_SECMGR_EVENT).
  2204. *
  2205. * @param none
  2206. *
  2207. * @return none
  2208. */
  2209. void ZDSecMgrEvent( void )
  2210. {
  2211. uint8 action;
  2212. uint8 restart;
  2213. uint16 index;
  2214. AddrMgrEntry_t entry;
  2215. ZDSecMgrDevice_t device;
  2216. // verify data is available
  2217. if ( ZDSecMgrCtrlData != NULL )
  2218. {
  2219. action = FALSE;
  2220. restart = FALSE;
  2221. // update all the counters
  2222. for ( index = 0; index < ZDSECMGR_ENTRY_MAX; index++ )
  2223. {
  2224. if ( ZDSecMgrCtrlData[index].state != ZDSECMGR_CTRL_NONE )
  2225. {
  2226. if ( ZDSecMgrCtrlData[index].cntr != 0 )
  2227. {
  2228. ZDSecMgrCtrlData[index].cntr--;
  2229. }
  2230. if ( ( action == FALSE ) && ( ZDSecMgrCtrlData[index].cntr == 0 ) )
  2231. {
  2232. action = TRUE;
  2233. // update from control data
  2234. device.parentAddr = ZDSecMgrCtrlData[index].parentAddr;
  2235. device.secure = ZDSecMgrCtrlData[index].secure;
  2236. device.ctrl = &ZDSecMgrCtrlData[index];
  2237. // set the user and address index
  2238. entry.user = ADDRMGR_USER_SECURITY;
  2239. entry.index = ZDSecMgrCtrlData[index].entry->ami;
  2240. // get the address data
  2241. AddrMgrEntryGet( &entry );
  2242. // set device address data
  2243. device.nwkAddr = entry.nwkAddr;
  2244. device.extAddr = entry.extAddr;
  2245. // update from entry data
  2246. ZDSecMgrDeviceCtrlHandler( &device );
  2247. }
  2248. else
  2249. {
  2250. restart = TRUE;
  2251. }
  2252. }
  2253. }
  2254. // check for timer restart
  2255. if ( restart == TRUE )
  2256. {
  2257. osal_start_timerEx(ZDAppTaskID, ZDO_SECMGR_EVENT, 100 );
  2258. }
  2259. }
  2260. }
  2261. /******************************************************************************
  2262. * @fn ZDSecMgrEstablishKeyCfm
  2263. *
  2264. * @brief Process the ZDO_EstablishKeyCfm_t message.
  2265. *
  2266. * @param cfm - [in] ZDO_EstablishKeyCfm_t confirmation
  2267. *
  2268. * @return none
  2269. */
  2270. void ZDSecMgrEstablishKeyCfm( ZDO_EstablishKeyCfm_t* cfm )
  2271. {
  2272. // send the NWK key
  2273. if ( ( ZG_BUILD_COORDINATOR_TYPE ) && ( ZG_DEVICE_COORDINATOR_TYPE ) )
  2274. {
  2275. // update control for specified EXT address
  2276. ZDSecMgrDeviceCtrlUpdate( cfm->partExtAddr, ZDSECMGR_CTRL_SKKE_DONE );
  2277. }
  2278. else
  2279. {
  2280. // this should be done when receiving the NWK key
  2281. // if devState ==
  2282. //if ( devState == DEV_END_DEVICE_UNAUTH )
  2283. //osal_set_event( ZDAppTaskID, ZDO_DEVICE_AUTH );
  2284. // if not in joining state -- this should trigger an event for an
  2285. // end point that requested SKKE
  2286. // if ( devState == DEV_END_DEVICE )
  2287. // devState == DEV_ROUTER;
  2288. }
  2289. }
  2290. /******************************************************************************
  2291. * @fn ZDSecMgrTCExtAddrCheck
  2292. *
  2293. * @brief Verifies if received ext. address matches TC ext. address.
  2294. *
  2295. * @param extAddr - Extended address to be verified.
  2296. *
  2297. * @return TRUE - extended address matches
  2298. * FALSE - otherwise
  2299. */
  2300. uint8 ZDSecMgrTCExtAddrCheck( uint8* extAddr )
  2301. {
  2302. uint8 match;
  2303. uint8 lookup[Z_EXTADDR_LEN];
  2304. match = FALSE;
  2305. if ( AddrMgrExtAddrLookup( APSME_TRUSTCENTER_NWKADDR, lookup ) )
  2306. {
  2307. match = AddrMgrExtAddrEqual( lookup, extAddr );
  2308. }
  2309. return match;
  2310. }
  2311. /******************************************************************************
  2312. * @fn ZDSecMgrTCDataLoad
  2313. *
  2314. * @brief Stores the address of TC into address manager and stores the
  2315. * preconfigured ZDSecMgrTCMasterKey to NV if zgPreConfigKeys
  2316. * is set to TRUE.
  2317. *
  2318. * @param extAddr - Extended address to be verified.
  2319. *
  2320. * @return none
  2321. */
  2322. void ZDSecMgrTCDataLoad( uint8* extAddr )
  2323. {
  2324. uint16 ami;
  2325. uint16 keyNvId;
  2326. uint8 masterKey[SEC_KEY_LEN];
  2327. AddrMgrEntry_t entry;
  2328. // lookup using TC short address
  2329. entry.user = ADDRMGR_USER_DEFAULT;
  2330. entry.nwkAddr = zgTrustCenterAddr;
  2331. // Verify if TC address has been added to Address Manager
  2332. if ( AddrMgrEntryLookupNwk( &entry ) != TRUE )
  2333. {
  2334. if ( ZDSecMgrAddrStore( zgTrustCenterAddr, extAddr, &ami ) == ZSuccess )
  2335. {
  2336. // if preconfigured load key
  2337. if ( zgPreConfigKeys == TRUE )
  2338. {
  2339. if ( ZDSecMgrMasterKeyLookup( ami, &keyNvId ) != ZSuccess )
  2340. {
  2341. // temporary copy
  2342. osal_memcpy( masterKey, ZDSecMgrTCMasterKey, SEC_KEY_LEN);
  2343. ZDSecMgrMasterKeyStore( ami, masterKey );
  2344. // remove copy of key in RAM
  2345. osal_memset( masterKey, 0x00, SEC_KEY_LEN );
  2346. }
  2347. }
  2348. }
  2349. }
  2350. }
  2351. /******************************************************************************
  2352. * @fn ZDSecMgrEstablishKeyInd
  2353. *
  2354. * @brief Process the ZDO_EstablishKeyInd_t message.
  2355. *
  2356. * @param ind - [in] ZDO_EstablishKeyInd_t indication
  2357. *
  2358. * @return none
  2359. */
  2360. void ZDSecMgrEstablishKeyInd( ZDO_EstablishKeyInd_t* ind )
  2361. {
  2362. ZDSecMgrDevice_t device;
  2363. APSME_EstablishKeyRsp_t rsp;
  2364. // load Trust Center data if needed
  2365. ZDSecMgrTCDataLoad( ind->initExtAddr );
  2366. if ( ZDSecMgrTCExtAddrCheck( ind->initExtAddr ) )
  2367. {
  2368. //IF (ind->srcAddr == APSME_TRUSTCENTER_NWKADDR)
  2369. //OR
  2370. //!ZDSecMgrTCAuthenticated
  2371. //devtag.0604.critical
  2372. //how is the parentAddr used here
  2373. // initial SKKE from Trust Center via parent
  2374. device.nwkAddr = APSME_TRUSTCENTER_NWKADDR;
  2375. device.parentAddr = ind->srcAddr;
  2376. }
  2377. else
  2378. {
  2379. // Trust Center direct or E2E SKKE
  2380. device.nwkAddr = ind->srcAddr;
  2381. device.parentAddr = INVALID_NODE_ADDR;
  2382. }
  2383. device.extAddr = ind->initExtAddr;
  2384. //devtag.pro.security.0724.todo - verify usage
  2385. device.secure = ind->nwkSecure;
  2386. // validate device for SKKE
  2387. if ( ZDSecMgrDeviceValidateSKKE( &device ) == ZSuccess )
  2388. {
  2389. rsp.accept = TRUE;
  2390. }
  2391. else
  2392. {
  2393. rsp.accept = FALSE;
  2394. }
  2395. rsp.dstAddr = ind->srcAddr;
  2396. rsp.initExtAddr = &ind->initExtAddr[0];
  2397. //devtag.0604.todo - remove obsolete
  2398. rsp.apsSecure = ind->apsSecure;
  2399. rsp.nwkSecure = ind->nwkSecure;
  2400. APSME_EstablishKeyRsp( &rsp );
  2401. }
  2402. //devtag.pro.security
  2403. #if 0
  2404. void ZDSecMgrEstablishKeyInd( ZDO_EstablishKeyInd_t* ind )
  2405. {
  2406. ZDSecMgrDevice_t device;
  2407. APSME_EstablishKeyRsp_t rsp;
  2408. device.extAddr = ind->initExtAddr;
  2409. device.secure = ind->secure;
  2410. if ( ind->secure == FALSE )
  2411. {
  2412. // SKKE from Trust Center is not secured between child and parent
  2413. device.nwkAddr = APSME_TRUSTCENTER_NWKADDR;
  2414. device.parentAddr = ind->srcAddr;
  2415. }
  2416. else
  2417. {
  2418. // SKKE from initiator should be secured
  2419. device.nwkAddr = ind->srcAddr;
  2420. device.parentAddr = INVALID_NODE_ADDR;
  2421. }
  2422. rsp.dstAddr = ind->srcAddr;
  2423. rsp.initExtAddr = &ind->initExtAddr[0];
  2424. rsp.secure = ind->secure;
  2425. // validate device for SKKE
  2426. if ( ZDSecMgrDeviceValidateSKKE( &device ) == ZSuccess )
  2427. {
  2428. rsp.accept = TRUE;
  2429. }
  2430. else
  2431. {
  2432. rsp.accept = FALSE;
  2433. }
  2434. APSME_EstablishKeyRsp( &rsp );
  2435. }
  2436. #endif
  2437. /******************************************************************************
  2438. * @fn ZDSecMgrTransportKeyInd
  2439. *
  2440. * @brief Process the ZDO_TransportKeyInd_t message.
  2441. *
  2442. * @param ind - [in] ZDO_TransportKeyInd_t indication
  2443. *
  2444. * @return none
  2445. */
  2446. void ZDSecMgrTransportKeyInd( ZDO_TransportKeyInd_t* ind )
  2447. {
  2448. uint8 index;
  2449. uint8 zgPreConfigKey[SEC_KEY_LEN];
  2450. // load Trust Center data if needed
  2451. ZDSecMgrTCDataLoad( ind->srcExtAddr );
  2452. if ( ind->keyType == KEY_TYPE_TC_MASTER )
  2453. {
  2454. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  2455. //ZDSecMgrTCMasterKey( ind );
  2456. {
  2457. if ( zgPreConfigKeys != TRUE )
  2458. {
  2459. // devtag.pro.security.todo - check if Trust Center address is configured and correct
  2460. ZDSecMgrMasterKeyLoad( ind->srcExtAddr, ind->key );
  2461. }
  2462. else
  2463. {
  2464. // error condition - reject key
  2465. }
  2466. }
  2467. }
  2468. else if ( ( ind->keyType == KEY_TYPE_NWK ) ||
  2469. ( ind->keyType == 6 ) ||
  2470. ( ind->keyType == KEY_TYPE_NWK_HIGH ) )
  2471. {
  2472. // check for dummy NWK key (all zeros)
  2473. for ( index = 0;
  2474. ( (index < SEC_KEY_LEN) && (ind->key[index] == 0) );
  2475. index++ );
  2476. if ( index == SEC_KEY_LEN )
  2477. {
  2478. // load preconfigured key - once!!
  2479. if ( !_NIB.nwkKeyLoaded )
  2480. {
  2481. ZDSecMgrReadKeyFromNv(ZCD_NV_PRECFGKEY, zgPreConfigKey);
  2482. SSP_UpdateNwkKey( zgPreConfigKey, 0 );
  2483. SSP_SwitchNwkKey( 0 );
  2484. // clear local copy of key
  2485. osal_memset(zgPreConfigKey, 0x00, SEC_KEY_LEN);
  2486. }
  2487. }
  2488. else
  2489. {
  2490. SSP_UpdateNwkKey( ind->key, ind->keySeqNum );
  2491. if ( !_NIB.nwkKeyLoaded )
  2492. {
  2493. SSP_SwitchNwkKey( ind->keySeqNum );
  2494. }
  2495. }
  2496. // handle next step in authentication process
  2497. ZDSecMgrAuthNwkKey();
  2498. }
  2499. else if ( ind->keyType == KEY_TYPE_TC_LINK )
  2500. {
  2501. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  2502. {
  2503. // This should not happen because TCLK should not be Tx
  2504. }
  2505. }
  2506. else if ( ind->keyType == KEY_TYPE_APP_MASTER )
  2507. {
  2508. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  2509. {
  2510. uint16 ami;
  2511. AddrMgrEntry_t entry;
  2512. ZDSecMgrEntry_t* entryZD;
  2513. ZDSecMgrExtAddrLookup( ind->srcExtAddr, &ami );
  2514. if ( ind->initiator == TRUE )
  2515. {
  2516. // get the ami data
  2517. entry.user = ADDRMGR_USER_SECURITY;
  2518. entry.index = ami;
  2519. AddrMgrEntryGet( &entry );
  2520. if ( entry.nwkAddr != INVALID_NODE_ADDR )
  2521. {
  2522. APSME_EstablishKeyReq_t req;
  2523. ZDSecMgrMasterKeyLoad( ind->srcExtAddr, ind->key );
  2524. ZDSecMgrEntryLookupAMI( ami, &entryZD );
  2525. if ( entryZD == NULL )
  2526. {
  2527. // get new entry
  2528. if ( ZDSecMgrEntryNew( &entryZD ) == ZSuccess )
  2529. {
  2530. // finish setting up entry
  2531. entryZD->ami = ami;
  2532. }
  2533. }
  2534. req.respExtAddr = ind->srcExtAddr;
  2535. req.method = APSME_SKKE_METHOD;
  2536. req.dstAddr = entry.nwkAddr;
  2537. req.apsSecure = FALSE;
  2538. req.nwkSecure = TRUE;
  2539. APSME_EstablishKeyReq( &req );
  2540. }
  2541. }
  2542. else
  2543. {
  2544. if ( ami == INVALID_NODE_ADDR )
  2545. {
  2546. // store new EXT address
  2547. ZDSecMgrAddrStore( INVALID_NODE_ADDR, ind->srcExtAddr, &ami );
  2548. }
  2549. ZDSecMgrMasterKeyLoad( ind->srcExtAddr, ind->key );
  2550. }
  2551. }
  2552. }
  2553. else if ( ind->keyType == KEY_TYPE_APP_LINK )
  2554. {
  2555. if ( ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH ) ||
  2556. ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_SE_STANDARD ) )
  2557. {
  2558. uint16 ami;
  2559. ZDSecMgrEntry_t* entry;
  2560. // get the address index
  2561. if ( ZDSecMgrExtAddrLookup( ind->srcExtAddr, &ami ) != ZSuccess )
  2562. {
  2563. // store new EXT address
  2564. ZDSecMgrAddrStore( INVALID_NODE_ADDR, ind->srcExtAddr, &ami );
  2565. ZDP_NwkAddrReq( ind->srcExtAddr, ZDP_ADDR_REQTYPE_SINGLE, 0, 0 );
  2566. }
  2567. ZDSecMgrEntryLookupAMI( ami, &entry );
  2568. if ( entry == NULL )
  2569. {
  2570. // get new entry
  2571. if ( ZDSecMgrEntryNew( &entry ) == ZSuccess )
  2572. {
  2573. // finish setting up entry
  2574. entry->ami = ami;
  2575. }
  2576. }
  2577. ZDSecMgrLinkKeySet( ind->srcExtAddr, ind->key );
  2578. #if defined NV_RESTORE
  2579. ZDSecMgrWriteNV(); // Write the control record for the new established link key to NV.
  2580. #endif
  2581. }
  2582. }
  2583. }
  2584. /******************************************************************************
  2585. * @fn ZDSecMgrUpdateDeviceInd
  2586. *
  2587. * @brief Process the ZDO_UpdateDeviceInd_t message.
  2588. *
  2589. * @param ind - [in] ZDO_UpdateDeviceInd_t indication
  2590. *
  2591. * @return none
  2592. */
  2593. void ZDSecMgrUpdateDeviceInd( ZDO_UpdateDeviceInd_t* ind )
  2594. {
  2595. ZDSecMgrDevice_t device;
  2596. device.nwkAddr = ind->devAddr;
  2597. device.extAddr = ind->devExtAddr;
  2598. device.parentAddr = ind->srcAddr;
  2599. //if ( ( ind->status == APSME_UD_SECURED_JOIN ) ||
  2600. // ( ind->status == APSME_UD_UNSECURED_JOIN ) )
  2601. //{
  2602. // if ( ind->status == APSME_UD_SECURED_JOIN )
  2603. // {
  2604. // device.secure = TRUE;
  2605. // }
  2606. // else
  2607. // {
  2608. // device.secure = FALSE;
  2609. // }
  2610. // try to join this device
  2611. ZDSecMgrDeviceJoin( &device );
  2612. //}
  2613. }
  2614. /******************************************************************************
  2615. * @fn ZDSecMgrRemoveDeviceInd
  2616. *
  2617. * @brief Process the ZDO_RemoveDeviceInd_t message.
  2618. *
  2619. * @param ind - [in] ZDO_RemoveDeviceInd_t indication
  2620. *
  2621. * @return none
  2622. */
  2623. void ZDSecMgrRemoveDeviceInd( ZDO_RemoveDeviceInd_t* ind )
  2624. {
  2625. ZDSecMgrDevice_t device;
  2626. // only accept from Trust Center
  2627. if ( ind->srcAddr == APSME_TRUSTCENTER_NWKADDR )
  2628. {
  2629. // look up NWK address
  2630. if ( APSME_LookupNwkAddr( ind->childExtAddr, &device.nwkAddr ) == TRUE )
  2631. {
  2632. device.parentAddr = NLME_GetShortAddr();
  2633. device.extAddr = ind->childExtAddr;
  2634. // remove device
  2635. ZDSecMgrDeviceRemove( &device );
  2636. }
  2637. }
  2638. }
  2639. /******************************************************************************
  2640. * @fn ZDSecMgrRequestKeyInd
  2641. *
  2642. * @brief Process the ZDO_RequestKeyInd_t message.
  2643. *
  2644. * @param ind - [in] ZDO_RequestKeyInd_t indication
  2645. *
  2646. * @return none
  2647. */
  2648. void ZDSecMgrRequestKeyInd( ZDO_RequestKeyInd_t* ind )
  2649. {
  2650. if ( ind->keyType == KEY_TYPE_NWK )
  2651. {
  2652. }
  2653. else if ( ind->keyType == KEY_TYPE_APP_MASTER )
  2654. {
  2655. ZDSecMgrAppKeyReq( ind );
  2656. }
  2657. else if ( ind->keyType == KEY_TYPE_TC_LINK )
  2658. {
  2659. }
  2660. //else ignore
  2661. }
  2662. /******************************************************************************
  2663. * @fn ZDSecMgrSwitchKeyInd
  2664. *
  2665. * @brief Process the ZDO_SwitchKeyInd_t message.
  2666. *
  2667. * @param ind - [in] ZDO_SwitchKeyInd_t indication
  2668. *
  2669. * @return none
  2670. */
  2671. void ZDSecMgrSwitchKeyInd( ZDO_SwitchKeyInd_t* ind )
  2672. {
  2673. SSP_SwitchNwkKey( ind->keySeqNum );
  2674. // Save if nv
  2675. ZDApp_NVUpdate();
  2676. }
  2677. /******************************************************************************
  2678. * @fn ZDSecMgrAuthenticateInd
  2679. *
  2680. * @brief Process the ZDO_AuthenticateInd_t message.
  2681. *
  2682. * @param ind - [in] ZDO_AuthenticateInd_t indication
  2683. *
  2684. * @return none
  2685. */
  2686. void ZDSecMgrAuthenticateInd( ZDO_AuthenticateInd_t* ind )
  2687. {
  2688. APSME_AuthenticateReq_t req;
  2689. AddrMgrEntry_t entry;
  2690. // update the address manager
  2691. //---------------------------------------------------------------------------
  2692. // note:
  2693. // required for EA processing, but ultimately EA logic could also use the
  2694. // neighbor table to look up addresses -- also(IF using EA) the neighbor
  2695. // table is supposed to have authentication states for neighbors
  2696. //---------------------------------------------------------------------------
  2697. entry.user = ADDRMGR_USER_SECURITY;
  2698. entry.nwkAddr = ind->aps.initNwkAddr;
  2699. AddrMgrExtAddrSet( entry.extAddr, ind->aps.initExtAddr );
  2700. if ( AddrMgrEntryUpdate( &entry ) == TRUE )
  2701. {
  2702. // set request fields
  2703. req.nwkAddr = ind->aps.initNwkAddr;
  2704. req.extAddr = ind->aps.initExtAddr;
  2705. req.action = APSME_EA_ACCEPT;
  2706. req.challenge = ind->aps.challenge;
  2707. // start EA processing
  2708. APSME_AuthenticateReq( &req );
  2709. }
  2710. }
  2711. /******************************************************************************
  2712. * @fn ZDSecMgrAuthenticateCfm
  2713. *
  2714. * @brief Process the ZDO_AuthenticateCfm_t message.
  2715. *
  2716. * @param cfm - [in] ZDO_AuthenticateCfm_t confirmation
  2717. *
  2718. * @return none
  2719. */
  2720. void ZDSecMgrAuthenticateCfm( ZDO_AuthenticateCfm_t* cfm )
  2721. {
  2722. if ( cfm->aps.status == ZSuccess )
  2723. {
  2724. if ( ( cfm->aps.initiator == TRUE ) && ( devState == DEV_END_DEVICE_UNAUTH ) )
  2725. {
  2726. // inform ZDO that device has been authenticated
  2727. osal_set_event ( ZDAppTaskID, ZDO_DEVICE_AUTH );
  2728. }
  2729. }
  2730. }
  2731. #if ( ZG_BUILD_COORDINATOR_TYPE )
  2732. /******************************************************************************
  2733. * @fn ZDSecMgrUpdateNwkKey
  2734. *
  2735. * @brief Load a new NWK key and trigger a network wide update.
  2736. *
  2737. * @param key - [in] new NWK key
  2738. * @param keySeqNum - [in] new NWK key sequence number
  2739. *
  2740. * @return ZStatus_t
  2741. */
  2742. ZStatus_t ZDSecMgrUpdateNwkKey( uint8* key, uint8 keySeqNum, uint16 dstAddr )
  2743. {
  2744. ZStatus_t status;
  2745. APSME_TransportKeyReq_t req;
  2746. // initialize common elements of local variables
  2747. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  2748. {
  2749. req.keyType = KEY_TYPE_NWK_HIGH;
  2750. }
  2751. else
  2752. {
  2753. req.keyType = KEY_TYPE_NWK;
  2754. }
  2755. req.dstAddr = dstAddr;
  2756. req.keySeqNum = keySeqNum;
  2757. req.key = key;
  2758. req.extAddr = NULL;
  2759. req.nwkSecure = TRUE;
  2760. req.apsSecure = TRUE;
  2761. req.tunnel = NULL;
  2762. if (( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH ) ||
  2763. ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_SE_STANDARD ))
  2764. {
  2765. ZDSecMgrEntry_t* entry;
  2766. uint16 index;
  2767. AddrMgrEntry_t addrEntry;
  2768. addrEntry.user = ADDRMGR_USER_SECURITY;
  2769. status = ZFailure;
  2770. // verify data is available
  2771. if ( ZDSecMgrEntries != NULL )
  2772. {
  2773. // find available entry
  2774. for ( index = 0; index < ZDSECMGR_ENTRY_MAX ; index++ )
  2775. {
  2776. if ( ZDSecMgrEntries[index].ami != INVALID_NODE_ADDR )
  2777. {
  2778. // return successful result
  2779. entry = &ZDSecMgrEntries[index];
  2780. // get NWK address
  2781. addrEntry.index = entry->ami;
  2782. if ( AddrMgrEntryGet( &addrEntry ) == TRUE )
  2783. {
  2784. req.dstAddr = addrEntry.nwkAddr;
  2785. req.extAddr = addrEntry.extAddr;
  2786. status = APSME_TransportKeyReq( &req );
  2787. }
  2788. }
  2789. }
  2790. }
  2791. }
  2792. else // ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_RESIDENTIAL )
  2793. {
  2794. status = APSME_TransportKeyReq( &req );
  2795. }
  2796. SSP_UpdateNwkKey( key, keySeqNum );
  2797. // Save if nv
  2798. ZDApp_NVUpdate();
  2799. return status;
  2800. }
  2801. #endif // ( ZG_BUILD_COORDINATOR_TYPE )
  2802. #if ( ZG_BUILD_COORDINATOR_TYPE )
  2803. /******************************************************************************
  2804. * @fn ZDSecMgrSwitchNwkKey
  2805. *
  2806. * @brief Causes the NWK key to switch via a network wide command.
  2807. *
  2808. * @param keySeqNum - [in] new NWK key sequence number
  2809. *
  2810. * @return ZStatus_t
  2811. */
  2812. ZStatus_t ZDSecMgrSwitchNwkKey( uint8 keySeqNum, uint16 dstAddr )
  2813. {
  2814. ZStatus_t status;
  2815. APSME_SwitchKeyReq_t req;
  2816. // initialize common elements of local variables
  2817. req.dstAddr = dstAddr;
  2818. req.keySeqNum = keySeqNum;
  2819. if (( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH ) ||
  2820. ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_SE_STANDARD ))
  2821. {
  2822. ZDSecMgrEntry_t* entry;
  2823. uint16 index;
  2824. AddrMgrEntry_t addrEntry;
  2825. addrEntry.user = ADDRMGR_USER_SECURITY;
  2826. status = ZFailure;
  2827. // verify data is available
  2828. if ( ZDSecMgrEntries != NULL )
  2829. {
  2830. // find available entry
  2831. for ( index = 0; index < ZDSECMGR_ENTRY_MAX ; index++ )
  2832. {
  2833. if ( ZDSecMgrEntries[index].ami != INVALID_NODE_ADDR )
  2834. {
  2835. // return successful result
  2836. entry = &ZDSecMgrEntries[index];
  2837. // get NWK address
  2838. addrEntry.index = entry->ami;
  2839. if ( AddrMgrEntryGet( &addrEntry ) == TRUE )
  2840. {
  2841. req.dstAddr = addrEntry.nwkAddr;
  2842. status = APSME_SwitchKeyReq( &req );
  2843. }
  2844. }
  2845. }
  2846. }
  2847. }
  2848. else // ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_RESIDENTIAL )
  2849. {
  2850. status = APSME_SwitchKeyReq( &req );
  2851. }
  2852. SSP_SwitchNwkKey( keySeqNum );
  2853. // Save if nv
  2854. ZDApp_NVUpdate();
  2855. return status;
  2856. }
  2857. #endif // ( ZG_BUILD_COORDINATOR_TYPE )
  2858. /******************************************************************************
  2859. * @fn ZDSecMgrRequestAppKey
  2860. *
  2861. * @brief Request an application key with partner.
  2862. *
  2863. * @param partExtAddr - [in] partner extended address
  2864. *
  2865. * @return ZStatus_t
  2866. */
  2867. ZStatus_t ZDSecMgrRequestAppKey( uint8 *partExtAddr )
  2868. {
  2869. ZStatus_t status;
  2870. APSME_RequestKeyReq_t req;
  2871. req.dstAddr = 0;
  2872. req.keyType = KEY_TYPE_APP_MASTER;
  2873. req.partExtAddr = partExtAddr;
  2874. status = APSME_RequestKeyReq( &req );
  2875. return status;
  2876. }
  2877. #if ( ZG_BUILD_JOINING_TYPE )
  2878. /******************************************************************************
  2879. * @fn ZDSecMgrSetupPartner
  2880. *
  2881. * @brief Setup for application key partner.
  2882. *
  2883. * @param partNwkAddr - [in] partner network address
  2884. *
  2885. * @return ZStatus_t
  2886. */
  2887. ZStatus_t ZDSecMgrSetupPartner( uint16 partNwkAddr, uint8* partExtAddr )
  2888. {
  2889. AddrMgrEntry_t entry;
  2890. ZStatus_t status;
  2891. status = ZFailure;
  2892. // update the address manager
  2893. entry.user = ADDRMGR_USER_SECURITY;
  2894. entry.nwkAddr = partNwkAddr;
  2895. AddrMgrExtAddrSet( entry.extAddr, partExtAddr );
  2896. if ( AddrMgrEntryUpdate( &entry ) == TRUE )
  2897. {
  2898. status = ZSuccess;
  2899. // check for address discovery
  2900. if ( partNwkAddr == INVALID_NODE_ADDR )
  2901. {
  2902. status = ZDP_NwkAddrReq( partExtAddr, ZDP_ADDR_REQTYPE_SINGLE, 0, 0 );
  2903. }
  2904. else if ( !AddrMgrExtAddrValid( partExtAddr ) )
  2905. {
  2906. status = ZDP_IEEEAddrReq( partNwkAddr, ZDP_ADDR_REQTYPE_SINGLE, 0, 0 );
  2907. }
  2908. }
  2909. return status;
  2910. }
  2911. #endif // ( ZG_BUILD_JOINING_TYPE )
  2912. #if ( ZG_BUILD_COORDINATOR_TYPE )
  2913. /******************************************************************************
  2914. * @fn ZDSecMgrAppKeyTypeSet
  2915. *
  2916. * @brief Set application key type.
  2917. *
  2918. * @param keyType - [in] application key type (KEY_TYPE_APP_MASTER@2 or
  2919. * KEY_TYPE_APP_LINK@3
  2920. *
  2921. * @return ZStatus_t
  2922. */
  2923. ZStatus_t ZDSecMgrAppKeyTypeSet( uint8 keyType )
  2924. {
  2925. if ( keyType == KEY_TYPE_APP_LINK )
  2926. {
  2927. ZDSecMgrAppKeyType = KEY_TYPE_APP_LINK;
  2928. }
  2929. else
  2930. {
  2931. ZDSecMgrAppKeyType = KEY_TYPE_APP_MASTER;
  2932. }
  2933. return ZSuccess;
  2934. }
  2935. #endif
  2936. /******************************************************************************
  2937. * ZigBee Device Security Manager - Stub Implementations
  2938. */
  2939. /******************************************************************************
  2940. * @fn ZDSecMgrMasterKeyGet (stubs APSME_MasterKeyGet)
  2941. *
  2942. * @brief Get MASTER key for specified EXT address.
  2943. *
  2944. * @param extAddr - [in] EXT address
  2945. * @param pKeyNvId - [out] MASTER key NV ID
  2946. *
  2947. * @return ZStatus_t
  2948. */
  2949. ZStatus_t ZDSecMgrMasterKeyGet( uint8* extAddr, uint16* pKeyNvId )
  2950. {
  2951. ZStatus_t status;
  2952. uint16 ami;
  2953. // lookup entry for specified EXT address
  2954. status = ZDSecMgrExtAddrLookup( extAddr, &ami );
  2955. if ( status == ZSuccess )
  2956. {
  2957. ZDSecMgrMasterKeyLookup( ami, pKeyNvId );
  2958. }
  2959. else
  2960. {
  2961. *pKeyNvId = SEC_NO_KEY_NV_ID;
  2962. }
  2963. return status;
  2964. }
  2965. /******************************************************************************
  2966. * @fn ZDSecMgrLinkKeySet (stubs APSME_LinkKeySet)
  2967. *
  2968. * @brief Set <APSME_LinkKeyData_t> for specified NWK address.
  2969. *
  2970. * @param extAddr - [in] EXT address
  2971. * @param data - [in] APSME_LinkKeyData_t
  2972. *
  2973. * @return ZStatus_t
  2974. */
  2975. ZStatus_t ZDSecMgrLinkKeySet( uint8* extAddr, uint8* key )
  2976. {
  2977. ZStatus_t status;
  2978. ZDSecMgrEntry_t* entry;
  2979. APSME_LinkKeyData_t *pApsLinkKey = NULL;
  2980. uint16 Index;
  2981. // lookup entry index for specified EXT address
  2982. status = ZDSecMgrEntryLookupExtGetIndex( extAddr, &entry, &Index );
  2983. if ( status == ZSuccess )
  2984. {
  2985. // point to NV item
  2986. entry->keyNvId = ZCD_NV_APS_LINK_KEY_DATA_START + Index;
  2987. pApsLinkKey = (APSME_LinkKeyData_t *)osal_mem_alloc(sizeof(APSME_LinkKeyData_t));
  2988. if (pApsLinkKey != NULL)
  2989. {
  2990. // read the key form NV, keyNvId must be ZCD_NV_APS_LINK_KEY_DATA_START based
  2991. osal_nv_read( entry->keyNvId, 0,
  2992. sizeof(APSME_LinkKeyData_t), pApsLinkKey );
  2993. // set new values of the key
  2994. osal_memcpy( pApsLinkKey->key, key, SEC_KEY_LEN );
  2995. pApsLinkKey->rxFrmCntr = 0;
  2996. pApsLinkKey->txFrmCntr = 0;
  2997. osal_nv_write( entry->keyNvId, 0,
  2998. sizeof(APSME_LinkKeyData_t), pApsLinkKey );
  2999. // clear copy of key in RAM
  3000. osal_memset(pApsLinkKey, 0x00, sizeof(APSME_LinkKeyData_t));
  3001. osal_mem_free(pApsLinkKey);
  3002. // set initial values for counters in RAM
  3003. ApsLinkKeyFrmCntr[entry->keyNvId - ZCD_NV_APS_LINK_KEY_DATA_START].txFrmCntr = 0;
  3004. ApsLinkKeyFrmCntr[entry->keyNvId - ZCD_NV_APS_LINK_KEY_DATA_START].rxFrmCntr = 0;
  3005. }
  3006. }
  3007. return status;
  3008. }
  3009. /******************************************************************************
  3010. * @fn ZDSecMgrAuthenticationSet
  3011. *
  3012. * @brief Mark the specific device as authenticated or not
  3013. *
  3014. * @param extAddr - [in] EXT address
  3015. * @param option - [in] authenticated or not
  3016. *
  3017. * @return ZStatus_t
  3018. */
  3019. ZStatus_t ZDSecMgrAuthenticationSet( uint8* extAddr, ZDSecMgr_Authentication_Option option )
  3020. {
  3021. ZStatus_t status;
  3022. ZDSecMgrEntry_t* entry;
  3023. // lookup entry index for specified EXT address
  3024. status = ZDSecMgrEntryLookupExt( extAddr, &entry );
  3025. if ( status == ZSuccess )
  3026. {
  3027. entry->authenticateOption = option;
  3028. }
  3029. return status;
  3030. }
  3031. /******************************************************************************
  3032. * @fn ZDSecMgrAuthenticationCheck
  3033. *
  3034. * @brief Check if the specific device has been authenticated or not
  3035. * For non-trust center device, always return TRUE
  3036. *
  3037. * @param shortAddr - [in] short address
  3038. *
  3039. * @return TRUE @ authenticated with CBKE
  3040. * FALSE @ not authenticated
  3041. */
  3042. uint8 ZDSecMgrAuthenticationCheck( uint16 shortAddr )
  3043. {
  3044. #if defined (TC_LINKKEY_JOIN)
  3045. ZDSecMgrEntry_t* entry;
  3046. uint8 extAddr[Z_EXTADDR_LEN];
  3047. // If the local device is not the trust center, always return TRUE
  3048. if ( NLME_GetShortAddr() != zgTrustCenterAddr )
  3049. {
  3050. return TRUE;
  3051. }
  3052. // Otherwise, check the authentication option
  3053. else if ( AddrMgrExtAddrLookup( shortAddr, extAddr ) )
  3054. {
  3055. // lookup entry index for specified EXT address
  3056. if ( ZDSecMgrEntryLookupExt( extAddr, &entry ) == ZSuccess )
  3057. {
  3058. if ( entry->authenticateOption != ZDSecMgr_Not_Authenticated )
  3059. {
  3060. return TRUE;
  3061. }
  3062. else
  3063. {
  3064. return FALSE;
  3065. }
  3066. }
  3067. }
  3068. return FALSE;
  3069. #else
  3070. (void)shortAddr; // Intentionally unreferenced parameter
  3071. // For non AMI/SE Profile, perform no check and always return TRUE.
  3072. return TRUE;
  3073. #endif // TC_LINKKEY_JOIN
  3074. }
  3075. /******************************************************************************
  3076. * @fn ZDSecMgrLinkKeyNVIdGet (stubs APSME_LinkKeyNVIdGet)
  3077. *
  3078. * @brief Get Key NV ID for specified NWK address.
  3079. *
  3080. * @param extAddr - [in] EXT address
  3081. * @param keyNvId - [out] NV ID
  3082. *
  3083. * @return ZStatus_t
  3084. */
  3085. ZStatus_t ZDSecMgrLinkKeyNVIdGet(uint8* extAddr, uint16 *pKeyNvId)
  3086. {
  3087. ZStatus_t status;
  3088. ZDSecMgrEntry_t* entry;
  3089. // lookup entry index for specified NWK address
  3090. status = ZDSecMgrEntryLookupExt( extAddr, &entry );
  3091. if ( status == ZSuccess )
  3092. {
  3093. // return the index to the NV table
  3094. *pKeyNvId = entry->keyNvId;
  3095. }
  3096. else
  3097. {
  3098. *pKeyNvId = SEC_NO_KEY_NV_ID;
  3099. }
  3100. return status;
  3101. }
  3102. /******************************************************************************
  3103. * @fn ZDSecMgrIsLinkKeyValid (stubs APSME_IsLinkKeyValid)
  3104. *
  3105. * @brief Verifies if Link Key in NV has been set.
  3106. *
  3107. * @param extAddr - [in] EXT address
  3108. *
  3109. * @return TRUE - Link Key has been established
  3110. * FALSE - Link Key in NV has default value.
  3111. */
  3112. uint8 ZDSecMgrIsLinkKeyValid(uint8* extAddr)
  3113. {
  3114. APSME_LinkKeyData_t *pKeyData;
  3115. uint16 apsLinkKeyNvId;
  3116. uint8 nullKey[SEC_KEY_LEN];
  3117. uint8 status = FALSE;
  3118. // initialize default vealue to compare to
  3119. osal_memset(nullKey, 0x00, SEC_KEY_LEN);
  3120. // check for APS link NV ID
  3121. APSME_LinkKeyNVIdGet( extAddr, &apsLinkKeyNvId );
  3122. if (apsLinkKeyNvId != SEC_NO_KEY_NV_ID )
  3123. {
  3124. pKeyData = (APSME_LinkKeyData_t *)osal_mem_alloc(sizeof(APSME_LinkKeyData_t));
  3125. if (pKeyData != NULL)
  3126. {
  3127. // retrieve key from NV
  3128. if ( osal_nv_read( apsLinkKeyNvId, 0,
  3129. sizeof(APSME_LinkKeyData_t), pKeyData) == ZSUCCESS)
  3130. {
  3131. // if stored key is different than default value, then a key has been established
  3132. if (!osal_memcmp(pKeyData, nullKey, SEC_KEY_LEN))
  3133. {
  3134. status = TRUE;
  3135. }
  3136. }
  3137. // clear copy of key in RAM
  3138. osal_memset(pKeyData, 0x00, sizeof(APSME_LinkKeyData_t));
  3139. osal_mem_free(pKeyData);
  3140. }
  3141. }
  3142. return status;
  3143. }
  3144. /******************************************************************************
  3145. * @fn ZDSecMgrKeyFwdToChild (stubs APSME_KeyFwdToChild)
  3146. *
  3147. * @brief Verify and process key transportation to child.
  3148. *
  3149. * @param ind - [in] APSME_TransportKeyInd_t
  3150. *
  3151. * @return uint8 - success(TRUE:FALSE)
  3152. */
  3153. uint8 ZDSecMgrKeyFwdToChild( APSME_TransportKeyInd_t* ind )
  3154. {
  3155. // verify from Trust Center
  3156. if ( ind->srcAddr == APSME_TRUSTCENTER_NWKADDR )
  3157. {
  3158. // check for initial NWK key
  3159. if ( ( ind->keyType == KEY_TYPE_NWK ) ||
  3160. ( ind->keyType == 6 ) ||
  3161. ( ind->keyType == KEY_TYPE_NWK_HIGH ) )
  3162. {
  3163. // set association status to authenticated
  3164. ZDSecMgrAssocDeviceAuth( AssocGetWithExt( ind->dstExtAddr ) );
  3165. }
  3166. return TRUE;
  3167. }
  3168. return FALSE;
  3169. }
  3170. /******************************************************************************
  3171. * @fn ZDSecMgrAddLinkKey
  3172. *
  3173. * @brief Add the application link key to ZDSecMgr. Also mark the device
  3174. * as authenticated in the authenticateOption. Note that this function
  3175. * is hardwared to CBKE right now.
  3176. *
  3177. * @param shortAddr - short address of the partner device
  3178. * @param extAddr - extended address of the partner device
  3179. * @param key - link key
  3180. *
  3181. * @return ZStatus_t
  3182. */
  3183. ZStatus_t ZDSecMgrAddLinkKey( uint16 shortAddr, uint8 *extAddr, uint8 *key)
  3184. {
  3185. uint16 ami;
  3186. ZDSecMgrEntry_t* entry;
  3187. /* Store the device address in the addr manager */
  3188. if( ZDSecMgrAddrStore( shortAddr, extAddr, &ami ) != ZSuccess )
  3189. {
  3190. /* Adding to Addr Manager fails */
  3191. return ZFailure;
  3192. }
  3193. /* Lookup entry using specified address index */
  3194. ZDSecMgrEntryLookupAMI( ami, &entry );
  3195. // If no existing entry, create one
  3196. if ( entry == NULL )
  3197. {
  3198. if ( ZDSecMgrEntryNew( &entry ) == ZSuccess )
  3199. {
  3200. entry->ami = ami;
  3201. }
  3202. else
  3203. {
  3204. /* Security Manager full */
  3205. return ZBufferFull;
  3206. }
  3207. }
  3208. // Write the link key
  3209. APSME_LinkKeySet( extAddr, key );
  3210. #if defined (TC_LINKKEY_JOIN)
  3211. // Mark the device as authenticated.
  3212. ZDSecMgrAuthenticationSet( extAddr, ZDSecMgr_Authenticated_CBCK );
  3213. #endif
  3214. #if defined NV_RESTORE
  3215. ZDSecMgrWriteNV(); // Write the new established link key to NV.
  3216. #endif
  3217. return ZSuccess;
  3218. }
  3219. #if defined ( NV_RESTORE )
  3220. /******************************************************************************
  3221. * @fn ZDSecMgrInitNV
  3222. *
  3223. * @brief Initialize the SecMgr entry data in NV with all values set to 0
  3224. *
  3225. * @param none
  3226. *
  3227. * @return uint8 - <osal_nv_item_init> return codes
  3228. */
  3229. uint8 ZDSecMgrInitNV(void)
  3230. {
  3231. uint8 rtrn = osal_nv_item_init(ZCD_NV_APS_LINK_KEY_TABLE,
  3232. (sizeof(nvDeviceListHdr_t) + (sizeof(ZDSecMgrEntry_t) * ZDSECMGR_ENTRY_MAX)), NULL);
  3233. // If the item does not already exist, set all values to 0
  3234. if (rtrn != SUCCESS)
  3235. {
  3236. nvDeviceListHdr_t hdr;
  3237. hdr.numRecs = 0;
  3238. osal_nv_write(ZCD_NV_APS_LINK_KEY_TABLE, 0, sizeof(nvDeviceListHdr_t), &hdr);
  3239. }
  3240. return rtrn;
  3241. }
  3242. #endif // NV_RESTORE
  3243. #if defined ( NV_RESTORE )
  3244. /*********************************************************************
  3245. * @fn ZDSecMgrWriteNV()
  3246. *
  3247. * @brief Save off the APS link key list to NV
  3248. *
  3249. * @param none
  3250. *
  3251. * @return none
  3252. */
  3253. static void ZDSecMgrWriteNV( void )
  3254. {
  3255. uint16 i;
  3256. nvDeviceListHdr_t hdr;
  3257. hdr.numRecs = 0;
  3258. if (ZDSecMgrEntries != NULL)
  3259. {
  3260. for ( i = 0; i < ZDSECMGR_ENTRY_MAX; i++ )
  3261. {
  3262. // Save off the record
  3263. osal_nv_write( ZCD_NV_APS_LINK_KEY_TABLE,
  3264. (uint16)((sizeof(nvDeviceListHdr_t)) + (i * sizeof(ZDSecMgrEntry_t))),
  3265. sizeof(ZDSecMgrEntry_t), &ZDSecMgrEntries[i] );
  3266. if ( ZDSecMgrEntries[i].ami != INVALID_NODE_ADDR )
  3267. {
  3268. hdr.numRecs++;
  3269. }
  3270. }
  3271. }
  3272. // Save off the header
  3273. osal_nv_write( ZCD_NV_APS_LINK_KEY_TABLE, 0, sizeof( nvDeviceListHdr_t ), &hdr );
  3274. }
  3275. #endif // NV_RESTORE
  3276. #if defined ( NV_RESTORE )
  3277. /******************************************************************************
  3278. * @fn ZDSecMgrRestoreFromNV
  3279. *
  3280. * @brief Restore the APS Link Key entry data from NV. It does not restore
  3281. * the key data itself as they remain in NV until they are used.
  3282. * Only list data is restored.
  3283. *
  3284. * @param none
  3285. *
  3286. * @return None.
  3287. */
  3288. static void ZDSecMgrRestoreFromNV( void )
  3289. {
  3290. nvDeviceListHdr_t hdr;
  3291. APSME_LinkKeyData_t *pApsLinkKey = NULL;
  3292. if ((osal_nv_read(ZCD_NV_APS_LINK_KEY_TABLE, 0, sizeof(nvDeviceListHdr_t), &hdr) == ZSUCCESS) &&
  3293. ((hdr.numRecs > 0) && (hdr.numRecs <= ZDSECMGR_ENTRY_MAX)))
  3294. {
  3295. uint8 x;
  3296. pApsLinkKey = (APSME_LinkKeyData_t *)osal_mem_alloc(sizeof(APSME_LinkKeyData_t));
  3297. for (x = 0; x < ZDSECMGR_ENTRY_MAX; x++)
  3298. {
  3299. if ( osal_nv_read( ZCD_NV_APS_LINK_KEY_TABLE,
  3300. (uint16)(sizeof(nvDeviceListHdr_t) + (x * sizeof(ZDSecMgrEntry_t))),
  3301. sizeof(ZDSecMgrEntry_t), &ZDSecMgrEntries[x] ) == SUCCESS )
  3302. {
  3303. // update data only for valid entries
  3304. if ( ZDSecMgrEntries[x].ami != INVALID_NODE_ADDR )
  3305. {
  3306. if (pApsLinkKey != NULL)
  3307. {
  3308. // read the key form NV, keyNvId must be ZCD_NV_APS_LINK_KEY_DATA_START based
  3309. osal_nv_read( ZDSecMgrEntries[x].keyNvId, 0,
  3310. sizeof(APSME_LinkKeyData_t), pApsLinkKey );
  3311. // set new values for the counter
  3312. pApsLinkKey->txFrmCntr += ( MAX_APS_FRAMECOUNTER_CHANGES + 1 );
  3313. // restore values for counters in RAM
  3314. ApsLinkKeyFrmCntr[ZDSecMgrEntries[x].keyNvId - ZCD_NV_APS_LINK_KEY_DATA_START].txFrmCntr =
  3315. pApsLinkKey->txFrmCntr;
  3316. ApsLinkKeyFrmCntr[ZDSecMgrEntries[x].keyNvId - ZCD_NV_APS_LINK_KEY_DATA_START].rxFrmCntr =
  3317. pApsLinkKey->rxFrmCntr;
  3318. osal_nv_write( ZDSecMgrEntries[x].keyNvId, 0,
  3319. sizeof(APSME_LinkKeyData_t), pApsLinkKey );
  3320. // clear copy of key in RAM
  3321. osal_memset(pApsLinkKey, 0x00, sizeof(APSME_LinkKeyData_t));
  3322. }
  3323. }
  3324. }
  3325. }
  3326. if (pApsLinkKey != NULL)
  3327. {
  3328. osal_mem_free(pApsLinkKey);
  3329. }
  3330. }
  3331. }
  3332. #endif // NV_RESTORE
  3333. /*********************************************************************
  3334. * @fn ZDSecMgrSetDefaultNV
  3335. *
  3336. * @brief Write the defaults to NV for Entry table and for APS key data table
  3337. *
  3338. * @param none
  3339. *
  3340. * @return none
  3341. */
  3342. void ZDSecMgrSetDefaultNV( void )
  3343. {
  3344. uint16 i;
  3345. nvDeviceListHdr_t hdr;
  3346. ZDSecMgrEntry_t secMgrEntry;
  3347. APSME_LinkKeyData_t *pApsLinkKey = NULL;
  3348. // Initialize the header
  3349. hdr.numRecs = 0;
  3350. // clear the header
  3351. osal_nv_write(ZCD_NV_APS_LINK_KEY_TABLE, 0, sizeof(nvDeviceListHdr_t), &hdr);
  3352. osal_memset( &secMgrEntry, 0x00, sizeof(ZDSecMgrEntry_t) );
  3353. for ( i = 0; i < ZDSECMGR_ENTRY_MAX; i++ )
  3354. {
  3355. // Clear the record
  3356. osal_nv_write( ZCD_NV_APS_LINK_KEY_TABLE,
  3357. (uint16)((sizeof(nvDeviceListHdr_t)) + (i * sizeof(ZDSecMgrEntry_t))),
  3358. sizeof(ZDSecMgrEntry_t), &secMgrEntry );
  3359. }
  3360. pApsLinkKey = (APSME_LinkKeyData_t *)osal_mem_alloc(sizeof(APSME_LinkKeyData_t));
  3361. if (pApsLinkKey != NULL)
  3362. {
  3363. osal_memset( pApsLinkKey, 0x00, sizeof(APSME_LinkKeyData_t) );
  3364. for ( i = 0; i < ZDSECMGR_ENTRY_MAX; i++ )
  3365. {
  3366. // Clear the record
  3367. osal_nv_write( (ZCD_NV_APS_LINK_KEY_DATA_START + i), 0,
  3368. sizeof(APSME_LinkKeyData_t), pApsLinkKey);
  3369. }
  3370. osal_mem_free(pApsLinkKey);
  3371. }
  3372. }
  3373. #if defined ( NV_RESTORE )
  3374. /*********************************************************************
  3375. * @fn ZDSecMgrUpdateNV()
  3376. *
  3377. * @brief Updates one entry of the APS link key table to NV
  3378. *
  3379. * @param index - to the entry in security manager table
  3380. *
  3381. * @return none
  3382. */
  3383. static void ZDSecMgrUpdateNV( uint16 index )
  3384. {
  3385. nvDeviceListHdr_t hdr;
  3386. if (ZDSecMgrEntries != NULL)
  3387. {
  3388. // Save off the record
  3389. osal_nv_write( ZCD_NV_APS_LINK_KEY_TABLE,
  3390. (uint16)((sizeof(nvDeviceListHdr_t)) + (index * sizeof(ZDSecMgrEntry_t))),
  3391. sizeof(ZDSecMgrEntry_t), &ZDSecMgrEntries[index] );
  3392. }
  3393. if (osal_nv_read(ZCD_NV_APS_LINK_KEY_TABLE, 0, sizeof(nvDeviceListHdr_t), &hdr) == ZSUCCESS)
  3394. {
  3395. if ( ZDSecMgrEntries[index].ami == INVALID_NODE_ADDR )
  3396. {
  3397. if (hdr.numRecs > 0)
  3398. {
  3399. hdr.numRecs--;
  3400. }
  3401. }
  3402. else
  3403. {
  3404. hdr.numRecs++;
  3405. }
  3406. // Save off the header
  3407. osal_nv_write( ZCD_NV_APS_LINK_KEY_TABLE, 0, sizeof( nvDeviceListHdr_t ), &hdr );
  3408. }
  3409. }
  3410. #endif // NV_RESTORE
  3411. /******************************************************************************
  3412. * @fn ZDSecMgrAPSRemove
  3413. *
  3414. * @brief Remove device from network.
  3415. *
  3416. * @param nwkAddr - device's NWK address
  3417. * @param extAddr - device's Extended address
  3418. * @param parentAddr - parent's NWK address
  3419. *
  3420. * @return ZStatus_t
  3421. */
  3422. ZStatus_t ZDSecMgrAPSRemove( uint16 nwkAddr, uint8 *extAddr, uint16 parentAddr )
  3423. {
  3424. ZDSecMgrDevice_t device;
  3425. if ( ( nwkAddr == INVALID_NODE_ADDR ) ||
  3426. ( extAddr == NULL ) ||
  3427. ( parentAddr == INVALID_NODE_ADDR ) )
  3428. {
  3429. return ( ZFailure );
  3430. }
  3431. device.nwkAddr = nwkAddr;
  3432. device.extAddr = extAddr;
  3433. device.parentAddr = parentAddr;
  3434. // remove device
  3435. ZDSecMgrDeviceRemove( &device );
  3436. return ( ZSuccess );
  3437. }
  3438. /******************************************************************************
  3439. * @fn APSME_TCLinkKeyInit
  3440. *
  3441. * @brief Initialize the NV table for preconfigured TC link key
  3442. *
  3443. * When zgUseDefaultTCL is set to TRUE, the default preconfig
  3444. * Trust Center Link Key is written to NV. A single tclk is used
  3445. * by all devices joining the network.
  3446. *
  3447. * @param setDefault - TRUE to set default values
  3448. *
  3449. * @return none
  3450. */
  3451. void APSME_TCLinkKeyInit(uint8 setDefault)
  3452. {
  3453. uint8 i;
  3454. APSME_TCLinkKey_t tcLinkKey;
  3455. uint8 rtrn;
  3456. // Initialize all NV items for preconfigured TCLK
  3457. for( i = 0; i < ZDSECMGR_TC_DEVICE_MAX; i++ )
  3458. {
  3459. // Making sure data is cleared for every key all the time
  3460. osal_memset( &tcLinkKey, 0x00, sizeof(APSME_TCLinkKey_t) );
  3461. // Initialize first element of the table with the default TCLK
  3462. if((i == 0) && ( zgUseDefaultTCLK == TRUE ))
  3463. {
  3464. osal_memset( tcLinkKey.extAddr, 0xFF, Z_EXTADDR_LEN );
  3465. osal_memcpy( tcLinkKey.key, defaultTCLinkKey, SEC_KEY_LEN);
  3466. }
  3467. // If the item doesn't exist in NV memory, create and initialize
  3468. // it with the default value passed in, either defaultTCLK or 0
  3469. rtrn = osal_nv_item_init( (ZCD_NV_TCLK_TABLE_START + i),
  3470. sizeof(APSME_TCLinkKey_t), &tcLinkKey);
  3471. if (rtrn == SUCCESS)
  3472. {
  3473. // set the Frame counters to 0 to existing keys in NV
  3474. osal_nv_read( ( ZCD_NV_TCLK_TABLE_START + i), 0,
  3475. sizeof(APSME_TCLinkKey_t), &tcLinkKey );
  3476. #if defined ( NV_RESTORE )
  3477. if (setDefault == TRUE)
  3478. {
  3479. // clear the value stored in NV
  3480. tcLinkKey.txFrmCntr = 0;
  3481. }
  3482. else
  3483. {
  3484. // increase the value stored in NV
  3485. tcLinkKey.txFrmCntr += ( MAX_TCLK_FRAMECOUNTER_CHANGES + 1 );
  3486. }
  3487. #else
  3488. // Clear the counters if NV_RESTORE is not enabled and this NV item
  3489. // already existed in the NV memory
  3490. tcLinkKey.txFrmCntr = 0;
  3491. tcLinkKey.rxFrmCntr = 0;
  3492. #endif // NV_RESTORE
  3493. osal_nv_write( ( ZCD_NV_TCLK_TABLE_START + i), 0,
  3494. sizeof(APSME_TCLinkKey_t), &tcLinkKey );
  3495. // set initial values for counters in RAM
  3496. TCLinkKeyFrmCntr[i].txFrmCntr = tcLinkKey.txFrmCntr;
  3497. TCLinkKeyFrmCntr[i].rxFrmCntr = tcLinkKey.rxFrmCntr;
  3498. }
  3499. }
  3500. // clear copy of key in RAM
  3501. osal_memset( &tcLinkKey, 0x00, sizeof(APSME_TCLinkKey_t) );
  3502. }
  3503. /******************************************************************************
  3504. * @fn APSME_TCLinkKeySync
  3505. *
  3506. * @brief Sync Trust Center LINK key data.
  3507. *
  3508. * @param srcAddr - [in] srcAddr
  3509. * @param si - [in, out] SSP_Info_t
  3510. *
  3511. * @return ZStatus_t
  3512. */
  3513. ZStatus_t APSME_TCLinkKeySync( uint16 srcAddr, SSP_Info_t* si )
  3514. {
  3515. uint8 i;
  3516. ZStatus_t status = ZSecNoKey;
  3517. APSME_TCLinkKey_t tcLinkKey;
  3518. uint32 *tclkRxFrmCntr;
  3519. // Look up the IEEE address of the trust center if it's available
  3520. if ( AddrMgrExtAddrValid( si->extAddr ) == FALSE )
  3521. {
  3522. APSME_LookupExtAddr( srcAddr, si->extAddr );
  3523. }
  3524. // Look up the TC link key associated with the device
  3525. // or the default TC link key (extAddr is all FFs), whichever is found
  3526. for( i = 0; i < ZDSECMGR_TC_DEVICE_MAX; i++ )
  3527. {
  3528. // Read entry i of the TC link key table from NV
  3529. osal_nv_read( (ZCD_NV_TCLK_TABLE_START + i), 0,
  3530. sizeof(APSME_TCLinkKey_t), &tcLinkKey );
  3531. if( AddrMgrExtAddrEqual(si->extAddr, tcLinkKey.extAddr) ||
  3532. APSME_IsDefaultTCLK(tcLinkKey.extAddr))
  3533. {
  3534. tclkRxFrmCntr = &TCLinkKeyFrmCntr[i].rxFrmCntr;
  3535. // verify that the incoming frame counter is valid
  3536. if ( si->frmCntr >= *tclkRxFrmCntr )
  3537. {
  3538. // set the keyNvId to use
  3539. si->keyNvId = (ZCD_NV_TCLK_TABLE_START + i);
  3540. // update the rx frame counter
  3541. *tclkRxFrmCntr = si->frmCntr + 1;
  3542. status = ZSuccess;
  3543. }
  3544. else
  3545. {
  3546. status = ZSecOldFrmCount;
  3547. }
  3548. // break from the loop
  3549. break;
  3550. }
  3551. }
  3552. // clear copy of key in RAM
  3553. osal_memset( &tcLinkKey, 0x00, sizeof(APSME_TCLinkKey_t) );
  3554. return status;
  3555. }
  3556. /******************************************************************************
  3557. * @fn APSME_TCLinkKeyLoad
  3558. *
  3559. * @brief Load Trust Center LINK key data.
  3560. *
  3561. * @param dstAddr - [in] dstAddr
  3562. * @param si - [in, out] SSP_Info_t
  3563. *
  3564. * @return ZStatus_t
  3565. */
  3566. ZStatus_t APSME_TCLinkKeyLoad( uint16 dstAddr, SSP_Info_t* si )
  3567. {
  3568. uint8 i;
  3569. ZStatus_t status = ZSecNoKey;
  3570. APSME_TCLinkKey_t tcLinkKey;
  3571. AddrMgrEntry_t addrEntry;
  3572. uint32 *tclkTxFrmCntr;
  3573. uint8 extAddrFound;
  3574. uint8 defaultTCLKIdx = ZDSECMGR_TC_DEVICE_MAX;
  3575. // Look up the ami of the srcAddr if available
  3576. addrEntry.user = ADDRMGR_USER_DEFAULT;
  3577. addrEntry.nwkAddr = dstAddr;
  3578. APSME_LookupExtAddr( dstAddr, si->extAddr );
  3579. extAddrFound = AddrMgrExtAddrValid( si->extAddr );
  3580. // Look up the TC link key associated with the device
  3581. // or the master TC link key (ami = 0xFFFF), whichever is found
  3582. for( i = 0; i < ZDSECMGR_TC_DEVICE_MAX; i++ )
  3583. {
  3584. // Read entry i of the TC link key table from NV
  3585. osal_nv_read( (ZCD_NV_TCLK_TABLE_START + i), 0,
  3586. sizeof(APSME_TCLinkKey_t), &tcLinkKey );
  3587. if( extAddrFound && AddrMgrExtAddrEqual(si->extAddr, tcLinkKey.extAddr) )
  3588. {
  3589. status = ZSuccess;
  3590. break; // break from the loop
  3591. }
  3592. if ( APSME_IsDefaultTCLK(tcLinkKey.extAddr) )
  3593. {
  3594. if ( !extAddrFound )
  3595. {
  3596. status = ZSuccess;
  3597. break; // break from the loop
  3598. }
  3599. // Remember the default TCLK index
  3600. defaultTCLKIdx = i;
  3601. }
  3602. }
  3603. if ( (status != ZSuccess) && (defaultTCLKIdx < ZDSECMGR_TC_DEVICE_MAX) )
  3604. {
  3605. // Exact match was not found; use the default TC Link Key
  3606. i = defaultTCLKIdx;
  3607. status = ZSuccess;
  3608. }
  3609. if ( status == ZSuccess )
  3610. {
  3611. tclkTxFrmCntr = &TCLinkKeyFrmCntr[i].txFrmCntr;
  3612. // set the keyNvId to use
  3613. si->keyNvId = (ZCD_NV_TCLK_TABLE_START + i);
  3614. // update link key related fields
  3615. si->keyID = SEC_KEYID_LINK;
  3616. si->frmCntr = *tclkTxFrmCntr;
  3617. // update outgoing frame counter
  3618. (*tclkTxFrmCntr)++;
  3619. #if defined ( NV_RESTORE )
  3620. // write periodically to NV
  3621. if ( !(*tclkTxFrmCntr % MAX_TCLK_FRAMECOUNTER_CHANGES) )
  3622. {
  3623. // set the flag to write key to NV
  3624. TCLinkKeyFrmCntr[i].pendingFlag = TRUE;
  3625. // Notify the ZDApp that the frame counter has changed.
  3626. osal_set_event( ZDAppTaskID, ZDO_TCLK_FRAMECOUNTER_CHANGE );
  3627. }
  3628. #endif
  3629. }
  3630. // If no TC link key found, remove the device from the address manager
  3631. if ( (status != ZSuccess) && (AddrMgrEntryLookupNwk(&addrEntry) == TRUE) )
  3632. {
  3633. AddrMgrEntryRelease( &addrEntry );
  3634. }
  3635. // clear copy of key in RAM
  3636. osal_memset( &tcLinkKey, 0x00, sizeof(APSME_TCLinkKey_t) );
  3637. return status;
  3638. }
  3639. /******************************************************************************
  3640. * @fn APSME_IsDefaultTCLK
  3641. *
  3642. * @brief Return TRUE or FALSE based on the extended address. If the
  3643. * input ext address is all FFs, it means the trust center link
  3644. * assoiciated with the address is the default trust center link key
  3645. *
  3646. * @param extAddr - [in] extended address
  3647. *
  3648. * @return uint8 TRUE/FALSE
  3649. */
  3650. uint8 APSME_IsDefaultTCLK( uint8 *extAddr )
  3651. {
  3652. uint8 i = 0;
  3653. if( extAddr == NULL )
  3654. {
  3655. return FALSE;
  3656. }
  3657. while( i++ < Z_EXTADDR_LEN )
  3658. {
  3659. if( *extAddr++ != 0xFF )
  3660. {
  3661. return FALSE;
  3662. }
  3663. }
  3664. return TRUE;
  3665. }
  3666. /******************************************************************************
  3667. * @fn ZDSecMgrNwkKeyInit
  3668. *
  3669. * @brief Initialize the NV items for
  3670. * ZCD_NV_NWKKEY,
  3671. * ZCD_NV_NWK_ACTIVE_KEY_INFO and
  3672. * ZCD_NV_NWK_ALTERN_KEY_INFO
  3673. *
  3674. * @param setDefault
  3675. *
  3676. * @return none
  3677. */
  3678. void ZDSecMgrNwkKeyInit(uint8 setDefault)
  3679. {
  3680. uint8 status;
  3681. nwkKeyDesc nwkKey;
  3682. // Initialize NV items for NWK key, this structure contains the frame counter
  3683. // and is only used when NV_RESTORE is enabled
  3684. nwkActiveKeyItems keyItems;
  3685. osal_memset( &keyItems, 0, sizeof( nwkActiveKeyItems ) );
  3686. status = osal_nv_item_init( ZCD_NV_NWKKEY, sizeof(nwkActiveKeyItems), (void *)&keyItems );
  3687. #if defined ( NV_RESTORE )
  3688. // reset the values of NV items if NV_RESTORE is not enabled
  3689. if ((status == SUCCESS) && (setDefault == TRUE))
  3690. {
  3691. // clear NV data to default values
  3692. osal_nv_write( ZCD_NV_NWKKEY, 0, sizeof(nwkActiveKeyItems), &keyItems );
  3693. }
  3694. #else
  3695. (void)setDefault; // to eliminate compiler warning
  3696. // reset the values of NV items if NV_RESTORE is not enabled
  3697. if (status == SUCCESS)
  3698. {
  3699. osal_nv_write( ZCD_NV_NWKKEY, 0, sizeof(nwkActiveKeyItems), &keyItems );
  3700. }
  3701. #endif // defined (NV_RESTORE)
  3702. // Initialize NV items for NWK Active and Alternate keys. These items are used
  3703. // all the time, independently of NV_RESTORE being set or not
  3704. osal_memset( &nwkKey, 0x00, sizeof(nwkKey) );
  3705. status = osal_nv_item_init( ZCD_NV_NWK_ACTIVE_KEY_INFO, sizeof(nwkKey), &nwkKey);
  3706. #if defined ( NV_RESTORE )
  3707. // reset the values of NV items if NV_RESTORE is not enabled
  3708. if ((status == SUCCESS) && (setDefault == TRUE))
  3709. {
  3710. // clear NV data to default values
  3711. osal_nv_write( ZCD_NV_NWK_ACTIVE_KEY_INFO, 0, sizeof(nwkKey), &nwkKey );
  3712. }
  3713. #else
  3714. // reset the values of NV items if NV_RESTORE is not enabled
  3715. if (status == SUCCESS)
  3716. {
  3717. osal_nv_write( ZCD_NV_NWK_ACTIVE_KEY_INFO, 0, sizeof(nwkKey), &nwkKey );
  3718. }
  3719. #endif // defined (NV_RESTORE)
  3720. status = osal_nv_item_init( ZCD_NV_NWK_ALTERN_KEY_INFO, sizeof(nwkKey), &nwkKey );
  3721. #if defined ( NV_RESTORE )
  3722. // reset the values of NV items if NV_RESTORE is not enabled
  3723. if ((status == SUCCESS) && (setDefault == TRUE))
  3724. {
  3725. // clear NV data to default values
  3726. osal_nv_write( ZCD_NV_NWK_ALTERN_KEY_INFO, 0, sizeof(nwkKey), &nwkKey );
  3727. }
  3728. #else
  3729. // reset the values of NV items if NV_RESTORE is not enabled
  3730. if (status == SUCCESS)
  3731. {
  3732. osal_nv_write( ZCD_NV_NWK_ALTERN_KEY_INFO, 0, sizeof(nwkKey), &nwkKey );
  3733. }
  3734. #endif // defined (NV_RESTORE)
  3735. }
  3736. /*********************************************************************
  3737. * @fn ZDSecMgrReadKeyFromNv
  3738. *
  3739. * @brief Looks for a specific key in NV based on Index value
  3740. *
  3741. * @param keyNvId - Index of key to look in NV
  3742. * valid values are:
  3743. * ZCD_NV_NWK_ACTIVE_KEY_INFO
  3744. * ZCD_NV_NWK_ALTERN_KEY_INFO
  3745. * ZCD_NV_TCLK_TABLE_START + <offset_in_table>
  3746. * ZCD_NV_APS_LINK_KEY_DATA_START + <offset_in_table>
  3747. * ZCD_NV_MASTER_KEY_DATA_START + <offset_in_table>
  3748. * ZCD_NV_PRECFGKEY
  3749. *
  3750. * @param *keyinfo - Data is read into this buffer.
  3751. *
  3752. * @return SUCCESS if NV data was copied to the keyinfo parameter .
  3753. * Otherwise, NV_OPER_FAILED for failure.
  3754. */
  3755. ZStatus_t ZDSecMgrReadKeyFromNv(uint16 keyNvId, void *keyinfo)
  3756. {
  3757. if ((keyNvId == ZCD_NV_NWK_ACTIVE_KEY_INFO) ||
  3758. (keyNvId == ZCD_NV_NWK_ALTERN_KEY_INFO))
  3759. {
  3760. // get NWK active or alternate key from NV
  3761. return (osal_nv_read(keyNvId,
  3762. osal_offsetof(nwkKeyDesc, key),
  3763. SEC_KEY_LEN,
  3764. keyinfo));
  3765. }
  3766. else if ((keyNvId >= ZCD_NV_TCLK_TABLE_START) &&
  3767. (keyNvId < (ZCD_NV_TCLK_TABLE_START + ZDSECMGR_TC_DEVICE_MAX)))
  3768. {
  3769. // Read entry keyNvId of the TC link key table from NV. keyNvId should be
  3770. // ZCD_NV_TCLK_TABLE_START + <offset_in_table>
  3771. return (osal_nv_read(keyNvId,
  3772. osal_offsetof(APSME_TCLinkKey_t, key),
  3773. SEC_KEY_LEN,
  3774. keyinfo));
  3775. }
  3776. else if ((keyNvId >= ZCD_NV_APS_LINK_KEY_DATA_START) &&
  3777. (keyNvId < (ZCD_NV_APS_LINK_KEY_DATA_START + ZDSECMGR_ENTRY_MAX)))
  3778. {
  3779. // Read entry keyNvId of the APS link key table from NV. keyNvId should be
  3780. // ZCD_NV_APS_LINK_KEY_DATA_START + <offset_in_table>
  3781. return (osal_nv_read(keyNvId,
  3782. osal_offsetof(APSME_LinkKeyData_t, key),
  3783. SEC_KEY_LEN,
  3784. keyinfo));
  3785. }
  3786. else if ((keyNvId >= ZCD_NV_MASTER_KEY_DATA_START) &&
  3787. (keyNvId < (ZCD_NV_MASTER_KEY_DATA_START + ZDSECMGR_MASTERKEY_MAX)))
  3788. {
  3789. // Read entry keyNvId of the MASTER key table from NV. keyNvId should be
  3790. // ZCD_NV_MASTER_KEY_DATA_START + <offset_in_table>
  3791. return (osal_nv_read(keyNvId,
  3792. osal_offsetof(ZDSecMgrMasterKeyData_t, key),
  3793. SEC_KEY_LEN,
  3794. keyinfo));
  3795. }
  3796. else if (keyNvId == ZCD_NV_PRECFGKEY)
  3797. {
  3798. // Read entry keyNvId of the Preconfig key from NV.
  3799. return (osal_nv_read(keyNvId,
  3800. 0,
  3801. SEC_KEY_LEN,
  3802. keyinfo));
  3803. }
  3804. return NV_OPER_FAILED;
  3805. }
  3806. /******************************************************************************
  3807. * @fn ZDSecMgrApsLinkKeyInit
  3808. *
  3809. * @brief Initialize the NV table for Application link keys
  3810. *
  3811. * @param none
  3812. *
  3813. * @return none
  3814. */
  3815. void ZDSecMgrApsLinkKeyInit(void)
  3816. {
  3817. APSME_LinkKeyData_t pApsLinkKey;
  3818. uint8 i;
  3819. uint8 status;
  3820. // Initialize all NV items for APS link key, if not exist already.
  3821. osal_memset( &pApsLinkKey, 0x00, sizeof(APSME_LinkKeyData_t) );
  3822. for( i = 0; i < ZDSECMGR_ENTRY_MAX; i++ )
  3823. {
  3824. status = osal_nv_item_init( (ZCD_NV_APS_LINK_KEY_DATA_START + i),
  3825. sizeof(APSME_LinkKeyData_t), &pApsLinkKey );
  3826. #if defined ( NV_RESTORE )
  3827. (void)status; // to eliminate compiler warning
  3828. #else
  3829. // reset the values of NV items if NV_RESTORE is not enabled
  3830. if (status == SUCCESS)
  3831. {
  3832. osal_nv_write( (ZCD_NV_APS_LINK_KEY_DATA_START + i), 0,
  3833. sizeof(APSME_LinkKeyData_t), &pApsLinkKey );
  3834. }
  3835. #endif // defined (NV_RESTORE)
  3836. }
  3837. }
  3838. /******************************************************************************
  3839. * @fn ZDSecMgrInitNVKeyTables
  3840. *
  3841. * @brief Initialize the NV table for All keys: NWK, Master, TCLK and APS
  3842. *
  3843. * @param setDefault - TRUE to set default values
  3844. *
  3845. * @return none
  3846. */
  3847. void ZDSecMgrInitNVKeyTables(uint8 setDefault)
  3848. {
  3849. ZDSecMgrNwkKeyInit(setDefault);
  3850. ZDSecMgrMasterKeyInit();
  3851. ZDSecMgrApsLinkKeyInit();
  3852. APSME_TCLinkKeyInit(setDefault);
  3853. }
  3854. /******************************************************************************
  3855. * @fn ZDSecMgrSaveApsLinkKey
  3856. *
  3857. * @brief Save APS Link Key to NV. It will loop through all the keys
  3858. * to see which one to save.
  3859. *
  3860. * @param none
  3861. *
  3862. * @return none
  3863. */
  3864. void ZDSecMgrSaveApsLinkKey(void)
  3865. {
  3866. APSME_LinkKeyData_t *pKeyData = NULL;
  3867. int i;
  3868. pKeyData = (APSME_LinkKeyData_t *)osal_mem_alloc(sizeof(APSME_LinkKeyData_t));
  3869. if (pKeyData != NULL)
  3870. {
  3871. // checks all pending flags to know which one to save
  3872. for (i = 0; i < ZDSECMGR_ENTRY_MAX; i++)
  3873. {
  3874. if (ApsLinkKeyFrmCntr[i].pendingFlag == TRUE)
  3875. {
  3876. // retrieve key from NV
  3877. if (osal_nv_read(ZCD_NV_APS_LINK_KEY_DATA_START + i, 0,
  3878. sizeof(APSME_LinkKeyData_t), pKeyData) == SUCCESS)
  3879. {
  3880. pKeyData->txFrmCntr = ApsLinkKeyFrmCntr[i].txFrmCntr;
  3881. pKeyData->rxFrmCntr = ApsLinkKeyFrmCntr[i].rxFrmCntr;
  3882. // Write the APS link key back to the NV
  3883. osal_nv_write(ZCD_NV_APS_LINK_KEY_DATA_START + i, 0,
  3884. sizeof(APSME_LinkKeyData_t), pKeyData);
  3885. // clear the pending write flag
  3886. ApsLinkKeyFrmCntr[i].pendingFlag = FALSE;
  3887. }
  3888. }
  3889. }
  3890. // clear copy of key in RAM
  3891. osal_memset( pKeyData, 0x00, sizeof(APSME_LinkKeyData_t) );
  3892. osal_mem_free(pKeyData);
  3893. }
  3894. }
  3895. /******************************************************************************
  3896. * @fn ZDSecMgrSaveTCLinkKey
  3897. *
  3898. * @brief Save TC Link Key to NV. It will loop through all the keys
  3899. * to see which one to save.
  3900. *
  3901. * @param none
  3902. *
  3903. * @return none
  3904. */
  3905. void ZDSecMgrSaveTCLinkKey(void)
  3906. {
  3907. APSME_TCLinkKey_t *pKeyData = NULL;
  3908. uint16 i;
  3909. pKeyData = (APSME_TCLinkKey_t *)osal_mem_alloc(sizeof(APSME_TCLinkKey_t));
  3910. if (pKeyData != NULL)
  3911. {
  3912. for( i = 0; i < ZDSECMGR_TC_DEVICE_MAX; i++ )
  3913. {
  3914. if (TCLinkKeyFrmCntr[i].pendingFlag == TRUE)
  3915. {
  3916. if (osal_nv_read(ZCD_NV_TCLK_TABLE_START + i, 0,
  3917. sizeof(APSME_TCLinkKey_t), pKeyData) == SUCCESS)
  3918. {
  3919. pKeyData->txFrmCntr = TCLinkKeyFrmCntr[i].txFrmCntr;
  3920. pKeyData->rxFrmCntr = TCLinkKeyFrmCntr[i].rxFrmCntr;
  3921. // Write the TC link key back to the NV
  3922. osal_nv_write(ZCD_NV_TCLK_TABLE_START + i, 0,
  3923. sizeof(APSME_TCLinkKey_t), pKeyData);
  3924. // clear the pending write flag
  3925. TCLinkKeyFrmCntr[i].pendingFlag = FALSE;
  3926. }
  3927. }
  3928. }
  3929. // clear copy of key in RAM
  3930. osal_memset( pKeyData, 0x00, sizeof(APSME_TCLinkKey_t) );
  3931. osal_mem_free(pKeyData);
  3932. }
  3933. }
  3934. #if defined ( ZBA_FALLBACK_NWKKEY )
  3935. /******************************************************************************
  3936. * @fn ZDSecMgrFallbackNwkKey
  3937. *
  3938. * @brief Use the ZBA fallback network key.
  3939. *
  3940. * @param none
  3941. *
  3942. * @return none
  3943. */
  3944. void ZDSecMgrFallbackNwkKey( void )
  3945. {
  3946. if ( !_NIB.nwkKeyLoaded )
  3947. {
  3948. uint8 fallbackKey[SEC_KEY_LEN];
  3949. ZDSecMgrReadKeyFromNv( ZCD_NV_PRECFGKEY, fallbackKey );
  3950. SSP_UpdateNwkKey( fallbackKey, 0);
  3951. SSP_SwitchNwkKey( 0 );
  3952. // clear local copy of key
  3953. osal_memset( fallbackKey, 0x00, SEC_KEY_LEN );
  3954. // handle next step in authentication process
  3955. ZDSecMgrAuthNwkKey();
  3956. }
  3957. }
  3958. #endif // defined ( ZBA_FALLBACK_NWKKEY )
  3959. #if defined ( NV_RESTORE )
  3960. /******************************************************************************
  3961. * @fn ZDSecMgrClearNVKeyValues
  3962. *
  3963. * @brief If NV_RESTORE is enabled and the status of the network needs
  3964. * default values this fuction clears ZCD_NV_NWKKEY,
  3965. * ZCD_NV_NWK_ACTIVE_KEY_INFO and ZCD_NV_NWK_ALTERN_KEY_INFO link
  3966. *
  3967. * @param none
  3968. *
  3969. * @return none
  3970. */
  3971. void ZDSecMgrClearNVKeyValues(void)
  3972. {
  3973. nwkActiveKeyItems keyItems;
  3974. nwkKeyDesc nwkKey;
  3975. osal_memset(&keyItems, 0x00, sizeof(nwkActiveKeyItems));
  3976. osal_nv_write(ZCD_NV_NWKKEY, 0, sizeof(nwkActiveKeyItems), &keyItems);
  3977. // Initialize NV items for NWK Active and Alternate keys.
  3978. osal_memset( &nwkKey, 0x00, sizeof(nwkKeyDesc) );
  3979. osal_nv_write(ZCD_NV_NWK_ACTIVE_KEY_INFO, 0, sizeof(nwkKeyDesc), &nwkKey);
  3980. osal_nv_write(ZCD_NV_NWK_ALTERN_KEY_INFO, 0, sizeof(nwkKeyDesc), &nwkKey);
  3981. }
  3982. #endif // defined ( NV_RESTORE )
  3983. /******************************************************************************
  3984. ******************************************************************************/