test_ap_psk.py 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882
  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. import binascii
  7. from Crypto.Cipher import AES
  8. import hashlib
  9. import hmac
  10. import logging
  11. logger = logging.getLogger()
  12. import os
  13. import re
  14. import struct
  15. import subprocess
  16. import time
  17. import hostapd
  18. from utils import HwsimSkip
  19. import hwsim_utils
  20. from wpasupplicant import WpaSupplicant
  21. def check_mib(dev, vals):
  22. mib = dev.get_mib()
  23. for v in vals:
  24. if mib[v[0]] != v[1]:
  25. raise Exception("Unexpected {} = {} (expected {})".format(v[0], mib[v[0]], v[1]))
  26. def test_ap_wpa2_psk(dev, apdev):
  27. """WPA2-PSK AP with PSK instead of passphrase"""
  28. ssid = "test-wpa2-psk"
  29. passphrase = 'qwertyuiop'
  30. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  31. params = hostapd.wpa2_params(ssid=ssid)
  32. params['wpa_psk'] = psk
  33. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  34. key_mgmt = hapd.get_config()['key_mgmt']
  35. if key_mgmt.split(' ')[0] != "WPA-PSK":
  36. raise Exception("Unexpected GET_CONFIG(key_mgmt): " + key_mgmt)
  37. dev[0].connect(ssid, raw_psk=psk, scan_freq="2412")
  38. dev[1].connect(ssid, psk=passphrase, scan_freq="2412")
  39. sig = dev[0].request("SIGNAL_POLL").splitlines()
  40. pkt = dev[0].request("PKTCNT_POLL").splitlines()
  41. if "FREQUENCY=2412" not in sig:
  42. raise Exception("Unexpected SIGNAL_POLL value: " + str(sig))
  43. if "TXBAD=0" not in pkt:
  44. raise Exception("Unexpected TXBAD value: " + str(pkt))
  45. def test_ap_wpa2_psk_file(dev, apdev):
  46. """WPA2-PSK AP with PSK from a file"""
  47. ssid = "test-wpa2-psk"
  48. passphrase = 'qwertyuiop'
  49. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  50. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  51. params['wpa_psk_file'] = 'hostapd.wpa_psk'
  52. hostapd.add_ap(apdev[0]['ifname'], params)
  53. dev[1].connect(ssid, psk="very secret", scan_freq="2412", wait_connect=False)
  54. dev[2].connect(ssid, raw_psk=psk, scan_freq="2412")
  55. dev[2].request("REMOVE_NETWORK all")
  56. dev[0].connect(ssid, psk="very secret", scan_freq="2412")
  57. dev[0].request("REMOVE_NETWORK all")
  58. dev[2].connect(ssid, psk="another passphrase for all STAs", scan_freq="2412")
  59. dev[0].connect(ssid, psk="another passphrase for all STAs", scan_freq="2412")
  60. ev = dev[1].wait_event(["WPA: 4-Way Handshake failed"], timeout=10)
  61. if ev is None:
  62. raise Exception("Timed out while waiting for failure report")
  63. dev[1].request("REMOVE_NETWORK all")
  64. def test_ap_wpa2_ptk_rekey(dev, apdev):
  65. """WPA2-PSK AP and PTK rekey enforced by station"""
  66. ssid = "test-wpa2-psk"
  67. passphrase = 'qwertyuiop'
  68. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  69. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  70. dev[0].connect(ssid, psk=passphrase, wpa_ptk_rekey="1", scan_freq="2412")
  71. ev = dev[0].wait_event(["WPA: Key negotiation completed"])
  72. if ev is None:
  73. raise Exception("PTK rekey timed out")
  74. hwsim_utils.test_connectivity(dev[0], hapd)
  75. def test_ap_wpa2_ptk_rekey_ap(dev, apdev):
  76. """WPA2-PSK AP and PTK rekey enforced by AP"""
  77. ssid = "test-wpa2-psk"
  78. passphrase = 'qwertyuiop'
  79. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  80. params['wpa_ptk_rekey'] = '2'
  81. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  82. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  83. ev = dev[0].wait_event(["WPA: Key negotiation completed"])
  84. if ev is None:
  85. raise Exception("PTK rekey timed out")
  86. hwsim_utils.test_connectivity(dev[0], hapd)
  87. def test_ap_wpa2_sha256_ptk_rekey(dev, apdev):
  88. """WPA2-PSK/SHA256 AKM AP and PTK rekey enforced by station"""
  89. ssid = "test-wpa2-psk"
  90. passphrase = 'qwertyuiop'
  91. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  92. params["wpa_key_mgmt"] = "WPA-PSK-SHA256"
  93. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  94. dev[0].connect(ssid, psk=passphrase, key_mgmt="WPA-PSK-SHA256",
  95. wpa_ptk_rekey="1", scan_freq="2412")
  96. ev = dev[0].wait_event(["WPA: Key negotiation completed"])
  97. if ev is None:
  98. raise Exception("PTK rekey timed out")
  99. hwsim_utils.test_connectivity(dev[0], hapd)
  100. check_mib(dev[0], [ ("dot11RSNAAuthenticationSuiteRequested", "00-0f-ac-6"),
  101. ("dot11RSNAAuthenticationSuiteSelected", "00-0f-ac-6") ])
  102. def test_ap_wpa2_sha256_ptk_rekey_ap(dev, apdev):
  103. """WPA2-PSK/SHA256 AKM AP and PTK rekey enforced by AP"""
  104. ssid = "test-wpa2-psk"
  105. passphrase = 'qwertyuiop'
  106. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  107. params["wpa_key_mgmt"] = "WPA-PSK-SHA256"
  108. params['wpa_ptk_rekey'] = '2'
  109. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  110. dev[0].connect(ssid, psk=passphrase, key_mgmt="WPA-PSK-SHA256",
  111. scan_freq="2412")
  112. ev = dev[0].wait_event(["WPA: Key negotiation completed"])
  113. if ev is None:
  114. raise Exception("PTK rekey timed out")
  115. hwsim_utils.test_connectivity(dev[0], hapd)
  116. check_mib(dev[0], [ ("dot11RSNAAuthenticationSuiteRequested", "00-0f-ac-6"),
  117. ("dot11RSNAAuthenticationSuiteSelected", "00-0f-ac-6") ])
  118. def test_ap_wpa_ptk_rekey(dev, apdev):
  119. """WPA-PSK/TKIP AP and PTK rekey enforced by station"""
  120. ssid = "test-wpa-psk"
  121. passphrase = 'qwertyuiop'
  122. params = hostapd.wpa_params(ssid=ssid, passphrase=passphrase)
  123. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  124. dev[0].connect(ssid, psk=passphrase, wpa_ptk_rekey="1", scan_freq="2412")
  125. if "[WPA-PSK-TKIP]" not in dev[0].request("SCAN_RESULTS"):
  126. raise Exception("Scan results missing WPA element info")
  127. ev = dev[0].wait_event(["WPA: Key negotiation completed"])
  128. if ev is None:
  129. raise Exception("PTK rekey timed out")
  130. hwsim_utils.test_connectivity(dev[0], hapd)
  131. def test_ap_wpa_ptk_rekey_ap(dev, apdev):
  132. """WPA-PSK/TKIP AP and PTK rekey enforced by AP"""
  133. ssid = "test-wpa-psk"
  134. passphrase = 'qwertyuiop'
  135. params = hostapd.wpa_params(ssid=ssid, passphrase=passphrase)
  136. params['wpa_ptk_rekey'] = '2'
  137. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  138. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  139. ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=10)
  140. if ev is None:
  141. raise Exception("PTK rekey timed out")
  142. hwsim_utils.test_connectivity(dev[0], hapd)
  143. def test_ap_wpa_ccmp(dev, apdev):
  144. """WPA-PSK/CCMP"""
  145. ssid = "test-wpa-psk"
  146. passphrase = 'qwertyuiop'
  147. params = hostapd.wpa_params(ssid=ssid, passphrase=passphrase)
  148. params['wpa_pairwise'] = "CCMP"
  149. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  150. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  151. hwsim_utils.test_connectivity(dev[0], hapd)
  152. check_mib(dev[0], [ ("dot11RSNAConfigGroupCipherSize", "128"),
  153. ("dot11RSNAGroupCipherRequested", "00-50-f2-4"),
  154. ("dot11RSNAPairwiseCipherRequested", "00-50-f2-4"),
  155. ("dot11RSNAAuthenticationSuiteRequested", "00-50-f2-2"),
  156. ("dot11RSNAGroupCipherSelected", "00-50-f2-4"),
  157. ("dot11RSNAPairwiseCipherSelected", "00-50-f2-4"),
  158. ("dot11RSNAAuthenticationSuiteSelected", "00-50-f2-2"),
  159. ("dot1xSuppSuppControlledPortStatus", "Authorized") ])
  160. def test_ap_wpa2_psk_file(dev, apdev):
  161. """WPA2-PSK AP with various PSK file error and success cases"""
  162. addr0 = dev[0].own_addr()
  163. addr1 = dev[1].own_addr()
  164. addr2 = dev[2].own_addr()
  165. ssid = "psk"
  166. pskfile = "/tmp/ap_wpa2_psk_file_errors.psk_file"
  167. try:
  168. os.remove(pskfile)
  169. except:
  170. pass
  171. params = { "ssid": ssid, "wpa": "2", "wpa_key_mgmt": "WPA-PSK",
  172. "rsn_pairwise": "CCMP", "wpa_psk_file": pskfile }
  173. try:
  174. # missing PSK file
  175. hapd = hostapd.add_ap(apdev[0]['ifname'], params, no_enable=True)
  176. if "FAIL" not in hapd.request("ENABLE"):
  177. raise Exception("Unexpected ENABLE success")
  178. hapd.request("DISABLE")
  179. # invalid MAC address
  180. with open(pskfile, "w") as f:
  181. f.write("\n")
  182. f.write("foo\n")
  183. if "FAIL" not in hapd.request("ENABLE"):
  184. raise Exception("Unexpected ENABLE success")
  185. hapd.request("DISABLE")
  186. # no PSK on line
  187. with open(pskfile, "w") as f:
  188. f.write("00:11:22:33:44:55\n")
  189. if "FAIL" not in hapd.request("ENABLE"):
  190. raise Exception("Unexpected ENABLE success")
  191. hapd.request("DISABLE")
  192. # invalid PSK
  193. with open(pskfile, "w") as f:
  194. f.write("00:11:22:33:44:55 1234567\n")
  195. if "FAIL" not in hapd.request("ENABLE"):
  196. raise Exception("Unexpected ENABLE success")
  197. hapd.request("DISABLE")
  198. # valid PSK file
  199. with open(pskfile, "w") as f:
  200. f.write("00:11:22:33:44:55 12345678\n")
  201. f.write(addr0 + " 123456789\n")
  202. f.write(addr1 + " 123456789a\n")
  203. f.write(addr2 + " 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef\n")
  204. if "FAIL" in hapd.request("ENABLE"):
  205. raise Exception("Unexpected ENABLE failure")
  206. dev[0].connect(ssid, psk="123456789", scan_freq="2412")
  207. dev[1].connect(ssid, psk="123456789a", scan_freq="2412")
  208. dev[2].connect(ssid, raw_psk="0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", scan_freq="2412")
  209. finally:
  210. try:
  211. os.remove(pskfile)
  212. except:
  213. pass
  214. def test_ap_wpa2_psk_wildcard_ssid(dev, apdev):
  215. """WPA2-PSK AP and wildcard SSID configuration"""
  216. ssid = "test-wpa2-psk"
  217. passphrase = 'qwertyuiop'
  218. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  219. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  220. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  221. dev[0].connect("", bssid=apdev[0]['bssid'], psk=passphrase,
  222. scan_freq="2412")
  223. dev[1].connect("", bssid=apdev[0]['bssid'], raw_psk=psk, scan_freq="2412")
  224. def test_ap_wpa2_gtk_rekey(dev, apdev):
  225. """WPA2-PSK AP and GTK rekey enforced by AP"""
  226. ssid = "test-wpa2-psk"
  227. passphrase = 'qwertyuiop'
  228. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  229. params['wpa_group_rekey'] = '1'
  230. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  231. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  232. ev = dev[0].wait_event(["WPA: Group rekeying completed"], timeout=2)
  233. if ev is None:
  234. raise Exception("GTK rekey timed out")
  235. hwsim_utils.test_connectivity(dev[0], hapd)
  236. def test_ap_wpa_gtk_rekey(dev, apdev):
  237. """WPA-PSK/TKIP AP and GTK rekey enforced by AP"""
  238. ssid = "test-wpa-psk"
  239. passphrase = 'qwertyuiop'
  240. params = hostapd.wpa_params(ssid=ssid, passphrase=passphrase)
  241. params['wpa_group_rekey'] = '1'
  242. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  243. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  244. ev = dev[0].wait_event(["WPA: Group rekeying completed"], timeout=2)
  245. if ev is None:
  246. raise Exception("GTK rekey timed out")
  247. hwsim_utils.test_connectivity(dev[0], hapd)
  248. def test_ap_wpa2_gmk_rekey(dev, apdev):
  249. """WPA2-PSK AP and GMK and GTK rekey enforced by AP"""
  250. ssid = "test-wpa2-psk"
  251. passphrase = 'qwertyuiop'
  252. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  253. params['wpa_group_rekey'] = '1'
  254. params['wpa_gmk_rekey'] = '2'
  255. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  256. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  257. for i in range(0, 3):
  258. ev = dev[0].wait_event(["WPA: Group rekeying completed"], timeout=2)
  259. if ev is None:
  260. raise Exception("GTK rekey timed out")
  261. hwsim_utils.test_connectivity(dev[0], hapd)
  262. def test_ap_wpa2_strict_rekey(dev, apdev):
  263. """WPA2-PSK AP and strict GTK rekey enforced by AP"""
  264. ssid = "test-wpa2-psk"
  265. passphrase = 'qwertyuiop'
  266. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  267. params['wpa_strict_rekey'] = '1'
  268. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  269. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  270. dev[1].connect(ssid, psk=passphrase, scan_freq="2412")
  271. dev[1].request("DISCONNECT")
  272. ev = dev[0].wait_event(["WPA: Group rekeying completed"], timeout=2)
  273. if ev is None:
  274. raise Exception("GTK rekey timed out")
  275. hwsim_utils.test_connectivity(dev[0], hapd)
  276. def test_ap_wpa2_bridge_fdb(dev, apdev):
  277. """Bridge FDB entry removal"""
  278. try:
  279. ssid = "test-wpa2-psk"
  280. passphrase = "12345678"
  281. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  282. params['bridge'] = 'ap-br0'
  283. hostapd.add_ap(apdev[0]['ifname'], params)
  284. subprocess.call(['brctl', 'setfd', 'ap-br0', '0'])
  285. subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'up'])
  286. dev[0].connect(ssid, psk=passphrase, scan_freq="2412",
  287. bssid=apdev[0]['bssid'])
  288. dev[1].connect(ssid, psk=passphrase, scan_freq="2412",
  289. bssid=apdev[0]['bssid'])
  290. addr0 = dev[0].p2p_interface_addr()
  291. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  292. cmd = subprocess.Popen(['brctl', 'showmacs', 'ap-br0'],
  293. stdout=subprocess.PIPE)
  294. macs1 = cmd.stdout.read()
  295. dev[0].request("DISCONNECT")
  296. dev[1].request("DISCONNECT")
  297. time.sleep(1)
  298. cmd = subprocess.Popen(['brctl', 'showmacs', 'ap-br0'],
  299. stdout=subprocess.PIPE)
  300. macs2 = cmd.stdout.read()
  301. addr1 = dev[1].p2p_interface_addr()
  302. if addr0 not in macs1 or addr1 not in macs1:
  303. raise Exception("Bridge FDB entry missing")
  304. if addr0 in macs2 or addr1 in macs2:
  305. raise Exception("Bridge FDB entry was not removed")
  306. finally:
  307. subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'down'])
  308. subprocess.call(['brctl', 'delbr', 'ap-br0'])
  309. def test_ap_wpa2_already_in_bridge(dev, apdev):
  310. """hostapd behavior with interface already in bridge"""
  311. ifname = apdev[0]['ifname']
  312. br_ifname = 'ext-ap-br0'
  313. try:
  314. ssid = "test-wpa2-psk"
  315. passphrase = "12345678"
  316. subprocess.call(['brctl', 'addbr', br_ifname])
  317. subprocess.call(['brctl', 'setfd', br_ifname, '0'])
  318. subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'up'])
  319. subprocess.call(['iw', ifname, 'set', 'type', '__ap'])
  320. subprocess.call(['brctl', 'addif', br_ifname, ifname])
  321. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  322. hapd = hostapd.add_ap(ifname, params)
  323. if hapd.get_driver_status_field('brname') != br_ifname:
  324. raise Exception("Bridge name not identified correctly")
  325. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  326. finally:
  327. subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'down'])
  328. subprocess.call(['brctl', 'delif', br_ifname, ifname])
  329. subprocess.call(['iw', ifname, 'set', 'type', 'station'])
  330. subprocess.call(['brctl', 'delbr', br_ifname])
  331. def test_ap_wpa2_in_different_bridge(dev, apdev):
  332. """hostapd behavior with interface in different bridge"""
  333. ifname = apdev[0]['ifname']
  334. br_ifname = 'ext-ap-br0'
  335. try:
  336. ssid = "test-wpa2-psk"
  337. passphrase = "12345678"
  338. subprocess.call(['brctl', 'addbr', br_ifname])
  339. subprocess.call(['brctl', 'setfd', br_ifname, '0'])
  340. subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'up'])
  341. subprocess.call(['iw', ifname, 'set', 'type', '__ap'])
  342. subprocess.call(['brctl', 'addif', br_ifname, ifname])
  343. time.sleep(0.5)
  344. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  345. params['bridge'] = 'ap-br0'
  346. hapd = hostapd.add_ap(ifname, params)
  347. subprocess.call(['brctl', 'setfd', 'ap-br0', '0'])
  348. subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'up'])
  349. brname = hapd.get_driver_status_field('brname')
  350. if brname != 'ap-br0':
  351. raise Exception("Incorrect bridge: " + brname)
  352. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  353. hwsim_utils.test_connectivity_iface(dev[0], hapd, "ap-br0")
  354. if hapd.get_driver_status_field("added_bridge") != "1":
  355. raise Exception("Unexpected added_bridge value")
  356. if hapd.get_driver_status_field("added_if_into_bridge") != "1":
  357. raise Exception("Unexpected added_if_into_bridge value")
  358. dev[0].request("DISCONNECT")
  359. hapd.disable()
  360. finally:
  361. subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'down'])
  362. subprocess.call(['brctl', 'delif', br_ifname, ifname],
  363. stderr=open('/dev/null', 'w'))
  364. subprocess.call(['brctl', 'delbr', br_ifname])
  365. def test_ap_wpa2_ext_add_to_bridge(dev, apdev):
  366. """hostapd behavior with interface added to bridge externally"""
  367. ifname = apdev[0]['ifname']
  368. br_ifname = 'ext-ap-br0'
  369. try:
  370. ssid = "test-wpa2-psk"
  371. passphrase = "12345678"
  372. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  373. hapd = hostapd.add_ap(ifname, params)
  374. subprocess.call(['brctl', 'addbr', br_ifname])
  375. subprocess.call(['brctl', 'setfd', br_ifname, '0'])
  376. subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'up'])
  377. subprocess.call(['brctl', 'addif', br_ifname, ifname])
  378. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  379. if hapd.get_driver_status_field('brname') != br_ifname:
  380. raise Exception("Bridge name not identified correctly")
  381. finally:
  382. subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'down'])
  383. subprocess.call(['brctl', 'delif', br_ifname, ifname])
  384. subprocess.call(['brctl', 'delbr', br_ifname])
  385. def test_ap_wpa2_psk_ext(dev, apdev):
  386. """WPA2-PSK AP using external EAPOL I/O"""
  387. bssid = apdev[0]['bssid']
  388. ssid = "test-wpa2-psk"
  389. passphrase = 'qwertyuiop'
  390. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  391. params = hostapd.wpa2_params(ssid=ssid)
  392. params['wpa_psk'] = psk
  393. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  394. hapd.request("SET ext_eapol_frame_io 1")
  395. dev[0].request("SET ext_eapol_frame_io 1")
  396. dev[0].connect(ssid, psk=passphrase, scan_freq="2412", wait_connect=False)
  397. addr = dev[0].p2p_interface_addr()
  398. while True:
  399. ev = hapd.wait_event(["EAPOL-TX", "AP-STA-CONNECTED"], timeout=15)
  400. if ev is None:
  401. raise Exception("Timeout on EAPOL-TX from hostapd")
  402. if "AP-STA-CONNECTED" in ev:
  403. dev[0].wait_connected(timeout=15)
  404. break
  405. res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2])
  406. if "OK" not in res:
  407. raise Exception("EAPOL_RX to wpa_supplicant failed")
  408. ev = dev[0].wait_event(["EAPOL-TX", "CTRL-EVENT-CONNECTED"], timeout=15)
  409. if ev is None:
  410. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  411. if "CTRL-EVENT-CONNECTED" in ev:
  412. break
  413. res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2])
  414. if "OK" not in res:
  415. raise Exception("EAPOL_RX to hostapd failed")
  416. def parse_eapol(data):
  417. (version, type, length) = struct.unpack('>BBH', data[0:4])
  418. payload = data[4:]
  419. if length > len(payload):
  420. raise Exception("Invalid EAPOL length")
  421. if length < len(payload):
  422. payload = payload[0:length]
  423. eapol = {}
  424. eapol['version'] = version
  425. eapol['type'] = type
  426. eapol['length'] = length
  427. eapol['payload'] = payload
  428. if type == 3:
  429. # EAPOL-Key
  430. (eapol['descr_type'],) = struct.unpack('B', payload[0:1])
  431. payload = payload[1:]
  432. if eapol['descr_type'] == 2 or eapol['descr_type'] == 254:
  433. # RSN EAPOL-Key
  434. (key_info, key_len) = struct.unpack('>HH', payload[0:4])
  435. eapol['rsn_key_info'] = key_info
  436. eapol['rsn_key_len'] = key_len
  437. eapol['rsn_replay_counter'] = payload[4:12]
  438. eapol['rsn_key_nonce'] = payload[12:44]
  439. eapol['rsn_key_iv'] = payload[44:60]
  440. eapol['rsn_key_rsc'] = payload[60:68]
  441. eapol['rsn_key_id'] = payload[68:76]
  442. eapol['rsn_key_mic'] = payload[76:92]
  443. payload = payload[92:]
  444. (eapol['rsn_key_data_len'],) = struct.unpack('>H', payload[0:2])
  445. payload = payload[2:]
  446. eapol['rsn_key_data'] = payload
  447. return eapol
  448. def build_eapol(msg):
  449. data = struct.pack(">BBH", msg['version'], msg['type'], msg['length'])
  450. if msg['type'] == 3:
  451. data += struct.pack('>BHH', msg['descr_type'], msg['rsn_key_info'],
  452. msg['rsn_key_len'])
  453. data += msg['rsn_replay_counter']
  454. data += msg['rsn_key_nonce']
  455. data += msg['rsn_key_iv']
  456. data += msg['rsn_key_rsc']
  457. data += msg['rsn_key_id']
  458. data += msg['rsn_key_mic']
  459. data += struct.pack('>H', msg['rsn_key_data_len'])
  460. data += msg['rsn_key_data']
  461. else:
  462. data += msg['payload']
  463. return data
  464. def sha1_prf(key, label, data, outlen):
  465. res = ''
  466. counter = 0
  467. while outlen > 0:
  468. m = hmac.new(key, label, hashlib.sha1)
  469. m.update(struct.pack('B', 0))
  470. m.update(data)
  471. m.update(struct.pack('B', counter))
  472. counter += 1
  473. hash = m.digest()
  474. if outlen > len(hash):
  475. res += hash
  476. outlen -= len(hash)
  477. else:
  478. res += hash[0:outlen]
  479. outlen = 0
  480. return res
  481. def pmk_to_ptk(pmk, addr1, addr2, nonce1, nonce2):
  482. if addr1 < addr2:
  483. data = binascii.unhexlify(addr1.replace(':','')) + binascii.unhexlify(addr2.replace(':',''))
  484. else:
  485. data = binascii.unhexlify(addr2.replace(':','')) + binascii.unhexlify(addr1.replace(':',''))
  486. if nonce1 < nonce2:
  487. data += nonce1 + nonce2
  488. else:
  489. data += nonce2 + nonce1
  490. label = "Pairwise key expansion"
  491. ptk = sha1_prf(pmk, label, data, 48)
  492. kck = ptk[0:16]
  493. kek = ptk[16:32]
  494. return (ptk, kck, kek)
  495. def eapol_key_mic(kck, msg):
  496. msg['rsn_key_mic'] = binascii.unhexlify('00000000000000000000000000000000')
  497. data = build_eapol(msg)
  498. m = hmac.new(kck, data, hashlib.sha1)
  499. msg['rsn_key_mic'] = m.digest()[0:16]
  500. def rsn_eapol_key_set(msg, key_info, key_len, nonce, data):
  501. msg['rsn_key_info'] = key_info
  502. msg['rsn_key_len'] = key_len
  503. if nonce:
  504. msg['rsn_key_nonce'] = nonce
  505. else:
  506. msg['rsn_key_nonce'] = binascii.unhexlify('0000000000000000000000000000000000000000000000000000000000000000')
  507. if data:
  508. msg['rsn_key_data_len'] = len(data)
  509. msg['rsn_key_data'] = data
  510. msg['length'] = 95 + len(data)
  511. else:
  512. msg['rsn_key_data_len'] = 0
  513. msg['rsn_key_data'] = ''
  514. msg['length'] = 95
  515. def recv_eapol(hapd):
  516. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  517. if ev is None:
  518. raise Exception("Timeout on EAPOL-TX from hostapd")
  519. eapol = binascii.unhexlify(ev.split(' ')[2])
  520. return parse_eapol(eapol)
  521. def send_eapol(hapd, addr, data):
  522. res = hapd.request("EAPOL_RX " + addr + " " + binascii.hexlify(data))
  523. if "OK" not in res:
  524. raise Exception("EAPOL_RX to hostapd failed")
  525. def reply_eapol(info, hapd, addr, msg, key_info, nonce, data, kck):
  526. logger.info("Send EAPOL-Key msg " + info)
  527. rsn_eapol_key_set(msg, key_info, 0, nonce, data)
  528. eapol_key_mic(kck, msg)
  529. send_eapol(hapd, addr, build_eapol(msg))
  530. def hapd_connected(hapd):
  531. ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=15)
  532. if ev is None:
  533. raise Exception("Timeout on AP-STA-CONNECTED from hostapd")
  534. def eapol_test(apdev, dev, wpa2=True):
  535. bssid = apdev['bssid']
  536. if wpa2:
  537. ssid = "test-wpa2-psk"
  538. else:
  539. ssid = "test-wpa-psk"
  540. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  541. pmk = binascii.unhexlify(psk)
  542. if wpa2:
  543. params = hostapd.wpa2_params(ssid=ssid)
  544. else:
  545. params = hostapd.wpa_params(ssid=ssid)
  546. params['wpa_psk'] = psk
  547. hapd = hostapd.add_ap(apdev['ifname'], params)
  548. hapd.request("SET ext_eapol_frame_io 1")
  549. dev.request("SET ext_eapol_frame_io 1")
  550. dev.connect(ssid, raw_psk=psk, scan_freq="2412", wait_connect=False)
  551. addr = dev.p2p_interface_addr()
  552. if wpa2:
  553. rsne = binascii.unhexlify('30140100000fac040100000fac040100000fac020000')
  554. else:
  555. rsne = binascii.unhexlify('dd160050f20101000050f20201000050f20201000050f202')
  556. snonce = binascii.unhexlify('1111111111111111111111111111111111111111111111111111111111111111')
  557. return (bssid,ssid,hapd,snonce,pmk,addr,rsne)
  558. def test_ap_wpa2_psk_ext_eapol(dev, apdev):
  559. """WPA2-PSK AP using external EAPOL supplicant"""
  560. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  561. msg = recv_eapol(hapd)
  562. anonce = msg['rsn_key_nonce']
  563. logger.info("Replay same data back")
  564. send_eapol(hapd, addr, build_eapol(msg))
  565. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  566. logger.info("Truncated Key Data in EAPOL-Key msg 2/4")
  567. rsn_eapol_key_set(msg, 0x0101, 0, snonce, rsne)
  568. msg['length'] = 95 + 22 - 1
  569. send_eapol(hapd, addr, build_eapol(msg))
  570. reply_eapol("2/4", hapd, addr, msg, 0x010a, snonce, rsne, kck)
  571. msg = recv_eapol(hapd)
  572. if anonce != msg['rsn_key_nonce']:
  573. raise Exception("ANonce changed")
  574. logger.info("Replay same data back")
  575. send_eapol(hapd, addr, build_eapol(msg))
  576. reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck)
  577. hapd_connected(hapd)
  578. def test_ap_wpa2_psk_ext_eapol_retry1(dev, apdev):
  579. """WPA2 4-way handshake with EAPOL-Key 1/4 retransmitted"""
  580. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  581. msg1 = recv_eapol(hapd)
  582. anonce = msg1['rsn_key_nonce']
  583. msg2 = recv_eapol(hapd)
  584. if anonce != msg2['rsn_key_nonce']:
  585. raise Exception("ANonce changed")
  586. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  587. logger.info("Send EAPOL-Key msg 2/4")
  588. msg = msg2
  589. rsn_eapol_key_set(msg, 0x010a, 0, snonce, rsne)
  590. eapol_key_mic(kck, msg)
  591. send_eapol(hapd, addr, build_eapol(msg))
  592. msg = recv_eapol(hapd)
  593. if anonce != msg['rsn_key_nonce']:
  594. raise Exception("ANonce changed")
  595. reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck)
  596. hapd_connected(hapd)
  597. def test_ap_wpa2_psk_ext_eapol_retry1b(dev, apdev):
  598. """WPA2 4-way handshake with EAPOL-Key 1/4 and 2/4 retransmitted"""
  599. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  600. msg1 = recv_eapol(hapd)
  601. anonce = msg1['rsn_key_nonce']
  602. msg2 = recv_eapol(hapd)
  603. if anonce != msg2['rsn_key_nonce']:
  604. raise Exception("ANonce changed")
  605. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  606. reply_eapol("2/4 (a)", hapd, addr, msg1, 0x010a, snonce, rsne, kck)
  607. reply_eapol("2/4 (b)", hapd, addr, msg2, 0x010a, snonce, rsne, kck)
  608. msg = recv_eapol(hapd)
  609. if anonce != msg['rsn_key_nonce']:
  610. raise Exception("ANonce changed")
  611. reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck)
  612. hapd_connected(hapd)
  613. def test_ap_wpa2_psk_ext_eapol_retry1c(dev, apdev):
  614. """WPA2 4-way handshake with EAPOL-Key 1/4 and 2/4 retransmitted and SNonce changing"""
  615. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  616. msg1 = recv_eapol(hapd)
  617. anonce = msg1['rsn_key_nonce']
  618. msg2 = recv_eapol(hapd)
  619. if anonce != msg2['rsn_key_nonce']:
  620. raise Exception("ANonce changed")
  621. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  622. reply_eapol("2/4 (a)", hapd, addr, msg1, 0x010a, snonce, rsne, kck)
  623. snonce2 = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  624. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce2, anonce)
  625. reply_eapol("2/4 (b)", hapd, addr, msg2, 0x010a, snonce2, rsne, kck)
  626. msg = recv_eapol(hapd)
  627. if anonce != msg['rsn_key_nonce']:
  628. raise Exception("ANonce changed")
  629. reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck)
  630. hapd_connected(hapd)
  631. def test_ap_wpa2_psk_ext_eapol_retry1d(dev, apdev):
  632. """WPA2 4-way handshake with EAPOL-Key 1/4 and 2/4 retransmitted and SNonce changing and older used"""
  633. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  634. msg1 = recv_eapol(hapd)
  635. anonce = msg1['rsn_key_nonce']
  636. msg2 = recv_eapol(hapd)
  637. if anonce != msg2['rsn_key_nonce']:
  638. raise Exception("ANonce changed")
  639. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  640. reply_eapol("2/4 (a)", hapd, addr, msg1, 0x010a, snonce, rsne, kck)
  641. snonce2 = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  642. (ptk2, kck2, kek2) = pmk_to_ptk(pmk, addr, bssid, snonce2, anonce)
  643. reply_eapol("2/4 (b)", hapd, addr, msg2, 0x010a, snonce2, rsne, kck2)
  644. msg = recv_eapol(hapd)
  645. if anonce != msg['rsn_key_nonce']:
  646. raise Exception("ANonce changed")
  647. reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck)
  648. hapd_connected(hapd)
  649. def test_ap_wpa2_psk_ext_eapol_type_diff(dev, apdev):
  650. """WPA2 4-way handshake using external EAPOL supplicant"""
  651. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  652. msg = recv_eapol(hapd)
  653. anonce = msg['rsn_key_nonce']
  654. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  655. # Incorrect descriptor type (frame dropped)
  656. msg['descr_type'] = 253
  657. rsn_eapol_key_set(msg, 0x010a, 0, snonce, rsne)
  658. eapol_key_mic(kck, msg)
  659. send_eapol(hapd, addr, build_eapol(msg))
  660. # Incorrect descriptor type, but with a workaround (frame processed)
  661. msg['descr_type'] = 254
  662. rsn_eapol_key_set(msg, 0x010a, 0, snonce, rsne)
  663. eapol_key_mic(kck, msg)
  664. send_eapol(hapd, addr, build_eapol(msg))
  665. msg = recv_eapol(hapd)
  666. if anonce != msg['rsn_key_nonce']:
  667. raise Exception("ANonce changed")
  668. logger.info("Replay same data back")
  669. send_eapol(hapd, addr, build_eapol(msg))
  670. reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck)
  671. hapd_connected(hapd)
  672. def test_ap_wpa_psk_ext_eapol(dev, apdev):
  673. """WPA2-PSK AP using external EAPOL supplicant"""
  674. (bssid,ssid,hapd,snonce,pmk,addr,wpae) = eapol_test(apdev[0], dev[0],
  675. wpa2=False)
  676. msg = recv_eapol(hapd)
  677. anonce = msg['rsn_key_nonce']
  678. logger.info("Replay same data back")
  679. send_eapol(hapd, addr, build_eapol(msg))
  680. logger.info("Too short data")
  681. send_eapol(hapd, addr, build_eapol(msg)[0:98])
  682. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  683. msg['descr_type'] = 2
  684. reply_eapol("2/4(invalid type)", hapd, addr, msg, 0x010a, snonce, wpae, kck)
  685. msg['descr_type'] = 254
  686. reply_eapol("2/4", hapd, addr, msg, 0x010a, snonce, wpae, kck)
  687. msg = recv_eapol(hapd)
  688. if anonce != msg['rsn_key_nonce']:
  689. raise Exception("ANonce changed")
  690. logger.info("Replay same data back")
  691. send_eapol(hapd, addr, build_eapol(msg))
  692. reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck)
  693. hapd_connected(hapd)
  694. def test_ap_wpa2_psk_ext_eapol_key_info(dev, apdev):
  695. """WPA2-PSK 4-way handshake with strange key info values"""
  696. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  697. msg = recv_eapol(hapd)
  698. anonce = msg['rsn_key_nonce']
  699. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  700. rsn_eapol_key_set(msg, 0x0000, 0, snonce, rsne)
  701. send_eapol(hapd, addr, build_eapol(msg))
  702. rsn_eapol_key_set(msg, 0xffff, 0, snonce, rsne)
  703. send_eapol(hapd, addr, build_eapol(msg))
  704. # SMK M1
  705. rsn_eapol_key_set(msg, 0x2802, 0, snonce, rsne)
  706. send_eapol(hapd, addr, build_eapol(msg))
  707. # SMK M3
  708. rsn_eapol_key_set(msg, 0x2002, 0, snonce, rsne)
  709. send_eapol(hapd, addr, build_eapol(msg))
  710. # Request
  711. rsn_eapol_key_set(msg, 0x0902, 0, snonce, rsne)
  712. send_eapol(hapd, addr, build_eapol(msg))
  713. # Request
  714. rsn_eapol_key_set(msg, 0x0902, 0, snonce, rsne)
  715. tmp_kck = binascii.unhexlify('00000000000000000000000000000000')
  716. eapol_key_mic(tmp_kck, msg)
  717. send_eapol(hapd, addr, build_eapol(msg))
  718. reply_eapol("2/4", hapd, addr, msg, 0x010a, snonce, rsne, kck)
  719. msg = recv_eapol(hapd)
  720. if anonce != msg['rsn_key_nonce']:
  721. raise Exception("ANonce changed")
  722. # Request (valic MIC)
  723. rsn_eapol_key_set(msg, 0x0902, 0, snonce, rsne)
  724. eapol_key_mic(kck, msg)
  725. send_eapol(hapd, addr, build_eapol(msg))
  726. # Request (valid MIC, replayed counter)
  727. rsn_eapol_key_set(msg, 0x0902, 0, snonce, rsne)
  728. eapol_key_mic(kck, msg)
  729. send_eapol(hapd, addr, build_eapol(msg))
  730. reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck)
  731. hapd_connected(hapd)
  732. def build_eapol_key_1_4(anonce, replay_counter=1, key_data='', key_len=16):
  733. msg = {}
  734. msg['version'] = 2
  735. msg['type'] = 3
  736. msg['length'] = 95 + len(key_data)
  737. msg['descr_type'] = 2
  738. msg['rsn_key_info'] = 0x8a
  739. msg['rsn_key_len'] = key_len
  740. msg['rsn_replay_counter'] = struct.pack('>Q', replay_counter)
  741. msg['rsn_key_nonce'] = anonce
  742. msg['rsn_key_iv'] = binascii.unhexlify('00000000000000000000000000000000')
  743. msg['rsn_key_rsc'] = binascii.unhexlify('0000000000000000')
  744. msg['rsn_key_id'] = binascii.unhexlify('0000000000000000')
  745. msg['rsn_key_mic'] = binascii.unhexlify('00000000000000000000000000000000')
  746. msg['rsn_key_data_len'] = len(key_data)
  747. msg['rsn_key_data'] = key_data
  748. return msg
  749. def build_eapol_key_3_4(anonce, kck, key_data, replay_counter=2,
  750. key_info=0x13ca, extra_len=0, descr_type=2, key_len=16):
  751. msg = {}
  752. msg['version'] = 2
  753. msg['type'] = 3
  754. msg['length'] = 95 + len(key_data) + extra_len
  755. msg['descr_type'] = descr_type
  756. msg['rsn_key_info'] = key_info
  757. msg['rsn_key_len'] = key_len
  758. msg['rsn_replay_counter'] = struct.pack('>Q', replay_counter)
  759. msg['rsn_key_nonce'] = anonce
  760. msg['rsn_key_iv'] = binascii.unhexlify('00000000000000000000000000000000')
  761. msg['rsn_key_rsc'] = binascii.unhexlify('0000000000000000')
  762. msg['rsn_key_id'] = binascii.unhexlify('0000000000000000')
  763. msg['rsn_key_data_len'] = len(key_data)
  764. msg['rsn_key_data'] = key_data
  765. eapol_key_mic(kck, msg)
  766. return msg
  767. def aes_wrap(kek, plain):
  768. n = len(plain) / 8
  769. a = 0xa6a6a6a6a6a6a6a6
  770. enc = AES.new(kek).encrypt
  771. r = [plain[i * 8:(i + 1) * 8] for i in range(0, n)]
  772. for j in range(6):
  773. for i in range(1, n + 1):
  774. b = enc(struct.pack('>Q', a) + r[i - 1])
  775. a = struct.unpack('>Q', b[:8])[0] ^ (n * j + i)
  776. r[i - 1] =b[8:]
  777. return struct.pack('>Q', a) + ''.join(r)
  778. def pad_key_data(plain):
  779. pad_len = len(plain) % 8
  780. if pad_len:
  781. pad_len = 8 - pad_len
  782. plain += '\xdd'
  783. pad_len -= 1
  784. plain += pad_len * '\0'
  785. return plain
  786. def test_ap_wpa2_psk_supp_proto(dev, apdev):
  787. """WPA2-PSK 4-way handshake protocol testing for supplicant"""
  788. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  789. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  790. msg = recv_eapol(hapd)
  791. dev[0].dump_monitor()
  792. # Build own EAPOL-Key msg 1/4
  793. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  794. counter = 1
  795. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  796. counter += 1
  797. send_eapol(dev[0], addr, build_eapol(msg))
  798. msg = recv_eapol(dev[0])
  799. snonce = msg['rsn_key_nonce']
  800. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  801. logger.debug("Invalid AES wrap data length 0")
  802. dev[0].dump_monitor()
  803. msg = build_eapol_key_3_4(anonce, kck, '', replay_counter=counter)
  804. counter += 1
  805. send_eapol(dev[0], addr, build_eapol(msg))
  806. ev = dev[0].wait_event(["WPA: Unsupported AES-WRAP len 0"])
  807. if ev is None:
  808. raise Exception("Unsupported AES-WRAP len 0 not reported")
  809. logger.debug("Invalid AES wrap data length 1")
  810. dev[0].dump_monitor()
  811. msg = build_eapol_key_3_4(anonce, kck, '1', replay_counter=counter)
  812. counter += 1
  813. send_eapol(dev[0], addr, build_eapol(msg))
  814. ev = dev[0].wait_event(["WPA: Unsupported AES-WRAP len 1"])
  815. if ev is None:
  816. raise Exception("Unsupported AES-WRAP len 1 not reported")
  817. logger.debug("Invalid AES wrap data length 9")
  818. dev[0].dump_monitor()
  819. msg = build_eapol_key_3_4(anonce, kck, '123456789', replay_counter=counter)
  820. counter += 1
  821. send_eapol(dev[0], addr, build_eapol(msg))
  822. ev = dev[0].wait_event(["WPA: Unsupported AES-WRAP len 9"])
  823. if ev is None:
  824. raise Exception("Unsupported AES-WRAP len 9 not reported")
  825. logger.debug("Invalid AES wrap data payload")
  826. dev[0].dump_monitor()
  827. msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter)
  828. # do not increment counter to test replay protection
  829. send_eapol(dev[0], addr, build_eapol(msg))
  830. ev = dev[0].wait_event(["WPA: AES unwrap failed"])
  831. if ev is None:
  832. raise Exception("AES unwrap failure not reported")
  833. logger.debug("Replay Count not increasing")
  834. dev[0].dump_monitor()
  835. msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter)
  836. counter += 1
  837. send_eapol(dev[0], addr, build_eapol(msg))
  838. ev = dev[0].wait_event(["WPA: EAPOL-Key Replay Counter did not increase"])
  839. if ev is None:
  840. raise Exception("Replay Counter replay not reported")
  841. logger.debug("Missing Ack bit in key info")
  842. dev[0].dump_monitor()
  843. msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter,
  844. key_info=0x134a)
  845. counter += 1
  846. send_eapol(dev[0], addr, build_eapol(msg))
  847. ev = dev[0].wait_event(["WPA: No Ack bit in key_info"])
  848. if ev is None:
  849. raise Exception("Missing Ack bit not reported")
  850. logger.debug("Unexpected Request bit in key info")
  851. dev[0].dump_monitor()
  852. msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter,
  853. key_info=0x1bca)
  854. counter += 1
  855. send_eapol(dev[0], addr, build_eapol(msg))
  856. ev = dev[0].wait_event(["WPA: EAPOL-Key with Request bit"])
  857. if ev is None:
  858. raise Exception("Request bit not reported")
  859. logger.debug("Unsupported key descriptor version 0")
  860. dev[0].dump_monitor()
  861. msg = build_eapol_key_3_4(anonce, kck, '0123456789abcdef',
  862. replay_counter=counter, key_info=0x13c8)
  863. counter += 1
  864. send_eapol(dev[0], addr, build_eapol(msg))
  865. ev = dev[0].wait_event(["WPA: Unsupported EAPOL-Key descriptor version 0"])
  866. if ev is None:
  867. raise Exception("Unsupported EAPOL-Key descriptor version 0 not reported")
  868. logger.debug("Key descriptor version 1 not allowed with CCMP")
  869. dev[0].dump_monitor()
  870. msg = build_eapol_key_3_4(anonce, kck, '0123456789abcdef',
  871. replay_counter=counter, key_info=0x13c9)
  872. counter += 1
  873. send_eapol(dev[0], addr, build_eapol(msg))
  874. ev = dev[0].wait_event(["WPA: CCMP is used, but EAPOL-Key descriptor version (1) is not 2"])
  875. if ev is None:
  876. raise Exception("Not allowed EAPOL-Key descriptor version not reported")
  877. logger.debug("Invalid AES wrap payload with key descriptor version 2")
  878. dev[0].dump_monitor()
  879. msg = build_eapol_key_3_4(anonce, kck, '0123456789abcdef',
  880. replay_counter=counter, key_info=0x13ca)
  881. counter += 1
  882. send_eapol(dev[0], addr, build_eapol(msg))
  883. ev = dev[0].wait_event(["WPA: AES unwrap failed"])
  884. if ev is None:
  885. raise Exception("AES unwrap failure not reported")
  886. logger.debug("Key descriptor version 3 workaround")
  887. dev[0].dump_monitor()
  888. msg = build_eapol_key_3_4(anonce, kck, '0123456789abcdef',
  889. replay_counter=counter, key_info=0x13cb)
  890. counter += 1
  891. send_eapol(dev[0], addr, build_eapol(msg))
  892. ev = dev[0].wait_event(["WPA: CCMP is used, but EAPOL-Key descriptor version (3) is not 2"])
  893. if ev is None:
  894. raise Exception("CCMP key descriptor mismatch not reported")
  895. ev = dev[0].wait_event(["WPA: Interoperability workaround"])
  896. if ev is None:
  897. raise Exception("AES-128-CMAC workaround not reported")
  898. ev = dev[0].wait_event(["WPA: Invalid EAPOL-Key MIC - dropping packet"])
  899. if ev is None:
  900. raise Exception("MIC failure with AES-128-CMAC workaround not reported")
  901. logger.debug("Unsupported key descriptor version 4")
  902. dev[0].dump_monitor()
  903. msg = build_eapol_key_3_4(anonce, kck, '0123456789abcdef',
  904. replay_counter=counter, key_info=0x13cc)
  905. counter += 1
  906. send_eapol(dev[0], addr, build_eapol(msg))
  907. ev = dev[0].wait_event(["WPA: Unsupported EAPOL-Key descriptor version 4"])
  908. if ev is None:
  909. raise Exception("Unsupported EAPOL-Key descriptor version 4 not reported")
  910. logger.debug("Unsupported key descriptor version 7")
  911. dev[0].dump_monitor()
  912. msg = build_eapol_key_3_4(anonce, kck, '0123456789abcdef',
  913. replay_counter=counter, key_info=0x13cf)
  914. counter += 1
  915. send_eapol(dev[0], addr, build_eapol(msg))
  916. ev = dev[0].wait_event(["WPA: Unsupported EAPOL-Key descriptor version 7"])
  917. if ev is None:
  918. raise Exception("Unsupported EAPOL-Key descriptor version 7 not reported")
  919. logger.debug("Too short EAPOL header length")
  920. dev[0].dump_monitor()
  921. msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter,
  922. extra_len=-1)
  923. counter += 1
  924. send_eapol(dev[0], addr, build_eapol(msg))
  925. ev = dev[0].wait_event(["WPA: Invalid EAPOL-Key frame - key_data overflow (8 > 7)"])
  926. if ev is None:
  927. raise Exception("Key data overflow not reported")
  928. logger.debug("Too long EAPOL header length")
  929. msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter,
  930. extra_len=1)
  931. counter += 1
  932. send_eapol(dev[0], addr, build_eapol(msg))
  933. logger.debug("Unsupported descriptor type 0")
  934. msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter,
  935. descr_type=0)
  936. counter += 1
  937. send_eapol(dev[0], addr, build_eapol(msg))
  938. logger.debug("WPA descriptor type 0")
  939. msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter,
  940. descr_type=254)
  941. counter += 1
  942. send_eapol(dev[0], addr, build_eapol(msg))
  943. logger.debug("Non-zero key index for pairwise key")
  944. dev[0].dump_monitor()
  945. wrapped = aes_wrap(kek, 16*'z')
  946. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter,
  947. key_info=0x13ea)
  948. counter += 1
  949. send_eapol(dev[0], addr, build_eapol(msg))
  950. ev = dev[0].wait_event(["WPA: Ignored EAPOL-Key (Pairwise) with non-zero key index"])
  951. if ev is None:
  952. raise Exception("Non-zero key index not reported")
  953. logger.debug("Invalid Key Data plaintext payload --> disconnect")
  954. dev[0].dump_monitor()
  955. wrapped = aes_wrap(kek, 16*'z')
  956. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  957. counter += 1
  958. send_eapol(dev[0], addr, build_eapol(msg))
  959. dev[0].wait_disconnected(timeout=1)
  960. def test_ap_wpa2_psk_supp_proto_no_ie(dev, apdev):
  961. """WPA2-PSK supplicant protocol testing: IE not included"""
  962. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  963. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  964. msg = recv_eapol(hapd)
  965. dev[0].dump_monitor()
  966. # Build own EAPOL-Key msg 1/4
  967. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  968. counter = 1
  969. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  970. counter += 1
  971. send_eapol(dev[0], addr, build_eapol(msg))
  972. msg = recv_eapol(dev[0])
  973. snonce = msg['rsn_key_nonce']
  974. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  975. logger.debug("No IEs in msg 3/4 --> disconnect")
  976. dev[0].dump_monitor()
  977. wrapped = aes_wrap(kek, 16*'\0')
  978. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  979. counter += 1
  980. send_eapol(dev[0], addr, build_eapol(msg))
  981. dev[0].wait_disconnected(timeout=1)
  982. def test_ap_wpa2_psk_supp_proto_ie_mismatch(dev, apdev):
  983. """WPA2-PSK supplicant protocol testing: IE mismatch"""
  984. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  985. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  986. msg = recv_eapol(hapd)
  987. dev[0].dump_monitor()
  988. # Build own EAPOL-Key msg 1/4
  989. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  990. counter = 1
  991. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  992. counter += 1
  993. send_eapol(dev[0], addr, build_eapol(msg))
  994. msg = recv_eapol(dev[0])
  995. snonce = msg['rsn_key_nonce']
  996. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  997. logger.debug("Msg 3/4 with mismatching IE")
  998. dev[0].dump_monitor()
  999. wrapped = aes_wrap(kek, pad_key_data(binascii.unhexlify('30060100000fac04dd16000fac010100dc11188831bf4aa4a8678d2b41498618')))
  1000. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1001. counter += 1
  1002. send_eapol(dev[0], addr, build_eapol(msg))
  1003. dev[0].wait_disconnected(timeout=1)
  1004. def test_ap_wpa2_psk_supp_proto_ok(dev, apdev):
  1005. """WPA2-PSK supplicant protocol testing: success"""
  1006. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1007. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1008. msg = recv_eapol(hapd)
  1009. dev[0].dump_monitor()
  1010. # Build own EAPOL-Key msg 1/4
  1011. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1012. counter = 1
  1013. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1014. counter += 1
  1015. send_eapol(dev[0], addr, build_eapol(msg))
  1016. msg = recv_eapol(dev[0])
  1017. snonce = msg['rsn_key_nonce']
  1018. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1019. logger.debug("Valid EAPOL-Key msg 3/4")
  1020. dev[0].dump_monitor()
  1021. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010100dc11188831bf4aa4a8678d2b41498618')
  1022. wrapped = aes_wrap(kek, pad_key_data(plain))
  1023. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1024. counter += 1
  1025. send_eapol(dev[0], addr, build_eapol(msg))
  1026. dev[0].wait_connected(timeout=1)
  1027. def test_ap_wpa2_psk_supp_proto_no_gtk(dev, apdev):
  1028. """WPA2-PSK supplicant protocol testing: no GTK"""
  1029. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1030. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1031. msg = recv_eapol(hapd)
  1032. dev[0].dump_monitor()
  1033. # Build own EAPOL-Key msg 1/4
  1034. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1035. counter = 1
  1036. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1037. counter += 1
  1038. send_eapol(dev[0], addr, build_eapol(msg))
  1039. msg = recv_eapol(dev[0])
  1040. snonce = msg['rsn_key_nonce']
  1041. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1042. logger.debug("EAPOL-Key msg 3/4 without GTK KDE")
  1043. dev[0].dump_monitor()
  1044. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00')
  1045. wrapped = aes_wrap(kek, pad_key_data(plain))
  1046. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1047. counter += 1
  1048. send_eapol(dev[0], addr, build_eapol(msg))
  1049. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=0.1)
  1050. if ev is not None:
  1051. raise Exception("Unexpected connection completion reported")
  1052. def test_ap_wpa2_psk_supp_proto_anonce_change(dev, apdev):
  1053. """WPA2-PSK supplicant protocol testing: ANonce change"""
  1054. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1055. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1056. msg = recv_eapol(hapd)
  1057. dev[0].dump_monitor()
  1058. # Build own EAPOL-Key msg 1/4
  1059. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1060. counter = 1
  1061. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1062. counter += 1
  1063. send_eapol(dev[0], addr, build_eapol(msg))
  1064. msg = recv_eapol(dev[0])
  1065. snonce = msg['rsn_key_nonce']
  1066. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1067. logger.debug("Valid EAPOL-Key msg 3/4")
  1068. dev[0].dump_monitor()
  1069. anonce2 = binascii.unhexlify('3333333333333333333333333333333333333333333333333333333333333333')
  1070. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010100dc11188831bf4aa4a8678d2b41498618')
  1071. wrapped = aes_wrap(kek, pad_key_data(plain))
  1072. msg = build_eapol_key_3_4(anonce2, kck, wrapped, replay_counter=counter)
  1073. counter += 1
  1074. send_eapol(dev[0], addr, build_eapol(msg))
  1075. ev = dev[0].wait_event(["WPA: ANonce from message 1 of 4-Way Handshake differs from 3 of 4-Way Handshake"])
  1076. if ev is None:
  1077. raise Exception("ANonce change not reported")
  1078. def test_ap_wpa2_psk_supp_proto_unexpected_group_msg(dev, apdev):
  1079. """WPA2-PSK supplicant protocol testing: unexpected group message"""
  1080. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1081. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1082. msg = recv_eapol(hapd)
  1083. dev[0].dump_monitor()
  1084. # Build own EAPOL-Key msg 1/4
  1085. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1086. counter = 1
  1087. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1088. counter += 1
  1089. send_eapol(dev[0], addr, build_eapol(msg))
  1090. msg = recv_eapol(dev[0])
  1091. snonce = msg['rsn_key_nonce']
  1092. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1093. logger.debug("Group key 1/2 instead of msg 3/4")
  1094. dev[0].dump_monitor()
  1095. wrapped = aes_wrap(kek, binascii.unhexlify('dd16000fac010100dc11188831bf4aa4a8678d2b41498618'))
  1096. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter,
  1097. key_info=0x13c2)
  1098. counter += 1
  1099. send_eapol(dev[0], addr, build_eapol(msg))
  1100. ev = dev[0].wait_event(["WPA: Group Key Handshake started prior to completion of 4-way handshake"])
  1101. if ev is None:
  1102. raise Exception("Unexpected group key message not reported")
  1103. dev[0].wait_disconnected(timeout=1)
  1104. def test_ap_wpa2_psk_supp_proto_msg_1_invalid_kde(dev, apdev):
  1105. """WPA2-PSK supplicant protocol testing: invalid KDE in msg 1/4"""
  1106. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1107. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1108. msg = recv_eapol(hapd)
  1109. dev[0].dump_monitor()
  1110. # Build own EAPOL-Key msg 1/4 with invalid KDE
  1111. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1112. counter = 1
  1113. msg = build_eapol_key_1_4(anonce, replay_counter=counter,
  1114. key_data=binascii.unhexlify('5555'))
  1115. counter += 1
  1116. send_eapol(dev[0], addr, build_eapol(msg))
  1117. dev[0].wait_disconnected(timeout=1)
  1118. def test_ap_wpa2_psk_supp_proto_wrong_pairwise_key_len(dev, apdev):
  1119. """WPA2-PSK supplicant protocol testing: wrong pairwise key length"""
  1120. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1121. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1122. msg = recv_eapol(hapd)
  1123. dev[0].dump_monitor()
  1124. # Build own EAPOL-Key msg 1/4
  1125. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1126. counter = 1
  1127. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1128. counter += 1
  1129. send_eapol(dev[0], addr, build_eapol(msg))
  1130. msg = recv_eapol(dev[0])
  1131. snonce = msg['rsn_key_nonce']
  1132. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1133. logger.debug("Valid EAPOL-Key msg 3/4")
  1134. dev[0].dump_monitor()
  1135. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010100dc11188831bf4aa4a8678d2b41498618')
  1136. wrapped = aes_wrap(kek, pad_key_data(plain))
  1137. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter,
  1138. key_len=15)
  1139. counter += 1
  1140. send_eapol(dev[0], addr, build_eapol(msg))
  1141. ev = dev[0].wait_event(["WPA: Invalid CCMP key length 15"])
  1142. if ev is None:
  1143. raise Exception("Invalid CCMP key length not reported")
  1144. dev[0].wait_disconnected(timeout=1)
  1145. def test_ap_wpa2_psk_supp_proto_wrong_group_key_len(dev, apdev):
  1146. """WPA2-PSK supplicant protocol testing: wrong group key length"""
  1147. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1148. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1149. msg = recv_eapol(hapd)
  1150. dev[0].dump_monitor()
  1151. # Build own EAPOL-Key msg 1/4
  1152. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1153. counter = 1
  1154. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1155. counter += 1
  1156. send_eapol(dev[0], addr, build_eapol(msg))
  1157. msg = recv_eapol(dev[0])
  1158. snonce = msg['rsn_key_nonce']
  1159. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1160. logger.debug("Valid EAPOL-Key msg 3/4")
  1161. dev[0].dump_monitor()
  1162. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd15000fac010100dc11188831bf4aa4a8678d2b414986')
  1163. wrapped = aes_wrap(kek, pad_key_data(plain))
  1164. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1165. counter += 1
  1166. send_eapol(dev[0], addr, build_eapol(msg))
  1167. ev = dev[0].wait_event(["WPA: Unsupported CCMP Group Cipher key length 15"])
  1168. if ev is None:
  1169. raise Exception("Invalid CCMP key length not reported")
  1170. dev[0].wait_disconnected(timeout=1)
  1171. def test_ap_wpa2_psk_supp_proto_gtk_tx_bit_workaround(dev, apdev):
  1172. """WPA2-PSK supplicant protocol testing: GTK TX bit workaround"""
  1173. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1174. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1175. msg = recv_eapol(hapd)
  1176. dev[0].dump_monitor()
  1177. # Build own EAPOL-Key msg 1/4
  1178. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1179. counter = 1
  1180. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1181. counter += 1
  1182. send_eapol(dev[0], addr, build_eapol(msg))
  1183. msg = recv_eapol(dev[0])
  1184. snonce = msg['rsn_key_nonce']
  1185. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1186. logger.debug("Valid EAPOL-Key msg 3/4")
  1187. dev[0].dump_monitor()
  1188. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010500dc11188831bf4aa4a8678d2b41498618')
  1189. wrapped = aes_wrap(kek, pad_key_data(plain))
  1190. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1191. counter += 1
  1192. send_eapol(dev[0], addr, build_eapol(msg))
  1193. ev = dev[0].wait_event(["WPA: Tx bit set for GTK, but pairwise keys are used - ignore Tx bit"])
  1194. if ev is None:
  1195. raise Exception("GTK Tx bit workaround not reported")
  1196. dev[0].wait_connected(timeout=1)
  1197. def test_ap_wpa2_psk_supp_proto_gtk_keyidx_0_and_3(dev, apdev):
  1198. """WPA2-PSK supplicant protocol testing: GTK key index 0 and 3"""
  1199. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1200. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1201. msg = recv_eapol(hapd)
  1202. dev[0].dump_monitor()
  1203. # Build own EAPOL-Key msg 1/4
  1204. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1205. counter = 1
  1206. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1207. counter += 1
  1208. send_eapol(dev[0], addr, build_eapol(msg))
  1209. msg = recv_eapol(dev[0])
  1210. snonce = msg['rsn_key_nonce']
  1211. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1212. logger.debug("Valid EAPOL-Key msg 3/4 (GTK keyidx 0)")
  1213. dev[0].dump_monitor()
  1214. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010000dc11188831bf4aa4a8678d2b41498618')
  1215. wrapped = aes_wrap(kek, pad_key_data(plain))
  1216. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1217. counter += 1
  1218. send_eapol(dev[0], addr, build_eapol(msg))
  1219. dev[0].wait_connected(timeout=1)
  1220. logger.debug("Valid EAPOL-Key group msg 1/2 (GTK keyidx 3)")
  1221. dev[0].dump_monitor()
  1222. plain = binascii.unhexlify('dd16000fac010300dc11188831bf4aa4a8678d2b41498618')
  1223. wrapped = aes_wrap(kek, pad_key_data(plain))
  1224. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter,
  1225. key_info=0x13c2)
  1226. counter += 1
  1227. send_eapol(dev[0], addr, build_eapol(msg))
  1228. msg = recv_eapol(dev[0])
  1229. ev = dev[0].wait_event(["WPA: Group rekeying completed"])
  1230. if ev is None:
  1231. raise Exception("GTK rekeing not reported")
  1232. logger.debug("Unencrypted GTK KDE in group msg 1/2")
  1233. dev[0].dump_monitor()
  1234. plain = binascii.unhexlify('dd16000fac010300dc11188831bf4aa4a8678d2b41498618')
  1235. msg = build_eapol_key_3_4(anonce, kck, plain, replay_counter=counter,
  1236. key_info=0x03c2)
  1237. counter += 1
  1238. send_eapol(dev[0], addr, build_eapol(msg))
  1239. ev = dev[0].wait_event(["WPA: GTK IE in unencrypted key data"])
  1240. if ev is None:
  1241. raise Exception("Unencrypted GTK KDE not reported")
  1242. dev[0].wait_disconnected(timeout=1)
  1243. def test_ap_wpa2_psk_supp_proto_no_gtk_in_group_msg(dev, apdev):
  1244. """WPA2-PSK supplicant protocol testing: GTK KDE missing from group msg"""
  1245. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1246. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1247. msg = recv_eapol(hapd)
  1248. dev[0].dump_monitor()
  1249. # Build own EAPOL-Key msg 1/4
  1250. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1251. counter = 1
  1252. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1253. counter += 1
  1254. send_eapol(dev[0], addr, build_eapol(msg))
  1255. msg = recv_eapol(dev[0])
  1256. snonce = msg['rsn_key_nonce']
  1257. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1258. logger.debug("Valid EAPOL-Key msg 3/4 (GTK keyidx 0)")
  1259. dev[0].dump_monitor()
  1260. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010000dc11188831bf4aa4a8678d2b41498618')
  1261. wrapped = aes_wrap(kek, pad_key_data(plain))
  1262. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1263. counter += 1
  1264. send_eapol(dev[0], addr, build_eapol(msg))
  1265. dev[0].wait_connected(timeout=1)
  1266. logger.debug("No GTK KDE in EAPOL-Key group msg 1/2")
  1267. dev[0].dump_monitor()
  1268. plain = binascii.unhexlify('dd00dd00dd00dd00dd00dd00dd00dd00')
  1269. wrapped = aes_wrap(kek, pad_key_data(plain))
  1270. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter,
  1271. key_info=0x13c2)
  1272. counter += 1
  1273. send_eapol(dev[0], addr, build_eapol(msg))
  1274. ev = dev[0].wait_event(["WPA: No GTK IE in Group Key msg 1/2"])
  1275. if ev is None:
  1276. raise Exception("Missing GTK KDE not reported")
  1277. dev[0].wait_disconnected(timeout=1)
  1278. def test_ap_wpa2_psk_supp_proto_too_long_gtk_in_group_msg(dev, apdev):
  1279. """WPA2-PSK supplicant protocol testing: too long GTK KDE in group msg"""
  1280. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1281. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1282. msg = recv_eapol(hapd)
  1283. dev[0].dump_monitor()
  1284. # Build own EAPOL-Key msg 1/4
  1285. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1286. counter = 1
  1287. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1288. counter += 1
  1289. send_eapol(dev[0], addr, build_eapol(msg))
  1290. msg = recv_eapol(dev[0])
  1291. snonce = msg['rsn_key_nonce']
  1292. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1293. logger.debug("Valid EAPOL-Key msg 3/4 (GTK keyidx 0)")
  1294. dev[0].dump_monitor()
  1295. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010000dc11188831bf4aa4a8678d2b41498618')
  1296. wrapped = aes_wrap(kek, pad_key_data(plain))
  1297. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1298. counter += 1
  1299. send_eapol(dev[0], addr, build_eapol(msg))
  1300. dev[0].wait_connected(timeout=1)
  1301. logger.debug("EAPOL-Key group msg 1/2 with too long GTK KDE")
  1302. dev[0].dump_monitor()
  1303. plain = binascii.unhexlify('dd27000fac010100ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff')
  1304. wrapped = aes_wrap(kek, pad_key_data(plain))
  1305. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter,
  1306. key_info=0x13c2)
  1307. counter += 1
  1308. send_eapol(dev[0], addr, build_eapol(msg))
  1309. ev = dev[0].wait_event(["WPA: Unsupported CCMP Group Cipher key length 33"])
  1310. if ev is None:
  1311. raise Exception("Too long GTK KDE not reported")
  1312. dev[0].wait_disconnected(timeout=1)
  1313. def test_ap_wpa2_psk_supp_proto_too_long_gtk_kde(dev, apdev):
  1314. """WPA2-PSK supplicant protocol testing: too long GTK KDE"""
  1315. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1316. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1317. msg = recv_eapol(hapd)
  1318. dev[0].dump_monitor()
  1319. # Build own EAPOL-Key msg 1/4
  1320. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1321. counter = 1
  1322. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1323. counter += 1
  1324. send_eapol(dev[0], addr, build_eapol(msg))
  1325. msg = recv_eapol(dev[0])
  1326. snonce = msg['rsn_key_nonce']
  1327. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1328. logger.debug("EAPOL-Key msg 3/4 with too short GTK KDE")
  1329. dev[0].dump_monitor()
  1330. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd27000fac010100ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff')
  1331. wrapped = aes_wrap(kek, pad_key_data(plain))
  1332. msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter)
  1333. counter += 1
  1334. send_eapol(dev[0], addr, build_eapol(msg))
  1335. dev[0].wait_disconnected(timeout=1)
  1336. def test_ap_wpa2_psk_supp_proto_gtk_not_encrypted(dev, apdev):
  1337. """WPA2-PSK supplicant protocol testing: GTK KDE not encrypted"""
  1338. (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0])
  1339. # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated
  1340. msg = recv_eapol(hapd)
  1341. dev[0].dump_monitor()
  1342. # Build own EAPOL-Key msg 1/4
  1343. anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222')
  1344. counter = 1
  1345. msg = build_eapol_key_1_4(anonce, replay_counter=counter)
  1346. counter += 1
  1347. send_eapol(dev[0], addr, build_eapol(msg))
  1348. msg = recv_eapol(dev[0])
  1349. snonce = msg['rsn_key_nonce']
  1350. (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce)
  1351. logger.debug("Valid EAPOL-Key msg 3/4")
  1352. dev[0].dump_monitor()
  1353. plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010100dc11188831bf4aa4a8678d2b41498618')
  1354. msg = build_eapol_key_3_4(anonce, kck, plain, replay_counter=counter,
  1355. key_info=0x03ca)
  1356. counter += 1
  1357. send_eapol(dev[0], addr, build_eapol(msg))
  1358. ev = dev[0].wait_event(["WPA: GTK IE in unencrypted key data"])
  1359. if ev is None:
  1360. raise Exception("Unencrypted GTK KDE not reported")
  1361. dev[0].wait_disconnected(timeout=1)
  1362. def find_wpas_process(dev):
  1363. ifname = dev.ifname
  1364. cmd = subprocess.Popen(['ps', 'ax'], stdout=subprocess.PIPE)
  1365. (data,err) = cmd.communicate()
  1366. for l in data.splitlines():
  1367. if "wpa_supplicant" not in l:
  1368. continue
  1369. if "-i" + ifname not in l:
  1370. continue
  1371. return int(l.strip().split(' ')[0])
  1372. raise Exception("Could not find wpa_supplicant process")
  1373. def read_process_memory(pid, key=None):
  1374. buf = bytes()
  1375. with open('/proc/%d/maps' % pid, 'r') as maps, \
  1376. open('/proc/%d/mem' % pid, 'r') as mem:
  1377. for l in maps.readlines():
  1378. m = re.match(r'([0-9a-f]+)-([0-9a-f]+) ([-r][-w][-x][-p])', l)
  1379. if not m:
  1380. continue
  1381. start = int(m.group(1), 16)
  1382. end = int(m.group(2), 16)
  1383. perm = m.group(3)
  1384. if start > 0xffffffffffff:
  1385. continue
  1386. if end < start:
  1387. continue
  1388. if not perm.startswith('rw'):
  1389. continue
  1390. mem.seek(start)
  1391. data = mem.read(end - start)
  1392. buf += data
  1393. if key and key in data:
  1394. logger.info("Key found in " + l)
  1395. return buf
  1396. def verify_not_present(buf, key, fname, keyname):
  1397. pos = buf.find(key)
  1398. if pos < 0:
  1399. return
  1400. prefix = 2048 if pos > 2048 else pos
  1401. with open(fname + keyname, 'w') as f:
  1402. f.write(buf[pos - prefix:pos + 2048])
  1403. raise Exception(keyname + " found after disassociation")
  1404. def get_key_locations(buf, key, keyname):
  1405. count = 0
  1406. pos = 0
  1407. while True:
  1408. pos = buf.find(key, pos)
  1409. if pos < 0:
  1410. break
  1411. logger.info("Found %s at %d" % (keyname, pos))
  1412. count += 1
  1413. pos += len(key)
  1414. return count
  1415. def test_wpa2_psk_key_lifetime_in_memory(dev, apdev, params):
  1416. """WPA2-PSK and PSK/PTK lifetime in memory"""
  1417. ssid = "test-wpa2-psk"
  1418. passphrase = 'qwertyuiop'
  1419. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  1420. pmk = binascii.unhexlify(psk)
  1421. p = hostapd.wpa2_params(ssid=ssid)
  1422. p['wpa_psk'] = psk
  1423. hapd = hostapd.add_ap(apdev[0]['ifname'], p)
  1424. pid = find_wpas_process(dev[0])
  1425. id = dev[0].connect(ssid, raw_psk=psk, scan_freq="2412",
  1426. only_add_network=True)
  1427. logger.info("Checking keys in memory after network profile configuration")
  1428. buf = read_process_memory(pid, pmk)
  1429. get_key_locations(buf, pmk, "PMK")
  1430. dev[0].request("REMOVE_NETWORK all")
  1431. logger.info("Checking keys in memory after network profile removal")
  1432. buf = read_process_memory(pid, pmk)
  1433. get_key_locations(buf, pmk, "PMK")
  1434. id = dev[0].connect(ssid, psk=passphrase, scan_freq="2412",
  1435. only_add_network=True)
  1436. logger.info("Checking keys in memory before connection")
  1437. buf = read_process_memory(pid, pmk)
  1438. get_key_locations(buf, pmk, "PMK")
  1439. dev[0].connect_network(id, timeout=20)
  1440. time.sleep(1)
  1441. buf = read_process_memory(pid, pmk)
  1442. dev[0].request("DISCONNECT")
  1443. dev[0].wait_disconnected()
  1444. dev[0].relog()
  1445. ptk = None
  1446. gtk = None
  1447. with open(os.path.join(params['logdir'], 'log0'), 'r') as f:
  1448. for l in f.readlines():
  1449. if "WPA: PTK - hexdump" in l:
  1450. val = l.strip().split(':')[3].replace(' ', '')
  1451. ptk = binascii.unhexlify(val)
  1452. if "WPA: Group Key - hexdump" in l:
  1453. val = l.strip().split(':')[3].replace(' ', '')
  1454. gtk = binascii.unhexlify(val)
  1455. if not pmk or not ptk or not gtk:
  1456. raise Exception("Could not find keys from debug log")
  1457. if len(gtk) != 16:
  1458. raise Exception("Unexpected GTK length")
  1459. kck = ptk[0:16]
  1460. kek = ptk[16:32]
  1461. tk = ptk[32:48]
  1462. logger.info("Checking keys in memory while associated")
  1463. get_key_locations(buf, pmk, "PMK")
  1464. if pmk not in buf:
  1465. raise HwsimSkip("PMK not found while associated")
  1466. if kck not in buf:
  1467. raise Exception("KCK not found while associated")
  1468. if kek not in buf:
  1469. raise Exception("KEK not found while associated")
  1470. if tk in buf:
  1471. raise Exception("TK found from memory")
  1472. if gtk in buf:
  1473. raise Exception("GTK found from memory")
  1474. logger.info("Checking keys in memory after disassociation")
  1475. buf = read_process_memory(pid, pmk)
  1476. get_key_locations(buf, pmk, "PMK")
  1477. # Note: PMK/PSK is still present in network configuration
  1478. fname = os.path.join(params['logdir'],
  1479. 'wpa2_psk_key_lifetime_in_memory.memctx-')
  1480. verify_not_present(buf, kck, fname, "KCK")
  1481. verify_not_present(buf, kek, fname, "KEK")
  1482. verify_not_present(buf, tk, fname, "TK")
  1483. verify_not_present(buf, gtk, fname, "GTK")
  1484. dev[0].request("REMOVE_NETWORK all")
  1485. logger.info("Checking keys in memory after network profile removal")
  1486. buf = read_process_memory(pid, pmk)
  1487. get_key_locations(buf, pmk, "PMK")
  1488. verify_not_present(buf, pmk, fname, "PMK")
  1489. verify_not_present(buf, kck, fname, "KCK")
  1490. verify_not_present(buf, kek, fname, "KEK")
  1491. verify_not_present(buf, tk, fname, "TK")
  1492. verify_not_present(buf, gtk, fname, "GTK")
  1493. def test_ap_wpa2_psk_wep(dev, apdev):
  1494. """WPA2-PSK AP and WEP enabled"""
  1495. ssid = "test-wpa2-psk"
  1496. passphrase = 'qwertyuiop'
  1497. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  1498. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  1499. try:
  1500. hapd.set('wep_key0', '"hello"')
  1501. raise Exception("WEP key accepted to WPA2 network")
  1502. except Exception:
  1503. pass
  1504. def test_ap_wpa2_psk_wpas_in_bridge(dev, apdev):
  1505. """WPA2-PSK AP and wpas interface in a bridge"""
  1506. br_ifname='sta-br0'
  1507. ifname='wlan5'
  1508. try:
  1509. _test_ap_wpa2_psk_wpas_in_bridge(dev, apdev)
  1510. finally:
  1511. subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'down'])
  1512. subprocess.call(['brctl', 'delif', br_ifname, ifname])
  1513. subprocess.call(['brctl', 'delbr', br_ifname])
  1514. subprocess.call(['iw', ifname, 'set', '4addr', 'off'])
  1515. def _test_ap_wpa2_psk_wpas_in_bridge(dev, apdev):
  1516. ssid = "test-wpa2-psk"
  1517. passphrase = 'qwertyuiop'
  1518. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  1519. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  1520. br_ifname='sta-br0'
  1521. ifname='wlan5'
  1522. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  1523. subprocess.call(['brctl', 'addbr', br_ifname])
  1524. subprocess.call(['brctl', 'setfd', br_ifname, '0'])
  1525. subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'up'])
  1526. subprocess.call(['iw', ifname, 'set', '4addr', 'on'])
  1527. subprocess.check_call(['brctl', 'addif', br_ifname, ifname])
  1528. wpas.interface_add(ifname, br_ifname=br_ifname)
  1529. wpas.connect(ssid, psk=passphrase, scan_freq="2412")
  1530. def test_ap_wpa2_psk_ifdown(dev, apdev):
  1531. """AP with open mode and external ifconfig down"""
  1532. ssid = "test-wpa2-psk"
  1533. passphrase = 'qwertyuiop'
  1534. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  1535. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  1536. bssid = apdev[0]['bssid']
  1537. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  1538. subprocess.call(['ip', 'link', 'set', 'dev', apdev[0]['ifname'], 'down'])
  1539. ev = hapd.wait_event(["INTERFACE-DISABLED"], timeout=10)
  1540. if ev is None:
  1541. raise Exception("No INTERFACE-DISABLED event")
  1542. # this wait tests beacon loss detection in mac80211
  1543. dev[0].wait_disconnected()
  1544. subprocess.call(['ip', 'link', 'set', 'dev', apdev[0]['ifname'], 'up'])
  1545. ev = hapd.wait_event(["INTERFACE-ENABLED"], timeout=10)
  1546. if ev is None:
  1547. raise Exception("No INTERFACE-ENABLED event")
  1548. dev[0].wait_connected()
  1549. hwsim_utils.test_connectivity(dev[0], hapd)
  1550. def test_ap_wpa2_psk_drop_first_msg_4(dev, apdev):
  1551. """WPA2-PSK and first EAPOL-Key msg 4/4 dropped"""
  1552. bssid = apdev[0]['bssid']
  1553. ssid = "test-wpa2-psk"
  1554. passphrase = 'qwertyuiop'
  1555. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  1556. params = hostapd.wpa2_params(ssid=ssid)
  1557. params['wpa_psk'] = psk
  1558. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  1559. hapd.request("SET ext_eapol_frame_io 1")
  1560. dev[0].request("SET ext_eapol_frame_io 1")
  1561. dev[0].connect(ssid, psk=passphrase, scan_freq="2412", wait_connect=False)
  1562. addr = dev[0].own_addr()
  1563. # EAPOL-Key msg 1/4
  1564. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  1565. if ev is None:
  1566. raise Exception("Timeout on EAPOL-TX from hostapd")
  1567. res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2])
  1568. if "OK" not in res:
  1569. raise Exception("EAPOL_RX to wpa_supplicant failed")
  1570. # EAPOL-Key msg 2/4
  1571. ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
  1572. if ev is None:
  1573. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  1574. res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2])
  1575. if "OK" not in res:
  1576. raise Exception("EAPOL_RX to hostapd failed")
  1577. # EAPOL-Key msg 3/4
  1578. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  1579. if ev is None:
  1580. raise Exception("Timeout on EAPOL-TX from hostapd")
  1581. res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2])
  1582. if "OK" not in res:
  1583. raise Exception("EAPOL_RX to wpa_supplicant failed")
  1584. # EAPOL-Key msg 4/4
  1585. ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
  1586. if ev is None:
  1587. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  1588. logger.info("Drop the first EAPOL-Key msg 4/4")
  1589. # wpa_supplicant believes now that 4-way handshake succeeded; hostapd
  1590. # doesn't. Use normal EAPOL TX/RX to handle retries.
  1591. hapd.request("SET ext_eapol_frame_io 0")
  1592. dev[0].request("SET ext_eapol_frame_io 0")
  1593. dev[0].wait_connected()
  1594. ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=15)
  1595. if ev is None:
  1596. raise Exception("Timeout on AP-STA-CONNECTED from hostapd")
  1597. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=0.1)
  1598. if ev is not None:
  1599. logger.info("Disconnection detected")
  1600. # The EAPOL-Key retries are supposed to allow the connection to be
  1601. # established without having to reassociate. However, this does not
  1602. # currently work since mac80211 ends up encrypting EAPOL-Key msg 4/4
  1603. # after the pairwise key has been configured and AP will drop those and
  1604. # disconnect the station after reaching retransmission limit. Connection
  1605. # is then established after reassociation. Once that behavior has been
  1606. # optimized to prevent EAPOL-Key frame encryption for retransmission
  1607. # case, this exception can be uncommented here.
  1608. #raise Exception("Unexpected disconnection")
  1609. def test_ap_wpa2_psk_disable_enable(dev, apdev):
  1610. """WPA2-PSK AP getting disabled and re-enabled"""
  1611. ssid = "test-wpa2-psk"
  1612. passphrase = 'qwertyuiop'
  1613. psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
  1614. params = hostapd.wpa2_params(ssid=ssid)
  1615. params['wpa_psk'] = psk
  1616. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  1617. dev[0].connect(ssid, raw_psk=psk, scan_freq="2412")
  1618. for i in range(2):
  1619. hapd.request("DISABLE")
  1620. dev[0].wait_disconnected()
  1621. hapd.request("ENABLE")
  1622. dev[0].wait_connected()
  1623. hwsim_utils.test_connectivity(dev[0], hapd)
  1624. def test_ap_wpa2_psk_incorrect_passphrase(dev, apdev):
  1625. """WPA2-PSK AP and station using incorrect passphrase"""
  1626. ssid = "test-wpa2-psk"
  1627. passphrase = 'qwertyuiop'
  1628. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  1629. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  1630. dev[0].connect(ssid, psk="incorrect passphrase", scan_freq="2412",
  1631. wait_connect=False)
  1632. ev = hapd.wait_event(["AP-STA-POSSIBLE-PSK-MISMATCH"], timeout=10)
  1633. if ev is None:
  1634. raise Exception("No AP-STA-POSSIBLE-PSK-MISMATCH reported")
  1635. dev[0].dump_monitor()
  1636. hapd.disable()
  1637. hapd.set("wpa_passphrase", "incorrect passphrase")
  1638. hapd.enable()
  1639. dev[0].wait_connected(timeout=20)