12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566 |
- /*
- * IEEE 802.1X-2010 Key Agree Protocol of PAE state machine
- * Copyright (c) 2013, Qualcomm Atheros, Inc.
- *
- * This software may be distributed under the terms of the BSD license.
- * See README for more details.
- */
- #include <time.h>
- #include "includes.h"
- #include "common.h"
- #include "list.h"
- #include "eloop.h"
- #include "wpabuf.h"
- #include "state_machine.h"
- #include "l2_packet/l2_packet.h"
- #include "common/eapol_common.h"
- #include "crypto/aes_wrap.h"
- #include "ieee802_1x_cp.h"
- #include "ieee802_1x_key.h"
- #include "ieee802_1x_kay.h"
- #include "ieee802_1x_kay_i.h"
- #include "ieee802_1x_secy_ops.h"
- #define DEFAULT_SA_KEY_LEN 16
- #define DEFAULT_ICV_LEN 16
- #define MAX_ICV_LEN 32 /* 32 bytes, 256 bits */
- #define PENDING_PN_EXHAUSTION 0xC0000000
- /* IEEE Std 802.1X-2010, Table 9-1 - MKA Algorithm Agility */
- #define MKA_ALGO_AGILITY_2009 { 0x00, 0x80, 0xC2, 0x01 }
- static u8 mka_algo_agility[4] = MKA_ALGO_AGILITY_2009;
- /* IEEE802.1AE-2006 Table 14-1 MACsec Cipher Suites */
- static struct macsec_ciphersuite cipher_suite_tbl[] = {
- /* GCM-AES-128 */
- {
- CS_ID_GCM_AES_128,
- CS_NAME_GCM_AES_128,
- MACSEC_CAP_INTEG_AND_CONF_0_30_50,
- 16,
- 0 /* index */
- },
- };
- #define CS_TABLE_SIZE (ARRAY_SIZE(cipher_suite_tbl))
- #define DEFAULT_CS_INDEX 0
- static struct mka_alg mka_alg_tbl[] = {
- {
- MKA_ALGO_AGILITY_2009,
- /* 128-bit CAK, KEK, ICK, ICV */
- 16, 16, 16, 16,
- ieee802_1x_cak_128bits_aes_cmac,
- ieee802_1x_ckn_128bits_aes_cmac,
- ieee802_1x_kek_128bits_aes_cmac,
- ieee802_1x_ick_128bits_aes_cmac,
- ieee802_1x_icv_128bits_aes_cmac,
- 1, /* index */
- },
- };
- #define MKA_ALG_TABLE_SIZE (ARRAY_SIZE(mka_alg_tbl))
- static int is_ki_equal(struct ieee802_1x_mka_ki *ki1,
- struct ieee802_1x_mka_ki *ki2)
- {
- return os_memcmp(ki1->mi, ki2->mi, MI_LEN) == 0 &&
- ki1->kn == ki2->kn;
- }
- struct mka_param_body_handler {
- int (*body_tx)(struct ieee802_1x_mka_participant *participant,
- struct wpabuf *buf);
- int (*body_rx)(struct ieee802_1x_mka_participant *participant,
- const u8 *mka_msg, size_t msg_len);
- int (*body_length)(struct ieee802_1x_mka_participant *participant);
- Boolean (*body_present)(struct ieee802_1x_mka_participant *participant);
- };
- static void set_mka_param_body_len(void *body, unsigned int len)
- {
- struct ieee802_1x_mka_hdr *hdr = body;
- hdr->length = (len >> 8) & 0x0f;
- hdr->length1 = len & 0xff;
- }
- static unsigned int get_mka_param_body_len(const void *body)
- {
- const struct ieee802_1x_mka_hdr *hdr = body;
- return (hdr->length << 8) | hdr->length1;
- }
- static u8 get_mka_param_body_type(const void *body)
- {
- const struct ieee802_1x_mka_hdr *hdr = body;
- return hdr->type;
- }
- /**
- * ieee802_1x_mka_dump_basic_body -
- */
- static void
- ieee802_1x_mka_dump_basic_body(struct ieee802_1x_mka_basic_body *body)
- {
- size_t body_len;
- if (!body)
- return;
- body_len = get_mka_param_body_len(body);
- wpa_printf(MSG_DEBUG, "*** MKA Basic Parameter set ***");
- wpa_printf(MSG_DEBUG, "\tVersion.......: %d", body->version);
- wpa_printf(MSG_DEBUG, "\tPriority......: %d", body->priority);
- wpa_printf(MSG_DEBUG, "\tKeySvr........: %d", body->key_server);
- wpa_printf(MSG_DEBUG, "\tMACSecDesired.: %d", body->macsec_desired);
- wpa_printf(MSG_DEBUG, "\tMACSecCapable.: %d", body->macsec_capability);
- wpa_printf(MSG_DEBUG, "\tBody Length...: %d", (int) body_len);
- wpa_printf(MSG_DEBUG, "\tSCI MAC.......: " MACSTR,
- MAC2STR(body->actor_sci.addr));
- wpa_printf(MSG_DEBUG, "\tSCI Port .....: %d",
- be_to_host16(body->actor_sci.port));
- wpa_hexdump(MSG_DEBUG, "\tMember Id.....:",
- body->actor_mi, sizeof(body->actor_mi));
- wpa_printf(MSG_DEBUG, "\tMessage Number: %d",
- be_to_host32(body->actor_mn));
- wpa_hexdump(MSG_DEBUG, "\tAlgo Agility..:",
- body->algo_agility, sizeof(body->algo_agility));
- wpa_hexdump_ascii(MSG_DEBUG, "\tCAK Name......:", body->ckn,
- body_len + MKA_HDR_LEN - sizeof(*body));
- }
- /**
- * ieee802_1x_mka_dump_peer_body -
- */
- static void
- ieee802_1x_mka_dump_peer_body(struct ieee802_1x_mka_peer_body *body)
- {
- size_t body_len;
- size_t i;
- u8 *mi;
- be32 mn;
- if (body == NULL)
- return;
- body_len = get_mka_param_body_len(body);
- if (body->type == MKA_LIVE_PEER_LIST) {
- wpa_printf(MSG_DEBUG, "*** Live Peer List ***");
- wpa_printf(MSG_DEBUG, "\tBody Length...: %d", (int) body_len);
- } else if (body->type == MKA_POTENTIAL_PEER_LIST) {
- wpa_printf(MSG_DEBUG, "*** Potential Live Peer List ***");
- wpa_printf(MSG_DEBUG, "\tBody Length...: %d", (int) body_len);
- }
- for (i = 0; i < body_len; i += MI_LEN + sizeof(mn)) {
- mi = body->peer + i;
- os_memcpy(&mn, mi + MI_LEN, sizeof(mn));
- wpa_hexdump_ascii(MSG_DEBUG, "\tMember Id.....:", mi, MI_LEN);
- wpa_printf(MSG_DEBUG, "\tMessage Number: %d", be_to_host32(mn));
- }
- }
- /**
- * ieee802_1x_mka_dump_dist_sak_body -
- */
- static void
- ieee802_1x_mka_dump_dist_sak_body(struct ieee802_1x_mka_dist_sak_body *body)
- {
- size_t body_len;
- if (body == NULL)
- return;
- body_len = get_mka_param_body_len(body);
- wpa_printf(MSG_INFO, "*** Distributed SAK ***");
- wpa_printf(MSG_INFO, "\tDistributed AN........: %d", body->dan);
- wpa_printf(MSG_INFO, "\tConfidentiality Offset: %d",
- body->confid_offset);
- wpa_printf(MSG_INFO, "\tBody Length...........: %d", (int) body_len);
- if (!body_len)
- return;
- wpa_printf(MSG_INFO, "\tKey Number............: %d",
- be_to_host32(body->kn));
- wpa_hexdump(MSG_INFO, "\tAES Key Wrap of SAK...:", body->sak, 24);
- }
- static const char * yes_no(int val)
- {
- return val ? "Yes" : "No";
- }
- /**
- * ieee802_1x_mka_dump_sak_use_body -
- */
- static void
- ieee802_1x_mka_dump_sak_use_body(struct ieee802_1x_mka_sak_use_body *body)
- {
- int body_len;
- if (body == NULL)
- return;
- body_len = get_mka_param_body_len(body);
- wpa_printf(MSG_DEBUG, "*** MACsec SAK Use ***");
- wpa_printf(MSG_DEBUG, "\tLatest Key AN....: %d", body->lan);
- wpa_printf(MSG_DEBUG, "\tLatest Key Tx....: %s", yes_no(body->ltx));
- wpa_printf(MSG_DEBUG, "\tLatest Key Rx....: %s", yes_no(body->lrx));
- wpa_printf(MSG_DEBUG, "\tOld Key AN....: %d", body->oan);
- wpa_printf(MSG_DEBUG, "\tOld Key Tx....: %s", yes_no(body->otx));
- wpa_printf(MSG_DEBUG, "\tOld Key Rx....: %s", yes_no(body->orx));
- wpa_printf(MSG_DEBUG, "\tPlain Key Tx....: %s", yes_no(body->ptx));
- wpa_printf(MSG_DEBUG, "\tPlain Key Rx....: %s", yes_no(body->prx));
- wpa_printf(MSG_DEBUG, "\tDelay Protect....: %s",
- yes_no(body->delay_protect));
- wpa_printf(MSG_DEBUG, "\tBody Length......: %d", body_len);
- if (!body_len)
- return;
- wpa_hexdump(MSG_DEBUG, "\tKey Server MI....:",
- body->lsrv_mi, sizeof(body->lsrv_mi));
- wpa_printf(MSG_DEBUG, "\tKey Number.......: %u",
- be_to_host32(body->lkn));
- wpa_printf(MSG_DEBUG, "\tLowest PN........: %u",
- be_to_host32(body->llpn));
- wpa_hexdump_ascii(MSG_DEBUG, "\tOld Key Server MI....:",
- body->osrv_mi, sizeof(body->osrv_mi));
- wpa_printf(MSG_DEBUG, "\tOld Key Number.......: %u",
- be_to_host32(body->okn));
- wpa_printf(MSG_DEBUG, "\tOld Lowest PN........: %u",
- be_to_host32(body->olpn));
- }
- /**
- * ieee802_1x_kay_get_participant -
- */
- static struct ieee802_1x_mka_participant *
- ieee802_1x_kay_get_participant(struct ieee802_1x_kay *kay, const u8 *ckn)
- {
- struct ieee802_1x_mka_participant *participant;
- dl_list_for_each(participant, &kay->participant_list,
- struct ieee802_1x_mka_participant, list) {
- if (os_memcmp(participant->ckn.name, ckn,
- participant->ckn.len) == 0)
- return participant;
- }
- wpa_printf(MSG_DEBUG, "KaY: participant is not found");
- return NULL;
- }
- /**
- * ieee802_1x_kay_get_principal_participant -
- */
- static struct ieee802_1x_mka_participant *
- ieee802_1x_kay_get_principal_participant(struct ieee802_1x_kay *kay)
- {
- struct ieee802_1x_mka_participant *participant;
- dl_list_for_each(participant, &kay->participant_list,
- struct ieee802_1x_mka_participant, list) {
- if (participant->principal)
- return participant;
- }
- wpa_printf(MSG_DEBUG, "KaY: principal participant is not founded");
- return NULL;
- }
- static struct ieee802_1x_kay_peer * get_peer_mi(struct dl_list *peers,
- const u8 *mi)
- {
- struct ieee802_1x_kay_peer *peer;
- dl_list_for_each(peer, peers, struct ieee802_1x_kay_peer, list) {
- if (os_memcmp(peer->mi, mi, MI_LEN) == 0)
- return peer;
- }
- return NULL;
- }
- /**
- * ieee802_1x_kay_get_potential_peer
- */
- static struct ieee802_1x_kay_peer *
- ieee802_1x_kay_get_potential_peer(
- struct ieee802_1x_mka_participant *participant, const u8 *mi)
- {
- return get_peer_mi(&participant->potential_peers, mi);
- }
- /**
- * ieee802_1x_kay_get_live_peer
- */
- static struct ieee802_1x_kay_peer *
- ieee802_1x_kay_get_live_peer(struct ieee802_1x_mka_participant *participant,
- const u8 *mi)
- {
- return get_peer_mi(&participant->live_peers, mi);
- }
- /**
- * ieee802_1x_kay_is_in_potential_peer
- */
- static Boolean
- ieee802_1x_kay_is_in_potential_peer(
- struct ieee802_1x_mka_participant *participant, const u8 *mi)
- {
- return ieee802_1x_kay_get_potential_peer(participant, mi) != NULL;
- }
- /**
- * ieee802_1x_kay_is_in_live_peer
- */
- static Boolean
- ieee802_1x_kay_is_in_live_peer(
- struct ieee802_1x_mka_participant *participant, const u8 *mi)
- {
- return ieee802_1x_kay_get_live_peer(participant, mi) != NULL;
- }
- /**
- * ieee802_1x_kay_is_in_peer
- */
- static Boolean
- ieee802_1x_kay_is_in_peer(struct ieee802_1x_mka_participant *participant,
- const u8 *mi)
- {
- return ieee802_1x_kay_is_in_live_peer(participant, mi) ||
- ieee802_1x_kay_is_in_potential_peer(participant, mi);
- }
- /**
- * ieee802_1x_kay_get_peer
- */
- static struct ieee802_1x_kay_peer *
- ieee802_1x_kay_get_peer(struct ieee802_1x_mka_participant *participant,
- const u8 *mi)
- {
- struct ieee802_1x_kay_peer *peer;
- peer = ieee802_1x_kay_get_live_peer(participant, mi);
- if (peer)
- return peer;
- return ieee802_1x_kay_get_potential_peer(participant, mi);
- }
- /**
- * ieee802_1x_kay_get_cipher_suite
- */
- static struct macsec_ciphersuite *
- ieee802_1x_kay_get_cipher_suite(struct ieee802_1x_mka_participant *participant,
- u8 *cs_id)
- {
- unsigned int i;
- for (i = 0; i < CS_TABLE_SIZE; i++) {
- if (os_memcmp(cipher_suite_tbl[i].id, cs_id, CS_ID_LEN) == 0)
- return &cipher_suite_tbl[i];
- }
- return NULL;
- }
- static Boolean sci_equal(const struct ieee802_1x_mka_sci *a,
- const struct ieee802_1x_mka_sci *b)
- {
- return os_memcmp(a, b, sizeof(struct ieee802_1x_mka_sci)) == 0;
- }
- /**
- * ieee802_1x_kay_get_peer_sci
- */
- static struct ieee802_1x_kay_peer *
- ieee802_1x_kay_get_peer_sci(struct ieee802_1x_mka_participant *participant,
- const struct ieee802_1x_mka_sci *sci)
- {
- struct ieee802_1x_kay_peer *peer;
- dl_list_for_each(peer, &participant->live_peers,
- struct ieee802_1x_kay_peer, list) {
- if (sci_equal(&peer->sci, sci))
- return peer;
- }
- dl_list_for_each(peer, &participant->potential_peers,
- struct ieee802_1x_kay_peer, list) {
- if (sci_equal(&peer->sci, sci))
- return peer;
- }
- return NULL;
- }
- /**
- * ieee802_1x_kay_init_receive_sa -
- */
- static struct receive_sa *
- ieee802_1x_kay_init_receive_sa(struct receive_sc *psc, u8 an, u32 lowest_pn,
- struct data_key *key)
- {
- struct receive_sa *psa;
- if (!psc || !key)
- return NULL;
- psa = os_zalloc(sizeof(*psa));
- if (!psa) {
- wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
- return NULL;
- }
- psa->pkey = key;
- psa->lowest_pn = lowest_pn;
- psa->next_pn = lowest_pn;
- psa->an = an;
- psa->sc = psc;
- os_get_time(&psa->created_time);
- psa->in_use = FALSE;
- dl_list_add(&psc->sa_list, &psa->list);
- wpa_printf(MSG_DEBUG,
- "KaY: Create receive SA(AN: %d lowest_pn: %u of SC(channel: %d)",
- (int) an, lowest_pn, psc->channel);
- return psa;
- }
- /**
- * ieee802_1x_kay_deinit_receive_sa -
- */
- static void ieee802_1x_kay_deinit_receive_sa(struct receive_sa *psa)
- {
- psa->pkey = NULL;
- wpa_printf(MSG_DEBUG,
- "KaY: Delete receive SA(an: %d) of SC(channel: %d)",
- psa->an, psa->sc->channel);
- dl_list_del(&psa->list);
- os_free(psa);
- }
- /**
- * ieee802_1x_kay_init_receive_sc -
- */
- static struct receive_sc *
- ieee802_1x_kay_init_receive_sc(const struct ieee802_1x_mka_sci *psci,
- int channel)
- {
- struct receive_sc *psc;
- if (!psci)
- return NULL;
- psc = os_zalloc(sizeof(*psc));
- if (!psc) {
- wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
- return NULL;
- }
- os_memcpy(&psc->sci, psci, sizeof(psc->sci));
- psc->channel = channel;
- os_get_time(&psc->created_time);
- psc->receiving = FALSE;
- dl_list_init(&psc->sa_list);
- wpa_printf(MSG_DEBUG, "KaY: Create receive SC(channel: %d)", channel);
- wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)psci, sizeof(*psci));
- return psc;
- }
- /**
- * ieee802_1x_kay_deinit_receive_sc -
- **/
- static void
- ieee802_1x_kay_deinit_receive_sc(
- struct ieee802_1x_mka_participant *participant, struct receive_sc *psc)
- {
- struct receive_sa *psa, *pre_sa;
- wpa_printf(MSG_DEBUG, "KaY: Delete receive SC(channel: %d)",
- psc->channel);
- dl_list_for_each_safe(psa, pre_sa, &psc->sa_list, struct receive_sa,
- list) {
- secy_disable_receive_sa(participant->kay, psa);
- ieee802_1x_kay_deinit_receive_sa(psa);
- }
- dl_list_del(&psc->list);
- os_free(psc);
- }
- /**
- * ieee802_1x_kay_create_live_peer
- */
- static struct ieee802_1x_kay_peer *
- ieee802_1x_kay_create_live_peer(struct ieee802_1x_mka_participant *participant,
- u8 *mi, u32 mn)
- {
- struct ieee802_1x_kay_peer *peer;
- struct receive_sc *rxsc;
- u32 sc_ch = 0;
- peer = os_zalloc(sizeof(*peer));
- if (peer == NULL) {
- wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
- return NULL;
- }
- os_memcpy(peer->mi, mi, MI_LEN);
- peer->mn = mn;
- peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
- peer->sak_used = FALSE;
- os_memcpy(&peer->sci, &participant->current_peer_sci,
- sizeof(peer->sci));
- secy_get_available_receive_sc(participant->kay, &sc_ch);
- rxsc = ieee802_1x_kay_init_receive_sc(&peer->sci, sc_ch);
- if (!rxsc) {
- os_free(peer);
- return NULL;
- }
- dl_list_add(&participant->live_peers, &peer->list);
- dl_list_add(&participant->rxsc_list, &rxsc->list);
- secy_create_receive_sc(participant->kay, rxsc);
- wpa_printf(MSG_DEBUG, "KaY: Live peer created");
- wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
- wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
- wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
- wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
- return peer;
- }
- /**
- * ieee802_1x_kay_create_potential_peer
- */
- static struct ieee802_1x_kay_peer *
- ieee802_1x_kay_create_potential_peer(
- struct ieee802_1x_mka_participant *participant, const u8 *mi, u32 mn)
- {
- struct ieee802_1x_kay_peer *peer;
- peer = os_zalloc(sizeof(*peer));
- if (peer == NULL) {
- wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
- return NULL;
- }
- os_memcpy(peer->mi, mi, MI_LEN);
- peer->mn = mn;
- peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
- peer->sak_used = FALSE;
- dl_list_add(&participant->potential_peers, &peer->list);
- wpa_printf(MSG_DEBUG, "KaY: potential peer created");
- wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
- wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
- wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
- wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
- return peer;
- }
- /**
- * ieee802_1x_kay_move_live_peer
- */
- static struct ieee802_1x_kay_peer *
- ieee802_1x_kay_move_live_peer(struct ieee802_1x_mka_participant *participant,
- u8 *mi, u32 mn)
- {
- struct ieee802_1x_kay_peer *peer;
- struct receive_sc *rxsc;
- u32 sc_ch = 0;
- peer = ieee802_1x_kay_get_potential_peer(participant, mi);
- rxsc = ieee802_1x_kay_init_receive_sc(&participant->current_peer_sci,
- sc_ch);
- if (!rxsc)
- return NULL;
- os_memcpy(&peer->sci, &participant->current_peer_sci,
- sizeof(peer->sci));
- peer->mn = mn;
- peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
- wpa_printf(MSG_DEBUG, "KaY: move potential peer to live peer");
- wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
- wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
- wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
- wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
- dl_list_del(&peer->list);
- dl_list_add_tail(&participant->live_peers, &peer->list);
- secy_get_available_receive_sc(participant->kay, &sc_ch);
- dl_list_add(&participant->rxsc_list, &rxsc->list);
- secy_create_receive_sc(participant->kay, rxsc);
- return peer;
- }
- /**
- * ieee802_1x_mka_basic_body_present -
- */
- static Boolean
- ieee802_1x_mka_basic_body_present(
- struct ieee802_1x_mka_participant *participant)
- {
- return TRUE;
- }
- /**
- * ieee802_1x_mka_basic_body_length -
- */
- static int
- ieee802_1x_mka_basic_body_length(struct ieee802_1x_mka_participant *participant)
- {
- int length;
- length = sizeof(struct ieee802_1x_mka_basic_body);
- length += participant->ckn.len;
- return (length + 0x3) & ~0x3;
- }
- /**
- * ieee802_1x_mka_encode_basic_body
- */
- static int
- ieee802_1x_mka_encode_basic_body(
- struct ieee802_1x_mka_participant *participant,
- struct wpabuf *buf)
- {
- struct ieee802_1x_mka_basic_body *body;
- struct ieee802_1x_kay *kay = participant->kay;
- unsigned int length = ieee802_1x_mka_basic_body_length(participant);
- body = wpabuf_put(buf, length);
- body->version = kay->mka_version;
- body->priority = kay->actor_priority;
- if (participant->is_elected)
- body->key_server = participant->is_key_server;
- else
- body->key_server = participant->can_be_key_server;
- body->macsec_desired = kay->macsec_desired;
- body->macsec_capability = kay->macsec_capable;
- set_mka_param_body_len(body, length - MKA_HDR_LEN);
- os_memcpy(body->actor_sci.addr, kay->actor_sci.addr,
- sizeof(kay->actor_sci.addr));
- body->actor_sci.port = kay->actor_sci.port;
- os_memcpy(body->actor_mi, participant->mi, sizeof(body->actor_mi));
- participant->mn = participant->mn + 1;
- body->actor_mn = host_to_be32(participant->mn);
- os_memcpy(body->algo_agility, participant->kay->algo_agility,
- sizeof(body->algo_agility));
- os_memcpy(body->ckn, participant->ckn.name, participant->ckn.len);
- ieee802_1x_mka_dump_basic_body(body);
- return 0;
- }
- /**
- * ieee802_1x_mka_decode_basic_body -
- */
- static struct ieee802_1x_mka_participant *
- ieee802_1x_mka_decode_basic_body(struct ieee802_1x_kay *kay, const u8 *mka_msg,
- size_t msg_len)
- {
- struct ieee802_1x_mka_participant *participant;
- const struct ieee802_1x_mka_basic_body *body;
- struct ieee802_1x_kay_peer *peer;
- body = (const struct ieee802_1x_mka_basic_body *) mka_msg;
- if (body->version > MKA_VERSION_ID) {
- wpa_printf(MSG_DEBUG,
- "KaY: peer's version(%d) greater than mka current version(%d)",
- body->version, MKA_VERSION_ID);
- }
- if (kay->is_obliged_key_server && body->key_server) {
- wpa_printf(MSG_DEBUG, "I must be as key server");
- return NULL;
- }
- participant = ieee802_1x_kay_get_participant(kay, body->ckn);
- if (!participant) {
- wpa_printf(MSG_DEBUG, "Peer is not included in my CA");
- return NULL;
- }
- /* If the peer's MI is my MI, I will choose new MI */
- if (os_memcmp(body->actor_mi, participant->mi, MI_LEN) == 0) {
- if (os_get_random(participant->mi, sizeof(participant->mi)) < 0)
- return NULL;
- participant->mn = 0;
- }
- os_memcpy(participant->current_peer_id.mi, body->actor_mi, MI_LEN);
- participant->current_peer_id.mn = body->actor_mn;
- os_memcpy(participant->current_peer_sci.addr, body->actor_sci.addr,
- sizeof(participant->current_peer_sci.addr));
- participant->current_peer_sci.port = body->actor_sci.port;
- /* handler peer */
- peer = ieee802_1x_kay_get_peer(participant, body->actor_mi);
- if (!peer) {
- /* Check duplicated SCI */
- /* TODO: What policy should be applied to detect duplicated SCI
- * is active attacker or a valid peer whose MI is be changed?
- */
- peer = ieee802_1x_kay_get_peer_sci(participant,
- &body->actor_sci);
- if (peer) {
- wpa_printf(MSG_WARNING,
- "KaY: duplicated SCI detected, Maybe active attacker");
- dl_list_del(&peer->list);
- os_free(peer);
- }
- peer = ieee802_1x_kay_create_potential_peer(
- participant, body->actor_mi,
- be_to_host32(body->actor_mn));
- if (!peer)
- return NULL;
- peer->macsec_desired = body->macsec_desired;
- peer->macsec_capability = body->macsec_capability;
- peer->is_key_server = (Boolean) body->key_server;
- peer->key_server_priority = body->priority;
- } else if (peer->mn < be_to_host32(body->actor_mn)) {
- peer->mn = be_to_host32(body->actor_mn);
- peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
- peer->macsec_desired = body->macsec_desired;
- peer->macsec_capability = body->macsec_capability;
- peer->is_key_server = (Boolean) body->key_server;
- peer->key_server_priority = body->priority;
- } else {
- wpa_printf(MSG_WARNING, "KaY: The peer MN have received");
- return NULL;
- }
- return participant;
- }
- /**
- * ieee802_1x_mka_live_peer_body_present
- */
- static Boolean
- ieee802_1x_mka_live_peer_body_present(
- struct ieee802_1x_mka_participant *participant)
- {
- return !dl_list_empty(&participant->live_peers);
- }
- /**
- * ieee802_1x_kay_get_live_peer_length
- */
- static int
- ieee802_1x_mka_get_live_peer_length(
- struct ieee802_1x_mka_participant *participant)
- {
- int len = MKA_HDR_LEN;
- struct ieee802_1x_kay_peer *peer;
- dl_list_for_each(peer, &participant->live_peers,
- struct ieee802_1x_kay_peer, list)
- len += sizeof(struct ieee802_1x_mka_peer_id);
- return (len + 0x3) & ~0x3;
- }
- /**
- * ieee802_1x_mka_encode_live_peer_body -
- */
- static int
- ieee802_1x_mka_encode_live_peer_body(
- struct ieee802_1x_mka_participant *participant,
- struct wpabuf *buf)
- {
- struct ieee802_1x_mka_peer_body *body;
- struct ieee802_1x_kay_peer *peer;
- unsigned int length;
- struct ieee802_1x_mka_peer_id *body_peer;
- length = ieee802_1x_mka_get_live_peer_length(participant);
- body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
- body->type = MKA_LIVE_PEER_LIST;
- set_mka_param_body_len(body, length - MKA_HDR_LEN);
- dl_list_for_each(peer, &participant->live_peers,
- struct ieee802_1x_kay_peer, list) {
- body_peer = wpabuf_put(buf,
- sizeof(struct ieee802_1x_mka_peer_id));
- os_memcpy(body_peer->mi, peer->mi, MI_LEN);
- body_peer->mn = host_to_be32(peer->mn);
- body_peer++;
- }
- ieee802_1x_mka_dump_peer_body(body);
- return 0;
- }
- /**
- * ieee802_1x_mka_potential_peer_body_present
- */
- static Boolean
- ieee802_1x_mka_potential_peer_body_present(
- struct ieee802_1x_mka_participant *participant)
- {
- return !dl_list_empty(&participant->potential_peers);
- }
- /**
- * ieee802_1x_kay_get_potential_peer_length
- */
- static int
- ieee802_1x_mka_get_potential_peer_length(
- struct ieee802_1x_mka_participant *participant)
- {
- int len = MKA_HDR_LEN;
- struct ieee802_1x_kay_peer *peer;
- dl_list_for_each(peer, &participant->potential_peers,
- struct ieee802_1x_kay_peer, list)
- len += sizeof(struct ieee802_1x_mka_peer_id);
- return (len + 0x3) & ~0x3;
- }
- /**
- * ieee802_1x_mka_encode_potential_peer_body -
- */
- static int
- ieee802_1x_mka_encode_potential_peer_body(
- struct ieee802_1x_mka_participant *participant,
- struct wpabuf *buf)
- {
- struct ieee802_1x_mka_peer_body *body;
- struct ieee802_1x_kay_peer *peer;
- unsigned int length;
- struct ieee802_1x_mka_peer_id *body_peer;
- length = ieee802_1x_mka_get_potential_peer_length(participant);
- body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
- body->type = MKA_POTENTIAL_PEER_LIST;
- set_mka_param_body_len(body, length - MKA_HDR_LEN);
- dl_list_for_each(peer, &participant->potential_peers,
- struct ieee802_1x_kay_peer, list) {
- body_peer = wpabuf_put(buf,
- sizeof(struct ieee802_1x_mka_peer_id));
- os_memcpy(body_peer->mi, peer->mi, MI_LEN);
- body_peer->mn = host_to_be32(peer->mn);
- body_peer++;
- }
- ieee802_1x_mka_dump_peer_body(body);
- return 0;
- }
- /**
- * ieee802_1x_mka_i_in_peerlist -
- */
- static Boolean
- ieee802_1x_mka_i_in_peerlist(struct ieee802_1x_mka_participant *participant,
- const u8 *mka_msg, size_t msg_len)
- {
- Boolean included = FALSE;
- struct ieee802_1x_mka_hdr *hdr;
- size_t body_len;
- size_t left_len;
- u8 body_type;
- u32 peer_mn;
- be32 _peer_mn;
- const u8 *peer_mi;
- const u8 *pos;
- size_t i;
- pos = mka_msg;
- left_len = msg_len;
- while (left_len > (MKA_HDR_LEN + DEFAULT_ICV_LEN)) {
- hdr = (struct ieee802_1x_mka_hdr *) pos;
- body_len = get_mka_param_body_len(hdr);
- body_type = get_mka_param_body_type(hdr);
- if (body_type != MKA_LIVE_PEER_LIST &&
- body_type != MKA_POTENTIAL_PEER_LIST)
- goto SKIP_PEER;
- ieee802_1x_mka_dump_peer_body(
- (struct ieee802_1x_mka_peer_body *)pos);
- if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
- wpa_printf(MSG_ERROR,
- "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",
- (int) left_len, (int) MKA_HDR_LEN,
- (int) body_len, DEFAULT_ICV_LEN);
- goto SKIP_PEER;
- }
- if ((body_len % 16) != 0) {
- wpa_printf(MSG_ERROR,
- "KaY: MKA Peer Packet Body Length (%d bytes) should multiple of 16 octets",
- (int) body_len);
- goto SKIP_PEER;
- }
- for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
- peer_mi = MKA_HDR_LEN + pos + i;
- os_memcpy(&_peer_mn, peer_mi + MI_LEN,
- sizeof(_peer_mn));
- peer_mn = be_to_host32(_peer_mn);
- if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0 &&
- peer_mn == participant->mn) {
- included = TRUE;
- break;
- }
- }
- if (included)
- return TRUE;
- SKIP_PEER:
- left_len -= body_len + MKA_HDR_LEN;
- pos += body_len + MKA_HDR_LEN;
- }
- return FALSE;
- }
- /**
- * ieee802_1x_mka_decode_live_peer_body -
- */
- static int ieee802_1x_mka_decode_live_peer_body(
- struct ieee802_1x_mka_participant *participant,
- const u8 *peer_msg, size_t msg_len)
- {
- const struct ieee802_1x_mka_hdr *hdr;
- struct ieee802_1x_kay_peer *peer;
- size_t body_len;
- u32 peer_mn;
- be32 _peer_mn;
- const u8 *peer_mi;
- size_t i;
- Boolean is_included;
- is_included = ieee802_1x_kay_is_in_live_peer(
- participant, participant->current_peer_id.mi);
- hdr = (const struct ieee802_1x_mka_hdr *) peer_msg;
- body_len = get_mka_param_body_len(hdr);
- if (body_len % 16 != 0) {
- wpa_printf(MSG_ERROR,
- "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
- body_len);
- return -1;
- }
- for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
- peer_mi = MKA_HDR_LEN + peer_msg + i;
- os_memcpy(&_peer_mn, peer_mi + MI_LEN, sizeof(_peer_mn));
- peer_mn = be_to_host32(_peer_mn);
- /* it is myself */
- if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
- /* My message id is used by other participant */
- if (peer_mn > participant->mn) {
- if (os_get_random(participant->mi,
- sizeof(participant->mi)) < 0)
- wpa_printf(MSG_DEBUG,
- "KaY: Could not update mi");
- participant->mn = 0;
- }
- continue;
- }
- if (!is_included)
- continue;
- peer = ieee802_1x_kay_get_peer(participant, peer_mi);
- if (NULL != peer) {
- peer->mn = peer_mn;
- peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
- } else {
- if (!ieee802_1x_kay_create_potential_peer(
- participant, peer_mi, peer_mn)) {
- return -1;
- }
- }
- }
- return 0;
- }
- /**
- * ieee802_1x_mka_decode_potential_peer_body -
- */
- static int
- ieee802_1x_mka_decode_potential_peer_body(
- struct ieee802_1x_mka_participant *participant,
- const u8 *peer_msg, size_t msg_len)
- {
- struct ieee802_1x_mka_hdr *hdr;
- size_t body_len;
- u32 peer_mn;
- be32 _peer_mn;
- const u8 *peer_mi;
- size_t i;
- hdr = (struct ieee802_1x_mka_hdr *) peer_msg;
- body_len = get_mka_param_body_len(hdr);
- if (body_len % 16 != 0) {
- wpa_printf(MSG_ERROR,
- "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
- body_len);
- return -1;
- }
- for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
- peer_mi = MKA_HDR_LEN + peer_msg + i;
- os_memcpy(&_peer_mn, peer_mi + MI_LEN, sizeof(_peer_mn));
- peer_mn = be_to_host32(_peer_mn);
- /* it is myself */
- if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
- /* My message id is used by other participant */
- if (peer_mn > participant->mn) {
- if (os_get_random(participant->mi,
- sizeof(participant->mi)) < 0)
- wpa_printf(MSG_DEBUG,
- "KaY: Could not update mi");
- participant->mn = 0;
- }
- continue;
- }
- }
- return 0;
- }
- /**
- * ieee802_1x_mka_sak_use_body_present
- */
- static Boolean
- ieee802_1x_mka_sak_use_body_present(
- struct ieee802_1x_mka_participant *participant)
- {
- if (participant->to_use_sak)
- return TRUE;
- else
- return FALSE;
- }
- /**
- * ieee802_1x_mka_get_sak_use_length
- */
- static int
- ieee802_1x_mka_get_sak_use_length(
- struct ieee802_1x_mka_participant *participant)
- {
- int length = MKA_HDR_LEN;
- if (participant->kay->macsec_desired && participant->advised_desired)
- length = sizeof(struct ieee802_1x_mka_sak_use_body);
- else
- length = MKA_HDR_LEN;
- length = (length + 0x3) & ~0x3;
- return length;
- }
- /**
- *
- */
- static u32
- ieee802_1x_mka_get_lpn(struct ieee802_1x_mka_participant *principal,
- struct ieee802_1x_mka_ki *ki)
- {
- struct receive_sa *rxsa;
- struct receive_sc *rxsc;
- u32 lpn = 0;
- dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
- dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
- {
- if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
- secy_get_receive_lowest_pn(principal->kay,
- rxsa);
- lpn = lpn > rxsa->lowest_pn ?
- lpn : rxsa->lowest_pn;
- break;
- }
- }
- }
- if (lpn == 0)
- lpn = 1;
- return lpn;
- }
- /**
- * ieee802_1x_mka_encode_sak_use_body -
- */
- static int
- ieee802_1x_mka_encode_sak_use_body(
- struct ieee802_1x_mka_participant *participant,
- struct wpabuf *buf)
- {
- struct ieee802_1x_mka_sak_use_body *body;
- unsigned int length;
- u32 pn = 1;
- length = ieee802_1x_mka_get_sak_use_length(participant);
- body = wpabuf_put(buf, length);
- body->type = MKA_SAK_USE;
- set_mka_param_body_len(body, length - MKA_HDR_LEN);
- if (length == MKA_HDR_LEN) {
- body->ptx = TRUE;
- body->prx = TRUE;
- body->lan = 0;
- body->lrx = FALSE;
- body->ltx = FALSE;
- body->delay_protect = FALSE;
- return 0;
- }
- /* data protect, lowest accept packet number */
- body->delay_protect = participant->kay->macsec_replay_protect;
- pn = ieee802_1x_mka_get_lpn(participant, &participant->lki);
- if (pn > participant->kay->pn_exhaustion) {
- wpa_printf(MSG_WARNING, "KaY: My LPN exhaustion");
- if (participant->is_key_server)
- participant->new_sak = TRUE;
- }
- body->llpn = host_to_be32(pn);
- pn = ieee802_1x_mka_get_lpn(participant, &participant->oki);
- body->olpn = host_to_be32(pn);
- /* plain tx, plain rx */
- if (participant->kay->macsec_protect)
- body->ptx = FALSE;
- else
- body->ptx = TRUE;
- if (participant->kay->macsec_validate == Strict)
- body->prx = FALSE;
- else
- body->prx = TRUE;
- /* latest key: rx, tx, key server member identifier key number */
- body->lan = participant->lan;
- os_memcpy(body->lsrv_mi, participant->lki.mi,
- sizeof(body->lsrv_mi));
- body->lkn = host_to_be32(participant->lki.kn);
- body->lrx = participant->lrx;
- body->ltx = participant->ltx;
- /* old key: rx, tx, key server member identifier key number */
- body->oan = participant->oan;
- if (participant->oki.kn != participant->lki.kn &&
- participant->oki.kn != 0) {
- body->otx = TRUE;
- body->orx = TRUE;
- os_memcpy(body->osrv_mi, participant->oki.mi,
- sizeof(body->osrv_mi));
- body->okn = host_to_be32(participant->oki.kn);
- } else {
- body->otx = FALSE;
- body->orx = FALSE;
- }
- /* set CP's variable */
- if (body->ltx) {
- if (!participant->kay->tx_enable)
- participant->kay->tx_enable = TRUE;
- if (!participant->kay->port_enable)
- participant->kay->port_enable = TRUE;
- }
- if (body->lrx) {
- if (!participant->kay->rx_enable)
- participant->kay->rx_enable = TRUE;
- }
- ieee802_1x_mka_dump_sak_use_body(body);
- return 0;
- }
- /**
- * ieee802_1x_mka_decode_sak_use_body -
- */
- static int
- ieee802_1x_mka_decode_sak_use_body(
- struct ieee802_1x_mka_participant *participant,
- const u8 *mka_msg, size_t msg_len)
- {
- struct ieee802_1x_mka_hdr *hdr;
- struct ieee802_1x_mka_sak_use_body *body;
- struct ieee802_1x_kay_peer *peer;
- struct transmit_sa *txsa;
- struct data_key *sa_key = NULL;
- size_t body_len;
- struct ieee802_1x_mka_ki ki;
- u32 lpn;
- Boolean all_receiving;
- Boolean founded;
- if (!participant->principal) {
- wpa_printf(MSG_WARNING, "KaY: Participant is not principal");
- return -1;
- }
- peer = ieee802_1x_kay_get_live_peer(participant,
- participant->current_peer_id.mi);
- if (!peer) {
- wpa_printf(MSG_WARNING, "KaY: the peer is not my live peer");
- return -1;
- }
- hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
- body_len = get_mka_param_body_len(hdr);
- body = (struct ieee802_1x_mka_sak_use_body *) mka_msg;
- ieee802_1x_mka_dump_sak_use_body(body);
- if ((body_len != 0) && (body_len < 40)) {
- wpa_printf(MSG_ERROR,
- "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 0, 40, or more octets",
- (int) body_len);
- return -1;
- }
- /* TODO: what action should I take when peer does not support MACsec */
- if (body_len == 0) {
- wpa_printf(MSG_WARNING, "KaY: Peer does not support MACsec");
- return 0;
- }
- /* TODO: when the plain tx or rx of peer is true, should I change
- * the attribute of controlled port
- */
- if (body->prx)
- wpa_printf(MSG_WARNING, "KaY: peer's plain rx are TRUE");
- if (body->ptx)
- wpa_printf(MSG_WARNING, "KaY: peer's plain tx are TRUE");
- /* check latest key is valid */
- if (body->ltx || body->lrx) {
- founded = FALSE;
- os_memcpy(ki.mi, body->lsrv_mi, sizeof(ki.mi));
- ki.kn = be_to_host32(body->lkn);
- dl_list_for_each(sa_key, &participant->sak_list,
- struct data_key, list) {
- if (is_ki_equal(&sa_key->key_identifier, &ki)) {
- founded = TRUE;
- break;
- }
- }
- if (!founded) {
- wpa_printf(MSG_WARNING, "KaY: Latest key is invalid");
- return -1;
- }
- if (os_memcmp(participant->lki.mi, body->lsrv_mi,
- sizeof(participant->lki.mi)) == 0 &&
- be_to_host32(body->lkn) == participant->lki.kn &&
- body->lan == participant->lan) {
- peer->sak_used = TRUE;
- }
- if (body->ltx && peer->is_key_server) {
- ieee802_1x_cp_set_servertransmitting(
- participant->kay->cp, TRUE);
- ieee802_1x_cp_sm_step(participant->kay->cp);
- }
- }
- /* check old key is valid */
- if (body->otx || body->orx) {
- if (os_memcmp(participant->oki.mi, body->osrv_mi,
- sizeof(participant->oki.mi)) != 0 ||
- be_to_host32(body->okn) != participant->oki.kn ||
- body->oan != participant->oan) {
- wpa_printf(MSG_WARNING, "KaY: Old key is invalid");
- return -1;
- }
- }
- /* TODO: how to set the MACsec hardware when delay_protect is true */
- if (body->delay_protect &&
- (!be_to_host32(body->llpn) || !be_to_host32(body->olpn))) {
- wpa_printf(MSG_WARNING,
- "KaY: Lowest packet number should greater than 0 when delay_protect is TRUE");
- return -1;
- }
- /* check all live peer have used the sak for receiving sa */
- all_receiving = TRUE;
- dl_list_for_each(peer, &participant->live_peers,
- struct ieee802_1x_kay_peer, list) {
- if (!peer->sak_used) {
- all_receiving = FALSE;
- break;
- }
- }
- if (all_receiving) {
- participant->to_dist_sak = FALSE;
- ieee802_1x_cp_set_allreceiving(participant->kay->cp, TRUE);
- ieee802_1x_cp_sm_step(participant->kay->cp);
- }
- /* if i'm key server, and detects peer member pn exhaustion, rekey.*/
- lpn = be_to_host32(body->llpn);
- if (lpn > participant->kay->pn_exhaustion) {
- if (participant->is_key_server) {
- participant->new_sak = TRUE;
- wpa_printf(MSG_WARNING, "KaY: Peer LPN exhaustion");
- }
- }
- founded = FALSE;
- dl_list_for_each(txsa, &participant->txsc->sa_list,
- struct transmit_sa, list) {
- if (sa_key != NULL && txsa->pkey == sa_key) {
- founded = TRUE;
- break;
- }
- }
- if (!founded) {
- wpa_printf(MSG_WARNING, "KaY: Can't find txsa");
- return -1;
- }
- /* FIXME: Secy creates txsa with default npn. If MKA detected Latest Key
- * npn is larger than txsa's npn, set it to txsa.
- */
- secy_get_transmit_next_pn(participant->kay, txsa);
- if (lpn > txsa->next_pn) {
- secy_set_transmit_next_pn(participant->kay, txsa);
- wpa_printf(MSG_INFO, "KaY: update lpn =0x%x", lpn);
- }
- return 0;
- }
- /**
- * ieee802_1x_mka_dist_sak_body_present
- */
- static Boolean
- ieee802_1x_mka_dist_sak_body_present(
- struct ieee802_1x_mka_participant *participant)
- {
- if (!participant->to_dist_sak || !participant->new_key)
- return FALSE;
- return TRUE;
- }
- /**
- * ieee802_1x_kay_get_dist_sak_length
- */
- static int
- ieee802_1x_mka_get_dist_sak_length(
- struct ieee802_1x_mka_participant *participant)
- {
- int length;
- int cs_index = participant->kay->macsec_csindex;
- if (participant->advised_desired) {
- length = sizeof(struct ieee802_1x_mka_dist_sak_body);
- if (cs_index != DEFAULT_CS_INDEX)
- length += CS_ID_LEN;
- length += cipher_suite_tbl[cs_index].sak_len + 8;
- } else {
- length = MKA_HDR_LEN;
- }
- length = (length + 0x3) & ~0x3;
- return length;
- }
- /**
- * ieee802_1x_mka_encode_dist_sak_body -
- */
- static int
- ieee802_1x_mka_encode_dist_sak_body(
- struct ieee802_1x_mka_participant *participant,
- struct wpabuf *buf)
- {
- struct ieee802_1x_mka_dist_sak_body *body;
- struct data_key *sak;
- unsigned int length;
- int cs_index;
- int sak_pos;
- length = ieee802_1x_mka_get_dist_sak_length(participant);
- body = wpabuf_put(buf, length);
- body->type = MKA_DISTRIBUTED_SAK;
- set_mka_param_body_len(body, length - MKA_HDR_LEN);
- if (length == MKA_HDR_LEN) {
- body->confid_offset = 0;
- body->dan = 0;
- return 0;
- }
- sak = participant->new_key;
- body->confid_offset = sak->confidentiality_offset;
- body->dan = sak->an;
- body->kn = host_to_be32(sak->key_identifier.kn);
- cs_index = participant->kay->macsec_csindex;
- sak_pos = 0;
- if (cs_index != DEFAULT_CS_INDEX) {
- os_memcpy(body->sak, cipher_suite_tbl[cs_index].id, CS_ID_LEN);
- sak_pos = CS_ID_LEN;
- }
- if (aes_wrap(participant->kek.key, 16,
- cipher_suite_tbl[cs_index].sak_len / 8,
- sak->key, body->sak + sak_pos)) {
- wpa_printf(MSG_ERROR, "KaY: AES wrap failed");
- return -1;
- }
- ieee802_1x_mka_dump_dist_sak_body(body);
- return 0;
- }
- /**
- * ieee802_1x_kay_init_data_key -
- */
- static struct data_key *
- ieee802_1x_kay_init_data_key(const struct key_conf *conf)
- {
- struct data_key *pkey;
- if (!conf)
- return NULL;
- pkey = os_zalloc(sizeof(*pkey));
- if (pkey == NULL) {
- wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
- return NULL;
- }
- pkey->key = os_zalloc(conf->key_len);
- if (pkey->key == NULL) {
- wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
- os_free(pkey);
- return NULL;
- }
- os_memcpy(pkey->key, conf->key, conf->key_len);
- os_memcpy(&pkey->key_identifier, &conf->ki,
- sizeof(pkey->key_identifier));
- pkey->confidentiality_offset = conf->offset;
- pkey->an = conf->an;
- pkey->transmits = conf->tx;
- pkey->receives = conf->rx;
- os_get_time(&pkey->created_time);
- pkey->user = 1;
- return pkey;
- }
- /**
- * ieee802_1x_kay_decode_dist_sak_body -
- */
- static int
- ieee802_1x_mka_decode_dist_sak_body(
- struct ieee802_1x_mka_participant *participant,
- const u8 *mka_msg, size_t msg_len)
- {
- struct ieee802_1x_mka_hdr *hdr;
- struct ieee802_1x_mka_dist_sak_body *body;
- struct ieee802_1x_kay_peer *peer;
- struct macsec_ciphersuite *cs;
- size_t body_len;
- struct key_conf *conf;
- struct data_key *sa_key = NULL;
- struct ieee802_1x_mka_ki sak_ki;
- int sak_len;
- u8 *wrap_sak;
- u8 *unwrap_sak;
- hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
- body_len = get_mka_param_body_len(hdr);
- if ((body_len != 0) && (body_len != 28) && (body_len < 36)) {
- wpa_printf(MSG_ERROR,
- "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 0, 28, 36, or more octets",
- (int) body_len);
- return -1;
- }
- if (!participant->principal) {
- wpa_printf(MSG_ERROR,
- "KaY: I can't accept the distributed SAK as I am not principal");
- return -1;
- }
- if (participant->is_key_server) {
- wpa_printf(MSG_ERROR,
- "KaY: I can't accept the distributed SAK as myself is key server ");
- return -1;
- }
- if (!participant->kay->macsec_desired ||
- participant->kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
- wpa_printf(MSG_ERROR,
- "KaY: I am not MACsec-desired or without MACsec capable");
- return -1;
- }
- peer = ieee802_1x_kay_get_live_peer(participant,
- participant->current_peer_id.mi);
- if (!peer) {
- wpa_printf(MSG_ERROR,
- "KaY: The key server is not in my live peers list");
- return -1;
- }
- if (!sci_equal(&participant->kay->key_server_sci, &peer->sci)) {
- wpa_printf(MSG_ERROR, "KaY: The key server is not elected");
- return -1;
- }
- if (body_len == 0) {
- participant->kay->authenticated = TRUE;
- participant->kay->secured = FALSE;
- participant->kay->failed = FALSE;
- participant->advised_desired = FALSE;
- ieee802_1x_cp_connect_authenticated(participant->kay->cp);
- ieee802_1x_cp_sm_step(participant->kay->cp);
- wpa_printf(MSG_WARNING, "KaY:The Key server advise no MACsec");
- participant->to_use_sak = TRUE;
- return 0;
- }
- participant->advised_desired = TRUE;
- participant->kay->authenticated = FALSE;
- participant->kay->secured = TRUE;
- participant->kay->failed = FALSE;
- ieee802_1x_cp_connect_secure(participant->kay->cp);
- ieee802_1x_cp_sm_step(participant->kay->cp);
- body = (struct ieee802_1x_mka_dist_sak_body *)mka_msg;
- ieee802_1x_mka_dump_dist_sak_body(body);
- dl_list_for_each(sa_key, &participant->sak_list, struct data_key, list)
- {
- if (os_memcmp(sa_key->key_identifier.mi,
- participant->current_peer_id.mi, MI_LEN) == 0 &&
- sa_key->key_identifier.kn == be_to_host32(body->kn)) {
- wpa_printf(MSG_WARNING, "KaY:The Key has installed");
- return 0;
- }
- }
- if (body_len == 28) {
- sak_len = DEFAULT_SA_KEY_LEN;
- wrap_sak = body->sak;
- participant->kay->macsec_csindex = DEFAULT_CS_INDEX;
- } else {
- cs = ieee802_1x_kay_get_cipher_suite(participant, body->sak);
- if (!cs) {
- wpa_printf(MSG_ERROR,
- "KaY: I can't support the Cipher Suite advised by key server");
- return -1;
- }
- sak_len = cs->sak_len;
- wrap_sak = body->sak + CS_ID_LEN;
- participant->kay->macsec_csindex = cs->index;
- }
- unwrap_sak = os_zalloc(sak_len);
- if (!unwrap_sak) {
- wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
- return -1;
- }
- if (aes_unwrap(participant->kek.key, 16, sak_len >> 3, wrap_sak,
- unwrap_sak)) {
- wpa_printf(MSG_ERROR, "KaY: AES unwrap failed");
- os_free(unwrap_sak);
- return -1;
- }
- wpa_hexdump(MSG_DEBUG, "\tAES Key Unwrap of SAK:", unwrap_sak, sak_len);
- conf = os_zalloc(sizeof(*conf));
- if (!conf) {
- wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
- os_free(unwrap_sak);
- return -1;
- }
- conf->key_len = sak_len;
- conf->key = os_zalloc(conf->key_len);
- if (!conf->key) {
- wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
- os_free(unwrap_sak);
- os_free(conf);
- return -1;
- }
- os_memcpy(conf->key, unwrap_sak, conf->key_len);
- os_memcpy(&sak_ki.mi, &participant->current_peer_id.mi,
- sizeof(sak_ki.mi));
- sak_ki.kn = be_to_host32(body->kn);
- os_memcpy(conf->ki.mi, sak_ki.mi, MI_LEN);
- conf->ki.kn = sak_ki.kn;
- conf->an = body->dan;
- conf->offset = body->confid_offset;
- conf->rx = TRUE;
- conf->tx = TRUE;
- sa_key = ieee802_1x_kay_init_data_key(conf);
- if (!sa_key) {
- os_free(unwrap_sak);
- os_free(conf->key);
- os_free(conf);
- return -1;
- }
- dl_list_add(&participant->sak_list, &sa_key->list);
- ieee802_1x_cp_set_ciphersuite(
- participant->kay->cp,
- cipher_suite_tbl[participant->kay->macsec_csindex].id);
- ieee802_1x_cp_sm_step(participant->kay->cp);
- ieee802_1x_cp_set_offset(participant->kay->cp, body->confid_offset);
- ieee802_1x_cp_sm_step(participant->kay->cp);
- ieee802_1x_cp_set_distributedki(participant->kay->cp, &sak_ki);
- ieee802_1x_cp_set_distributedan(participant->kay->cp, body->dan);
- ieee802_1x_cp_signal_newsak(participant->kay->cp);
- ieee802_1x_cp_sm_step(participant->kay->cp);
- participant->to_use_sak = TRUE;
- os_free(unwrap_sak);
- os_free(conf->key);
- os_free(conf);
- return 0;
- }
- /**
- * ieee802_1x_mka_icv_body_present
- */
- static Boolean
- ieee802_1x_mka_icv_body_present(struct ieee802_1x_mka_participant *participant)
- {
- return TRUE;
- }
- /**
- * ieee802_1x_kay_get_icv_length
- */
- static int
- ieee802_1x_mka_get_icv_length(struct ieee802_1x_mka_participant *participant)
- {
- int length;
- length = sizeof(struct ieee802_1x_mka_icv_body);
- length += mka_alg_tbl[participant->kay->mka_algindex].icv_len;
- return (length + 0x3) & ~0x3;
- }
- /**
- * ieee802_1x_mka_encode_icv_body -
- */
- static int
- ieee802_1x_mka_encode_icv_body(struct ieee802_1x_mka_participant *participant,
- struct wpabuf *buf)
- {
- struct ieee802_1x_mka_icv_body *body;
- unsigned int length;
- u8 cmac[MAX_ICV_LEN];
- length = ieee802_1x_mka_get_icv_length(participant);
- if (length != DEFAULT_ICV_LEN) {
- body = wpabuf_put(buf, MKA_HDR_LEN);
- body->type = MKA_ICV_INDICATOR;
- set_mka_param_body_len(body, length - MKA_HDR_LEN);
- }
- if (mka_alg_tbl[participant->kay->mka_algindex].icv_hash(
- participant->ick.key, wpabuf_head(buf), buf->used, cmac)) {
- wpa_printf(MSG_ERROR, "KaY, omac1_aes_128 failed");
- return -1;
- }
- if (length != DEFAULT_ICV_LEN) {
- os_memcpy(wpabuf_put(buf, length - MKA_HDR_LEN), cmac,
- length - MKA_HDR_LEN);
- } else {
- os_memcpy(wpabuf_put(buf, length), cmac, length);
- }
- return 0;
- }
- /**
- * ieee802_1x_mka_decode_icv_body -
- */
- static u8 *
- ieee802_1x_mka_decode_icv_body(struct ieee802_1x_mka_participant *participant,
- const u8 *mka_msg, size_t msg_len)
- {
- struct ieee802_1x_mka_hdr *hdr;
- struct ieee802_1x_mka_icv_body *body;
- size_t body_len;
- size_t left_len;
- u8 body_type;
- const u8 *pos;
- pos = mka_msg;
- left_len = msg_len;
- while (left_len > (MKA_HDR_LEN + DEFAULT_ICV_LEN)) {
- hdr = (struct ieee802_1x_mka_hdr *) pos;
- body_len = get_mka_param_body_len(hdr);
- body_type = get_mka_param_body_type(hdr);
- if (left_len < (body_len + MKA_HDR_LEN))
- break;
- if (body_type != MKA_ICV_INDICATOR) {
- left_len -= MKA_HDR_LEN + body_len;
- pos += MKA_HDR_LEN + body_len;
- continue;
- }
- body = (struct ieee802_1x_mka_icv_body *)pos;
- if (body_len
- < mka_alg_tbl[participant->kay->mka_algindex].icv_len) {
- return NULL;
- }
- return body->icv;
- }
- return (u8 *) (mka_msg + msg_len - DEFAULT_ICV_LEN);
- }
- /**
- * ieee802_1x_mka_decode_dist_cak_body-
- */
- static int
- ieee802_1x_mka_decode_dist_cak_body(
- struct ieee802_1x_mka_participant *participant,
- const u8 *mka_msg, size_t msg_len)
- {
- struct ieee802_1x_mka_hdr *hdr;
- size_t body_len;
- hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
- body_len = get_mka_param_body_len(hdr);
- if (body_len < 28) {
- wpa_printf(MSG_ERROR,
- "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 28 or more octets",
- (int) body_len);
- return -1;
- }
- return 0;
- }
- /**
- * ieee802_1x_mka_decode_kmd_body -
- */
- static int
- ieee802_1x_mka_decode_kmd_body(
- struct ieee802_1x_mka_participant *participant,
- const u8 *mka_msg, size_t msg_len)
- {
- struct ieee802_1x_mka_hdr *hdr;
- size_t body_len;
- hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
- body_len = get_mka_param_body_len(hdr);
- if (body_len < 5) {
- wpa_printf(MSG_ERROR,
- "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 5 or more octets",
- (int) body_len);
- return -1;
- }
- return 0;
- }
- /**
- * ieee802_1x_mka_decode_announce_body -
- */
- static int ieee802_1x_mka_decode_announce_body(
- struct ieee802_1x_mka_participant *participant,
- const u8 *mka_msg, size_t msg_len)
- {
- return 0;
- }
- static struct mka_param_body_handler mka_body_handler[] = {
- /* basic parameter set */
- {
- ieee802_1x_mka_encode_basic_body,
- NULL,
- ieee802_1x_mka_basic_body_length,
- ieee802_1x_mka_basic_body_present
- },
- /* live peer list parameter set */
- {
- ieee802_1x_mka_encode_live_peer_body,
- ieee802_1x_mka_decode_live_peer_body,
- ieee802_1x_mka_get_live_peer_length,
- ieee802_1x_mka_live_peer_body_present
- },
- /* potential peer list parameter set */
- {
- ieee802_1x_mka_encode_potential_peer_body,
- ieee802_1x_mka_decode_potential_peer_body,
- ieee802_1x_mka_get_potential_peer_length,
- ieee802_1x_mka_potential_peer_body_present
- },
- /* sak use parameter set */
- {
- ieee802_1x_mka_encode_sak_use_body,
- ieee802_1x_mka_decode_sak_use_body,
- ieee802_1x_mka_get_sak_use_length,
- ieee802_1x_mka_sak_use_body_present
- },
- /* distribute sak parameter set */
- {
- ieee802_1x_mka_encode_dist_sak_body,
- ieee802_1x_mka_decode_dist_sak_body,
- ieee802_1x_mka_get_dist_sak_length,
- ieee802_1x_mka_dist_sak_body_present
- },
- /* distribute cak parameter set */
- {
- NULL,
- ieee802_1x_mka_decode_dist_cak_body,
- NULL,
- NULL
- },
- /* kmd parameter set */
- {
- NULL,
- ieee802_1x_mka_decode_kmd_body,
- NULL,
- NULL
- },
- /* announce parameter set */
- {
- NULL,
- ieee802_1x_mka_decode_announce_body,
- NULL,
- NULL
- },
- /* icv parameter set */
- {
- ieee802_1x_mka_encode_icv_body,
- NULL,
- ieee802_1x_mka_get_icv_length,
- ieee802_1x_mka_icv_body_present
- },
- };
- /**
- * ieee802_1x_kay_deinit_data_key -
- */
- static void ieee802_1x_kay_deinit_data_key(struct data_key *pkey)
- {
- if (!pkey)
- return;
- pkey->user--;
- if (pkey->user > 1)
- return;
- dl_list_del(&pkey->list);
- os_free(pkey->key);
- os_free(pkey);
- }
- /**
- * ieee802_1x_kay_generate_new_sak -
- */
- static int
- ieee802_1x_kay_generate_new_sak(struct ieee802_1x_mka_participant *participant)
- {
- struct data_key *sa_key = NULL;
- struct key_conf *conf;
- struct ieee802_1x_kay_peer *peer;
- struct ieee802_1x_kay *kay = participant->kay;
- int ctx_len, ctx_offset;
- u8 *context;
- /* check condition for generating a fresh SAK:
- * must have one live peer
- * and MKA life time elapse since last distribution
- * or potential peer is empty
- */
- if (dl_list_empty(&participant->live_peers)) {
- wpa_printf(MSG_ERROR,
- "KaY: Live peers list must not empty when generating fresh SAK");
- return -1;
- }
- /* FIXME: A fresh SAK not generated until
- * the live peer list contains at least one peer and
- * MKA life time has elapsed since the prior SAK was first distributed,
- * or the Key server's potential peer is empty
- * but I can't understand the second item, so
- * here only check first item and ingore
- * && (!dl_list_empty(&participant->potential_peers))) {
- */
- if ((time(NULL) - kay->dist_time) < MKA_LIFE_TIME / 1000) {
- wpa_printf(MSG_ERROR,
- "KaY: Life time have not elapsed since prior SAK distributed");
- return -1;
- }
- conf = os_zalloc(sizeof(*conf));
- if (!conf) {
- wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
- return -1;
- }
- conf->key_len = cipher_suite_tbl[kay->macsec_csindex].sak_len;
- conf->key = os_zalloc(conf->key_len);
- if (!conf->key) {
- os_free(conf);
- wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
- return -1;
- }
- ctx_len = conf->key_len + sizeof(kay->dist_kn);
- dl_list_for_each(peer, &participant->live_peers,
- struct ieee802_1x_kay_peer, list)
- ctx_len += sizeof(peer->mi);
- ctx_len += sizeof(participant->mi);
- context = os_zalloc(ctx_len);
- if (!context) {
- os_free(conf->key);
- os_free(conf);
- return -1;
- }
- ctx_offset = 0;
- if (os_get_random(context + ctx_offset, conf->key_len) < 0) {
- os_free(context);
- os_free(conf->key);
- os_free(conf);
- return -1;
- }
- ctx_offset += conf->key_len;
- dl_list_for_each(peer, &participant->live_peers,
- struct ieee802_1x_kay_peer, list) {
- os_memcpy(context + ctx_offset, peer->mi, sizeof(peer->mi));
- ctx_offset += sizeof(peer->mi);
- }
- os_memcpy(context + ctx_offset, participant->mi,
- sizeof(participant->mi));
- ctx_offset += sizeof(participant->mi);
- os_memcpy(context + ctx_offset, &kay->dist_kn, sizeof(kay->dist_kn));
- if (conf->key_len == 16) {
- ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
- context, ctx_len, conf->key);
- } else if (conf->key_len == 32) {
- ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
- context, ctx_len, conf->key);
- } else {
- wpa_printf(MSG_ERROR, "KaY: SAK Length not support");
- os_free(conf->key);
- os_free(conf);
- os_free(context);
- return -1;
- }
- wpa_hexdump(MSG_DEBUG, "KaY: generated new SAK",
- conf->key, conf->key_len);
- os_memcpy(conf->ki.mi, participant->mi, MI_LEN);
- conf->ki.kn = participant->kay->dist_kn;
- conf->an = participant->kay->dist_an;
- conf->offset = kay->macsec_confidentiality;
- conf->rx = TRUE;
- conf->tx = TRUE;
- sa_key = ieee802_1x_kay_init_data_key(conf);
- if (!sa_key) {
- os_free(conf->key);
- os_free(conf);
- os_free(context);
- return -1;
- }
- participant->new_key = sa_key;
- dl_list_add(&participant->sak_list, &sa_key->list);
- ieee802_1x_cp_set_ciphersuite(participant->kay->cp,
- cipher_suite_tbl[kay->macsec_csindex].id);
- ieee802_1x_cp_sm_step(kay->cp);
- ieee802_1x_cp_set_offset(kay->cp, conf->offset);
- ieee802_1x_cp_sm_step(kay->cp);
- ieee802_1x_cp_set_distributedki(kay->cp, &conf->ki);
- ieee802_1x_cp_set_distributedan(kay->cp, conf->an);
- ieee802_1x_cp_signal_newsak(kay->cp);
- ieee802_1x_cp_sm_step(kay->cp);
- dl_list_for_each(peer, &participant->live_peers,
- struct ieee802_1x_kay_peer, list)
- peer->sak_used = FALSE;
- participant->kay->dist_kn++;
- participant->kay->dist_an++;
- if (participant->kay->dist_an > 3)
- participant->kay->dist_an = 0;
- participant->kay->dist_time = time(NULL);
- os_free(conf->key);
- os_free(conf);
- os_free(context);
- return 0;
- }
- /**
- * ieee802_1x_kay_elect_key_server - elect the key server
- * when to elect: whenever the live peers list changes
- */
- static int
- ieee802_1x_kay_elect_key_server(struct ieee802_1x_mka_participant *participant)
- {
- struct ieee802_1x_kay_peer *peer;
- struct ieee802_1x_kay_peer *key_server = NULL;
- struct ieee802_1x_kay *kay = participant->kay;
- Boolean i_is_key_server;
- if (participant->is_obliged_key_server) {
- participant->new_sak = TRUE;
- participant->to_dist_sak = FALSE;
- ieee802_1x_cp_set_electedself(kay->cp, TRUE);
- return 0;
- }
- /* elect the key server among the peers */
- dl_list_for_each(peer, &participant->live_peers,
- struct ieee802_1x_kay_peer, list) {
- if (!peer->is_key_server)
- continue;
- if (!key_server) {
- key_server = peer;
- continue;
- }
- if (peer->key_server_priority <
- key_server->key_server_priority) {
- key_server = peer;
- } else if (peer->key_server_priority ==
- key_server->key_server_priority) {
- if (os_memcmp(peer->sci.addr, key_server->sci.addr,
- ETH_ALEN) < 0)
- key_server = peer;
- }
- }
- /* elect the key server between me and the above elected peer */
- i_is_key_server = FALSE;
- if (key_server && participant->can_be_key_server) {
- if (kay->actor_priority
- < key_server->key_server_priority) {
- i_is_key_server = TRUE;
- } else if (kay->actor_priority
- == key_server->key_server_priority) {
- if (os_memcmp(kay->actor_sci.addr, key_server->sci.addr,
- ETH_ALEN) < 0)
- i_is_key_server = TRUE;
- }
- } else if (participant->can_be_key_server) {
- i_is_key_server = TRUE;
- }
- if (i_is_key_server) {
- ieee802_1x_cp_set_electedself(kay->cp, TRUE);
- if (!sci_equal(&kay->key_server_sci, &kay->actor_sci)) {
- ieee802_1x_cp_signal_chgdserver(kay->cp);
- ieee802_1x_cp_sm_step(kay->cp);
- }
- participant->is_key_server = TRUE;
- participant->principal = TRUE;
- participant->new_sak = TRUE;
- wpa_printf(MSG_DEBUG, "KaY: I is elected as key server");
- participant->to_dist_sak = FALSE;
- participant->is_elected = TRUE;
- os_memcpy(&kay->key_server_sci, &kay->actor_sci,
- sizeof(kay->key_server_sci));
- kay->key_server_priority = kay->actor_priority;
- } else if (key_server) {
- ieee802_1x_cp_set_electedself(kay->cp, FALSE);
- if (!sci_equal(&kay->key_server_sci, &key_server->sci)) {
- ieee802_1x_cp_signal_chgdserver(kay->cp);
- ieee802_1x_cp_sm_step(kay->cp);
- }
- participant->is_key_server = FALSE;
- participant->principal = TRUE;
- participant->is_elected = TRUE;
- os_memcpy(&kay->key_server_sci, &key_server->sci,
- sizeof(kay->key_server_sci));
- kay->key_server_priority = key_server->key_server_priority;
- } else {
- participant->principal = FALSE;
- participant->is_key_server = FALSE;
- participant->is_elected = FALSE;
- }
- return 0;
- }
- /**
- * ieee802_1x_kay_decide_macsec_use - the key server determinate
- * how to use MACsec: whether use MACsec and its capability
- * protectFrames will be advised if the key server and one of its live peers are
- * MACsec capable and one of those request MACsec protection
- */
- static int
- ieee802_1x_kay_decide_macsec_use(
- struct ieee802_1x_mka_participant *participant)
- {
- struct ieee802_1x_kay *kay = participant->kay;
- struct ieee802_1x_kay_peer *peer;
- enum macsec_cap less_capability;
- Boolean has_peer;
- if (!participant->is_key_server)
- return -1;
- /* key server self is MACsec-desired and requesting MACsec */
- if (!kay->macsec_desired) {
- participant->advised_desired = FALSE;
- return -1;
- }
- if (kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
- participant->advised_desired = FALSE;
- return -1;
- }
- less_capability = kay->macsec_capable;
- /* at least one of peers is MACsec-desired and requesting MACsec */
- has_peer = FALSE;
- dl_list_for_each(peer, &participant->live_peers,
- struct ieee802_1x_kay_peer, list) {
- if (!peer->macsec_desired)
- continue;
- if (peer->macsec_capability == MACSEC_CAP_NOT_IMPLEMENTED)
- continue;
- less_capability = (less_capability < peer->macsec_capability) ?
- less_capability : peer->macsec_capability;
- has_peer = TRUE;
- }
- if (has_peer) {
- participant->advised_desired = TRUE;
- participant->advised_capability = less_capability;
- kay->authenticated = FALSE;
- kay->secured = TRUE;
- kay->failed = FALSE;
- ieee802_1x_cp_connect_secure(kay->cp);
- ieee802_1x_cp_sm_step(kay->cp);
- } else {
- participant->advised_desired = FALSE;
- participant->advised_capability = MACSEC_CAP_NOT_IMPLEMENTED;
- participant->to_use_sak = FALSE;
- kay->authenticated = TRUE;
- kay->secured = FALSE;
- kay->failed = FALSE;
- kay->ltx_kn = 0;
- kay->ltx_an = 0;
- kay->lrx_kn = 0;
- kay->lrx_an = 0;
- kay->otx_kn = 0;
- kay->otx_an = 0;
- kay->orx_kn = 0;
- kay->orx_an = 0;
- ieee802_1x_cp_connect_authenticated(kay->cp);
- ieee802_1x_cp_sm_step(kay->cp);
- }
- return 0;
- }
- static const u8 pae_group_addr[ETH_ALEN] = {
- 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03
- };
- /**
- * ieee802_1x_kay_encode_mkpdu -
- */
- static int
- ieee802_1x_kay_encode_mkpdu(struct ieee802_1x_mka_participant *participant,
- struct wpabuf *pbuf)
- {
- unsigned int i;
- struct ieee8023_hdr *ether_hdr;
- struct ieee802_1x_hdr *eapol_hdr;
- ether_hdr = wpabuf_put(pbuf, sizeof(*ether_hdr));
- os_memcpy(ether_hdr->dest, pae_group_addr, sizeof(ether_hdr->dest));
- os_memcpy(ether_hdr->src, participant->kay->actor_sci.addr,
- sizeof(ether_hdr->dest));
- ether_hdr->ethertype = host_to_be16(ETH_P_EAPOL);
- eapol_hdr = wpabuf_put(pbuf, sizeof(*eapol_hdr));
- eapol_hdr->version = EAPOL_VERSION;
- eapol_hdr->type = IEEE802_1X_TYPE_EAPOL_MKA;
- eapol_hdr->length = host_to_be16(pbuf->size - pbuf->used);
- for (i = 0; i < ARRAY_SIZE(mka_body_handler); i++) {
- if (mka_body_handler[i].body_present &&
- mka_body_handler[i].body_present(participant)) {
- if (mka_body_handler[i].body_tx(participant, pbuf))
- return -1;
- }
- }
- return 0;
- }
- /**
- * ieee802_1x_participant_send_mkpdu -
- */
- static int
- ieee802_1x_participant_send_mkpdu(
- struct ieee802_1x_mka_participant *participant)
- {
- struct wpabuf *buf;
- struct ieee802_1x_kay *kay = participant->kay;
- size_t length = 0;
- unsigned int i;
- wpa_printf(MSG_DEBUG, "KaY: to enpacket and send the MKPDU");
- length += sizeof(struct ieee802_1x_hdr) + sizeof(struct ieee8023_hdr);
- for (i = 0; i < ARRAY_SIZE(mka_body_handler); i++) {
- if (mka_body_handler[i].body_present &&
- mka_body_handler[i].body_present(participant))
- length += mka_body_handler[i].body_length(participant);
- }
- buf = wpabuf_alloc(length);
- if (!buf) {
- wpa_printf(MSG_ERROR, "KaY: out of memory");
- return -1;
- }
- if (ieee802_1x_kay_encode_mkpdu(participant, buf)) {
- wpa_printf(MSG_ERROR, "KaY: encode mkpdu fail!");
- return -1;
- }
- l2_packet_send(kay->l2_mka, NULL, 0, wpabuf_head(buf), wpabuf_len(buf));
- wpabuf_free(buf);
- kay->active = TRUE;
- participant->active = TRUE;
- return 0;
- }
- static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa);
- /**
- * ieee802_1x_participant_timer -
- */
- static void ieee802_1x_participant_timer(void *eloop_ctx, void *timeout_ctx)
- {
- struct ieee802_1x_mka_participant *participant;
- struct ieee802_1x_kay *kay;
- struct ieee802_1x_kay_peer *peer, *pre_peer;
- time_t now = time(NULL);
- Boolean lp_changed;
- struct receive_sc *rxsc, *pre_rxsc;
- struct transmit_sa *txsa, *pre_txsa;
- participant = (struct ieee802_1x_mka_participant *)eloop_ctx;
- kay = participant->kay;
- if (participant->cak_life) {
- if (now > participant->cak_life) {
- kay->authenticated = FALSE;
- kay->secured = FALSE;
- kay->failed = TRUE;
- ieee802_1x_kay_delete_mka(kay, &participant->ckn);
- return;
- }
- }
- /* should delete MKA instance if there are not live peers
- * when the MKA life elapsed since its creating */
- if (participant->mka_life) {
- if (dl_list_empty(&participant->live_peers)) {
- if (now > participant->mka_life) {
- kay->authenticated = FALSE;
- kay->secured = FALSE;
- kay->failed = TRUE;
- ieee802_1x_kay_delete_mka(kay,
- &participant->ckn);
- return;
- }
- } else {
- participant->mka_life = 0;
- }
- }
- lp_changed = FALSE;
- dl_list_for_each_safe(peer, pre_peer, &participant->live_peers,
- struct ieee802_1x_kay_peer, list) {
- if (now > peer->expire) {
- wpa_printf(MSG_DEBUG, "KaY: Live peer removed");
- wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
- sizeof(peer->mi));
- wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
- dl_list_for_each_safe(rxsc, pre_rxsc,
- &participant->rxsc_list,
- struct receive_sc, list) {
- if (sci_equal(&rxsc->sci, &peer->sci)) {
- secy_delete_receive_sc(kay, rxsc);
- ieee802_1x_kay_deinit_receive_sc(
- participant, rxsc);
- }
- }
- dl_list_del(&peer->list);
- os_free(peer);
- lp_changed = TRUE;
- }
- }
- if (lp_changed) {
- if (dl_list_empty(&participant->live_peers)) {
- participant->advised_desired = FALSE;
- participant->advised_capability =
- MACSEC_CAP_NOT_IMPLEMENTED;
- participant->to_use_sak = FALSE;
- kay->authenticated = TRUE;
- kay->secured = FALSE;
- kay->failed = FALSE;
- kay->ltx_kn = 0;
- kay->ltx_an = 0;
- kay->lrx_kn = 0;
- kay->lrx_an = 0;
- kay->otx_kn = 0;
- kay->otx_an = 0;
- kay->orx_kn = 0;
- kay->orx_an = 0;
- dl_list_for_each_safe(txsa, pre_txsa,
- &participant->txsc->sa_list,
- struct transmit_sa, list) {
- secy_disable_transmit_sa(kay, txsa);
- ieee802_1x_kay_deinit_transmit_sa(txsa);
- }
- ieee802_1x_cp_connect_authenticated(kay->cp);
- ieee802_1x_cp_sm_step(kay->cp);
- } else {
- ieee802_1x_kay_elect_key_server(participant);
- ieee802_1x_kay_decide_macsec_use(participant);
- }
- }
- dl_list_for_each_safe(peer, pre_peer, &participant->potential_peers,
- struct ieee802_1x_kay_peer, list) {
- if (now > peer->expire) {
- wpa_printf(MSG_DEBUG, "KaY: Potential peer removed");
- wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
- sizeof(peer->mi));
- wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
- dl_list_del(&peer->list);
- os_free(peer);
- }
- }
- if (participant->new_sak) {
- if (!ieee802_1x_kay_generate_new_sak(participant))
- participant->to_dist_sak = TRUE;
- participant->new_sak = FALSE;
- }
- if (participant->retry_count < MAX_RETRY_CNT) {
- ieee802_1x_participant_send_mkpdu(participant);
- participant->retry_count++;
- }
- eloop_register_timeout(MKA_HELLO_TIME / 1000, 0,
- ieee802_1x_participant_timer,
- participant, NULL);
- }
- /**
- * ieee802_1x_kay_init_transmit_sa -
- */
- static struct transmit_sa *
- ieee802_1x_kay_init_transmit_sa(struct transmit_sc *psc, u8 an, u32 next_PN,
- struct data_key *key)
- {
- struct transmit_sa *psa;
- key->tx_latest = TRUE;
- key->rx_latest = TRUE;
- psa = os_zalloc(sizeof(*psa));
- if (!psa) {
- wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
- return NULL;
- }
- if (key->confidentiality_offset >= CONFIDENTIALITY_OFFSET_0 &&
- key->confidentiality_offset <= CONFIDENTIALITY_OFFSET_50)
- psa->confidentiality = TRUE;
- else
- psa->confidentiality = FALSE;
- psa->an = an;
- psa->pkey = key;
- psa->next_pn = next_PN;
- psa->sc = psc;
- os_get_time(&psa->created_time);
- psa->in_use = FALSE;
- dl_list_add(&psc->sa_list, &psa->list);
- wpa_printf(MSG_DEBUG,
- "KaY: Create transmit SA(an: %d, next_PN: %u) of SC(channel: %d)",
- (int) an, next_PN, psc->channel);
- return psa;
- }
- /**
- * ieee802_1x_kay_deinit_transmit_sa -
- */
- static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa)
- {
- psa->pkey = NULL;
- wpa_printf(MSG_DEBUG,
- "KaY: Delete transmit SA(an: %d) of SC(channel: %d)",
- psa->an, psa->sc->channel);
- dl_list_del(&psa->list);
- os_free(psa);
- }
- /**
- * init_transmit_sc -
- */
- static struct transmit_sc *
- ieee802_1x_kay_init_transmit_sc(const struct ieee802_1x_mka_sci *sci,
- int channel)
- {
- struct transmit_sc *psc;
- psc = os_zalloc(sizeof(*psc));
- if (!psc) {
- wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
- return NULL;
- }
- os_memcpy(&psc->sci, sci, sizeof(psc->sci));
- psc->channel = channel;
- os_get_time(&psc->created_time);
- psc->transmitting = FALSE;
- psc->encoding_sa = FALSE;
- psc->enciphering_sa = FALSE;
- dl_list_init(&psc->sa_list);
- wpa_printf(MSG_DEBUG, "KaY: Create transmit SC(channel: %d)", channel);
- wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)sci , sizeof(*sci));
- return psc;
- }
- /**
- * ieee802_1x_kay_deinit_transmit_sc -
- */
- static void
- ieee802_1x_kay_deinit_transmit_sc(
- struct ieee802_1x_mka_participant *participant, struct transmit_sc *psc)
- {
- struct transmit_sa *psa, *tmp;
- wpa_printf(MSG_DEBUG, "KaY: Delete transmit SC(channel: %d)",
- psc->channel);
- dl_list_for_each_safe(psa, tmp, &psc->sa_list, struct transmit_sa,
- list) {
- secy_disable_transmit_sa(participant->kay, psa);
- ieee802_1x_kay_deinit_transmit_sa(psa);
- }
- os_free(psc);
- }
- /****************** Interface between CP and KAY *********************/
- /**
- * ieee802_1x_kay_set_latest_sa_attr -
- */
- int ieee802_1x_kay_set_latest_sa_attr(struct ieee802_1x_kay *kay,
- struct ieee802_1x_mka_ki *lki, u8 lan,
- Boolean ltx, Boolean lrx)
- {
- struct ieee802_1x_mka_participant *principal;
- principal = ieee802_1x_kay_get_principal_participant(kay);
- if (!principal)
- return -1;
- if (!lki)
- os_memset(&principal->lki, 0, sizeof(principal->lki));
- else
- os_memcpy(&principal->lki, lki, sizeof(principal->lki));
- principal->lan = lan;
- principal->ltx = ltx;
- principal->lrx = lrx;
- if (!lki) {
- kay->ltx_kn = 0;
- kay->lrx_kn = 0;
- } else {
- kay->ltx_kn = lki->kn;
- kay->lrx_kn = lki->kn;
- }
- kay->ltx_an = lan;
- kay->lrx_an = lan;
- return 0;
- }
- /**
- * ieee802_1x_kay_set_old_sa_attr -
- */
- int ieee802_1x_kay_set_old_sa_attr(struct ieee802_1x_kay *kay,
- struct ieee802_1x_mka_ki *oki,
- u8 oan, Boolean otx, Boolean orx)
- {
- struct ieee802_1x_mka_participant *principal;
- principal = ieee802_1x_kay_get_principal_participant(kay);
- if (!principal)
- return -1;
- if (!oki)
- os_memset(&principal->oki, 0, sizeof(principal->oki));
- else
- os_memcpy(&principal->oki, oki, sizeof(principal->oki));
- principal->oan = oan;
- principal->otx = otx;
- principal->orx = orx;
- if (!oki) {
- kay->otx_kn = 0;
- kay->orx_kn = 0;
- } else {
- kay->otx_kn = oki->kn;
- kay->orx_kn = oki->kn;
- }
- kay->otx_an = oan;
- kay->orx_an = oan;
- return 0;
- }
- /**
- * ieee802_1x_kay_create_sas -
- */
- int ieee802_1x_kay_create_sas(struct ieee802_1x_kay *kay,
- struct ieee802_1x_mka_ki *lki)
- {
- struct data_key *sa_key, *latest_sak;
- struct ieee802_1x_mka_participant *principal;
- struct receive_sc *rxsc;
- struct receive_sa *rxsa;
- struct transmit_sa *txsa;
- principal = ieee802_1x_kay_get_principal_participant(kay);
- if (!principal)
- return -1;
- latest_sak = NULL;
- dl_list_for_each(sa_key, &principal->sak_list, struct data_key, list) {
- if (is_ki_equal(&sa_key->key_identifier, lki)) {
- sa_key->rx_latest = TRUE;
- sa_key->tx_latest = TRUE;
- latest_sak = sa_key;
- principal->to_use_sak = TRUE;
- } else {
- sa_key->rx_latest = FALSE;
- sa_key->tx_latest = FALSE;
- }
- }
- if (!latest_sak) {
- wpa_printf(MSG_ERROR, "lki related sak not found");
- return -1;
- }
- dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
- rxsa = ieee802_1x_kay_init_receive_sa(rxsc, latest_sak->an, 1,
- latest_sak);
- if (!rxsa)
- return -1;
- secy_create_receive_sa(kay, rxsa);
- }
- txsa = ieee802_1x_kay_init_transmit_sa(principal->txsc, latest_sak->an,
- 1, latest_sak);
- if (!txsa)
- return -1;
- secy_create_transmit_sa(kay, txsa);
- return 0;
- }
- /**
- * ieee802_1x_kay_delete_sas -
- */
- int ieee802_1x_kay_delete_sas(struct ieee802_1x_kay *kay,
- struct ieee802_1x_mka_ki *ki)
- {
- struct data_key *sa_key, *pre_key;
- struct transmit_sa *txsa, *pre_txsa;
- struct receive_sa *rxsa, *pre_rxsa;
- struct receive_sc *rxsc;
- struct ieee802_1x_mka_participant *principal;
- wpa_printf(MSG_DEBUG, "KaY: Entry into %s", __func__);
- principal = ieee802_1x_kay_get_principal_participant(kay);
- if (!principal)
- return -1;
- /* remove the transmit sa */
- dl_list_for_each_safe(txsa, pre_txsa, &principal->txsc->sa_list,
- struct transmit_sa, list) {
- if (is_ki_equal(&txsa->pkey->key_identifier, ki)) {
- secy_disable_transmit_sa(kay, txsa);
- ieee802_1x_kay_deinit_transmit_sa(txsa);
- }
- }
- /* remove the receive sa */
- dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
- dl_list_for_each_safe(rxsa, pre_rxsa, &rxsc->sa_list,
- struct receive_sa, list) {
- if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
- secy_disable_receive_sa(kay, rxsa);
- ieee802_1x_kay_deinit_receive_sa(rxsa);
- }
- }
- }
- /* remove the sak */
- dl_list_for_each_safe(sa_key, pre_key, &principal->sak_list,
- struct data_key, list) {
- if (is_ki_equal(&sa_key->key_identifier, ki)) {
- ieee802_1x_kay_deinit_data_key(sa_key);
- break;
- }
- if (principal->new_key == sa_key)
- principal->new_key = NULL;
- }
- return 0;
- }
- /**
- * ieee802_1x_kay_enable_tx_sas -
- */
- int ieee802_1x_kay_enable_tx_sas(struct ieee802_1x_kay *kay,
- struct ieee802_1x_mka_ki *lki)
- {
- struct ieee802_1x_mka_participant *principal;
- struct transmit_sa *txsa;
- principal = ieee802_1x_kay_get_principal_participant(kay);
- if (!principal)
- return -1;
- dl_list_for_each(txsa, &principal->txsc->sa_list, struct transmit_sa,
- list) {
- if (is_ki_equal(&txsa->pkey->key_identifier, lki)) {
- txsa->in_use = TRUE;
- secy_enable_transmit_sa(kay, txsa);
- ieee802_1x_cp_set_usingtransmitas(
- principal->kay->cp, TRUE);
- ieee802_1x_cp_sm_step(principal->kay->cp);
- }
- }
- return 0;
- }
- /**
- * ieee802_1x_kay_enable_rx_sas -
- */
- int ieee802_1x_kay_enable_rx_sas(struct ieee802_1x_kay *kay,
- struct ieee802_1x_mka_ki *lki)
- {
- struct ieee802_1x_mka_participant *principal;
- struct receive_sa *rxsa;
- struct receive_sc *rxsc;
- principal = ieee802_1x_kay_get_principal_participant(kay);
- if (!principal)
- return -1;
- dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
- dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
- {
- if (is_ki_equal(&rxsa->pkey->key_identifier, lki)) {
- rxsa->in_use = TRUE;
- secy_enable_receive_sa(kay, rxsa);
- ieee802_1x_cp_set_usingreceivesas(
- principal->kay->cp, TRUE);
- ieee802_1x_cp_sm_step(principal->kay->cp);
- }
- }
- }
- return 0;
- }
- /**
- * ieee802_1x_kay_enable_new_info -
- */
- int ieee802_1x_kay_enable_new_info(struct ieee802_1x_kay *kay)
- {
- struct ieee802_1x_mka_participant *principal;
- principal = ieee802_1x_kay_get_principal_participant(kay);
- if (!principal)
- return -1;
- if (principal->retry_count < MAX_RETRY_CNT) {
- ieee802_1x_participant_send_mkpdu(principal);
- principal->retry_count++;
- }
- return 0;
- }
- /**
- * ieee802_1x_kay_cp_conf -
- */
- int ieee802_1x_kay_cp_conf(struct ieee802_1x_kay *kay,
- struct ieee802_1x_cp_conf *pconf)
- {
- pconf->protect = kay->macsec_protect;
- pconf->replay_protect = kay->macsec_replay_protect;
- pconf->validate = kay->macsec_validate;
- return 0;
- }
- /**
- * ieee802_1x_kay_alloc_cp_sm -
- */
- static struct ieee802_1x_cp_sm *
- ieee802_1x_kay_alloc_cp_sm(struct ieee802_1x_kay *kay)
- {
- struct ieee802_1x_cp_conf conf;
- os_memset(&conf, 0, sizeof(conf));
- conf.protect = kay->macsec_protect;
- conf.replay_protect = kay->macsec_replay_protect;
- conf.validate = kay->macsec_validate;
- conf.replay_window = kay->macsec_replay_window;
- return ieee802_1x_cp_sm_init(kay, &conf);
- }
- /**
- * ieee802_1x_kay_mkpdu_sanity_check -
- * sanity check specified in clause 11.11.2 of IEEE802.1X-2010
- */
- static int ieee802_1x_kay_mkpdu_sanity_check(struct ieee802_1x_kay *kay,
- const u8 *buf, size_t len)
- {
- struct ieee8023_hdr *eth_hdr;
- struct ieee802_1x_hdr *eapol_hdr;
- struct ieee802_1x_mka_hdr *mka_hdr;
- struct ieee802_1x_mka_basic_body *body;
- size_t mka_msg_len;
- struct ieee802_1x_mka_participant *participant;
- size_t body_len;
- u8 icv[MAX_ICV_LEN];
- u8 *msg_icv;
- eth_hdr = (struct ieee8023_hdr *) buf;
- eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
- mka_hdr = (struct ieee802_1x_mka_hdr *) (eapol_hdr + 1);
- /* destination address should be not individual address */
- if (os_memcmp(eth_hdr->dest, pae_group_addr, ETH_ALEN) != 0) {
- wpa_printf(MSG_MSGDUMP,
- "KaY: ethernet destination address is not PAE group address");
- return -1;
- }
- /* MKPDU should not less than 32 octets */
- mka_msg_len = be_to_host16(eapol_hdr->length);
- if (mka_msg_len < 32) {
- wpa_printf(MSG_MSGDUMP, "KaY: MKPDU is less than 32 octets");
- return -1;
- }
- /* MKPDU should multiple 4 octets */
- if ((mka_msg_len % 4) != 0) {
- wpa_printf(MSG_MSGDUMP,
- "KaY: MKPDU is not multiple of 4 octets");
- return -1;
- }
- body = (struct ieee802_1x_mka_basic_body *) mka_hdr;
- ieee802_1x_mka_dump_basic_body(body);
- body_len = get_mka_param_body_len(body);
- /* EAPOL-MKA body should comprise basic parameter set and ICV */
- if (mka_msg_len < MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN) {
- wpa_printf(MSG_ERROR,
- "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",
- (int) mka_msg_len, (int) MKA_HDR_LEN,
- (int) body_len, DEFAULT_ICV_LEN);
- return -1;
- }
- /* CKN should be owned by I */
- participant = ieee802_1x_kay_get_participant(kay, body->ckn);
- if (!participant) {
- wpa_printf(MSG_DEBUG, "CKN is not included in my CA");
- return -1;
- }
- /* algorithm agility check */
- if (os_memcmp(body->algo_agility, mka_algo_agility,
- sizeof(body->algo_agility)) != 0) {
- wpa_printf(MSG_ERROR,
- "KaY: peer's algorithm agility not supported for me");
- return -1;
- }
- /* ICV check */
- /*
- * The ICV will comprise the final octets of the packet body, whatever
- * its size, not the fixed length 16 octets, indicated by the EAPOL
- * packet body length.
- */
- if (mka_alg_tbl[kay->mka_algindex].icv_hash(
- participant->ick.key,
- buf, len - mka_alg_tbl[kay->mka_algindex].icv_len, icv)) {
- wpa_printf(MSG_ERROR, "KaY: omac1_aes_128 failed");
- return -1;
- }
- msg_icv = ieee802_1x_mka_decode_icv_body(participant, (u8 *) mka_hdr,
- mka_msg_len);
- if (msg_icv) {
- if (os_memcmp_const(msg_icv, icv,
- mka_alg_tbl[kay->mka_algindex].icv_len) !=
- 0) {
- wpa_printf(MSG_ERROR,
- "KaY: Computed ICV is not equal to Received ICV");
- return -1;
- }
- } else {
- wpa_printf(MSG_ERROR, "KaY: No ICV");
- return -1;
- }
- return 0;
- }
- /**
- * ieee802_1x_kay_decode_mkpdu -
- */
- static int ieee802_1x_kay_decode_mkpdu(struct ieee802_1x_kay *kay,
- const u8 *buf, size_t len)
- {
- struct ieee802_1x_mka_participant *participant;
- struct ieee802_1x_mka_hdr *hdr;
- size_t body_len;
- size_t left_len;
- u8 body_type;
- int i;
- const u8 *pos;
- Boolean my_included;
- Boolean handled[256];
- if (ieee802_1x_kay_mkpdu_sanity_check(kay, buf, len))
- return -1;
- /* handle basic parameter set */
- pos = buf + sizeof(struct ieee8023_hdr) + sizeof(struct ieee802_1x_hdr);
- left_len = len - sizeof(struct ieee8023_hdr) -
- sizeof(struct ieee802_1x_hdr);
- participant = ieee802_1x_mka_decode_basic_body(kay, pos, left_len);
- if (!participant)
- return -1;
- /* to skip basic parameter set */
- hdr = (struct ieee802_1x_mka_hdr *) pos;
- body_len = get_mka_param_body_len(hdr);
- pos += body_len + MKA_HDR_LEN;
- left_len -= body_len + MKA_HDR_LEN;
- /* check i am in the peer's peer list */
- my_included = ieee802_1x_mka_i_in_peerlist(participant, pos, left_len);
- if (my_included) {
- /* accept the peer as live peer */
- if (!ieee802_1x_kay_is_in_peer(
- participant,
- participant->current_peer_id.mi)) {
- if (!ieee802_1x_kay_create_live_peer(
- participant,
- participant->current_peer_id.mi,
- be_to_host32(
- participant->current_peer_id.mn)))
- return -1;
- ieee802_1x_kay_elect_key_server(participant);
- ieee802_1x_kay_decide_macsec_use(participant);
- }
- if (ieee802_1x_kay_is_in_potential_peer(
- participant, participant->current_peer_id.mi)) {
- if (!ieee802_1x_kay_move_live_peer(
- participant,
- participant->current_peer_id.mi,
- be_to_host32(participant->
- current_peer_id.mn)))
- return -1;
- ieee802_1x_kay_elect_key_server(participant);
- ieee802_1x_kay_decide_macsec_use(participant);
- }
- }
- /*
- * Handle other parameter set than basic parameter set.
- * Each parameter set should be present only once.
- */
- for (i = 0; i < 256; i++)
- handled[i] = FALSE;
- handled[0] = TRUE;
- while (left_len > MKA_HDR_LEN + DEFAULT_ICV_LEN) {
- hdr = (struct ieee802_1x_mka_hdr *) pos;
- body_len = get_mka_param_body_len(hdr);
- body_type = get_mka_param_body_type(hdr);
- if (body_type == MKA_ICV_INDICATOR)
- return 0;
- if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
- wpa_printf(MSG_ERROR,
- "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",
- (int) left_len, (int) MKA_HDR_LEN,
- (int) body_len, DEFAULT_ICV_LEN);
- goto next_para_set;
- }
- if (handled[body_type])
- goto next_para_set;
- handled[body_type] = TRUE;
- if (body_type < ARRAY_SIZE(mka_body_handler) &&
- mka_body_handler[body_type].body_rx) {
- mka_body_handler[body_type].body_rx
- (participant, pos, left_len);
- } else {
- wpa_printf(MSG_ERROR,
- "The type %d not supported in this MKA version %d",
- body_type, MKA_VERSION_ID);
- }
- next_para_set:
- pos += body_len + MKA_HDR_LEN;
- left_len -= body_len + MKA_HDR_LEN;
- }
- kay->active = TRUE;
- participant->retry_count = 0;
- participant->active = TRUE;
- return 0;
- }
- static void kay_l2_receive(void *ctx, const u8 *src_addr, const u8 *buf,
- size_t len)
- {
- struct ieee802_1x_kay *kay = ctx;
- struct ieee8023_hdr *eth_hdr;
- struct ieee802_1x_hdr *eapol_hdr;
- /* must contain at least ieee8023_hdr + ieee802_1x_hdr */
- if (len < sizeof(*eth_hdr) + sizeof(*eapol_hdr)) {
- wpa_printf(MSG_MSGDUMP, "KaY: EAPOL frame too short (%lu)",
- (unsigned long) len);
- return;
- }
- eth_hdr = (struct ieee8023_hdr *) buf;
- eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
- if (len != sizeof(*eth_hdr) + sizeof(*eapol_hdr) +
- be_to_host16(eapol_hdr->length)) {
- wpa_printf(MSG_MSGDUMP, "KAY: EAPOL MPDU is invalid: (%lu-%lu)",
- (unsigned long) len,
- (unsigned long) be_to_host16(eapol_hdr->length));
- return;
- }
- if (eapol_hdr->version < EAPOL_VERSION) {
- wpa_printf(MSG_MSGDUMP, "KaY: version %d does not support MKA",
- eapol_hdr->version);
- return;
- }
- if (be_to_host16(eth_hdr->ethertype) != ETH_P_PAE ||
- eapol_hdr->type != IEEE802_1X_TYPE_EAPOL_MKA)
- return;
- wpa_hexdump(MSG_DEBUG, "RX EAPOL-MKA: ", buf, len);
- if (dl_list_empty(&kay->participant_list)) {
- wpa_printf(MSG_ERROR, "KaY: no MKA participant instance");
- return;
- }
- ieee802_1x_kay_decode_mkpdu(kay, buf, len);
- }
- /**
- * ieee802_1x_kay_init -
- */
- struct ieee802_1x_kay *
- ieee802_1x_kay_init(struct ieee802_1x_kay_ctx *ctx, enum macsec_policy policy,
- const char *ifname, const u8 *addr)
- {
- struct ieee802_1x_kay *kay;
- kay = os_zalloc(sizeof(*kay));
- if (!kay) {
- wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
- return NULL;
- }
- kay->ctx = ctx;
- kay->enable = TRUE;
- kay->active = FALSE;
- kay->authenticated = FALSE;
- kay->secured = FALSE;
- kay->failed = FALSE;
- kay->policy = policy;
- os_strlcpy(kay->if_name, ifname, IFNAMSIZ);
- os_memcpy(kay->actor_sci.addr, addr, ETH_ALEN);
- kay->actor_sci.port = host_to_be16(0x0001);
- kay->actor_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
- /* While actor acts as a key server, shall distribute sakey */
- kay->dist_kn = 1;
- kay->dist_an = 0;
- kay->dist_time = 0;
- kay->pn_exhaustion = PENDING_PN_EXHAUSTION;
- kay->macsec_csindex = DEFAULT_CS_INDEX;
- kay->mka_algindex = DEFAULT_MKA_ALG_INDEX;
- kay->mka_version = MKA_VERSION_ID;
- os_memcpy(kay->algo_agility, mka_algo_agility,
- sizeof(kay->algo_agility));
- dl_list_init(&kay->participant_list);
- if (policy == DO_NOT_SECURE) {
- kay->macsec_capable = MACSEC_CAP_NOT_IMPLEMENTED;
- kay->macsec_desired = FALSE;
- kay->macsec_protect = FALSE;
- kay->macsec_validate = Disabled;
- kay->macsec_replay_protect = FALSE;
- kay->macsec_replay_window = 0;
- kay->macsec_confidentiality = CONFIDENTIALITY_NONE;
- } else {
- kay->macsec_capable = MACSEC_CAP_INTEG_AND_CONF_0_30_50;
- kay->macsec_desired = TRUE;
- kay->macsec_protect = TRUE;
- kay->macsec_validate = Strict;
- kay->macsec_replay_protect = FALSE;
- kay->macsec_replay_window = 0;
- kay->macsec_confidentiality = CONFIDENTIALITY_OFFSET_0;
- }
- wpa_printf(MSG_DEBUG, "KaY: state machine created");
- /* Initialize the SecY must be prio to CP, as CP will control SecY */
- secy_init_macsec(kay);
- secy_get_available_transmit_sc(kay, &kay->sc_ch);
- wpa_printf(MSG_DEBUG, "KaY: secy init macsec done");
- /* init CP */
- kay->cp = ieee802_1x_kay_alloc_cp_sm(kay);
- if (kay->cp == NULL) {
- ieee802_1x_kay_deinit(kay);
- return NULL;
- }
- if (policy == DO_NOT_SECURE) {
- ieee802_1x_cp_connect_authenticated(kay->cp);
- ieee802_1x_cp_sm_step(kay->cp);
- } else {
- kay->l2_mka = l2_packet_init(kay->if_name, NULL, ETH_P_PAE,
- kay_l2_receive, kay, 1);
- if (kay->l2_mka == NULL) {
- wpa_printf(MSG_WARNING,
- "KaY: Failed to initialize L2 packet processing for MKA packet");
- ieee802_1x_kay_deinit(kay);
- return NULL;
- }
- }
- return kay;
- }
- /**
- * ieee802_1x_kay_deinit -
- */
- void
- ieee802_1x_kay_deinit(struct ieee802_1x_kay *kay)
- {
- struct ieee802_1x_mka_participant *participant;
- if (!kay)
- return;
- wpa_printf(MSG_DEBUG, "KaY: state machine removed");
- while (!dl_list_empty(&kay->participant_list)) {
- participant = dl_list_entry(kay->participant_list.next,
- struct ieee802_1x_mka_participant,
- list);
- ieee802_1x_kay_delete_mka(kay, &participant->ckn);
- }
- ieee802_1x_cp_sm_deinit(kay->cp);
- secy_deinit_macsec(kay);
- if (kay->l2_mka) {
- l2_packet_deinit(kay->l2_mka);
- kay->l2_mka = NULL;
- }
- os_free(kay->ctx);
- os_free(kay);
- }
- /**
- * ieee802_1x_kay_create_mka -
- */
- struct ieee802_1x_mka_participant *
- ieee802_1x_kay_create_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn,
- struct mka_key *cak, u32 life,
- enum mka_created_mode mode, Boolean is_authenticator)
- {
- struct ieee802_1x_mka_participant *participant;
- unsigned int usecs;
- if (!kay || !ckn || !cak) {
- wpa_printf(MSG_ERROR, "KaY: ckn or cak is null");
- return NULL;
- }
- if (cak->len != mka_alg_tbl[kay->mka_algindex].cak_len) {
- wpa_printf(MSG_ERROR, "KaY: CAK length not follow key schema");
- return NULL;
- }
- if (ckn->len > MAX_CKN_LEN) {
- wpa_printf(MSG_ERROR, "KaY: CKN is out of range(<=32 bytes)");
- return NULL;
- }
- if (!kay->enable) {
- wpa_printf(MSG_ERROR, "KaY: Now is at disable state");
- return NULL;
- }
- participant = os_zalloc(sizeof(*participant));
- if (!participant) {
- wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
- return NULL;
- }
- participant->ckn.len = ckn->len;
- os_memcpy(participant->ckn.name, ckn->name, ckn->len);
- participant->cak.len = cak->len;
- os_memcpy(participant->cak.key, cak->key, cak->len);
- if (life)
- participant->cak_life = life + time(NULL);
- switch (mode) {
- case EAP_EXCHANGE:
- if (is_authenticator) {
- participant->is_obliged_key_server = TRUE;
- participant->can_be_key_server = TRUE;
- participant->is_key_server = TRUE;
- participant->principal = TRUE;
- os_memcpy(&kay->key_server_sci, &kay->actor_sci,
- sizeof(kay->key_server_sci));
- kay->key_server_priority = kay->actor_priority;
- participant->is_elected = TRUE;
- } else {
- participant->is_obliged_key_server = FALSE;
- participant->can_be_key_server = FALSE;
- participant->is_key_server = FALSE;
- participant->is_elected = TRUE;
- }
- break;
- default:
- participant->is_obliged_key_server = FALSE;
- participant->can_be_key_server = TRUE;
- participant->is_key_server = TRUE;
- participant->is_elected = FALSE;
- break;
- }
- participant->cached = FALSE;
- participant->active = FALSE;
- participant->participant = FALSE;
- participant->retain = FALSE;
- participant->activate = DEFAULT;
- if (participant->is_key_server)
- participant->principal = TRUE;
- dl_list_init(&participant->live_peers);
- dl_list_init(&participant->potential_peers);
- participant->retry_count = 0;
- participant->kay = kay;
- if (os_get_random(participant->mi, sizeof(participant->mi)) < 0)
- goto fail;
- participant->mn = 0;
- participant->lrx = FALSE;
- participant->ltx = FALSE;
- participant->orx = FALSE;
- participant->otx = FALSE;
- participant->to_dist_sak = FALSE;
- participant->to_use_sak = FALSE;
- participant->new_sak = FALSE;
- dl_list_init(&participant->sak_list);
- participant->new_key = NULL;
- dl_list_init(&participant->rxsc_list);
- participant->txsc = ieee802_1x_kay_init_transmit_sc(&kay->actor_sci,
- kay->sc_ch);
- secy_cp_control_protect_frames(kay, kay->macsec_protect);
- secy_cp_control_replay(kay, kay->macsec_replay_protect,
- kay->macsec_replay_window);
- secy_create_transmit_sc(kay, participant->txsc);
- /* to derive KEK from CAK and CKN */
- participant->kek.len = mka_alg_tbl[kay->mka_algindex].kek_len;
- if (mka_alg_tbl[kay->mka_algindex].kek_trfm(participant->cak.key,
- participant->ckn.name,
- participant->ckn.len,
- participant->kek.key)) {
- wpa_printf(MSG_ERROR, "KaY: Derived KEK failed");
- goto fail;
- }
- wpa_hexdump_key(MSG_DEBUG, "KaY: Derived KEK",
- participant->kek.key, participant->kek.len);
- /* to derive ICK from CAK and CKN */
- participant->ick.len = mka_alg_tbl[kay->mka_algindex].ick_len;
- if (mka_alg_tbl[kay->mka_algindex].ick_trfm(participant->cak.key,
- participant->ckn.name,
- participant->ckn.len,
- participant->ick.key)) {
- wpa_printf(MSG_ERROR, "KaY: Derived ICK failed");
- goto fail;
- }
- wpa_hexdump_key(MSG_DEBUG, "KaY: Derived ICK",
- participant->ick.key, participant->ick.len);
- dl_list_add(&kay->participant_list, &participant->list);
- wpa_hexdump(MSG_DEBUG, "KaY: Participant created:",
- ckn->name, ckn->len);
- usecs = os_random() % (MKA_HELLO_TIME * 1000);
- eloop_register_timeout(0, usecs, ieee802_1x_participant_timer,
- participant, NULL);
- participant->mka_life = MKA_LIFE_TIME / 1000 + time(NULL) +
- usecs / 1000000;
- return participant;
- fail:
- os_free(participant);
- return NULL;
- }
- /**
- * ieee802_1x_kay_delete_mka -
- */
- void
- ieee802_1x_kay_delete_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn)
- {
- struct ieee802_1x_mka_participant *participant;
- struct ieee802_1x_kay_peer *peer;
- struct data_key *sak;
- struct receive_sc *rxsc;
- if (!kay || !ckn)
- return;
- wpa_printf(MSG_DEBUG, "KaY: participant removed");
- /* get the participant */
- participant = ieee802_1x_kay_get_participant(kay, ckn->name);
- if (!participant) {
- wpa_hexdump(MSG_DEBUG, "KaY: participant is not found",
- ckn->name, ckn->len);
- return;
- }
- eloop_cancel_timeout(ieee802_1x_participant_timer, participant, NULL);
- dl_list_del(&participant->list);
- /* remove live peer */
- while (!dl_list_empty(&participant->live_peers)) {
- peer = dl_list_entry(participant->live_peers.next,
- struct ieee802_1x_kay_peer, list);
- dl_list_del(&peer->list);
- os_free(peer);
- }
- /* remove potential peer */
- while (!dl_list_empty(&participant->potential_peers)) {
- peer = dl_list_entry(participant->potential_peers.next,
- struct ieee802_1x_kay_peer, list);
- dl_list_del(&peer->list);
- os_free(peer);
- }
- /* remove sak */
- while (!dl_list_empty(&participant->sak_list)) {
- sak = dl_list_entry(participant->sak_list.next,
- struct data_key, list);
- dl_list_del(&sak->list);
- os_free(sak->key);
- os_free(sak);
- }
- while (!dl_list_empty(&participant->rxsc_list)) {
- rxsc = dl_list_entry(participant->rxsc_list.next,
- struct receive_sc, list);
- secy_delete_receive_sc(kay, rxsc);
- ieee802_1x_kay_deinit_receive_sc(participant, rxsc);
- }
- secy_delete_transmit_sc(kay, participant->txsc);
- ieee802_1x_kay_deinit_transmit_sc(participant, participant->txsc);
- os_memset(&participant->cak, 0, sizeof(participant->cak));
- os_memset(&participant->kek, 0, sizeof(participant->kek));
- os_memset(&participant->ick, 0, sizeof(participant->ick));
- os_free(participant);
- }
- /**
- * ieee802_1x_kay_mka_participate -
- */
- void ieee802_1x_kay_mka_participate(struct ieee802_1x_kay *kay,
- struct mka_key_name *ckn,
- Boolean status)
- {
- struct ieee802_1x_mka_participant *participant;
- if (!kay || !ckn)
- return;
- participant = ieee802_1x_kay_get_participant(kay, ckn->name);
- if (!participant)
- return;
- participant->active = status;
- }
- /**
- * ieee802_1x_kay_new_sak -
- */
- int
- ieee802_1x_kay_new_sak(struct ieee802_1x_kay *kay)
- {
- struct ieee802_1x_mka_participant *participant;
- if (!kay)
- return -1;
- participant = ieee802_1x_kay_get_principal_participant(kay);
- if (!participant)
- return -1;
- participant->new_sak = TRUE;
- wpa_printf(MSG_DEBUG, "KaY: new SAK signal");
- return 0;
- }
- /**
- * ieee802_1x_kay_change_cipher_suite -
- */
- int
- ieee802_1x_kay_change_cipher_suite(struct ieee802_1x_kay *kay, int cs_index)
- {
- struct ieee802_1x_mka_participant *participant;
- if (!kay)
- return -1;
- if ((unsigned int) cs_index >= CS_TABLE_SIZE) {
- wpa_printf(MSG_ERROR,
- "KaY: Configured cipher suite index is out of range");
- return -1;
- }
- if (kay->macsec_csindex == cs_index)
- return -2;
- if (cs_index == 0)
- kay->macsec_desired = FALSE;
- kay->macsec_csindex = cs_index;
- kay->macsec_capable = cipher_suite_tbl[kay->macsec_csindex].capable;
- participant = ieee802_1x_kay_get_principal_participant(kay);
- if (participant) {
- wpa_printf(MSG_INFO, "KaY: Cipher Suite changed");
- participant->new_sak = TRUE;
- }
- return 0;
- }
|