WPS: Add UFD support (USBA out-of-band mechanism)
[wpasupplicant] / src / wps / wps_registrar.c
1 /*
2  * Wi-Fi Protected Setup - Registrar
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 "sha256.h"
19 #include "base64.h"
20 #include "ieee802_11_defs.h"
21 #include "eloop.h"
22 #include "wps_i.h"
23 #include "wps_dev_attr.h"
24 #include "wps_upnp.h"
25 #include "crypto.h"
26
27
28 struct wps_uuid_pin {
29         struct wps_uuid_pin *next;
30         u8 uuid[WPS_UUID_LEN];
31         int wildcard_uuid;
32         u8 *pin;
33         size_t pin_len;
34         int locked;
35 };
36
37
38 static void wps_free_pin(struct wps_uuid_pin *pin)
39 {
40         os_free(pin->pin);
41         os_free(pin);
42 }
43
44
45 static void wps_free_pins(struct wps_uuid_pin *pins)
46 {
47         struct wps_uuid_pin *pin, *prev;
48
49         pin = pins;
50         while (pin) {
51                 prev = pin;
52                 pin = pin->next;
53                 wps_free_pin(prev);
54         }
55 }
56
57
58 struct wps_pbc_session {
59         struct wps_pbc_session *next;
60         u8 addr[ETH_ALEN];
61         u8 uuid_e[WPS_UUID_LEN];
62         struct os_time timestamp;
63 };
64
65
66 static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
67 {
68         struct wps_pbc_session *prev;
69
70         while (pbc) {
71                 prev = pbc;
72                 pbc = pbc->next;
73                 os_free(prev);
74         }
75 }
76
77
78 struct wps_registrar {
79         struct wps_context *wps;
80
81         int pbc;
82         int selected_registrar;
83
84         int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
85                           size_t psk_len);
86         int (*set_ie_cb)(void *ctx, const u8 *beacon_ie, size_t beacon_ie_len,
87                          const u8 *probe_resp_ie, size_t probe_resp_ie_len);
88         void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
89                               const struct wps_device_data *dev);
90         void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
91                                const u8 *uuid_e);
92         void *cb_ctx;
93
94         struct wps_uuid_pin *pins;
95         struct wps_pbc_session *pbc_sessions;
96
97         int skip_cred_build;
98         struct wpabuf *extra_cred;
99         int disable_auto_conf;
100         int sel_reg_dev_password_id_override;
101         int sel_reg_config_methods_override;
102 };
103
104
105 static int wps_set_ie(struct wps_registrar *reg);
106 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
107 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
108                                                void *timeout_ctx);
109
110
111 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
112                                           const u8 *addr, const u8 *uuid_e)
113 {
114         struct wps_pbc_session *pbc, *prev = NULL;
115         struct os_time now;
116
117         os_get_time(&now);
118
119         pbc = reg->pbc_sessions;
120         while (pbc) {
121                 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
122                     os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
123                         if (prev)
124                                 prev->next = pbc->next;
125                         else
126                                 reg->pbc_sessions = pbc->next;
127                         break;
128                 }
129                 prev = pbc;
130                 pbc = pbc->next;
131         }
132
133         if (!pbc) {
134                 pbc = os_zalloc(sizeof(*pbc));
135                 if (pbc == NULL)
136                         return;
137                 os_memcpy(pbc->addr, addr, ETH_ALEN);
138                 if (uuid_e)
139                         os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
140         }
141
142         pbc->next = reg->pbc_sessions;
143         reg->pbc_sessions = pbc;
144         pbc->timestamp = now;
145
146         /* remove entries that have timed out */
147         prev = pbc;
148         pbc = pbc->next;
149
150         while (pbc) {
151                 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
152                         prev->next = NULL;
153                         wps_free_pbc_sessions(pbc);
154                         break;
155                 }
156                 prev = pbc;
157                 pbc = pbc->next;
158         }
159 }
160
161
162 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
163                                              const u8 *addr, const u8 *uuid_e)
164 {
165         struct wps_pbc_session *pbc, *prev = NULL;
166
167         pbc = reg->pbc_sessions;
168         while (pbc) {
169                 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
170                     os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
171                         if (prev)
172                                 prev->next = pbc->next;
173                         else
174                                 reg->pbc_sessions = pbc->next;
175                         os_free(pbc);
176                         break;
177                 }
178                 prev = pbc;
179                 pbc = pbc->next;
180         }
181 }
182
183
184 static int wps_registrar_pbc_overlap(struct wps_registrar *reg,
185                                      const u8 *addr, const u8 *uuid_e)
186 {
187         int count = 0;
188         struct wps_pbc_session *pbc;
189         struct os_time now;
190
191         os_get_time(&now);
192
193         for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
194                 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME)
195                         break;
196                 if (addr == NULL || os_memcmp(addr, pbc->addr, ETH_ALEN) ||
197                     uuid_e == NULL ||
198                     os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN))
199                         count++;
200         }
201
202         if (addr || uuid_e)
203                 count++;
204
205         return count > 1 ? 1 : 0;
206 }
207
208
209 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
210 {
211         wpa_printf(MSG_DEBUG, "WPS:  * Wi-Fi Protected Setup State (%d)",
212                    wps->wps_state);
213         wpabuf_put_be16(msg, ATTR_WPS_STATE);
214         wpabuf_put_be16(msg, 1);
215         wpabuf_put_u8(msg, wps->wps_state);
216         return 0;
217 }
218
219
220 #ifdef CONFIG_WPS_UPNP
221 static void wps_registrar_free_pending_m2(struct wps_context *wps)
222 {
223         struct upnp_pending_message *p, *p2, *prev = NULL;
224         p = wps->upnp_msgs;
225         while (p) {
226                 if (p->type == WPS_M2 || p->type == WPS_M2D) {
227                         if (prev == NULL)
228                                 wps->upnp_msgs = p->next;
229                         else
230                                 prev->next = p->next;
231                         wpa_printf(MSG_DEBUG, "WPS UPnP: Drop pending M2/M2D");
232                         p2 = p;
233                         p = p->next;
234                         wpabuf_free(p2->msg);
235                         os_free(p2);
236                         continue;
237                 }
238                 prev = p;
239                 p = p->next;
240         }
241 }
242 #endif /* CONFIG_WPS_UPNP */
243
244
245 static int wps_build_ap_setup_locked(struct wps_context *wps,
246                                      struct wpabuf *msg)
247 {
248         if (wps->ap_setup_locked) {
249                 wpa_printf(MSG_DEBUG, "WPS:  * AP Setup Locked");
250                 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
251                 wpabuf_put_be16(msg, 1);
252                 wpabuf_put_u8(msg, 1);
253         }
254         return 0;
255 }
256
257
258 static int wps_build_selected_registrar(struct wps_registrar *reg,
259                                         struct wpabuf *msg)
260 {
261         if (!reg->selected_registrar)
262                 return 0;
263         wpa_printf(MSG_DEBUG, "WPS:  * Selected Registrar");
264         wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
265         wpabuf_put_be16(msg, 1);
266         wpabuf_put_u8(msg, 1);
267         return 0;
268 }
269
270
271 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
272                                              struct wpabuf *msg)
273 {
274         u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
275         if (!reg->selected_registrar)
276                 return 0;
277         if (reg->sel_reg_dev_password_id_override >= 0)
278                 id = reg->sel_reg_dev_password_id_override;
279         wpa_printf(MSG_DEBUG, "WPS:  * Device Password ID (%d)", id);
280         wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
281         wpabuf_put_be16(msg, 2);
282         wpabuf_put_be16(msg, id);
283         return 0;
284 }
285
286
287 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
288                                             struct wpabuf *msg)
289 {
290         u16 methods;
291         if (!reg->selected_registrar)
292                 return 0;
293         methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
294         if (reg->pbc)
295                 methods |= WPS_CONFIG_PUSHBUTTON;
296         if (reg->sel_reg_config_methods_override >= 0)
297                 methods = reg->sel_reg_config_methods_override;
298         wpa_printf(MSG_DEBUG, "WPS:  * Selected Registrar Config Methods (%x)",
299                    methods);
300         wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
301         wpabuf_put_be16(msg, 2);
302         wpabuf_put_be16(msg, methods);
303         return 0;
304 }
305
306
307 static int wps_build_probe_config_methods(struct wps_registrar *reg,
308                                           struct wpabuf *msg)
309 {
310         u16 methods;
311         methods = 0;
312         wpa_printf(MSG_DEBUG, "WPS:  * Config Methods (%x)", methods);
313         wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
314         wpabuf_put_be16(msg, 2);
315         wpabuf_put_be16(msg, methods);
316         return 0;
317 }
318
319
320 static int wps_build_config_methods_r(struct wps_registrar *reg,
321                                       struct wpabuf *msg)
322 {
323         u16 methods;
324         methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
325         if (reg->pbc)
326                 methods |= WPS_CONFIG_PUSHBUTTON;
327         return wps_build_config_methods(msg, methods);
328 }
329
330
331 static int wps_build_resp_type(struct wps_registrar *reg, struct wpabuf *msg)
332 {
333         u8 resp = reg->wps->ap ? WPS_RESP_AP : WPS_RESP_REGISTRAR;
334         wpa_printf(MSG_DEBUG, "WPS:  * Response Type (%d)", resp);
335         wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
336         wpabuf_put_be16(msg, 1);
337         wpabuf_put_u8(msg, resp);
338         return 0;
339 }
340
341
342 /**
343  * wps_registrar_init - Initialize WPS Registrar data
344  * @wps: Pointer to longterm WPS context
345  * @cfg: Registrar configuration
346  * Returns: Pointer to allocated Registrar data or %NULL on failure
347  *
348  * This function is used to initialize WPS Registrar functionality. It can be
349  * used for a single Registrar run (e.g., when run in a supplicant) or multiple
350  * runs (e.g., when run as an internal Registrar in an AP). Caller is
351  * responsible for freeing the returned data with wps_registrar_deinit() when
352  * Registrar functionality is not needed anymore.
353  */
354 struct wps_registrar *
355 wps_registrar_init(struct wps_context *wps,
356                    const struct wps_registrar_config *cfg)
357 {
358         struct wps_registrar *reg = os_zalloc(sizeof(*reg));
359         if (reg == NULL)
360                 return NULL;
361
362         reg->wps = wps;
363         reg->new_psk_cb = cfg->new_psk_cb;
364         reg->set_ie_cb = cfg->set_ie_cb;
365         reg->pin_needed_cb = cfg->pin_needed_cb;
366         reg->reg_success_cb = cfg->reg_success_cb;
367         reg->cb_ctx = cfg->cb_ctx;
368         reg->skip_cred_build = cfg->skip_cred_build;
369         if (cfg->extra_cred) {
370                 reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
371                                                     cfg->extra_cred_len);
372                 if (reg->extra_cred == NULL) {
373                         os_free(reg);
374                         return NULL;
375                 }
376         }
377         reg->disable_auto_conf = cfg->disable_auto_conf;
378         reg->sel_reg_dev_password_id_override = -1;
379         reg->sel_reg_config_methods_override = -1;
380
381         if (wps_set_ie(reg)) {
382                 wps_registrar_deinit(reg);
383                 return NULL;
384         }
385
386         return reg;
387 }
388
389
390 /**
391  * wps_registrar_deinit - Deinitialize WPS Registrar data
392  * @reg: Registrar data from wps_registrar_init()
393  */
394 void wps_registrar_deinit(struct wps_registrar *reg)
395 {
396         if (reg == NULL)
397                 return;
398         eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
399         eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
400         wps_free_pins(reg->pins);
401         wps_free_pbc_sessions(reg->pbc_sessions);
402         wpabuf_free(reg->extra_cred);
403         os_free(reg);
404 }
405
406
407 /**
408  * wps_registrar_add_pin - Configure a new PIN for Registrar
409  * @reg: Registrar data from wps_registrar_init()
410  * @uuid: UUID-E or %NULL for wildcard (any UUID)
411  * @pin: PIN (Device Password)
412  * @pin_len: Length of pin in octets
413  * Returns: 0 on success, -1 on failure
414  */
415 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
416                           const u8 *pin, size_t pin_len)
417 {
418         struct wps_uuid_pin *p;
419
420         p = os_zalloc(sizeof(*p));
421         if (p == NULL)
422                 return -1;
423         if (uuid == NULL)
424                 p->wildcard_uuid = 1;
425         else
426                 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
427         p->pin = os_malloc(pin_len);
428         if (p->pin == NULL) {
429                 os_free(p);
430                 return -1;
431         }
432         os_memcpy(p->pin, pin, pin_len);
433         p->pin_len = pin_len;
434
435         p->next = reg->pins;
436         reg->pins = p;
437
438         wpa_printf(MSG_DEBUG, "WPS: A new PIN configured");
439         wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
440         wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
441         reg->selected_registrar = 1;
442         reg->pbc = 0;
443         wps_set_ie(reg);
444
445         return 0;
446 }
447
448
449 /**
450  * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
451  * @reg: Registrar data from wps_registrar_init()
452  * @uuid: UUID-E
453  * Returns: 0 on success, -1 on failure (e.g., PIN not found)
454  */
455 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
456 {
457         struct wps_uuid_pin *pin, *prev;
458
459         prev = NULL;
460         pin = reg->pins;
461         while (pin) {
462                 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
463                         if (prev == NULL)
464                                 reg->pins = pin->next;
465                         else
466                                 prev->next = pin->next;
467                         wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
468                                     pin->uuid, WPS_UUID_LEN);
469                         wps_free_pin(pin);
470                         return 0;
471                 }
472                 prev = pin;
473                 pin = pin->next;
474         }
475
476         return -1;
477 }
478
479
480 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
481                                         const u8 *uuid, size_t *pin_len)
482 {
483         struct wps_uuid_pin *pin;
484
485         pin = reg->pins;
486         while (pin) {
487                 if (!pin->wildcard_uuid &&
488                     os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0)
489                         break;
490                 pin = pin->next;
491         }
492
493         if (!pin) {
494                 /* Check for wildcard UUIDs since none of the UUID-specific
495                  * PINs matched */
496                 pin = reg->pins;
497                 while (pin) {
498                         if (pin->wildcard_uuid == 1) {
499                                 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
500                                            "PIN. Assigned it for this UUID-E");
501                                 pin->wildcard_uuid = 2;
502                                 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
503                                 break;
504                         }
505                         pin = pin->next;
506                 }
507         }
508
509         if (!pin)
510                 return NULL;
511
512         /*
513          * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
514          * that could otherwise avoid PIN invalidations.
515          */
516         if (pin->locked) {
517                 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
518                            "allow concurrent re-use");
519                 return NULL;
520         }
521         *pin_len = pin->pin_len;
522         pin->locked = 1;
523         return pin->pin;
524 }
525
526
527 /**
528  * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
529  * @reg: Registrar data from wps_registrar_init()
530  * @uuid: UUID-E
531  * Returns: 0 on success, -1 on failure
532  *
533  * PINs are locked to enforce only one concurrent use. This function unlocks a
534  * PIN to allow it to be used again. If the specified PIN was configured using
535  * a wildcard UUID, it will be removed instead of allowing multiple uses.
536  */
537 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
538 {
539         struct wps_uuid_pin *pin;
540
541         pin = reg->pins;
542         while (pin) {
543                 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
544                         if (pin->wildcard_uuid == 2) {
545                                 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
546                                            "wildcard PIN");
547                                 return wps_registrar_invalidate_pin(reg, uuid);
548                         }
549                         pin->locked = 0;
550                         return 0;
551                 }
552                 pin = pin->next;
553         }
554
555         return -1;
556 }
557
558
559 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
560 {
561         reg->selected_registrar = 0;
562         reg->pbc = 0;
563         wps_set_ie(reg);
564 }
565
566
567 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
568 {
569         struct wps_registrar *reg = eloop_ctx;
570
571         wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
572         wps_registrar_stop_pbc(reg);
573 }
574
575
576 /**
577  * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
578  * @reg: Registrar data from wps_registrar_init()
579  * Returns: 0 on success, -1 on failure
580  *
581  * This function is called on an AP when a push button is pushed to activate
582  * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
583  * or when a PBC registration is completed.
584  */
585 int wps_registrar_button_pushed(struct wps_registrar *reg)
586 {
587         if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
588                 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
589                            "mode");
590                 return -1;
591         }
592         wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
593         reg->selected_registrar = 1;
594         reg->pbc = 1;
595         wps_set_ie(reg);
596
597         eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
598         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
599                                reg, NULL);
600         return 0;
601 }
602
603
604 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
605 {
606         wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
607         eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
608         wps_registrar_stop_pbc(reg);
609 }
610
611
612 /**
613  * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
614  * @reg: Registrar data from wps_registrar_init()
615  * @addr: MAC address of the Probe Request sender
616  * @wps_data: WPS IE contents
617  *
618  * This function is called on an AP when a Probe Request with WPS IE is
619  * received. This is used to track PBC mode use and to detect possible overlap
620  * situation with other WPS APs.
621  */
622 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
623                                 const struct wpabuf *wps_data)
624 {
625         struct wps_parse_attr attr;
626         u16 methods;
627
628         wpa_hexdump_buf(MSG_MSGDUMP,
629                         "WPS: Probe Request with WPS data received",
630                         wps_data);
631
632         if (wps_parse_msg(wps_data, &attr) < 0)
633                 return;
634         if (!wps_version_supported(attr.version)) {
635                 wpa_printf(MSG_DEBUG, "WPS: Unsupported ProbeReq WPS IE "
636                            "version 0x%x", attr.version ? *attr.version : 0);
637                 return;
638         }
639
640         if (attr.config_methods == NULL) {
641                 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
642                            "Probe Request");
643                 return;
644         }
645
646         methods = WPA_GET_BE16(attr.config_methods);
647         if (!(methods & WPS_CONFIG_PUSHBUTTON))
648                 return; /* Not PBC */
649
650         wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
651                    MACSTR, MAC2STR(addr));
652
653         wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
654 }
655
656
657 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
658                           const u8 *psk, size_t psk_len)
659 {
660         if (reg->new_psk_cb == NULL)
661                 return 0;
662
663         return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
664 }
665
666
667 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
668                               const struct wps_device_data *dev)
669 {
670         if (reg->pin_needed_cb == NULL)
671                 return;
672
673         reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
674 }
675
676
677 static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
678                                const u8 *uuid_e)
679 {
680         if (reg->reg_success_cb == NULL)
681                 return;
682
683         reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e);
684 }
685
686
687 static int wps_cb_set_ie(struct wps_registrar *reg,
688                          const struct wpabuf *beacon_ie,
689                          const struct wpabuf *probe_resp_ie)
690 {
691         if (reg->set_ie_cb == NULL)
692                 return 0;
693
694         return reg->set_ie_cb(reg->cb_ctx, wpabuf_head(beacon_ie),
695                               wpabuf_len(beacon_ie),
696                               wpabuf_head(probe_resp_ie),
697                               wpabuf_len(probe_resp_ie));
698 }
699
700
701 /* Encapsulate WPS IE data with one (or more, if needed) IE headers */
702 static struct wpabuf * wps_ie_encapsulate(struct wpabuf *data)
703 {
704         struct wpabuf *ie;
705         const u8 *pos, *end;
706
707         ie = wpabuf_alloc(wpabuf_len(data) + 100);
708         if (ie == NULL) {
709                 wpabuf_free(data);
710                 return NULL;
711         }
712
713         pos = wpabuf_head(data);
714         end = pos + wpabuf_len(data);
715
716         while (end > pos) {
717                 size_t frag_len = end - pos;
718                 if (frag_len > 251)
719                         frag_len = 251;
720                 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
721                 wpabuf_put_u8(ie, 4 + frag_len);
722                 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
723                 wpabuf_put_data(ie, pos, frag_len);
724                 pos += frag_len;
725         }
726
727         wpabuf_free(data);
728
729         return ie;
730 }
731
732
733 static int wps_set_ie(struct wps_registrar *reg)
734 {
735         struct wpabuf *beacon;
736         struct wpabuf *probe;
737         int ret;
738
739         wpa_printf(MSG_DEBUG, "WPS: Build Beacon and Probe Response IEs");
740
741         beacon = wpabuf_alloc(300);
742         if (beacon == NULL)
743                 return -1;
744         probe = wpabuf_alloc(400);
745         if (probe == NULL) {
746                 wpabuf_free(beacon);
747                 return -1;
748         }
749
750         if (wps_build_version(beacon) ||
751             wps_build_wps_state(reg->wps, beacon) ||
752             wps_build_ap_setup_locked(reg->wps, beacon) ||
753             wps_build_selected_registrar(reg, beacon) ||
754             wps_build_sel_reg_dev_password_id(reg, beacon) ||
755             wps_build_sel_reg_config_methods(reg, beacon) ||
756             wps_build_version(probe) ||
757             wps_build_wps_state(reg->wps, probe) ||
758             wps_build_ap_setup_locked(reg->wps, probe) ||
759             wps_build_selected_registrar(reg, probe) ||
760             wps_build_sel_reg_dev_password_id(reg, probe) ||
761             wps_build_sel_reg_config_methods(reg, probe) ||
762             wps_build_resp_type(reg, probe) ||
763             wps_build_uuid_e(probe, reg->wps->uuid) ||
764             wps_build_device_attrs(&reg->wps->dev, probe) ||
765             wps_build_probe_config_methods(reg, probe) ||
766             wps_build_rf_bands(&reg->wps->dev, probe)) {
767                 wpabuf_free(beacon);
768                 wpabuf_free(probe);
769                 return -1;
770         }
771
772         beacon = wps_ie_encapsulate(beacon);
773         probe = wps_ie_encapsulate(probe);
774
775         if (!beacon || !probe) {
776                 wpabuf_free(beacon);
777                 wpabuf_free(probe);
778                 return -1;
779         }
780
781         ret = wps_cb_set_ie(reg, beacon, probe);
782         wpabuf_free(beacon);
783         wpabuf_free(probe);
784
785         return ret;
786 }
787
788
789 static int wps_get_dev_password(struct wps_data *wps)
790 {
791         const u8 *pin;
792         size_t pin_len = 0;
793
794         os_free(wps->dev_password);
795         wps->dev_password = NULL;
796
797         if (wps->pbc) {
798                 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
799                 pin = (const u8 *) "00000000";
800                 pin_len = 8;
801         } else {
802                 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
803                                             &pin_len);
804         }
805         if (pin == NULL) {
806                 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
807                            "the Enrollee");
808                 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
809                                   &wps->peer_dev);
810                 return -1;
811         }
812
813         wps->dev_password = os_malloc(pin_len);
814         if (wps->dev_password == NULL)
815                 return -1;
816         os_memcpy(wps->dev_password, pin, pin_len);
817         wps->dev_password_len = pin_len;
818
819         return 0;
820 }
821
822
823 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
824 {
825         wpa_printf(MSG_DEBUG, "WPS:  * UUID-R");
826         wpabuf_put_be16(msg, ATTR_UUID_R);
827         wpabuf_put_be16(msg, WPS_UUID_LEN);
828         wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
829         return 0;
830 }
831
832
833 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
834 {
835         u8 *hash;
836         const u8 *addr[4];
837         size_t len[4];
838
839         if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
840                 return -1;
841         wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
842         wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
843                     wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
844
845         if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
846                 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
847                            "R-Hash derivation");
848                 return -1;
849         }
850
851         wpa_printf(MSG_DEBUG, "WPS:  * R-Hash1");
852         wpabuf_put_be16(msg, ATTR_R_HASH1);
853         wpabuf_put_be16(msg, SHA256_MAC_LEN);
854         hash = wpabuf_put(msg, SHA256_MAC_LEN);
855         /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
856         addr[0] = wps->snonce;
857         len[0] = WPS_SECRET_NONCE_LEN;
858         addr[1] = wps->psk1;
859         len[1] = WPS_PSK_LEN;
860         addr[2] = wpabuf_head(wps->dh_pubkey_e);
861         len[2] = wpabuf_len(wps->dh_pubkey_e);
862         addr[3] = wpabuf_head(wps->dh_pubkey_r);
863         len[3] = wpabuf_len(wps->dh_pubkey_r);
864         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
865         wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
866
867         wpa_printf(MSG_DEBUG, "WPS:  * R-Hash2");
868         wpabuf_put_be16(msg, ATTR_R_HASH2);
869         wpabuf_put_be16(msg, SHA256_MAC_LEN);
870         hash = wpabuf_put(msg, SHA256_MAC_LEN);
871         /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
872         addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
873         addr[1] = wps->psk2;
874         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
875         wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
876
877         return 0;
878 }
879
880
881 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
882 {
883         wpa_printf(MSG_DEBUG, "WPS:  * R-SNonce1");
884         wpabuf_put_be16(msg, ATTR_R_SNONCE1);
885         wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
886         wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
887         return 0;
888 }
889
890
891 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
892 {
893         wpa_printf(MSG_DEBUG, "WPS:  * R-SNonce2");
894         wpabuf_put_be16(msg, ATTR_R_SNONCE2);
895         wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
896         wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
897                         WPS_SECRET_NONCE_LEN);
898         return 0;
899 }
900
901
902 static int wps_build_cred_network_idx(struct wpabuf *msg,
903                                       struct wps_credential *cred)
904 {
905         wpa_printf(MSG_DEBUG, "WPS:  * Network Index");
906         wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
907         wpabuf_put_be16(msg, 1);
908         wpabuf_put_u8(msg, 1);
909         return 0;
910 }
911
912
913 static int wps_build_cred_ssid(struct wpabuf *msg,
914                                struct wps_credential *cred)
915 {
916         wpa_printf(MSG_DEBUG, "WPS:  * SSID");
917         wpabuf_put_be16(msg, ATTR_SSID);
918         wpabuf_put_be16(msg, cred->ssid_len);
919         wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
920         return 0;
921 }
922
923
924 static int wps_build_cred_auth_type(struct wpabuf *msg,
925                                     struct wps_credential *cred)
926 {
927         wpa_printf(MSG_DEBUG, "WPS:  * Authentication Type (0x%x)",
928                    cred->auth_type);
929         wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
930         wpabuf_put_be16(msg, 2);
931         wpabuf_put_be16(msg, cred->auth_type);
932         return 0;
933 }
934
935
936 static int wps_build_cred_encr_type(struct wpabuf *msg,
937                                     struct wps_credential *cred)
938 {
939         wpa_printf(MSG_DEBUG, "WPS:  * Encryption Type (0x%x)",
940                    cred->encr_type);
941         wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
942         wpabuf_put_be16(msg, 2);
943         wpabuf_put_be16(msg, cred->encr_type);
944         return 0;
945 }
946
947
948 static int wps_build_cred_network_key(struct wpabuf *msg,
949                                       struct wps_credential *cred)
950 {
951         wpa_printf(MSG_DEBUG, "WPS:  * Network Key");
952         wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
953         wpabuf_put_be16(msg, cred->key_len);
954         wpabuf_put_data(msg, cred->key, cred->key_len);
955         return 0;
956 }
957
958
959 static int wps_build_cred_mac_addr(struct wpabuf *msg,
960                                    struct wps_credential *cred)
961 {
962         wpa_printf(MSG_DEBUG, "WPS:  * MAC Address (" MACSTR ")",
963                    MAC2STR(cred->mac_addr));
964         wpabuf_put_be16(msg, ATTR_MAC_ADDR);
965         wpabuf_put_be16(msg, ETH_ALEN);
966         wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
967         return 0;
968 }
969
970
971 static int wps_build_credential(struct wpabuf *msg,
972                                 struct wps_credential *cred)
973 {
974         if (wps_build_cred_network_idx(msg, cred) ||
975             wps_build_cred_ssid(msg, cred) ||
976             wps_build_cred_auth_type(msg, cred) ||
977             wps_build_cred_encr_type(msg, cred) ||
978             wps_build_cred_network_key(msg, cred) ||
979             wps_build_cred_mac_addr(msg, cred))
980                 return -1;
981         return 0;
982 }
983
984
985 int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
986 {
987         struct wpabuf *cred;
988
989         if (wps->wps->registrar->skip_cred_build)
990                 goto skip_cred_build;
991
992         wpa_printf(MSG_DEBUG, "WPS:  * Credential");
993         os_memset(&wps->cred, 0, sizeof(wps->cred));
994
995         os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
996         wps->cred.ssid_len = wps->wps->ssid_len;
997
998         /* Select the best authentication and encryption type */
999         if (wps->auth_type & WPS_AUTH_WPA2PSK)
1000                 wps->auth_type = WPS_AUTH_WPA2PSK;
1001         else if (wps->auth_type & WPS_AUTH_WPAPSK)
1002                 wps->auth_type = WPS_AUTH_WPAPSK;
1003         else if (wps->auth_type & WPS_AUTH_OPEN)
1004                 wps->auth_type = WPS_AUTH_OPEN;
1005         else if (wps->auth_type & WPS_AUTH_SHARED)
1006                 wps->auth_type = WPS_AUTH_SHARED;
1007         else {
1008                 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
1009                            wps->auth_type);
1010                 return -1;
1011         }
1012         wps->cred.auth_type = wps->auth_type;
1013
1014         if (wps->auth_type == WPS_AUTH_WPA2PSK ||
1015             wps->auth_type == WPS_AUTH_WPAPSK) {
1016                 if (wps->encr_type & WPS_ENCR_AES)
1017                         wps->encr_type = WPS_ENCR_AES;
1018                 else if (wps->encr_type & WPS_ENCR_TKIP)
1019                         wps->encr_type = WPS_ENCR_TKIP;
1020                 else {
1021                         wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1022                                    "type for WPA/WPA2");
1023                         return -1;
1024                 }
1025         } else {
1026                 if (wps->encr_type & WPS_ENCR_WEP)
1027                         wps->encr_type = WPS_ENCR_WEP;
1028                 else if (wps->encr_type & WPS_ENCR_NONE)
1029                         wps->encr_type = WPS_ENCR_NONE;
1030                 else {
1031                         wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1032                                    "type for non-WPA/WPA2 mode");
1033                         return -1;
1034                 }
1035         }
1036         wps->cred.encr_type = wps->encr_type;
1037         /* Set MAC address in the Credential to be the AP's address (BSSID) */
1038         os_memcpy(wps->cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN);
1039
1040         if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
1041             !wps->wps->registrar->disable_auto_conf) {
1042                 u8 r[16];
1043                 /* Generate a random passphrase */
1044                 if (os_get_random(r, sizeof(r)) < 0)
1045                         return -1;
1046                 os_free(wps->new_psk);
1047                 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
1048                 if (wps->new_psk == NULL)
1049                         return -1;
1050                 wps->new_psk_len--; /* remove newline */
1051                 while (wps->new_psk_len &&
1052                        wps->new_psk[wps->new_psk_len - 1] == '=')
1053                         wps->new_psk_len--;
1054                 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
1055                                       wps->new_psk, wps->new_psk_len);
1056                 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
1057                 wps->cred.key_len = wps->new_psk_len;
1058         } else if (wps->wps->network_key) {
1059                 os_memcpy(wps->cred.key, wps->wps->network_key,
1060                           wps->wps->network_key_len);
1061                 wps->cred.key_len = wps->wps->network_key_len;
1062         } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
1063                 char hex[65];
1064                 /* Generate a random per-device PSK */
1065                 os_free(wps->new_psk);
1066                 wps->new_psk_len = 32;
1067                 wps->new_psk = os_malloc(wps->new_psk_len);
1068                 if (wps->new_psk == NULL)
1069                         return -1;
1070                 if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
1071                         os_free(wps->new_psk);
1072                         wps->new_psk = NULL;
1073                         return -1;
1074                 }
1075                 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1076                                 wps->new_psk, wps->new_psk_len);
1077                 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1078                                  wps->new_psk_len);
1079                 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1080                 wps->cred.key_len = wps->new_psk_len * 2;
1081         }
1082
1083         cred = wpabuf_alloc(200);
1084         if (cred == NULL)
1085                 return -1;
1086
1087         if (wps_build_credential(cred, &wps->cred)) {
1088                 wpabuf_free(cred);
1089                 return -1;
1090         }
1091
1092         wpabuf_put_be16(msg, ATTR_CRED);
1093         wpabuf_put_be16(msg, wpabuf_len(cred));
1094         wpabuf_put_buf(msg, cred);
1095         wpabuf_free(cred);
1096
1097 skip_cred_build:
1098         if (wps->wps->registrar->extra_cred) {
1099                 wpa_printf(MSG_DEBUG, "WPS:  * Credential (pre-configured)");
1100                 wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
1101         }
1102
1103         return 0;
1104 }
1105
1106
1107 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1108 {
1109         wpa_printf(MSG_DEBUG, "WPS:  * AP Settings");
1110
1111         if (wps_build_credential(msg, &wps->cred))
1112                 return -1;
1113
1114         return 0;
1115 }
1116
1117
1118 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1119 {
1120         struct wpabuf *msg;
1121
1122         if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
1123                 return NULL;
1124         wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1125                     wps->nonce_r, WPS_NONCE_LEN);
1126         wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1127
1128         wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1129         msg = wpabuf_alloc(1000);
1130         if (msg == NULL)
1131                 return NULL;
1132
1133         if (wps_build_version(msg) ||
1134             wps_build_msg_type(msg, WPS_M2) ||
1135             wps_build_enrollee_nonce(wps, msg) ||
1136             wps_build_registrar_nonce(wps, msg) ||
1137             wps_build_uuid_r(wps, msg) ||
1138             wps_build_public_key(wps, msg) ||
1139             wps_derive_keys(wps) ||
1140             wps_build_auth_type_flags(wps, msg) ||
1141             wps_build_encr_type_flags(wps, msg) ||
1142             wps_build_conn_type_flags(wps, msg) ||
1143             wps_build_config_methods_r(wps->wps->registrar, msg) ||
1144             wps_build_device_attrs(&wps->wps->dev, msg) ||
1145             wps_build_rf_bands(&wps->wps->dev, msg) ||
1146             wps_build_assoc_state(wps, msg) ||
1147             wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1148             wps_build_dev_password_id(msg, wps->dev_pw_id) ||
1149             wps_build_os_version(&wps->wps->dev, msg) ||
1150             wps_build_authenticator(wps, msg)) {
1151                 wpabuf_free(msg);
1152                 return NULL;
1153         }
1154
1155         wps->state = RECV_M3;
1156         return msg;
1157 }
1158
1159
1160 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1161 {
1162         struct wpabuf *msg;
1163         u16 err = WPS_CFG_NO_ERROR;
1164
1165         wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1166         msg = wpabuf_alloc(1000);
1167         if (msg == NULL)
1168                 return NULL;
1169
1170         if (wps->wps->ap && wps->wps->ap_setup_locked)
1171                 err = WPS_CFG_SETUP_LOCKED;
1172
1173         if (wps_build_version(msg) ||
1174             wps_build_msg_type(msg, WPS_M2D) ||
1175             wps_build_enrollee_nonce(wps, msg) ||
1176             wps_build_registrar_nonce(wps, msg) ||
1177             wps_build_uuid_r(wps, msg) ||
1178             wps_build_auth_type_flags(wps, msg) ||
1179             wps_build_encr_type_flags(wps, msg) ||
1180             wps_build_conn_type_flags(wps, msg) ||
1181             wps_build_config_methods_r(wps->wps->registrar, msg) ||
1182             wps_build_device_attrs(&wps->wps->dev, msg) ||
1183             wps_build_rf_bands(&wps->wps->dev, msg) ||
1184             wps_build_assoc_state(wps, msg) ||
1185             wps_build_config_error(msg, err) ||
1186             wps_build_os_version(&wps->wps->dev, msg)) {
1187                 wpabuf_free(msg);
1188                 return NULL;
1189         }
1190
1191         wps->state = RECV_M2D_ACK;
1192         return msg;
1193 }
1194
1195
1196 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1197 {
1198         struct wpabuf *msg, *plain;
1199
1200         wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1201
1202         wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1203
1204         plain = wpabuf_alloc(200);
1205         if (plain == NULL)
1206                 return NULL;
1207
1208         msg = wpabuf_alloc(1000);
1209         if (msg == NULL) {
1210                 wpabuf_free(plain);
1211                 return NULL;
1212         }
1213
1214         if (wps_build_version(msg) ||
1215             wps_build_msg_type(msg, WPS_M4) ||
1216             wps_build_enrollee_nonce(wps, msg) ||
1217             wps_build_r_hash(wps, msg) ||
1218             wps_build_r_snonce1(wps, plain) ||
1219             wps_build_key_wrap_auth(wps, plain) ||
1220             wps_build_encr_settings(wps, msg, plain) ||
1221             wps_build_authenticator(wps, msg)) {
1222                 wpabuf_free(plain);
1223                 wpabuf_free(msg);
1224                 return NULL;
1225         }
1226         wpabuf_free(plain);
1227
1228         wps->state = RECV_M5;
1229         return msg;
1230 }
1231
1232
1233 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1234 {
1235         struct wpabuf *msg, *plain;
1236
1237         wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1238
1239         plain = wpabuf_alloc(200);
1240         if (plain == NULL)
1241                 return NULL;
1242
1243         msg = wpabuf_alloc(1000);
1244         if (msg == NULL) {
1245                 wpabuf_free(plain);
1246                 return NULL;
1247         }
1248
1249         if (wps_build_version(msg) ||
1250             wps_build_msg_type(msg, WPS_M6) ||
1251             wps_build_enrollee_nonce(wps, msg) ||
1252             wps_build_r_snonce2(wps, plain) ||
1253             wps_build_key_wrap_auth(wps, plain) ||
1254             wps_build_encr_settings(wps, msg, plain) ||
1255             wps_build_authenticator(wps, msg)) {
1256                 wpabuf_free(plain);
1257                 wpabuf_free(msg);
1258                 return NULL;
1259         }
1260         wpabuf_free(plain);
1261
1262         wps->wps_pin_revealed = 1;
1263         wps->state = RECV_M7;
1264         return msg;
1265 }
1266
1267
1268 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1269 {
1270         struct wpabuf *msg, *plain;
1271
1272         wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1273
1274         plain = wpabuf_alloc(500);
1275         if (plain == NULL)
1276                 return NULL;
1277
1278         msg = wpabuf_alloc(1000);
1279         if (msg == NULL) {
1280                 wpabuf_free(plain);
1281                 return NULL;
1282         }
1283
1284         if (wps_build_version(msg) ||
1285             wps_build_msg_type(msg, WPS_M8) ||
1286             wps_build_enrollee_nonce(wps, msg) ||
1287             (wps->wps->ap && wps_build_cred(wps, plain)) ||
1288             (!wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
1289             wps_build_key_wrap_auth(wps, plain) ||
1290             wps_build_encr_settings(wps, msg, plain) ||
1291             wps_build_authenticator(wps, msg)) {
1292                 wpabuf_free(plain);
1293                 wpabuf_free(msg);
1294                 return NULL;
1295         }
1296         wpabuf_free(plain);
1297
1298         wps->state = RECV_DONE;
1299         return msg;
1300 }
1301
1302
1303 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
1304 {
1305         struct wpabuf *msg;
1306
1307         wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
1308
1309         msg = wpabuf_alloc(1000);
1310         if (msg == NULL)
1311                 return NULL;
1312
1313         if (wps_build_version(msg) ||
1314             wps_build_msg_type(msg, WPS_WSC_ACK) ||
1315             wps_build_enrollee_nonce(wps, msg) ||
1316             wps_build_registrar_nonce(wps, msg)) {
1317                 wpabuf_free(msg);
1318                 return NULL;
1319         }
1320
1321         return msg;
1322 }
1323
1324
1325 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
1326 {
1327         struct wpabuf *msg;
1328
1329         wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
1330
1331         msg = wpabuf_alloc(1000);
1332         if (msg == NULL)
1333                 return NULL;
1334
1335         if (wps_build_version(msg) ||
1336             wps_build_msg_type(msg, WPS_WSC_NACK) ||
1337             wps_build_enrollee_nonce(wps, msg) ||
1338             wps_build_registrar_nonce(wps, msg) ||
1339             wps_build_config_error(msg, wps->config_error)) {
1340                 wpabuf_free(msg);
1341                 return NULL;
1342         }
1343
1344         return msg;
1345 }
1346
1347
1348 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1349                                       enum wsc_op_code *op_code)
1350 {
1351         struct wpabuf *msg;
1352
1353 #ifdef CONFIG_WPS_UPNP
1354         if (wps->wps->wps_upnp) {
1355                 struct upnp_pending_message *p, *prev = NULL;
1356                 if (wps->ext_reg > 1)
1357                         wps_registrar_free_pending_m2(wps->wps);
1358                 p = wps->wps->upnp_msgs;
1359                 /* TODO: check pending message MAC address */
1360                 while (p && p->next) {
1361                         prev = p;
1362                         p = p->next;
1363                 }
1364                 if (p) {
1365                         wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
1366                                    "UPnP");
1367                         if (prev)
1368                                 prev->next = NULL;
1369                         else
1370                                 wps->wps->upnp_msgs = NULL;
1371                         msg = p->msg;
1372                         os_free(p);
1373                         *op_code = WSC_MSG;
1374                         if (wps->ext_reg == 0)
1375                                 wps->ext_reg = 1;
1376                         return msg;
1377                 }
1378         }
1379         if (wps->ext_reg) {
1380                 wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
1381                            "pending message available");
1382                 return NULL;
1383         }
1384 #endif /* CONFIG_WPS_UPNP */
1385
1386         switch (wps->state) {
1387         case SEND_M2:
1388                 if (wps_get_dev_password(wps) < 0)
1389                         msg = wps_build_m2d(wps);
1390                 else
1391                         msg = wps_build_m2(wps);
1392                 *op_code = WSC_MSG;
1393                 break;
1394         case SEND_M2D:
1395                 msg = wps_build_m2d(wps);
1396                 *op_code = WSC_MSG;
1397                 break;
1398         case SEND_M4:
1399                 msg = wps_build_m4(wps);
1400                 *op_code = WSC_MSG;
1401                 break;
1402         case SEND_M6:
1403                 msg = wps_build_m6(wps);
1404                 *op_code = WSC_MSG;
1405                 break;
1406         case SEND_M8:
1407                 msg = wps_build_m8(wps);
1408                 *op_code = WSC_MSG;
1409                 break;
1410         case RECV_DONE:
1411                 msg = wps_build_wsc_ack(wps);
1412                 *op_code = WSC_ACK;
1413                 break;
1414         case SEND_WSC_NACK:
1415                 msg = wps_build_wsc_nack(wps);
1416                 *op_code = WSC_NACK;
1417                 break;
1418         default:
1419                 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
1420                            "a message", wps->state);
1421                 msg = NULL;
1422                 break;
1423         }
1424
1425         if (*op_code == WSC_MSG && msg) {
1426                 /* Save a copy of the last message for Authenticator derivation
1427                  */
1428                 wpabuf_free(wps->last_msg);
1429                 wps->last_msg = wpabuf_dup(msg);
1430         }
1431
1432         return msg;
1433 }
1434
1435
1436 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
1437 {
1438         if (e_nonce == NULL) {
1439                 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
1440                 return -1;
1441         }
1442
1443         os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
1444         wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
1445                     wps->nonce_e, WPS_NONCE_LEN);
1446
1447         return 0;
1448 }
1449
1450
1451 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
1452 {
1453         if (r_nonce == NULL) {
1454                 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
1455                 return -1;
1456         }
1457
1458         if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
1459                 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
1460                 return -1;
1461         }
1462
1463         return 0;
1464 }
1465
1466
1467 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
1468 {
1469         if (uuid_e == NULL) {
1470                 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
1471                 return -1;
1472         }
1473
1474         os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
1475         wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
1476
1477         return 0;
1478 }
1479
1480
1481 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
1482 {
1483         if (pw_id == NULL) {
1484                 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
1485                 return -1;
1486         }
1487
1488         wps->dev_pw_id = WPA_GET_BE16(pw_id);
1489         wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
1490
1491         return 0;
1492 }
1493
1494
1495 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
1496 {
1497         if (e_hash1 == NULL) {
1498                 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
1499                 return -1;
1500         }
1501
1502         os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
1503         wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
1504
1505         return 0;
1506 }
1507
1508
1509 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
1510 {
1511         if (e_hash2 == NULL) {
1512                 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
1513                 return -1;
1514         }
1515
1516         os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
1517         wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
1518
1519         return 0;
1520 }
1521
1522
1523 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
1524 {
1525         u8 hash[SHA256_MAC_LEN];
1526         const u8 *addr[4];
1527         size_t len[4];
1528
1529         if (e_snonce1 == NULL) {
1530                 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
1531                 return -1;
1532         }
1533
1534         wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
1535                         WPS_SECRET_NONCE_LEN);
1536
1537         /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1538         addr[0] = e_snonce1;
1539         len[0] = WPS_SECRET_NONCE_LEN;
1540         addr[1] = wps->psk1;
1541         len[1] = WPS_PSK_LEN;
1542         addr[2] = wpabuf_head(wps->dh_pubkey_e);
1543         len[2] = wpabuf_len(wps->dh_pubkey_e);
1544         addr[3] = wpabuf_head(wps->dh_pubkey_r);
1545         len[3] = wpabuf_len(wps->dh_pubkey_r);
1546         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1547
1548         if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
1549                 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
1550                            "not match with the pre-committed value");
1551                 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1552                 wps_pwd_auth_fail_event(wps->wps, 0, 1);
1553                 return -1;
1554         }
1555
1556         wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
1557                    "half of the device password");
1558
1559         return 0;
1560 }
1561
1562
1563 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
1564 {
1565         u8 hash[SHA256_MAC_LEN];
1566         const u8 *addr[4];
1567         size_t len[4];
1568
1569         if (e_snonce2 == NULL) {
1570                 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
1571                 return -1;
1572         }
1573
1574         wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
1575                         WPS_SECRET_NONCE_LEN);
1576
1577         /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
1578         addr[0] = e_snonce2;
1579         len[0] = WPS_SECRET_NONCE_LEN;
1580         addr[1] = wps->psk2;
1581         len[1] = WPS_PSK_LEN;
1582         addr[2] = wpabuf_head(wps->dh_pubkey_e);
1583         len[2] = wpabuf_len(wps->dh_pubkey_e);
1584         addr[3] = wpabuf_head(wps->dh_pubkey_r);
1585         len[3] = wpabuf_len(wps->dh_pubkey_r);
1586         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1587
1588         if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
1589                 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
1590                            "not match with the pre-committed value");
1591                 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
1592                 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1593                 wps_pwd_auth_fail_event(wps->wps, 0, 2);
1594                 return -1;
1595         }
1596
1597         wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
1598                    "half of the device password");
1599         wps->wps_pin_revealed = 0;
1600         wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
1601
1602         return 0;
1603 }
1604
1605
1606 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
1607 {
1608         if (mac_addr == NULL) {
1609                 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
1610                 return -1;
1611         }
1612
1613         wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
1614                    MAC2STR(mac_addr));
1615         os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
1616         os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
1617
1618         return 0;
1619 }
1620
1621
1622 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
1623                               size_t pk_len)
1624 {
1625         if (pk == NULL || pk_len == 0) {
1626                 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
1627                 return -1;
1628         }
1629
1630         if (wps->wps->oob_conf.pubkey_hash != NULL) {
1631                 const u8 *addr[1];
1632                 u8 hash[WPS_HASH_LEN];
1633
1634                 addr[0] = pk;
1635                 sha256_vector(1, addr, &pk_len, hash);
1636                 if (os_memcmp(hash,
1637                               wpabuf_head(wps->wps->oob_conf.pubkey_hash),
1638                               WPS_OOB_PUBKEY_HASH_LEN) != 0) {
1639                         wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
1640                         return -1;
1641                 }
1642         }
1643
1644         wpabuf_free(wps->dh_pubkey_e);
1645         wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
1646         if (wps->dh_pubkey_e == NULL)
1647                 return -1;
1648
1649         return 0;
1650 }
1651
1652
1653 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
1654 {
1655         u16 auth_types;
1656
1657         if (auth == NULL) {
1658                 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
1659                            "received");
1660                 return -1;
1661         }
1662
1663         auth_types = WPA_GET_BE16(auth);
1664
1665         wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
1666                    auth_types);
1667         wps->auth_type = wps->wps->auth_types & auth_types;
1668         if (wps->auth_type == 0) {
1669                 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1670                            "authentication types (own 0x%x Enrollee 0x%x)",
1671                            wps->wps->auth_types, auth_types);
1672                 return -1;
1673         }
1674
1675         return 0;
1676 }
1677
1678
1679 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
1680 {
1681         u16 encr_types;
1682
1683         if (encr == NULL) {
1684                 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
1685                            "received");
1686                 return -1;
1687         }
1688
1689         encr_types = WPA_GET_BE16(encr);
1690
1691         wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
1692                    encr_types);
1693         wps->encr_type = wps->wps->encr_types & encr_types;
1694         if (wps->encr_type == 0) {
1695                 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1696                            "encryption types");
1697                 return -1;
1698         }
1699
1700         return 0;
1701 }
1702
1703
1704 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
1705 {
1706         if (conn == NULL) {
1707                 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
1708                            "received");
1709                 return -1;
1710         }
1711
1712         wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
1713                    *conn);
1714
1715         return 0;
1716 }
1717
1718
1719 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
1720 {
1721         u16 m;
1722
1723         if (methods == NULL) {
1724                 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
1725                 return -1;
1726         }
1727
1728         m = WPA_GET_BE16(methods);
1729
1730         wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x", m);
1731
1732         return 0;
1733 }
1734
1735
1736 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
1737 {
1738         if (state == NULL) {
1739                 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
1740                            "received");
1741                 return -1;
1742         }
1743
1744         wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
1745                    *state);
1746
1747         return 0;
1748 }
1749
1750
1751 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
1752 {
1753         u16 a;
1754
1755         if (assoc == NULL) {
1756                 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
1757                 return -1;
1758         }
1759
1760         a = WPA_GET_BE16(assoc);
1761         wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
1762
1763         return 0;
1764 }
1765
1766
1767 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
1768 {
1769         u16 e;
1770
1771         if (err == NULL) {
1772                 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
1773                 return -1;
1774         }
1775
1776         e = WPA_GET_BE16(err);
1777         wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
1778
1779         return 0;
1780 }
1781
1782
1783 static enum wps_process_res wps_process_m1(struct wps_data *wps,
1784                                            struct wps_parse_attr *attr)
1785 {
1786         wpa_printf(MSG_DEBUG, "WPS: Received M1");
1787
1788         if (wps->state != RECV_M1) {
1789                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1790                            "receiving M1", wps->state);
1791                 return WPS_FAILURE;
1792         }
1793
1794         if (wps_process_uuid_e(wps, attr->uuid_e) ||
1795             wps_process_mac_addr(wps, attr->mac_addr) ||
1796             wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1797             wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
1798             wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
1799             wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
1800             wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
1801             wps_process_config_methods(wps, attr->config_methods) ||
1802             wps_process_wps_state(wps, attr->wps_state) ||
1803             wps_process_device_attrs(&wps->peer_dev, attr) ||
1804             wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
1805             wps_process_assoc_state(wps, attr->assoc_state) ||
1806             wps_process_dev_password_id(wps, attr->dev_password_id) ||
1807             wps_process_config_error(wps, attr->config_error) ||
1808             wps_process_os_version(&wps->peer_dev, attr->os_version))
1809                 return WPS_FAILURE;
1810
1811         if (wps->dev_pw_id < 0x10 &&
1812             wps->dev_pw_id != DEV_PW_DEFAULT &&
1813             wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
1814             wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
1815             wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
1816             (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
1817              !wps->wps->registrar->pbc)) {
1818                 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
1819                            wps->dev_pw_id);
1820                 wps->state = SEND_M2D;
1821                 return WPS_CONTINUE;
1822         }
1823
1824         if (wps->dev_pw_id >= 0x10 &&
1825             wps->dev_pw_id != wps->wps->oob_dev_pw_id) {
1826                 wpa_printf(MSG_DEBUG, "WPS: OOB Device Password ID "
1827                            "%d mismatch", wps->dev_pw_id);
1828                 wps->state = SEND_M2D;
1829                 return WPS_CONTINUE;
1830         }
1831
1832         if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
1833                 if (wps_registrar_pbc_overlap(wps->wps->registrar,
1834                                               wps->mac_addr_e, wps->uuid_e)) {
1835                         wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
1836                                    "negotiation");
1837                         wps->state = SEND_M2D;
1838                         return WPS_CONTINUE;
1839                 }
1840                 wps_registrar_add_pbc_session(wps->wps->registrar,
1841                                               wps->mac_addr_e, wps->uuid_e);
1842                 wps->pbc = 1;
1843         }
1844
1845         wps->state = SEND_M2;
1846         return WPS_CONTINUE;
1847 }
1848
1849
1850 static enum wps_process_res wps_process_m3(struct wps_data *wps,
1851                                            const struct wpabuf *msg,
1852                                            struct wps_parse_attr *attr)
1853 {
1854         wpa_printf(MSG_DEBUG, "WPS: Received M3");
1855
1856         if (wps->state != RECV_M3) {
1857                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1858                            "receiving M3", wps->state);
1859                 wps->state = SEND_WSC_NACK;
1860                 return WPS_CONTINUE;
1861         }
1862
1863         if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1864             wps_process_authenticator(wps, attr->authenticator, msg) ||
1865             wps_process_e_hash1(wps, attr->e_hash1) ||
1866             wps_process_e_hash2(wps, attr->e_hash2)) {
1867                 wps->state = SEND_WSC_NACK;
1868                 return WPS_CONTINUE;
1869         }
1870
1871         wps->state = SEND_M4;
1872         return WPS_CONTINUE;
1873 }
1874
1875
1876 static enum wps_process_res wps_process_m5(struct wps_data *wps,
1877                                            const struct wpabuf *msg,
1878                                            struct wps_parse_attr *attr)
1879 {
1880         struct wpabuf *decrypted;
1881         struct wps_parse_attr eattr;
1882
1883         wpa_printf(MSG_DEBUG, "WPS: Received M5");
1884
1885         if (wps->state != RECV_M5) {
1886                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1887                            "receiving M5", wps->state);
1888                 wps->state = SEND_WSC_NACK;
1889                 return WPS_CONTINUE;
1890         }
1891
1892         if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1893             wps_process_authenticator(wps, attr->authenticator, msg)) {
1894                 wps->state = SEND_WSC_NACK;
1895                 return WPS_CONTINUE;
1896         }
1897
1898         decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1899                                               attr->encr_settings_len);
1900         if (decrypted == NULL) {
1901                 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1902                            "Settings attribute");
1903                 wps->state = SEND_WSC_NACK;
1904                 return WPS_CONTINUE;
1905         }
1906
1907         wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1908                    "attribute");
1909         if (wps_parse_msg(decrypted, &eattr) < 0 ||
1910             wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1911             wps_process_e_snonce1(wps, eattr.e_snonce1)) {
1912                 wpabuf_free(decrypted);
1913                 wps->state = SEND_WSC_NACK;
1914                 return WPS_CONTINUE;
1915         }
1916         wpabuf_free(decrypted);
1917
1918         wps->state = SEND_M6;
1919         return WPS_CONTINUE;
1920 }
1921
1922
1923 static int wps_process_ap_settings_r(struct wps_data *wps,
1924                                      struct wps_parse_attr *attr)
1925 {
1926         if (wps->wps->ap)
1927                 return 0;
1928
1929         /* AP Settings Attributes in M7 when Enrollee is an AP */
1930         if (wps_process_ap_settings(attr, &wps->cred) < 0)
1931                 return -1;
1932
1933         wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
1934
1935         /*
1936          * TODO: Provide access to AP settings and allow changes before sending
1937          * out M8. For now, just copy the settings unchanged into M8.
1938          */
1939
1940         return 0;
1941 }
1942
1943
1944 static enum wps_process_res wps_process_m7(struct wps_data *wps,
1945                                            const struct wpabuf *msg,
1946                                            struct wps_parse_attr *attr)
1947 {
1948         struct wpabuf *decrypted;
1949         struct wps_parse_attr eattr;
1950
1951         wpa_printf(MSG_DEBUG, "WPS: Received M7");
1952
1953         if (wps->state != RECV_M7) {
1954                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1955                            "receiving M7", wps->state);
1956                 wps->state = SEND_WSC_NACK;
1957                 return WPS_CONTINUE;
1958         }
1959
1960         if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1961             wps_process_authenticator(wps, attr->authenticator, msg)) {
1962                 wps->state = SEND_WSC_NACK;
1963                 return WPS_CONTINUE;
1964         }
1965
1966         decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1967                                               attr->encr_settings_len);
1968         if (decrypted == NULL) {
1969                 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1970                            "Settings attribute");
1971                 wps->state = SEND_WSC_NACK;
1972                 return WPS_CONTINUE;
1973         }
1974
1975         wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1976                    "attribute");
1977         if (wps_parse_msg(decrypted, &eattr) < 0 ||
1978             wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1979             wps_process_e_snonce2(wps, eattr.e_snonce2) ||
1980             wps_process_ap_settings_r(wps, &eattr)) {
1981                 wpabuf_free(decrypted);
1982                 wps->state = SEND_WSC_NACK;
1983                 return WPS_CONTINUE;
1984         }
1985
1986         wpabuf_free(decrypted);
1987
1988         wps->state = SEND_M8;
1989         return WPS_CONTINUE;
1990 }
1991
1992
1993 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
1994                                                 const struct wpabuf *msg)
1995 {
1996         struct wps_parse_attr attr;
1997         enum wps_process_res ret = WPS_CONTINUE;
1998
1999         wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
2000
2001         if (wps_parse_msg(msg, &attr) < 0)
2002                 return WPS_FAILURE;
2003
2004         if (!wps_version_supported(attr.version)) {
2005                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2006                            attr.version ? *attr.version : 0);
2007                 return WPS_FAILURE;
2008         }
2009
2010         if (attr.msg_type == NULL) {
2011                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2012                 return WPS_FAILURE;
2013         }
2014
2015         if (*attr.msg_type != WPS_M1 &&
2016             (attr.registrar_nonce == NULL ||
2017              os_memcmp(wps->nonce_r, attr.registrar_nonce,
2018                        WPS_NONCE_LEN != 0))) {
2019                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2020                 return WPS_FAILURE;
2021         }
2022
2023         switch (*attr.msg_type) {
2024         case WPS_M1:
2025 #ifdef CONFIG_WPS_UPNP
2026                 if (wps->wps->wps_upnp && attr.mac_addr) {
2027                         /* Remove old pending messages when starting new run */
2028                         wps_free_pending_msgs(wps->wps->upnp_msgs);
2029                         wps->wps->upnp_msgs = NULL;
2030
2031                         upnp_wps_device_send_wlan_event(
2032                                 wps->wps->wps_upnp, attr.mac_addr,
2033                                 UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
2034                 }
2035 #endif /* CONFIG_WPS_UPNP */
2036                 ret = wps_process_m1(wps, &attr);
2037                 break;
2038         case WPS_M3:
2039                 ret = wps_process_m3(wps, msg, &attr);
2040                 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2041                         wps_fail_event(wps->wps, WPS_M3);
2042                 break;
2043         case WPS_M5:
2044                 ret = wps_process_m5(wps, msg, &attr);
2045                 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2046                         wps_fail_event(wps->wps, WPS_M5);
2047                 break;
2048         case WPS_M7:
2049                 ret = wps_process_m7(wps, msg, &attr);
2050                 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2051                         wps_fail_event(wps->wps, WPS_M7);
2052                 break;
2053         default:
2054                 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
2055                            *attr.msg_type);
2056                 return WPS_FAILURE;
2057         }
2058
2059         if (ret == WPS_CONTINUE) {
2060                 /* Save a copy of the last message for Authenticator derivation
2061                  */
2062                 wpabuf_free(wps->last_msg);
2063                 wps->last_msg = wpabuf_dup(msg);
2064         }
2065
2066         return ret;
2067 }
2068
2069
2070 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
2071                                                 const struct wpabuf *msg)
2072 {
2073         struct wps_parse_attr attr;
2074
2075         wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
2076
2077         if (wps_parse_msg(msg, &attr) < 0)
2078                 return WPS_FAILURE;
2079
2080         if (!wps_version_supported(attr.version)) {
2081                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2082                            attr.version ? *attr.version : 0);
2083                 return WPS_FAILURE;
2084         }
2085
2086         if (attr.msg_type == NULL) {
2087                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2088                 return WPS_FAILURE;
2089         }
2090
2091         if (*attr.msg_type != WPS_WSC_ACK) {
2092                 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2093                            *attr.msg_type);
2094                 return WPS_FAILURE;
2095         }
2096
2097 #ifdef CONFIG_WPS_UPNP
2098         if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
2099             upnp_wps_subscribers(wps->wps->wps_upnp)) {
2100                 if (wps->wps->upnp_msgs)
2101                         return WPS_CONTINUE;
2102                 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2103                            "external Registrar");
2104                 return WPS_PENDING;
2105         }
2106 #endif /* CONFIG_WPS_UPNP */
2107
2108         if (attr.registrar_nonce == NULL ||
2109             os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2110         {
2111                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2112                 return WPS_FAILURE;
2113         }
2114
2115         if (attr.enrollee_nonce == NULL ||
2116             os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2117                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2118                 return WPS_FAILURE;
2119         }
2120
2121         if (wps->state == RECV_M2D_ACK) {
2122 #ifdef CONFIG_WPS_UPNP
2123                 if (wps->wps->wps_upnp &&
2124                     upnp_wps_subscribers(wps->wps->wps_upnp)) {
2125                         if (wps->wps->upnp_msgs)
2126                                 return WPS_CONTINUE;
2127                         if (wps->ext_reg == 0)
2128                                 wps->ext_reg = 1;
2129                         wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2130                                    "external Registrar");
2131                         return WPS_PENDING;
2132                 }
2133 #endif /* CONFIG_WPS_UPNP */
2134
2135                 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
2136                            "terminate negotiation");
2137         }
2138
2139         return WPS_FAILURE;
2140 }
2141
2142
2143 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
2144                                                  const struct wpabuf *msg)
2145 {
2146         struct wps_parse_attr attr;
2147         int old_state;
2148
2149         wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
2150
2151         old_state = wps->state;
2152         wps->state = SEND_WSC_NACK;
2153
2154         if (wps_parse_msg(msg, &attr) < 0)
2155                 return WPS_FAILURE;
2156
2157         if (!wps_version_supported(attr.version)) {
2158                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2159                            attr.version ? *attr.version : 0);
2160                 return WPS_FAILURE;
2161         }
2162
2163         if (attr.msg_type == NULL) {
2164                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2165                 return WPS_FAILURE;
2166         }
2167
2168         if (*attr.msg_type != WPS_WSC_NACK) {
2169                 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2170                            *attr.msg_type);
2171                 return WPS_FAILURE;
2172         }
2173
2174 #ifdef CONFIG_WPS_UPNP
2175         if (wps->wps->wps_upnp && wps->ext_reg) {
2176                 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2177                            "Registrar terminated by the Enrollee");
2178                 return WPS_FAILURE;
2179         }
2180 #endif /* CONFIG_WPS_UPNP */
2181
2182         if (attr.registrar_nonce == NULL ||
2183             os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2184         {
2185                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2186                 return WPS_FAILURE;
2187         }
2188
2189         if (attr.enrollee_nonce == NULL ||
2190             os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2191                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2192                 return WPS_FAILURE;
2193         }
2194
2195         if (attr.config_error == NULL) {
2196                 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
2197                            "in WSC_NACK");
2198                 return WPS_FAILURE;
2199         }
2200
2201         wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
2202                    "Configuration Error %d", WPA_GET_BE16(attr.config_error));
2203
2204         switch (old_state) {
2205         case RECV_M3:
2206                 wps_fail_event(wps->wps, WPS_M2);
2207                 break;
2208         case RECV_M5:
2209                 wps_fail_event(wps->wps, WPS_M4);
2210                 break;
2211         case RECV_M7:
2212                 wps_fail_event(wps->wps, WPS_M6);
2213                 break;
2214         case RECV_DONE:
2215                 wps_fail_event(wps->wps, WPS_M8);
2216                 break;
2217         default:
2218                 break;
2219         }
2220
2221         return WPS_FAILURE;
2222 }
2223
2224
2225 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
2226                                                  const struct wpabuf *msg)
2227 {
2228         struct wps_parse_attr attr;
2229
2230         wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
2231
2232         if (wps->state != RECV_DONE &&
2233             (!wps->wps->wps_upnp || !wps->ext_reg)) {
2234                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2235                            "receiving WSC_Done", wps->state);
2236                 return WPS_FAILURE;
2237         }
2238
2239         if (wps_parse_msg(msg, &attr) < 0)
2240                 return WPS_FAILURE;
2241
2242         if (!wps_version_supported(attr.version)) {
2243                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2244                            attr.version ? *attr.version : 0);
2245                 return WPS_FAILURE;
2246         }
2247
2248         if (attr.msg_type == NULL) {
2249                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2250                 return WPS_FAILURE;
2251         }
2252
2253         if (*attr.msg_type != WPS_WSC_DONE) {
2254                 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2255                            *attr.msg_type);
2256                 return WPS_FAILURE;
2257         }
2258
2259 #ifdef CONFIG_WPS_UPNP
2260         if (wps->wps->wps_upnp && wps->ext_reg) {
2261                 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2262                            "Registrar completed successfully");
2263                 return WPS_DONE;
2264         }
2265 #endif /* CONFIG_WPS_UPNP */
2266
2267         if (attr.registrar_nonce == NULL ||
2268             os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2269         {
2270                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2271                 return WPS_FAILURE;
2272         }
2273
2274         if (attr.enrollee_nonce == NULL ||
2275             os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2276                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2277                 return WPS_FAILURE;
2278         }
2279
2280         wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
2281
2282         if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
2283             wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
2284                 struct wps_credential cred;
2285
2286                 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
2287                            "on first Enrollee connection");
2288
2289                 os_memset(&cred, 0, sizeof(cred));
2290                 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
2291                 cred.ssid_len = wps->wps->ssid_len;
2292                 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
2293                 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
2294                 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
2295                 cred.key_len = wps->new_psk_len;
2296
2297                 wps->wps->wps_state = WPS_STATE_CONFIGURED;
2298                 wpa_hexdump_ascii_key(MSG_DEBUG,
2299                                       "WPS: Generated random passphrase",
2300                                       wps->new_psk, wps->new_psk_len);
2301                 if (wps->wps->cred_cb)
2302                         wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
2303
2304                 os_free(wps->new_psk);
2305                 wps->new_psk = NULL;
2306         }
2307
2308         if (!wps->wps->ap) {
2309                 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based "
2310                            "on the modified AP configuration");
2311                 if (wps->wps->cred_cb)
2312                         wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2313         }
2314
2315         if (wps->new_psk) {
2316                 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
2317                                    wps->new_psk, wps->new_psk_len)) {
2318                         wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
2319                                    "new PSK");
2320                 }
2321                 os_free(wps->new_psk);
2322                 wps->new_psk = NULL;
2323         }
2324
2325         wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e);
2326
2327         if (wps->pbc) {
2328                 wps_registrar_remove_pbc_session(wps->wps->registrar,
2329                                                  wps->mac_addr_e, wps->uuid_e);
2330                 wps_registrar_pbc_completed(wps->wps->registrar);
2331         }
2332
2333         wps_success_event(wps->wps);
2334
2335         return WPS_DONE;
2336 }
2337
2338
2339 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
2340                                                enum wsc_op_code op_code,
2341                                                const struct wpabuf *msg)
2342 {
2343         enum wps_process_res ret;
2344
2345         wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
2346                    "op_code=%d)",
2347                    (unsigned long) wpabuf_len(msg), op_code);
2348
2349 #ifdef CONFIG_WPS_UPNP
2350         if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
2351                 struct wps_parse_attr attr;
2352                 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
2353                     *attr.msg_type == WPS_M3)
2354                         wps->ext_reg = 2; /* past M2/M2D phase */
2355         }
2356         if (wps->ext_reg > 1)
2357                 wps_registrar_free_pending_m2(wps->wps);
2358         if (wps->wps->wps_upnp && wps->ext_reg &&
2359             wps->wps->upnp_msgs == NULL &&
2360             (op_code == WSC_MSG || op_code == WSC_Done)) {
2361                 struct wps_parse_attr attr;
2362                 int type;
2363                 if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
2364                         type = -1;
2365                 else
2366                         type = *attr.msg_type;
2367                 wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
2368                            " to external Registrar for processing", type);
2369                 upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
2370                                                 wps->mac_addr_e,
2371                                                 UPNP_WPS_WLANEVENT_TYPE_EAP,
2372                                                 msg);
2373                 if (op_code == WSC_MSG)
2374                         return WPS_PENDING;
2375         } else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
2376                 wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
2377                            "external Registrar");
2378                 return WPS_CONTINUE;
2379         }
2380 #endif /* CONFIG_WPS_UPNP */
2381
2382         switch (op_code) {
2383         case WSC_MSG:
2384                 return wps_process_wsc_msg(wps, msg);
2385         case WSC_ACK:
2386                 return wps_process_wsc_ack(wps, msg);
2387         case WSC_NACK:
2388                 return wps_process_wsc_nack(wps, msg);
2389         case WSC_Done:
2390                 ret = wps_process_wsc_done(wps, msg);
2391                 if (ret == WPS_FAILURE) {
2392                         wps->state = SEND_WSC_NACK;
2393                         wps_fail_event(wps->wps, WPS_WSC_DONE);
2394                 }
2395                 return ret;
2396         default:
2397                 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
2398                 return WPS_FAILURE;
2399         }
2400 }
2401
2402
2403 int wps_registrar_update_ie(struct wps_registrar *reg)
2404 {
2405         return wps_set_ie(reg);
2406 }
2407
2408
2409 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
2410                                                void *timeout_ctx)
2411 {
2412         struct wps_registrar *reg = eloop_ctx;
2413
2414         wpa_printf(MSG_DEBUG, "WPS: SetSelectedRegistrar timed out - "
2415                    "unselect Registrar");
2416         reg->selected_registrar = 0;
2417         reg->pbc = 0;
2418         reg->sel_reg_dev_password_id_override = -1;
2419         reg->sel_reg_config_methods_override = -1;
2420         wps_set_ie(reg);
2421 }
2422
2423
2424 /**
2425  * wps_registrar_set_selected_registrar - Notification of SetSelectedRegistrar
2426  * @reg: Registrar data from wps_registrar_init()
2427  * @msg: Received message from SetSelectedRegistrar
2428  * @msg_len: Length of msg in octets
2429  * Returns: 0 on success, -1 on failure
2430  *
2431  * This function is called when an AP receives a SetSelectedRegistrar UPnP
2432  * message.
2433  */
2434 int wps_registrar_set_selected_registrar(struct wps_registrar *reg,
2435                                          const struct wpabuf *msg)
2436 {
2437         struct wps_parse_attr attr;
2438
2439         wpa_hexdump_buf(MSG_MSGDUMP, "WPS: SetSelectedRegistrar attributes",
2440                         msg);
2441
2442         if (wps_parse_msg(msg, &attr) < 0)
2443                 return -1;
2444         if (!wps_version_supported(attr.version)) {
2445                 wpa_printf(MSG_DEBUG, "WPS: Unsupported SetSelectedRegistrar "
2446                            "version 0x%x", attr.version ? *attr.version : 0);
2447                 return -1;
2448         }
2449
2450         if (attr.selected_registrar == NULL ||
2451             *attr.selected_registrar == 0) {
2452                 wpa_printf(MSG_DEBUG, "WPS: SetSelectedRegistrar: Disable "
2453                            "Selected Registrar");
2454                 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg,
2455                                      NULL);
2456                 wps_registrar_set_selected_timeout(reg, NULL);
2457                 return 0;
2458         }
2459
2460         reg->selected_registrar = 1;
2461         reg->sel_reg_dev_password_id_override = attr.dev_password_id ?
2462                 WPA_GET_BE16(attr.dev_password_id) : DEV_PW_DEFAULT;
2463         reg->sel_reg_config_methods_override = attr.sel_reg_config_methods ?
2464                 WPA_GET_BE16(attr.sel_reg_config_methods) : -1;
2465         wps_set_ie(reg);
2466
2467         eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
2468         eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
2469                                wps_registrar_set_selected_timeout,
2470                                reg, NULL);
2471         return 0;
2472 }