test_eap_proto.py 167 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011
  1. # EAP protocol tests
  2. # Copyright (c) 2014-2015, Jouni Malinen <j@w1.fi>
  3. #
  4. # This software may be distributed under the terms of the BSD license.
  5. # See README for more details.
  6. import hmac
  7. import logging
  8. logger = logging.getLogger()
  9. import select
  10. import struct
  11. import threading
  12. import time
  13. import hostapd
  14. from utils import HwsimSkip
  15. from test_ap_eap import check_eap_capa
  16. EAP_CODE_REQUEST = 1
  17. EAP_CODE_RESPONSE = 2
  18. EAP_CODE_SUCCESS = 3
  19. EAP_CODE_FAILURE = 4
  20. EAP_TYPE_IDENTITY = 1
  21. EAP_TYPE_NOTIFICATION = 2
  22. EAP_TYPE_NAK = 3
  23. EAP_TYPE_MD5 = 4
  24. EAP_TYPE_OTP = 5
  25. EAP_TYPE_GTC = 6
  26. EAP_TYPE_TLS = 13
  27. EAP_TYPE_LEAP = 17
  28. EAP_TYPE_SIM = 18
  29. EAP_TYPE_TTLS = 21
  30. EAP_TYPE_AKA = 23
  31. EAP_TYPE_PEAP = 25
  32. EAP_TYPE_MSCHAPV2 = 26
  33. EAP_TYPE_TLV = 33
  34. EAP_TYPE_TNC = 38
  35. EAP_TYPE_FAST = 43
  36. EAP_TYPE_PAX = 46
  37. EAP_TYPE_PSK = 47
  38. EAP_TYPE_SAKE = 48
  39. EAP_TYPE_IKEV2 = 49
  40. EAP_TYPE_AKA_PRIME = 50
  41. EAP_TYPE_GPSK = 51
  42. EAP_TYPE_PWD = 52
  43. EAP_TYPE_EKE = 53
  44. def run_pyrad_server(srv, t_stop, eap_handler):
  45. srv.RunWithStop(t_stop, eap_handler)
  46. def start_radius_server(eap_handler):
  47. try:
  48. import pyrad.server
  49. import pyrad.packet
  50. import pyrad.dictionary
  51. except ImportError:
  52. raise HwsimSkip("No pyrad modules available")
  53. class TestServer(pyrad.server.Server):
  54. def _HandleAuthPacket(self, pkt):
  55. pyrad.server.Server._HandleAuthPacket(self, pkt)
  56. if len(pkt[79]) > 1:
  57. logger.info("Multiple EAP-Message attributes")
  58. # TODO: reassemble
  59. eap = pkt[79][0]
  60. eap_req = self.eap_handler(self.ctx, eap)
  61. reply = self.CreateReplyPacket(pkt)
  62. if eap_req:
  63. while True:
  64. if len(eap_req) > 253:
  65. reply.AddAttribute("EAP-Message", eap_req[0:253])
  66. eap_req = eap_req[253:]
  67. else:
  68. reply.AddAttribute("EAP-Message", eap_req)
  69. break
  70. else:
  71. logger.info("No EAP request available")
  72. reply.code = pyrad.packet.AccessChallenge
  73. hmac_obj = hmac.new(reply.secret)
  74. hmac_obj.update(struct.pack("B", reply.code))
  75. hmac_obj.update(struct.pack("B", reply.id))
  76. # reply attributes
  77. reply.AddAttribute("Message-Authenticator",
  78. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
  79. attrs = reply._PktEncodeAttributes()
  80. # Length
  81. flen = 4 + 16 + len(attrs)
  82. hmac_obj.update(struct.pack(">H", flen))
  83. hmac_obj.update(pkt.authenticator)
  84. hmac_obj.update(attrs)
  85. del reply[80]
  86. reply.AddAttribute("Message-Authenticator", hmac_obj.digest())
  87. self.SendReplyPacket(pkt.fd, reply)
  88. def RunWithStop(self, t_stop, eap_handler):
  89. self._poll = select.poll()
  90. self._fdmap = {}
  91. self._PrepareSockets()
  92. self.t_stop = t_stop
  93. self.eap_handler = eap_handler
  94. self.ctx = {}
  95. while not t_stop.is_set():
  96. for (fd, event) in self._poll.poll(1000):
  97. if event == select.POLLIN:
  98. try:
  99. fdo = self._fdmap[fd]
  100. self._ProcessInput(fdo)
  101. except pyrad.server.ServerPacketError as err:
  102. logger.info("pyrad server dropping packet: " + str(err))
  103. except pyrad.packet.PacketError as err:
  104. logger.info("pyrad server received invalid packet: " + str(err))
  105. else:
  106. logger.error("Unexpected event in pyrad server main loop")
  107. srv = TestServer(dict=pyrad.dictionary.Dictionary("dictionary.radius"),
  108. authport=18138, acctport=18139)
  109. srv.hosts["127.0.0.1"] = pyrad.server.RemoteHost("127.0.0.1",
  110. "radius",
  111. "localhost")
  112. srv.BindToAddress("")
  113. t_stop = threading.Event()
  114. t = threading.Thread(target=run_pyrad_server, args=(srv, t_stop, eap_handler))
  115. t.start()
  116. return { 'srv': srv, 'stop': t_stop, 'thread': t }
  117. def stop_radius_server(srv):
  118. srv['stop'].set()
  119. srv['thread'].join()
  120. def start_ap(ifname):
  121. params = hostapd.wpa2_eap_params(ssid="eap-test")
  122. params['auth_server_port'] = "18138"
  123. hapd = hostapd.add_ap(ifname, params)
  124. return hapd
  125. def test_eap_proto(dev, apdev):
  126. """EAP protocol tests"""
  127. check_eap_capa(dev[0], "MD5")
  128. def eap_handler(ctx, req):
  129. logger.info("eap_handler - RX " + req.encode("hex"))
  130. if 'num' not in ctx:
  131. ctx['num'] = 0
  132. ctx['num'] = ctx['num'] + 1
  133. if 'id' not in ctx:
  134. ctx['id'] = 1
  135. ctx['id'] = (ctx['id'] + 1) % 256
  136. idx = 0
  137. idx += 1
  138. if ctx['num'] == idx:
  139. logger.info("Test: MD5 challenge")
  140. return struct.pack(">BBHBBBB", EAP_CODE_REQUEST, ctx['id'],
  141. 4 + 1 + 3,
  142. EAP_TYPE_MD5,
  143. 1, 0xaa, ord('n'))
  144. idx += 1
  145. if ctx['num'] == idx:
  146. logger.info("Test: EAP-Success - id off by 2")
  147. return struct.pack(">BBH", EAP_CODE_SUCCESS, ctx['id'] + 1, 4)
  148. idx += 1
  149. if ctx['num'] == idx:
  150. logger.info("Test: MD5 challenge")
  151. return struct.pack(">BBHBBBB", EAP_CODE_REQUEST, ctx['id'],
  152. 4 + 1 + 3,
  153. EAP_TYPE_MD5,
  154. 1, 0xaa, ord('n'))
  155. idx += 1
  156. if ctx['num'] == idx:
  157. logger.info("Test: EAP-Success - id off by 3")
  158. return struct.pack(">BBH", EAP_CODE_SUCCESS, ctx['id'] + 2, 4)
  159. idx += 1
  160. if ctx['num'] == idx:
  161. logger.info("Test: MD5 challenge")
  162. return struct.pack(">BBHBBBB", EAP_CODE_REQUEST, ctx['id'],
  163. 4 + 1 + 3,
  164. EAP_TYPE_MD5,
  165. 1, 0xaa, ord('n'))
  166. idx += 1
  167. if ctx['num'] == idx:
  168. logger.info("Test: EAP-Notification/Request")
  169. return struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  170. 4 + 1 + 1,
  171. EAP_TYPE_NOTIFICATION,
  172. ord('A'))
  173. idx += 1
  174. if ctx['num'] == idx:
  175. logger.info("Test: EAP-Success")
  176. return struct.pack(">BBH", EAP_CODE_SUCCESS, ctx['id'] - 1, 4)
  177. idx += 1
  178. if ctx['num'] == idx:
  179. logger.info("Test: EAP-Notification/Request")
  180. return struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  181. 4 + 1 + 1,
  182. EAP_TYPE_NOTIFICATION,
  183. ord('B'))
  184. idx += 1
  185. if ctx['num'] == idx:
  186. logger.info("Test: MD5 challenge")
  187. return struct.pack(">BBHBBBB", EAP_CODE_REQUEST, ctx['id'],
  188. 4 + 1 + 3,
  189. EAP_TYPE_MD5,
  190. 1, 0xaa, ord('n'))
  191. idx += 1
  192. if ctx['num'] == idx:
  193. logger.info("Test: EAP-Success")
  194. return struct.pack(">BBH", EAP_CODE_SUCCESS, ctx['id'] - 1, 4)
  195. idx += 1
  196. if ctx['num'] == idx:
  197. logger.info("Test: EAP-Notification/Request")
  198. return struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  199. 4 + 1 + 1,
  200. EAP_TYPE_NOTIFICATION,
  201. ord('C'))
  202. idx += 1
  203. if ctx['num'] == idx:
  204. logger.info("Test: MD5 challenge")
  205. return struct.pack(">BBHBBBB", EAP_CODE_REQUEST, ctx['id'],
  206. 4 + 1 + 3,
  207. EAP_TYPE_MD5,
  208. 1, 0xaa, ord('n'))
  209. idx += 1
  210. if ctx['num'] == idx:
  211. logger.info("Test: EAP-Notification/Request")
  212. return struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  213. 4 + 1 + 1,
  214. EAP_TYPE_NOTIFICATION,
  215. ord('D'))
  216. idx += 1
  217. if ctx['num'] == idx:
  218. logger.info("Test: EAP-Success")
  219. return struct.pack(">BBH", EAP_CODE_SUCCESS, ctx['id'] - 1, 4)
  220. idx += 1
  221. if ctx['num'] == idx:
  222. logger.info("Test: EAP-Notification/Request")
  223. return struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  224. 4 + 1 + 1,
  225. EAP_TYPE_NOTIFICATION,
  226. ord('E'))
  227. idx += 1
  228. if ctx['num'] == idx:
  229. logger.info("Test: EAP-Notification/Request (same id)")
  230. return struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'] - 1,
  231. 4 + 1 + 1,
  232. EAP_TYPE_NOTIFICATION,
  233. ord('F'))
  234. idx += 1
  235. if ctx['num'] == idx:
  236. logger.info("Test: Unexpected EAP-Success")
  237. return struct.pack(">BBH", EAP_CODE_SUCCESS, ctx['id'] - 2, 4)
  238. return None
  239. srv = start_radius_server(eap_handler)
  240. try:
  241. hapd = start_ap(apdev[0]['ifname'])
  242. dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412",
  243. eap="MD5", identity="user", password="password",
  244. wait_connect=False)
  245. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=15)
  246. if ev is None:
  247. raise Exception("Timeout on EAP start")
  248. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
  249. if ev is None:
  250. raise Exception("Timeout on EAP success")
  251. dev[0].request("REMOVE_NETWORK all")
  252. dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412",
  253. eap="MD5", identity="user", password="password",
  254. wait_connect=False)
  255. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=15)
  256. if ev is None:
  257. raise Exception("Timeout on EAP start")
  258. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=1)
  259. if ev is not None:
  260. raise Exception("Unexpected EAP success")
  261. dev[0].request("REMOVE_NETWORK all")
  262. dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412",
  263. eap="MD5", identity="user", password="password",
  264. wait_connect=False)
  265. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=15)
  266. if ev is None:
  267. raise Exception("Timeout on EAP start")
  268. ev = dev[0].wait_event(["CTRL-EVENT-EAP-NOTIFICATION"], timeout=10)
  269. if ev is None:
  270. raise Exception("Timeout on EAP notification")
  271. if ev != "<3>CTRL-EVENT-EAP-NOTIFICATION A":
  272. raise Exception("Unexpected notification contents: " + ev)
  273. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
  274. if ev is None:
  275. raise Exception("Timeout on EAP success")
  276. dev[0].request("REMOVE_NETWORK all")
  277. dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412",
  278. eap="MD5", identity="user", password="password",
  279. wait_connect=False)
  280. ev = dev[0].wait_event(["CTRL-EVENT-EAP-NOTIFICATION"], timeout=10)
  281. if ev is None:
  282. raise Exception("Timeout on EAP notification")
  283. if ev != "<3>CTRL-EVENT-EAP-NOTIFICATION B":
  284. raise Exception("Unexpected notification contents: " + ev)
  285. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=15)
  286. if ev is None:
  287. raise Exception("Timeout on EAP start")
  288. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
  289. if ev is None:
  290. raise Exception("Timeout on EAP success")
  291. dev[0].request("REMOVE_NETWORK all")
  292. dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412",
  293. eap="MD5", identity="user", password="password",
  294. wait_connect=False)
  295. ev = dev[0].wait_event(["CTRL-EVENT-EAP-NOTIFICATION"], timeout=10)
  296. if ev is None:
  297. raise Exception("Timeout on EAP notification")
  298. if ev != "<3>CTRL-EVENT-EAP-NOTIFICATION C":
  299. raise Exception("Unexpected notification contents: " + ev)
  300. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=15)
  301. if ev is None:
  302. raise Exception("Timeout on EAP start")
  303. ev = dev[0].wait_event(["CTRL-EVENT-EAP-NOTIFICATION"], timeout=10)
  304. if ev is None:
  305. raise Exception("Timeout on EAP notification")
  306. if ev != "<3>CTRL-EVENT-EAP-NOTIFICATION D":
  307. raise Exception("Unexpected notification contents: " + ev)
  308. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
  309. if ev is None:
  310. raise Exception("Timeout on EAP success")
  311. dev[0].request("REMOVE_NETWORK all")
  312. dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412",
  313. eap="MD5", identity="user", password="password",
  314. wait_connect=False)
  315. ev = dev[0].wait_event(["CTRL-EVENT-EAP-NOTIFICATION"], timeout=10)
  316. if ev is None:
  317. raise Exception("Timeout on EAP notification")
  318. if ev != "<3>CTRL-EVENT-EAP-NOTIFICATION E":
  319. raise Exception("Unexpected notification contents: " + ev)
  320. ev = dev[0].wait_event(["CTRL-EVENT-EAP-NOTIFICATION"], timeout=10)
  321. if ev is None:
  322. raise Exception("Timeout on EAP notification")
  323. if ev != "<3>CTRL-EVENT-EAP-NOTIFICATION F":
  324. raise Exception("Unexpected notification contents: " + ev)
  325. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=15)
  326. if ev is None:
  327. raise Exception("Timeout on EAP failure")
  328. dev[0].request("REMOVE_NETWORK all")
  329. finally:
  330. stop_radius_server(srv)
  331. EAP_SAKE_VERSION = 2
  332. EAP_SAKE_SUBTYPE_CHALLENGE = 1
  333. EAP_SAKE_SUBTYPE_CONFIRM = 2
  334. EAP_SAKE_SUBTYPE_AUTH_REJECT = 3
  335. EAP_SAKE_SUBTYPE_IDENTITY = 4
  336. EAP_SAKE_AT_RAND_S = 1
  337. EAP_SAKE_AT_RAND_P = 2
  338. EAP_SAKE_AT_MIC_S = 3
  339. EAP_SAKE_AT_MIC_P = 4
  340. EAP_SAKE_AT_SERVERID = 5
  341. EAP_SAKE_AT_PEERID = 6
  342. EAP_SAKE_AT_SPI_S = 7
  343. EAP_SAKE_AT_SPI_P = 8
  344. EAP_SAKE_AT_ANY_ID_REQ = 9
  345. EAP_SAKE_AT_PERM_ID_REQ = 10
  346. EAP_SAKE_AT_ENCR_DATA = 128
  347. EAP_SAKE_AT_IV = 129
  348. EAP_SAKE_AT_PADDING = 130
  349. EAP_SAKE_AT_NEXT_TMPID = 131
  350. EAP_SAKE_AT_MSK_LIFE = 132
  351. def test_eap_proto_sake(dev, apdev):
  352. """EAP-SAKE protocol tests"""
  353. def sake_challenge(ctx):
  354. logger.info("Test: Challenge subtype")
  355. return struct.pack(">BBHBBBBBBLLLL", EAP_CODE_REQUEST, ctx['id'],
  356. 4 + 1 + 3 + 18,
  357. EAP_TYPE_SAKE,
  358. EAP_SAKE_VERSION, 0, EAP_SAKE_SUBTYPE_CHALLENGE,
  359. EAP_SAKE_AT_RAND_S, 18, 0, 0, 0, 0)
  360. def sake_handler(ctx, req):
  361. logger.info("sake_handler - RX " + req.encode("hex"))
  362. if 'num' not in ctx:
  363. ctx['num'] = 0
  364. ctx['num'] = ctx['num'] + 1
  365. if 'id' not in ctx:
  366. ctx['id'] = 1
  367. ctx['id'] = (ctx['id'] + 1) % 256
  368. if ctx['num'] == 1:
  369. logger.info("Test: Missing payload")
  370. return struct.pack(">BBHB", EAP_CODE_REQUEST, ctx['id'], 4 + 1,
  371. EAP_TYPE_SAKE)
  372. if ctx['num'] == 2:
  373. logger.info("Test: Identity subtype without any attributes")
  374. return struct.pack(">BBHBBBB", EAP_CODE_REQUEST, ctx['id'],
  375. 4 + 1 + 3,
  376. EAP_TYPE_SAKE,
  377. EAP_SAKE_VERSION, 0, EAP_SAKE_SUBTYPE_IDENTITY)
  378. if ctx['num'] == 3:
  379. logger.info("Test: Identity subtype")
  380. return struct.pack(">BBHBBBBBBH", EAP_CODE_REQUEST, ctx['id'],
  381. 4 + 1 + 3 + 4,
  382. EAP_TYPE_SAKE,
  383. EAP_SAKE_VERSION, 0, EAP_SAKE_SUBTYPE_IDENTITY,
  384. EAP_SAKE_AT_ANY_ID_REQ, 4, 0)
  385. if ctx['num'] == 4:
  386. logger.info("Test: Identity subtype (different session id)")
  387. return struct.pack(">BBHBBBBBBH", EAP_CODE_REQUEST, ctx['id'],
  388. 4 + 1 + 3 + 4,
  389. EAP_TYPE_SAKE,
  390. EAP_SAKE_VERSION, 1, EAP_SAKE_SUBTYPE_IDENTITY,
  391. EAP_SAKE_AT_PERM_ID_REQ, 4, 0)
  392. if ctx['num'] == 5:
  393. logger.info("Test: Identity subtype with too short attribute")
  394. return struct.pack(">BBHBBBBBB", EAP_CODE_REQUEST, ctx['id'],
  395. 4 + 1 + 3 + 2,
  396. EAP_TYPE_SAKE,
  397. EAP_SAKE_VERSION, 0, EAP_SAKE_SUBTYPE_IDENTITY,
  398. EAP_SAKE_AT_ANY_ID_REQ, 2)
  399. if ctx['num'] == 6:
  400. logger.info("Test: Identity subtype with truncated attribute")
  401. return struct.pack(">BBHBBBBBB", EAP_CODE_REQUEST, ctx['id'],
  402. 4 + 1 + 3 + 2,
  403. EAP_TYPE_SAKE,
  404. EAP_SAKE_VERSION, 0, EAP_SAKE_SUBTYPE_IDENTITY,
  405. EAP_SAKE_AT_ANY_ID_REQ, 4)
  406. if ctx['num'] == 7:
  407. logger.info("Test: Unknown subtype")
  408. return struct.pack(">BBHBBBB", EAP_CODE_REQUEST, ctx['id'],
  409. 4 + 1 + 3,
  410. EAP_TYPE_SAKE,
  411. EAP_SAKE_VERSION, 0, 123)
  412. if ctx['num'] == 8:
  413. logger.info("Test: Challenge subtype without any attributes")
  414. return struct.pack(">BBHBBBB", EAP_CODE_REQUEST, ctx['id'],
  415. 4 + 1 + 3,
  416. EAP_TYPE_SAKE,
  417. EAP_SAKE_VERSION, 0, EAP_SAKE_SUBTYPE_CHALLENGE)
  418. if ctx['num'] == 9:
  419. logger.info("Test: Challenge subtype with too short AT_RAND_S")
  420. return struct.pack(">BBHBBBBBB", EAP_CODE_REQUEST, ctx['id'],
  421. 4 + 1 + 3 + 2,
  422. EAP_TYPE_SAKE,
  423. EAP_SAKE_VERSION, 0, EAP_SAKE_SUBTYPE_CHALLENGE,
  424. EAP_SAKE_AT_RAND_S, 2)
  425. if ctx['num'] == 10:
  426. return sake_challenge(ctx)
  427. if ctx['num'] == 11:
  428. logger.info("Test: Unexpected Identity subtype")
  429. return struct.pack(">BBHBBBBBBH", EAP_CODE_REQUEST, ctx['id'],
  430. 4 + 1 + 3 + 4,
  431. EAP_TYPE_SAKE,
  432. EAP_SAKE_VERSION, 0, EAP_SAKE_SUBTYPE_IDENTITY,
  433. EAP_SAKE_AT_ANY_ID_REQ, 4, 0)
  434. if ctx['num'] == 12:
  435. return sake_challenge(ctx)
  436. if ctx['num'] == 13:
  437. logger.info("Test: Unexpected Challenge subtype")
  438. return struct.pack(">BBHBBBBBBLLLL", EAP_CODE_REQUEST, ctx['id'],
  439. 4 + 1 + 3 + 18,
  440. EAP_TYPE_SAKE,
  441. EAP_SAKE_VERSION, 0, EAP_SAKE_SUBTYPE_CHALLENGE,
  442. EAP_SAKE_AT_RAND_S, 18, 0, 0, 0, 0)
  443. if ctx['num'] == 14:
  444. return sake_challenge(ctx)
  445. if ctx['num'] == 15:
  446. logger.info("Test: Confirm subtype without any attributes")
  447. return struct.pack(">BBHBBBB", EAP_CODE_REQUEST, ctx['id'],
  448. 4 + 1 + 3,
  449. EAP_TYPE_SAKE,
  450. EAP_SAKE_VERSION, 0, EAP_SAKE_SUBTYPE_CONFIRM)
  451. if ctx['num'] == 16:
  452. return sake_challenge(ctx)
  453. if ctx['num'] == 17:
  454. logger.info("Test: Confirm subtype with too short AT_MIC_S")
  455. return struct.pack(">BBHBBBBBB", EAP_CODE_REQUEST, ctx['id'],
  456. 4 + 1 + 3 + 2,
  457. EAP_TYPE_SAKE,
  458. EAP_SAKE_VERSION, 0, EAP_SAKE_SUBTYPE_CONFIRM,
  459. EAP_SAKE_AT_MIC_S, 2)
  460. if ctx['num'] == 18:
  461. logger.info("Test: Unexpected Confirm subtype")
  462. return struct.pack(">BBHBBBBBBLLLL", EAP_CODE_REQUEST, ctx['id'],
  463. 4 + 1 + 3 + 18,
  464. EAP_TYPE_SAKE,
  465. EAP_SAKE_VERSION, 0, EAP_SAKE_SUBTYPE_CONFIRM,
  466. EAP_SAKE_AT_MIC_S, 18, 0, 0, 0, 0)
  467. if ctx['num'] == 19:
  468. return sake_challenge(ctx)
  469. if ctx['num'] == 20:
  470. logger.info("Test: Confirm subtype with incorrect AT_MIC_S")
  471. return struct.pack(">BBHBBBBBBLLLL", EAP_CODE_REQUEST, ctx['id'],
  472. 4 + 1 + 3 + 18,
  473. EAP_TYPE_SAKE,
  474. EAP_SAKE_VERSION, 0, EAP_SAKE_SUBTYPE_CONFIRM,
  475. EAP_SAKE_AT_MIC_S, 18, 0, 0, 0, 0)
  476. return sake_challenge(ctx)
  477. srv = start_radius_server(sake_handler)
  478. try:
  479. hapd = start_ap(apdev[0]['ifname'])
  480. for i in range(0, 14):
  481. dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412",
  482. eap="SAKE", identity="sake user",
  483. password_hex="0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
  484. wait_connect=False)
  485. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=15)
  486. if ev is None:
  487. raise Exception("Timeout on EAP start")
  488. time.sleep(0.1)
  489. dev[0].request("REMOVE_NETWORK all")
  490. logger.info("Too short password")
  491. dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412",
  492. eap="SAKE", identity="sake user",
  493. password_hex="0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcd",
  494. wait_connect=False)
  495. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=15)
  496. if ev is None:
  497. raise Exception("Timeout on EAP start")
  498. time.sleep(0.1)
  499. finally:
  500. stop_radius_server(srv)
  501. def test_eap_proto_leap(dev, apdev):
  502. """EAP-LEAP protocol tests"""
  503. def leap_handler(ctx, req):
  504. logger.info("leap_handler - RX " + req.encode("hex"))
  505. if 'num' not in ctx:
  506. ctx['num'] = 0
  507. ctx['num'] = ctx['num'] + 1
  508. if 'id' not in ctx:
  509. ctx['id'] = 1
  510. ctx['id'] = (ctx['id'] + 1) % 256
  511. if ctx['num'] == 1:
  512. logger.info("Test: Missing payload")
  513. return struct.pack(">BBHB", EAP_CODE_REQUEST, ctx['id'],
  514. 4 + 1,
  515. EAP_TYPE_LEAP)
  516. if ctx['num'] == 2:
  517. logger.info("Test: Unexpected version")
  518. return struct.pack(">BBHBBBB", EAP_CODE_REQUEST, ctx['id'],
  519. 4 + 1 + 3,
  520. EAP_TYPE_LEAP,
  521. 0, 0, 0)
  522. if ctx['num'] == 3:
  523. logger.info("Test: Invalid challenge length")
  524. return struct.pack(">BBHBBBB", EAP_CODE_REQUEST, ctx['id'],
  525. 4 + 1 + 3,
  526. EAP_TYPE_LEAP,
  527. 1, 0, 0)
  528. if ctx['num'] == 4:
  529. logger.info("Test: Truncated challenge")
  530. return struct.pack(">BBHBBBB", EAP_CODE_REQUEST, ctx['id'],
  531. 4 + 1 + 3,
  532. EAP_TYPE_LEAP,
  533. 1, 0, 8)
  534. if ctx['num'] == 5:
  535. logger.info("Test: Valid challenge")
  536. return struct.pack(">BBHBBBBLL", EAP_CODE_REQUEST, ctx['id'],
  537. 4 + 1 + 3 + 8,
  538. EAP_TYPE_LEAP,
  539. 1, 0, 8, 0, 0)
  540. if ctx['num'] == 6:
  541. logger.info("Test: Missing payload in Response")
  542. return struct.pack(">BBHB", EAP_CODE_RESPONSE, ctx['id'],
  543. 4 + 1,
  544. EAP_TYPE_LEAP)
  545. if ctx['num'] == 7:
  546. logger.info("Test: Valid challenge")
  547. return struct.pack(">BBHBBBBLL", EAP_CODE_REQUEST, ctx['id'],
  548. 4 + 1 + 3 + 8,
  549. EAP_TYPE_LEAP,
  550. 1, 0, 8, 0, 0)
  551. if ctx['num'] == 8:
  552. logger.info("Test: Unexpected version in Response")
  553. return struct.pack(">BBHBBBB", EAP_CODE_RESPONSE, ctx['id'],
  554. 4 + 1 + 3,
  555. EAP_TYPE_LEAP,
  556. 0, 0, 8)
  557. if ctx['num'] == 9:
  558. logger.info("Test: Valid challenge")
  559. return struct.pack(">BBHBBBBLL", EAP_CODE_REQUEST, ctx['id'],
  560. 4 + 1 + 3 + 8,
  561. EAP_TYPE_LEAP,
  562. 1, 0, 8, 0, 0)
  563. if ctx['num'] == 10:
  564. logger.info("Test: Invalid challenge length in Response")
  565. return struct.pack(">BBHBBBB", EAP_CODE_RESPONSE, ctx['id'],
  566. 4 + 1 + 3,
  567. EAP_TYPE_LEAP,
  568. 1, 0, 0)
  569. if ctx['num'] == 11:
  570. logger.info("Test: Valid challenge")
  571. return struct.pack(">BBHBBBBLL", EAP_CODE_REQUEST, ctx['id'],
  572. 4 + 1 + 3 + 8,
  573. EAP_TYPE_LEAP,
  574. 1, 0, 8, 0, 0)
  575. if ctx['num'] == 12:
  576. logger.info("Test: Truncated challenge in Response")
  577. return struct.pack(">BBHBBBB", EAP_CODE_RESPONSE, ctx['id'],
  578. 4 + 1 + 3,
  579. EAP_TYPE_LEAP,
  580. 1, 0, 24)
  581. if ctx['num'] == 13:
  582. logger.info("Test: Valid challenge")
  583. return struct.pack(">BBHBBBBLL", EAP_CODE_REQUEST, ctx['id'],
  584. 4 + 1 + 3 + 8,
  585. EAP_TYPE_LEAP,
  586. 1, 0, 8, 0, 0)
  587. if ctx['num'] == 14:
  588. logger.info("Test: Invalid challange value in Response")
  589. return struct.pack(">BBHBBBB6L", EAP_CODE_RESPONSE, ctx['id'],
  590. 4 + 1 + 3 + 24,
  591. EAP_TYPE_LEAP,
  592. 1, 0, 24,
  593. 0, 0, 0, 0, 0, 0)
  594. if ctx['num'] == 15:
  595. logger.info("Test: Valid challenge")
  596. return struct.pack(">BBHBBBBLL", EAP_CODE_REQUEST, ctx['id'],
  597. 4 + 1 + 3 + 8,
  598. EAP_TYPE_LEAP,
  599. 1, 0, 8, 0, 0)
  600. if ctx['num'] == 16:
  601. logger.info("Test: Valid challange value in Response")
  602. return struct.pack(">BBHBBBB24B", EAP_CODE_RESPONSE, ctx['id'],
  603. 4 + 1 + 3 + 24,
  604. EAP_TYPE_LEAP,
  605. 1, 0, 24,
  606. 0x48, 0x4e, 0x46, 0xe3, 0x88, 0x49, 0x46, 0xbd,
  607. 0x28, 0x48, 0xf8, 0x53, 0x82, 0x50, 0x00, 0x04,
  608. 0x93, 0x50, 0x30, 0xd7, 0x25, 0xea, 0x5f, 0x66)
  609. if ctx['num'] == 17:
  610. logger.info("Test: Valid challenge")
  611. return struct.pack(">BBHBBBBLL", EAP_CODE_REQUEST, ctx['id'],
  612. 4 + 1 + 3 + 8,
  613. EAP_TYPE_LEAP,
  614. 1, 0, 8, 0, 0)
  615. if ctx['num'] == 18:
  616. logger.info("Test: Success")
  617. return struct.pack(">BBHB", EAP_CODE_SUCCESS, ctx['id'],
  618. 4 + 1,
  619. EAP_TYPE_LEAP)
  620. # hostapd will drop the next frame in the sequence
  621. if ctx['num'] == 19:
  622. logger.info("Test: Valid challenge")
  623. return struct.pack(">BBHBBBBLL", EAP_CODE_REQUEST, ctx['id'],
  624. 4 + 1 + 3 + 8,
  625. EAP_TYPE_LEAP,
  626. 1, 0, 8, 0, 0)
  627. if ctx['num'] == 20:
  628. logger.info("Test: Failure")
  629. return struct.pack(">BBHB", EAP_CODE_FAILURE, ctx['id'],
  630. 4 + 1,
  631. EAP_TYPE_LEAP)
  632. return None
  633. srv = start_radius_server(leap_handler)
  634. try:
  635. hapd = start_ap(apdev[0]['ifname'])
  636. for i in range(0, 12):
  637. dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412",
  638. eap="LEAP", identity="user", password="password",
  639. wait_connect=False)
  640. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=15)
  641. if ev is None:
  642. raise Exception("Timeout on EAP start")
  643. time.sleep(0.1)
  644. if i == 10:
  645. logger.info("Wait for additional roundtrip")
  646. time.sleep(1)
  647. dev[0].request("REMOVE_NETWORK all")
  648. finally:
  649. stop_radius_server(srv)
  650. def test_eap_proto_md5(dev, apdev):
  651. """EAP-MD5 protocol tests"""
  652. check_eap_capa(dev[0], "MD5")
  653. def md5_handler(ctx, req):
  654. logger.info("md5_handler - RX " + req.encode("hex"))
  655. if 'num' not in ctx:
  656. ctx['num'] = 0
  657. ctx['num'] = ctx['num'] + 1
  658. if 'id' not in ctx:
  659. ctx['id'] = 1
  660. ctx['id'] = (ctx['id'] + 1) % 256
  661. if ctx['num'] == 1:
  662. logger.info("Test: Missing payload")
  663. return struct.pack(">BBHB", EAP_CODE_REQUEST, ctx['id'],
  664. 4 + 1,
  665. EAP_TYPE_MD5)
  666. if ctx['num'] == 2:
  667. logger.info("Test: Zero-length challenge")
  668. return struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  669. 4 + 1 + 1,
  670. EAP_TYPE_MD5,
  671. 0)
  672. if ctx['num'] == 3:
  673. logger.info("Test: Truncated challenge")
  674. return struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  675. 4 + 1 + 1,
  676. EAP_TYPE_MD5,
  677. 1)
  678. if ctx['num'] == 4:
  679. logger.info("Test: Shortest possible challenge and name")
  680. return struct.pack(">BBHBBBB", EAP_CODE_REQUEST, ctx['id'],
  681. 4 + 1 + 3,
  682. EAP_TYPE_MD5,
  683. 1, 0xaa, ord('n'))
  684. return None
  685. srv = start_radius_server(md5_handler)
  686. try:
  687. hapd = start_ap(apdev[0]['ifname'])
  688. for i in range(0, 4):
  689. dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412",
  690. eap="MD5", identity="user", password="password",
  691. wait_connect=False)
  692. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=15)
  693. if ev is None:
  694. raise Exception("Timeout on EAP start")
  695. time.sleep(0.1)
  696. dev[0].request("REMOVE_NETWORK all")
  697. finally:
  698. stop_radius_server(srv)
  699. def test_eap_proto_otp(dev, apdev):
  700. """EAP-OTP protocol tests"""
  701. def otp_handler(ctx, req):
  702. logger.info("otp_handler - RX " + req.encode("hex"))
  703. if 'num' not in ctx:
  704. ctx['num'] = 0
  705. ctx['num'] = ctx['num'] + 1
  706. if 'id' not in ctx:
  707. ctx['id'] = 1
  708. ctx['id'] = (ctx['id'] + 1) % 256
  709. if ctx['num'] == 1:
  710. logger.info("Test: Empty payload")
  711. return struct.pack(">BBHB", EAP_CODE_REQUEST, ctx['id'],
  712. 4 + 1,
  713. EAP_TYPE_OTP)
  714. if ctx['num'] == 2:
  715. logger.info("Test: Success")
  716. return struct.pack(">BBH", EAP_CODE_SUCCESS, ctx['id'],
  717. 4)
  718. if ctx['num'] == 3:
  719. logger.info("Test: Challenge included")
  720. return struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  721. 4 + 1 + 1,
  722. EAP_TYPE_OTP,
  723. ord('A'))
  724. if ctx['num'] == 4:
  725. logger.info("Test: Success")
  726. return struct.pack(">BBH", EAP_CODE_SUCCESS, ctx['id'],
  727. 4)
  728. return None
  729. srv = start_radius_server(otp_handler)
  730. try:
  731. hapd = start_ap(apdev[0]['ifname'])
  732. for i in range(0, 1):
  733. dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412",
  734. eap="OTP", identity="user", password="password",
  735. wait_connect=False)
  736. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"],
  737. timeout=15)
  738. if ev is None:
  739. raise Exception("Timeout on EAP start")
  740. time.sleep(0.1)
  741. dev[0].request("REMOVE_NETWORK all")
  742. dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412",
  743. eap="OTP", identity="user", wait_connect=False)
  744. ev = dev[0].wait_event(["CTRL-REQ-OTP"])
  745. if ev is None:
  746. raise Exception("Request for password timed out")
  747. id = ev.split(':')[0].split('-')[-1]
  748. dev[0].request("CTRL-RSP-OTP-" + id + ":password")
  749. ev = dev[0].wait_event("CTRL-EVENT-EAP-SUCCESS")
  750. if ev is None:
  751. raise Exception("Success not reported")
  752. finally:
  753. stop_radius_server(srv)
  754. EAP_GPSK_OPCODE_GPSK_1 = 1
  755. EAP_GPSK_OPCODE_GPSK_2 = 2
  756. EAP_GPSK_OPCODE_GPSK_3 = 3
  757. EAP_GPSK_OPCODE_GPSK_4 = 4
  758. EAP_GPSK_OPCODE_FAIL = 5
  759. EAP_GPSK_OPCODE_PROTECTED_FAIL = 6
  760. def test_eap_proto_gpsk(dev, apdev):
  761. """EAP-GPSK protocol tests"""
  762. def gpsk_handler(ctx, req):
  763. logger.info("gpsk_handler - RX " + req.encode("hex"))
  764. if 'num' not in ctx:
  765. ctx['num'] = 0
  766. ctx['num'] = ctx['num'] + 1
  767. if 'id' not in ctx:
  768. ctx['id'] = 1
  769. ctx['id'] = (ctx['id'] + 1) % 256
  770. idx = 0
  771. idx += 1
  772. if ctx['num'] == idx:
  773. logger.info("Test: Missing payload")
  774. return struct.pack(">BBHB", EAP_CODE_REQUEST, ctx['id'],
  775. 4 + 1,
  776. EAP_TYPE_GPSK)
  777. idx += 1
  778. if ctx['num'] == idx:
  779. logger.info("Test: Unknown opcode")
  780. return struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  781. 4 + 1 + 1,
  782. EAP_TYPE_GPSK,
  783. 255)
  784. idx += 1
  785. if ctx['num'] == idx:
  786. logger.info("Test: Unexpected GPSK-3")
  787. return struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  788. 4 + 1 + 1,
  789. EAP_TYPE_GPSK,
  790. EAP_GPSK_OPCODE_GPSK_3)
  791. idx += 1
  792. if ctx['num'] == idx:
  793. logger.info("Test: GPSK-1 Too short GPSK-1")
  794. return struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  795. 4 + 1 + 1,
  796. EAP_TYPE_GPSK,
  797. EAP_GPSK_OPCODE_GPSK_1)
  798. idx += 1
  799. if ctx['num'] == idx:
  800. logger.info("Test: GPSK-1 Truncated ID_Server")
  801. return struct.pack(">BBHBBH", EAP_CODE_REQUEST, ctx['id'],
  802. 4 + 1 + 1 + 2,
  803. EAP_TYPE_GPSK,
  804. EAP_GPSK_OPCODE_GPSK_1, 1)
  805. idx += 1
  806. if ctx['num'] == idx:
  807. logger.info("Test: GPSK-1 Missing RAND_Server")
  808. return struct.pack(">BBHBBH", EAP_CODE_REQUEST, ctx['id'],
  809. 4 + 1 + 1 + 2,
  810. EAP_TYPE_GPSK,
  811. EAP_GPSK_OPCODE_GPSK_1, 0)
  812. idx += 1
  813. if ctx['num'] == idx:
  814. logger.info("Test: GPSK-1 Missing CSuite_List")
  815. return struct.pack(">BBHBBH8L", EAP_CODE_REQUEST, ctx['id'],
  816. 4 + 1 + 1 + 2 + 32,
  817. EAP_TYPE_GPSK,
  818. EAP_GPSK_OPCODE_GPSK_1, 0,
  819. 0, 0, 0, 0, 0, 0, 0, 0)
  820. idx += 1
  821. if ctx['num'] == idx:
  822. logger.info("Test: GPSK-1 Truncated CSuite_List")
  823. return struct.pack(">BBHBBH8LH", EAP_CODE_REQUEST, ctx['id'],
  824. 4 + 1 + 1 + 2 + 32 + 2,
  825. EAP_TYPE_GPSK,
  826. EAP_GPSK_OPCODE_GPSK_1, 0,
  827. 0, 0, 0, 0, 0, 0, 0, 0,
  828. 1)
  829. idx += 1
  830. if ctx['num'] == idx:
  831. logger.info("Test: GPSK-1 Empty CSuite_List")
  832. return struct.pack(">BBHBBH8LH", EAP_CODE_REQUEST, ctx['id'],
  833. 4 + 1 + 1 + 2 + 32 + 2,
  834. EAP_TYPE_GPSK,
  835. EAP_GPSK_OPCODE_GPSK_1, 0,
  836. 0, 0, 0, 0, 0, 0, 0, 0,
  837. 0)
  838. idx += 1
  839. if ctx['num'] == idx:
  840. logger.info("Test: GPSK-1 Invalid CSuite_List")
  841. return struct.pack(">BBHBBH8LHB", EAP_CODE_REQUEST, ctx['id'],
  842. 4 + 1 + 1 + 2 + 32 + 2 + 1,
  843. EAP_TYPE_GPSK,
  844. EAP_GPSK_OPCODE_GPSK_1, 0,
  845. 0, 0, 0, 0, 0, 0, 0, 0,
  846. 1, 0)
  847. idx += 1
  848. if ctx['num'] == idx:
  849. logger.info("Test: GPSK-1 No supported CSuite")
  850. return struct.pack(">BBHBBH8LHLH", EAP_CODE_REQUEST, ctx['id'],
  851. 4 + 1 + 1 + 2 + 32 + 2 + 6,
  852. EAP_TYPE_GPSK,
  853. EAP_GPSK_OPCODE_GPSK_1, 0,
  854. 0, 0, 0, 0, 0, 0, 0, 0,
  855. 6, 0, 0)
  856. idx += 1
  857. if ctx['num'] == idx:
  858. logger.info("Test: GPSK-1 Supported CSuite")
  859. return struct.pack(">BBHBBH8LHLH", EAP_CODE_REQUEST, ctx['id'],
  860. 4 + 1 + 1 + 2 + 32 + 2 + 6,
  861. EAP_TYPE_GPSK,
  862. EAP_GPSK_OPCODE_GPSK_1, 0,
  863. 0, 0, 0, 0, 0, 0, 0, 0,
  864. 6, 0, 1)
  865. idx += 1
  866. if ctx['num'] == idx:
  867. logger.info("Test: Unexpected GPSK-1")
  868. return struct.pack(">BBHBBH8LHLH", EAP_CODE_REQUEST, ctx['id'],
  869. 4 + 1 + 1 + 2 + 32 + 2 + 6,
  870. EAP_TYPE_GPSK,
  871. EAP_GPSK_OPCODE_GPSK_1, 0,
  872. 0, 0, 0, 0, 0, 0, 0, 0,
  873. 6, 0, 1)
  874. idx += 1
  875. if ctx['num'] == idx:
  876. logger.info("Test: GPSK-1 Supported CSuite but too short key")
  877. return struct.pack(">BBHBBH8LHLH", EAP_CODE_REQUEST, ctx['id'],
  878. 4 + 1 + 1 + 2 + 32 + 2 + 6,
  879. EAP_TYPE_GPSK,
  880. EAP_GPSK_OPCODE_GPSK_1, 0,
  881. 0, 0, 0, 0, 0, 0, 0, 0,
  882. 6, 0, 1)
  883. idx += 1
  884. if ctx['num'] == idx:
  885. logger.info("Test: GPSK-1 Supported CSuite")
  886. return struct.pack(">BBHBBH8LHLH", EAP_CODE_REQUEST, ctx['id'],
  887. 4 + 1 + 1 + 2 + 32 + 2 + 6,
  888. EAP_TYPE_GPSK,
  889. EAP_GPSK_OPCODE_GPSK_1, 0,
  890. 0, 0, 0, 0, 0, 0, 0, 0,
  891. 6, 0, 1)
  892. idx += 1
  893. if ctx['num'] == idx:
  894. logger.info("Test: Too short GPSK-3")
  895. return struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  896. 4 + 1 + 1,
  897. EAP_TYPE_GPSK,
  898. EAP_GPSK_OPCODE_GPSK_3)
  899. idx += 1
  900. if ctx['num'] == idx:
  901. logger.info("Test: GPSK-1 Supported CSuite")
  902. return struct.pack(">BBHBBH8LHLH", EAP_CODE_REQUEST, ctx['id'],
  903. 4 + 1 + 1 + 2 + 32 + 2 + 6,
  904. EAP_TYPE_GPSK,
  905. EAP_GPSK_OPCODE_GPSK_1, 0,
  906. 0, 0, 0, 0, 0, 0, 0, 0,
  907. 6, 0, 1)
  908. idx += 1
  909. if ctx['num'] == idx:
  910. logger.info("Test: GPSK-3 Mismatch in RAND_Peer")
  911. return struct.pack(">BBHBB8L", EAP_CODE_REQUEST, ctx['id'],
  912. 4 + 1 + 1 + 32,
  913. EAP_TYPE_GPSK,
  914. EAP_GPSK_OPCODE_GPSK_3,
  915. 0, 0, 0, 0, 0, 0, 0, 0)
  916. idx += 1
  917. if ctx['num'] == idx:
  918. logger.info("Test: GPSK-1 Supported CSuite")
  919. return struct.pack(">BBHBBH8LHLH", EAP_CODE_REQUEST, ctx['id'],
  920. 4 + 1 + 1 + 2 + 32 + 2 + 6,
  921. EAP_TYPE_GPSK,
  922. EAP_GPSK_OPCODE_GPSK_1, 0,
  923. 0, 0, 0, 0, 0, 0, 0, 0,
  924. 6, 0, 1)
  925. idx += 1
  926. if ctx['num'] == idx:
  927. logger.info("Test: GPSK-3 Missing RAND_Server")
  928. msg = struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  929. 4 + 1 + 1 + 32,
  930. EAP_TYPE_GPSK,
  931. EAP_GPSK_OPCODE_GPSK_3)
  932. msg += req[14:46]
  933. return msg
  934. idx += 1
  935. if ctx['num'] == idx:
  936. logger.info("Test: GPSK-1 Supported CSuite")
  937. return struct.pack(">BBHBBH8LHLH", EAP_CODE_REQUEST, ctx['id'],
  938. 4 + 1 + 1 + 2 + 32 + 2 + 6,
  939. EAP_TYPE_GPSK,
  940. EAP_GPSK_OPCODE_GPSK_1, 0,
  941. 0, 0, 0, 0, 0, 0, 0, 0,
  942. 6, 0, 1)
  943. idx += 1
  944. if ctx['num'] == idx:
  945. logger.info("Test: GPSK-3 Mismatch in RAND_Server")
  946. msg = struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  947. 4 + 1 + 1 + 32 + 32,
  948. EAP_TYPE_GPSK,
  949. EAP_GPSK_OPCODE_GPSK_3)
  950. msg += req[14:46]
  951. msg += struct.pack(">8L", 1, 1, 1, 1, 1, 1, 1, 1)
  952. return msg
  953. idx += 1
  954. if ctx['num'] == idx:
  955. logger.info("Test: GPSK-1 Supported CSuite")
  956. return struct.pack(">BBHBBH8LHLH", EAP_CODE_REQUEST, ctx['id'],
  957. 4 + 1 + 1 + 2 + 32 + 2 + 6,
  958. EAP_TYPE_GPSK,
  959. EAP_GPSK_OPCODE_GPSK_1, 0,
  960. 0, 0, 0, 0, 0, 0, 0, 0,
  961. 6, 0, 1)
  962. idx += 1
  963. if ctx['num'] == idx:
  964. logger.info("Test: GPSK-3 Missing ID_Server")
  965. msg = struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  966. 4 + 1 + 1 + 32 + 32,
  967. EAP_TYPE_GPSK,
  968. EAP_GPSK_OPCODE_GPSK_3)
  969. msg += req[14:46]
  970. msg += struct.pack(">8L", 0, 0, 0, 0, 0, 0, 0, 0)
  971. return msg
  972. idx += 1
  973. if ctx['num'] == idx:
  974. logger.info("Test: GPSK-1 Supported CSuite")
  975. return struct.pack(">BBHBBH8LHLH", EAP_CODE_REQUEST, ctx['id'],
  976. 4 + 1 + 1 + 2 + 32 + 2 + 6,
  977. EAP_TYPE_GPSK,
  978. EAP_GPSK_OPCODE_GPSK_1, 0,
  979. 0, 0, 0, 0, 0, 0, 0, 0,
  980. 6, 0, 1)
  981. idx += 1
  982. if ctx['num'] == idx:
  983. logger.info("Test: GPSK-3 Truncated ID_Server")
  984. msg = struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  985. 4 + 1 + 1 + 32 + 32 + 2,
  986. EAP_TYPE_GPSK,
  987. EAP_GPSK_OPCODE_GPSK_3)
  988. msg += req[14:46]
  989. msg += struct.pack(">8LH", 0, 0, 0, 0, 0, 0, 0, 0, 1)
  990. return msg
  991. idx += 1
  992. if ctx['num'] == idx:
  993. logger.info("Test: GPSK-1 Supported CSuite")
  994. return struct.pack(">BBHBBH8LHLH", EAP_CODE_REQUEST, ctx['id'],
  995. 4 + 1 + 1 + 2 + 32 + 2 + 6,
  996. EAP_TYPE_GPSK,
  997. EAP_GPSK_OPCODE_GPSK_1, 0,
  998. 0, 0, 0, 0, 0, 0, 0, 0,
  999. 6, 0, 1)
  1000. idx += 1
  1001. if ctx['num'] == idx:
  1002. logger.info("Test: GPSK-3 Mismatch in ID_Server")
  1003. msg = struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  1004. 4 + 1 + 1 + 32 + 32 + 3,
  1005. EAP_TYPE_GPSK,
  1006. EAP_GPSK_OPCODE_GPSK_3)
  1007. msg += req[14:46]
  1008. msg += struct.pack(">8LHB", 0, 0, 0, 0, 0, 0, 0, 0, 1, ord('B'))
  1009. return msg
  1010. idx += 1
  1011. if ctx['num'] == idx:
  1012. logger.info("Test: GPSK-1 Supported CSuite")
  1013. return struct.pack(">BBHBBHB8LHLH", EAP_CODE_REQUEST, ctx['id'],
  1014. 4 + 1 + 1 + 3 + 32 + 2 + 6,
  1015. EAP_TYPE_GPSK,
  1016. EAP_GPSK_OPCODE_GPSK_1, 1, ord('A'),
  1017. 0, 0, 0, 0, 0, 0, 0, 0,
  1018. 6, 0, 1)
  1019. idx += 1
  1020. if ctx['num'] == idx:
  1021. logger.info("Test: GPSK-3 Mismatch in ID_Server (same length)")
  1022. msg = struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  1023. 4 + 1 + 1 + 32 + 32 + 3,
  1024. EAP_TYPE_GPSK,
  1025. EAP_GPSK_OPCODE_GPSK_3)
  1026. msg += req[15:47]
  1027. msg += struct.pack(">8LHB", 0, 0, 0, 0, 0, 0, 0, 0, 1, ord('B'))
  1028. return msg
  1029. idx += 1
  1030. if ctx['num'] == idx:
  1031. logger.info("Test: GPSK-1 Supported CSuite")
  1032. return struct.pack(">BBHBBH8LHLH", EAP_CODE_REQUEST, ctx['id'],
  1033. 4 + 1 + 1 + 2 + 32 + 2 + 6,
  1034. EAP_TYPE_GPSK,
  1035. EAP_GPSK_OPCODE_GPSK_1, 0,
  1036. 0, 0, 0, 0, 0, 0, 0, 0,
  1037. 6, 0, 1)
  1038. idx += 1
  1039. if ctx['num'] == idx:
  1040. logger.info("Test: GPSK-3 Missing CSuite_Sel")
  1041. msg = struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  1042. 4 + 1 + 1 + 32 + 32 + 2,
  1043. EAP_TYPE_GPSK,
  1044. EAP_GPSK_OPCODE_GPSK_3)
  1045. msg += req[14:46]
  1046. msg += struct.pack(">8LH", 0, 0, 0, 0, 0, 0, 0, 0, 0)
  1047. return msg
  1048. idx += 1
  1049. if ctx['num'] == idx:
  1050. logger.info("Test: GPSK-1 Supported CSuite")
  1051. return struct.pack(">BBHBBH8LHLH", EAP_CODE_REQUEST, ctx['id'],
  1052. 4 + 1 + 1 + 2 + 32 + 2 + 6,
  1053. EAP_TYPE_GPSK,
  1054. EAP_GPSK_OPCODE_GPSK_1, 0,
  1055. 0, 0, 0, 0, 0, 0, 0, 0,
  1056. 6, 0, 1)
  1057. idx += 1
  1058. if ctx['num'] == idx:
  1059. logger.info("Test: GPSK-3 Mismatch in CSuite_Sel")
  1060. msg = struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  1061. 4 + 1 + 1 + 32 + 32 + 2 + 6,
  1062. EAP_TYPE_GPSK,
  1063. EAP_GPSK_OPCODE_GPSK_3)
  1064. msg += req[14:46]
  1065. msg += struct.pack(">8LHLH", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2)
  1066. return msg
  1067. idx += 1
  1068. if ctx['num'] == idx:
  1069. logger.info("Test: GPSK-1 Supported CSuite")
  1070. return struct.pack(">BBHBBH8LHLH", EAP_CODE_REQUEST, ctx['id'],
  1071. 4 + 1 + 1 + 2 + 32 + 2 + 6,
  1072. EAP_TYPE_GPSK,
  1073. EAP_GPSK_OPCODE_GPSK_1, 0,
  1074. 0, 0, 0, 0, 0, 0, 0, 0,
  1075. 6, 0, 1)
  1076. idx += 1
  1077. if ctx['num'] == idx:
  1078. logger.info("Test: GPSK-3 Missing len(PD_Payload_Block)")
  1079. msg = struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  1080. 4 + 1 + 1 + 32 + 32 + 2 + 6,
  1081. EAP_TYPE_GPSK,
  1082. EAP_GPSK_OPCODE_GPSK_3)
  1083. msg += req[14:46]
  1084. msg += struct.pack(">8LHLH", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)
  1085. return msg
  1086. idx += 1
  1087. if ctx['num'] == idx:
  1088. logger.info("Test: GPSK-1 Supported CSuite")
  1089. return struct.pack(">BBHBBH8LHLH", EAP_CODE_REQUEST, ctx['id'],
  1090. 4 + 1 + 1 + 2 + 32 + 2 + 6,
  1091. EAP_TYPE_GPSK,
  1092. EAP_GPSK_OPCODE_GPSK_1, 0,
  1093. 0, 0, 0, 0, 0, 0, 0, 0,
  1094. 6, 0, 1)
  1095. idx += 1
  1096. if ctx['num'] == idx:
  1097. logger.info("Test: GPSK-3 Truncated PD_Payload_Block")
  1098. msg = struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  1099. 4 + 1 + 1 + 32 + 32 + 2 + 6 + 2,
  1100. EAP_TYPE_GPSK,
  1101. EAP_GPSK_OPCODE_GPSK_3)
  1102. msg += req[14:46]
  1103. msg += struct.pack(">8LHLHH", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1)
  1104. return msg
  1105. idx += 1
  1106. if ctx['num'] == idx:
  1107. logger.info("Test: GPSK-1 Supported CSuite")
  1108. return struct.pack(">BBHBBH8LHLH", EAP_CODE_REQUEST, ctx['id'],
  1109. 4 + 1 + 1 + 2 + 32 + 2 + 6,
  1110. EAP_TYPE_GPSK,
  1111. EAP_GPSK_OPCODE_GPSK_1, 0,
  1112. 0, 0, 0, 0, 0, 0, 0, 0,
  1113. 6, 0, 1)
  1114. idx += 1
  1115. if ctx['num'] == idx:
  1116. logger.info("Test: GPSK-3 Missing MAC")
  1117. msg = struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  1118. 4 + 1 + 1 + 32 + 32 + 2 + 6 + 3,
  1119. EAP_TYPE_GPSK,
  1120. EAP_GPSK_OPCODE_GPSK_3)
  1121. msg += req[14:46]
  1122. msg += struct.pack(">8LHLHHB",
  1123. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 123)
  1124. return msg
  1125. idx += 1
  1126. if ctx['num'] == idx:
  1127. logger.info("Test: GPSK-1 Supported CSuite")
  1128. return struct.pack(">BBHBBH8LHLH", EAP_CODE_REQUEST, ctx['id'],
  1129. 4 + 1 + 1 + 2 + 32 + 2 + 6,
  1130. EAP_TYPE_GPSK,
  1131. EAP_GPSK_OPCODE_GPSK_1, 0,
  1132. 0, 0, 0, 0, 0, 0, 0, 0,
  1133. 6, 0, 1)
  1134. idx += 1
  1135. if ctx['num'] == idx:
  1136. logger.info("Test: GPSK-3 Incorrect MAC")
  1137. msg = struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  1138. 4 + 1 + 1 + 32 + 32 + 2 + 6 + 3 + 16,
  1139. EAP_TYPE_GPSK,
  1140. EAP_GPSK_OPCODE_GPSK_3)
  1141. msg += req[14:46]
  1142. msg += struct.pack(">8LHLHHB4L",
  1143. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 123,
  1144. 0, 0, 0, 0)
  1145. return msg
  1146. return None
  1147. srv = start_radius_server(gpsk_handler)
  1148. try:
  1149. hapd = start_ap(apdev[0]['ifname'])
  1150. for i in range(0, 27):
  1151. if i == 12:
  1152. pw = "short"
  1153. else:
  1154. pw = "abcdefghijklmnop0123456789abcdef"
  1155. dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412",
  1156. eap="GPSK", identity="user", password=pw,
  1157. wait_connect=False)
  1158. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"],
  1159. timeout=15)
  1160. if ev is None:
  1161. raise Exception("Timeout on EAP start")
  1162. time.sleep(0.05)
  1163. dev[0].request("REMOVE_NETWORK all")
  1164. finally:
  1165. stop_radius_server(srv)
  1166. EAP_EKE_ID = 1
  1167. EAP_EKE_COMMIT = 2
  1168. EAP_EKE_CONFIRM = 3
  1169. EAP_EKE_FAILURE = 4
  1170. def test_eap_proto_eke(dev, apdev):
  1171. """EAP-EKE protocol tests"""
  1172. def eke_handler(ctx, req):
  1173. logger.info("eke_handler - RX " + req.encode("hex"))
  1174. if 'num' not in ctx:
  1175. ctx['num'] = 0
  1176. ctx['num'] = ctx['num'] + 1
  1177. if 'id' not in ctx:
  1178. ctx['id'] = 1
  1179. ctx['id'] = (ctx['id'] + 1) % 256
  1180. idx = 0
  1181. idx += 1
  1182. if ctx['num'] == idx:
  1183. logger.info("Test: Missing payload")
  1184. return struct.pack(">BBHB", EAP_CODE_REQUEST, ctx['id'],
  1185. 4 + 1,
  1186. EAP_TYPE_EKE)
  1187. idx += 1
  1188. if ctx['num'] == idx:
  1189. logger.info("Test: Unknown exchange")
  1190. return struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  1191. 4 + 1 + 1,
  1192. EAP_TYPE_EKE,
  1193. 255)
  1194. idx += 1
  1195. if ctx['num'] == idx:
  1196. logger.info("Test: No NumProposals in EAP-EKE-ID/Request")
  1197. return struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  1198. 4 + 1 + 1,
  1199. EAP_TYPE_EKE,
  1200. EAP_EKE_ID)
  1201. idx += 1
  1202. if ctx['num'] == idx:
  1203. logger.info("Test: EAP-Failure")
  1204. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  1205. idx += 1
  1206. if ctx['num'] == idx:
  1207. logger.info("Test: NumProposals=0 in EAP-EKE-ID/Request")
  1208. return struct.pack(">BBHBBB", EAP_CODE_REQUEST, ctx['id'],
  1209. 4 + 1 + 1 + 1,
  1210. EAP_TYPE_EKE,
  1211. EAP_EKE_ID,
  1212. 0)
  1213. idx += 1
  1214. if ctx['num'] == idx:
  1215. logger.info("Test: EAP-Failure")
  1216. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  1217. idx += 1
  1218. if ctx['num'] == idx:
  1219. logger.info("Test: Truncated Proposals list in EAP-EKE-ID/Request")
  1220. return struct.pack(">BBHBBBB4B", EAP_CODE_REQUEST, ctx['id'],
  1221. 4 + 1 + 1 + 2 + 4,
  1222. EAP_TYPE_EKE,
  1223. EAP_EKE_ID,
  1224. 2, 0, 0, 0, 0, 0)
  1225. idx += 1
  1226. if ctx['num'] == idx:
  1227. logger.info("Test: EAP-Failure")
  1228. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  1229. idx += 1
  1230. if ctx['num'] == idx:
  1231. logger.info("Test: Unsupported proposals in EAP-EKE-ID/Request")
  1232. return struct.pack(">BBHBBBB4B4B4B4B", EAP_CODE_REQUEST, ctx['id'],
  1233. 4 + 1 + 1 + 2 + 4 * 4,
  1234. EAP_TYPE_EKE,
  1235. EAP_EKE_ID,
  1236. 4, 0,
  1237. 0, 0, 0, 0,
  1238. 3, 0, 0, 0,
  1239. 3, 1, 0, 0,
  1240. 3, 1, 1, 0)
  1241. idx += 1
  1242. if ctx['num'] == idx:
  1243. logger.info("Test: EAP-Failure")
  1244. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  1245. idx += 1
  1246. if ctx['num'] == idx:
  1247. logger.info("Test: Missing IDType/Identity in EAP-EKE-ID/Request")
  1248. return struct.pack(">BBHBBBB4B4B4B4B4B",
  1249. EAP_CODE_REQUEST, ctx['id'],
  1250. 4 + 1 + 1 + 2 + 5 * 4,
  1251. EAP_TYPE_EKE,
  1252. EAP_EKE_ID,
  1253. 5, 0,
  1254. 0, 0, 0, 0,
  1255. 3, 0, 0, 0,
  1256. 3, 1, 0, 0,
  1257. 3, 1, 1, 0,
  1258. 3, 1, 1, 1)
  1259. idx += 1
  1260. if ctx['num'] == idx:
  1261. logger.info("Test: EAP-Failure")
  1262. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  1263. idx += 1
  1264. if ctx['num'] == idx:
  1265. logger.info("Test: Valid EAP-EKE-ID/Request")
  1266. return struct.pack(">BBHBBBB4BB",
  1267. EAP_CODE_REQUEST, ctx['id'],
  1268. 4 + 1 + 1 + 2 + 4 + 1,
  1269. EAP_TYPE_EKE,
  1270. EAP_EKE_ID,
  1271. 1, 0,
  1272. 3, 1, 1, 1,
  1273. 255)
  1274. idx += 1
  1275. if ctx['num'] == idx:
  1276. logger.info("Test: Unexpected EAP-EKE-ID/Request")
  1277. return struct.pack(">BBHBBBB4BB",
  1278. EAP_CODE_REQUEST, ctx['id'],
  1279. 4 + 1 + 1 + 2 + 4 + 1,
  1280. EAP_TYPE_EKE,
  1281. EAP_EKE_ID,
  1282. 1, 0,
  1283. 3, 1, 1, 1,
  1284. 255)
  1285. idx += 1
  1286. if ctx['num'] == idx:
  1287. logger.info("Test: EAP-Failure")
  1288. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  1289. idx += 1
  1290. if ctx['num'] == idx:
  1291. logger.info("Test: Valid EAP-EKE-ID/Request")
  1292. return struct.pack(">BBHBBBB4BB",
  1293. EAP_CODE_REQUEST, ctx['id'],
  1294. 4 + 1 + 1 + 2 + 4 + 1,
  1295. EAP_TYPE_EKE,
  1296. EAP_EKE_ID,
  1297. 1, 0,
  1298. 3, 1, 1, 1,
  1299. 255)
  1300. idx += 1
  1301. if ctx['num'] == idx:
  1302. logger.info("Test: Unexpected EAP-EKE-Confirm/Request")
  1303. return struct.pack(">BBHBB",
  1304. EAP_CODE_REQUEST, ctx['id'],
  1305. 4 + 1 + 1,
  1306. EAP_TYPE_EKE,
  1307. EAP_EKE_CONFIRM)
  1308. idx += 1
  1309. if ctx['num'] == idx:
  1310. logger.info("Test: EAP-Failure")
  1311. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  1312. idx += 1
  1313. if ctx['num'] == idx:
  1314. logger.info("Test: Too short EAP-EKE-Failure/Request")
  1315. return struct.pack(">BBHBB",
  1316. EAP_CODE_REQUEST, ctx['id'],
  1317. 4 + 1 + 1,
  1318. EAP_TYPE_EKE,
  1319. EAP_EKE_FAILURE)
  1320. idx += 1
  1321. if ctx['num'] == idx:
  1322. logger.info("Test: EAP-Failure")
  1323. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  1324. idx += 1
  1325. if ctx['num'] == idx:
  1326. logger.info("Test: Unexpected EAP-EKE-Commit/Request")
  1327. return struct.pack(">BBHBB",
  1328. EAP_CODE_REQUEST, ctx['id'],
  1329. 4 + 1 + 1,
  1330. EAP_TYPE_EKE,
  1331. EAP_EKE_COMMIT)
  1332. idx += 1
  1333. if ctx['num'] == idx:
  1334. logger.info("Test: EAP-Failure")
  1335. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  1336. idx += 1
  1337. if ctx['num'] == idx:
  1338. logger.info("Test: Valid EAP-EKE-ID/Request")
  1339. return struct.pack(">BBHBBBB4BB",
  1340. EAP_CODE_REQUEST, ctx['id'],
  1341. 4 + 1 + 1 + 2 + 4 + 1,
  1342. EAP_TYPE_EKE,
  1343. EAP_EKE_ID,
  1344. 1, 0,
  1345. 3, 1, 1, 1,
  1346. 255)
  1347. idx += 1
  1348. if ctx['num'] == idx:
  1349. logger.info("Test: Too short EAP-EKE-Commit/Request")
  1350. return struct.pack(">BBHBB",
  1351. EAP_CODE_REQUEST, ctx['id'],
  1352. 4 + 1 + 1,
  1353. EAP_TYPE_EKE,
  1354. EAP_EKE_COMMIT)
  1355. idx += 1
  1356. if ctx['num'] == idx:
  1357. logger.info("Test: EAP-Failure")
  1358. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  1359. idx += 1
  1360. if ctx['num'] == idx:
  1361. logger.info("Test: Valid EAP-EKE-ID/Request")
  1362. return struct.pack(">BBHBBBB4BB",
  1363. EAP_CODE_REQUEST, ctx['id'],
  1364. 4 + 1 + 1 + 2 + 4 + 1,
  1365. EAP_TYPE_EKE,
  1366. EAP_EKE_ID,
  1367. 1, 0,
  1368. 1, 1, 1, 1,
  1369. 255)
  1370. idx += 1
  1371. if ctx['num'] == idx:
  1372. logger.info("Test: All zeroes DHComponent_S and empty CBvalue in EAP-EKE-Commit/Request")
  1373. return struct.pack(">BBHBB4L32L",
  1374. EAP_CODE_REQUEST, ctx['id'],
  1375. 4 + 1 + 1 + 16 + 128,
  1376. EAP_TYPE_EKE,
  1377. EAP_EKE_COMMIT,
  1378. 0, 0, 0, 0,
  1379. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1380. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
  1381. idx += 1
  1382. if ctx['num'] == idx:
  1383. logger.info("Test: Too short EAP-EKE-Confirm/Request")
  1384. return struct.pack(">BBHBB",
  1385. EAP_CODE_REQUEST, ctx['id'],
  1386. 4 + 1 + 1,
  1387. EAP_TYPE_EKE,
  1388. EAP_EKE_CONFIRM)
  1389. idx += 1
  1390. if ctx['num'] == idx:
  1391. logger.info("Test: EAP-Failure")
  1392. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  1393. idx += 1
  1394. if ctx['num'] == idx:
  1395. logger.info("Test: Valid EAP-EKE-ID/Request")
  1396. return struct.pack(">BBHBBBB4BB",
  1397. EAP_CODE_REQUEST, ctx['id'],
  1398. 4 + 1 + 1 + 2 + 4 + 1,
  1399. EAP_TYPE_EKE,
  1400. EAP_EKE_ID,
  1401. 1, 0,
  1402. 1, 1, 1, 1,
  1403. 255)
  1404. idx += 1
  1405. if ctx['num'] == idx:
  1406. logger.info("Test: All zeroes DHComponent_S and empty CBvalue in EAP-EKE-Commit/Request")
  1407. return struct.pack(">BBHBB4L32L",
  1408. EAP_CODE_REQUEST, ctx['id'],
  1409. 4 + 1 + 1 + 16 + 128,
  1410. EAP_TYPE_EKE,
  1411. EAP_EKE_COMMIT,
  1412. 0, 0, 0, 0,
  1413. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1414. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
  1415. idx += 1
  1416. if ctx['num'] == idx:
  1417. logger.info("Test: Invalid PNonce_PS and Auth_S values in EAP-EKE-Confirm/Request")
  1418. return struct.pack(">BBHBB4L8L5L5L",
  1419. EAP_CODE_REQUEST, ctx['id'],
  1420. 4 + 1 + 1 + 16 + 2 * 16 + 20 + 20,
  1421. EAP_TYPE_EKE,
  1422. EAP_EKE_CONFIRM,
  1423. 0, 0, 0, 0,
  1424. 0, 0, 0, 0, 0, 0, 0, 0,
  1425. 0, 0, 0, 0, 0,
  1426. 0, 0, 0, 0, 0)
  1427. idx += 1
  1428. if ctx['num'] == idx:
  1429. logger.info("Test: EAP-Failure")
  1430. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  1431. return None
  1432. srv = start_radius_server(eke_handler)
  1433. try:
  1434. hapd = start_ap(apdev[0]['ifname'])
  1435. for i in range(0, 14):
  1436. dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412",
  1437. eap="EKE", identity="user", password="password",
  1438. wait_connect=False)
  1439. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"],
  1440. timeout=15)
  1441. if ev is None:
  1442. raise Exception("Timeout on EAP start")
  1443. if i in [ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ]:
  1444. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"],
  1445. timeout=10)
  1446. if ev is None:
  1447. raise Exception("Timeout on EAP failure")
  1448. else:
  1449. time.sleep(0.05)
  1450. dev[0].request("REMOVE_NETWORK all")
  1451. dev[0].dump_monitor()
  1452. finally:
  1453. stop_radius_server(srv)
  1454. EAP_PAX_OP_STD_1 = 0x01
  1455. EAP_PAX_OP_STD_2 = 0x02
  1456. EAP_PAX_OP_STD_3 = 0x03
  1457. EAP_PAX_OP_SEC_1 = 0x11
  1458. EAP_PAX_OP_SEC_2 = 0x12
  1459. EAP_PAX_OP_SEC_3 = 0x13
  1460. EAP_PAX_OP_SEC_4 = 0x14
  1461. EAP_PAX_OP_SEC_5 = 0x15
  1462. EAP_PAX_OP_ACK = 0x21
  1463. EAP_PAX_FLAGS_MF = 0x01
  1464. EAP_PAX_FLAGS_CE = 0x02
  1465. EAP_PAX_FLAGS_AI = 0x04
  1466. EAP_PAX_MAC_HMAC_SHA1_128 = 0x01
  1467. EAP_PAX_HMAC_SHA256_128 = 0x02
  1468. EAP_PAX_DH_GROUP_NONE = 0x00
  1469. EAP_PAX_DH_GROUP_2048_MODP = 0x01
  1470. EAP_PAX_DH_GROUP_3072_MODP = 0x02
  1471. EAP_PAX_DH_GROUP_NIST_ECC_P_256 = 0x03
  1472. EAP_PAX_PUBLIC_KEY_NONE = 0x00
  1473. EAP_PAX_PUBLIC_KEY_RSAES_OAEP = 0x01
  1474. EAP_PAX_PUBLIC_KEY_RSA_PKCS1_V1_5 = 0x02
  1475. EAP_PAX_PUBLIC_KEY_EL_GAMAL_NIST_ECC = 0x03
  1476. EAP_PAX_ADE_VENDOR_SPECIFIC = 0x01
  1477. EAP_PAX_ADE_CLIENT_CHANNEL_BINDING = 0x02
  1478. EAP_PAX_ADE_SERVER_CHANNEL_BINDING = 0x03
  1479. def test_eap_proto_pax(dev, apdev):
  1480. """EAP-PAX protocol tests"""
  1481. def pax_std_1(ctx):
  1482. logger.info("Test: STD-1")
  1483. ctx['id'] = 10
  1484. return struct.pack(">BBHBBBBBBH8L16B", EAP_CODE_REQUEST, ctx['id'],
  1485. 4 + 1 + 5 + 2 + 32 + 16,
  1486. EAP_TYPE_PAX,
  1487. EAP_PAX_OP_STD_1, 0, EAP_PAX_MAC_HMAC_SHA1_128,
  1488. EAP_PAX_DH_GROUP_NONE, EAP_PAX_PUBLIC_KEY_NONE,
  1489. 32, 0, 0, 0, 0, 0, 0, 0, 0,
  1490. 0x16, 0xc9, 0x08, 0x9d, 0x98, 0xa5, 0x6e, 0x1f,
  1491. 0xf0, 0xac, 0xcf, 0xc4, 0x66, 0xcd, 0x2d, 0xbf)
  1492. def pax_handler(ctx, req):
  1493. logger.info("pax_handler - RX " + req.encode("hex"))
  1494. if 'num' not in ctx:
  1495. ctx['num'] = 0
  1496. ctx['num'] = ctx['num'] + 1
  1497. if 'id' not in ctx:
  1498. ctx['id'] = 1
  1499. ctx['id'] = (ctx['id'] + 1) % 256
  1500. idx = 0
  1501. idx += 1
  1502. if ctx['num'] == idx:
  1503. logger.info("Test: Missing payload")
  1504. return struct.pack(">BBHB", EAP_CODE_REQUEST, ctx['id'],
  1505. 4 + 1,
  1506. EAP_TYPE_PAX)
  1507. idx += 1
  1508. if ctx['num'] == idx:
  1509. logger.info("Test: Minimum length payload")
  1510. return struct.pack(">BBHB4L", EAP_CODE_REQUEST, ctx['id'],
  1511. 4 + 1 + 16,
  1512. EAP_TYPE_PAX,
  1513. 0, 0, 0, 0)
  1514. idx += 1
  1515. if ctx['num'] == idx:
  1516. logger.info("Test: Unsupported MAC ID")
  1517. return struct.pack(">BBHBBBBBB4L", EAP_CODE_REQUEST, ctx['id'],
  1518. 4 + 1 + 5 + 16,
  1519. EAP_TYPE_PAX,
  1520. EAP_PAX_OP_STD_1, 0, 255, EAP_PAX_DH_GROUP_NONE,
  1521. EAP_PAX_PUBLIC_KEY_NONE,
  1522. 0, 0, 0, 0)
  1523. idx += 1
  1524. if ctx['num'] == idx:
  1525. logger.info("Test: Unsupported DH Group ID")
  1526. return struct.pack(">BBHBBBBBB4L", EAP_CODE_REQUEST, ctx['id'],
  1527. 4 + 1 + 5 + 16,
  1528. EAP_TYPE_PAX,
  1529. EAP_PAX_OP_STD_1, 0, EAP_PAX_MAC_HMAC_SHA1_128,
  1530. 255, EAP_PAX_PUBLIC_KEY_NONE,
  1531. 0, 0, 0, 0)
  1532. idx += 1
  1533. if ctx['num'] == idx:
  1534. logger.info("Test: Unsupported Public Key ID")
  1535. return struct.pack(">BBHBBBBBB4L", EAP_CODE_REQUEST, ctx['id'],
  1536. 4 + 1 + 5 + 16,
  1537. EAP_TYPE_PAX,
  1538. EAP_PAX_OP_STD_1, 0, EAP_PAX_MAC_HMAC_SHA1_128,
  1539. EAP_PAX_DH_GROUP_NONE, 255,
  1540. 0, 0, 0, 0)
  1541. idx += 1
  1542. if ctx['num'] == idx:
  1543. logger.info("Test: More fragments")
  1544. return struct.pack(">BBHBBBBBB4L", EAP_CODE_REQUEST, ctx['id'],
  1545. 4 + 1 + 5 + 16,
  1546. EAP_TYPE_PAX,
  1547. EAP_PAX_OP_STD_1, EAP_PAX_FLAGS_MF,
  1548. EAP_PAX_MAC_HMAC_SHA1_128,
  1549. EAP_PAX_DH_GROUP_NONE, EAP_PAX_PUBLIC_KEY_NONE,
  1550. 0, 0, 0, 0)
  1551. idx += 1
  1552. if ctx['num'] == idx:
  1553. logger.info("Test: Invalid ICV")
  1554. return struct.pack(">BBHBBBBBB4L", EAP_CODE_REQUEST, ctx['id'],
  1555. 4 + 1 + 5 + 16,
  1556. EAP_TYPE_PAX,
  1557. EAP_PAX_OP_STD_1, 0, EAP_PAX_MAC_HMAC_SHA1_128,
  1558. EAP_PAX_DH_GROUP_NONE, EAP_PAX_PUBLIC_KEY_NONE,
  1559. 0, 0, 0, 0)
  1560. idx += 1
  1561. if ctx['num'] == idx:
  1562. logger.info("Test: Invalid ICV in short frame")
  1563. return struct.pack(">BBHBBBBBB3L", EAP_CODE_REQUEST, ctx['id'],
  1564. 4 + 1 + 5 + 12,
  1565. EAP_TYPE_PAX,
  1566. EAP_PAX_OP_STD_1, 0, EAP_PAX_MAC_HMAC_SHA1_128,
  1567. EAP_PAX_DH_GROUP_NONE, EAP_PAX_PUBLIC_KEY_NONE,
  1568. 0, 0, 0)
  1569. idx += 1
  1570. if ctx['num'] == idx:
  1571. logger.info("Test: Correct ICV - unsupported op_code")
  1572. ctx['id'] = 10
  1573. return struct.pack(">BBHBBBBBB16B", EAP_CODE_REQUEST, ctx['id'],
  1574. 4 + 1 + 5 + 16,
  1575. EAP_TYPE_PAX,
  1576. 255, 0, EAP_PAX_MAC_HMAC_SHA1_128,
  1577. EAP_PAX_DH_GROUP_NONE, EAP_PAX_PUBLIC_KEY_NONE,
  1578. 0x90, 0x78, 0x97, 0x38, 0x29, 0x94, 0x32, 0xd4,
  1579. 0x81, 0x27, 0xe0, 0xf6, 0x3b, 0x0d, 0xb2, 0xb2)
  1580. idx += 1
  1581. if ctx['num'] == idx:
  1582. logger.info("Test: Correct ICV - CE flag in STD-1")
  1583. ctx['id'] = 10
  1584. return struct.pack(">BBHBBBBBB16B", EAP_CODE_REQUEST, ctx['id'],
  1585. 4 + 1 + 5 + 16,
  1586. EAP_TYPE_PAX,
  1587. EAP_PAX_OP_STD_1, EAP_PAX_FLAGS_CE,
  1588. EAP_PAX_MAC_HMAC_SHA1_128,
  1589. EAP_PAX_DH_GROUP_NONE, EAP_PAX_PUBLIC_KEY_NONE,
  1590. 0x9c, 0x98, 0xb4, 0x0b, 0x94, 0x90, 0xde, 0x88,
  1591. 0xb7, 0x72, 0x63, 0x44, 0x1d, 0xe3, 0x7c, 0x5c)
  1592. idx += 1
  1593. if ctx['num'] == idx:
  1594. logger.info("Test: Correct ICV - too short STD-1 payload")
  1595. ctx['id'] = 10
  1596. return struct.pack(">BBHBBBBBB16B", EAP_CODE_REQUEST, ctx['id'],
  1597. 4 + 1 + 5 + 16,
  1598. EAP_TYPE_PAX,
  1599. EAP_PAX_OP_STD_1, 0, EAP_PAX_MAC_HMAC_SHA1_128,
  1600. EAP_PAX_DH_GROUP_NONE, EAP_PAX_PUBLIC_KEY_NONE,
  1601. 0xda, 0xab, 0x2c, 0xe7, 0x84, 0x41, 0xb5, 0x5c,
  1602. 0xee, 0xcf, 0x62, 0x03, 0xc5, 0x69, 0xcb, 0xf4)
  1603. idx += 1
  1604. if ctx['num'] == idx:
  1605. logger.info("Test: Correct ICV - incorrect A length in STD-1")
  1606. ctx['id'] = 10
  1607. return struct.pack(">BBHBBBBBBH8L16B", EAP_CODE_REQUEST, ctx['id'],
  1608. 4 + 1 + 5 + 2 + 32 + 16,
  1609. EAP_TYPE_PAX,
  1610. EAP_PAX_OP_STD_1, 0, EAP_PAX_MAC_HMAC_SHA1_128,
  1611. EAP_PAX_DH_GROUP_NONE, EAP_PAX_PUBLIC_KEY_NONE,
  1612. 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1613. 0xc4, 0xb0, 0x81, 0xe4, 0x6c, 0x8c, 0x20, 0x23,
  1614. 0x60, 0x46, 0x89, 0xea, 0x94, 0x60, 0xf3, 0x2a)
  1615. idx += 1
  1616. if ctx['num'] == idx:
  1617. logger.info("Test: Correct ICV - extra data in STD-1")
  1618. ctx['id'] = 10
  1619. return struct.pack(">BBHBBBBBBH8LB16B", EAP_CODE_REQUEST, ctx['id'],
  1620. 4 + 1 + 5 + 2 + 32 + 1 + 16,
  1621. EAP_TYPE_PAX,
  1622. EAP_PAX_OP_STD_1, 0, EAP_PAX_MAC_HMAC_SHA1_128,
  1623. EAP_PAX_DH_GROUP_NONE, EAP_PAX_PUBLIC_KEY_NONE,
  1624. 32, 0, 0, 0, 0, 0, 0, 0, 0,
  1625. 1,
  1626. 0x61, 0x49, 0x65, 0x37, 0x21, 0xe8, 0xd8, 0xbf,
  1627. 0xf3, 0x02, 0x01, 0xe5, 0x42, 0x51, 0xd3, 0x34)
  1628. idx += 1
  1629. if ctx['num'] == idx:
  1630. logger.info("Test: Unexpected STD-1")
  1631. return struct.pack(">BBHBBBBBBH8L16B", EAP_CODE_REQUEST, ctx['id'],
  1632. 4 + 1 + 5 + 2 + 32 + 16,
  1633. EAP_TYPE_PAX,
  1634. EAP_PAX_OP_STD_1, 0, EAP_PAX_MAC_HMAC_SHA1_128,
  1635. EAP_PAX_DH_GROUP_NONE, EAP_PAX_PUBLIC_KEY_NONE,
  1636. 32, 0, 0, 0, 0, 0, 0, 0, 0,
  1637. 0xe5, 0x1d, 0xbf, 0xb8, 0x70, 0x20, 0x5c, 0xba,
  1638. 0x41, 0xbb, 0x34, 0xda, 0x1a, 0x08, 0xe6, 0x8d)
  1639. idx += 1
  1640. if ctx['num'] == idx:
  1641. return pax_std_1(ctx)
  1642. idx += 1
  1643. if ctx['num'] == idx:
  1644. logger.info("Test: MAC ID changed during session")
  1645. return struct.pack(">BBHBBBBBBH8L16B", EAP_CODE_REQUEST, ctx['id'],
  1646. 4 + 1 + 5 + 2 + 32 + 16,
  1647. EAP_TYPE_PAX,
  1648. EAP_PAX_OP_STD_1, 0, EAP_PAX_HMAC_SHA256_128,
  1649. EAP_PAX_DH_GROUP_NONE, EAP_PAX_PUBLIC_KEY_NONE,
  1650. 32, 0, 0, 0, 0, 0, 0, 0, 0,
  1651. 0xee, 0x00, 0xbf, 0xb8, 0x70, 0x20, 0x5c, 0xba,
  1652. 0x41, 0xbb, 0x34, 0xda, 0x1a, 0x08, 0xe6, 0x8d)
  1653. idx += 1
  1654. if ctx['num'] == idx:
  1655. return pax_std_1(ctx)
  1656. idx += 1
  1657. if ctx['num'] == idx:
  1658. logger.info("Test: DH Group ID changed during session")
  1659. return struct.pack(">BBHBBBBBBH8L16B", EAP_CODE_REQUEST, ctx['id'],
  1660. 4 + 1 + 5 + 2 + 32 + 16,
  1661. EAP_TYPE_PAX,
  1662. EAP_PAX_OP_STD_1, 0, EAP_PAX_MAC_HMAC_SHA1_128,
  1663. EAP_PAX_DH_GROUP_2048_MODP,
  1664. EAP_PAX_PUBLIC_KEY_NONE,
  1665. 32, 0, 0, 0, 0, 0, 0, 0, 0,
  1666. 0xee, 0x01, 0xbf, 0xb8, 0x70, 0x20, 0x5c, 0xba,
  1667. 0x41, 0xbb, 0x34, 0xda, 0x1a, 0x08, 0xe6, 0x8d)
  1668. idx += 1
  1669. if ctx['num'] == idx:
  1670. return pax_std_1(ctx)
  1671. idx += 1
  1672. if ctx['num'] == idx:
  1673. logger.info("Test: Public Key ID changed during session")
  1674. return struct.pack(">BBHBBBBBBH8L16B", EAP_CODE_REQUEST, ctx['id'],
  1675. 4 + 1 + 5 + 2 + 32 + 16,
  1676. EAP_TYPE_PAX,
  1677. EAP_PAX_OP_STD_1, 0, EAP_PAX_MAC_HMAC_SHA1_128,
  1678. EAP_PAX_DH_GROUP_NONE,
  1679. EAP_PAX_PUBLIC_KEY_RSAES_OAEP,
  1680. 32, 0, 0, 0, 0, 0, 0, 0, 0,
  1681. 0xee, 0x02, 0xbf, 0xb8, 0x70, 0x20, 0x5c, 0xba,
  1682. 0x41, 0xbb, 0x34, 0xda, 0x1a, 0x08, 0xe6, 0x8d)
  1683. idx += 1
  1684. if ctx['num'] == idx:
  1685. logger.info("Test: Unexpected STD-3")
  1686. ctx['id'] = 10
  1687. return struct.pack(">BBHBBBBBBH8L16B", EAP_CODE_REQUEST, ctx['id'],
  1688. 4 + 1 + 5 + 2 + 32 + 16,
  1689. EAP_TYPE_PAX,
  1690. EAP_PAX_OP_STD_3, 0, EAP_PAX_MAC_HMAC_SHA1_128,
  1691. EAP_PAX_DH_GROUP_NONE, EAP_PAX_PUBLIC_KEY_NONE,
  1692. 32, 0, 0, 0, 0, 0, 0, 0, 0,
  1693. 0x47, 0xbb, 0xc0, 0xf9, 0xb9, 0x69, 0xf5, 0xcb,
  1694. 0x3a, 0xe8, 0xe7, 0xd6, 0x80, 0x28, 0xf2, 0x59)
  1695. idx += 1
  1696. if ctx['num'] == idx:
  1697. return pax_std_1(ctx)
  1698. idx += 1
  1699. if ctx['num'] == idx:
  1700. # TODO: MAC calculation; for now, this gets dropped due to incorrect
  1701. # ICV
  1702. logger.info("Test: STD-3 with CE flag")
  1703. return struct.pack(">BBHBBBBBBH8L16B", EAP_CODE_REQUEST, ctx['id'],
  1704. 4 + 1 + 5 + 2 + 32 + 16,
  1705. EAP_TYPE_PAX,
  1706. EAP_PAX_OP_STD_3, EAP_PAX_FLAGS_CE,
  1707. EAP_PAX_MAC_HMAC_SHA1_128,
  1708. EAP_PAX_DH_GROUP_NONE, EAP_PAX_PUBLIC_KEY_NONE,
  1709. 32, 0, 0, 0, 0, 0, 0, 0, 0,
  1710. 0x8a, 0xc2, 0xf9, 0xf4, 0x8b, 0x75, 0x72, 0xa2,
  1711. 0x4d, 0xd3, 0x1e, 0x54, 0x77, 0x04, 0x05, 0xe2)
  1712. idx += 1
  1713. if ctx['num'] & 0x1 == idx & 0x1:
  1714. logger.info("Test: Default request")
  1715. return struct.pack(">BBHB", EAP_CODE_REQUEST, ctx['id'],
  1716. 4 + 1,
  1717. EAP_TYPE_PAX)
  1718. else:
  1719. logger.info("Test: Default EAP-Failure")
  1720. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  1721. srv = start_radius_server(pax_handler)
  1722. try:
  1723. hapd = start_ap(apdev[0]['ifname'])
  1724. for i in range(0, 18):
  1725. dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412",
  1726. eap="PAX", identity="user",
  1727. password_hex="0123456789abcdef0123456789abcdef",
  1728. wait_connect=False)
  1729. logger.info("Waiting for EAP method to start")
  1730. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"],
  1731. timeout=15)
  1732. if ev is None:
  1733. raise Exception("Timeout on EAP start")
  1734. time.sleep(0.05)
  1735. dev[0].request("REMOVE_NETWORK all")
  1736. dev[0].dump_monitor()
  1737. logger.info("Too short password")
  1738. dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412",
  1739. eap="PAX", identity="user",
  1740. password_hex="0123456789abcdef0123456789abcd",
  1741. wait_connect=False)
  1742. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=15)
  1743. if ev is None:
  1744. raise Exception("Timeout on EAP start")
  1745. time.sleep(0.1)
  1746. dev[0].request("REMOVE_NETWORK all")
  1747. dev[0].dump_monitor()
  1748. logger.info("No password")
  1749. dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412",
  1750. eap="PAX", identity="user",
  1751. wait_connect=False)
  1752. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=15)
  1753. if ev is None:
  1754. raise Exception("Timeout on EAP start")
  1755. time.sleep(0.1)
  1756. dev[0].request("REMOVE_NETWORK all")
  1757. dev[0].dump_monitor()
  1758. finally:
  1759. stop_radius_server(srv)
  1760. def test_eap_proto_psk(dev, apdev):
  1761. """EAP-PSK protocol tests"""
  1762. def psk_handler(ctx, req):
  1763. logger.info("psk_handler - RX " + req.encode("hex"))
  1764. if 'num' not in ctx:
  1765. ctx['num'] = 0
  1766. ctx['num'] = ctx['num'] + 1
  1767. if 'id' not in ctx:
  1768. ctx['id'] = 1
  1769. ctx['id'] = (ctx['id'] + 1) % 256
  1770. idx = 0
  1771. idx += 1
  1772. if ctx['num'] == idx:
  1773. logger.info("Test: Missing payload")
  1774. return struct.pack(">BBHB", EAP_CODE_REQUEST, ctx['id'],
  1775. 4 + 1,
  1776. EAP_TYPE_PSK)
  1777. idx += 1
  1778. if ctx['num'] == idx:
  1779. logger.info("Test: Non-zero T in first message")
  1780. return struct.pack(">BBHBB4L", EAP_CODE_REQUEST, ctx['id'],
  1781. 4 + 1 + 1 + 16,
  1782. EAP_TYPE_PSK, 0xc0, 0, 0, 0, 0)
  1783. idx += 1
  1784. if ctx['num'] == idx:
  1785. logger.info("Test: Valid first message")
  1786. return struct.pack(">BBHBB4L", EAP_CODE_REQUEST, ctx['id'],
  1787. 4 + 1 + 1 + 16,
  1788. EAP_TYPE_PSK, 0, 0, 0, 0, 0)
  1789. idx += 1
  1790. if ctx['num'] == idx:
  1791. logger.info("Test: Too short third message")
  1792. return struct.pack(">BBHB", EAP_CODE_REQUEST, ctx['id'],
  1793. 4 + 1,
  1794. EAP_TYPE_PSK)
  1795. idx += 1
  1796. if ctx['num'] == idx:
  1797. logger.info("Test: Valid first message")
  1798. return struct.pack(">BBHBB4L", EAP_CODE_REQUEST, ctx['id'],
  1799. 4 + 1 + 1 + 16,
  1800. EAP_TYPE_PSK, 0, 0, 0, 0, 0)
  1801. idx += 1
  1802. if ctx['num'] == idx:
  1803. logger.info("Test: Incorrect T in third message")
  1804. return struct.pack(">BBHBB4L4L", EAP_CODE_REQUEST, ctx['id'],
  1805. 4 + 1 + 1 + 16 + 16,
  1806. EAP_TYPE_PSK, 0, 0, 0, 0, 0, 0, 0, 0, 0)
  1807. idx += 1
  1808. if ctx['num'] == idx:
  1809. logger.info("Test: Valid first message")
  1810. return struct.pack(">BBHBB4L", EAP_CODE_REQUEST, ctx['id'],
  1811. 4 + 1 + 1 + 16,
  1812. EAP_TYPE_PSK, 0, 0, 0, 0, 0)
  1813. idx += 1
  1814. if ctx['num'] == idx:
  1815. logger.info("Test: Missing PCHANNEL in third message")
  1816. return struct.pack(">BBHBB4L4L", EAP_CODE_REQUEST, ctx['id'],
  1817. 4 + 1 + 1 + 16 + 16,
  1818. EAP_TYPE_PSK, 0x80, 0, 0, 0, 0, 0, 0, 0, 0)
  1819. idx += 1
  1820. if ctx['num'] == idx:
  1821. logger.info("Test: Valid first message")
  1822. return struct.pack(">BBHBB4L", EAP_CODE_REQUEST, ctx['id'],
  1823. 4 + 1 + 1 + 16,
  1824. EAP_TYPE_PSK, 0, 0, 0, 0, 0)
  1825. idx += 1
  1826. if ctx['num'] == idx:
  1827. logger.info("Test: Invalic MAC_S in third message")
  1828. return struct.pack(">BBHBB4L4L5LB", EAP_CODE_REQUEST, ctx['id'],
  1829. 4 + 1 + 1 + 16 + 16 + 21,
  1830. EAP_TYPE_PSK, 0x80, 0, 0, 0, 0, 0, 0, 0, 0,
  1831. 0, 0, 0, 0, 0, 0)
  1832. idx += 1
  1833. if ctx['num'] == idx:
  1834. logger.info("Test: Valid first message")
  1835. return struct.pack(">BBHBB4L", EAP_CODE_REQUEST, ctx['id'],
  1836. 4 + 1 + 1 + 16,
  1837. EAP_TYPE_PSK, 0, 0, 0, 0, 0)
  1838. idx += 1
  1839. if ctx['num'] == idx:
  1840. logger.info("Test: EAP-Failure")
  1841. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  1842. return None
  1843. srv = start_radius_server(psk_handler)
  1844. try:
  1845. hapd = start_ap(apdev[0]['ifname'])
  1846. for i in range(0, 6):
  1847. dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412",
  1848. eap="PSK", identity="user",
  1849. password_hex="0123456789abcdef0123456789abcdef",
  1850. wait_connect=False)
  1851. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"],
  1852. timeout=15)
  1853. if ev is None:
  1854. raise Exception("Timeout on EAP start")
  1855. time.sleep(0.1)
  1856. dev[0].request("REMOVE_NETWORK all")
  1857. logger.info("Test: Invalid PSK length")
  1858. dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412",
  1859. eap="PSK", identity="user",
  1860. password_hex="0123456789abcdef0123456789abcd",
  1861. wait_connect=False)
  1862. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"],
  1863. timeout=15)
  1864. if ev is None:
  1865. raise Exception("Timeout on EAP start")
  1866. time.sleep(0.1)
  1867. dev[0].request("REMOVE_NETWORK all")
  1868. finally:
  1869. stop_radius_server(srv)
  1870. EAP_SIM_SUBTYPE_START = 10
  1871. EAP_SIM_SUBTYPE_CHALLENGE = 11
  1872. EAP_SIM_SUBTYPE_NOTIFICATION = 12
  1873. EAP_SIM_SUBTYPE_REAUTHENTICATION = 13
  1874. EAP_SIM_SUBTYPE_CLIENT_ERROR = 14
  1875. EAP_AKA_SUBTYPE_CHALLENGE = 1
  1876. EAP_AKA_SUBTYPE_AUTHENTICATION_REJECT = 2
  1877. EAP_AKA_SUBTYPE_SYNCHRONIZATION_FAILURE = 4
  1878. EAP_AKA_SUBTYPE_IDENTITY = 5
  1879. EAP_AKA_SUBTYPE_NOTIFICATION = 12
  1880. EAP_AKA_SUBTYPE_REAUTHENTICATION = 13
  1881. EAP_AKA_SUBTYPE_CLIENT_ERROR = 14
  1882. EAP_SIM_AT_RAND = 1
  1883. EAP_SIM_AT_AUTN = 2
  1884. EAP_SIM_AT_RES = 3
  1885. EAP_SIM_AT_AUTS = 4
  1886. EAP_SIM_AT_PADDING = 6
  1887. EAP_SIM_AT_NONCE_MT = 7
  1888. EAP_SIM_AT_PERMANENT_ID_REQ = 10
  1889. EAP_SIM_AT_MAC = 11
  1890. EAP_SIM_AT_NOTIFICATION = 12
  1891. EAP_SIM_AT_ANY_ID_REQ = 13
  1892. EAP_SIM_AT_IDENTITY = 14
  1893. EAP_SIM_AT_VERSION_LIST = 15
  1894. EAP_SIM_AT_SELECTED_VERSION = 16
  1895. EAP_SIM_AT_FULLAUTH_ID_REQ = 17
  1896. EAP_SIM_AT_COUNTER = 19
  1897. EAP_SIM_AT_COUNTER_TOO_SMALL = 20
  1898. EAP_SIM_AT_NONCE_S = 21
  1899. EAP_SIM_AT_CLIENT_ERROR_CODE = 22
  1900. EAP_SIM_AT_KDF_INPUT = 23
  1901. EAP_SIM_AT_KDF = 24
  1902. EAP_SIM_AT_IV = 129
  1903. EAP_SIM_AT_ENCR_DATA = 130
  1904. EAP_SIM_AT_NEXT_PSEUDONYM = 132
  1905. EAP_SIM_AT_NEXT_REAUTH_ID = 133
  1906. EAP_SIM_AT_CHECKCODE = 134
  1907. EAP_SIM_AT_RESULT_IND = 135
  1908. EAP_SIM_AT_BIDDING = 136
  1909. def test_eap_proto_aka(dev, apdev):
  1910. """EAP-AKA protocol tests"""
  1911. def aka_handler(ctx, req):
  1912. logger.info("aka_handler - RX " + req.encode("hex"))
  1913. if 'num' not in ctx:
  1914. ctx['num'] = 0
  1915. ctx['num'] = ctx['num'] + 1
  1916. if 'id' not in ctx:
  1917. ctx['id'] = 1
  1918. ctx['id'] = (ctx['id'] + 1) % 256
  1919. idx = 0
  1920. idx += 1
  1921. if ctx['num'] == idx:
  1922. logger.info("Test: Missing payload")
  1923. return struct.pack(">BBHB", EAP_CODE_REQUEST, ctx['id'],
  1924. 4 + 1,
  1925. EAP_TYPE_AKA)
  1926. idx += 1
  1927. if ctx['num'] == idx:
  1928. logger.info("Test: Unknown subtype")
  1929. return struct.pack(">BBHBBH", EAP_CODE_REQUEST, ctx['id'],
  1930. 4 + 1 + 3,
  1931. EAP_TYPE_AKA, 255, 0)
  1932. idx += 1
  1933. if ctx['num'] == idx:
  1934. logger.info("Test: EAP-Failure")
  1935. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  1936. idx += 1
  1937. if ctx['num'] == idx:
  1938. logger.info("Test: Client Error")
  1939. return struct.pack(">BBHBBH", EAP_CODE_REQUEST, ctx['id'],
  1940. 4 + 1 + 3,
  1941. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_CLIENT_ERROR, 0)
  1942. idx += 1
  1943. if ctx['num'] == idx:
  1944. logger.info("Test: EAP-Failure")
  1945. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  1946. idx += 1
  1947. if ctx['num'] == idx:
  1948. logger.info("Test: Too short attribute header")
  1949. return struct.pack(">BBHBBHB", EAP_CODE_REQUEST, ctx['id'],
  1950. 4 + 1 + 1 + 3,
  1951. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0, 255)
  1952. idx += 1
  1953. if ctx['num'] == idx:
  1954. logger.info("Test: EAP-Failure")
  1955. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  1956. idx += 1
  1957. if ctx['num'] == idx:
  1958. logger.info("Test: Truncated attribute")
  1959. return struct.pack(">BBHBBHBB", EAP_CODE_REQUEST, ctx['id'],
  1960. 4 + 1 + 1 + 4,
  1961. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0, 255,
  1962. 255)
  1963. idx += 1
  1964. if ctx['num'] == idx:
  1965. logger.info("Test: EAP-Failure")
  1966. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  1967. idx += 1
  1968. if ctx['num'] == idx:
  1969. logger.info("Test: Too short attribute data")
  1970. return struct.pack(">BBHBBHBB", EAP_CODE_REQUEST, ctx['id'],
  1971. 4 + 1 + 1 + 4,
  1972. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0, 255,
  1973. 0)
  1974. idx += 1
  1975. if ctx['num'] == idx:
  1976. logger.info("Test: EAP-Failure")
  1977. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  1978. idx += 1
  1979. if ctx['num'] == idx:
  1980. logger.info("Test: Skippable/non-skippable unrecognzized attribute")
  1981. return struct.pack(">BBHBBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  1982. 4 + 1 + 1 + 10,
  1983. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  1984. 255, 1, 0, 127, 1, 0)
  1985. idx += 1
  1986. if ctx['num'] == idx:
  1987. logger.info("Test: EAP-Failure")
  1988. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  1989. idx += 1
  1990. if ctx['num'] == idx:
  1991. logger.info("Test: Identity request without ID type")
  1992. return struct.pack(">BBHBBH", EAP_CODE_REQUEST, ctx['id'],
  1993. 4 + 1 + 3,
  1994. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0)
  1995. idx += 1
  1996. if ctx['num'] == idx:
  1997. logger.info("Test: Identity request ANY_ID")
  1998. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  1999. 4 + 1 + 3 + 4,
  2000. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2001. EAP_SIM_AT_ANY_ID_REQ, 1, 0)
  2002. idx += 1
  2003. if ctx['num'] == idx:
  2004. logger.info("Test: Identity request ANY_ID (duplicate)")
  2005. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2006. 4 + 1 + 3 + 4,
  2007. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2008. EAP_SIM_AT_ANY_ID_REQ, 1, 0)
  2009. idx += 1
  2010. if ctx['num'] == idx:
  2011. logger.info("Test: EAP-Failure")
  2012. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2013. idx += 1
  2014. if ctx['num'] == idx:
  2015. logger.info("Test: Identity request ANY_ID")
  2016. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2017. 4 + 1 + 3 + 4,
  2018. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2019. EAP_SIM_AT_ANY_ID_REQ, 1, 0)
  2020. idx += 1
  2021. if ctx['num'] == idx:
  2022. logger.info("Test: Identity request FULLAUTH_ID")
  2023. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2024. 4 + 1 + 3 + 4,
  2025. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2026. EAP_SIM_AT_FULLAUTH_ID_REQ, 1, 0)
  2027. idx += 1
  2028. if ctx['num'] == idx:
  2029. logger.info("Test: Identity request FULLAUTH_ID (duplicate)")
  2030. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2031. 4 + 1 + 3 + 4,
  2032. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2033. EAP_SIM_AT_FULLAUTH_ID_REQ, 1, 0)
  2034. idx += 1
  2035. if ctx['num'] == idx:
  2036. logger.info("Test: EAP-Failure")
  2037. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2038. idx += 1
  2039. if ctx['num'] == idx:
  2040. logger.info("Test: Identity request ANY_ID")
  2041. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2042. 4 + 1 + 3 + 4,
  2043. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2044. EAP_SIM_AT_ANY_ID_REQ, 1, 0)
  2045. idx += 1
  2046. if ctx['num'] == idx:
  2047. logger.info("Test: Identity request FULLAUTH_ID")
  2048. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2049. 4 + 1 + 3 + 4,
  2050. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2051. EAP_SIM_AT_FULLAUTH_ID_REQ, 1, 0)
  2052. idx += 1
  2053. if ctx['num'] == idx:
  2054. logger.info("Test: Identity request PERMANENT_ID")
  2055. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2056. 4 + 1 + 3 + 4,
  2057. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2058. EAP_SIM_AT_PERMANENT_ID_REQ, 1, 0)
  2059. idx += 1
  2060. if ctx['num'] == idx:
  2061. logger.info("Test: Identity request PERMANENT_ID (duplicate)")
  2062. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2063. 4 + 1 + 3 + 4,
  2064. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2065. EAP_SIM_AT_PERMANENT_ID_REQ, 1, 0)
  2066. idx += 1
  2067. if ctx['num'] == idx:
  2068. logger.info("Test: EAP-Failure")
  2069. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2070. idx += 1
  2071. if ctx['num'] == idx:
  2072. logger.info("Test: Challenge with no attributes")
  2073. return struct.pack(">BBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2074. 4 + 1 + 3,
  2075. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_CHALLENGE, 0)
  2076. idx += 1
  2077. if ctx['num'] == idx:
  2078. logger.info("Test: EAP-Failure")
  2079. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2080. idx += 1
  2081. if ctx['num'] == idx:
  2082. logger.info("Test: AKA Challenge with BIDDING")
  2083. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2084. 4 + 1 + 3 + 4,
  2085. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_CHALLENGE, 0,
  2086. EAP_SIM_AT_BIDDING, 1, 0x8000)
  2087. idx += 1
  2088. if ctx['num'] == idx:
  2089. logger.info("Test: EAP-Failure")
  2090. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2091. idx += 1
  2092. if ctx['num'] == idx:
  2093. logger.info("Test: Notification with no attributes")
  2094. return struct.pack(">BBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2095. 4 + 1 + 3,
  2096. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_NOTIFICATION, 0)
  2097. idx += 1
  2098. if ctx['num'] == idx:
  2099. logger.info("Test: EAP-Failure")
  2100. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2101. idx += 1
  2102. if ctx['num'] == idx:
  2103. logger.info("Test: Notification indicating success, but no MAC")
  2104. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2105. 4 + 1 + 3 + 4,
  2106. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_NOTIFICATION, 0,
  2107. EAP_SIM_AT_NOTIFICATION, 1, 32768)
  2108. idx += 1
  2109. if ctx['num'] == idx:
  2110. logger.info("Test: EAP-Failure")
  2111. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2112. idx += 1
  2113. if ctx['num'] == idx:
  2114. logger.info("Test: Notification indicating success, but invalid MAC value")
  2115. return struct.pack(">BBHBBHBBHBBH4L", EAP_CODE_REQUEST, ctx['id'],
  2116. 4 + 1 + 3 + 4 + 20,
  2117. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_NOTIFICATION, 0,
  2118. EAP_SIM_AT_NOTIFICATION, 1, 32768,
  2119. EAP_SIM_AT_MAC, 5, 0, 0, 0, 0, 0)
  2120. idx += 1
  2121. if ctx['num'] == idx:
  2122. logger.info("Test: EAP-Failure")
  2123. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2124. idx += 1
  2125. if ctx['num'] == idx:
  2126. logger.info("Test: Notification indicating success with zero-key MAC")
  2127. return struct.pack(">BBHBBHBBHBBH16B", EAP_CODE_REQUEST,
  2128. ctx['id'] - 2,
  2129. 4 + 1 + 3 + 4 + 20,
  2130. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_NOTIFICATION, 0,
  2131. EAP_SIM_AT_NOTIFICATION, 1, 32768,
  2132. EAP_SIM_AT_MAC, 5, 0,
  2133. 0xbe, 0x2e, 0xbb, 0xa9, 0xfa, 0x2e, 0x82, 0x36,
  2134. 0x37, 0x8c, 0x32, 0x41, 0xb7, 0xc7, 0x58, 0xa3)
  2135. idx += 1
  2136. if ctx['num'] == idx:
  2137. logger.info("Test: EAP-Success")
  2138. return struct.pack(">BBH", EAP_CODE_SUCCESS, ctx['id'], 4)
  2139. idx += 1
  2140. if ctx['num'] == idx:
  2141. logger.info("Test: Notification before auth")
  2142. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2143. 4 + 1 + 3 + 4,
  2144. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_NOTIFICATION, 0,
  2145. EAP_SIM_AT_NOTIFICATION, 1, 16384)
  2146. idx += 1
  2147. if ctx['num'] == idx:
  2148. logger.info("Test: EAP-Failure")
  2149. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2150. idx += 1
  2151. if ctx['num'] == idx:
  2152. logger.info("Test: Notification before auth")
  2153. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2154. 4 + 1 + 3 + 4,
  2155. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_NOTIFICATION, 0,
  2156. EAP_SIM_AT_NOTIFICATION, 1, 16385)
  2157. idx += 1
  2158. if ctx['num'] == idx:
  2159. logger.info("Test: EAP-Failure")
  2160. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2161. idx += 1
  2162. if ctx['num'] == idx:
  2163. logger.info("Test: Notification with unrecognized non-failure")
  2164. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2165. 4 + 1 + 3 + 4,
  2166. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_NOTIFICATION, 0,
  2167. EAP_SIM_AT_NOTIFICATION, 1, 0xc000)
  2168. idx += 1
  2169. if ctx['num'] == idx:
  2170. logger.info("Test: Notification before auth (duplicate)")
  2171. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2172. 4 + 1 + 3 + 4,
  2173. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_NOTIFICATION, 0,
  2174. EAP_SIM_AT_NOTIFICATION, 1, 0xc000)
  2175. idx += 1
  2176. if ctx['num'] == idx:
  2177. logger.info("Test: EAP-Failure")
  2178. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2179. idx += 1
  2180. if ctx['num'] == idx:
  2181. logger.info("Test: Re-authentication (unexpected) with no attributes")
  2182. return struct.pack(">BBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2183. 4 + 1 + 3,
  2184. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_REAUTHENTICATION,
  2185. 0)
  2186. idx += 1
  2187. if ctx['num'] == idx:
  2188. logger.info("Test: EAP-Failure")
  2189. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2190. idx += 1
  2191. if ctx['num'] == idx:
  2192. logger.info("Test: AKA Challenge with Checkcode claiming identity round was used")
  2193. return struct.pack(">BBHBBHBBH5L", EAP_CODE_REQUEST, ctx['id'],
  2194. 4 + 1 + 3 + 24,
  2195. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_CHALLENGE, 0,
  2196. EAP_SIM_AT_CHECKCODE, 6, 0, 0, 0, 0, 0, 0)
  2197. idx += 1
  2198. if ctx['num'] == idx:
  2199. logger.info("Test: EAP-Failure")
  2200. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2201. idx += 1
  2202. if ctx['num'] == idx:
  2203. logger.info("Test: Identity request ANY_ID")
  2204. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2205. 4 + 1 + 3 + 4,
  2206. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2207. EAP_SIM_AT_ANY_ID_REQ, 1, 0)
  2208. idx += 1
  2209. if ctx['num'] == idx:
  2210. logger.info("Test: AKA Challenge with Checkcode claiming no identity round was used")
  2211. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2212. 4 + 1 + 3 + 4,
  2213. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_CHALLENGE, 0,
  2214. EAP_SIM_AT_CHECKCODE, 1, 0)
  2215. idx += 1
  2216. if ctx['num'] == idx:
  2217. logger.info("Test: EAP-Failure")
  2218. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2219. idx += 1
  2220. if ctx['num'] == idx:
  2221. logger.info("Test: Identity request ANY_ID")
  2222. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2223. 4 + 1 + 3 + 4,
  2224. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2225. EAP_SIM_AT_ANY_ID_REQ, 1, 0)
  2226. idx += 1
  2227. if ctx['num'] == idx:
  2228. logger.info("Test: AKA Challenge with mismatching Checkcode value")
  2229. return struct.pack(">BBHBBHBBH5L", EAP_CODE_REQUEST, ctx['id'],
  2230. 4 + 1 + 3 + 24,
  2231. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_CHALLENGE, 0,
  2232. EAP_SIM_AT_CHECKCODE, 6, 0, 0, 0, 0, 0, 0)
  2233. idx += 1
  2234. if ctx['num'] == idx:
  2235. logger.info("Test: EAP-Failure")
  2236. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2237. idx += 1
  2238. if ctx['num'] == idx:
  2239. logger.info("Test: Re-authentication (unexpected) with Checkcode claimin identity round was used")
  2240. return struct.pack(">BBHBBHBBH5L", EAP_CODE_REQUEST, ctx['id'],
  2241. 4 + 1 + 3 + 24,
  2242. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_REAUTHENTICATION,
  2243. 0,
  2244. EAP_SIM_AT_CHECKCODE, 6, 0, 0, 0, 0, 0, 0)
  2245. idx += 1
  2246. if ctx['num'] == idx:
  2247. logger.info("Test: EAP-Failure")
  2248. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2249. idx += 1
  2250. if ctx['num'] == idx:
  2251. logger.info("Test: Invalid AT_RAND length")
  2252. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2253. 4 + 1 + 3 + 4,
  2254. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2255. EAP_SIM_AT_RAND, 1, 0)
  2256. idx += 1
  2257. if ctx['num'] == idx:
  2258. logger.info("Test: EAP-Failure")
  2259. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2260. idx += 1
  2261. if ctx['num'] == idx:
  2262. logger.info("Test: Invalid AT_AUTN length")
  2263. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2264. 4 + 1 + 3 + 4,
  2265. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2266. EAP_SIM_AT_AUTN, 1, 0)
  2267. idx += 1
  2268. if ctx['num'] == idx:
  2269. logger.info("Test: EAP-Failure")
  2270. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2271. idx += 1
  2272. if ctx['num'] == idx:
  2273. logger.info("Test: Unencrypted AT_PADDING")
  2274. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2275. 4 + 1 + 3 + 4,
  2276. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2277. EAP_SIM_AT_PADDING, 1, 0)
  2278. idx += 1
  2279. if ctx['num'] == idx:
  2280. logger.info("Test: EAP-Failure")
  2281. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2282. idx += 1
  2283. if ctx['num'] == idx:
  2284. logger.info("Test: Invalid AT_NONCE_MT length")
  2285. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2286. 4 + 1 + 3 + 4,
  2287. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2288. EAP_SIM_AT_NONCE_MT, 1, 0)
  2289. idx += 1
  2290. if ctx['num'] == idx:
  2291. logger.info("Test: EAP-Failure")
  2292. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2293. idx += 1
  2294. if ctx['num'] == idx:
  2295. logger.info("Test: Invalid AT_MAC length")
  2296. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2297. 4 + 1 + 3 + 4,
  2298. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2299. EAP_SIM_AT_MAC, 1, 0)
  2300. idx += 1
  2301. if ctx['num'] == idx:
  2302. logger.info("Test: EAP-Failure")
  2303. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2304. idx += 1
  2305. if ctx['num'] == idx:
  2306. logger.info("Test: Invalid AT_NOTIFICATION length")
  2307. return struct.pack(">BBHBBHBBHL", EAP_CODE_REQUEST, ctx['id'],
  2308. 4 + 1 + 3 + 8,
  2309. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2310. EAP_SIM_AT_NOTIFICATION, 2, 0, 0)
  2311. idx += 1
  2312. if ctx['num'] == idx:
  2313. logger.info("Test: EAP-Failure")
  2314. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2315. idx += 1
  2316. if ctx['num'] == idx:
  2317. logger.info("Test: AT_IDENTITY overflow")
  2318. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2319. 4 + 1 + 3 + 4,
  2320. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2321. EAP_SIM_AT_IDENTITY, 1, 0xffff)
  2322. idx += 1
  2323. if ctx['num'] == idx:
  2324. logger.info("Test: EAP-Failure")
  2325. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2326. idx += 1
  2327. if ctx['num'] == idx:
  2328. logger.info("Test: Unexpected AT_VERSION_LIST")
  2329. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2330. 4 + 1 + 3 + 4,
  2331. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2332. EAP_SIM_AT_VERSION_LIST, 1, 0)
  2333. idx += 1
  2334. if ctx['num'] == idx:
  2335. logger.info("Test: EAP-Failure")
  2336. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2337. idx += 1
  2338. if ctx['num'] == idx:
  2339. logger.info("Test: Invalid AT_SELECTED_VERSION length")
  2340. return struct.pack(">BBHBBHBBHL", EAP_CODE_REQUEST, ctx['id'],
  2341. 4 + 1 + 3 + 8,
  2342. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2343. EAP_SIM_AT_SELECTED_VERSION, 2, 0, 0)
  2344. idx += 1
  2345. if ctx['num'] == idx:
  2346. logger.info("Test: EAP-Failure")
  2347. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2348. idx += 1
  2349. if ctx['num'] == idx:
  2350. logger.info("Test: Unencrypted AT_COUNTER")
  2351. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2352. 4 + 1 + 3 + 4,
  2353. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2354. EAP_SIM_AT_COUNTER, 1, 0)
  2355. idx += 1
  2356. if ctx['num'] == idx:
  2357. logger.info("Test: EAP-Failure")
  2358. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2359. idx += 1
  2360. if ctx['num'] == idx:
  2361. logger.info("Test: Unencrypted AT_COUNTER_TOO_SMALL")
  2362. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2363. 4 + 1 + 3 + 4,
  2364. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2365. EAP_SIM_AT_COUNTER_TOO_SMALL, 1, 0)
  2366. idx += 1
  2367. if ctx['num'] == idx:
  2368. logger.info("Test: EAP-Failure")
  2369. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2370. idx += 1
  2371. if ctx['num'] == idx:
  2372. logger.info("Test: Unencrypted AT_NONCE_S")
  2373. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2374. 4 + 1 + 3 + 4,
  2375. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2376. EAP_SIM_AT_NONCE_S, 1, 0)
  2377. idx += 1
  2378. if ctx['num'] == idx:
  2379. logger.info("Test: EAP-Failure")
  2380. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2381. idx += 1
  2382. if ctx['num'] == idx:
  2383. logger.info("Test: Invalid AT_CLIENT_ERROR_CODE length")
  2384. return struct.pack(">BBHBBHBBHL", EAP_CODE_REQUEST, ctx['id'],
  2385. 4 + 1 + 3 + 8,
  2386. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2387. EAP_SIM_AT_CLIENT_ERROR_CODE, 2, 0, 0)
  2388. idx += 1
  2389. if ctx['num'] == idx:
  2390. logger.info("Test: EAP-Failure")
  2391. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2392. idx += 1
  2393. if ctx['num'] == idx:
  2394. logger.info("Test: Invalid AT_IV length")
  2395. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2396. 4 + 1 + 3 + 4,
  2397. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2398. EAP_SIM_AT_IV, 1, 0)
  2399. idx += 1
  2400. if ctx['num'] == idx:
  2401. logger.info("Test: EAP-Failure")
  2402. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2403. idx += 1
  2404. if ctx['num'] == idx:
  2405. logger.info("Test: Invalid AT_ENCR_DATA length")
  2406. return struct.pack(">BBHBBHBBHL", EAP_CODE_REQUEST, ctx['id'],
  2407. 4 + 1 + 3 + 8,
  2408. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2409. EAP_SIM_AT_ENCR_DATA, 2, 0, 0)
  2410. idx += 1
  2411. if ctx['num'] == idx:
  2412. logger.info("Test: EAP-Failure")
  2413. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2414. idx += 1
  2415. if ctx['num'] == idx:
  2416. logger.info("Test: Unencrypted AT_NEXT_PSEUDONYM")
  2417. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2418. 4 + 1 + 3 + 4,
  2419. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2420. EAP_SIM_AT_NEXT_PSEUDONYM, 1, 0)
  2421. idx += 1
  2422. if ctx['num'] == idx:
  2423. logger.info("Test: EAP-Failure")
  2424. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2425. idx += 1
  2426. if ctx['num'] == idx:
  2427. logger.info("Test: Unencrypted AT_NEXT_REAUTH_ID")
  2428. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2429. 4 + 1 + 3 + 4,
  2430. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2431. EAP_SIM_AT_NEXT_REAUTH_ID, 1, 0)
  2432. idx += 1
  2433. if ctx['num'] == idx:
  2434. logger.info("Test: EAP-Failure")
  2435. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2436. idx += 1
  2437. if ctx['num'] == idx:
  2438. logger.info("Test: Invalid AT_RES length")
  2439. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2440. 4 + 1 + 3 + 4,
  2441. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2442. EAP_SIM_AT_RES, 1, 0)
  2443. idx += 1
  2444. if ctx['num'] == idx:
  2445. logger.info("Test: EAP-Failure")
  2446. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2447. idx += 1
  2448. if ctx['num'] == idx:
  2449. logger.info("Test: Invalid AT_RES length")
  2450. return struct.pack(">BBHBBHBBH5L", EAP_CODE_REQUEST, ctx['id'],
  2451. 4 + 1 + 3 + 24,
  2452. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2453. EAP_SIM_AT_RES, 6, 0xffff, 0, 0, 0, 0, 0)
  2454. idx += 1
  2455. if ctx['num'] == idx:
  2456. logger.info("Test: EAP-Failure")
  2457. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2458. idx += 1
  2459. if ctx['num'] == idx:
  2460. logger.info("Test: Invalid AT_AUTS length")
  2461. return struct.pack(">BBHBBHBBHL", EAP_CODE_REQUEST, ctx['id'],
  2462. 4 + 1 + 3 + 8,
  2463. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2464. EAP_SIM_AT_AUTS, 2, 0, 0)
  2465. idx += 1
  2466. if ctx['num'] == idx:
  2467. logger.info("Test: EAP-Failure")
  2468. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2469. idx += 1
  2470. if ctx['num'] == idx:
  2471. logger.info("Test: Invalid AT_CHECKCODE length")
  2472. return struct.pack(">BBHBBHBBHL", EAP_CODE_REQUEST, ctx['id'],
  2473. 4 + 1 + 3 + 8,
  2474. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2475. EAP_SIM_AT_CHECKCODE, 2, 0, 0)
  2476. idx += 1
  2477. if ctx['num'] == idx:
  2478. logger.info("Test: EAP-Failure")
  2479. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2480. idx += 1
  2481. if ctx['num'] == idx:
  2482. logger.info("Test: Invalid AT_RESULT_IND length")
  2483. return struct.pack(">BBHBBHBBHL", EAP_CODE_REQUEST, ctx['id'],
  2484. 4 + 1 + 3 + 8,
  2485. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2486. EAP_SIM_AT_RESULT_IND, 2, 0, 0)
  2487. idx += 1
  2488. if ctx['num'] == idx:
  2489. logger.info("Test: EAP-Failure")
  2490. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2491. idx += 1
  2492. if ctx['num'] == idx:
  2493. logger.info("Test: Unexpected AT_KDF_INPUT")
  2494. return struct.pack(">BBHBBHBBHL", EAP_CODE_REQUEST, ctx['id'],
  2495. 4 + 1 + 3 + 8,
  2496. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2497. EAP_SIM_AT_KDF_INPUT, 2, 0, 0)
  2498. idx += 1
  2499. if ctx['num'] == idx:
  2500. logger.info("Test: EAP-Failure")
  2501. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2502. idx += 1
  2503. if ctx['num'] == idx:
  2504. logger.info("Test: Unexpected AT_KDF")
  2505. return struct.pack(">BBHBBHBBHL", EAP_CODE_REQUEST, ctx['id'],
  2506. 4 + 1 + 3 + 8,
  2507. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2508. EAP_SIM_AT_KDF, 2, 0, 0)
  2509. idx += 1
  2510. if ctx['num'] == idx:
  2511. logger.info("Test: EAP-Failure")
  2512. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2513. idx += 1
  2514. if ctx['num'] == idx:
  2515. logger.info("Test: Invalid AT_BIDDING length")
  2516. return struct.pack(">BBHBBHBBHL", EAP_CODE_REQUEST, ctx['id'],
  2517. 4 + 1 + 3 + 8,
  2518. EAP_TYPE_AKA, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2519. EAP_SIM_AT_BIDDING, 2, 0, 0)
  2520. idx += 1
  2521. if ctx['num'] == idx:
  2522. logger.info("Test: EAP-Failure")
  2523. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2524. return None
  2525. srv = start_radius_server(aka_handler)
  2526. try:
  2527. hapd = start_ap(apdev[0]['ifname'])
  2528. for i in range(0, 49):
  2529. eap = "AKA AKA'" if i == 11 else "AKA"
  2530. dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412",
  2531. eap=eap, identity="0232010000000000",
  2532. password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581:000000000123",
  2533. wait_connect=False)
  2534. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"],
  2535. timeout=15)
  2536. if ev is None:
  2537. raise Exception("Timeout on EAP start")
  2538. if i in [ 0, 15 ]:
  2539. time.sleep(0.1)
  2540. else:
  2541. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"],
  2542. timeout=10)
  2543. if ev is None:
  2544. raise Exception("Timeout on EAP failure")
  2545. dev[0].request("REMOVE_NETWORK all")
  2546. dev[0].dump_monitor()
  2547. finally:
  2548. stop_radius_server(srv)
  2549. def test_eap_proto_aka_prime(dev, apdev):
  2550. """EAP-AKA' protocol tests"""
  2551. def aka_prime_handler(ctx, req):
  2552. logger.info("aka_prime_handler - RX " + req.encode("hex"))
  2553. if 'num' not in ctx:
  2554. ctx['num'] = 0
  2555. ctx['num'] = ctx['num'] + 1
  2556. if 'id' not in ctx:
  2557. ctx['id'] = 1
  2558. ctx['id'] = (ctx['id'] + 1) % 256
  2559. idx = 0
  2560. idx += 1
  2561. if ctx['num'] == idx:
  2562. logger.info("Test: Missing payload")
  2563. return struct.pack(">BBHB", EAP_CODE_REQUEST, ctx['id'],
  2564. 4 + 1,
  2565. EAP_TYPE_AKA_PRIME)
  2566. idx += 1
  2567. if ctx['num'] == idx:
  2568. logger.info("Test: Challenge with no attributes")
  2569. return struct.pack(">BBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2570. 4 + 1 + 3,
  2571. EAP_TYPE_AKA_PRIME, EAP_AKA_SUBTYPE_CHALLENGE, 0)
  2572. idx += 1
  2573. if ctx['num'] == idx:
  2574. logger.info("Test: EAP-Failure")
  2575. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2576. idx += 1
  2577. if ctx['num'] == idx:
  2578. logger.info("Test: Challenge with empty AT_KDF_INPUT")
  2579. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2580. 4 + 1 + 3 + 4,
  2581. EAP_TYPE_AKA_PRIME, EAP_AKA_SUBTYPE_CHALLENGE, 0,
  2582. EAP_SIM_AT_KDF_INPUT, 1, 0)
  2583. idx += 1
  2584. if ctx['num'] == idx:
  2585. logger.info("Test: EAP-Failure")
  2586. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2587. idx += 1
  2588. if ctx['num'] == idx:
  2589. logger.info("Test: Challenge with AT_KDF_INPUT")
  2590. return struct.pack(">BBHBBHBBHBBBB", EAP_CODE_REQUEST, ctx['id'],
  2591. 4 + 1 + 3 + 8,
  2592. EAP_TYPE_AKA_PRIME, EAP_AKA_SUBTYPE_CHALLENGE, 0,
  2593. EAP_SIM_AT_KDF_INPUT, 2, 1, ord('a'), ord('b'),
  2594. ord('c'), ord('d'))
  2595. idx += 1
  2596. if ctx['num'] == idx:
  2597. logger.info("Test: EAP-Failure")
  2598. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2599. idx += 1
  2600. if ctx['num'] == idx:
  2601. logger.info("Test: Challenge with duplicated KDF")
  2602. return struct.pack(">BBHBBHBBHBBBBBBHBBHBBH",
  2603. EAP_CODE_REQUEST, ctx['id'],
  2604. 4 + 1 + 3 + 8 + 3 * 4,
  2605. EAP_TYPE_AKA_PRIME, EAP_AKA_SUBTYPE_CHALLENGE, 0,
  2606. EAP_SIM_AT_KDF_INPUT, 2, 1, ord('a'), ord('b'),
  2607. ord('c'), ord('d'),
  2608. EAP_SIM_AT_KDF, 1, 1,
  2609. EAP_SIM_AT_KDF, 1, 2,
  2610. EAP_SIM_AT_KDF, 1, 1)
  2611. idx += 1
  2612. if ctx['num'] == idx:
  2613. logger.info("Test: EAP-Failure")
  2614. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2615. idx += 1
  2616. if ctx['num'] == idx:
  2617. logger.info("Test: Challenge with multiple KDF proposals")
  2618. return struct.pack(">BBHBBHBBHBBBBBBHBBHBBH",
  2619. EAP_CODE_REQUEST, ctx['id'],
  2620. 4 + 1 + 3 + 8 + 3 * 4,
  2621. EAP_TYPE_AKA_PRIME, EAP_AKA_SUBTYPE_CHALLENGE, 0,
  2622. EAP_SIM_AT_KDF_INPUT, 2, 1, ord('a'), ord('b'),
  2623. ord('c'), ord('d'),
  2624. EAP_SIM_AT_KDF, 1, 255,
  2625. EAP_SIM_AT_KDF, 1, 254,
  2626. EAP_SIM_AT_KDF, 1, 1)
  2627. idx += 1
  2628. if ctx['num'] == idx:
  2629. logger.info("Test: Challenge with incorrect KDF selected")
  2630. return struct.pack(">BBHBBHBBHBBBBBBHBBHBBHBBH",
  2631. EAP_CODE_REQUEST, ctx['id'],
  2632. 4 + 1 + 3 + 8 + 4 * 4,
  2633. EAP_TYPE_AKA_PRIME, EAP_AKA_SUBTYPE_CHALLENGE, 0,
  2634. EAP_SIM_AT_KDF_INPUT, 2, 1, ord('a'), ord('b'),
  2635. ord('c'), ord('d'),
  2636. EAP_SIM_AT_KDF, 1, 255,
  2637. EAP_SIM_AT_KDF, 1, 255,
  2638. EAP_SIM_AT_KDF, 1, 254,
  2639. EAP_SIM_AT_KDF, 1, 1)
  2640. idx += 1
  2641. if ctx['num'] == idx:
  2642. logger.info("Test: EAP-Failure")
  2643. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2644. idx += 1
  2645. if ctx['num'] == idx:
  2646. logger.info("Test: Challenge with multiple KDF proposals")
  2647. return struct.pack(">BBHBBHBBHBBBBBBHBBHBBH",
  2648. EAP_CODE_REQUEST, ctx['id'],
  2649. 4 + 1 + 3 + 8 + 3 * 4,
  2650. EAP_TYPE_AKA_PRIME, EAP_AKA_SUBTYPE_CHALLENGE, 0,
  2651. EAP_SIM_AT_KDF_INPUT, 2, 1, ord('a'), ord('b'),
  2652. ord('c'), ord('d'),
  2653. EAP_SIM_AT_KDF, 1, 255,
  2654. EAP_SIM_AT_KDF, 1, 254,
  2655. EAP_SIM_AT_KDF, 1, 1)
  2656. idx += 1
  2657. if ctx['num'] == idx:
  2658. logger.info("Test: Challenge with selected KDF not duplicated")
  2659. return struct.pack(">BBHBBHBBHBBBBBBHBBHBBH",
  2660. EAP_CODE_REQUEST, ctx['id'],
  2661. 4 + 1 + 3 + 8 + 3 * 4,
  2662. EAP_TYPE_AKA_PRIME, EAP_AKA_SUBTYPE_CHALLENGE, 0,
  2663. EAP_SIM_AT_KDF_INPUT, 2, 1, ord('a'), ord('b'),
  2664. ord('c'), ord('d'),
  2665. EAP_SIM_AT_KDF, 1, 1,
  2666. EAP_SIM_AT_KDF, 1, 255,
  2667. EAP_SIM_AT_KDF, 1, 254)
  2668. idx += 1
  2669. if ctx['num'] == idx:
  2670. logger.info("Test: EAP-Failure")
  2671. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2672. idx += 1
  2673. if ctx['num'] == idx:
  2674. logger.info("Test: Challenge with multiple KDF proposals")
  2675. return struct.pack(">BBHBBHBBHBBBBBBHBBHBBH",
  2676. EAP_CODE_REQUEST, ctx['id'],
  2677. 4 + 1 + 3 + 8 + 3 * 4,
  2678. EAP_TYPE_AKA_PRIME, EAP_AKA_SUBTYPE_CHALLENGE, 0,
  2679. EAP_SIM_AT_KDF_INPUT, 2, 1, ord('a'), ord('b'),
  2680. ord('c'), ord('d'),
  2681. EAP_SIM_AT_KDF, 1, 255,
  2682. EAP_SIM_AT_KDF, 1, 254,
  2683. EAP_SIM_AT_KDF, 1, 1)
  2684. idx += 1
  2685. if ctx['num'] == idx:
  2686. logger.info("Test: Challenge with selected KDF duplicated (missing MAC, RAND, AUTN)")
  2687. return struct.pack(">BBHBBHBBHBBBBBBHBBHBBHBBH",
  2688. EAP_CODE_REQUEST, ctx['id'],
  2689. 4 + 1 + 3 + 8 + 4 * 4,
  2690. EAP_TYPE_AKA_PRIME, EAP_AKA_SUBTYPE_CHALLENGE, 0,
  2691. EAP_SIM_AT_KDF_INPUT, 2, 1, ord('a'), ord('b'),
  2692. ord('c'), ord('d'),
  2693. EAP_SIM_AT_KDF, 1, 1,
  2694. EAP_SIM_AT_KDF, 1, 255,
  2695. EAP_SIM_AT_KDF, 1, 254,
  2696. EAP_SIM_AT_KDF, 1, 1)
  2697. idx += 1
  2698. if ctx['num'] == idx:
  2699. logger.info("Test: EAP-Failure")
  2700. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2701. idx += 1
  2702. if ctx['num'] == idx:
  2703. logger.info("Test: Challenge with multiple unsupported KDF proposals")
  2704. return struct.pack(">BBHBBHBBHBBBBBBHBBH",
  2705. EAP_CODE_REQUEST, ctx['id'],
  2706. 4 + 1 + 3 + 8 + 2 * 4,
  2707. EAP_TYPE_AKA_PRIME, EAP_AKA_SUBTYPE_CHALLENGE, 0,
  2708. EAP_SIM_AT_KDF_INPUT, 2, 1, ord('a'), ord('b'),
  2709. ord('c'), ord('d'),
  2710. EAP_SIM_AT_KDF, 1, 255,
  2711. EAP_SIM_AT_KDF, 1, 254)
  2712. idx += 1
  2713. if ctx['num'] == idx:
  2714. logger.info("Test: EAP-Failure")
  2715. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2716. idx += 1
  2717. if ctx['num'] == idx:
  2718. logger.info("Test: Challenge with multiple KDF proposals")
  2719. return struct.pack(">BBHBBHBBHBBBBBBHBBHBBH",
  2720. EAP_CODE_REQUEST, ctx['id'],
  2721. 4 + 1 + 3 + 8 + 3 * 4,
  2722. EAP_TYPE_AKA_PRIME, EAP_AKA_SUBTYPE_CHALLENGE, 0,
  2723. EAP_SIM_AT_KDF_INPUT, 2, 1, ord('a'), ord('b'),
  2724. ord('c'), ord('d'),
  2725. EAP_SIM_AT_KDF, 1, 255,
  2726. EAP_SIM_AT_KDF, 1, 254,
  2727. EAP_SIM_AT_KDF, 1, 1)
  2728. idx += 1
  2729. if ctx['num'] == idx:
  2730. logger.info("Test: Challenge with invalid MAC, RAND, AUTN values)")
  2731. return struct.pack(">BBHBBHBBHBBBBBBHBBHBBHBBHBBH4LBBH4LBBH4L",
  2732. EAP_CODE_REQUEST, ctx['id'],
  2733. 4 + 1 + 3 + 8 + 4 * 4 + 20 + 20 + 20,
  2734. EAP_TYPE_AKA_PRIME, EAP_AKA_SUBTYPE_CHALLENGE, 0,
  2735. EAP_SIM_AT_KDF_INPUT, 2, 1, ord('a'), ord('b'),
  2736. ord('c'), ord('d'),
  2737. EAP_SIM_AT_KDF, 1, 1,
  2738. EAP_SIM_AT_KDF, 1, 255,
  2739. EAP_SIM_AT_KDF, 1, 254,
  2740. EAP_SIM_AT_KDF, 1, 1,
  2741. EAP_SIM_AT_MAC, 5, 0, 0, 0, 0, 0,
  2742. EAP_SIM_AT_RAND, 5, 0, 0, 0, 0, 0,
  2743. EAP_SIM_AT_AUTN, 5, 0, 0, 0, 0, 0)
  2744. idx += 1
  2745. if ctx['num'] == idx:
  2746. logger.info("Test: EAP-Failure")
  2747. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2748. idx += 1
  2749. if ctx['num'] == idx:
  2750. logger.info("Test: Challenge - AMF separation bit not set)")
  2751. return struct.pack(">BBHBBHBBHBBBBBBHBBH4LBBH4LBBH4L",
  2752. EAP_CODE_REQUEST, ctx['id'],
  2753. 4 + 1 + 3 + 8 + 4 + 20 + 20 + 20,
  2754. EAP_TYPE_AKA_PRIME, EAP_AKA_SUBTYPE_CHALLENGE, 0,
  2755. EAP_SIM_AT_KDF_INPUT, 2, 1, ord('a'), ord('b'),
  2756. ord('c'), ord('d'),
  2757. EAP_SIM_AT_KDF, 1, 1,
  2758. EAP_SIM_AT_MAC, 5, 0, 1, 2, 3, 4,
  2759. EAP_SIM_AT_RAND, 5, 0, 5, 6, 7, 8,
  2760. EAP_SIM_AT_AUTN, 5, 0, 9, 10,
  2761. 0x2fda8ef7, 0xbba518cc)
  2762. idx += 1
  2763. if ctx['num'] == idx:
  2764. logger.info("Test: EAP-Failure")
  2765. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2766. idx += 1
  2767. if ctx['num'] == idx:
  2768. logger.info("Test: Challenge - Invalid MAC")
  2769. return struct.pack(">BBHBBHBBHBBBBBBHBBH4LBBH4LBBH4L",
  2770. EAP_CODE_REQUEST, ctx['id'],
  2771. 4 + 1 + 3 + 8 + 4 + 20 + 20 + 20,
  2772. EAP_TYPE_AKA_PRIME, EAP_AKA_SUBTYPE_CHALLENGE, 0,
  2773. EAP_SIM_AT_KDF_INPUT, 2, 1, ord('a'), ord('b'),
  2774. ord('c'), ord('d'),
  2775. EAP_SIM_AT_KDF, 1, 1,
  2776. EAP_SIM_AT_MAC, 5, 0, 1, 2, 3, 4,
  2777. EAP_SIM_AT_RAND, 5, 0, 5, 6, 7, 8,
  2778. EAP_SIM_AT_AUTN, 5, 0, 0xffffffff, 0xffffffff,
  2779. 0xd1f90322, 0x40514cb4)
  2780. idx += 1
  2781. if ctx['num'] == idx:
  2782. logger.info("Test: EAP-Failure")
  2783. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2784. idx += 1
  2785. if ctx['num'] == idx:
  2786. logger.info("Test: Challenge - Valid MAC")
  2787. return struct.pack(">BBHBBHBBHBBBBBBHBBH4LBBH4LBBH4L",
  2788. EAP_CODE_REQUEST, ctx['id'],
  2789. 4 + 1 + 3 + 8 + 4 + 20 + 20 + 20,
  2790. EAP_TYPE_AKA_PRIME, EAP_AKA_SUBTYPE_CHALLENGE, 0,
  2791. EAP_SIM_AT_KDF_INPUT, 2, 1, ord('a'), ord('b'),
  2792. ord('c'), ord('d'),
  2793. EAP_SIM_AT_KDF, 1, 1,
  2794. EAP_SIM_AT_MAC, 5, 0,
  2795. 0xf4a3c1d3, 0x7c901401, 0x34bd8b01, 0x6f7fa32f,
  2796. EAP_SIM_AT_RAND, 5, 0, 5, 6, 7, 8,
  2797. EAP_SIM_AT_AUTN, 5, 0, 0xffffffff, 0xffffffff,
  2798. 0xd1f90322, 0x40514cb4)
  2799. idx += 1
  2800. if ctx['num'] == idx:
  2801. logger.info("Test: EAP-Failure")
  2802. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2803. idx += 1
  2804. if ctx['num'] == idx:
  2805. logger.info("Test: Invalid AT_KDF_INPUT length")
  2806. return struct.pack(">BBHBBHBBHL", EAP_CODE_REQUEST, ctx['id'],
  2807. 4 + 1 + 3 + 8,
  2808. EAP_TYPE_AKA_PRIME, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2809. EAP_SIM_AT_KDF_INPUT, 2, 0xffff, 0)
  2810. idx += 1
  2811. if ctx['num'] == idx:
  2812. logger.info("Test: EAP-Failure")
  2813. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2814. idx += 1
  2815. if ctx['num'] == idx:
  2816. logger.info("Test: Invalid AT_KDF length")
  2817. return struct.pack(">BBHBBHBBHL", EAP_CODE_REQUEST, ctx['id'],
  2818. 4 + 1 + 3 + 8,
  2819. EAP_TYPE_AKA_PRIME, EAP_AKA_SUBTYPE_IDENTITY, 0,
  2820. EAP_SIM_AT_KDF, 2, 0, 0)
  2821. idx += 1
  2822. if ctx['num'] == idx:
  2823. logger.info("Test: EAP-Failure")
  2824. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2825. idx += 1
  2826. if ctx['num'] == idx:
  2827. logger.info("Test: Challenge with large number of KDF proposals")
  2828. return struct.pack(">BBHBBHBBHBBHBBHBBHBBHBBHBBHBBHBBHBBHBBHBBH",
  2829. EAP_CODE_REQUEST, ctx['id'],
  2830. 4 + 1 + 3 + 12 * 4,
  2831. EAP_TYPE_AKA_PRIME, EAP_AKA_SUBTYPE_CHALLENGE, 0,
  2832. EAP_SIM_AT_KDF, 1, 255,
  2833. EAP_SIM_AT_KDF, 1, 254,
  2834. EAP_SIM_AT_KDF, 1, 253,
  2835. EAP_SIM_AT_KDF, 1, 252,
  2836. EAP_SIM_AT_KDF, 1, 251,
  2837. EAP_SIM_AT_KDF, 1, 250,
  2838. EAP_SIM_AT_KDF, 1, 249,
  2839. EAP_SIM_AT_KDF, 1, 248,
  2840. EAP_SIM_AT_KDF, 1, 247,
  2841. EAP_SIM_AT_KDF, 1, 246,
  2842. EAP_SIM_AT_KDF, 1, 245,
  2843. EAP_SIM_AT_KDF, 1, 244)
  2844. idx += 1
  2845. if ctx['num'] == idx:
  2846. logger.info("Test: EAP-Failure")
  2847. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2848. return None
  2849. srv = start_radius_server(aka_prime_handler)
  2850. try:
  2851. hapd = start_ap(apdev[0]['ifname'])
  2852. for i in range(0, 16):
  2853. dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412",
  2854. eap="AKA'", identity="6555444333222111",
  2855. password="5122250214c33e723a5dd523fc145fc0:981d464c7c52eb6e5036234984ad0bcf:000000000123",
  2856. wait_connect=False)
  2857. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"],
  2858. timeout=15)
  2859. if ev is None:
  2860. raise Exception("Timeout on EAP start")
  2861. if i in [ 0 ]:
  2862. time.sleep(0.1)
  2863. else:
  2864. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"],
  2865. timeout=10)
  2866. if ev is None:
  2867. raise Exception("Timeout on EAP failure")
  2868. dev[0].request("REMOVE_NETWORK all")
  2869. dev[0].dump_monitor()
  2870. finally:
  2871. stop_radius_server(srv)
  2872. def test_eap_proto_sim(dev, apdev):
  2873. """EAP-SIM protocol tests"""
  2874. def sim_handler(ctx, req):
  2875. logger.info("sim_handler - RX " + req.encode("hex"))
  2876. if 'num' not in ctx:
  2877. ctx['num'] = 0
  2878. ctx['num'] = ctx['num'] + 1
  2879. if 'id' not in ctx:
  2880. ctx['id'] = 1
  2881. ctx['id'] = (ctx['id'] + 1) % 256
  2882. idx = 0
  2883. idx += 1
  2884. if ctx['num'] == idx:
  2885. logger.info("Test: Missing payload")
  2886. return struct.pack(">BBHB", EAP_CODE_REQUEST, ctx['id'],
  2887. 4 + 1,
  2888. EAP_TYPE_SIM)
  2889. idx += 1
  2890. if ctx['num'] == idx:
  2891. logger.info("Test: Unexpected AT_AUTN")
  2892. return struct.pack(">BBHBBHBBHL", EAP_CODE_REQUEST, ctx['id'],
  2893. 4 + 1 + 3 + 8,
  2894. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_START, 0,
  2895. EAP_SIM_AT_AUTN, 2, 0, 0)
  2896. idx += 1
  2897. if ctx['num'] == idx:
  2898. logger.info("Test: EAP-Failure")
  2899. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2900. idx += 1
  2901. if ctx['num'] == idx:
  2902. logger.info("Test: Too short AT_VERSION_LIST")
  2903. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2904. 4 + 1 + 3 + 4,
  2905. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_START, 0,
  2906. EAP_SIM_AT_VERSION_LIST, 1, 0)
  2907. idx += 1
  2908. if ctx['num'] == idx:
  2909. logger.info("Test: EAP-Failure")
  2910. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2911. idx += 1
  2912. if ctx['num'] == idx:
  2913. logger.info("Test: AT_VERSION_LIST overflow")
  2914. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2915. 4 + 1 + 3 + 4,
  2916. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_START, 0,
  2917. EAP_SIM_AT_VERSION_LIST, 1, 0xffff)
  2918. idx += 1
  2919. if ctx['num'] == idx:
  2920. logger.info("Test: EAP-Failure")
  2921. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2922. idx += 1
  2923. if ctx['num'] == idx:
  2924. logger.info("Test: Unexpected AT_AUTS")
  2925. return struct.pack(">BBHBBHBBHL", EAP_CODE_REQUEST, ctx['id'],
  2926. 4 + 1 + 3 + 8,
  2927. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_START, 0,
  2928. EAP_SIM_AT_AUTS, 2, 0, 0)
  2929. idx += 1
  2930. if ctx['num'] == idx:
  2931. logger.info("Test: EAP-Failure")
  2932. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2933. idx += 1
  2934. if ctx['num'] == idx:
  2935. logger.info("Test: Unexpected AT_CHECKCODE")
  2936. return struct.pack(">BBHBBHBBHL", EAP_CODE_REQUEST, ctx['id'],
  2937. 4 + 1 + 3 + 8,
  2938. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_START, 0,
  2939. EAP_SIM_AT_CHECKCODE, 2, 0, 0)
  2940. idx += 1
  2941. if ctx['num'] == idx:
  2942. logger.info("Test: EAP-Failure")
  2943. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2944. idx += 1
  2945. if ctx['num'] == idx:
  2946. logger.info("Test: No AT_VERSION_LIST in Start")
  2947. return struct.pack(">BBHBBH", EAP_CODE_REQUEST, ctx['id'],
  2948. 4 + 1 + 3,
  2949. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_START, 0)
  2950. idx += 1
  2951. if ctx['num'] == idx:
  2952. logger.info("Test: EAP-Failure")
  2953. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2954. idx += 1
  2955. if ctx['num'] == idx:
  2956. logger.info("Test: No support version in AT_VERSION_LIST")
  2957. return struct.pack(">BBHBBHBBH4B", EAP_CODE_REQUEST, ctx['id'],
  2958. 4 + 1 + 3 + 8,
  2959. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_START, 0,
  2960. EAP_SIM_AT_VERSION_LIST, 2, 3, 2, 3, 4, 5)
  2961. idx += 1
  2962. if ctx['num'] == idx:
  2963. logger.info("Test: EAP-Failure")
  2964. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2965. idx += 1
  2966. if ctx['num'] == idx:
  2967. logger.info("Test: Identity request without ID type")
  2968. return struct.pack(">BBHBBHBBH2H", EAP_CODE_REQUEST, ctx['id'],
  2969. 4 + 1 + 3 + 8,
  2970. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_START, 0,
  2971. EAP_SIM_AT_VERSION_LIST, 2, 2, 1, 0)
  2972. idx += 1
  2973. if ctx['num'] == idx:
  2974. logger.info("Test: Identity request ANY_ID")
  2975. return struct.pack(">BBHBBHBBH2HBBH", EAP_CODE_REQUEST, ctx['id'],
  2976. 4 + 1 + 3 + 8 + 4,
  2977. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_START, 0,
  2978. EAP_SIM_AT_VERSION_LIST, 2, 2, 1, 0,
  2979. EAP_SIM_AT_ANY_ID_REQ, 1, 0)
  2980. idx += 1
  2981. if ctx['num'] == idx:
  2982. logger.info("Test: Identity request ANY_ID (duplicate)")
  2983. return struct.pack(">BBHBBHBBH2HBBH", EAP_CODE_REQUEST, ctx['id'],
  2984. 4 + 1 + 3 + 8 + 4,
  2985. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_START, 0,
  2986. EAP_SIM_AT_VERSION_LIST, 2, 2, 1, 0,
  2987. EAP_SIM_AT_ANY_ID_REQ, 1, 0)
  2988. idx += 1
  2989. if ctx['num'] == idx:
  2990. logger.info("Test: EAP-Failure")
  2991. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  2992. idx += 1
  2993. if ctx['num'] == idx:
  2994. logger.info("Test: Identity request ANY_ID")
  2995. return struct.pack(">BBHBBHBBH2HBBH", EAP_CODE_REQUEST, ctx['id'],
  2996. 4 + 1 + 3 + 8 + 4,
  2997. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_START, 0,
  2998. EAP_SIM_AT_VERSION_LIST, 2, 2, 1, 0,
  2999. EAP_SIM_AT_ANY_ID_REQ, 1, 0)
  3000. idx += 1
  3001. if ctx['num'] == idx:
  3002. logger.info("Test: Identity request FULLAUTH_ID")
  3003. return struct.pack(">BBHBBHBBH2HBBH", EAP_CODE_REQUEST, ctx['id'],
  3004. 4 + 1 + 3 + 8 + 4,
  3005. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_START, 0,
  3006. EAP_SIM_AT_VERSION_LIST, 2, 2, 1, 0,
  3007. EAP_SIM_AT_FULLAUTH_ID_REQ, 1, 0)
  3008. idx += 1
  3009. if ctx['num'] == idx:
  3010. logger.info("Test: Identity request FULLAUTH_ID (duplicate)")
  3011. return struct.pack(">BBHBBHBBH2HBBH", EAP_CODE_REQUEST, ctx['id'],
  3012. 4 + 1 + 3 + 8 + 4,
  3013. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_START, 0,
  3014. EAP_SIM_AT_VERSION_LIST, 2, 2, 1, 0,
  3015. EAP_SIM_AT_FULLAUTH_ID_REQ, 1, 0)
  3016. idx += 1
  3017. if ctx['num'] == idx:
  3018. logger.info("Test: EAP-Failure")
  3019. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  3020. idx += 1
  3021. if ctx['num'] == idx:
  3022. logger.info("Test: Identity request ANY_ID")
  3023. return struct.pack(">BBHBBHBBH2HBBH", EAP_CODE_REQUEST, ctx['id'],
  3024. 4 + 1 + 3 + 8 + 4,
  3025. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_START, 0,
  3026. EAP_SIM_AT_VERSION_LIST, 2, 2, 1, 0,
  3027. EAP_SIM_AT_ANY_ID_REQ, 1, 0)
  3028. idx += 1
  3029. if ctx['num'] == idx:
  3030. logger.info("Test: Identity request FULLAUTH_ID")
  3031. return struct.pack(">BBHBBHBBH2HBBH", EAP_CODE_REQUEST, ctx['id'],
  3032. 4 + 1 + 3 + 8 + 4,
  3033. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_START, 0,
  3034. EAP_SIM_AT_VERSION_LIST, 2, 2, 1, 0,
  3035. EAP_SIM_AT_FULLAUTH_ID_REQ, 1, 0)
  3036. idx += 1
  3037. if ctx['num'] == idx:
  3038. logger.info("Test: Identity request PERMANENT_ID")
  3039. return struct.pack(">BBHBBHBBH2HBBH", EAP_CODE_REQUEST, ctx['id'],
  3040. 4 + 1 + 3 + 8 + 4,
  3041. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_START, 0,
  3042. EAP_SIM_AT_VERSION_LIST, 2, 2, 1, 0,
  3043. EAP_SIM_AT_PERMANENT_ID_REQ, 1, 0)
  3044. idx += 1
  3045. if ctx['num'] == idx:
  3046. logger.info("Test: Identity request PERMANENT_ID (duplicate)")
  3047. return struct.pack(">BBHBBHBBH2HBBH", EAP_CODE_REQUEST, ctx['id'],
  3048. 4 + 1 + 3 + 8 + 4,
  3049. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_START, 0,
  3050. EAP_SIM_AT_VERSION_LIST, 2, 2, 1, 0,
  3051. EAP_SIM_AT_PERMANENT_ID_REQ, 1, 0)
  3052. idx += 1
  3053. if ctx['num'] == idx:
  3054. logger.info("Test: EAP-Failure")
  3055. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  3056. idx += 1
  3057. if ctx['num'] == idx:
  3058. logger.info("Test: No AT_MAC and AT_RAND in Challenge")
  3059. return struct.pack(">BBHBBH", EAP_CODE_REQUEST, ctx['id'],
  3060. 4 + 1 + 3,
  3061. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_CHALLENGE, 0)
  3062. idx += 1
  3063. if ctx['num'] == idx:
  3064. logger.info("Test: EAP-Failure")
  3065. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  3066. idx += 1
  3067. if ctx['num'] == idx:
  3068. logger.info("Test: No AT_RAND in Challenge")
  3069. return struct.pack(">BBHBBHBBH4L", EAP_CODE_REQUEST, ctx['id'],
  3070. 4 + 1 + 3 + 20,
  3071. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_CHALLENGE, 0,
  3072. EAP_SIM_AT_MAC, 5, 0, 0, 0, 0, 0)
  3073. idx += 1
  3074. if ctx['num'] == idx:
  3075. logger.info("Test: EAP-Failure")
  3076. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  3077. idx += 1
  3078. if ctx['num'] == idx:
  3079. logger.info("Test: Insufficient number of challenges in Challenge")
  3080. return struct.pack(">BBHBBHBBH4LBBH4L", EAP_CODE_REQUEST, ctx['id'],
  3081. 4 + 1 + 3 + 20 + 20,
  3082. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_CHALLENGE, 0,
  3083. EAP_SIM_AT_RAND, 5, 0, 0, 0, 0, 0,
  3084. EAP_SIM_AT_MAC, 5, 0, 0, 0, 0, 0)
  3085. idx += 1
  3086. if ctx['num'] == idx:
  3087. logger.info("Test: EAP-Failure")
  3088. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  3089. idx += 1
  3090. if ctx['num'] == idx:
  3091. logger.info("Test: Too many challenges in Challenge")
  3092. return struct.pack(">BBHBBHBBH4L4L4L4LBBH4L", EAP_CODE_REQUEST,
  3093. ctx['id'],
  3094. 4 + 1 + 3 + 4 + 4 * 16 + 20,
  3095. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_CHALLENGE, 0,
  3096. EAP_SIM_AT_RAND, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  3097. 0, 0, 0, 0, 0, 0, 0, 0,
  3098. EAP_SIM_AT_MAC, 5, 0, 0, 0, 0, 0)
  3099. idx += 1
  3100. if ctx['num'] == idx:
  3101. logger.info("Test: EAP-Failure")
  3102. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  3103. idx += 1
  3104. if ctx['num'] == idx:
  3105. logger.info("Test: Same RAND multiple times in Challenge")
  3106. return struct.pack(">BBHBBHBBH4L4L4LBBH4L", EAP_CODE_REQUEST,
  3107. ctx['id'],
  3108. 4 + 1 + 3 + 4 + 3 * 16 + 20,
  3109. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_CHALLENGE, 0,
  3110. EAP_SIM_AT_RAND, 13, 0, 0, 0, 0, 0, 0, 0, 0, 1,
  3111. 0, 0, 0, 0,
  3112. EAP_SIM_AT_MAC, 5, 0, 0, 0, 0, 0)
  3113. idx += 1
  3114. if ctx['num'] == idx:
  3115. logger.info("Test: EAP-Failure")
  3116. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  3117. idx += 1
  3118. if ctx['num'] == idx:
  3119. logger.info("Test: Notification with no attributes")
  3120. return struct.pack(">BBHBBH", EAP_CODE_REQUEST, ctx['id'],
  3121. 4 + 1 + 3,
  3122. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_NOTIFICATION, 0)
  3123. idx += 1
  3124. if ctx['num'] == idx:
  3125. logger.info("Test: EAP-Failure")
  3126. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  3127. idx += 1
  3128. if ctx['num'] == idx:
  3129. logger.info("Test: Notification indicating success, but no MAC")
  3130. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  3131. 4 + 1 + 3 + 4,
  3132. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_NOTIFICATION, 0,
  3133. EAP_SIM_AT_NOTIFICATION, 1, 32768)
  3134. idx += 1
  3135. if ctx['num'] == idx:
  3136. logger.info("Test: EAP-Failure")
  3137. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  3138. idx += 1
  3139. if ctx['num'] == idx:
  3140. logger.info("Test: Notification indicating success, but invalid MAC value")
  3141. return struct.pack(">BBHBBHBBHBBH4L", EAP_CODE_REQUEST, ctx['id'],
  3142. 4 + 1 + 3 + 4 + 20,
  3143. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_NOTIFICATION, 0,
  3144. EAP_SIM_AT_NOTIFICATION, 1, 32768,
  3145. EAP_SIM_AT_MAC, 5, 0, 0, 0, 0, 0)
  3146. idx += 1
  3147. if ctx['num'] == idx:
  3148. logger.info("Test: EAP-Failure")
  3149. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  3150. idx += 1
  3151. if ctx['num'] == idx:
  3152. logger.info("Test: Notification before auth")
  3153. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  3154. 4 + 1 + 3 + 4,
  3155. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_NOTIFICATION, 0,
  3156. EAP_SIM_AT_NOTIFICATION, 1, 16384)
  3157. idx += 1
  3158. if ctx['num'] == idx:
  3159. logger.info("Test: EAP-Failure")
  3160. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  3161. idx += 1
  3162. if ctx['num'] == idx:
  3163. logger.info("Test: Notification before auth")
  3164. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  3165. 4 + 1 + 3 + 4,
  3166. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_NOTIFICATION, 0,
  3167. EAP_SIM_AT_NOTIFICATION, 1, 16385)
  3168. idx += 1
  3169. if ctx['num'] == idx:
  3170. logger.info("Test: EAP-Failure")
  3171. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  3172. idx += 1
  3173. if ctx['num'] == idx:
  3174. logger.info("Test: Notification with unrecognized non-failure")
  3175. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  3176. 4 + 1 + 3 + 4,
  3177. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_NOTIFICATION, 0,
  3178. EAP_SIM_AT_NOTIFICATION, 1, 0xc000)
  3179. idx += 1
  3180. if ctx['num'] == idx:
  3181. logger.info("Test: Notification before auth (duplicate)")
  3182. return struct.pack(">BBHBBHBBH", EAP_CODE_REQUEST, ctx['id'],
  3183. 4 + 1 + 3 + 4,
  3184. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_NOTIFICATION, 0,
  3185. EAP_SIM_AT_NOTIFICATION, 1, 0xc000)
  3186. idx += 1
  3187. if ctx['num'] == idx:
  3188. logger.info("Test: EAP-Failure")
  3189. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  3190. idx += 1
  3191. if ctx['num'] == idx:
  3192. logger.info("Test: Re-authentication (unexpected) with no attributes")
  3193. return struct.pack(">BBHBBH", EAP_CODE_REQUEST, ctx['id'],
  3194. 4 + 1 + 3,
  3195. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_REAUTHENTICATION,
  3196. 0)
  3197. idx += 1
  3198. if ctx['num'] == idx:
  3199. logger.info("Test: EAP-Failure")
  3200. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  3201. idx += 1
  3202. if ctx['num'] == idx:
  3203. logger.info("Test: Client Error")
  3204. return struct.pack(">BBHBBH", EAP_CODE_REQUEST, ctx['id'],
  3205. 4 + 1 + 3,
  3206. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_CLIENT_ERROR, 0)
  3207. idx += 1
  3208. if ctx['num'] == idx:
  3209. logger.info("Test: EAP-Failure")
  3210. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  3211. idx += 1
  3212. if ctx['num'] == idx:
  3213. logger.info("Test: Unknown subtype")
  3214. return struct.pack(">BBHBBH", EAP_CODE_REQUEST, ctx['id'],
  3215. 4 + 1 + 3,
  3216. EAP_TYPE_SIM, 255, 0)
  3217. idx += 1
  3218. if ctx['num'] == idx:
  3219. logger.info("Test: EAP-Failure")
  3220. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  3221. return None
  3222. srv = start_radius_server(sim_handler)
  3223. try:
  3224. hapd = start_ap(apdev[0]['ifname'])
  3225. for i in range(0, 25):
  3226. dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412",
  3227. eap="SIM", identity="1232010000000000",
  3228. password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581",
  3229. wait_connect=False)
  3230. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"],
  3231. timeout=15)
  3232. if ev is None:
  3233. raise Exception("Timeout on EAP start")
  3234. if i in [ 0 ]:
  3235. time.sleep(0.1)
  3236. else:
  3237. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"],
  3238. timeout=10)
  3239. if ev is None:
  3240. raise Exception("Timeout on EAP failure")
  3241. dev[0].request("REMOVE_NETWORK all")
  3242. dev[0].dump_monitor()
  3243. finally:
  3244. stop_radius_server(srv)
  3245. def test_eap_proto_ikev2(dev, apdev):
  3246. """EAP-IKEv2 protocol tests"""
  3247. def ikev2_handler(ctx, req):
  3248. logger.info("ikev2_handler - RX " + req.encode("hex"))
  3249. if 'num' not in ctx:
  3250. ctx['num'] = 0
  3251. ctx['num'] = ctx['num'] + 1
  3252. if 'id' not in ctx:
  3253. ctx['id'] = 1
  3254. ctx['id'] = (ctx['id'] + 1) % 256
  3255. idx = 0
  3256. idx += 1
  3257. if ctx['num'] == idx:
  3258. logger.info("Test: Missing payload")
  3259. return struct.pack(">BBHB", EAP_CODE_REQUEST, ctx['id'],
  3260. 4 + 1,
  3261. EAP_TYPE_IKEV2)
  3262. idx += 1
  3263. if ctx['num'] == idx:
  3264. logger.info("Test: Truncated Message Length field")
  3265. return struct.pack(">BBHBB3B", EAP_CODE_REQUEST, ctx['id'],
  3266. 4 + 1 + 1 + 3,
  3267. EAP_TYPE_IKEV2, 0x80, 0, 0, 0)
  3268. idx += 1
  3269. if ctx['num'] == idx:
  3270. logger.info("Test: Too short Message Length value")
  3271. return struct.pack(">BBHBBLB", EAP_CODE_REQUEST, ctx['id'],
  3272. 4 + 1 + 1 + 4 + 1,
  3273. EAP_TYPE_IKEV2, 0x80, 0, 1)
  3274. idx += 1
  3275. if ctx['num'] == idx:
  3276. logger.info("Test: Truncated message")
  3277. return struct.pack(">BBHBBL", EAP_CODE_REQUEST, ctx['id'],
  3278. 4 + 1 + 1 + 4,
  3279. EAP_TYPE_IKEV2, 0x80, 1)
  3280. idx += 1
  3281. if ctx['num'] == idx:
  3282. logger.info("Test: Truncated message(2)")
  3283. return struct.pack(">BBHBBL", EAP_CODE_REQUEST, ctx['id'],
  3284. 4 + 1 + 1 + 4,
  3285. EAP_TYPE_IKEV2, 0x80, 0xffffffff)
  3286. idx += 1
  3287. if ctx['num'] == idx:
  3288. logger.info("Test: Truncated message(3)")
  3289. return struct.pack(">BBHBBL", EAP_CODE_REQUEST, ctx['id'],
  3290. 4 + 1 + 1 + 4,
  3291. EAP_TYPE_IKEV2, 0xc0, 0xffffffff)
  3292. idx += 1
  3293. if ctx['num'] == idx:
  3294. logger.info("Test: Truncated message(4)")
  3295. return struct.pack(">BBHBBL", EAP_CODE_REQUEST, ctx['id'],
  3296. 4 + 1 + 1 + 4,
  3297. EAP_TYPE_IKEV2, 0xc0, 10000000)
  3298. idx += 1
  3299. if ctx['num'] == idx:
  3300. logger.info("Test: Too long fragments (first fragment)")
  3301. return struct.pack(">BBHBBLB", EAP_CODE_REQUEST, ctx['id'],
  3302. 4 + 1 + 1 + 4 + 1,
  3303. EAP_TYPE_IKEV2, 0xc0, 2, 1)
  3304. idx += 1
  3305. if ctx['num'] == idx:
  3306. logger.info("Test: Too long fragments (second fragment)")
  3307. return struct.pack(">BBHBB2B", EAP_CODE_REQUEST, ctx['id'],
  3308. 4 + 1 + 1 + 2,
  3309. EAP_TYPE_IKEV2, 0x00, 2, 3)
  3310. idx += 1
  3311. if ctx['num'] == idx:
  3312. logger.info("Test: No Message Length field in first fragment")
  3313. return struct.pack(">BBHBBB", EAP_CODE_REQUEST, ctx['id'],
  3314. 4 + 1 + 1 + 1,
  3315. EAP_TYPE_IKEV2, 0x40, 1)
  3316. idx += 1
  3317. if ctx['num'] == idx:
  3318. logger.info("Test: ICV before keys")
  3319. return struct.pack(">BBHBB", EAP_CODE_REQUEST, ctx['id'],
  3320. 4 + 1 + 1,
  3321. EAP_TYPE_IKEV2, 0x20)
  3322. idx += 1
  3323. if ctx['num'] == idx:
  3324. logger.info("Test: Unsupported IKEv2 header version")
  3325. return struct.pack(">BBHBB2L2LBBBBLL", EAP_CODE_REQUEST, ctx['id'],
  3326. 4 + 1 + 1 + 28,
  3327. EAP_TYPE_IKEV2, 0x00,
  3328. 0, 0, 0, 0,
  3329. 0, 0, 0, 0, 0, 0)
  3330. idx += 1
  3331. if ctx['num'] == idx:
  3332. logger.info("Test: Incorrect IKEv2 header Length")
  3333. return struct.pack(">BBHBB2L2LBBBBLL", EAP_CODE_REQUEST, ctx['id'],
  3334. 4 + 1 + 1 + 28,
  3335. EAP_TYPE_IKEV2, 0x00,
  3336. 0, 0, 0, 0,
  3337. 0, 0x20, 0, 0, 0, 0)
  3338. idx += 1
  3339. if ctx['num'] == idx:
  3340. logger.info("Test: Unexpected IKEv2 Exchange Type in SA_INIT state")
  3341. return struct.pack(">BBHBB2L2LBBBBLL", EAP_CODE_REQUEST, ctx['id'],
  3342. 4 + 1 + 1 + 28,
  3343. EAP_TYPE_IKEV2, 0x00,
  3344. 0, 0, 0, 0,
  3345. 0, 0x20, 0, 0, 0, 28)
  3346. idx += 1
  3347. if ctx['num'] == idx:
  3348. logger.info("Test: Unexpected IKEv2 Message ID in SA_INIT state")
  3349. return struct.pack(">BBHBB2L2LBBBBLL", EAP_CODE_REQUEST, ctx['id'],
  3350. 4 + 1 + 1 + 28,
  3351. EAP_TYPE_IKEV2, 0x00,
  3352. 0, 0, 0, 0,
  3353. 0, 0x20, 34, 0, 1, 28)
  3354. idx += 1
  3355. if ctx['num'] == idx:
  3356. logger.info("Test: Unexpected IKEv2 Flags value")
  3357. return struct.pack(">BBHBB2L2LBBBBLL", EAP_CODE_REQUEST, ctx['id'],
  3358. 4 + 1 + 1 + 28,
  3359. EAP_TYPE_IKEV2, 0x00,
  3360. 0, 0, 0, 0,
  3361. 0, 0x20, 34, 0, 0, 28)
  3362. idx += 1
  3363. if ctx['num'] == idx:
  3364. logger.info("Test: Unexpected IKEv2 Flags value(2)")
  3365. return struct.pack(">BBHBB2L2LBBBBLL", EAP_CODE_REQUEST, ctx['id'],
  3366. 4 + 1 + 1 + 28,
  3367. EAP_TYPE_IKEV2, 0x00,
  3368. 0, 0, 0, 0,
  3369. 0, 0x20, 34, 0x20, 0, 28)
  3370. idx += 1
  3371. if ctx['num'] == idx:
  3372. logger.info("Test: No SAi1 in SA_INIT")
  3373. return struct.pack(">BBHBB2L2LBBBBLL", EAP_CODE_REQUEST, ctx['id'],
  3374. 4 + 1 + 1 + 28,
  3375. EAP_TYPE_IKEV2, 0x00,
  3376. 0, 0, 0, 0,
  3377. 0, 0x20, 34, 0x08, 0, 28)
  3378. def build_ike(id, next=0, exch_type=34, flags=0x00, ike=''):
  3379. return struct.pack(">BBHBB2L2LBBBBLL", EAP_CODE_REQUEST, id,
  3380. 4 + 1 + 1 + 28 + len(ike),
  3381. EAP_TYPE_IKEV2, flags,
  3382. 0, 0, 0, 0,
  3383. next, 0x20, exch_type, 0x08, 0,
  3384. 28 + len(ike)) + ike
  3385. idx += 1
  3386. if ctx['num'] == idx:
  3387. logger.info("Test: Unexpected extra data after payloads")
  3388. return build_ike(ctx['id'], ike=struct.pack(">B", 1))
  3389. idx += 1
  3390. if ctx['num'] == idx:
  3391. logger.info("Test: Truncated payload header")
  3392. return build_ike(ctx['id'], next=128, ike=struct.pack(">B", 1))
  3393. idx += 1
  3394. if ctx['num'] == idx:
  3395. logger.info("Test: Too small payload header length")
  3396. ike = struct.pack(">BBH", 0, 0, 3)
  3397. return build_ike(ctx['id'], next=128, ike=ike)
  3398. idx += 1
  3399. if ctx['num'] == idx:
  3400. logger.info("Test: Too large payload header length")
  3401. ike = struct.pack(">BBH", 0, 0, 5)
  3402. return build_ike(ctx['id'], next=128, ike=ike)
  3403. idx += 1
  3404. if ctx['num'] == idx:
  3405. logger.info("Test: Unsupported payload (non-critical and critical)")
  3406. ike = struct.pack(">BBHBBH", 129, 0, 4, 0, 0x01, 4)
  3407. return build_ike(ctx['id'], next=128, ike=ike)
  3408. idx += 1
  3409. if ctx['num'] == idx:
  3410. logger.info("Test: Certificate and empty SAi1")
  3411. ike = struct.pack(">BBHBBH", 33, 0, 4, 0, 0, 4)
  3412. return build_ike(ctx['id'], next=37, ike=ike)
  3413. idx += 1
  3414. if ctx['num'] == idx:
  3415. logger.info("Test: Too short proposal")
  3416. ike = struct.pack(">BBHBBHBBB", 0, 0, 4 + 7,
  3417. 0, 0, 7, 0, 0, 0)
  3418. return build_ike(ctx['id'], next=33, ike=ike)
  3419. idx += 1
  3420. if ctx['num'] == idx:
  3421. logger.info("Test: Too small proposal length in SAi1")
  3422. ike = struct.pack(">BBHBBHBBBB", 0, 0, 4 + 8,
  3423. 0, 0, 7, 0, 0, 0, 0)
  3424. return build_ike(ctx['id'], next=33, ike=ike)
  3425. idx += 1
  3426. if ctx['num'] == idx:
  3427. logger.info("Test: Too large proposal length in SAi1")
  3428. ike = struct.pack(">BBHBBHBBBB", 0, 0, 4 + 8,
  3429. 0, 0, 9, 0, 0, 0, 0)
  3430. return build_ike(ctx['id'], next=33, ike=ike)
  3431. idx += 1
  3432. if ctx['num'] == idx:
  3433. logger.info("Test: Unexpected proposal type in SAi1")
  3434. ike = struct.pack(">BBHBBHBBBB", 0, 0, 4 + 8,
  3435. 1, 0, 8, 0, 0, 0, 0)
  3436. return build_ike(ctx['id'], next=33, ike=ike)
  3437. idx += 1
  3438. if ctx['num'] == idx:
  3439. logger.info("Test: Unexpected Protocol ID in SAi1")
  3440. ike = struct.pack(">BBHBBHBBBB", 0, 0, 4 + 8,
  3441. 0, 0, 8, 0, 0, 0, 0)
  3442. return build_ike(ctx['id'], next=33, ike=ike)
  3443. idx += 1
  3444. if ctx['num'] == idx:
  3445. logger.info("Test: Unexpected proposal number in SAi1")
  3446. ike = struct.pack(">BBHBBHBBBB", 0, 0, 4 + 8,
  3447. 0, 0, 8, 0, 1, 0, 0)
  3448. return build_ike(ctx['id'], next=33, ike=ike)
  3449. idx += 1
  3450. if ctx['num'] == idx:
  3451. logger.info("Test: Not enough room for SPI in SAi1")
  3452. ike = struct.pack(">BBHBBHBBBB", 0, 0, 4 + 8,
  3453. 0, 0, 8, 1, 1, 1, 0)
  3454. return build_ike(ctx['id'], next=33, ike=ike)
  3455. idx += 1
  3456. if ctx['num'] == idx:
  3457. logger.info("Test: Unexpected SPI in SAi1")
  3458. ike = struct.pack(">BBHBBHBBBBB", 0, 0, 4 + 9,
  3459. 0, 0, 9, 1, 1, 1, 0, 1)
  3460. return build_ike(ctx['id'], next=33, ike=ike)
  3461. idx += 1
  3462. if ctx['num'] == idx:
  3463. logger.info("Test: No transforms in SAi1")
  3464. ike = struct.pack(">BBHBBHBBBB", 0, 0, 4 + 8,
  3465. 0, 0, 8, 1, 1, 0, 0)
  3466. return build_ike(ctx['id'], next=33, ike=ike)
  3467. idx += 1
  3468. if ctx['num'] == idx:
  3469. logger.info("Test: Too short transform in SAi1")
  3470. ike = struct.pack(">BBHBBHBBBB", 0, 0, 4 + 8,
  3471. 0, 0, 8, 1, 1, 0, 1)
  3472. return build_ike(ctx['id'], next=33, ike=ike)
  3473. idx += 1
  3474. if ctx['num'] == idx:
  3475. logger.info("Test: Too small transform length in SAi1")
  3476. ike = struct.pack(">BBHBBHBBBBBBHBBH", 0, 0, 4 + 8 + 8,
  3477. 0, 0, 8 + 8, 1, 1, 0, 1,
  3478. 0, 0, 7, 0, 0, 0)
  3479. return build_ike(ctx['id'], next=33, ike=ike)
  3480. idx += 1
  3481. if ctx['num'] == idx:
  3482. logger.info("Test: Too large transform length in SAi1")
  3483. ike = struct.pack(">BBHBBHBBBBBBHBBH", 0, 0, 4 + 8 + 8,
  3484. 0, 0, 8 + 8, 1, 1, 0, 1,
  3485. 0, 0, 9, 0, 0, 0)
  3486. return build_ike(ctx['id'], next=33, ike=ike)
  3487. idx += 1
  3488. if ctx['num'] == idx:
  3489. logger.info("Test: Unexpected Transform type in SAi1")
  3490. ike = struct.pack(">BBHBBHBBBBBBHBBH", 0, 0, 4 + 8 + 8,
  3491. 0, 0, 8 + 8, 1, 1, 0, 1,
  3492. 1, 0, 8, 0, 0, 0)
  3493. return build_ike(ctx['id'], next=33, ike=ike)
  3494. idx += 1
  3495. if ctx['num'] == idx:
  3496. logger.info("Test: No transform attributes in SAi1")
  3497. ike = struct.pack(">BBHBBHBBBBBBHBBH", 0, 0, 4 + 8 + 8,
  3498. 0, 0, 8 + 8, 1, 1, 0, 1,
  3499. 0, 0, 8, 0, 0, 0)
  3500. return build_ike(ctx['id'], next=33, ike=ike)
  3501. idx += 1
  3502. if ctx['num'] == idx:
  3503. logger.info("Test: No transform attr for AES and unexpected data after transforms in SAi1")
  3504. tlen1 = 8 + 3
  3505. tlen2 = 8 + 4
  3506. tlen3 = 8 + 4
  3507. tlen = tlen1 + tlen2 + tlen3
  3508. ike = struct.pack(">BBHBBHBBBBBBHBBH3BBBHBBHHHBBHBBHHHB",
  3509. 0, 0, 4 + 8 + tlen + 1,
  3510. 0, 0, 8 + tlen + 1, 1, 1, 0, 3,
  3511. 3, 0, tlen1, 1, 0, 12, 1, 2, 3,
  3512. 3, 0, tlen2, 1, 0, 12, 0, 128,
  3513. 0, 0, tlen3, 1, 0, 12, 0x8000 | 14, 127,
  3514. 1)
  3515. return build_ike(ctx['id'], next=33, ike=ike)
  3516. def build_sa(next=0):
  3517. tlen = 5 * 8
  3518. return struct.pack(">BBHBBHBBBBBBHBBHBBHBBHBBHBBHBBHBBHBBHBBH",
  3519. next, 0, 4 + 8 + tlen,
  3520. 0, 0, 8 + tlen, 1, 1, 0, 5,
  3521. 3, 0, 8, 1, 0, 3,
  3522. 3, 0, 8, 2, 0, 1,
  3523. 3, 0, 8, 3, 0, 1,
  3524. 3, 0, 8, 4, 0, 5,
  3525. 0, 0, 8, 241, 0, 0)
  3526. idx += 1
  3527. if ctx['num'] == idx:
  3528. logger.info("Test: Valid proposal, but no KEi in SAi1")
  3529. ike = build_sa()
  3530. return build_ike(ctx['id'], next=33, ike=ike)
  3531. idx += 1
  3532. if ctx['num'] == idx:
  3533. logger.info("Test: Empty KEi in SAi1")
  3534. ike = build_sa(next=34) + struct.pack(">BBH", 0, 0, 4)
  3535. return build_ike(ctx['id'], next=33, ike=ike)
  3536. idx += 1
  3537. if ctx['num'] == idx:
  3538. logger.info("Test: Mismatch in DH Group in SAi1")
  3539. ike = build_sa(next=34)
  3540. ike += struct.pack(">BBHHH", 0, 0, 4 + 4 + 96, 12345, 0)
  3541. ike += 96*'\x00'
  3542. return build_ike(ctx['id'], next=33, ike=ike)
  3543. idx += 1
  3544. if ctx['num'] == idx:
  3545. logger.info("Test: EAP-Failure")
  3546. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  3547. idx += 1
  3548. if ctx['num'] == idx:
  3549. logger.info("Test: Invalid DH public value length in SAi1")
  3550. ike = build_sa(next=34)
  3551. ike += struct.pack(">BBHHH", 0, 0, 4 + 4 + 96, 5, 0)
  3552. ike += 96*'\x00'
  3553. return build_ike(ctx['id'], next=33, ike=ike)
  3554. def build_ke(next=0):
  3555. ke = struct.pack(">BBHHH", next, 0, 4 + 4 + 192, 5, 0)
  3556. ke += 192*'\x00'
  3557. return ke
  3558. idx += 1
  3559. if ctx['num'] == idx:
  3560. logger.info("Test: Valid proposal and KEi, but no Ni in SAi1")
  3561. ike = build_sa(next=34)
  3562. ike += build_ke()
  3563. return build_ike(ctx['id'], next=33, ike=ike)
  3564. idx += 1
  3565. if ctx['num'] == idx:
  3566. logger.info("Test: Too short Ni in SAi1")
  3567. ike = build_sa(next=34)
  3568. ike += build_ke(next=40)
  3569. ike += struct.pack(">BBH", 0, 0, 4)
  3570. return build_ike(ctx['id'], next=33, ike=ike)
  3571. idx += 1
  3572. if ctx['num'] == idx:
  3573. logger.info("Test: Too long Ni in SAi1")
  3574. ike = build_sa(next=34)
  3575. ike += build_ke(next=40)
  3576. ike += struct.pack(">BBH", 0, 0, 4 + 257) + 257*'\x00'
  3577. return build_ike(ctx['id'], next=33, ike=ike)
  3578. def build_ni(next=0):
  3579. return struct.pack(">BBH", next, 0, 4 + 256) + 256*'\x00'
  3580. def build_sai1(id):
  3581. ike = build_sa(next=34)
  3582. ike += build_ke(next=40)
  3583. ike += build_ni()
  3584. return build_ike(ctx['id'], next=33, ike=ike)
  3585. idx += 1
  3586. if ctx['num'] == idx:
  3587. logger.info("Test: Valid proposal, KEi, and Ni in SAi1")
  3588. return build_sai1(ctx['id'])
  3589. idx += 1
  3590. if ctx['num'] == idx:
  3591. logger.info("Test: EAP-Failure")
  3592. return struct.pack(">BBH", EAP_CODE_FAILURE, ctx['id'], 4)
  3593. idx += 1
  3594. if ctx['num'] == idx:
  3595. logger.info("Test: Valid proposal, KEi, and Ni in SAi1")
  3596. return build_sai1(ctx['id'])
  3597. idx += 1
  3598. if ctx['num'] == idx:
  3599. logger.info("Test: No integrity checksum")
  3600. ike = ''
  3601. return build_ike(ctx['id'], next=37, ike=ike)
  3602. idx += 1
  3603. if ctx['num'] == idx:
  3604. logger.info("Test: Valid proposal, KEi, and Ni in SAi1")
  3605. return build_sai1(ctx['id'])
  3606. idx += 1
  3607. if ctx['num'] == idx:
  3608. logger.info("Test: Truncated integrity checksum")
  3609. return struct.pack(">BBHBB",
  3610. EAP_CODE_REQUEST, ctx['id'],
  3611. 4 + 1 + 1,
  3612. EAP_TYPE_IKEV2, 0x20)
  3613. idx += 1
  3614. if ctx['num'] == idx:
  3615. logger.info("Test: Valid proposal, KEi, and Ni in SAi1")
  3616. return build_sai1(ctx['id'])
  3617. idx += 1
  3618. if ctx['num'] == idx:
  3619. logger.info("Test: Invalid integrity checksum")
  3620. ike = ''
  3621. return build_ike(ctx['id'], next=37, flags=0x20, ike=ike)
  3622. return None
  3623. srv = start_radius_server(ikev2_handler)
  3624. try:
  3625. hapd = start_ap(apdev[0]['ifname'])
  3626. for i in range(49):
  3627. dev[0].connect("eap-test", key_mgmt="WPA-EAP", scan_freq="2412",
  3628. eap="IKEV2", identity="user",
  3629. password="password",
  3630. wait_connect=False)
  3631. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"],
  3632. timeout=15)
  3633. if ev is None:
  3634. raise Exception("Timeout on EAP start")
  3635. if i in [ 40, 45 ]:
  3636. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"],
  3637. timeout=10)
  3638. if ev is None:
  3639. raise Exception("Timeout on EAP failure")
  3640. else:
  3641. time.sleep(0.05)
  3642. dev[0].request("REMOVE_NETWORK all")
  3643. finally:
  3644. stop_radius_server(srv)