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