test_ap_psk.py 79 KB

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