wps_dev_attr.c revision 4b06059785b935dd1f4f09314e4e12c417d2c6a4
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 = dup_binstr(str, str_len); 261 if (dev->manufacturer == NULL) 262 return -1; 263 264 return 0; 265} 266 267 268static int wps_process_model_name(struct wps_device_data *dev, const u8 *str, 269 size_t str_len) 270{ 271 if (str == NULL) { 272 wpa_printf(MSG_DEBUG, "WPS: No Model Name received"); 273 return -1; 274 } 275 276 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name", str, str_len); 277 278 os_free(dev->model_name); 279 dev->model_name = dup_binstr(str, str_len); 280 if (dev->model_name == NULL) 281 return -1; 282 283 return 0; 284} 285 286 287static int wps_process_model_number(struct wps_device_data *dev, const u8 *str, 288 size_t str_len) 289{ 290 if (str == NULL) { 291 wpa_printf(MSG_DEBUG, "WPS: No Model Number received"); 292 return -1; 293 } 294 295 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number", str, str_len); 296 297 os_free(dev->model_number); 298 dev->model_number = dup_binstr(str, str_len); 299 if (dev->model_number == NULL) 300 return -1; 301 302 return 0; 303} 304 305 306static int wps_process_serial_number(struct wps_device_data *dev, 307 const u8 *str, size_t str_len) 308{ 309 if (str == NULL) { 310 wpa_printf(MSG_DEBUG, "WPS: No Serial Number received"); 311 return -1; 312 } 313 314 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number", str, str_len); 315 316 os_free(dev->serial_number); 317 dev->serial_number = dup_binstr(str, str_len); 318 if (dev->serial_number == NULL) 319 return -1; 320 321 return 0; 322} 323 324 325static int wps_process_dev_name(struct wps_device_data *dev, const u8 *str, 326 size_t str_len) 327{ 328 if (str == NULL) { 329 wpa_printf(MSG_DEBUG, "WPS: No Device Name received"); 330 return -1; 331 } 332 333 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name", str, str_len); 334 335 os_free(dev->device_name); 336 dev->device_name = dup_binstr(str, str_len); 337 if (dev->device_name == NULL) 338 return -1; 339 340 return 0; 341} 342 343 344static int wps_process_primary_dev_type(struct wps_device_data *dev, 345 const u8 *dev_type) 346{ 347#ifndef CONFIG_NO_STDOUT_DEBUG 348 char devtype[WPS_DEV_TYPE_BUFSIZE]; 349#endif /* CONFIG_NO_STDOUT_DEBUG */ 350 351 if (dev_type == NULL) { 352 wpa_printf(MSG_DEBUG, "WPS: No Primary Device Type received"); 353 return -1; 354 } 355 356 os_memcpy(dev->pri_dev_type, dev_type, WPS_DEV_TYPE_LEN); 357 wpa_printf(MSG_DEBUG, "WPS: Primary Device Type: %s", 358 wps_dev_type_bin2str(dev->pri_dev_type, devtype, 359 sizeof(devtype))); 360 361 return 0; 362} 363 364 365int wps_process_device_attrs(struct wps_device_data *dev, 366 struct wps_parse_attr *attr) 367{ 368 if (wps_process_manufacturer(dev, attr->manufacturer, 369 attr->manufacturer_len) || 370 wps_process_model_name(dev, attr->model_name, 371 attr->model_name_len) || 372 wps_process_model_number(dev, attr->model_number, 373 attr->model_number_len) || 374 wps_process_serial_number(dev, attr->serial_number, 375 attr->serial_number_len) || 376 wps_process_primary_dev_type(dev, attr->primary_dev_type) || 377 wps_process_dev_name(dev, attr->dev_name, attr->dev_name_len)) 378 return -1; 379 return 0; 380} 381 382 383int wps_process_os_version(struct wps_device_data *dev, const u8 *ver) 384{ 385 if (ver == NULL) { 386 wpa_printf(MSG_DEBUG, "WPS: No OS Version received"); 387 return -1; 388 } 389 390 dev->os_version = WPA_GET_BE32(ver); 391 wpa_printf(MSG_DEBUG, "WPS: OS Version %08x", dev->os_version); 392 393 return 0; 394} 395 396 397int wps_process_rf_bands(struct wps_device_data *dev, const u8 *bands) 398{ 399 if (bands == NULL) { 400 wpa_printf(MSG_DEBUG, "WPS: No RF Bands received"); 401 return -1; 402 } 403 404 dev->rf_bands = *bands; 405 wpa_printf(MSG_DEBUG, "WPS: Enrollee RF Bands 0x%x", dev->rf_bands); 406 407 return 0; 408} 409 410 411void wps_device_data_dup(struct wps_device_data *dst, 412 const struct wps_device_data *src) 413{ 414 if (src->device_name) 415 dst->device_name = os_strdup(src->device_name); 416 if (src->manufacturer) 417 dst->manufacturer = os_strdup(src->manufacturer); 418 if (src->model_name) 419 dst->model_name = os_strdup(src->model_name); 420 if (src->model_number) 421 dst->model_number = os_strdup(src->model_number); 422 if (src->serial_number) 423 dst->serial_number = os_strdup(src->serial_number); 424 os_memcpy(dst->pri_dev_type, src->pri_dev_type, WPS_DEV_TYPE_LEN); 425 dst->os_version = src->os_version; 426 dst->rf_bands = src->rf_bands; 427} 428 429 430void wps_device_data_free(struct wps_device_data *dev) 431{ 432 os_free(dev->device_name); 433 dev->device_name = NULL; 434 os_free(dev->manufacturer); 435 dev->manufacturer = NULL; 436 os_free(dev->model_name); 437 dev->model_name = NULL; 438 os_free(dev->model_number); 439 dev->model_number = NULL; 440 os_free(dev->serial_number); 441 dev->serial_number = NULL; 442} 443