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