WPS: Moved Credential building to use struct wps_credential
[libeap.git] / 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
25
26 struct wps_uuid_pin {
27         struct wps_uuid_pin *next;
28         u8 uuid[WPS_UUID_LEN];
29         u8 *pin;
30         size_t pin_len;
31         int locked;
32 };
33
34
35 static void wps_free_pin(struct wps_uuid_pin *pin)
36 {
37         os_free(pin->pin);
38         os_free(pin);
39 }
40
41
42 static void wps_free_pins(struct wps_uuid_pin *pins)
43 {
44         struct wps_uuid_pin *pin, *prev;
45
46         pin = pins;
47         while (pin) {
48                 prev = pin;
49                 pin = pin->next;
50                 wps_free_pin(prev);
51         }
52 }
53
54
55 struct wps_pbc_session {
56         struct wps_pbc_session *next;
57         u8 addr[ETH_ALEN];
58         u8 uuid_e[WPS_UUID_LEN];
59         struct os_time timestamp;
60 };
61
62
63 static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
64 {
65         struct wps_pbc_session *prev;
66
67         while (pbc) {
68                 prev = pbc;
69                 pbc = pbc->next;
70                 os_free(prev);
71         }
72 }
73
74
75 struct wps_registrar {
76         struct wps_context *wps;
77
78         int pbc;
79         int selected_registrar;
80
81         int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
82                           size_t psk_len);
83         int (*set_ie_cb)(void *ctx, const u8 *beacon_ie, size_t beacon_ie_len,
84                          const u8 *probe_resp_ie, size_t probe_resp_ie_len);
85         void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
86                               const struct wps_device_data *dev);
87         void *cb_ctx;
88
89         struct wps_uuid_pin *pins;
90         struct wps_pbc_session *pbc_sessions;
91 };
92
93
94 static int wps_set_ie(struct wps_registrar *reg);
95 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
96
97
98 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
99                                           const u8 *addr, const u8 *uuid_e)
100 {
101         struct wps_pbc_session *pbc, *prev = NULL;
102         struct os_time now;
103
104         os_get_time(&now);
105
106         pbc = reg->pbc_sessions;
107         while (pbc) {
108                 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
109                     os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
110                         if (prev)
111                                 prev->next = pbc->next;
112                         else
113                                 reg->pbc_sessions = pbc->next;
114                         break;
115                 }
116                 prev = pbc;
117                 pbc = pbc->next;
118         }
119
120         if (!pbc) {
121                 pbc = os_zalloc(sizeof(*pbc));
122                 if (pbc == NULL)
123                         return;
124                 os_memcpy(pbc->addr, addr, ETH_ALEN);
125                 if (uuid_e)
126                         os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
127         }
128
129         pbc->next = reg->pbc_sessions;
130         reg->pbc_sessions = pbc;
131         pbc->timestamp = now;
132
133         /* remove entries that have timed out */
134         prev = pbc;
135         pbc = pbc->next;
136
137         while (pbc) {
138                 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
139                         prev->next = NULL;
140                         wps_free_pbc_sessions(pbc);
141                         break;
142                 }
143                 prev = pbc;
144                 pbc = pbc->next;
145         }
146 }
147
148
149 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
150                                              const u8 *addr, const u8 *uuid_e)
151 {
152         struct wps_pbc_session *pbc, *prev = NULL;
153
154         pbc = reg->pbc_sessions;
155         while (pbc) {
156                 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
157                     os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
158                         if (prev)
159                                 prev->next = pbc->next;
160                         else
161                                 reg->pbc_sessions = pbc->next;
162                         os_free(pbc);
163                         break;
164                 }
165                 prev = pbc;
166                 pbc = pbc->next;
167         }
168 }
169
170
171 int wps_registrar_pbc_overlap(struct wps_registrar *reg,
172                               const u8 *addr, const u8 *uuid_e)
173 {
174         int count = 0;
175         struct wps_pbc_session *pbc;
176         struct os_time now;
177
178         os_get_time(&now);
179
180         for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
181                 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME)
182                         break;
183                 if (addr == NULL || os_memcmp(addr, pbc->addr, ETH_ALEN) ||
184                     uuid_e == NULL ||
185                     os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN))
186                         count++;
187         }
188
189         if (addr || uuid_e)
190                 count++;
191
192         return count > 1 ? 1 : 0;
193 }
194
195
196 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
197 {
198         wpa_printf(MSG_DEBUG, "WPS:  * Wi-Fi Protected Setup State (%d)",
199                    wps->wps_state);
200         wpabuf_put_be16(msg, ATTR_WPS_STATE);
201         wpabuf_put_be16(msg, 1);
202         wpabuf_put_u8(msg, wps->wps_state);
203         return 0;
204 }
205
206
207 static int wps_build_ap_setup_locked(struct wps_context *wps,
208                                      struct wpabuf *msg)
209 {
210         if (wps->ap_setup_locked) {
211                 wpa_printf(MSG_DEBUG, "WPS:  * AP Setup Locked");
212                 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
213                 wpabuf_put_be16(msg, 1);
214                 wpabuf_put_u8(msg, 1);
215         }
216         return 0;
217 }
218
219
220 static int wps_build_selected_registrar(struct wps_registrar *reg,
221                                         struct wpabuf *msg)
222 {
223         if (!reg->selected_registrar)
224                 return 0;
225         wpa_printf(MSG_DEBUG, "WPS:  * Selected Registrar");
226         wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
227         wpabuf_put_be16(msg, 1);
228         wpabuf_put_u8(msg, 1);
229         return 0;
230 }
231
232
233 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
234                                              struct wpabuf *msg)
235 {
236         u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
237         if (!reg->selected_registrar)
238                 return 0;
239         wpa_printf(MSG_DEBUG, "WPS:  * Device Password ID (%d)", id);
240         wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
241         wpabuf_put_be16(msg, 2);
242         wpabuf_put_be16(msg, id);
243         return 0;
244 }
245
246
247 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
248                                             struct wpabuf *msg)
249 {
250         u16 methods;
251         if (!reg->selected_registrar)
252                 return 0;
253         methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
254         if (reg->pbc)
255                 methods |= WPS_CONFIG_PUSHBUTTON;
256         wpa_printf(MSG_DEBUG, "WPS:  * Selected Registrar Config Methods (%x)",
257                    methods);
258         wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
259         wpabuf_put_be16(msg, 2);
260         wpabuf_put_be16(msg, methods);
261         return 0;
262 }
263
264
265 static int wps_build_probe_config_methods(struct wps_registrar *reg,
266                                           struct wpabuf *msg)
267 {
268         u16 methods;
269         methods = 0;
270         wpa_printf(MSG_DEBUG, "WPS:  * Config Methods (%x)", methods);
271         wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
272         wpabuf_put_be16(msg, 2);
273         wpabuf_put_be16(msg, methods);
274         return 0;
275 }
276
277
278 static int wps_build_config_methods(struct wps_registrar *reg,
279                                     struct wpabuf *msg)
280 {
281         u16 methods;
282         methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
283         if (reg->pbc)
284                 methods |= WPS_CONFIG_PUSHBUTTON;
285         wpa_printf(MSG_DEBUG, "WPS:  * Config Methods (%x)", methods);
286         wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
287         wpabuf_put_be16(msg, 2);
288         wpabuf_put_be16(msg, methods);
289         return 0;
290 }
291
292
293 static int wps_build_rf_bands(struct wps_registrar *reg, struct wpabuf *msg)
294 {
295         u8 bands = WPS_RF_24GHZ /* TODO: | WPS_RF_50GHZ */;
296         wpa_printf(MSG_DEBUG, "WPS:  * RF Bands (%x)", bands);
297         wpabuf_put_be16(msg, ATTR_RF_BANDS);
298         wpabuf_put_be16(msg, 1);
299         wpabuf_put_u8(msg, bands);
300         return 0;
301 }
302
303
304 static int wps_build_uuid_e(struct wps_registrar *reg, struct wpabuf *msg)
305 {
306         wpa_printf(MSG_DEBUG, "WPS:  * UUID-E");
307         wpabuf_put_be16(msg, ATTR_UUID_E);
308         wpabuf_put_be16(msg, WPS_UUID_LEN);
309         wpabuf_put_data(msg, reg->wps->uuid, WPS_UUID_LEN);
310         return 0;
311 }
312
313
314 static int wps_build_resp_type(struct wps_registrar *reg, struct wpabuf *msg)
315 {
316         u8 resp = reg->wps->ap ? WPS_RESP_AP : WPS_RESP_REGISTRAR;
317         wpa_printf(MSG_DEBUG, "WPS:  * Response Type (%d)", resp);
318         wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
319         wpabuf_put_be16(msg, 1);
320         wpabuf_put_u8(msg, resp);
321         return 0;
322 }
323
324
325 struct wps_registrar *
326 wps_registrar_init(struct wps_context *wps,
327                    const struct wps_registrar_config *cfg)
328 {
329         struct wps_registrar *reg = os_zalloc(sizeof(*reg));
330         if (reg == NULL)
331                 return NULL;
332
333         reg->wps = wps;
334         reg->new_psk_cb = cfg->new_psk_cb;
335         reg->set_ie_cb = cfg->set_ie_cb;
336         reg->pin_needed_cb = cfg->pin_needed_cb;
337         reg->cb_ctx = cfg->cb_ctx;
338
339         if (wps_set_ie(reg)) {
340                 wps_registrar_deinit(reg);
341                 return NULL;
342         }
343
344         return reg;
345 }
346
347
348 void wps_registrar_deinit(struct wps_registrar *reg)
349 {
350         if (reg == NULL)
351                 return;
352         eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
353         wps_free_pins(reg->pins);
354         wps_free_pbc_sessions(reg->pbc_sessions);
355         os_free(reg);
356 }
357
358
359 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
360                           const u8 *pin, size_t pin_len)
361 {
362         struct wps_uuid_pin *p;
363
364         p = os_zalloc(sizeof(*p));
365         if (p == NULL)
366                 return -1;
367         os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
368         p->pin = os_malloc(pin_len);
369         if (p->pin == NULL) {
370                 os_free(p);
371                 return -1;
372         }
373         os_memcpy(p->pin, pin, pin_len);
374         p->pin_len = pin_len;
375
376         p->next = reg->pins;
377         reg->pins = p;
378
379         wpa_printf(MSG_DEBUG, "WPS: A new PIN configured");
380         wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
381         wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
382         reg->selected_registrar = 1;
383         reg->pbc = 0;
384         wps_set_ie(reg);
385
386         return 0;
387 }
388
389
390 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
391 {
392         struct wps_uuid_pin *pin, *prev;
393
394         prev = NULL;
395         pin = reg->pins;
396         while (pin) {
397                 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
398                         if (prev == NULL)
399                                 reg->pins = pin->next;
400                         else
401                                 prev->next = pin->next;
402                         wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
403                                     pin->uuid, WPS_UUID_LEN);
404                         wps_free_pin(pin);
405                         return 0;
406                 }
407                 prev = pin;
408                 pin = pin->next;
409         }
410
411         return -1;
412 }
413
414
415 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
416                                         const u8 *uuid, size_t *pin_len)
417 {
418         struct wps_uuid_pin *pin;
419
420         pin = reg->pins;
421         while (pin) {
422                 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
423                         /*
424                          * Lock the PIN to avoid attacks based on concurrent
425                          * re-use of the PIN that could otherwise avoid PIN
426                          * invalidations.
427                          */
428                         if (pin->locked) {
429                                 wpa_printf(MSG_DEBUG, "WPS: Selected PIN "
430                                            "locked - do not allow concurrent "
431                                            "re-use");
432                                 return NULL;
433                         }
434                         *pin_len = pin->pin_len;
435                         pin->locked = 1;
436                         return pin->pin;
437                 }
438                 pin = pin->next;
439         }
440
441         return NULL;
442 }
443
444
445 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
446 {
447         struct wps_uuid_pin *pin;
448
449         pin = reg->pins;
450         while (pin) {
451                 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
452                         pin->locked = 0;
453                         return 0;
454                 }
455                 pin = pin->next;
456         }
457
458         return -1;
459 }
460
461
462 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
463 {
464         struct wps_registrar *reg = eloop_ctx;
465
466         wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
467         reg->selected_registrar = 0;
468         reg->pbc = 0;
469         wps_set_ie(reg);
470 }
471
472
473 int wps_registrar_button_pushed(struct wps_registrar *reg)
474 {
475         if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
476                 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
477                            "mode");
478                 return -1;
479         }
480         wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
481         reg->selected_registrar = 1;
482         reg->pbc = 1;
483         wps_set_ie(reg);
484
485         eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
486         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
487                                reg, NULL);
488         return 0;
489 }
490
491
492 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
493 {
494         wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
495         eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
496         reg->selected_registrar = 0;
497         reg->pbc = 0;
498         wps_set_ie(reg);
499 }
500
501
502 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
503                                 const struct wpabuf *wps_data)
504 {
505         struct wps_parse_attr attr;
506         u16 methods;
507
508         wpa_hexdump_buf(MSG_MSGDUMP,
509                         "WPS: Probe Request with WPS data received",
510                         wps_data);
511
512         if (wps_parse_msg(wps_data, &attr) < 0 ||
513             attr.version == NULL || *attr.version != WPS_VERSION) {
514                 wpa_printf(MSG_DEBUG, "WPS: Unsupported ProbeReq WPS IE "
515                            "version 0x%x", attr.version ? *attr.version : 0);
516                 return;
517         }
518
519         if (attr.config_methods == NULL) {
520                 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
521                            "Probe Request");
522                 return;
523         }
524
525         methods = WPA_GET_BE16(attr.config_methods);
526         if (!(methods & WPS_CONFIG_PUSHBUTTON))
527                 return; /* Not PBC */
528
529         wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
530                    MACSTR, MAC2STR(addr));
531
532         wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
533 }
534
535
536 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
537                           const u8 *psk, size_t psk_len)
538 {
539         if (reg->new_psk_cb == NULL)
540                 return 0;
541
542         return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
543 }
544
545
546 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
547                               const struct wps_device_data *dev)
548 {
549         if (reg->pin_needed_cb == NULL)
550                 return;
551
552         reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
553 }
554
555
556 static int wps_cb_set_ie(struct wps_registrar *reg,
557                          const struct wpabuf *beacon_ie,
558                          const struct wpabuf *probe_resp_ie)
559 {
560         if (reg->set_ie_cb == NULL)
561                 return 0;
562
563         return reg->set_ie_cb(reg->cb_ctx, wpabuf_head(beacon_ie),
564                               wpabuf_len(beacon_ie),
565                               wpabuf_head(probe_resp_ie),
566                               wpabuf_len(probe_resp_ie));
567 }
568
569
570 static int wps_set_ie(struct wps_registrar *reg)
571 {
572         struct wpabuf *beacon;
573         struct wpabuf *probe;
574         int ret;
575         u8 *blen, *plen;
576
577         wpa_printf(MSG_DEBUG, "WPS: Build Beacon and Probe Response IEs");
578
579         beacon = wpabuf_alloc(300);
580         if (beacon == NULL)
581                 return -1;
582         probe = wpabuf_alloc(300);
583         if (probe == NULL) {
584                 wpabuf_free(beacon);
585                 return -1;
586         }
587
588         wpabuf_put_u8(beacon, WLAN_EID_VENDOR_SPECIFIC);
589         blen = wpabuf_put(beacon, 1);
590         wpabuf_put_be32(beacon, WPS_DEV_OUI_WFA);
591
592         wpabuf_put_u8(probe, WLAN_EID_VENDOR_SPECIFIC);
593         plen = wpabuf_put(probe, 1);
594         wpabuf_put_be32(probe, WPS_DEV_OUI_WFA);
595
596         if (wps_build_version(beacon) ||
597             wps_build_wps_state(reg->wps, beacon) ||
598             wps_build_ap_setup_locked(reg->wps, beacon) ||
599             wps_build_selected_registrar(reg, beacon) ||
600             wps_build_sel_reg_dev_password_id(reg, beacon) ||
601             wps_build_sel_reg_config_methods(reg, beacon) ||
602             wps_build_version(probe) ||
603             wps_build_wps_state(reg->wps, probe) ||
604             wps_build_ap_setup_locked(reg->wps, probe) ||
605             wps_build_selected_registrar(reg, probe) ||
606             wps_build_sel_reg_dev_password_id(reg, probe) ||
607             wps_build_sel_reg_config_methods(reg, probe) ||
608             wps_build_resp_type(reg, probe) ||
609             wps_build_uuid_e(reg, probe) ||
610             wps_build_device_attrs(&reg->wps->dev, probe) ||
611             wps_build_probe_config_methods(reg, probe) ||
612             wps_build_rf_bands(reg, probe)) {
613                 wpabuf_free(beacon);
614                 wpabuf_free(probe);
615                 return -1;
616         }
617
618         *blen = wpabuf_len(beacon) - 2;
619         *plen = wpabuf_len(probe) - 2;
620
621         ret = wps_cb_set_ie(reg, beacon, probe);
622         wpabuf_free(beacon);
623         wpabuf_free(probe);
624
625         return ret;
626 }
627
628
629 static int wps_get_dev_password(struct wps_data *wps)
630 {
631         const u8 *pin;
632         size_t pin_len;
633
634         os_free(wps->dev_password);
635         wps->dev_password = NULL;
636
637         if (wps->pbc) {
638                 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
639                 pin = (const u8 *) "00000000";
640                 pin_len = 8;
641         } else {
642                 pin = wps_registrar_get_pin(wps->registrar, wps->uuid_e,
643                                             &pin_len);
644         }
645         if (pin == NULL) {
646                 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
647                            "the Enrollee");
648                 wps_cb_pin_needed(wps->registrar, wps->uuid_e, &wps->peer_dev);
649                 return -1;
650         }
651
652         wps->dev_password = os_malloc(pin_len);
653         if (wps->dev_password == NULL)
654                 return -1;
655         os_memcpy(wps->dev_password, pin, pin_len);
656         wps->dev_password_len = pin_len;
657
658         return 0;
659 }
660
661
662 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
663 {
664         wpa_printf(MSG_DEBUG, "WPS:  * UUID-R");
665         wpabuf_put_be16(msg, ATTR_UUID_R);
666         wpabuf_put_be16(msg, WPS_UUID_LEN);
667         wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
668         return 0;
669 }
670
671
672 static int wps_build_dev_password_id(struct wps_data *wps, struct wpabuf *msg)
673 {
674         wpa_printf(MSG_DEBUG, "WPS:  * Device Password ID");
675         wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
676         wpabuf_put_be16(msg, 2);
677         wpabuf_put_be16(msg, DEV_PW_DEFAULT);
678         return 0;
679 }
680
681
682 static int wps_build_config_error(struct wps_data *wps, struct wpabuf *msg)
683 {
684         wpa_printf(MSG_DEBUG, "WPS:  * Configuration Error");
685         wpabuf_put_be16(msg, ATTR_CONFIG_ERROR);
686         wpabuf_put_be16(msg, 2);
687         wpabuf_put_be16(msg, WPS_CFG_NO_ERROR);
688         return 0;
689 }
690
691
692 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
693 {
694         u8 *hash;
695         const u8 *addr[4];
696         size_t len[4];
697
698         if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
699                 return -1;
700         wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
701         wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
702                     wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
703
704         if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
705                 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
706                            "R-Hash derivation");
707                 return -1;
708         }
709
710         wpa_printf(MSG_DEBUG, "WPS:  * R-Hash1");
711         wpabuf_put_be16(msg, ATTR_R_HASH1);
712         wpabuf_put_be16(msg, SHA256_MAC_LEN);
713         hash = wpabuf_put(msg, SHA256_MAC_LEN);
714         /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
715         addr[0] = wps->snonce;
716         len[0] = WPS_SECRET_NONCE_LEN;
717         addr[1] = wps->psk1;
718         len[1] = WPS_PSK_LEN;
719         addr[2] = wpabuf_head(wps->dh_pubkey_e);
720         len[2] = wpabuf_len(wps->dh_pubkey_e);
721         addr[3] = wpabuf_head(wps->dh_pubkey_r);
722         len[3] = wpabuf_len(wps->dh_pubkey_r);
723         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
724         wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
725
726         wpa_printf(MSG_DEBUG, "WPS:  * R-Hash2");
727         wpabuf_put_be16(msg, ATTR_R_HASH2);
728         wpabuf_put_be16(msg, SHA256_MAC_LEN);
729         hash = wpabuf_put(msg, SHA256_MAC_LEN);
730         /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
731         addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
732         addr[1] = wps->psk2;
733         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
734         wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
735
736         return 0;
737 }
738
739
740 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
741 {
742         wpa_printf(MSG_DEBUG, "WPS:  * R-SNonce1");
743         wpabuf_put_be16(msg, ATTR_R_SNONCE1);
744         wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
745         wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
746         return 0;
747 }
748
749
750 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
751 {
752         wpa_printf(MSG_DEBUG, "WPS:  * R-SNonce2");
753         wpabuf_put_be16(msg, ATTR_R_SNONCE2);
754         wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
755         wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
756                         WPS_SECRET_NONCE_LEN);
757         return 0;
758 }
759
760
761 static int wps_build_cred_network_idx(struct wpabuf *msg,
762                                       struct wps_credential *cred)
763 {
764         wpa_printf(MSG_DEBUG, "WPS:  * Network Index");
765         wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
766         wpabuf_put_be16(msg, 1);
767         wpabuf_put_u8(msg, 0);
768         return 0;
769 }
770
771
772 static int wps_build_cred_ssid(struct wpabuf *msg,
773                                struct wps_credential *cred)
774 {
775         wpa_printf(MSG_DEBUG, "WPS:  * SSID");
776         wpabuf_put_be16(msg, ATTR_SSID);
777         wpabuf_put_be16(msg, cred->ssid_len);
778         wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
779         return 0;
780 }
781
782
783 static int wps_build_cred_auth_type(struct wpabuf *msg,
784                                     struct wps_credential *cred)
785 {
786         wpa_printf(MSG_DEBUG, "WPS:  * Authentication Type (0x%x)",
787                    cred->auth_type);
788         wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
789         wpabuf_put_be16(msg, 2);
790         wpabuf_put_be16(msg, cred->auth_type);
791         return 0;
792 }
793
794
795 static int wps_build_cred_encr_type(struct wpabuf *msg,
796                                     struct wps_credential *cred)
797 {
798         wpa_printf(MSG_DEBUG, "WPS:  * Encryption Type (0x%x)",
799                    cred->encr_type);
800         wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
801         wpabuf_put_be16(msg, 2);
802         wpabuf_put_be16(msg, cred->encr_type);
803         return 0;
804 }
805
806
807 static int wps_build_cred_network_key(struct wpabuf *msg,
808                                       struct wps_credential *cred)
809 {
810         wpa_printf(MSG_DEBUG, "WPS:  * Network Key");
811         wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
812         wpabuf_put_be16(msg, cred->key_len);
813         wpabuf_put_data(msg, cred->key, cred->key_len);
814         return 0;
815 }
816
817
818 static int wps_build_cred_mac_addr(struct wpabuf *msg,
819                                    struct wps_credential *cred)
820 {
821         wpa_printf(MSG_DEBUG, "WPS:  * MAC Address");
822         wpabuf_put_be16(msg, ATTR_MAC_ADDR);
823         wpabuf_put_be16(msg, ETH_ALEN);
824         wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
825         return 0;
826 }
827
828
829 static int wps_build_credential(struct wpabuf *msg,
830                                 struct wps_credential *cred)
831 {
832         if (wps_build_cred_network_idx(msg, cred) ||
833             wps_build_cred_ssid(msg, cred) ||
834             wps_build_cred_auth_type(msg, cred) ||
835             wps_build_cred_encr_type(msg, cred) ||
836             wps_build_cred_network_key(msg, cred) ||
837             wps_build_cred_mac_addr(msg, cred))
838                 return -1;
839         return 0;
840 }
841
842
843 static int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
844 {
845         struct wpabuf *cred;
846
847         wpa_printf(MSG_DEBUG, "WPS:  * Credential");
848         os_memset(&wps->cred, 0, sizeof(wps->cred));
849
850         os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
851         wps->cred.ssid_len = wps->wps->ssid_len;
852
853         /* Select the best authentication and encryption type */
854         if (wps->auth_type & WPS_AUTH_WPA2PSK)
855                 wps->auth_type = WPS_AUTH_WPA2PSK;
856         else if (wps->auth_type & WPS_AUTH_WPAPSK)
857                 wps->auth_type = WPS_AUTH_WPAPSK;
858         else if (wps->auth_type & WPS_AUTH_OPEN)
859                 wps->auth_type = WPS_AUTH_OPEN;
860         else if (wps->auth_type & WPS_AUTH_SHARED)
861                 wps->auth_type = WPS_AUTH_SHARED;
862         else {
863                 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
864                            wps->auth_type);
865                 return -1;
866         }
867         wps->cred.auth_type = wps->auth_type;
868
869         if (wps->auth_type == WPS_AUTH_WPA2PSK ||
870             wps->auth_type == WPS_AUTH_WPAPSK) {
871                 if (wps->encr_type & WPS_ENCR_AES)
872                         wps->encr_type = WPS_ENCR_AES;
873                 else if (wps->encr_type & WPS_ENCR_TKIP)
874                         wps->encr_type = WPS_ENCR_TKIP;
875                 else {
876                         wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
877                                    "type for WPA/WPA2");
878                         return -1;
879                 }
880         } else {
881                 if (wps->encr_type & WPS_ENCR_WEP)
882                         wps->encr_type = WPS_ENCR_WEP;
883                 else if (wps->encr_type & WPS_ENCR_NONE)
884                         wps->encr_type = WPS_ENCR_NONE;
885                 else {
886                         wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
887                                    "type for non-WPA/WPA2 mode");
888                         return -1;
889                 }
890         }
891         wps->cred.encr_type = wps->encr_type;
892         os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
893
894         if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap) {
895                 u8 r[16];
896                 /* Generate a random passphrase */
897                 if (os_get_random(r, sizeof(r)) < 0)
898                         return -1;
899                 os_free(wps->new_psk);
900                 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
901                 if (wps->new_psk == NULL)
902                         return -1;
903                 wps->new_psk_len--; /* remove newline */
904                 while (wps->new_psk_len &&
905                        wps->new_psk[wps->new_psk_len - 1] == '=')
906                         wps->new_psk_len--;
907                 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
908                                       wps->new_psk, wps->new_psk_len);
909                 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
910                 wps->cred.key_len = wps->new_psk_len;
911         } else if (wps->wps->network_key) {
912                 os_memcpy(wps->cred.key, wps->wps->network_key,
913                           wps->wps->network_key_len);
914                 wps->cred.key_len = wps->wps->network_key_len;
915         } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
916                 char hex[65];
917                 /* Generate a random per-device PSK */
918                 os_free(wps->new_psk);
919                 wps->new_psk_len = 32;
920                 wps->new_psk = os_malloc(wps->new_psk_len);
921                 if (wps->new_psk == NULL)
922                         return -1;
923                 if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
924                         os_free(wps->new_psk);
925                         wps->new_psk = NULL;
926                         return -1;
927                 }
928                 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
929                                 wps->new_psk, wps->new_psk_len);
930                 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
931                                  wps->new_psk_len);
932                 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
933                 wps->cred.key_len = wps->new_psk_len * 2;
934         }
935
936         cred = wpabuf_alloc(200);
937         if (cred == NULL)
938                 return -1;
939
940         if (wps_build_credential(cred, &wps->cred)) {
941                 wpabuf_free(cred);
942                 return -1;
943         }
944
945         wpabuf_put_be16(msg, ATTR_CRED);
946         wpabuf_put_be16(msg, wpabuf_len(cred));
947         wpabuf_put_buf(msg, cred);
948         wpabuf_free(cred);
949
950         return 0;
951 }
952
953
954 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
955 {
956         wpa_printf(MSG_DEBUG, "WPS:  * AP Settings");
957
958         if (wps_build_credential(msg, &wps->cred))
959                 return -1;
960
961         return 0;
962 }
963
964
965 static struct wpabuf * wps_build_m2(struct wps_data *wps)
966 {
967         struct wpabuf *msg;
968
969         if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
970                 return NULL;
971         wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
972                     wps->nonce_r, WPS_NONCE_LEN);
973         os_memcpy(wps->uuid_r, wps->wps->uuid, WPS_UUID_LEN);
974         wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
975
976         wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
977         msg = wpabuf_alloc(1000);
978         if (msg == NULL)
979                 return NULL;
980
981         if (wps_build_version(msg) ||
982             wps_build_msg_type(msg, WPS_M2) ||
983             wps_build_enrollee_nonce(wps, msg) ||
984             wps_build_registrar_nonce(wps, msg) ||
985             wps_build_uuid_r(wps, msg) ||
986             wps_build_public_key(wps, msg) ||
987             wps_derive_keys(wps) ||
988             wps_build_auth_type_flags(wps, msg) ||
989             wps_build_encr_type_flags(wps, msg) ||
990             wps_build_conn_type_flags(wps, msg) ||
991             wps_build_config_methods(wps->registrar, msg) ||
992             wps_build_device_attrs(&wps->wps->dev, msg) ||
993             wps_build_rf_bands(wps->registrar, msg) ||
994             wps_build_assoc_state(wps, msg) ||
995             wps_build_config_error(wps, msg) ||
996             wps_build_dev_password_id(wps, msg) ||
997             wps_build_os_version(&wps->wps->dev, msg) ||
998             wps_build_authenticator(wps, msg)) {
999                 wpabuf_free(msg);
1000                 return NULL;
1001         }
1002
1003         wps->state = RECV_M3;
1004         return msg;
1005 }
1006
1007
1008 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1009 {
1010         struct wpabuf *msg;
1011
1012         wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1013         msg = wpabuf_alloc(1000);
1014         if (msg == NULL)
1015                 return NULL;
1016
1017         if (wps_build_version(msg) ||
1018             wps_build_msg_type(msg, WPS_M2D) ||
1019             wps_build_enrollee_nonce(wps, msg) ||
1020             wps_build_registrar_nonce(wps, msg) ||
1021             wps_build_uuid_r(wps, msg) ||
1022             wps_build_auth_type_flags(wps, msg) ||
1023             wps_build_encr_type_flags(wps, msg) ||
1024             wps_build_conn_type_flags(wps, msg) ||
1025             wps_build_config_methods(wps->registrar, msg) ||
1026             wps_build_device_attrs(&wps->wps->dev, msg) ||
1027             wps_build_rf_bands(wps->registrar, msg) ||
1028             wps_build_assoc_state(wps, msg) ||
1029             wps_build_config_error(wps, msg) ||
1030             wps_build_os_version(&wps->wps->dev, msg)) {
1031                 wpabuf_free(msg);
1032                 return NULL;
1033         }
1034
1035         wps->state = RECV_M2D_ACK;
1036         return msg;
1037 }
1038
1039
1040 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1041 {
1042         struct wpabuf *msg, *plain;
1043
1044         wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1045
1046         wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1047
1048         plain = wpabuf_alloc(200);
1049         if (plain == NULL)
1050                 return NULL;
1051
1052         msg = wpabuf_alloc(1000);
1053         if (msg == NULL) {
1054                 wpabuf_free(plain);
1055                 return NULL;
1056         }
1057
1058         if (wps_build_version(msg) ||
1059             wps_build_msg_type(msg, WPS_M4) ||
1060             wps_build_enrollee_nonce(wps, msg) ||
1061             wps_build_r_hash(wps, msg) ||
1062             wps_build_r_snonce1(wps, plain) ||
1063             wps_build_key_wrap_auth(wps, plain) ||
1064             wps_build_encr_settings(wps, msg, plain) ||
1065             wps_build_authenticator(wps, msg)) {
1066                 wpabuf_free(plain);
1067                 wpabuf_free(msg);
1068                 return NULL;
1069         }
1070         wpabuf_free(plain);
1071
1072         wps->state = RECV_M5;
1073         return msg;
1074 }
1075
1076
1077 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1078 {
1079         struct wpabuf *msg, *plain;
1080
1081         wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1082
1083         plain = wpabuf_alloc(200);
1084         if (plain == NULL)
1085                 return NULL;
1086
1087         msg = wpabuf_alloc(1000);
1088         if (msg == NULL) {
1089                 wpabuf_free(plain);
1090                 return NULL;
1091         }
1092
1093         if (wps_build_version(msg) ||
1094             wps_build_msg_type(msg, WPS_M6) ||
1095             wps_build_enrollee_nonce(wps, msg) ||
1096             wps_build_r_snonce2(wps, plain) ||
1097             wps_build_key_wrap_auth(wps, plain) ||
1098             wps_build_encr_settings(wps, msg, plain) ||
1099             wps_build_authenticator(wps, msg)) {
1100                 wpabuf_free(plain);
1101                 wpabuf_free(msg);
1102                 return NULL;
1103         }
1104         wpabuf_free(plain);
1105
1106         wps->wps_pin_revealed = 1;
1107         wps->state = RECV_M7;
1108         return msg;
1109 }
1110
1111
1112 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1113 {
1114         struct wpabuf *msg, *plain;
1115
1116         wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1117
1118         plain = wpabuf_alloc(500);
1119         if (plain == NULL)
1120                 return NULL;
1121
1122         msg = wpabuf_alloc(1000);
1123         if (msg == NULL) {
1124                 wpabuf_free(plain);
1125                 return NULL;
1126         }
1127
1128         if (wps_build_version(msg) ||
1129             wps_build_msg_type(msg, WPS_M8) ||
1130             wps_build_enrollee_nonce(wps, msg) ||
1131             (wps->wps->ap && wps_build_cred(wps, plain)) ||
1132             (!wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
1133             wps_build_key_wrap_auth(wps, plain) ||
1134             wps_build_encr_settings(wps, msg, plain) ||
1135             wps_build_authenticator(wps, msg)) {
1136                 wpabuf_free(plain);
1137                 wpabuf_free(msg);
1138                 return NULL;
1139         }
1140         wpabuf_free(plain);
1141
1142         wps->state = RECV_DONE;
1143         return msg;
1144 }
1145
1146
1147 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
1148 {
1149         struct wpabuf *msg;
1150
1151         wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
1152
1153         msg = wpabuf_alloc(1000);
1154         if (msg == NULL)
1155                 return NULL;
1156
1157         if (wps_build_version(msg) ||
1158             wps_build_msg_type(msg, WPS_WSC_ACK) ||
1159             wps_build_enrollee_nonce(wps, msg) ||
1160             wps_build_registrar_nonce(wps, msg)) {
1161                 wpabuf_free(msg);
1162                 return NULL;
1163         }
1164
1165         return msg;
1166 }
1167
1168
1169 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps, u8 *op_code)
1170 {
1171         struct wpabuf *msg;
1172
1173         switch (wps->state) {
1174         case SEND_M2:
1175                 if (wps_get_dev_password(wps) < 0)
1176                         msg = wps_build_m2d(wps);
1177                 else
1178                         msg = wps_build_m2(wps);
1179                 *op_code = WSC_MSG;
1180                 break;
1181         case SEND_M2D:
1182                 msg = wps_build_m2d(wps);
1183                 *op_code = WSC_MSG;
1184                 break;
1185         case SEND_M4:
1186                 msg = wps_build_m4(wps);
1187                 *op_code = WSC_MSG;
1188                 break;
1189         case SEND_M6:
1190                 msg = wps_build_m6(wps);
1191                 *op_code = WSC_MSG;
1192                 break;
1193         case SEND_M8:
1194                 msg = wps_build_m8(wps);
1195                 *op_code = WSC_MSG;
1196                 break;
1197         case RECV_DONE:
1198                 msg = wps_build_wsc_ack(wps);
1199                 *op_code = WSC_ACK;
1200                 break;
1201         default:
1202                 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
1203                            "a message", wps->state);
1204                 msg = NULL;
1205                 break;
1206         }
1207
1208         if (*op_code == WSC_MSG && msg) {
1209                 /* Save a copy of the last message for Authenticator derivation
1210                  */
1211                 wpabuf_free(wps->last_msg);
1212                 wps->last_msg = wpabuf_dup(msg);
1213         }
1214
1215         return msg;
1216 }
1217
1218
1219 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
1220 {
1221         if (e_nonce == NULL) {
1222                 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
1223                 return -1;
1224         }
1225
1226         os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
1227         wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
1228                     wps->nonce_e, WPS_NONCE_LEN);
1229
1230         return 0;
1231 }
1232
1233
1234 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
1235 {
1236         if (r_nonce == NULL) {
1237                 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
1238                 return -1;
1239         }
1240
1241         if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
1242                 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
1243                 return -1;
1244         }
1245
1246         return 0;
1247 }
1248
1249
1250 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
1251 {
1252         if (uuid_e == NULL) {
1253                 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
1254                 return -1;
1255         }
1256
1257         os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
1258         wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
1259
1260         return 0;
1261 }
1262
1263
1264 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
1265 {
1266         if (pw_id == NULL) {
1267                 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
1268                 return -1;
1269         }
1270
1271         wps->dev_pw_id = WPA_GET_BE16(pw_id);
1272         wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
1273
1274         return 0;
1275 }
1276
1277
1278 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
1279 {
1280         if (e_hash1 == NULL) {
1281                 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
1282                 return -1;
1283         }
1284
1285         os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
1286         wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
1287
1288         return 0;
1289 }
1290
1291
1292 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
1293 {
1294         if (e_hash2 == NULL) {
1295                 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
1296                 return -1;
1297         }
1298
1299         os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
1300         wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
1301
1302         return 0;
1303 }
1304
1305
1306 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
1307 {
1308         u8 hash[SHA256_MAC_LEN];
1309         const u8 *addr[4];
1310         size_t len[4];
1311
1312         if (e_snonce1 == NULL) {
1313                 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
1314                 return -1;
1315         }
1316
1317         wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
1318                         WPS_SECRET_NONCE_LEN);
1319
1320         /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1321         addr[0] = e_snonce1;
1322         len[0] = WPS_SECRET_NONCE_LEN;
1323         addr[1] = wps->psk1;
1324         len[1] = WPS_PSK_LEN;
1325         addr[2] = wpabuf_head(wps->dh_pubkey_e);
1326         len[2] = wpabuf_len(wps->dh_pubkey_e);
1327         addr[3] = wpabuf_head(wps->dh_pubkey_r);
1328         len[3] = wpabuf_len(wps->dh_pubkey_r);
1329         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1330
1331         if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
1332                 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
1333                            "not match with the pre-committed value");
1334                 return -1;
1335         }
1336
1337         wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
1338                    "half of the device password");
1339
1340         return 0;
1341 }
1342
1343
1344 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
1345 {
1346         u8 hash[SHA256_MAC_LEN];
1347         const u8 *addr[4];
1348         size_t len[4];
1349
1350         if (e_snonce2 == NULL) {
1351                 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
1352                 return -1;
1353         }
1354
1355         wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
1356                         WPS_SECRET_NONCE_LEN);
1357
1358         /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
1359         addr[0] = e_snonce2;
1360         len[0] = WPS_SECRET_NONCE_LEN;
1361         addr[1] = wps->psk2;
1362         len[1] = WPS_PSK_LEN;
1363         addr[2] = wpabuf_head(wps->dh_pubkey_e);
1364         len[2] = wpabuf_len(wps->dh_pubkey_e);
1365         addr[3] = wpabuf_head(wps->dh_pubkey_r);
1366         len[3] = wpabuf_len(wps->dh_pubkey_r);
1367         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1368
1369         if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
1370                 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
1371                            "not match with the pre-committed value");
1372                 wps_registrar_invalidate_pin(wps->registrar, wps->uuid_e);
1373                 return -1;
1374         }
1375
1376         wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
1377                    "half of the device password");
1378         wps->wps_pin_revealed = 0;
1379         wps_registrar_unlock_pin(wps->registrar, wps->uuid_e);
1380
1381         return 0;
1382 }
1383
1384
1385 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
1386 {
1387         if (mac_addr == NULL) {
1388                 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
1389                 return -1;
1390         }
1391
1392         wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
1393                    MAC2STR(mac_addr));
1394         os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
1395         os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
1396
1397         return 0;
1398 }
1399
1400
1401 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
1402                               size_t pk_len)
1403 {
1404         if (pk == NULL || pk_len == 0) {
1405                 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
1406                 return -1;
1407         }
1408
1409         wpabuf_free(wps->dh_pubkey_e);
1410         wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
1411         if (wps->dh_pubkey_e == NULL)
1412                 return -1;
1413
1414         return 0;
1415 }
1416
1417
1418 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
1419 {
1420         u16 auth_types;
1421
1422         if (auth == NULL) {
1423                 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
1424                            "received");
1425                 return -1;
1426         }
1427
1428         auth_types = WPA_GET_BE16(auth);
1429
1430         wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
1431                    auth_types);
1432         wps->auth_type = wps->wps->auth_types & auth_types;
1433         if (wps->auth_type == 0) {
1434                 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1435                            "authentication types");
1436                 return -1;
1437         }
1438
1439         return 0;
1440 }
1441
1442
1443 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
1444 {
1445         u16 encr_types;
1446
1447         if (encr == NULL) {
1448                 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
1449                            "received");
1450                 return -1;
1451         }
1452
1453         encr_types = WPA_GET_BE16(encr);
1454
1455         wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
1456                    encr_types);
1457         wps->encr_type = wps->wps->encr_types & encr_types;
1458         if (wps->encr_type == 0) {
1459                 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1460                            "encryption types");
1461                 return -1;
1462         }
1463
1464         return 0;
1465 }
1466
1467
1468 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
1469 {
1470         if (conn == NULL) {
1471                 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
1472                            "received");
1473                 return -1;
1474         }
1475
1476         wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
1477                    *conn);
1478
1479         return 0;
1480 }
1481
1482
1483 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
1484 {
1485         u16 m;
1486
1487         if (methods == NULL) {
1488                 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
1489                 return -1;
1490         }
1491
1492         m = WPA_GET_BE16(methods);
1493
1494         wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x", m);
1495
1496         return 0;
1497 }
1498
1499
1500 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
1501 {
1502         if (state == NULL) {
1503                 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
1504                            "received");
1505                 return -1;
1506         }
1507
1508         wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
1509                    *state);
1510
1511         return 0;
1512 }
1513
1514
1515 static int wps_process_rf_bands(struct wps_data *wps, const u8 *bands)
1516 {
1517         if (bands == NULL) {
1518                 wpa_printf(MSG_DEBUG, "WPS: No RF Bands received");
1519                 return -1;
1520         }
1521
1522         wpa_printf(MSG_DEBUG, "WPS: Enrollee RF Bands 0x%x", *bands);
1523
1524         return 0;
1525 }
1526
1527
1528 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
1529 {
1530         u16 a;
1531
1532         if (assoc == NULL) {
1533                 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
1534                 return -1;
1535         }
1536
1537         a = WPA_GET_BE16(assoc);
1538         wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
1539
1540         return 0;
1541 }
1542
1543
1544 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
1545 {
1546         u16 e;
1547
1548         if (err == NULL) {
1549                 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
1550                 return -1;
1551         }
1552
1553         e = WPA_GET_BE16(err);
1554         wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
1555
1556         return 0;
1557 }
1558
1559
1560 static enum wps_process_res wps_process_m1(struct wps_data *wps,
1561                                            struct wps_parse_attr *attr)
1562 {
1563         wpa_printf(MSG_DEBUG, "WPS: Received M1");
1564
1565         if (wps->state != RECV_M1) {
1566                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1567                            "receiving M1", wps->state);
1568                 return WPS_FAILURE;
1569         }
1570
1571         if (wps_process_uuid_e(wps, attr->uuid_e) ||
1572             wps_process_mac_addr(wps, attr->mac_addr) ||
1573             wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1574             wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
1575             wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
1576             wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
1577             wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
1578             wps_process_config_methods(wps, attr->config_methods) ||
1579             wps_process_wps_state(wps, attr->wps_state) ||
1580             wps_process_device_attrs(&wps->peer_dev, attr) ||
1581             wps_process_rf_bands(wps, attr->rf_bands) ||
1582             wps_process_assoc_state(wps, attr->assoc_state) ||
1583             wps_process_dev_password_id(wps, attr->dev_password_id) ||
1584             wps_process_config_error(wps, attr->config_error) ||
1585             wps_process_os_version(&wps->peer_dev, attr->os_version))
1586                 return WPS_FAILURE;
1587
1588         if (wps->dev_pw_id != DEV_PW_DEFAULT &&
1589             wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
1590             wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
1591             wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
1592             (wps->dev_pw_id != DEV_PW_PUSHBUTTON || !wps->registrar->pbc)) {
1593                 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
1594                            wps->dev_pw_id);
1595                 wps->state = SEND_M2D;
1596                 return WPS_CONTINUE;
1597         }
1598
1599         if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
1600                 if (wps_registrar_pbc_overlap(wps->registrar, wps->mac_addr_e,
1601                                               wps->uuid_e)) {
1602                         wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
1603                                    "negotiation");
1604                         wps->state = SEND_M2D;
1605                         return WPS_CONTINUE;
1606                 }
1607                 wps_registrar_add_pbc_session(wps->registrar, wps->mac_addr_e,
1608                                               wps->uuid_e);
1609                 wps->pbc = 1;
1610         }
1611
1612         wps->state = SEND_M2;
1613         return WPS_CONTINUE;
1614 }
1615
1616
1617 static enum wps_process_res wps_process_m3(struct wps_data *wps,
1618                                            const struct wpabuf *msg,
1619                                            struct wps_parse_attr *attr)
1620 {
1621         wpa_printf(MSG_DEBUG, "WPS: Received M3");
1622
1623         if (wps->state != RECV_M3) {
1624                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1625                            "receiving M3", wps->state);
1626                 return WPS_FAILURE;
1627         }
1628
1629         if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1630             wps_process_authenticator(wps, attr->authenticator, msg) ||
1631             wps_process_e_hash1(wps, attr->e_hash1) ||
1632             wps_process_e_hash2(wps, attr->e_hash2))
1633                 return WPS_FAILURE;
1634
1635         wps->state = SEND_M4;
1636         return WPS_CONTINUE;
1637 }
1638
1639
1640 static enum wps_process_res wps_process_m5(struct wps_data *wps,
1641                                            const struct wpabuf *msg,
1642                                            struct wps_parse_attr *attr)
1643 {
1644         struct wpabuf *decrypted;
1645         struct wps_parse_attr eattr;
1646
1647         wpa_printf(MSG_DEBUG, "WPS: Received M5");
1648
1649         if (wps->state != RECV_M5) {
1650                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1651                            "receiving M5", wps->state);
1652                 return WPS_FAILURE;
1653         }
1654
1655         if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1656             wps_process_authenticator(wps, attr->authenticator, msg))
1657                 return WPS_FAILURE;
1658
1659         decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1660                                               attr->encr_settings_len);
1661         if (decrypted == NULL) {
1662                 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1663                            "Settings attribute");
1664                 return WPS_FAILURE;
1665         }
1666
1667         wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1668                    "attribute");
1669         if (wps_parse_msg(decrypted, &eattr) < 0 ||
1670             wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1671             wps_process_e_snonce1(wps, eattr.e_snonce1)) {
1672                 wpabuf_free(decrypted);
1673                 return WPS_FAILURE;
1674         }
1675         wpabuf_free(decrypted);
1676
1677         wps->state = SEND_M6;
1678         return WPS_CONTINUE;
1679 }
1680
1681
1682 static int wps_process_ap_settings_r(struct wps_data *wps,
1683                                      struct wps_parse_attr *attr)
1684 {
1685         if (wps->wps->ap)
1686                 return 0;
1687
1688         /* AP Settings Attributes in M7 when Enrollee is an AP */
1689         if (wps_process_ap_settings(attr, &wps->cred) < 0)
1690                 return -1;
1691
1692         wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
1693
1694         /*
1695          * TODO: Provide access to AP settings and allow changes before sending
1696          * out M8. For now, just copy the settings unchanged into M8.
1697          */
1698
1699         return 0;
1700 }
1701
1702
1703 static enum wps_process_res wps_process_m7(struct wps_data *wps,
1704                                            const struct wpabuf *msg,
1705                                            struct wps_parse_attr *attr)
1706 {
1707         struct wpabuf *decrypted;
1708         struct wps_parse_attr eattr;
1709
1710         wpa_printf(MSG_DEBUG, "WPS: Received M7");
1711
1712         if (wps->state != RECV_M7) {
1713                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1714                            "receiving M7", wps->state);
1715                 return WPS_FAILURE;
1716         }
1717
1718         if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1719             wps_process_authenticator(wps, attr->authenticator, msg))
1720                 return WPS_FAILURE;
1721
1722         decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1723                                               attr->encr_settings_len);
1724         if (decrypted == NULL) {
1725                 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1726                            "Settings attribute");
1727                 return WPS_FAILURE;
1728         }
1729
1730         wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1731                    "attribute");
1732         if (wps_parse_msg(decrypted, &eattr) < 0 ||
1733             wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1734             wps_process_e_snonce2(wps, eattr.e_snonce2) ||
1735             wps_process_ap_settings_r(wps, &eattr)) {
1736                 wpabuf_free(decrypted);
1737                 return WPS_FAILURE;
1738         }
1739
1740         wpabuf_free(decrypted);
1741
1742         wps->state = SEND_M8;
1743         return WPS_CONTINUE;
1744 }
1745
1746
1747 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
1748                                                 const struct wpabuf *msg)
1749 {
1750         struct wps_parse_attr attr;
1751         enum wps_process_res ret = WPS_CONTINUE;
1752
1753         wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
1754
1755         if (wps_parse_msg(msg, &attr) < 0)
1756                 return WPS_FAILURE;
1757
1758         if (attr.version == NULL || *attr.version != WPS_VERSION) {
1759                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1760                            attr.version ? *attr.version : 0);
1761                 return WPS_FAILURE;
1762         }
1763
1764         if (attr.msg_type == NULL) {
1765                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1766                 return WPS_FAILURE;
1767         }
1768
1769         if (*attr.msg_type != WPS_M1 &&
1770             (attr.registrar_nonce == NULL ||
1771              os_memcmp(wps->nonce_r, attr.registrar_nonce,
1772                        WPS_NONCE_LEN != 0))) {
1773                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1774                 return WPS_FAILURE;
1775         }
1776
1777         switch (*attr.msg_type) {
1778         case WPS_M1:
1779                 ret = wps_process_m1(wps, &attr);
1780                 break;
1781         case WPS_M3:
1782                 ret = wps_process_m3(wps, msg, &attr);
1783                 break;
1784         case WPS_M5:
1785                 ret = wps_process_m5(wps, msg, &attr);
1786                 break;
1787         case WPS_M7:
1788                 ret = wps_process_m7(wps, msg, &attr);
1789                 break;
1790         default:
1791                 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
1792                            *attr.msg_type);
1793                 return WPS_FAILURE;
1794         }
1795
1796         if (ret == WPS_CONTINUE) {
1797                 /* Save a copy of the last message for Authenticator derivation
1798                  */
1799                 wpabuf_free(wps->last_msg);
1800                 wps->last_msg = wpabuf_dup(msg);
1801         }
1802
1803         return ret;
1804 }
1805
1806
1807 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
1808                                                 const struct wpabuf *msg)
1809 {
1810         struct wps_parse_attr attr;
1811
1812         wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
1813
1814         if (wps_parse_msg(msg, &attr) < 0)
1815                 return WPS_FAILURE;
1816
1817         if (attr.version == NULL || *attr.version != WPS_VERSION) {
1818                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1819                            attr.version ? *attr.version : 0);
1820                 return WPS_FAILURE;
1821         }
1822
1823         if (attr.msg_type == NULL) {
1824                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1825                 return WPS_FAILURE;
1826         }
1827
1828         if (*attr.msg_type != WPS_WSC_ACK) {
1829                 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1830                            *attr.msg_type);
1831                 return WPS_FAILURE;
1832         }
1833
1834         if (attr.registrar_nonce == NULL ||
1835             os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1836         {
1837                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1838                 return WPS_FAILURE;
1839         }
1840
1841         if (attr.enrollee_nonce == NULL ||
1842             os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1843                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1844                 return WPS_FAILURE;
1845         }
1846
1847         if (wps->state == RECV_M2D_ACK) {
1848                 /* TODO: support for multiple registrars and sending of
1849                  * multiple M2/M2D messages */
1850
1851                 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
1852                            "terminate negotiation");
1853         }
1854
1855         return WPS_FAILURE;
1856 }
1857
1858
1859 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1860                                                  const struct wpabuf *msg)
1861 {
1862         struct wps_parse_attr attr;
1863
1864         wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1865
1866         if (wps_parse_msg(msg, &attr) < 0)
1867                 return WPS_FAILURE;
1868
1869         if (attr.version == NULL || *attr.version != WPS_VERSION) {
1870                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1871                            attr.version ? *attr.version : 0);
1872                 return WPS_FAILURE;
1873         }
1874
1875         if (attr.msg_type == NULL) {
1876                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1877                 return WPS_FAILURE;
1878         }
1879
1880         if (*attr.msg_type != WPS_WSC_NACK) {
1881                 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1882                            *attr.msg_type);
1883                 return WPS_FAILURE;
1884         }
1885
1886         if (attr.registrar_nonce == NULL ||
1887             os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1888         {
1889                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1890                 return WPS_FAILURE;
1891         }
1892
1893         if (attr.enrollee_nonce == NULL ||
1894             os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1895                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1896                 return WPS_FAILURE;
1897         }
1898
1899         if (attr.config_error == NULL) {
1900                 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1901                            "in WSC_NACK");
1902                 return WPS_FAILURE;
1903         }
1904
1905         wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
1906                    "Configuration Error %d", WPA_GET_BE16(attr.config_error));
1907
1908         return WPS_FAILURE;
1909 }
1910
1911
1912 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
1913                                                  const struct wpabuf *msg)
1914 {
1915         struct wps_parse_attr attr;
1916
1917         wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
1918
1919         if (wps->state != RECV_DONE) {
1920                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1921                            "receiving WSC_Done", wps->state);
1922                 return WPS_FAILURE;
1923         }
1924
1925         if (wps_parse_msg(msg, &attr) < 0)
1926                 return WPS_FAILURE;
1927
1928         if (attr.version == NULL || *attr.version != WPS_VERSION) {
1929                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1930                            attr.version ? *attr.version : 0);
1931                 return WPS_FAILURE;
1932         }
1933
1934         if (attr.msg_type == NULL) {
1935                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1936                 return WPS_FAILURE;
1937         }
1938
1939         if (*attr.msg_type != WPS_WSC_DONE) {
1940                 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1941                            *attr.msg_type);
1942                 return WPS_FAILURE;
1943         }
1944
1945         if (attr.registrar_nonce == NULL ||
1946             os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1947         {
1948                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1949                 return WPS_FAILURE;
1950         }
1951
1952         if (attr.enrollee_nonce == NULL ||
1953             os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1954                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1955                 return WPS_FAILURE;
1956         }
1957
1958         wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
1959
1960         if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
1961             wps->wps->ap) {
1962                 struct wps_credential cred;
1963
1964                 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
1965                            "on first Enrollee connection");
1966
1967                 os_memset(&cred, 0, sizeof(cred));
1968                 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
1969                 cred.ssid_len = wps->wps->ssid_len;
1970                 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
1971                 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
1972                 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
1973                 cred.key_len = wps->new_psk_len;
1974
1975                 wps->wps->wps_state = WPS_STATE_CONFIGURED;
1976                 wpa_hexdump_ascii_key(MSG_DEBUG,
1977                                       "WPS: Generated random passphrase",
1978                                       wps->new_psk, wps->new_psk_len);
1979                 if (wps->wps->cred_cb)
1980                         wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
1981
1982                 os_free(wps->new_psk);
1983                 wps->new_psk = NULL;
1984         }
1985
1986         if (wps->new_psk) {
1987                 if (wps_cb_new_psk(wps->registrar, wps->mac_addr_e,
1988                                    wps->new_psk, wps->new_psk_len)) {
1989                         wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
1990                                    "new PSK");
1991                 }
1992                 os_free(wps->new_psk);
1993                 wps->new_psk = NULL;
1994         }
1995
1996         if (wps->pbc) {
1997                 wps_registrar_remove_pbc_session(wps->registrar,
1998                                                  wps->mac_addr_e, wps->uuid_e);
1999                 wps_registrar_pbc_completed(wps->registrar);
2000         }
2001
2002         return WPS_DONE;
2003 }
2004
2005
2006 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
2007                                                u8 op_code,
2008                                                const struct wpabuf *msg)
2009 {
2010
2011         wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
2012                    "op_code=%d)",
2013                    (unsigned long) wpabuf_len(msg), op_code);
2014
2015         switch (op_code) {
2016         case WSC_MSG:
2017                 return wps_process_wsc_msg(wps, msg);
2018         case WSC_ACK:
2019                 return wps_process_wsc_ack(wps, msg);
2020         case WSC_NACK:
2021                 return wps_process_wsc_nack(wps, msg);
2022         case WSC_Done:
2023                 return wps_process_wsc_done(wps, msg);
2024         default:
2025                 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
2026                 return WPS_FAILURE;
2027         }
2028 }