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