]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/wps/wps.c
WPS NFC: Validate peer public key hash on Enrollee
[thirdparty/hostap.git] / src / wps / wps.c
1 /*
2 * Wi-Fi Protected Setup
3 * Copyright (c) 2007-2009, 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 "crypto/dh_group5.h"
13 #include "common/ieee802_11_defs.h"
14 #include "wps_i.h"
15 #include "wps_dev_attr.h"
16
17
18 #ifdef CONFIG_WPS_TESTING
19 int wps_version_number = 0x20;
20 int wps_testing_dummy_cred = 0;
21 #endif /* CONFIG_WPS_TESTING */
22
23
24 /**
25 * wps_init - Initialize WPS Registration protocol data
26 * @cfg: WPS configuration
27 * Returns: Pointer to allocated data or %NULL on failure
28 *
29 * This function is used to initialize WPS data for a registration protocol
30 * instance (i.e., each run of registration protocol as a Registrar of
31 * Enrollee. The caller is responsible for freeing this data after the
32 * registration run has been completed by calling wps_deinit().
33 */
34 struct wps_data * wps_init(const struct wps_config *cfg)
35 {
36 struct wps_data *data = os_zalloc(sizeof(*data));
37 if (data == NULL)
38 return NULL;
39 data->wps = cfg->wps;
40 data->registrar = cfg->registrar;
41 if (cfg->registrar) {
42 os_memcpy(data->uuid_r, cfg->wps->uuid, WPS_UUID_LEN);
43 } else {
44 os_memcpy(data->mac_addr_e, cfg->wps->dev.mac_addr, ETH_ALEN);
45 os_memcpy(data->uuid_e, cfg->wps->uuid, WPS_UUID_LEN);
46 }
47 if (cfg->pin) {
48 data->dev_pw_id = cfg->dev_pw_id;
49 data->dev_password = os_malloc(cfg->pin_len);
50 if (data->dev_password == NULL) {
51 os_free(data);
52 return NULL;
53 }
54 os_memcpy(data->dev_password, cfg->pin, cfg->pin_len);
55 data->dev_password_len = cfg->pin_len;
56 wpa_hexdump_key(MSG_DEBUG, "WPS: AP PIN dev_password",
57 data->dev_password, data->dev_password_len);
58 }
59
60 #ifdef CONFIG_WPS_NFC
61 if (cfg->wps->ap && !cfg->registrar && cfg->wps->ap_nfc_dev_pw_id) {
62 /* Keep AP PIN as alternative Device Password */
63 data->alt_dev_pw_id = data->dev_pw_id;
64 data->alt_dev_password = data->dev_password;
65 data->alt_dev_password_len = data->dev_password_len;
66
67 data->dev_pw_id = cfg->wps->ap_nfc_dev_pw_id;
68 data->dev_password =
69 os_malloc(wpabuf_len(cfg->wps->ap_nfc_dev_pw));
70 if (data->dev_password == NULL) {
71 os_free(data);
72 return NULL;
73 }
74 os_memcpy(data->dev_password,
75 wpabuf_head(cfg->wps->ap_nfc_dev_pw),
76 wpabuf_len(cfg->wps->ap_nfc_dev_pw));
77 data->dev_password_len = wpabuf_len(cfg->wps->ap_nfc_dev_pw);
78 wpa_hexdump_key(MSG_DEBUG, "WPS: NFC dev_password",
79 data->dev_password, data->dev_password_len);
80 }
81 #endif /* CONFIG_WPS_NFC */
82
83 data->pbc = cfg->pbc;
84 if (cfg->pbc) {
85 /* Use special PIN '00000000' for PBC */
86 data->dev_pw_id = DEV_PW_PUSHBUTTON;
87 os_free(data->dev_password);
88 data->dev_password = (u8 *) os_strdup("00000000");
89 if (data->dev_password == NULL) {
90 os_free(data);
91 return NULL;
92 }
93 data->dev_password_len = 8;
94 }
95
96 data->state = data->registrar ? RECV_M1 : SEND_M1;
97
98 if (cfg->assoc_wps_ie) {
99 struct wps_parse_attr attr;
100 wpa_hexdump_buf(MSG_DEBUG, "WPS: WPS IE from (Re)AssocReq",
101 cfg->assoc_wps_ie);
102 if (wps_parse_msg(cfg->assoc_wps_ie, &attr) < 0) {
103 wpa_printf(MSG_DEBUG, "WPS: Failed to parse WPS IE "
104 "from (Re)AssocReq");
105 } else if (attr.request_type == NULL) {
106 wpa_printf(MSG_DEBUG, "WPS: No Request Type attribute "
107 "in (Re)AssocReq WPS IE");
108 } else {
109 wpa_printf(MSG_DEBUG, "WPS: Request Type (from WPS IE "
110 "in (Re)AssocReq WPS IE): %d",
111 *attr.request_type);
112 data->request_type = *attr.request_type;
113 }
114 }
115
116 if (cfg->new_ap_settings) {
117 data->new_ap_settings =
118 os_malloc(sizeof(*data->new_ap_settings));
119 if (data->new_ap_settings == NULL) {
120 os_free(data->dev_password);
121 os_free(data);
122 return NULL;
123 }
124 os_memcpy(data->new_ap_settings, cfg->new_ap_settings,
125 sizeof(*data->new_ap_settings));
126 }
127
128 if (cfg->peer_addr)
129 os_memcpy(data->peer_dev.mac_addr, cfg->peer_addr, ETH_ALEN);
130 if (cfg->p2p_dev_addr)
131 os_memcpy(data->p2p_dev_addr, cfg->p2p_dev_addr, ETH_ALEN);
132
133 data->use_psk_key = cfg->use_psk_key;
134 data->pbc_in_m1 = cfg->pbc_in_m1;
135
136 if (cfg->peer_pubkey_hash) {
137 os_memcpy(data->peer_pubkey_hash, cfg->peer_pubkey_hash,
138 WPS_OOB_PUBKEY_HASH_LEN);
139 data->peer_pubkey_hash_set = 1;
140 }
141
142 return data;
143 }
144
145
146 /**
147 * wps_deinit - Deinitialize WPS Registration protocol data
148 * @data: WPS Registration protocol data from wps_init()
149 */
150 void wps_deinit(struct wps_data *data)
151 {
152 #ifdef CONFIG_WPS_NFC
153 if (data->registrar && data->nfc_pw_token)
154 wps_registrar_remove_nfc_pw_token(data->wps->registrar,
155 data->nfc_pw_token);
156 #endif /* CONFIG_WPS_NFC */
157
158 if (data->wps_pin_revealed) {
159 wpa_printf(MSG_DEBUG, "WPS: Full PIN information revealed and "
160 "negotiation failed");
161 if (data->registrar)
162 wps_registrar_invalidate_pin(data->wps->registrar,
163 data->uuid_e);
164 } else if (data->registrar)
165 wps_registrar_unlock_pin(data->wps->registrar, data->uuid_e);
166
167 wpabuf_free(data->dh_privkey);
168 wpabuf_free(data->dh_pubkey_e);
169 wpabuf_free(data->dh_pubkey_r);
170 wpabuf_free(data->last_msg);
171 os_free(data->dev_password);
172 os_free(data->alt_dev_password);
173 os_free(data->new_psk);
174 wps_device_data_free(&data->peer_dev);
175 os_free(data->new_ap_settings);
176 dh5_free(data->dh_ctx);
177 os_free(data->nfc_pw_token);
178 os_free(data);
179 }
180
181
182 /**
183 * wps_process_msg - Process a WPS message
184 * @wps: WPS Registration protocol data from wps_init()
185 * @op_code: Message OP Code
186 * @msg: Message data
187 * Returns: Processing result
188 *
189 * This function is used to process WPS messages with OP Codes WSC_ACK,
190 * WSC_NACK, WSC_MSG, and WSC_Done. The caller (e.g., EAP server/peer) is
191 * responsible for reassembling the messages before calling this function.
192 * Response to this message is built by calling wps_get_msg().
193 */
194 enum wps_process_res wps_process_msg(struct wps_data *wps,
195 enum wsc_op_code op_code,
196 const struct wpabuf *msg)
197 {
198 if (wps->registrar)
199 return wps_registrar_process_msg(wps, op_code, msg);
200 else
201 return wps_enrollee_process_msg(wps, op_code, msg);
202 }
203
204
205 /**
206 * wps_get_msg - Build a WPS message
207 * @wps: WPS Registration protocol data from wps_init()
208 * @op_code: Buffer for returning message OP Code
209 * Returns: The generated WPS message or %NULL on failure
210 *
211 * This function is used to build a response to a message processed by calling
212 * wps_process_msg(). The caller is responsible for freeing the buffer.
213 */
214 struct wpabuf * wps_get_msg(struct wps_data *wps, enum wsc_op_code *op_code)
215 {
216 if (wps->registrar)
217 return wps_registrar_get_msg(wps, op_code);
218 else
219 return wps_enrollee_get_msg(wps, op_code);
220 }
221
222
223 /**
224 * wps_is_selected_pbc_registrar - Check whether WPS IE indicates active PBC
225 * @msg: WPS IE contents from Beacon or Probe Response frame
226 * Returns: 1 if PBC Registrar is active, 0 if not
227 */
228 int wps_is_selected_pbc_registrar(const struct wpabuf *msg)
229 {
230 struct wps_parse_attr attr;
231
232 /*
233 * In theory, this could also verify that attr.sel_reg_config_methods
234 * includes WPS_CONFIG_PUSHBUTTON, but some deployed AP implementations
235 * do not set Selected Registrar Config Methods attribute properly, so
236 * it is safer to just use Device Password ID here.
237 */
238
239 if (wps_parse_msg(msg, &attr) < 0 ||
240 !attr.selected_registrar || *attr.selected_registrar == 0 ||
241 !attr.dev_password_id ||
242 WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
243 return 0;
244
245 #ifdef CONFIG_WPS_STRICT
246 if (!attr.sel_reg_config_methods ||
247 !(WPA_GET_BE16(attr.sel_reg_config_methods) &
248 WPS_CONFIG_PUSHBUTTON))
249 return 0;
250 #endif /* CONFIG_WPS_STRICT */
251
252 return 1;
253 }
254
255
256 static int is_selected_pin_registrar(struct wps_parse_attr *attr)
257 {
258 /*
259 * In theory, this could also verify that attr.sel_reg_config_methods
260 * includes WPS_CONFIG_LABEL, WPS_CONFIG_DISPLAY, or WPS_CONFIG_KEYPAD,
261 * but some deployed AP implementations do not set Selected Registrar
262 * Config Methods attribute properly, so it is safer to just use
263 * Device Password ID here.
264 */
265
266 if (!attr->selected_registrar || *attr->selected_registrar == 0)
267 return 0;
268
269 if (attr->dev_password_id != NULL &&
270 WPA_GET_BE16(attr->dev_password_id) == DEV_PW_PUSHBUTTON)
271 return 0;
272
273 #ifdef CONFIG_WPS_STRICT
274 if (!attr->sel_reg_config_methods ||
275 !(WPA_GET_BE16(attr->sel_reg_config_methods) &
276 (WPS_CONFIG_LABEL | WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD)))
277 return 0;
278 #endif /* CONFIG_WPS_STRICT */
279
280 return 1;
281 }
282
283
284 /**
285 * wps_is_selected_pin_registrar - Check whether WPS IE indicates active PIN
286 * @msg: WPS IE contents from Beacon or Probe Response frame
287 * Returns: 1 if PIN Registrar is active, 0 if not
288 */
289 int wps_is_selected_pin_registrar(const struct wpabuf *msg)
290 {
291 struct wps_parse_attr attr;
292
293 if (wps_parse_msg(msg, &attr) < 0)
294 return 0;
295
296 return is_selected_pin_registrar(&attr);
297 }
298
299
300 /**
301 * wps_is_addr_authorized - Check whether WPS IE authorizes MAC address
302 * @msg: WPS IE contents from Beacon or Probe Response frame
303 * @addr: MAC address to search for
304 * @ver1_compat: Whether to use version 1 compatibility mode
305 * Returns: 2 if the specified address is explicit authorized, 1 if address is
306 * authorized (broadcast), 0 if not
307 */
308 int wps_is_addr_authorized(const struct wpabuf *msg, const u8 *addr,
309 int ver1_compat)
310 {
311 struct wps_parse_attr attr;
312 unsigned int i;
313 const u8 *pos;
314 const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
315
316 if (wps_parse_msg(msg, &attr) < 0)
317 return 0;
318
319 if (!attr.version2 && ver1_compat) {
320 /*
321 * Version 1.0 AP - AuthorizedMACs not used, so revert back to
322 * old mechanism of using SelectedRegistrar.
323 */
324 return is_selected_pin_registrar(&attr);
325 }
326
327 if (!attr.authorized_macs)
328 return 0;
329
330 pos = attr.authorized_macs;
331 for (i = 0; i < attr.authorized_macs_len / ETH_ALEN; i++) {
332 if (os_memcmp(pos, addr, ETH_ALEN) == 0)
333 return 2;
334 if (os_memcmp(pos, bcast, ETH_ALEN) == 0)
335 return 1;
336 pos += ETH_ALEN;
337 }
338
339 return 0;
340 }
341
342
343 /**
344 * wps_ap_priority_compar - Prioritize WPS IE from two APs
345 * @wps_a: WPS IE contents from Beacon or Probe Response frame
346 * @wps_b: WPS IE contents from Beacon or Probe Response frame
347 * Returns: 1 if wps_b is considered more likely selection for WPS
348 * provisioning, -1 if wps_a is considered more like, or 0 if no preference
349 */
350 int wps_ap_priority_compar(const struct wpabuf *wps_a,
351 const struct wpabuf *wps_b)
352 {
353 struct wps_parse_attr attr_a, attr_b;
354 int sel_a, sel_b;
355
356 if (wps_a == NULL || wps_parse_msg(wps_a, &attr_a) < 0)
357 return 1;
358 if (wps_b == NULL || wps_parse_msg(wps_b, &attr_b) < 0)
359 return -1;
360
361 sel_a = attr_a.selected_registrar && *attr_a.selected_registrar != 0;
362 sel_b = attr_b.selected_registrar && *attr_b.selected_registrar != 0;
363
364 if (sel_a && !sel_b)
365 return -1;
366 if (!sel_a && sel_b)
367 return 1;
368
369 return 0;
370 }
371
372
373 /**
374 * wps_get_uuid_e - Get UUID-E from WPS IE
375 * @msg: WPS IE contents from Beacon or Probe Response frame
376 * Returns: Pointer to UUID-E or %NULL if not included
377 *
378 * The returned pointer is to the msg contents and it remains valid only as
379 * long as the msg buffer is valid.
380 */
381 const u8 * wps_get_uuid_e(const struct wpabuf *msg)
382 {
383 struct wps_parse_attr attr;
384
385 if (wps_parse_msg(msg, &attr) < 0)
386 return NULL;
387 return attr.uuid_e;
388 }
389
390
391 /**
392 * wps_is_20 - Check whether WPS attributes claim support for WPS 2.0
393 */
394 int wps_is_20(const struct wpabuf *msg)
395 {
396 struct wps_parse_attr attr;
397
398 if (msg == NULL || wps_parse_msg(msg, &attr) < 0)
399 return 0;
400 return attr.version2 != NULL;
401 }
402
403
404 /**
405 * wps_build_assoc_req_ie - Build WPS IE for (Re)Association Request
406 * @req_type: Value for Request Type attribute
407 * Returns: WPS IE or %NULL on failure
408 *
409 * The caller is responsible for freeing the buffer.
410 */
411 struct wpabuf * wps_build_assoc_req_ie(enum wps_request_type req_type)
412 {
413 struct wpabuf *ie;
414 u8 *len;
415
416 wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for (Re)Association "
417 "Request");
418 ie = wpabuf_alloc(100);
419 if (ie == NULL)
420 return NULL;
421
422 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
423 len = wpabuf_put(ie, 1);
424 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
425
426 if (wps_build_version(ie) ||
427 wps_build_req_type(ie, req_type) ||
428 wps_build_wfa_ext(ie, 0, NULL, 0)) {
429 wpabuf_free(ie);
430 return NULL;
431 }
432
433 *len = wpabuf_len(ie) - 2;
434
435 return ie;
436 }
437
438
439 /**
440 * wps_build_assoc_resp_ie - Build WPS IE for (Re)Association Response
441 * Returns: WPS IE or %NULL on failure
442 *
443 * The caller is responsible for freeing the buffer.
444 */
445 struct wpabuf * wps_build_assoc_resp_ie(void)
446 {
447 struct wpabuf *ie;
448 u8 *len;
449
450 wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for (Re)Association "
451 "Response");
452 ie = wpabuf_alloc(100);
453 if (ie == NULL)
454 return NULL;
455
456 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
457 len = wpabuf_put(ie, 1);
458 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
459
460 if (wps_build_version(ie) ||
461 wps_build_resp_type(ie, WPS_RESP_AP) ||
462 wps_build_wfa_ext(ie, 0, NULL, 0)) {
463 wpabuf_free(ie);
464 return NULL;
465 }
466
467 *len = wpabuf_len(ie) - 2;
468
469 return ie;
470 }
471
472
473 /**
474 * wps_build_probe_req_ie - Build WPS IE for Probe Request
475 * @pw_id: Password ID (DEV_PW_PUSHBUTTON for active PBC and DEV_PW_DEFAULT for
476 * most other use cases)
477 * @dev: Device attributes
478 * @uuid: Own UUID
479 * @req_type: Value for Request Type attribute
480 * @num_req_dev_types: Number of requested device types
481 * @req_dev_types: Requested device types (8 * num_req_dev_types octets) or
482 * %NULL if none
483 * Returns: WPS IE or %NULL on failure
484 *
485 * The caller is responsible for freeing the buffer.
486 */
487 struct wpabuf * wps_build_probe_req_ie(u16 pw_id, struct wps_device_data *dev,
488 const u8 *uuid,
489 enum wps_request_type req_type,
490 unsigned int num_req_dev_types,
491 const u8 *req_dev_types)
492 {
493 struct wpabuf *ie;
494
495 wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for Probe Request");
496
497 ie = wpabuf_alloc(500);
498 if (ie == NULL)
499 return NULL;
500
501 if (wps_build_version(ie) ||
502 wps_build_req_type(ie, req_type) ||
503 wps_build_config_methods(ie, dev->config_methods) ||
504 wps_build_uuid_e(ie, uuid) ||
505 wps_build_primary_dev_type(dev, ie) ||
506 wps_build_rf_bands(dev, ie, 0) ||
507 wps_build_assoc_state(NULL, ie) ||
508 wps_build_config_error(ie, WPS_CFG_NO_ERROR) ||
509 wps_build_dev_password_id(ie, pw_id) ||
510 #ifdef CONFIG_WPS2
511 wps_build_manufacturer(dev, ie) ||
512 wps_build_model_name(dev, ie) ||
513 wps_build_model_number(dev, ie) ||
514 wps_build_dev_name(dev, ie) ||
515 wps_build_wfa_ext(ie, req_type == WPS_REQ_ENROLLEE, NULL, 0) ||
516 #endif /* CONFIG_WPS2 */
517 wps_build_req_dev_type(dev, ie, num_req_dev_types, req_dev_types)
518 ||
519 wps_build_secondary_dev_type(dev, ie)
520 ) {
521 wpabuf_free(ie);
522 return NULL;
523 }
524
525 #ifndef CONFIG_WPS2
526 if (dev->p2p && wps_build_dev_name(dev, ie)) {
527 wpabuf_free(ie);
528 return NULL;
529 }
530 #endif /* CONFIG_WPS2 */
531
532 return wps_ie_encapsulate(ie);
533 }
534
535
536 void wps_free_pending_msgs(struct upnp_pending_message *msgs)
537 {
538 struct upnp_pending_message *p, *prev;
539 p = msgs;
540 while (p) {
541 prev = p;
542 p = p->next;
543 wpabuf_free(prev->msg);
544 os_free(prev);
545 }
546 }
547
548
549 int wps_attr_text(struct wpabuf *data, char *buf, char *end)
550 {
551 struct wps_parse_attr attr;
552 char *pos = buf;
553 int ret;
554
555 if (wps_parse_msg(data, &attr) < 0)
556 return -1;
557
558 if (attr.wps_state) {
559 if (*attr.wps_state == WPS_STATE_NOT_CONFIGURED)
560 ret = os_snprintf(pos, end - pos,
561 "wps_state=unconfigured\n");
562 else if (*attr.wps_state == WPS_STATE_CONFIGURED)
563 ret = os_snprintf(pos, end - pos,
564 "wps_state=configured\n");
565 else
566 ret = 0;
567 if (ret < 0 || ret >= end - pos)
568 return pos - buf;
569 pos += ret;
570 }
571
572 if (attr.ap_setup_locked && *attr.ap_setup_locked) {
573 ret = os_snprintf(pos, end - pos,
574 "wps_ap_setup_locked=1\n");
575 if (ret < 0 || ret >= end - pos)
576 return pos - buf;
577 pos += ret;
578 }
579
580 if (attr.selected_registrar && *attr.selected_registrar) {
581 ret = os_snprintf(pos, end - pos,
582 "wps_selected_registrar=1\n");
583 if (ret < 0 || ret >= end - pos)
584 return pos - buf;
585 pos += ret;
586 }
587
588 if (attr.dev_password_id) {
589 ret = os_snprintf(pos, end - pos,
590 "wps_device_password_id=%u\n",
591 WPA_GET_BE16(attr.dev_password_id));
592 if (ret < 0 || ret >= end - pos)
593 return pos - buf;
594 pos += ret;
595 }
596
597 if (attr.sel_reg_config_methods) {
598 ret = os_snprintf(pos, end - pos,
599 "wps_selected_registrar_config_methods="
600 "0x%04x\n",
601 WPA_GET_BE16(attr.sel_reg_config_methods));
602 if (ret < 0 || ret >= end - pos)
603 return pos - buf;
604 pos += ret;
605 }
606
607 if (attr.primary_dev_type) {
608 char devtype[WPS_DEV_TYPE_BUFSIZE];
609 ret = os_snprintf(pos, end - pos,
610 "wps_primary_device_type=%s\n",
611 wps_dev_type_bin2str(attr.primary_dev_type,
612 devtype,
613 sizeof(devtype)));
614 if (ret < 0 || ret >= end - pos)
615 return pos - buf;
616 pos += ret;
617 }
618
619 if (attr.dev_name) {
620 char *str = os_malloc(attr.dev_name_len + 1);
621 size_t i;
622 if (str == NULL)
623 return pos - buf;
624 for (i = 0; i < attr.dev_name_len; i++) {
625 if (attr.dev_name[i] < 32)
626 str[i] = '_';
627 else
628 str[i] = attr.dev_name[i];
629 }
630 str[i] = '\0';
631 ret = os_snprintf(pos, end - pos, "wps_device_name=%s\n", str);
632 os_free(str);
633 if (ret < 0 || ret >= end - pos)
634 return pos - buf;
635 pos += ret;
636 }
637
638 if (attr.config_methods) {
639 ret = os_snprintf(pos, end - pos,
640 "wps_config_methods=0x%04x\n",
641 WPA_GET_BE16(attr.config_methods));
642 if (ret < 0 || ret >= end - pos)
643 return pos - buf;
644 pos += ret;
645 }
646
647 return pos - buf;
648 }
649
650
651 const char * wps_ei_str(enum wps_error_indication ei)
652 {
653 switch (ei) {
654 case WPS_EI_NO_ERROR:
655 return "No Error";
656 case WPS_EI_SECURITY_TKIP_ONLY_PROHIBITED:
657 return "TKIP Only Prohibited";
658 case WPS_EI_SECURITY_WEP_PROHIBITED:
659 return "WEP Prohibited";
660 case WPS_EI_AUTH_FAILURE:
661 return "Authentication Failure";
662 default:
663 return "Unknown";
664 }
665 }