test_ap_psk.py 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852
  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 test_ap_wpa2_psk_ext_delayed_ptk_rekey(dev, apdev):
  870. """WPA2-PSK AP using external EAPOL I/O and delayed PTK rekey exchange"""
  871. bssid = apdev[0]['bssid']
  872. ssid = "test-wpa2-psk"
  873. passphrase = 'qwertyuiop'
  874. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  875. params = hostapd.wpa2_params(ssid=ssid)
  876. params['wpa_psk'] = psk
  877. params['wpa_ptk_rekey'] = '3'
  878. hapd = hostapd.add_ap(apdev[0], params)
  879. hapd.request("SET ext_eapol_frame_io 1")
  880. dev[0].request("SET ext_eapol_frame_io 1")
  881. dev[0].connect(ssid, psk=passphrase, scan_freq="2412", wait_connect=False)
  882. addr = dev[0].p2p_interface_addr()
  883. # EAPOL-Key msg 1/4
  884. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  885. if ev is None:
  886. raise Exception("Timeout on EAPOL-TX from hostapd")
  887. res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2])
  888. if "OK" not in res:
  889. raise Exception("EAPOL_RX to wpa_supplicant failed")
  890. # EAPOL-Key msg 2/4
  891. ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
  892. if ev is None:
  893. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  894. msg2 = ev.split(' ')[2]
  895. # Do not send this to the AP
  896. # EAPOL-Key msg 1/4 (retry)
  897. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  898. if ev is None:
  899. raise Exception("Timeout on EAPOL-TX from hostapd")
  900. res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2])
  901. if "OK" not in res:
  902. raise Exception("EAPOL_RX to wpa_supplicant failed")
  903. # EAPOL-Key msg 2/4
  904. ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
  905. if ev is None:
  906. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  907. res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2])
  908. if "OK" not in res:
  909. raise Exception("EAPOL_RX to hostapd failed")
  910. # EAPOL-Key msg 3/4
  911. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  912. if ev is None:
  913. raise Exception("Timeout on EAPOL-TX from hostapd")
  914. res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2])
  915. if "OK" not in res:
  916. raise Exception("EAPOL_RX to wpa_supplicant failed")
  917. # EAPOL-Key msg 4/4
  918. ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
  919. if ev is None:
  920. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  921. msg4 = ev.split(' ')[2]
  922. # Do not send msg 4/4 to AP
  923. # EAPOL-Key msg 3/4 (retry)
  924. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  925. if ev is None:
  926. raise Exception("Timeout on EAPOL-TX from hostapd")
  927. res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2])
  928. if "OK" not in res:
  929. raise Exception("EAPOL_RX to wpa_supplicant failed")
  930. # EAPOL-Key msg 4/4
  931. ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
  932. if ev is None:
  933. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  934. msg4b = ev.split(' ')[2]
  935. # Do not send msg 4/4 to AP
  936. # Send the previous EAPOL-Key msg 4/4 to AP
  937. res = hapd.request("EAPOL_RX " + addr + " " + msg4)
  938. if "OK" not in res:
  939. raise Exception("EAPOL_RX to hostapd failed")
  940. ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=15)
  941. if ev is None:
  942. raise Exception("Timeout on AP-STA-CONNECTED from hostapd")
  943. # Wait for PTK rekeying to be initialized
  944. # EAPOL-Key msg 1/4
  945. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  946. if ev is None:
  947. raise Exception("Timeout on EAPOL-TX from hostapd")
  948. # EAPOL-Key msg 2/4 from the previous 4-way handshake
  949. # hostapd is expected to ignore this due to unexpected Replay Counter
  950. res = hapd.request("EAPOL_RX " + addr + " " + msg2)
  951. if "OK" not in res:
  952. raise Exception("EAPOL_RX to hostapd failed")
  953. # EAPOL-Key msg 3/4 (actually, this ends up being retransmitted 1/4)
  954. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  955. if ev is None:
  956. raise Exception("Timeout on EAPOL-TX from hostapd")
  957. keyinfo = ev.split(' ')[2][10:14]
  958. if keyinfo != "008a":
  959. raise Exception("Unexpected key info when expected msg 1/4:" + keyinfo)
  960. # EAPOL-Key msg 4/4 from the previous 4-way handshake
  961. # hostapd is expected to ignore this due to unexpected Replay Counter
  962. res = hapd.request("EAPOL_RX " + addr + " " + msg4b)
  963. if "OK" not in res:
  964. raise Exception("EAPOL_RX to hostapd failed")
  965. # Check if any more EAPOL-Key frames are seen. If the second 4-way handshake
  966. # was accepted, there would be no more EAPOL-Key frames. If the Replay
  967. # Counters were rejected, there would be a retransmitted msg 1/4 here.
  968. ev = hapd.wait_event(["EAPOL-TX"], timeout=1)
  969. if ev is None:
  970. raise Exception("Did not see EAPOL-TX from hostapd in the end (expected msg 1/4)")
  971. keyinfo = ev.split(' ')[2][10:14]
  972. if keyinfo != "008a":
  973. raise Exception("Unexpected key info when expected msg 1/4:" + keyinfo)
  974. def parse_eapol(data):
  975. (version, type, length) = struct.unpack('>BBH', data[0:4])
  976. payload = data[4:]
  977. if length > len(payload):
  978. raise Exception("Invalid EAPOL length")
  979. if length < len(payload):
  980. payload = payload[0:length]
  981. eapol = {}
  982. eapol['version'] = version
  983. eapol['type'] = type
  984. eapol['length'] = length
  985. eapol['payload'] = payload
  986. if type == 3:
  987. # EAPOL-Key
  988. (eapol['descr_type'],) = struct.unpack('B', payload[0:1])
  989. payload = payload[1:]
  990. if eapol['descr_type'] == 2 or eapol['descr_type'] == 254:
  991. # RSN EAPOL-Key
  992. (key_info, key_len) = struct.unpack('>HH', payload[0:4])
  993. eapol['rsn_key_info'] = key_info
  994. eapol['rsn_key_len'] = key_len
  995. eapol['rsn_replay_counter'] = payload[4:12]
  996. eapol['rsn_key_nonce'] = payload[12:44]
  997. eapol['rsn_key_iv'] = payload[44:60]
  998. eapol['rsn_key_rsc'] = payload[60:68]
  999. eapol['rsn_key_id'] = payload[68:76]
  1000. eapol['rsn_key_mic'] = payload[76:92]
  1001. payload = payload[92:]
  1002. (eapol['rsn_key_data_len'],) = struct.unpack('>H', payload[0:2])
  1003. payload = payload[2:]
  1004. eapol['rsn_key_data'] = payload
  1005. return eapol
  1006. def build_eapol(msg):
  1007. data = struct.pack(">BBH", msg['version'], msg['type'], msg['length'])
  1008. if msg['type'] == 3:
  1009. data += struct.pack('>BHH', msg['descr_type'], msg['rsn_key_info'],
  1010. msg['rsn_key_len'])
  1011. data += msg['rsn_replay_counter']
  1012. data += msg['rsn_key_nonce']
  1013. data += msg['rsn_key_iv']
  1014. data += msg['rsn_key_rsc']
  1015. data += msg['rsn_key_id']
  1016. data += msg['rsn_key_mic']
  1017. data += struct.pack('>H', msg['rsn_key_data_len'])
  1018. data += msg['rsn_key_data']
  1019. else:
  1020. data += msg['payload']
  1021. return data
  1022. def sha1_prf(key, label, data, outlen):
  1023. res = ''
  1024. counter = 0
  1025. while outlen > 0:
  1026. m = hmac.new(key, label, hashlib.sha1)
  1027. m.update(struct.pack('B', 0))
  1028. m.update(data)
  1029. m.update(struct.pack('B', counter))
  1030. counter += 1
  1031. hash = m.digest()
  1032. if outlen > len(hash):
  1033. res += hash
  1034. outlen -= len(hash)
  1035. else:
  1036. res += hash[0:outlen]
  1037. outlen = 0
  1038. return res
  1039. def pmk_to_ptk(pmk, addr1, addr2, nonce1, nonce2):
  1040. if addr1 < addr2:
  1041. data = binascii.unhexlify(addr1.replace(':','')) + binascii.unhexlify(addr2.replace(':',''))
  1042. else:
  1043. data = binascii.unhexlify(addr2.replace(':','')) + binascii.unhexlify(addr1.replace(':',''))
  1044. if nonce1 < nonce2:
  1045. data += nonce1 + nonce2
  1046. else:
  1047. data += nonce2 + nonce1
  1048. label = "Pairwise key expansion"
  1049. ptk = sha1_prf(pmk, label, data, 48)
  1050. kck = ptk[0:16]
  1051. kek = ptk[16:32]
  1052. return (ptk, kck, kek)
  1053. def eapol_key_mic(kck, msg):
  1054. msg['rsn_key_mic'] = binascii.unhexlify('00000000000000000000000000000000')
  1055. data = build_eapol(msg)
  1056. m = hmac.new(kck, data, hashlib.sha1)
  1057. msg['rsn_key_mic'] = m.digest()[0:16]
  1058. def rsn_eapol_key_set(msg, key_info, key_len, nonce, data):
  1059. msg['rsn_key_info'] = key_info
  1060. msg['rsn_key_len'] = key_len
  1061. if nonce:
  1062. msg['rsn_key_nonce'] = nonce
  1063. else:
  1064. msg['rsn_key_nonce'] = binascii.unhexlify('0000000000000000000000000000000000000000000000000000000000000000')
  1065. if data:
  1066. msg['rsn_key_data_len'] = len(data)
  1067. msg['rsn_key_data'] = data
  1068. msg['length'] = 95 + len(data)
  1069. else:
  1070. msg['rsn_key_data_len'] = 0
  1071. msg['rsn_key_data'] = ''
  1072. msg['length'] = 95
  1073. def recv_eapol(hapd):
  1074. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  1075. if ev is None:
  1076. raise Exception("Timeout on EAPOL-TX from hostapd")
  1077. eapol = binascii.unhexlify(ev.split(' ')[2])
  1078. return parse_eapol(eapol)
  1079. def send_eapol(hapd, addr, data):
  1080. res = hapd.request("EAPOL_RX " + addr + " " + binascii.hexlify(data))
  1081. if "OK" not in res:
  1082. raise Exception("EAPOL_RX to hostapd failed")
  1083. def reply_eapol(info, hapd, addr, msg, key_info, nonce, data, kck):
  1084. logger.info("Send EAPOL-Key msg " + info)
  1085. rsn_eapol_key_set(msg, key_info, 0, nonce, data)
  1086. eapol_key_mic(kck, msg)
  1087. send_eapol(hapd, addr, build_eapol(msg))
  1088. def hapd_connected(hapd):
  1089. ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=15)
  1090. if ev is None:
  1091. raise Exception("Timeout on AP-STA-CONNECTED from hostapd")
  1092. def eapol_test(apdev, dev, wpa2=True):
  1093. bssid = apdev['bssid']
  1094. if wpa2:
  1095. ssid = "test-wpa2-psk"
  1096. else:
  1097. ssid = "test-wpa-psk"
  1098. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  1099. pmk = binascii.unhexlify(psk)
  1100. if wpa2:
  1101. params = hostapd.wpa2_params(ssid=ssid)
  1102. else:
  1103. params = hostapd.wpa_params(ssid=ssid)
  1104. params['wpa_psk'] = psk
  1105. hapd = hostapd.add_ap(apdev, params)
  1106. hapd.request("SET ext_eapol_frame_io 1")
  1107. dev.request("SET ext_eapol_frame_io 1")
  1108. dev.connect(ssid, raw_psk=psk, scan_freq="2412", wait_connect=False)
  1109. addr = dev.p2p_interface_addr()
  1110. if wpa2:
  1111. rsne = binascii.unhexlify('30140100000fac040100000fac040100000fac020000')
  1112. else:
  1113. rsne = binascii.unhexlify('dd160050f20101000050f20201000050f20201000050f202')
  1114. snonce = binascii.unhexlify('1111111111111111111111111111111111111111111111111111111111111111')
  1115. return (bssid,ssid,hapd,snonce,pmk,addr,rsne)
  1116. @remote_compatible
  1117. def test_ap_wpa2_psk_ext_eapol(dev, apdev):
  1118. """WPA2-PSK AP using external EAPOL supplicant"""
  1119. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1120. msg = recv_eapol(hapd)
  1121. anonce = msg['rsn_key_nonce']
  1122. logger.info("Replay same data back")
  1123. send_eapol(hapd, addr, build_eapol(msg))
  1124. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1125. logger.info("Truncated Key Data in EAPOL-Key msg 2/4")
  1126. rsn_eapol_key_set(msg, 0x0101, 0, snonce, rsne)
  1127. msg['length'] = 95 + 22 - 1
  1128. send_eapol(hapd, addr, build_eapol(msg))
  1129. reply_eapol("2/4", hapd, addr, msg, 0x010a, snonce, rsne, kck)
  1130. msg = recv_eapol(hapd)
  1131. if anonce != msg['rsn_key_nonce']:
  1132. raise Exception("ANonce changed")
  1133. logger.info("Replay same data back")
  1134. send_eapol(hapd, addr, build_eapol(msg))
  1135. reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck)
  1136. hapd_connected(hapd)
  1137. @remote_compatible
  1138. def test_ap_wpa2_psk_ext_eapol_retry1(dev, apdev):
  1139. """WPA2 4-way handshake with EAPOL-Key 1/4 retransmitted"""
  1140. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1141. msg1 = recv_eapol(hapd)
  1142. anonce = msg1['rsn_key_nonce']
  1143. msg2 = recv_eapol(hapd)
  1144. if anonce != msg2['rsn_key_nonce']:
  1145. raise Exception("ANonce changed")
  1146. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1147. logger.info("Send EAPOL-Key msg 2/4")
  1148. msg = msg2
  1149. rsn_eapol_key_set(msg, 0x010a, 0, snonce, rsne)
  1150. eapol_key_mic(kck, msg)
  1151. send_eapol(hapd, addr, build_eapol(msg))
  1152. msg = recv_eapol(hapd)
  1153. if anonce != msg['rsn_key_nonce']:
  1154. raise Exception("ANonce changed")
  1155. reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck)
  1156. hapd_connected(hapd)
  1157. @remote_compatible
  1158. def test_ap_wpa2_psk_ext_eapol_retry1b(dev, apdev):
  1159. """WPA2 4-way handshake with EAPOL-Key 1/4 and 2/4 retransmitted"""
  1160. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1161. msg1 = recv_eapol(hapd)
  1162. anonce = msg1['rsn_key_nonce']
  1163. msg2 = recv_eapol(hapd)
  1164. if anonce != msg2['rsn_key_nonce']:
  1165. raise Exception("ANonce changed")
  1166. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1167. reply_eapol("2/4 (a)", hapd, addr, msg1, 0x010a, snonce, rsne, kck)
  1168. reply_eapol("2/4 (b)", hapd, addr, msg2, 0x010a, snonce, rsne, kck)
  1169. msg = recv_eapol(hapd)
  1170. if anonce != msg['rsn_key_nonce']:
  1171. raise Exception("ANonce changed")
  1172. reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck)
  1173. hapd_connected(hapd)
  1174. @remote_compatible
  1175. def test_ap_wpa2_psk_ext_eapol_retry1c(dev, apdev):
  1176. """WPA2 4-way handshake with EAPOL-Key 1/4 and 2/4 retransmitted and SNonce changing"""
  1177. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1178. msg1 = recv_eapol(hapd)
  1179. anonce = msg1['rsn_key_nonce']
  1180. msg2 = recv_eapol(hapd)
  1181. if anonce != msg2['rsn_key_nonce']:
  1182. raise Exception("ANonce changed")
  1183. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1184. reply_eapol("2/4 (a)", hapd, addr, msg1, 0x010a, snonce, rsne, kck)
  1185. snonce2 = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1186. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce2, anonce)
  1187. reply_eapol("2/4 (b)", hapd, addr, msg2, 0x010a, snonce2, rsne, kck)
  1188. msg = recv_eapol(hapd)
  1189. if anonce != msg['rsn_key_nonce']:
  1190. raise Exception("ANonce changed")
  1191. reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck)
  1192. hapd_connected(hapd)
  1193. @remote_compatible
  1194. def test_ap_wpa2_psk_ext_eapol_retry1d(dev, apdev):
  1195. """WPA2 4-way handshake with EAPOL-Key 1/4 and 2/4 retransmitted and SNonce changing and older used"""
  1196. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1197. msg1 = recv_eapol(hapd)
  1198. anonce = msg1['rsn_key_nonce']
  1199. msg2 = recv_eapol(hapd)
  1200. if anonce != msg2['rsn_key_nonce']:
  1201. raise Exception("ANonce changed")
  1202. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1203. reply_eapol("2/4 (a)", hapd, addr, msg1, 0x010a, snonce, rsne, kck)
  1204. snonce2 = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1205. (ptk2, kck2, kek2) = pmk_to_ptk(pmk, addr, bssid, snonce2, anonce)
  1206. reply_eapol("2/4 (b)", hapd, addr, msg2, 0x010a, snonce2, rsne, kck2)
  1207. msg = recv_eapol(hapd)
  1208. if anonce != msg['rsn_key_nonce']:
  1209. raise Exception("ANonce changed")
  1210. reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck)
  1211. hapd_connected(hapd)
  1212. @remote_compatible
  1213. def test_ap_wpa2_psk_ext_eapol_type_diff(dev, apdev):
  1214. """WPA2 4-way handshake using external EAPOL supplicant"""
  1215. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1216. msg = recv_eapol(hapd)
  1217. anonce = msg['rsn_key_nonce']
  1218. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1219. # Incorrect descriptor type (frame dropped)
  1220. msg['descr_type'] = 253
  1221. rsn_eapol_key_set(msg, 0x010a, 0, snonce, rsne)
  1222. eapol_key_mic(kck, msg)
  1223. send_eapol(hapd, addr, build_eapol(msg))
  1224. # Incorrect descriptor type, but with a workaround (frame processed)
  1225. msg['descr_type'] = 254
  1226. rsn_eapol_key_set(msg, 0x010a, 0, snonce, rsne)
  1227. eapol_key_mic(kck, msg)
  1228. send_eapol(hapd, addr, build_eapol(msg))
  1229. msg = recv_eapol(hapd)
  1230. if anonce != msg['rsn_key_nonce']:
  1231. raise Exception("ANonce changed")
  1232. logger.info("Replay same data back")
  1233. send_eapol(hapd, addr, build_eapol(msg))
  1234. reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck)
  1235. hapd_connected(hapd)
  1236. @remote_compatible
  1237. def test_ap_wpa_psk_ext_eapol(dev, apdev):
  1238. """WPA2-PSK AP using external EAPOL supplicant"""
  1239. (bssid,ssid,hapd,snonce,pmk,addr,wpae) = eapol_test(apdev[0], dev[0],
  1240. wpa2=False)
  1241. msg = recv_eapol(hapd)
  1242. anonce = msg['rsn_key_nonce']
  1243. logger.info("Replay same data back")
  1244. send_eapol(hapd, addr, build_eapol(msg))
  1245. logger.info("Too short data")
  1246. send_eapol(hapd, addr, build_eapol(msg)[0:98])
  1247. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1248. msg['descr_type'] = 2
  1249. reply_eapol("2/4(invalid type)", hapd, addr, msg, 0x010a, snonce, wpae, kck)
  1250. msg['descr_type'] = 254
  1251. reply_eapol("2/4", hapd, addr, msg, 0x010a, snonce, wpae, kck)
  1252. msg = recv_eapol(hapd)
  1253. if anonce != msg['rsn_key_nonce']:
  1254. raise Exception("ANonce changed")
  1255. logger.info("Replay same data back")
  1256. send_eapol(hapd, addr, build_eapol(msg))
  1257. reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck)
  1258. hapd_connected(hapd)
  1259. @remote_compatible
  1260. def test_ap_wpa2_psk_ext_eapol_key_info(dev, apdev):
  1261. """WPA2-PSK 4-way handshake with strange key info values"""
  1262. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1263. msg = recv_eapol(hapd)
  1264. anonce = msg['rsn_key_nonce']
  1265. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1266. rsn_eapol_key_set(msg, 0x0000, 0, snonce, rsne)
  1267. send_eapol(hapd, addr, build_eapol(msg))
  1268. rsn_eapol_key_set(msg, 0xffff, 0, snonce, rsne)
  1269. send_eapol(hapd, addr, build_eapol(msg))
  1270. # SMK M1
  1271. rsn_eapol_key_set(msg, 0x2802, 0, snonce, rsne)
  1272. send_eapol(hapd, addr, build_eapol(msg))
  1273. # SMK M3
  1274. rsn_eapol_key_set(msg, 0x2002, 0, snonce, rsne)
  1275. send_eapol(hapd, addr, build_eapol(msg))
  1276. # Request
  1277. rsn_eapol_key_set(msg, 0x0902, 0, snonce, rsne)
  1278. send_eapol(hapd, addr, build_eapol(msg))
  1279. # Request
  1280. rsn_eapol_key_set(msg, 0x0902, 0, snonce, rsne)
  1281. tmp_kck = binascii.unhexlify('00000000000000000000000000000000')
  1282. eapol_key_mic(tmp_kck, msg)
  1283. send_eapol(hapd, addr, build_eapol(msg))
  1284. reply_eapol("2/4", hapd, addr, msg, 0x010a, snonce, rsne, kck)
  1285. msg = recv_eapol(hapd)
  1286. if anonce != msg['rsn_key_nonce']:
  1287. raise Exception("ANonce changed")
  1288. # Request (valic MIC)
  1289. rsn_eapol_key_set(msg, 0x0902, 0, snonce, rsne)
  1290. eapol_key_mic(kck, msg)
  1291. send_eapol(hapd, addr, build_eapol(msg))
  1292. # Request (valid MIC, replayed counter)
  1293. rsn_eapol_key_set(msg, 0x0902, 0, snonce, rsne)
  1294. eapol_key_mic(kck, msg)
  1295. send_eapol(hapd, addr, build_eapol(msg))
  1296. reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck)
  1297. hapd_connected(hapd)
  1298. def build_eapol_key_1_4(anonce, replay_counter=1, key_data='', key_len=16):
  1299. msg = {}
  1300. msg['version'] = 2
  1301. msg['type'] = 3
  1302. msg['length'] = 95 + len(key_data)
  1303. msg['descr_type'] = 2
  1304. msg['rsn_key_info'] = 0x8a
  1305. msg['rsn_key_len'] = key_len
  1306. msg['rsn_replay_counter'] = struct.pack('>Q', replay_counter)
  1307. msg['rsn_key_nonce'] = anonce
  1308. msg['rsn_key_iv'] = binascii.unhexlify('00000000000000000000000000000000')
  1309. msg['rsn_key_rsc'] = binascii.unhexlify('0000000000000000')
  1310. msg['rsn_key_id'] = binascii.unhexlify('0000000000000000')
  1311. msg['rsn_key_mic'] = binascii.unhexlify('00000000000000000000000000000000')
  1312. msg['rsn_key_data_len'] = len(key_data)
  1313. msg['rsn_key_data'] = key_data
  1314. return msg
  1315. def build_eapol_key_3_4(anonce, kck, key_data, replay_counter=2,
  1316. key_info=0x13ca, extra_len=0, descr_type=2, key_len=16):
  1317. msg = {}
  1318. msg['version'] = 2
  1319. msg['type'] = 3
  1320. msg['length'] = 95 + len(key_data) + extra_len
  1321. msg['descr_type'] = descr_type
  1322. msg['rsn_key_info'] = key_info
  1323. msg['rsn_key_len'] = key_len
  1324. msg['rsn_replay_counter'] = struct.pack('>Q', replay_counter)
  1325. msg['rsn_key_nonce'] = anonce
  1326. msg['rsn_key_iv'] = binascii.unhexlify('00000000000000000000000000000000')
  1327. msg['rsn_key_rsc'] = binascii.unhexlify('0000000000000000')
  1328. msg['rsn_key_id'] = binascii.unhexlify('0000000000000000')
  1329. msg['rsn_key_data_len'] = len(key_data)
  1330. msg['rsn_key_data'] = key_data
  1331. eapol_key_mic(kck, msg)
  1332. return msg
  1333. def aes_wrap(kek, plain):
  1334. n = len(plain) / 8
  1335. a = 0xa6a6a6a6a6a6a6a6
  1336. enc = AES.new(kek).encrypt
  1337. r = [plain[i * 8:(i + 1) * 8] for i in range(0, n)]
  1338. for j in range(6):
  1339. for i in range(1, n + 1):
  1340. b = enc(struct.pack('>Q', a) + r[i - 1])
  1341. a = struct.unpack('>Q', b[:8])[0] ^ (n * j + i)
  1342. r[i - 1] =b[8:]
  1343. return struct.pack('>Q', a) + ''.join(r)
  1344. def pad_key_data(plain):
  1345. pad_len = len(plain) % 8
  1346. if pad_len:
  1347. pad_len = 8 - pad_len
  1348. plain += '\xdd'
  1349. pad_len -= 1
  1350. plain += pad_len * '\0'
  1351. return plain
  1352. def test_ap_wpa2_psk_supp_proto(dev, apdev):
  1353. """WPA2-PSK 4-way handshake protocol testing for supplicant"""
  1354. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1355. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1356. msg = recv_eapol(hapd)
  1357. dev[0].dump_monitor()
  1358. # Build own EAPOL-Key msg 1/4
  1359. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1360. counter = 1
  1361. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1362. counter += 1
  1363. send_eapol(dev[0], bssid, build_eapol(msg))
  1364. msg = recv_eapol(dev[0])
  1365. snonce = msg['rsn_key_nonce']
  1366. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1367. logger.debug("Invalid AES wrap data length 0")
  1368. dev[0].dump_monitor()
  1369. msg = build_eapol_key_3_4(anonce, kck, '', replay_counter=counter)
  1370. counter += 1
  1371. send_eapol(dev[0], bssid, build_eapol(msg))
  1372. ev = dev[0].wait_event(["WPA: Unsupported AES-WRAP len 0"])
  1373. if ev is None:
  1374. raise Exception("Unsupported AES-WRAP len 0 not reported")
  1375. logger.debug("Invalid AES wrap data length 1")
  1376. dev[0].dump_monitor()
  1377. msg = build_eapol_key_3_4(anonce, kck, '1', replay_counter=counter)
  1378. counter += 1
  1379. send_eapol(dev[0], bssid, build_eapol(msg))
  1380. ev = dev[0].wait_event(["WPA: Unsupported AES-WRAP len 1"])
  1381. if ev is None:
  1382. raise Exception("Unsupported AES-WRAP len 1 not reported")
  1383. logger.debug("Invalid AES wrap data length 9")
  1384. dev[0].dump_monitor()
  1385. msg = build_eapol_key_3_4(anonce, kck, '123456789', replay_counter=counter)
  1386. counter += 1
  1387. send_eapol(dev[0], bssid, build_eapol(msg))
  1388. ev = dev[0].wait_event(["WPA: Unsupported AES-WRAP len 9"])
  1389. if ev is None:
  1390. raise Exception("Unsupported AES-WRAP len 9 not reported")
  1391. logger.debug("Invalid AES wrap data payload")
  1392. dev[0].dump_monitor()
  1393. msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter)
  1394. # do not increment counter to test replay protection
  1395. send_eapol(dev[0], bssid, build_eapol(msg))
  1396. ev = dev[0].wait_event(["WPA: AES unwrap failed"])
  1397. if ev is None:
  1398. raise Exception("AES unwrap failure not reported")
  1399. logger.debug("Replay Count not increasing")
  1400. dev[0].dump_monitor()
  1401. msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter)
  1402. counter += 1
  1403. send_eapol(dev[0], bssid, build_eapol(msg))
  1404. ev = dev[0].wait_event(["WPA: EAPOL-Key Replay Counter did not increase"])
  1405. if ev is None:
  1406. raise Exception("Replay Counter replay not reported")
  1407. logger.debug("Missing Ack bit in key info")
  1408. dev[0].dump_monitor()
  1409. msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter,
  1410. key_info=0x134a)
  1411. counter += 1
  1412. send_eapol(dev[0], bssid, build_eapol(msg))
  1413. ev = dev[0].wait_event(["WPA: No Ack bit in key_info"])
  1414. if ev is None:
  1415. raise Exception("Missing Ack bit not reported")
  1416. logger.debug("Unexpected Request bit in key info")
  1417. dev[0].dump_monitor()
  1418. msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter,
  1419. key_info=0x1bca)
  1420. counter += 1
  1421. send_eapol(dev[0], bssid, build_eapol(msg))
  1422. ev = dev[0].wait_event(["WPA: EAPOL-Key with Request bit"])
  1423. if ev is None:
  1424. raise Exception("Request bit not reported")
  1425. logger.debug("Unsupported key descriptor version 0")
  1426. dev[0].dump_monitor()
  1427. msg = build_eapol_key_3_4(anonce, kck, '0123456789abcdef',
  1428. replay_counter=counter, key_info=0x13c8)
  1429. counter += 1
  1430. send_eapol(dev[0], bssid, build_eapol(msg))
  1431. ev = dev[0].wait_event(["WPA: Unsupported EAPOL-Key descriptor version 0"])
  1432. if ev is None:
  1433. raise Exception("Unsupported EAPOL-Key descriptor version 0 not reported")
  1434. logger.debug("Key descriptor version 1 not allowed with CCMP")
  1435. dev[0].dump_monitor()
  1436. msg = build_eapol_key_3_4(anonce, kck, '0123456789abcdef',
  1437. replay_counter=counter, key_info=0x13c9)
  1438. counter += 1
  1439. send_eapol(dev[0], bssid, build_eapol(msg))
  1440. ev = dev[0].wait_event(["WPA: CCMP is used, but EAPOL-Key descriptor version (1) is not 2"])
  1441. if ev is None:
  1442. raise Exception("Not allowed EAPOL-Key descriptor version not reported")
  1443. logger.debug("Invalid AES wrap payload with key descriptor version 2")
  1444. dev[0].dump_monitor()
  1445. msg = build_eapol_key_3_4(anonce, kck, '0123456789abcdef',
  1446. replay_counter=counter, key_info=0x13ca)
  1447. counter += 1
  1448. send_eapol(dev[0], bssid, build_eapol(msg))
  1449. ev = dev[0].wait_event(["WPA: AES unwrap failed"])
  1450. if ev is None:
  1451. raise Exception("AES unwrap failure not reported")
  1452. logger.debug("Key descriptor version 3 workaround")
  1453. dev[0].dump_monitor()
  1454. msg = build_eapol_key_3_4(anonce, kck, '0123456789abcdef',
  1455. replay_counter=counter, key_info=0x13cb)
  1456. counter += 1
  1457. send_eapol(dev[0], bssid, build_eapol(msg))
  1458. ev = dev[0].wait_event(["WPA: CCMP is used, but EAPOL-Key descriptor version (3) is not 2"])
  1459. if ev is None:
  1460. raise Exception("CCMP key descriptor mismatch not reported")
  1461. ev = dev[0].wait_event(["WPA: Interoperability workaround"])
  1462. if ev is None:
  1463. raise Exception("AES-128-CMAC workaround not reported")
  1464. ev = dev[0].wait_event(["WPA: Invalid EAPOL-Key MIC - dropping packet"])
  1465. if ev is None:
  1466. raise Exception("MIC failure with AES-128-CMAC workaround not reported")
  1467. logger.debug("Unsupported key descriptor version 4")
  1468. dev[0].dump_monitor()
  1469. msg = build_eapol_key_3_4(anonce, kck, '0123456789abcdef',
  1470. replay_counter=counter, key_info=0x13cc)
  1471. counter += 1
  1472. send_eapol(dev[0], bssid, build_eapol(msg))
  1473. ev = dev[0].wait_event(["WPA: Unsupported EAPOL-Key descriptor version 4"])
  1474. if ev is None:
  1475. raise Exception("Unsupported EAPOL-Key descriptor version 4 not reported")
  1476. logger.debug("Unsupported key descriptor version 7")
  1477. dev[0].dump_monitor()
  1478. msg = build_eapol_key_3_4(anonce, kck, '0123456789abcdef',
  1479. replay_counter=counter, key_info=0x13cf)
  1480. counter += 1
  1481. send_eapol(dev[0], bssid, build_eapol(msg))
  1482. ev = dev[0].wait_event(["WPA: Unsupported EAPOL-Key descriptor version 7"])
  1483. if ev is None:
  1484. raise Exception("Unsupported EAPOL-Key descriptor version 7 not reported")
  1485. logger.debug("Too short EAPOL header length")
  1486. dev[0].dump_monitor()
  1487. msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter,
  1488. extra_len=-1)
  1489. counter += 1
  1490. send_eapol(dev[0], bssid, build_eapol(msg))
  1491. ev = dev[0].wait_event(["WPA: Invalid EAPOL-Key frame - key_data overflow (8 > 7)"])
  1492. if ev is None:
  1493. raise Exception("Key data overflow not reported")
  1494. logger.debug("Too long EAPOL header length")
  1495. msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter,
  1496. extra_len=1)
  1497. counter += 1
  1498. send_eapol(dev[0], bssid, build_eapol(msg))
  1499. logger.debug("Unsupported descriptor type 0")
  1500. msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter,
  1501. descr_type=0)
  1502. counter += 1
  1503. send_eapol(dev[0], bssid, build_eapol(msg))
  1504. logger.debug("WPA descriptor type 0")
  1505. msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter,
  1506. descr_type=254)
  1507. counter += 1
  1508. send_eapol(dev[0], bssid, build_eapol(msg))
  1509. logger.debug("Non-zero key index for pairwise key")
  1510. dev[0].dump_monitor()
  1511. wrapped = aes_wrap(kek, 16*'z')
  1512. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter,
  1513. key_info=0x13ea)
  1514. counter += 1
  1515. send_eapol(dev[0], bssid, build_eapol(msg))
  1516. ev = dev[0].wait_event(["WPA: Ignored EAPOL-Key (Pairwise) with non-zero key index"])
  1517. if ev is None:
  1518. raise Exception("Non-zero key index not reported")
  1519. logger.debug("Invalid Key Data plaintext payload --> disconnect")
  1520. dev[0].dump_monitor()
  1521. wrapped = aes_wrap(kek, 16*'z')
  1522. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1523. counter += 1
  1524. send_eapol(dev[0], bssid, build_eapol(msg))
  1525. dev[0].wait_disconnected(timeout=1)
  1526. def test_ap_wpa2_psk_supp_proto_no_ie(dev, apdev):
  1527. """WPA2-PSK supplicant protocol testing: IE not included"""
  1528. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1529. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1530. msg = recv_eapol(hapd)
  1531. dev[0].dump_monitor()
  1532. # Build own EAPOL-Key msg 1/4
  1533. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1534. counter = 1
  1535. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1536. counter += 1
  1537. send_eapol(dev[0], bssid, build_eapol(msg))
  1538. msg = recv_eapol(dev[0])
  1539. snonce = msg['rsn_key_nonce']
  1540. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1541. logger.debug("No IEs in msg 3/4 --> disconnect")
  1542. dev[0].dump_monitor()
  1543. wrapped = aes_wrap(kek, 16*'\0')
  1544. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1545. counter += 1
  1546. send_eapol(dev[0], bssid, build_eapol(msg))
  1547. dev[0].wait_disconnected(timeout=1)
  1548. def test_ap_wpa2_psk_supp_proto_ie_mismatch(dev, apdev):
  1549. """WPA2-PSK supplicant protocol testing: IE mismatch"""
  1550. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1551. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1552. msg = recv_eapol(hapd)
  1553. dev[0].dump_monitor()
  1554. # Build own EAPOL-Key msg 1/4
  1555. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1556. counter = 1
  1557. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1558. counter += 1
  1559. send_eapol(dev[0], bssid, build_eapol(msg))
  1560. msg = recv_eapol(dev[0])
  1561. snonce = msg['rsn_key_nonce']
  1562. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1563. logger.debug("Msg 3/4 with mismatching IE")
  1564. dev[0].dump_monitor()
  1565. wrapped = aes_wrap(kek, pad_key_data(binascii.unhexlify('30060100000fac04dd16000fac010100dc11188831bf4aa4a8678d2b41498618')))
  1566. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1567. counter += 1
  1568. send_eapol(dev[0], bssid, build_eapol(msg))
  1569. dev[0].wait_disconnected(timeout=1)
  1570. def test_ap_wpa2_psk_supp_proto_ok(dev, apdev):
  1571. """WPA2-PSK supplicant protocol testing: success"""
  1572. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1573. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1574. msg = recv_eapol(hapd)
  1575. dev[0].dump_monitor()
  1576. # Build own EAPOL-Key msg 1/4
  1577. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1578. counter = 1
  1579. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1580. counter += 1
  1581. send_eapol(dev[0], bssid, build_eapol(msg))
  1582. msg = recv_eapol(dev[0])
  1583. snonce = msg['rsn_key_nonce']
  1584. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1585. logger.debug("Valid EAPOL-Key msg 3/4")
  1586. dev[0].dump_monitor()
  1587. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010100dc11188831bf4aa4a8678d2b41498618')
  1588. wrapped = aes_wrap(kek, pad_key_data(plain))
  1589. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1590. counter += 1
  1591. send_eapol(dev[0], bssid, build_eapol(msg))
  1592. dev[0].wait_connected(timeout=1)
  1593. def test_ap_wpa2_psk_supp_proto_no_gtk(dev, apdev):
  1594. """WPA2-PSK supplicant protocol testing: no GTK"""
  1595. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1596. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1597. msg = recv_eapol(hapd)
  1598. dev[0].dump_monitor()
  1599. # Build own EAPOL-Key msg 1/4
  1600. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1601. counter = 1
  1602. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1603. counter += 1
  1604. send_eapol(dev[0], bssid, build_eapol(msg))
  1605. msg = recv_eapol(dev[0])
  1606. snonce = msg['rsn_key_nonce']
  1607. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1608. logger.debug("EAPOL-Key msg 3/4 without GTK KDE")
  1609. dev[0].dump_monitor()
  1610. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00')
  1611. wrapped = aes_wrap(kek, pad_key_data(plain))
  1612. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1613. counter += 1
  1614. send_eapol(dev[0], bssid, build_eapol(msg))
  1615. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=0.1)
  1616. if ev is not None:
  1617. raise Exception("Unexpected connection completion reported")
  1618. def test_ap_wpa2_psk_supp_proto_anonce_change(dev, apdev):
  1619. """WPA2-PSK supplicant protocol testing: ANonce change"""
  1620. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1621. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1622. msg = recv_eapol(hapd)
  1623. dev[0].dump_monitor()
  1624. # Build own EAPOL-Key msg 1/4
  1625. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1626. counter = 1
  1627. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1628. counter += 1
  1629. send_eapol(dev[0], bssid, build_eapol(msg))
  1630. msg = recv_eapol(dev[0])
  1631. snonce = msg['rsn_key_nonce']
  1632. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1633. logger.debug("Valid EAPOL-Key msg 3/4")
  1634. dev[0].dump_monitor()
  1635. anonce2 = binascii.unhexlify('3333333333333333333333333333333333333333333333333333333333333333')
  1636. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010100dc11188831bf4aa4a8678d2b41498618')
  1637. wrapped = aes_wrap(kek, pad_key_data(plain))
  1638. msg = build_eapol_key_3_4(anonce2, kck, wrapped, replay_counter=counter)
  1639. counter += 1
  1640. send_eapol(dev[0], bssid, build_eapol(msg))
  1641. ev = dev[0].wait_event(["WPA: ANonce from message 1 of 4-Way Handshake differs from 3 of 4-Way Handshake"])
  1642. if ev is None:
  1643. raise Exception("ANonce change not reported")
  1644. def test_ap_wpa2_psk_supp_proto_unexpected_group_msg(dev, apdev):
  1645. """WPA2-PSK supplicant protocol testing: unexpected group message"""
  1646. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1647. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1648. msg = recv_eapol(hapd)
  1649. dev[0].dump_monitor()
  1650. # Build own EAPOL-Key msg 1/4
  1651. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1652. counter = 1
  1653. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1654. counter += 1
  1655. send_eapol(dev[0], bssid, build_eapol(msg))
  1656. msg = recv_eapol(dev[0])
  1657. snonce = msg['rsn_key_nonce']
  1658. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1659. logger.debug("Group key 1/2 instead of msg 3/4")
  1660. dev[0].dump_monitor()
  1661. wrapped = aes_wrap(kek, binascii.unhexlify('dd16000fac010100dc11188831bf4aa4a8678d2b41498618'))
  1662. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter,
  1663. key_info=0x13c2)
  1664. counter += 1
  1665. send_eapol(dev[0], bssid, build_eapol(msg))
  1666. ev = dev[0].wait_event(["WPA: Group Key Handshake started prior to completion of 4-way handshake"])
  1667. if ev is None:
  1668. raise Exception("Unexpected group key message not reported")
  1669. dev[0].wait_disconnected(timeout=1)
  1670. @remote_compatible
  1671. def test_ap_wpa2_psk_supp_proto_msg_1_invalid_kde(dev, apdev):
  1672. """WPA2-PSK supplicant protocol testing: invalid KDE in msg 1/4"""
  1673. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1674. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1675. msg = recv_eapol(hapd)
  1676. dev[0].dump_monitor()
  1677. # Build own EAPOL-Key msg 1/4 with invalid KDE
  1678. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1679. counter = 1
  1680. msg = build_eapol_key_1_4(anonce, replay_counter=counter,
  1681. key_data=binascii.unhexlify('5555'))
  1682. counter += 1
  1683. send_eapol(dev[0], bssid, build_eapol(msg))
  1684. dev[0].wait_disconnected(timeout=1)
  1685. def test_ap_wpa2_psk_supp_proto_wrong_pairwise_key_len(dev, apdev):
  1686. """WPA2-PSK supplicant protocol testing: wrong pairwise key length"""
  1687. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1688. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1689. msg = recv_eapol(hapd)
  1690. dev[0].dump_monitor()
  1691. # Build own EAPOL-Key msg 1/4
  1692. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1693. counter = 1
  1694. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1695. counter += 1
  1696. send_eapol(dev[0], bssid, build_eapol(msg))
  1697. msg = recv_eapol(dev[0])
  1698. snonce = msg['rsn_key_nonce']
  1699. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1700. logger.debug("Valid EAPOL-Key msg 3/4")
  1701. dev[0].dump_monitor()
  1702. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010100dc11188831bf4aa4a8678d2b41498618')
  1703. wrapped = aes_wrap(kek, pad_key_data(plain))
  1704. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter,
  1705. key_len=15)
  1706. counter += 1
  1707. send_eapol(dev[0], bssid, build_eapol(msg))
  1708. ev = dev[0].wait_event(["WPA: Invalid CCMP key length 15"])
  1709. if ev is None:
  1710. raise Exception("Invalid CCMP key length not reported")
  1711. dev[0].wait_disconnected(timeout=1)
  1712. def test_ap_wpa2_psk_supp_proto_wrong_group_key_len(dev, apdev):
  1713. """WPA2-PSK supplicant protocol testing: wrong group key length"""
  1714. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1715. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1716. msg = recv_eapol(hapd)
  1717. dev[0].dump_monitor()
  1718. # Build own EAPOL-Key msg 1/4
  1719. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1720. counter = 1
  1721. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1722. counter += 1
  1723. send_eapol(dev[0], bssid, build_eapol(msg))
  1724. msg = recv_eapol(dev[0])
  1725. snonce = msg['rsn_key_nonce']
  1726. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1727. logger.debug("Valid EAPOL-Key msg 3/4")
  1728. dev[0].dump_monitor()
  1729. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd15000fac010100dc11188831bf4aa4a8678d2b414986')
  1730. wrapped = aes_wrap(kek, pad_key_data(plain))
  1731. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1732. counter += 1
  1733. send_eapol(dev[0], bssid, build_eapol(msg))
  1734. ev = dev[0].wait_event(["WPA: Unsupported CCMP Group Cipher key length 15"])
  1735. if ev is None:
  1736. raise Exception("Invalid CCMP key length not reported")
  1737. dev[0].wait_disconnected(timeout=1)
  1738. def test_ap_wpa2_psk_supp_proto_gtk_tx_bit_workaround(dev, apdev):
  1739. """WPA2-PSK supplicant protocol testing: GTK TX bit workaround"""
  1740. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1741. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1742. msg = recv_eapol(hapd)
  1743. dev[0].dump_monitor()
  1744. # Build own EAPOL-Key msg 1/4
  1745. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1746. counter = 1
  1747. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1748. counter += 1
  1749. send_eapol(dev[0], bssid, build_eapol(msg))
  1750. msg = recv_eapol(dev[0])
  1751. snonce = msg['rsn_key_nonce']
  1752. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1753. logger.debug("Valid EAPOL-Key msg 3/4")
  1754. dev[0].dump_monitor()
  1755. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010500dc11188831bf4aa4a8678d2b41498618')
  1756. wrapped = aes_wrap(kek, pad_key_data(plain))
  1757. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1758. counter += 1
  1759. send_eapol(dev[0], bssid, build_eapol(msg))
  1760. ev = dev[0].wait_event(["WPA: Tx bit set for GTK, but pairwise keys are used - ignore Tx bit"])
  1761. if ev is None:
  1762. raise Exception("GTK Tx bit workaround not reported")
  1763. dev[0].wait_connected(timeout=1)
  1764. def test_ap_wpa2_psk_supp_proto_gtk_keyidx_0_and_3(dev, apdev):
  1765. """WPA2-PSK supplicant protocol testing: GTK key index 0 and 3"""
  1766. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1767. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1768. msg = recv_eapol(hapd)
  1769. dev[0].dump_monitor()
  1770. # Build own EAPOL-Key msg 1/4
  1771. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1772. counter = 1
  1773. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1774. counter += 1
  1775. send_eapol(dev[0], bssid, build_eapol(msg))
  1776. msg = recv_eapol(dev[0])
  1777. snonce = msg['rsn_key_nonce']
  1778. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1779. logger.debug("Valid EAPOL-Key msg 3/4 (GTK keyidx 0)")
  1780. dev[0].dump_monitor()
  1781. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010000dc11188831bf4aa4a8678d2b41498618')
  1782. wrapped = aes_wrap(kek, pad_key_data(plain))
  1783. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1784. counter += 1
  1785. send_eapol(dev[0], bssid, build_eapol(msg))
  1786. dev[0].wait_connected(timeout=1)
  1787. logger.debug("Valid EAPOL-Key group msg 1/2 (GTK keyidx 3)")
  1788. dev[0].dump_monitor()
  1789. plain = binascii.unhexlify('dd16000fac010300dc11188831bf4aa4a8678d2b41498618')
  1790. wrapped = aes_wrap(kek, pad_key_data(plain))
  1791. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter,
  1792. key_info=0x13c2)
  1793. counter += 1
  1794. send_eapol(dev[0], bssid, build_eapol(msg))
  1795. msg = recv_eapol(dev[0])
  1796. ev = dev[0].wait_event(["WPA: Group rekeying completed"])
  1797. if ev is None:
  1798. raise Exception("GTK rekeing not reported")
  1799. logger.debug("Unencrypted GTK KDE in group msg 1/2")
  1800. dev[0].dump_monitor()
  1801. plain = binascii.unhexlify('dd16000fac010300dc11188831bf4aa4a8678d2b41498618')
  1802. msg = build_eapol_key_3_4(anonce, kck, plain, replay_counter=counter,
  1803. key_info=0x03c2)
  1804. counter += 1
  1805. send_eapol(dev[0], bssid, build_eapol(msg))
  1806. ev = dev[0].wait_event(["WPA: GTK IE in unencrypted key data"])
  1807. if ev is None:
  1808. raise Exception("Unencrypted GTK KDE not reported")
  1809. dev[0].wait_disconnected(timeout=1)
  1810. def test_ap_wpa2_psk_supp_proto_no_gtk_in_group_msg(dev, apdev):
  1811. """WPA2-PSK supplicant protocol testing: GTK KDE missing from group msg"""
  1812. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1813. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1814. msg = recv_eapol(hapd)
  1815. dev[0].dump_monitor()
  1816. # Build own EAPOL-Key msg 1/4
  1817. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1818. counter = 1
  1819. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1820. counter += 1
  1821. send_eapol(dev[0], bssid, build_eapol(msg))
  1822. msg = recv_eapol(dev[0])
  1823. snonce = msg['rsn_key_nonce']
  1824. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1825. logger.debug("Valid EAPOL-Key msg 3/4 (GTK keyidx 0)")
  1826. dev[0].dump_monitor()
  1827. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010000dc11188831bf4aa4a8678d2b41498618')
  1828. wrapped = aes_wrap(kek, pad_key_data(plain))
  1829. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1830. counter += 1
  1831. send_eapol(dev[0], bssid, build_eapol(msg))
  1832. dev[0].wait_connected(timeout=1)
  1833. logger.debug("No GTK KDE in EAPOL-Key group msg 1/2")
  1834. dev[0].dump_monitor()
  1835. plain = binascii.unhexlify('dd00dd00dd00dd00dd00dd00dd00dd00')
  1836. wrapped = aes_wrap(kek, pad_key_data(plain))
  1837. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter,
  1838. key_info=0x13c2)
  1839. counter += 1
  1840. send_eapol(dev[0], bssid, build_eapol(msg))
  1841. ev = dev[0].wait_event(["WPA: No GTK IE in Group Key msg 1/2"])
  1842. if ev is None:
  1843. raise Exception("Missing GTK KDE not reported")
  1844. dev[0].wait_disconnected(timeout=1)
  1845. def test_ap_wpa2_psk_supp_proto_too_long_gtk_in_group_msg(dev, apdev):
  1846. """WPA2-PSK supplicant protocol testing: too long GTK KDE in group msg"""
  1847. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1848. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1849. msg = recv_eapol(hapd)
  1850. dev[0].dump_monitor()
  1851. # Build own EAPOL-Key msg 1/4
  1852. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1853. counter = 1
  1854. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1855. counter += 1
  1856. send_eapol(dev[0], bssid, build_eapol(msg))
  1857. msg = recv_eapol(dev[0])
  1858. snonce = msg['rsn_key_nonce']
  1859. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1860. logger.debug("Valid EAPOL-Key msg 3/4 (GTK keyidx 0)")
  1861. dev[0].dump_monitor()
  1862. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010000dc11188831bf4aa4a8678d2b41498618')
  1863. wrapped = aes_wrap(kek, pad_key_data(plain))
  1864. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1865. counter += 1
  1866. send_eapol(dev[0], bssid, build_eapol(msg))
  1867. dev[0].wait_connected(timeout=1)
  1868. logger.debug("EAPOL-Key group msg 1/2 with too long GTK KDE")
  1869. dev[0].dump_monitor()
  1870. plain = binascii.unhexlify('dd27000fac010100ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff')
  1871. wrapped = aes_wrap(kek, pad_key_data(plain))
  1872. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter,
  1873. key_info=0x13c2)
  1874. counter += 1
  1875. send_eapol(dev[0], bssid, build_eapol(msg))
  1876. ev = dev[0].wait_event(["WPA: Unsupported CCMP Group Cipher key length 33"])
  1877. if ev is None:
  1878. raise Exception("Too long GTK KDE not reported")
  1879. dev[0].wait_disconnected(timeout=1)
  1880. def test_ap_wpa2_psk_supp_proto_too_long_gtk_kde(dev, apdev):
  1881. """WPA2-PSK supplicant protocol testing: too long GTK KDE"""
  1882. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1883. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1884. msg = recv_eapol(hapd)
  1885. dev[0].dump_monitor()
  1886. # Build own EAPOL-Key msg 1/4
  1887. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1888. counter = 1
  1889. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1890. counter += 1
  1891. send_eapol(dev[0], bssid, build_eapol(msg))
  1892. msg = recv_eapol(dev[0])
  1893. snonce = msg['rsn_key_nonce']
  1894. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1895. logger.debug("EAPOL-Key msg 3/4 with too short GTK KDE")
  1896. dev[0].dump_monitor()
  1897. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd27000fac010100ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff')
  1898. wrapped = aes_wrap(kek, pad_key_data(plain))
  1899. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1900. counter += 1
  1901. send_eapol(dev[0], bssid, build_eapol(msg))
  1902. dev[0].wait_disconnected(timeout=1)
  1903. def test_ap_wpa2_psk_supp_proto_gtk_not_encrypted(dev, apdev):
  1904. """WPA2-PSK supplicant protocol testing: GTK KDE not encrypted"""
  1905. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1906. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1907. msg = recv_eapol(hapd)
  1908. dev[0].dump_monitor()
  1909. # Build own EAPOL-Key msg 1/4
  1910. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1911. counter = 1
  1912. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1913. counter += 1
  1914. send_eapol(dev[0], bssid, build_eapol(msg))
  1915. msg = recv_eapol(dev[0])
  1916. snonce = msg['rsn_key_nonce']
  1917. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1918. logger.debug("Valid EAPOL-Key msg 3/4")
  1919. dev[0].dump_monitor()
  1920. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010100dc11188831bf4aa4a8678d2b41498618')
  1921. msg = build_eapol_key_3_4(anonce, kck, plain, replay_counter=counter,
  1922. key_info=0x03ca)
  1923. counter += 1
  1924. send_eapol(dev[0], bssid, build_eapol(msg))
  1925. ev = dev[0].wait_event(["WPA: GTK IE in unencrypted key data"])
  1926. if ev is None:
  1927. raise Exception("Unencrypted GTK KDE not reported")
  1928. dev[0].wait_disconnected(timeout=1)
  1929. def find_wpas_process(dev):
  1930. ifname = dev.ifname
  1931. err, data = dev.cmd_execute(['ps', 'ax'])
  1932. for l in data.splitlines():
  1933. if "wpa_supplicant" not in l:
  1934. continue
  1935. if "-i" + ifname not in l:
  1936. continue
  1937. return int(l.strip().split(' ')[0])
  1938. raise Exception("Could not find wpa_supplicant process")
  1939. def read_process_memory(pid, key=None):
  1940. buf = bytes()
  1941. logger.info("Reading process memory (pid=%d)" % pid)
  1942. with open('/proc/%d/maps' % pid, 'r') as maps, \
  1943. open('/proc/%d/mem' % pid, 'r') as mem:
  1944. for l in maps.readlines():
  1945. m = re.match(r'([0-9a-f]+)-([0-9a-f]+) ([-r][-w][-x][-p])', l)
  1946. if not m:
  1947. continue
  1948. start = int(m.group(1), 16)
  1949. end = int(m.group(2), 16)
  1950. perm = m.group(3)
  1951. if start > 0xffffffffffff:
  1952. continue
  1953. if end < start:
  1954. continue
  1955. if not perm.startswith('rw'):
  1956. continue
  1957. for name in [ "[heap]", "[stack]" ]:
  1958. if name in l:
  1959. logger.info("%s 0x%x-0x%x is at %d-%d" % (name, start, end, len(buf), len(buf) + (end - start)))
  1960. mem.seek(start)
  1961. data = mem.read(end - start)
  1962. buf += data
  1963. if key and key in data:
  1964. logger.info("Key found in " + l)
  1965. logger.info("Total process memory read: %d bytes" % len(buf))
  1966. return buf
  1967. def verify_not_present(buf, key, fname, keyname):
  1968. pos = buf.find(key)
  1969. if pos < 0:
  1970. return
  1971. prefix = 2048 if pos > 2048 else pos
  1972. with open(fname + keyname, 'w') as f:
  1973. f.write(buf[pos - prefix:pos + 2048])
  1974. raise Exception(keyname + " found after disassociation")
  1975. def get_key_locations(buf, key, keyname):
  1976. count = 0
  1977. pos = 0
  1978. while True:
  1979. pos = buf.find(key, pos)
  1980. if pos < 0:
  1981. break
  1982. logger.info("Found %s at %d" % (keyname, pos))
  1983. context = 128
  1984. start = pos - context if pos > context else 0
  1985. before = binascii.hexlify(buf[start:pos])
  1986. context += len(key)
  1987. end = pos + context if pos < len(buf) - context else len(buf) - context
  1988. after = binascii.hexlify(buf[pos + len(key):end])
  1989. logger.debug("Memory context %d-%d: %s|%s|%s" % (start, end, before, binascii.hexlify(key), after))
  1990. count += 1
  1991. pos += len(key)
  1992. return count
  1993. def test_wpa2_psk_key_lifetime_in_memory(dev, apdev, params):
  1994. """WPA2-PSK and PSK/PTK lifetime in memory"""
  1995. ssid = "test-wpa2-psk"
  1996. passphrase = 'qwertyuiop'
  1997. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  1998. pmk = binascii.unhexlify(psk)
  1999. p = hostapd.wpa2_params(ssid=ssid)
  2000. p['wpa_psk'] = psk
  2001. hapd = hostapd.add_ap(apdev[0], p)
  2002. pid = find_wpas_process(dev[0])
  2003. id = dev[0].connect(ssid, raw_psk=psk, scan_freq="2412",
  2004. only_add_network=True)
  2005. logger.info("Checking keys in memory after network profile configuration")
  2006. buf = read_process_memory(pid, pmk)
  2007. get_key_locations(buf, pmk, "PMK")
  2008. dev[0].request("REMOVE_NETWORK all")
  2009. logger.info("Checking keys in memory after network profile removal")
  2010. buf = read_process_memory(pid, pmk)
  2011. get_key_locations(buf, pmk, "PMK")
  2012. id = dev[0].connect(ssid, psk=passphrase, scan_freq="2412",
  2013. only_add_network=True)
  2014. logger.info("Checking keys in memory before connection")
  2015. buf = read_process_memory(pid, pmk)
  2016. get_key_locations(buf, pmk, "PMK")
  2017. dev[0].connect_network(id, timeout=20)
  2018. # The decrypted copy of GTK is freed only after the CTRL-EVENT-CONNECTED
  2019. # event has been delivered, so verify that wpa_supplicant has returned to
  2020. # eloop before reading process memory.
  2021. time.sleep(1)
  2022. dev[0].ping()
  2023. buf = read_process_memory(pid, pmk)
  2024. dev[0].request("DISCONNECT")
  2025. dev[0].wait_disconnected()
  2026. dev[0].relog()
  2027. ptk = None
  2028. gtk = None
  2029. with open(os.path.join(params['logdir'], 'log0'), 'r') as f:
  2030. for l in f.readlines():
  2031. if "WPA: PTK - hexdump" in l:
  2032. val = l.strip().split(':')[3].replace(' ', '')
  2033. ptk = binascii.unhexlify(val)
  2034. if "WPA: Group Key - hexdump" in l:
  2035. val = l.strip().split(':')[3].replace(' ', '')
  2036. gtk = binascii.unhexlify(val)
  2037. if not pmk or not ptk or not gtk:
  2038. raise Exception("Could not find keys from debug log")
  2039. if len(gtk) != 16:
  2040. raise Exception("Unexpected GTK length")
  2041. kck = ptk[0:16]
  2042. kek = ptk[16:32]
  2043. tk = ptk[32:48]
  2044. logger.info("Checking keys in memory while associated")
  2045. get_key_locations(buf, pmk, "PMK")
  2046. if pmk not in buf:
  2047. raise HwsimSkip("PMK not found while associated")
  2048. if kck not in buf:
  2049. raise Exception("KCK not found while associated")
  2050. if kek not in buf:
  2051. raise Exception("KEK not found while associated")
  2052. #if tk in buf:
  2053. # raise Exception("TK found from memory")
  2054. logger.info("Checking keys in memory after disassociation")
  2055. buf = read_process_memory(pid, pmk)
  2056. get_key_locations(buf, pmk, "PMK")
  2057. # Note: PMK/PSK is still present in network configuration
  2058. fname = os.path.join(params['logdir'],
  2059. 'wpa2_psk_key_lifetime_in_memory.memctx-')
  2060. verify_not_present(buf, kck, fname, "KCK")
  2061. verify_not_present(buf, kek, fname, "KEK")
  2062. verify_not_present(buf, tk, fname, "TK")
  2063. if gtk in buf:
  2064. get_key_locations(buf, gtk, "GTK")
  2065. verify_not_present(buf, gtk, fname, "GTK")
  2066. dev[0].request("REMOVE_NETWORK all")
  2067. logger.info("Checking keys in memory after network profile removal")
  2068. buf = read_process_memory(pid, pmk)
  2069. get_key_locations(buf, pmk, "PMK")
  2070. verify_not_present(buf, pmk, fname, "PMK")
  2071. verify_not_present(buf, kck, fname, "KCK")
  2072. verify_not_present(buf, kek, fname, "KEK")
  2073. verify_not_present(buf, tk, fname, "TK")
  2074. verify_not_present(buf, gtk, fname, "GTK")
  2075. @remote_compatible
  2076. def test_ap_wpa2_psk_wep(dev, apdev):
  2077. """WPA2-PSK AP and WEP enabled"""
  2078. ssid = "test-wpa2-psk"
  2079. passphrase = 'qwertyuiop'
  2080. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  2081. hapd = hostapd.add_ap(apdev[0], params)
  2082. try:
  2083. hapd.set('wep_key0', '"hello"')
  2084. raise Exception("WEP key accepted to WPA2 network")
  2085. except Exception:
  2086. pass
  2087. def test_ap_wpa2_psk_wpas_in_bridge(dev, apdev):
  2088. """WPA2-PSK AP and wpas interface in a bridge"""
  2089. br_ifname='sta-br0'
  2090. ifname='wlan5'
  2091. try:
  2092. _test_ap_wpa2_psk_wpas_in_bridge(dev, apdev)
  2093. finally:
  2094. subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'down'])
  2095. subprocess.call(['brctl', 'delif', br_ifname, ifname])
  2096. subprocess.call(['brctl', 'delbr', br_ifname])
  2097. subprocess.call(['iw', ifname, 'set', '4addr', 'off'])
  2098. def _test_ap_wpa2_psk_wpas_in_bridge(dev, apdev):
  2099. ssid = "test-wpa2-psk"
  2100. passphrase = 'qwertyuiop'
  2101. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  2102. hapd = hostapd.add_ap(apdev[0], params)
  2103. br_ifname='sta-br0'
  2104. ifname='wlan5'
  2105. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  2106. subprocess.call(['brctl', 'addbr', br_ifname])
  2107. subprocess.call(['brctl', 'setfd', br_ifname, '0'])
  2108. subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'up'])
  2109. subprocess.call(['iw', ifname, 'set', '4addr', 'on'])
  2110. subprocess.check_call(['brctl', 'addif', br_ifname, ifname])
  2111. wpas.interface_add(ifname, br_ifname=br_ifname)
  2112. wpas.dump_monitor()
  2113. wpas.connect(ssid, psk=passphrase, scan_freq="2412")
  2114. wpas.dump_monitor()
  2115. @remote_compatible
  2116. def test_ap_wpa2_psk_ifdown(dev, apdev):
  2117. """AP with open mode and external ifconfig down"""
  2118. ssid = "test-wpa2-psk"
  2119. passphrase = 'qwertyuiop'
  2120. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  2121. hapd = hostapd.add_ap(apdev[0], params)
  2122. bssid = apdev[0]['bssid']
  2123. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  2124. hapd.cmd_execute(['ip', 'link', 'set', 'dev', apdev[0]['ifname'], 'down'])
  2125. ev = hapd.wait_event(["INTERFACE-DISABLED"], timeout=10)
  2126. if ev is None:
  2127. raise Exception("No INTERFACE-DISABLED event")
  2128. # this wait tests beacon loss detection in mac80211
  2129. dev[0].wait_disconnected()
  2130. hapd.cmd_execute(['ip', 'link', 'set', 'dev', apdev[0]['ifname'], 'up'])
  2131. ev = hapd.wait_event(["INTERFACE-ENABLED"], timeout=10)
  2132. if ev is None:
  2133. raise Exception("No INTERFACE-ENABLED event")
  2134. dev[0].wait_connected()
  2135. hwsim_utils.test_connectivity(dev[0], hapd)
  2136. def test_ap_wpa2_psk_drop_first_msg_4(dev, apdev):
  2137. """WPA2-PSK and first EAPOL-Key msg 4/4 dropped"""
  2138. bssid = apdev[0]['bssid']
  2139. ssid = "test-wpa2-psk"
  2140. passphrase = 'qwertyuiop'
  2141. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  2142. params = hostapd.wpa2_params(ssid=ssid)
  2143. params['wpa_psk'] = psk
  2144. hapd = hostapd.add_ap(apdev[0], params)
  2145. hapd.request("SET ext_eapol_frame_io 1")
  2146. dev[0].request("SET ext_eapol_frame_io 1")
  2147. dev[0].connect(ssid, psk=passphrase, scan_freq="2412", wait_connect=False)
  2148. addr = dev[0].own_addr()
  2149. # EAPOL-Key msg 1/4
  2150. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  2151. if ev is None:
  2152. raise Exception("Timeout on EAPOL-TX from hostapd")
  2153. res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2])
  2154. if "OK" not in res:
  2155. raise Exception("EAPOL_RX to wpa_supplicant failed")
  2156. # EAPOL-Key msg 2/4
  2157. ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
  2158. if ev is None:
  2159. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  2160. res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2])
  2161. if "OK" not in res:
  2162. raise Exception("EAPOL_RX to hostapd failed")
  2163. # EAPOL-Key msg 3/4
  2164. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  2165. if ev is None:
  2166. raise Exception("Timeout on EAPOL-TX from hostapd")
  2167. res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2])
  2168. if "OK" not in res:
  2169. raise Exception("EAPOL_RX to wpa_supplicant failed")
  2170. # EAPOL-Key msg 4/4
  2171. ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
  2172. if ev is None:
  2173. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  2174. logger.info("Drop the first EAPOL-Key msg 4/4")
  2175. # wpa_supplicant believes now that 4-way handshake succeeded; hostapd
  2176. # doesn't. Use normal EAPOL TX/RX to handle retries.
  2177. hapd.request("SET ext_eapol_frame_io 0")
  2178. dev[0].request("SET ext_eapol_frame_io 0")
  2179. dev[0].wait_connected()
  2180. ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=15)
  2181. if ev is None:
  2182. raise Exception("Timeout on AP-STA-CONNECTED from hostapd")
  2183. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=0.1)
  2184. if ev is not None:
  2185. logger.info("Disconnection detected")
  2186. # The EAPOL-Key retries are supposed to allow the connection to be
  2187. # established without having to reassociate. However, this does not
  2188. # currently work since mac80211 ends up encrypting EAPOL-Key msg 4/4
  2189. # after the pairwise key has been configured and AP will drop those and
  2190. # disconnect the station after reaching retransmission limit. Connection
  2191. # is then established after reassociation. Once that behavior has been
  2192. # optimized to prevent EAPOL-Key frame encryption for retransmission
  2193. # case, this exception can be uncommented here.
  2194. #raise Exception("Unexpected disconnection")
  2195. @remote_compatible
  2196. def test_ap_wpa2_psk_disable_enable(dev, apdev):
  2197. """WPA2-PSK AP getting disabled and re-enabled"""
  2198. ssid = "test-wpa2-psk"
  2199. passphrase = 'qwertyuiop'
  2200. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  2201. params = hostapd.wpa2_params(ssid=ssid)
  2202. params['wpa_psk'] = psk
  2203. hapd = hostapd.add_ap(apdev[0], params)
  2204. dev[0].connect(ssid, raw_psk=psk, scan_freq="2412")
  2205. for i in range(2):
  2206. hapd.request("DISABLE")
  2207. dev[0].wait_disconnected()
  2208. hapd.request("ENABLE")
  2209. dev[0].wait_connected()
  2210. hwsim_utils.test_connectivity(dev[0], hapd)
  2211. @remote_compatible
  2212. def test_ap_wpa2_psk_incorrect_passphrase(dev, apdev):
  2213. """WPA2-PSK AP and station using incorrect passphrase"""
  2214. ssid = "test-wpa2-psk"
  2215. passphrase = 'qwertyuiop'
  2216. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  2217. hapd = hostapd.add_ap(apdev[0], params)
  2218. dev[0].connect(ssid, psk="incorrect passphrase", scan_freq="2412",
  2219. wait_connect=False)
  2220. ev = hapd.wait_event(["AP-STA-POSSIBLE-PSK-MISMATCH"], timeout=10)
  2221. if ev is None:
  2222. raise Exception("No AP-STA-POSSIBLE-PSK-MISMATCH reported")
  2223. dev[0].dump_monitor()
  2224. hapd.disable()
  2225. hapd.set("wpa_passphrase", "incorrect passphrase")
  2226. hapd.enable()
  2227. dev[0].wait_connected(timeout=20)
  2228. @remote_compatible
  2229. def test_ap_wpa_ie_parsing(dev, apdev):
  2230. """WPA IE parsing"""
  2231. skip_with_fips(dev[0])
  2232. ssid = "test-wpa-psk"
  2233. passphrase = 'qwertyuiop'
  2234. params = hostapd.wpa_params(ssid=ssid, passphrase=passphrase)
  2235. hapd = hostapd.add_ap(apdev[0], params)
  2236. id = dev[0].connect(ssid, psk=passphrase, scan_freq="2412",
  2237. only_add_network=True)
  2238. tests = [ "dd040050f201",
  2239. "dd050050f20101",
  2240. "dd060050f2010100",
  2241. "dd060050f2010001",
  2242. "dd070050f201010000",
  2243. "dd080050f20101000050",
  2244. "dd090050f20101000050f2",
  2245. "dd0a0050f20101000050f202",
  2246. "dd0b0050f20101000050f20201",
  2247. "dd0c0050f20101000050f2020100",
  2248. "dd0c0050f20101000050f2020000",
  2249. "dd0c0050f20101000050f202ffff",
  2250. "dd0d0050f20101000050f202010000",
  2251. "dd0e0050f20101000050f20201000050",
  2252. "dd0f0050f20101000050f20201000050f2",
  2253. "dd100050f20101000050f20201000050f202",
  2254. "dd110050f20101000050f20201000050f20201",
  2255. "dd120050f20101000050f20201000050f2020100",
  2256. "dd120050f20101000050f20201000050f2020000",
  2257. "dd120050f20101000050f20201000050f202ffff",
  2258. "dd130050f20101000050f20201000050f202010000",
  2259. "dd140050f20101000050f20201000050f20201000050",
  2260. "dd150050f20101000050f20201000050f20201000050f2" ]
  2261. for t in tests:
  2262. try:
  2263. if "OK" not in dev[0].request("VENDOR_ELEM_ADD 13 " + t):
  2264. raise Exception("VENDOR_ELEM_ADD failed")
  2265. dev[0].select_network(id)
  2266. ev = dev[0].wait_event(["CTRL-EVENT-ASSOC-REJECT"], timeout=10)
  2267. if ev is None:
  2268. raise Exception("Association rejection not reported")
  2269. dev[0].request("DISCONNECT")
  2270. dev[0].dump_monitor()
  2271. finally:
  2272. dev[0].request("VENDOR_ELEM_REMOVE 13 *")
  2273. tests = [ "dd170050f20101000050f20201000050f20201000050f202ff",
  2274. "dd180050f20101000050f20201000050f20201000050f202ffff",
  2275. "dd190050f20101000050f20201000050f20201000050f202ffffff" ]
  2276. for t in tests:
  2277. try:
  2278. if "OK" not in dev[0].request("VENDOR_ELEM_ADD 13 " + t):
  2279. raise Exception("VENDOR_ELEM_ADD failed")
  2280. dev[0].select_network(id)
  2281. dev[0].wait_connected()
  2282. dev[0].request("DISCONNECT")
  2283. dev[0].dump_monitor()
  2284. finally:
  2285. dev[0].request("VENDOR_ELEM_REMOVE 13 *")
  2286. @remote_compatible
  2287. def test_ap_wpa2_psk_no_random(dev, apdev):
  2288. """WPA2-PSK AP and no random numbers available"""
  2289. ssid = "test-wpa2-psk"
  2290. passphrase = 'qwertyuiop'
  2291. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  2292. params = hostapd.wpa2_params(ssid=ssid)
  2293. params['wpa_psk'] = psk
  2294. hapd = hostapd.add_ap(apdev[0], params)
  2295. with fail_test(hapd, 1, "wpa_gmk_to_gtk"):
  2296. id = dev[0].connect(ssid, raw_psk=psk, scan_freq="2412",
  2297. wait_connect=False)
  2298. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=15)
  2299. if ev is None:
  2300. raise Exception("Disconnection event not reported")
  2301. dev[0].request("DISCONNECT")
  2302. dev[0].select_network(id, freq=2412)
  2303. dev[0].wait_connected()
  2304. @remote_compatible
  2305. def test_rsn_ie_proto_psk_sta(dev, apdev):
  2306. """RSN element protocol testing for PSK cases on STA side"""
  2307. bssid = apdev[0]['bssid']
  2308. ssid = "test-wpa2-psk"
  2309. passphrase = 'qwertyuiop'
  2310. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  2311. # This is the RSN element used normally by hostapd
  2312. params['own_ie_override'] = '30140100000fac040100000fac040100000fac020c00'
  2313. hapd = hostapd.add_ap(apdev[0], params)
  2314. if "FAIL" not in hapd.request("SET own_ie_override qwerty"):
  2315. raise Exception("Invalid own_ie_override value accepted")
  2316. id = dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  2317. tests = [ ('No RSN Capabilities field',
  2318. '30120100000fac040100000fac040100000fac02'),
  2319. ('Reserved RSN Capabilities bits set',
  2320. '30140100000fac040100000fac040100000fac023cff'),
  2321. ('Extra pairwise cipher suite (unsupported)',
  2322. '30180100000fac040200ffffffff000fac040100000fac020c00'),
  2323. ('Extra AKM suite (unsupported)',
  2324. '30180100000fac040100000fac040200ffffffff000fac020c00'),
  2325. ('PMKIDCount field included',
  2326. '30160100000fac040100000fac040100000fac020c000000'),
  2327. ('Unexpected Group Management Cipher Suite with PMF disabled',
  2328. '301a0100000fac040100000fac040100000fac020c000000000fac06'),
  2329. ('Extra octet after defined fields (future extensibility)',
  2330. '301b0100000fac040100000fac040100000fac020c000000000fac0600') ]
  2331. for txt,ie in tests:
  2332. dev[0].request("DISCONNECT")
  2333. dev[0].wait_disconnected()
  2334. logger.info(txt)
  2335. hapd.disable()
  2336. hapd.set('own_ie_override', ie)
  2337. hapd.enable()
  2338. dev[0].request("BSS_FLUSH 0")
  2339. dev[0].scan_for_bss(bssid, 2412, force_scan=True, only_new=True)
  2340. dev[0].select_network(id, freq=2412)
  2341. dev[0].wait_connected()
  2342. @remote_compatible
  2343. def test_ap_cli_order(dev, apdev):
  2344. ssid = "test-rsn-setup"
  2345. passphrase = 'zzzzzzzz'
  2346. hapd = hostapd.add_ap(apdev[0], {}, no_enable=True)
  2347. hapd.set('ssid', ssid)
  2348. hapd.set('wpa_passphrase', passphrase)
  2349. hapd.set('rsn_pairwise', 'CCMP')
  2350. hapd.set('wpa_key_mgmt', 'WPA-PSK')
  2351. hapd.set('wpa', '2')
  2352. hapd.enable()
  2353. cfg = hapd.get_config()
  2354. if cfg['group_cipher'] != 'CCMP':
  2355. raise Exception("Unexpected group_cipher: " + cfg['group_cipher'])
  2356. if cfg['rsn_pairwise_cipher'] != 'CCMP':
  2357. raise Exception("Unexpected rsn_pairwise_cipher: " + cfg['rsn_pairwise_cipher'])
  2358. ev = hapd.wait_event(["AP-ENABLED", "AP-DISABLED"], timeout=30)
  2359. if ev is None:
  2360. raise Exception("AP startup timed out")
  2361. if "AP-ENABLED" not in ev:
  2362. raise Exception("AP startup failed")
  2363. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  2364. def set_test_assoc_ie(dev, ie):
  2365. if "OK" not in dev.request("TEST_ASSOC_IE " + ie):
  2366. raise Exception("Could not set TEST_ASSOC_IE")
  2367. @remote_compatible
  2368. def test_ap_wpa2_psk_assoc_rsn(dev, apdev):
  2369. """WPA2-PSK AP and association request RSN IE differences"""
  2370. ssid = "test-wpa2-psk"
  2371. passphrase = 'qwertyuiop'
  2372. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  2373. hapd = hostapd.add_ap(apdev[0], params)
  2374. tests = [ ("Normal wpa_supplicant assoc req RSN IE",
  2375. "30140100000fac040100000fac040100000fac020000"),
  2376. ("RSN IE without RSN Capabilities",
  2377. "30120100000fac040100000fac040100000fac02") ]
  2378. for title, ie in tests:
  2379. logger.info(title)
  2380. set_test_assoc_ie(dev[0], ie)
  2381. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  2382. dev[0].request("REMOVE_NETWORK all")
  2383. dev[0].wait_disconnected()
  2384. tests = [ ("WPA IE instead of RSN IE and only RSN enabled on AP",
  2385. "dd160050f20101000050f20201000050f20201000050f202", 40),
  2386. ("Empty RSN IE", "3000", 40),
  2387. ("RSN IE with truncated Version", "300101", 40),
  2388. ("RSN IE with only Version", "30020100", 43) ]
  2389. for title, ie, status in tests:
  2390. logger.info(title)
  2391. set_test_assoc_ie(dev[0], ie)
  2392. dev[0].connect(ssid, psk=passphrase, scan_freq="2412",
  2393. wait_connect=False)
  2394. ev = dev[0].wait_event(["CTRL-EVENT-ASSOC-REJECT"])
  2395. if ev is None:
  2396. raise Exception("Association rejection not reported")
  2397. if "status_code=" + str(status) not in ev:
  2398. raise Exception("Unexpected status code: " + ev)
  2399. dev[0].request("REMOVE_NETWORK all")
  2400. dev[0].dump_monitor()
  2401. def test_ap_wpa_psk_rsn_pairwise(dev, apdev):
  2402. """WPA-PSK AP and only rsn_pairwise set"""
  2403. params = { "ssid": "wpapsk", "wpa": "1", "wpa_key_mgmt": "WPA-PSK",
  2404. "rsn_pairwise": "TKIP", "wpa_passphrase": "1234567890" }
  2405. hapd = hostapd.add_ap(apdev[0], params)
  2406. dev[0].connect("wpapsk", psk="1234567890", proto="WPA", pairwise="TKIP",
  2407. scan_freq="2412")
  2408. def test_ap_wpa2_eapol_retry_limit(dev, apdev):
  2409. """WPA2-PSK EAPOL-Key retry limit configuration"""
  2410. ssid = "test-wpa2-psk"
  2411. passphrase = 'qwertyuiop'
  2412. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  2413. params['wpa_ptk_rekey'] = '2'
  2414. params['wpa_group_update_count'] = '1'
  2415. params['wpa_pairwise_update_count'] = '1'
  2416. hapd = hostapd.add_ap(apdev[0], params)
  2417. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  2418. ev = dev[0].wait_event(["WPA: Key negotiation completed"])
  2419. if ev is None:
  2420. raise Exception("PTK rekey timed out")
  2421. if "FAIL" not in hapd.request("SET wpa_group_update_count 0"):
  2422. raise Exception("Invalid wpa_group_update_count value accepted")
  2423. if "FAIL" not in hapd.request("SET wpa_pairwise_update_count 0"):
  2424. raise Exception("Invalid wpa_pairwise_update_count value accepted")
  2425. def test_ap_wpa2_disable_eapol_retry(dev, apdev):
  2426. """WPA2-PSK disable EAPOL-Key retry"""
  2427. ssid = "test-wpa2-psk"
  2428. passphrase = 'qwertyuiop'
  2429. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  2430. params['wpa_disable_eapol_key_retries'] = '1'
  2431. hapd = hostapd.add_ap(apdev[0], params)
  2432. bssid = apdev[0]['bssid']
  2433. logger.info("Verify working 4-way handshake without retries")
  2434. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  2435. dev[0].request("REMOVE_NETWORK all")
  2436. dev[0].wait_disconnected()
  2437. dev[0].dump_monitor()
  2438. addr = dev[0].own_addr()
  2439. logger.info("Verify no retransmission of message 3/4")
  2440. hapd.request("SET ext_eapol_frame_io 1")
  2441. dev[0].request("SET ext_eapol_frame_io 1")
  2442. dev[0].connect(ssid, psk=passphrase, scan_freq="2412", wait_connect=False)
  2443. ev = hapd.wait_event(["EAPOL-TX"], timeout=5)
  2444. if ev is None:
  2445. raise Exception("Timeout on EAPOL-TX (M1) from hostapd")
  2446. ev = hapd.wait_event(["EAPOL-TX"], timeout=5)
  2447. if ev is None:
  2448. raise Exception("Timeout on EAPOL-TX (M1 retry) from hostapd")
  2449. res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2])
  2450. if "OK" not in res:
  2451. raise Exception("EAPOL_RX (M1) to wpa_supplicant failed")
  2452. ev = dev[0].wait_event(["EAPOL-TX"], timeout=5)
  2453. if ev is None:
  2454. raise Exception("Timeout on EAPOL-TX (M2) from wpa_supplicant")
  2455. dev[0].dump_monitor()
  2456. res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2])
  2457. if "OK" not in res:
  2458. raise Exception("EAPOL_RX (M2) to hostapd failed")
  2459. ev = hapd.wait_event(["EAPOL-TX"], timeout=5)
  2460. if ev is None:
  2461. raise Exception("Timeout on EAPOL-TX (M3) from hostapd")
  2462. ev = hapd.wait_event(["EAPOL-TX"], timeout=2)
  2463. if ev is not None:
  2464. raise Exception("Unexpected EAPOL-TX M3 retry from hostapd")
  2465. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=3)
  2466. if ev is None:
  2467. raise Exception("Disconnection not reported")
  2468. dev[0].request("REMOVE_NETWORK all")
  2469. dev[0].dump_monitor()
  2470. def test_ap_wpa2_disable_eapol_retry_group(dev, apdev):
  2471. """WPA2-PSK disable EAPOL-Key retry for group handshake"""
  2472. ssid = "test-wpa2-psk"
  2473. passphrase = 'qwertyuiop'
  2474. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  2475. params['wpa_disable_eapol_key_retries'] = '1'
  2476. params['wpa_strict_rekey'] = '1'
  2477. hapd = hostapd.add_ap(apdev[0], params)
  2478. bssid = apdev[0]['bssid']
  2479. id = dev[1].connect(ssid, psk=passphrase, scan_freq="2412")
  2480. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  2481. dev[0].dump_monitor()
  2482. addr = dev[0].own_addr()
  2483. dev[1].request("DISCONNECT")
  2484. ev = dev[0].wait_event(["WPA: Group rekeying completed"], timeout=2)
  2485. if ev is None:
  2486. raise Exception("GTK rekey timed out")
  2487. dev[1].request("RECONNECT")
  2488. dev[1].wait_connected()
  2489. dev[0].dump_monitor()
  2490. hapd.request("SET ext_eapol_frame_io 1")
  2491. dev[0].request("SET ext_eapol_frame_io 1")
  2492. dev[1].request("DISCONNECT")
  2493. ev = hapd.wait_event(["EAPOL-TX"], timeout=5)
  2494. if ev is None:
  2495. raise Exception("Timeout on EAPOL-TX (group M1) from hostapd")
  2496. ev = hapd.wait_event(["EAPOL-TX"], timeout=2)
  2497. if ev is not None:
  2498. raise Exception("Unexpected EAPOL-TX group M1 retry from hostapd")
  2499. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=3)
  2500. if ev is None:
  2501. raise Exception("Disconnection not reported")
  2502. dev[0].request("REMOVE_NETWORK all")
  2503. dev[0].dump_monitor()