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