ieee802_1x_kay.c 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568
  1. /*
  2. * IEEE 802.1X-2010 Key Agree Protocol of PAE state machine
  3. * Copyright (c) 2013, Qualcomm Atheros, Inc.
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include <time.h>
  9. #include "includes.h"
  10. #include "common.h"
  11. #include "list.h"
  12. #include "eloop.h"
  13. #include "wpabuf.h"
  14. #include "state_machine.h"
  15. #include "l2_packet/l2_packet.h"
  16. #include "common/eapol_common.h"
  17. #include "crypto/aes_wrap.h"
  18. #include "ieee802_1x_cp.h"
  19. #include "ieee802_1x_key.h"
  20. #include "ieee802_1x_kay.h"
  21. #include "ieee802_1x_kay_i.h"
  22. #include "ieee802_1x_secy_ops.h"
  23. #define DEFAULT_SA_KEY_LEN 16
  24. #define DEFAULT_ICV_LEN 16
  25. #define MAX_ICV_LEN 32 /* 32 bytes, 256 bits */
  26. #define PENDING_PN_EXHAUSTION 0xC0000000
  27. #define MKA_ALIGN_LENGTH(len) (((len) + 0x3) & ~0x3)
  28. /* IEEE Std 802.1X-2010, Table 9-1 - MKA Algorithm Agility */
  29. #define MKA_ALGO_AGILITY_2009 { 0x00, 0x80, 0xC2, 0x01 }
  30. static u8 mka_algo_agility[4] = MKA_ALGO_AGILITY_2009;
  31. /* IEEE802.1AE-2006 Table 14-1 MACsec Cipher Suites */
  32. static struct macsec_ciphersuite cipher_suite_tbl[] = {
  33. /* GCM-AES-128 */
  34. {
  35. CS_ID_GCM_AES_128,
  36. CS_NAME_GCM_AES_128,
  37. MACSEC_CAP_INTEG_AND_CONF_0_30_50,
  38. 16,
  39. 0 /* index */
  40. },
  41. };
  42. #define CS_TABLE_SIZE (ARRAY_SIZE(cipher_suite_tbl))
  43. #define DEFAULT_CS_INDEX 0
  44. static struct mka_alg mka_alg_tbl[] = {
  45. {
  46. MKA_ALGO_AGILITY_2009,
  47. /* 128-bit CAK, KEK, ICK, ICV */
  48. 16, 16, 16, 16,
  49. ieee802_1x_cak_128bits_aes_cmac,
  50. ieee802_1x_ckn_128bits_aes_cmac,
  51. ieee802_1x_kek_128bits_aes_cmac,
  52. ieee802_1x_ick_128bits_aes_cmac,
  53. ieee802_1x_icv_128bits_aes_cmac,
  54. 1, /* index */
  55. },
  56. };
  57. #define MKA_ALG_TABLE_SIZE (ARRAY_SIZE(mka_alg_tbl))
  58. static int is_ki_equal(struct ieee802_1x_mka_ki *ki1,
  59. struct ieee802_1x_mka_ki *ki2)
  60. {
  61. return os_memcmp(ki1->mi, ki2->mi, MI_LEN) == 0 &&
  62. ki1->kn == ki2->kn;
  63. }
  64. struct mka_param_body_handler {
  65. int (*body_tx)(struct ieee802_1x_mka_participant *participant,
  66. struct wpabuf *buf);
  67. int (*body_rx)(struct ieee802_1x_mka_participant *participant,
  68. const u8 *mka_msg, size_t msg_len);
  69. int (*body_length)(struct ieee802_1x_mka_participant *participant);
  70. Boolean (*body_present)(struct ieee802_1x_mka_participant *participant);
  71. };
  72. static void set_mka_param_body_len(void *body, unsigned int len)
  73. {
  74. struct ieee802_1x_mka_hdr *hdr = body;
  75. hdr->length = (len >> 8) & 0x0f;
  76. hdr->length1 = len & 0xff;
  77. }
  78. static unsigned int get_mka_param_body_len(const void *body)
  79. {
  80. const struct ieee802_1x_mka_hdr *hdr = body;
  81. return (hdr->length << 8) | hdr->length1;
  82. }
  83. static u8 get_mka_param_body_type(const void *body)
  84. {
  85. const struct ieee802_1x_mka_hdr *hdr = body;
  86. return hdr->type;
  87. }
  88. /**
  89. * ieee802_1x_mka_dump_basic_body -
  90. */
  91. static void
  92. ieee802_1x_mka_dump_basic_body(struct ieee802_1x_mka_basic_body *body)
  93. {
  94. size_t body_len;
  95. if (!body)
  96. return;
  97. body_len = get_mka_param_body_len(body);
  98. wpa_printf(MSG_DEBUG, "*** MKA Basic Parameter set ***");
  99. wpa_printf(MSG_DEBUG, "\tVersion.......: %d", body->version);
  100. wpa_printf(MSG_DEBUG, "\tPriority......: %d", body->priority);
  101. wpa_printf(MSG_DEBUG, "\tKeySvr........: %d", body->key_server);
  102. wpa_printf(MSG_DEBUG, "\tMACSecDesired.: %d", body->macsec_desired);
  103. wpa_printf(MSG_DEBUG, "\tMACSecCapable.: %d", body->macsec_capability);
  104. wpa_printf(MSG_DEBUG, "\tBody Length...: %d", (int) body_len);
  105. wpa_printf(MSG_DEBUG, "\tSCI MAC.......: " MACSTR,
  106. MAC2STR(body->actor_sci.addr));
  107. wpa_printf(MSG_DEBUG, "\tSCI Port .....: %d",
  108. be_to_host16(body->actor_sci.port));
  109. wpa_hexdump(MSG_DEBUG, "\tMember Id.....:",
  110. body->actor_mi, sizeof(body->actor_mi));
  111. wpa_printf(MSG_DEBUG, "\tMessage Number: %d",
  112. be_to_host32(body->actor_mn));
  113. wpa_hexdump(MSG_DEBUG, "\tAlgo Agility..:",
  114. body->algo_agility, sizeof(body->algo_agility));
  115. wpa_hexdump_ascii(MSG_DEBUG, "\tCAK Name......:", body->ckn,
  116. body_len + MKA_HDR_LEN - sizeof(*body));
  117. }
  118. /**
  119. * ieee802_1x_mka_dump_peer_body -
  120. */
  121. static void
  122. ieee802_1x_mka_dump_peer_body(struct ieee802_1x_mka_peer_body *body)
  123. {
  124. size_t body_len;
  125. size_t i;
  126. u8 *mi;
  127. be32 mn;
  128. if (body == NULL)
  129. return;
  130. body_len = get_mka_param_body_len(body);
  131. if (body->type == MKA_LIVE_PEER_LIST) {
  132. wpa_printf(MSG_DEBUG, "*** Live Peer List ***");
  133. wpa_printf(MSG_DEBUG, "\tBody Length...: %d", (int) body_len);
  134. } else if (body->type == MKA_POTENTIAL_PEER_LIST) {
  135. wpa_printf(MSG_DEBUG, "*** Potential Live Peer List ***");
  136. wpa_printf(MSG_DEBUG, "\tBody Length...: %d", (int) body_len);
  137. }
  138. for (i = 0; i < body_len; i += MI_LEN + sizeof(mn)) {
  139. mi = body->peer + i;
  140. os_memcpy(&mn, mi + MI_LEN, sizeof(mn));
  141. wpa_hexdump_ascii(MSG_DEBUG, "\tMember Id.....:", mi, MI_LEN);
  142. wpa_printf(MSG_DEBUG, "\tMessage Number: %d", be_to_host32(mn));
  143. }
  144. }
  145. /**
  146. * ieee802_1x_mka_dump_dist_sak_body -
  147. */
  148. static void
  149. ieee802_1x_mka_dump_dist_sak_body(struct ieee802_1x_mka_dist_sak_body *body)
  150. {
  151. size_t body_len;
  152. if (body == NULL)
  153. return;
  154. body_len = get_mka_param_body_len(body);
  155. wpa_printf(MSG_INFO, "*** Distributed SAK ***");
  156. wpa_printf(MSG_INFO, "\tDistributed AN........: %d", body->dan);
  157. wpa_printf(MSG_INFO, "\tConfidentiality Offset: %d",
  158. body->confid_offset);
  159. wpa_printf(MSG_INFO, "\tBody Length...........: %d", (int) body_len);
  160. if (!body_len)
  161. return;
  162. wpa_printf(MSG_INFO, "\tKey Number............: %d",
  163. be_to_host32(body->kn));
  164. wpa_hexdump(MSG_INFO, "\tAES Key Wrap of SAK...:", body->sak, 24);
  165. }
  166. static const char * yes_no(int val)
  167. {
  168. return val ? "Yes" : "No";
  169. }
  170. /**
  171. * ieee802_1x_mka_dump_sak_use_body -
  172. */
  173. static void
  174. ieee802_1x_mka_dump_sak_use_body(struct ieee802_1x_mka_sak_use_body *body)
  175. {
  176. int body_len;
  177. if (body == NULL)
  178. return;
  179. body_len = get_mka_param_body_len(body);
  180. wpa_printf(MSG_DEBUG, "*** MACsec SAK Use ***");
  181. wpa_printf(MSG_DEBUG, "\tLatest Key AN....: %d", body->lan);
  182. wpa_printf(MSG_DEBUG, "\tLatest Key Tx....: %s", yes_no(body->ltx));
  183. wpa_printf(MSG_DEBUG, "\tLatest Key Rx....: %s", yes_no(body->lrx));
  184. wpa_printf(MSG_DEBUG, "\tOld Key AN....: %d", body->oan);
  185. wpa_printf(MSG_DEBUG, "\tOld Key Tx....: %s", yes_no(body->otx));
  186. wpa_printf(MSG_DEBUG, "\tOld Key Rx....: %s", yes_no(body->orx));
  187. wpa_printf(MSG_DEBUG, "\tPlain Key Tx....: %s", yes_no(body->ptx));
  188. wpa_printf(MSG_DEBUG, "\tPlain Key Rx....: %s", yes_no(body->prx));
  189. wpa_printf(MSG_DEBUG, "\tDelay Protect....: %s",
  190. yes_no(body->delay_protect));
  191. wpa_printf(MSG_DEBUG, "\tBody Length......: %d", body_len);
  192. if (!body_len)
  193. return;
  194. wpa_hexdump(MSG_DEBUG, "\tKey Server MI....:",
  195. body->lsrv_mi, sizeof(body->lsrv_mi));
  196. wpa_printf(MSG_DEBUG, "\tKey Number.......: %u",
  197. be_to_host32(body->lkn));
  198. wpa_printf(MSG_DEBUG, "\tLowest PN........: %u",
  199. be_to_host32(body->llpn));
  200. wpa_hexdump_ascii(MSG_DEBUG, "\tOld Key Server MI....:",
  201. body->osrv_mi, sizeof(body->osrv_mi));
  202. wpa_printf(MSG_DEBUG, "\tOld Key Number.......: %u",
  203. be_to_host32(body->okn));
  204. wpa_printf(MSG_DEBUG, "\tOld Lowest PN........: %u",
  205. be_to_host32(body->olpn));
  206. }
  207. /**
  208. * ieee802_1x_kay_get_participant -
  209. */
  210. static struct ieee802_1x_mka_participant *
  211. ieee802_1x_kay_get_participant(struct ieee802_1x_kay *kay, const u8 *ckn)
  212. {
  213. struct ieee802_1x_mka_participant *participant;
  214. dl_list_for_each(participant, &kay->participant_list,
  215. struct ieee802_1x_mka_participant, list) {
  216. if (os_memcmp(participant->ckn.name, ckn,
  217. participant->ckn.len) == 0)
  218. return participant;
  219. }
  220. wpa_printf(MSG_DEBUG, "KaY: participant is not found");
  221. return NULL;
  222. }
  223. /**
  224. * ieee802_1x_kay_get_principal_participant -
  225. */
  226. static struct ieee802_1x_mka_participant *
  227. ieee802_1x_kay_get_principal_participant(struct ieee802_1x_kay *kay)
  228. {
  229. struct ieee802_1x_mka_participant *participant;
  230. dl_list_for_each(participant, &kay->participant_list,
  231. struct ieee802_1x_mka_participant, list) {
  232. if (participant->principal)
  233. return participant;
  234. }
  235. wpa_printf(MSG_DEBUG, "KaY: principal participant is not founded");
  236. return NULL;
  237. }
  238. static struct ieee802_1x_kay_peer * get_peer_mi(struct dl_list *peers,
  239. const u8 *mi)
  240. {
  241. struct ieee802_1x_kay_peer *peer;
  242. dl_list_for_each(peer, peers, struct ieee802_1x_kay_peer, list) {
  243. if (os_memcmp(peer->mi, mi, MI_LEN) == 0)
  244. return peer;
  245. }
  246. return NULL;
  247. }
  248. /**
  249. * ieee802_1x_kay_get_potential_peer
  250. */
  251. static struct ieee802_1x_kay_peer *
  252. ieee802_1x_kay_get_potential_peer(
  253. struct ieee802_1x_mka_participant *participant, const u8 *mi)
  254. {
  255. return get_peer_mi(&participant->potential_peers, mi);
  256. }
  257. /**
  258. * ieee802_1x_kay_get_live_peer
  259. */
  260. static struct ieee802_1x_kay_peer *
  261. ieee802_1x_kay_get_live_peer(struct ieee802_1x_mka_participant *participant,
  262. const u8 *mi)
  263. {
  264. return get_peer_mi(&participant->live_peers, mi);
  265. }
  266. /**
  267. * ieee802_1x_kay_is_in_potential_peer
  268. */
  269. static Boolean
  270. ieee802_1x_kay_is_in_potential_peer(
  271. struct ieee802_1x_mka_participant *participant, const u8 *mi)
  272. {
  273. return ieee802_1x_kay_get_potential_peer(participant, mi) != NULL;
  274. }
  275. /**
  276. * ieee802_1x_kay_is_in_live_peer
  277. */
  278. static Boolean
  279. ieee802_1x_kay_is_in_live_peer(
  280. struct ieee802_1x_mka_participant *participant, const u8 *mi)
  281. {
  282. return ieee802_1x_kay_get_live_peer(participant, mi) != NULL;
  283. }
  284. /**
  285. * ieee802_1x_kay_is_in_peer
  286. */
  287. static Boolean
  288. ieee802_1x_kay_is_in_peer(struct ieee802_1x_mka_participant *participant,
  289. const u8 *mi)
  290. {
  291. return ieee802_1x_kay_is_in_live_peer(participant, mi) ||
  292. ieee802_1x_kay_is_in_potential_peer(participant, mi);
  293. }
  294. /**
  295. * ieee802_1x_kay_get_peer
  296. */
  297. static struct ieee802_1x_kay_peer *
  298. ieee802_1x_kay_get_peer(struct ieee802_1x_mka_participant *participant,
  299. const u8 *mi)
  300. {
  301. struct ieee802_1x_kay_peer *peer;
  302. peer = ieee802_1x_kay_get_live_peer(participant, mi);
  303. if (peer)
  304. return peer;
  305. return ieee802_1x_kay_get_potential_peer(participant, mi);
  306. }
  307. /**
  308. * ieee802_1x_kay_get_cipher_suite
  309. */
  310. static struct macsec_ciphersuite *
  311. ieee802_1x_kay_get_cipher_suite(struct ieee802_1x_mka_participant *participant,
  312. u8 *cs_id)
  313. {
  314. unsigned int i;
  315. for (i = 0; i < CS_TABLE_SIZE; i++) {
  316. if (os_memcmp(cipher_suite_tbl[i].id, cs_id, CS_ID_LEN) == 0)
  317. return &cipher_suite_tbl[i];
  318. }
  319. return NULL;
  320. }
  321. static Boolean sci_equal(const struct ieee802_1x_mka_sci *a,
  322. const struct ieee802_1x_mka_sci *b)
  323. {
  324. return os_memcmp(a, b, sizeof(struct ieee802_1x_mka_sci)) == 0;
  325. }
  326. /**
  327. * ieee802_1x_kay_get_peer_sci
  328. */
  329. static struct ieee802_1x_kay_peer *
  330. ieee802_1x_kay_get_peer_sci(struct ieee802_1x_mka_participant *participant,
  331. const struct ieee802_1x_mka_sci *sci)
  332. {
  333. struct ieee802_1x_kay_peer *peer;
  334. dl_list_for_each(peer, &participant->live_peers,
  335. struct ieee802_1x_kay_peer, list) {
  336. if (sci_equal(&peer->sci, sci))
  337. return peer;
  338. }
  339. dl_list_for_each(peer, &participant->potential_peers,
  340. struct ieee802_1x_kay_peer, list) {
  341. if (sci_equal(&peer->sci, sci))
  342. return peer;
  343. }
  344. return NULL;
  345. }
  346. /**
  347. * ieee802_1x_kay_init_receive_sa -
  348. */
  349. static struct receive_sa *
  350. ieee802_1x_kay_init_receive_sa(struct receive_sc *psc, u8 an, u32 lowest_pn,
  351. struct data_key *key)
  352. {
  353. struct receive_sa *psa;
  354. if (!psc || !key)
  355. return NULL;
  356. psa = os_zalloc(sizeof(*psa));
  357. if (!psa) {
  358. wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
  359. return NULL;
  360. }
  361. psa->pkey = key;
  362. psa->lowest_pn = lowest_pn;
  363. psa->next_pn = lowest_pn;
  364. psa->an = an;
  365. psa->sc = psc;
  366. os_get_time(&psa->created_time);
  367. psa->in_use = FALSE;
  368. dl_list_add(&psc->sa_list, &psa->list);
  369. wpa_printf(MSG_DEBUG,
  370. "KaY: Create receive SA(AN: %d lowest_pn: %u of SC(channel: %d)",
  371. (int) an, lowest_pn, psc->channel);
  372. return psa;
  373. }
  374. /**
  375. * ieee802_1x_kay_deinit_receive_sa -
  376. */
  377. static void ieee802_1x_kay_deinit_receive_sa(struct receive_sa *psa)
  378. {
  379. psa->pkey = NULL;
  380. wpa_printf(MSG_DEBUG,
  381. "KaY: Delete receive SA(an: %d) of SC(channel: %d)",
  382. psa->an, psa->sc->channel);
  383. dl_list_del(&psa->list);
  384. os_free(psa);
  385. }
  386. /**
  387. * ieee802_1x_kay_init_receive_sc -
  388. */
  389. static struct receive_sc *
  390. ieee802_1x_kay_init_receive_sc(const struct ieee802_1x_mka_sci *psci,
  391. int channel)
  392. {
  393. struct receive_sc *psc;
  394. if (!psci)
  395. return NULL;
  396. psc = os_zalloc(sizeof(*psc));
  397. if (!psc) {
  398. wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
  399. return NULL;
  400. }
  401. os_memcpy(&psc->sci, psci, sizeof(psc->sci));
  402. psc->channel = channel;
  403. os_get_time(&psc->created_time);
  404. psc->receiving = FALSE;
  405. dl_list_init(&psc->sa_list);
  406. wpa_printf(MSG_DEBUG, "KaY: Create receive SC(channel: %d)", channel);
  407. wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)psci, sizeof(*psci));
  408. return psc;
  409. }
  410. /**
  411. * ieee802_1x_kay_deinit_receive_sc -
  412. **/
  413. static void
  414. ieee802_1x_kay_deinit_receive_sc(
  415. struct ieee802_1x_mka_participant *participant, struct receive_sc *psc)
  416. {
  417. struct receive_sa *psa, *pre_sa;
  418. wpa_printf(MSG_DEBUG, "KaY: Delete receive SC(channel: %d)",
  419. psc->channel);
  420. dl_list_for_each_safe(psa, pre_sa, &psc->sa_list, struct receive_sa,
  421. list) {
  422. secy_disable_receive_sa(participant->kay, psa);
  423. ieee802_1x_kay_deinit_receive_sa(psa);
  424. }
  425. dl_list_del(&psc->list);
  426. os_free(psc);
  427. }
  428. static void ieee802_1x_kay_dump_peer(struct ieee802_1x_kay_peer *peer)
  429. {
  430. wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
  431. wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
  432. wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
  433. wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
  434. }
  435. static struct ieee802_1x_kay_peer *
  436. ieee802_1x_kay_create_peer(const u8 *mi, u32 mn)
  437. {
  438. struct ieee802_1x_kay_peer *peer;
  439. peer = os_zalloc(sizeof(*peer));
  440. if (!peer) {
  441. wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
  442. return NULL;
  443. }
  444. os_memcpy(peer->mi, mi, MI_LEN);
  445. peer->mn = mn;
  446. peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
  447. peer->sak_used = FALSE;
  448. return peer;
  449. }
  450. /**
  451. * ieee802_1x_kay_create_live_peer
  452. */
  453. static struct ieee802_1x_kay_peer *
  454. ieee802_1x_kay_create_live_peer(struct ieee802_1x_mka_participant *participant,
  455. const u8 *mi, u32 mn)
  456. {
  457. struct ieee802_1x_kay_peer *peer;
  458. struct receive_sc *rxsc;
  459. u32 sc_ch = 0;
  460. peer = ieee802_1x_kay_create_peer(mi, mn);
  461. if (!peer)
  462. return NULL;
  463. os_memcpy(&peer->sci, &participant->current_peer_sci,
  464. sizeof(peer->sci));
  465. secy_get_available_receive_sc(participant->kay, &sc_ch);
  466. rxsc = ieee802_1x_kay_init_receive_sc(&peer->sci, sc_ch);
  467. if (!rxsc) {
  468. os_free(peer);
  469. return NULL;
  470. }
  471. dl_list_add(&participant->live_peers, &peer->list);
  472. dl_list_add(&participant->rxsc_list, &rxsc->list);
  473. secy_create_receive_sc(participant->kay, rxsc);
  474. wpa_printf(MSG_DEBUG, "KaY: Live peer created");
  475. ieee802_1x_kay_dump_peer(peer);
  476. return peer;
  477. }
  478. /**
  479. * ieee802_1x_kay_create_potential_peer
  480. */
  481. static struct ieee802_1x_kay_peer *
  482. ieee802_1x_kay_create_potential_peer(
  483. struct ieee802_1x_mka_participant *participant, const u8 *mi, u32 mn)
  484. {
  485. struct ieee802_1x_kay_peer *peer;
  486. peer = ieee802_1x_kay_create_peer(mi, mn);
  487. if (!peer)
  488. return NULL;
  489. dl_list_add(&participant->potential_peers, &peer->list);
  490. wpa_printf(MSG_DEBUG, "KaY: potential peer created");
  491. ieee802_1x_kay_dump_peer(peer);
  492. return peer;
  493. }
  494. /**
  495. * ieee802_1x_kay_move_live_peer
  496. */
  497. static struct ieee802_1x_kay_peer *
  498. ieee802_1x_kay_move_live_peer(struct ieee802_1x_mka_participant *participant,
  499. u8 *mi, u32 mn)
  500. {
  501. struct ieee802_1x_kay_peer *peer;
  502. struct receive_sc *rxsc;
  503. u32 sc_ch = 0;
  504. peer = ieee802_1x_kay_get_potential_peer(participant, mi);
  505. rxsc = ieee802_1x_kay_init_receive_sc(&participant->current_peer_sci,
  506. sc_ch);
  507. if (!rxsc)
  508. return NULL;
  509. os_memcpy(&peer->sci, &participant->current_peer_sci,
  510. sizeof(peer->sci));
  511. peer->mn = mn;
  512. peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
  513. wpa_printf(MSG_DEBUG, "KaY: move potential peer to live peer");
  514. ieee802_1x_kay_dump_peer(peer);
  515. dl_list_del(&peer->list);
  516. dl_list_add_tail(&participant->live_peers, &peer->list);
  517. secy_get_available_receive_sc(participant->kay, &sc_ch);
  518. dl_list_add(&participant->rxsc_list, &rxsc->list);
  519. secy_create_receive_sc(participant->kay, rxsc);
  520. return peer;
  521. }
  522. /**
  523. * ieee802_1x_mka_basic_body_present -
  524. */
  525. static Boolean
  526. ieee802_1x_mka_basic_body_present(
  527. struct ieee802_1x_mka_participant *participant)
  528. {
  529. return TRUE;
  530. }
  531. /**
  532. * ieee802_1x_mka_basic_body_length -
  533. */
  534. static int
  535. ieee802_1x_mka_basic_body_length(struct ieee802_1x_mka_participant *participant)
  536. {
  537. int length;
  538. length = sizeof(struct ieee802_1x_mka_basic_body);
  539. length += participant->ckn.len;
  540. return MKA_ALIGN_LENGTH(length);
  541. }
  542. /**
  543. * ieee802_1x_mka_encode_basic_body
  544. */
  545. static int
  546. ieee802_1x_mka_encode_basic_body(
  547. struct ieee802_1x_mka_participant *participant,
  548. struct wpabuf *buf)
  549. {
  550. struct ieee802_1x_mka_basic_body *body;
  551. struct ieee802_1x_kay *kay = participant->kay;
  552. unsigned int length = ieee802_1x_mka_basic_body_length(participant);
  553. body = wpabuf_put(buf, length);
  554. body->version = kay->mka_version;
  555. body->priority = kay->actor_priority;
  556. if (participant->is_elected)
  557. body->key_server = participant->is_key_server;
  558. else
  559. body->key_server = participant->can_be_key_server;
  560. body->macsec_desired = kay->macsec_desired;
  561. body->macsec_capability = kay->macsec_capable;
  562. set_mka_param_body_len(body, length - MKA_HDR_LEN);
  563. os_memcpy(body->actor_sci.addr, kay->actor_sci.addr,
  564. sizeof(kay->actor_sci.addr));
  565. body->actor_sci.port = kay->actor_sci.port;
  566. os_memcpy(body->actor_mi, participant->mi, sizeof(body->actor_mi));
  567. participant->mn = participant->mn + 1;
  568. body->actor_mn = host_to_be32(participant->mn);
  569. os_memcpy(body->algo_agility, participant->kay->algo_agility,
  570. sizeof(body->algo_agility));
  571. os_memcpy(body->ckn, participant->ckn.name, participant->ckn.len);
  572. ieee802_1x_mka_dump_basic_body(body);
  573. return 0;
  574. }
  575. /**
  576. * ieee802_1x_mka_decode_basic_body -
  577. */
  578. static struct ieee802_1x_mka_participant *
  579. ieee802_1x_mka_decode_basic_body(struct ieee802_1x_kay *kay, const u8 *mka_msg,
  580. size_t msg_len)
  581. {
  582. struct ieee802_1x_mka_participant *participant;
  583. const struct ieee802_1x_mka_basic_body *body;
  584. struct ieee802_1x_kay_peer *peer;
  585. body = (const struct ieee802_1x_mka_basic_body *) mka_msg;
  586. if (body->version > MKA_VERSION_ID) {
  587. wpa_printf(MSG_DEBUG,
  588. "KaY: peer's version(%d) greater than mka current version(%d)",
  589. body->version, MKA_VERSION_ID);
  590. }
  591. if (kay->is_obliged_key_server && body->key_server) {
  592. wpa_printf(MSG_DEBUG, "I must be as key server");
  593. return NULL;
  594. }
  595. participant = ieee802_1x_kay_get_participant(kay, body->ckn);
  596. if (!participant) {
  597. wpa_printf(MSG_DEBUG, "Peer is not included in my CA");
  598. return NULL;
  599. }
  600. /* If the peer's MI is my MI, I will choose new MI */
  601. if (os_memcmp(body->actor_mi, participant->mi, MI_LEN) == 0) {
  602. if (os_get_random(participant->mi, sizeof(participant->mi)) < 0)
  603. return NULL;
  604. participant->mn = 0;
  605. }
  606. os_memcpy(participant->current_peer_id.mi, body->actor_mi, MI_LEN);
  607. participant->current_peer_id.mn = body->actor_mn;
  608. os_memcpy(participant->current_peer_sci.addr, body->actor_sci.addr,
  609. sizeof(participant->current_peer_sci.addr));
  610. participant->current_peer_sci.port = body->actor_sci.port;
  611. /* handler peer */
  612. peer = ieee802_1x_kay_get_peer(participant, body->actor_mi);
  613. if (!peer) {
  614. /* Check duplicated SCI */
  615. /* TODO: What policy should be applied to detect duplicated SCI
  616. * is active attacker or a valid peer whose MI is be changed?
  617. */
  618. peer = ieee802_1x_kay_get_peer_sci(participant,
  619. &body->actor_sci);
  620. if (peer) {
  621. wpa_printf(MSG_WARNING,
  622. "KaY: duplicated SCI detected, Maybe active attacker");
  623. dl_list_del(&peer->list);
  624. os_free(peer);
  625. }
  626. peer = ieee802_1x_kay_create_potential_peer(
  627. participant, body->actor_mi,
  628. be_to_host32(body->actor_mn));
  629. if (!peer)
  630. return NULL;
  631. peer->macsec_desired = body->macsec_desired;
  632. peer->macsec_capability = body->macsec_capability;
  633. peer->is_key_server = (Boolean) body->key_server;
  634. peer->key_server_priority = body->priority;
  635. } else if (peer->mn < be_to_host32(body->actor_mn)) {
  636. peer->mn = be_to_host32(body->actor_mn);
  637. peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
  638. peer->macsec_desired = body->macsec_desired;
  639. peer->macsec_capability = body->macsec_capability;
  640. peer->is_key_server = (Boolean) body->key_server;
  641. peer->key_server_priority = body->priority;
  642. } else {
  643. wpa_printf(MSG_WARNING, "KaY: The peer MN have received");
  644. return NULL;
  645. }
  646. return participant;
  647. }
  648. /**
  649. * ieee802_1x_mka_live_peer_body_present
  650. */
  651. static Boolean
  652. ieee802_1x_mka_live_peer_body_present(
  653. struct ieee802_1x_mka_participant *participant)
  654. {
  655. return !dl_list_empty(&participant->live_peers);
  656. }
  657. /**
  658. * ieee802_1x_kay_get_live_peer_length
  659. */
  660. static int
  661. ieee802_1x_mka_get_live_peer_length(
  662. struct ieee802_1x_mka_participant *participant)
  663. {
  664. int len = MKA_HDR_LEN;
  665. struct ieee802_1x_kay_peer *peer;
  666. dl_list_for_each(peer, &participant->live_peers,
  667. struct ieee802_1x_kay_peer, list)
  668. len += sizeof(struct ieee802_1x_mka_peer_id);
  669. return MKA_ALIGN_LENGTH(len);
  670. }
  671. /**
  672. * ieee802_1x_mka_encode_live_peer_body -
  673. */
  674. static int
  675. ieee802_1x_mka_encode_live_peer_body(
  676. struct ieee802_1x_mka_participant *participant,
  677. struct wpabuf *buf)
  678. {
  679. struct ieee802_1x_mka_peer_body *body;
  680. struct ieee802_1x_kay_peer *peer;
  681. unsigned int length;
  682. struct ieee802_1x_mka_peer_id *body_peer;
  683. length = ieee802_1x_mka_get_live_peer_length(participant);
  684. body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
  685. body->type = MKA_LIVE_PEER_LIST;
  686. set_mka_param_body_len(body, length - MKA_HDR_LEN);
  687. dl_list_for_each(peer, &participant->live_peers,
  688. struct ieee802_1x_kay_peer, list) {
  689. body_peer = wpabuf_put(buf,
  690. sizeof(struct ieee802_1x_mka_peer_id));
  691. os_memcpy(body_peer->mi, peer->mi, MI_LEN);
  692. body_peer->mn = host_to_be32(peer->mn);
  693. body_peer++;
  694. }
  695. ieee802_1x_mka_dump_peer_body(body);
  696. return 0;
  697. }
  698. /**
  699. * ieee802_1x_mka_potential_peer_body_present
  700. */
  701. static Boolean
  702. ieee802_1x_mka_potential_peer_body_present(
  703. struct ieee802_1x_mka_participant *participant)
  704. {
  705. return !dl_list_empty(&participant->potential_peers);
  706. }
  707. /**
  708. * ieee802_1x_kay_get_potential_peer_length
  709. */
  710. static int
  711. ieee802_1x_mka_get_potential_peer_length(
  712. struct ieee802_1x_mka_participant *participant)
  713. {
  714. int len = MKA_HDR_LEN;
  715. struct ieee802_1x_kay_peer *peer;
  716. dl_list_for_each(peer, &participant->potential_peers,
  717. struct ieee802_1x_kay_peer, list)
  718. len += sizeof(struct ieee802_1x_mka_peer_id);
  719. return MKA_ALIGN_LENGTH(len);
  720. }
  721. /**
  722. * ieee802_1x_mka_encode_potential_peer_body -
  723. */
  724. static int
  725. ieee802_1x_mka_encode_potential_peer_body(
  726. struct ieee802_1x_mka_participant *participant,
  727. struct wpabuf *buf)
  728. {
  729. struct ieee802_1x_mka_peer_body *body;
  730. struct ieee802_1x_kay_peer *peer;
  731. unsigned int length;
  732. struct ieee802_1x_mka_peer_id *body_peer;
  733. length = ieee802_1x_mka_get_potential_peer_length(participant);
  734. body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
  735. body->type = MKA_POTENTIAL_PEER_LIST;
  736. set_mka_param_body_len(body, length - MKA_HDR_LEN);
  737. dl_list_for_each(peer, &participant->potential_peers,
  738. struct ieee802_1x_kay_peer, list) {
  739. body_peer = wpabuf_put(buf,
  740. sizeof(struct ieee802_1x_mka_peer_id));
  741. os_memcpy(body_peer->mi, peer->mi, MI_LEN);
  742. body_peer->mn = host_to_be32(peer->mn);
  743. body_peer++;
  744. }
  745. ieee802_1x_mka_dump_peer_body(body);
  746. return 0;
  747. }
  748. /**
  749. * ieee802_1x_mka_i_in_peerlist -
  750. */
  751. static Boolean
  752. ieee802_1x_mka_i_in_peerlist(struct ieee802_1x_mka_participant *participant,
  753. const u8 *mka_msg, size_t msg_len)
  754. {
  755. Boolean included = FALSE;
  756. struct ieee802_1x_mka_hdr *hdr;
  757. size_t body_len;
  758. size_t left_len;
  759. u8 body_type;
  760. u32 peer_mn;
  761. be32 _peer_mn;
  762. const u8 *peer_mi;
  763. const u8 *pos;
  764. size_t i;
  765. pos = mka_msg;
  766. left_len = msg_len;
  767. while (left_len > (MKA_HDR_LEN + DEFAULT_ICV_LEN)) {
  768. hdr = (struct ieee802_1x_mka_hdr *) pos;
  769. body_len = get_mka_param_body_len(hdr);
  770. body_type = get_mka_param_body_type(hdr);
  771. if (body_type != MKA_LIVE_PEER_LIST &&
  772. body_type != MKA_POTENTIAL_PEER_LIST)
  773. goto SKIP_PEER;
  774. ieee802_1x_mka_dump_peer_body(
  775. (struct ieee802_1x_mka_peer_body *)pos);
  776. if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
  777. wpa_printf(MSG_ERROR,
  778. "KaY: MKA Peer Packet Body Length (%d bytes) is less than the Parameter Set Header Length (%d bytes) + the Parameter Set Body Length (%d bytes) + %d bytes of ICV",
  779. (int) left_len, (int) MKA_HDR_LEN,
  780. (int) body_len, DEFAULT_ICV_LEN);
  781. goto SKIP_PEER;
  782. }
  783. if ((body_len % 16) != 0) {
  784. wpa_printf(MSG_ERROR,
  785. "KaY: MKA Peer Packet Body Length (%d bytes) should multiple of 16 octets",
  786. (int) body_len);
  787. goto SKIP_PEER;
  788. }
  789. for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
  790. peer_mi = MKA_HDR_LEN + pos + i;
  791. os_memcpy(&_peer_mn, peer_mi + MI_LEN,
  792. sizeof(_peer_mn));
  793. peer_mn = be_to_host32(_peer_mn);
  794. if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0 &&
  795. peer_mn == participant->mn) {
  796. included = TRUE;
  797. break;
  798. }
  799. }
  800. if (included)
  801. return TRUE;
  802. SKIP_PEER:
  803. left_len -= body_len + MKA_HDR_LEN;
  804. pos += body_len + MKA_HDR_LEN;
  805. }
  806. return FALSE;
  807. }
  808. /**
  809. * ieee802_1x_mka_decode_live_peer_body -
  810. */
  811. static int ieee802_1x_mka_decode_live_peer_body(
  812. struct ieee802_1x_mka_participant *participant,
  813. const u8 *peer_msg, size_t msg_len)
  814. {
  815. const struct ieee802_1x_mka_hdr *hdr;
  816. struct ieee802_1x_kay_peer *peer;
  817. size_t body_len;
  818. u32 peer_mn;
  819. be32 _peer_mn;
  820. const u8 *peer_mi;
  821. size_t i;
  822. Boolean is_included;
  823. is_included = ieee802_1x_kay_is_in_live_peer(
  824. participant, participant->current_peer_id.mi);
  825. hdr = (const struct ieee802_1x_mka_hdr *) peer_msg;
  826. body_len = get_mka_param_body_len(hdr);
  827. if (body_len % 16 != 0) {
  828. wpa_printf(MSG_ERROR,
  829. "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
  830. body_len);
  831. return -1;
  832. }
  833. for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
  834. peer_mi = MKA_HDR_LEN + peer_msg + i;
  835. os_memcpy(&_peer_mn, peer_mi + MI_LEN, sizeof(_peer_mn));
  836. peer_mn = be_to_host32(_peer_mn);
  837. /* it is myself */
  838. if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
  839. /* My message id is used by other participant */
  840. if (peer_mn > participant->mn) {
  841. if (os_get_random(participant->mi,
  842. sizeof(participant->mi)) < 0)
  843. wpa_printf(MSG_DEBUG,
  844. "KaY: Could not update mi");
  845. participant->mn = 0;
  846. }
  847. continue;
  848. }
  849. if (!is_included)
  850. continue;
  851. peer = ieee802_1x_kay_get_peer(participant, peer_mi);
  852. if (NULL != peer) {
  853. peer->mn = peer_mn;
  854. peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
  855. } else {
  856. if (!ieee802_1x_kay_create_potential_peer(
  857. participant, peer_mi, peer_mn)) {
  858. return -1;
  859. }
  860. }
  861. }
  862. return 0;
  863. }
  864. /**
  865. * ieee802_1x_mka_decode_potential_peer_body -
  866. */
  867. static int
  868. ieee802_1x_mka_decode_potential_peer_body(
  869. struct ieee802_1x_mka_participant *participant,
  870. const u8 *peer_msg, size_t msg_len)
  871. {
  872. struct ieee802_1x_mka_hdr *hdr;
  873. size_t body_len;
  874. u32 peer_mn;
  875. be32 _peer_mn;
  876. const u8 *peer_mi;
  877. size_t i;
  878. hdr = (struct ieee802_1x_mka_hdr *) peer_msg;
  879. body_len = get_mka_param_body_len(hdr);
  880. if (body_len % 16 != 0) {
  881. wpa_printf(MSG_ERROR,
  882. "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
  883. body_len);
  884. return -1;
  885. }
  886. for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
  887. peer_mi = MKA_HDR_LEN + peer_msg + i;
  888. os_memcpy(&_peer_mn, peer_mi + MI_LEN, sizeof(_peer_mn));
  889. peer_mn = be_to_host32(_peer_mn);
  890. /* it is myself */
  891. if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
  892. /* My message id is used by other participant */
  893. if (peer_mn > participant->mn) {
  894. if (os_get_random(participant->mi,
  895. sizeof(participant->mi)) < 0)
  896. wpa_printf(MSG_DEBUG,
  897. "KaY: Could not update mi");
  898. participant->mn = 0;
  899. }
  900. continue;
  901. }
  902. }
  903. return 0;
  904. }
  905. /**
  906. * ieee802_1x_mka_sak_use_body_present
  907. */
  908. static Boolean
  909. ieee802_1x_mka_sak_use_body_present(
  910. struct ieee802_1x_mka_participant *participant)
  911. {
  912. if (participant->to_use_sak)
  913. return TRUE;
  914. else
  915. return FALSE;
  916. }
  917. /**
  918. * ieee802_1x_mka_get_sak_use_length
  919. */
  920. static int
  921. ieee802_1x_mka_get_sak_use_length(
  922. struct ieee802_1x_mka_participant *participant)
  923. {
  924. int length = MKA_HDR_LEN;
  925. if (participant->kay->macsec_desired && participant->advised_desired)
  926. length = sizeof(struct ieee802_1x_mka_sak_use_body);
  927. return MKA_ALIGN_LENGTH(length);
  928. }
  929. /**
  930. *
  931. */
  932. static u32
  933. ieee802_1x_mka_get_lpn(struct ieee802_1x_mka_participant *principal,
  934. struct ieee802_1x_mka_ki *ki)
  935. {
  936. struct receive_sa *rxsa;
  937. struct receive_sc *rxsc;
  938. u32 lpn = 0;
  939. dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
  940. dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
  941. {
  942. if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
  943. secy_get_receive_lowest_pn(principal->kay,
  944. rxsa);
  945. lpn = lpn > rxsa->lowest_pn ?
  946. lpn : rxsa->lowest_pn;
  947. break;
  948. }
  949. }
  950. }
  951. if (lpn == 0)
  952. lpn = 1;
  953. return lpn;
  954. }
  955. /**
  956. * ieee802_1x_mka_encode_sak_use_body -
  957. */
  958. static int
  959. ieee802_1x_mka_encode_sak_use_body(
  960. struct ieee802_1x_mka_participant *participant,
  961. struct wpabuf *buf)
  962. {
  963. struct ieee802_1x_mka_sak_use_body *body;
  964. unsigned int length;
  965. u32 pn = 1;
  966. length = ieee802_1x_mka_get_sak_use_length(participant);
  967. body = wpabuf_put(buf, length);
  968. body->type = MKA_SAK_USE;
  969. set_mka_param_body_len(body, length - MKA_HDR_LEN);
  970. if (length == MKA_HDR_LEN) {
  971. body->ptx = TRUE;
  972. body->prx = TRUE;
  973. body->lan = 0;
  974. body->lrx = FALSE;
  975. body->ltx = FALSE;
  976. body->delay_protect = FALSE;
  977. return 0;
  978. }
  979. /* data protect, lowest accept packet number */
  980. body->delay_protect = participant->kay->macsec_replay_protect;
  981. pn = ieee802_1x_mka_get_lpn(participant, &participant->lki);
  982. if (pn > participant->kay->pn_exhaustion) {
  983. wpa_printf(MSG_WARNING, "KaY: My LPN exhaustion");
  984. if (participant->is_key_server)
  985. participant->new_sak = TRUE;
  986. }
  987. body->llpn = host_to_be32(pn);
  988. pn = ieee802_1x_mka_get_lpn(participant, &participant->oki);
  989. body->olpn = host_to_be32(pn);
  990. /* plain tx, plain rx */
  991. if (participant->kay->macsec_protect)
  992. body->ptx = FALSE;
  993. else
  994. body->ptx = TRUE;
  995. if (participant->kay->macsec_validate == Strict)
  996. body->prx = FALSE;
  997. else
  998. body->prx = TRUE;
  999. /* latest key: rx, tx, key server member identifier key number */
  1000. body->lan = participant->lan;
  1001. os_memcpy(body->lsrv_mi, participant->lki.mi,
  1002. sizeof(body->lsrv_mi));
  1003. body->lkn = host_to_be32(participant->lki.kn);
  1004. body->lrx = participant->lrx;
  1005. body->ltx = participant->ltx;
  1006. /* old key: rx, tx, key server member identifier key number */
  1007. body->oan = participant->oan;
  1008. if (participant->oki.kn != participant->lki.kn &&
  1009. participant->oki.kn != 0) {
  1010. body->otx = TRUE;
  1011. body->orx = TRUE;
  1012. os_memcpy(body->osrv_mi, participant->oki.mi,
  1013. sizeof(body->osrv_mi));
  1014. body->okn = host_to_be32(participant->oki.kn);
  1015. } else {
  1016. body->otx = FALSE;
  1017. body->orx = FALSE;
  1018. }
  1019. /* set CP's variable */
  1020. if (body->ltx) {
  1021. if (!participant->kay->tx_enable)
  1022. participant->kay->tx_enable = TRUE;
  1023. if (!participant->kay->port_enable)
  1024. participant->kay->port_enable = TRUE;
  1025. }
  1026. if (body->lrx) {
  1027. if (!participant->kay->rx_enable)
  1028. participant->kay->rx_enable = TRUE;
  1029. }
  1030. ieee802_1x_mka_dump_sak_use_body(body);
  1031. return 0;
  1032. }
  1033. /**
  1034. * ieee802_1x_mka_decode_sak_use_body -
  1035. */
  1036. static int
  1037. ieee802_1x_mka_decode_sak_use_body(
  1038. struct ieee802_1x_mka_participant *participant,
  1039. const u8 *mka_msg, size_t msg_len)
  1040. {
  1041. struct ieee802_1x_mka_hdr *hdr;
  1042. struct ieee802_1x_mka_sak_use_body *body;
  1043. struct ieee802_1x_kay_peer *peer;
  1044. struct transmit_sa *txsa;
  1045. struct data_key *sa_key = NULL;
  1046. size_t body_len;
  1047. struct ieee802_1x_mka_ki ki;
  1048. u32 lpn;
  1049. Boolean all_receiving;
  1050. Boolean founded;
  1051. if (!participant->principal) {
  1052. wpa_printf(MSG_WARNING, "KaY: Participant is not principal");
  1053. return -1;
  1054. }
  1055. peer = ieee802_1x_kay_get_live_peer(participant,
  1056. participant->current_peer_id.mi);
  1057. if (!peer) {
  1058. wpa_printf(MSG_WARNING, "KaY: the peer is not my live peer");
  1059. return -1;
  1060. }
  1061. hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
  1062. body_len = get_mka_param_body_len(hdr);
  1063. body = (struct ieee802_1x_mka_sak_use_body *) mka_msg;
  1064. ieee802_1x_mka_dump_sak_use_body(body);
  1065. if ((body_len != 0) && (body_len < 40)) {
  1066. wpa_printf(MSG_ERROR,
  1067. "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 0, 40, or more octets",
  1068. (int) body_len);
  1069. return -1;
  1070. }
  1071. /* TODO: what action should I take when peer does not support MACsec */
  1072. if (body_len == 0) {
  1073. wpa_printf(MSG_WARNING, "KaY: Peer does not support MACsec");
  1074. return 0;
  1075. }
  1076. /* TODO: when the plain tx or rx of peer is true, should I change
  1077. * the attribute of controlled port
  1078. */
  1079. if (body->prx)
  1080. wpa_printf(MSG_WARNING, "KaY: peer's plain rx are TRUE");
  1081. if (body->ptx)
  1082. wpa_printf(MSG_WARNING, "KaY: peer's plain tx are TRUE");
  1083. /* check latest key is valid */
  1084. if (body->ltx || body->lrx) {
  1085. founded = FALSE;
  1086. os_memcpy(ki.mi, body->lsrv_mi, sizeof(ki.mi));
  1087. ki.kn = be_to_host32(body->lkn);
  1088. dl_list_for_each(sa_key, &participant->sak_list,
  1089. struct data_key, list) {
  1090. if (is_ki_equal(&sa_key->key_identifier, &ki)) {
  1091. founded = TRUE;
  1092. break;
  1093. }
  1094. }
  1095. if (!founded) {
  1096. wpa_printf(MSG_WARNING, "KaY: Latest key is invalid");
  1097. return -1;
  1098. }
  1099. if (os_memcmp(participant->lki.mi, body->lsrv_mi,
  1100. sizeof(participant->lki.mi)) == 0 &&
  1101. be_to_host32(body->lkn) == participant->lki.kn &&
  1102. body->lan == participant->lan) {
  1103. peer->sak_used = TRUE;
  1104. }
  1105. if (body->ltx && peer->is_key_server) {
  1106. ieee802_1x_cp_set_servertransmitting(
  1107. participant->kay->cp, TRUE);
  1108. ieee802_1x_cp_sm_step(participant->kay->cp);
  1109. }
  1110. }
  1111. /* check old key is valid */
  1112. if (body->otx || body->orx) {
  1113. if (os_memcmp(participant->oki.mi, body->osrv_mi,
  1114. sizeof(participant->oki.mi)) != 0 ||
  1115. be_to_host32(body->okn) != participant->oki.kn ||
  1116. body->oan != participant->oan) {
  1117. wpa_printf(MSG_WARNING, "KaY: Old key is invalid");
  1118. return -1;
  1119. }
  1120. }
  1121. /* TODO: how to set the MACsec hardware when delay_protect is true */
  1122. if (body->delay_protect &&
  1123. (!be_to_host32(body->llpn) || !be_to_host32(body->olpn))) {
  1124. wpa_printf(MSG_WARNING,
  1125. "KaY: Lowest packet number should greater than 0 when delay_protect is TRUE");
  1126. return -1;
  1127. }
  1128. /* check all live peer have used the sak for receiving sa */
  1129. all_receiving = TRUE;
  1130. dl_list_for_each(peer, &participant->live_peers,
  1131. struct ieee802_1x_kay_peer, list) {
  1132. if (!peer->sak_used) {
  1133. all_receiving = FALSE;
  1134. break;
  1135. }
  1136. }
  1137. if (all_receiving) {
  1138. participant->to_dist_sak = FALSE;
  1139. ieee802_1x_cp_set_allreceiving(participant->kay->cp, TRUE);
  1140. ieee802_1x_cp_sm_step(participant->kay->cp);
  1141. }
  1142. /* if i'm key server, and detects peer member pn exhaustion, rekey.*/
  1143. lpn = be_to_host32(body->llpn);
  1144. if (lpn > participant->kay->pn_exhaustion) {
  1145. if (participant->is_key_server) {
  1146. participant->new_sak = TRUE;
  1147. wpa_printf(MSG_WARNING, "KaY: Peer LPN exhaustion");
  1148. }
  1149. }
  1150. founded = FALSE;
  1151. dl_list_for_each(txsa, &participant->txsc->sa_list,
  1152. struct transmit_sa, list) {
  1153. if (sa_key != NULL && txsa->pkey == sa_key) {
  1154. founded = TRUE;
  1155. break;
  1156. }
  1157. }
  1158. if (!founded) {
  1159. wpa_printf(MSG_WARNING, "KaY: Can't find txsa");
  1160. return -1;
  1161. }
  1162. /* FIXME: Secy creates txsa with default npn. If MKA detected Latest Key
  1163. * npn is larger than txsa's npn, set it to txsa.
  1164. */
  1165. secy_get_transmit_next_pn(participant->kay, txsa);
  1166. if (lpn > txsa->next_pn) {
  1167. secy_set_transmit_next_pn(participant->kay, txsa);
  1168. wpa_printf(MSG_INFO, "KaY: update lpn =0x%x", lpn);
  1169. }
  1170. return 0;
  1171. }
  1172. /**
  1173. * ieee802_1x_mka_dist_sak_body_present
  1174. */
  1175. static Boolean
  1176. ieee802_1x_mka_dist_sak_body_present(
  1177. struct ieee802_1x_mka_participant *participant)
  1178. {
  1179. if (!participant->to_dist_sak || !participant->new_key)
  1180. return FALSE;
  1181. return TRUE;
  1182. }
  1183. /**
  1184. * ieee802_1x_kay_get_dist_sak_length
  1185. */
  1186. static int
  1187. ieee802_1x_mka_get_dist_sak_length(
  1188. struct ieee802_1x_mka_participant *participant)
  1189. {
  1190. int length = MKA_HDR_LEN;
  1191. int cs_index = participant->kay->macsec_csindex;
  1192. if (participant->advised_desired) {
  1193. length = sizeof(struct ieee802_1x_mka_dist_sak_body);
  1194. if (cs_index != DEFAULT_CS_INDEX)
  1195. length += CS_ID_LEN;
  1196. length += cipher_suite_tbl[cs_index].sak_len + 8;
  1197. }
  1198. return MKA_ALIGN_LENGTH(length);
  1199. }
  1200. /**
  1201. * ieee802_1x_mka_encode_dist_sak_body -
  1202. */
  1203. static int
  1204. ieee802_1x_mka_encode_dist_sak_body(
  1205. struct ieee802_1x_mka_participant *participant,
  1206. struct wpabuf *buf)
  1207. {
  1208. struct ieee802_1x_mka_dist_sak_body *body;
  1209. struct data_key *sak;
  1210. unsigned int length;
  1211. int cs_index;
  1212. int sak_pos;
  1213. length = ieee802_1x_mka_get_dist_sak_length(participant);
  1214. body = wpabuf_put(buf, length);
  1215. body->type = MKA_DISTRIBUTED_SAK;
  1216. set_mka_param_body_len(body, length - MKA_HDR_LEN);
  1217. if (length == MKA_HDR_LEN) {
  1218. body->confid_offset = 0;
  1219. body->dan = 0;
  1220. return 0;
  1221. }
  1222. sak = participant->new_key;
  1223. body->confid_offset = sak->confidentiality_offset;
  1224. body->dan = sak->an;
  1225. body->kn = host_to_be32(sak->key_identifier.kn);
  1226. cs_index = participant->kay->macsec_csindex;
  1227. sak_pos = 0;
  1228. if (cs_index != DEFAULT_CS_INDEX) {
  1229. os_memcpy(body->sak, cipher_suite_tbl[cs_index].id, CS_ID_LEN);
  1230. sak_pos = CS_ID_LEN;
  1231. }
  1232. if (aes_wrap(participant->kek.key, 16,
  1233. cipher_suite_tbl[cs_index].sak_len / 8,
  1234. sak->key, body->sak + sak_pos)) {
  1235. wpa_printf(MSG_ERROR, "KaY: AES wrap failed");
  1236. return -1;
  1237. }
  1238. ieee802_1x_mka_dump_dist_sak_body(body);
  1239. return 0;
  1240. }
  1241. /**
  1242. * ieee802_1x_kay_init_data_key -
  1243. */
  1244. static struct data_key *
  1245. ieee802_1x_kay_init_data_key(const struct key_conf *conf)
  1246. {
  1247. struct data_key *pkey;
  1248. if (!conf)
  1249. return NULL;
  1250. pkey = os_zalloc(sizeof(*pkey));
  1251. if (pkey == NULL) {
  1252. wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
  1253. return NULL;
  1254. }
  1255. pkey->key = os_zalloc(conf->key_len);
  1256. if (pkey->key == NULL) {
  1257. wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
  1258. os_free(pkey);
  1259. return NULL;
  1260. }
  1261. os_memcpy(pkey->key, conf->key, conf->key_len);
  1262. os_memcpy(&pkey->key_identifier, &conf->ki,
  1263. sizeof(pkey->key_identifier));
  1264. pkey->confidentiality_offset = conf->offset;
  1265. pkey->an = conf->an;
  1266. pkey->transmits = conf->tx;
  1267. pkey->receives = conf->rx;
  1268. os_get_time(&pkey->created_time);
  1269. pkey->user = 1;
  1270. return pkey;
  1271. }
  1272. /**
  1273. * ieee802_1x_kay_decode_dist_sak_body -
  1274. */
  1275. static int
  1276. ieee802_1x_mka_decode_dist_sak_body(
  1277. struct ieee802_1x_mka_participant *participant,
  1278. const u8 *mka_msg, size_t msg_len)
  1279. {
  1280. struct ieee802_1x_mka_hdr *hdr;
  1281. struct ieee802_1x_mka_dist_sak_body *body;
  1282. struct ieee802_1x_kay_peer *peer;
  1283. struct macsec_ciphersuite *cs;
  1284. size_t body_len;
  1285. struct key_conf *conf;
  1286. struct data_key *sa_key = NULL;
  1287. struct ieee802_1x_mka_ki sak_ki;
  1288. int sak_len;
  1289. u8 *wrap_sak;
  1290. u8 *unwrap_sak;
  1291. hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
  1292. body_len = get_mka_param_body_len(hdr);
  1293. if ((body_len != 0) && (body_len != 28) && (body_len < 36)) {
  1294. wpa_printf(MSG_ERROR,
  1295. "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 0, 28, 36, or more octets",
  1296. (int) body_len);
  1297. return -1;
  1298. }
  1299. if (!participant->principal) {
  1300. wpa_printf(MSG_ERROR,
  1301. "KaY: I can't accept the distributed SAK as I am not principal");
  1302. return -1;
  1303. }
  1304. if (participant->is_key_server) {
  1305. wpa_printf(MSG_ERROR,
  1306. "KaY: I can't accept the distributed SAK as myself is key server ");
  1307. return -1;
  1308. }
  1309. if (!participant->kay->macsec_desired ||
  1310. participant->kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
  1311. wpa_printf(MSG_ERROR,
  1312. "KaY: I am not MACsec-desired or without MACsec capable");
  1313. return -1;
  1314. }
  1315. peer = ieee802_1x_kay_get_live_peer(participant,
  1316. participant->current_peer_id.mi);
  1317. if (!peer) {
  1318. wpa_printf(MSG_ERROR,
  1319. "KaY: The key server is not in my live peers list");
  1320. return -1;
  1321. }
  1322. if (!sci_equal(&participant->kay->key_server_sci, &peer->sci)) {
  1323. wpa_printf(MSG_ERROR, "KaY: The key server is not elected");
  1324. return -1;
  1325. }
  1326. if (body_len == 0) {
  1327. participant->kay->authenticated = TRUE;
  1328. participant->kay->secured = FALSE;
  1329. participant->kay->failed = FALSE;
  1330. participant->advised_desired = FALSE;
  1331. ieee802_1x_cp_connect_authenticated(participant->kay->cp);
  1332. ieee802_1x_cp_sm_step(participant->kay->cp);
  1333. wpa_printf(MSG_WARNING, "KaY:The Key server advise no MACsec");
  1334. participant->to_use_sak = TRUE;
  1335. return 0;
  1336. }
  1337. participant->advised_desired = TRUE;
  1338. participant->kay->authenticated = FALSE;
  1339. participant->kay->secured = TRUE;
  1340. participant->kay->failed = FALSE;
  1341. ieee802_1x_cp_connect_secure(participant->kay->cp);
  1342. ieee802_1x_cp_sm_step(participant->kay->cp);
  1343. body = (struct ieee802_1x_mka_dist_sak_body *)mka_msg;
  1344. ieee802_1x_mka_dump_dist_sak_body(body);
  1345. dl_list_for_each(sa_key, &participant->sak_list, struct data_key, list)
  1346. {
  1347. if (os_memcmp(sa_key->key_identifier.mi,
  1348. participant->current_peer_id.mi, MI_LEN) == 0 &&
  1349. sa_key->key_identifier.kn == be_to_host32(body->kn)) {
  1350. wpa_printf(MSG_WARNING, "KaY:The Key has installed");
  1351. return 0;
  1352. }
  1353. }
  1354. if (body_len == 28) {
  1355. sak_len = DEFAULT_SA_KEY_LEN;
  1356. wrap_sak = body->sak;
  1357. participant->kay->macsec_csindex = DEFAULT_CS_INDEX;
  1358. } else {
  1359. cs = ieee802_1x_kay_get_cipher_suite(participant, body->sak);
  1360. if (!cs) {
  1361. wpa_printf(MSG_ERROR,
  1362. "KaY: I can't support the Cipher Suite advised by key server");
  1363. return -1;
  1364. }
  1365. sak_len = cs->sak_len;
  1366. wrap_sak = body->sak + CS_ID_LEN;
  1367. participant->kay->macsec_csindex = cs->index;
  1368. }
  1369. unwrap_sak = os_zalloc(sak_len);
  1370. if (!unwrap_sak) {
  1371. wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
  1372. return -1;
  1373. }
  1374. if (aes_unwrap(participant->kek.key, 16, sak_len >> 3, wrap_sak,
  1375. unwrap_sak)) {
  1376. wpa_printf(MSG_ERROR, "KaY: AES unwrap failed");
  1377. os_free(unwrap_sak);
  1378. return -1;
  1379. }
  1380. wpa_hexdump(MSG_DEBUG, "\tAES Key Unwrap of SAK:", unwrap_sak, sak_len);
  1381. conf = os_zalloc(sizeof(*conf));
  1382. if (!conf) {
  1383. wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
  1384. os_free(unwrap_sak);
  1385. return -1;
  1386. }
  1387. conf->key_len = sak_len;
  1388. conf->key = os_zalloc(conf->key_len);
  1389. if (!conf->key) {
  1390. wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
  1391. os_free(unwrap_sak);
  1392. os_free(conf);
  1393. return -1;
  1394. }
  1395. os_memcpy(conf->key, unwrap_sak, conf->key_len);
  1396. os_memcpy(&sak_ki.mi, &participant->current_peer_id.mi,
  1397. sizeof(sak_ki.mi));
  1398. sak_ki.kn = be_to_host32(body->kn);
  1399. os_memcpy(conf->ki.mi, sak_ki.mi, MI_LEN);
  1400. conf->ki.kn = sak_ki.kn;
  1401. conf->an = body->dan;
  1402. conf->offset = body->confid_offset;
  1403. conf->rx = TRUE;
  1404. conf->tx = TRUE;
  1405. sa_key = ieee802_1x_kay_init_data_key(conf);
  1406. if (!sa_key) {
  1407. os_free(unwrap_sak);
  1408. os_free(conf->key);
  1409. os_free(conf);
  1410. return -1;
  1411. }
  1412. dl_list_add(&participant->sak_list, &sa_key->list);
  1413. ieee802_1x_cp_set_ciphersuite(
  1414. participant->kay->cp,
  1415. cipher_suite_tbl[participant->kay->macsec_csindex].id);
  1416. ieee802_1x_cp_sm_step(participant->kay->cp);
  1417. ieee802_1x_cp_set_offset(participant->kay->cp, body->confid_offset);
  1418. ieee802_1x_cp_sm_step(participant->kay->cp);
  1419. ieee802_1x_cp_set_distributedki(participant->kay->cp, &sak_ki);
  1420. ieee802_1x_cp_set_distributedan(participant->kay->cp, body->dan);
  1421. ieee802_1x_cp_signal_newsak(participant->kay->cp);
  1422. ieee802_1x_cp_sm_step(participant->kay->cp);
  1423. participant->to_use_sak = TRUE;
  1424. os_free(unwrap_sak);
  1425. os_free(conf->key);
  1426. os_free(conf);
  1427. return 0;
  1428. }
  1429. /**
  1430. * ieee802_1x_mka_icv_body_present
  1431. */
  1432. static Boolean
  1433. ieee802_1x_mka_icv_body_present(struct ieee802_1x_mka_participant *participant)
  1434. {
  1435. return TRUE;
  1436. }
  1437. /**
  1438. * ieee802_1x_kay_get_icv_length
  1439. */
  1440. static int
  1441. ieee802_1x_mka_get_icv_length(struct ieee802_1x_mka_participant *participant)
  1442. {
  1443. int length;
  1444. length = sizeof(struct ieee802_1x_mka_icv_body);
  1445. length += mka_alg_tbl[participant->kay->mka_algindex].icv_len;
  1446. return MKA_ALIGN_LENGTH(length);
  1447. }
  1448. /**
  1449. * ieee802_1x_mka_encode_icv_body -
  1450. */
  1451. static int
  1452. ieee802_1x_mka_encode_icv_body(struct ieee802_1x_mka_participant *participant,
  1453. struct wpabuf *buf)
  1454. {
  1455. struct ieee802_1x_mka_icv_body *body;
  1456. unsigned int length;
  1457. u8 cmac[MAX_ICV_LEN];
  1458. length = ieee802_1x_mka_get_icv_length(participant);
  1459. if (length != DEFAULT_ICV_LEN) {
  1460. body = wpabuf_put(buf, MKA_HDR_LEN);
  1461. body->type = MKA_ICV_INDICATOR;
  1462. set_mka_param_body_len(body, length - MKA_HDR_LEN);
  1463. }
  1464. if (mka_alg_tbl[participant->kay->mka_algindex].icv_hash(
  1465. participant->ick.key, wpabuf_head(buf), buf->used, cmac)) {
  1466. wpa_printf(MSG_ERROR, "KaY, omac1_aes_128 failed");
  1467. return -1;
  1468. }
  1469. if (length != DEFAULT_ICV_LEN) {
  1470. os_memcpy(wpabuf_put(buf, length - MKA_HDR_LEN), cmac,
  1471. length - MKA_HDR_LEN);
  1472. } else {
  1473. os_memcpy(wpabuf_put(buf, length), cmac, length);
  1474. }
  1475. return 0;
  1476. }
  1477. /**
  1478. * ieee802_1x_mka_decode_icv_body -
  1479. */
  1480. static u8 *
  1481. ieee802_1x_mka_decode_icv_body(struct ieee802_1x_mka_participant *participant,
  1482. const u8 *mka_msg, size_t msg_len)
  1483. {
  1484. struct ieee802_1x_mka_hdr *hdr;
  1485. struct ieee802_1x_mka_icv_body *body;
  1486. size_t body_len;
  1487. size_t left_len;
  1488. u8 body_type;
  1489. const u8 *pos;
  1490. pos = mka_msg;
  1491. left_len = msg_len;
  1492. while (left_len > (MKA_HDR_LEN + DEFAULT_ICV_LEN)) {
  1493. hdr = (struct ieee802_1x_mka_hdr *) pos;
  1494. body_len = get_mka_param_body_len(hdr);
  1495. body_type = get_mka_param_body_type(hdr);
  1496. if (left_len < (body_len + MKA_HDR_LEN))
  1497. break;
  1498. if (body_type != MKA_ICV_INDICATOR) {
  1499. left_len -= MKA_HDR_LEN + body_len;
  1500. pos += MKA_HDR_LEN + body_len;
  1501. continue;
  1502. }
  1503. body = (struct ieee802_1x_mka_icv_body *)pos;
  1504. if (body_len
  1505. < mka_alg_tbl[participant->kay->mka_algindex].icv_len) {
  1506. return NULL;
  1507. }
  1508. return body->icv;
  1509. }
  1510. return (u8 *) (mka_msg + msg_len - DEFAULT_ICV_LEN);
  1511. }
  1512. /**
  1513. * ieee802_1x_mka_decode_dist_cak_body-
  1514. */
  1515. static int
  1516. ieee802_1x_mka_decode_dist_cak_body(
  1517. struct ieee802_1x_mka_participant *participant,
  1518. const u8 *mka_msg, size_t msg_len)
  1519. {
  1520. struct ieee802_1x_mka_hdr *hdr;
  1521. size_t body_len;
  1522. hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
  1523. body_len = get_mka_param_body_len(hdr);
  1524. if (body_len < 28) {
  1525. wpa_printf(MSG_ERROR,
  1526. "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 28 or more octets",
  1527. (int) body_len);
  1528. return -1;
  1529. }
  1530. return 0;
  1531. }
  1532. /**
  1533. * ieee802_1x_mka_decode_kmd_body -
  1534. */
  1535. static int
  1536. ieee802_1x_mka_decode_kmd_body(
  1537. struct ieee802_1x_mka_participant *participant,
  1538. const u8 *mka_msg, size_t msg_len)
  1539. {
  1540. struct ieee802_1x_mka_hdr *hdr;
  1541. size_t body_len;
  1542. hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
  1543. body_len = get_mka_param_body_len(hdr);
  1544. if (body_len < 5) {
  1545. wpa_printf(MSG_ERROR,
  1546. "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 5 or more octets",
  1547. (int) body_len);
  1548. return -1;
  1549. }
  1550. return 0;
  1551. }
  1552. /**
  1553. * ieee802_1x_mka_decode_announce_body -
  1554. */
  1555. static int ieee802_1x_mka_decode_announce_body(
  1556. struct ieee802_1x_mka_participant *participant,
  1557. const u8 *mka_msg, size_t msg_len)
  1558. {
  1559. return 0;
  1560. }
  1561. static struct mka_param_body_handler mka_body_handler[] = {
  1562. /* basic parameter set */
  1563. {
  1564. ieee802_1x_mka_encode_basic_body,
  1565. NULL,
  1566. ieee802_1x_mka_basic_body_length,
  1567. ieee802_1x_mka_basic_body_present
  1568. },
  1569. /* live peer list parameter set */
  1570. {
  1571. ieee802_1x_mka_encode_live_peer_body,
  1572. ieee802_1x_mka_decode_live_peer_body,
  1573. ieee802_1x_mka_get_live_peer_length,
  1574. ieee802_1x_mka_live_peer_body_present
  1575. },
  1576. /* potential peer list parameter set */
  1577. {
  1578. ieee802_1x_mka_encode_potential_peer_body,
  1579. ieee802_1x_mka_decode_potential_peer_body,
  1580. ieee802_1x_mka_get_potential_peer_length,
  1581. ieee802_1x_mka_potential_peer_body_present
  1582. },
  1583. /* sak use parameter set */
  1584. {
  1585. ieee802_1x_mka_encode_sak_use_body,
  1586. ieee802_1x_mka_decode_sak_use_body,
  1587. ieee802_1x_mka_get_sak_use_length,
  1588. ieee802_1x_mka_sak_use_body_present
  1589. },
  1590. /* distribute sak parameter set */
  1591. {
  1592. ieee802_1x_mka_encode_dist_sak_body,
  1593. ieee802_1x_mka_decode_dist_sak_body,
  1594. ieee802_1x_mka_get_dist_sak_length,
  1595. ieee802_1x_mka_dist_sak_body_present
  1596. },
  1597. /* distribute cak parameter set */
  1598. {
  1599. NULL,
  1600. ieee802_1x_mka_decode_dist_cak_body,
  1601. NULL,
  1602. NULL
  1603. },
  1604. /* kmd parameter set */
  1605. {
  1606. NULL,
  1607. ieee802_1x_mka_decode_kmd_body,
  1608. NULL,
  1609. NULL
  1610. },
  1611. /* announce parameter set */
  1612. {
  1613. NULL,
  1614. ieee802_1x_mka_decode_announce_body,
  1615. NULL,
  1616. NULL
  1617. },
  1618. /* icv parameter set */
  1619. {
  1620. ieee802_1x_mka_encode_icv_body,
  1621. NULL,
  1622. ieee802_1x_mka_get_icv_length,
  1623. ieee802_1x_mka_icv_body_present
  1624. },
  1625. };
  1626. /**
  1627. * ieee802_1x_kay_deinit_data_key -
  1628. */
  1629. static void ieee802_1x_kay_deinit_data_key(struct data_key *pkey)
  1630. {
  1631. if (!pkey)
  1632. return;
  1633. pkey->user--;
  1634. if (pkey->user > 1)
  1635. return;
  1636. dl_list_del(&pkey->list);
  1637. os_free(pkey->key);
  1638. os_free(pkey);
  1639. }
  1640. /**
  1641. * ieee802_1x_kay_generate_new_sak -
  1642. */
  1643. static int
  1644. ieee802_1x_kay_generate_new_sak(struct ieee802_1x_mka_participant *participant)
  1645. {
  1646. struct data_key *sa_key = NULL;
  1647. struct key_conf *conf;
  1648. struct ieee802_1x_kay_peer *peer;
  1649. struct ieee802_1x_kay *kay = participant->kay;
  1650. int ctx_len, ctx_offset;
  1651. u8 *context;
  1652. /* check condition for generating a fresh SAK:
  1653. * must have one live peer
  1654. * and MKA life time elapse since last distribution
  1655. * or potential peer is empty
  1656. */
  1657. if (dl_list_empty(&participant->live_peers)) {
  1658. wpa_printf(MSG_ERROR,
  1659. "KaY: Live peers list must not empty when generating fresh SAK");
  1660. return -1;
  1661. }
  1662. /* FIXME: A fresh SAK not generated until
  1663. * the live peer list contains at least one peer and
  1664. * MKA life time has elapsed since the prior SAK was first distributed,
  1665. * or the Key server's potential peer is empty
  1666. * but I can't understand the second item, so
  1667. * here only check first item and ingore
  1668. * && (!dl_list_empty(&participant->potential_peers))) {
  1669. */
  1670. if ((time(NULL) - kay->dist_time) < MKA_LIFE_TIME / 1000) {
  1671. wpa_printf(MSG_ERROR,
  1672. "KaY: Life time have not elapsed since prior SAK distributed");
  1673. return -1;
  1674. }
  1675. conf = os_zalloc(sizeof(*conf));
  1676. if (!conf) {
  1677. wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
  1678. return -1;
  1679. }
  1680. conf->key_len = cipher_suite_tbl[kay->macsec_csindex].sak_len;
  1681. conf->key = os_zalloc(conf->key_len);
  1682. if (!conf->key) {
  1683. os_free(conf);
  1684. wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
  1685. return -1;
  1686. }
  1687. ctx_len = conf->key_len + sizeof(kay->dist_kn);
  1688. dl_list_for_each(peer, &participant->live_peers,
  1689. struct ieee802_1x_kay_peer, list)
  1690. ctx_len += sizeof(peer->mi);
  1691. ctx_len += sizeof(participant->mi);
  1692. context = os_zalloc(ctx_len);
  1693. if (!context) {
  1694. os_free(conf->key);
  1695. os_free(conf);
  1696. return -1;
  1697. }
  1698. ctx_offset = 0;
  1699. if (os_get_random(context + ctx_offset, conf->key_len) < 0) {
  1700. os_free(context);
  1701. os_free(conf->key);
  1702. os_free(conf);
  1703. return -1;
  1704. }
  1705. ctx_offset += conf->key_len;
  1706. dl_list_for_each(peer, &participant->live_peers,
  1707. struct ieee802_1x_kay_peer, list) {
  1708. os_memcpy(context + ctx_offset, peer->mi, sizeof(peer->mi));
  1709. ctx_offset += sizeof(peer->mi);
  1710. }
  1711. os_memcpy(context + ctx_offset, participant->mi,
  1712. sizeof(participant->mi));
  1713. ctx_offset += sizeof(participant->mi);
  1714. os_memcpy(context + ctx_offset, &kay->dist_kn, sizeof(kay->dist_kn));
  1715. if (conf->key_len == 16) {
  1716. ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
  1717. context, ctx_len, conf->key);
  1718. } else if (conf->key_len == 32) {
  1719. ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
  1720. context, ctx_len, conf->key);
  1721. } else {
  1722. wpa_printf(MSG_ERROR, "KaY: SAK Length not support");
  1723. os_free(conf->key);
  1724. os_free(conf);
  1725. os_free(context);
  1726. return -1;
  1727. }
  1728. wpa_hexdump(MSG_DEBUG, "KaY: generated new SAK",
  1729. conf->key, conf->key_len);
  1730. os_memcpy(conf->ki.mi, participant->mi, MI_LEN);
  1731. conf->ki.kn = participant->kay->dist_kn;
  1732. conf->an = participant->kay->dist_an;
  1733. conf->offset = kay->macsec_confidentiality;
  1734. conf->rx = TRUE;
  1735. conf->tx = TRUE;
  1736. sa_key = ieee802_1x_kay_init_data_key(conf);
  1737. if (!sa_key) {
  1738. os_free(conf->key);
  1739. os_free(conf);
  1740. os_free(context);
  1741. return -1;
  1742. }
  1743. participant->new_key = sa_key;
  1744. dl_list_add(&participant->sak_list, &sa_key->list);
  1745. ieee802_1x_cp_set_ciphersuite(participant->kay->cp,
  1746. cipher_suite_tbl[kay->macsec_csindex].id);
  1747. ieee802_1x_cp_sm_step(kay->cp);
  1748. ieee802_1x_cp_set_offset(kay->cp, conf->offset);
  1749. ieee802_1x_cp_sm_step(kay->cp);
  1750. ieee802_1x_cp_set_distributedki(kay->cp, &conf->ki);
  1751. ieee802_1x_cp_set_distributedan(kay->cp, conf->an);
  1752. ieee802_1x_cp_signal_newsak(kay->cp);
  1753. ieee802_1x_cp_sm_step(kay->cp);
  1754. dl_list_for_each(peer, &participant->live_peers,
  1755. struct ieee802_1x_kay_peer, list)
  1756. peer->sak_used = FALSE;
  1757. participant->kay->dist_kn++;
  1758. participant->kay->dist_an++;
  1759. if (participant->kay->dist_an > 3)
  1760. participant->kay->dist_an = 0;
  1761. participant->kay->dist_time = time(NULL);
  1762. os_free(conf->key);
  1763. os_free(conf);
  1764. os_free(context);
  1765. return 0;
  1766. }
  1767. /**
  1768. * ieee802_1x_kay_elect_key_server - elect the key server
  1769. * when to elect: whenever the live peers list changes
  1770. */
  1771. static int
  1772. ieee802_1x_kay_elect_key_server(struct ieee802_1x_mka_participant *participant)
  1773. {
  1774. struct ieee802_1x_kay_peer *peer;
  1775. struct ieee802_1x_kay_peer *key_server = NULL;
  1776. struct ieee802_1x_kay *kay = participant->kay;
  1777. Boolean i_is_key_server;
  1778. if (participant->is_obliged_key_server) {
  1779. participant->new_sak = TRUE;
  1780. participant->to_dist_sak = FALSE;
  1781. ieee802_1x_cp_set_electedself(kay->cp, TRUE);
  1782. return 0;
  1783. }
  1784. /* elect the key server among the peers */
  1785. dl_list_for_each(peer, &participant->live_peers,
  1786. struct ieee802_1x_kay_peer, list) {
  1787. if (!peer->is_key_server)
  1788. continue;
  1789. if (!key_server) {
  1790. key_server = peer;
  1791. continue;
  1792. }
  1793. if (peer->key_server_priority <
  1794. key_server->key_server_priority) {
  1795. key_server = peer;
  1796. } else if (peer->key_server_priority ==
  1797. key_server->key_server_priority) {
  1798. if (os_memcmp(peer->sci.addr, key_server->sci.addr,
  1799. ETH_ALEN) < 0)
  1800. key_server = peer;
  1801. }
  1802. }
  1803. /* elect the key server between me and the above elected peer */
  1804. i_is_key_server = FALSE;
  1805. if (key_server && participant->can_be_key_server) {
  1806. if (kay->actor_priority
  1807. < key_server->key_server_priority) {
  1808. i_is_key_server = TRUE;
  1809. } else if (kay->actor_priority
  1810. == key_server->key_server_priority) {
  1811. if (os_memcmp(kay->actor_sci.addr, key_server->sci.addr,
  1812. ETH_ALEN) < 0)
  1813. i_is_key_server = TRUE;
  1814. }
  1815. } else if (participant->can_be_key_server) {
  1816. i_is_key_server = TRUE;
  1817. }
  1818. if (i_is_key_server) {
  1819. ieee802_1x_cp_set_electedself(kay->cp, TRUE);
  1820. if (!sci_equal(&kay->key_server_sci, &kay->actor_sci)) {
  1821. ieee802_1x_cp_signal_chgdserver(kay->cp);
  1822. ieee802_1x_cp_sm_step(kay->cp);
  1823. }
  1824. participant->is_key_server = TRUE;
  1825. participant->principal = TRUE;
  1826. participant->new_sak = TRUE;
  1827. wpa_printf(MSG_DEBUG, "KaY: I is elected as key server");
  1828. participant->to_dist_sak = FALSE;
  1829. participant->is_elected = TRUE;
  1830. os_memcpy(&kay->key_server_sci, &kay->actor_sci,
  1831. sizeof(kay->key_server_sci));
  1832. kay->key_server_priority = kay->actor_priority;
  1833. } else if (key_server) {
  1834. ieee802_1x_cp_set_electedself(kay->cp, FALSE);
  1835. if (!sci_equal(&kay->key_server_sci, &key_server->sci)) {
  1836. ieee802_1x_cp_signal_chgdserver(kay->cp);
  1837. ieee802_1x_cp_sm_step(kay->cp);
  1838. }
  1839. participant->is_key_server = FALSE;
  1840. participant->principal = TRUE;
  1841. participant->is_elected = TRUE;
  1842. os_memcpy(&kay->key_server_sci, &key_server->sci,
  1843. sizeof(kay->key_server_sci));
  1844. kay->key_server_priority = key_server->key_server_priority;
  1845. } else {
  1846. participant->principal = FALSE;
  1847. participant->is_key_server = FALSE;
  1848. participant->is_elected = FALSE;
  1849. }
  1850. return 0;
  1851. }
  1852. /**
  1853. * ieee802_1x_kay_decide_macsec_use - the key server determinate
  1854. * how to use MACsec: whether use MACsec and its capability
  1855. * protectFrames will be advised if the key server and one of its live peers are
  1856. * MACsec capable and one of those request MACsec protection
  1857. */
  1858. static int
  1859. ieee802_1x_kay_decide_macsec_use(
  1860. struct ieee802_1x_mka_participant *participant)
  1861. {
  1862. struct ieee802_1x_kay *kay = participant->kay;
  1863. struct ieee802_1x_kay_peer *peer;
  1864. enum macsec_cap less_capability;
  1865. Boolean has_peer;
  1866. if (!participant->is_key_server)
  1867. return -1;
  1868. /* key server self is MACsec-desired and requesting MACsec */
  1869. if (!kay->macsec_desired) {
  1870. participant->advised_desired = FALSE;
  1871. return -1;
  1872. }
  1873. if (kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
  1874. participant->advised_desired = FALSE;
  1875. return -1;
  1876. }
  1877. less_capability = kay->macsec_capable;
  1878. /* at least one of peers is MACsec-desired and requesting MACsec */
  1879. has_peer = FALSE;
  1880. dl_list_for_each(peer, &participant->live_peers,
  1881. struct ieee802_1x_kay_peer, list) {
  1882. if (!peer->macsec_desired)
  1883. continue;
  1884. if (peer->macsec_capability == MACSEC_CAP_NOT_IMPLEMENTED)
  1885. continue;
  1886. less_capability = (less_capability < peer->macsec_capability) ?
  1887. less_capability : peer->macsec_capability;
  1888. has_peer = TRUE;
  1889. }
  1890. if (has_peer) {
  1891. participant->advised_desired = TRUE;
  1892. participant->advised_capability = less_capability;
  1893. kay->authenticated = FALSE;
  1894. kay->secured = TRUE;
  1895. kay->failed = FALSE;
  1896. ieee802_1x_cp_connect_secure(kay->cp);
  1897. ieee802_1x_cp_sm_step(kay->cp);
  1898. } else {
  1899. participant->advised_desired = FALSE;
  1900. participant->advised_capability = MACSEC_CAP_NOT_IMPLEMENTED;
  1901. participant->to_use_sak = FALSE;
  1902. kay->authenticated = TRUE;
  1903. kay->secured = FALSE;
  1904. kay->failed = FALSE;
  1905. kay->ltx_kn = 0;
  1906. kay->ltx_an = 0;
  1907. kay->lrx_kn = 0;
  1908. kay->lrx_an = 0;
  1909. kay->otx_kn = 0;
  1910. kay->otx_an = 0;
  1911. kay->orx_kn = 0;
  1912. kay->orx_an = 0;
  1913. ieee802_1x_cp_connect_authenticated(kay->cp);
  1914. ieee802_1x_cp_sm_step(kay->cp);
  1915. }
  1916. return 0;
  1917. }
  1918. static const u8 pae_group_addr[ETH_ALEN] = {
  1919. 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03
  1920. };
  1921. /**
  1922. * ieee802_1x_kay_encode_mkpdu -
  1923. */
  1924. static int
  1925. ieee802_1x_kay_encode_mkpdu(struct ieee802_1x_mka_participant *participant,
  1926. struct wpabuf *pbuf)
  1927. {
  1928. unsigned int i;
  1929. struct ieee8023_hdr *ether_hdr;
  1930. struct ieee802_1x_hdr *eapol_hdr;
  1931. ether_hdr = wpabuf_put(pbuf, sizeof(*ether_hdr));
  1932. os_memcpy(ether_hdr->dest, pae_group_addr, sizeof(ether_hdr->dest));
  1933. os_memcpy(ether_hdr->src, participant->kay->actor_sci.addr,
  1934. sizeof(ether_hdr->dest));
  1935. ether_hdr->ethertype = host_to_be16(ETH_P_EAPOL);
  1936. eapol_hdr = wpabuf_put(pbuf, sizeof(*eapol_hdr));
  1937. eapol_hdr->version = EAPOL_VERSION;
  1938. eapol_hdr->type = IEEE802_1X_TYPE_EAPOL_MKA;
  1939. eapol_hdr->length = host_to_be16(pbuf->size - pbuf->used);
  1940. for (i = 0; i < ARRAY_SIZE(mka_body_handler); i++) {
  1941. if (mka_body_handler[i].body_present &&
  1942. mka_body_handler[i].body_present(participant)) {
  1943. if (mka_body_handler[i].body_tx(participant, pbuf))
  1944. return -1;
  1945. }
  1946. }
  1947. return 0;
  1948. }
  1949. /**
  1950. * ieee802_1x_participant_send_mkpdu -
  1951. */
  1952. static int
  1953. ieee802_1x_participant_send_mkpdu(
  1954. struct ieee802_1x_mka_participant *participant)
  1955. {
  1956. struct wpabuf *buf;
  1957. struct ieee802_1x_kay *kay = participant->kay;
  1958. size_t length = 0;
  1959. unsigned int i;
  1960. wpa_printf(MSG_DEBUG, "KaY: to enpacket and send the MKPDU");
  1961. length += sizeof(struct ieee802_1x_hdr) + sizeof(struct ieee8023_hdr);
  1962. for (i = 0; i < ARRAY_SIZE(mka_body_handler); i++) {
  1963. if (mka_body_handler[i].body_present &&
  1964. mka_body_handler[i].body_present(participant))
  1965. length += mka_body_handler[i].body_length(participant);
  1966. }
  1967. buf = wpabuf_alloc(length);
  1968. if (!buf) {
  1969. wpa_printf(MSG_ERROR, "KaY: out of memory");
  1970. return -1;
  1971. }
  1972. if (ieee802_1x_kay_encode_mkpdu(participant, buf)) {
  1973. wpa_printf(MSG_ERROR, "KaY: encode mkpdu fail!");
  1974. return -1;
  1975. }
  1976. l2_packet_send(kay->l2_mka, NULL, 0, wpabuf_head(buf), wpabuf_len(buf));
  1977. wpabuf_free(buf);
  1978. kay->active = TRUE;
  1979. participant->active = TRUE;
  1980. return 0;
  1981. }
  1982. static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa);
  1983. /**
  1984. * ieee802_1x_participant_timer -
  1985. */
  1986. static void ieee802_1x_participant_timer(void *eloop_ctx, void *timeout_ctx)
  1987. {
  1988. struct ieee802_1x_mka_participant *participant;
  1989. struct ieee802_1x_kay *kay;
  1990. struct ieee802_1x_kay_peer *peer, *pre_peer;
  1991. time_t now = time(NULL);
  1992. Boolean lp_changed;
  1993. struct receive_sc *rxsc, *pre_rxsc;
  1994. struct transmit_sa *txsa, *pre_txsa;
  1995. participant = (struct ieee802_1x_mka_participant *)eloop_ctx;
  1996. kay = participant->kay;
  1997. if (participant->cak_life) {
  1998. if (now > participant->cak_life) {
  1999. kay->authenticated = FALSE;
  2000. kay->secured = FALSE;
  2001. kay->failed = TRUE;
  2002. ieee802_1x_kay_delete_mka(kay, &participant->ckn);
  2003. return;
  2004. }
  2005. }
  2006. /* should delete MKA instance if there are not live peers
  2007. * when the MKA life elapsed since its creating */
  2008. if (participant->mka_life) {
  2009. if (dl_list_empty(&participant->live_peers)) {
  2010. if (now > participant->mka_life) {
  2011. kay->authenticated = FALSE;
  2012. kay->secured = FALSE;
  2013. kay->failed = TRUE;
  2014. ieee802_1x_kay_delete_mka(kay,
  2015. &participant->ckn);
  2016. return;
  2017. }
  2018. } else {
  2019. participant->mka_life = 0;
  2020. }
  2021. }
  2022. lp_changed = FALSE;
  2023. dl_list_for_each_safe(peer, pre_peer, &participant->live_peers,
  2024. struct ieee802_1x_kay_peer, list) {
  2025. if (now > peer->expire) {
  2026. wpa_printf(MSG_DEBUG, "KaY: Live peer removed");
  2027. wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
  2028. sizeof(peer->mi));
  2029. wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
  2030. dl_list_for_each_safe(rxsc, pre_rxsc,
  2031. &participant->rxsc_list,
  2032. struct receive_sc, list) {
  2033. if (sci_equal(&rxsc->sci, &peer->sci)) {
  2034. secy_delete_receive_sc(kay, rxsc);
  2035. ieee802_1x_kay_deinit_receive_sc(
  2036. participant, rxsc);
  2037. }
  2038. }
  2039. dl_list_del(&peer->list);
  2040. os_free(peer);
  2041. lp_changed = TRUE;
  2042. }
  2043. }
  2044. if (lp_changed) {
  2045. if (dl_list_empty(&participant->live_peers)) {
  2046. participant->advised_desired = FALSE;
  2047. participant->advised_capability =
  2048. MACSEC_CAP_NOT_IMPLEMENTED;
  2049. participant->to_use_sak = FALSE;
  2050. kay->authenticated = TRUE;
  2051. kay->secured = FALSE;
  2052. kay->failed = FALSE;
  2053. kay->ltx_kn = 0;
  2054. kay->ltx_an = 0;
  2055. kay->lrx_kn = 0;
  2056. kay->lrx_an = 0;
  2057. kay->otx_kn = 0;
  2058. kay->otx_an = 0;
  2059. kay->orx_kn = 0;
  2060. kay->orx_an = 0;
  2061. dl_list_for_each_safe(txsa, pre_txsa,
  2062. &participant->txsc->sa_list,
  2063. struct transmit_sa, list) {
  2064. secy_disable_transmit_sa(kay, txsa);
  2065. ieee802_1x_kay_deinit_transmit_sa(txsa);
  2066. }
  2067. ieee802_1x_cp_connect_authenticated(kay->cp);
  2068. ieee802_1x_cp_sm_step(kay->cp);
  2069. } else {
  2070. ieee802_1x_kay_elect_key_server(participant);
  2071. ieee802_1x_kay_decide_macsec_use(participant);
  2072. }
  2073. }
  2074. dl_list_for_each_safe(peer, pre_peer, &participant->potential_peers,
  2075. struct ieee802_1x_kay_peer, list) {
  2076. if (now > peer->expire) {
  2077. wpa_printf(MSG_DEBUG, "KaY: Potential peer removed");
  2078. wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
  2079. sizeof(peer->mi));
  2080. wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
  2081. dl_list_del(&peer->list);
  2082. os_free(peer);
  2083. }
  2084. }
  2085. if (participant->new_sak) {
  2086. if (!ieee802_1x_kay_generate_new_sak(participant))
  2087. participant->to_dist_sak = TRUE;
  2088. participant->new_sak = FALSE;
  2089. }
  2090. if (participant->retry_count < MAX_RETRY_CNT) {
  2091. ieee802_1x_participant_send_mkpdu(participant);
  2092. participant->retry_count++;
  2093. }
  2094. eloop_register_timeout(MKA_HELLO_TIME / 1000, 0,
  2095. ieee802_1x_participant_timer,
  2096. participant, NULL);
  2097. }
  2098. /**
  2099. * ieee802_1x_kay_init_transmit_sa -
  2100. */
  2101. static struct transmit_sa *
  2102. ieee802_1x_kay_init_transmit_sa(struct transmit_sc *psc, u8 an, u32 next_PN,
  2103. struct data_key *key)
  2104. {
  2105. struct transmit_sa *psa;
  2106. key->tx_latest = TRUE;
  2107. key->rx_latest = TRUE;
  2108. psa = os_zalloc(sizeof(*psa));
  2109. if (!psa) {
  2110. wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
  2111. return NULL;
  2112. }
  2113. if (key->confidentiality_offset >= CONFIDENTIALITY_OFFSET_0 &&
  2114. key->confidentiality_offset <= CONFIDENTIALITY_OFFSET_50)
  2115. psa->confidentiality = TRUE;
  2116. else
  2117. psa->confidentiality = FALSE;
  2118. psa->an = an;
  2119. psa->pkey = key;
  2120. psa->next_pn = next_PN;
  2121. psa->sc = psc;
  2122. os_get_time(&psa->created_time);
  2123. psa->in_use = FALSE;
  2124. dl_list_add(&psc->sa_list, &psa->list);
  2125. wpa_printf(MSG_DEBUG,
  2126. "KaY: Create transmit SA(an: %d, next_PN: %u) of SC(channel: %d)",
  2127. (int) an, next_PN, psc->channel);
  2128. return psa;
  2129. }
  2130. /**
  2131. * ieee802_1x_kay_deinit_transmit_sa -
  2132. */
  2133. static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa)
  2134. {
  2135. psa->pkey = NULL;
  2136. wpa_printf(MSG_DEBUG,
  2137. "KaY: Delete transmit SA(an: %d) of SC(channel: %d)",
  2138. psa->an, psa->sc->channel);
  2139. dl_list_del(&psa->list);
  2140. os_free(psa);
  2141. }
  2142. /**
  2143. * init_transmit_sc -
  2144. */
  2145. static struct transmit_sc *
  2146. ieee802_1x_kay_init_transmit_sc(const struct ieee802_1x_mka_sci *sci,
  2147. int channel)
  2148. {
  2149. struct transmit_sc *psc;
  2150. psc = os_zalloc(sizeof(*psc));
  2151. if (!psc) {
  2152. wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
  2153. return NULL;
  2154. }
  2155. os_memcpy(&psc->sci, sci, sizeof(psc->sci));
  2156. psc->channel = channel;
  2157. os_get_time(&psc->created_time);
  2158. psc->transmitting = FALSE;
  2159. psc->encoding_sa = FALSE;
  2160. psc->enciphering_sa = FALSE;
  2161. dl_list_init(&psc->sa_list);
  2162. wpa_printf(MSG_DEBUG, "KaY: Create transmit SC(channel: %d)", channel);
  2163. wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)sci , sizeof(*sci));
  2164. return psc;
  2165. }
  2166. /**
  2167. * ieee802_1x_kay_deinit_transmit_sc -
  2168. */
  2169. static void
  2170. ieee802_1x_kay_deinit_transmit_sc(
  2171. struct ieee802_1x_mka_participant *participant, struct transmit_sc *psc)
  2172. {
  2173. struct transmit_sa *psa, *tmp;
  2174. wpa_printf(MSG_DEBUG, "KaY: Delete transmit SC(channel: %d)",
  2175. psc->channel);
  2176. dl_list_for_each_safe(psa, tmp, &psc->sa_list, struct transmit_sa,
  2177. list) {
  2178. secy_disable_transmit_sa(participant->kay, psa);
  2179. ieee802_1x_kay_deinit_transmit_sa(psa);
  2180. }
  2181. os_free(psc);
  2182. }
  2183. /****************** Interface between CP and KAY *********************/
  2184. /**
  2185. * ieee802_1x_kay_set_latest_sa_attr -
  2186. */
  2187. int ieee802_1x_kay_set_latest_sa_attr(struct ieee802_1x_kay *kay,
  2188. struct ieee802_1x_mka_ki *lki, u8 lan,
  2189. Boolean ltx, Boolean lrx)
  2190. {
  2191. struct ieee802_1x_mka_participant *principal;
  2192. principal = ieee802_1x_kay_get_principal_participant(kay);
  2193. if (!principal)
  2194. return -1;
  2195. if (!lki)
  2196. os_memset(&principal->lki, 0, sizeof(principal->lki));
  2197. else
  2198. os_memcpy(&principal->lki, lki, sizeof(principal->lki));
  2199. principal->lan = lan;
  2200. principal->ltx = ltx;
  2201. principal->lrx = lrx;
  2202. if (!lki) {
  2203. kay->ltx_kn = 0;
  2204. kay->lrx_kn = 0;
  2205. } else {
  2206. kay->ltx_kn = lki->kn;
  2207. kay->lrx_kn = lki->kn;
  2208. }
  2209. kay->ltx_an = lan;
  2210. kay->lrx_an = lan;
  2211. return 0;
  2212. }
  2213. /**
  2214. * ieee802_1x_kay_set_old_sa_attr -
  2215. */
  2216. int ieee802_1x_kay_set_old_sa_attr(struct ieee802_1x_kay *kay,
  2217. struct ieee802_1x_mka_ki *oki,
  2218. u8 oan, Boolean otx, Boolean orx)
  2219. {
  2220. struct ieee802_1x_mka_participant *principal;
  2221. principal = ieee802_1x_kay_get_principal_participant(kay);
  2222. if (!principal)
  2223. return -1;
  2224. if (!oki)
  2225. os_memset(&principal->oki, 0, sizeof(principal->oki));
  2226. else
  2227. os_memcpy(&principal->oki, oki, sizeof(principal->oki));
  2228. principal->oan = oan;
  2229. principal->otx = otx;
  2230. principal->orx = orx;
  2231. if (!oki) {
  2232. kay->otx_kn = 0;
  2233. kay->orx_kn = 0;
  2234. } else {
  2235. kay->otx_kn = oki->kn;
  2236. kay->orx_kn = oki->kn;
  2237. }
  2238. kay->otx_an = oan;
  2239. kay->orx_an = oan;
  2240. return 0;
  2241. }
  2242. /**
  2243. * ieee802_1x_kay_create_sas -
  2244. */
  2245. int ieee802_1x_kay_create_sas(struct ieee802_1x_kay *kay,
  2246. struct ieee802_1x_mka_ki *lki)
  2247. {
  2248. struct data_key *sa_key, *latest_sak;
  2249. struct ieee802_1x_mka_participant *principal;
  2250. struct receive_sc *rxsc;
  2251. struct receive_sa *rxsa;
  2252. struct transmit_sa *txsa;
  2253. principal = ieee802_1x_kay_get_principal_participant(kay);
  2254. if (!principal)
  2255. return -1;
  2256. latest_sak = NULL;
  2257. dl_list_for_each(sa_key, &principal->sak_list, struct data_key, list) {
  2258. if (is_ki_equal(&sa_key->key_identifier, lki)) {
  2259. sa_key->rx_latest = TRUE;
  2260. sa_key->tx_latest = TRUE;
  2261. latest_sak = sa_key;
  2262. principal->to_use_sak = TRUE;
  2263. } else {
  2264. sa_key->rx_latest = FALSE;
  2265. sa_key->tx_latest = FALSE;
  2266. }
  2267. }
  2268. if (!latest_sak) {
  2269. wpa_printf(MSG_ERROR, "lki related sak not found");
  2270. return -1;
  2271. }
  2272. dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
  2273. rxsa = ieee802_1x_kay_init_receive_sa(rxsc, latest_sak->an, 1,
  2274. latest_sak);
  2275. if (!rxsa)
  2276. return -1;
  2277. secy_create_receive_sa(kay, rxsa);
  2278. }
  2279. txsa = ieee802_1x_kay_init_transmit_sa(principal->txsc, latest_sak->an,
  2280. 1, latest_sak);
  2281. if (!txsa)
  2282. return -1;
  2283. secy_create_transmit_sa(kay, txsa);
  2284. return 0;
  2285. }
  2286. /**
  2287. * ieee802_1x_kay_delete_sas -
  2288. */
  2289. int ieee802_1x_kay_delete_sas(struct ieee802_1x_kay *kay,
  2290. struct ieee802_1x_mka_ki *ki)
  2291. {
  2292. struct data_key *sa_key, *pre_key;
  2293. struct transmit_sa *txsa, *pre_txsa;
  2294. struct receive_sa *rxsa, *pre_rxsa;
  2295. struct receive_sc *rxsc;
  2296. struct ieee802_1x_mka_participant *principal;
  2297. wpa_printf(MSG_DEBUG, "KaY: Entry into %s", __func__);
  2298. principal = ieee802_1x_kay_get_principal_participant(kay);
  2299. if (!principal)
  2300. return -1;
  2301. /* remove the transmit sa */
  2302. dl_list_for_each_safe(txsa, pre_txsa, &principal->txsc->sa_list,
  2303. struct transmit_sa, list) {
  2304. if (is_ki_equal(&txsa->pkey->key_identifier, ki)) {
  2305. secy_disable_transmit_sa(kay, txsa);
  2306. ieee802_1x_kay_deinit_transmit_sa(txsa);
  2307. }
  2308. }
  2309. /* remove the receive sa */
  2310. dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
  2311. dl_list_for_each_safe(rxsa, pre_rxsa, &rxsc->sa_list,
  2312. struct receive_sa, list) {
  2313. if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
  2314. secy_disable_receive_sa(kay, rxsa);
  2315. ieee802_1x_kay_deinit_receive_sa(rxsa);
  2316. }
  2317. }
  2318. }
  2319. /* remove the sak */
  2320. dl_list_for_each_safe(sa_key, pre_key, &principal->sak_list,
  2321. struct data_key, list) {
  2322. if (is_ki_equal(&sa_key->key_identifier, ki)) {
  2323. ieee802_1x_kay_deinit_data_key(sa_key);
  2324. break;
  2325. }
  2326. if (principal->new_key == sa_key)
  2327. principal->new_key = NULL;
  2328. }
  2329. return 0;
  2330. }
  2331. /**
  2332. * ieee802_1x_kay_enable_tx_sas -
  2333. */
  2334. int ieee802_1x_kay_enable_tx_sas(struct ieee802_1x_kay *kay,
  2335. struct ieee802_1x_mka_ki *lki)
  2336. {
  2337. struct ieee802_1x_mka_participant *principal;
  2338. struct transmit_sa *txsa;
  2339. principal = ieee802_1x_kay_get_principal_participant(kay);
  2340. if (!principal)
  2341. return -1;
  2342. dl_list_for_each(txsa, &principal->txsc->sa_list, struct transmit_sa,
  2343. list) {
  2344. if (is_ki_equal(&txsa->pkey->key_identifier, lki)) {
  2345. txsa->in_use = TRUE;
  2346. secy_enable_transmit_sa(kay, txsa);
  2347. ieee802_1x_cp_set_usingtransmitas(
  2348. principal->kay->cp, TRUE);
  2349. ieee802_1x_cp_sm_step(principal->kay->cp);
  2350. }
  2351. }
  2352. return 0;
  2353. }
  2354. /**
  2355. * ieee802_1x_kay_enable_rx_sas -
  2356. */
  2357. int ieee802_1x_kay_enable_rx_sas(struct ieee802_1x_kay *kay,
  2358. struct ieee802_1x_mka_ki *lki)
  2359. {
  2360. struct ieee802_1x_mka_participant *principal;
  2361. struct receive_sa *rxsa;
  2362. struct receive_sc *rxsc;
  2363. principal = ieee802_1x_kay_get_principal_participant(kay);
  2364. if (!principal)
  2365. return -1;
  2366. dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
  2367. dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
  2368. {
  2369. if (is_ki_equal(&rxsa->pkey->key_identifier, lki)) {
  2370. rxsa->in_use = TRUE;
  2371. secy_enable_receive_sa(kay, rxsa);
  2372. ieee802_1x_cp_set_usingreceivesas(
  2373. principal->kay->cp, TRUE);
  2374. ieee802_1x_cp_sm_step(principal->kay->cp);
  2375. }
  2376. }
  2377. }
  2378. return 0;
  2379. }
  2380. /**
  2381. * ieee802_1x_kay_enable_new_info -
  2382. */
  2383. int ieee802_1x_kay_enable_new_info(struct ieee802_1x_kay *kay)
  2384. {
  2385. struct ieee802_1x_mka_participant *principal;
  2386. principal = ieee802_1x_kay_get_principal_participant(kay);
  2387. if (!principal)
  2388. return -1;
  2389. if (principal->retry_count < MAX_RETRY_CNT) {
  2390. ieee802_1x_participant_send_mkpdu(principal);
  2391. principal->retry_count++;
  2392. }
  2393. return 0;
  2394. }
  2395. /**
  2396. * ieee802_1x_kay_cp_conf -
  2397. */
  2398. int ieee802_1x_kay_cp_conf(struct ieee802_1x_kay *kay,
  2399. struct ieee802_1x_cp_conf *pconf)
  2400. {
  2401. pconf->protect = kay->macsec_protect;
  2402. pconf->replay_protect = kay->macsec_replay_protect;
  2403. pconf->validate = kay->macsec_validate;
  2404. return 0;
  2405. }
  2406. /**
  2407. * ieee802_1x_kay_alloc_cp_sm -
  2408. */
  2409. static struct ieee802_1x_cp_sm *
  2410. ieee802_1x_kay_alloc_cp_sm(struct ieee802_1x_kay *kay)
  2411. {
  2412. struct ieee802_1x_cp_conf conf;
  2413. os_memset(&conf, 0, sizeof(conf));
  2414. conf.protect = kay->macsec_protect;
  2415. conf.replay_protect = kay->macsec_replay_protect;
  2416. conf.validate = kay->macsec_validate;
  2417. conf.replay_window = kay->macsec_replay_window;
  2418. return ieee802_1x_cp_sm_init(kay, &conf);
  2419. }
  2420. /**
  2421. * ieee802_1x_kay_mkpdu_sanity_check -
  2422. * sanity check specified in clause 11.11.2 of IEEE802.1X-2010
  2423. */
  2424. static int ieee802_1x_kay_mkpdu_sanity_check(struct ieee802_1x_kay *kay,
  2425. const u8 *buf, size_t len)
  2426. {
  2427. struct ieee8023_hdr *eth_hdr;
  2428. struct ieee802_1x_hdr *eapol_hdr;
  2429. struct ieee802_1x_mka_hdr *mka_hdr;
  2430. struct ieee802_1x_mka_basic_body *body;
  2431. size_t mka_msg_len;
  2432. struct ieee802_1x_mka_participant *participant;
  2433. size_t body_len;
  2434. u8 icv[MAX_ICV_LEN];
  2435. u8 *msg_icv;
  2436. eth_hdr = (struct ieee8023_hdr *) buf;
  2437. eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
  2438. mka_hdr = (struct ieee802_1x_mka_hdr *) (eapol_hdr + 1);
  2439. /* destination address should be not individual address */
  2440. if (os_memcmp(eth_hdr->dest, pae_group_addr, ETH_ALEN) != 0) {
  2441. wpa_printf(MSG_MSGDUMP,
  2442. "KaY: ethernet destination address is not PAE group address");
  2443. return -1;
  2444. }
  2445. /* MKPDU should not less than 32 octets */
  2446. mka_msg_len = be_to_host16(eapol_hdr->length);
  2447. if (mka_msg_len < 32) {
  2448. wpa_printf(MSG_MSGDUMP, "KaY: MKPDU is less than 32 octets");
  2449. return -1;
  2450. }
  2451. /* MKPDU should multiple 4 octets */
  2452. if ((mka_msg_len % 4) != 0) {
  2453. wpa_printf(MSG_MSGDUMP,
  2454. "KaY: MKPDU is not multiple of 4 octets");
  2455. return -1;
  2456. }
  2457. body = (struct ieee802_1x_mka_basic_body *) mka_hdr;
  2458. ieee802_1x_mka_dump_basic_body(body);
  2459. body_len = get_mka_param_body_len(body);
  2460. /* EAPOL-MKA body should comprise basic parameter set and ICV */
  2461. if (mka_msg_len < MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN) {
  2462. wpa_printf(MSG_ERROR,
  2463. "KaY: Received EAPOL-MKA Packet Body Length (%d bytes) is less than the Basic Parameter Set Header Length (%d bytes) + the Basic Parameter Set Body Length (%d bytes) + %d bytes of ICV",
  2464. (int) mka_msg_len, (int) MKA_HDR_LEN,
  2465. (int) body_len, DEFAULT_ICV_LEN);
  2466. return -1;
  2467. }
  2468. /* CKN should be owned by I */
  2469. participant = ieee802_1x_kay_get_participant(kay, body->ckn);
  2470. if (!participant) {
  2471. wpa_printf(MSG_DEBUG, "CKN is not included in my CA");
  2472. return -1;
  2473. }
  2474. /* algorithm agility check */
  2475. if (os_memcmp(body->algo_agility, mka_algo_agility,
  2476. sizeof(body->algo_agility)) != 0) {
  2477. wpa_printf(MSG_ERROR,
  2478. "KaY: peer's algorithm agility not supported for me");
  2479. return -1;
  2480. }
  2481. /* ICV check */
  2482. /*
  2483. * The ICV will comprise the final octets of the packet body, whatever
  2484. * its size, not the fixed length 16 octets, indicated by the EAPOL
  2485. * packet body length.
  2486. */
  2487. if (mka_alg_tbl[kay->mka_algindex].icv_hash(
  2488. participant->ick.key,
  2489. buf, len - mka_alg_tbl[kay->mka_algindex].icv_len, icv)) {
  2490. wpa_printf(MSG_ERROR, "KaY: omac1_aes_128 failed");
  2491. return -1;
  2492. }
  2493. msg_icv = ieee802_1x_mka_decode_icv_body(participant, (u8 *) mka_hdr,
  2494. mka_msg_len);
  2495. if (msg_icv) {
  2496. if (os_memcmp_const(msg_icv, icv,
  2497. mka_alg_tbl[kay->mka_algindex].icv_len) !=
  2498. 0) {
  2499. wpa_printf(MSG_ERROR,
  2500. "KaY: Computed ICV is not equal to Received ICV");
  2501. return -1;
  2502. }
  2503. } else {
  2504. wpa_printf(MSG_ERROR, "KaY: No ICV");
  2505. return -1;
  2506. }
  2507. return 0;
  2508. }
  2509. /**
  2510. * ieee802_1x_kay_decode_mkpdu -
  2511. */
  2512. static int ieee802_1x_kay_decode_mkpdu(struct ieee802_1x_kay *kay,
  2513. const u8 *buf, size_t len)
  2514. {
  2515. struct ieee802_1x_mka_participant *participant;
  2516. struct ieee802_1x_mka_hdr *hdr;
  2517. size_t body_len;
  2518. size_t left_len;
  2519. u8 body_type;
  2520. int i;
  2521. const u8 *pos;
  2522. Boolean my_included;
  2523. Boolean handled[256];
  2524. if (ieee802_1x_kay_mkpdu_sanity_check(kay, buf, len))
  2525. return -1;
  2526. /* handle basic parameter set */
  2527. pos = buf + sizeof(struct ieee8023_hdr) + sizeof(struct ieee802_1x_hdr);
  2528. left_len = len - sizeof(struct ieee8023_hdr) -
  2529. sizeof(struct ieee802_1x_hdr);
  2530. participant = ieee802_1x_mka_decode_basic_body(kay, pos, left_len);
  2531. if (!participant)
  2532. return -1;
  2533. /* to skip basic parameter set */
  2534. hdr = (struct ieee802_1x_mka_hdr *) pos;
  2535. body_len = get_mka_param_body_len(hdr);
  2536. pos += body_len + MKA_HDR_LEN;
  2537. left_len -= body_len + MKA_HDR_LEN;
  2538. /* check i am in the peer's peer list */
  2539. my_included = ieee802_1x_mka_i_in_peerlist(participant, pos, left_len);
  2540. if (my_included) {
  2541. /* accept the peer as live peer */
  2542. if (!ieee802_1x_kay_is_in_peer(
  2543. participant,
  2544. participant->current_peer_id.mi)) {
  2545. if (!ieee802_1x_kay_create_live_peer(
  2546. participant,
  2547. participant->current_peer_id.mi,
  2548. be_to_host32(
  2549. participant->current_peer_id.mn)))
  2550. return -1;
  2551. ieee802_1x_kay_elect_key_server(participant);
  2552. ieee802_1x_kay_decide_macsec_use(participant);
  2553. }
  2554. if (ieee802_1x_kay_is_in_potential_peer(
  2555. participant, participant->current_peer_id.mi)) {
  2556. if (!ieee802_1x_kay_move_live_peer(
  2557. participant,
  2558. participant->current_peer_id.mi,
  2559. be_to_host32(participant->
  2560. current_peer_id.mn)))
  2561. return -1;
  2562. ieee802_1x_kay_elect_key_server(participant);
  2563. ieee802_1x_kay_decide_macsec_use(participant);
  2564. }
  2565. }
  2566. /*
  2567. * Handle other parameter set than basic parameter set.
  2568. * Each parameter set should be present only once.
  2569. */
  2570. for (i = 0; i < 256; i++)
  2571. handled[i] = FALSE;
  2572. handled[0] = TRUE;
  2573. while (left_len > MKA_HDR_LEN + DEFAULT_ICV_LEN) {
  2574. hdr = (struct ieee802_1x_mka_hdr *) pos;
  2575. body_len = get_mka_param_body_len(hdr);
  2576. body_type = get_mka_param_body_type(hdr);
  2577. if (body_type == MKA_ICV_INDICATOR)
  2578. return 0;
  2579. if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
  2580. wpa_printf(MSG_ERROR,
  2581. "KaY: MKA Peer Packet Body Length (%d bytes) is less than the Parameter Set Header Length (%d bytes) + the Parameter Set Body Length (%d bytes) + %d bytes of ICV",
  2582. (int) left_len, (int) MKA_HDR_LEN,
  2583. (int) body_len, DEFAULT_ICV_LEN);
  2584. goto next_para_set;
  2585. }
  2586. if (handled[body_type])
  2587. goto next_para_set;
  2588. handled[body_type] = TRUE;
  2589. if (body_type < ARRAY_SIZE(mka_body_handler) &&
  2590. mka_body_handler[body_type].body_rx) {
  2591. mka_body_handler[body_type].body_rx
  2592. (participant, pos, left_len);
  2593. } else {
  2594. wpa_printf(MSG_ERROR,
  2595. "The type %d not supported in this MKA version %d",
  2596. body_type, MKA_VERSION_ID);
  2597. }
  2598. next_para_set:
  2599. pos += body_len + MKA_HDR_LEN;
  2600. left_len -= body_len + MKA_HDR_LEN;
  2601. }
  2602. kay->active = TRUE;
  2603. participant->retry_count = 0;
  2604. participant->active = TRUE;
  2605. return 0;
  2606. }
  2607. static void kay_l2_receive(void *ctx, const u8 *src_addr, const u8 *buf,
  2608. size_t len)
  2609. {
  2610. struct ieee802_1x_kay *kay = ctx;
  2611. struct ieee8023_hdr *eth_hdr;
  2612. struct ieee802_1x_hdr *eapol_hdr;
  2613. /* must contain at least ieee8023_hdr + ieee802_1x_hdr */
  2614. if (len < sizeof(*eth_hdr) + sizeof(*eapol_hdr)) {
  2615. wpa_printf(MSG_MSGDUMP, "KaY: EAPOL frame too short (%lu)",
  2616. (unsigned long) len);
  2617. return;
  2618. }
  2619. eth_hdr = (struct ieee8023_hdr *) buf;
  2620. eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
  2621. if (len != sizeof(*eth_hdr) + sizeof(*eapol_hdr) +
  2622. be_to_host16(eapol_hdr->length)) {
  2623. wpa_printf(MSG_MSGDUMP, "KAY: EAPOL MPDU is invalid: (%lu-%lu)",
  2624. (unsigned long) len,
  2625. (unsigned long) be_to_host16(eapol_hdr->length));
  2626. return;
  2627. }
  2628. if (eapol_hdr->version < EAPOL_VERSION) {
  2629. wpa_printf(MSG_MSGDUMP, "KaY: version %d does not support MKA",
  2630. eapol_hdr->version);
  2631. return;
  2632. }
  2633. if (be_to_host16(eth_hdr->ethertype) != ETH_P_PAE ||
  2634. eapol_hdr->type != IEEE802_1X_TYPE_EAPOL_MKA)
  2635. return;
  2636. wpa_hexdump(MSG_DEBUG, "RX EAPOL-MKA: ", buf, len);
  2637. if (dl_list_empty(&kay->participant_list)) {
  2638. wpa_printf(MSG_ERROR, "KaY: no MKA participant instance");
  2639. return;
  2640. }
  2641. ieee802_1x_kay_decode_mkpdu(kay, buf, len);
  2642. }
  2643. /**
  2644. * ieee802_1x_kay_init -
  2645. */
  2646. struct ieee802_1x_kay *
  2647. ieee802_1x_kay_init(struct ieee802_1x_kay_ctx *ctx, enum macsec_policy policy,
  2648. const char *ifname, const u8 *addr)
  2649. {
  2650. struct ieee802_1x_kay *kay;
  2651. kay = os_zalloc(sizeof(*kay));
  2652. if (!kay) {
  2653. wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
  2654. return NULL;
  2655. }
  2656. kay->ctx = ctx;
  2657. kay->enable = TRUE;
  2658. kay->active = FALSE;
  2659. kay->authenticated = FALSE;
  2660. kay->secured = FALSE;
  2661. kay->failed = FALSE;
  2662. kay->policy = policy;
  2663. os_strlcpy(kay->if_name, ifname, IFNAMSIZ);
  2664. os_memcpy(kay->actor_sci.addr, addr, ETH_ALEN);
  2665. kay->actor_sci.port = host_to_be16(0x0001);
  2666. kay->actor_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
  2667. /* While actor acts as a key server, shall distribute sakey */
  2668. kay->dist_kn = 1;
  2669. kay->dist_an = 0;
  2670. kay->dist_time = 0;
  2671. kay->pn_exhaustion = PENDING_PN_EXHAUSTION;
  2672. kay->macsec_csindex = DEFAULT_CS_INDEX;
  2673. kay->mka_algindex = DEFAULT_MKA_ALG_INDEX;
  2674. kay->mka_version = MKA_VERSION_ID;
  2675. os_memcpy(kay->algo_agility, mka_algo_agility,
  2676. sizeof(kay->algo_agility));
  2677. dl_list_init(&kay->participant_list);
  2678. if (policy == DO_NOT_SECURE) {
  2679. kay->macsec_capable = MACSEC_CAP_NOT_IMPLEMENTED;
  2680. kay->macsec_desired = FALSE;
  2681. kay->macsec_protect = FALSE;
  2682. kay->macsec_validate = Disabled;
  2683. kay->macsec_replay_protect = FALSE;
  2684. kay->macsec_replay_window = 0;
  2685. kay->macsec_confidentiality = CONFIDENTIALITY_NONE;
  2686. } else {
  2687. kay->macsec_capable = MACSEC_CAP_INTEG_AND_CONF_0_30_50;
  2688. kay->macsec_desired = TRUE;
  2689. kay->macsec_protect = TRUE;
  2690. kay->macsec_validate = Strict;
  2691. kay->macsec_replay_protect = FALSE;
  2692. kay->macsec_replay_window = 0;
  2693. kay->macsec_confidentiality = CONFIDENTIALITY_OFFSET_0;
  2694. }
  2695. wpa_printf(MSG_DEBUG, "KaY: state machine created");
  2696. /* Initialize the SecY must be prio to CP, as CP will control SecY */
  2697. secy_init_macsec(kay);
  2698. secy_get_available_transmit_sc(kay, &kay->sc_ch);
  2699. wpa_printf(MSG_DEBUG, "KaY: secy init macsec done");
  2700. /* init CP */
  2701. kay->cp = ieee802_1x_kay_alloc_cp_sm(kay);
  2702. if (kay->cp == NULL) {
  2703. ieee802_1x_kay_deinit(kay);
  2704. return NULL;
  2705. }
  2706. if (policy == DO_NOT_SECURE) {
  2707. ieee802_1x_cp_connect_authenticated(kay->cp);
  2708. ieee802_1x_cp_sm_step(kay->cp);
  2709. } else {
  2710. kay->l2_mka = l2_packet_init(kay->if_name, NULL, ETH_P_PAE,
  2711. kay_l2_receive, kay, 1);
  2712. if (kay->l2_mka == NULL) {
  2713. wpa_printf(MSG_WARNING,
  2714. "KaY: Failed to initialize L2 packet processing for MKA packet");
  2715. ieee802_1x_kay_deinit(kay);
  2716. return NULL;
  2717. }
  2718. }
  2719. return kay;
  2720. }
  2721. /**
  2722. * ieee802_1x_kay_deinit -
  2723. */
  2724. void
  2725. ieee802_1x_kay_deinit(struct ieee802_1x_kay *kay)
  2726. {
  2727. struct ieee802_1x_mka_participant *participant;
  2728. if (!kay)
  2729. return;
  2730. wpa_printf(MSG_DEBUG, "KaY: state machine removed");
  2731. while (!dl_list_empty(&kay->participant_list)) {
  2732. participant = dl_list_entry(kay->participant_list.next,
  2733. struct ieee802_1x_mka_participant,
  2734. list);
  2735. ieee802_1x_kay_delete_mka(kay, &participant->ckn);
  2736. }
  2737. ieee802_1x_cp_sm_deinit(kay->cp);
  2738. secy_deinit_macsec(kay);
  2739. if (kay->l2_mka) {
  2740. l2_packet_deinit(kay->l2_mka);
  2741. kay->l2_mka = NULL;
  2742. }
  2743. os_free(kay->ctx);
  2744. os_free(kay);
  2745. }
  2746. /**
  2747. * ieee802_1x_kay_create_mka -
  2748. */
  2749. struct ieee802_1x_mka_participant *
  2750. ieee802_1x_kay_create_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn,
  2751. struct mka_key *cak, u32 life,
  2752. enum mka_created_mode mode, Boolean is_authenticator)
  2753. {
  2754. struct ieee802_1x_mka_participant *participant;
  2755. unsigned int usecs;
  2756. if (!kay || !ckn || !cak) {
  2757. wpa_printf(MSG_ERROR, "KaY: ckn or cak is null");
  2758. return NULL;
  2759. }
  2760. if (cak->len != mka_alg_tbl[kay->mka_algindex].cak_len) {
  2761. wpa_printf(MSG_ERROR, "KaY: CAK length not follow key schema");
  2762. return NULL;
  2763. }
  2764. if (ckn->len > MAX_CKN_LEN) {
  2765. wpa_printf(MSG_ERROR, "KaY: CKN is out of range(<=32 bytes)");
  2766. return NULL;
  2767. }
  2768. if (!kay->enable) {
  2769. wpa_printf(MSG_ERROR, "KaY: Now is at disable state");
  2770. return NULL;
  2771. }
  2772. participant = os_zalloc(sizeof(*participant));
  2773. if (!participant) {
  2774. wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
  2775. return NULL;
  2776. }
  2777. participant->ckn.len = ckn->len;
  2778. os_memcpy(participant->ckn.name, ckn->name, ckn->len);
  2779. participant->cak.len = cak->len;
  2780. os_memcpy(participant->cak.key, cak->key, cak->len);
  2781. if (life)
  2782. participant->cak_life = life + time(NULL);
  2783. switch (mode) {
  2784. case EAP_EXCHANGE:
  2785. if (is_authenticator) {
  2786. participant->is_obliged_key_server = TRUE;
  2787. participant->can_be_key_server = TRUE;
  2788. participant->is_key_server = TRUE;
  2789. participant->principal = TRUE;
  2790. os_memcpy(&kay->key_server_sci, &kay->actor_sci,
  2791. sizeof(kay->key_server_sci));
  2792. kay->key_server_priority = kay->actor_priority;
  2793. participant->is_elected = TRUE;
  2794. } else {
  2795. participant->is_obliged_key_server = FALSE;
  2796. participant->can_be_key_server = FALSE;
  2797. participant->is_key_server = FALSE;
  2798. participant->is_elected = TRUE;
  2799. }
  2800. break;
  2801. default:
  2802. participant->is_obliged_key_server = FALSE;
  2803. participant->can_be_key_server = TRUE;
  2804. participant->is_key_server = TRUE;
  2805. participant->is_elected = FALSE;
  2806. break;
  2807. }
  2808. participant->cached = FALSE;
  2809. participant->active = FALSE;
  2810. participant->participant = FALSE;
  2811. participant->retain = FALSE;
  2812. participant->activate = DEFAULT;
  2813. if (participant->is_key_server)
  2814. participant->principal = TRUE;
  2815. dl_list_init(&participant->live_peers);
  2816. dl_list_init(&participant->potential_peers);
  2817. participant->retry_count = 0;
  2818. participant->kay = kay;
  2819. if (os_get_random(participant->mi, sizeof(participant->mi)) < 0)
  2820. goto fail;
  2821. participant->mn = 0;
  2822. participant->lrx = FALSE;
  2823. participant->ltx = FALSE;
  2824. participant->orx = FALSE;
  2825. participant->otx = FALSE;
  2826. participant->to_dist_sak = FALSE;
  2827. participant->to_use_sak = FALSE;
  2828. participant->new_sak = FALSE;
  2829. dl_list_init(&participant->sak_list);
  2830. participant->new_key = NULL;
  2831. dl_list_init(&participant->rxsc_list);
  2832. participant->txsc = ieee802_1x_kay_init_transmit_sc(&kay->actor_sci,
  2833. kay->sc_ch);
  2834. secy_cp_control_protect_frames(kay, kay->macsec_protect);
  2835. secy_cp_control_replay(kay, kay->macsec_replay_protect,
  2836. kay->macsec_replay_window);
  2837. secy_create_transmit_sc(kay, participant->txsc);
  2838. /* to derive KEK from CAK and CKN */
  2839. participant->kek.len = mka_alg_tbl[kay->mka_algindex].kek_len;
  2840. if (mka_alg_tbl[kay->mka_algindex].kek_trfm(participant->cak.key,
  2841. participant->ckn.name,
  2842. participant->ckn.len,
  2843. participant->kek.key)) {
  2844. wpa_printf(MSG_ERROR, "KaY: Derived KEK failed");
  2845. goto fail;
  2846. }
  2847. wpa_hexdump_key(MSG_DEBUG, "KaY: Derived KEK",
  2848. participant->kek.key, participant->kek.len);
  2849. /* to derive ICK from CAK and CKN */
  2850. participant->ick.len = mka_alg_tbl[kay->mka_algindex].ick_len;
  2851. if (mka_alg_tbl[kay->mka_algindex].ick_trfm(participant->cak.key,
  2852. participant->ckn.name,
  2853. participant->ckn.len,
  2854. participant->ick.key)) {
  2855. wpa_printf(MSG_ERROR, "KaY: Derived ICK failed");
  2856. goto fail;
  2857. }
  2858. wpa_hexdump_key(MSG_DEBUG, "KaY: Derived ICK",
  2859. participant->ick.key, participant->ick.len);
  2860. dl_list_add(&kay->participant_list, &participant->list);
  2861. wpa_hexdump(MSG_DEBUG, "KaY: Participant created:",
  2862. ckn->name, ckn->len);
  2863. usecs = os_random() % (MKA_HELLO_TIME * 1000);
  2864. eloop_register_timeout(0, usecs, ieee802_1x_participant_timer,
  2865. participant, NULL);
  2866. participant->mka_life = MKA_LIFE_TIME / 1000 + time(NULL) +
  2867. usecs / 1000000;
  2868. return participant;
  2869. fail:
  2870. os_free(participant);
  2871. return NULL;
  2872. }
  2873. /**
  2874. * ieee802_1x_kay_delete_mka -
  2875. */
  2876. void
  2877. ieee802_1x_kay_delete_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn)
  2878. {
  2879. struct ieee802_1x_mka_participant *participant;
  2880. struct ieee802_1x_kay_peer *peer;
  2881. struct data_key *sak;
  2882. struct receive_sc *rxsc;
  2883. if (!kay || !ckn)
  2884. return;
  2885. wpa_printf(MSG_DEBUG, "KaY: participant removed");
  2886. /* get the participant */
  2887. participant = ieee802_1x_kay_get_participant(kay, ckn->name);
  2888. if (!participant) {
  2889. wpa_hexdump(MSG_DEBUG, "KaY: participant is not found",
  2890. ckn->name, ckn->len);
  2891. return;
  2892. }
  2893. eloop_cancel_timeout(ieee802_1x_participant_timer, participant, NULL);
  2894. dl_list_del(&participant->list);
  2895. /* remove live peer */
  2896. while (!dl_list_empty(&participant->live_peers)) {
  2897. peer = dl_list_entry(participant->live_peers.next,
  2898. struct ieee802_1x_kay_peer, list);
  2899. dl_list_del(&peer->list);
  2900. os_free(peer);
  2901. }
  2902. /* remove potential peer */
  2903. while (!dl_list_empty(&participant->potential_peers)) {
  2904. peer = dl_list_entry(participant->potential_peers.next,
  2905. struct ieee802_1x_kay_peer, list);
  2906. dl_list_del(&peer->list);
  2907. os_free(peer);
  2908. }
  2909. /* remove sak */
  2910. while (!dl_list_empty(&participant->sak_list)) {
  2911. sak = dl_list_entry(participant->sak_list.next,
  2912. struct data_key, list);
  2913. dl_list_del(&sak->list);
  2914. os_free(sak->key);
  2915. os_free(sak);
  2916. }
  2917. while (!dl_list_empty(&participant->rxsc_list)) {
  2918. rxsc = dl_list_entry(participant->rxsc_list.next,
  2919. struct receive_sc, list);
  2920. secy_delete_receive_sc(kay, rxsc);
  2921. ieee802_1x_kay_deinit_receive_sc(participant, rxsc);
  2922. }
  2923. secy_delete_transmit_sc(kay, participant->txsc);
  2924. ieee802_1x_kay_deinit_transmit_sc(participant, participant->txsc);
  2925. os_memset(&participant->cak, 0, sizeof(participant->cak));
  2926. os_memset(&participant->kek, 0, sizeof(participant->kek));
  2927. os_memset(&participant->ick, 0, sizeof(participant->ick));
  2928. os_free(participant);
  2929. }
  2930. /**
  2931. * ieee802_1x_kay_mka_participate -
  2932. */
  2933. void ieee802_1x_kay_mka_participate(struct ieee802_1x_kay *kay,
  2934. struct mka_key_name *ckn,
  2935. Boolean status)
  2936. {
  2937. struct ieee802_1x_mka_participant *participant;
  2938. if (!kay || !ckn)
  2939. return;
  2940. participant = ieee802_1x_kay_get_participant(kay, ckn->name);
  2941. if (!participant)
  2942. return;
  2943. participant->active = status;
  2944. }
  2945. /**
  2946. * ieee802_1x_kay_new_sak -
  2947. */
  2948. int
  2949. ieee802_1x_kay_new_sak(struct ieee802_1x_kay *kay)
  2950. {
  2951. struct ieee802_1x_mka_participant *participant;
  2952. if (!kay)
  2953. return -1;
  2954. participant = ieee802_1x_kay_get_principal_participant(kay);
  2955. if (!participant)
  2956. return -1;
  2957. participant->new_sak = TRUE;
  2958. wpa_printf(MSG_DEBUG, "KaY: new SAK signal");
  2959. return 0;
  2960. }
  2961. /**
  2962. * ieee802_1x_kay_change_cipher_suite -
  2963. */
  2964. int
  2965. ieee802_1x_kay_change_cipher_suite(struct ieee802_1x_kay *kay, int cs_index)
  2966. {
  2967. struct ieee802_1x_mka_participant *participant;
  2968. if (!kay)
  2969. return -1;
  2970. if ((unsigned int) cs_index >= CS_TABLE_SIZE) {
  2971. wpa_printf(MSG_ERROR,
  2972. "KaY: Configured cipher suite index is out of range");
  2973. return -1;
  2974. }
  2975. if (kay->macsec_csindex == cs_index)
  2976. return -2;
  2977. if (cs_index == 0)
  2978. kay->macsec_desired = FALSE;
  2979. kay->macsec_csindex = cs_index;
  2980. kay->macsec_capable = cipher_suite_tbl[kay->macsec_csindex].capable;
  2981. participant = ieee802_1x_kay_get_principal_participant(kay);
  2982. if (participant) {
  2983. wpa_printf(MSG_INFO, "KaY: Cipher Suite changed");
  2984. participant->new_sak = TRUE;
  2985. }
  2986. return 0;
  2987. }