Fixed WPS Authenticator attribute processing after M2D
[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 int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *plain)
272 {
273         if (wps->wps->ap_settings) {
274                 wpa_printf(MSG_DEBUG, "WPS:  * AP Settings (pre-configured)");
275                 wpabuf_put_data(plain, wps->wps->ap_settings,
276                                 wps->wps->ap_settings_len);
277                 return 0;
278         }
279
280         return wps_build_cred_ssid(wps, plain) ||
281                 wps_build_cred_mac_addr(wps, plain) ||
282                 wps_build_cred_auth_type(wps, plain) ||
283                 wps_build_cred_encr_type(wps, plain) ||
284                 wps_build_cred_network_key(wps, plain);
285 }
286
287
288 static struct wpabuf * wps_build_m7(struct wps_data *wps)
289 {
290         struct wpabuf *msg, *plain;
291
292         wpa_printf(MSG_DEBUG, "WPS: Building Message M7");
293
294         plain = wpabuf_alloc(500 + wps->wps->ap_settings_len);
295         if (plain == NULL)
296                 return NULL;
297
298         msg = wpabuf_alloc(1000 + wps->wps->ap_settings_len);
299         if (msg == NULL) {
300                 wpabuf_free(plain);
301                 return NULL;
302         }
303
304         if (wps_build_version(msg) ||
305             wps_build_msg_type(msg, WPS_M7) ||
306             wps_build_registrar_nonce(wps, msg) ||
307             wps_build_e_snonce2(wps, plain) ||
308             (wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
309             wps_build_key_wrap_auth(wps, plain) ||
310             wps_build_encr_settings(wps, msg, plain) ||
311             wps_build_authenticator(wps, msg)) {
312                 wpabuf_free(plain);
313                 wpabuf_free(msg);
314                 return NULL;
315         }
316         wpabuf_free(plain);
317
318         wps->state = RECV_M8;
319         return msg;
320 }
321
322
323 static struct wpabuf * wps_build_wsc_done(struct wps_data *wps)
324 {
325         struct wpabuf *msg;
326
327         wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_Done");
328
329         msg = wpabuf_alloc(1000);
330         if (msg == NULL)
331                 return NULL;
332
333         if (wps_build_version(msg) ||
334             wps_build_msg_type(msg, WPS_WSC_DONE) ||
335             wps_build_enrollee_nonce(wps, msg) ||
336             wps_build_registrar_nonce(wps, msg)) {
337                 wpabuf_free(msg);
338                 return NULL;
339         }
340
341         if (wps->wps->ap)
342                 wps->state = RECV_ACK;
343         else {
344                 wps_success_event(wps->wps);
345                 wps->state = WPS_FINISHED;
346         }
347         return msg;
348 }
349
350
351 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
352 {
353         struct wpabuf *msg;
354
355         wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
356
357         msg = wpabuf_alloc(1000);
358         if (msg == NULL)
359                 return NULL;
360
361         if (wps_build_version(msg) ||
362             wps_build_msg_type(msg, WPS_WSC_ACK) ||
363             wps_build_enrollee_nonce(wps, msg) ||
364             wps_build_registrar_nonce(wps, msg)) {
365                 wpabuf_free(msg);
366                 return NULL;
367         }
368
369         return msg;
370 }
371
372
373 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
374 {
375         struct wpabuf *msg;
376
377         wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
378
379         msg = wpabuf_alloc(1000);
380         if (msg == NULL)
381                 return NULL;
382
383         if (wps_build_version(msg) ||
384             wps_build_msg_type(msg, WPS_WSC_NACK) ||
385             wps_build_enrollee_nonce(wps, msg) ||
386             wps_build_registrar_nonce(wps, msg) ||
387             wps_build_config_error(msg, wps->config_error)) {
388                 wpabuf_free(msg);
389                 return NULL;
390         }
391
392         return msg;
393 }
394
395
396 struct wpabuf * wps_enrollee_get_msg(struct wps_data *wps,
397                                      enum wsc_op_code *op_code)
398 {
399         struct wpabuf *msg;
400
401         switch (wps->state) {
402         case SEND_M1:
403                 msg = wps_build_m1(wps);
404                 *op_code = WSC_MSG;
405                 break;
406         case SEND_M3:
407                 msg = wps_build_m3(wps);
408                 *op_code = WSC_MSG;
409                 break;
410         case SEND_M5:
411                 msg = wps_build_m5(wps);
412                 *op_code = WSC_MSG;
413                 break;
414         case SEND_M7:
415                 msg = wps_build_m7(wps);
416                 *op_code = WSC_MSG;
417                 break;
418         case RECEIVED_M2D:
419                 if (wps->wps->ap) {
420                         msg = wps_build_wsc_nack(wps);
421                         *op_code = WSC_NACK;
422                         break;
423                 }
424                 msg = wps_build_wsc_ack(wps);
425                 *op_code = WSC_ACK;
426                 if (msg) {
427                         /* Another M2/M2D may be received */
428                         wps->state = RECV_M2;
429                 }
430                 break;
431         case SEND_WSC_NACK:
432                 msg = wps_build_wsc_nack(wps);
433                 *op_code = WSC_NACK;
434                 break;
435         case WPS_MSG_DONE:
436                 msg = wps_build_wsc_done(wps);
437                 *op_code = WSC_Done;
438                 break;
439         default:
440                 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
441                            "a message", wps->state);
442                 msg = NULL;
443                 break;
444         }
445
446         if (*op_code == WSC_MSG && msg) {
447                 /* Save a copy of the last message for Authenticator derivation
448                  */
449                 wpabuf_free(wps->last_msg);
450                 wps->last_msg = wpabuf_dup(msg);
451         }
452
453         return msg;
454 }
455
456
457 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
458 {
459         if (r_nonce == NULL) {
460                 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
461                 return -1;
462         }
463
464         os_memcpy(wps->nonce_r, r_nonce, WPS_NONCE_LEN);
465         wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
466                     wps->nonce_r, WPS_NONCE_LEN);
467
468         return 0;
469 }
470
471
472 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
473 {
474         if (e_nonce == NULL) {
475                 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
476                 return -1;
477         }
478
479         if (os_memcmp(wps->nonce_e, e_nonce, WPS_NONCE_LEN) != 0) {
480                 wpa_printf(MSG_DEBUG, "WPS: Invalid Enrollee Nonce received");
481                 return -1;
482         }
483
484         return 0;
485 }
486
487
488 static int wps_process_uuid_r(struct wps_data *wps, const u8 *uuid_r)
489 {
490         if (uuid_r == NULL) {
491                 wpa_printf(MSG_DEBUG, "WPS: No UUID-R received");
492                 return -1;
493         }
494
495         os_memcpy(wps->uuid_r, uuid_r, WPS_UUID_LEN);
496         wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
497
498         return 0;
499 }
500
501
502 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
503                               size_t pk_len)
504 {
505         if (pk == NULL || pk_len == 0) {
506                 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
507                 return -1;
508         }
509
510         wpabuf_free(wps->dh_pubkey_r);
511         wps->dh_pubkey_r = wpabuf_alloc_copy(pk, pk_len);
512         if (wps->dh_pubkey_r == NULL)
513                 return -1;
514
515         if (wps_derive_keys(wps) < 0)
516                 return -1;
517
518         if (wps->request_type == WPS_REQ_WLAN_MANAGER_REGISTRAR &&
519             wps_derive_mgmt_keys(wps) < 0)
520                 return -1;
521
522         return 0;
523 }
524
525
526 static int wps_process_r_hash1(struct wps_data *wps, const u8 *r_hash1)
527 {
528         if (r_hash1 == NULL) {
529                 wpa_printf(MSG_DEBUG, "WPS: No R-Hash1 received");
530                 return -1;
531         }
532
533         os_memcpy(wps->peer_hash1, r_hash1, WPS_HASH_LEN);
534         wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", wps->peer_hash1, WPS_HASH_LEN);
535
536         return 0;
537 }
538
539
540 static int wps_process_r_hash2(struct wps_data *wps, const u8 *r_hash2)
541 {
542         if (r_hash2 == NULL) {
543                 wpa_printf(MSG_DEBUG, "WPS: No R-Hash2 received");
544                 return -1;
545         }
546
547         os_memcpy(wps->peer_hash2, r_hash2, WPS_HASH_LEN);
548         wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", wps->peer_hash2, WPS_HASH_LEN);
549
550         return 0;
551 }
552
553
554 static int wps_process_r_snonce1(struct wps_data *wps, const u8 *r_snonce1)
555 {
556         u8 hash[SHA256_MAC_LEN];
557         const u8 *addr[4];
558         size_t len[4];
559
560         if (r_snonce1 == NULL) {
561                 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce1 received");
562                 return -1;
563         }
564
565         wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce1", r_snonce1,
566                         WPS_SECRET_NONCE_LEN);
567
568         /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
569         addr[0] = r_snonce1;
570         len[0] = WPS_SECRET_NONCE_LEN;
571         addr[1] = wps->psk1;
572         len[1] = WPS_PSK_LEN;
573         addr[2] = wpabuf_head(wps->dh_pubkey_e);
574         len[2] = wpabuf_len(wps->dh_pubkey_e);
575         addr[3] = wpabuf_head(wps->dh_pubkey_r);
576         len[3] = wpabuf_len(wps->dh_pubkey_r);
577         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
578
579         if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
580                 wpa_printf(MSG_DEBUG, "WPS: R-Hash1 derived from R-S1 does "
581                            "not match with the pre-committed value");
582                 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
583                 wps_pwd_auth_fail_event(wps->wps, 1, 1);
584                 return -1;
585         }
586
587         wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the first "
588                    "half of the device password");
589
590         return 0;
591 }
592
593
594 static int wps_process_r_snonce2(struct wps_data *wps, const u8 *r_snonce2)
595 {
596         u8 hash[SHA256_MAC_LEN];
597         const u8 *addr[4];
598         size_t len[4];
599
600         if (r_snonce2 == NULL) {
601                 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce2 received");
602                 return -1;
603         }
604
605         wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce2", r_snonce2,
606                         WPS_SECRET_NONCE_LEN);
607
608         /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
609         addr[0] = r_snonce2;
610         len[0] = WPS_SECRET_NONCE_LEN;
611         addr[1] = wps->psk2;
612         len[1] = WPS_PSK_LEN;
613         addr[2] = wpabuf_head(wps->dh_pubkey_e);
614         len[2] = wpabuf_len(wps->dh_pubkey_e);
615         addr[3] = wpabuf_head(wps->dh_pubkey_r);
616         len[3] = wpabuf_len(wps->dh_pubkey_r);
617         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
618
619         if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
620                 wpa_printf(MSG_DEBUG, "WPS: R-Hash2 derived from R-S2 does "
621                            "not match with the pre-committed value");
622                 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
623                 wps_pwd_auth_fail_event(wps->wps, 1, 2);
624                 return -1;
625         }
626
627         wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the second "
628                    "half of the device password");
629
630         return 0;
631 }
632
633
634 static int wps_process_cred_e(struct wps_data *wps, const u8 *cred,
635                               size_t cred_len)
636 {
637         struct wps_parse_attr attr;
638         struct wpabuf msg;
639
640         wpa_printf(MSG_DEBUG, "WPS: Received Credential");
641         os_memset(&wps->cred, 0, sizeof(wps->cred));
642         wpabuf_set(&msg, cred, cred_len);
643         if (wps_parse_msg(&msg, &attr) < 0 ||
644             wps_process_cred(&attr, &wps->cred))
645                 return -1;
646
647         if (wps->wps->cred_cb) {
648                 wps->cred.cred_attr = cred - 4;
649                 wps->cred.cred_attr_len = cred_len + 4;
650                 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
651                 wps->cred.cred_attr = NULL;
652                 wps->cred.cred_attr_len = 0;
653         }
654
655         return 0;
656 }
657
658
659 static int wps_process_creds(struct wps_data *wps, const u8 *cred[],
660                              size_t cred_len[], size_t num_cred)
661 {
662         size_t i;
663
664         if (wps->wps->ap)
665                 return 0;
666
667         if (num_cred == 0) {
668                 wpa_printf(MSG_DEBUG, "WPS: No Credential attributes "
669                            "received");
670                 return -1;
671         }
672
673         for (i = 0; i < num_cred; i++) {
674                 if (wps_process_cred_e(wps, cred[i], cred_len[i]))
675                         return -1;
676         }
677
678         return 0;
679 }
680
681
682 static int wps_process_ap_settings_e(struct wps_data *wps,
683                                      struct wps_parse_attr *attr,
684                                      struct wpabuf *attrs)
685 {
686         struct wps_credential cred;
687
688         if (!wps->wps->ap)
689                 return 0;
690
691         if (wps_process_ap_settings(attr, &cred) < 0)
692                 return -1;
693
694         wpa_printf(MSG_INFO, "WPS: Received new AP configuration from "
695                    "Registrar");
696
697         if (wps->wps->cred_cb) {
698                 cred.cred_attr = wpabuf_head(attrs);
699                 cred.cred_attr_len = wpabuf_len(attrs);
700                 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
701         }
702
703         return 0;
704 }
705
706
707 static enum wps_process_res wps_process_m2(struct wps_data *wps,
708                                            const struct wpabuf *msg,
709                                            struct wps_parse_attr *attr)
710 {
711         wpa_printf(MSG_DEBUG, "WPS: Received M2");
712
713         if (wps->state != RECV_M2) {
714                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
715                            "receiving M2", wps->state);
716                 wps->state = SEND_WSC_NACK;
717                 return WPS_CONTINUE;
718         }
719
720         if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
721             wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
722             wps_process_uuid_r(wps, attr->uuid_r) ||
723             wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
724             wps_process_authenticator(wps, attr->authenticator, msg)) {
725                 wps->state = SEND_WSC_NACK;
726                 return WPS_CONTINUE;
727         }
728
729         if (wps->wps->ap && wps->wps->ap_setup_locked) {
730                 wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse "
731                            "registration of a new Registrar");
732                 wps->config_error = WPS_CFG_SETUP_LOCKED;
733                 wps->state = SEND_WSC_NACK;
734                 return WPS_CONTINUE;
735         }
736
737         wps->state = SEND_M3;
738         return WPS_CONTINUE;
739 }
740
741
742 static enum wps_process_res wps_process_m2d(struct wps_data *wps,
743                                             struct wps_parse_attr *attr)
744 {
745         wpa_printf(MSG_DEBUG, "WPS: Received M2D");
746
747         if (wps->state != RECV_M2) {
748                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
749                            "receiving M2D", wps->state);
750                 wps->state = SEND_WSC_NACK;
751                 return WPS_CONTINUE;
752         }
753
754         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer",
755                           attr->manufacturer, attr->manufacturer_len);
756         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name",
757                           attr->model_name, attr->model_name_len);
758         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number",
759                           attr->model_number, attr->model_number_len);
760         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number",
761                           attr->serial_number, attr->serial_number_len);
762         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name",
763                           attr->dev_name, attr->dev_name_len);
764
765         if (wps->wps->event_cb) {
766                 union wps_event_data data;
767                 struct wps_event_m2d *m2d = &data.m2d;
768                 os_memset(&data, 0, sizeof(data));
769                 if (attr->config_methods)
770                         m2d->config_methods =
771                                 WPA_GET_BE16(attr->config_methods);
772                 m2d->manufacturer = attr->manufacturer;
773                 m2d->manufacturer_len = attr->manufacturer_len;
774                 m2d->model_name = attr->model_name;
775                 m2d->model_name_len = attr->model_name_len;
776                 m2d->model_number = attr->model_number;
777                 m2d->model_number_len = attr->model_number_len;
778                 m2d->serial_number = attr->serial_number;
779                 m2d->serial_number_len = attr->serial_number_len;
780                 m2d->dev_name = attr->dev_name;
781                 m2d->dev_name_len = attr->dev_name_len;
782                 m2d->primary_dev_type = attr->primary_dev_type;
783                 if (attr->config_error)
784                         m2d->config_error =
785                                 WPA_GET_BE16(attr->config_error);
786                 if (attr->dev_password_id)
787                         m2d->dev_password_id =
788                                 WPA_GET_BE16(attr->dev_password_id);
789                 wps->wps->event_cb(wps->wps->cb_ctx, WPS_EV_M2D, &data);
790         }
791
792         wps->state = RECEIVED_M2D;
793         return WPS_CONTINUE;
794 }
795
796
797 static enum wps_process_res wps_process_m4(struct wps_data *wps,
798                                            const struct wpabuf *msg,
799                                            struct wps_parse_attr *attr)
800 {
801         struct wpabuf *decrypted;
802         struct wps_parse_attr eattr;
803
804         wpa_printf(MSG_DEBUG, "WPS: Received M4");
805
806         if (wps->state != RECV_M4) {
807                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
808                            "receiving M4", wps->state);
809                 wps->state = SEND_WSC_NACK;
810                 return WPS_CONTINUE;
811         }
812
813         if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
814             wps_process_authenticator(wps, attr->authenticator, msg) ||
815             wps_process_r_hash1(wps, attr->r_hash1) ||
816             wps_process_r_hash2(wps, attr->r_hash2)) {
817                 wps->state = SEND_WSC_NACK;
818                 return WPS_CONTINUE;
819         }
820
821         decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
822                                               attr->encr_settings_len);
823         if (decrypted == NULL) {
824                 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
825                            "Settings attribute");
826                 wps->state = SEND_WSC_NACK;
827                 return WPS_CONTINUE;
828         }
829
830         wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
831                    "attribute");
832         if (wps_parse_msg(decrypted, &eattr) < 0 ||
833             wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
834             wps_process_r_snonce1(wps, eattr.r_snonce1)) {
835                 wpabuf_free(decrypted);
836                 wps->state = SEND_WSC_NACK;
837                 return WPS_CONTINUE;
838         }
839         wpabuf_free(decrypted);
840
841         wps->state = SEND_M5;
842         return WPS_CONTINUE;
843 }
844
845
846 static enum wps_process_res wps_process_m6(struct wps_data *wps,
847                                            const struct wpabuf *msg,
848                                            struct wps_parse_attr *attr)
849 {
850         struct wpabuf *decrypted;
851         struct wps_parse_attr eattr;
852
853         wpa_printf(MSG_DEBUG, "WPS: Received M6");
854
855         if (wps->state != RECV_M6) {
856                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
857                            "receiving M6", wps->state);
858                 wps->state = SEND_WSC_NACK;
859                 return WPS_CONTINUE;
860         }
861
862         if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
863             wps_process_authenticator(wps, attr->authenticator, msg)) {
864                 wps->state = SEND_WSC_NACK;
865                 return WPS_CONTINUE;
866         }
867
868         decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
869                                               attr->encr_settings_len);
870         if (decrypted == NULL) {
871                 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
872                            "Settings attribute");
873                 wps->state = SEND_WSC_NACK;
874                 return WPS_CONTINUE;
875         }
876
877         wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
878                    "attribute");
879         if (wps_parse_msg(decrypted, &eattr) < 0 ||
880             wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
881             wps_process_r_snonce2(wps, eattr.r_snonce2)) {
882                 wpabuf_free(decrypted);
883                 wps->state = SEND_WSC_NACK;
884                 return WPS_CONTINUE;
885         }
886         wpabuf_free(decrypted);
887
888         wps->state = SEND_M7;
889         return WPS_CONTINUE;
890 }
891
892
893 static enum wps_process_res wps_process_m8(struct wps_data *wps,
894                                            const struct wpabuf *msg,
895                                            struct wps_parse_attr *attr)
896 {
897         struct wpabuf *decrypted;
898         struct wps_parse_attr eattr;
899
900         wpa_printf(MSG_DEBUG, "WPS: Received M8");
901
902         if (wps->state != RECV_M8) {
903                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
904                            "receiving M8", wps->state);
905                 wps->state = SEND_WSC_NACK;
906                 return WPS_CONTINUE;
907         }
908
909         if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
910             wps_process_authenticator(wps, attr->authenticator, msg)) {
911                 wps->state = SEND_WSC_NACK;
912                 return WPS_CONTINUE;
913         }
914
915         decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
916                                               attr->encr_settings_len);
917         if (decrypted == NULL) {
918                 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
919                            "Settings attribute");
920                 wps->state = SEND_WSC_NACK;
921                 return WPS_CONTINUE;
922         }
923
924         wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
925                    "attribute");
926         if (wps_parse_msg(decrypted, &eattr) < 0 ||
927             wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
928             wps_process_creds(wps, eattr.cred, eattr.cred_len,
929                               eattr.num_cred) ||
930             wps_process_ap_settings_e(wps, &eattr, decrypted)) {
931                 wpabuf_free(decrypted);
932                 wps->state = SEND_WSC_NACK;
933                 return WPS_CONTINUE;
934         }
935         wpabuf_free(decrypted);
936
937         wps->state = WPS_MSG_DONE;
938         return WPS_CONTINUE;
939 }
940
941
942 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
943                                                 const struct wpabuf *msg)
944 {
945         struct wps_parse_attr attr;
946         enum wps_process_res ret = WPS_CONTINUE;
947
948         wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
949
950         if (wps_parse_msg(msg, &attr) < 0)
951                 return WPS_FAILURE;
952
953         if (attr.version == NULL || *attr.version != WPS_VERSION) {
954                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
955                            attr.version ? *attr.version : 0);
956                 return WPS_FAILURE;
957         }
958
959         if (attr.enrollee_nonce == NULL ||
960             os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
961                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
962                 return WPS_FAILURE;
963         }
964
965         if (attr.msg_type == NULL) {
966                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
967                 return WPS_FAILURE;
968         }
969
970         switch (*attr.msg_type) {
971         case WPS_M2:
972                 ret = wps_process_m2(wps, msg, &attr);
973                 break;
974         case WPS_M2D:
975                 ret = wps_process_m2d(wps, &attr);
976                 break;
977         case WPS_M4:
978                 ret = wps_process_m4(wps, msg, &attr);
979                 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
980                         wps_fail_event(wps->wps, WPS_M4);
981                 break;
982         case WPS_M6:
983                 ret = wps_process_m6(wps, msg, &attr);
984                 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
985                         wps_fail_event(wps->wps, WPS_M6);
986                 break;
987         case WPS_M8:
988                 ret = wps_process_m8(wps, msg, &attr);
989                 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
990                         wps_fail_event(wps->wps, WPS_M8);
991                 break;
992         default:
993                 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
994                            *attr.msg_type);
995                 return WPS_FAILURE;
996         }
997
998         /*
999          * Save a copy of the last message for Authenticator derivation if we
1000          * are continuing. However, skip M2D since it is not authenticated and
1001          * neither is the ACK/NACK response frame. This allows the possibly
1002          * following M2 to be processed correctly by using the previously sent
1003          * M1 in Authenticator derivation.
1004          */
1005         if (ret == WPS_CONTINUE && *attr.msg_type != WPS_M2D) {
1006                 /* Save a copy of the last message for Authenticator derivation
1007                  */
1008                 wpabuf_free(wps->last_msg);
1009                 wps->last_msg = wpabuf_dup(msg);
1010         }
1011
1012         return ret;
1013 }
1014
1015
1016 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
1017                                                 const struct wpabuf *msg)
1018 {
1019         struct wps_parse_attr attr;
1020
1021         wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
1022
1023         if (wps_parse_msg(msg, &attr) < 0)
1024                 return WPS_FAILURE;
1025
1026         if (attr.version == NULL || *attr.version != WPS_VERSION) {
1027                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1028                            attr.version ? *attr.version : 0);
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         if (*attr.msg_type != WPS_WSC_ACK) {
1038                 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1039                            *attr.msg_type);
1040                 return WPS_FAILURE;
1041         }
1042
1043         if (attr.registrar_nonce == NULL ||
1044             os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1045         {
1046                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1047                 return WPS_FAILURE;
1048         }
1049
1050         if (attr.enrollee_nonce == NULL ||
1051             os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1052                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1053                 return WPS_FAILURE;
1054         }
1055
1056         if (wps->state == RECV_ACK && wps->wps->ap) {
1057                 wpa_printf(MSG_DEBUG, "WPS: External Registrar registration "
1058                            "completed successfully");
1059                 wps_success_event(wps->wps);
1060                 wps->state = WPS_FINISHED;
1061                 return WPS_DONE;
1062         }
1063
1064         return WPS_FAILURE;
1065 }
1066
1067
1068 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1069                                                  const struct wpabuf *msg)
1070 {
1071         struct wps_parse_attr attr;
1072
1073         wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1074
1075         if (wps_parse_msg(msg, &attr) < 0)
1076                 return WPS_FAILURE;
1077
1078         if (attr.version == NULL || *attr.version != WPS_VERSION) {
1079                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1080                            attr.version ? *attr.version : 0);
1081                 return WPS_FAILURE;
1082         }
1083
1084         if (attr.msg_type == NULL) {
1085                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1086                 return WPS_FAILURE;
1087         }
1088
1089         if (*attr.msg_type != WPS_WSC_NACK) {
1090                 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1091                            *attr.msg_type);
1092                 return WPS_FAILURE;
1093         }
1094
1095         if (attr.registrar_nonce == NULL ||
1096             os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1097         {
1098                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1099                 wpa_hexdump(MSG_DEBUG, "WPS: Received Registrar Nonce",
1100                             attr.registrar_nonce, WPS_NONCE_LEN);
1101                 wpa_hexdump(MSG_DEBUG, "WPS: Expected Registrar Nonce",
1102                             wps->nonce_r, WPS_NONCE_LEN);
1103                 return WPS_FAILURE;
1104         }
1105
1106         if (attr.enrollee_nonce == NULL ||
1107             os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1108                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1109                 wpa_hexdump(MSG_DEBUG, "WPS: Received Enrollee Nonce",
1110                             attr.enrollee_nonce, WPS_NONCE_LEN);
1111                 wpa_hexdump(MSG_DEBUG, "WPS: Expected Enrollee Nonce",
1112                             wps->nonce_e, WPS_NONCE_LEN);
1113                 return WPS_FAILURE;
1114         }
1115
1116         if (attr.config_error == NULL) {
1117                 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1118                            "in WSC_NACK");
1119                 return WPS_FAILURE;
1120         }
1121
1122         wpa_printf(MSG_DEBUG, "WPS: Registrar terminated negotiation with "
1123                    "Configuration Error %d", WPA_GET_BE16(attr.config_error));
1124
1125         switch (wps->state) {
1126         case RECV_M4:
1127                 wps_fail_event(wps->wps, WPS_M3);
1128                 break;
1129         case RECV_M6:
1130                 wps_fail_event(wps->wps, WPS_M5);
1131                 break;
1132         case RECV_M8:
1133                 wps_fail_event(wps->wps, WPS_M7);
1134                 break;
1135         default:
1136                 break;
1137         }
1138
1139         /* Followed by NACK if Enrollee is Supplicant or EAP-Failure if
1140          * Enrollee is Authenticator */
1141         wps->state = SEND_WSC_NACK;
1142
1143         return WPS_FAILURE;
1144 }
1145
1146
1147 enum wps_process_res wps_enrollee_process_msg(struct wps_data *wps,
1148                                               enum wsc_op_code op_code,
1149                                               const struct wpabuf *msg)
1150 {
1151
1152         wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
1153                    "op_code=%d)",
1154                    (unsigned long) wpabuf_len(msg), op_code);
1155
1156         switch (op_code) {
1157         case WSC_MSG:
1158                 return wps_process_wsc_msg(wps, msg);
1159         case WSC_ACK:
1160                 return wps_process_wsc_ack(wps, msg);
1161         case WSC_NACK:
1162                 return wps_process_wsc_nack(wps, msg);
1163         default:
1164                 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
1165                 return WPS_FAILURE;
1166         }
1167 }