2 * Wi-Fi Protected Setup - attribute parsing
3 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
13 #include "wps_attr_parse.h"
15 #ifndef CONFIG_WPS_STRICT
16 #define WPS_WORKAROUNDS
17 #endif /* CONFIG_WPS_STRICT */
20 static int wps_set_vendor_ext_wfa_subelem(struct wps_parse_attr *attr,
21 u8 id, u8 len, const u8 *pos)
23 wpa_printf(MSG_EXCESSIVE, "WPS: WFA subelement id=%u len=%u",
26 case WFA_ELEM_VERSION2:
28 wpa_printf(MSG_DEBUG, "WPS: Invalid Version2 length "
34 case WFA_ELEM_AUTHORIZEDMACS:
35 attr->authorized_macs = pos;
36 attr->authorized_macs_len = len;
38 case WFA_ELEM_NETWORK_KEY_SHAREABLE:
40 wpa_printf(MSG_DEBUG, "WPS: Invalid Network Key "
41 "Shareable length %u", len);
44 attr->network_key_shareable = pos;
46 case WFA_ELEM_REQUEST_TO_ENROLL:
48 wpa_printf(MSG_DEBUG, "WPS: Invalid Request to Enroll "
52 attr->request_to_enroll = pos;
54 case WFA_ELEM_SETTINGS_DELAY_TIME:
56 wpa_printf(MSG_DEBUG, "WPS: Invalid Settings Delay "
57 "Time length %u", len);
60 attr->settings_delay_time = pos;
62 case WFA_ELEM_REGISTRAR_CONFIGURATION_METHODS:
64 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Configuration Methods length %u",
68 attr->registrar_configuration_methods = pos;
71 wpa_printf(MSG_MSGDUMP, "WPS: Skipped unknown WFA Vendor "
72 "Extension subelement %u", id);
80 static int wps_parse_vendor_ext_wfa(struct wps_parse_attr *attr, const u8 *pos,
83 const u8 *end = pos + len;
86 while (pos + 2 <= end) {
91 if (wps_set_vendor_ext_wfa_subelem(attr, id, elen, pos) < 0)
100 static int wps_parse_vendor_ext(struct wps_parse_attr *attr, const u8 *pos,
106 wpa_printf(MSG_DEBUG, "WPS: Skip invalid Vendor Extension");
110 vendor_id = WPA_GET_BE24(pos);
112 case WPS_VENDOR_ID_WFA:
113 return wps_parse_vendor_ext_wfa(attr, pos + 3, len - 3);
116 /* Handle unknown vendor extensions */
118 wpa_printf(MSG_MSGDUMP, "WPS: Unknown Vendor Extension (Vendor ID %u)",
121 if (len > WPS_MAX_VENDOR_EXT_LEN) {
122 wpa_printf(MSG_DEBUG, "WPS: Too long Vendor Extension (%u)",
127 if (attr->num_vendor_ext >= MAX_WPS_PARSE_VENDOR_EXT) {
128 wpa_printf(MSG_DEBUG, "WPS: Skipped Vendor Extension "
129 "attribute (max %d vendor extensions)",
130 MAX_WPS_PARSE_VENDOR_EXT);
133 attr->vendor_ext[attr->num_vendor_ext] = pos;
134 attr->vendor_ext_len[attr->num_vendor_ext] = len;
135 attr->num_vendor_ext++;
141 static int wps_set_attr(struct wps_parse_attr *attr, u16 type,
142 const u8 *pos, u16 len)
147 wpa_printf(MSG_DEBUG, "WPS: Invalid Version length %u",
155 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type "
159 attr->msg_type = pos;
161 case ATTR_ENROLLEE_NONCE:
162 if (len != WPS_NONCE_LEN) {
163 wpa_printf(MSG_DEBUG, "WPS: Invalid Enrollee Nonce "
167 attr->enrollee_nonce = pos;
169 case ATTR_REGISTRAR_NONCE:
170 if (len != WPS_NONCE_LEN) {
171 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce "
175 attr->registrar_nonce = pos;
178 if (len != WPS_UUID_LEN) {
179 wpa_printf(MSG_DEBUG, "WPS: Invalid UUID-E length %u",
186 if (len != WPS_UUID_LEN) {
187 wpa_printf(MSG_DEBUG, "WPS: Invalid UUID-R length %u",
193 case ATTR_AUTH_TYPE_FLAGS:
195 wpa_printf(MSG_DEBUG, "WPS: Invalid Authentication "
196 "Type Flags length %u", len);
199 attr->auth_type_flags = pos;
201 case ATTR_ENCR_TYPE_FLAGS:
203 wpa_printf(MSG_DEBUG, "WPS: Invalid Encryption Type "
204 "Flags length %u", len);
207 attr->encr_type_flags = pos;
209 case ATTR_CONN_TYPE_FLAGS:
211 wpa_printf(MSG_DEBUG, "WPS: Invalid Connection Type "
212 "Flags length %u", len);
215 attr->conn_type_flags = pos;
217 case ATTR_CONFIG_METHODS:
219 wpa_printf(MSG_DEBUG, "WPS: Invalid Config Methods "
223 attr->config_methods = pos;
225 case ATTR_SELECTED_REGISTRAR_CONFIG_METHODS:
227 wpa_printf(MSG_DEBUG, "WPS: Invalid Selected "
228 "Registrar Config Methods length %u", len);
231 attr->sel_reg_config_methods = pos;
233 case ATTR_PRIMARY_DEV_TYPE:
234 if (len != WPS_DEV_TYPE_LEN) {
235 wpa_printf(MSG_DEBUG, "WPS: Invalid Primary Device "
236 "Type length %u", len);
239 attr->primary_dev_type = pos;
243 wpa_printf(MSG_DEBUG, "WPS: Invalid RF Bands length "
247 attr->rf_bands = pos;
249 case ATTR_ASSOC_STATE:
251 wpa_printf(MSG_DEBUG, "WPS: Invalid Association State "
255 attr->assoc_state = pos;
257 case ATTR_CONFIG_ERROR:
259 wpa_printf(MSG_DEBUG, "WPS: Invalid Configuration "
260 "Error length %u", len);
263 attr->config_error = pos;
265 case ATTR_DEV_PASSWORD_ID:
267 wpa_printf(MSG_DEBUG, "WPS: Invalid Device Password "
268 "ID length %u", len);
271 attr->dev_password_id = pos;
273 case ATTR_OOB_DEVICE_PASSWORD:
274 if (len < WPS_OOB_PUBKEY_HASH_LEN + 2 ||
275 len > WPS_OOB_PUBKEY_HASH_LEN + 2 +
276 WPS_OOB_DEVICE_PASSWORD_LEN ||
277 (len < WPS_OOB_PUBKEY_HASH_LEN + 2 +
278 WPS_OOB_DEVICE_PASSWORD_MIN_LEN &&
279 WPA_GET_BE16(pos + WPS_OOB_PUBKEY_HASH_LEN) !=
280 DEV_PW_NFC_CONNECTION_HANDOVER)) {
281 wpa_printf(MSG_DEBUG, "WPS: Invalid OOB Device "
282 "Password length %u", len);
285 attr->oob_dev_password = pos;
286 attr->oob_dev_password_len = len;
288 case ATTR_OS_VERSION:
290 wpa_printf(MSG_DEBUG, "WPS: Invalid OS Version length "
294 attr->os_version = pos;
298 wpa_printf(MSG_DEBUG, "WPS: Invalid Wi-Fi Protected "
299 "Setup State length %u", len);
302 attr->wps_state = pos;
304 case ATTR_AUTHENTICATOR:
305 if (len != WPS_AUTHENTICATOR_LEN) {
306 wpa_printf(MSG_DEBUG, "WPS: Invalid Authenticator "
310 attr->authenticator = pos;
313 if (len != WPS_HASH_LEN) {
314 wpa_printf(MSG_DEBUG, "WPS: Invalid R-Hash1 length %u",
321 if (len != WPS_HASH_LEN) {
322 wpa_printf(MSG_DEBUG, "WPS: Invalid R-Hash2 length %u",
329 if (len != WPS_HASH_LEN) {
330 wpa_printf(MSG_DEBUG, "WPS: Invalid E-Hash1 length %u",
337 if (len != WPS_HASH_LEN) {
338 wpa_printf(MSG_DEBUG, "WPS: Invalid E-Hash2 length %u",
345 if (len != WPS_SECRET_NONCE_LEN) {
346 wpa_printf(MSG_DEBUG, "WPS: Invalid R-SNonce1 length "
350 attr->r_snonce1 = pos;
353 if (len != WPS_SECRET_NONCE_LEN) {
354 wpa_printf(MSG_DEBUG, "WPS: Invalid R-SNonce2 length "
358 attr->r_snonce2 = pos;
361 if (len != WPS_SECRET_NONCE_LEN) {
362 wpa_printf(MSG_DEBUG, "WPS: Invalid E-SNonce1 length "
366 attr->e_snonce1 = pos;
369 if (len != WPS_SECRET_NONCE_LEN) {
370 wpa_printf(MSG_DEBUG, "WPS: Invalid E-SNonce2 length "
374 attr->e_snonce2 = pos;
376 case ATTR_KEY_WRAP_AUTH:
377 if (len != WPS_KWA_LEN) {
378 wpa_printf(MSG_DEBUG, "WPS: Invalid Key Wrap "
379 "Authenticator length %u", len);
382 attr->key_wrap_auth = pos;
386 wpa_printf(MSG_DEBUG, "WPS: Invalid Authentication "
387 "Type length %u", len);
390 attr->auth_type = pos;
394 wpa_printf(MSG_DEBUG, "WPS: Invalid Encryption "
395 "Type length %u", len);
398 attr->encr_type = pos;
400 case ATTR_NETWORK_INDEX:
402 wpa_printf(MSG_DEBUG, "WPS: Invalid Network Index "
406 attr->network_idx = pos;
408 case ATTR_NETWORK_KEY_INDEX:
410 wpa_printf(MSG_DEBUG, "WPS: Invalid Network Key Index "
414 attr->network_key_idx = pos;
417 if (len != ETH_ALEN) {
418 wpa_printf(MSG_DEBUG, "WPS: Invalid MAC Address "
422 attr->mac_addr = pos;
424 case ATTR_SELECTED_REGISTRAR:
426 wpa_printf(MSG_DEBUG, "WPS: Invalid Selected Registrar"
430 attr->selected_registrar = pos;
432 case ATTR_REQUEST_TYPE:
434 wpa_printf(MSG_DEBUG, "WPS: Invalid Request Type "
438 attr->request_type = pos;
440 case ATTR_RESPONSE_TYPE:
442 wpa_printf(MSG_DEBUG, "WPS: Invalid Response Type "
446 attr->response_type = pos;
448 case ATTR_MANUFACTURER:
449 attr->manufacturer = pos;
450 attr->manufacturer_len = len;
452 case ATTR_MODEL_NAME:
453 attr->model_name = pos;
454 attr->model_name_len = len;
456 case ATTR_MODEL_NUMBER:
457 attr->model_number = pos;
458 attr->model_number_len = len;
460 case ATTR_SERIAL_NUMBER:
461 attr->serial_number = pos;
462 attr->serial_number_len = len;
465 attr->dev_name = pos;
466 attr->dev_name_len = len;
468 case ATTR_PUBLIC_KEY:
469 attr->public_key = pos;
470 attr->public_key_len = len;
472 case ATTR_ENCR_SETTINGS:
473 attr->encr_settings = pos;
474 attr->encr_settings_len = len;
477 if (attr->num_cred >= MAX_CRED_COUNT) {
478 wpa_printf(MSG_DEBUG, "WPS: Skipped Credential "
479 "attribute (max %d credentials)",
483 attr->cred[attr->num_cred] = pos;
484 attr->cred_len[attr->num_cred] = len;
489 attr->ssid_len = len;
491 case ATTR_NETWORK_KEY:
492 attr->network_key = pos;
493 attr->network_key_len = len;
495 case ATTR_AP_SETUP_LOCKED:
497 wpa_printf(MSG_DEBUG, "WPS: Invalid AP Setup Locked "
501 attr->ap_setup_locked = pos;
503 case ATTR_REQUESTED_DEV_TYPE:
504 if (len != WPS_DEV_TYPE_LEN) {
505 wpa_printf(MSG_DEBUG, "WPS: Invalid Requested Device "
506 "Type length %u", len);
509 if (attr->num_req_dev_type >= MAX_REQ_DEV_TYPE_COUNT) {
510 wpa_printf(MSG_DEBUG, "WPS: Skipped Requested Device "
511 "Type attribute (max %u types)",
512 MAX_REQ_DEV_TYPE_COUNT);
515 attr->req_dev_type[attr->num_req_dev_type] = pos;
516 attr->num_req_dev_type++;
518 case ATTR_SECONDARY_DEV_TYPE_LIST:
519 if (len > WPS_SEC_DEV_TYPE_MAX_LEN ||
520 (len % WPS_DEV_TYPE_LEN) > 0) {
521 wpa_printf(MSG_DEBUG, "WPS: Invalid Secondary Device "
522 "Type length %u", len);
525 attr->sec_dev_type_list = pos;
526 attr->sec_dev_type_list_len = len;
528 case ATTR_VENDOR_EXT:
529 if (wps_parse_vendor_ext(attr, pos, len) < 0)
532 case ATTR_AP_CHANNEL:
534 wpa_printf(MSG_DEBUG, "WPS: Invalid AP Channel "
538 attr->ap_channel = pos;
541 wpa_printf(MSG_DEBUG, "WPS: Unsupported attribute type 0x%x "
542 "len=%u", type, len);
550 int wps_parse_msg(const struct wpabuf *msg, struct wps_parse_attr *attr)
554 #ifdef WPS_WORKAROUNDS
556 #endif /* WPS_WORKAROUNDS */
558 os_memset(attr, 0, sizeof(*attr));
559 pos = wpabuf_head(msg);
560 end = pos + wpabuf_len(msg);
564 wpa_printf(MSG_DEBUG, "WPS: Invalid message - "
565 "%lu bytes remaining",
566 (unsigned long) (end - pos));
570 type = WPA_GET_BE16(pos);
572 len = WPA_GET_BE16(pos);
574 wpa_printf(MSG_EXCESSIVE, "WPS: attr type=0x%x len=%u",
576 if (len > end - pos) {
577 wpa_printf(MSG_DEBUG, "WPS: Attribute overflow");
578 wpa_hexdump_buf(MSG_MSGDUMP, "WPS: Message data", msg);
579 #ifdef WPS_WORKAROUNDS
581 * Some deployed APs seem to have a bug in encoding of
582 * Network Key attribute in the Credential attribute
583 * where they add an extra octet after the Network Key
584 * attribute at least when open network is being
587 if ((type & 0xff00) != 0x1000 &&
588 prev_type == ATTR_NETWORK_KEY) {
589 wpa_printf(MSG_DEBUG, "WPS: Workaround - try "
590 "to skip unexpected octet after "
595 #endif /* WPS_WORKAROUNDS */
599 #ifdef WPS_WORKAROUNDS
600 if (type == 0 && len == 0) {
602 * Mac OS X 10.6 seems to be adding 0x00 padding to the
603 * end of M1. Skip those to avoid interop issues.
606 for (i = 0; i < end - pos; i++) {
610 if (i == end - pos) {
611 wpa_printf(MSG_DEBUG, "WPS: Workaround - skip "
612 "unexpected message padding");
616 #endif /* WPS_WORKAROUNDS */
618 if (wps_set_attr(attr, type, pos, len) < 0)
621 #ifdef WPS_WORKAROUNDS
623 #endif /* WPS_WORKAROUNDS */