test_radius.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. params['radius_auth_req_attr'] = [ "126:s:Operator", "77:s:testing" ]
  61. params['radius_acct_req_attr'] = [ "126:s:Operator", "77:s:testing" ]
  62. hostapd.add_ap(apdev[0]['ifname'], params)
  63. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  64. connect(dev[0], "radius-acct")
  65. dev[1].connect("radius-acct", key_mgmt="WPA-EAP", scan_freq="2412",
  66. eap="PAX", identity="test-class",
  67. password_hex="0123456789abcdef0123456789abcdef")
  68. dev[2].connect("radius-acct", key_mgmt="WPA-EAP",
  69. eap="GPSK", identity="gpsk-cui",
  70. password="abcdefghijklmnop0123456789abcdef",
  71. scan_freq="2412")
  72. logger.info("Checking for RADIUS counters")
  73. count = 0
  74. while True:
  75. mib = hapd.get_mib()
  76. if int(mib['radiusAccClientResponses']) >= 3:
  77. break
  78. time.sleep(0.1)
  79. count += 1
  80. if count > 10:
  81. raise Exception("Did not receive Accounting-Response packets")
  82. if int(mib['radiusAccClientRetransmissions']) > 0:
  83. raise Exception("Unexpected Accounting-Request retransmission")
  84. as_mib_end = as_hapd.get_mib(param="radius_server")
  85. req_s = int(as_mib_start['radiusAccServTotalRequests'])
  86. req_e = int(as_mib_end['radiusAccServTotalRequests'])
  87. if req_e < req_s + 2:
  88. raise Exception("Unexpected RADIUS server acct MIB value")
  89. acc_s = int(as_mib_start['radiusAuthServAccessAccepts'])
  90. acc_e = int(as_mib_end['radiusAuthServAccessAccepts'])
  91. if acc_e < acc_s + 1:
  92. raise Exception("Unexpected RADIUS server auth MIB value")
  93. def test_radius_acct_interim(dev, apdev):
  94. """RADIUS Accounting interim update"""
  95. as_hapd = hostapd.Hostapd("as")
  96. params = hostapd.wpa2_eap_params(ssid="radius-acct")
  97. params['acct_server_addr'] = "127.0.0.1"
  98. params['acct_server_port'] = "1813"
  99. params['acct_server_shared_secret'] = "radius"
  100. params['radius_acct_interim_interval'] = "1"
  101. hostapd.add_ap(apdev[0]['ifname'], params)
  102. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  103. connect(dev[0], "radius-acct")
  104. logger.info("Checking for RADIUS counters")
  105. as_mib_start = as_hapd.get_mib(param="radius_server")
  106. time.sleep(3.1)
  107. as_mib_end = as_hapd.get_mib(param="radius_server")
  108. req_s = int(as_mib_start['radiusAccServTotalRequests'])
  109. req_e = int(as_mib_end['radiusAccServTotalRequests'])
  110. if req_e < req_s + 3:
  111. raise Exception("Unexpected RADIUS server acct MIB value")
  112. def test_radius_das_disconnect(dev, apdev):
  113. """RADIUS Dynamic Authorization Extensions - Disconnect"""
  114. try:
  115. import pyrad.client
  116. import pyrad.packet
  117. import pyrad.dictionary
  118. import radius_das
  119. except ImportError:
  120. return "skip"
  121. params = hostapd.wpa2_eap_params(ssid="radius-das")
  122. params['radius_das_port'] = "3799"
  123. params['radius_das_client'] = "127.0.0.1 secret"
  124. params['radius_das_require_event_timestamp'] = "1"
  125. params['own_ip_addr'] = "127.0.0.1"
  126. params['nas_identifier'] = "nas.example.com"
  127. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  128. connect(dev[0], "radius-das")
  129. addr = dev[0].p2p_interface_addr()
  130. sta = hapd.get_sta(addr)
  131. id = sta['dot1xAuthSessionId']
  132. dict = pyrad.dictionary.Dictionary("dictionary.radius")
  133. srv = pyrad.client.Client(server="127.0.0.1", acctport=3799,
  134. secret="secret", dict=dict)
  135. srv.retries = 1
  136. srv.timeout = 1
  137. logger.info("Disconnect-Request with incorrect secret")
  138. req = radius_das.DisconnectPacket(dict=dict, secret="incorrect",
  139. User_Name="foo",
  140. NAS_Identifier="localhost",
  141. Event_Timestamp=int(time.time()))
  142. logger.debug(req)
  143. try:
  144. reply = srv.SendPacket(req)
  145. raise Exception("Unexpected response to Disconnect-Request")
  146. except pyrad.client.Timeout:
  147. logger.info("Disconnect-Request with incorrect secret properly ignored")
  148. logger.info("Disconnect-Request without Event-Timestamp")
  149. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  150. User_Name="psk.user@example.com")
  151. logger.debug(req)
  152. try:
  153. reply = srv.SendPacket(req)
  154. raise Exception("Unexpected response to Disconnect-Request")
  155. except pyrad.client.Timeout:
  156. logger.info("Disconnect-Request without Event-Timestamp properly ignored")
  157. logger.info("Disconnect-Request with non-matching Event-Timestamp")
  158. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  159. User_Name="psk.user@example.com",
  160. Event_Timestamp=123456789)
  161. logger.debug(req)
  162. try:
  163. reply = srv.SendPacket(req)
  164. raise Exception("Unexpected response to Disconnect-Request")
  165. except pyrad.client.Timeout:
  166. logger.info("Disconnect-Request with non-matching Event-Timestamp properly ignored")
  167. logger.info("Disconnect-Request with unsupported attribute")
  168. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  169. User_Name="foo",
  170. User_Password="foo",
  171. Event_Timestamp=int(time.time()))
  172. reply = srv.SendPacket(req)
  173. logger.debug("RADIUS response from hostapd")
  174. for i in reply.keys():
  175. logger.debug("%s: %s" % (i, reply[i]))
  176. if reply.code != pyrad.packet.DisconnectNAK:
  177. raise Exception("Unexpected response code")
  178. if 'Error-Cause' not in reply:
  179. raise Exception("Missing Error-Cause")
  180. if reply['Error-Cause'][0] != 401:
  181. raise Exception("Unexpected Error-Cause: {}".format(reply['Error-Cause']))
  182. logger.info("Disconnect-Request with invalid Calling-Station-Id")
  183. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  184. User_Name="foo",
  185. Calling_Station_Id="foo",
  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] != 407:
  196. raise Exception("Unexpected Error-Cause: {}".format(reply['Error-Cause']))
  197. logger.info("Disconnect-Request with mismatching User-Name")
  198. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  199. User_Name="foo",
  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. logger.info("Disconnect-Request with mismatching Calling-Station-Id")
  212. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  213. Calling_Station_Id="12:34:56:78:90:aa",
  214. Event_Timestamp=int(time.time()))
  215. reply = srv.SendPacket(req)
  216. logger.debug("RADIUS response from hostapd")
  217. for i in reply.keys():
  218. logger.debug("%s: %s" % (i, reply[i]))
  219. if reply.code != pyrad.packet.DisconnectNAK:
  220. raise Exception("Unexpected response code")
  221. if 'Error-Cause' not in reply:
  222. raise Exception("Missing Error-Cause")
  223. if reply['Error-Cause'][0] != 503:
  224. raise Exception("Unexpected Error-Cause: {}".format(reply['Error-Cause']))
  225. logger.info("Disconnect-Request with mismatching Acct-Session-Id")
  226. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  227. Acct_Session_Id="12345678-87654321",
  228. Event_Timestamp=int(time.time()))
  229. reply = srv.SendPacket(req)
  230. logger.debug("RADIUS response from hostapd")
  231. for i in reply.keys():
  232. logger.debug("%s: %s" % (i, reply[i]))
  233. if reply.code != pyrad.packet.DisconnectNAK:
  234. raise Exception("Unexpected response code")
  235. if 'Error-Cause' not in reply:
  236. raise Exception("Missing Error-Cause")
  237. if reply['Error-Cause'][0] != 503:
  238. raise Exception("Unexpected Error-Cause: {}".format(reply['Error-Cause']))
  239. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=1)
  240. if ev is not None:
  241. raise Exception("Unexpected disconnection")
  242. logger.info("Disconnect-Request with mismatching NAS-IP-Address")
  243. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  244. NAS_IP_Address="192.168.3.4",
  245. Acct_Session_Id=id,
  246. Event_Timestamp=int(time.time()))
  247. reply = srv.SendPacket(req)
  248. logger.debug("RADIUS response from hostapd")
  249. for i in reply.keys():
  250. logger.debug("%s: %s" % (i, reply[i]))
  251. if reply.code != pyrad.packet.DisconnectNAK:
  252. raise Exception("Unexpected response code")
  253. if 'Error-Cause' not in reply:
  254. raise Exception("Missing Error-Cause")
  255. if reply['Error-Cause'][0] != 403:
  256. raise Exception("Unexpected Error-Cause: {}".format(reply['Error-Cause']))
  257. logger.info("Disconnect-Request with mismatching NAS-Identifier")
  258. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  259. NAS_Identifier="unknown.example.com",
  260. Acct_Session_Id=id,
  261. Event_Timestamp=int(time.time()))
  262. reply = srv.SendPacket(req)
  263. logger.debug("RADIUS response from hostapd")
  264. for i in reply.keys():
  265. logger.debug("%s: %s" % (i, reply[i]))
  266. if reply.code != pyrad.packet.DisconnectNAK:
  267. raise Exception("Unexpected response code")
  268. if 'Error-Cause' not in reply:
  269. raise Exception("Missing Error-Cause")
  270. if reply['Error-Cause'][0] != 403:
  271. raise Exception("Unexpected Error-Cause: {}".format(reply['Error-Cause']))
  272. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=1)
  273. if ev is not None:
  274. raise Exception("Unexpected disconnection")
  275. logger.info("Disconnect-Request with matching Acct-Session-Id")
  276. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  277. NAS_IP_Address="127.0.0.1",
  278. NAS_Identifier="nas.example.com",
  279. Acct_Session_Id=id,
  280. Event_Timestamp=int(time.time()))
  281. reply = srv.SendPacket(req)
  282. logger.debug("RADIUS response from hostapd")
  283. for i in reply.keys():
  284. logger.debug("%s: %s" % (i, reply[i]))
  285. if reply.code != pyrad.packet.DisconnectACK:
  286. raise Exception("Unexpected response code")
  287. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  288. if ev is None:
  289. raise Exception("Timeout while waiting for disconnection")
  290. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"])
  291. if ev is None:
  292. raise Exception("Timeout while waiting for re-connection")
  293. logger.info("Disconnect-Request with matching User-Name")
  294. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  295. NAS_Identifier="nas.example.com",
  296. User_Name="psk.user@example.com",
  297. Event_Timestamp=int(time.time()))
  298. reply = srv.SendPacket(req)
  299. logger.debug("RADIUS response from hostapd")
  300. for i in reply.keys():
  301. logger.debug("%s: %s" % (i, reply[i]))
  302. if reply.code != pyrad.packet.DisconnectACK:
  303. raise Exception("Unexpected response code")
  304. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  305. if ev is None:
  306. raise Exception("Timeout while waiting for disconnection")
  307. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"])
  308. if ev is None:
  309. raise Exception("Timeout while waiting for re-connection")
  310. logger.info("Disconnect-Request with matching Calling-Station-Id")
  311. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  312. NAS_IP_Address="127.0.0.1",
  313. Calling_Station_Id=addr,
  314. Event_Timestamp=int(time.time()))
  315. reply = srv.SendPacket(req)
  316. logger.debug("RADIUS response from hostapd")
  317. for i in reply.keys():
  318. logger.debug("%s: %s" % (i, reply[i]))
  319. if reply.code != pyrad.packet.DisconnectACK:
  320. raise Exception("Unexpected response code")
  321. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  322. if ev is None:
  323. raise Exception("Timeout while waiting for disconnection")
  324. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED", "CTRL-EVENT-CONNECTED"])
  325. if ev is None:
  326. raise Exception("Timeout while waiting for re-connection")
  327. if "CTRL-EVENT-EAP-STARTED" not in ev:
  328. raise Exception("Unexpected skipping of EAP authentication in reconnection")
  329. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"])
  330. if ev is None:
  331. raise Exception("Timeout while waiting for re-connection to complete")
  332. logger.info("Disconnect-Request with matching Calling-Station-Id and non-matching CUI")
  333. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  334. Calling_Station_Id=addr,
  335. Chargeable_User_Identity="foo@example.com",
  336. Event_Timestamp=int(time.time()))
  337. reply = srv.SendPacket(req)
  338. logger.debug("RADIUS response from hostapd")
  339. for i in reply.keys():
  340. logger.debug("%s: %s" % (i, reply[i]))
  341. if reply.code != pyrad.packet.DisconnectACK:
  342. raise Exception("Unexpected response code")
  343. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  344. if ev is None:
  345. raise Exception("Timeout while waiting for disconnection")
  346. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"])
  347. if ev is None:
  348. raise Exception("Timeout while waiting for re-connection")
  349. logger.info("Disconnect-Request with matching CUI")
  350. dev[1].connect("radius-das", key_mgmt="WPA-EAP",
  351. eap="GPSK", identity="gpsk-cui",
  352. password="abcdefghijklmnop0123456789abcdef",
  353. scan_freq="2412")
  354. req = radius_das.DisconnectPacket(dict=dict, secret="secret",
  355. Chargeable_User_Identity="gpsk-chargeable-user-identity",
  356. Event_Timestamp=int(time.time()))
  357. reply = srv.SendPacket(req)
  358. logger.debug("RADIUS response from hostapd")
  359. for i in reply.keys():
  360. logger.debug("%s: %s" % (i, reply[i]))
  361. if reply.code != pyrad.packet.DisconnectACK:
  362. raise Exception("Unexpected response code")
  363. ev = dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"])
  364. if ev is None:
  365. raise Exception("Timeout while waiting for disconnection")
  366. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"])
  367. if ev is None:
  368. raise Exception("Timeout while waiting for re-connection")
  369. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=1)
  370. if ev is not None:
  371. raise Exception("Unexpected disconnection")
  372. def test_radius_das_coa(dev, apdev):
  373. """RADIUS Dynamic Authorization Extensions - CoA"""
  374. try:
  375. import pyrad.client
  376. import pyrad.packet
  377. import pyrad.dictionary
  378. import radius_das
  379. except ImportError:
  380. return "skip"
  381. params = hostapd.wpa2_eap_params(ssid="radius-das")
  382. params['radius_das_port'] = "3799"
  383. params['radius_das_client'] = "127.0.0.1 secret"
  384. params['radius_das_require_event_timestamp'] = "1"
  385. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  386. connect(dev[0], "radius-das")
  387. addr = dev[0].p2p_interface_addr()
  388. sta = hapd.get_sta(addr)
  389. id = sta['dot1xAuthSessionId']
  390. dict = pyrad.dictionary.Dictionary("dictionary.radius")
  391. srv = pyrad.client.Client(server="127.0.0.1", acctport=3799,
  392. secret="secret", dict=dict)
  393. srv.retries = 1
  394. srv.timeout = 1
  395. # hostapd does not currently support CoA-Request, so NAK is expected
  396. logger.info("CoA-Request with matching Acct-Session-Id")
  397. req = radius_das.CoAPacket(dict=dict, secret="secret",
  398. Acct_Session_Id=id,
  399. Event_Timestamp=int(time.time()))
  400. reply = srv.SendPacket(req)
  401. logger.debug("RADIUS response from hostapd")
  402. for i in reply.keys():
  403. logger.debug("%s: %s" % (i, reply[i]))
  404. if reply.code != pyrad.packet.CoANAK:
  405. raise Exception("Unexpected response code")
  406. if 'Error-Cause' not in reply:
  407. raise Exception("Missing Error-Cause")
  408. if reply['Error-Cause'][0] != 405:
  409. raise Exception("Unexpected Error-Cause: {}".format(reply['Error-Cause']))
  410. def test_radius_ipv6(dev, apdev):
  411. """RADIUS connection over IPv6"""
  412. params = {}
  413. params['ssid'] = 'as'
  414. params['beacon_int'] = '2000'
  415. params['radius_server_clients'] = 'auth_serv/radius_clients_ipv6.conf'
  416. params['radius_server_ipv6'] = '1'
  417. params['radius_server_auth_port'] = '18129'
  418. params['radius_server_acct_port'] = '18139'
  419. params['eap_server'] = '1'
  420. params['eap_user_file'] = 'auth_serv/eap_user.conf'
  421. params['ca_cert'] = 'auth_serv/ca.pem'
  422. params['server_cert'] = 'auth_serv/server.pem'
  423. params['private_key'] = 'auth_serv/server.key'
  424. hostapd.add_ap(apdev[1]['ifname'], params)
  425. params = hostapd.wpa2_eap_params(ssid="radius-ipv6")
  426. params['auth_server_addr'] = "::0"
  427. params['auth_server_port'] = "18129"
  428. params['acct_server_addr'] = "::0"
  429. params['acct_server_port'] = "18139"
  430. params['acct_server_shared_secret'] = "radius"
  431. params['own_ip_addr'] = "::0"
  432. hostapd.add_ap(apdev[0]['ifname'], params)
  433. connect(dev[0], "radius-ipv6")