ieee802_1x_kay.c 88 KB

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