test_ap_psk.py 104 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642
  1. # WPA2-Personal tests
  2. # Copyright (c) 2014, Qualcomm Atheros, Inc.
  3. #
  4. # This software may be distributed under the terms of the BSD license.
  5. # See README for more details.
  6. from remotehost import remote_compatible
  7. import binascii
  8. from Crypto.Cipher import AES
  9. import hashlib
  10. import hmac
  11. import logging
  12. logger = logging.getLogger()
  13. import os
  14. import re
  15. import struct
  16. import subprocess
  17. import time
  18. import hostapd
  19. from utils import HwsimSkip, fail_test, skip_with_fips
  20. import hwsim_utils
  21. from wpasupplicant import WpaSupplicant
  22. def check_mib(dev, vals):
  23. mib = dev.get_mib()
  24. for v in vals:
  25. if mib[v[0]] != v[1]:
  26. raise Exception("Unexpected {} = {} (expected {})".format(v[0], mib[v[0]], v[1]))
  27. @remote_compatible
  28. def test_ap_wpa2_psk(dev, apdev):
  29. """WPA2-PSK AP with PSK instead of passphrase"""
  30. ssid = "test-wpa2-psk"
  31. passphrase = 'qwertyuiop'
  32. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  33. params = hostapd.wpa2_params(ssid=ssid)
  34. params['wpa_psk'] = psk
  35. hapd = hostapd.add_ap(apdev[0], params)
  36. key_mgmt = hapd.get_config()['key_mgmt']
  37. if key_mgmt.split(' ')[0] != "WPA-PSK":
  38. raise Exception("Unexpected GET_CONFIG(key_mgmt): " + key_mgmt)
  39. dev[0].connect(ssid, raw_psk=psk, scan_freq="2412")
  40. dev[1].connect(ssid, psk=passphrase, scan_freq="2412")
  41. sig = dev[0].request("SIGNAL_POLL").splitlines()
  42. pkt = dev[0].request("PKTCNT_POLL").splitlines()
  43. if "FREQUENCY=2412" not in sig:
  44. raise Exception("Unexpected SIGNAL_POLL value: " + str(sig))
  45. if "TXBAD=0" not in pkt:
  46. raise Exception("Unexpected TXBAD value: " + str(pkt))
  47. def test_ap_wpa2_psk_file(dev, apdev):
  48. """WPA2-PSK AP with PSK from a file"""
  49. ssid = "test-wpa2-psk"
  50. passphrase = 'qwertyuiop'
  51. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  52. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  53. params['wpa_psk_file'] = 'hostapd.wpa_psk'
  54. hostapd.add_ap(apdev[0], params)
  55. dev[1].connect(ssid, psk="very secret", scan_freq="2412", wait_connect=False)
  56. dev[2].connect(ssid, raw_psk=psk, scan_freq="2412")
  57. dev[2].request("REMOVE_NETWORK all")
  58. dev[0].connect(ssid, psk="very secret", scan_freq="2412")
  59. dev[0].request("REMOVE_NETWORK all")
  60. dev[2].connect(ssid, psk="another passphrase for all STAs", scan_freq="2412")
  61. dev[0].connect(ssid, psk="another passphrase for all STAs", scan_freq="2412")
  62. ev = dev[1].wait_event(["WPA: 4-Way Handshake failed"], timeout=10)
  63. if ev is None:
  64. raise Exception("Timed out while waiting for failure report")
  65. dev[1].request("REMOVE_NETWORK all")
  66. @remote_compatible
  67. def test_ap_wpa2_psk_mem(dev, apdev):
  68. """WPA2-PSK AP with passphrase only in memory"""
  69. try:
  70. _test_ap_wpa2_psk_mem(dev, apdev)
  71. finally:
  72. dev[0].request("SCAN_INTERVAL 5")
  73. dev[1].request("SCAN_INTERVAL 5")
  74. def _test_ap_wpa2_psk_mem(dev, apdev):
  75. ssid = "test-wpa2-psk"
  76. passphrase = 'qwertyuiop'
  77. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  78. params = hostapd.wpa2_params(ssid=ssid)
  79. params['wpa_psk'] = psk
  80. hapd = hostapd.add_ap(apdev[0], params)
  81. dev[0].connect(ssid, mem_only_psk="1", scan_freq="2412", wait_connect=False)
  82. dev[0].request("SCAN_INTERVAL 1")
  83. ev = dev[0].wait_event(["CTRL-REQ-PSK_PASSPHRASE"], timeout=10)
  84. if ev is None:
  85. raise Exception("Request for PSK/passphrase timed out")
  86. id = ev.split(':')[0].split('-')[-1]
  87. dev[0].request("CTRL-RSP-PSK_PASSPHRASE-" + id + ':"' + passphrase + '"')
  88. dev[0].wait_connected(timeout=10)
  89. dev[1].connect(ssid, mem_only_psk="1", scan_freq="2412", wait_connect=False)
  90. dev[1].request("SCAN_INTERVAL 1")
  91. ev = dev[1].wait_event(["CTRL-REQ-PSK_PASSPHRASE"], timeout=10)
  92. if ev is None:
  93. raise Exception("Request for PSK/passphrase timed out(2)")
  94. id = ev.split(':')[0].split('-')[-1]
  95. dev[1].request("CTRL-RSP-PSK_PASSPHRASE-" + id + ':' + psk)
  96. dev[1].wait_connected(timeout=10)
  97. @remote_compatible
  98. def test_ap_wpa2_ptk_rekey(dev, apdev):
  99. """WPA2-PSK AP and PTK rekey enforced by station"""
  100. ssid = "test-wpa2-psk"
  101. passphrase = 'qwertyuiop'
  102. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  103. hapd = hostapd.add_ap(apdev[0], params)
  104. dev[0].connect(ssid, psk=passphrase, wpa_ptk_rekey="1", scan_freq="2412")
  105. ev = dev[0].wait_event(["WPA: Key negotiation completed"])
  106. if ev is None:
  107. raise Exception("PTK rekey timed out")
  108. hwsim_utils.test_connectivity(dev[0], hapd)
  109. def test_ap_wpa2_ptk_rekey_anonce(dev, apdev):
  110. """WPA2-PSK AP and PTK rekey enforced by station and ANonce change"""
  111. ssid = "test-wpa2-psk"
  112. passphrase = 'qwertyuiop'
  113. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  114. hapd = hostapd.add_ap(apdev[0], params)
  115. dev[0].connect(ssid, psk=passphrase, wpa_ptk_rekey="1", scan_freq="2412")
  116. dev[0].dump_monitor()
  117. anonce1 = dev[0].request("GET anonce")
  118. if "OK" not in dev[0].request("KEY_REQUEST 0 1"):
  119. raise Exception("KEY_REQUEST failed")
  120. ev = dev[0].wait_event(["WPA: Key negotiation completed"])
  121. if ev is None:
  122. raise Exception("PTK rekey timed out")
  123. anonce2 = dev[0].request("GET anonce")
  124. if anonce1 == anonce2:
  125. raise Exception("AP did not update ANonce in requested PTK rekeying")
  126. hwsim_utils.test_connectivity(dev[0], hapd)
  127. @remote_compatible
  128. def test_ap_wpa2_ptk_rekey_ap(dev, apdev):
  129. """WPA2-PSK AP and PTK rekey enforced by AP"""
  130. ssid = "test-wpa2-psk"
  131. passphrase = 'qwertyuiop'
  132. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  133. params['wpa_ptk_rekey'] = '2'
  134. hapd = hostapd.add_ap(apdev[0], params)
  135. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  136. ev = dev[0].wait_event(["WPA: Key negotiation completed"])
  137. if ev is None:
  138. raise Exception("PTK rekey timed out")
  139. hwsim_utils.test_connectivity(dev[0], hapd)
  140. @remote_compatible
  141. def test_ap_wpa2_sha256_ptk_rekey(dev, apdev):
  142. """WPA2-PSK/SHA256 AKM AP and PTK rekey enforced by station"""
  143. ssid = "test-wpa2-psk"
  144. passphrase = 'qwertyuiop'
  145. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  146. params["wpa_key_mgmt"] = "WPA-PSK-SHA256"
  147. hapd = hostapd.add_ap(apdev[0], params)
  148. dev[0].connect(ssid, psk=passphrase, key_mgmt="WPA-PSK-SHA256",
  149. wpa_ptk_rekey="1", scan_freq="2412")
  150. ev = dev[0].wait_event(["WPA: Key negotiation completed"])
  151. if ev is None:
  152. raise Exception("PTK rekey timed out")
  153. hwsim_utils.test_connectivity(dev[0], hapd)
  154. check_mib(dev[0], [ ("dot11RSNAAuthenticationSuiteRequested", "00-0f-ac-6"),
  155. ("dot11RSNAAuthenticationSuiteSelected", "00-0f-ac-6") ])
  156. @remote_compatible
  157. def test_ap_wpa2_sha256_ptk_rekey_ap(dev, apdev):
  158. """WPA2-PSK/SHA256 AKM AP and PTK rekey enforced by AP"""
  159. ssid = "test-wpa2-psk"
  160. passphrase = 'qwertyuiop'
  161. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  162. params["wpa_key_mgmt"] = "WPA-PSK-SHA256"
  163. params['wpa_ptk_rekey'] = '2'
  164. hapd = hostapd.add_ap(apdev[0], params)
  165. dev[0].connect(ssid, psk=passphrase, key_mgmt="WPA-PSK-SHA256",
  166. scan_freq="2412")
  167. ev = dev[0].wait_event(["WPA: Key negotiation completed"])
  168. if ev is None:
  169. raise Exception("PTK rekey timed out")
  170. hwsim_utils.test_connectivity(dev[0], hapd)
  171. check_mib(dev[0], [ ("dot11RSNAAuthenticationSuiteRequested", "00-0f-ac-6"),
  172. ("dot11RSNAAuthenticationSuiteSelected", "00-0f-ac-6") ])
  173. @remote_compatible
  174. def test_ap_wpa_ptk_rekey(dev, apdev):
  175. """WPA-PSK/TKIP AP and PTK rekey enforced by station"""
  176. skip_with_fips(dev[0])
  177. ssid = "test-wpa-psk"
  178. passphrase = 'qwertyuiop'
  179. params = hostapd.wpa_params(ssid=ssid, passphrase=passphrase)
  180. hapd = hostapd.add_ap(apdev[0], params)
  181. dev[0].connect(ssid, psk=passphrase, wpa_ptk_rekey="1", scan_freq="2412")
  182. if "[WPA-PSK-TKIP]" not in dev[0].request("SCAN_RESULTS"):
  183. raise Exception("Scan results missing WPA element info")
  184. ev = dev[0].wait_event(["WPA: Key negotiation completed"])
  185. if ev is None:
  186. raise Exception("PTK rekey timed out")
  187. hwsim_utils.test_connectivity(dev[0], hapd)
  188. @remote_compatible
  189. def test_ap_wpa_ptk_rekey_ap(dev, apdev):
  190. """WPA-PSK/TKIP AP and PTK rekey enforced by AP"""
  191. skip_with_fips(dev[0])
  192. ssid = "test-wpa-psk"
  193. passphrase = 'qwertyuiop'
  194. params = hostapd.wpa_params(ssid=ssid, passphrase=passphrase)
  195. params['wpa_ptk_rekey'] = '2'
  196. hapd = hostapd.add_ap(apdev[0], params)
  197. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  198. ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=10)
  199. if ev is None:
  200. raise Exception("PTK rekey timed out")
  201. hwsim_utils.test_connectivity(dev[0], hapd)
  202. @remote_compatible
  203. def test_ap_wpa_ccmp(dev, apdev):
  204. """WPA-PSK/CCMP"""
  205. ssid = "test-wpa-psk"
  206. passphrase = 'qwertyuiop'
  207. params = hostapd.wpa_params(ssid=ssid, passphrase=passphrase)
  208. params['wpa_pairwise'] = "CCMP"
  209. hapd = hostapd.add_ap(apdev[0], params)
  210. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  211. hwsim_utils.test_connectivity(dev[0], hapd)
  212. check_mib(dev[0], [ ("dot11RSNAConfigGroupCipherSize", "128"),
  213. ("dot11RSNAGroupCipherRequested", "00-50-f2-4"),
  214. ("dot11RSNAPairwiseCipherRequested", "00-50-f2-4"),
  215. ("dot11RSNAAuthenticationSuiteRequested", "00-50-f2-2"),
  216. ("dot11RSNAGroupCipherSelected", "00-50-f2-4"),
  217. ("dot11RSNAPairwiseCipherSelected", "00-50-f2-4"),
  218. ("dot11RSNAAuthenticationSuiteSelected", "00-50-f2-2"),
  219. ("dot1xSuppSuppControlledPortStatus", "Authorized") ])
  220. def test_ap_wpa2_psk_file_errors(dev, apdev):
  221. """WPA2-PSK AP with various PSK file error and success cases"""
  222. addr0 = dev[0].own_addr()
  223. addr1 = dev[1].own_addr()
  224. addr2 = dev[2].own_addr()
  225. ssid = "psk"
  226. pskfile = "/tmp/ap_wpa2_psk_file_errors.psk_file"
  227. try:
  228. os.remove(pskfile)
  229. except:
  230. pass
  231. params = { "ssid": ssid, "wpa": "2", "wpa_key_mgmt": "WPA-PSK",
  232. "rsn_pairwise": "CCMP", "wpa_psk_file": pskfile }
  233. try:
  234. # missing PSK file
  235. hapd = hostapd.add_ap(apdev[0], params, no_enable=True)
  236. if "FAIL" not in hapd.request("ENABLE"):
  237. raise Exception("Unexpected ENABLE success")
  238. hapd.request("DISABLE")
  239. # invalid MAC address
  240. with open(pskfile, "w") as f:
  241. f.write("\n")
  242. f.write("foo\n")
  243. if "FAIL" not in hapd.request("ENABLE"):
  244. raise Exception("Unexpected ENABLE success")
  245. hapd.request("DISABLE")
  246. # no PSK on line
  247. with open(pskfile, "w") as f:
  248. f.write("00:11:22:33:44:55\n")
  249. if "FAIL" not in hapd.request("ENABLE"):
  250. raise Exception("Unexpected ENABLE success")
  251. hapd.request("DISABLE")
  252. # invalid PSK
  253. with open(pskfile, "w") as f:
  254. f.write("00:11:22:33:44:55 1234567\n")
  255. if "FAIL" not in hapd.request("ENABLE"):
  256. raise Exception("Unexpected ENABLE success")
  257. hapd.request("DISABLE")
  258. # valid PSK file
  259. with open(pskfile, "w") as f:
  260. f.write("00:11:22:33:44:55 12345678\n")
  261. f.write(addr0 + " 123456789\n")
  262. f.write(addr1 + " 123456789a\n")
  263. f.write(addr2 + " 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef\n")
  264. if "FAIL" in hapd.request("ENABLE"):
  265. raise Exception("Unexpected ENABLE failure")
  266. dev[0].connect(ssid, psk="123456789", scan_freq="2412")
  267. dev[1].connect(ssid, psk="123456789a", scan_freq="2412")
  268. dev[2].connect(ssid, raw_psk="0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", scan_freq="2412")
  269. finally:
  270. try:
  271. os.remove(pskfile)
  272. except:
  273. pass
  274. @remote_compatible
  275. def test_ap_wpa2_psk_wildcard_ssid(dev, apdev):
  276. """WPA2-PSK AP and wildcard SSID configuration"""
  277. ssid = "test-wpa2-psk"
  278. passphrase = 'qwertyuiop'
  279. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  280. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  281. hapd = hostapd.add_ap(apdev[0], params)
  282. dev[0].connect("", bssid=apdev[0]['bssid'], psk=passphrase,
  283. scan_freq="2412")
  284. dev[1].connect("", bssid=apdev[0]['bssid'], raw_psk=psk, scan_freq="2412")
  285. @remote_compatible
  286. def test_ap_wpa2_gtk_rekey(dev, apdev):
  287. """WPA2-PSK AP and GTK rekey enforced by AP"""
  288. ssid = "test-wpa2-psk"
  289. passphrase = 'qwertyuiop'
  290. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  291. params['wpa_group_rekey'] = '1'
  292. hapd = hostapd.add_ap(apdev[0], params)
  293. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  294. ev = dev[0].wait_event(["WPA: Group rekeying completed"], timeout=2)
  295. if ev is None:
  296. raise Exception("GTK rekey timed out")
  297. hwsim_utils.test_connectivity(dev[0], hapd)
  298. @remote_compatible
  299. def test_ap_wpa_gtk_rekey(dev, apdev):
  300. """WPA-PSK/TKIP AP and GTK rekey enforced by AP"""
  301. skip_with_fips(dev[0])
  302. ssid = "test-wpa-psk"
  303. passphrase = 'qwertyuiop'
  304. params = hostapd.wpa_params(ssid=ssid, passphrase=passphrase)
  305. params['wpa_group_rekey'] = '1'
  306. hapd = hostapd.add_ap(apdev[0], params)
  307. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  308. ev = dev[0].wait_event(["WPA: Group rekeying completed"], timeout=2)
  309. if ev is None:
  310. raise Exception("GTK rekey timed out")
  311. hwsim_utils.test_connectivity(dev[0], hapd)
  312. @remote_compatible
  313. def test_ap_wpa2_gmk_rekey(dev, apdev):
  314. """WPA2-PSK AP and GMK and GTK rekey enforced by AP"""
  315. ssid = "test-wpa2-psk"
  316. passphrase = 'qwertyuiop'
  317. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  318. params['wpa_group_rekey'] = '1'
  319. params['wpa_gmk_rekey'] = '2'
  320. hapd = hostapd.add_ap(apdev[0], params)
  321. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  322. for i in range(0, 3):
  323. ev = dev[0].wait_event(["WPA: Group rekeying completed"], timeout=2)
  324. if ev is None:
  325. raise Exception("GTK rekey timed out")
  326. hwsim_utils.test_connectivity(dev[0], hapd)
  327. @remote_compatible
  328. def test_ap_wpa2_strict_rekey(dev, apdev):
  329. """WPA2-PSK AP and strict GTK rekey enforced by AP"""
  330. ssid = "test-wpa2-psk"
  331. passphrase = 'qwertyuiop'
  332. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  333. params['wpa_strict_rekey'] = '1'
  334. hapd = hostapd.add_ap(apdev[0], params)
  335. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  336. dev[1].connect(ssid, psk=passphrase, scan_freq="2412")
  337. dev[1].request("DISCONNECT")
  338. ev = dev[0].wait_event(["WPA: Group rekeying completed"], timeout=2)
  339. if ev is None:
  340. raise Exception("GTK rekey timed out")
  341. hwsim_utils.test_connectivity(dev[0], hapd)
  342. @remote_compatible
  343. def test_ap_wpa2_bridge_fdb(dev, apdev):
  344. """Bridge FDB entry removal"""
  345. hapd = None
  346. try:
  347. ssid = "test-wpa2-psk"
  348. passphrase = "12345678"
  349. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  350. params['bridge'] = 'ap-br0'
  351. hapd = hostapd.add_ap(apdev[0], params)
  352. hapd.cmd_execute(['brctl', 'setfd', 'ap-br0', '0'])
  353. hapd.cmd_execute(['ip', 'link', 'set', 'dev', 'ap-br0', 'up'])
  354. dev[0].connect(ssid, psk=passphrase, scan_freq="2412",
  355. bssid=apdev[0]['bssid'])
  356. dev[1].connect(ssid, psk=passphrase, scan_freq="2412",
  357. bssid=apdev[0]['bssid'])
  358. addr0 = dev[0].p2p_interface_addr()
  359. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  360. err, macs1 = hapd.cmd_execute(['brctl', 'showmacs', 'ap-br0'])
  361. hapd.cmd_execute(['brctl', 'setageing', 'ap-br0', '1'])
  362. dev[0].request("DISCONNECT")
  363. dev[1].request("DISCONNECT")
  364. time.sleep(1)
  365. err, macs2 = hapd.cmd_execute(['brctl', 'showmacs', 'ap-br0'])
  366. addr1 = dev[1].p2p_interface_addr()
  367. if addr0 not in macs1 or addr1 not in macs1:
  368. raise Exception("Bridge FDB entry missing")
  369. if addr0 in macs2 or addr1 in macs2:
  370. raise Exception("Bridge FDB entry was not removed")
  371. finally:
  372. hostapd.cmd_execute(apdev[0], ['ip', 'link', 'set', 'dev', 'ap-br0',
  373. 'down'])
  374. hostapd.cmd_execute(apdev[0], ['brctl', 'delbr', 'ap-br0'])
  375. @remote_compatible
  376. def test_ap_wpa2_already_in_bridge(dev, apdev):
  377. """hostapd behavior with interface already in bridge"""
  378. ifname = apdev[0]['ifname']
  379. br_ifname = 'ext-ap-br0'
  380. try:
  381. ssid = "test-wpa2-psk"
  382. passphrase = "12345678"
  383. hostapd.cmd_execute(apdev[0], ['brctl', 'addbr', br_ifname])
  384. hostapd.cmd_execute(apdev[0], ['brctl', 'setfd', br_ifname, '0'])
  385. hostapd.cmd_execute(apdev[0], ['ip', 'link', 'set', 'dev', br_ifname,
  386. 'up'])
  387. hostapd.cmd_execute(apdev[0], ['iw', ifname, 'set', 'type', '__ap'])
  388. hostapd.cmd_execute(apdev[0], ['brctl', 'addif', br_ifname, ifname])
  389. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  390. hapd = hostapd.add_ap(apdev[0], params)
  391. if hapd.get_driver_status_field('brname') != br_ifname:
  392. raise Exception("Bridge name not identified correctly")
  393. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  394. finally:
  395. hostapd.cmd_execute(apdev[0], ['ip', 'link', 'set', 'dev', br_ifname,
  396. 'down'])
  397. hostapd.cmd_execute(apdev[0], ['brctl', 'delif', br_ifname, ifname])
  398. hostapd.cmd_execute(apdev[0], ['iw', ifname, 'set', 'type', 'station'])
  399. hostapd.cmd_execute(apdev[0], ['brctl', 'delbr', br_ifname])
  400. @remote_compatible
  401. def test_ap_wpa2_in_different_bridge(dev, apdev):
  402. """hostapd behavior with interface in different bridge"""
  403. ifname = apdev[0]['ifname']
  404. br_ifname = 'ext-ap-br0'
  405. try:
  406. ssid = "test-wpa2-psk"
  407. passphrase = "12345678"
  408. hostapd.cmd_execute(apdev[0], ['brctl', 'addbr', br_ifname])
  409. hostapd.cmd_execute(apdev[0], ['brctl', 'setfd', br_ifname, '0'])
  410. hostapd.cmd_execute(apdev[0], ['ip', 'link', 'set', 'dev', br_ifname,
  411. 'up'])
  412. hostapd.cmd_execute(apdev[0], ['iw', ifname, 'set', 'type', '__ap'])
  413. hostapd.cmd_execute(apdev[0], ['brctl', 'addif', br_ifname, ifname])
  414. time.sleep(0.5)
  415. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  416. params['bridge'] = 'ap-br0'
  417. hapd = hostapd.add_ap(apdev[0], params)
  418. hostapd.cmd_execute(apdev[0], ['brctl', 'setfd', 'ap-br0', '0'])
  419. hostapd.cmd_execute(apdev[0], ['ip', 'link', 'set', 'dev', 'ap-br0',
  420. 'up'])
  421. brname = hapd.get_driver_status_field('brname')
  422. if brname != 'ap-br0':
  423. raise Exception("Incorrect bridge: " + brname)
  424. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  425. hwsim_utils.test_connectivity_iface(dev[0], hapd, "ap-br0")
  426. if hapd.get_driver_status_field("added_bridge") != "1":
  427. raise Exception("Unexpected added_bridge value")
  428. if hapd.get_driver_status_field("added_if_into_bridge") != "1":
  429. raise Exception("Unexpected added_if_into_bridge value")
  430. dev[0].request("DISCONNECT")
  431. hapd.disable()
  432. finally:
  433. hostapd.cmd_execute(apdev[0], ['ip', 'link', 'set', 'dev', br_ifname,
  434. 'down'])
  435. hostapd.cmd_execute(apdev[0], ['brctl', 'delif', br_ifname, ifname,
  436. "2>", "/dev/null"], shell=True)
  437. hostapd.cmd_execute(apdev[0], ['brctl', 'delbr', br_ifname])
  438. @remote_compatible
  439. def test_ap_wpa2_ext_add_to_bridge(dev, apdev):
  440. """hostapd behavior with interface added to bridge externally"""
  441. ifname = apdev[0]['ifname']
  442. br_ifname = 'ext-ap-br0'
  443. try:
  444. ssid = "test-wpa2-psk"
  445. passphrase = "12345678"
  446. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  447. hapd = hostapd.add_ap(apdev[0], params)
  448. hostapd.cmd_execute(apdev[0], ['brctl', 'addbr', br_ifname])
  449. hostapd.cmd_execute(apdev[0], ['brctl', 'setfd', br_ifname, '0'])
  450. hostapd.cmd_execute(apdev[0], ['ip', 'link', 'set', 'dev', br_ifname,
  451. 'up'])
  452. hostapd.cmd_execute(apdev[0], ['brctl', 'addif', br_ifname, ifname])
  453. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  454. if hapd.get_driver_status_field('brname') != br_ifname:
  455. raise Exception("Bridge name not identified correctly")
  456. finally:
  457. hostapd.cmd_execute(apdev[0], ['ip', 'link', 'set', 'dev', br_ifname,
  458. 'down'])
  459. hostapd.cmd_execute(apdev[0], ['brctl', 'delif', br_ifname, ifname])
  460. hostapd.cmd_execute(apdev[0], ['brctl', 'delbr', br_ifname])
  461. def test_ap_wpa2_psk_ext(dev, apdev):
  462. """WPA2-PSK AP using external EAPOL I/O"""
  463. bssid = apdev[0]['bssid']
  464. ssid = "test-wpa2-psk"
  465. passphrase = 'qwertyuiop'
  466. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  467. params = hostapd.wpa2_params(ssid=ssid)
  468. params['wpa_psk'] = psk
  469. hapd = hostapd.add_ap(apdev[0], params)
  470. hapd.request("SET ext_eapol_frame_io 1")
  471. dev[0].request("SET ext_eapol_frame_io 1")
  472. dev[0].connect(ssid, psk=passphrase, scan_freq="2412", wait_connect=False)
  473. addr = dev[0].p2p_interface_addr()
  474. while True:
  475. ev = hapd.wait_event(["EAPOL-TX", "AP-STA-CONNECTED"], timeout=15)
  476. if ev is None:
  477. raise Exception("Timeout on EAPOL-TX from hostapd")
  478. if "AP-STA-CONNECTED" in ev:
  479. dev[0].wait_connected(timeout=15)
  480. break
  481. res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2])
  482. if "OK" not in res:
  483. raise Exception("EAPOL_RX to wpa_supplicant failed")
  484. ev = dev[0].wait_event(["EAPOL-TX", "CTRL-EVENT-CONNECTED"], timeout=15)
  485. if ev is None:
  486. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  487. if "CTRL-EVENT-CONNECTED" in ev:
  488. break
  489. res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2])
  490. if "OK" not in res:
  491. raise Exception("EAPOL_RX to hostapd failed")
  492. def test_ap_wpa2_psk_ext_retry_msg_3(dev, apdev):
  493. """WPA2-PSK AP using external EAPOL I/O and retry for EAPOL-Key msg 3/4"""
  494. bssid = apdev[0]['bssid']
  495. ssid = "test-wpa2-psk"
  496. passphrase = 'qwertyuiop'
  497. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  498. params = hostapd.wpa2_params(ssid=ssid)
  499. params['wpa_psk'] = psk
  500. hapd = hostapd.add_ap(apdev[0], params)
  501. hapd.request("SET ext_eapol_frame_io 1")
  502. dev[0].request("SET ext_eapol_frame_io 1")
  503. dev[0].connect(ssid, psk=passphrase, scan_freq="2412", wait_connect=False)
  504. addr = dev[0].p2p_interface_addr()
  505. # EAPOL-Key msg 1/4
  506. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  507. if ev is None:
  508. raise Exception("Timeout on EAPOL-TX from hostapd")
  509. res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2])
  510. if "OK" not in res:
  511. raise Exception("EAPOL_RX to wpa_supplicant failed")
  512. # EAPOL-Key msg 2/4
  513. ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
  514. if ev is None:
  515. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  516. res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2])
  517. if "OK" not in res:
  518. raise Exception("EAPOL_RX to hostapd failed")
  519. # EAPOL-Key msg 3/4
  520. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  521. if ev is None:
  522. raise Exception("Timeout on EAPOL-TX from hostapd")
  523. res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2])
  524. if "OK" not in res:
  525. raise Exception("EAPOL_RX to wpa_supplicant failed")
  526. # EAPOL-Key msg 4/4
  527. ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
  528. if ev is None:
  529. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  530. # Do not send to the AP
  531. dev[0].wait_connected(timeout=15)
  532. # EAPOL-Key msg 3/4 (retry)
  533. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  534. if ev is None:
  535. raise Exception("Timeout on EAPOL-TX from hostapd")
  536. res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2])
  537. if "OK" not in res:
  538. raise Exception("EAPOL_RX to wpa_supplicant failed")
  539. # EAPOL-Key msg 4/4
  540. ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
  541. if ev is None:
  542. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  543. res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2])
  544. if "OK" not in res:
  545. raise Exception("EAPOL_RX to hostapd failed")
  546. ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=15)
  547. if ev is None:
  548. raise Exception("Timeout on AP-STA-CONNECTED from hostapd")
  549. hwsim_utils.test_connectivity(dev[0], hapd)
  550. def test_ap_wpa2_psk_ext_retry_msg_3b(dev, apdev):
  551. """WPA2-PSK AP using external EAPOL I/O and retry for EAPOL-Key msg 3/4 (b)"""
  552. bssid = apdev[0]['bssid']
  553. ssid = "test-wpa2-psk"
  554. passphrase = 'qwertyuiop'
  555. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  556. params = hostapd.wpa2_params(ssid=ssid)
  557. params['wpa_psk'] = psk
  558. hapd = hostapd.add_ap(apdev[0], params)
  559. hapd.request("SET ext_eapol_frame_io 1")
  560. dev[0].request("SET ext_eapol_frame_io 1")
  561. dev[0].connect(ssid, psk=passphrase, scan_freq="2412", wait_connect=False)
  562. addr = dev[0].p2p_interface_addr()
  563. # EAPOL-Key msg 1/4
  564. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  565. if ev is None:
  566. raise Exception("Timeout on EAPOL-TX from hostapd")
  567. res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2])
  568. if "OK" not in res:
  569. raise Exception("EAPOL_RX to wpa_supplicant failed")
  570. # EAPOL-Key msg 2/4
  571. ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
  572. if ev is None:
  573. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  574. res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2])
  575. if "OK" not in res:
  576. raise Exception("EAPOL_RX to hostapd failed")
  577. # EAPOL-Key msg 3/4
  578. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  579. if ev is None:
  580. raise Exception("Timeout on EAPOL-TX from hostapd")
  581. # Do not send the first msg 3/4 to the STA yet; wait for retransmission
  582. # from AP.
  583. msg3_1 = ev
  584. # EAPOL-Key msg 3/4 (retry)
  585. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  586. if ev is None:
  587. raise Exception("Timeout on EAPOL-TX from hostapd")
  588. msg3_2 = ev
  589. # Send the first msg 3/4 to STA
  590. res = dev[0].request("EAPOL_RX " + bssid + " " + msg3_1.split(' ')[2])
  591. if "OK" not in res:
  592. raise Exception("EAPOL_RX to wpa_supplicant failed")
  593. # EAPOL-Key msg 4/4
  594. ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
  595. if ev is None:
  596. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  597. res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2])
  598. if "OK" not in res:
  599. raise Exception("EAPOL_RX to hostapd failed")
  600. dev[0].wait_connected(timeout=15)
  601. ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=15)
  602. if ev is None:
  603. raise Exception("Timeout on AP-STA-CONNECTED from hostapd")
  604. hwsim_utils.test_connectivity(dev[0], hapd)
  605. # Send the second msg 3/4 to STA
  606. res = dev[0].request("EAPOL_RX " + bssid + " " + msg3_2.split(' ')[2])
  607. if "OK" not in res:
  608. raise Exception("EAPOL_RX to wpa_supplicant failed")
  609. # EAPOL-Key msg 4/4
  610. ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
  611. if ev is None:
  612. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  613. # Do not send the second msg 4/4 to the AP
  614. hwsim_utils.test_connectivity(dev[0], hapd)
  615. def test_ap_wpa2_psk_ext_retry_msg_3c(dev, apdev):
  616. """WPA2-PSK AP using external EAPOL I/O and retry for EAPOL-Key msg 3/4 (c)"""
  617. bssid = apdev[0]['bssid']
  618. ssid = "test-wpa2-psk"
  619. passphrase = 'qwertyuiop'
  620. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  621. params = hostapd.wpa2_params(ssid=ssid)
  622. params['wpa_psk'] = psk
  623. hapd = hostapd.add_ap(apdev[0], params)
  624. hapd.request("SET ext_eapol_frame_io 1")
  625. dev[0].request("SET ext_eapol_frame_io 1")
  626. dev[0].connect(ssid, psk=passphrase, scan_freq="2412", wait_connect=False)
  627. addr = dev[0].p2p_interface_addr()
  628. # EAPOL-Key msg 1/4
  629. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  630. if ev is None:
  631. raise Exception("Timeout on EAPOL-TX from hostapd")
  632. msg1 = ev.split(' ')[2]
  633. res = dev[0].request("EAPOL_RX " + bssid + " " + msg1)
  634. if "OK" not in res:
  635. raise Exception("EAPOL_RX to wpa_supplicant failed")
  636. # EAPOL-Key msg 2/4
  637. ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
  638. if ev is None:
  639. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  640. res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2])
  641. if "OK" not in res:
  642. raise Exception("EAPOL_RX to hostapd failed")
  643. # EAPOL-Key msg 3/4
  644. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  645. if ev is None:
  646. raise Exception("Timeout on EAPOL-TX from hostapd")
  647. res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2])
  648. if "OK" not in res:
  649. raise Exception("EAPOL_RX to wpa_supplicant failed")
  650. # EAPOL-Key msg 4/4
  651. ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
  652. if ev is None:
  653. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  654. msg4 = ev.split(' ')[2]
  655. # Do not send msg 4/4 to hostapd to trigger retry
  656. # STA believes everything is ready
  657. dev[0].wait_connected()
  658. # EAPOL-Key msg 3/4 (retry)
  659. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  660. if ev is None:
  661. raise Exception("Timeout on EAPOL-TX from hostapd")
  662. msg3 = ev.split(' ')[2]
  663. # Send a forged msg 1/4 to STA (update replay counter)
  664. msg1b = msg1[0:18] + msg3[18:34] + msg1[34:]
  665. # and replace nonce (this results in "WPA: ANonce from message 1 of
  666. # 4-Way Handshake differs from 3 of 4-Way Handshake - drop packet" when
  667. # wpa_supplicant processed msg 3/4 afterwards)
  668. #msg1b = msg1[0:18] + msg3[18:34] + 32*"ff" + msg1[98:]
  669. res = dev[0].request("EAPOL_RX " + bssid + " " + msg1b)
  670. if "OK" not in res:
  671. raise Exception("EAPOL_RX to wpa_supplicant failed")
  672. # EAPOL-Key msg 2/4
  673. ev = dev[0].wait_event(["EAPOL-TX"], timeout=1)
  674. if ev is None:
  675. # wpa_supplicant seems to have ignored the forged message. This means
  676. # the attack would fail.
  677. logger.info("wpa_supplicant ignored forged EAPOL-Key msg 1/4")
  678. return
  679. # Do not send msg 2/4 to hostapd
  680. # Send previously received msg 3/4 to STA
  681. res = dev[0].request("EAPOL_RX " + bssid + " " + msg3)
  682. if "OK" not in res:
  683. raise Exception("EAPOL_RX to wpa_supplicant failed")
  684. # EAPOL-Key msg 4/4
  685. ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
  686. if ev is None:
  687. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  688. res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2])
  689. if "OK" not in res:
  690. raise Exception("EAPOL_RX to hostapd failed")
  691. ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=15)
  692. if ev is None:
  693. raise Exception("Timeout on AP-STA-CONNECTED from hostapd")
  694. hwsim_utils.test_connectivity(dev[0], hapd)
  695. def test_ap_wpa2_psk_ext_retry_msg_3d(dev, apdev):
  696. """WPA2-PSK AP using external EAPOL I/O and retry for EAPOL-Key msg 3/4 (d)"""
  697. bssid = apdev[0]['bssid']
  698. ssid = "test-wpa2-psk"
  699. passphrase = 'qwertyuiop'
  700. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  701. params = hostapd.wpa2_params(ssid=ssid)
  702. params['wpa_psk'] = psk
  703. hapd = hostapd.add_ap(apdev[0], params)
  704. hapd.request("SET ext_eapol_frame_io 1")
  705. dev[0].request("SET ext_eapol_frame_io 1")
  706. dev[0].connect(ssid, psk=passphrase, scan_freq="2412", wait_connect=False)
  707. addr = dev[0].p2p_interface_addr()
  708. # EAPOL-Key msg 1/4
  709. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  710. if ev is None:
  711. raise Exception("Timeout on EAPOL-TX from hostapd")
  712. msg1 = ev.split(' ')[2]
  713. res = dev[0].request("EAPOL_RX " + bssid + " " + msg1)
  714. if "OK" not in res:
  715. raise Exception("EAPOL_RX to wpa_supplicant failed")
  716. # EAPOL-Key msg 2/4
  717. ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
  718. if ev is None:
  719. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  720. res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2])
  721. if "OK" not in res:
  722. raise Exception("EAPOL_RX to hostapd failed")
  723. # EAPOL-Key msg 3/4
  724. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  725. if ev is None:
  726. raise Exception("Timeout on EAPOL-TX from hostapd")
  727. res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2])
  728. if "OK" not in res:
  729. raise Exception("EAPOL_RX to wpa_supplicant failed")
  730. # EAPOL-Key msg 4/4
  731. ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
  732. if ev is None:
  733. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  734. msg4 = ev.split(' ')[2]
  735. # Do not send msg 4/4 to hostapd to trigger retry
  736. # STA believes everything is ready
  737. dev[0].wait_connected()
  738. # EAPOL-Key msg 3/4 (retry)
  739. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  740. if ev is None:
  741. raise Exception("Timeout on EAPOL-TX from hostapd")
  742. msg3 = ev.split(' ')[2]
  743. # Send a forged msg 1/4 to STA (update replay counter)
  744. msg1b = msg1[0:18] + msg3[18:34] + msg1[34:]
  745. res = dev[0].request("EAPOL_RX " + bssid + " " + msg1b)
  746. if "OK" not in res:
  747. raise Exception("EAPOL_RX to wpa_supplicant failed")
  748. # EAPOL-Key msg 2/4
  749. ev = dev[0].wait_event(["EAPOL-TX"], timeout=1)
  750. if ev is None:
  751. # wpa_supplicant seems to have ignored the forged message. This means
  752. # the attack would fail.
  753. logger.info("wpa_supplicant ignored forged EAPOL-Key msg 1/4")
  754. return
  755. # Do not send msg 2/4 to hostapd
  756. # EAPOL-Key msg 3/4 (retry 2)
  757. # New one needed to get the correct Replay Counter value
  758. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  759. if ev is None:
  760. raise Exception("Timeout on EAPOL-TX from hostapd")
  761. msg3 = ev.split(' ')[2]
  762. # Send msg 3/4 to STA
  763. res = dev[0].request("EAPOL_RX " + bssid + " " + msg3)
  764. if "OK" not in res:
  765. raise Exception("EAPOL_RX to wpa_supplicant failed")
  766. # EAPOL-Key msg 4/4
  767. ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
  768. if ev is None:
  769. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  770. res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2])
  771. if "OK" not in res:
  772. raise Exception("EAPOL_RX to hostapd failed")
  773. ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=15)
  774. if ev is None:
  775. raise Exception("Timeout on AP-STA-CONNECTED from hostapd")
  776. hwsim_utils.test_connectivity(dev[0], hapd)
  777. def test_ap_wpa2_psk_ext_retry_msg_3e(dev, apdev):
  778. """WPA2-PSK AP using external EAPOL I/O and retry for EAPOL-Key msg 3/4 (e)"""
  779. bssid = apdev[0]['bssid']
  780. ssid = "test-wpa2-psk"
  781. passphrase = 'qwertyuiop'
  782. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  783. params = hostapd.wpa2_params(ssid=ssid)
  784. params['wpa_psk'] = psk
  785. hapd = hostapd.add_ap(apdev[0], params)
  786. hapd.request("SET ext_eapol_frame_io 1")
  787. dev[0].request("SET ext_eapol_frame_io 1")
  788. dev[0].connect(ssid, psk=passphrase, scan_freq="2412", wait_connect=False)
  789. addr = dev[0].p2p_interface_addr()
  790. # EAPOL-Key msg 1/4
  791. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  792. if ev is None:
  793. raise Exception("Timeout on EAPOL-TX from hostapd")
  794. msg1 = ev.split(' ')[2]
  795. res = dev[0].request("EAPOL_RX " + bssid + " " + msg1)
  796. if "OK" not in res:
  797. raise Exception("EAPOL_RX to wpa_supplicant failed")
  798. # EAPOL-Key msg 2/4
  799. ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
  800. if ev is None:
  801. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  802. res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2])
  803. if "OK" not in res:
  804. raise Exception("EAPOL_RX to hostapd failed")
  805. # EAPOL-Key msg 3/4
  806. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  807. if ev is None:
  808. raise Exception("Timeout on EAPOL-TX from hostapd")
  809. res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2])
  810. if "OK" not in res:
  811. raise Exception("EAPOL_RX to wpa_supplicant failed")
  812. # EAPOL-Key msg 4/4
  813. ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
  814. if ev is None:
  815. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  816. msg4 = ev.split(' ')[2]
  817. # Do not send msg 4/4 to hostapd to trigger retry
  818. # STA believes everything is ready
  819. dev[0].wait_connected()
  820. # EAPOL-Key msg 3/4 (retry)
  821. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  822. if ev is None:
  823. raise Exception("Timeout on EAPOL-TX from hostapd")
  824. msg3 = ev.split(' ')[2]
  825. # Send a forged msg 1/4 to STA (update replay counter and replace ANonce)
  826. msg1b = msg1[0:18] + msg3[18:34] + 32*"ff" + msg1[98:]
  827. res = dev[0].request("EAPOL_RX " + bssid + " " + msg1b)
  828. if "OK" not in res:
  829. raise Exception("EAPOL_RX to wpa_supplicant failed")
  830. # EAPOL-Key msg 2/4
  831. ev = dev[0].wait_event(["EAPOL-TX"], timeout=1)
  832. if ev is None:
  833. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  834. # Do not send msg 2/4 to hostapd
  835. # Send a forged msg 1/4 to STA (back to previously used ANonce)
  836. msg1b = msg1[0:18] + msg3[18:34] + msg1[34:]
  837. res = dev[0].request("EAPOL_RX " + bssid + " " + msg1b)
  838. if "OK" not in res:
  839. raise Exception("EAPOL_RX to wpa_supplicant failed")
  840. # EAPOL-Key msg 2/4
  841. ev = dev[0].wait_event(["EAPOL-TX"], timeout=1)
  842. if ev is None:
  843. # wpa_supplicant seems to have ignored the forged message. This means
  844. # the attack would fail.
  845. logger.info("wpa_supplicant ignored forged EAPOL-Key msg 1/4")
  846. return
  847. # Do not send msg 2/4 to hostapd
  848. # EAPOL-Key msg 3/4 (retry 2)
  849. # New one needed to get the correct Replay Counter value
  850. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  851. if ev is None:
  852. raise Exception("Timeout on EAPOL-TX from hostapd")
  853. msg3 = ev.split(' ')[2]
  854. # Send msg 3/4 to STA
  855. res = dev[0].request("EAPOL_RX " + bssid + " " + msg3)
  856. if "OK" not in res:
  857. raise Exception("EAPOL_RX to wpa_supplicant failed")
  858. # EAPOL-Key msg 4/4
  859. ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
  860. if ev is None:
  861. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  862. res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2])
  863. if "OK" not in res:
  864. raise Exception("EAPOL_RX to hostapd failed")
  865. ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=15)
  866. if ev is None:
  867. raise Exception("Timeout on AP-STA-CONNECTED from hostapd")
  868. hwsim_utils.test_connectivity(dev[0], hapd)
  869. def parse_eapol(data):
  870. (version, type, length) = struct.unpack('>BBH', data[0:4])
  871. payload = data[4:]
  872. if length > len(payload):
  873. raise Exception("Invalid EAPOL length")
  874. if length < len(payload):
  875. payload = payload[0:length]
  876. eapol = {}
  877. eapol['version'] = version
  878. eapol['type'] = type
  879. eapol['length'] = length
  880. eapol['payload'] = payload
  881. if type == 3:
  882. # EAPOL-Key
  883. (eapol['descr_type'],) = struct.unpack('B', payload[0:1])
  884. payload = payload[1:]
  885. if eapol['descr_type'] == 2 or eapol['descr_type'] == 254:
  886. # RSN EAPOL-Key
  887. (key_info, key_len) = struct.unpack('>HH', payload[0:4])
  888. eapol['rsn_key_info'] = key_info
  889. eapol['rsn_key_len'] = key_len
  890. eapol['rsn_replay_counter'] = payload[4:12]
  891. eapol['rsn_key_nonce'] = payload[12:44]
  892. eapol['rsn_key_iv'] = payload[44:60]
  893. eapol['rsn_key_rsc'] = payload[60:68]
  894. eapol['rsn_key_id'] = payload[68:76]
  895. eapol['rsn_key_mic'] = payload[76:92]
  896. payload = payload[92:]
  897. (eapol['rsn_key_data_len'],) = struct.unpack('>H', payload[0:2])
  898. payload = payload[2:]
  899. eapol['rsn_key_data'] = payload
  900. return eapol
  901. def build_eapol(msg):
  902. data = struct.pack(">BBH", msg['version'], msg['type'], msg['length'])
  903. if msg['type'] == 3:
  904. data += struct.pack('>BHH', msg['descr_type'], msg['rsn_key_info'],
  905. msg['rsn_key_len'])
  906. data += msg['rsn_replay_counter']
  907. data += msg['rsn_key_nonce']
  908. data += msg['rsn_key_iv']
  909. data += msg['rsn_key_rsc']
  910. data += msg['rsn_key_id']
  911. data += msg['rsn_key_mic']
  912. data += struct.pack('>H', msg['rsn_key_data_len'])
  913. data += msg['rsn_key_data']
  914. else:
  915. data += msg['payload']
  916. return data
  917. def sha1_prf(key, label, data, outlen):
  918. res = ''
  919. counter = 0
  920. while outlen > 0:
  921. m = hmac.new(key, label, hashlib.sha1)
  922. m.update(struct.pack('B', 0))
  923. m.update(data)
  924. m.update(struct.pack('B', counter))
  925. counter += 1
  926. hash = m.digest()
  927. if outlen > len(hash):
  928. res += hash
  929. outlen -= len(hash)
  930. else:
  931. res += hash[0:outlen]
  932. outlen = 0
  933. return res
  934. def pmk_to_ptk(pmk, addr1, addr2, nonce1, nonce2):
  935. if addr1 < addr2:
  936. data = binascii.unhexlify(addr1.replace(':','')) + binascii.unhexlify(addr2.replace(':',''))
  937. else:
  938. data = binascii.unhexlify(addr2.replace(':','')) + binascii.unhexlify(addr1.replace(':',''))
  939. if nonce1 < nonce2:
  940. data += nonce1 + nonce2
  941. else:
  942. data += nonce2 + nonce1
  943. label = "Pairwise key expansion"
  944. ptk = sha1_prf(pmk, label, data, 48)
  945. kck = ptk[0:16]
  946. kek = ptk[16:32]
  947. return (ptk, kck, kek)
  948. def eapol_key_mic(kck, msg):
  949. msg['rsn_key_mic'] = binascii.unhexlify('00000000000000000000000000000000')
  950. data = build_eapol(msg)
  951. m = hmac.new(kck, data, hashlib.sha1)
  952. msg['rsn_key_mic'] = m.digest()[0:16]
  953. def rsn_eapol_key_set(msg, key_info, key_len, nonce, data):
  954. msg['rsn_key_info'] = key_info
  955. msg['rsn_key_len'] = key_len
  956. if nonce:
  957. msg['rsn_key_nonce'] = nonce
  958. else:
  959. msg['rsn_key_nonce'] = binascii.unhexlify('0000000000000000000000000000000000000000000000000000000000000000')
  960. if data:
  961. msg['rsn_key_data_len'] = len(data)
  962. msg['rsn_key_data'] = data
  963. msg['length'] = 95 + len(data)
  964. else:
  965. msg['rsn_key_data_len'] = 0
  966. msg['rsn_key_data'] = ''
  967. msg['length'] = 95
  968. def recv_eapol(hapd):
  969. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  970. if ev is None:
  971. raise Exception("Timeout on EAPOL-TX from hostapd")
  972. eapol = binascii.unhexlify(ev.split(' ')[2])
  973. return parse_eapol(eapol)
  974. def send_eapol(hapd, addr, data):
  975. res = hapd.request("EAPOL_RX " + addr + " " + binascii.hexlify(data))
  976. if "OK" not in res:
  977. raise Exception("EAPOL_RX to hostapd failed")
  978. def reply_eapol(info, hapd, addr, msg, key_info, nonce, data, kck):
  979. logger.info("Send EAPOL-Key msg " + info)
  980. rsn_eapol_key_set(msg, key_info, 0, nonce, data)
  981. eapol_key_mic(kck, msg)
  982. send_eapol(hapd, addr, build_eapol(msg))
  983. def hapd_connected(hapd):
  984. ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=15)
  985. if ev is None:
  986. raise Exception("Timeout on AP-STA-CONNECTED from hostapd")
  987. def eapol_test(apdev, dev, wpa2=True):
  988. bssid = apdev['bssid']
  989. if wpa2:
  990. ssid = "test-wpa2-psk"
  991. else:
  992. ssid = "test-wpa-psk"
  993. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  994. pmk = binascii.unhexlify(psk)
  995. if wpa2:
  996. params = hostapd.wpa2_params(ssid=ssid)
  997. else:
  998. params = hostapd.wpa_params(ssid=ssid)
  999. params['wpa_psk'] = psk
  1000. hapd = hostapd.add_ap(apdev, params)
  1001. hapd.request("SET ext_eapol_frame_io 1")
  1002. dev.request("SET ext_eapol_frame_io 1")
  1003. dev.connect(ssid, raw_psk=psk, scan_freq="2412", wait_connect=False)
  1004. addr = dev.p2p_interface_addr()
  1005. if wpa2:
  1006. rsne = binascii.unhexlify('30140100000fac040100000fac040100000fac020000')
  1007. else:
  1008. rsne = binascii.unhexlify('dd160050f20101000050f20201000050f20201000050f202')
  1009. snonce = binascii.unhexlify('1111111111111111111111111111111111111111111111111111111111111111')
  1010. return (bssid,ssid,hapd,snonce,pmk,addr,rsne)
  1011. @remote_compatible
  1012. def test_ap_wpa2_psk_ext_eapol(dev, apdev):
  1013. """WPA2-PSK AP using external EAPOL supplicant"""
  1014. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1015. msg = recv_eapol(hapd)
  1016. anonce = msg['rsn_key_nonce']
  1017. logger.info("Replay same data back")
  1018. send_eapol(hapd, addr, build_eapol(msg))
  1019. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1020. logger.info("Truncated Key Data in EAPOL-Key msg 2/4")
  1021. rsn_eapol_key_set(msg, 0x0101, 0, snonce, rsne)
  1022. msg['length'] = 95 + 22 - 1
  1023. send_eapol(hapd, addr, build_eapol(msg))
  1024. reply_eapol("2/4", hapd, addr, msg, 0x010a, snonce, rsne, kck)
  1025. msg = recv_eapol(hapd)
  1026. if anonce != msg['rsn_key_nonce']:
  1027. raise Exception("ANonce changed")
  1028. logger.info("Replay same data back")
  1029. send_eapol(hapd, addr, build_eapol(msg))
  1030. reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck)
  1031. hapd_connected(hapd)
  1032. @remote_compatible
  1033. def test_ap_wpa2_psk_ext_eapol_retry1(dev, apdev):
  1034. """WPA2 4-way handshake with EAPOL-Key 1/4 retransmitted"""
  1035. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1036. msg1 = recv_eapol(hapd)
  1037. anonce = msg1['rsn_key_nonce']
  1038. msg2 = recv_eapol(hapd)
  1039. if anonce != msg2['rsn_key_nonce']:
  1040. raise Exception("ANonce changed")
  1041. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1042. logger.info("Send EAPOL-Key msg 2/4")
  1043. msg = msg2
  1044. rsn_eapol_key_set(msg, 0x010a, 0, snonce, rsne)
  1045. eapol_key_mic(kck, msg)
  1046. send_eapol(hapd, addr, build_eapol(msg))
  1047. msg = recv_eapol(hapd)
  1048. if anonce != msg['rsn_key_nonce']:
  1049. raise Exception("ANonce changed")
  1050. reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck)
  1051. hapd_connected(hapd)
  1052. @remote_compatible
  1053. def test_ap_wpa2_psk_ext_eapol_retry1b(dev, apdev):
  1054. """WPA2 4-way handshake with EAPOL-Key 1/4 and 2/4 retransmitted"""
  1055. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1056. msg1 = recv_eapol(hapd)
  1057. anonce = msg1['rsn_key_nonce']
  1058. msg2 = recv_eapol(hapd)
  1059. if anonce != msg2['rsn_key_nonce']:
  1060. raise Exception("ANonce changed")
  1061. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1062. reply_eapol("2/4 (a)", hapd, addr, msg1, 0x010a, snonce, rsne, kck)
  1063. reply_eapol("2/4 (b)", hapd, addr, msg2, 0x010a, snonce, rsne, kck)
  1064. msg = recv_eapol(hapd)
  1065. if anonce != msg['rsn_key_nonce']:
  1066. raise Exception("ANonce changed")
  1067. reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck)
  1068. hapd_connected(hapd)
  1069. @remote_compatible
  1070. def test_ap_wpa2_psk_ext_eapol_retry1c(dev, apdev):
  1071. """WPA2 4-way handshake with EAPOL-Key 1/4 and 2/4 retransmitted and SNonce changing"""
  1072. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1073. msg1 = recv_eapol(hapd)
  1074. anonce = msg1['rsn_key_nonce']
  1075. msg2 = recv_eapol(hapd)
  1076. if anonce != msg2['rsn_key_nonce']:
  1077. raise Exception("ANonce changed")
  1078. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1079. reply_eapol("2/4 (a)", hapd, addr, msg1, 0x010a, snonce, rsne, kck)
  1080. snonce2 = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1081. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce2, anonce)
  1082. reply_eapol("2/4 (b)", hapd, addr, msg2, 0x010a, snonce2, rsne, kck)
  1083. msg = recv_eapol(hapd)
  1084. if anonce != msg['rsn_key_nonce']:
  1085. raise Exception("ANonce changed")
  1086. reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck)
  1087. hapd_connected(hapd)
  1088. @remote_compatible
  1089. def test_ap_wpa2_psk_ext_eapol_retry1d(dev, apdev):
  1090. """WPA2 4-way handshake with EAPOL-Key 1/4 and 2/4 retransmitted and SNonce changing and older used"""
  1091. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1092. msg1 = recv_eapol(hapd)
  1093. anonce = msg1['rsn_key_nonce']
  1094. msg2 = recv_eapol(hapd)
  1095. if anonce != msg2['rsn_key_nonce']:
  1096. raise Exception("ANonce changed")
  1097. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1098. reply_eapol("2/4 (a)", hapd, addr, msg1, 0x010a, snonce, rsne, kck)
  1099. snonce2 = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1100. (ptk2, kck2, kek2) = pmk_to_ptk(pmk, addr, bssid, snonce2, anonce)
  1101. reply_eapol("2/4 (b)", hapd, addr, msg2, 0x010a, snonce2, rsne, kck2)
  1102. msg = recv_eapol(hapd)
  1103. if anonce != msg['rsn_key_nonce']:
  1104. raise Exception("ANonce changed")
  1105. reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck)
  1106. hapd_connected(hapd)
  1107. @remote_compatible
  1108. def test_ap_wpa2_psk_ext_eapol_type_diff(dev, apdev):
  1109. """WPA2 4-way handshake using external EAPOL supplicant"""
  1110. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1111. msg = recv_eapol(hapd)
  1112. anonce = msg['rsn_key_nonce']
  1113. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1114. # Incorrect descriptor type (frame dropped)
  1115. msg['descr_type'] = 253
  1116. rsn_eapol_key_set(msg, 0x010a, 0, snonce, rsne)
  1117. eapol_key_mic(kck, msg)
  1118. send_eapol(hapd, addr, build_eapol(msg))
  1119. # Incorrect descriptor type, but with a workaround (frame processed)
  1120. msg['descr_type'] = 254
  1121. rsn_eapol_key_set(msg, 0x010a, 0, snonce, rsne)
  1122. eapol_key_mic(kck, msg)
  1123. send_eapol(hapd, addr, build_eapol(msg))
  1124. msg = recv_eapol(hapd)
  1125. if anonce != msg['rsn_key_nonce']:
  1126. raise Exception("ANonce changed")
  1127. logger.info("Replay same data back")
  1128. send_eapol(hapd, addr, build_eapol(msg))
  1129. reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck)
  1130. hapd_connected(hapd)
  1131. @remote_compatible
  1132. def test_ap_wpa_psk_ext_eapol(dev, apdev):
  1133. """WPA2-PSK AP using external EAPOL supplicant"""
  1134. (bssid,ssid,hapd,snonce,pmk,addr,wpae) = eapol_test(apdev[0], dev[0],
  1135. wpa2=False)
  1136. msg = recv_eapol(hapd)
  1137. anonce = msg['rsn_key_nonce']
  1138. logger.info("Replay same data back")
  1139. send_eapol(hapd, addr, build_eapol(msg))
  1140. logger.info("Too short data")
  1141. send_eapol(hapd, addr, build_eapol(msg)[0:98])
  1142. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1143. msg['descr_type'] = 2
  1144. reply_eapol("2/4(invalid type)", hapd, addr, msg, 0x010a, snonce, wpae, kck)
  1145. msg['descr_type'] = 254
  1146. reply_eapol("2/4", hapd, addr, msg, 0x010a, snonce, wpae, kck)
  1147. msg = recv_eapol(hapd)
  1148. if anonce != msg['rsn_key_nonce']:
  1149. raise Exception("ANonce changed")
  1150. logger.info("Replay same data back")
  1151. send_eapol(hapd, addr, build_eapol(msg))
  1152. reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck)
  1153. hapd_connected(hapd)
  1154. @remote_compatible
  1155. def test_ap_wpa2_psk_ext_eapol_key_info(dev, apdev):
  1156. """WPA2-PSK 4-way handshake with strange key info values"""
  1157. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1158. msg = recv_eapol(hapd)
  1159. anonce = msg['rsn_key_nonce']
  1160. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1161. rsn_eapol_key_set(msg, 0x0000, 0, snonce, rsne)
  1162. send_eapol(hapd, addr, build_eapol(msg))
  1163. rsn_eapol_key_set(msg, 0xffff, 0, snonce, rsne)
  1164. send_eapol(hapd, addr, build_eapol(msg))
  1165. # SMK M1
  1166. rsn_eapol_key_set(msg, 0x2802, 0, snonce, rsne)
  1167. send_eapol(hapd, addr, build_eapol(msg))
  1168. # SMK M3
  1169. rsn_eapol_key_set(msg, 0x2002, 0, snonce, rsne)
  1170. send_eapol(hapd, addr, build_eapol(msg))
  1171. # Request
  1172. rsn_eapol_key_set(msg, 0x0902, 0, snonce, rsne)
  1173. send_eapol(hapd, addr, build_eapol(msg))
  1174. # Request
  1175. rsn_eapol_key_set(msg, 0x0902, 0, snonce, rsne)
  1176. tmp_kck = binascii.unhexlify('00000000000000000000000000000000')
  1177. eapol_key_mic(tmp_kck, msg)
  1178. send_eapol(hapd, addr, build_eapol(msg))
  1179. reply_eapol("2/4", hapd, addr, msg, 0x010a, snonce, rsne, kck)
  1180. msg = recv_eapol(hapd)
  1181. if anonce != msg['rsn_key_nonce']:
  1182. raise Exception("ANonce changed")
  1183. # Request (valic MIC)
  1184. rsn_eapol_key_set(msg, 0x0902, 0, snonce, rsne)
  1185. eapol_key_mic(kck, msg)
  1186. send_eapol(hapd, addr, build_eapol(msg))
  1187. # Request (valid MIC, replayed counter)
  1188. rsn_eapol_key_set(msg, 0x0902, 0, snonce, rsne)
  1189. eapol_key_mic(kck, msg)
  1190. send_eapol(hapd, addr, build_eapol(msg))
  1191. reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck)
  1192. hapd_connected(hapd)
  1193. def build_eapol_key_1_4(anonce, replay_counter=1, key_data='', key_len=16):
  1194. msg = {}
  1195. msg['version'] = 2
  1196. msg['type'] = 3
  1197. msg['length'] = 95 + len(key_data)
  1198. msg['descr_type'] = 2
  1199. msg['rsn_key_info'] = 0x8a
  1200. msg['rsn_key_len'] = key_len
  1201. msg['rsn_replay_counter'] = struct.pack('>Q', replay_counter)
  1202. msg['rsn_key_nonce'] = anonce
  1203. msg['rsn_key_iv'] = binascii.unhexlify('00000000000000000000000000000000')
  1204. msg['rsn_key_rsc'] = binascii.unhexlify('0000000000000000')
  1205. msg['rsn_key_id'] = binascii.unhexlify('0000000000000000')
  1206. msg['rsn_key_mic'] = binascii.unhexlify('00000000000000000000000000000000')
  1207. msg['rsn_key_data_len'] = len(key_data)
  1208. msg['rsn_key_data'] = key_data
  1209. return msg
  1210. def build_eapol_key_3_4(anonce, kck, key_data, replay_counter=2,
  1211. key_info=0x13ca, extra_len=0, descr_type=2, key_len=16):
  1212. msg = {}
  1213. msg['version'] = 2
  1214. msg['type'] = 3
  1215. msg['length'] = 95 + len(key_data) + extra_len
  1216. msg['descr_type'] = descr_type
  1217. msg['rsn_key_info'] = key_info
  1218. msg['rsn_key_len'] = key_len
  1219. msg['rsn_replay_counter'] = struct.pack('>Q', replay_counter)
  1220. msg['rsn_key_nonce'] = anonce
  1221. msg['rsn_key_iv'] = binascii.unhexlify('00000000000000000000000000000000')
  1222. msg['rsn_key_rsc'] = binascii.unhexlify('0000000000000000')
  1223. msg['rsn_key_id'] = binascii.unhexlify('0000000000000000')
  1224. msg['rsn_key_data_len'] = len(key_data)
  1225. msg['rsn_key_data'] = key_data
  1226. eapol_key_mic(kck, msg)
  1227. return msg
  1228. def aes_wrap(kek, plain):
  1229. n = len(plain) / 8
  1230. a = 0xa6a6a6a6a6a6a6a6
  1231. enc = AES.new(kek).encrypt
  1232. r = [plain[i * 8:(i + 1) * 8] for i in range(0, n)]
  1233. for j in range(6):
  1234. for i in range(1, n + 1):
  1235. b = enc(struct.pack('>Q', a) + r[i - 1])
  1236. a = struct.unpack('>Q', b[:8])[0] ^ (n * j + i)
  1237. r[i - 1] =b[8:]
  1238. return struct.pack('>Q', a) + ''.join(r)
  1239. def pad_key_data(plain):
  1240. pad_len = len(plain) % 8
  1241. if pad_len:
  1242. pad_len = 8 - pad_len
  1243. plain += '\xdd'
  1244. pad_len -= 1
  1245. plain += pad_len * '\0'
  1246. return plain
  1247. def test_ap_wpa2_psk_supp_proto(dev, apdev):
  1248. """WPA2-PSK 4-way handshake protocol testing for supplicant"""
  1249. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1250. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1251. msg = recv_eapol(hapd)
  1252. dev[0].dump_monitor()
  1253. # Build own EAPOL-Key msg 1/4
  1254. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1255. counter = 1
  1256. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1257. counter += 1
  1258. send_eapol(dev[0], bssid, build_eapol(msg))
  1259. msg = recv_eapol(dev[0])
  1260. snonce = msg['rsn_key_nonce']
  1261. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1262. logger.debug("Invalid AES wrap data length 0")
  1263. dev[0].dump_monitor()
  1264. msg = build_eapol_key_3_4(anonce, kck, '', replay_counter=counter)
  1265. counter += 1
  1266. send_eapol(dev[0], bssid, build_eapol(msg))
  1267. ev = dev[0].wait_event(["WPA: Unsupported AES-WRAP len 0"])
  1268. if ev is None:
  1269. raise Exception("Unsupported AES-WRAP len 0 not reported")
  1270. logger.debug("Invalid AES wrap data length 1")
  1271. dev[0].dump_monitor()
  1272. msg = build_eapol_key_3_4(anonce, kck, '1', replay_counter=counter)
  1273. counter += 1
  1274. send_eapol(dev[0], bssid, build_eapol(msg))
  1275. ev = dev[0].wait_event(["WPA: Unsupported AES-WRAP len 1"])
  1276. if ev is None:
  1277. raise Exception("Unsupported AES-WRAP len 1 not reported")
  1278. logger.debug("Invalid AES wrap data length 9")
  1279. dev[0].dump_monitor()
  1280. msg = build_eapol_key_3_4(anonce, kck, '123456789', replay_counter=counter)
  1281. counter += 1
  1282. send_eapol(dev[0], bssid, build_eapol(msg))
  1283. ev = dev[0].wait_event(["WPA: Unsupported AES-WRAP len 9"])
  1284. if ev is None:
  1285. raise Exception("Unsupported AES-WRAP len 9 not reported")
  1286. logger.debug("Invalid AES wrap data payload")
  1287. dev[0].dump_monitor()
  1288. msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter)
  1289. # do not increment counter to test replay protection
  1290. send_eapol(dev[0], bssid, build_eapol(msg))
  1291. ev = dev[0].wait_event(["WPA: AES unwrap failed"])
  1292. if ev is None:
  1293. raise Exception("AES unwrap failure not reported")
  1294. logger.debug("Replay Count not increasing")
  1295. dev[0].dump_monitor()
  1296. msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter)
  1297. counter += 1
  1298. send_eapol(dev[0], bssid, build_eapol(msg))
  1299. ev = dev[0].wait_event(["WPA: EAPOL-Key Replay Counter did not increase"])
  1300. if ev is None:
  1301. raise Exception("Replay Counter replay not reported")
  1302. logger.debug("Missing Ack bit in key info")
  1303. dev[0].dump_monitor()
  1304. msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter,
  1305. key_info=0x134a)
  1306. counter += 1
  1307. send_eapol(dev[0], bssid, build_eapol(msg))
  1308. ev = dev[0].wait_event(["WPA: No Ack bit in key_info"])
  1309. if ev is None:
  1310. raise Exception("Missing Ack bit not reported")
  1311. logger.debug("Unexpected Request bit in key info")
  1312. dev[0].dump_monitor()
  1313. msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter,
  1314. key_info=0x1bca)
  1315. counter += 1
  1316. send_eapol(dev[0], bssid, build_eapol(msg))
  1317. ev = dev[0].wait_event(["WPA: EAPOL-Key with Request bit"])
  1318. if ev is None:
  1319. raise Exception("Request bit not reported")
  1320. logger.debug("Unsupported key descriptor version 0")
  1321. dev[0].dump_monitor()
  1322. msg = build_eapol_key_3_4(anonce, kck, '0123456789abcdef',
  1323. replay_counter=counter, key_info=0x13c8)
  1324. counter += 1
  1325. send_eapol(dev[0], bssid, build_eapol(msg))
  1326. ev = dev[0].wait_event(["WPA: Unsupported EAPOL-Key descriptor version 0"])
  1327. if ev is None:
  1328. raise Exception("Unsupported EAPOL-Key descriptor version 0 not reported")
  1329. logger.debug("Key descriptor version 1 not allowed with CCMP")
  1330. dev[0].dump_monitor()
  1331. msg = build_eapol_key_3_4(anonce, kck, '0123456789abcdef',
  1332. replay_counter=counter, key_info=0x13c9)
  1333. counter += 1
  1334. send_eapol(dev[0], bssid, build_eapol(msg))
  1335. ev = dev[0].wait_event(["WPA: CCMP is used, but EAPOL-Key descriptor version (1) is not 2"])
  1336. if ev is None:
  1337. raise Exception("Not allowed EAPOL-Key descriptor version not reported")
  1338. logger.debug("Invalid AES wrap payload with key descriptor version 2")
  1339. dev[0].dump_monitor()
  1340. msg = build_eapol_key_3_4(anonce, kck, '0123456789abcdef',
  1341. replay_counter=counter, key_info=0x13ca)
  1342. counter += 1
  1343. send_eapol(dev[0], bssid, build_eapol(msg))
  1344. ev = dev[0].wait_event(["WPA: AES unwrap failed"])
  1345. if ev is None:
  1346. raise Exception("AES unwrap failure not reported")
  1347. logger.debug("Key descriptor version 3 workaround")
  1348. dev[0].dump_monitor()
  1349. msg = build_eapol_key_3_4(anonce, kck, '0123456789abcdef',
  1350. replay_counter=counter, key_info=0x13cb)
  1351. counter += 1
  1352. send_eapol(dev[0], bssid, build_eapol(msg))
  1353. ev = dev[0].wait_event(["WPA: CCMP is used, but EAPOL-Key descriptor version (3) is not 2"])
  1354. if ev is None:
  1355. raise Exception("CCMP key descriptor mismatch not reported")
  1356. ev = dev[0].wait_event(["WPA: Interoperability workaround"])
  1357. if ev is None:
  1358. raise Exception("AES-128-CMAC workaround not reported")
  1359. ev = dev[0].wait_event(["WPA: Invalid EAPOL-Key MIC - dropping packet"])
  1360. if ev is None:
  1361. raise Exception("MIC failure with AES-128-CMAC workaround not reported")
  1362. logger.debug("Unsupported key descriptor version 4")
  1363. dev[0].dump_monitor()
  1364. msg = build_eapol_key_3_4(anonce, kck, '0123456789abcdef',
  1365. replay_counter=counter, key_info=0x13cc)
  1366. counter += 1
  1367. send_eapol(dev[0], bssid, build_eapol(msg))
  1368. ev = dev[0].wait_event(["WPA: Unsupported EAPOL-Key descriptor version 4"])
  1369. if ev is None:
  1370. raise Exception("Unsupported EAPOL-Key descriptor version 4 not reported")
  1371. logger.debug("Unsupported key descriptor version 7")
  1372. dev[0].dump_monitor()
  1373. msg = build_eapol_key_3_4(anonce, kck, '0123456789abcdef',
  1374. replay_counter=counter, key_info=0x13cf)
  1375. counter += 1
  1376. send_eapol(dev[0], bssid, build_eapol(msg))
  1377. ev = dev[0].wait_event(["WPA: Unsupported EAPOL-Key descriptor version 7"])
  1378. if ev is None:
  1379. raise Exception("Unsupported EAPOL-Key descriptor version 7 not reported")
  1380. logger.debug("Too short EAPOL header length")
  1381. dev[0].dump_monitor()
  1382. msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter,
  1383. extra_len=-1)
  1384. counter += 1
  1385. send_eapol(dev[0], bssid, build_eapol(msg))
  1386. ev = dev[0].wait_event(["WPA: Invalid EAPOL-Key frame - key_data overflow (8 > 7)"])
  1387. if ev is None:
  1388. raise Exception("Key data overflow not reported")
  1389. logger.debug("Too long EAPOL header length")
  1390. msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter,
  1391. extra_len=1)
  1392. counter += 1
  1393. send_eapol(dev[0], bssid, build_eapol(msg))
  1394. logger.debug("Unsupported descriptor type 0")
  1395. msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter,
  1396. descr_type=0)
  1397. counter += 1
  1398. send_eapol(dev[0], bssid, build_eapol(msg))
  1399. logger.debug("WPA descriptor type 0")
  1400. msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter,
  1401. descr_type=254)
  1402. counter += 1
  1403. send_eapol(dev[0], bssid, build_eapol(msg))
  1404. logger.debug("Non-zero key index for pairwise key")
  1405. dev[0].dump_monitor()
  1406. wrapped = aes_wrap(kek, 16*'z')
  1407. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter,
  1408. key_info=0x13ea)
  1409. counter += 1
  1410. send_eapol(dev[0], bssid, build_eapol(msg))
  1411. ev = dev[0].wait_event(["WPA: Ignored EAPOL-Key (Pairwise) with non-zero key index"])
  1412. if ev is None:
  1413. raise Exception("Non-zero key index not reported")
  1414. logger.debug("Invalid Key Data plaintext payload --> disconnect")
  1415. dev[0].dump_monitor()
  1416. wrapped = aes_wrap(kek, 16*'z')
  1417. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1418. counter += 1
  1419. send_eapol(dev[0], bssid, build_eapol(msg))
  1420. dev[0].wait_disconnected(timeout=1)
  1421. def test_ap_wpa2_psk_supp_proto_no_ie(dev, apdev):
  1422. """WPA2-PSK supplicant protocol testing: IE not included"""
  1423. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1424. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1425. msg = recv_eapol(hapd)
  1426. dev[0].dump_monitor()
  1427. # Build own EAPOL-Key msg 1/4
  1428. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1429. counter = 1
  1430. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1431. counter += 1
  1432. send_eapol(dev[0], bssid, build_eapol(msg))
  1433. msg = recv_eapol(dev[0])
  1434. snonce = msg['rsn_key_nonce']
  1435. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1436. logger.debug("No IEs in msg 3/4 --> disconnect")
  1437. dev[0].dump_monitor()
  1438. wrapped = aes_wrap(kek, 16*'\0')
  1439. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1440. counter += 1
  1441. send_eapol(dev[0], bssid, build_eapol(msg))
  1442. dev[0].wait_disconnected(timeout=1)
  1443. def test_ap_wpa2_psk_supp_proto_ie_mismatch(dev, apdev):
  1444. """WPA2-PSK supplicant protocol testing: IE mismatch"""
  1445. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1446. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1447. msg = recv_eapol(hapd)
  1448. dev[0].dump_monitor()
  1449. # Build own EAPOL-Key msg 1/4
  1450. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1451. counter = 1
  1452. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1453. counter += 1
  1454. send_eapol(dev[0], bssid, build_eapol(msg))
  1455. msg = recv_eapol(dev[0])
  1456. snonce = msg['rsn_key_nonce']
  1457. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1458. logger.debug("Msg 3/4 with mismatching IE")
  1459. dev[0].dump_monitor()
  1460. wrapped = aes_wrap(kek, pad_key_data(binascii.unhexlify('30060100000fac04dd16000fac010100dc11188831bf4aa4a8678d2b41498618')))
  1461. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1462. counter += 1
  1463. send_eapol(dev[0], bssid, build_eapol(msg))
  1464. dev[0].wait_disconnected(timeout=1)
  1465. def test_ap_wpa2_psk_supp_proto_ok(dev, apdev):
  1466. """WPA2-PSK supplicant protocol testing: success"""
  1467. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1468. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1469. msg = recv_eapol(hapd)
  1470. dev[0].dump_monitor()
  1471. # Build own EAPOL-Key msg 1/4
  1472. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1473. counter = 1
  1474. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1475. counter += 1
  1476. send_eapol(dev[0], bssid, build_eapol(msg))
  1477. msg = recv_eapol(dev[0])
  1478. snonce = msg['rsn_key_nonce']
  1479. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1480. logger.debug("Valid EAPOL-Key msg 3/4")
  1481. dev[0].dump_monitor()
  1482. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010100dc11188831bf4aa4a8678d2b41498618')
  1483. wrapped = aes_wrap(kek, pad_key_data(plain))
  1484. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1485. counter += 1
  1486. send_eapol(dev[0], bssid, build_eapol(msg))
  1487. dev[0].wait_connected(timeout=1)
  1488. def test_ap_wpa2_psk_supp_proto_no_gtk(dev, apdev):
  1489. """WPA2-PSK supplicant protocol testing: no GTK"""
  1490. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1491. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1492. msg = recv_eapol(hapd)
  1493. dev[0].dump_monitor()
  1494. # Build own EAPOL-Key msg 1/4
  1495. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1496. counter = 1
  1497. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1498. counter += 1
  1499. send_eapol(dev[0], bssid, build_eapol(msg))
  1500. msg = recv_eapol(dev[0])
  1501. snonce = msg['rsn_key_nonce']
  1502. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1503. logger.debug("EAPOL-Key msg 3/4 without GTK KDE")
  1504. dev[0].dump_monitor()
  1505. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00')
  1506. wrapped = aes_wrap(kek, pad_key_data(plain))
  1507. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1508. counter += 1
  1509. send_eapol(dev[0], bssid, build_eapol(msg))
  1510. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=0.1)
  1511. if ev is not None:
  1512. raise Exception("Unexpected connection completion reported")
  1513. def test_ap_wpa2_psk_supp_proto_anonce_change(dev, apdev):
  1514. """WPA2-PSK supplicant protocol testing: ANonce change"""
  1515. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1516. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1517. msg = recv_eapol(hapd)
  1518. dev[0].dump_monitor()
  1519. # Build own EAPOL-Key msg 1/4
  1520. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1521. counter = 1
  1522. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1523. counter += 1
  1524. send_eapol(dev[0], bssid, build_eapol(msg))
  1525. msg = recv_eapol(dev[0])
  1526. snonce = msg['rsn_key_nonce']
  1527. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1528. logger.debug("Valid EAPOL-Key msg 3/4")
  1529. dev[0].dump_monitor()
  1530. anonce2 = binascii.unhexlify('3333333333333333333333333333333333333333333333333333333333333333')
  1531. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010100dc11188831bf4aa4a8678d2b41498618')
  1532. wrapped = aes_wrap(kek, pad_key_data(plain))
  1533. msg = build_eapol_key_3_4(anonce2, kck, wrapped, replay_counter=counter)
  1534. counter += 1
  1535. send_eapol(dev[0], bssid, build_eapol(msg))
  1536. ev = dev[0].wait_event(["WPA: ANonce from message 1 of 4-Way Handshake differs from 3 of 4-Way Handshake"])
  1537. if ev is None:
  1538. raise Exception("ANonce change not reported")
  1539. def test_ap_wpa2_psk_supp_proto_unexpected_group_msg(dev, apdev):
  1540. """WPA2-PSK supplicant protocol testing: unexpected group message"""
  1541. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1542. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1543. msg = recv_eapol(hapd)
  1544. dev[0].dump_monitor()
  1545. # Build own EAPOL-Key msg 1/4
  1546. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1547. counter = 1
  1548. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1549. counter += 1
  1550. send_eapol(dev[0], bssid, build_eapol(msg))
  1551. msg = recv_eapol(dev[0])
  1552. snonce = msg['rsn_key_nonce']
  1553. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1554. logger.debug("Group key 1/2 instead of msg 3/4")
  1555. dev[0].dump_monitor()
  1556. wrapped = aes_wrap(kek, binascii.unhexlify('dd16000fac010100dc11188831bf4aa4a8678d2b41498618'))
  1557. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter,
  1558. key_info=0x13c2)
  1559. counter += 1
  1560. send_eapol(dev[0], bssid, build_eapol(msg))
  1561. ev = dev[0].wait_event(["WPA: Group Key Handshake started prior to completion of 4-way handshake"])
  1562. if ev is None:
  1563. raise Exception("Unexpected group key message not reported")
  1564. dev[0].wait_disconnected(timeout=1)
  1565. @remote_compatible
  1566. def test_ap_wpa2_psk_supp_proto_msg_1_invalid_kde(dev, apdev):
  1567. """WPA2-PSK supplicant protocol testing: invalid KDE in msg 1/4"""
  1568. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1569. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1570. msg = recv_eapol(hapd)
  1571. dev[0].dump_monitor()
  1572. # Build own EAPOL-Key msg 1/4 with invalid KDE
  1573. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1574. counter = 1
  1575. msg = build_eapol_key_1_4(anonce, replay_counter=counter,
  1576. key_data=binascii.unhexlify('5555'))
  1577. counter += 1
  1578. send_eapol(dev[0], bssid, build_eapol(msg))
  1579. dev[0].wait_disconnected(timeout=1)
  1580. def test_ap_wpa2_psk_supp_proto_wrong_pairwise_key_len(dev, apdev):
  1581. """WPA2-PSK supplicant protocol testing: wrong pairwise key length"""
  1582. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1583. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1584. msg = recv_eapol(hapd)
  1585. dev[0].dump_monitor()
  1586. # Build own EAPOL-Key msg 1/4
  1587. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1588. counter = 1
  1589. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1590. counter += 1
  1591. send_eapol(dev[0], bssid, build_eapol(msg))
  1592. msg = recv_eapol(dev[0])
  1593. snonce = msg['rsn_key_nonce']
  1594. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1595. logger.debug("Valid EAPOL-Key msg 3/4")
  1596. dev[0].dump_monitor()
  1597. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010100dc11188831bf4aa4a8678d2b41498618')
  1598. wrapped = aes_wrap(kek, pad_key_data(plain))
  1599. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter,
  1600. key_len=15)
  1601. counter += 1
  1602. send_eapol(dev[0], bssid, build_eapol(msg))
  1603. ev = dev[0].wait_event(["WPA: Invalid CCMP key length 15"])
  1604. if ev is None:
  1605. raise Exception("Invalid CCMP key length not reported")
  1606. dev[0].wait_disconnected(timeout=1)
  1607. def test_ap_wpa2_psk_supp_proto_wrong_group_key_len(dev, apdev):
  1608. """WPA2-PSK supplicant protocol testing: wrong group key length"""
  1609. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1610. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1611. msg = recv_eapol(hapd)
  1612. dev[0].dump_monitor()
  1613. # Build own EAPOL-Key msg 1/4
  1614. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1615. counter = 1
  1616. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1617. counter += 1
  1618. send_eapol(dev[0], bssid, build_eapol(msg))
  1619. msg = recv_eapol(dev[0])
  1620. snonce = msg['rsn_key_nonce']
  1621. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1622. logger.debug("Valid EAPOL-Key msg 3/4")
  1623. dev[0].dump_monitor()
  1624. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd15000fac010100dc11188831bf4aa4a8678d2b414986')
  1625. wrapped = aes_wrap(kek, pad_key_data(plain))
  1626. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1627. counter += 1
  1628. send_eapol(dev[0], bssid, build_eapol(msg))
  1629. ev = dev[0].wait_event(["WPA: Unsupported CCMP Group Cipher key length 15"])
  1630. if ev is None:
  1631. raise Exception("Invalid CCMP key length not reported")
  1632. dev[0].wait_disconnected(timeout=1)
  1633. def test_ap_wpa2_psk_supp_proto_gtk_tx_bit_workaround(dev, apdev):
  1634. """WPA2-PSK supplicant protocol testing: GTK TX bit workaround"""
  1635. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1636. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1637. msg = recv_eapol(hapd)
  1638. dev[0].dump_monitor()
  1639. # Build own EAPOL-Key msg 1/4
  1640. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1641. counter = 1
  1642. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1643. counter += 1
  1644. send_eapol(dev[0], bssid, build_eapol(msg))
  1645. msg = recv_eapol(dev[0])
  1646. snonce = msg['rsn_key_nonce']
  1647. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1648. logger.debug("Valid EAPOL-Key msg 3/4")
  1649. dev[0].dump_monitor()
  1650. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010500dc11188831bf4aa4a8678d2b41498618')
  1651. wrapped = aes_wrap(kek, pad_key_data(plain))
  1652. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1653. counter += 1
  1654. send_eapol(dev[0], bssid, build_eapol(msg))
  1655. ev = dev[0].wait_event(["WPA: Tx bit set for GTK, but pairwise keys are used - ignore Tx bit"])
  1656. if ev is None:
  1657. raise Exception("GTK Tx bit workaround not reported")
  1658. dev[0].wait_connected(timeout=1)
  1659. def test_ap_wpa2_psk_supp_proto_gtk_keyidx_0_and_3(dev, apdev):
  1660. """WPA2-PSK supplicant protocol testing: GTK key index 0 and 3"""
  1661. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1662. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1663. msg = recv_eapol(hapd)
  1664. dev[0].dump_monitor()
  1665. # Build own EAPOL-Key msg 1/4
  1666. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1667. counter = 1
  1668. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1669. counter += 1
  1670. send_eapol(dev[0], bssid, build_eapol(msg))
  1671. msg = recv_eapol(dev[0])
  1672. snonce = msg['rsn_key_nonce']
  1673. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1674. logger.debug("Valid EAPOL-Key msg 3/4 (GTK keyidx 0)")
  1675. dev[0].dump_monitor()
  1676. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010000dc11188831bf4aa4a8678d2b41498618')
  1677. wrapped = aes_wrap(kek, pad_key_data(plain))
  1678. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1679. counter += 1
  1680. send_eapol(dev[0], bssid, build_eapol(msg))
  1681. dev[0].wait_connected(timeout=1)
  1682. logger.debug("Valid EAPOL-Key group msg 1/2 (GTK keyidx 3)")
  1683. dev[0].dump_monitor()
  1684. plain = binascii.unhexlify('dd16000fac010300dc11188831bf4aa4a8678d2b41498618')
  1685. wrapped = aes_wrap(kek, pad_key_data(plain))
  1686. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter,
  1687. key_info=0x13c2)
  1688. counter += 1
  1689. send_eapol(dev[0], bssid, build_eapol(msg))
  1690. msg = recv_eapol(dev[0])
  1691. ev = dev[0].wait_event(["WPA: Group rekeying completed"])
  1692. if ev is None:
  1693. raise Exception("GTK rekeing not reported")
  1694. logger.debug("Unencrypted GTK KDE in group msg 1/2")
  1695. dev[0].dump_monitor()
  1696. plain = binascii.unhexlify('dd16000fac010300dc11188831bf4aa4a8678d2b41498618')
  1697. msg = build_eapol_key_3_4(anonce, kck, plain, replay_counter=counter,
  1698. key_info=0x03c2)
  1699. counter += 1
  1700. send_eapol(dev[0], bssid, build_eapol(msg))
  1701. ev = dev[0].wait_event(["WPA: GTK IE in unencrypted key data"])
  1702. if ev is None:
  1703. raise Exception("Unencrypted GTK KDE not reported")
  1704. dev[0].wait_disconnected(timeout=1)
  1705. def test_ap_wpa2_psk_supp_proto_no_gtk_in_group_msg(dev, apdev):
  1706. """WPA2-PSK supplicant protocol testing: GTK KDE missing from group msg"""
  1707. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1708. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1709. msg = recv_eapol(hapd)
  1710. dev[0].dump_monitor()
  1711. # Build own EAPOL-Key msg 1/4
  1712. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1713. counter = 1
  1714. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1715. counter += 1
  1716. send_eapol(dev[0], bssid, build_eapol(msg))
  1717. msg = recv_eapol(dev[0])
  1718. snonce = msg['rsn_key_nonce']
  1719. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1720. logger.debug("Valid EAPOL-Key msg 3/4 (GTK keyidx 0)")
  1721. dev[0].dump_monitor()
  1722. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010000dc11188831bf4aa4a8678d2b41498618')
  1723. wrapped = aes_wrap(kek, pad_key_data(plain))
  1724. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1725. counter += 1
  1726. send_eapol(dev[0], bssid, build_eapol(msg))
  1727. dev[0].wait_connected(timeout=1)
  1728. logger.debug("No GTK KDE in EAPOL-Key group msg 1/2")
  1729. dev[0].dump_monitor()
  1730. plain = binascii.unhexlify('dd00dd00dd00dd00dd00dd00dd00dd00')
  1731. wrapped = aes_wrap(kek, pad_key_data(plain))
  1732. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter,
  1733. key_info=0x13c2)
  1734. counter += 1
  1735. send_eapol(dev[0], bssid, build_eapol(msg))
  1736. ev = dev[0].wait_event(["WPA: No GTK IE in Group Key msg 1/2"])
  1737. if ev is None:
  1738. raise Exception("Missing GTK KDE not reported")
  1739. dev[0].wait_disconnected(timeout=1)
  1740. def test_ap_wpa2_psk_supp_proto_too_long_gtk_in_group_msg(dev, apdev):
  1741. """WPA2-PSK supplicant protocol testing: too long GTK KDE in group msg"""
  1742. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1743. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1744. msg = recv_eapol(hapd)
  1745. dev[0].dump_monitor()
  1746. # Build own EAPOL-Key msg 1/4
  1747. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1748. counter = 1
  1749. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1750. counter += 1
  1751. send_eapol(dev[0], bssid, build_eapol(msg))
  1752. msg = recv_eapol(dev[0])
  1753. snonce = msg['rsn_key_nonce']
  1754. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1755. logger.debug("Valid EAPOL-Key msg 3/4 (GTK keyidx 0)")
  1756. dev[0].dump_monitor()
  1757. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010000dc11188831bf4aa4a8678d2b41498618')
  1758. wrapped = aes_wrap(kek, pad_key_data(plain))
  1759. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1760. counter += 1
  1761. send_eapol(dev[0], bssid, build_eapol(msg))
  1762. dev[0].wait_connected(timeout=1)
  1763. logger.debug("EAPOL-Key group msg 1/2 with too long GTK KDE")
  1764. dev[0].dump_monitor()
  1765. plain = binascii.unhexlify('dd27000fac010100ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff')
  1766. wrapped = aes_wrap(kek, pad_key_data(plain))
  1767. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter,
  1768. key_info=0x13c2)
  1769. counter += 1
  1770. send_eapol(dev[0], bssid, build_eapol(msg))
  1771. ev = dev[0].wait_event(["WPA: Unsupported CCMP Group Cipher key length 33"])
  1772. if ev is None:
  1773. raise Exception("Too long GTK KDE not reported")
  1774. dev[0].wait_disconnected(timeout=1)
  1775. def test_ap_wpa2_psk_supp_proto_too_long_gtk_kde(dev, apdev):
  1776. """WPA2-PSK supplicant protocol testing: too long GTK KDE"""
  1777. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1778. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1779. msg = recv_eapol(hapd)
  1780. dev[0].dump_monitor()
  1781. # Build own EAPOL-Key msg 1/4
  1782. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1783. counter = 1
  1784. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1785. counter += 1
  1786. send_eapol(dev[0], bssid, build_eapol(msg))
  1787. msg = recv_eapol(dev[0])
  1788. snonce = msg['rsn_key_nonce']
  1789. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1790. logger.debug("EAPOL-Key msg 3/4 with too short GTK KDE")
  1791. dev[0].dump_monitor()
  1792. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd27000fac010100ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff')
  1793. wrapped = aes_wrap(kek, pad_key_data(plain))
  1794. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1795. counter += 1
  1796. send_eapol(dev[0], bssid, build_eapol(msg))
  1797. dev[0].wait_disconnected(timeout=1)
  1798. def test_ap_wpa2_psk_supp_proto_gtk_not_encrypted(dev, apdev):
  1799. """WPA2-PSK supplicant protocol testing: GTK KDE not encrypted"""
  1800. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1801. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1802. msg = recv_eapol(hapd)
  1803. dev[0].dump_monitor()
  1804. # Build own EAPOL-Key msg 1/4
  1805. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1806. counter = 1
  1807. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1808. counter += 1
  1809. send_eapol(dev[0], bssid, build_eapol(msg))
  1810. msg = recv_eapol(dev[0])
  1811. snonce = msg['rsn_key_nonce']
  1812. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1813. logger.debug("Valid EAPOL-Key msg 3/4")
  1814. dev[0].dump_monitor()
  1815. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010100dc11188831bf4aa4a8678d2b41498618')
  1816. msg = build_eapol_key_3_4(anonce, kck, plain, replay_counter=counter,
  1817. key_info=0x03ca)
  1818. counter += 1
  1819. send_eapol(dev[0], bssid, build_eapol(msg))
  1820. ev = dev[0].wait_event(["WPA: GTK IE in unencrypted key data"])
  1821. if ev is None:
  1822. raise Exception("Unencrypted GTK KDE not reported")
  1823. dev[0].wait_disconnected(timeout=1)
  1824. def find_wpas_process(dev):
  1825. ifname = dev.ifname
  1826. err, data = dev.cmd_execute(['ps', 'ax'])
  1827. for l in data.splitlines():
  1828. if "wpa_supplicant" not in l:
  1829. continue
  1830. if "-i" + ifname not in l:
  1831. continue
  1832. return int(l.strip().split(' ')[0])
  1833. raise Exception("Could not find wpa_supplicant process")
  1834. def read_process_memory(pid, key=None):
  1835. buf = bytes()
  1836. logger.info("Reading process memory (pid=%d)" % pid)
  1837. with open('/proc/%d/maps' % pid, 'r') as maps, \
  1838. open('/proc/%d/mem' % pid, 'r') as mem:
  1839. for l in maps.readlines():
  1840. m = re.match(r'([0-9a-f]+)-([0-9a-f]+) ([-r][-w][-x][-p])', l)
  1841. if not m:
  1842. continue
  1843. start = int(m.group(1), 16)
  1844. end = int(m.group(2), 16)
  1845. perm = m.group(3)
  1846. if start > 0xffffffffffff:
  1847. continue
  1848. if end < start:
  1849. continue
  1850. if not perm.startswith('rw'):
  1851. continue
  1852. for name in [ "[heap]", "[stack]" ]:
  1853. if name in l:
  1854. logger.info("%s 0x%x-0x%x is at %d-%d" % (name, start, end, len(buf), len(buf) + (end - start)))
  1855. mem.seek(start)
  1856. data = mem.read(end - start)
  1857. buf += data
  1858. if key and key in data:
  1859. logger.info("Key found in " + l)
  1860. logger.info("Total process memory read: %d bytes" % len(buf))
  1861. return buf
  1862. def verify_not_present(buf, key, fname, keyname):
  1863. pos = buf.find(key)
  1864. if pos < 0:
  1865. return
  1866. prefix = 2048 if pos > 2048 else pos
  1867. with open(fname + keyname, 'w') as f:
  1868. f.write(buf[pos - prefix:pos + 2048])
  1869. raise Exception(keyname + " found after disassociation")
  1870. def get_key_locations(buf, key, keyname):
  1871. count = 0
  1872. pos = 0
  1873. while True:
  1874. pos = buf.find(key, pos)
  1875. if pos < 0:
  1876. break
  1877. logger.info("Found %s at %d" % (keyname, pos))
  1878. context = 128
  1879. start = pos - context if pos > context else 0
  1880. before = binascii.hexlify(buf[start:pos])
  1881. context += len(key)
  1882. end = pos + context if pos < len(buf) - context else len(buf) - context
  1883. after = binascii.hexlify(buf[pos + len(key):end])
  1884. logger.debug("Memory context %d-%d: %s|%s|%s" % (start, end, before, binascii.hexlify(key), after))
  1885. count += 1
  1886. pos += len(key)
  1887. return count
  1888. def test_wpa2_psk_key_lifetime_in_memory(dev, apdev, params):
  1889. """WPA2-PSK and PSK/PTK lifetime in memory"""
  1890. ssid = "test-wpa2-psk"
  1891. passphrase = 'qwertyuiop'
  1892. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  1893. pmk = binascii.unhexlify(psk)
  1894. p = hostapd.wpa2_params(ssid=ssid)
  1895. p['wpa_psk'] = psk
  1896. hapd = hostapd.add_ap(apdev[0], p)
  1897. pid = find_wpas_process(dev[0])
  1898. id = dev[0].connect(ssid, raw_psk=psk, scan_freq="2412",
  1899. only_add_network=True)
  1900. logger.info("Checking keys in memory after network profile configuration")
  1901. buf = read_process_memory(pid, pmk)
  1902. get_key_locations(buf, pmk, "PMK")
  1903. dev[0].request("REMOVE_NETWORK all")
  1904. logger.info("Checking keys in memory after network profile removal")
  1905. buf = read_process_memory(pid, pmk)
  1906. get_key_locations(buf, pmk, "PMK")
  1907. id = dev[0].connect(ssid, psk=passphrase, scan_freq="2412",
  1908. only_add_network=True)
  1909. logger.info("Checking keys in memory before connection")
  1910. buf = read_process_memory(pid, pmk)
  1911. get_key_locations(buf, pmk, "PMK")
  1912. dev[0].connect_network(id, timeout=20)
  1913. # The decrypted copy of GTK is freed only after the CTRL-EVENT-CONNECTED
  1914. # event has been delivered, so verify that wpa_supplicant has returned to
  1915. # eloop before reading process memory.
  1916. time.sleep(1)
  1917. dev[0].ping()
  1918. buf = read_process_memory(pid, pmk)
  1919. dev[0].request("DISCONNECT")
  1920. dev[0].wait_disconnected()
  1921. dev[0].relog()
  1922. ptk = None
  1923. gtk = None
  1924. with open(os.path.join(params['logdir'], 'log0'), 'r') as f:
  1925. for l in f.readlines():
  1926. if "WPA: PTK - hexdump" in l:
  1927. val = l.strip().split(':')[3].replace(' ', '')
  1928. ptk = binascii.unhexlify(val)
  1929. if "WPA: Group Key - hexdump" in l:
  1930. val = l.strip().split(':')[3].replace(' ', '')
  1931. gtk = binascii.unhexlify(val)
  1932. if not pmk or not ptk or not gtk:
  1933. raise Exception("Could not find keys from debug log")
  1934. if len(gtk) != 16:
  1935. raise Exception("Unexpected GTK length")
  1936. kck = ptk[0:16]
  1937. kek = ptk[16:32]
  1938. tk = ptk[32:48]
  1939. logger.info("Checking keys in memory while associated")
  1940. get_key_locations(buf, pmk, "PMK")
  1941. if pmk not in buf:
  1942. raise HwsimSkip("PMK not found while associated")
  1943. if kck not in buf:
  1944. raise Exception("KCK not found while associated")
  1945. if kek not in buf:
  1946. raise Exception("KEK not found while associated")
  1947. #if tk in buf:
  1948. # raise Exception("TK found from memory")
  1949. logger.info("Checking keys in memory after disassociation")
  1950. buf = read_process_memory(pid, pmk)
  1951. get_key_locations(buf, pmk, "PMK")
  1952. # Note: PMK/PSK is still present in network configuration
  1953. fname = os.path.join(params['logdir'],
  1954. 'wpa2_psk_key_lifetime_in_memory.memctx-')
  1955. verify_not_present(buf, kck, fname, "KCK")
  1956. verify_not_present(buf, kek, fname, "KEK")
  1957. verify_not_present(buf, tk, fname, "TK")
  1958. if gtk in buf:
  1959. get_key_locations(buf, gtk, "GTK")
  1960. verify_not_present(buf, gtk, fname, "GTK")
  1961. dev[0].request("REMOVE_NETWORK all")
  1962. logger.info("Checking keys in memory after network profile removal")
  1963. buf = read_process_memory(pid, pmk)
  1964. get_key_locations(buf, pmk, "PMK")
  1965. verify_not_present(buf, pmk, fname, "PMK")
  1966. verify_not_present(buf, kck, fname, "KCK")
  1967. verify_not_present(buf, kek, fname, "KEK")
  1968. verify_not_present(buf, tk, fname, "TK")
  1969. verify_not_present(buf, gtk, fname, "GTK")
  1970. @remote_compatible
  1971. def test_ap_wpa2_psk_wep(dev, apdev):
  1972. """WPA2-PSK AP and WEP enabled"""
  1973. ssid = "test-wpa2-psk"
  1974. passphrase = 'qwertyuiop'
  1975. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  1976. hapd = hostapd.add_ap(apdev[0], params)
  1977. try:
  1978. hapd.set('wep_key0', '"hello"')
  1979. raise Exception("WEP key accepted to WPA2 network")
  1980. except Exception:
  1981. pass
  1982. def test_ap_wpa2_psk_wpas_in_bridge(dev, apdev):
  1983. """WPA2-PSK AP and wpas interface in a bridge"""
  1984. br_ifname='sta-br0'
  1985. ifname='wlan5'
  1986. try:
  1987. _test_ap_wpa2_psk_wpas_in_bridge(dev, apdev)
  1988. finally:
  1989. subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'down'])
  1990. subprocess.call(['brctl', 'delif', br_ifname, ifname])
  1991. subprocess.call(['brctl', 'delbr', br_ifname])
  1992. subprocess.call(['iw', ifname, 'set', '4addr', 'off'])
  1993. def _test_ap_wpa2_psk_wpas_in_bridge(dev, apdev):
  1994. ssid = "test-wpa2-psk"
  1995. passphrase = 'qwertyuiop'
  1996. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  1997. hapd = hostapd.add_ap(apdev[0], params)
  1998. br_ifname='sta-br0'
  1999. ifname='wlan5'
  2000. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  2001. subprocess.call(['brctl', 'addbr', br_ifname])
  2002. subprocess.call(['brctl', 'setfd', br_ifname, '0'])
  2003. subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'up'])
  2004. subprocess.call(['iw', ifname, 'set', '4addr', 'on'])
  2005. subprocess.check_call(['brctl', 'addif', br_ifname, ifname])
  2006. wpas.interface_add(ifname, br_ifname=br_ifname)
  2007. wpas.dump_monitor()
  2008. wpas.connect(ssid, psk=passphrase, scan_freq="2412")
  2009. wpas.dump_monitor()
  2010. @remote_compatible
  2011. def test_ap_wpa2_psk_ifdown(dev, apdev):
  2012. """AP with open mode and external ifconfig down"""
  2013. ssid = "test-wpa2-psk"
  2014. passphrase = 'qwertyuiop'
  2015. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  2016. hapd = hostapd.add_ap(apdev[0], params)
  2017. bssid = apdev[0]['bssid']
  2018. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  2019. hapd.cmd_execute(['ip', 'link', 'set', 'dev', apdev[0]['ifname'], 'down'])
  2020. ev = hapd.wait_event(["INTERFACE-DISABLED"], timeout=10)
  2021. if ev is None:
  2022. raise Exception("No INTERFACE-DISABLED event")
  2023. # this wait tests beacon loss detection in mac80211
  2024. dev[0].wait_disconnected()
  2025. hapd.cmd_execute(['ip', 'link', 'set', 'dev', apdev[0]['ifname'], 'up'])
  2026. ev = hapd.wait_event(["INTERFACE-ENABLED"], timeout=10)
  2027. if ev is None:
  2028. raise Exception("No INTERFACE-ENABLED event")
  2029. dev[0].wait_connected()
  2030. hwsim_utils.test_connectivity(dev[0], hapd)
  2031. def test_ap_wpa2_psk_drop_first_msg_4(dev, apdev):
  2032. """WPA2-PSK and first EAPOL-Key msg 4/4 dropped"""
  2033. bssid = apdev[0]['bssid']
  2034. ssid = "test-wpa2-psk"
  2035. passphrase = 'qwertyuiop'
  2036. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  2037. params = hostapd.wpa2_params(ssid=ssid)
  2038. params['wpa_psk'] = psk
  2039. hapd = hostapd.add_ap(apdev[0], params)
  2040. hapd.request("SET ext_eapol_frame_io 1")
  2041. dev[0].request("SET ext_eapol_frame_io 1")
  2042. dev[0].connect(ssid, psk=passphrase, scan_freq="2412", wait_connect=False)
  2043. addr = dev[0].own_addr()
  2044. # EAPOL-Key msg 1/4
  2045. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  2046. if ev is None:
  2047. raise Exception("Timeout on EAPOL-TX from hostapd")
  2048. res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2])
  2049. if "OK" not in res:
  2050. raise Exception("EAPOL_RX to wpa_supplicant failed")
  2051. # EAPOL-Key msg 2/4
  2052. ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
  2053. if ev is None:
  2054. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  2055. res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2])
  2056. if "OK" not in res:
  2057. raise Exception("EAPOL_RX to hostapd failed")
  2058. # EAPOL-Key msg 3/4
  2059. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  2060. if ev is None:
  2061. raise Exception("Timeout on EAPOL-TX from hostapd")
  2062. res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2])
  2063. if "OK" not in res:
  2064. raise Exception("EAPOL_RX to wpa_supplicant failed")
  2065. # EAPOL-Key msg 4/4
  2066. ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
  2067. if ev is None:
  2068. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  2069. logger.info("Drop the first EAPOL-Key msg 4/4")
  2070. # wpa_supplicant believes now that 4-way handshake succeeded; hostapd
  2071. # doesn't. Use normal EAPOL TX/RX to handle retries.
  2072. hapd.request("SET ext_eapol_frame_io 0")
  2073. dev[0].request("SET ext_eapol_frame_io 0")
  2074. dev[0].wait_connected()
  2075. ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=15)
  2076. if ev is None:
  2077. raise Exception("Timeout on AP-STA-CONNECTED from hostapd")
  2078. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=0.1)
  2079. if ev is not None:
  2080. logger.info("Disconnection detected")
  2081. # The EAPOL-Key retries are supposed to allow the connection to be
  2082. # established without having to reassociate. However, this does not
  2083. # currently work since mac80211 ends up encrypting EAPOL-Key msg 4/4
  2084. # after the pairwise key has been configured and AP will drop those and
  2085. # disconnect the station after reaching retransmission limit. Connection
  2086. # is then established after reassociation. Once that behavior has been
  2087. # optimized to prevent EAPOL-Key frame encryption for retransmission
  2088. # case, this exception can be uncommented here.
  2089. #raise Exception("Unexpected disconnection")
  2090. @remote_compatible
  2091. def test_ap_wpa2_psk_disable_enable(dev, apdev):
  2092. """WPA2-PSK AP getting disabled and re-enabled"""
  2093. ssid = "test-wpa2-psk"
  2094. passphrase = 'qwertyuiop'
  2095. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  2096. params = hostapd.wpa2_params(ssid=ssid)
  2097. params['wpa_psk'] = psk
  2098. hapd = hostapd.add_ap(apdev[0], params)
  2099. dev[0].connect(ssid, raw_psk=psk, scan_freq="2412")
  2100. for i in range(2):
  2101. hapd.request("DISABLE")
  2102. dev[0].wait_disconnected()
  2103. hapd.request("ENABLE")
  2104. dev[0].wait_connected()
  2105. hwsim_utils.test_connectivity(dev[0], hapd)
  2106. @remote_compatible
  2107. def test_ap_wpa2_psk_incorrect_passphrase(dev, apdev):
  2108. """WPA2-PSK AP and station using incorrect passphrase"""
  2109. ssid = "test-wpa2-psk"
  2110. passphrase = 'qwertyuiop'
  2111. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  2112. hapd = hostapd.add_ap(apdev[0], params)
  2113. dev[0].connect(ssid, psk="incorrect passphrase", scan_freq="2412",
  2114. wait_connect=False)
  2115. ev = hapd.wait_event(["AP-STA-POSSIBLE-PSK-MISMATCH"], timeout=10)
  2116. if ev is None:
  2117. raise Exception("No AP-STA-POSSIBLE-PSK-MISMATCH reported")
  2118. dev[0].dump_monitor()
  2119. hapd.disable()
  2120. hapd.set("wpa_passphrase", "incorrect passphrase")
  2121. hapd.enable()
  2122. dev[0].wait_connected(timeout=20)
  2123. @remote_compatible
  2124. def test_ap_wpa_ie_parsing(dev, apdev):
  2125. """WPA IE parsing"""
  2126. skip_with_fips(dev[0])
  2127. ssid = "test-wpa-psk"
  2128. passphrase = 'qwertyuiop'
  2129. params = hostapd.wpa_params(ssid=ssid, passphrase=passphrase)
  2130. hapd = hostapd.add_ap(apdev[0], params)
  2131. id = dev[0].connect(ssid, psk=passphrase, scan_freq="2412",
  2132. only_add_network=True)
  2133. tests = [ "dd040050f201",
  2134. "dd050050f20101",
  2135. "dd060050f2010100",
  2136. "dd060050f2010001",
  2137. "dd070050f201010000",
  2138. "dd080050f20101000050",
  2139. "dd090050f20101000050f2",
  2140. "dd0a0050f20101000050f202",
  2141. "dd0b0050f20101000050f20201",
  2142. "dd0c0050f20101000050f2020100",
  2143. "dd0c0050f20101000050f2020000",
  2144. "dd0c0050f20101000050f202ffff",
  2145. "dd0d0050f20101000050f202010000",
  2146. "dd0e0050f20101000050f20201000050",
  2147. "dd0f0050f20101000050f20201000050f2",
  2148. "dd100050f20101000050f20201000050f202",
  2149. "dd110050f20101000050f20201000050f20201",
  2150. "dd120050f20101000050f20201000050f2020100",
  2151. "dd120050f20101000050f20201000050f2020000",
  2152. "dd120050f20101000050f20201000050f202ffff",
  2153. "dd130050f20101000050f20201000050f202010000",
  2154. "dd140050f20101000050f20201000050f20201000050",
  2155. "dd150050f20101000050f20201000050f20201000050f2" ]
  2156. for t in tests:
  2157. try:
  2158. if "OK" not in dev[0].request("VENDOR_ELEM_ADD 13 " + t):
  2159. raise Exception("VENDOR_ELEM_ADD failed")
  2160. dev[0].select_network(id)
  2161. ev = dev[0].wait_event(["CTRL-EVENT-ASSOC-REJECT"], timeout=10)
  2162. if ev is None:
  2163. raise Exception("Association rejection not reported")
  2164. dev[0].request("DISCONNECT")
  2165. dev[0].dump_monitor()
  2166. finally:
  2167. dev[0].request("VENDOR_ELEM_REMOVE 13 *")
  2168. tests = [ "dd170050f20101000050f20201000050f20201000050f202ff",
  2169. "dd180050f20101000050f20201000050f20201000050f202ffff",
  2170. "dd190050f20101000050f20201000050f20201000050f202ffffff" ]
  2171. for t in tests:
  2172. try:
  2173. if "OK" not in dev[0].request("VENDOR_ELEM_ADD 13 " + t):
  2174. raise Exception("VENDOR_ELEM_ADD failed")
  2175. dev[0].select_network(id)
  2176. dev[0].wait_connected()
  2177. dev[0].request("DISCONNECT")
  2178. dev[0].dump_monitor()
  2179. finally:
  2180. dev[0].request("VENDOR_ELEM_REMOVE 13 *")
  2181. @remote_compatible
  2182. def test_ap_wpa2_psk_no_random(dev, apdev):
  2183. """WPA2-PSK AP and no random numbers available"""
  2184. ssid = "test-wpa2-psk"
  2185. passphrase = 'qwertyuiop'
  2186. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  2187. params = hostapd.wpa2_params(ssid=ssid)
  2188. params['wpa_psk'] = psk
  2189. hapd = hostapd.add_ap(apdev[0], params)
  2190. with fail_test(hapd, 1, "wpa_gmk_to_gtk"):
  2191. id = dev[0].connect(ssid, raw_psk=psk, scan_freq="2412",
  2192. wait_connect=False)
  2193. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=15)
  2194. if ev is None:
  2195. raise Exception("Disconnection event not reported")
  2196. dev[0].request("DISCONNECT")
  2197. dev[0].select_network(id, freq=2412)
  2198. dev[0].wait_connected()
  2199. @remote_compatible
  2200. def test_rsn_ie_proto_psk_sta(dev, apdev):
  2201. """RSN element protocol testing for PSK cases on STA side"""
  2202. bssid = apdev[0]['bssid']
  2203. ssid = "test-wpa2-psk"
  2204. passphrase = 'qwertyuiop'
  2205. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  2206. # This is the RSN element used normally by hostapd
  2207. params['own_ie_override'] = '30140100000fac040100000fac040100000fac020c00'
  2208. hapd = hostapd.add_ap(apdev[0], params)
  2209. if "FAIL" not in hapd.request("SET own_ie_override qwerty"):
  2210. raise Exception("Invalid own_ie_override value accepted")
  2211. id = dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  2212. tests = [ ('No RSN Capabilities field',
  2213. '30120100000fac040100000fac040100000fac02'),
  2214. ('Reserved RSN Capabilities bits set',
  2215. '30140100000fac040100000fac040100000fac023cff'),
  2216. ('Extra pairwise cipher suite (unsupported)',
  2217. '30180100000fac040200ffffffff000fac040100000fac020c00'),
  2218. ('Extra AKM suite (unsupported)',
  2219. '30180100000fac040100000fac040200ffffffff000fac020c00'),
  2220. ('PMKIDCount field included',
  2221. '30160100000fac040100000fac040100000fac020c000000'),
  2222. ('Unexpected Group Management Cipher Suite with PMF disabled',
  2223. '301a0100000fac040100000fac040100000fac020c000000000fac06'),
  2224. ('Extra octet after defined fields (future extensibility)',
  2225. '301b0100000fac040100000fac040100000fac020c000000000fac0600') ]
  2226. for txt,ie in tests:
  2227. dev[0].request("DISCONNECT")
  2228. dev[0].wait_disconnected()
  2229. logger.info(txt)
  2230. hapd.disable()
  2231. hapd.set('own_ie_override', ie)
  2232. hapd.enable()
  2233. dev[0].request("BSS_FLUSH 0")
  2234. dev[0].scan_for_bss(bssid, 2412, force_scan=True, only_new=True)
  2235. dev[0].select_network(id, freq=2412)
  2236. dev[0].wait_connected()
  2237. @remote_compatible
  2238. def test_ap_cli_order(dev, apdev):
  2239. ssid = "test-rsn-setup"
  2240. passphrase = 'zzzzzzzz'
  2241. hapd = hostapd.add_ap(apdev[0], {}, no_enable=True)
  2242. hapd.set('ssid', ssid)
  2243. hapd.set('wpa_passphrase', passphrase)
  2244. hapd.set('rsn_pairwise', 'CCMP')
  2245. hapd.set('wpa_key_mgmt', 'WPA-PSK')
  2246. hapd.set('wpa', '2')
  2247. hapd.enable()
  2248. cfg = hapd.get_config()
  2249. if cfg['group_cipher'] != 'CCMP':
  2250. raise Exception("Unexpected group_cipher: " + cfg['group_cipher'])
  2251. if cfg['rsn_pairwise_cipher'] != 'CCMP':
  2252. raise Exception("Unexpected rsn_pairwise_cipher: " + cfg['rsn_pairwise_cipher'])
  2253. ev = hapd.wait_event(["AP-ENABLED", "AP-DISABLED"], timeout=30)
  2254. if ev is None:
  2255. raise Exception("AP startup timed out")
  2256. if "AP-ENABLED" not in ev:
  2257. raise Exception("AP startup failed")
  2258. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  2259. def set_test_assoc_ie(dev, ie):
  2260. if "OK" not in dev.request("TEST_ASSOC_IE " + ie):
  2261. raise Exception("Could not set TEST_ASSOC_IE")
  2262. @remote_compatible
  2263. def test_ap_wpa2_psk_assoc_rsn(dev, apdev):
  2264. """WPA2-PSK AP and association request RSN IE differences"""
  2265. ssid = "test-wpa2-psk"
  2266. passphrase = 'qwertyuiop'
  2267. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  2268. hapd = hostapd.add_ap(apdev[0], params)
  2269. tests = [ ("Normal wpa_supplicant assoc req RSN IE",
  2270. "30140100000fac040100000fac040100000fac020000"),
  2271. ("RSN IE without RSN Capabilities",
  2272. "30120100000fac040100000fac040100000fac02") ]
  2273. for title, ie in tests:
  2274. logger.info(title)
  2275. set_test_assoc_ie(dev[0], ie)
  2276. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  2277. dev[0].request("REMOVE_NETWORK all")
  2278. dev[0].wait_disconnected()
  2279. tests = [ ("WPA IE instead of RSN IE and only RSN enabled on AP",
  2280. "dd160050f20101000050f20201000050f20201000050f202", 40),
  2281. ("Empty RSN IE", "3000", 40),
  2282. ("RSN IE with truncated Version", "300101", 40),
  2283. ("RSN IE with only Version", "30020100", 43) ]
  2284. for title, ie, status in tests:
  2285. logger.info(title)
  2286. set_test_assoc_ie(dev[0], ie)
  2287. dev[0].connect(ssid, psk=passphrase, scan_freq="2412",
  2288. wait_connect=False)
  2289. ev = dev[0].wait_event(["CTRL-EVENT-ASSOC-REJECT"])
  2290. if ev is None:
  2291. raise Exception("Association rejection not reported")
  2292. if "status_code=" + str(status) not in ev:
  2293. raise Exception("Unexpected status code: " + ev)
  2294. dev[0].request("REMOVE_NETWORK all")
  2295. dev[0].dump_monitor()
  2296. def test_ap_wpa_psk_rsn_pairwise(dev, apdev):
  2297. """WPA-PSK AP and only rsn_pairwise set"""
  2298. params = { "ssid": "wpapsk", "wpa": "1", "wpa_key_mgmt": "WPA-PSK",
  2299. "rsn_pairwise": "TKIP", "wpa_passphrase": "1234567890" }
  2300. hapd = hostapd.add_ap(apdev[0], params)
  2301. dev[0].connect("wpapsk", psk="1234567890", proto="WPA", pairwise="TKIP",
  2302. scan_freq="2412")
  2303. def test_ap_wpa2_eapol_retry_limit(dev, apdev):
  2304. """WPA2-PSK EAPOL-Key retry limit configuration"""
  2305. ssid = "test-wpa2-psk"
  2306. passphrase = 'qwertyuiop'
  2307. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  2308. params['wpa_ptk_rekey'] = '2'
  2309. params['wpa_group_update_count'] = '1'
  2310. params['wpa_pairwise_update_count'] = '1'
  2311. hapd = hostapd.add_ap(apdev[0], params)
  2312. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  2313. ev = dev[0].wait_event(["WPA: Key negotiation completed"])
  2314. if ev is None:
  2315. raise Exception("PTK rekey timed out")
  2316. if "FAIL" not in hapd.request("SET wpa_group_update_count 0"):
  2317. raise Exception("Invalid wpa_group_update_count value accepted")
  2318. if "FAIL" not in hapd.request("SET wpa_pairwise_update_count 0"):
  2319. raise Exception("Invalid wpa_pairwise_update_count value accepted")