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