Coverage Report

Created: 2025-04-24 06:18

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