test_wpas_config.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. ("openssl_ciphers", "DEFAULT") ]
  121. def check_config(config):
  122. with open(config, "r") as f:
  123. data = f.read()
  124. if "ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=" not in data:
  125. raise Exception("Missing ctrl_interface")
  126. if "blob-base64-foo={" not in data:
  127. raise Exception("Missing blob")
  128. if "cred={" not in data:
  129. raise Exception("Missing cred")
  130. if "network={" not in data:
  131. raise Exception("Missing network")
  132. for field, value in config_checks:
  133. if "\n" + field + "=" + value + "\n" not in data:
  134. raise Exception("Missing value: " + field)
  135. return data
  136. def test_wpas_config_file(dev, apdev, params):
  137. """wpa_supplicant config file parsing/writing"""
  138. config = os.path.join(params['logdir'], 'wpas_config_file.conf')
  139. if os.path.exists(config):
  140. try:
  141. os.remove(config)
  142. except:
  143. pass
  144. try:
  145. os.rmdir(config)
  146. except:
  147. pass
  148. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  149. try:
  150. wpas.interface_add("wlan5", config=config)
  151. initialized = True
  152. except:
  153. initialized = False
  154. if initialized:
  155. raise Exception("Missing config file did not result in an error")
  156. try:
  157. with open(config, "w") as f:
  158. f.write("update_config=1 \t\r\n")
  159. f.write("# foo\n")
  160. f.write("\n")
  161. f.write(" \t\reapol_version=2")
  162. for i in range(0, 100):
  163. f.write(" ")
  164. f.write("foo\n")
  165. f.write("device_name=name#foo\n")
  166. wpas.interface_add("wlan5", config=config)
  167. id = wpas.add_network()
  168. wpas.set_network_quoted(id, "ssid", "foo")
  169. wpas.set_network_quoted(id, "psk", "12345678")
  170. wpas.set_network(id, "bssid", "00:11:22:33:44:55")
  171. wpas.set_network(id, "proto", "RSN")
  172. wpas.set_network(id, "key_mgmt", "WPA-PSK-SHA256")
  173. wpas.set_network(id, "pairwise", "CCMP")
  174. wpas.set_network(id, "group", "CCMP")
  175. wpas.set_network(id, "auth_alg", "OPEN")
  176. id = wpas.add_cred()
  177. wpas.set_cred(id, "priority", "3")
  178. wpas.set_cred(id, "sp_priority", "6")
  179. wpas.set_cred(id, "update_identifier", "4")
  180. wpas.set_cred(id, "ocsp", "1")
  181. wpas.set_cred(id, "eap", "TTLS")
  182. wpas.set_cred(id, "req_conn_capab", "6:1234")
  183. wpas.set_cred_quoted(id, "realm", "example.com")
  184. wpas.set_cred_quoted(id, "provisioning_sp", "example.com")
  185. wpas.set_cred_quoted(id, "domain", "example.com")
  186. wpas.set_cred_quoted(id, "domain_suffix_match", "example.com")
  187. wpas.set_cred(id, "roaming_consortium", "112233")
  188. wpas.set_cred(id, "required_roaming_consortium", "112233")
  189. wpas.set_cred_quoted(id, "roaming_partner",
  190. "roaming.example.net,1,127,*")
  191. wpas.set_cred_quoted(id, "ca_cert", "/tmp/ca.pem")
  192. wpas.set_cred_quoted(id, "username", "user")
  193. wpas.set_cred_quoted(id, "password", "secret")
  194. ev = wpas.wait_event(["CRED-MODIFIED 0 password"])
  195. wpas.request("SET blob foo 12345678")
  196. for field, value in config_checks:
  197. wpas.set(field, value)
  198. if "OK" not in wpas.request("SAVE_CONFIG"):
  199. raise Exception("Failed to save configuration file")
  200. if "OK" not in wpas.global_request("SAVE_CONFIG"):
  201. raise Exception("Failed to save configuration file")
  202. wpas.interface_remove("wlan5")
  203. data1 = check_config(config)
  204. wpas.interface_add("wlan5", config=config)
  205. if len(wpas.list_networks()) != 1:
  206. raise Exception("Unexpected number of networks")
  207. if len(wpas.request("LIST_CREDS").splitlines()) != 2:
  208. raise Exception("Unexpected number of credentials")
  209. if "OK" not in wpas.request("SAVE_CONFIG"):
  210. raise Exception("Failed to save configuration file")
  211. data2 = check_config(config)
  212. if data1 != data2:
  213. logger.debug(data1)
  214. logger.debug(data2)
  215. raise Exception("Unexpected configuration change")
  216. wpas.request("SET update_config 0")
  217. wpas.global_request("SET update_config 0")
  218. if "OK" in wpas.request("SAVE_CONFIG"):
  219. raise Exception("SAVE_CONFIG succeeded unexpectedly")
  220. if "OK" in wpas.global_request("SAVE_CONFIG"):
  221. raise Exception("SAVE_CONFIG (global) succeeded unexpectedly")
  222. # replace the config file with a directory to break writing/renaming
  223. os.remove(config)
  224. os.mkdir(config)
  225. wpas.request("SET update_config 1")
  226. wpas.global_request("SET update_config 1")
  227. if "OK" in wpas.request("SAVE_CONFIG"):
  228. raise Exception("SAVE_CONFIG succeeded unexpectedly")
  229. if "OK" in wpas.global_request("SAVE_CONFIG"):
  230. raise Exception("SAVE_CONFIG (global) succeeded unexpectedly")
  231. finally:
  232. try:
  233. os.rmdir(config)
  234. except:
  235. pass
  236. wpas.dump_monitor()
  237. wpas.request("SET country 00")
  238. wpas.wait_event(["CTRL-EVENT-REGDOM-CHANGE"], timeout=1)
  239. def test_wpas_config_file_wps(dev, apdev):
  240. """wpa_supplicant config file parsing/writing with WPS"""
  241. config = "/tmp/test_wpas_config_file.conf"
  242. if os.path.exists(config):
  243. os.remove(config)
  244. params = { "ssid": "test-wps", "eap_server": "1", "wps_state": "2",
  245. "skip_cred_build": "1", "extra_cred": "wps-ctrl-cred" }
  246. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  247. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  248. try:
  249. with open(config, "w") as f:
  250. f.write("update_config=1\n")
  251. wpas.interface_add("wlan5", config=config)
  252. hapd.request("WPS_PIN any 12345670")
  253. wpas.scan_for_bss(apdev[0]['bssid'], freq="2412")
  254. wpas.request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  255. ev = wpas.wait_event(["WPS-FAIL"], timeout=10)
  256. if ev is None:
  257. raise Exception("WPS-FAIL event timed out")
  258. with open(config, "r") as f:
  259. data = f.read()
  260. logger.info("Configuration file contents: " + data)
  261. if "network=" in data:
  262. raise Exception("Unexpected network block in configuration data")
  263. finally:
  264. try:
  265. os.remove(config)
  266. except:
  267. pass
  268. try:
  269. os.remove(config + ".tmp")
  270. except:
  271. pass
  272. try:
  273. os.rmdir(config)
  274. except:
  275. pass
  276. def test_wpas_config_file_wps2(dev, apdev):
  277. """wpa_supplicant config file parsing/writing with WPS (2)"""
  278. config = "/tmp/test_wpas_config_file.conf"
  279. if os.path.exists(config):
  280. os.remove(config)
  281. params = { "ssid": "test-wps", "eap_server": "1", "wps_state": "2",
  282. "skip_cred_build": "1", "extra_cred": "wps-ctrl-cred2" }
  283. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  284. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  285. try:
  286. with open(config, "w") as f:
  287. f.write("update_config=1\n")
  288. wpas.interface_add("wlan5", config=config)
  289. hapd.request("WPS_PIN any 12345670")
  290. wpas.scan_for_bss(apdev[0]['bssid'], freq="2412")
  291. wpas.request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  292. ev = wpas.wait_event(["WPS-SUCCESS"], timeout=10)
  293. if ev is None:
  294. raise Exception("WPS-SUCCESS event timed out")
  295. with open(config, "r") as f:
  296. data = f.read()
  297. logger.info("Configuration file contents: " + data)
  298. with open(config, "r") as f:
  299. data = f.read()
  300. if "network=" not in data:
  301. raise Exception("Missing network block in configuration data")
  302. if "ssid=410a420d430044" not in data:
  303. raise Exception("Unexpected ssid parameter value")
  304. finally:
  305. try:
  306. os.remove(config)
  307. except:
  308. pass
  309. try:
  310. os.remove(config + ".tmp")
  311. except:
  312. pass
  313. try:
  314. os.rmdir(config)
  315. except:
  316. pass
  317. def test_wpas_config_file_set_psk(dev):
  318. """wpa_supplicant config file parsing/writing with arbitrary PSK value"""
  319. config = "/tmp/test_wpas_config_file.conf"
  320. if os.path.exists(config):
  321. os.remove(config)
  322. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  323. try:
  324. with open(config, "w") as f:
  325. f.write("update_config=1\n")
  326. wpas.interface_add("wlan5", config=config)
  327. id = wpas.add_network()
  328. wpas.set_network_quoted(id, "ssid", "foo")
  329. if "OK" in wpas.request('SET_NETWORK %d psk "12345678"\n}\nmodel_name=foobar\nnetwork={\n#\"' % id):
  330. raise Exception("Invalid psk value accepted")
  331. if "OK" not in wpas.request("SAVE_CONFIG"):
  332. raise Exception("Failed to save configuration file")
  333. with open(config, "r") as f:
  334. data = f.read()
  335. logger.info("Configuration file contents: " + data)
  336. if "model_name" in data:
  337. raise Exception("Unexpected parameter added to configuration")
  338. wpas.interface_remove("wlan5")
  339. wpas.interface_add("wlan5", config=config)
  340. finally:
  341. try:
  342. os.remove(config)
  343. except:
  344. pass
  345. try:
  346. os.remove(config + ".tmp")
  347. except:
  348. pass
  349. try:
  350. os.rmdir(config)
  351. except:
  352. pass
  353. def test_wpas_config_file_set_cred(dev):
  354. """wpa_supplicant config file parsing/writing with arbitrary cred values"""
  355. config = "/tmp/test_wpas_config_file.conf"
  356. if os.path.exists(config):
  357. os.remove(config)
  358. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  359. try:
  360. with open(config, "w") as f:
  361. f.write("update_config=1\n")
  362. wpas.interface_add("wlan5", config=config)
  363. id = wpas.add_cred()
  364. wpas.set_cred_quoted(id, "username", "hello")
  365. fields = [ "username", "milenage", "imsi", "password", "realm",
  366. "phase1", "phase2", "provisioning_sp" ]
  367. for field in fields:
  368. if "FAIL" not in wpas.request('SET_CRED %d %s "hello"\n}\nmodel_name=foobar\ncred={\n#\"' % (id, field)):
  369. raise Exception("Invalid %s value accepted" % field)
  370. if "OK" not in wpas.request("SAVE_CONFIG"):
  371. raise Exception("Failed to save configuration file")
  372. with open(config, "r") as f:
  373. data = f.read()
  374. logger.info("Configuration file contents: " + data)
  375. if "model_name" in data:
  376. raise Exception("Unexpected parameter added to configuration")
  377. wpas.interface_remove("wlan5")
  378. wpas.interface_add("wlan5", config=config)
  379. finally:
  380. try:
  381. os.remove(config)
  382. except:
  383. pass
  384. try:
  385. os.remove(config + ".tmp")
  386. except:
  387. pass
  388. try:
  389. os.rmdir(config)
  390. except:
  391. pass
  392. def test_wpas_config_file_set_global(dev):
  393. """wpa_supplicant config file parsing/writing with arbitrary global values"""
  394. config = "/tmp/test_wpas_config_file.conf"
  395. if os.path.exists(config):
  396. os.remove(config)
  397. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  398. try:
  399. with open(config, "w") as f:
  400. f.write("update_config=1\n")
  401. wpas.interface_add("wlan5", config=config)
  402. fields = [ "model_name", "device_name", "ctrl_interface_group",
  403. "opensc_engine_path", "pkcs11_engine_path",
  404. "pkcs11_module_path", "openssl_ciphers", "pcsc_reader",
  405. "pcsc_pin", "driver_param", "manufacturer", "model_name",
  406. "model_number", "serial_number", "config_methods",
  407. "p2p_ssid_postfix", "autoscan", "ext_password_backend",
  408. "osu_dir", "wowlan_triggers", "fst_group_id",
  409. "sched_scan_plans", "non_pref_chan" ]
  410. for field in fields:
  411. if "FAIL" not in wpas.request('SET %s hello\nmodel_name=foobar' % field):
  412. raise Exception("Invalid %s value accepted" % field)
  413. if "OK" not in wpas.request("SAVE_CONFIG"):
  414. raise Exception("Failed to save configuration file")
  415. with open(config, "r") as f:
  416. data = f.read()
  417. logger.info("Configuration file contents: " + data)
  418. if "model_name" in data:
  419. raise Exception("Unexpected parameter added to configuration")
  420. wpas.interface_remove("wlan5")
  421. wpas.interface_add("wlan5", config=config)
  422. finally:
  423. try:
  424. os.remove(config)
  425. except:
  426. pass
  427. try:
  428. os.remove(config + ".tmp")
  429. except:
  430. pass
  431. try:
  432. os.rmdir(config)
  433. except:
  434. pass