wps_module_tests.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * WPS module tests
  3. * Copyright (c) 2014, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "wps_attr_parse.h"
  11. struct wps_attr_parse_test {
  12. const char *data;
  13. int result;
  14. int extra;
  15. };
  16. const struct wps_attr_parse_test wps_attr_parse_test_cases[] = {
  17. /* Empty message */
  18. { "", 0, 0 },
  19. /* Truncated attribute header */
  20. { "10", -1, 0 },
  21. { "1010", -1, 0 },
  22. { "101000", -1, 0 },
  23. /* Attribute overflow */
  24. { "10100001", -1, 0 },
  25. #ifdef CONFIG_WPS_STRICT
  26. { "10270000001057000101", -1, 0 },
  27. { "1027000010570001010000000000", -1, 0 },
  28. #else /* CONFIG_WPS_STRICT */
  29. /* Network Key workaround */
  30. { "10270000001057000101", 0, 1 },
  31. { "10230000001057000101", -1, 0 },
  32. { "10270000101057000101", -1, 0 },
  33. /* Mac OS X 10.6 padding workaround */
  34. { "1027000010570001010000000000", 0, 1 },
  35. { "1027000010570001010000000000000001000000", -1, 0 },
  36. #endif /* CONFIG_WPS_STRICT */
  37. /* Version */
  38. { "104a000110", 0, 0 },
  39. { "104a0000", -1, 0 },
  40. /* Message Type */
  41. { "1022000101", 0, 0 },
  42. { "10220000", -1, 0 },
  43. /* Enrollee Nonce */
  44. { "101a001000112233445566778899aabbccddeeff", 0, 0 },
  45. { "101a00111122334455667788990011223344556677", -1, 0 },
  46. /* Registrar Nonce */
  47. { "1039001000112233445566778899aabbccddeeff", 0, 0 },
  48. { "103900111122334455667788990011223344556677", -1, 0 },
  49. /* UUID-E */
  50. { "1047001000112233445566778899aabbccddeeff", 0, 0 },
  51. { "10470000", -1, 0 },
  52. { "104700111122334455667788990011223344556677", -1, 0 },
  53. /* UUID-R */
  54. { "1048001000112233445566778899aabbccddeeff", 0, 0 },
  55. { "10480000", -1, 0 },
  56. { "104800111122334455667788990011223344556677", -1, 0 },
  57. /* Auth Type Flags */
  58. { "100400021122", 0, 0 },
  59. { "10040001ff", -1, 0 },
  60. /* Encr Type Flags */
  61. { "101000021122", 0, 0 },
  62. { "10100001ff", -1, 0 },
  63. /* Connection Type Flags */
  64. { "100d0001ff", 0, 0 },
  65. { "100d0002ffff", -1, 0 },
  66. /* Config Methods */
  67. { "10080002ffff", 0, 0 },
  68. { "10080001ff", -1, 0 },
  69. /* Selected Registrar Config Methods */
  70. { "10530002ffff", 0, 0 },
  71. { "10530001ff", -1, 0 },
  72. /* Primary Device Type */
  73. { "105400081122334455667788", 0, 0 },
  74. { "105400111122334455667788990011223344556677", -1, 0 },
  75. /* RF Bands */
  76. { "103c0001ff", 0, 0 },
  77. { "103c0002ffff", -1, 0 },
  78. /* Association State */
  79. { "10020002ffff", 0, 0 },
  80. { "10020001ff", -1, 0 },
  81. /* Config Error */
  82. { "100900020001", 0, 0 },
  83. { "10090001ff", -1, 0 },
  84. /* Device Password ID */
  85. { "101200020004", 0, 0 },
  86. { "10120001ff", -1, 0 },
  87. /* OOB Device Password */
  88. { "102c001611223344556677889900112233445566778899000007", 0, 0 },
  89. { "102c0036112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455667788990011223344", 0, 0 },
  90. { "102c0001ff", -1, 0 },
  91. { "102c003711223344556677889900112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455", -1, 0 },
  92. { "102c002511223344556677889900112233445566778899001122334455667788990011223344556677", -1, 0 },
  93. /* OS Version */
  94. { "102d000411223344", 0, 0 },
  95. { "102d00111122334455667788990011223344556677", -1, 0 },
  96. /* WPS State */
  97. { "1044000101", 0, 0 },
  98. { "10440002ffff", -1, 0 },
  99. /* Authenticator */
  100. { "100500081122334455667788", 0, 0 },
  101. { "10050000", -1, 0 },
  102. { "100500111122334455667788990011223344556677", -1, 0 },
  103. /* R-Hash1 */
  104. { "103d00201122334455667788990011223344556677889900112233445566778899001122", 0, 0 },
  105. { "103d0000", -1, 0 },
  106. { "103d0021112233445566778899001122334455667788990011223344556677889900112233", -1, 0 },
  107. /* R-Hash2 */
  108. { "103e00201122334455667788990011223344556677889900112233445566778899001122", 0, 0 },
  109. { "103e0000", -1, 0 },
  110. { "103e0021112233445566778899001122334455667788990011223344556677889900112233", -1, 0 },
  111. /* E-Hash1 */
  112. { "101400201122334455667788990011223344556677889900112233445566778899001122", 0, 0 },
  113. { "10140000", -1, 0 },
  114. { "10140021112233445566778899001122334455667788990011223344556677889900112233", -1, 0 },
  115. /* E-Hash2 */
  116. { "101500201122334455667788990011223344556677889900112233445566778899001122", 0, 0 },
  117. { "10150000", -1, 0 },
  118. { "10150021112233445566778899001122334455667788990011223344556677889900112233", -1, 0 },
  119. /* R-SNonce1 */
  120. { "103f001011223344556677889900112233445566", 0, 0 },
  121. { "103f0000", -1, 0 },
  122. { "103f00111122334455667788990011223344556677", -1, 0 },
  123. /* R-SNonce2 */
  124. { "1040001011223344556677889900112233445566", 0, 0 },
  125. { "10400000", -1, 0 },
  126. { "104000111122334455667788990011223344556677", -1, 0 },
  127. /* E-SNonce1 */
  128. { "1016001011223344556677889900112233445566", 0, 0 },
  129. { "10160000", -1, 0 },
  130. { "101600111122334455667788990011223344556677", -1, 0 },
  131. /* E-SNonce2 */
  132. { "1017001011223344556677889900112233445566", 0, 0 },
  133. { "10170000", -1, 0 },
  134. { "101700111122334455667788990011223344556677", -1, 0 },
  135. /* Key Wrap Authenticator */
  136. { "101e00081122334455667788", 0, 0 },
  137. { "101e0000", -1, 0 },
  138. { "101e0009112233445566778899", -1, 0 },
  139. /* Authentication Type */
  140. { "100300020001", 0, 0 },
  141. { "10030001ff", -1, 0 },
  142. /* Encryption Type */
  143. { "100f00020001", 0, 0 },
  144. { "100f0001ff", -1, 0 },
  145. /* Network Index */
  146. { "1026000101", 0, 0 },
  147. { "10260002ffff", -1, 0 },
  148. /* Network Key Index */
  149. { "1028000101", 0, 3 },
  150. { "10280002ffff", -1, 0 },
  151. /* MAC Address */
  152. { "10200006112233445566", 0, 0 },
  153. { "10200000", -1, 0 },
  154. { "1020000711223344556677", -1, 0 },
  155. /* Selected Registrar */
  156. { "1041000101", 0, 0 },
  157. { "10410002ffff", -1, 0 },
  158. /* Request Type */
  159. { "103a000101", 0, 0 },
  160. { "103a0002ffff", -1, 0 },
  161. /* Response Type */
  162. { "103b000101", 0, 0 },
  163. { "103b0002ffff", -1, 0 },
  164. /* Manufacturer */
  165. { "10210000", 0, 0 },
  166. /* Model Name */
  167. { "10230000", 0, 0 },
  168. /* Model Number */
  169. { "10240000", 0, 0 },
  170. /* Serial Number */
  171. { "10420000", 0, 0 },
  172. /* Device Name */
  173. { "10110000", 0, 0 },
  174. /* Public Key */
  175. { "10320000", 0, 0 },
  176. /* Enc Settings */
  177. { "10180000", 0, 0 },
  178. /* SSID */
  179. { "10450000", 0, 0 },
  180. /* AP Setup Locked */
  181. { "1057000101", 0, 0 },
  182. { "10570002ffff", -1, 0 },
  183. /* Requested Device Type */
  184. { "106a00081122334455667788", 0, 0 },
  185. { "106a0000", -1, 0 },
  186. { "106a0009112233445566778899", -1, 0 },
  187. /* More than maximum Requested Device Type attributes */
  188. { "106a00081122334455667788106a00081122334455667788106a00081122334455667788106a00081122334455667788106a00081122334455667788106a00081122334455667788106a00081122334455667788106a00081122334455667788106a00081122334455667788106a00081122334455667788106a00081122334455667788106a00081122334455667788", 0, 4 },
  189. /* Secondary Device Type List */
  190. { "105500081122334455667788", 0, 0 },
  191. { "1055000711223344556677", -1, 0 },
  192. { "1055008811223344556677889900112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677889900112233445566", -1, 0 },
  193. /* AP Channel */
  194. { "100100020001", 0, 0 },
  195. { "1001000101", -1, 0 },
  196. /* Skip invalid Vendor Extension */
  197. { "10490000", 0, 0 },
  198. { "1049000100", 0, 0 },
  199. { "104900020000", 0, 0 },
  200. /* Too long unknown vendor extension */
  201. { "10490401"
  202. "112233445566778899001122334455667788990011223344556677889900"
  203. "112233445566778899001122334455667788990011223344556677889900"
  204. "112233445566778899001122334455667788990011223344556677889900"
  205. "112233445566778899001122334455667788990011223344556677889900"
  206. "112233445566778899001122334455667788990011223344556677889900"
  207. "112233445566778899001122334455667788990011223344556677889900"
  208. "112233445566778899001122334455667788990011223344556677889900"
  209. "112233445566778899001122334455667788990011223344556677889900"
  210. "112233445566778899001122334455667788990011223344556677889900"
  211. "112233445566778899001122334455667788990011223344556677889900"
  212. "112233445566778899001122334455667788990011223344556677889900"
  213. "112233445566778899001122334455667788990011223344556677889900"
  214. "112233445566778899001122334455667788990011223344556677889900"
  215. "112233445566778899001122334455667788990011223344556677889900"
  216. "112233445566778899001122334455667788990011223344556677889900"
  217. "112233445566778899001122334455667788990011223344556677889900"
  218. "112233445566778899001122334455667788990011223344556677889900"
  219. "112233445566778899001122334455667788990011223344556677889900"
  220. "112233445566778899001122334455667788990011223344556677889900"
  221. "112233445566778899001122334455667788990011223344556677889900"
  222. "112233445566778899001122334455667788990011223344556677889900"
  223. "112233445566778899001122334455667788990011223344556677889900"
  224. "112233445566778899001122334455667788990011223344556677889900"
  225. "112233445566778899001122334455667788990011223344556677889900"
  226. "112233445566778899001122334455667788990011223344556677889900"
  227. "112233445566778899001122334455667788990011223344556677889900"
  228. "112233445566778899001122334455667788990011223344556677889900"
  229. "112233445566778899001122334455667788990011223344556677889900"
  230. "112233445566778899001122334455667788990011223344556677889900"
  231. "112233445566778899001122334455667788990011223344556677889900"
  232. "112233445566778899001122334455667788990011223344556677889900"
  233. "112233445566778899001122334455667788990011223344556677889900"
  234. "112233445566778899001122334455667788990011223344556677889900"
  235. "112233445566778899001122334455667788990011223344556677889900"
  236. "1122334455", -1, 0 },
  237. /* Maximum unknown vendor extensions */
  238. { "10490003111111104900032222221049000333333310490003444444104900035555551049000366666610490003777777104900038888881049000399999910490003AAAAAA", 0, 5 },
  239. /* More than maximum unknown vendor extensions */
  240. { "10490003111111104900032222221049000333333310490003444444104900035555551049000366666610490003777777104900038888881049000399999910490003AAAAAA10490003BBBBBB", -1, 0 },
  241. /* WFA vendor extensions */
  242. { "1049000300372a", 0, 0 },
  243. { "1049000400372a00", 0, 0 },
  244. { "1049000500372a0001", 0, 0 },
  245. { "1049001600372a0001ff0100020101030101040101ff00fe0101", 0, 6 },
  246. /* Invalid Version2 length */
  247. { "1049000500372a0000", -1, 0 },
  248. /* Invalid Network Key Shareable length */
  249. { "1049000500372a0200", -1, 0 },
  250. /* Invalid Requedt To Enroll length */
  251. { "1049000500372a0300", -1, 0 },
  252. /* Invalid Settings Delay Time length */
  253. { "1049000500372a0400", -1, 0 },
  254. /* More than maximum Credential attributes */
  255. { "100e0000100e0000100e0000100e0000100e0000100e0000100e0000100e0000100e0000100e0000100e0000100e0000", 0, 2 },
  256. };
  257. static int wps_attr_parse_tests(void)
  258. {
  259. struct wps_parse_attr attr;
  260. unsigned int i;
  261. int ret = 0;
  262. wpa_printf(MSG_INFO, "WPS attribute parsing tests");
  263. for (i = 0; i < ARRAY_SIZE(wps_attr_parse_test_cases); i++) {
  264. struct wpabuf *buf;
  265. size_t len;
  266. const struct wps_attr_parse_test *test =
  267. &wps_attr_parse_test_cases[i];
  268. len = os_strlen(test->data) / 2;
  269. buf = wpabuf_alloc(len);
  270. if (buf == NULL)
  271. return -1;
  272. if (hexstr2bin(test->data, wpabuf_put(buf, len), len) < 0) {
  273. wpabuf_free(buf);
  274. return -1;
  275. }
  276. if (wps_parse_msg(buf, &attr) != test->result) {
  277. wpa_printf(MSG_ERROR, "WPS attribute parsing test %u failed: %s",
  278. i, test->data);
  279. ret = -1;
  280. }
  281. switch (test->extra) {
  282. case 1:
  283. if (!attr.network_key || !attr.ap_setup_locked)
  284. ret = -1;
  285. break;
  286. case 2:
  287. if (attr.num_cred != MAX_CRED_COUNT)
  288. ret = -1;
  289. break;
  290. case 3:
  291. if (!attr.network_key_idx)
  292. ret = -1;
  293. break;
  294. case 4:
  295. if (attr.num_req_dev_type != MAX_REQ_DEV_TYPE_COUNT)
  296. ret = -1;
  297. break;
  298. case 5:
  299. if (attr.num_vendor_ext != MAX_WPS_PARSE_VENDOR_EXT)
  300. ret = -1;
  301. break;
  302. case 6:
  303. if (!attr.version2 ||
  304. !attr.authorized_macs ||
  305. !attr.network_key_shareable ||
  306. !attr.request_to_enroll ||
  307. !attr.settings_delay_time)
  308. ret = -1;
  309. break;
  310. }
  311. wpabuf_free(buf);
  312. }
  313. return ret;
  314. }
  315. int wps_module_tests(void)
  316. {
  317. int ret = 0;
  318. wpa_printf(MSG_INFO, "WPS module tests");
  319. if (wps_attr_parse_tests() < 0)
  320. ret = -1;
  321. return ret;
  322. }