ZDSecMgr.c 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696
  1. /**************************************************************************************************
  2. Filename: ZDSecMgr.c
  3. Revised: $Date: 2009-03-31 09:06:47 -0700 (Tue, 31 Mar 2009) $
  4. Revision: $Revision: 19604 $
  5. Description: The ZigBee Device Security Manager.
  6. Copyright 2005-2008 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 "AF.h"
  52. #include "ZDConfig.h"
  53. #include "ZDApp.h"
  54. #include "ZDSecMgr.h"
  55. /******************************************************************************
  56. * CONSTANTS
  57. */
  58. // maximum number of devices managed by this Security Manager
  59. #if !defined ( ZDSECMGR_DEVICE_MAX )
  60. #define ZDSECMGR_DEVICE_MAX 3
  61. #endif
  62. // total number of preconfigured devices (EXT address, MASTER key)
  63. //devtag.pro.security
  64. //#define ZDSECMGR_PRECONFIG_MAX ZDSECMGR_DEVICE_MAX
  65. #define ZDSECMGR_PRECONFIG_MAX 0
  66. // maximum number of MASTER keys this device may hold
  67. #define ZDSECMGR_MASTERKEY_MAX ZDSECMGR_DEVICE_MAX
  68. // maximum number of LINK keys this device may store
  69. #define ZDSECMGR_ENTRY_MAX ZDSECMGR_DEVICE_MAX
  70. // total number of devices under control - authentication, SKKE, etc.
  71. #define ZDSECMGR_CTRL_MAX ZDSECMGR_DEVICE_MAX
  72. // total number of stored devices
  73. #if !defined ( ZDSECMGR_STORED_DEVICES )
  74. #define ZDSECMGR_STORED_DEVICES 3
  75. #endif
  76. #define ZDSECMGR_CTRL_NONE 0
  77. #define ZDSECMGR_CTRL_INIT 1
  78. #define ZDSECMGR_CTRL_TK_MASTER 2
  79. #define ZDSECMGR_CTRL_SKKE_INIT 3
  80. #define ZDSECMGR_CTRL_SKKE_WAIT 4
  81. #define ZDSECMGR_CTRL_SKKE_DONE 5
  82. #define ZDSECMGR_CTRL_SKKE_FAIL 6
  83. #define ZDSECMGR_CTRL_TK_NWK 7
  84. #define ZDSECMGR_CTRL_BASE_CNTR 1
  85. #define ZDSECMGR_CTRL_SKKE_INIT_CNTR 1
  86. #define ZDSECMGR_CTRL_TK_NWK_CNTR 1
  87. // set SKA slot maximum
  88. #define ZDSECMGR_SKA_SLOT_MAX 1
  89. // APSME Stub Implementations
  90. #define ZDSecMgrMasterKeyGet APSME_MasterKeyGet
  91. #define ZDSecMgrLinkKeySet APSME_LinkKeySet
  92. #define ZDSecMgrLinkKeyDataGet APSME_LinkKeyDataGet
  93. #define ZDSecMgrKeyFwdToChild APSME_KeyFwdToChild
  94. #if !defined( MAX_APS_FRAMECOUNTER_CHANGES )
  95. // The number of times the frame counter can change before
  96. // saving to NV
  97. #define MAX_APS_FRAMECOUNTER_CHANGES 10
  98. #endif
  99. /******************************************************************************
  100. * TYPEDEFS
  101. */
  102. typedef struct
  103. {
  104. uint8 extAddr[Z_EXTADDR_LEN];
  105. uint8 key[SEC_KEY_LEN];
  106. } ZDSecMgrPreConfigData_t;
  107. typedef struct
  108. {
  109. uint16 ami;
  110. uint8 key[SEC_KEY_LEN];
  111. } ZDSecMgrMasterKeyData_t;
  112. //should match APSME_LinkKeyData_t;
  113. typedef struct
  114. {
  115. uint8 key[SEC_KEY_LEN];
  116. APSME_LinkKeyData_t apsmelkd;
  117. } ZDSecMgrLinkKeyData_t;
  118. typedef struct
  119. {
  120. uint16 ami;
  121. ZDSecMgrLinkKeyData_t lkd;
  122. ZDSecMgr_Authentication_Option authenticateOption;
  123. } ZDSecMgrEntry_t;
  124. typedef struct
  125. {
  126. ZDSecMgrEntry_t* entry;
  127. uint16 parentAddr;
  128. uint8 secure;
  129. uint8 state;
  130. uint8 cntr;
  131. //uint8 next;
  132. } ZDSecMgrCtrl_t;
  133. typedef struct
  134. {
  135. uint16 nwkAddr;
  136. uint8* extAddr;
  137. uint16 parentAddr;
  138. uint8 secure;
  139. uint8 devStatus;
  140. ZDSecMgrCtrl_t* ctrl;
  141. } ZDSecMgrDevice_t;
  142. /******************************************************************************
  143. * LOCAL VARIABLES
  144. */
  145. #if 0 // Taken out because the following functionality is only used for test
  146. // purpose. A more efficient (above) way is used. It can be put
  147. // back in if customers request for a white/black list feature.
  148. uint8 ZDSecMgrStoredDeviceList[ZDSECMGR_STORED_DEVICES][Z_EXTADDR_LEN] =
  149. {
  150. { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  151. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
  152. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  153. };
  154. #endif
  155. uint8 ZDSecMgrTCExtAddr[Z_EXTADDR_LEN]=
  156. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  157. uint8 ZDSecMgrTCMasterKey[SEC_KEY_LEN] =
  158. {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  159. 0x89,0x67,0x45,0x23,0x01,0xEF,0xCD,0xAB};
  160. uint8 ZDSecMgrTCAuthenticated = FALSE;
  161. uint8 ZDSecMgrTCDataLoaded = FALSE;
  162. //devtag.pro.security - remove this
  163. #if ( ZDSECMGR_PRECONFIG_MAX != 0 )
  164. const ZDSecMgrPreConfigData_t ZDSecMgrPreConfigData[ZDSECMGR_PRECONFIG_MAX] =
  165. {
  166. //---------------------------------------------------------------------------
  167. // DEVICE A
  168. //---------------------------------------------------------------------------
  169. {
  170. // extAddr
  171. {0x7C,0x01,0x12,0x13,0x14,0x15,0x16,0x17},
  172. // key
  173. {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  174. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
  175. },
  176. //---------------------------------------------------------------------------
  177. // DEVICE B
  178. //---------------------------------------------------------------------------
  179. {
  180. // extAddr
  181. {0x84,0x03,0x00,0x00,0x00,0x4B,0x12,0x00},
  182. // key
  183. {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  184. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
  185. },
  186. //---------------------------------------------------------------------------
  187. // DEVICE C
  188. //---------------------------------------------------------------------------
  189. {
  190. // extAddr
  191. {0x3E,0x01,0x12,0x13,0x14,0x15,0x16,0x17},
  192. // key
  193. {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  194. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
  195. },
  196. };
  197. #endif // ( ZDSECMGR_PRECONFIG_MAX != 0 )
  198. ZDSecMgrMasterKeyData_t* ZDSecMgrMasterKeyData = NULL;
  199. ZDSecMgrEntry_t* ZDSecMgrEntries = NULL;
  200. ZDSecMgrCtrl_t* ZDSecMgrCtrlData = NULL;
  201. void ZDSecMgrAddrMgrUpdate( uint16 ami, uint16 nwkAddr );
  202. void ZDSecMgrAddrMgrCB( uint8 update, AddrMgrEntry_t* newEntry, AddrMgrEntry_t* oldEntry );
  203. uint8 ZDSecMgrPermitJoiningEnabled;
  204. uint8 ZDSecMgrPermitJoiningTimed;
  205. APSME_LinkKeyData_t TrustCenterLinkKey;
  206. /******************************************************************************
  207. * PRIVATE FUNCTIONS
  208. *
  209. * ZDSecMgrMasterKeyInit
  210. * ZDSecMgrAddrStore
  211. * ZDSecMgrExtAddrStore
  212. * ZDSecMgrExtAddrLookup
  213. * ZDSecMgrMasterKeyLookup
  214. * ZDSecMgrMasterKeyStore
  215. * ZDSecMgrEntryInit
  216. * ZDSecMgrEntryLookup
  217. * ZDSecMgrEntryLookupAMI
  218. * ZDSecMgrEntryLookupExt
  219. * ZDSecMgrEntryFree
  220. * ZDSecMgrEntryNew
  221. * ZDSecMgrCtrlInit
  222. * ZDSecMgrCtrlRelease
  223. * ZDSecMgrCtrlLookup
  224. * ZDSecMgrCtrlSet
  225. * ZDSecMgrCtrlAdd
  226. * ZDSecMgrCtrlTerm
  227. * ZDSecMgrCtrlReset
  228. * ZDSecMgrMasterKeyLoad
  229. * ZDSecMgrAppKeyGet
  230. * ZDSecMgrAppKeyReq
  231. * ZDSecMgrEstablishKey
  232. * ZDSecMgrSendMasterKey
  233. * ZDSecMgrSendNwkKey
  234. * ZDSecMgrDeviceEntryRemove
  235. * ZDSecMgrDeviceEntryAdd
  236. * ZDSecMgrDeviceCtrlHandler
  237. * ZDSecMgrDeviceCtrlSetup
  238. * ZDSecMgrDeviceCtrlUpdate
  239. * ZDSecMgrDeviceRemove
  240. * ZDSecMgrDeviceValidateSKKE
  241. * ZDSecMgrDeviceValidateRM
  242. * ZDSecMgrDeviceValidateCM
  243. * ZDSecMgrDeviceValidate
  244. * ZDSecMgrDeviceJoin
  245. * ZDSecMgrDeviceJoinDirect
  246. * ZDSecMgrDeviceJoinFwd
  247. * ZDSecMgrDeviceNew
  248. * ZDSecMgrAssocDeviceAuth
  249. * ZDSecMgrAuthInitiate
  250. * ZDSecMgrAuthNwkKey
  251. */
  252. //-----------------------------------------------------------------------------
  253. // master key data
  254. //-----------------------------------------------------------------------------
  255. void ZDSecMgrMasterKeyInit( void );
  256. //-----------------------------------------------------------------------------
  257. // address management
  258. //-----------------------------------------------------------------------------
  259. ZStatus_t ZDSecMgrAddrStore( uint16 nwkAddr, uint8* extAddr, uint16* ami );
  260. ZStatus_t ZDSecMgrExtAddrStore( uint16 nwkAddr, uint8* extAddr, uint16* ami );
  261. ZStatus_t ZDSecMgrExtAddrLookup( uint8* extAddr, uint16* ami );
  262. //-----------------------------------------------------------------------------
  263. // MASTER key data
  264. //-----------------------------------------------------------------------------
  265. ZStatus_t ZDSecMgrMasterKeyLookup( uint16 ami, uint8** key );
  266. ZStatus_t ZDSecMgrMasterKeyStore( uint16 ami, uint8* key );
  267. //-----------------------------------------------------------------------------
  268. // entry data
  269. //-----------------------------------------------------------------------------
  270. void ZDSecMgrEntryInit( void );
  271. ZStatus_t ZDSecMgrEntryLookup( uint16 nwkAddr, ZDSecMgrEntry_t** entry );
  272. ZStatus_t ZDSecMgrEntryLookupAMI( uint16 ami, ZDSecMgrEntry_t** entry );
  273. ZStatus_t ZDSecMgrEntryLookupExt( uint8* extAddr, ZDSecMgrEntry_t** entry );
  274. void ZDSecMgrEntryFree( ZDSecMgrEntry_t* entry );
  275. ZStatus_t ZDSecMgrEntryNew( ZDSecMgrEntry_t** entry );
  276. ZStatus_t ZDSecMgrAuthenticationSet( uint8* extAddr, ZDSecMgr_Authentication_Option option );
  277. //-----------------------------------------------------------------------------
  278. // control data
  279. //-----------------------------------------------------------------------------
  280. void ZDSecMgrCtrlInit( void );
  281. void ZDSecMgrCtrlRelease( ZDSecMgrCtrl_t* ctrl );
  282. void ZDSecMgrCtrlLookup( ZDSecMgrEntry_t* entry, ZDSecMgrCtrl_t** ctrl );
  283. void ZDSecMgrCtrlSet( ZDSecMgrDevice_t* device,
  284. ZDSecMgrEntry_t* entry,
  285. ZDSecMgrCtrl_t* ctrl );
  286. ZStatus_t ZDSecMgrCtrlAdd( ZDSecMgrDevice_t* device, ZDSecMgrEntry_t* entry );
  287. void ZDSecMgrCtrlTerm( ZDSecMgrEntry_t* entry );
  288. ZStatus_t ZDSecMgrCtrlReset( ZDSecMgrDevice_t* device,
  289. ZDSecMgrEntry_t* entry );
  290. //-----------------------------------------------------------------------------
  291. // key support
  292. //-----------------------------------------------------------------------------
  293. ZStatus_t ZDSecMgrMasterKeyLoad( uint8* extAddr, uint8* key );
  294. ZStatus_t ZDSecMgrAppKeyGet( uint16 initNwkAddr,
  295. uint8* initExtAddr,
  296. uint16 partNwkAddr,
  297. uint8* partExtAddr,
  298. uint8** key,
  299. uint8* keyType );
  300. void ZDSecMgrAppKeyReq( ZDO_RequestKeyInd_t* ind );
  301. ZStatus_t ZDSecMgrEstablishKey( ZDSecMgrDevice_t* device );
  302. ZStatus_t ZDSecMgrSendMasterKey( ZDSecMgrDevice_t* device );
  303. ZStatus_t ZDSecMgrSendNwkKey( ZDSecMgrDevice_t* device );
  304. //-----------------------------------------------------------------------------
  305. // device entry
  306. //-----------------------------------------------------------------------------
  307. void ZDSecMgrDeviceEntryRemove( ZDSecMgrEntry_t* entry );
  308. ZStatus_t ZDSecMgrDeviceEntryAdd( ZDSecMgrDevice_t* device, uint16 ami );
  309. //-----------------------------------------------------------------------------
  310. // device control
  311. //-----------------------------------------------------------------------------
  312. void ZDSecMgrDeviceCtrlHandler( ZDSecMgrDevice_t* device );
  313. void ZDSecMgrDeviceCtrlSetup( ZDSecMgrDevice_t* device );
  314. void ZDSecMgrDeviceCtrlUpdate( uint8* extAddr, uint8 state );
  315. //-----------------------------------------------------------------------------
  316. // device management
  317. //-----------------------------------------------------------------------------
  318. void ZDSecMgrDeviceRemove( ZDSecMgrDevice_t* device );
  319. ZStatus_t ZDSecMgrDeviceValidateSKKE( ZDSecMgrDevice_t* device );
  320. ZStatus_t ZDSecMgrDeviceValidateRM( ZDSecMgrDevice_t* device );
  321. ZStatus_t ZDSecMgrDeviceValidateCM( ZDSecMgrDevice_t* device );
  322. ZStatus_t ZDSecMgrDeviceValidate( ZDSecMgrDevice_t* device );
  323. ZStatus_t ZDSecMgrDeviceJoin( ZDSecMgrDevice_t* device );
  324. ZStatus_t ZDSecMgrDeviceJoinDirect( ZDSecMgrDevice_t* device );
  325. ZStatus_t ZDSecMgrDeviceJoinFwd( ZDSecMgrDevice_t* device );
  326. ZStatus_t ZDSecMgrDeviceNew( ZDSecMgrDevice_t* device );
  327. //-----------------------------------------------------------------------------
  328. // association management
  329. //-----------------------------------------------------------------------------
  330. void ZDSecMgrAssocDeviceAuth( associated_devices_t* assoc );
  331. //-----------------------------------------------------------------------------
  332. // authentication management
  333. //-----------------------------------------------------------------------------
  334. void ZDSecMgrAuthInitiate( uint8* responder );
  335. void ZDSecMgrAuthNwkKey( void );
  336. /******************************************************************************
  337. * @fn ZDSecMgrMasterKeyInit ]
  338. *
  339. * @brief Initialize master key data.
  340. *
  341. * @param none
  342. *
  343. * @return none
  344. */
  345. void ZDSecMgrMasterKeyInit( void )
  346. {
  347. uint16 index;
  348. uint16 size;
  349. // allocate MASTER key data
  350. size = (short)( sizeof(ZDSecMgrMasterKeyData_t) * ZDSECMGR_MASTERKEY_MAX );
  351. ZDSecMgrMasterKeyData = osal_mem_alloc( size );
  352. // initialize MASTER key data
  353. if ( ZDSecMgrMasterKeyData != NULL )
  354. {
  355. for ( index = 0; index < ZDSECMGR_MASTERKEY_MAX; index++ )
  356. {
  357. ZDSecMgrMasterKeyData[index].ami = INVALID_NODE_ADDR;
  358. }
  359. }
  360. }
  361. //devtag.pro.security
  362. #if 0
  363. void ZDSecMgrMasterKeyInit( void )
  364. {
  365. uint16 index;
  366. uint16 size;
  367. AddrMgrEntry_t entry;
  368. // allocate MASTER key data
  369. size = (short)( sizeof(ZDSecMgrMasterKeyData_t) * ZDSECMGR_MASTERKEY_MAX );
  370. ZDSecMgrMasterKeyData = osal_mem_alloc( size );
  371. // initialize MASTER key data
  372. if ( ZDSecMgrMasterKeyData != NULL )
  373. {
  374. for ( index = 0; index < ZDSECMGR_MASTERKEY_MAX; index++ )
  375. {
  376. ZDSecMgrMasterKeyData[index].ami = INVALID_NODE_ADDR;
  377. }
  378. // check if preconfigured keys are enabled
  379. //-------------------------------------------------------------------------
  380. #if ( ZDSECMGR_PRECONFIG_MAX != 0 )
  381. //-------------------------------------------------------------------------
  382. if ( zgPreConfigKeys == TRUE )
  383. {
  384. // sync configured data
  385. entry.user = ADDRMGR_USER_SECURITY;
  386. for ( index = 0; index < ZDSECMGR_PRECONFIG_MAX; index++ )
  387. {
  388. // check for Address Manager entry
  389. AddrMgrExtAddrSet( entry.extAddr,
  390. (uint8*)ZDSecMgrPreConfigData[index].extAddr );
  391. if ( AddrMgrEntryLookupExt( &entry ) != TRUE )
  392. {
  393. // update Address Manager
  394. AddrMgrEntryUpdate( &entry );
  395. }
  396. if ( entry.index != INVALID_NODE_ADDR )
  397. {
  398. // sync MASTER keys with Address Manager index
  399. ZDSecMgrMasterKeyData[index].ami = entry.index;
  400. osal_memcpy( ZDSecMgrMasterKeyData[index].key,
  401. (void*)ZDSecMgrPreConfigData[index].key, SEC_KEY_LEN );
  402. }
  403. }
  404. }
  405. //-------------------------------------------------------------------------
  406. #endif // ( ZDSECMGR_PRECONFIG_MAX != 0 )
  407. //-------------------------------------------------------------------------
  408. }
  409. }
  410. #endif
  411. /******************************************************************************
  412. * @fn ZDSecMgrAddrStore
  413. *
  414. * @brief Store device addresses.
  415. *
  416. * @param nwkAddr - [in] NWK address
  417. * @param extAddr - [in] EXT address
  418. * @param ami - [out] Address Manager index
  419. *
  420. * @return ZStatus_t
  421. */
  422. ZStatus_t ZDSecMgrAddrStore( 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 ZDSecMgrExtAddrStore
  446. *
  447. * @brief Store 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 ZDSecMgrExtAddrStore( uint16 nwkAddr, uint8* extAddr, uint16* ami )
  455. {
  456. ZStatus_t status;
  457. AddrMgrEntry_t entry;
  458. // add entry
  459. entry.user = ADDRMGR_USER_SECURITY;
  460. entry.nwkAddr = nwkAddr;
  461. AddrMgrExtAddrSet( entry.extAddr, extAddr );
  462. if ( AddrMgrEntryUpdate( &entry ) == TRUE )
  463. {
  464. // return successful results
  465. *ami = entry.index;
  466. status = ZSuccess;
  467. }
  468. else
  469. {
  470. // return failed results
  471. *ami = entry.index;
  472. status = ZNwkUnknownDevice;
  473. }
  474. return status;
  475. }
  476. /******************************************************************************
  477. * @fn ZDSecMgrExtAddrLookup
  478. *
  479. * @brief Lookup index for specified EXT address.
  480. *
  481. * @param extAddr - [in] EXT address
  482. * @param ami - [out] Address Manager index
  483. *
  484. * @return ZStatus_t
  485. */
  486. ZStatus_t ZDSecMgrExtAddrLookup( uint8* extAddr, uint16* ami )
  487. {
  488. ZStatus_t status;
  489. AddrMgrEntry_t entry;
  490. // lookup entry
  491. entry.user = ADDRMGR_USER_SECURITY;
  492. AddrMgrExtAddrSet( entry.extAddr, extAddr );
  493. if ( AddrMgrEntryLookupExt( &entry ) == TRUE )
  494. {
  495. // return successful results
  496. *ami = entry.index;
  497. status = ZSuccess;
  498. }
  499. else
  500. {
  501. // return failed results
  502. *ami = entry.index;
  503. status = ZNwkUnknownDevice;
  504. }
  505. return status;
  506. }
  507. /******************************************************************************
  508. * @fn ZDSecMgrMasterKeyLookup
  509. *
  510. * @brief Lookup MASTER key for specified address index.
  511. *
  512. * @param ami - [in] Address Manager index
  513. * @param key - [out] valid MASTER key
  514. *
  515. * @return ZStatus_t
  516. */
  517. ZStatus_t ZDSecMgrMasterKeyLookup( uint16 ami, uint8** key )
  518. {
  519. ZStatus_t status;
  520. uint16 index;
  521. // initialize results
  522. *key = NULL;
  523. status = ZNwkUnknownDevice;
  524. // verify data is available
  525. if ( ZDSecMgrMasterKeyData != NULL )
  526. {
  527. for ( index = 0; index < ZDSECMGR_MASTERKEY_MAX ; index++ )
  528. {
  529. if ( ZDSecMgrMasterKeyData[index].ami == ami )
  530. {
  531. // return successful results
  532. *key = ZDSecMgrMasterKeyData[index].key;
  533. status = ZSuccess;
  534. // break from loop
  535. index = ZDSECMGR_MASTERKEY_MAX;
  536. }
  537. }
  538. }
  539. return status;
  540. }
  541. /******************************************************************************
  542. * @fn ZDSecMgrMasterKeyStore
  543. *
  544. * @brief Store MASTER key for specified address index.
  545. *
  546. * @param ami - [in] Address Manager index
  547. * @param key - [in] valid key to store
  548. *
  549. * @return ZStatus_t
  550. */
  551. ZStatus_t ZDSecMgrMasterKeyStore( uint16 ami, uint8* key )
  552. {
  553. ZStatus_t status;
  554. uint16 index;
  555. uint8* entry;
  556. // initialize results
  557. status = ZNwkUnknownDevice;
  558. // verify data is available
  559. if ( ZDSecMgrMasterKeyData != NULL )
  560. {
  561. for ( index = 0; index < ZDSECMGR_MASTERKEY_MAX ; index++ )
  562. {
  563. if ( ZDSecMgrMasterKeyData[index].ami == INVALID_NODE_ADDR )
  564. {
  565. // store EXT address index
  566. ZDSecMgrMasterKeyData[index].ami = ami;
  567. entry = ZDSecMgrMasterKeyData[index].key;
  568. if ( key != NULL )
  569. {
  570. osal_memcpy( entry, key, SEC_KEY_LEN );
  571. }
  572. else
  573. {
  574. osal_memset( entry, 0, SEC_KEY_LEN );
  575. }
  576. // return successful results
  577. status = ZSuccess;
  578. // break from loop
  579. index = ZDSECMGR_MASTERKEY_MAX;
  580. }
  581. }
  582. }
  583. return status;
  584. }
  585. /******************************************************************************
  586. * @fn ZDSecMgrEntryInit
  587. *
  588. * @brief Initialize entry sub module
  589. *
  590. * @param none
  591. *
  592. * @return none
  593. */
  594. void ZDSecMgrEntryInit( void )
  595. {
  596. uint16 size;
  597. uint16 index;
  598. // allocate entry data
  599. size = (short)( sizeof(ZDSecMgrEntry_t) * ZDSECMGR_ENTRY_MAX );
  600. ZDSecMgrEntries = osal_mem_alloc( size );
  601. // initialize data
  602. if ( ZDSecMgrEntries != NULL )
  603. {
  604. for( index = 0; index < ZDSECMGR_ENTRY_MAX; index++ )
  605. {
  606. ZDSecMgrEntries[index].ami = INVALID_NODE_ADDR;
  607. }
  608. }
  609. ZDSecMgrRestoreFromNV();
  610. }
  611. /******************************************************************************
  612. * @fn ZDSecMgrEntryLookup
  613. *
  614. * @brief Lookup entry index using specified NWK address.
  615. *
  616. * @param nwkAddr - [in] NWK address
  617. * @param entry - [out] valid entry
  618. *
  619. * @return ZStatus_t
  620. */
  621. ZStatus_t ZDSecMgrEntryLookup( uint16 nwkAddr, ZDSecMgrEntry_t** entry )
  622. {
  623. ZStatus_t status;
  624. uint16 index;
  625. AddrMgrEntry_t addrMgrEntry;
  626. // initialize results
  627. *entry = NULL;
  628. status = ZNwkUnknownDevice;
  629. // verify data is available
  630. if ( ZDSecMgrEntries != NULL )
  631. {
  632. addrMgrEntry.user = ADDRMGR_USER_SECURITY;
  633. addrMgrEntry.nwkAddr = nwkAddr;
  634. if ( AddrMgrEntryLookupNwk( &addrMgrEntry ) == TRUE )
  635. {
  636. for ( index = 0; index < ZDSECMGR_ENTRY_MAX ; index++ )
  637. {
  638. if ( addrMgrEntry.index == ZDSecMgrEntries[index].ami )
  639. {
  640. // return successful results
  641. *entry = &ZDSecMgrEntries[index];
  642. status = ZSuccess;
  643. // break from loop
  644. index = ZDSECMGR_ENTRY_MAX;
  645. }
  646. }
  647. }
  648. }
  649. return status;
  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. ZStatus_t status;
  664. uint16 index;
  665. // initialize results
  666. *entry = NULL;
  667. status = ZNwkUnknownDevice;
  668. // verify data is available
  669. if ( ZDSecMgrEntries != NULL )
  670. {
  671. for ( index = 0; index < ZDSECMGR_ENTRY_MAX ; index++ )
  672. {
  673. if ( ZDSecMgrEntries[index].ami == ami )
  674. {
  675. // return successful results
  676. *entry = &ZDSecMgrEntries[index];
  677. status = ZSuccess;
  678. // break from loop
  679. index = ZDSECMGR_ENTRY_MAX;
  680. }
  681. }
  682. }
  683. return status;
  684. }
  685. /******************************************************************************
  686. * @fn ZDSecMgrEntryLookupExt
  687. *
  688. * @brief Lookup entry index using specified EXT address.
  689. *
  690. * @param extAddr - [in] EXT address
  691. * @param entry - [out] valid entry
  692. *
  693. * @return ZStatus_t
  694. */
  695. ZStatus_t ZDSecMgrEntryLookupExt( uint8* extAddr, ZDSecMgrEntry_t** entry )
  696. {
  697. ZStatus_t status;
  698. uint16 ami;
  699. // initialize results
  700. *entry = NULL;
  701. status = ZNwkUnknownDevice;
  702. // lookup address index
  703. if ( ZDSecMgrExtAddrLookup( extAddr, &ami ) == ZSuccess )
  704. {
  705. status = ZDSecMgrEntryLookupAMI( ami, entry );
  706. }
  707. return status;
  708. }
  709. /******************************************************************************
  710. * @fn ZDSecMgrEntryFree
  711. *
  712. * @brief Free entry.
  713. *
  714. * @param entry - [in] valid entry
  715. *
  716. * @return ZStatus_t
  717. */
  718. void ZDSecMgrEntryFree( ZDSecMgrEntry_t* entry )
  719. {
  720. entry->ami = INVALID_NODE_ADDR;
  721. }
  722. /******************************************************************************
  723. * @fn ZDSecMgrEntryNew
  724. *
  725. * @brief Get a new entry.
  726. *
  727. * @param entry - [out] valid entry
  728. *
  729. * @return ZStatus_t
  730. */
  731. ZStatus_t ZDSecMgrEntryNew( ZDSecMgrEntry_t** entry )
  732. {
  733. ZStatus_t status;
  734. uint16 index;
  735. // initialize results
  736. *entry = NULL;
  737. status = ZNwkUnknownDevice;
  738. // verify data is available
  739. if ( ZDSecMgrEntries != NULL )
  740. {
  741. // find available entry
  742. for ( index = 0; index < ZDSECMGR_ENTRY_MAX ; index++ )
  743. {
  744. if ( ZDSecMgrEntries[index].ami == INVALID_NODE_ADDR )
  745. {
  746. // return successful result
  747. *entry = &ZDSecMgrEntries[index];
  748. status = ZSuccess;
  749. // Set the authentication option to default
  750. ZDSecMgrEntries[index].authenticateOption = ZDSecMgr_Not_Authenticated;
  751. // break from loop
  752. index = ZDSECMGR_ENTRY_MAX;
  753. }
  754. }
  755. }
  756. return status;
  757. }
  758. /******************************************************************************
  759. * @fn ZDSecMgrCtrlInit
  760. *
  761. * @brief Initialize control sub module
  762. *
  763. * @param none
  764. *
  765. * @return none
  766. */
  767. void ZDSecMgrCtrlInit( void )
  768. {
  769. uint16 size;
  770. uint16 index;
  771. // allocate entry data
  772. size = (short)( sizeof(ZDSecMgrCtrl_t) * ZDSECMGR_CTRL_MAX );
  773. ZDSecMgrCtrlData = osal_mem_alloc( size );
  774. // initialize data
  775. if ( ZDSecMgrCtrlData != NULL )
  776. {
  777. for( index = 0; index < ZDSECMGR_CTRL_MAX; index++ )
  778. {
  779. ZDSecMgrCtrlData[index].state = ZDSECMGR_CTRL_NONE;
  780. }
  781. }
  782. }
  783. /******************************************************************************
  784. * @fn ZDSecMgrCtrlRelease
  785. *
  786. * @brief Release control data.
  787. *
  788. * @param ctrl - [in] valid control data
  789. *
  790. * @return none
  791. */
  792. void ZDSecMgrCtrlRelease( ZDSecMgrCtrl_t* ctrl )
  793. {
  794. // should always be enough entry control data
  795. ctrl->state = ZDSECMGR_CTRL_NONE;
  796. }
  797. /******************************************************************************
  798. * @fn ZDSecMgrCtrlLookup
  799. *
  800. * @brief Lookup control data.
  801. *
  802. * @param entry - [in] valid entry data
  803. * @param ctrl - [out] control data - NULL if not found
  804. *
  805. * @return none
  806. */
  807. void ZDSecMgrCtrlLookup( ZDSecMgrEntry_t* entry, ZDSecMgrCtrl_t** ctrl )
  808. {
  809. uint16 index;
  810. // initialize search results
  811. *ctrl = NULL;
  812. // verify data is available
  813. if ( ZDSecMgrCtrlData != NULL )
  814. {
  815. for ( index = 0; index < ZDSECMGR_CTRL_MAX; index++ )
  816. {
  817. // make sure control data is in use
  818. if ( ZDSecMgrCtrlData[index].state != ZDSECMGR_CTRL_NONE )
  819. {
  820. // check for entry match
  821. if ( ZDSecMgrCtrlData[index].entry == entry )
  822. {
  823. // return this control data
  824. *ctrl = &ZDSecMgrCtrlData[index];
  825. // break from loop
  826. index = ZDSECMGR_CTRL_MAX;
  827. }
  828. }
  829. }
  830. }
  831. }
  832. /******************************************************************************
  833. * @fn ZDSecMgrCtrlSet
  834. *
  835. * @brief Set control data.
  836. *
  837. * @param device - [in] valid device data
  838. * @param entry - [in] valid entry data
  839. * @param ctrl - [in] valid control data
  840. *
  841. * @return none
  842. */
  843. void ZDSecMgrCtrlSet( ZDSecMgrDevice_t* device,
  844. ZDSecMgrEntry_t* entry,
  845. ZDSecMgrCtrl_t* ctrl )
  846. {
  847. // set control date
  848. ctrl->parentAddr = device->parentAddr;
  849. ctrl->secure = device->secure;
  850. ctrl->entry = entry;
  851. ctrl->state = ZDSECMGR_CTRL_INIT;
  852. ctrl->cntr = 0;
  853. // set device pointer
  854. device->ctrl = ctrl;
  855. }
  856. /******************************************************************************
  857. * @fn ZDSecMgrCtrlAdd
  858. *
  859. * @brief Add control data.
  860. *
  861. * @param device - [in] valid device data
  862. * @param entry - [in] valid entry data
  863. *
  864. * @return ZStatus_t
  865. */
  866. ZStatus_t ZDSecMgrCtrlAdd( ZDSecMgrDevice_t* device, ZDSecMgrEntry_t* entry )
  867. {
  868. ZStatus_t status;
  869. uint16 index;
  870. // initialize results
  871. status = ZNwkUnknownDevice;
  872. // verify data is available
  873. if ( ZDSecMgrCtrlData != NULL )
  874. {
  875. // look for an empty slot
  876. for ( index = 0; index < ZDSECMGR_CTRL_MAX; index++ )
  877. {
  878. if ( ZDSecMgrCtrlData[index].state == ZDSECMGR_CTRL_NONE )
  879. {
  880. // return successful results
  881. ZDSecMgrCtrlSet( device, entry, &ZDSecMgrCtrlData[index] );
  882. status = ZSuccess;
  883. // break from loop
  884. index = ZDSECMGR_CTRL_MAX;
  885. }
  886. }
  887. }
  888. return status;
  889. }
  890. /******************************************************************************
  891. * @fn ZDSecMgrCtrlTerm
  892. *
  893. * @brief Terminate device control.
  894. *
  895. * @param entry - [in] valid entry data
  896. *
  897. * @return none
  898. */
  899. void ZDSecMgrCtrlTerm( ZDSecMgrEntry_t* entry )
  900. {
  901. ZDSecMgrCtrl_t* ctrl;
  902. // remove device from control data
  903. ZDSecMgrCtrlLookup ( entry, &ctrl );
  904. if ( ctrl != NULL )
  905. {
  906. ZDSecMgrCtrlRelease ( ctrl );
  907. }
  908. }
  909. /******************************************************************************
  910. * @fn ZDSecMgrCtrlReset
  911. *
  912. * @brief Reset control data.
  913. *
  914. * @param device - [in] valid device data
  915. * @param entry - [in] valid entry data
  916. *
  917. * @return ZStatus_t
  918. */
  919. ZStatus_t ZDSecMgrCtrlReset( ZDSecMgrDevice_t* device, ZDSecMgrEntry_t* entry )
  920. {
  921. ZStatus_t status;
  922. ZDSecMgrCtrl_t* ctrl;
  923. // initialize results
  924. status = ZNwkUnknownDevice;
  925. // look for a match for the entry
  926. ZDSecMgrCtrlLookup( entry, &ctrl );
  927. if ( ctrl != NULL )
  928. {
  929. ZDSecMgrCtrlSet( device, entry, ctrl );
  930. status = ZSuccess;
  931. }
  932. else
  933. {
  934. status = ZDSecMgrCtrlAdd( device, entry );
  935. }
  936. return status;
  937. }
  938. /******************************************************************************
  939. * @fn ZDSecMgrMasterKeyLoad
  940. *
  941. * @brief Load the MASTER key for device with specified EXT
  942. * address.
  943. *
  944. * @param extAddr - [in] EXT address of device
  945. * @param key - [in] MASTER key shared with device
  946. *
  947. * @return ZStatus_t
  948. */
  949. ZStatus_t ZDSecMgrMasterKeyLoad( uint8* extAddr, uint8* key )
  950. {
  951. ZStatus_t status;
  952. uint8* loaded;
  953. uint16 ami;
  954. // set status based on policy
  955. status = ZDSecMgrExtAddrLookup( extAddr, &ami );
  956. if ( status == ZSuccess )
  957. {
  958. // get the address index
  959. if ( ZDSecMgrMasterKeyLookup( ami, &loaded ) == ZSuccess )
  960. {
  961. // overwrite old key
  962. osal_memcpy( loaded, key, SEC_KEY_LEN );
  963. }
  964. else
  965. {
  966. // store new key -- NULL will zero key
  967. status = ZDSecMgrMasterKeyStore( ami, key );
  968. }
  969. }
  970. return status;
  971. }
  972. /******************************************************************************
  973. * @fn ZDSecMgrAppKeyGet
  974. *
  975. * @brief get an APP key - option APP(MASTER or LINK) key
  976. *
  977. * @param initNwkAddr - [in] NWK address of initiator device
  978. * @param initExtAddr - [in] EXT address of initiator device
  979. * @param partNwkAddr - [in] NWK address of partner device
  980. * @param partExtAddr - [in] EXT address of partner device
  981. * @param key - [out] APP(MASTER or LINK) key
  982. * @param keyType - [out] APP(MASTER or LINK) key type
  983. *
  984. * @return ZStatus_t
  985. */
  986. uint8 ZDSecMgrAppKeyType = KEY_TYPE_APP_LINK; // Set the default key type
  987. // to KEY_TYPE_APP_LINK since
  988. // only specific requirement
  989. // right now comes from SE profile
  990. ZStatus_t ZDSecMgrAppKeyGet( uint16 initNwkAddr,
  991. uint8* initExtAddr,
  992. uint16 partNwkAddr,
  993. uint8* partExtAddr,
  994. uint8** key,
  995. uint8* keyType )
  996. {
  997. // Intentionally unreferenced parameters
  998. (void)initNwkAddr;
  999. (void)initExtAddr;
  1000. (void)partNwkAddr;
  1001. (void)partExtAddr;
  1002. //---------------------------------------------------------------------------
  1003. // note:
  1004. // should use a robust mechanism to generate keys, for example
  1005. // combine EXT addresses and call a hash function
  1006. //---------------------------------------------------------------------------
  1007. SSP_GetTrueRand( SEC_KEY_LEN, *key );
  1008. *keyType = ZDSecMgrAppKeyType;
  1009. return ZSuccess;
  1010. }
  1011. /******************************************************************************
  1012. * @fn ZDSecMgrAppKeyReq
  1013. *
  1014. * @brief Process request for APP key between two devices.
  1015. *
  1016. * @param device - [in] ZDO_RequestKeyInd_t, request info
  1017. *
  1018. * @return none
  1019. */
  1020. void ZDSecMgrAppKeyReq( ZDO_RequestKeyInd_t* ind )
  1021. {
  1022. APSME_TransportKeyReq_t req;
  1023. uint8 initExtAddr[Z_EXTADDR_LEN];
  1024. uint16 partNwkAddr;
  1025. uint8 key[SEC_KEY_LEN];
  1026. // validate initiator and partner
  1027. if ( ( APSME_LookupNwkAddr( ind->partExtAddr, &partNwkAddr ) == TRUE ) &&
  1028. ( APSME_LookupExtAddr( ind->srcAddr, initExtAddr ) == TRUE ) )
  1029. {
  1030. // point the key to some memory
  1031. req.key = key;
  1032. // get an APP key - option APP (MASTER or LINK) key
  1033. if ( ZDSecMgrAppKeyGet( ind->srcAddr,
  1034. initExtAddr,
  1035. partNwkAddr,
  1036. ind->partExtAddr,
  1037. &req.key,
  1038. &req.keyType ) == ZSuccess )
  1039. {
  1040. // always secure
  1041. req.nwkSecure = TRUE;
  1042. req.apsSecure = TRUE;
  1043. req.tunnel = NULL;
  1044. // send key to initiator device
  1045. req.dstAddr = ind->srcAddr;
  1046. req.extAddr = ind->partExtAddr;
  1047. req.initiator = TRUE;
  1048. APSME_TransportKeyReq( &req );
  1049. // send key to partner device
  1050. req.dstAddr = partNwkAddr;
  1051. req.extAddr = initExtAddr;
  1052. req.initiator = FALSE;
  1053. APSME_TransportKeyReq( &req );
  1054. }
  1055. }
  1056. }
  1057. /******************************************************************************
  1058. * @fn ZDSecMgrEstablishKey
  1059. *
  1060. * @brief Start SKKE with device joining network.
  1061. *
  1062. * @param device - [in] ZDSecMgrDevice_t, device info
  1063. *
  1064. * @return ZStatus_t
  1065. */
  1066. ZStatus_t ZDSecMgrEstablishKey( ZDSecMgrDevice_t* device )
  1067. {
  1068. ZStatus_t status;
  1069. APSME_EstablishKeyReq_t req;
  1070. req.respExtAddr = device->extAddr;
  1071. req.method = APSME_SKKE_METHOD;
  1072. if ( device->parentAddr == NLME_GetShortAddr() )
  1073. {
  1074. req.dstAddr = device->nwkAddr;
  1075. //devtag.0604.todo - remove obsolete
  1076. req.apsSecure = FALSE;
  1077. req.nwkSecure = FALSE;
  1078. }
  1079. else
  1080. {
  1081. req.dstAddr = device->parentAddr;
  1082. //devtag.0604.todo - remove obsolete
  1083. req.apsSecure = TRUE;
  1084. req.nwkSecure = TRUE;
  1085. }
  1086. status = APSME_EstablishKeyReq( &req );
  1087. return status;
  1088. }
  1089. /******************************************************************************
  1090. * @fn ZDSecMgrSendMasterKey
  1091. *
  1092. * @brief Send MASTER key to device joining network.
  1093. *
  1094. * @param device - [in] ZDSecMgrDevice_t, device info
  1095. *
  1096. * @return ZStatus_t
  1097. */
  1098. ZStatus_t ZDSecMgrSendMasterKey( ZDSecMgrDevice_t* device )
  1099. {
  1100. ZStatus_t status;
  1101. APSME_TransportKeyReq_t req;
  1102. req.keyType = KEY_TYPE_TC_MASTER;
  1103. req.extAddr = device->extAddr;
  1104. req.tunnel = NULL;
  1105. ZDSecMgrMasterKeyLookup( device->ctrl->entry->ami, &req.key );
  1106. //check if using secure hop to to parent
  1107. if ( device->parentAddr != NLME_GetShortAddr() )
  1108. {
  1109. //send to parent with security
  1110. req.dstAddr = device->parentAddr;
  1111. req.nwkSecure = TRUE;
  1112. req.apsSecure = TRUE;
  1113. }
  1114. else
  1115. {
  1116. //direct with no security
  1117. req.dstAddr = device->nwkAddr;
  1118. req.nwkSecure = FALSE;
  1119. req.apsSecure = FALSE;
  1120. }
  1121. status = APSME_TransportKeyReq( &req );
  1122. return status;
  1123. }
  1124. /******************************************************************************
  1125. * @fn ZDSecMgrSendNwkKey
  1126. *
  1127. * @brief Send NWK key to device joining network.
  1128. *
  1129. * @param device - [in] ZDSecMgrDevice_t, device info
  1130. *
  1131. * @return ZStatus_t
  1132. */
  1133. ZStatus_t ZDSecMgrSendNwkKey( ZDSecMgrDevice_t* device )
  1134. {
  1135. ZStatus_t status;
  1136. APSME_TransportKeyReq_t req;
  1137. APSDE_FrameTunnel_t tunnel;
  1138. req.dstAddr = device->nwkAddr;
  1139. req.extAddr = device->extAddr;
  1140. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  1141. req.keyType = KEY_TYPE_NWK_HIGH;
  1142. else
  1143. req.keyType = KEY_TYPE_NWK;
  1144. if ( (ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH)
  1145. || (ZG_CHECK_SECURITY_MODE == ZG_SECURITY_SE_STANDARD) )
  1146. {
  1147. // set values
  1148. req.keySeqNum = _NIB.nwkActiveKey.keySeqNum;
  1149. req.key = _NIB.nwkActiveKey.key;
  1150. //devtag.pro.security.todo - make sure that if there is no link key the NWK
  1151. //key isn't used to secure the frame at the APS layer -- since the receiving
  1152. //device may not have a NWK key yet
  1153. req.apsSecure = TRUE;
  1154. // check if using secure hop to to parent
  1155. if ( device->parentAddr == NLME_GetShortAddr() )
  1156. {
  1157. req.nwkSecure = FALSE;
  1158. req.tunnel = NULL;
  1159. }
  1160. else
  1161. {
  1162. req.nwkSecure = TRUE;
  1163. req.tunnel = &tunnel;
  1164. req.tunnel->tna = device->parentAddr;
  1165. req.tunnel->dea = device->extAddr;
  1166. }
  1167. }
  1168. else
  1169. {
  1170. // default values
  1171. //devtag.0604.verify
  1172. req.nwkSecure = TRUE;
  1173. req.apsSecure = FALSE;
  1174. req.tunnel = NULL;
  1175. if ( device->parentAddr != NLME_GetShortAddr() )
  1176. {
  1177. req.dstAddr = device->parentAddr;
  1178. }
  1179. // special cases
  1180. //devtag.0604.todo - modify to preconfig flag
  1181. if ( device->secure == FALSE )
  1182. {
  1183. req.keySeqNum = _NIB.nwkActiveKey.keySeqNum;
  1184. req.key = _NIB.nwkActiveKey.key;
  1185. // check if using secure hop to to parent
  1186. if ( device->parentAddr == NLME_GetShortAddr() )
  1187. {
  1188. req.nwkSecure = FALSE;
  1189. }
  1190. }
  1191. else
  1192. {
  1193. req.key = NULL;
  1194. req.keySeqNum = 0;
  1195. }
  1196. }
  1197. status = APSME_TransportKeyReq( &req );
  1198. return status;
  1199. }
  1200. /******************************************************************************
  1201. * @fn ZDSecMgrDeviceEntryRemove
  1202. *
  1203. * @brief Remove device entry.
  1204. *
  1205. * @param entry - [in] valid entry
  1206. *
  1207. * @return none
  1208. */
  1209. void ZDSecMgrDeviceEntryRemove( ZDSecMgrEntry_t* entry )
  1210. {
  1211. // terminate device control
  1212. if ( ( ZG_BUILD_COORDINATOR_TYPE ) && ( ZG_DEVICE_COORDINATOR_TYPE ) )
  1213. {
  1214. ZDSecMgrCtrlTerm( entry );
  1215. }
  1216. // remove device from entry data
  1217. ZDSecMgrEntryFree( entry );
  1218. // remove EXT address
  1219. //ZDSecMgrExtAddrRelease( aiOld );
  1220. }
  1221. /******************************************************************************
  1222. * @fn ZDSecMgrDeviceEntryAdd
  1223. *
  1224. * @brief Add entry.
  1225. *
  1226. * @param device - [in] ZDSecMgrDevice_t, device info
  1227. * @param ami - [in] Address Manager index
  1228. *
  1229. * @return ZStatus_t
  1230. */
  1231. void ZDSecMgrAddrMgrUpdate( uint16 ami, uint16 nwkAddr )
  1232. {
  1233. AddrMgrEntry_t entry;
  1234. // get the ami data
  1235. entry.user = ADDRMGR_USER_SECURITY;
  1236. entry.index = ami;
  1237. AddrMgrEntryGet( &entry );
  1238. // check if NWK address is same
  1239. if ( entry.nwkAddr != nwkAddr )
  1240. {
  1241. // update NWK address
  1242. entry.nwkAddr = nwkAddr;
  1243. AddrMgrEntryUpdate( &entry );
  1244. }
  1245. }
  1246. ZStatus_t ZDSecMgrDeviceEntryAdd( ZDSecMgrDevice_t* device, uint16 ami )
  1247. {
  1248. ZStatus_t status;
  1249. ZDSecMgrEntry_t* entry;
  1250. // initialize as unknown until completion
  1251. status = ZNwkUnknownDevice;
  1252. device->ctrl = NULL;
  1253. // make sure not already registered
  1254. if ( ZDSecMgrEntryLookup( device->nwkAddr, &entry ) == ZSuccess )
  1255. {
  1256. // verify that address index is same
  1257. if ( entry->ami != ami )
  1258. {
  1259. // remove conflicting entry
  1260. ZDSecMgrDeviceEntryRemove( entry );
  1261. if ( ZDSecMgrEntryLookupAMI( ami, &entry ) == ZSuccess )
  1262. {
  1263. // update NWK address
  1264. ZDSecMgrAddrMgrUpdate( ami, device->nwkAddr );
  1265. }
  1266. }
  1267. }
  1268. else if ( ZDSecMgrEntryLookupAMI( ami, &entry ) == ZSuccess )
  1269. {
  1270. // update NWK address
  1271. ZDSecMgrAddrMgrUpdate( ami, device->nwkAddr );
  1272. }
  1273. // check if a new entry needs to be created
  1274. if ( entry == NULL )
  1275. {
  1276. // get new entry
  1277. if ( ZDSecMgrEntryNew( &entry ) == ZSuccess )
  1278. {
  1279. // reset entry lkd
  1280. // finish setting up entry
  1281. entry->ami = ami;
  1282. // update NWK address
  1283. ZDSecMgrAddrMgrUpdate( ami, device->nwkAddr );
  1284. // enter new device into device control
  1285. if ( ( ZG_BUILD_COORDINATOR_TYPE ) && ( ZG_DEVICE_COORDINATOR_TYPE ) )
  1286. {
  1287. status = ZDSecMgrCtrlAdd( device, entry );
  1288. }
  1289. else
  1290. {
  1291. status = ZSuccess;
  1292. }
  1293. }
  1294. }
  1295. else
  1296. {
  1297. // reset entry lkd
  1298. // reset entry in entry control
  1299. if ( ( ZG_BUILD_COORDINATOR_TYPE ) && ( ZG_DEVICE_COORDINATOR_TYPE ) )
  1300. {
  1301. status = ZDSecMgrCtrlReset( device, entry );
  1302. }
  1303. else
  1304. {
  1305. status = ZSuccess;
  1306. }
  1307. }
  1308. return status;
  1309. }
  1310. /******************************************************************************
  1311. * @fn ZDSecMgrDeviceCtrlHandler
  1312. *
  1313. * @brief Device control handler.
  1314. *
  1315. * @param device - [in] ZDSecMgrDevice_t, device info
  1316. *
  1317. * @return none
  1318. */
  1319. void ZDSecMgrDeviceCtrlHandler( ZDSecMgrDevice_t* device )
  1320. {
  1321. uint8 state;
  1322. uint8 cntr;
  1323. state = device->ctrl->state;
  1324. cntr = ZDSECMGR_CTRL_BASE_CNTR;
  1325. switch ( state )
  1326. {
  1327. case ZDSECMGR_CTRL_TK_MASTER:
  1328. if ( ZDSecMgrSendMasterKey( device ) == ZSuccess )
  1329. {
  1330. state = ZDSECMGR_CTRL_SKKE_INIT;
  1331. cntr = ZDSECMGR_CTRL_SKKE_INIT_CNTR;
  1332. }
  1333. break;
  1334. case ZDSECMGR_CTRL_SKKE_INIT:
  1335. if ( ZDSecMgrEstablishKey( device ) == ZSuccess )
  1336. {
  1337. state = ZDSECMGR_CTRL_SKKE_WAIT;
  1338. }
  1339. break;
  1340. case ZDSECMGR_CTRL_SKKE_WAIT:
  1341. // continue to wait for SKA control timeout
  1342. break;
  1343. case ZDSECMGR_CTRL_TK_NWK:
  1344. if ( ZDSecMgrSendNwkKey( device ) == ZSuccess )
  1345. {
  1346. state = ZDSECMGR_CTRL_NONE;
  1347. }
  1348. break;
  1349. default:
  1350. state = ZDSECMGR_CTRL_NONE;
  1351. break;
  1352. }
  1353. if ( state != ZDSECMGR_CTRL_NONE )
  1354. {
  1355. device->ctrl->state = state;
  1356. device->ctrl->cntr = cntr;
  1357. osal_start_timerEx(ZDAppTaskID, ZDO_SECMGR_EVENT, 100 );
  1358. }
  1359. else
  1360. {
  1361. ZDSecMgrCtrlRelease( device->ctrl );
  1362. }
  1363. }
  1364. /******************************************************************************
  1365. * @fn ZDSecMgrDeviceCtrlSetup
  1366. *
  1367. * @brief Setup device control.
  1368. *
  1369. * @param device - [in] ZDSecMgrDevice_t, device info
  1370. *
  1371. * @return ZStatus_t
  1372. */
  1373. void ZDSecMgrDeviceCtrlSetup( ZDSecMgrDevice_t* device )
  1374. {
  1375. if ( device->ctrl != NULL )
  1376. {
  1377. if ( device->secure == FALSE )
  1378. {
  1379. // send the master key data to the joining device
  1380. device->ctrl->state = ZDSECMGR_CTRL_TK_MASTER;
  1381. }
  1382. else
  1383. {
  1384. // start SKKE
  1385. device->ctrl->state = ZDSECMGR_CTRL_SKKE_INIT;
  1386. }
  1387. ZDSecMgrDeviceCtrlHandler( device );
  1388. }
  1389. }
  1390. /******************************************************************************
  1391. * @fn ZDSecMgrDeviceCtrlUpdate
  1392. *
  1393. * @brief Update control data.
  1394. *
  1395. * @param extAddr - [in] EXT address
  1396. * @param state - [in] new control state
  1397. *
  1398. * @return none
  1399. */
  1400. void ZDSecMgrDeviceCtrlUpdate( uint8* extAddr, uint8 state )
  1401. {
  1402. ZDSecMgrEntry_t* entry;
  1403. ZDSecMgrCtrl_t* ctrl;
  1404. // lookup device entry data
  1405. ZDSecMgrEntryLookupExt( extAddr, &entry );
  1406. if ( entry != NULL )
  1407. {
  1408. // lookup device control data
  1409. ZDSecMgrCtrlLookup( entry, &ctrl );
  1410. // make sure control data is valid
  1411. if ( ctrl != NULL )
  1412. {
  1413. // possible state transitions
  1414. if ( ctrl->state == ZDSECMGR_CTRL_SKKE_WAIT )
  1415. {
  1416. if ( state == ZDSECMGR_CTRL_SKKE_DONE )
  1417. {
  1418. // send the network key
  1419. ctrl->state = ZDSECMGR_CTRL_TK_NWK;
  1420. ctrl->cntr = ZDSECMGR_CTRL_TK_NWK_CNTR;
  1421. }
  1422. else if ( state == ZDSECMGR_CTRL_SKKE_FAIL )
  1423. {
  1424. // force default timeout in order to cleanup control logic
  1425. ctrl->state = ZDSECMGR_CTRL_SKKE_FAIL;
  1426. ctrl->cntr = ZDSECMGR_CTRL_BASE_CNTR;
  1427. }
  1428. }
  1429. // timer should be active
  1430. }
  1431. }
  1432. }
  1433. void APSME_SKA_TimerExpired( uint8 initiator, uint8* partExtAddr );
  1434. void APSME_SKA_TimerExpired( uint8 initiator, uint8* partExtAddr )
  1435. {
  1436. if ( ( ZG_BUILD_COORDINATOR_TYPE ) && ( ZG_DEVICE_COORDINATOR_TYPE ) )
  1437. {
  1438. if ( initiator == TRUE )
  1439. {
  1440. ZDSecMgrDeviceCtrlUpdate( partExtAddr, ZDSECMGR_CTRL_SKKE_FAIL );
  1441. }
  1442. }
  1443. }
  1444. /******************************************************************************
  1445. * @fn ZDSecMgrDeviceRemove
  1446. *
  1447. * @brief Remove device from network.
  1448. *
  1449. * @param device - [in] ZDSecMgrDevice_t, device info
  1450. *
  1451. * @return none
  1452. */
  1453. void ZDSecMgrDeviceRemove( ZDSecMgrDevice_t* device )
  1454. {
  1455. APSME_RemoveDeviceReq_t remDevReq;
  1456. NLME_LeaveReq_t leaveReq;
  1457. associated_devices_t* assoc;
  1458. // check if parent, remove the device
  1459. if ( device->parentAddr == NLME_GetShortAddr() )
  1460. {
  1461. // this is the parent of the device
  1462. leaveReq.extAddr = device->extAddr;
  1463. leaveReq.removeChildren = FALSE;
  1464. leaveReq.rejoin = FALSE;
  1465. // find child association
  1466. assoc = AssocGetWithExt( device->extAddr );
  1467. if ( ( assoc != NULL ) &&
  1468. ( assoc->nodeRelation >= CHILD_RFD ) &&
  1469. ( assoc->nodeRelation <= CHILD_FFD_RX_IDLE ) )
  1470. {
  1471. // check if associated device is authenticated
  1472. if ( assoc->devStatus & DEV_SEC_AUTH_STATUS )
  1473. {
  1474. leaveReq.silent = FALSE;
  1475. }
  1476. else
  1477. {
  1478. leaveReq.silent = TRUE;
  1479. }
  1480. NLME_LeaveReq( &leaveReq );
  1481. }
  1482. }
  1483. else
  1484. {
  1485. // this is not the parent of the device
  1486. remDevReq.parentAddr = device->parentAddr;
  1487. remDevReq.childExtAddr = device->extAddr;
  1488. APSME_RemoveDeviceReq( &remDevReq );
  1489. }
  1490. }
  1491. /******************************************************************************
  1492. * @fn ZDSecMgrDeviceValidateSKKE
  1493. *
  1494. * @brief Decide whether device is allowed for SKKE.
  1495. *
  1496. * @param device - [in] ZDSecMgrDevice_t, device info
  1497. *
  1498. * @return ZStatus_t
  1499. */
  1500. ZStatus_t ZDSecMgrDeviceValidateSKKE( ZDSecMgrDevice_t* device )
  1501. {
  1502. ZStatus_t status;
  1503. uint16 ami;
  1504. uint8* key;
  1505. // get EXT address
  1506. status = ZDSecMgrExtAddrLookup( device->extAddr, &ami );
  1507. if ( status == ZSuccess )
  1508. {
  1509. // get MASTER key
  1510. status = ZDSecMgrMasterKeyLookup( ami, &key );
  1511. if ( status == ZSuccess )
  1512. {
  1513. // // check if initiator is Trust Center
  1514. // if ( device->nwkAddr == APSME_TRUSTCENTER_NWKADDR )
  1515. // {
  1516. // // verify NWK key not sent
  1517. // // devtag.todo
  1518. // // temporary - add device to internal data
  1519. // status = ZDSecMgrDeviceEntryAdd( device, ami );
  1520. // }
  1521. // else
  1522. // {
  1523. // // initiator not Trust Center - End to End SKKE - set policy
  1524. // // for accepting an SKKE initiation
  1525. // // temporary - add device to internal data
  1526. // status = ZDSecMgrDeviceEntryAdd( device, ami );
  1527. // }
  1528. status = ZDSecMgrDeviceEntryAdd( device, ami );
  1529. }
  1530. }
  1531. return status;
  1532. }
  1533. /******************************************************************************
  1534. * @fn ZDSecMgrDeviceValidateRM (RESIDENTIAL MODE)
  1535. *
  1536. * @brief Decide whether device is allowed.
  1537. *
  1538. * @param device - [in] ZDSecMgrDevice_t, device info
  1539. *
  1540. * @return ZStatus_t
  1541. */
  1542. ZStatus_t ZDSecMgrDeviceValidateRM( ZDSecMgrDevice_t* device )
  1543. {
  1544. ZStatus_t status;
  1545. status = ZSuccess;
  1546. (void)device; // Intentionally unreferenced parameter
  1547. // For test purpose, turning off the zgSecurePermitJoin flag will force
  1548. // the trust center to reject any newly joining devices by sending
  1549. // Remove-device to the parents.
  1550. if ( zgSecurePermitJoin == false )
  1551. {
  1552. status = ZNwkUnknownDevice;
  1553. }
  1554. #if 0 // Taken out because the following functionality is only used for test
  1555. // purpose. A more efficient (above) way is used. It can be put
  1556. // back in if customers request for a white/black list feature.
  1557. // ZDSecMgrStoredDeviceList[] is defined in ZDSecMgr.c
  1558. // The following code processes the device black list (stored device list)
  1559. // If the joining device is not part of the forbidden device list
  1560. // Return ZSuccess. Otherwise, return ZNwkUnknownDevice. The trust center
  1561. // will send Remove-device and ban the device from joining.
  1562. uint8 index;
  1563. uint8* restricted;
  1564. // Look through the stored device list - used for restricted devices
  1565. for ( index = 0; index < ZDSECMGR_STORED_DEVICES; index++ )
  1566. {
  1567. restricted = ZDSecMgrStoredDeviceList[index];
  1568. if ( AddrMgrExtAddrEqual( restricted, device->extAddr ) == TRUE )
  1569. {
  1570. // return as unknown device in regards to validation
  1571. status = ZNwkUnknownDevice;
  1572. // break from loop
  1573. index = ZDSECMGR_STORED_DEVICES;
  1574. }
  1575. }
  1576. #endif
  1577. return status;
  1578. }
  1579. /******************************************************************************
  1580. * @fn ZDSecMgrDeviceValidateCM (COMMERCIAL MODE)
  1581. *
  1582. * @brief Decide whether device is allowed.
  1583. *
  1584. * @param device - [in] ZDSecMgrDevice_t, device info
  1585. *
  1586. * @return ZStatus_t
  1587. */
  1588. //devtag.pro.security
  1589. ZStatus_t ZDSecMgrDeviceValidateCM( ZDSecMgrDevice_t* device )
  1590. {
  1591. ZStatus_t status;
  1592. uint16 ami;
  1593. uint8* key;
  1594. // // check for pre configured setting
  1595. // if ( device->secure == TRUE )
  1596. // {
  1597. // // get EXT address and MASTER key
  1598. // status = ZDSecMgrExtAddrLookup( device->extAddr, &ami );
  1599. //
  1600. // if ( status == ZSuccess )
  1601. // {
  1602. // status = ZDSecMgrMasterKeyLookup( ami, &key );
  1603. // }
  1604. // }
  1605. // else
  1606. // {
  1607. // implement EXT address and MASTER key policy here -- the total number of
  1608. // Security Manager entries should never exceed the number of EXT addresses
  1609. // and MASTER keys available
  1610. // set status based on policy
  1611. //status = ZNwkUnknownDevice;
  1612. // set status based on policy
  1613. status = ZSuccess; // ZNwkUnknownDevice;
  1614. // get key based on policy
  1615. key = ZDSecMgrTCMasterKey;
  1616. // if policy, store new EXT address
  1617. status = ZDSecMgrAddrStore( device->nwkAddr, device->extAddr, &ami );
  1618. // set the key
  1619. ZDSecMgrMasterKeyLoad( device->extAddr, key );
  1620. // }
  1621. // if EXT address and MASTER key available -- add device
  1622. if ( status == ZSuccess )
  1623. {
  1624. // add device to internal data - with control
  1625. status = ZDSecMgrDeviceEntryAdd( device, ami );
  1626. }
  1627. return status;
  1628. }
  1629. //devtag.pro.security
  1630. #if 0
  1631. ZStatus_t ZDSecMgrDeviceValidateCM( ZDSecMgrDevice_t* device )
  1632. {
  1633. ZStatus_t status;
  1634. uint16 ami;
  1635. uint8* key;
  1636. // check for pre configured setting
  1637. if ( device->secure == TRUE )
  1638. {
  1639. // get EXT address and MASTER key
  1640. status = ZDSecMgrExtAddrLookup( device->extAddr, &ami );
  1641. if ( status == ZSuccess )
  1642. {
  1643. status = ZDSecMgrMasterKeyLookup( ami, &key );
  1644. }
  1645. }
  1646. else
  1647. {
  1648. // implement EXT address and MASTER key policy here -- the total number of
  1649. // Security Manager entries should never exceed the number of EXT addresses
  1650. // and MASTER keys available
  1651. // set status based on policy
  1652. status = ZSuccess; // ZNwkUnknownDevice;
  1653. // get the address index
  1654. if ( ZDSecMgrExtAddrLookup( device->extAddr, &ami ) != ZSuccess )
  1655. {
  1656. // if policy, store new EXT address
  1657. status = ZDSecMgrAddrStore( device->nwkAddr, device->extAddr, &ami );
  1658. }
  1659. // get the address index
  1660. if ( ZDSecMgrMasterKeyLookup( ami, &key ) != ZSuccess )
  1661. {
  1662. // if policy, store new key -- NULL will zero key
  1663. status = ZDSecMgrMasterKeyStore( ami, NULL );
  1664. }
  1665. }
  1666. // if EXT address and MASTER key available -- add device
  1667. if ( status == ZSuccess )
  1668. {
  1669. // add device to internal data - with control
  1670. status = ZDSecMgrDeviceEntryAdd( device, ami );
  1671. }
  1672. return status;
  1673. }
  1674. #endif
  1675. /******************************************************************************
  1676. * @fn ZDSecMgrDeviceValidate
  1677. *
  1678. * @brief Decide whether device is allowed.
  1679. *
  1680. * @param device - [in] ZDSecMgrDevice_t, device info
  1681. *
  1682. * @return ZStatus_t
  1683. */
  1684. ZStatus_t ZDSecMgrDeviceValidate( ZDSecMgrDevice_t* device )
  1685. {
  1686. ZStatus_t status;
  1687. if ( ZDSecMgrPermitJoiningEnabled == TRUE )
  1688. {
  1689. // device may be joining with a secure flag but it is ultimately the Trust
  1690. // Center that decides -- check if expected pre configured device --
  1691. // override settings
  1692. if ( zgPreConfigKeys == TRUE )
  1693. {
  1694. device->secure = TRUE;
  1695. }
  1696. else
  1697. {
  1698. device->secure = FALSE;
  1699. }
  1700. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  1701. {
  1702. status = ZDSecMgrDeviceValidateCM( device );
  1703. }
  1704. else // ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_RESIDENTIAL )
  1705. {
  1706. status = ZDSecMgrDeviceValidateRM( device );
  1707. }
  1708. }
  1709. else
  1710. {
  1711. status = ZNwkUnknownDevice;
  1712. }
  1713. return status;
  1714. }
  1715. /******************************************************************************
  1716. * @fn ZDSecMgrDeviceJoin
  1717. *
  1718. * @brief Try to join this device.
  1719. *
  1720. * @param device - [in] ZDSecMgrDevice_t, device info
  1721. *
  1722. * @return ZStatus_t
  1723. */
  1724. ZStatus_t ZDSecMgrDeviceJoin( ZDSecMgrDevice_t* device )
  1725. {
  1726. ZStatus_t status;
  1727. // attempt to validate device
  1728. status = ZDSecMgrDeviceValidate( device );
  1729. if ( status == ZSuccess )
  1730. {
  1731. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  1732. {
  1733. ZDSecMgrDeviceCtrlSetup( device );
  1734. }
  1735. else // ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_RESIDENTIAL )
  1736. {
  1737. //send the nwk key data to the joining device
  1738. status = ZDSecMgrSendNwkKey( device );
  1739. }
  1740. }
  1741. else
  1742. {
  1743. // not allowed, remove the device
  1744. ZDSecMgrDeviceRemove( device );
  1745. }
  1746. return status;
  1747. }
  1748. /******************************************************************************
  1749. * @fn ZDSecMgrDeviceJoinDirect
  1750. *
  1751. * @brief Try to join this device as a direct child.
  1752. *
  1753. * @param device - [in] ZDSecMgrDevice_t, device info
  1754. *
  1755. * @return ZStatus_t
  1756. */
  1757. ZStatus_t ZDSecMgrDeviceJoinDirect( ZDSecMgrDevice_t* device )
  1758. {
  1759. ZStatus_t status;
  1760. status = ZDSecMgrDeviceJoin( device );
  1761. if ( status == ZSuccess )
  1762. {
  1763. // set association status to authenticated
  1764. ZDSecMgrAssocDeviceAuth( AssocGetWithShort( device->nwkAddr ) );
  1765. }
  1766. return status;
  1767. }
  1768. /******************************************************************************
  1769. * @fn ZDSecMgrDeviceJoinFwd
  1770. *
  1771. * @brief Forward join to Trust Center.
  1772. *
  1773. * @param device - [in] ZDSecMgrDevice_t, device info
  1774. *
  1775. * @return ZStatus_t
  1776. */
  1777. ZStatus_t ZDSecMgrDeviceJoinFwd( ZDSecMgrDevice_t* device )
  1778. {
  1779. ZStatus_t status;
  1780. APSME_UpdateDeviceReq_t req;
  1781. // forward any joining device to the Trust Center -- the Trust Center will
  1782. // decide if the device is allowed to join
  1783. status = ZSuccess;
  1784. // forward authorization to the Trust Center
  1785. req.dstAddr = APSME_TRUSTCENTER_NWKADDR;
  1786. req.devAddr = device->nwkAddr;
  1787. req.devExtAddr = device->extAddr;
  1788. // set security status, option for router to reject if policy set
  1789. if ( (device->devStatus & DEV_HIGH_SEC_STATUS) )
  1790. {
  1791. if ( device->devStatus & DEV_REJOIN_STATUS )
  1792. {
  1793. if ( device->secure == TRUE )
  1794. req.status = APSME_UD_HIGH_SECURED_REJOIN;
  1795. else
  1796. req.status = APSME_UD_HIGH_UNSECURED_REJOIN;
  1797. }
  1798. else
  1799. req.status = APSME_UD_HIGH_UNSECURED_JOIN;
  1800. }
  1801. else
  1802. {
  1803. if ( device->devStatus & DEV_REJOIN_STATUS )
  1804. {
  1805. if ( device->secure == TRUE )
  1806. req.status = APSME_UD_STANDARD_SECURED_REJOIN;
  1807. else
  1808. req.status = APSME_UD_STANDARD_UNSECURED_REJOIN;
  1809. }
  1810. else
  1811. req.status = APSME_UD_STANDARD_UNSECURED_JOIN;
  1812. }
  1813. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  1814. req.apsSecure = TRUE;
  1815. else
  1816. req.apsSecure = FALSE;
  1817. // send and APSME_UPDATE_DEVICE request to the trust center
  1818. status = APSME_UpdateDeviceReq( &req );
  1819. return status;
  1820. }
  1821. /******************************************************************************
  1822. * @fn ZDSecMgrDeviceNew
  1823. *
  1824. * @brief Process a new device.
  1825. *
  1826. * @param device - [in] ZDSecMgrDevice_t, device info
  1827. *
  1828. * @return ZStatus_t
  1829. */
  1830. ZStatus_t ZDSecMgrDeviceNew( ZDSecMgrDevice_t* joiner )
  1831. {
  1832. ZStatus_t status;
  1833. if ( ( ZG_BUILD_COORDINATOR_TYPE ) && ( ZG_DEVICE_COORDINATOR_TYPE ) )
  1834. {
  1835. // try to join this device
  1836. status = ZDSecMgrDeviceJoinDirect( joiner );
  1837. }
  1838. else
  1839. {
  1840. status = ZDSecMgrDeviceJoinFwd( joiner );
  1841. }
  1842. return status;
  1843. }
  1844. /******************************************************************************
  1845. * @fn ZDSecMgrAssocDeviceAuth
  1846. *
  1847. * @brief Set associated device status to authenticated
  1848. *
  1849. * @param assoc - [in, out] associated_devices_t
  1850. *
  1851. * @return none
  1852. */
  1853. void ZDSecMgrAssocDeviceAuth( associated_devices_t* assoc )
  1854. {
  1855. if ( assoc != NULL )
  1856. {
  1857. assoc->devStatus |= DEV_SEC_AUTH_STATUS;
  1858. }
  1859. }
  1860. /******************************************************************************
  1861. * @fn ZDSecMgrAuthInitiate
  1862. *
  1863. * @brief Initiate entity authentication
  1864. *
  1865. * @param responder - [in] responder EXT address
  1866. *
  1867. * @return none
  1868. */
  1869. void ZDSecMgrAuthInitiate( uint8* responder )
  1870. {
  1871. APSME_AuthenticateReq_t req;
  1872. // make sure NWK address is available
  1873. if ( APSME_LookupNwkAddr( responder, &req.nwkAddr ) )
  1874. {
  1875. // set request fields
  1876. req.extAddr = responder;
  1877. req.action = APSME_EA_INITIATE;
  1878. req.challenge = NULL;
  1879. // start EA processing
  1880. APSME_AuthenticateReq( &req );
  1881. }
  1882. }
  1883. /******************************************************************************
  1884. * @fn ZDSecMgrAuthNwkKey
  1885. *
  1886. * @brief Handle next step in authentication process
  1887. *
  1888. * @param none
  1889. *
  1890. * @return none
  1891. */
  1892. void ZDSecMgrAuthNwkKey()
  1893. {
  1894. if ( devState == DEV_END_DEVICE_UNAUTH )
  1895. {
  1896. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  1897. {
  1898. uint8 parent[Z_EXTADDR_LEN];
  1899. // get parent's EXT address
  1900. NLME_GetCoordExtAddr( parent );
  1901. // begin entity authentication with parent
  1902. ZDSecMgrAuthInitiate( parent );
  1903. }
  1904. else // ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_RESIDENTIAL )
  1905. {
  1906. // inform ZDO that device has been authenticated
  1907. osal_set_event ( ZDAppTaskID, ZDO_DEVICE_AUTH );
  1908. }
  1909. }
  1910. }
  1911. /******************************************************************************
  1912. * PUBLIC FUNCTIONS
  1913. */
  1914. /******************************************************************************
  1915. * @fn ZDSecMgrInit
  1916. *
  1917. * @brief Initialize ZigBee Device Security Manager.
  1918. *
  1919. * @param none
  1920. *
  1921. * @return none
  1922. */
  1923. #if ( ADDRMGR_CALLBACK_ENABLED == 1 )
  1924. void ZDSecMgrAddrMgrCB( uint8 update, AddrMgrEntry_t* newEntry, AddrMgrEntry_t* oldEntry );
  1925. void ZDSecMgrAddrMgrCB( uint8 update,
  1926. AddrMgrEntry_t* newEntry,
  1927. AddrMgrEntry_t* oldEntry )
  1928. {
  1929. (void)update;
  1930. (void)newEntry;
  1931. (void)oldEntry;
  1932. }
  1933. #endif // ( ADDRMGR_CALLBACK_ENABLED == 1 )
  1934. void ZDSecMgrInit( void )
  1935. {
  1936. if ( (ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH)
  1937. || (ZG_CHECK_SECURITY_MODE == ZG_SECURITY_SE_STANDARD) )
  1938. {
  1939. // initialize sub modules
  1940. ZDSecMgrMasterKeyInit();
  1941. ZDSecMgrEntryInit();
  1942. if ( ( ZG_BUILD_COORDINATOR_TYPE ) && ( ZG_DEVICE_COORDINATOR_TYPE ) )
  1943. {
  1944. ZDSecMgrCtrlInit();
  1945. }
  1946. // register with Address Manager
  1947. #if ( ADDRMGR_CALLBACK_ENABLED == 1 )
  1948. AddrMgrRegister( ADDRMGR_REG_SECURITY, ZDSecMgrAddrMgrCB );
  1949. #endif
  1950. }
  1951. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  1952. {
  1953. // configure SKA slot data
  1954. APSME_SKA_SlotInit( ZDSECMGR_SKA_SLOT_MAX );
  1955. }
  1956. else if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_SE_STANDARD )
  1957. {
  1958. // Setup the preconfig Trust Center Link Key
  1959. TrustCenterLinkKey.key = zgPreConfigTCLinkKey;
  1960. TrustCenterLinkKey.txFrmCntr = 0;
  1961. TrustCenterLinkKey.rxFrmCntr = 0;
  1962. #if defined ( NV_RESTORE )
  1963. if ( osal_nv_item_init( ZCD_NV_SECURE_TCLINKKEY_TXFRAME, sizeof(uint32), &(TrustCenterLinkKey.txFrmCntr) ) == ZSUCCESS )
  1964. {
  1965. osal_nv_read( ZCD_NV_SECURE_TCLINKKEY_TXFRAME, 0, sizeof(uint32), &(TrustCenterLinkKey.txFrmCntr) );
  1966. }
  1967. if ( osal_nv_item_init( ZCD_NV_SECURE_TCLINKKEY_RXFRAME, sizeof(uint32), &(TrustCenterLinkKey.rxFrmCntr) ) == ZSUCCESS )
  1968. {
  1969. osal_nv_read( ZCD_NV_SECURE_TCLINKKEY_RXFRAME, 0, sizeof(uint32), &(TrustCenterLinkKey.rxFrmCntr) );
  1970. }
  1971. #endif
  1972. APSME_TCLinkKeySetup( 0x0000, &TrustCenterLinkKey );
  1973. }
  1974. if ( ZG_SECURE_ENABLED )
  1975. {
  1976. if ( ( ZG_BUILD_COORDINATOR_TYPE ) && ( ZG_DEVICE_COORDINATOR_TYPE ) )
  1977. {
  1978. // setup joining permissions
  1979. ZDSecMgrPermitJoiningEnabled = TRUE;
  1980. ZDSecMgrPermitJoiningTimed = FALSE;
  1981. }
  1982. }
  1983. // configure security based on security mode and type of device
  1984. ZDSecMgrConfig();
  1985. }
  1986. /******************************************************************************
  1987. * @fn ZDSecMgrConfig
  1988. *
  1989. * @brief Configure ZigBee Device Security Manager.
  1990. *
  1991. * @param none
  1992. *
  1993. * @return none
  1994. */
  1995. void ZDSecMgrConfig( void )
  1996. {
  1997. if ( ZG_SECURE_ENABLED )
  1998. {
  1999. SSP_Init();
  2000. if ( (ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH)
  2001. || (ZG_CHECK_SECURITY_MODE == ZG_SECURITY_SE_STANDARD) )
  2002. {
  2003. if ( ( ZG_BUILD_COORDINATOR_TYPE ) && ( ZG_DEVICE_COORDINATOR_TYPE ) )
  2004. {
  2005. // COMMERCIAL MODE - COORDINATOR DEVICE
  2006. APSME_SecurityCM_CD();
  2007. }
  2008. else if ( ZSTACK_ROUTER_BUILD )
  2009. {
  2010. // COMMERCIAL MODE - ROUTER DEVICE
  2011. APSME_SecurityCM_RD();
  2012. }
  2013. else
  2014. {
  2015. // COMMERCIAL MODE - END DEVICE
  2016. APSME_SecurityCM_ED();
  2017. }
  2018. }
  2019. else // ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_RESIDENTIAL )
  2020. {
  2021. if ( ( ZG_BUILD_COORDINATOR_TYPE ) && ( ZG_DEVICE_COORDINATOR_TYPE ) )
  2022. {
  2023. // RESIDENTIAL MODE - COORDINATOR DEVICE
  2024. APSME_SecurityRM_CD();
  2025. }
  2026. else if ( ZSTACK_ROUTER_BUILD )
  2027. {
  2028. // RESIDENTIAL MODE - ROUTER DEVICE
  2029. APSME_SecurityRM_RD();
  2030. }
  2031. else
  2032. {
  2033. // RESIDENTIAL MODE - END DEVICE
  2034. APSME_SecurityRM_ED();
  2035. }
  2036. }
  2037. }
  2038. else
  2039. {
  2040. // NO SECURITY
  2041. APSME_SecurityNM();
  2042. }
  2043. }
  2044. /******************************************************************************
  2045. * @fn ZDSecMgrPermitJoining
  2046. *
  2047. * @brief Process request to change joining permissions.
  2048. *
  2049. * @param duration - [in] timed duration for join in seconds
  2050. * - 0x00 not allowed
  2051. * - 0xFF allowed without timeout
  2052. *
  2053. * @return uint8 - success(TRUE:FALSE)
  2054. */
  2055. uint8 ZDSecMgrPermitJoining( uint8 duration )
  2056. {
  2057. uint8 accept;
  2058. ZDSecMgrPermitJoiningTimed = FALSE;
  2059. if ( duration > 0 )
  2060. {
  2061. ZDSecMgrPermitJoiningEnabled = TRUE;
  2062. if ( duration != 0xFF )
  2063. {
  2064. ZDSecMgrPermitJoiningTimed = TRUE;
  2065. }
  2066. }
  2067. else
  2068. {
  2069. ZDSecMgrPermitJoiningEnabled = FALSE;
  2070. }
  2071. accept = TRUE;
  2072. return accept;
  2073. }
  2074. /******************************************************************************
  2075. * @fn ZDSecMgrPermitJoiningTimeout
  2076. *
  2077. * @brief Process permit joining timeout
  2078. *
  2079. * @param none
  2080. *
  2081. * @return none
  2082. */
  2083. void ZDSecMgrPermitJoiningTimeout( void )
  2084. {
  2085. if ( ZDSecMgrPermitJoiningTimed == TRUE )
  2086. {
  2087. ZDSecMgrPermitJoiningEnabled = FALSE;
  2088. ZDSecMgrPermitJoiningTimed = FALSE;
  2089. }
  2090. }
  2091. /******************************************************************************
  2092. * @fn ZDSecMgrNewDeviceEvent
  2093. *
  2094. * @brief Process a the new device event, if found reset new device
  2095. * event/timer.
  2096. *
  2097. * @param none
  2098. *
  2099. * @return uint8 - found(TRUE:FALSE)
  2100. */
  2101. uint8 ZDSecMgrNewDeviceEvent( void )
  2102. {
  2103. uint8 found;
  2104. ZDSecMgrDevice_t device;
  2105. AddrMgrEntry_t addrEntry;
  2106. associated_devices_t* assoc;
  2107. ZStatus_t status;
  2108. // initialize return results
  2109. found = FALSE;
  2110. // look for device in the security init state
  2111. assoc = AssocMatchDeviceStatus( DEV_SEC_INIT_STATUS );
  2112. if ( assoc != NULL )
  2113. {
  2114. // device found
  2115. found = TRUE;
  2116. // check for preconfigured security
  2117. if ( zgPreConfigKeys == TRUE )
  2118. {
  2119. // set association status to authenticated
  2120. ZDSecMgrAssocDeviceAuth( assoc );
  2121. }
  2122. // set up device info
  2123. addrEntry.user = ADDRMGR_USER_DEFAULT;
  2124. addrEntry.index = assoc->addrIdx;
  2125. AddrMgrEntryGet( &addrEntry );
  2126. device.nwkAddr = assoc->shortAddr;
  2127. device.extAddr = addrEntry.extAddr;
  2128. device.parentAddr = NLME_GetShortAddr();
  2129. device.secure = FALSE;
  2130. device.devStatus = assoc->devStatus;
  2131. // process new device
  2132. status = ZDSecMgrDeviceNew( &device );
  2133. if ( status == ZSuccess )
  2134. {
  2135. assoc->devStatus &= ~DEV_SEC_INIT_STATUS;
  2136. }
  2137. else if ( status == ZNwkUnknownDevice )
  2138. {
  2139. AssocRemove( addrEntry.extAddr );
  2140. }
  2141. }
  2142. return found;
  2143. }
  2144. /******************************************************************************
  2145. * @fn ZDSecMgrEvent
  2146. *
  2147. * @brief Handle ZDO Security Manager event/timer(ZDO_SECMGR_EVENT).
  2148. *
  2149. * @param none
  2150. *
  2151. * @return none
  2152. */
  2153. void ZDSecMgrEvent( void )
  2154. {
  2155. uint8 action;
  2156. uint8 restart;
  2157. uint16 index;
  2158. AddrMgrEntry_t entry;
  2159. ZDSecMgrDevice_t device;
  2160. // verify data is available
  2161. if ( ZDSecMgrCtrlData != NULL )
  2162. {
  2163. action = FALSE;
  2164. restart = FALSE;
  2165. // update all the counters
  2166. for ( index = 0; index < ZDSECMGR_ENTRY_MAX; index++ )
  2167. {
  2168. if ( ZDSecMgrCtrlData[index].state != ZDSECMGR_CTRL_NONE )
  2169. {
  2170. if ( ZDSecMgrCtrlData[index].cntr != 0 )
  2171. {
  2172. ZDSecMgrCtrlData[index].cntr--;
  2173. }
  2174. if ( ( action == FALSE ) && ( ZDSecMgrCtrlData[index].cntr == 0 ) )
  2175. {
  2176. action = TRUE;
  2177. // update from control data
  2178. device.parentAddr = ZDSecMgrCtrlData[index].parentAddr;
  2179. device.secure = ZDSecMgrCtrlData[index].secure;
  2180. device.ctrl = &ZDSecMgrCtrlData[index];
  2181. // set the user and address index
  2182. entry.user = ADDRMGR_USER_SECURITY;
  2183. entry.index = ZDSecMgrCtrlData[index].entry->ami;
  2184. // get the address data
  2185. AddrMgrEntryGet( &entry );
  2186. // set device address data
  2187. device.nwkAddr = entry.nwkAddr;
  2188. device.extAddr = entry.extAddr;
  2189. // update from entry data
  2190. ZDSecMgrDeviceCtrlHandler( &device );
  2191. }
  2192. else
  2193. {
  2194. restart = TRUE;
  2195. }
  2196. }
  2197. }
  2198. // check for timer restart
  2199. if ( restart == TRUE )
  2200. {
  2201. osal_start_timerEx(ZDAppTaskID, ZDO_SECMGR_EVENT, 100 );
  2202. }
  2203. }
  2204. }
  2205. /******************************************************************************
  2206. * @fn ZDSecMgrEstablishKeyCfm
  2207. *
  2208. * @brief Process the ZDO_EstablishKeyCfm_t message.
  2209. *
  2210. * @param cfm - [in] ZDO_EstablishKeyCfm_t confirmation
  2211. *
  2212. * @return none
  2213. */
  2214. void ZDSecMgrEstablishKeyCfm( ZDO_EstablishKeyCfm_t* cfm )
  2215. {
  2216. // send the NWK key
  2217. if ( ( ZG_BUILD_COORDINATOR_TYPE ) && ( ZG_DEVICE_COORDINATOR_TYPE ) )
  2218. {
  2219. // update control for specified EXT address
  2220. ZDSecMgrDeviceCtrlUpdate( cfm->partExtAddr, ZDSECMGR_CTRL_SKKE_DONE );
  2221. }
  2222. else
  2223. {
  2224. // this should be done when receiving the NWK key
  2225. // if devState ==
  2226. //if ( devState == DEV_END_DEVICE_UNAUTH )
  2227. //osal_set_event( ZDAppTaskID, ZDO_DEVICE_AUTH );
  2228. // if not in joining state -- this should trigger an event for an
  2229. // end point that requested SKKE
  2230. // if ( devState == DEV_END_DEVICE )
  2231. // devState == DEV_ROUTER;
  2232. }
  2233. }
  2234. uint8 ZDSecMgrTCExtAddrCheck( uint8* extAddr );
  2235. uint8 ZDSecMgrTCExtAddrCheck( uint8* extAddr )
  2236. {
  2237. uint8 match;
  2238. uint8 lookup[Z_EXTADDR_LEN];
  2239. match = FALSE;
  2240. if ( AddrMgrExtAddrLookup( APSME_TRUSTCENTER_NWKADDR, lookup ) )
  2241. {
  2242. match = AddrMgrExtAddrEqual( lookup, extAddr );
  2243. }
  2244. return match;
  2245. }
  2246. void ZDSecMgrTCDataLoad( uint8* extAddr );
  2247. void ZDSecMgrTCDataLoad( uint8* extAddr )
  2248. {
  2249. uint16 ami;
  2250. uint8* key;
  2251. if ( !ZDSecMgrTCDataLoaded )
  2252. {
  2253. if ( ZDSecMgrAddrStore( APSME_TRUSTCENTER_NWKADDR, extAddr, &ami ) == ZSuccess )
  2254. {
  2255. // if preconfigured load key
  2256. if ( zgPreConfigKeys == TRUE )
  2257. {
  2258. if ( ZDSecMgrMasterKeyLookup( ami, &key ) != ZSuccess )
  2259. {
  2260. ZDSecMgrMasterKeyStore( ami, ZDSecMgrTCMasterKey );
  2261. }
  2262. }
  2263. }
  2264. ZDSecMgrTCDataLoaded = TRUE;
  2265. }
  2266. }
  2267. /******************************************************************************
  2268. * @fn ZDSecMgrEstablishKeyInd
  2269. *
  2270. * @brief Process the ZDO_EstablishKeyInd_t message.
  2271. *
  2272. * @param ind - [in] ZDO_EstablishKeyInd_t indication
  2273. *
  2274. * @return none
  2275. */
  2276. void ZDSecMgrEstablishKeyInd( ZDO_EstablishKeyInd_t* ind )
  2277. {
  2278. ZDSecMgrDevice_t device;
  2279. APSME_EstablishKeyRsp_t rsp;
  2280. // load Trust Center data if needed
  2281. ZDSecMgrTCDataLoad( ind->initExtAddr );
  2282. if ( ZDSecMgrTCExtAddrCheck( ind->initExtAddr ) )
  2283. {
  2284. //IF (ind->srcAddr == APSME_TRUSTCENTER_NWKADDR)
  2285. //OR
  2286. //!ZDSecMgrTCAuthenticated
  2287. //devtag.0604.critical
  2288. //how is the parentAddr used here
  2289. // initial SKKE from Trust Center via parent
  2290. device.nwkAddr = APSME_TRUSTCENTER_NWKADDR;
  2291. device.parentAddr = ind->srcAddr;
  2292. }
  2293. else
  2294. {
  2295. // Trust Center direct or E2E SKKE
  2296. device.nwkAddr = ind->srcAddr;
  2297. device.parentAddr = INVALID_NODE_ADDR;
  2298. }
  2299. device.extAddr = ind->initExtAddr;
  2300. //devtag.pro.security.0724.todo - verify usage
  2301. device.secure = ind->nwkSecure;
  2302. // validate device for SKKE
  2303. if ( ZDSecMgrDeviceValidateSKKE( &device ) == ZSuccess )
  2304. {
  2305. rsp.accept = TRUE;
  2306. }
  2307. else
  2308. {
  2309. rsp.accept = FALSE;
  2310. }
  2311. rsp.dstAddr = ind->srcAddr;
  2312. rsp.initExtAddr = &ind->initExtAddr[0];
  2313. //devtag.0604.todo - remove obsolete
  2314. rsp.apsSecure = ind->apsSecure;
  2315. rsp.nwkSecure = ind->nwkSecure;
  2316. APSME_EstablishKeyRsp( &rsp );
  2317. }
  2318. //devtag.pro.security
  2319. #if 0
  2320. void ZDSecMgrEstablishKeyInd( ZDO_EstablishKeyInd_t* ind )
  2321. {
  2322. ZDSecMgrDevice_t device;
  2323. APSME_EstablishKeyRsp_t rsp;
  2324. device.extAddr = ind->initExtAddr;
  2325. device.secure = ind->secure;
  2326. if ( ind->secure == FALSE )
  2327. {
  2328. // SKKE from Trust Center is not secured between child and parent
  2329. device.nwkAddr = APSME_TRUSTCENTER_NWKADDR;
  2330. device.parentAddr = ind->srcAddr;
  2331. }
  2332. else
  2333. {
  2334. // SKKE from initiator should be secured
  2335. device.nwkAddr = ind->srcAddr;
  2336. device.parentAddr = INVALID_NODE_ADDR;
  2337. }
  2338. rsp.dstAddr = ind->srcAddr;
  2339. rsp.initExtAddr = &ind->initExtAddr[0];
  2340. rsp.secure = ind->secure;
  2341. // validate device for SKKE
  2342. if ( ZDSecMgrDeviceValidateSKKE( &device ) == ZSuccess )
  2343. {
  2344. rsp.accept = TRUE;
  2345. }
  2346. else
  2347. {
  2348. rsp.accept = FALSE;
  2349. }
  2350. APSME_EstablishKeyRsp( &rsp );
  2351. }
  2352. #endif
  2353. /******************************************************************************
  2354. * @fn ZDSecMgrTransportKeyInd
  2355. *
  2356. * @brief Process the ZDO_TransportKeyInd_t message.
  2357. *
  2358. * @param ind - [in] ZDO_TransportKeyInd_t indication
  2359. *
  2360. * @return none
  2361. */
  2362. void ZDSecMgrTransportKeyInd( ZDO_TransportKeyInd_t* ind )
  2363. {
  2364. uint8 index;
  2365. // load Trust Center data if needed
  2366. ZDSecMgrTCDataLoad( ind->srcExtAddr );
  2367. if ( ind->keyType == KEY_TYPE_TC_MASTER )
  2368. {
  2369. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  2370. //ZDSecMgrTCMasterKey( ind );
  2371. {
  2372. if ( zgPreConfigKeys != TRUE )
  2373. {
  2374. // devtag.pro.security.todo - check if Trust Center address is configured and correct
  2375. ZDSecMgrMasterKeyLoad( ind->srcExtAddr, ind->key );
  2376. }
  2377. else
  2378. {
  2379. // error condition - reject key
  2380. }
  2381. }
  2382. }
  2383. else if ( ( ind->keyType == KEY_TYPE_NWK ) ||
  2384. ( ind->keyType == 6 ) ||
  2385. ( ind->keyType == KEY_TYPE_NWK_HIGH ) )
  2386. {
  2387. // check for dummy NWK key (all zeros)
  2388. for ( index = 0;
  2389. ( (index < SEC_KEY_LEN) && (ind->key[index] == 0) );
  2390. index++ );
  2391. if ( index == SEC_KEY_LEN )
  2392. {
  2393. // load preconfigured key - once!!
  2394. if ( !_NIB.nwkKeyLoaded )
  2395. {
  2396. SSP_UpdateNwkKey( (byte*)zgPreConfigKey, 0 );
  2397. SSP_SwitchNwkKey( 0 );
  2398. }
  2399. }
  2400. else
  2401. {
  2402. SSP_UpdateNwkKey( ind->key, ind->keySeqNum );
  2403. if ( !_NIB.nwkKeyLoaded )
  2404. {
  2405. SSP_SwitchNwkKey( ind->keySeqNum );
  2406. }
  2407. }
  2408. // handle next step in authentication process
  2409. ZDSecMgrAuthNwkKey();
  2410. }
  2411. else if ( ind->keyType == KEY_TYPE_TC_LINK )
  2412. {
  2413. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  2414. {
  2415. //ZDSecMgrTCLinkKey( ind );
  2416. }
  2417. }
  2418. else if ( ind->keyType == KEY_TYPE_APP_MASTER )
  2419. {
  2420. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  2421. {
  2422. uint16 ami;
  2423. AddrMgrEntry_t entry;
  2424. ZDSecMgrEntry_t* entryZD;
  2425. ZDSecMgrExtAddrLookup( ind->srcExtAddr, &ami );
  2426. if ( ind->initiator == TRUE )
  2427. {
  2428. // get the ami data
  2429. entry.user = ADDRMGR_USER_SECURITY;
  2430. entry.index = ami;
  2431. AddrMgrEntryGet( &entry );
  2432. if ( entry.nwkAddr != INVALID_NODE_ADDR )
  2433. {
  2434. APSME_EstablishKeyReq_t req;
  2435. ZDSecMgrMasterKeyLoad( ind->srcExtAddr, ind->key );
  2436. ZDSecMgrEntryLookupAMI( ami, &entryZD );
  2437. if ( entryZD == NULL )
  2438. {
  2439. // get new entry
  2440. if ( ZDSecMgrEntryNew( &entryZD ) == ZSuccess )
  2441. {
  2442. // finish setting up entry
  2443. entryZD->ami = ami;
  2444. }
  2445. }
  2446. req.respExtAddr = ind->srcExtAddr;
  2447. req.method = APSME_SKKE_METHOD;
  2448. req.dstAddr = entry.nwkAddr;
  2449. //devtag.0604.todo - remove obsolete
  2450. req.apsSecure = FALSE;
  2451. req.nwkSecure = TRUE;
  2452. APSME_EstablishKeyReq( &req );
  2453. }
  2454. }
  2455. else
  2456. {
  2457. if ( ami == INVALID_NODE_ADDR )
  2458. {
  2459. // store new EXT address
  2460. ZDSecMgrAddrStore( INVALID_NODE_ADDR, ind->srcExtAddr, &ami );
  2461. }
  2462. ZDSecMgrMasterKeyLoad( ind->srcExtAddr, ind->key );
  2463. }
  2464. //if ( entry.nwkAddr == INVALID_NODE_ADDR )
  2465. //{
  2466. // ZDP_NwkAddrReq( ind->srcExtAddr, ZDP_ADDR_REQTYPE_SINGLE, 0, 0 );
  2467. //}
  2468. }
  2469. }
  2470. else if ( ind->keyType == KEY_TYPE_APP_LINK )
  2471. {
  2472. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  2473. {
  2474. uint16 ami;
  2475. ZDSecMgrEntry_t* entry;
  2476. // get the address index
  2477. if ( ZDSecMgrExtAddrLookup( ind->srcExtAddr, &ami ) != ZSuccess )
  2478. {
  2479. // store new EXT address
  2480. ZDSecMgrAddrStore( INVALID_NODE_ADDR, ind->srcExtAddr, &ami );
  2481. ZDP_NwkAddrReq( ind->srcExtAddr, ZDP_ADDR_REQTYPE_SINGLE, 0, 0 );
  2482. }
  2483. ZDSecMgrEntryLookupAMI( ami, &entry );
  2484. if ( entry == NULL )
  2485. {
  2486. // get new entry
  2487. if ( ZDSecMgrEntryNew( &entry ) == ZSuccess )
  2488. {
  2489. // finish setting up entry
  2490. entry->ami = ami;
  2491. }
  2492. }
  2493. ZDSecMgrLinkKeySet( ind->srcExtAddr, ind->key );
  2494. }
  2495. }
  2496. }
  2497. /******************************************************************************
  2498. * @fn ZDSecMgrUpdateDeviceInd
  2499. *
  2500. * @brief Process the ZDO_UpdateDeviceInd_t message.
  2501. *
  2502. * @param ind - [in] ZDO_UpdateDeviceInd_t indication
  2503. *
  2504. * @return none
  2505. */
  2506. void ZDSecMgrUpdateDeviceInd( ZDO_UpdateDeviceInd_t* ind )
  2507. {
  2508. ZDSecMgrDevice_t device;
  2509. device.nwkAddr = ind->devAddr;
  2510. device.extAddr = ind->devExtAddr;
  2511. device.parentAddr = ind->srcAddr;
  2512. //if ( ( ind->status == APSME_UD_SECURED_JOIN ) ||
  2513. // ( ind->status == APSME_UD_UNSECURED_JOIN ) )
  2514. //{
  2515. // if ( ind->status == APSME_UD_SECURED_JOIN )
  2516. // {
  2517. // device.secure = TRUE;
  2518. // }
  2519. // else
  2520. // {
  2521. // device.secure = FALSE;
  2522. // }
  2523. // try to join this device
  2524. ZDSecMgrDeviceJoin( &device );
  2525. //}
  2526. }
  2527. /******************************************************************************
  2528. * @fn ZDSecMgrRemoveDeviceInd
  2529. *
  2530. * @brief Process the ZDO_RemoveDeviceInd_t message.
  2531. *
  2532. * @param ind - [in] ZDO_RemoveDeviceInd_t indication
  2533. *
  2534. * @return none
  2535. */
  2536. void ZDSecMgrRemoveDeviceInd( ZDO_RemoveDeviceInd_t* ind )
  2537. {
  2538. ZDSecMgrDevice_t device;
  2539. // only accept from Trust Center
  2540. if ( ind->srcAddr == APSME_TRUSTCENTER_NWKADDR )
  2541. {
  2542. // look up NWK address
  2543. if ( APSME_LookupNwkAddr( ind->childExtAddr, &device.nwkAddr ) == TRUE )
  2544. {
  2545. device.parentAddr = NLME_GetShortAddr();
  2546. device.extAddr = ind->childExtAddr;
  2547. // remove device
  2548. ZDSecMgrDeviceRemove( &device );
  2549. }
  2550. }
  2551. }
  2552. /******************************************************************************
  2553. * @fn ZDSecMgrRequestKeyInd
  2554. *
  2555. * @brief Process the ZDO_RequestKeyInd_t message.
  2556. *
  2557. * @param ind - [in] ZDO_RequestKeyInd_t indication
  2558. *
  2559. * @return none
  2560. */
  2561. void ZDSecMgrRequestKeyInd( ZDO_RequestKeyInd_t* ind )
  2562. {
  2563. if ( ind->keyType == KEY_TYPE_NWK )
  2564. {
  2565. }
  2566. else if ( ind->keyType == KEY_TYPE_APP_MASTER )
  2567. {
  2568. ZDSecMgrAppKeyReq( ind );
  2569. }
  2570. else if ( ind->keyType == KEY_TYPE_TC_LINK )
  2571. {
  2572. }
  2573. //else ignore
  2574. }
  2575. /******************************************************************************
  2576. * @fn ZDSecMgrSwitchKeyInd
  2577. *
  2578. * @brief Process the ZDO_SwitchKeyInd_t message.
  2579. *
  2580. * @param ind - [in] ZDO_SwitchKeyInd_t indication
  2581. *
  2582. * @return none
  2583. */
  2584. void ZDSecMgrSwitchKeyInd( ZDO_SwitchKeyInd_t* ind )
  2585. {
  2586. SSP_SwitchNwkKey( ind->keySeqNum );
  2587. // Save if nv
  2588. ZDApp_NVUpdate();
  2589. }
  2590. /******************************************************************************
  2591. * @fn ZDSecMgrAuthenticateInd
  2592. *
  2593. * @brief Process the ZDO_AuthenticateInd_t message.
  2594. *
  2595. * @param ind - [in] ZDO_AuthenticateInd_t indication
  2596. *
  2597. * @return none
  2598. */
  2599. void ZDSecMgrAuthenticateInd( ZDO_AuthenticateInd_t* ind )
  2600. {
  2601. APSME_AuthenticateReq_t req;
  2602. AddrMgrEntry_t entry;
  2603. // update the address manager
  2604. //---------------------------------------------------------------------------
  2605. // note:
  2606. // required for EA processing, but ultimately EA logic could also use the
  2607. // neighbor table to look up addresses -- also(IF using EA) the neighbor
  2608. // table is supposed to have authentication states for neighbors
  2609. //---------------------------------------------------------------------------
  2610. entry.user = ADDRMGR_USER_SECURITY;
  2611. entry.nwkAddr = ind->aps.initNwkAddr;
  2612. AddrMgrExtAddrSet( entry.extAddr, ind->aps.initExtAddr );
  2613. if ( AddrMgrEntryUpdate( &entry ) == TRUE )
  2614. {
  2615. // set request fields
  2616. req.nwkAddr = ind->aps.initNwkAddr;
  2617. req.extAddr = ind->aps.initExtAddr;
  2618. req.action = APSME_EA_ACCEPT;
  2619. req.challenge = ind->aps.challenge;
  2620. // start EA processing
  2621. APSME_AuthenticateReq( &req );
  2622. }
  2623. }
  2624. /******************************************************************************
  2625. * @fn ZDSecMgrAuthenticateCfm
  2626. *
  2627. * @brief Process the ZDO_AuthenticateCfm_t message.
  2628. *
  2629. * @param cfm - [in] ZDO_AuthenticateCfm_t confirmation
  2630. *
  2631. * @return none
  2632. */
  2633. void ZDSecMgrAuthenticateCfm( ZDO_AuthenticateCfm_t* cfm )
  2634. {
  2635. if ( cfm->aps.status == ZSuccess )
  2636. {
  2637. if ( ( cfm->aps.initiator == TRUE ) && ( devState == DEV_END_DEVICE_UNAUTH ) )
  2638. {
  2639. // inform ZDO that device has been authenticated
  2640. osal_set_event ( ZDAppTaskID, ZDO_DEVICE_AUTH );
  2641. }
  2642. }
  2643. }
  2644. #if ( ZG_BUILD_COORDINATOR_TYPE )
  2645. /******************************************************************************
  2646. * @fn ZDSecMgrUpdateNwkKey
  2647. *
  2648. * @brief Load a new NWK key and trigger a network wide update.
  2649. *
  2650. * @param key - [in] new NWK key
  2651. * @param keySeqNum - [in] new NWK key sequence number
  2652. *
  2653. * @return ZStatus_t
  2654. */
  2655. ZStatus_t ZDSecMgrUpdateNwkKey( uint8* key, uint8 keySeqNum, uint16 dstAddr )
  2656. {
  2657. ZStatus_t status;
  2658. APSME_TransportKeyReq_t req;
  2659. // initialize common elements of local variables
  2660. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  2661. req.keyType = KEY_TYPE_NWK_HIGH;
  2662. else
  2663. req.keyType = KEY_TYPE_NWK;
  2664. req.dstAddr = dstAddr;
  2665. req.keySeqNum = keySeqNum;
  2666. req.key = key;
  2667. req.extAddr = NULL;
  2668. req.nwkSecure = TRUE;
  2669. req.apsSecure = TRUE;
  2670. req.tunnel = NULL;
  2671. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  2672. {
  2673. ZDSecMgrEntry_t* entry;
  2674. uint16 index;
  2675. AddrMgrEntry_t addrEntry;
  2676. addrEntry.user = ADDRMGR_USER_SECURITY;
  2677. status = ZFailure;
  2678. // verify data is available
  2679. if ( ZDSecMgrEntries != NULL )
  2680. {
  2681. // find available entry
  2682. for ( index = 0; index < ZDSECMGR_ENTRY_MAX ; index++ )
  2683. {
  2684. if ( ZDSecMgrEntries[index].ami != INVALID_NODE_ADDR )
  2685. {
  2686. // return successful result
  2687. entry = &ZDSecMgrEntries[index];
  2688. // get NWK address
  2689. addrEntry.index = entry->ami;
  2690. if ( AddrMgrEntryGet( &addrEntry ) == TRUE )
  2691. {
  2692. req.dstAddr = addrEntry.nwkAddr;
  2693. req.extAddr = addrEntry.extAddr;
  2694. status = APSME_TransportKeyReq( &req );
  2695. }
  2696. }
  2697. }
  2698. }
  2699. }
  2700. else // ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_RESIDENTIAL )
  2701. {
  2702. status = APSME_TransportKeyReq( &req );
  2703. }
  2704. SSP_UpdateNwkKey( key, keySeqNum );
  2705. // Save if nv
  2706. ZDApp_NVUpdate();
  2707. return status;
  2708. }
  2709. #endif // ( ZG_BUILD_COORDINATOR_TYPE )
  2710. #if ( ZG_BUILD_COORDINATOR_TYPE )
  2711. /******************************************************************************
  2712. * @fn ZDSecMgrSwitchNwkKey
  2713. *
  2714. * @brief Causes the NWK key to switch via a network wide command.
  2715. *
  2716. * @param keySeqNum - [in] new NWK key sequence number
  2717. *
  2718. * @return ZStatus_t
  2719. */
  2720. ZStatus_t ZDSecMgrSwitchNwkKey( uint8 keySeqNum, uint16 dstAddr )
  2721. {
  2722. ZStatus_t status;
  2723. APSME_SwitchKeyReq_t req;
  2724. // initialize common elements of local variables
  2725. req.dstAddr = dstAddr;
  2726. req.keySeqNum = keySeqNum;
  2727. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  2728. {
  2729. ZDSecMgrEntry_t* entry;
  2730. uint16 index;
  2731. AddrMgrEntry_t addrEntry;
  2732. addrEntry.user = ADDRMGR_USER_SECURITY;
  2733. status = ZFailure;
  2734. // verify data is available
  2735. if ( ZDSecMgrEntries != NULL )
  2736. {
  2737. // find available entry
  2738. for ( index = 0; index < ZDSECMGR_ENTRY_MAX ; index++ )
  2739. {
  2740. if ( ZDSecMgrEntries[index].ami != INVALID_NODE_ADDR )
  2741. {
  2742. // return successful result
  2743. entry = &ZDSecMgrEntries[index];
  2744. // get NWK address
  2745. addrEntry.index = entry->ami;
  2746. if ( AddrMgrEntryGet( &addrEntry ) == TRUE )
  2747. {
  2748. req.dstAddr = addrEntry.nwkAddr;
  2749. status = APSME_SwitchKeyReq( &req );
  2750. }
  2751. }
  2752. }
  2753. }
  2754. }
  2755. else // ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_RESIDENTIAL )
  2756. {
  2757. status = APSME_SwitchKeyReq( &req );
  2758. }
  2759. SSP_SwitchNwkKey( keySeqNum );
  2760. // Save if nv
  2761. ZDApp_NVUpdate();
  2762. return status;
  2763. }
  2764. #endif // ( ZG_BUILD_COORDINATOR_TYPE )
  2765. #if ( ZG_BUILD_JOINING_TYPE )
  2766. /******************************************************************************
  2767. * @fn ZDSecMgrRequestAppKey
  2768. *
  2769. * @brief Request an application key with partner.
  2770. *
  2771. * @param partNwkAddr - [in] partner network address
  2772. *
  2773. * @return ZStatus_t
  2774. */
  2775. ZStatus_t ZDSecMgrRequestAppKey( uint16 partNwkAddr )
  2776. {
  2777. ZStatus_t status;
  2778. APSME_RequestKeyReq_t req;
  2779. uint8 partExtAddr[Z_EXTADDR_LEN];
  2780. if ( AddrMgrExtAddrLookup( partNwkAddr, partExtAddr ) )
  2781. {
  2782. req.dstAddr = 0;
  2783. req.keyType = KEY_TYPE_APP_MASTER;
  2784. req.partExtAddr = partExtAddr;
  2785. status = APSME_RequestKeyReq( &req );
  2786. }
  2787. else
  2788. {
  2789. status = ZFailure;
  2790. }
  2791. return status;
  2792. }
  2793. #endif // ( ZG_BUILD_JOINING_TYPE )
  2794. #if ( ZG_BUILD_JOINING_TYPE )
  2795. /******************************************************************************
  2796. * @fn ZDSecMgrSetupPartner
  2797. *
  2798. * @brief Setup for application key partner.
  2799. *
  2800. * @param partNwkAddr - [in] partner network address
  2801. *
  2802. * @return ZStatus_t
  2803. */
  2804. ZStatus_t ZDSecMgrSetupPartner( uint16 partNwkAddr, uint8* partExtAddr )
  2805. {
  2806. AddrMgrEntry_t entry;
  2807. ZStatus_t status;
  2808. status = ZFailure;
  2809. // update the address manager
  2810. entry.user = ADDRMGR_USER_SECURITY;
  2811. entry.nwkAddr = partNwkAddr;
  2812. AddrMgrExtAddrSet( entry.extAddr, partExtAddr );
  2813. if ( AddrMgrEntryUpdate( &entry ) == TRUE )
  2814. {
  2815. status = ZSuccess;
  2816. // check for address discovery
  2817. if ( partNwkAddr == INVALID_NODE_ADDR )
  2818. {
  2819. status = ZDP_NwkAddrReq( partExtAddr, ZDP_ADDR_REQTYPE_SINGLE, 0, 0 );
  2820. }
  2821. else if ( !AddrMgrExtAddrValid( partExtAddr ) )
  2822. {
  2823. status = ZDP_IEEEAddrReq( partNwkAddr, ZDP_ADDR_REQTYPE_SINGLE, 0, 0 );
  2824. }
  2825. }
  2826. return status;
  2827. }
  2828. #endif // ( ZG_BUILD_JOINING_TYPE )
  2829. #if ( ZG_BUILD_COORDINATOR_TYPE )
  2830. /******************************************************************************
  2831. * @fn ZDSecMgrAppKeyTypeSet
  2832. *
  2833. * @brief Set application key type.
  2834. *
  2835. * @param keyType - [in] application key type (KEY_TYPE_APP_MASTER@2 or
  2836. * KEY_TYPE_APP_LINK@3
  2837. *
  2838. * @return ZStatus_t
  2839. */
  2840. ZStatus_t ZDSecMgrAppKeyTypeSet( uint8 keyType )
  2841. {
  2842. if ( keyType == KEY_TYPE_APP_LINK )
  2843. {
  2844. ZDSecMgrAppKeyType = KEY_TYPE_APP_LINK;
  2845. }
  2846. else
  2847. {
  2848. ZDSecMgrAppKeyType = KEY_TYPE_APP_MASTER;
  2849. }
  2850. return ZSuccess;
  2851. }
  2852. #endif
  2853. /******************************************************************************
  2854. * ZigBee Device Security Manager - Stub Implementations
  2855. */
  2856. /******************************************************************************
  2857. * @fn ZDSecMgrMasterKeyGet (stubs APSME_MasterKeyGet)
  2858. *
  2859. * @brief Get MASTER key for specified EXT address.
  2860. *
  2861. * @param extAddr - [in] EXT address
  2862. * @param key - [out] MASTER key
  2863. *
  2864. * @return ZStatus_t
  2865. */
  2866. ZStatus_t ZDSecMgrMasterKeyGet( uint8* extAddr, uint8** key )
  2867. {
  2868. ZStatus_t status;
  2869. uint16 ami;
  2870. // lookup entry for specified EXT address
  2871. status = ZDSecMgrExtAddrLookup( extAddr, &ami );
  2872. //status = ZDSecMgrEntryLookupExt( extAddr, &entry );
  2873. if ( status == ZSuccess )
  2874. {
  2875. ZDSecMgrMasterKeyLookup( ami, key );
  2876. }
  2877. else
  2878. {
  2879. *key = NULL;
  2880. }
  2881. return status;
  2882. }
  2883. /******************************************************************************
  2884. * @fn ZDSecMgrLinkKeySet (stubs APSME_LinkKeySet)
  2885. *
  2886. * @brief Set <APSME_LinkKeyData_t> for specified NWK address.
  2887. *
  2888. * @param extAddr - [in] EXT address
  2889. * @param data - [in] APSME_LinkKeyData_t
  2890. *
  2891. * @return ZStatus_t
  2892. */
  2893. ZStatus_t ZDSecMgrLinkKeySet( uint8* extAddr, uint8* key )
  2894. {
  2895. ZStatus_t status;
  2896. ZDSecMgrEntry_t* entry;
  2897. // lookup entry index for specified EXT address
  2898. status = ZDSecMgrEntryLookupExt( extAddr, &entry );
  2899. if ( status == ZSuccess )
  2900. {
  2901. // setup the link key data reference
  2902. osal_memcpy( entry->lkd.key, key, SEC_KEY_LEN );
  2903. entry->lkd.apsmelkd.rxFrmCntr = 0;
  2904. entry->lkd.apsmelkd.txFrmCntr = 0;
  2905. }
  2906. return status;
  2907. }
  2908. /******************************************************************************
  2909. * @fn ZDSecMgrAuthenticationSet
  2910. *
  2911. * @brief Mark the specific device as authenticated or not
  2912. *
  2913. * @param extAddr - [in] EXT address
  2914. * @param option - [in] authenticated or not
  2915. *
  2916. * @return ZStatus_t
  2917. */
  2918. ZStatus_t ZDSecMgrAuthenticationSet( uint8* extAddr, ZDSecMgr_Authentication_Option option )
  2919. {
  2920. ZStatus_t status;
  2921. ZDSecMgrEntry_t* entry;
  2922. // lookup entry index for specified EXT address
  2923. status = ZDSecMgrEntryLookupExt( extAddr, &entry );
  2924. if ( status == ZSuccess )
  2925. {
  2926. entry->authenticateOption = option;
  2927. }
  2928. return status;
  2929. }
  2930. /******************************************************************************
  2931. * @fn ZDSecMgrAuthenticationCheck
  2932. *
  2933. * @brief Check if the specific device has been authenticated or not
  2934. * For non-trust center device, always return true
  2935. *
  2936. * @param shortAddr - [in] short address
  2937. *
  2938. * @return TRUE @ authenticated with CBKE
  2939. * FALSE @ not authenticated
  2940. */
  2941. uint8 ZDSecMgrAuthenticationCheck( uint16 shortAddr )
  2942. {
  2943. #if defined (SE_PROFILE)
  2944. ZDSecMgrEntry_t* entry;
  2945. uint8 extAddr[Z_EXTADDR_LEN];
  2946. // If the local device is not the trust center, always return TRUE
  2947. if ( NLME_GetShortAddr() != TCshortAddr )
  2948. {
  2949. return TRUE;
  2950. }
  2951. // Otherwise, check the authentication option
  2952. else if ( AddrMgrExtAddrLookup( shortAddr, extAddr ) )
  2953. {
  2954. // lookup entry index for specified EXT address
  2955. if ( ZDSecMgrEntryLookupExt( extAddr, &entry ) == ZSuccess )
  2956. {
  2957. if ( entry->authenticateOption != ZDSecMgr_Not_Authenticated )
  2958. {
  2959. return TRUE;
  2960. }
  2961. else
  2962. {
  2963. return FALSE;
  2964. }
  2965. }
  2966. }
  2967. return FALSE;
  2968. #else
  2969. (void)shortAddr; // Intentionally unreferenced parameter
  2970. // For non AMI/SE Profile, perform no check and always return true.
  2971. return TRUE;
  2972. #endif // SE_PROFILE
  2973. }
  2974. /******************************************************************************
  2975. * @fn ZDSecMgrLinkKeyDataGet (stubs APSME_LinkKeyDataGet)
  2976. *
  2977. * @brief Get <APSME_LinkKeyData_t> for specified NWK address.
  2978. *
  2979. * @param extAddr - [in] EXT address
  2980. * @param data - [out] APSME_LinkKeyData_t
  2981. *
  2982. * @return ZStatus_t
  2983. */
  2984. ZStatus_t ZDSecMgrLinkKeyDataGet(uint8* extAddr, APSME_LinkKeyData_t** data)
  2985. {
  2986. ZStatus_t status;
  2987. ZDSecMgrEntry_t* entry;
  2988. // lookup entry index for specified NWK address
  2989. status = ZDSecMgrEntryLookupExt( extAddr, &entry );
  2990. if ( status == ZSuccess )
  2991. {
  2992. // setup the link key data reference
  2993. (*data) = &entry->lkd.apsmelkd;
  2994. (*data)->key = entry->lkd.key;
  2995. }
  2996. else
  2997. {
  2998. *data = NULL;
  2999. }
  3000. return status;
  3001. }
  3002. /******************************************************************************
  3003. * @fn ZDSecMgrKeyFwdToChild (stubs APSME_KeyFwdToChild)
  3004. *
  3005. * @brief Verify and process key transportation to child.
  3006. *
  3007. * @param ind - [in] APSME_TransportKeyInd_t
  3008. *
  3009. * @return uint8 - success(TRUE:FALSE)
  3010. */
  3011. uint8 ZDSecMgrKeyFwdToChild( APSME_TransportKeyInd_t* ind )
  3012. {
  3013. uint8 success;
  3014. success = FALSE;
  3015. // verify from Trust Center
  3016. if ( ind->srcAddr == APSME_TRUSTCENTER_NWKADDR )
  3017. {
  3018. success = TRUE;
  3019. // check for initial NWK key
  3020. if ( ( ind->keyType == KEY_TYPE_NWK ) ||
  3021. ( ind->keyType == 6 ) ||
  3022. ( ind->keyType == KEY_TYPE_NWK_HIGH ) )
  3023. {
  3024. // set association status to authenticated
  3025. ZDSecMgrAssocDeviceAuth( AssocGetWithExt( ind->dstExtAddr ) );
  3026. }
  3027. }
  3028. return success;
  3029. }
  3030. /******************************************************************************
  3031. * @fn ZDSecMgrAddLinkKey
  3032. *
  3033. * @brief Add the application link key to ZDSecMgr. Also mark the device
  3034. * as authenticated in the authenticateOption. Note that this function
  3035. * is hardwared to CBKE right now.
  3036. *
  3037. * @param shortAddr - short address of the partner device
  3038. * @param extAddr - extended address of the partner device
  3039. * @param key - link key
  3040. *
  3041. * @return none
  3042. */
  3043. void ZDSecMgrAddLinkKey( uint16 shortAddr, uint8 *extAddr, uint8 *key)
  3044. {
  3045. uint16 ami;
  3046. ZDSecMgrEntry_t* entry;
  3047. ZDSecMgrAddrStore( shortAddr, extAddr, &ami );
  3048. ZDSecMgrEntryLookupAMI( ami, &entry );
  3049. // If no existing entry, create one
  3050. if ( entry == NULL )
  3051. {
  3052. if ( ZDSecMgrEntryNew( &entry ) == ZSuccess )
  3053. {
  3054. entry->ami = ami;
  3055. }
  3056. }
  3057. // Write the link key
  3058. APSME_LinkKeySet( extAddr, key );
  3059. #if defined (SE_PROFILE)
  3060. // Mark the device as authenticated.
  3061. ZDSecMgrAuthenticationSet( extAddr, ZDSecMgr_Authenticated_CBCK );
  3062. #endif
  3063. // Write the new established link key to NV.
  3064. ZDSecMgrWriteNV();
  3065. }
  3066. /******************************************************************************
  3067. * @fn ZDSecMgrInitNV
  3068. *
  3069. * @brief Initialize the SecMgr entry data in NV.
  3070. *
  3071. * @param none
  3072. *
  3073. * @return uint8 - <osal_nv_item_init> return codes
  3074. */
  3075. uint8 ZDSecMgrInitNV( void )
  3076. {
  3077. uint8 status;
  3078. uint16 size;
  3079. size = (uint16)( sizeof(ZDSecMgrEntry_t) * ZDSECMGR_ENTRY_MAX );
  3080. status = osal_nv_item_init( ZCD_NV_APS_LINK_KEY_TABLE, size, NULL );
  3081. // The item does not already exist
  3082. if ( status != ZSUCCESS )
  3083. {
  3084. ZDSecMgrSetDefaultNV();
  3085. }
  3086. return status;
  3087. }
  3088. /******************************************************************************
  3089. * @fn ZDSecMgrSetDefaultNV
  3090. *
  3091. * @brief Set default SecMgr entry data in NV.
  3092. *
  3093. * @param none
  3094. *
  3095. * @return none
  3096. */
  3097. void ZDSecMgrSetDefaultNV( void )
  3098. {
  3099. nvDeviceListHdr_t hdr;
  3100. // Initialize the header
  3101. hdr.numRecs = 0;
  3102. // Save off the header
  3103. osal_nv_write( ZCD_NV_APS_LINK_KEY_TABLE, 0, sizeof( nvDeviceListHdr_t ), &hdr );
  3104. }
  3105. /*********************************************************************
  3106. * @fn ZDSecMgrWriteNV()
  3107. *
  3108. * @brief Save off the link key list to NV
  3109. *
  3110. * @param none
  3111. *
  3112. * @return none
  3113. */
  3114. void ZDSecMgrWriteNV( void )
  3115. {
  3116. uint16 i;
  3117. nvDeviceListHdr_t hdr;
  3118. hdr.numRecs = 0;
  3119. for ( i = 0; i < ZDSECMGR_ENTRY_MAX; i++ )
  3120. {
  3121. if ( ZDSecMgrEntries[i].ami != INVALID_NODE_ADDR )
  3122. {
  3123. // Save off the record
  3124. osal_nv_write( ZCD_NV_APS_LINK_KEY_TABLE,
  3125. (uint16)((sizeof(nvDeviceListHdr_t)) + (hdr.numRecs * sizeof(ZDSecMgrEntry_t))),
  3126. sizeof(ZDSecMgrEntry_t), &ZDSecMgrEntries[i] );
  3127. hdr.numRecs++;
  3128. }
  3129. }
  3130. // Save off the header
  3131. osal_nv_write( ZCD_NV_APS_LINK_KEY_TABLE, 0, sizeof( nvDeviceListHdr_t ), &hdr );
  3132. }
  3133. /******************************************************************************
  3134. * @fn ZDSecMgrRestoreFromNV
  3135. *
  3136. * @brief Restore the SecMgr entry data from NV.
  3137. *
  3138. * @param none
  3139. *
  3140. * @return ZStatus_t ZSuccess or ZFailure
  3141. */
  3142. ZStatus_t ZDSecMgrRestoreFromNV( void )
  3143. {
  3144. uint8 x = 0;
  3145. nvDeviceListHdr_t hdr;
  3146. // Initialize the device list
  3147. if ( osal_nv_read( ZCD_NV_APS_LINK_KEY_TABLE, 0, sizeof(nvDeviceListHdr_t), &hdr ) == ZSUCCESS )
  3148. {
  3149. // Read in the device list
  3150. for ( ; x < hdr.numRecs; x++ )
  3151. {
  3152. if ( osal_nv_read( ZCD_NV_APS_LINK_KEY_TABLE,
  3153. (uint16)(sizeof(nvDeviceListHdr_t) + (x * sizeof(ZDSecMgrEntry_t))),
  3154. sizeof(ZDSecMgrEntry_t), &ZDSecMgrEntries[x] ) == ZSUCCESS )
  3155. {
  3156. ZDSecMgrEntries[x].lkd.apsmelkd.txFrmCntr += ( MAX_APS_FRAMECOUNTER_CHANGES + 1 );
  3157. }
  3158. }
  3159. // Write the updated entry back to NV.
  3160. ZDSecMgrWriteNV();
  3161. return ZSuccess;
  3162. }
  3163. return ZFailure;
  3164. }
  3165. /******************************************************************************
  3166. * @fn ZDSecMgrAPSRemove
  3167. *
  3168. * @brief Remove device from network.
  3169. *
  3170. * @param nwkAddr - device's NWK address
  3171. * @param extAddr - device's Extended address
  3172. * @param parentAddr - parent's NWK address
  3173. *
  3174. * @return ZStatus_t
  3175. */
  3176. ZStatus_t ZDSecMgrAPSRemove( uint16 nwkAddr, uint8 *extAddr, uint16 parentAddr )
  3177. {
  3178. ZDSecMgrDevice_t device;
  3179. if ( ( nwkAddr == INVALID_NODE_ADDR ) ||
  3180. ( extAddr == NULL ) ||
  3181. ( parentAddr == INVALID_NODE_ADDR ) )
  3182. {
  3183. return ( ZFailure );
  3184. }
  3185. device.nwkAddr = nwkAddr;
  3186. device.extAddr = extAddr;
  3187. device.parentAddr = parentAddr;
  3188. // remove device
  3189. ZDSecMgrDeviceRemove( &device );
  3190. return ( ZSuccess );
  3191. }
  3192. /******************************************************************************
  3193. ******************************************************************************/