xref: /dragonfly/contrib/wpa_supplicant/src/wps/wps_enrollee.c (revision 3a84a4273475ed07d0ab1c2dfeffdfedef35d9cd)
1 /*
2  * Wi-Fi Protected Setup - Enrollee
3  * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 
11 #include "common.h"
12 #include "crypto/crypto.h"
13 #include "crypto/sha256.h"
14 #include "crypto/random.h"
15 #include "wps_i.h"
16 #include "wps_dev_attr.h"
17 
18 
wps_build_wps_state(struct wps_data * wps,struct wpabuf * msg)19 static int wps_build_wps_state(struct wps_data *wps, struct wpabuf *msg)
20 {
21           u8 state;
22           if (wps->wps->ap)
23                     state = wps->wps->wps_state;
24           else
25                     state = WPS_STATE_NOT_CONFIGURED;
26           wpa_printf(MSG_DEBUG, "WPS:  * Wi-Fi Protected Setup State (%d)",
27                        state);
28           wpabuf_put_be16(msg, ATTR_WPS_STATE);
29           wpabuf_put_be16(msg, 1);
30           wpabuf_put_u8(msg, state);
31           return 0;
32 }
33 
34 
wps_build_e_hash(struct wps_data * wps,struct wpabuf * msg)35 static int wps_build_e_hash(struct wps_data *wps, struct wpabuf *msg)
36 {
37           u8 *hash;
38           const u8 *addr[4];
39           size_t len[4];
40 
41           if (random_get_bytes(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
42                     return -1;
43           wpa_hexdump(MSG_DEBUG, "WPS: E-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
44           wpa_hexdump(MSG_DEBUG, "WPS: E-S2",
45                         wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
46 
47           if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
48                     wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
49                                  "E-Hash derivation");
50                     return -1;
51           }
52 
53           wpa_printf(MSG_DEBUG, "WPS:  * E-Hash1");
54           wpabuf_put_be16(msg, ATTR_E_HASH1);
55           wpabuf_put_be16(msg, SHA256_MAC_LEN);
56           hash = wpabuf_put(msg, SHA256_MAC_LEN);
57           /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
58           addr[0] = wps->snonce;
59           len[0] = WPS_SECRET_NONCE_LEN;
60           addr[1] = wps->psk1;
61           len[1] = WPS_PSK_LEN;
62           addr[2] = wpabuf_head(wps->dh_pubkey_e);
63           len[2] = wpabuf_len(wps->dh_pubkey_e);
64           addr[3] = wpabuf_head(wps->dh_pubkey_r);
65           len[3] = wpabuf_len(wps->dh_pubkey_r);
66           hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
67           wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", hash, SHA256_MAC_LEN);
68 
69           wpa_printf(MSG_DEBUG, "WPS:  * E-Hash2");
70           wpabuf_put_be16(msg, ATTR_E_HASH2);
71           wpabuf_put_be16(msg, SHA256_MAC_LEN);
72           hash = wpabuf_put(msg, SHA256_MAC_LEN);
73           /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
74           addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
75           addr[1] = wps->psk2;
76           hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
77           wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", hash, SHA256_MAC_LEN);
78 
79           return 0;
80 }
81 
82 
wps_build_e_snonce1(struct wps_data * wps,struct wpabuf * msg)83 static int wps_build_e_snonce1(struct wps_data *wps, struct wpabuf *msg)
84 {
85           wpa_printf(MSG_DEBUG, "WPS:  * E-SNonce1");
86           wpabuf_put_be16(msg, ATTR_E_SNONCE1);
87           wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
88           wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
89           return 0;
90 }
91 
92 
wps_build_e_snonce2(struct wps_data * wps,struct wpabuf * msg)93 static int wps_build_e_snonce2(struct wps_data *wps, struct wpabuf *msg)
94 {
95           wpa_printf(MSG_DEBUG, "WPS:  * E-SNonce2");
96           wpabuf_put_be16(msg, ATTR_E_SNONCE2);
97           wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
98           wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
99                               WPS_SECRET_NONCE_LEN);
100           return 0;
101 }
102 
103 
wps_build_m1(struct wps_data * wps)104 static struct wpabuf * wps_build_m1(struct wps_data *wps)
105 {
106           struct wpabuf *msg;
107           u16 config_methods;
108           u8 multi_ap_backhaul_sta = 0;
109 
110           if (random_get_bytes(wps->nonce_e, WPS_NONCE_LEN) < 0)
111                     return NULL;
112           wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
113                         wps->nonce_e, WPS_NONCE_LEN);
114 
115           wpa_printf(MSG_DEBUG, "WPS: Building Message M1");
116           msg = wpabuf_alloc(1000);
117           if (msg == NULL)
118                     return NULL;
119 
120           config_methods = wps->wps->config_methods;
121           if (wps->wps->ap && !wps->pbc_in_m1 &&
122               (wps->dev_password_len != 0 ||
123                (config_methods & WPS_CONFIG_DISPLAY))) {
124                     /*
125                      * These are the methods that the AP supports as an Enrollee
126                      * for adding external Registrars, so remove PushButton.
127                      *
128                      * As a workaround for Windows 7 mechanism for probing WPS
129                      * capabilities from M1, leave PushButton option if no PIN
130                      * method is available or if WPS configuration enables PBC
131                      * workaround.
132                      */
133                     config_methods &= ~WPS_CONFIG_PUSHBUTTON;
134                     config_methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
135                                             WPS_CONFIG_PHY_PUSHBUTTON);
136           }
137 
138           if (wps->multi_ap_backhaul_sta)
139                     multi_ap_backhaul_sta = MULTI_AP_BACKHAUL_STA;
140 
141           if (wps_build_version(msg) ||
142               wps_build_msg_type(msg, WPS_M1) ||
143               wps_build_uuid_e(msg, wps->uuid_e) ||
144               wps_build_mac_addr(msg, wps->mac_addr_e) ||
145               wps_build_enrollee_nonce(wps, msg) ||
146               wps_build_public_key(wps, msg) ||
147               wps_build_auth_type_flags(wps, msg) ||
148               wps_build_encr_type_flags(wps, msg) ||
149               wps_build_conn_type_flags(wps, msg) ||
150               wps_build_config_methods(msg, config_methods) ||
151               wps_build_wps_state(wps, msg) ||
152               wps_build_device_attrs(&wps->wps->dev, msg) ||
153               wps_build_rf_bands(&wps->wps->dev, msg,
154                                      wps->wps->rf_band_cb(wps->wps->cb_ctx)) ||
155               wps_build_assoc_state(wps, msg) ||
156               wps_build_dev_password_id(msg, wps->dev_pw_id) ||
157               wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
158               wps_build_os_version(&wps->wps->dev, msg) ||
159               wps_build_wfa_ext(msg, 0, NULL, 0, multi_ap_backhaul_sta) ||
160               wps_build_vendor_ext_m1(&wps->wps->dev, msg)) {
161                     wpabuf_free(msg);
162                     return NULL;
163           }
164 
165           wps->state = RECV_M2;
166           return msg;
167 }
168 
169 
wps_build_m3(struct wps_data * wps)170 static struct wpabuf * wps_build_m3(struct wps_data *wps)
171 {
172           struct wpabuf *msg;
173 
174           wpa_printf(MSG_DEBUG, "WPS: Building Message M3");
175 
176           if (wps->dev_password == NULL) {
177                     wpa_printf(MSG_DEBUG, "WPS: No Device Password available");
178                     return NULL;
179           }
180           if (wps_derive_psk(wps, wps->dev_password, wps->dev_password_len) < 0)
181                     return NULL;
182 
183           if (wps->wps->ap && random_pool_ready() != 1) {
184                     wpa_printf(MSG_INFO,
185                                  "WPS: Not enough entropy in random pool to proceed - do not allow AP PIN to be used");
186                     return NULL;
187           }
188 
189           msg = wpabuf_alloc(1000);
190           if (msg == NULL)
191                     return NULL;
192 
193           if (wps_build_version(msg) ||
194               wps_build_msg_type(msg, WPS_M3) ||
195               wps_build_registrar_nonce(wps, msg) ||
196               wps_build_e_hash(wps, msg) ||
197               wps_build_wfa_ext(msg, 0, NULL, 0, 0) ||
198               wps_build_authenticator(wps, msg)) {
199                     wpabuf_free(msg);
200                     return NULL;
201           }
202 
203           wps->state = RECV_M4;
204           return msg;
205 }
206 
207 
wps_build_m5(struct wps_data * wps)208 static struct wpabuf * wps_build_m5(struct wps_data *wps)
209 {
210           struct wpabuf *msg, *plain;
211 
212           wpa_printf(MSG_DEBUG, "WPS: Building Message M5");
213 
214           plain = wpabuf_alloc(200);
215           if (plain == NULL)
216                     return NULL;
217 
218           msg = wpabuf_alloc(1000);
219           if (msg == NULL) {
220                     wpabuf_free(plain);
221                     return NULL;
222           }
223 
224           if (wps_build_version(msg) ||
225               wps_build_msg_type(msg, WPS_M5) ||
226               wps_build_registrar_nonce(wps, msg) ||
227               wps_build_e_snonce1(wps, plain) ||
228               wps_build_key_wrap_auth(wps, plain) ||
229               wps_build_encr_settings(wps, msg, plain) ||
230               wps_build_wfa_ext(msg, 0, NULL, 0, 0) ||
231               wps_build_authenticator(wps, msg)) {
232                     wpabuf_clear_free(plain);
233                     wpabuf_free(msg);
234                     return NULL;
235           }
236           wpabuf_clear_free(plain);
237 
238           wps->state = RECV_M6;
239           return msg;
240 }
241 
242 
wps_build_cred_ssid(struct wps_data * wps,struct wpabuf * msg)243 static int wps_build_cred_ssid(struct wps_data *wps, struct wpabuf *msg)
244 {
245           wpa_printf(MSG_DEBUG, "WPS:  * SSID");
246           wpabuf_put_be16(msg, ATTR_SSID);
247           wpabuf_put_be16(msg, wps->wps->ssid_len);
248           wpabuf_put_data(msg, wps->wps->ssid, wps->wps->ssid_len);
249           return 0;
250 }
251 
252 
wps_build_cred_auth_type(struct wps_data * wps,struct wpabuf * msg)253 static int wps_build_cred_auth_type(struct wps_data *wps, struct wpabuf *msg)
254 {
255           u16 auth_type = wps->wps->ap_auth_type;
256 
257           /*
258            * Work around issues with Windows 7 WPS implementation not liking
259            * multiple Authentication Type bits in M7 AP Settings attribute by
260            * showing only the most secure option from current configuration.
261            */
262           if (auth_type & WPS_AUTH_WPA2PSK)
263                     auth_type = WPS_AUTH_WPA2PSK;
264           else if (auth_type & WPS_AUTH_WPAPSK)
265                     auth_type = WPS_AUTH_WPAPSK;
266           else if (auth_type & WPS_AUTH_OPEN)
267                     auth_type = WPS_AUTH_OPEN;
268 
269           wpa_printf(MSG_DEBUG, "WPS:  * Authentication Type (0x%x)", auth_type);
270           wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
271           wpabuf_put_be16(msg, 2);
272           wpabuf_put_be16(msg, auth_type);
273           return 0;
274 }
275 
276 
wps_build_cred_encr_type(struct wps_data * wps,struct wpabuf * msg)277 static int wps_build_cred_encr_type(struct wps_data *wps, struct wpabuf *msg)
278 {
279           u16 encr_type = wps->wps->ap_encr_type;
280 
281           /*
282            * Work around issues with Windows 7 WPS implementation not liking
283            * multiple Encryption Type bits in M7 AP Settings attribute by
284            * showing only the most secure option from current configuration.
285            */
286           if (wps->wps->ap_auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK)) {
287                     if (encr_type & WPS_ENCR_AES)
288                               encr_type = WPS_ENCR_AES;
289                     else if (encr_type & WPS_ENCR_TKIP)
290                               encr_type = WPS_ENCR_TKIP;
291           }
292 
293           wpa_printf(MSG_DEBUG, "WPS:  * Encryption Type (0x%x)", encr_type);
294           wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
295           wpabuf_put_be16(msg, 2);
296           wpabuf_put_be16(msg, encr_type);
297           return 0;
298 }
299 
300 
wps_build_cred_network_key(struct wps_data * wps,struct wpabuf * msg)301 static int wps_build_cred_network_key(struct wps_data *wps, struct wpabuf *msg)
302 {
303           if ((wps->wps->ap_auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) &&
304               wps->wps->network_key_len == 0) {
305                     char hex[65];
306                     u8 psk[32];
307                     /* Generate a random per-device PSK */
308                     if (random_pool_ready() != 1 ||
309                         random_get_bytes(psk, sizeof(psk)) < 0) {
310                               wpa_printf(MSG_INFO,
311                                            "WPS: Could not generate random PSK");
312                               return -1;
313                     }
314                     wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
315                                         psk, sizeof(psk));
316                     wpa_printf(MSG_DEBUG, "WPS:  * Network Key (len=%u)",
317                                  (unsigned int) wps->new_psk_len * 2);
318                     wpa_snprintf_hex(hex, sizeof(hex), psk, sizeof(psk));
319                     wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
320                     wpabuf_put_be16(msg, sizeof(psk) * 2);
321                     wpabuf_put_data(msg, hex, sizeof(psk) * 2);
322                     if (wps->wps->registrar) {
323                               wps_cb_new_psk(wps->wps->registrar,
324                                                wps->peer_dev.mac_addr,
325                                                wps->p2p_dev_addr, psk, sizeof(psk));
326                     }
327                     return 0;
328           }
329 
330           wpa_printf(MSG_DEBUG, "WPS:  * Network Key (len=%u)",
331                        (unsigned int) wps->wps->network_key_len);
332           wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
333           wpabuf_put_be16(msg, wps->wps->network_key_len);
334           wpabuf_put_data(msg, wps->wps->network_key, wps->wps->network_key_len);
335           return 0;
336 }
337 
338 
wps_build_cred_mac_addr(struct wps_data * wps,struct wpabuf * msg)339 static int wps_build_cred_mac_addr(struct wps_data *wps, struct wpabuf *msg)
340 {
341           wpa_printf(MSG_DEBUG, "WPS:  * MAC Address (AP BSSID)");
342           wpabuf_put_be16(msg, ATTR_MAC_ADDR);
343           wpabuf_put_be16(msg, ETH_ALEN);
344           wpabuf_put_data(msg, wps->wps->dev.mac_addr, ETH_ALEN);
345           return 0;
346 }
347 
348 
wps_build_ap_settings(struct wps_data * wps,struct wpabuf * plain)349 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *plain)
350 {
351           const u8 *start, *end;
352           int ret;
353 
354           if (wps->wps->ap_settings) {
355                     wpa_printf(MSG_DEBUG, "WPS:  * AP Settings (pre-configured)");
356                     wpabuf_put_data(plain, wps->wps->ap_settings,
357                                         wps->wps->ap_settings_len);
358                     return 0;
359           }
360 
361           wpa_printf(MSG_DEBUG, "WPS:  * AP Settings based on current configuration");
362           start = wpabuf_put(plain, 0);
363           ret = wps_build_cred_ssid(wps, plain) ||
364                     wps_build_cred_mac_addr(wps, plain) ||
365                     wps_build_cred_auth_type(wps, plain) ||
366                     wps_build_cred_encr_type(wps, plain) ||
367                     wps_build_cred_network_key(wps, plain);
368           end = wpabuf_put(plain, 0);
369 
370           wpa_hexdump_key(MSG_DEBUG, "WPS: Plaintext AP Settings",
371                               start, end - start);
372 
373           return ret;
374 }
375 
376 
wps_build_m7(struct wps_data * wps)377 static struct wpabuf * wps_build_m7(struct wps_data *wps)
378 {
379           struct wpabuf *msg, *plain;
380 
381           wpa_printf(MSG_DEBUG, "WPS: Building Message M7");
382 
383           plain = wpabuf_alloc(500 + wps->wps->ap_settings_len);
384           if (plain == NULL)
385                     return NULL;
386 
387           msg = wpabuf_alloc(1000 + wps->wps->ap_settings_len);
388           if (msg == NULL) {
389                     wpabuf_free(plain);
390                     return NULL;
391           }
392 
393           if (wps_build_version(msg) ||
394               wps_build_msg_type(msg, WPS_M7) ||
395               wps_build_registrar_nonce(wps, msg) ||
396               wps_build_e_snonce2(wps, plain) ||
397               (wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
398               wps_build_key_wrap_auth(wps, plain) ||
399               wps_build_encr_settings(wps, msg, plain) ||
400               wps_build_wfa_ext(msg, 0, NULL, 0, 0) ||
401               wps_build_authenticator(wps, msg)) {
402                     wpabuf_clear_free(plain);
403                     wpabuf_free(msg);
404                     return NULL;
405           }
406           wpabuf_clear_free(plain);
407 
408           if (wps->wps->ap && wps->wps->registrar) {
409                     /*
410                      * If the Registrar is only learning our current configuration,
411                      * it may not continue protocol run to successful completion.
412                      * Store information here to make sure it remains available.
413                      */
414                     wps_device_store(wps->wps->registrar, &wps->peer_dev,
415                                          wps->uuid_r);
416           }
417 
418           wps->state = RECV_M8;
419           return msg;
420 }
421 
422 
wps_build_wsc_done(struct wps_data * wps)423 static struct wpabuf * wps_build_wsc_done(struct wps_data *wps)
424 {
425           struct wpabuf *msg;
426 
427           wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_Done");
428 
429           msg = wpabuf_alloc(1000);
430           if (msg == NULL)
431                     return NULL;
432 
433           if (wps_build_version(msg) ||
434               wps_build_msg_type(msg, WPS_WSC_DONE) ||
435               wps_build_enrollee_nonce(wps, msg) ||
436               wps_build_registrar_nonce(wps, msg) ||
437               wps_build_wfa_ext(msg, 0, NULL, 0, 0)) {
438                     wpabuf_free(msg);
439                     return NULL;
440           }
441 
442           if (wps->wps->ap)
443                     wps->state = RECV_ACK;
444           else {
445                     wps_success_event(wps->wps, wps->peer_dev.mac_addr);
446                     wps->state = WPS_FINISHED;
447           }
448           return msg;
449 }
450 
451 
wps_enrollee_get_msg(struct wps_data * wps,enum wsc_op_code * op_code)452 struct wpabuf * wps_enrollee_get_msg(struct wps_data *wps,
453                                              enum wsc_op_code *op_code)
454 {
455           struct wpabuf *msg;
456 
457           switch (wps->state) {
458           case SEND_M1:
459                     msg = wps_build_m1(wps);
460                     *op_code = WSC_MSG;
461                     break;
462           case SEND_M3:
463                     msg = wps_build_m3(wps);
464                     *op_code = WSC_MSG;
465                     break;
466           case SEND_M5:
467                     msg = wps_build_m5(wps);
468                     *op_code = WSC_MSG;
469                     break;
470           case SEND_M7:
471                     msg = wps_build_m7(wps);
472                     *op_code = WSC_MSG;
473                     break;
474           case RECEIVED_M2D:
475                     if (wps->wps->ap) {
476                               msg = wps_build_wsc_nack(wps);
477                               *op_code = WSC_NACK;
478                               break;
479                     }
480                     msg = wps_build_wsc_ack(wps);
481                     *op_code = WSC_ACK;
482                     if (msg) {
483                               /* Another M2/M2D may be received */
484                               wps->state = RECV_M2;
485                     }
486                     break;
487           case SEND_WSC_NACK:
488                     msg = wps_build_wsc_nack(wps);
489                     *op_code = WSC_NACK;
490                     break;
491           case WPS_MSG_DONE:
492                     msg = wps_build_wsc_done(wps);
493                     *op_code = WSC_Done;
494                     break;
495           default:
496                     wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
497                                  "a message", wps->state);
498                     msg = NULL;
499                     break;
500           }
501 
502           if (*op_code == WSC_MSG && msg) {
503                     /* Save a copy of the last message for Authenticator derivation
504                      */
505                     wpabuf_free(wps->last_msg);
506                     wps->last_msg = wpabuf_dup(msg);
507           }
508 
509           return msg;
510 }
511 
512 
wps_process_registrar_nonce(struct wps_data * wps,const u8 * r_nonce)513 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
514 {
515           if (r_nonce == NULL) {
516                     wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
517                     return -1;
518           }
519 
520           os_memcpy(wps->nonce_r, r_nonce, WPS_NONCE_LEN);
521           wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
522                         wps->nonce_r, WPS_NONCE_LEN);
523 
524           return 0;
525 }
526 
527 
wps_process_enrollee_nonce(struct wps_data * wps,const u8 * e_nonce)528 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
529 {
530           if (e_nonce == NULL) {
531                     wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
532                     return -1;
533           }
534 
535           if (os_memcmp(wps->nonce_e, e_nonce, WPS_NONCE_LEN) != 0) {
536                     wpa_printf(MSG_DEBUG, "WPS: Invalid Enrollee Nonce received");
537                     return -1;
538           }
539 
540           return 0;
541 }
542 
543 
wps_process_uuid_r(struct wps_data * wps,const u8 * uuid_r)544 static int wps_process_uuid_r(struct wps_data *wps, const u8 *uuid_r)
545 {
546           if (uuid_r == NULL) {
547                     wpa_printf(MSG_DEBUG, "WPS: No UUID-R received");
548                     return -1;
549           }
550 
551           os_memcpy(wps->uuid_r, uuid_r, WPS_UUID_LEN);
552           wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
553 
554           return 0;
555 }
556 
557 
wps_process_pubkey(struct wps_data * wps,const u8 * pk,size_t pk_len)558 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
559                                     size_t pk_len)
560 {
561           if (pk == NULL || pk_len == 0) {
562                     wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
563                     return -1;
564           }
565 
566           if (wps->peer_pubkey_hash_set) {
567                     u8 hash[WPS_HASH_LEN];
568                     sha256_vector(1, &pk, &pk_len, hash);
569                     if (os_memcmp_const(hash, wps->peer_pubkey_hash,
570                                             WPS_OOB_PUBKEY_HASH_LEN) != 0) {
571                               wpa_printf(MSG_ERROR, "WPS: Public Key hash mismatch");
572                               wpa_hexdump(MSG_DEBUG, "WPS: Received public key",
573                                             pk, pk_len);
574                               wpa_hexdump(MSG_DEBUG, "WPS: Calculated public key "
575                                             "hash", hash, WPS_OOB_PUBKEY_HASH_LEN);
576                               wpa_hexdump(MSG_DEBUG, "WPS: Expected public key hash",
577                                             wps->peer_pubkey_hash,
578                                             WPS_OOB_PUBKEY_HASH_LEN);
579                               wps->config_error = WPS_CFG_PUBLIC_KEY_HASH_MISMATCH;
580                               return -1;
581                     }
582           }
583 
584           wpabuf_free(wps->dh_pubkey_r);
585           wps->dh_pubkey_r = wpabuf_alloc_copy(pk, pk_len);
586           if (wps->dh_pubkey_r == NULL)
587                     return -1;
588 
589           if (wps_derive_keys(wps) < 0)
590                     return -1;
591 
592           return 0;
593 }
594 
595 
wps_process_r_hash1(struct wps_data * wps,const u8 * r_hash1)596 static int wps_process_r_hash1(struct wps_data *wps, const u8 *r_hash1)
597 {
598           if (r_hash1 == NULL) {
599                     wpa_printf(MSG_DEBUG, "WPS: No R-Hash1 received");
600                     return -1;
601           }
602 
603           os_memcpy(wps->peer_hash1, r_hash1, WPS_HASH_LEN);
604           wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", wps->peer_hash1, WPS_HASH_LEN);
605 
606           return 0;
607 }
608 
609 
wps_process_r_hash2(struct wps_data * wps,const u8 * r_hash2)610 static int wps_process_r_hash2(struct wps_data *wps, const u8 *r_hash2)
611 {
612           if (r_hash2 == NULL) {
613                     wpa_printf(MSG_DEBUG, "WPS: No R-Hash2 received");
614                     return -1;
615           }
616 
617           os_memcpy(wps->peer_hash2, r_hash2, WPS_HASH_LEN);
618           wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", wps->peer_hash2, WPS_HASH_LEN);
619 
620           return 0;
621 }
622 
623 
wps_process_r_snonce1(struct wps_data * wps,const u8 * r_snonce1)624 static int wps_process_r_snonce1(struct wps_data *wps, const u8 *r_snonce1)
625 {
626           u8 hash[SHA256_MAC_LEN];
627           const u8 *addr[4];
628           size_t len[4];
629 
630           if (r_snonce1 == NULL) {
631                     wpa_printf(MSG_DEBUG, "WPS: No R-SNonce1 received");
632                     return -1;
633           }
634 
635           wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce1", r_snonce1,
636                               WPS_SECRET_NONCE_LEN);
637 
638           /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
639           addr[0] = r_snonce1;
640           len[0] = WPS_SECRET_NONCE_LEN;
641           addr[1] = wps->psk1;
642           len[1] = WPS_PSK_LEN;
643           addr[2] = wpabuf_head(wps->dh_pubkey_e);
644           len[2] = wpabuf_len(wps->dh_pubkey_e);
645           addr[3] = wpabuf_head(wps->dh_pubkey_r);
646           len[3] = wpabuf_len(wps->dh_pubkey_r);
647           hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
648 
649           if (os_memcmp_const(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
650                     wpa_printf(MSG_DEBUG, "WPS: R-Hash1 derived from R-S1 does "
651                                  "not match with the pre-committed value");
652                     wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
653                     wps_pwd_auth_fail_event(wps->wps, 1, 1, wps->peer_dev.mac_addr);
654                     return -1;
655           }
656 
657           wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the first "
658                        "half of the device password");
659 
660           return 0;
661 }
662 
663 
wps_process_r_snonce2(struct wps_data * wps,const u8 * r_snonce2)664 static int wps_process_r_snonce2(struct wps_data *wps, const u8 *r_snonce2)
665 {
666           u8 hash[SHA256_MAC_LEN];
667           const u8 *addr[4];
668           size_t len[4];
669 
670           if (r_snonce2 == NULL) {
671                     wpa_printf(MSG_DEBUG, "WPS: No R-SNonce2 received");
672                     return -1;
673           }
674 
675           wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce2", r_snonce2,
676                               WPS_SECRET_NONCE_LEN);
677 
678           /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
679           addr[0] = r_snonce2;
680           len[0] = WPS_SECRET_NONCE_LEN;
681           addr[1] = wps->psk2;
682           len[1] = WPS_PSK_LEN;
683           addr[2] = wpabuf_head(wps->dh_pubkey_e);
684           len[2] = wpabuf_len(wps->dh_pubkey_e);
685           addr[3] = wpabuf_head(wps->dh_pubkey_r);
686           len[3] = wpabuf_len(wps->dh_pubkey_r);
687           hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
688 
689           if (os_memcmp_const(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
690                     wpa_printf(MSG_DEBUG, "WPS: R-Hash2 derived from R-S2 does "
691                                  "not match with the pre-committed value");
692                     wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
693                     wps_pwd_auth_fail_event(wps->wps, 1, 2, wps->peer_dev.mac_addr);
694                     return -1;
695           }
696 
697           wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the second "
698                        "half of the device password");
699 
700           return 0;
701 }
702 
703 
wps_process_cred_e(struct wps_data * wps,const u8 * cred,size_t cred_len,int wps2)704 static int wps_process_cred_e(struct wps_data *wps, const u8 *cred,
705                                     size_t cred_len, int wps2)
706 {
707           struct wps_parse_attr attr;
708           struct wpabuf msg;
709           int ret = 0;
710 
711           wpa_printf(MSG_DEBUG, "WPS: Received Credential");
712           os_memset(&wps->cred, 0, sizeof(wps->cred));
713           wpabuf_set(&msg, cred, cred_len);
714           if (wps_parse_msg(&msg, &attr) < 0 ||
715               wps_process_cred(&attr, &wps->cred))
716                     return -1;
717 
718           if (os_memcmp(wps->cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN) !=
719               0) {
720                     wpa_printf(MSG_DEBUG, "WPS: MAC Address in the Credential ("
721                                  MACSTR ") does not match with own address (" MACSTR
722                                  ")", MAC2STR(wps->cred.mac_addr),
723                                  MAC2STR(wps->wps->dev.mac_addr));
724                     /*
725                      * In theory, this could be consider fatal error, but there are
726                      * number of deployed implementations using other address here
727                      * due to unclarity in the specification. For interoperability
728                      * reasons, allow this to be processed since we do not really
729                      * use the MAC Address information for anything.
730                      */
731 #ifdef CONFIG_WPS_STRICT
732                     if (wps2) {
733                               wpa_printf(MSG_INFO, "WPS: Do not accept incorrect "
734                                            "MAC Address in AP Settings");
735                               return -1;
736                     }
737 #endif /* CONFIG_WPS_STRICT */
738           }
739 
740           if (!(wps->cred.encr_type &
741                 (WPS_ENCR_NONE | WPS_ENCR_TKIP | WPS_ENCR_AES))) {
742                     if (wps->cred.encr_type & WPS_ENCR_WEP) {
743                               wpa_printf(MSG_INFO, "WPS: Reject Credential "
744                                            "due to WEP configuration");
745                               wps->error_indication = WPS_EI_SECURITY_WEP_PROHIBITED;
746                               return -2;
747                     }
748 
749                     wpa_printf(MSG_INFO, "WPS: Reject Credential due to "
750                                  "invalid encr_type 0x%x", wps->cred.encr_type);
751                     return -1;
752           }
753 
754           if (wps->wps->cred_cb) {
755                     wps->cred.cred_attr = cred - 4;
756                     wps->cred.cred_attr_len = cred_len + 4;
757                     ret = wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
758                     wps->cred.cred_attr = NULL;
759                     wps->cred.cred_attr_len = 0;
760           }
761 
762           return ret;
763 }
764 
765 
wps_process_creds(struct wps_data * wps,const u8 * cred[],u16 cred_len[],unsigned int num_cred,int wps2)766 static int wps_process_creds(struct wps_data *wps, const u8 *cred[],
767                                    u16 cred_len[], unsigned int num_cred, int wps2)
768 {
769           size_t i;
770           int ok = 0;
771 
772           if (wps->wps->ap)
773                     return 0;
774 
775           if (num_cred == 0) {
776                     wpa_printf(MSG_DEBUG, "WPS: No Credential attributes "
777                                  "received");
778                     return -1;
779           }
780 
781           for (i = 0; i < num_cred; i++) {
782                     int res;
783                     res = wps_process_cred_e(wps, cred[i], cred_len[i], wps2);
784                     if (res == 0)
785                               ok++;
786                     else if (res == -2)
787                               wpa_printf(MSG_DEBUG, "WPS: WEP credential skipped");
788                     else
789                               return -1;
790           }
791 
792           if (ok == 0) {
793                     wpa_printf(MSG_DEBUG, "WPS: No valid Credential attribute "
794                                  "received");
795                     return -1;
796           }
797 
798           return 0;
799 }
800 
801 
wps_process_ap_settings_e(struct wps_data * wps,struct wps_parse_attr * attr,struct wpabuf * attrs,int wps2)802 static int wps_process_ap_settings_e(struct wps_data *wps,
803                                              struct wps_parse_attr *attr,
804                                              struct wpabuf *attrs, int wps2)
805 {
806           struct wps_credential cred;
807           int ret = 0;
808 
809           if (!wps->wps->ap)
810                     return 0;
811 
812           if (wps_process_ap_settings(attr, &cred) < 0)
813                     return -1;
814 
815           wpa_printf(MSG_INFO, "WPS: Received new AP configuration from "
816                        "Registrar");
817 
818           if (os_memcmp(cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN) !=
819               0) {
820                     wpa_printf(MSG_DEBUG, "WPS: MAC Address in the AP Settings ("
821                                  MACSTR ") does not match with own address (" MACSTR
822                                  ")", MAC2STR(cred.mac_addr),
823                                  MAC2STR(wps->wps->dev.mac_addr));
824                     /*
825                      * In theory, this could be consider fatal error, but there are
826                      * number of deployed implementations using other address here
827                      * due to unclarity in the specification. For interoperability
828                      * reasons, allow this to be processed since we do not really
829                      * use the MAC Address information for anything.
830                      */
831 #ifdef CONFIG_WPS_STRICT
832                     if (wps2) {
833                               wpa_printf(MSG_INFO, "WPS: Do not accept incorrect "
834                                            "MAC Address in AP Settings");
835                               return -1;
836                     }
837 #endif /* CONFIG_WPS_STRICT */
838           }
839 
840           if (!(cred.encr_type & (WPS_ENCR_NONE | WPS_ENCR_TKIP | WPS_ENCR_AES)))
841           {
842                     if (cred.encr_type & WPS_ENCR_WEP) {
843                               wpa_printf(MSG_INFO, "WPS: Reject new AP settings "
844                                            "due to WEP configuration");
845                               wps->error_indication = WPS_EI_SECURITY_WEP_PROHIBITED;
846                               return -1;
847                     }
848 
849                     wpa_printf(MSG_INFO, "WPS: Reject new AP settings due to "
850                                  "invalid encr_type 0x%x", cred.encr_type);
851                     return -1;
852           }
853 
854 #ifdef CONFIG_WPS_STRICT
855           if (wps2) {
856                     if ((cred.encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) ==
857                         WPS_ENCR_TKIP ||
858                         (cred.auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
859                         WPS_AUTH_WPAPSK) {
860                               wpa_printf(MSG_INFO, "WPS-STRICT: Invalid WSC 2.0 "
861                                            "AP Settings: WPA-Personal/TKIP only");
862                               wps->error_indication =
863                                         WPS_EI_SECURITY_TKIP_ONLY_PROHIBITED;
864                               return -1;
865                     }
866           }
867 #endif /* CONFIG_WPS_STRICT */
868 
869           if ((cred.encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) == WPS_ENCR_TKIP)
870           {
871                     wpa_printf(MSG_DEBUG, "WPS: Upgrade encr_type TKIP -> "
872                                  "TKIP+AES");
873                     cred.encr_type |= WPS_ENCR_AES;
874           }
875 
876           if ((cred.auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
877               WPS_AUTH_WPAPSK) {
878                     wpa_printf(MSG_DEBUG, "WPS: Upgrade auth_type WPAPSK -> "
879                                  "WPAPSK+WPA2PSK");
880                     cred.auth_type |= WPS_AUTH_WPA2PSK;
881           }
882 
883           if (wps->wps->cred_cb) {
884                     cred.cred_attr = wpabuf_head(attrs);
885                     cred.cred_attr_len = wpabuf_len(attrs);
886                     ret = wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
887           }
888 
889           return ret;
890 }
891 
892 
wps_process_dev_pw_id(struct wps_data * wps,const u8 * dev_pw_id)893 static int wps_process_dev_pw_id(struct wps_data *wps, const u8 *dev_pw_id)
894 {
895           u16 id;
896 
897           if (dev_pw_id == NULL) {
898                     wpa_printf(MSG_DEBUG, "WPS: Device Password ID");
899                     return -1;
900           }
901 
902           id = WPA_GET_BE16(dev_pw_id);
903           if (wps->dev_pw_id == id) {
904                     wpa_printf(MSG_DEBUG, "WPS: Device Password ID %u", id);
905                     return 0;
906           }
907 
908 #ifdef CONFIG_P2P
909           if ((id == DEV_PW_DEFAULT &&
910                wps->dev_pw_id == DEV_PW_REGISTRAR_SPECIFIED) ||
911               (id == DEV_PW_REGISTRAR_SPECIFIED &&
912                wps->dev_pw_id == DEV_PW_DEFAULT)) {
913                     /*
914                      * Common P2P use cases indicate whether the PIN is from the
915                      * client or GO using Device Password Id in M1/M2 in a way that
916                      * does not look fully compliant with WSC specification. Anyway,
917                      * this is deployed and needs to be allowed, so ignore changes
918                      * between Registrar-Specified and Default PIN.
919                      */
920                     wpa_printf(MSG_DEBUG, "WPS: Allow PIN Device Password ID "
921                                  "change");
922                     return 0;
923           }
924 #endif /* CONFIG_P2P */
925 
926           wpa_printf(MSG_DEBUG, "WPS: Registrar trying to change Device Password "
927                        "ID from %u to %u", wps->dev_pw_id, id);
928 
929           if (wps->dev_pw_id == DEV_PW_PUSHBUTTON && id == DEV_PW_DEFAULT) {
930                     wpa_printf(MSG_DEBUG,
931                                  "WPS: Workaround - ignore PBC-to-PIN change");
932                     return 0;
933           }
934 
935           if (wps->alt_dev_password && wps->alt_dev_pw_id == id) {
936                     wpa_printf(MSG_DEBUG, "WPS: Found a matching Device Password");
937                     bin_clear_free(wps->dev_password, wps->dev_password_len);
938                     wps->dev_pw_id = wps->alt_dev_pw_id;
939                     wps->dev_password = wps->alt_dev_password;
940                     wps->dev_password_len = wps->alt_dev_password_len;
941                     wps->alt_dev_password = NULL;
942                     wps->alt_dev_password_len = 0;
943                     return 0;
944           }
945 
946           return -1;
947 }
948 
949 
wps_process_m2(struct wps_data * wps,const struct wpabuf * msg,struct wps_parse_attr * attr)950 static enum wps_process_res wps_process_m2(struct wps_data *wps,
951                                                      const struct wpabuf *msg,
952                                                      struct wps_parse_attr *attr)
953 {
954           wpa_printf(MSG_DEBUG, "WPS: Received M2");
955 
956           if (wps->state != RECV_M2) {
957                     wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
958                                  "receiving M2", wps->state);
959                     wps->state = SEND_WSC_NACK;
960                     return WPS_CONTINUE;
961           }
962 
963           if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
964               wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
965               wps_process_uuid_r(wps, attr->uuid_r) ||
966               wps_process_dev_pw_id(wps, attr->dev_password_id)) {
967                     wps->state = SEND_WSC_NACK;
968                     return WPS_CONTINUE;
969           }
970 
971           /*
972            * Stop here on an AP as an Enrollee if AP Setup is locked unless the
973            * special locked mode is used to allow protocol run up to M7 in order
974            * to support external Registrars that only learn the current AP
975            * configuration without changing it.
976            */
977           if (wps->wps->ap &&
978               ((wps->wps->ap_setup_locked && wps->wps->ap_setup_locked != 2) ||
979                wps->dev_password == NULL)) {
980                     wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse "
981                                  "registration of a new Registrar");
982                     wps->config_error = WPS_CFG_SETUP_LOCKED;
983                     wps->state = SEND_WSC_NACK;
984                     return WPS_CONTINUE;
985           }
986 
987           if (wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
988               wps_process_authenticator(wps, attr->authenticator, msg) ||
989               wps_process_device_attrs(&wps->peer_dev, attr)) {
990                     wps->state = SEND_WSC_NACK;
991                     return WPS_CONTINUE;
992           }
993 
994 #ifdef CONFIG_WPS_NFC
995           if (wps->peer_pubkey_hash_set) {
996                     struct wpabuf *decrypted;
997                     struct wps_parse_attr eattr;
998 
999                     decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1000                                                                   attr->encr_settings_len);
1001                     if (decrypted == NULL) {
1002                               wpa_printf(MSG_DEBUG, "WPS: Failed to decrypt "
1003                                            "Encrypted Settings attribute");
1004                               wps->state = SEND_WSC_NACK;
1005                               return WPS_CONTINUE;
1006                     }
1007 
1008                     wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted "
1009                                  "Settings attribute");
1010                     if (wps_parse_msg(decrypted, &eattr) < 0 ||
1011                         wps_process_key_wrap_auth(wps, decrypted,
1012                                                         eattr.key_wrap_auth) ||
1013                         wps_process_creds(wps, eattr.cred, eattr.cred_len,
1014                                               eattr.num_cred, attr->version2 != NULL)) {
1015                               wpabuf_clear_free(decrypted);
1016                               wps->state = SEND_WSC_NACK;
1017                               return WPS_CONTINUE;
1018                     }
1019                     wpabuf_clear_free(decrypted);
1020 
1021                     wps->state = WPS_MSG_DONE;
1022                     return WPS_CONTINUE;
1023           }
1024 #endif /* CONFIG_WPS_NFC */
1025 
1026           wps->state = SEND_M3;
1027           return WPS_CONTINUE;
1028 }
1029 
1030 
wps_process_m2d(struct wps_data * wps,struct wps_parse_attr * attr)1031 static enum wps_process_res wps_process_m2d(struct wps_data *wps,
1032                                                       struct wps_parse_attr *attr)
1033 {
1034           wpa_printf(MSG_DEBUG, "WPS: Received M2D");
1035 
1036           if (wps->state != RECV_M2) {
1037                     wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1038                                  "receiving M2D", wps->state);
1039                     wps->state = SEND_WSC_NACK;
1040                     return WPS_CONTINUE;
1041           }
1042 
1043           wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer",
1044                                 attr->manufacturer, attr->manufacturer_len);
1045           wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name",
1046                                 attr->model_name, attr->model_name_len);
1047           wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number",
1048                                 attr->model_number, attr->model_number_len);
1049           wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number",
1050                                 attr->serial_number, attr->serial_number_len);
1051           wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name",
1052                                 attr->dev_name, attr->dev_name_len);
1053 
1054           if (wps->wps->event_cb) {
1055                     union wps_event_data data;
1056                     struct wps_event_m2d *m2d = &data.m2d;
1057                     os_memset(&data, 0, sizeof(data));
1058                     if (attr->config_methods)
1059                               m2d->config_methods =
1060                                         WPA_GET_BE16(attr->config_methods);
1061                     m2d->manufacturer = attr->manufacturer;
1062                     m2d->manufacturer_len = attr->manufacturer_len;
1063                     m2d->model_name = attr->model_name;
1064                     m2d->model_name_len = attr->model_name_len;
1065                     m2d->model_number = attr->model_number;
1066                     m2d->model_number_len = attr->model_number_len;
1067                     m2d->serial_number = attr->serial_number;
1068                     m2d->serial_number_len = attr->serial_number_len;
1069                     m2d->dev_name = attr->dev_name;
1070                     m2d->dev_name_len = attr->dev_name_len;
1071                     m2d->primary_dev_type = attr->primary_dev_type;
1072                     if (attr->config_error)
1073                               m2d->config_error =
1074                                         WPA_GET_BE16(attr->config_error);
1075                     if (attr->dev_password_id)
1076                               m2d->dev_password_id =
1077                                         WPA_GET_BE16(attr->dev_password_id);
1078                     wps->wps->event_cb(wps->wps->cb_ctx, WPS_EV_M2D, &data);
1079           }
1080 
1081           wps->state = RECEIVED_M2D;
1082           return WPS_CONTINUE;
1083 }
1084 
1085 
wps_process_m4(struct wps_data * wps,const struct wpabuf * msg,struct wps_parse_attr * attr)1086 static enum wps_process_res wps_process_m4(struct wps_data *wps,
1087                                                      const struct wpabuf *msg,
1088                                                      struct wps_parse_attr *attr)
1089 {
1090           struct wpabuf *decrypted;
1091           struct wps_parse_attr eattr;
1092 
1093           wpa_printf(MSG_DEBUG, "WPS: Received M4");
1094 
1095           if (wps->state != RECV_M4) {
1096                     wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1097                                  "receiving M4", wps->state);
1098                     wps->state = SEND_WSC_NACK;
1099                     return WPS_CONTINUE;
1100           }
1101 
1102           if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1103               wps_process_authenticator(wps, attr->authenticator, msg) ||
1104               wps_process_r_hash1(wps, attr->r_hash1) ||
1105               wps_process_r_hash2(wps, attr->r_hash2)) {
1106                     wps->state = SEND_WSC_NACK;
1107                     return WPS_CONTINUE;
1108           }
1109 
1110           decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1111                                                         attr->encr_settings_len);
1112           if (decrypted == NULL) {
1113                     wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1114                                  "Settings attribute");
1115                     wps->state = SEND_WSC_NACK;
1116                     return WPS_CONTINUE;
1117           }
1118 
1119           if (wps_validate_m4_encr(decrypted, attr->version2 != NULL) < 0) {
1120                     wpabuf_clear_free(decrypted);
1121                     wps->state = SEND_WSC_NACK;
1122                     return WPS_CONTINUE;
1123           }
1124 
1125           wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1126                        "attribute");
1127           if (wps_parse_msg(decrypted, &eattr) < 0 ||
1128               wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1129               wps_process_r_snonce1(wps, eattr.r_snonce1)) {
1130                     wpabuf_clear_free(decrypted);
1131                     wps->state = SEND_WSC_NACK;
1132                     return WPS_CONTINUE;
1133           }
1134           wpabuf_clear_free(decrypted);
1135 
1136           wps->state = SEND_M5;
1137           return WPS_CONTINUE;
1138 }
1139 
1140 
wps_process_m6(struct wps_data * wps,const struct wpabuf * msg,struct wps_parse_attr * attr)1141 static enum wps_process_res wps_process_m6(struct wps_data *wps,
1142                                                      const struct wpabuf *msg,
1143                                                      struct wps_parse_attr *attr)
1144 {
1145           struct wpabuf *decrypted;
1146           struct wps_parse_attr eattr;
1147 
1148           wpa_printf(MSG_DEBUG, "WPS: Received M6");
1149 
1150           if (wps->state != RECV_M6) {
1151                     wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1152                                  "receiving M6", wps->state);
1153                     wps->state = SEND_WSC_NACK;
1154                     return WPS_CONTINUE;
1155           }
1156 
1157           if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1158               wps_process_authenticator(wps, attr->authenticator, msg)) {
1159                     wps->state = SEND_WSC_NACK;
1160                     return WPS_CONTINUE;
1161           }
1162 
1163           decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1164                                                         attr->encr_settings_len);
1165           if (decrypted == NULL) {
1166                     wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1167                                  "Settings attribute");
1168                     wps->state = SEND_WSC_NACK;
1169                     return WPS_CONTINUE;
1170           }
1171 
1172           if (wps_validate_m6_encr(decrypted, attr->version2 != NULL) < 0) {
1173                     wpabuf_clear_free(decrypted);
1174                     wps->state = SEND_WSC_NACK;
1175                     return WPS_CONTINUE;
1176           }
1177 
1178           wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1179                        "attribute");
1180           if (wps_parse_msg(decrypted, &eattr) < 0 ||
1181               wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1182               wps_process_r_snonce2(wps, eattr.r_snonce2)) {
1183                     wpabuf_clear_free(decrypted);
1184                     wps->state = SEND_WSC_NACK;
1185                     return WPS_CONTINUE;
1186           }
1187           wpabuf_clear_free(decrypted);
1188 
1189           if (wps->wps->ap)
1190                     wps->wps->event_cb(wps->wps->cb_ctx, WPS_EV_AP_PIN_SUCCESS,
1191                                            NULL);
1192 
1193           wps->state = SEND_M7;
1194           return WPS_CONTINUE;
1195 }
1196 
1197 
wps_process_m8(struct wps_data * wps,const struct wpabuf * msg,struct wps_parse_attr * attr)1198 static enum wps_process_res wps_process_m8(struct wps_data *wps,
1199                                                      const struct wpabuf *msg,
1200                                                      struct wps_parse_attr *attr)
1201 {
1202           struct wpabuf *decrypted;
1203           struct wps_parse_attr eattr;
1204 
1205           wpa_printf(MSG_DEBUG, "WPS: Received M8");
1206 
1207           if (wps->state != RECV_M8) {
1208                     wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1209                                  "receiving M8", wps->state);
1210                     wps->state = SEND_WSC_NACK;
1211                     return WPS_CONTINUE;
1212           }
1213 
1214           if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1215               wps_process_authenticator(wps, attr->authenticator, msg)) {
1216                     wps->state = SEND_WSC_NACK;
1217                     return WPS_CONTINUE;
1218           }
1219 
1220           if (wps->wps->ap && wps->wps->ap_setup_locked) {
1221                     /*
1222                      * Stop here if special ap_setup_locked == 2 mode allowed the
1223                      * protocol to continue beyond M2. This allows ER to learn the
1224                      * current AP settings without changing them.
1225                      */
1226                     wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse "
1227                                  "registration of a new Registrar");
1228                     wps->config_error = WPS_CFG_SETUP_LOCKED;
1229                     wps->state = SEND_WSC_NACK;
1230                     return WPS_CONTINUE;
1231           }
1232 
1233           decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1234                                                         attr->encr_settings_len);
1235           if (decrypted == NULL) {
1236                     wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1237                                  "Settings attribute");
1238                     wps->state = SEND_WSC_NACK;
1239                     return WPS_CONTINUE;
1240           }
1241 
1242           if (wps_validate_m8_encr(decrypted, wps->wps->ap,
1243                                          attr->version2 != NULL) < 0) {
1244                     wpabuf_clear_free(decrypted);
1245                     wps->state = SEND_WSC_NACK;
1246                     return WPS_CONTINUE;
1247           }
1248 
1249           wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1250                        "attribute");
1251           if (wps_parse_msg(decrypted, &eattr) < 0 ||
1252               wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1253               wps_process_creds(wps, eattr.cred, eattr.cred_len,
1254                                     eattr.num_cred, attr->version2 != NULL) ||
1255               wps_process_ap_settings_e(wps, &eattr, decrypted,
1256                                               attr->version2 != NULL)) {
1257                     wpabuf_clear_free(decrypted);
1258                     wps->state = SEND_WSC_NACK;
1259                     return WPS_CONTINUE;
1260           }
1261           wpabuf_clear_free(decrypted);
1262 
1263           wps->state = WPS_MSG_DONE;
1264           return WPS_CONTINUE;
1265 }
1266 
1267 
wps_process_wsc_msg(struct wps_data * wps,const struct wpabuf * msg)1268 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
1269                                                             const struct wpabuf *msg)
1270 {
1271           struct wps_parse_attr attr;
1272           enum wps_process_res ret = WPS_CONTINUE;
1273 
1274           wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
1275 
1276           if (wps_parse_msg(msg, &attr) < 0)
1277                     return WPS_FAILURE;
1278 
1279           if (attr.enrollee_nonce == NULL ||
1280               os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
1281                     wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1282                     return WPS_FAILURE;
1283           }
1284 
1285           if (attr.msg_type == NULL) {
1286                     wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1287                     wps->state = SEND_WSC_NACK;
1288                     return WPS_CONTINUE;
1289           }
1290 
1291           switch (*attr.msg_type) {
1292           case WPS_M2:
1293                     if (wps_validate_m2(msg) < 0)
1294                               return WPS_FAILURE;
1295                     ret = wps_process_m2(wps, msg, &attr);
1296                     break;
1297           case WPS_M2D:
1298                     if (wps_validate_m2d(msg) < 0)
1299                               return WPS_FAILURE;
1300                     ret = wps_process_m2d(wps, &attr);
1301                     break;
1302           case WPS_M4:
1303                     if (wps_validate_m4(msg) < 0)
1304                               return WPS_FAILURE;
1305                     ret = wps_process_m4(wps, msg, &attr);
1306                     if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1307                               wps_fail_event(wps->wps, WPS_M4, wps->config_error,
1308                                                wps->error_indication,
1309                                                wps->peer_dev.mac_addr);
1310                     break;
1311           case WPS_M6:
1312                     if (wps_validate_m6(msg) < 0)
1313                               return WPS_FAILURE;
1314                     ret = wps_process_m6(wps, msg, &attr);
1315                     if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1316                               wps_fail_event(wps->wps, WPS_M6, wps->config_error,
1317                                                wps->error_indication,
1318                                                wps->peer_dev.mac_addr);
1319                     break;
1320           case WPS_M8:
1321                     if (wps_validate_m8(msg) < 0)
1322                               return WPS_FAILURE;
1323                     ret = wps_process_m8(wps, msg, &attr);
1324                     if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1325                               wps_fail_event(wps->wps, WPS_M8, wps->config_error,
1326                                                wps->error_indication,
1327                                                wps->peer_dev.mac_addr);
1328                     break;
1329           default:
1330                     wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
1331                                  *attr.msg_type);
1332                     return WPS_FAILURE;
1333           }
1334 
1335           /*
1336            * Save a copy of the last message for Authenticator derivation if we
1337            * are continuing. However, skip M2D since it is not authenticated and
1338            * neither is the ACK/NACK response frame. This allows the possibly
1339            * following M2 to be processed correctly by using the previously sent
1340            * M1 in Authenticator derivation.
1341            */
1342           if (ret == WPS_CONTINUE && *attr.msg_type != WPS_M2D) {
1343                     /* Save a copy of the last message for Authenticator derivation
1344                      */
1345                     wpabuf_free(wps->last_msg);
1346                     wps->last_msg = wpabuf_dup(msg);
1347           }
1348 
1349           return ret;
1350 }
1351 
1352 
wps_process_wsc_ack(struct wps_data * wps,const struct wpabuf * msg)1353 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
1354                                                             const struct wpabuf *msg)
1355 {
1356           struct wps_parse_attr attr;
1357 
1358           wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
1359 
1360           if (wps_parse_msg(msg, &attr) < 0)
1361                     return WPS_FAILURE;
1362 
1363           if (attr.msg_type == NULL) {
1364                     wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1365                     return WPS_FAILURE;
1366           }
1367 
1368           if (*attr.msg_type != WPS_WSC_ACK) {
1369                     wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1370                                  *attr.msg_type);
1371                     return WPS_FAILURE;
1372           }
1373 
1374           if (attr.registrar_nonce == NULL ||
1375               os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
1376           {
1377                     wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1378                     return WPS_FAILURE;
1379           }
1380 
1381           if (attr.enrollee_nonce == NULL ||
1382               os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
1383                     wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1384                     return WPS_FAILURE;
1385           }
1386 
1387           if (wps->state == RECV_ACK && wps->wps->ap) {
1388                     wpa_printf(MSG_DEBUG, "WPS: External Registrar registration "
1389                                  "completed successfully");
1390                     wps_success_event(wps->wps, wps->peer_dev.mac_addr);
1391                     wps->state = WPS_FINISHED;
1392                     return WPS_DONE;
1393           }
1394 
1395           return WPS_FAILURE;
1396 }
1397 
1398 
wps_process_wsc_nack(struct wps_data * wps,const struct wpabuf * msg)1399 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1400                                                              const struct wpabuf *msg)
1401 {
1402           struct wps_parse_attr attr;
1403           u16 config_error;
1404 
1405           wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1406 
1407           if (wps_parse_msg(msg, &attr) < 0)
1408                     return WPS_FAILURE;
1409 
1410           if (attr.msg_type == NULL) {
1411                     wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1412                     return WPS_FAILURE;
1413           }
1414 
1415           if (*attr.msg_type != WPS_WSC_NACK) {
1416                     wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1417                                  *attr.msg_type);
1418                     return WPS_FAILURE;
1419           }
1420 
1421           if (attr.registrar_nonce == NULL ||
1422               os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
1423           {
1424                     wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1425                     wpa_hexdump(MSG_DEBUG, "WPS: Received Registrar Nonce",
1426                                   attr.registrar_nonce, WPS_NONCE_LEN);
1427                     wpa_hexdump(MSG_DEBUG, "WPS: Expected Registrar Nonce",
1428                                   wps->nonce_r, WPS_NONCE_LEN);
1429                     return WPS_FAILURE;
1430           }
1431 
1432           if (attr.enrollee_nonce == NULL ||
1433               os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
1434                     wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1435                     wpa_hexdump(MSG_DEBUG, "WPS: Received Enrollee Nonce",
1436                                   attr.enrollee_nonce, WPS_NONCE_LEN);
1437                     wpa_hexdump(MSG_DEBUG, "WPS: Expected Enrollee Nonce",
1438                                   wps->nonce_e, WPS_NONCE_LEN);
1439                     return WPS_FAILURE;
1440           }
1441 
1442           if (attr.config_error == NULL) {
1443                     wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1444                                  "in WSC_NACK");
1445                     return WPS_FAILURE;
1446           }
1447 
1448           config_error = WPA_GET_BE16(attr.config_error);
1449           wpa_printf(MSG_DEBUG, "WPS: Registrar terminated negotiation with "
1450                        "Configuration Error %d", config_error);
1451 
1452           switch (wps->state) {
1453           case RECV_M4:
1454                     wps_fail_event(wps->wps, WPS_M3, config_error,
1455                                      wps->error_indication, wps->peer_dev.mac_addr);
1456                     break;
1457           case RECV_M6:
1458                     wps_fail_event(wps->wps, WPS_M5, config_error,
1459                                      wps->error_indication, wps->peer_dev.mac_addr);
1460                     break;
1461           case RECV_M8:
1462                     wps_fail_event(wps->wps, WPS_M7, config_error,
1463                                      wps->error_indication, wps->peer_dev.mac_addr);
1464                     break;
1465           default:
1466                     break;
1467           }
1468 
1469           /* Followed by NACK if Enrollee is Supplicant or EAP-Failure if
1470            * Enrollee is Authenticator */
1471           wps->state = SEND_WSC_NACK;
1472 
1473           return WPS_FAILURE;
1474 }
1475 
1476 
wps_enrollee_process_msg(struct wps_data * wps,enum wsc_op_code op_code,const struct wpabuf * msg)1477 enum wps_process_res wps_enrollee_process_msg(struct wps_data *wps,
1478                                                         enum wsc_op_code op_code,
1479                                                         const struct wpabuf *msg)
1480 {
1481 
1482           wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
1483                        "op_code=%d)",
1484                        (unsigned long) wpabuf_len(msg), op_code);
1485 
1486           if (op_code == WSC_UPnP) {
1487                     /* Determine the OpCode based on message type attribute */
1488                     struct wps_parse_attr attr;
1489                     if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type) {
1490                               if (*attr.msg_type == WPS_WSC_ACK)
1491                                         op_code = WSC_ACK;
1492                               else if (*attr.msg_type == WPS_WSC_NACK)
1493                                         op_code = WSC_NACK;
1494                     }
1495           }
1496 
1497           switch (op_code) {
1498           case WSC_MSG:
1499           case WSC_UPnP:
1500                     return wps_process_wsc_msg(wps, msg);
1501           case WSC_ACK:
1502                     if (wps_validate_wsc_ack(msg) < 0)
1503                               return WPS_FAILURE;
1504                     return wps_process_wsc_ack(wps, msg);
1505           case WSC_NACK:
1506                     if (wps_validate_wsc_nack(msg) < 0)
1507                               return WPS_FAILURE;
1508                     return wps_process_wsc_nack(wps, msg);
1509           default:
1510                     wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
1511                     return WPS_FAILURE;
1512           }
1513 }
1514