test_ieee8021x.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. # IEEE 802.1X tests
  2. # Copyright (c) 2013-2015, Jouni Malinen <j@w1.fi>
  3. #
  4. # This software may be distributed under the terms of the BSD license.
  5. # See README for more details.
  6. import binascii
  7. import hmac
  8. import logging
  9. import time
  10. import hostapd
  11. import hwsim_utils
  12. from utils import skip_with_fips
  13. logger = logging.getLogger()
  14. def test_ieee8021x_wep104(dev, apdev):
  15. """IEEE 802.1X connection using dynamic WEP104"""
  16. skip_with_fips(dev[0])
  17. params = hostapd.radius_params()
  18. params["ssid"] = "ieee8021x-wep"
  19. params["ieee8021x"] = "1"
  20. params["wep_key_len_broadcast"] = "13"
  21. params["wep_key_len_unicast"] = "13"
  22. hapd = hostapd.add_ap(apdev[0], params)
  23. dev[0].connect("ieee8021x-wep", key_mgmt="IEEE8021X", eap="PSK",
  24. identity="psk.user@example.com",
  25. password_hex="0123456789abcdef0123456789abcdef",
  26. scan_freq="2412")
  27. hwsim_utils.test_connectivity(dev[0], hapd)
  28. def test_ieee8021x_wep40(dev, apdev):
  29. """IEEE 802.1X connection using dynamic WEP40"""
  30. skip_with_fips(dev[0])
  31. params = hostapd.radius_params()
  32. params["ssid"] = "ieee8021x-wep"
  33. params["ieee8021x"] = "1"
  34. params["wep_key_len_broadcast"] = "5"
  35. params["wep_key_len_unicast"] = "5"
  36. hapd = hostapd.add_ap(apdev[0], params)
  37. dev[0].connect("ieee8021x-wep", key_mgmt="IEEE8021X", eap="PSK",
  38. identity="psk.user@example.com",
  39. password_hex="0123456789abcdef0123456789abcdef",
  40. scan_freq="2412")
  41. hwsim_utils.test_connectivity(dev[0], hapd)
  42. def test_ieee8021x_open(dev, apdev):
  43. """IEEE 802.1X connection using open network"""
  44. params = hostapd.radius_params()
  45. params["ssid"] = "ieee8021x-open"
  46. params["ieee8021x"] = "1"
  47. hapd = hostapd.add_ap(apdev[0], params)
  48. id = dev[0].connect("ieee8021x-open", key_mgmt="IEEE8021X", eapol_flags="0",
  49. eap="PSK", identity="psk.user@example.com",
  50. password_hex="0123456789abcdef0123456789abcdef",
  51. scan_freq="2412")
  52. hwsim_utils.test_connectivity(dev[0], hapd)
  53. logger.info("Test EAPOL-Logoff")
  54. dev[0].request("LOGOFF")
  55. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  56. if ev is None:
  57. raise Exception("Did not get disconnected")
  58. if "reason=23" not in ev:
  59. raise Exception("Unexpected disconnection reason")
  60. dev[0].request("LOGON")
  61. dev[0].connect_network(id)
  62. hwsim_utils.test_connectivity(dev[0], hapd)
  63. def test_ieee8021x_static_wep40(dev, apdev):
  64. """IEEE 802.1X connection using static WEP40"""
  65. params = hostapd.radius_params()
  66. params["ssid"] = "ieee8021x-wep"
  67. params["ieee8021x"] = "1"
  68. params["wep_key0"] = '"hello"'
  69. hapd = hostapd.add_ap(apdev[0], params)
  70. dev[0].connect("ieee8021x-wep", key_mgmt="IEEE8021X", eap="PSK",
  71. identity="psk.user@example.com",
  72. password_hex="0123456789abcdef0123456789abcdef",
  73. wep_key0='"hello"', eapol_flags="0",
  74. scan_freq="2412")
  75. hwsim_utils.test_connectivity(dev[0], hapd)
  76. def test_ieee8021x_proto(dev, apdev):
  77. """IEEE 802.1X and EAPOL supplicant protocol testing"""
  78. params = hostapd.radius_params()
  79. params["ssid"] = "ieee8021x-open"
  80. params["ieee8021x"] = "1"
  81. hapd = hostapd.add_ap(apdev[0], params)
  82. bssid = apdev[0]['bssid']
  83. dev[1].request("SET ext_eapol_frame_io 1")
  84. dev[1].connect("ieee8021x-open", key_mgmt="IEEE8021X", eapol_flags="0",
  85. eap="PSK", identity="psk.user@example.com",
  86. password_hex="0123456789abcdef0123456789abcdef",
  87. scan_freq="2412", wait_connect=False)
  88. id = dev[0].connect("ieee8021x-open", key_mgmt="IEEE8021X", eapol_flags="0",
  89. eap="PSK", identity="psk.user@example.com",
  90. password_hex="0123456789abcdef0123456789abcdef",
  91. scan_freq="2412")
  92. ev = dev[1].wait_event(["CTRL-EVENT-EAP-STARTED"], timeout=5)
  93. start = dev[0].get_mib()
  94. tests = [ "11",
  95. "11223344",
  96. "020000050a93000501",
  97. "020300050a93000501",
  98. "0203002c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  99. "0203002c0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  100. "0203002c0100050000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  101. "02aa00050a93000501" ]
  102. for frame in tests:
  103. res = dev[0].request("EAPOL_RX " + bssid + " " + frame)
  104. if "OK" not in res:
  105. raise Exception("EAPOL_RX to wpa_supplicant failed")
  106. dev[1].request("EAPOL_RX " + bssid + " " + frame)
  107. stop = dev[0].get_mib()
  108. logger.info("MIB before test frames: " + str(start))
  109. logger.info("MIB after test frames: " + str(stop))
  110. vals = [ 'dot1xSuppInvalidEapolFramesRx',
  111. 'dot1xSuppEapLengthErrorFramesRx' ]
  112. for val in vals:
  113. if int(stop[val]) <= int(start[val]):
  114. raise Exception(val + " did not increase")
  115. def test_ieee8021x_eapol_start(dev, apdev):
  116. """IEEE 802.1X and EAPOL-Start retransmissions"""
  117. params = hostapd.radius_params()
  118. params["ssid"] = "ieee8021x-open"
  119. params["ieee8021x"] = "1"
  120. hapd = hostapd.add_ap(apdev[0], params)
  121. bssid = apdev[0]['bssid']
  122. addr0 = dev[0].own_addr()
  123. hapd.set("ext_eapol_frame_io", "1")
  124. try:
  125. dev[0].request("SET EAPOL::startPeriod 1")
  126. dev[0].request("SET EAPOL::maxStart 1")
  127. dev[0].connect("ieee8021x-open", key_mgmt="IEEE8021X", eapol_flags="0",
  128. eap="PSK", identity="psk.user@example.com",
  129. password_hex="0123456789abcdef0123456789abcdef",
  130. scan_freq="2412", wait_connect=False)
  131. held = False
  132. for i in range(30):
  133. pae = dev[0].get_status_field('Supplicant PAE state')
  134. if pae == "HELD":
  135. mib = hapd.get_sta(addr0, info="eapol")
  136. if mib['auth_pae_state'] != 'AUTHENTICATING':
  137. raise Exception("Unexpected Auth PAE state: " + mib['auth_pae_state'])
  138. held = True
  139. break
  140. time.sleep(0.25)
  141. if not held:
  142. raise Exception("PAE state HELD not reached")
  143. dev[0].wait_disconnected()
  144. finally:
  145. dev[0].request("SET EAPOL::startPeriod 30")
  146. dev[0].request("SET EAPOL::maxStart 3")
  147. def test_ieee8021x_held(dev, apdev):
  148. """IEEE 802.1X and HELD state"""
  149. params = hostapd.radius_params()
  150. params["ssid"] = "ieee8021x-open"
  151. params["ieee8021x"] = "1"
  152. hapd = hostapd.add_ap(apdev[0], params)
  153. bssid = apdev[0]['bssid']
  154. hapd.set("ext_eapol_frame_io", "1")
  155. try:
  156. dev[0].request("SET EAPOL::startPeriod 1")
  157. dev[0].request("SET EAPOL::maxStart 0")
  158. dev[0].request("SET EAPOL::heldPeriod 1")
  159. dev[0].connect("ieee8021x-open", key_mgmt="IEEE8021X", eapol_flags="0",
  160. eap="PSK", identity="psk.user@example.com",
  161. password_hex="0123456789abcdef0123456789abcdef",
  162. scan_freq="2412", wait_connect=False)
  163. held = False
  164. for i in range(30):
  165. pae = dev[0].get_status_field('Supplicant PAE state')
  166. if pae == "HELD":
  167. held = True
  168. break
  169. time.sleep(0.25)
  170. if not held:
  171. raise Exception("PAE state HELD not reached")
  172. hapd.set("ext_eapol_frame_io", "0")
  173. for i in range(30):
  174. pae = dev[0].get_status_field('Supplicant PAE state')
  175. if pae != "HELD":
  176. held = False
  177. break
  178. time.sleep(0.25)
  179. if held:
  180. raise Exception("PAE state HELD not left")
  181. ev = dev[0].wait_event([ "CTRL-EVENT-CONNECTED",
  182. "CTRL-EVENT-DISCONNECTED" ], timeout=10)
  183. if ev is None:
  184. raise Exception("Connection timed out")
  185. if "CTRL-EVENT-DISCONNECTED" in ev:
  186. raise Exception("Unexpected disconnection")
  187. finally:
  188. dev[0].request("SET EAPOL::startPeriod 30")
  189. dev[0].request("SET EAPOL::maxStart 3")
  190. dev[0].request("SET EAPOL::heldPeriod 60")
  191. def send_eapol_key(dev, bssid, signkey, frame_start, frame_end):
  192. zero_sign = "00000000000000000000000000000000"
  193. frame = frame_start + zero_sign + frame_end
  194. hmac_obj = hmac.new(binascii.unhexlify(signkey))
  195. hmac_obj.update(binascii.unhexlify(frame))
  196. sign = hmac_obj.digest()
  197. frame = frame_start + binascii.hexlify(sign) + frame_end
  198. dev.request("EAPOL_RX " + bssid + " " + frame)
  199. def test_ieee8021x_eapol_key(dev, apdev):
  200. """IEEE 802.1X connection and EAPOL-Key protocol tests"""
  201. skip_with_fips(dev[0])
  202. params = hostapd.radius_params()
  203. params["ssid"] = "ieee8021x-wep"
  204. params["ieee8021x"] = "1"
  205. params["wep_key_len_broadcast"] = "5"
  206. params["wep_key_len_unicast"] = "5"
  207. hapd = hostapd.add_ap(apdev[0], params)
  208. bssid = apdev[0]['bssid']
  209. dev[0].connect("ieee8021x-wep", key_mgmt="IEEE8021X", eap="VENDOR-TEST",
  210. identity="vendor-test", scan_freq="2412")
  211. # Hardcoded MSK from VENDOR-TEST
  212. encrkey = "1111111111111111111111111111111111111111111111111111111111111111"
  213. signkey = "2222222222222222222222222222222222222222222222222222222222222222"
  214. # EAPOL-Key replay counter does not increase
  215. send_eapol_key(dev[0], bssid, signkey,
  216. "02030031" + "010005" + "0000000000000000" + "056c22d109f29d4d9fb9b9ccbad33283" + "02",
  217. "1c636a30a4")
  218. # EAPOL-Key too large Key Length field value
  219. send_eapol_key(dev[0], bssid, signkey,
  220. "02030031" + "010021" + "ffffffffffffffff" + "056c22d109f29d4d9fb9b9ccbad33283" + "02",
  221. "1c636a30a4")
  222. # EAPOL-Key too much key data
  223. send_eapol_key(dev[0], bssid, signkey,
  224. "0203004d" + "010005" + "ffffffffffffffff" + "056c22d109f29d4d9fb9b9ccbad33283" + "02",
  225. 33*"ff")
  226. # EAPOL-Key too little key data
  227. send_eapol_key(dev[0], bssid, signkey,
  228. "02030030" + "010005" + "ffffffffffffffff" + "056c22d109f29d4d9fb9b9ccbad33283" + "02",
  229. "1c636a30")
  230. # EAPOL-Key with no key data and too long WEP key length
  231. send_eapol_key(dev[0], bssid, signkey,
  232. "0203002c" + "010020" + "ffffffffffffffff" + "056c22d109f29d4d9fb9b9ccbad33283" + "02",
  233. "")
  234. def test_ieee8021x_reauth(dev, apdev):
  235. """IEEE 802.1X and EAPOL_REAUTH request"""
  236. params = hostapd.radius_params()
  237. params["ssid"] = "ieee8021x-open"
  238. params["ieee8021x"] = "1"
  239. hapd = hostapd.add_ap(apdev[0], params)
  240. dev[0].connect("ieee8021x-open", key_mgmt="IEEE8021X", eapol_flags="0",
  241. eap="PSK", identity="psk.user@example.com",
  242. password_hex="0123456789abcdef0123456789abcdef",
  243. scan_freq="2412")
  244. hapd.request("EAPOL_REAUTH " + dev[0].own_addr())
  245. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED"], timeout=5)
  246. if ev is None:
  247. raise Exception("EAP authentication did not start")
  248. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=5)
  249. if ev is None:
  250. raise Exception("EAP authentication did not succeed")
  251. time.sleep(0.1)
  252. hwsim_utils.test_connectivity(dev[0], hapd)
  253. def test_ieee8021x_set_conf(dev, apdev):
  254. """IEEE 802.1X and EAPOL_SET command"""
  255. params = hostapd.radius_params()
  256. params["ssid"] = "ieee8021x-open"
  257. params["ieee8021x"] = "1"
  258. hapd = hostapd.add_ap(apdev[0], params)
  259. dev[0].connect("ieee8021x-open", key_mgmt="IEEE8021X", eapol_flags="0",
  260. eap="PSK", identity="psk.user@example.com",
  261. password_hex="0123456789abcdef0123456789abcdef",
  262. scan_freq="2412")
  263. addr0 = dev[0].own_addr()
  264. tests = [ "EAPOL_SET 1",
  265. "EAPOL_SET %sfoo bar" % addr0,
  266. "EAPOL_SET %s foo" % addr0,
  267. "EAPOL_SET %s foo bar" % addr0,
  268. "EAPOL_SET %s AdminControlledDirections bar" % addr0,
  269. "EAPOL_SET %s AdminControlledPortControl bar" % addr0,
  270. "EAPOL_SET %s reAuthEnabled bar" % addr0,
  271. "EAPOL_SET %s KeyTransmissionEnabled bar" % addr0,
  272. "EAPOL_SET 11:22:33:44:55:66 AdminControlledDirections Both" ]
  273. for t in tests:
  274. if "FAIL" not in hapd.request(t):
  275. raise Exception("Invalid EAPOL_SET command accepted: " + t)
  276. tests = [ ("AdminControlledDirections", "adminControlledDirections", "In"),
  277. ("AdminControlledDirections", "adminControlledDirections",
  278. "Both"),
  279. ("quietPeriod", "quietPeriod", "13"),
  280. ("serverTimeout", "serverTimeout", "7"),
  281. ("reAuthPeriod", "reAuthPeriod", "1234"),
  282. ("reAuthEnabled", "reAuthEnabled", "FALSE"),
  283. ("reAuthEnabled", "reAuthEnabled", "TRUE"),
  284. ("KeyTransmissionEnabled", "keyTxEnabled", "TRUE"),
  285. ("KeyTransmissionEnabled", "keyTxEnabled", "FALSE"),
  286. ("AdminControlledPortControl", "portControl", "ForceAuthorized"),
  287. ("AdminControlledPortControl", "portControl",
  288. "ForceUnauthorized"),
  289. ("AdminControlledPortControl", "portControl", "Auto") ]
  290. for param,mibparam,val in tests:
  291. if "OK" not in hapd.request("EAPOL_SET %s %s %s" % (addr0, param, val)):
  292. raise Exception("Failed to set %s %s" % (param, val))
  293. mib = hapd.get_sta(addr0, info="eapol")
  294. if mib[mibparam] != val:
  295. raise Exception("Unexpected %s value: %s (expected %s)" % (param, mib[mibparam], val))
  296. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=5)
  297. if ev is None:
  298. raise Exception("EAP authentication did not succeed")
  299. time.sleep(0.1)
  300. hwsim_utils.test_connectivity(dev[0], hapd)
  301. def test_ieee8021x_auth_awhile(dev, apdev):
  302. """IEEE 802.1X and EAPOL Authenticator aWhile handling"""
  303. params = hostapd.radius_params()
  304. params["ssid"] = "ieee8021x-open"
  305. params["ieee8021x"] = "1"
  306. params['auth_server_port'] = "18129"
  307. hapd = hostapd.add_ap(apdev[0], params)
  308. bssid = apdev[0]['bssid']
  309. addr0 = dev[0].own_addr()
  310. params = {}
  311. params['ssid'] = 'as'
  312. params['beacon_int'] = '2000'
  313. params['radius_server_clients'] = 'auth_serv/radius_clients.conf'
  314. params['radius_server_auth_port'] = '18129'
  315. params['eap_server'] = '1'
  316. params['eap_user_file'] = 'auth_serv/eap_user.conf'
  317. params['ca_cert'] = 'auth_serv/ca.pem'
  318. params['server_cert'] = 'auth_serv/server.pem'
  319. params['private_key'] = 'auth_serv/server.key'
  320. hapd1 = hostapd.add_ap(apdev[1], params)
  321. dev[0].connect("ieee8021x-open", key_mgmt="IEEE8021X", eapol_flags="0",
  322. eap="PSK", identity="psk.user@example.com",
  323. password_hex="0123456789abcdef0123456789abcdef",
  324. scan_freq="2412")
  325. hapd1.disable()
  326. if "OK" not in hapd.request("EAPOL_SET %s serverTimeout 1" % addr0):
  327. raise Exception("Failed to set serverTimeout")
  328. hapd.request("EAPOL_REAUTH " + dev[0].own_addr())
  329. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED"], timeout=5)
  330. for i in range(40):
  331. mib = hapd.get_sta(addr0, info="eapol")
  332. val = int(mib['aWhile'])
  333. if val > 0:
  334. break
  335. time.sleep(1)
  336. if val == 0:
  337. raise Exception("aWhile did not increase")
  338. hapd.dump_monitor()
  339. for i in range(40):
  340. mib = hapd.get_sta(addr0, info="eapol")
  341. val = int(mib['aWhile'])
  342. if val < 5:
  343. break
  344. time.sleep(1)
  345. ev = hapd.wait_event(["CTRL-EVENT-EAP-PROPOSED"], timeout=10)
  346. if ev is None:
  347. raise Exception("Authentication restart not seen")