ieee802_1x_kay.c 89 KB

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