Remove src/crypto from default include path
[libeap.git] / src / wps / wps_enrollee.c
1 /*
2  * Wi-Fi Protected Setup - Enrollee
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 "crypto/crypto.h"
19 #include "crypto/sha256.h"
20 #include "wps_i.h"
21 #include "wps_dev_attr.h"
22
23
24 static int wps_build_mac_addr(struct wps_data *wps, struct wpabuf *msg)
25 {
26         wpa_printf(MSG_DEBUG, "WPS:  * MAC Address");
27         wpabuf_put_be16(msg, ATTR_MAC_ADDR);
28         wpabuf_put_be16(msg, ETH_ALEN);
29         wpabuf_put_data(msg, wps->mac_addr_e, ETH_ALEN);
30         return 0;
31 }
32
33
34 static int wps_build_wps_state(struct wps_data *wps, struct wpabuf *msg)
35 {
36         u8 state;
37         if (wps->wps->ap)
38                 state = wps->wps->wps_state;
39         else
40                 state = WPS_STATE_NOT_CONFIGURED;
41         wpa_printf(MSG_DEBUG, "WPS:  * Wi-Fi Protected Setup State (%d)",
42                    state);
43         wpabuf_put_be16(msg, ATTR_WPS_STATE);
44         wpabuf_put_be16(msg, 1);
45         wpabuf_put_u8(msg, state);
46         return 0;
47 }
48
49
50 static int wps_build_e_hash(struct wps_data *wps, struct wpabuf *msg)
51 {
52         u8 *hash;
53         const u8 *addr[4];
54         size_t len[4];
55
56         if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
57                 return -1;
58         wpa_hexdump(MSG_DEBUG, "WPS: E-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
59         wpa_hexdump(MSG_DEBUG, "WPS: E-S2",
60                     wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
61
62         if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
63                 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
64                            "E-Hash derivation");
65                 return -1;
66         }
67
68         wpa_printf(MSG_DEBUG, "WPS:  * E-Hash1");
69         wpabuf_put_be16(msg, ATTR_E_HASH1);
70         wpabuf_put_be16(msg, SHA256_MAC_LEN);
71         hash = wpabuf_put(msg, SHA256_MAC_LEN);
72         /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
73         addr[0] = wps->snonce;
74         len[0] = WPS_SECRET_NONCE_LEN;
75         addr[1] = wps->psk1;
76         len[1] = WPS_PSK_LEN;
77         addr[2] = wpabuf_head(wps->dh_pubkey_e);
78         len[2] = wpabuf_len(wps->dh_pubkey_e);
79         addr[3] = wpabuf_head(wps->dh_pubkey_r);
80         len[3] = wpabuf_len(wps->dh_pubkey_r);
81         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
82         wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", hash, SHA256_MAC_LEN);
83
84         wpa_printf(MSG_DEBUG, "WPS:  * E-Hash2");
85         wpabuf_put_be16(msg, ATTR_E_HASH2);
86         wpabuf_put_be16(msg, SHA256_MAC_LEN);
87         hash = wpabuf_put(msg, SHA256_MAC_LEN);
88         /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
89         addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
90         addr[1] = wps->psk2;
91         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
92         wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", hash, SHA256_MAC_LEN);
93
94         return 0;
95 }
96
97
98 static int wps_build_e_snonce1(struct wps_data *wps, struct wpabuf *msg)
99 {
100         wpa_printf(MSG_DEBUG, "WPS:  * E-SNonce1");
101         wpabuf_put_be16(msg, ATTR_E_SNONCE1);
102         wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
103         wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
104         return 0;
105 }
106
107
108 static int wps_build_e_snonce2(struct wps_data *wps, struct wpabuf *msg)
109 {
110         wpa_printf(MSG_DEBUG, "WPS:  * E-SNonce2");
111         wpabuf_put_be16(msg, ATTR_E_SNONCE2);
112         wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
113         wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
114                         WPS_SECRET_NONCE_LEN);
115         return 0;
116 }
117
118
119 static struct wpabuf * wps_build_m1(struct wps_data *wps)
120 {
121         struct wpabuf *msg;
122         u16 methods;
123
124         if (os_get_random(wps->nonce_e, WPS_NONCE_LEN) < 0)
125                 return NULL;
126         wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
127                     wps->nonce_e, WPS_NONCE_LEN);
128
129         wpa_printf(MSG_DEBUG, "WPS: Building Message M1");
130         msg = wpabuf_alloc(1000);
131         if (msg == NULL)
132                 return NULL;
133
134         methods = WPS_CONFIG_LABEL | WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
135 #ifdef CONFIG_WPS_UFD
136         methods |= WPS_CONFIG_USBA;
137 #endif /* CONFIG_WPS_UFD */
138 #ifdef CONFIG_WPS_NFC
139         methods |= WPS_CONFIG_NFC_INTERFACE;
140 #endif /* CONFIG_WPS_NFC */
141         if (wps->pbc)
142                 methods |= WPS_CONFIG_PUSHBUTTON;
143
144         if (wps_build_version(msg) ||
145             wps_build_msg_type(msg, WPS_M1) ||
146             wps_build_uuid_e(msg, wps->uuid_e) ||
147             wps_build_mac_addr(wps, msg) ||
148             wps_build_enrollee_nonce(wps, msg) ||
149             wps_build_public_key(wps, msg) ||
150             wps_build_auth_type_flags(wps, msg) ||
151             wps_build_encr_type_flags(wps, msg) ||
152             wps_build_conn_type_flags(wps, msg) ||
153             wps_build_config_methods(msg, methods) ||
154             wps_build_wps_state(wps, msg) ||
155             wps_build_device_attrs(&wps->wps->dev, msg) ||
156             wps_build_rf_bands(&wps->wps->dev, msg) ||
157             wps_build_assoc_state(wps, msg) ||
158             wps_build_dev_password_id(msg, wps->dev_pw_id) ||
159             wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
160             wps_build_os_version(&wps->wps->dev, msg)) {
161                 wpabuf_free(msg);
162                 return NULL;
163         }
164
165         wps->state = RECV_M2;
166         return msg;
167 }
168
169
170 static struct wpabuf * wps_build_m3(struct wps_data *wps)
171 {
172         struct wpabuf *msg;
173
174         wpa_printf(MSG_DEBUG, "WPS: Building Message M3");
175
176         if (wps->dev_password == NULL) {
177                 wpa_printf(MSG_DEBUG, "WPS: No Device Password available");
178                 return NULL;
179         }
180         wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
181
182         msg = wpabuf_alloc(1000);
183         if (msg == NULL)
184                 return NULL;
185
186         if (wps_build_version(msg) ||
187             wps_build_msg_type(msg, WPS_M3) ||
188             wps_build_registrar_nonce(wps, msg) ||
189             wps_build_e_hash(wps, msg) ||
190             wps_build_authenticator(wps, msg)) {
191                 wpabuf_free(msg);
192                 return NULL;
193         }
194
195         wps->state = RECV_M4;
196         return msg;
197 }
198
199
200 static struct wpabuf * wps_build_m5(struct wps_data *wps)
201 {
202         struct wpabuf *msg, *plain;
203
204         wpa_printf(MSG_DEBUG, "WPS: Building Message M5");
205
206         plain = wpabuf_alloc(200);
207         if (plain == NULL)
208                 return NULL;
209
210         msg = wpabuf_alloc(1000);
211         if (msg == NULL) {
212                 wpabuf_free(plain);
213                 return NULL;
214         }
215
216         if (wps_build_version(msg) ||
217             wps_build_msg_type(msg, WPS_M5) ||
218             wps_build_registrar_nonce(wps, msg) ||
219             wps_build_e_snonce1(wps, plain) ||
220             wps_build_key_wrap_auth(wps, plain) ||
221             wps_build_encr_settings(wps, msg, plain) ||
222             wps_build_authenticator(wps, msg)) {
223                 wpabuf_free(plain);
224                 wpabuf_free(msg);
225                 return NULL;
226         }
227         wpabuf_free(plain);
228
229         wps->state = RECV_M6;
230         return msg;
231 }
232
233
234 static int wps_build_cred_ssid(struct wps_data *wps, struct wpabuf *msg)
235 {
236         wpa_printf(MSG_DEBUG, "WPS:  * SSID");
237         wpabuf_put_be16(msg, ATTR_SSID);
238         wpabuf_put_be16(msg, wps->wps->ssid_len);
239         wpabuf_put_data(msg, wps->wps->ssid, wps->wps->ssid_len);
240         return 0;
241 }
242
243
244 static int wps_build_cred_auth_type(struct wps_data *wps, struct wpabuf *msg)
245 {
246         wpa_printf(MSG_DEBUG, "WPS:  * Authentication Type");
247         wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
248         wpabuf_put_be16(msg, 2);
249         wpabuf_put_be16(msg, wps->wps->auth_types);
250         return 0;
251 }
252
253
254 static int wps_build_cred_encr_type(struct wps_data *wps, struct wpabuf *msg)
255 {
256         wpa_printf(MSG_DEBUG, "WPS:  * Encryption Type");
257         wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
258         wpabuf_put_be16(msg, 2);
259         wpabuf_put_be16(msg, wps->wps->encr_types);
260         return 0;
261 }
262
263
264 static int wps_build_cred_network_key(struct wps_data *wps, struct wpabuf *msg)
265 {
266         wpa_printf(MSG_DEBUG, "WPS:  * Network Key");
267         wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
268         wpabuf_put_be16(msg, wps->wps->network_key_len);
269         wpabuf_put_data(msg, wps->wps->network_key, wps->wps->network_key_len);
270         return 0;
271 }
272
273
274 static int wps_build_cred_mac_addr(struct wps_data *wps, struct wpabuf *msg)
275 {
276         wpa_printf(MSG_DEBUG, "WPS:  * MAC Address (AP BSSID)");
277         wpabuf_put_be16(msg, ATTR_MAC_ADDR);
278         wpabuf_put_be16(msg, ETH_ALEN);
279         wpabuf_put_data(msg, wps->wps->dev.mac_addr, ETH_ALEN);
280         return 0;
281 }
282
283
284 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *plain)
285 {
286         if (wps->wps->ap_settings) {
287                 wpa_printf(MSG_DEBUG, "WPS:  * AP Settings (pre-configured)");
288                 wpabuf_put_data(plain, wps->wps->ap_settings,
289                                 wps->wps->ap_settings_len);
290                 return 0;
291         }
292
293         return wps_build_cred_ssid(wps, plain) ||
294                 wps_build_cred_mac_addr(wps, plain) ||
295                 wps_build_cred_auth_type(wps, plain) ||
296                 wps_build_cred_encr_type(wps, plain) ||
297                 wps_build_cred_network_key(wps, plain);
298 }
299
300
301 static struct wpabuf * wps_build_m7(struct wps_data *wps)
302 {
303         struct wpabuf *msg, *plain;
304
305         wpa_printf(MSG_DEBUG, "WPS: Building Message M7");
306
307         plain = wpabuf_alloc(500 + wps->wps->ap_settings_len);
308         if (plain == NULL)
309                 return NULL;
310
311         msg = wpabuf_alloc(1000 + wps->wps->ap_settings_len);
312         if (msg == NULL) {
313                 wpabuf_free(plain);
314                 return NULL;
315         }
316
317         if (wps_build_version(msg) ||
318             wps_build_msg_type(msg, WPS_M7) ||
319             wps_build_registrar_nonce(wps, msg) ||
320             wps_build_e_snonce2(wps, plain) ||
321             (wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
322             wps_build_key_wrap_auth(wps, plain) ||
323             wps_build_encr_settings(wps, msg, plain) ||
324             wps_build_authenticator(wps, msg)) {
325                 wpabuf_free(plain);
326                 wpabuf_free(msg);
327                 return NULL;
328         }
329         wpabuf_free(plain);
330
331         if (wps->wps->ap && wps->wps->registrar) {
332                 /*
333                  * If the Registrar is only learning our current configuration,
334                  * it may not continue protocol run to successful completion.
335                  * Store information here to make sure it remains available.
336                  */
337                 wps_device_store(wps->wps->registrar, &wps->peer_dev,
338                                  wps->uuid_r);
339         }
340
341         wps->state = RECV_M8;
342         return msg;
343 }
344
345
346 static struct wpabuf * wps_build_wsc_done(struct wps_data *wps)
347 {
348         struct wpabuf *msg;
349
350         wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_Done");
351
352         msg = wpabuf_alloc(1000);
353         if (msg == NULL)
354                 return NULL;
355
356         if (wps_build_version(msg) ||
357             wps_build_msg_type(msg, WPS_WSC_DONE) ||
358             wps_build_enrollee_nonce(wps, msg) ||
359             wps_build_registrar_nonce(wps, msg)) {
360                 wpabuf_free(msg);
361                 return NULL;
362         }
363
364         if (wps->wps->ap)
365                 wps->state = RECV_ACK;
366         else {
367                 wps_success_event(wps->wps);
368                 wps->state = WPS_FINISHED;
369         }
370         return msg;
371 }
372
373
374 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
375 {
376         struct wpabuf *msg;
377
378         wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
379
380         msg = wpabuf_alloc(1000);
381         if (msg == NULL)
382                 return NULL;
383
384         if (wps_build_version(msg) ||
385             wps_build_msg_type(msg, WPS_WSC_ACK) ||
386             wps_build_enrollee_nonce(wps, msg) ||
387             wps_build_registrar_nonce(wps, msg)) {
388                 wpabuf_free(msg);
389                 return NULL;
390         }
391
392         return msg;
393 }
394
395
396 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
397 {
398         struct wpabuf *msg;
399
400         wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
401
402         msg = wpabuf_alloc(1000);
403         if (msg == NULL)
404                 return NULL;
405
406         if (wps_build_version(msg) ||
407             wps_build_msg_type(msg, WPS_WSC_NACK) ||
408             wps_build_enrollee_nonce(wps, msg) ||
409             wps_build_registrar_nonce(wps, msg) ||
410             wps_build_config_error(msg, wps->config_error)) {
411                 wpabuf_free(msg);
412                 return NULL;
413         }
414
415         return msg;
416 }
417
418
419 struct wpabuf * wps_enrollee_get_msg(struct wps_data *wps,
420                                      enum wsc_op_code *op_code)
421 {
422         struct wpabuf *msg;
423
424         switch (wps->state) {
425         case SEND_M1:
426                 msg = wps_build_m1(wps);
427                 *op_code = WSC_MSG;
428                 break;
429         case SEND_M3:
430                 msg = wps_build_m3(wps);
431                 *op_code = WSC_MSG;
432                 break;
433         case SEND_M5:
434                 msg = wps_build_m5(wps);
435                 *op_code = WSC_MSG;
436                 break;
437         case SEND_M7:
438                 msg = wps_build_m7(wps);
439                 *op_code = WSC_MSG;
440                 break;
441         case RECEIVED_M2D:
442                 if (wps->wps->ap) {
443                         msg = wps_build_wsc_nack(wps);
444                         *op_code = WSC_NACK;
445                         break;
446                 }
447                 msg = wps_build_wsc_ack(wps);
448                 *op_code = WSC_ACK;
449                 if (msg) {
450                         /* Another M2/M2D may be received */
451                         wps->state = RECV_M2;
452                 }
453                 break;
454         case SEND_WSC_NACK:
455                 msg = wps_build_wsc_nack(wps);
456                 *op_code = WSC_NACK;
457                 break;
458         case WPS_MSG_DONE:
459                 msg = wps_build_wsc_done(wps);
460                 *op_code = WSC_Done;
461                 break;
462         default:
463                 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
464                            "a message", wps->state);
465                 msg = NULL;
466                 break;
467         }
468
469         if (*op_code == WSC_MSG && msg) {
470                 /* Save a copy of the last message for Authenticator derivation
471                  */
472                 wpabuf_free(wps->last_msg);
473                 wps->last_msg = wpabuf_dup(msg);
474         }
475
476         return msg;
477 }
478
479
480 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
481 {
482         if (r_nonce == NULL) {
483                 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
484                 return -1;
485         }
486
487         os_memcpy(wps->nonce_r, r_nonce, WPS_NONCE_LEN);
488         wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
489                     wps->nonce_r, WPS_NONCE_LEN);
490
491         return 0;
492 }
493
494
495 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
496 {
497         if (e_nonce == NULL) {
498                 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
499                 return -1;
500         }
501
502         if (os_memcmp(wps->nonce_e, e_nonce, WPS_NONCE_LEN) != 0) {
503                 wpa_printf(MSG_DEBUG, "WPS: Invalid Enrollee Nonce received");
504                 return -1;
505         }
506
507         return 0;
508 }
509
510
511 static int wps_process_uuid_r(struct wps_data *wps, const u8 *uuid_r)
512 {
513         if (uuid_r == NULL) {
514                 wpa_printf(MSG_DEBUG, "WPS: No UUID-R received");
515                 return -1;
516         }
517
518         os_memcpy(wps->uuid_r, uuid_r, WPS_UUID_LEN);
519         wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
520
521         return 0;
522 }
523
524
525 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
526                               size_t pk_len)
527 {
528         if (pk == NULL || pk_len == 0) {
529                 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
530                 return -1;
531         }
532
533 #ifdef CONFIG_WPS_OOB
534         if (wps->dev_pw_id != DEV_PW_DEFAULT &&
535             wps->wps->oob_conf.pubkey_hash) {
536                 const u8 *addr[1];
537                 u8 hash[WPS_HASH_LEN];
538
539                 addr[0] = pk;
540                 sha256_vector(1, addr, &pk_len, hash);
541                 if (os_memcmp(hash,
542                               wpabuf_head(wps->wps->oob_conf.pubkey_hash),
543                               WPS_OOB_PUBKEY_HASH_LEN) != 0) {
544                         wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
545                         return -1;
546                 }
547         }
548 #endif /* CONFIG_WPS_OOB */
549
550         wpabuf_free(wps->dh_pubkey_r);
551         wps->dh_pubkey_r = wpabuf_alloc_copy(pk, pk_len);
552         if (wps->dh_pubkey_r == NULL)
553                 return -1;
554
555         if (wps_derive_keys(wps) < 0)
556                 return -1;
557
558         return 0;
559 }
560
561
562 static int wps_process_r_hash1(struct wps_data *wps, const u8 *r_hash1)
563 {
564         if (r_hash1 == NULL) {
565                 wpa_printf(MSG_DEBUG, "WPS: No R-Hash1 received");
566                 return -1;
567         }
568
569         os_memcpy(wps->peer_hash1, r_hash1, WPS_HASH_LEN);
570         wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", wps->peer_hash1, WPS_HASH_LEN);
571
572         return 0;
573 }
574
575
576 static int wps_process_r_hash2(struct wps_data *wps, const u8 *r_hash2)
577 {
578         if (r_hash2 == NULL) {
579                 wpa_printf(MSG_DEBUG, "WPS: No R-Hash2 received");
580                 return -1;
581         }
582
583         os_memcpy(wps->peer_hash2, r_hash2, WPS_HASH_LEN);
584         wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", wps->peer_hash2, WPS_HASH_LEN);
585
586         return 0;
587 }
588
589
590 static int wps_process_r_snonce1(struct wps_data *wps, const u8 *r_snonce1)
591 {
592         u8 hash[SHA256_MAC_LEN];
593         const u8 *addr[4];
594         size_t len[4];
595
596         if (r_snonce1 == NULL) {
597                 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce1 received");
598                 return -1;
599         }
600
601         wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce1", r_snonce1,
602                         WPS_SECRET_NONCE_LEN);
603
604         /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
605         addr[0] = r_snonce1;
606         len[0] = WPS_SECRET_NONCE_LEN;
607         addr[1] = wps->psk1;
608         len[1] = WPS_PSK_LEN;
609         addr[2] = wpabuf_head(wps->dh_pubkey_e);
610         len[2] = wpabuf_len(wps->dh_pubkey_e);
611         addr[3] = wpabuf_head(wps->dh_pubkey_r);
612         len[3] = wpabuf_len(wps->dh_pubkey_r);
613         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
614
615         if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
616                 wpa_printf(MSG_DEBUG, "WPS: R-Hash1 derived from R-S1 does "
617                            "not match with the pre-committed value");
618                 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
619                 wps_pwd_auth_fail_event(wps->wps, 1, 1);
620                 return -1;
621         }
622
623         wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the first "
624                    "half of the device password");
625
626         return 0;
627 }
628
629
630 static int wps_process_r_snonce2(struct wps_data *wps, const u8 *r_snonce2)
631 {
632         u8 hash[SHA256_MAC_LEN];
633         const u8 *addr[4];
634         size_t len[4];
635
636         if (r_snonce2 == NULL) {
637                 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce2 received");
638                 return -1;
639         }
640
641         wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce2", r_snonce2,
642                         WPS_SECRET_NONCE_LEN);
643
644         /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
645         addr[0] = r_snonce2;
646         len[0] = WPS_SECRET_NONCE_LEN;
647         addr[1] = wps->psk2;
648         len[1] = WPS_PSK_LEN;
649         addr[2] = wpabuf_head(wps->dh_pubkey_e);
650         len[2] = wpabuf_len(wps->dh_pubkey_e);
651         addr[3] = wpabuf_head(wps->dh_pubkey_r);
652         len[3] = wpabuf_len(wps->dh_pubkey_r);
653         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
654
655         if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
656                 wpa_printf(MSG_DEBUG, "WPS: R-Hash2 derived from R-S2 does "
657                            "not match with the pre-committed value");
658                 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
659                 wps_pwd_auth_fail_event(wps->wps, 1, 2);
660                 return -1;
661         }
662
663         wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the second "
664                    "half of the device password");
665
666         return 0;
667 }
668
669
670 static int wps_process_cred_e(struct wps_data *wps, const u8 *cred,
671                               size_t cred_len)
672 {
673         struct wps_parse_attr attr;
674         struct wpabuf msg;
675
676         wpa_printf(MSG_DEBUG, "WPS: Received Credential");
677         os_memset(&wps->cred, 0, sizeof(wps->cred));
678         wpabuf_set(&msg, cred, cred_len);
679         if (wps_parse_msg(&msg, &attr) < 0 ||
680             wps_process_cred(&attr, &wps->cred))
681                 return -1;
682
683         if (os_memcmp(wps->cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN) !=
684             0) {
685                 wpa_printf(MSG_DEBUG, "WPS: MAC Address in the Credential ("
686                            MACSTR ") does not match with own address (" MACSTR
687                            ")", MAC2STR(wps->cred.mac_addr),
688                            MAC2STR(wps->wps->dev.mac_addr));
689                 /*
690                  * In theory, this could be consider fatal error, but there are
691                  * number of deployed implementations using other address here
692                  * due to unclarity in the specification. For interoperability
693                  * reasons, allow this to be processed since we do not really
694                  * use the MAC Address information for anything.
695                  */
696         }
697
698         if (wps->wps->cred_cb) {
699                 wps->cred.cred_attr = cred - 4;
700                 wps->cred.cred_attr_len = cred_len + 4;
701                 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
702                 wps->cred.cred_attr = NULL;
703                 wps->cred.cred_attr_len = 0;
704         }
705
706         return 0;
707 }
708
709
710 static int wps_process_creds(struct wps_data *wps, const u8 *cred[],
711                              size_t cred_len[], size_t num_cred)
712 {
713         size_t i;
714
715         if (wps->wps->ap)
716                 return 0;
717
718         if (num_cred == 0) {
719                 wpa_printf(MSG_DEBUG, "WPS: No Credential attributes "
720                            "received");
721                 return -1;
722         }
723
724         for (i = 0; i < num_cred; i++) {
725                 if (wps_process_cred_e(wps, cred[i], cred_len[i]))
726                         return -1;
727         }
728
729         return 0;
730 }
731
732
733 static int wps_process_ap_settings_e(struct wps_data *wps,
734                                      struct wps_parse_attr *attr,
735                                      struct wpabuf *attrs)
736 {
737         struct wps_credential cred;
738
739         if (!wps->wps->ap)
740                 return 0;
741
742         if (wps_process_ap_settings(attr, &cred) < 0)
743                 return -1;
744
745         wpa_printf(MSG_INFO, "WPS: Received new AP configuration from "
746                    "Registrar");
747
748         if (os_memcmp(cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN) !=
749             0) {
750                 wpa_printf(MSG_DEBUG, "WPS: MAC Address in the AP Settings ("
751                            MACSTR ") does not match with own address (" MACSTR
752                            ")", MAC2STR(cred.mac_addr),
753                            MAC2STR(wps->wps->dev.mac_addr));
754                 /*
755                  * In theory, this could be consider fatal error, but there are
756                  * number of deployed implementations using other address here
757                  * due to unclarity in the specification. For interoperability
758                  * reasons, allow this to be processed since we do not really
759                  * use the MAC Address information for anything.
760                  */
761         }
762
763         if (wps->wps->cred_cb) {
764                 cred.cred_attr = wpabuf_head(attrs);
765                 cred.cred_attr_len = wpabuf_len(attrs);
766                 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
767         }
768
769         return 0;
770 }
771
772
773 static enum wps_process_res wps_process_m2(struct wps_data *wps,
774                                            const struct wpabuf *msg,
775                                            struct wps_parse_attr *attr)
776 {
777         wpa_printf(MSG_DEBUG, "WPS: Received M2");
778
779         if (wps->state != RECV_M2) {
780                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
781                            "receiving M2", wps->state);
782                 wps->state = SEND_WSC_NACK;
783                 return WPS_CONTINUE;
784         }
785
786         if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
787             wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
788             wps_process_uuid_r(wps, attr->uuid_r) ||
789             wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
790             wps_process_authenticator(wps, attr->authenticator, msg) ||
791             wps_process_device_attrs(&wps->peer_dev, attr)) {
792                 wps->state = SEND_WSC_NACK;
793                 return WPS_CONTINUE;
794         }
795
796         if (wps->wps->ap && wps->wps->ap_setup_locked) {
797                 wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse "
798                            "registration of a new Registrar");
799                 wps->config_error = WPS_CFG_SETUP_LOCKED;
800                 wps->state = SEND_WSC_NACK;
801                 return WPS_CONTINUE;
802         }
803
804         wps->state = SEND_M3;
805         return WPS_CONTINUE;
806 }
807
808
809 static enum wps_process_res wps_process_m2d(struct wps_data *wps,
810                                             struct wps_parse_attr *attr)
811 {
812         wpa_printf(MSG_DEBUG, "WPS: Received M2D");
813
814         if (wps->state != RECV_M2) {
815                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
816                            "receiving M2D", wps->state);
817                 wps->state = SEND_WSC_NACK;
818                 return WPS_CONTINUE;
819         }
820
821         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer",
822                           attr->manufacturer, attr->manufacturer_len);
823         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name",
824                           attr->model_name, attr->model_name_len);
825         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number",
826                           attr->model_number, attr->model_number_len);
827         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number",
828                           attr->serial_number, attr->serial_number_len);
829         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name",
830                           attr->dev_name, attr->dev_name_len);
831
832         if (wps->wps->event_cb) {
833                 union wps_event_data data;
834                 struct wps_event_m2d *m2d = &data.m2d;
835                 os_memset(&data, 0, sizeof(data));
836                 if (attr->config_methods)
837                         m2d->config_methods =
838                                 WPA_GET_BE16(attr->config_methods);
839                 m2d->manufacturer = attr->manufacturer;
840                 m2d->manufacturer_len = attr->manufacturer_len;
841                 m2d->model_name = attr->model_name;
842                 m2d->model_name_len = attr->model_name_len;
843                 m2d->model_number = attr->model_number;
844                 m2d->model_number_len = attr->model_number_len;
845                 m2d->serial_number = attr->serial_number;
846                 m2d->serial_number_len = attr->serial_number_len;
847                 m2d->dev_name = attr->dev_name;
848                 m2d->dev_name_len = attr->dev_name_len;
849                 m2d->primary_dev_type = attr->primary_dev_type;
850                 if (attr->config_error)
851                         m2d->config_error =
852                                 WPA_GET_BE16(attr->config_error);
853                 if (attr->dev_password_id)
854                         m2d->dev_password_id =
855                                 WPA_GET_BE16(attr->dev_password_id);
856                 wps->wps->event_cb(wps->wps->cb_ctx, WPS_EV_M2D, &data);
857         }
858
859         wps->state = RECEIVED_M2D;
860         return WPS_CONTINUE;
861 }
862
863
864 static enum wps_process_res wps_process_m4(struct wps_data *wps,
865                                            const struct wpabuf *msg,
866                                            struct wps_parse_attr *attr)
867 {
868         struct wpabuf *decrypted;
869         struct wps_parse_attr eattr;
870
871         wpa_printf(MSG_DEBUG, "WPS: Received M4");
872
873         if (wps->state != RECV_M4) {
874                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
875                            "receiving M4", wps->state);
876                 wps->state = SEND_WSC_NACK;
877                 return WPS_CONTINUE;
878         }
879
880         if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
881             wps_process_authenticator(wps, attr->authenticator, msg) ||
882             wps_process_r_hash1(wps, attr->r_hash1) ||
883             wps_process_r_hash2(wps, attr->r_hash2)) {
884                 wps->state = SEND_WSC_NACK;
885                 return WPS_CONTINUE;
886         }
887
888         decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
889                                               attr->encr_settings_len);
890         if (decrypted == NULL) {
891                 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
892                            "Settings attribute");
893                 wps->state = SEND_WSC_NACK;
894                 return WPS_CONTINUE;
895         }
896
897         wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
898                    "attribute");
899         if (wps_parse_msg(decrypted, &eattr) < 0 ||
900             wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
901             wps_process_r_snonce1(wps, eattr.r_snonce1)) {
902                 wpabuf_free(decrypted);
903                 wps->state = SEND_WSC_NACK;
904                 return WPS_CONTINUE;
905         }
906         wpabuf_free(decrypted);
907
908         wps->state = SEND_M5;
909         return WPS_CONTINUE;
910 }
911
912
913 static enum wps_process_res wps_process_m6(struct wps_data *wps,
914                                            const struct wpabuf *msg,
915                                            struct wps_parse_attr *attr)
916 {
917         struct wpabuf *decrypted;
918         struct wps_parse_attr eattr;
919
920         wpa_printf(MSG_DEBUG, "WPS: Received M6");
921
922         if (wps->state != RECV_M6) {
923                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
924                            "receiving M6", wps->state);
925                 wps->state = SEND_WSC_NACK;
926                 return WPS_CONTINUE;
927         }
928
929         if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
930             wps_process_authenticator(wps, attr->authenticator, msg)) {
931                 wps->state = SEND_WSC_NACK;
932                 return WPS_CONTINUE;
933         }
934
935         decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
936                                               attr->encr_settings_len);
937         if (decrypted == NULL) {
938                 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
939                            "Settings attribute");
940                 wps->state = SEND_WSC_NACK;
941                 return WPS_CONTINUE;
942         }
943
944         wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
945                    "attribute");
946         if (wps_parse_msg(decrypted, &eattr) < 0 ||
947             wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
948             wps_process_r_snonce2(wps, eattr.r_snonce2)) {
949                 wpabuf_free(decrypted);
950                 wps->state = SEND_WSC_NACK;
951                 return WPS_CONTINUE;
952         }
953         wpabuf_free(decrypted);
954
955         wps->state = SEND_M7;
956         return WPS_CONTINUE;
957 }
958
959
960 static enum wps_process_res wps_process_m8(struct wps_data *wps,
961                                            const struct wpabuf *msg,
962                                            struct wps_parse_attr *attr)
963 {
964         struct wpabuf *decrypted;
965         struct wps_parse_attr eattr;
966
967         wpa_printf(MSG_DEBUG, "WPS: Received M8");
968
969         if (wps->state != RECV_M8) {
970                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
971                            "receiving M8", wps->state);
972                 wps->state = SEND_WSC_NACK;
973                 return WPS_CONTINUE;
974         }
975
976         if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
977             wps_process_authenticator(wps, attr->authenticator, msg)) {
978                 wps->state = SEND_WSC_NACK;
979                 return WPS_CONTINUE;
980         }
981
982         decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
983                                               attr->encr_settings_len);
984         if (decrypted == NULL) {
985                 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
986                            "Settings attribute");
987                 wps->state = SEND_WSC_NACK;
988                 return WPS_CONTINUE;
989         }
990
991         wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
992                    "attribute");
993         if (wps_parse_msg(decrypted, &eattr) < 0 ||
994             wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
995             wps_process_creds(wps, eattr.cred, eattr.cred_len,
996                               eattr.num_cred) ||
997             wps_process_ap_settings_e(wps, &eattr, decrypted)) {
998                 wpabuf_free(decrypted);
999                 wps->state = SEND_WSC_NACK;
1000                 return WPS_CONTINUE;
1001         }
1002         wpabuf_free(decrypted);
1003
1004         wps->state = WPS_MSG_DONE;
1005         return WPS_CONTINUE;
1006 }
1007
1008
1009 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
1010                                                 const struct wpabuf *msg)
1011 {
1012         struct wps_parse_attr attr;
1013         enum wps_process_res ret = WPS_CONTINUE;
1014
1015         wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
1016
1017         if (wps_parse_msg(msg, &attr) < 0)
1018                 return WPS_FAILURE;
1019
1020         if (!wps_version_supported(attr.version)) {
1021                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1022                            attr.version ? *attr.version : 0);
1023                 return WPS_FAILURE;
1024         }
1025
1026         if (attr.enrollee_nonce == NULL ||
1027             os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1028                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1029                 return WPS_FAILURE;
1030         }
1031
1032         if (attr.msg_type == NULL) {
1033                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1034                 return WPS_FAILURE;
1035         }
1036
1037         switch (*attr.msg_type) {
1038         case WPS_M2:
1039                 ret = wps_process_m2(wps, msg, &attr);
1040                 break;
1041         case WPS_M2D:
1042                 ret = wps_process_m2d(wps, &attr);
1043                 break;
1044         case WPS_M4:
1045                 ret = wps_process_m4(wps, msg, &attr);
1046                 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1047                         wps_fail_event(wps->wps, WPS_M4);
1048                 break;
1049         case WPS_M6:
1050                 ret = wps_process_m6(wps, msg, &attr);
1051                 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1052                         wps_fail_event(wps->wps, WPS_M6);
1053                 break;
1054         case WPS_M8:
1055                 ret = wps_process_m8(wps, msg, &attr);
1056                 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1057                         wps_fail_event(wps->wps, WPS_M8);
1058                 break;
1059         default:
1060                 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
1061                            *attr.msg_type);
1062                 return WPS_FAILURE;
1063         }
1064
1065         /*
1066          * Save a copy of the last message for Authenticator derivation if we
1067          * are continuing. However, skip M2D since it is not authenticated and
1068          * neither is the ACK/NACK response frame. This allows the possibly
1069          * following M2 to be processed correctly by using the previously sent
1070          * M1 in Authenticator derivation.
1071          */
1072         if (ret == WPS_CONTINUE && *attr.msg_type != WPS_M2D) {
1073                 /* Save a copy of the last message for Authenticator derivation
1074                  */
1075                 wpabuf_free(wps->last_msg);
1076                 wps->last_msg = wpabuf_dup(msg);
1077         }
1078
1079         return ret;
1080 }
1081
1082
1083 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
1084                                                 const struct wpabuf *msg)
1085 {
1086         struct wps_parse_attr attr;
1087
1088         wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
1089
1090         if (wps_parse_msg(msg, &attr) < 0)
1091                 return WPS_FAILURE;
1092
1093         if (!wps_version_supported(attr.version)) {
1094                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1095                            attr.version ? *attr.version : 0);
1096                 return WPS_FAILURE;
1097         }
1098
1099         if (attr.msg_type == NULL) {
1100                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1101                 return WPS_FAILURE;
1102         }
1103
1104         if (*attr.msg_type != WPS_WSC_ACK) {
1105                 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1106                            *attr.msg_type);
1107                 return WPS_FAILURE;
1108         }
1109
1110         if (attr.registrar_nonce == NULL ||
1111             os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1112         {
1113                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1114                 return WPS_FAILURE;
1115         }
1116
1117         if (attr.enrollee_nonce == NULL ||
1118             os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1119                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1120                 return WPS_FAILURE;
1121         }
1122
1123         if (wps->state == RECV_ACK && wps->wps->ap) {
1124                 wpa_printf(MSG_DEBUG, "WPS: External Registrar registration "
1125                            "completed successfully");
1126                 wps_success_event(wps->wps);
1127                 wps->state = WPS_FINISHED;
1128                 return WPS_DONE;
1129         }
1130
1131         return WPS_FAILURE;
1132 }
1133
1134
1135 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1136                                                  const struct wpabuf *msg)
1137 {
1138         struct wps_parse_attr attr;
1139
1140         wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1141
1142         if (wps_parse_msg(msg, &attr) < 0)
1143                 return WPS_FAILURE;
1144
1145         if (!wps_version_supported(attr.version)) {
1146                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1147                            attr.version ? *attr.version : 0);
1148                 return WPS_FAILURE;
1149         }
1150
1151         if (attr.msg_type == NULL) {
1152                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1153                 return WPS_FAILURE;
1154         }
1155
1156         if (*attr.msg_type != WPS_WSC_NACK) {
1157                 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1158                            *attr.msg_type);
1159                 return WPS_FAILURE;
1160         }
1161
1162         if (attr.registrar_nonce == NULL ||
1163             os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1164         {
1165                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1166                 wpa_hexdump(MSG_DEBUG, "WPS: Received Registrar Nonce",
1167                             attr.registrar_nonce, WPS_NONCE_LEN);
1168                 wpa_hexdump(MSG_DEBUG, "WPS: Expected Registrar Nonce",
1169                             wps->nonce_r, WPS_NONCE_LEN);
1170                 return WPS_FAILURE;
1171         }
1172
1173         if (attr.enrollee_nonce == NULL ||
1174             os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1175                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1176                 wpa_hexdump(MSG_DEBUG, "WPS: Received Enrollee Nonce",
1177                             attr.enrollee_nonce, WPS_NONCE_LEN);
1178                 wpa_hexdump(MSG_DEBUG, "WPS: Expected Enrollee Nonce",
1179                             wps->nonce_e, WPS_NONCE_LEN);
1180                 return WPS_FAILURE;
1181         }
1182
1183         if (attr.config_error == NULL) {
1184                 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1185                            "in WSC_NACK");
1186                 return WPS_FAILURE;
1187         }
1188
1189         wpa_printf(MSG_DEBUG, "WPS: Registrar terminated negotiation with "
1190                    "Configuration Error %d", WPA_GET_BE16(attr.config_error));
1191
1192         switch (wps->state) {
1193         case RECV_M4:
1194                 wps_fail_event(wps->wps, WPS_M3);
1195                 break;
1196         case RECV_M6:
1197                 wps_fail_event(wps->wps, WPS_M5);
1198                 break;
1199         case RECV_M8:
1200                 wps_fail_event(wps->wps, WPS_M7);
1201                 break;
1202         default:
1203                 break;
1204         }
1205
1206         /* Followed by NACK if Enrollee is Supplicant or EAP-Failure if
1207          * Enrollee is Authenticator */
1208         wps->state = SEND_WSC_NACK;
1209
1210         return WPS_FAILURE;
1211 }
1212
1213
1214 enum wps_process_res wps_enrollee_process_msg(struct wps_data *wps,
1215                                               enum wsc_op_code op_code,
1216                                               const struct wpabuf *msg)
1217 {
1218
1219         wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
1220                    "op_code=%d)",
1221                    (unsigned long) wpabuf_len(msg), op_code);
1222
1223         if (op_code == WSC_UPnP) {
1224                 /* Determine the OpCode based on message type attribute */
1225                 struct wps_parse_attr attr;
1226                 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type) {
1227                         if (*attr.msg_type == WPS_WSC_ACK)
1228                                 op_code = WSC_ACK;
1229                         else if (*attr.msg_type == WPS_WSC_NACK)
1230                                 op_code = WSC_NACK;
1231                 }
1232         }
1233
1234         switch (op_code) {
1235         case WSC_MSG:
1236         case WSC_UPnP:
1237                 return wps_process_wsc_msg(wps, msg);
1238         case WSC_ACK:
1239                 return wps_process_wsc_ack(wps, msg);
1240         case WSC_NACK:
1241                 return wps_process_wsc_nack(wps, msg);
1242         default:
1243                 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
1244                 return WPS_FAILURE;
1245         }
1246 }