test_wpas_config.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. # wpa_supplicant config file
  2. # Copyright (c) 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 os
  9. from wpasupplicant import WpaSupplicant
  10. import hostapd
  11. config_checks = [ ("ap_scan", "0"),
  12. ("update_config", "1"),
  13. ("device_name", "name"),
  14. ("eapol_version", "2"),
  15. ("wps_priority", "5"),
  16. ("ip_addr_go", "192.168.1.1"),
  17. ("ip_addr_mask", "255.255.255.0"),
  18. ("ip_addr_start", "192.168.1.10"),
  19. ("ip_addr_end", "192.168.1.20"),
  20. ("disable_scan_offload", "1"),
  21. ("fast_reauth", "0"),
  22. ("uuid", "6aeae5e3-c1fc-4e76-8293-7346e1d1459d"),
  23. ("manufacturer", "MANUF"),
  24. ("model_name", "MODEL"),
  25. ("model_number", "MODEL NUM"),
  26. ("serial_number", "123qwerty"),
  27. ("device_type", "1234-0050F204-4321"),
  28. ("os_version", "01020304"),
  29. ("config_methods", "label push_button"),
  30. ("wps_cred_processing", "1"),
  31. ("wps_vendor_ext_m1", "000137100100020001"),
  32. ("p2p_listen_reg_class", "81"),
  33. ("p2p_listen_channel", "6"),
  34. ("p2p_oper_reg_class", "82"),
  35. ("p2p_oper_channel", "14"),
  36. ("p2p_go_intent", "14"),
  37. ("p2p_ssid_postfix", "foobar"),
  38. ("persistent_reconnect", "1"),
  39. ("p2p_intra_bss", "0"),
  40. ("p2p_group_idle", "2"),
  41. ("p2p_passphrase_len", "63"),
  42. ("p2p_pref_chan", "81:1,82:14,81:11"),
  43. ("p2p_no_go_freq", "2412-2432,2462,5000-6000"),
  44. ("p2p_add_cli_chan", "1"),
  45. ("p2p_optimize_listen_chan", "1"),
  46. ("p2p_go_ht40", "1"),
  47. ("p2p_go_vht", "1"),
  48. ("p2p_go_ctwindow", "1"),
  49. ("p2p_disabled", "1"),
  50. ("p2p_no_group_iface", "1"),
  51. ("p2p_ignore_shared_freq", "1"),
  52. ("p2p_cli_probe", "1"),
  53. ("p2p_go_freq_change_policy", "0"),
  54. ("country", "FI"),
  55. ("bss_max_count", "123"),
  56. ("bss_expiration_age", "45"),
  57. ("bss_expiration_scan_count", "17"),
  58. ("filter_ssids", "1"),
  59. ("filter_rssi", "-10"),
  60. ("max_num_sta", "3"),
  61. ("disassoc_low_ack", "1"),
  62. ("hs20", "1"),
  63. ("interworking", "1"),
  64. ("hessid", "02:03:04:05:06:07"),
  65. ("access_network_type", "7"),
  66. ("pbc_in_m1", "1"),
  67. ("wps_nfc_dev_pw_id", "12345"),
  68. ("wps_nfc_dh_pubkey", "1234567890ABCDEF"),
  69. ("wps_nfc_dh_privkey", "FF1234567890ABCDEFFF"),
  70. ("ext_password_backend", "test"),
  71. ("p2p_go_max_inactivity", "9"),
  72. ("auto_interworking", "1"),
  73. ("okc", "1"),
  74. ("pmf", "1"),
  75. ("dtim_period", "3"),
  76. ("beacon_int", "102"),
  77. ("sae_groups", "5 19"),
  78. ("ap_vendor_elements", "dd0411223301"),
  79. ("ignore_old_scan_res", "1"),
  80. ("freq_list", "2412 2437"),
  81. ("scan_cur_freq", "1"),
  82. ("sched_scan_interval", "13"),
  83. ("external_sim", "1"),
  84. ("tdls_external_control", "1"),
  85. ("wowlan_triggers", "any"),
  86. ("bgscan", '"simple:30:-45:300"'),
  87. ("p2p_search_delay", "123"),
  88. ("mac_addr", "2"),
  89. ("rand_addr_lifetime", "123456789"),
  90. ("preassoc_mac_addr", "1"),
  91. ("gas_rand_addr_lifetime", "567"),
  92. ("gas_rand_mac_addr", "2"),
  93. ("key_mgmt_offload", "0"),
  94. ("user_mpm", "0"),
  95. ("max_peer_links", "17"),
  96. ("cert_in_cb", "0"),
  97. ("mesh_max_inactivity", "31"),
  98. ("dot11RSNASAERetransPeriod", "19"),
  99. ("passive_scan", "1"),
  100. ("reassoc_same_bss_optim", "1"),
  101. ("wpa_rsc_relaxation", "0"),
  102. ("sched_scan_plans", "10:100 20:200 30"),
  103. ("non_pref_chan", "81:5:10:2 81:1:0:2 81:9:0:2"),
  104. ("mbo_cell_capa", "1"),
  105. ("gas_address3", "1"),
  106. ("ftm_responder", "1"),
  107. ("ftm_initiator", "1"),
  108. ("pcsc_reader", "foo"),
  109. ("pcsc_pin", "1234"),
  110. ("driver_param", "testing"),
  111. ("dot11RSNAConfigPMKLifetime", "43201"),
  112. ("dot11RSNAConfigPMKReauthThreshold", "71"),
  113. ("dot11RSNAConfigSATimeout", "61"),
  114. ("sec_device_type", "12345-0050F204-54321"),
  115. ("autoscan", "exponential:3:300"),
  116. ("osu_dir", "/tmp/osu"),
  117. ("fst_group_id", "bond0"),
  118. ("fst_priority", "5"),
  119. ("fst_llt", "7"),
  120. ("go_interworking", "1"),
  121. ("go_access_network_type", "2"),
  122. ("go_internet", "1"),
  123. ("go_venue_group", "3"),
  124. ("go_venue_type", "4"),
  125. ("openssl_ciphers", "DEFAULT") ]
  126. def supported_param(capa, field):
  127. mesh_params = [ "user_mpm", "max_peer_links", "mesh_max_inactivity" ]
  128. if field in mesh_params and not capa['mesh']:
  129. return False
  130. sae_params = [ "dot11RSNASAERetransPeriod" ]
  131. if field in sae_params and not capa['sae']:
  132. return False
  133. return True
  134. def check_config(capa, config):
  135. with open(config, "r") as f:
  136. data = f.read()
  137. if "ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=" not in data:
  138. raise Exception("Missing ctrl_interface")
  139. if "blob-base64-foo={" not in data:
  140. raise Exception("Missing blob")
  141. if "cred={" not in data:
  142. raise Exception("Missing cred")
  143. if "network={" not in data:
  144. raise Exception("Missing network")
  145. for field, value in config_checks:
  146. if supported_param(capa, field):
  147. if "\n" + field + "=" + value + "\n" not in data:
  148. raise Exception("Missing value: " + field)
  149. return data
  150. def test_wpas_config_file(dev, apdev, params):
  151. """wpa_supplicant config file parsing/writing"""
  152. config = os.path.join(params['logdir'], 'wpas_config_file.conf')
  153. if os.path.exists(config):
  154. try:
  155. os.remove(config)
  156. except:
  157. pass
  158. try:
  159. os.rmdir(config)
  160. except:
  161. pass
  162. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  163. try:
  164. wpas.interface_add("wlan5", config=config)
  165. initialized = True
  166. except:
  167. initialized = False
  168. if initialized:
  169. raise Exception("Missing config file did not result in an error")
  170. try:
  171. with open(config, "w") as f:
  172. f.write("update_config=1 \t\r\n")
  173. f.write("# foo\n")
  174. f.write("\n")
  175. f.write(" \t\reapol_version=2")
  176. for i in range(0, 100):
  177. f.write(" ")
  178. f.write("foo\n")
  179. f.write("device_name=name#foo\n")
  180. wpas.interface_add("wlan5", config=config)
  181. capa = {}
  182. capa['mesh'] = "MESH" in wpas.get_capability("modes")
  183. capa['sae'] = "SAE" in wpas.get_capability("auth_alg")
  184. id = wpas.add_network()
  185. wpas.set_network_quoted(id, "ssid", "foo")
  186. wpas.set_network_quoted(id, "psk", "12345678")
  187. wpas.set_network(id, "bssid", "00:11:22:33:44:55")
  188. wpas.set_network(id, "proto", "RSN")
  189. wpas.set_network(id, "key_mgmt", "WPA-PSK-SHA256")
  190. wpas.set_network(id, "pairwise", "CCMP")
  191. wpas.set_network(id, "group", "CCMP")
  192. wpas.set_network(id, "auth_alg", "OPEN")
  193. id = wpas.add_cred()
  194. wpas.set_cred(id, "priority", "3")
  195. wpas.set_cred(id, "sp_priority", "6")
  196. wpas.set_cred(id, "update_identifier", "4")
  197. wpas.set_cred(id, "ocsp", "1")
  198. wpas.set_cred(id, "eap", "TTLS")
  199. wpas.set_cred(id, "req_conn_capab", "6:1234")
  200. wpas.set_cred_quoted(id, "realm", "example.com")
  201. wpas.set_cred_quoted(id, "provisioning_sp", "example.com")
  202. wpas.set_cred_quoted(id, "domain", "example.com")
  203. wpas.set_cred_quoted(id, "domain_suffix_match", "example.com")
  204. wpas.set_cred(id, "roaming_consortium", "112233")
  205. wpas.set_cred(id, "required_roaming_consortium", "112233")
  206. wpas.set_cred_quoted(id, "roaming_partner",
  207. "roaming.example.net,1,127,*")
  208. wpas.set_cred_quoted(id, "ca_cert", "/tmp/ca.pem")
  209. wpas.set_cred_quoted(id, "username", "user")
  210. wpas.set_cred_quoted(id, "password", "secret")
  211. ev = wpas.wait_event(["CRED-MODIFIED 0 password"])
  212. wpas.request("SET blob foo 12345678")
  213. for field, value in config_checks:
  214. if supported_param(capa, field):
  215. wpas.set(field, value)
  216. if "OK" not in wpas.request("SAVE_CONFIG"):
  217. raise Exception("Failed to save configuration file")
  218. if "OK" not in wpas.global_request("SAVE_CONFIG"):
  219. raise Exception("Failed to save configuration file")
  220. wpas.interface_remove("wlan5")
  221. data1 = check_config(capa, config)
  222. wpas.interface_add("wlan5", config=config)
  223. if len(wpas.list_networks()) != 1:
  224. raise Exception("Unexpected number of networks")
  225. if len(wpas.request("LIST_CREDS").splitlines()) != 2:
  226. raise Exception("Unexpected number of credentials")
  227. if "OK" not in wpas.request("SAVE_CONFIG"):
  228. raise Exception("Failed to save configuration file")
  229. data2 = check_config(capa, config)
  230. if data1 != data2:
  231. logger.debug(data1)
  232. logger.debug(data2)
  233. raise Exception("Unexpected configuration change")
  234. wpas.request("SET update_config 0")
  235. wpas.global_request("SET update_config 0")
  236. if "OK" in wpas.request("SAVE_CONFIG"):
  237. raise Exception("SAVE_CONFIG succeeded unexpectedly")
  238. if "OK" in wpas.global_request("SAVE_CONFIG"):
  239. raise Exception("SAVE_CONFIG (global) succeeded unexpectedly")
  240. # replace the config file with a directory to break writing/renaming
  241. os.remove(config)
  242. os.mkdir(config)
  243. wpas.request("SET update_config 1")
  244. wpas.global_request("SET update_config 1")
  245. if "OK" in wpas.request("SAVE_CONFIG"):
  246. raise Exception("SAVE_CONFIG succeeded unexpectedly")
  247. if "OK" in wpas.global_request("SAVE_CONFIG"):
  248. raise Exception("SAVE_CONFIG (global) succeeded unexpectedly")
  249. finally:
  250. try:
  251. os.rmdir(config)
  252. except:
  253. pass
  254. wpas.dump_monitor()
  255. wpas.request("SET country 00")
  256. wpas.wait_event(["CTRL-EVENT-REGDOM-CHANGE"], timeout=1)
  257. def test_wpas_config_file_wps(dev, apdev):
  258. """wpa_supplicant config file parsing/writing with WPS"""
  259. config = "/tmp/test_wpas_config_file.conf"
  260. if os.path.exists(config):
  261. os.remove(config)
  262. params = { "ssid": "test-wps", "eap_server": "1", "wps_state": "2",
  263. "skip_cred_build": "1", "extra_cred": "wps-ctrl-cred" }
  264. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  265. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  266. try:
  267. with open(config, "w") as f:
  268. f.write("update_config=1\n")
  269. wpas.interface_add("wlan5", config=config)
  270. hapd.request("WPS_PIN any 12345670")
  271. wpas.scan_for_bss(apdev[0]['bssid'], freq="2412")
  272. wpas.request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  273. ev = wpas.wait_event(["WPS-FAIL"], timeout=10)
  274. if ev is None:
  275. raise Exception("WPS-FAIL event timed out")
  276. with open(config, "r") as f:
  277. data = f.read()
  278. logger.info("Configuration file contents: " + data)
  279. if "network=" in data:
  280. raise Exception("Unexpected network block in configuration data")
  281. finally:
  282. try:
  283. os.remove(config)
  284. except:
  285. pass
  286. try:
  287. os.remove(config + ".tmp")
  288. except:
  289. pass
  290. try:
  291. os.rmdir(config)
  292. except:
  293. pass
  294. def test_wpas_config_file_wps2(dev, apdev):
  295. """wpa_supplicant config file parsing/writing with WPS (2)"""
  296. config = "/tmp/test_wpas_config_file.conf"
  297. if os.path.exists(config):
  298. os.remove(config)
  299. params = { "ssid": "test-wps", "eap_server": "1", "wps_state": "2",
  300. "skip_cred_build": "1", "extra_cred": "wps-ctrl-cred2" }
  301. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  302. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  303. try:
  304. with open(config, "w") as f:
  305. f.write("update_config=1\n")
  306. wpas.interface_add("wlan5", config=config)
  307. hapd.request("WPS_PIN any 12345670")
  308. wpas.scan_for_bss(apdev[0]['bssid'], freq="2412")
  309. wpas.request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  310. ev = wpas.wait_event(["WPS-SUCCESS"], timeout=10)
  311. if ev is None:
  312. raise Exception("WPS-SUCCESS event timed out")
  313. with open(config, "r") as f:
  314. data = f.read()
  315. logger.info("Configuration file contents: " + data)
  316. with open(config, "r") as f:
  317. data = f.read()
  318. if "network=" not in data:
  319. raise Exception("Missing network block in configuration data")
  320. if "ssid=410a420d430044" not in data:
  321. raise Exception("Unexpected ssid parameter value")
  322. finally:
  323. try:
  324. os.remove(config)
  325. except:
  326. pass
  327. try:
  328. os.remove(config + ".tmp")
  329. except:
  330. pass
  331. try:
  332. os.rmdir(config)
  333. except:
  334. pass
  335. def test_wpas_config_file_set_psk(dev):
  336. """wpa_supplicant config file parsing/writing with arbitrary PSK value"""
  337. config = "/tmp/test_wpas_config_file.conf"
  338. if os.path.exists(config):
  339. os.remove(config)
  340. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  341. try:
  342. with open(config, "w") as f:
  343. f.write("update_config=1\n")
  344. wpas.interface_add("wlan5", config=config)
  345. id = wpas.add_network()
  346. wpas.set_network_quoted(id, "ssid", "foo")
  347. if "OK" in wpas.request('SET_NETWORK %d psk "12345678"\n}\nmodel_name=foobar\nnetwork={\n#\"' % id):
  348. raise Exception("Invalid psk value accepted")
  349. if "OK" not in wpas.request("SAVE_CONFIG"):
  350. raise Exception("Failed to save configuration file")
  351. with open(config, "r") as f:
  352. data = f.read()
  353. logger.info("Configuration file contents: " + data)
  354. if "model_name" in data:
  355. raise Exception("Unexpected parameter added to configuration")
  356. wpas.interface_remove("wlan5")
  357. wpas.interface_add("wlan5", config=config)
  358. finally:
  359. try:
  360. os.remove(config)
  361. except:
  362. pass
  363. try:
  364. os.remove(config + ".tmp")
  365. except:
  366. pass
  367. try:
  368. os.rmdir(config)
  369. except:
  370. pass
  371. def test_wpas_config_file_set_cred(dev):
  372. """wpa_supplicant config file parsing/writing with arbitrary cred values"""
  373. config = "/tmp/test_wpas_config_file.conf"
  374. if os.path.exists(config):
  375. os.remove(config)
  376. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  377. try:
  378. with open(config, "w") as f:
  379. f.write("update_config=1\n")
  380. wpas.interface_add("wlan5", config=config)
  381. id = wpas.add_cred()
  382. wpas.set_cred_quoted(id, "username", "hello")
  383. fields = [ "username", "milenage", "imsi", "password", "realm",
  384. "phase1", "phase2", "provisioning_sp" ]
  385. for field in fields:
  386. if "FAIL" not in wpas.request('SET_CRED %d %s "hello"\n}\nmodel_name=foobar\ncred={\n#\"' % (id, field)):
  387. raise Exception("Invalid %s value accepted" % field)
  388. if "OK" not in wpas.request("SAVE_CONFIG"):
  389. raise Exception("Failed to save configuration file")
  390. with open(config, "r") as f:
  391. data = f.read()
  392. logger.info("Configuration file contents: " + data)
  393. if "model_name" in data:
  394. raise Exception("Unexpected parameter added to configuration")
  395. wpas.interface_remove("wlan5")
  396. wpas.interface_add("wlan5", config=config)
  397. finally:
  398. try:
  399. os.remove(config)
  400. except:
  401. pass
  402. try:
  403. os.remove(config + ".tmp")
  404. except:
  405. pass
  406. try:
  407. os.rmdir(config)
  408. except:
  409. pass
  410. def test_wpas_config_file_set_global(dev):
  411. """wpa_supplicant config file parsing/writing with arbitrary global values"""
  412. config = "/tmp/test_wpas_config_file.conf"
  413. if os.path.exists(config):
  414. os.remove(config)
  415. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  416. try:
  417. with open(config, "w") as f:
  418. f.write("update_config=1\n")
  419. wpas.interface_add("wlan5", config=config)
  420. fields = [ "model_name", "device_name", "ctrl_interface_group",
  421. "opensc_engine_path", "pkcs11_engine_path",
  422. "pkcs11_module_path", "openssl_ciphers", "pcsc_reader",
  423. "pcsc_pin", "driver_param", "manufacturer", "model_name",
  424. "model_number", "serial_number", "config_methods",
  425. "p2p_ssid_postfix", "autoscan", "ext_password_backend",
  426. "osu_dir", "wowlan_triggers", "fst_group_id",
  427. "sched_scan_plans", "non_pref_chan" ]
  428. for field in fields:
  429. if "FAIL" not in wpas.request('SET %s hello\nmodel_name=foobar' % field):
  430. raise Exception("Invalid %s value accepted" % field)
  431. if "OK" not in wpas.request("SAVE_CONFIG"):
  432. raise Exception("Failed to save configuration file")
  433. with open(config, "r") as f:
  434. data = f.read()
  435. logger.info("Configuration file contents: " + data)
  436. if "model_name" in data:
  437. raise Exception("Unexpected parameter added to configuration")
  438. wpas.interface_remove("wlan5")
  439. wpas.interface_add("wlan5", config=config)
  440. finally:
  441. try:
  442. os.remove(config)
  443. except:
  444. pass
  445. try:
  446. os.remove(config + ".tmp")
  447. except:
  448. pass
  449. try:
  450. os.rmdir(config)
  451. except:
  452. pass