1 /*
2 * Wi-Fi Protected Setup - device attributes
3 * Copyright (c) 2008, 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 "includes.h"
10
11 #include "common.h"
12 #include "wps_i.h"
13 #include "wps_dev_attr.h"
14
15
wps_build_manufacturer(struct wps_device_data * dev,struct wpabuf * msg)16 int wps_build_manufacturer(struct wps_device_data *dev, struct wpabuf *msg)
17 {
18 size_t len;
19 wpa_printf(MSG_DEBUG, "WPS: * Manufacturer");
20 wpabuf_put_be16(msg, ATTR_MANUFACTURER);
21 len = dev->manufacturer ? os_strlen(dev->manufacturer) : 0;
22 #ifndef CONFIG_WPS_STRICT
23 if (len == 0) {
24 /*
25 * Some deployed WPS implementations fail to parse zero-length
26 * attributes. As a workaround, send a space character if the
27 * device attribute string is empty.
28 */
29 wpabuf_put_be16(msg, 1);
30 wpabuf_put_u8(msg, ' ');
31 return 0;
32 }
33 #endif /* CONFIG_WPS_STRICT */
34 wpabuf_put_be16(msg, len);
35 wpabuf_put_data(msg, dev->manufacturer, len);
36 return 0;
37 }
38
39
wps_build_model_name(struct wps_device_data * dev,struct wpabuf * msg)40 int wps_build_model_name(struct wps_device_data *dev, struct wpabuf *msg)
41 {
42 size_t len;
43 wpa_printf(MSG_DEBUG, "WPS: * Model Name");
44 wpabuf_put_be16(msg, ATTR_MODEL_NAME);
45 len = dev->model_name ? os_strlen(dev->model_name) : 0;
46 #ifndef CONFIG_WPS_STRICT
47 if (len == 0) {
48 /*
49 * Some deployed WPS implementations fail to parse zero-length
50 * attributes. As a workaround, send a space character if the
51 * device attribute string is empty.
52 */
53 wpabuf_put_be16(msg, 1);
54 wpabuf_put_u8(msg, ' ');
55 return 0;
56 }
57 #endif /* CONFIG_WPS_STRICT */
58 wpabuf_put_be16(msg, len);
59 wpabuf_put_data(msg, dev->model_name, len);
60 return 0;
61 }
62
63
wps_build_model_number(struct wps_device_data * dev,struct wpabuf * msg)64 int wps_build_model_number(struct wps_device_data *dev, struct wpabuf *msg)
65 {
66 size_t len;
67 wpa_printf(MSG_DEBUG, "WPS: * Model Number");
68 wpabuf_put_be16(msg, ATTR_MODEL_NUMBER);
69 len = dev->model_number ? os_strlen(dev->model_number) : 0;
70 #ifndef CONFIG_WPS_STRICT
71 if (len == 0) {
72 /*
73 * Some deployed WPS implementations fail to parse zero-length
74 * attributes. As a workaround, send a space character if the
75 * device attribute string is empty.
76 */
77 wpabuf_put_be16(msg, 1);
78 wpabuf_put_u8(msg, ' ');
79 return 0;
80 }
81 #endif /* CONFIG_WPS_STRICT */
82 wpabuf_put_be16(msg, len);
83 wpabuf_put_data(msg, dev->model_number, len);
84 return 0;
85 }
86
87
wps_build_serial_number(struct wps_device_data * dev,struct wpabuf * msg)88 static int wps_build_serial_number(struct wps_device_data *dev,
89 struct wpabuf *msg)
90 {
91 size_t len;
92 wpa_printf(MSG_DEBUG, "WPS: * Serial Number");
93 wpabuf_put_be16(msg, ATTR_SERIAL_NUMBER);
94 len = dev->serial_number ? os_strlen(dev->serial_number) : 0;
95 #ifndef CONFIG_WPS_STRICT
96 if (len == 0) {
97 /*
98 * Some deployed WPS implementations fail to parse zero-length
99 * attributes. As a workaround, send a space character if the
100 * device attribute string is empty.
101 */
102 wpabuf_put_be16(msg, 1);
103 wpabuf_put_u8(msg, ' ');
104 return 0;
105 }
106 #endif /* CONFIG_WPS_STRICT */
107 wpabuf_put_be16(msg, len);
108 wpabuf_put_data(msg, dev->serial_number, len);
109 return 0;
110 }
111
112
wps_build_primary_dev_type(struct wps_device_data * dev,struct wpabuf * msg)113 int wps_build_primary_dev_type(struct wps_device_data *dev, struct wpabuf *msg)
114 {
115 wpa_printf(MSG_DEBUG, "WPS: * Primary Device Type");
116 wpabuf_put_be16(msg, ATTR_PRIMARY_DEV_TYPE);
117 wpabuf_put_be16(msg, WPS_DEV_TYPE_LEN);
118 wpabuf_put_data(msg, dev->pri_dev_type, WPS_DEV_TYPE_LEN);
119 return 0;
120 }
121
122
wps_build_secondary_dev_type(struct wps_device_data * dev,struct wpabuf * msg)123 int wps_build_secondary_dev_type(struct wps_device_data *dev,
124 struct wpabuf *msg)
125 {
126 if (!dev->num_sec_dev_types)
127 return 0;
128
129 wpa_printf(MSG_DEBUG, "WPS: * Secondary Device Type");
130 wpabuf_put_be16(msg, ATTR_SECONDARY_DEV_TYPE_LIST);
131 wpabuf_put_be16(msg, WPS_DEV_TYPE_LEN * dev->num_sec_dev_types);
132 wpabuf_put_data(msg, dev->sec_dev_type,
133 WPS_DEV_TYPE_LEN * dev->num_sec_dev_types);
134
135 return 0;
136 }
137
138
wps_build_req_dev_type(struct wps_device_data * dev,struct wpabuf * msg,unsigned int num_req_dev_types,const u8 * req_dev_types)139 int wps_build_req_dev_type(struct wps_device_data *dev, struct wpabuf *msg,
140 unsigned int num_req_dev_types,
141 const u8 *req_dev_types)
142 {
143 unsigned int i;
144
145 for (i = 0; i < num_req_dev_types; i++) {
146 wpa_hexdump(MSG_DEBUG, "WPS: * Requested Device Type",
147 req_dev_types + i * WPS_DEV_TYPE_LEN,
148 WPS_DEV_TYPE_LEN);
149 wpabuf_put_be16(msg, ATTR_REQUESTED_DEV_TYPE);
150 wpabuf_put_be16(msg, WPS_DEV_TYPE_LEN);
151 wpabuf_put_data(msg, req_dev_types + i * WPS_DEV_TYPE_LEN,
152 WPS_DEV_TYPE_LEN);
153 }
154
155 return 0;
156 }
157
158
wps_build_dev_name(struct wps_device_data * dev,struct wpabuf * msg)159 int wps_build_dev_name(struct wps_device_data *dev, struct wpabuf *msg)
160 {
161 size_t len;
162 wpa_printf(MSG_DEBUG, "WPS: * Device Name");
163 wpabuf_put_be16(msg, ATTR_DEV_NAME);
164 len = dev->device_name ? os_strlen(dev->device_name) : 0;
165 #ifndef CONFIG_WPS_STRICT
166 if (len == 0) {
167 /*
168 * Some deployed WPS implementations fail to parse zero-length
169 * attributes. As a workaround, send a space character if the
170 * device attribute string is empty.
171 */
172 wpabuf_put_be16(msg, 1);
173 wpabuf_put_u8(msg, ' ');
174 return 0;
175 }
176 #endif /* CONFIG_WPS_STRICT */
177 wpabuf_put_be16(msg, len);
178 wpabuf_put_data(msg, dev->device_name, len);
179 return 0;
180 }
181
182
wps_build_device_attrs(struct wps_device_data * dev,struct wpabuf * msg)183 int wps_build_device_attrs(struct wps_device_data *dev, struct wpabuf *msg)
184 {
185 if (wps_build_manufacturer(dev, msg) ||
186 wps_build_model_name(dev, msg) ||
187 wps_build_model_number(dev, msg) ||
188 wps_build_serial_number(dev, msg) ||
189 wps_build_primary_dev_type(dev, msg) ||
190 wps_build_dev_name(dev, msg))
191 return -1;
192 return 0;
193 }
194
195
wps_build_os_version(struct wps_device_data * dev,struct wpabuf * msg)196 int wps_build_os_version(struct wps_device_data *dev, struct wpabuf *msg)
197 {
198 wpa_printf(MSG_DEBUG, "WPS: * OS Version");
199 wpabuf_put_be16(msg, ATTR_OS_VERSION);
200 wpabuf_put_be16(msg, 4);
201 wpabuf_put_be32(msg, 0x80000000 | dev->os_version);
202 return 0;
203 }
204
205
wps_build_rf_bands(struct wps_device_data * dev,struct wpabuf * msg)206 int wps_build_rf_bands(struct wps_device_data *dev, struct wpabuf *msg)
207 {
208 wpa_printf(MSG_DEBUG, "WPS: * RF Bands (%x)", dev->rf_bands);
209 wpabuf_put_be16(msg, ATTR_RF_BANDS);
210 wpabuf_put_be16(msg, 1);
211 wpabuf_put_u8(msg, dev->rf_bands);
212 return 0;
213 }
214
215
wps_build_vendor_ext(struct wps_device_data * dev,struct wpabuf * msg)216 int wps_build_vendor_ext(struct wps_device_data *dev, struct wpabuf *msg)
217 {
218 int i;
219
220 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
221 if (dev->vendor_ext[i] == NULL)
222 continue;
223 wpa_hexdump(MSG_DEBUG, "WPS: * Vendor Extension",
224 wpabuf_head_u8(dev->vendor_ext[i]),
225 wpabuf_len(dev->vendor_ext[i]));
226 wpabuf_put_be16(msg, ATTR_VENDOR_EXT);
227 wpabuf_put_be16(msg, wpabuf_len(dev->vendor_ext[i]));
228 wpabuf_put_buf(msg, dev->vendor_ext[i]);
229 }
230
231 return 0;
232 }
233
234
wps_process_manufacturer(struct wps_device_data * dev,const u8 * str,size_t str_len)235 static int wps_process_manufacturer(struct wps_device_data *dev, const u8 *str,
236 size_t str_len)
237 {
238 if (str == NULL) {
239 wpa_printf(MSG_DEBUG, "WPS: No Manufacturer received");
240 return -1;
241 }
242
243 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer", str, str_len);
244
245 os_free(dev->manufacturer);
246 dev->manufacturer = os_malloc(str_len + 1);
247 if (dev->manufacturer == NULL)
248 return -1;
249 os_memcpy(dev->manufacturer, str, str_len);
250 dev->manufacturer[str_len] = '\0';
251
252 return 0;
253 }
254
255
wps_process_model_name(struct wps_device_data * dev,const u8 * str,size_t str_len)256 static int wps_process_model_name(struct wps_device_data *dev, const u8 *str,
257 size_t str_len)
258 {
259 if (str == NULL) {
260 wpa_printf(MSG_DEBUG, "WPS: No Model Name received");
261 return -1;
262 }
263
264 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name", str, str_len);
265
266 os_free(dev->model_name);
267 dev->model_name = os_malloc(str_len + 1);
268 if (dev->model_name == NULL)
269 return -1;
270 os_memcpy(dev->model_name, str, str_len);
271 dev->model_name[str_len] = '\0';
272
273 return 0;
274 }
275
276
wps_process_model_number(struct wps_device_data * dev,const u8 * str,size_t str_len)277 static int wps_process_model_number(struct wps_device_data *dev, const u8 *str,
278 size_t str_len)
279 {
280 if (str == NULL) {
281 wpa_printf(MSG_DEBUG, "WPS: No Model Number received");
282 return -1;
283 }
284
285 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number", str, str_len);
286
287 os_free(dev->model_number);
288 dev->model_number = os_malloc(str_len + 1);
289 if (dev->model_number == NULL)
290 return -1;
291 os_memcpy(dev->model_number, str, str_len);
292 dev->model_number[str_len] = '\0';
293
294 return 0;
295 }
296
297
wps_process_serial_number(struct wps_device_data * dev,const u8 * str,size_t str_len)298 static int wps_process_serial_number(struct wps_device_data *dev,
299 const u8 *str, size_t str_len)
300 {
301 if (str == NULL) {
302 wpa_printf(MSG_DEBUG, "WPS: No Serial Number received");
303 return -1;
304 }
305
306 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number", str, str_len);
307
308 os_free(dev->serial_number);
309 dev->serial_number = os_malloc(str_len + 1);
310 if (dev->serial_number == NULL)
311 return -1;
312 os_memcpy(dev->serial_number, str, str_len);
313 dev->serial_number[str_len] = '\0';
314
315 return 0;
316 }
317
318
wps_process_dev_name(struct wps_device_data * dev,const u8 * str,size_t str_len)319 static int wps_process_dev_name(struct wps_device_data *dev, const u8 *str,
320 size_t str_len)
321 {
322 if (str == NULL) {
323 wpa_printf(MSG_DEBUG, "WPS: No Device Name received");
324 return -1;
325 }
326
327 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name", str, str_len);
328
329 os_free(dev->device_name);
330 dev->device_name = os_malloc(str_len + 1);
331 if (dev->device_name == NULL)
332 return -1;
333 os_memcpy(dev->device_name, str, str_len);
334 dev->device_name[str_len] = '\0';
335
336 return 0;
337 }
338
339
wps_process_primary_dev_type(struct wps_device_data * dev,const u8 * dev_type)340 static int wps_process_primary_dev_type(struct wps_device_data *dev,
341 const u8 *dev_type)
342 {
343 #ifndef CONFIG_NO_STDOUT_DEBUG
344 char devtype[WPS_DEV_TYPE_BUFSIZE];
345 #endif /* CONFIG_NO_STDOUT_DEBUG */
346
347 if (dev_type == NULL) {
348 wpa_printf(MSG_DEBUG, "WPS: No Primary Device Type received");
349 return -1;
350 }
351
352 os_memcpy(dev->pri_dev_type, dev_type, WPS_DEV_TYPE_LEN);
353 wpa_printf(MSG_DEBUG, "WPS: Primary Device Type: %s",
354 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
355 sizeof(devtype)));
356
357 return 0;
358 }
359
360
wps_process_device_attrs(struct wps_device_data * dev,struct wps_parse_attr * attr)361 int wps_process_device_attrs(struct wps_device_data *dev,
362 struct wps_parse_attr *attr)
363 {
364 if (wps_process_manufacturer(dev, attr->manufacturer,
365 attr->manufacturer_len) ||
366 wps_process_model_name(dev, attr->model_name,
367 attr->model_name_len) ||
368 wps_process_model_number(dev, attr->model_number,
369 attr->model_number_len) ||
370 wps_process_serial_number(dev, attr->serial_number,
371 attr->serial_number_len) ||
372 wps_process_primary_dev_type(dev, attr->primary_dev_type) ||
373 wps_process_dev_name(dev, attr->dev_name, attr->dev_name_len))
374 return -1;
375 return 0;
376 }
377
378
wps_process_os_version(struct wps_device_data * dev,const u8 * ver)379 int wps_process_os_version(struct wps_device_data *dev, const u8 *ver)
380 {
381 if (ver == NULL) {
382 wpa_printf(MSG_DEBUG, "WPS: No OS Version received");
383 return -1;
384 }
385
386 dev->os_version = WPA_GET_BE32(ver);
387 wpa_printf(MSG_DEBUG, "WPS: OS Version %08x", dev->os_version);
388
389 return 0;
390 }
391
392
wps_process_rf_bands(struct wps_device_data * dev,const u8 * bands)393 int wps_process_rf_bands(struct wps_device_data *dev, const u8 *bands)
394 {
395 if (bands == NULL) {
396 wpa_printf(MSG_DEBUG, "WPS: No RF Bands received");
397 return -1;
398 }
399
400 dev->rf_bands = *bands;
401 wpa_printf(MSG_DEBUG, "WPS: Enrollee RF Bands 0x%x", dev->rf_bands);
402
403 return 0;
404 }
405
406
wps_device_data_dup(struct wps_device_data * dst,const struct wps_device_data * src)407 void wps_device_data_dup(struct wps_device_data *dst,
408 const struct wps_device_data *src)
409 {
410 if (src->device_name)
411 dst->device_name = os_strdup(src->device_name);
412 if (src->manufacturer)
413 dst->manufacturer = os_strdup(src->manufacturer);
414 if (src->model_name)
415 dst->model_name = os_strdup(src->model_name);
416 if (src->model_number)
417 dst->model_number = os_strdup(src->model_number);
418 if (src->serial_number)
419 dst->serial_number = os_strdup(src->serial_number);
420 os_memcpy(dst->pri_dev_type, src->pri_dev_type, WPS_DEV_TYPE_LEN);
421 dst->os_version = src->os_version;
422 dst->rf_bands = src->rf_bands;
423 }
424
425
wps_device_data_free(struct wps_device_data * dev)426 void wps_device_data_free(struct wps_device_data *dev)
427 {
428 os_free(dev->device_name);
429 dev->device_name = NULL;
430 os_free(dev->manufacturer);
431 dev->manufacturer = NULL;
432 os_free(dev->model_name);
433 dev->model_name = NULL;
434 os_free(dev->model_number);
435 dev->model_number = NULL;
436 os_free(dev->serial_number);
437 dev->serial_number = NULL;
438 }
439