test_radius.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. # RADIUS tests
  2. # Copyright (c) 2013-2014, 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 logging
  7. logger = logging.getLogger()
  8. import time
  9. import hostapd
  10. def connect(dev, ssid, wait_connect=True):
  11. dev.connect(ssid, key_mgmt="WPA-EAP", scan_freq="2412",
  12. eap="PSK", identity="psk.user@example.com",
  13. password_hex="0123456789abcdef0123456789abcdef",
  14. wait_connect=wait_connect)
  15. def test_radius_auth_unreachable(dev, apdev):
  16. """RADIUS Authentication server unreachable"""
  17. params = hostapd.wpa2_eap_params(ssid="radius-auth")
  18. params['auth_server_port'] = "18139"
  19. hostapd.add_ap(apdev[0]['ifname'], params)
  20. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  21. connect(dev[0], "radius-auth", wait_connect=False)
  22. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED"])
  23. if ev is None:
  24. raise Exception("Timeout on EAP start")
  25. logger.info("Checking for RADIUS retries")
  26. time.sleep(4)
  27. mib = hapd.get_mib()
  28. if "radiusAuthClientAccessRequests" not in mib:
  29. raise Exception("Missing MIB fields")
  30. if int(mib["radiusAuthClientAccessRetransmissions"]) < 1:
  31. raise Exception("Missing RADIUS Authentication retransmission")
  32. if int(mib["radiusAuthClientPendingRequests"]) < 1:
  33. raise Exception("Missing pending RADIUS Authentication request")
  34. def test_radius_acct_unreachable(dev, apdev):
  35. """RADIUS Accounting server unreachable"""
  36. params = hostapd.wpa2_eap_params(ssid="radius-acct")
  37. params['acct_server_addr'] = "127.0.0.1"
  38. params['acct_server_port'] = "18139"
  39. params['acct_server_shared_secret'] = "radius"
  40. hostapd.add_ap(apdev[0]['ifname'], params)
  41. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  42. connect(dev[0], "radius-acct")
  43. logger.info("Checking for RADIUS retries")
  44. time.sleep(4)
  45. mib = hapd.get_mib()
  46. if "radiusAccClientRetransmissions" not in mib:
  47. raise Exception("Missing MIB fields")
  48. if int(mib["radiusAccClientRetransmissions"]) < 2:
  49. raise Exception("Missing RADIUS Accounting retransmissions")
  50. if int(mib["radiusAccClientPendingRequests"]) < 2:
  51. raise Exception("Missing pending RADIUS Accounting requests")
  52. def test_radius_acct(dev, apdev):
  53. """RADIUS Accounting"""
  54. as_hapd = hostapd.Hostapd("as")
  55. as_mib_start = as_hapd.get_mib(param="radius_server")
  56. params = hostapd.wpa2_eap_params(ssid="radius-acct")
  57. params['acct_server_addr'] = "127.0.0.1"
  58. params['acct_server_port'] = "1813"
  59. params['acct_server_shared_secret'] = "radius"
  60. hostapd.add_ap(apdev[0]['ifname'], params)
  61. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  62. connect(dev[0], "radius-acct")
  63. logger.info("Checking for RADIUS counters")
  64. count = 0
  65. while True:
  66. mib = hapd.get_mib()
  67. if int(mib['radiusAccClientResponses']) >= 2:
  68. break
  69. time.sleep(0.1)
  70. count += 1
  71. if count > 10:
  72. raise Exception("Did not receive Accounting-Response packets")
  73. if int(mib['radiusAccClientRetransmissions']) > 0:
  74. raise Exception("Unexpected Accounting-Request retransmission")
  75. as_mib_end = as_hapd.get_mib(param="radius_server")
  76. req_s = int(as_mib_start['radiusAccServTotalRequests'])
  77. req_e = int(as_mib_end['radiusAccServTotalRequests'])
  78. if req_e < req_s + 2:
  79. raise Exception("Unexpected RADIUS server acct MIB value")
  80. acc_s = int(as_mib_start['radiusAuthServAccessAccepts'])
  81. acc_e = int(as_mib_end['radiusAuthServAccessAccepts'])
  82. if acc_e < acc_s + 1:
  83. raise Exception("Unexpected RADIUS server auth MIB value")
  84. def test_radius_das_disconnect(dev, apdev):
  85. """RADIUS Dynamic Authorization Extensions - Disconnect"""
  86. try:
  87. import pyrad.client
  88. import pyrad.packet
  89. import pyrad.dictionary
  90. import radius_das
  91. except ImportError:
  92. return "skip"
  93. params = hostapd.wpa2_eap_params(ssid="radius-das")
  94. params['radius_das_port'] = "3799"
  95. params['radius_das_client'] = "127.0.0.1 secret"
  96. params['radius_das_require_event_timestamp'] = "1"
  97. params['own_ip_addr'] = "127.0.0.1"
  98. params['nas_identifier'] = "nas.example.com"
  99. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  100. connect(dev[0], "radius-das")
  101. addr = dev[0].p2p_interface_addr()
  102. sta = hapd.get_sta(addr)
  103. id = sta['dot1xAuthSessionId']
  104. dict = pyrad.dictionary.Dictionary("dictionary.radius")
  105. srv = pyrad.client.Client(server="127.0.0.1", acctport=3799,
  106. secret="secret", dict=dict)
  107. srv.retries = 1
  108. srv.timeout = 1
  109. logger.info("Disconnect-Request with incorrect secret")
  110. req = radius_das.DisconnectPacket(dict=dict, secret="incorrect",
  111. User_Name="foo",
  112. NAS_Identifier="localhost",
  113. Event_Timestamp=int(time.time()))
  114. logger.debug(req)
  115. try:
  116. reply = srv.SendPacket(req)
  117. raise Exception("Unexpected response to Disconnect-Request")
  118. except pyrad.client.Timeout:
  119. logger.info("Disconnect-Request with incorrect secret properly ignored")
  120. logger.info("Disconnect-Request without Event-Timestamp")
  121. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  122. User_Name="psk.user@example.com")
  123. logger.debug(req)
  124. try:
  125. reply = srv.SendPacket(req)
  126. raise Exception("Unexpected response to Disconnect-Request")
  127. except pyrad.client.Timeout:
  128. logger.info("Disconnect-Request without Event-Timestamp properly ignored")
  129. logger.info("Disconnect-Request with non-matching Event-Timestamp")
  130. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  131. User_Name="psk.user@example.com",
  132. Event_Timestamp=123456789)
  133. logger.debug(req)
  134. try:
  135. reply = srv.SendPacket(req)
  136. raise Exception("Unexpected response to Disconnect-Request")
  137. except pyrad.client.Timeout:
  138. logger.info("Disconnect-Request with non-matching Event-Timestamp properly ignored")
  139. logger.info("Disconnect-Request with unsupported attribute")
  140. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  141. User_Name="foo",
  142. User_Password="foo",
  143. Event_Timestamp=int(time.time()))
  144. reply = srv.SendPacket(req)
  145. logger.debug("RADIUS response from hostapd")
  146. for i in reply.keys():
  147. logger.debug("%s: %s" % (i, reply[i]))
  148. if reply.code != pyrad.packet.DisconnectNAK:
  149. raise Exception("Unexpected response code")
  150. if 'Error-Cause' not in reply:
  151. raise Exception("Missing Error-Cause")
  152. if reply['Error-Cause'][0] != 401:
  153. raise Exception("Unexpected Error-Cause: {}".format(reply['Error-Cause']))
  154. logger.info("Disconnect-Request with invalid Calling-Station-Id")
  155. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  156. User_Name="foo",
  157. Calling_Station_Id="foo",
  158. Event_Timestamp=int(time.time()))
  159. reply = srv.SendPacket(req)
  160. logger.debug("RADIUS response from hostapd")
  161. for i in reply.keys():
  162. logger.debug("%s: %s" % (i, reply[i]))
  163. if reply.code != pyrad.packet.DisconnectNAK:
  164. raise Exception("Unexpected response code")
  165. if 'Error-Cause' not in reply:
  166. raise Exception("Missing Error-Cause")
  167. if reply['Error-Cause'][0] != 407:
  168. raise Exception("Unexpected Error-Cause: {}".format(reply['Error-Cause']))
  169. logger.info("Disconnect-Request with mismatching User-Name")
  170. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  171. User_Name="foo",
  172. Event_Timestamp=int(time.time()))
  173. reply = srv.SendPacket(req)
  174. logger.debug("RADIUS response from hostapd")
  175. for i in reply.keys():
  176. logger.debug("%s: %s" % (i, reply[i]))
  177. if reply.code != pyrad.packet.DisconnectNAK:
  178. raise Exception("Unexpected response code")
  179. if 'Error-Cause' not in reply:
  180. raise Exception("Missing Error-Cause")
  181. if reply['Error-Cause'][0] != 503:
  182. raise Exception("Unexpected Error-Cause: {}".format(reply['Error-Cause']))
  183. logger.info("Disconnect-Request with mismatching Calling-Station-Id")
  184. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  185. Calling_Station_Id="12:34:56:78:90:aa",
  186. Event_Timestamp=int(time.time()))
  187. reply = srv.SendPacket(req)
  188. logger.debug("RADIUS response from hostapd")
  189. for i in reply.keys():
  190. logger.debug("%s: %s" % (i, reply[i]))
  191. if reply.code != pyrad.packet.DisconnectNAK:
  192. raise Exception("Unexpected response code")
  193. if 'Error-Cause' not in reply:
  194. raise Exception("Missing Error-Cause")
  195. if reply['Error-Cause'][0] != 503:
  196. raise Exception("Unexpected Error-Cause: {}".format(reply['Error-Cause']))
  197. logger.info("Disconnect-Request with mismatching Acct-Session-Id")
  198. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  199. Acct_Session_Id="12345678-87654321",
  200. Event_Timestamp=int(time.time()))
  201. reply = srv.SendPacket(req)
  202. logger.debug("RADIUS response from hostapd")
  203. for i in reply.keys():
  204. logger.debug("%s: %s" % (i, reply[i]))
  205. if reply.code != pyrad.packet.DisconnectNAK:
  206. raise Exception("Unexpected response code")
  207. if 'Error-Cause' not in reply:
  208. raise Exception("Missing Error-Cause")
  209. if reply['Error-Cause'][0] != 503:
  210. raise Exception("Unexpected Error-Cause: {}".format(reply['Error-Cause']))
  211. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=1)
  212. if ev is not None:
  213. raise Exception("Unexpected disconnection")
  214. logger.info("Disconnect-Request with mismatching NAS-IP-Address")
  215. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  216. NAS_IP_Address="192.168.3.4",
  217. Acct_Session_Id=id,
  218. Event_Timestamp=int(time.time()))
  219. reply = srv.SendPacket(req)
  220. logger.debug("RADIUS response from hostapd")
  221. for i in reply.keys():
  222. logger.debug("%s: %s" % (i, reply[i]))
  223. if reply.code != pyrad.packet.DisconnectNAK:
  224. raise Exception("Unexpected response code")
  225. if 'Error-Cause' not in reply:
  226. raise Exception("Missing Error-Cause")
  227. if reply['Error-Cause'][0] != 403:
  228. raise Exception("Unexpected Error-Cause: {}".format(reply['Error-Cause']))
  229. logger.info("Disconnect-Request with mismatching NAS-Identifier")
  230. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  231. NAS_Identifier="unknown.example.com",
  232. Acct_Session_Id=id,
  233. Event_Timestamp=int(time.time()))
  234. reply = srv.SendPacket(req)
  235. logger.debug("RADIUS response from hostapd")
  236. for i in reply.keys():
  237. logger.debug("%s: %s" % (i, reply[i]))
  238. if reply.code != pyrad.packet.DisconnectNAK:
  239. raise Exception("Unexpected response code")
  240. if 'Error-Cause' not in reply:
  241. raise Exception("Missing Error-Cause")
  242. if reply['Error-Cause'][0] != 403:
  243. raise Exception("Unexpected Error-Cause: {}".format(reply['Error-Cause']))
  244. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=1)
  245. if ev is not None:
  246. raise Exception("Unexpected disconnection")
  247. logger.info("Disconnect-Request with matching Acct-Session-Id")
  248. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  249. NAS_IP_Address="127.0.0.1",
  250. NAS_Identifier="nas.example.com",
  251. Acct_Session_Id=id,
  252. Event_Timestamp=int(time.time()))
  253. reply = srv.SendPacket(req)
  254. logger.debug("RADIUS response from hostapd")
  255. for i in reply.keys():
  256. logger.debug("%s: %s" % (i, reply[i]))
  257. if reply.code != pyrad.packet.DisconnectACK:
  258. raise Exception("Unexpected response code")
  259. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  260. if ev is None:
  261. raise Exception("Timeout while waiting for disconnection")
  262. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"])
  263. if ev is None:
  264. raise Exception("Timeout while waiting for re-connection")
  265. logger.info("Disconnect-Request with matching User-Name")
  266. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  267. NAS_Identifier="nas.example.com",
  268. User_Name="psk.user@example.com",
  269. Event_Timestamp=int(time.time()))
  270. reply = srv.SendPacket(req)
  271. logger.debug("RADIUS response from hostapd")
  272. for i in reply.keys():
  273. logger.debug("%s: %s" % (i, reply[i]))
  274. if reply.code != pyrad.packet.DisconnectACK:
  275. raise Exception("Unexpected response code")
  276. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  277. if ev is None:
  278. raise Exception("Timeout while waiting for disconnection")
  279. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"])
  280. if ev is None:
  281. raise Exception("Timeout while waiting for re-connection")
  282. logger.info("Disconnect-Request with matching Calling-Station-Id")
  283. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  284. NAS_IP_Address="127.0.0.1",
  285. Calling_Station_Id=addr,
  286. Event_Timestamp=int(time.time()))
  287. reply = srv.SendPacket(req)
  288. logger.debug("RADIUS response from hostapd")
  289. for i in reply.keys():
  290. logger.debug("%s: %s" % (i, reply[i]))
  291. if reply.code != pyrad.packet.DisconnectACK:
  292. raise Exception("Unexpected response code")
  293. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  294. if ev is None:
  295. raise Exception("Timeout while waiting for disconnection")
  296. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED", "CTRL-EVENT-CONNECTED"])
  297. if ev is None:
  298. raise Exception("Timeout while waiting for re-connection")
  299. if "CTRL-EVENT-EAP-STARTED" not in ev:
  300. raise Exception("Unexpected skipping of EAP authentication in reconnection")
  301. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"])
  302. if ev is None:
  303. raise Exception("Timeout while waiting for re-connection to complete")
  304. logger.info("Disconnect-Request with matching Calling-Station-Id and non-matching CUI")
  305. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  306. Calling_Station_Id=addr,
  307. Chargeable_User_Identity="foo@example.com",
  308. Event_Timestamp=int(time.time()))
  309. reply = srv.SendPacket(req)
  310. logger.debug("RADIUS response from hostapd")
  311. for i in reply.keys():
  312. logger.debug("%s: %s" % (i, reply[i]))
  313. if reply.code != pyrad.packet.DisconnectACK:
  314. raise Exception("Unexpected response code")
  315. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  316. if ev is None:
  317. raise Exception("Timeout while waiting for disconnection")
  318. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"])
  319. if ev is None:
  320. raise Exception("Timeout while waiting for re-connection")
  321. def test_radius_das_coa(dev, apdev):
  322. """RADIUS Dynamic Authorization Extensions - CoA"""
  323. try:
  324. import pyrad.client
  325. import pyrad.packet
  326. import pyrad.dictionary
  327. import radius_das
  328. except ImportError:
  329. return "skip"
  330. params = hostapd.wpa2_eap_params(ssid="radius-das")
  331. params['radius_das_port'] = "3799"
  332. params['radius_das_client'] = "127.0.0.1 secret"
  333. params['radius_das_require_event_timestamp'] = "1"
  334. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  335. connect(dev[0], "radius-das")
  336. addr = dev[0].p2p_interface_addr()
  337. sta = hapd.get_sta(addr)
  338. id = sta['dot1xAuthSessionId']
  339. dict = pyrad.dictionary.Dictionary("dictionary.radius")
  340. srv = pyrad.client.Client(server="127.0.0.1", acctport=3799,
  341. secret="secret", dict=dict)
  342. srv.retries = 1
  343. srv.timeout = 1
  344. # hostapd does not currently support CoA-Request, so NAK is expected
  345. logger.info("CoA-Request with matching Acct-Session-Id")
  346. req = radius_das.CoAPacket(dict=dict, secret="secret",
  347. Acct_Session_Id=id,
  348. Event_Timestamp=int(time.time()))
  349. reply = srv.SendPacket(req)
  350. logger.debug("RADIUS response from hostapd")
  351. for i in reply.keys():
  352. logger.debug("%s: %s" % (i, reply[i]))
  353. if reply.code != pyrad.packet.CoANAK:
  354. raise Exception("Unexpected response code")
  355. if 'Error-Cause' not in reply:
  356. raise Exception("Missing Error-Cause")
  357. if reply['Error-Cause'][0] != 405:
  358. raise Exception("Unexpected Error-Cause: {}".format(reply['Error-Cause']))