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 16int 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 40int 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 64int 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 88static 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 113int 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 123int 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 139int 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 159int 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 183int 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 196int 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 206int wps_build_vendor_ext_m1(struct wps_device_data *dev, struct wpabuf *msg) 207{ 208 if (dev->vendor_ext_m1 != NULL) { 209 wpa_hexdump(MSG_DEBUG, "WPS: * Vendor Extension M1", 210 wpabuf_head_u8(dev->vendor_ext_m1), 211 wpabuf_len(dev->vendor_ext_m1)); 212 wpabuf_put_be16(msg, ATTR_VENDOR_EXT); 213 wpabuf_put_be16(msg, wpabuf_len(dev->vendor_ext_m1)); 214 wpabuf_put_buf(msg, dev->vendor_ext_m1); 215 } 216 return 0; 217} 218 219 220int wps_build_rf_bands(struct wps_device_data *dev, struct wpabuf *msg) 221{ 222 wpa_printf(MSG_DEBUG, "WPS: * RF Bands (%x)", dev->rf_bands); 223 wpabuf_put_be16(msg, ATTR_RF_BANDS); 224 wpabuf_put_be16(msg, 1); 225 wpabuf_put_u8(msg, dev->rf_bands); 226 return 0; 227} 228 229 230int wps_build_vendor_ext(struct wps_device_data *dev, struct wpabuf *msg) 231{ 232 int i; 233 234 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) { 235 if (dev->vendor_ext[i] == NULL) 236 continue; 237 wpa_hexdump(MSG_DEBUG, "WPS: * Vendor Extension", 238 wpabuf_head_u8(dev->vendor_ext[i]), 239 wpabuf_len(dev->vendor_ext[i])); 240 wpabuf_put_be16(msg, ATTR_VENDOR_EXT); 241 wpabuf_put_be16(msg, wpabuf_len(dev->vendor_ext[i])); 242 wpabuf_put_buf(msg, dev->vendor_ext[i]); 243 } 244 245 return 0; 246} 247 248 249static int wps_process_manufacturer(struct wps_device_data *dev, const u8 *str, 250 size_t str_len) 251{ 252 if (str == NULL) { 253 wpa_printf(MSG_DEBUG, "WPS: No Manufacturer received"); 254 return -1; 255 } 256 257 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer", str, str_len); 258 259 os_free(dev->manufacturer); 260 dev->manufacturer = os_malloc(str_len + 1); 261 if (dev->manufacturer == NULL) 262 return -1; 263 os_memcpy(dev->manufacturer, str, str_len); 264 dev->manufacturer[str_len] = '\0'; 265 266 return 0; 267} 268 269 270static int wps_process_model_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 Model Name received"); 275 return -1; 276 } 277 278 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name", str, str_len); 279 280 os_free(dev->model_name); 281 dev->model_name = os_malloc(str_len + 1); 282 if (dev->model_name == NULL) 283 return -1; 284 os_memcpy(dev->model_name, str, str_len); 285 dev->model_name[str_len] = '\0'; 286 287 return 0; 288} 289 290 291static int wps_process_model_number(struct wps_device_data *dev, const u8 *str, 292 size_t str_len) 293{ 294 if (str == NULL) { 295 wpa_printf(MSG_DEBUG, "WPS: No Model Number received"); 296 return -1; 297 } 298 299 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number", str, str_len); 300 301 os_free(dev->model_number); 302 dev->model_number = os_malloc(str_len + 1); 303 if (dev->model_number == NULL) 304 return -1; 305 os_memcpy(dev->model_number, str, str_len); 306 dev->model_number[str_len] = '\0'; 307 308 return 0; 309} 310 311 312static int wps_process_serial_number(struct wps_device_data *dev, 313 const u8 *str, size_t str_len) 314{ 315 if (str == NULL) { 316 wpa_printf(MSG_DEBUG, "WPS: No Serial Number received"); 317 return -1; 318 } 319 320 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number", str, str_len); 321 322 os_free(dev->serial_number); 323 dev->serial_number = os_malloc(str_len + 1); 324 if (dev->serial_number == NULL) 325 return -1; 326 os_memcpy(dev->serial_number, str, str_len); 327 dev->serial_number[str_len] = '\0'; 328 329 return 0; 330} 331 332 333static int wps_process_dev_name(struct wps_device_data *dev, const u8 *str, 334 size_t str_len) 335{ 336 if (str == NULL) { 337 wpa_printf(MSG_DEBUG, "WPS: No Device Name received"); 338 return -1; 339 } 340 341 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name", str, str_len); 342 343 os_free(dev->device_name); 344 dev->device_name = os_malloc(str_len + 1); 345 if (dev->device_name == NULL) 346 return -1; 347 os_memcpy(dev->device_name, str, str_len); 348 dev->device_name[str_len] = '\0'; 349 350 return 0; 351} 352 353 354static int wps_process_primary_dev_type(struct wps_device_data *dev, 355 const u8 *dev_type) 356{ 357#ifndef CONFIG_NO_STDOUT_DEBUG 358 char devtype[WPS_DEV_TYPE_BUFSIZE]; 359#endif /* CONFIG_NO_STDOUT_DEBUG */ 360 361 if (dev_type == NULL) { 362 wpa_printf(MSG_DEBUG, "WPS: No Primary Device Type received"); 363 return -1; 364 } 365 366 os_memcpy(dev->pri_dev_type, dev_type, WPS_DEV_TYPE_LEN); 367 wpa_printf(MSG_DEBUG, "WPS: Primary Device Type: %s", 368 wps_dev_type_bin2str(dev->pri_dev_type, devtype, 369 sizeof(devtype))); 370 371 return 0; 372} 373 374 375int wps_process_device_attrs(struct wps_device_data *dev, 376 struct wps_parse_attr *attr) 377{ 378 if (wps_process_manufacturer(dev, attr->manufacturer, 379 attr->manufacturer_len) || 380 wps_process_model_name(dev, attr->model_name, 381 attr->model_name_len) || 382 wps_process_model_number(dev, attr->model_number, 383 attr->model_number_len) || 384 wps_process_serial_number(dev, attr->serial_number, 385 attr->serial_number_len) || 386 wps_process_primary_dev_type(dev, attr->primary_dev_type) || 387 wps_process_dev_name(dev, attr->dev_name, attr->dev_name_len)) 388 return -1; 389 return 0; 390} 391 392 393int wps_process_os_version(struct wps_device_data *dev, const u8 *ver) 394{ 395 if (ver == NULL) { 396 wpa_printf(MSG_DEBUG, "WPS: No OS Version received"); 397 return -1; 398 } 399 400 dev->os_version = WPA_GET_BE32(ver); 401 wpa_printf(MSG_DEBUG, "WPS: OS Version %08x", dev->os_version); 402 403 return 0; 404} 405 406 407int wps_process_rf_bands(struct wps_device_data *dev, const u8 *bands) 408{ 409 if (bands == NULL) { 410 wpa_printf(MSG_DEBUG, "WPS: No RF Bands received"); 411 return -1; 412 } 413 414 dev->rf_bands = *bands; 415 wpa_printf(MSG_DEBUG, "WPS: Enrollee RF Bands 0x%x", dev->rf_bands); 416 417 return 0; 418} 419 420 421void wps_device_data_dup(struct wps_device_data *dst, 422 const struct wps_device_data *src) 423{ 424 if (src->device_name) 425 dst->device_name = os_strdup(src->device_name); 426 if (src->manufacturer) 427 dst->manufacturer = os_strdup(src->manufacturer); 428 if (src->model_name) 429 dst->model_name = os_strdup(src->model_name); 430 if (src->model_number) 431 dst->model_number = os_strdup(src->model_number); 432 if (src->serial_number) 433 dst->serial_number = os_strdup(src->serial_number); 434 os_memcpy(dst->pri_dev_type, src->pri_dev_type, WPS_DEV_TYPE_LEN); 435 dst->os_version = src->os_version; 436 dst->rf_bands = src->rf_bands; 437} 438 439 440void wps_device_data_free(struct wps_device_data *dev) 441{ 442 os_free(dev->device_name); 443 dev->device_name = NULL; 444 os_free(dev->manufacturer); 445 dev->manufacturer = NULL; 446 os_free(dev->model_name); 447 dev->model_name = NULL; 448 os_free(dev->model_number); 449 dev->model_number = NULL; 450 os_free(dev->serial_number); 451 dev->serial_number = NULL; 452} 453