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