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