test_ap_psk.py 114 KB

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