WPS NFC: Validate peer public key hash on Enrollee
[mech_eap.git] / src / eap_peer / eap_wsc.c
1 /*
2  * EAP-WSC peer for Wi-Fi Protected Setup
3  * Copyright (c) 2007-2009, 2012, 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 "uuid.h"
13 #include "eap_i.h"
14 #include "eap_common/eap_wsc_common.h"
15 #include "wps/wps.h"
16 #include "wps/wps_defs.h"
17
18
19 struct eap_wsc_data {
20         enum { WAIT_START, MESG, FRAG_ACK, WAIT_FRAG_ACK, DONE, FAIL } state;
21         int registrar;
22         struct wpabuf *in_buf;
23         struct wpabuf *out_buf;
24         enum wsc_op_code in_op_code, out_op_code;
25         size_t out_used;
26         size_t fragment_size;
27         struct wps_data *wps;
28         struct wps_context *wps_ctx;
29 };
30
31
32 static const char * eap_wsc_state_txt(int state)
33 {
34         switch (state) {
35         case WAIT_START:
36                 return "WAIT_START";
37         case MESG:
38                 return "MESG";
39         case FRAG_ACK:
40                 return "FRAG_ACK";
41         case WAIT_FRAG_ACK:
42                 return "WAIT_FRAG_ACK";
43         case DONE:
44                 return "DONE";
45         case FAIL:
46                 return "FAIL";
47         default:
48                 return "?";
49         }
50 }
51
52
53 static void eap_wsc_state(struct eap_wsc_data *data, int state)
54 {
55         wpa_printf(MSG_DEBUG, "EAP-WSC: %s -> %s",
56                    eap_wsc_state_txt(data->state),
57                    eap_wsc_state_txt(state));
58         data->state = state;
59 }
60
61
62 static int eap_wsc_new_ap_settings(struct wps_credential *cred,
63                                    const char *params)
64 {
65         const char *pos, *end;
66         size_t len;
67
68         os_memset(cred, 0, sizeof(*cred));
69
70         pos = os_strstr(params, "new_ssid=");
71         if (pos == NULL)
72                 return 0;
73         pos += 9;
74         end = os_strchr(pos, ' ');
75         if (end == NULL)
76                 len = os_strlen(pos);
77         else
78                 len = end - pos;
79         if ((len & 1) || len > 2 * sizeof(cred->ssid) ||
80             hexstr2bin(pos, cred->ssid, len / 2)) {
81                 wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid new_ssid");
82                 return -1;
83         }
84         cred->ssid_len = len / 2;
85
86         pos = os_strstr(params, "new_auth=");
87         if (pos == NULL) {
88                 wpa_printf(MSG_DEBUG, "EAP-WSC: Missing new_auth");
89                 return -1;
90         }
91         if (os_strncmp(pos + 9, "OPEN", 4) == 0)
92                 cred->auth_type = WPS_AUTH_OPEN;
93         else if (os_strncmp(pos + 9, "WPAPSK", 6) == 0)
94                 cred->auth_type = WPS_AUTH_WPAPSK;
95         else if (os_strncmp(pos + 9, "WPA2PSK", 7) == 0)
96                 cred->auth_type = WPS_AUTH_WPA2PSK;
97         else {
98                 wpa_printf(MSG_DEBUG, "EAP-WSC: Unknown new_auth");
99                 return -1;
100         }
101
102         pos = os_strstr(params, "new_encr=");
103         if (pos == NULL) {
104                 wpa_printf(MSG_DEBUG, "EAP-WSC: Missing new_encr");
105                 return -1;
106         }
107         if (os_strncmp(pos + 9, "NONE", 4) == 0)
108                 cred->encr_type = WPS_ENCR_NONE;
109         else if (os_strncmp(pos + 9, "WEP", 3) == 0)
110                 cred->encr_type = WPS_ENCR_WEP;
111         else if (os_strncmp(pos + 9, "TKIP", 4) == 0)
112                 cred->encr_type = WPS_ENCR_TKIP;
113         else if (os_strncmp(pos + 9, "CCMP", 4) == 0)
114                 cred->encr_type = WPS_ENCR_AES;
115         else {
116                 wpa_printf(MSG_DEBUG, "EAP-WSC: Unknown new_encr");
117                 return -1;
118         }
119
120         pos = os_strstr(params, "new_key=");
121         if (pos == NULL)
122                 return 0;
123         pos += 8;
124         end = os_strchr(pos, ' ');
125         if (end == NULL)
126                 len = os_strlen(pos);
127         else
128                 len = end - pos;
129         if ((len & 1) || len > 2 * sizeof(cred->key) ||
130             hexstr2bin(pos, cred->key, len / 2)) {
131                 wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid new_key");
132                 return -1;
133         }
134         cred->key_len = len / 2;
135
136         return 1;
137 }
138
139
140 static void * eap_wsc_init(struct eap_sm *sm)
141 {
142         struct eap_wsc_data *data;
143         const u8 *identity;
144         size_t identity_len;
145         int registrar;
146         struct wps_config cfg;
147         const char *pos, *end;
148         const char *phase1;
149         struct wps_context *wps;
150         struct wps_credential new_ap_settings;
151         int res;
152         int nfc = 0;
153         u8 pkhash[WPS_OOB_PUBKEY_HASH_LEN];
154
155         wps = sm->wps;
156         if (wps == NULL) {
157                 wpa_printf(MSG_ERROR, "EAP-WSC: WPS context not available");
158                 return NULL;
159         }
160
161         identity = eap_get_config_identity(sm, &identity_len);
162
163         if (identity && identity_len == WSC_ID_REGISTRAR_LEN &&
164             os_memcmp(identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) == 0)
165                 registrar = 1; /* Supplicant is Registrar */
166         else if (identity && identity_len == WSC_ID_ENROLLEE_LEN &&
167             os_memcmp(identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN) == 0)
168                 registrar = 0; /* Supplicant is Enrollee */
169         else {
170                 wpa_hexdump_ascii(MSG_INFO, "EAP-WSC: Unexpected identity",
171                                   identity, identity_len);
172                 return NULL;
173         }
174
175         data = os_zalloc(sizeof(*data));
176         if (data == NULL)
177                 return NULL;
178         data->state = registrar ? MESG : WAIT_START;
179         data->registrar = registrar;
180         data->wps_ctx = wps;
181
182         os_memset(&cfg, 0, sizeof(cfg));
183         cfg.wps = wps;
184         cfg.registrar = registrar;
185
186         phase1 = eap_get_config_phase1(sm);
187         if (phase1 == NULL) {
188                 wpa_printf(MSG_INFO, "EAP-WSC: phase1 configuration data not "
189                            "set");
190                 os_free(data);
191                 return NULL;
192         }
193
194         pos = os_strstr(phase1, "pin=");
195         if (pos) {
196                 pos += 4;
197                 cfg.pin = (const u8 *) pos;
198                 while (*pos != '\0' && *pos != ' ')
199                         pos++;
200                 cfg.pin_len = pos - (const char *) cfg.pin;
201                 if (cfg.pin_len == 6 &&
202                     os_strncmp((const char *) cfg.pin, "nfc-pw", 6) == 0) {
203                         cfg.pin = NULL;
204                         cfg.pin_len = 0;
205                         nfc = 1;
206                 }
207         } else {
208                 pos = os_strstr(phase1, "pbc=1");
209                 if (pos)
210                         cfg.pbc = 1;
211         }
212
213         if (cfg.pin == NULL && !cfg.pbc && !nfc) {
214                 wpa_printf(MSG_INFO, "EAP-WSC: PIN or PBC not set in phase1 "
215                            "configuration data");
216                 os_free(data);
217                 return NULL;
218         }
219
220         pos = os_strstr(phase1, "dev_pw_id=");
221         if (pos && cfg.pin)
222                 cfg.dev_pw_id = atoi(pos + 10);
223
224         pos = os_strstr(phase1, " pkhash=");
225         if (pos) {
226                 size_t len;
227                 pos += 8;
228                 end = os_strchr(pos, ' ');
229                 if (end)
230                         len = end - pos;
231                 else
232                         len = os_strlen(pos);
233                 if (len != 2 * WPS_OOB_PUBKEY_HASH_LEN ||
234                     hexstr2bin(pos, pkhash, WPS_OOB_PUBKEY_HASH_LEN)) {
235                         wpa_printf(MSG_INFO, "EAP-WSC: Invalid pkhash");
236                         os_free(data);
237                         return NULL;
238                 }
239                 cfg.peer_pubkey_hash = pkhash;
240         }
241
242         res = eap_wsc_new_ap_settings(&new_ap_settings, phase1);
243         if (res < 0) {
244                 os_free(data);
245                 wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to parse new AP "
246                            "settings");
247                 return NULL;
248         }
249         if (res == 1) {
250                 wpa_printf(MSG_DEBUG, "EAP-WSC: Provide new AP settings for "
251                            "WPS");
252                 cfg.new_ap_settings = &new_ap_settings;
253         }
254
255         data->wps = wps_init(&cfg);
256         if (data->wps == NULL) {
257                 os_free(data);
258                 wpa_printf(MSG_DEBUG, "EAP-WSC: wps_init failed");
259                 return NULL;
260         }
261         res = eap_get_config_fragment_size(sm);
262         if (res > 0)
263                 data->fragment_size = res;
264         else
265                 data->fragment_size = WSC_FRAGMENT_SIZE;
266         wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment size limit %u",
267                    (unsigned int) data->fragment_size);
268
269         if (registrar && cfg.pin) {
270                 wps_registrar_add_pin(data->wps_ctx->registrar, NULL, NULL,
271                                       cfg.pin, cfg.pin_len, 0);
272         }
273
274         /* Use reduced client timeout for WPS to avoid long wait */
275         if (sm->ClientTimeout > 30)
276                 sm->ClientTimeout = 30;
277
278         return data;
279 }
280
281
282 static void eap_wsc_deinit(struct eap_sm *sm, void *priv)
283 {
284         struct eap_wsc_data *data = priv;
285         wpabuf_free(data->in_buf);
286         wpabuf_free(data->out_buf);
287         wps_deinit(data->wps);
288         os_free(data->wps_ctx->network_key);
289         data->wps_ctx->network_key = NULL;
290         os_free(data);
291 }
292
293
294 static struct wpabuf * eap_wsc_build_msg(struct eap_wsc_data *data,
295                                          struct eap_method_ret *ret, u8 id)
296 {
297         struct wpabuf *resp;
298         u8 flags;
299         size_t send_len, plen;
300
301         ret->ignore = FALSE;
302         wpa_printf(MSG_DEBUG, "EAP-WSC: Generating Response");
303         ret->allowNotifications = TRUE;
304
305         flags = 0;
306         send_len = wpabuf_len(data->out_buf) - data->out_used;
307         if (2 + send_len > data->fragment_size) {
308                 send_len = data->fragment_size - 2;
309                 flags |= WSC_FLAGS_MF;
310                 if (data->out_used == 0) {
311                         flags |= WSC_FLAGS_LF;
312                         send_len -= 2;
313                 }
314         }
315         plen = 2 + send_len;
316         if (flags & WSC_FLAGS_LF)
317                 plen += 2;
318         resp = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, plen,
319                              EAP_CODE_RESPONSE, id);
320         if (resp == NULL)
321                 return NULL;
322
323         wpabuf_put_u8(resp, data->out_op_code); /* Op-Code */
324         wpabuf_put_u8(resp, flags); /* Flags */
325         if (flags & WSC_FLAGS_LF)
326                 wpabuf_put_be16(resp, wpabuf_len(data->out_buf));
327
328         wpabuf_put_data(resp, wpabuf_head_u8(data->out_buf) + data->out_used,
329                         send_len);
330         data->out_used += send_len;
331
332         ret->methodState = METHOD_MAY_CONT;
333         ret->decision = DECISION_FAIL;
334
335         if (data->out_used == wpabuf_len(data->out_buf)) {
336                 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
337                            "(message sent completely)",
338                            (unsigned long) send_len);
339                 wpabuf_free(data->out_buf);
340                 data->out_buf = NULL;
341                 data->out_used = 0;
342                 if ((data->state == FAIL && data->out_op_code == WSC_ACK) ||
343                     data->out_op_code == WSC_NACK ||
344                     data->out_op_code == WSC_Done) {
345                         eap_wsc_state(data, FAIL);
346                         ret->methodState = METHOD_DONE;
347                 } else
348                         eap_wsc_state(data, MESG);
349         } else {
350                 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
351                            "(%lu more to send)", (unsigned long) send_len,
352                            (unsigned long) wpabuf_len(data->out_buf) -
353                            data->out_used);
354                 eap_wsc_state(data, WAIT_FRAG_ACK);
355         }
356
357         return resp;
358 }
359
360
361 static int eap_wsc_process_cont(struct eap_wsc_data *data,
362                                 const u8 *buf, size_t len, u8 op_code)
363 {
364         /* Process continuation of a pending message */
365         if (op_code != data->in_op_code) {
366                 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d in "
367                            "fragment (expected %d)",
368                            op_code, data->in_op_code);
369                 return -1;
370         }
371
372         if (len > wpabuf_tailroom(data->in_buf)) {
373                 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment overflow");
374                 eap_wsc_state(data, FAIL);
375                 return -1;
376         }
377
378         wpabuf_put_data(data->in_buf, buf, len);
379         wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes, waiting "
380                    "for %lu bytes more", (unsigned long) len,
381                    (unsigned long) wpabuf_tailroom(data->in_buf));
382
383         return 0;
384 }
385
386
387 static struct wpabuf * eap_wsc_process_fragment(struct eap_wsc_data *data,
388                                                 struct eap_method_ret *ret,
389                                                 u8 id, u8 flags, u8 op_code,
390                                                 u16 message_length,
391                                                 const u8 *buf, size_t len)
392 {
393         /* Process a fragment that is not the last one of the message */
394         if (data->in_buf == NULL && !(flags & WSC_FLAGS_LF)) {
395                 wpa_printf(MSG_DEBUG, "EAP-WSC: No Message Length field in a "
396                            "fragmented packet");
397                 ret->ignore = TRUE;
398                 return NULL;
399         }
400
401         if (data->in_buf == NULL) {
402                 /* First fragment of the message */
403                 data->in_buf = wpabuf_alloc(message_length);
404                 if (data->in_buf == NULL) {
405                         wpa_printf(MSG_DEBUG, "EAP-WSC: No memory for "
406                                    "message");
407                         ret->ignore = TRUE;
408                         return NULL;
409                 }
410                 data->in_op_code = op_code;
411                 wpabuf_put_data(data->in_buf, buf, len);
412                 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes in first "
413                            "fragment, waiting for %lu bytes more",
414                            (unsigned long) len,
415                            (unsigned long) wpabuf_tailroom(data->in_buf));
416         }
417
418         return eap_wsc_build_frag_ack(id, EAP_CODE_RESPONSE);
419 }
420
421
422 static struct wpabuf * eap_wsc_process(struct eap_sm *sm, void *priv,
423                                        struct eap_method_ret *ret,
424                                        const struct wpabuf *reqData)
425 {
426         struct eap_wsc_data *data = priv;
427         const u8 *start, *pos, *end;
428         size_t len;
429         u8 op_code, flags, id;
430         u16 message_length = 0;
431         enum wps_process_res res;
432         struct wpabuf tmpbuf;
433         struct wpabuf *r;
434
435         pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, reqData,
436                                &len);
437         if (pos == NULL || len < 2) {
438                 ret->ignore = TRUE;
439                 return NULL;
440         }
441
442         id = eap_get_id(reqData);
443
444         start = pos;
445         end = start + len;
446
447         op_code = *pos++;
448         flags = *pos++;
449         if (flags & WSC_FLAGS_LF) {
450                 if (end - pos < 2) {
451                         wpa_printf(MSG_DEBUG, "EAP-WSC: Message underflow");
452                         ret->ignore = TRUE;
453                         return NULL;
454                 }
455                 message_length = WPA_GET_BE16(pos);
456                 pos += 2;
457
458                 if (message_length < end - pos) {
459                         wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid Message "
460                                    "Length");
461                         ret->ignore = TRUE;
462                         return NULL;
463                 }
464         }
465
466         wpa_printf(MSG_DEBUG, "EAP-WSC: Received packet: Op-Code %d "
467                    "Flags 0x%x Message Length %d",
468                    op_code, flags, message_length);
469
470         if (data->state == WAIT_FRAG_ACK) {
471                 if (op_code != WSC_FRAG_ACK) {
472                         wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
473                                    "in WAIT_FRAG_ACK state", op_code);
474                         ret->ignore = TRUE;
475                         return NULL;
476                 }
477                 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment acknowledged");
478                 eap_wsc_state(data, MESG);
479                 return eap_wsc_build_msg(data, ret, id);
480         }
481
482         if (op_code != WSC_ACK && op_code != WSC_NACK && op_code != WSC_MSG &&
483             op_code != WSC_Done && op_code != WSC_Start) {
484                 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
485                            op_code);
486                 ret->ignore = TRUE;
487                 return NULL;
488         }
489
490         if (data->state == WAIT_START) {
491                 if (op_code != WSC_Start) {
492                         wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
493                                    "in WAIT_START state", op_code);
494                         ret->ignore = TRUE;
495                         return NULL;
496                 }
497                 wpa_printf(MSG_DEBUG, "EAP-WSC: Received start");
498                 eap_wsc_state(data, MESG);
499                 /* Start message has empty payload, skip processing */
500                 goto send_msg;
501         } else if (op_code == WSC_Start) {
502                 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
503                            op_code);
504                 ret->ignore = TRUE;
505                 return NULL;
506         }
507
508         if (data->in_buf &&
509             eap_wsc_process_cont(data, pos, end - pos, op_code) < 0) {
510                 ret->ignore = TRUE;
511                 return NULL;
512         }
513
514         if (flags & WSC_FLAGS_MF) {
515                 return eap_wsc_process_fragment(data, ret, id, flags, op_code,
516                                                 message_length, pos,
517                                                 end - pos);
518         }
519
520         if (data->in_buf == NULL) {
521                 /* Wrap unfragmented messages as wpabuf without extra copy */
522                 wpabuf_set(&tmpbuf, pos, end - pos);
523                 data->in_buf = &tmpbuf;
524         }
525
526         res = wps_process_msg(data->wps, op_code, data->in_buf);
527         switch (res) {
528         case WPS_DONE:
529                 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing completed "
530                            "successfully - wait for EAP failure");
531                 eap_wsc_state(data, FAIL);
532                 break;
533         case WPS_CONTINUE:
534                 eap_wsc_state(data, MESG);
535                 break;
536         case WPS_FAILURE:
537         case WPS_PENDING:
538                 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing failed");
539                 eap_wsc_state(data, FAIL);
540                 break;
541         }
542
543         if (data->in_buf != &tmpbuf)
544                 wpabuf_free(data->in_buf);
545         data->in_buf = NULL;
546
547 send_msg:
548         if (data->out_buf == NULL) {
549                 data->out_buf = wps_get_msg(data->wps, &data->out_op_code);
550                 if (data->out_buf == NULL) {
551                         wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to receive "
552                                    "message from WPS");
553                         return NULL;
554                 }
555                 data->out_used = 0;
556         }
557
558         eap_wsc_state(data, MESG);
559         r = eap_wsc_build_msg(data, ret, id);
560         if (data->state == FAIL && ret->methodState == METHOD_DONE) {
561                 /* Use reduced client timeout for WPS to avoid long wait */
562                 if (sm->ClientTimeout > 2)
563                         sm->ClientTimeout = 2;
564         }
565         return r;
566 }
567
568
569 int eap_peer_wsc_register(void)
570 {
571         struct eap_method *eap;
572         int ret;
573
574         eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
575                                     EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
576                                     "WSC");
577         if (eap == NULL)
578                 return -1;
579
580         eap->init = eap_wsc_init;
581         eap->deinit = eap_wsc_deinit;
582         eap->process = eap_wsc_process;
583
584         ret = eap_peer_method_register(eap);
585         if (ret)
586                 eap_peer_method_free(eap);
587         return ret;
588 }