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