ieee802_1x_kay.c 88 KB

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