test_eap_proto.py 167 KB

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