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