test_ap_psk.py 85 KB

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