Change radius_msg_free() to free the buffer
[mech_eap.git] / wpa_supplicant / eapol_test.c
1 /*
2  * WPA Supplicant - test code
3  * Copyright (c) 2003-2007, 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  * IEEE 802.1X Supplicant test code (to be used in place of wpa_supplicant.c.
15  * Not used in production version.
16  */
17
18 #include "includes.h"
19 #include <assert.h>
20
21 #include "common.h"
22 #include "config.h"
23 #include "eapol_supp/eapol_supp_sm.h"
24 #include "eap_peer/eap.h"
25 #include "eloop.h"
26 #include "rsn_supp/wpa.h"
27 #include "eap_peer/eap_i.h"
28 #include "wpa_supplicant_i.h"
29 #include "radius/radius.h"
30 #include "radius/radius_client.h"
31 #include "ctrl_iface.h"
32 #include "pcsc_funcs.h"
33
34
35 extern int wpa_debug_level;
36 extern int wpa_debug_show_keys;
37
38 struct wpa_driver_ops *wpa_drivers[] = { NULL };
39
40
41 struct extra_radius_attr {
42         u8 type;
43         char syntax;
44         char *data;
45         struct extra_radius_attr *next;
46 };
47
48 struct eapol_test_data {
49         struct wpa_supplicant *wpa_s;
50
51         int eapol_test_num_reauths;
52         int no_mppe_keys;
53         int num_mppe_ok, num_mppe_mismatch;
54
55         u8 radius_identifier;
56         struct radius_msg *last_recv_radius;
57         struct in_addr own_ip_addr;
58         struct radius_client_data *radius;
59         struct hostapd_radius_servers *radius_conf;
60
61         u8 *last_eap_radius; /* last received EAP Response from Authentication
62                               * Server */
63         size_t last_eap_radius_len;
64
65         u8 authenticator_pmk[PMK_LEN];
66         size_t authenticator_pmk_len;
67         int radius_access_accept_received;
68         int radius_access_reject_received;
69         int auth_timed_out;
70
71         u8 *eap_identity;
72         size_t eap_identity_len;
73
74         char *connect_info;
75         u8 own_addr[ETH_ALEN];
76         struct extra_radius_attr *extra_attrs;
77 };
78
79 static struct eapol_test_data eapol_test;
80
81
82 static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx);
83
84
85 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
86                               int level, const char *txt, size_t len)
87 {
88         if (addr)
89                 wpa_printf(MSG_DEBUG, "STA " MACSTR ": %s\n",
90                            MAC2STR(addr), txt);
91         else
92                 wpa_printf(MSG_DEBUG, "%s", txt);
93 }
94
95
96 static int add_extra_attr(struct radius_msg *msg,
97                           struct extra_radius_attr *attr)
98 {
99         size_t len;
100         char *pos;
101         u32 val;
102         char buf[128];
103
104         switch (attr->syntax) {
105         case 's':
106                 os_snprintf(buf, sizeof(buf), "%s", attr->data);
107                 len = os_strlen(buf);
108                 break;
109         case 'n':
110                 buf[0] = '\0';
111                 len = 1;
112                 break;
113         case 'x':
114                 pos = attr->data;
115                 if (pos[0] == '0' && pos[1] == 'x')
116                         pos += 2;
117                 len = os_strlen(pos);
118                 if ((len & 1) || (len / 2) > sizeof(buf)) {
119                         printf("Invalid extra attribute hexstring\n");
120                         return -1;
121                 }
122                 len /= 2;
123                 if (hexstr2bin(pos, (u8 *) buf, len) < 0) {
124                         printf("Invalid extra attribute hexstring\n");
125                         return -1;
126                 }
127                 break;
128         case 'd':
129                 val = htonl(atoi(attr->data));
130                 os_memcpy(buf, &val, 4);
131                 len = 4;
132                 break;
133         default:
134                 printf("Incorrect extra attribute syntax specification\n");
135                 return -1;
136         }
137
138         if (!radius_msg_add_attr(msg, attr->type, (u8 *) buf, len)) {
139                 printf("Could not add attribute %d\n", attr->type);
140                 return -1;
141         }
142
143         return 0;
144 }
145
146
147 static int add_extra_attrs(struct radius_msg *msg,
148                            struct extra_radius_attr *attrs)
149 {
150         struct extra_radius_attr *p;
151         for (p = attrs; p; p = p->next) {
152                 if (add_extra_attr(msg, p) < 0)
153                         return -1;
154         }
155         return 0;
156 }
157
158
159 static struct extra_radius_attr *
160 find_extra_attr(struct extra_radius_attr *attrs, u8 type)
161 {
162         struct extra_radius_attr *p;
163         for (p = attrs; p; p = p->next) {
164                 if (p->type == type)
165                         return p;
166         }
167         return NULL;
168 }
169
170
171 static void ieee802_1x_encapsulate_radius(struct eapol_test_data *e,
172                                           const u8 *eap, size_t len)
173 {
174         struct radius_msg *msg;
175         char buf[128];
176         const struct eap_hdr *hdr;
177         const u8 *pos;
178
179         wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
180                    "packet");
181
182         e->radius_identifier = radius_client_get_id(e->radius);
183         msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
184                              e->radius_identifier);
185         if (msg == NULL) {
186                 printf("Could not create net RADIUS packet\n");
187                 return;
188         }
189
190         radius_msg_make_authenticator(msg, (u8 *) e, sizeof(*e));
191
192         hdr = (const struct eap_hdr *) eap;
193         pos = (const u8 *) (hdr + 1);
194         if (len > sizeof(*hdr) && hdr->code == EAP_CODE_RESPONSE &&
195             pos[0] == EAP_TYPE_IDENTITY) {
196                 pos++;
197                 os_free(e->eap_identity);
198                 e->eap_identity_len = len - sizeof(*hdr) - 1;
199                 e->eap_identity = os_malloc(e->eap_identity_len);
200                 if (e->eap_identity) {
201                         os_memcpy(e->eap_identity, pos, e->eap_identity_len);
202                         wpa_hexdump(MSG_DEBUG, "Learned identity from "
203                                     "EAP-Response-Identity",
204                                     e->eap_identity, e->eap_identity_len);
205                 }
206         }
207
208         if (e->eap_identity &&
209             !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
210                                  e->eap_identity, e->eap_identity_len)) {
211                 printf("Could not add User-Name\n");
212                 goto fail;
213         }
214
215         if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_NAS_IP_ADDRESS) &&
216             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
217                                  (u8 *) &e->own_ip_addr, 4)) {
218                 printf("Could not add NAS-IP-Address\n");
219                 goto fail;
220         }
221
222         os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
223                     MAC2STR(e->wpa_s->own_addr));
224         if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_CALLING_STATION_ID)
225             &&
226             !radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
227                                  (u8 *) buf, os_strlen(buf))) {
228                 printf("Could not add Calling-Station-Id\n");
229                 goto fail;
230         }
231
232         /* TODO: should probably check MTU from driver config; 2304 is max for
233          * IEEE 802.11, but use 1400 to avoid problems with too large packets
234          */
235         if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_FRAMED_MTU) &&
236             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
237                 printf("Could not add Framed-MTU\n");
238                 goto fail;
239         }
240
241         if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_NAS_PORT_TYPE) &&
242             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
243                                        RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
244                 printf("Could not add NAS-Port-Type\n");
245                 goto fail;
246         }
247
248         os_snprintf(buf, sizeof(buf), "%s", e->connect_info);
249         if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_CONNECT_INFO) &&
250             !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
251                                  (u8 *) buf, os_strlen(buf))) {
252                 printf("Could not add Connect-Info\n");
253                 goto fail;
254         }
255
256         if (add_extra_attrs(msg, e->extra_attrs) < 0)
257                 goto fail;
258
259         if (eap && !radius_msg_add_eap(msg, eap, len)) {
260                 printf("Could not add EAP-Message\n");
261                 goto fail;
262         }
263
264         /* State attribute must be copied if and only if this packet is
265          * Access-Request reply to the previous Access-Challenge */
266         if (e->last_recv_radius && e->last_recv_radius->hdr->code ==
267             RADIUS_CODE_ACCESS_CHALLENGE) {
268                 int res = radius_msg_copy_attr(msg, e->last_recv_radius,
269                                                RADIUS_ATTR_STATE);
270                 if (res < 0) {
271                         printf("Could not copy State attribute from previous "
272                                "Access-Challenge\n");
273                         goto fail;
274                 }
275                 if (res > 0) {
276                         wpa_printf(MSG_DEBUG, "  Copied RADIUS State "
277                                    "Attribute");
278                 }
279         }
280
281         radius_client_send(e->radius, msg, RADIUS_AUTH, e->wpa_s->own_addr);
282         return;
283
284  fail:
285         radius_msg_free(msg);
286 }
287
288
289 static int eapol_test_eapol_send(void *ctx, int type, const u8 *buf,
290                                  size_t len)
291 {
292         /* struct wpa_supplicant *wpa_s = ctx; */
293         printf("WPA: eapol_test_eapol_send(type=%d len=%lu)\n",
294                type, (unsigned long) len);
295         if (type == IEEE802_1X_TYPE_EAP_PACKET) {
296                 wpa_hexdump(MSG_DEBUG, "TX EAP -> RADIUS", buf, len);
297                 ieee802_1x_encapsulate_radius(&eapol_test, buf, len);
298         }
299         return 0;
300 }
301
302
303 static void eapol_test_set_config_blob(void *ctx,
304                                        struct wpa_config_blob *blob)
305 {
306         struct wpa_supplicant *wpa_s = ctx;
307         wpa_config_set_blob(wpa_s->conf, blob);
308 }
309
310
311 static const struct wpa_config_blob *
312 eapol_test_get_config_blob(void *ctx, const char *name)
313 {
314         struct wpa_supplicant *wpa_s = ctx;
315         return wpa_config_get_blob(wpa_s->conf, name);
316 }
317
318
319 static void eapol_test_eapol_done_cb(void *ctx)
320 {
321         printf("WPA: EAPOL processing complete\n");
322 }
323
324
325 static void eapol_sm_reauth(void *eloop_ctx, void *timeout_ctx)
326 {
327         struct eapol_test_data *e = eloop_ctx;
328         printf("\n\n\n\n\neapol_test: Triggering EAP reauthentication\n\n");
329         e->radius_access_accept_received = 0;
330         send_eap_request_identity(e->wpa_s, NULL);
331 }
332
333
334 static int eapol_test_compare_pmk(struct eapol_test_data *e)
335 {
336         u8 pmk[PMK_LEN];
337         int ret = 1;
338
339         if (eapol_sm_get_key(e->wpa_s->eapol, pmk, PMK_LEN) == 0) {
340                 wpa_hexdump(MSG_DEBUG, "PMK from EAPOL", pmk, PMK_LEN);
341                 if (os_memcmp(pmk, e->authenticator_pmk, PMK_LEN) != 0) {
342                         printf("WARNING: PMK mismatch\n");
343                         wpa_hexdump(MSG_DEBUG, "PMK from AS",
344                                     e->authenticator_pmk, PMK_LEN);
345                 } else if (e->radius_access_accept_received)
346                         ret = 0;
347         } else if (e->authenticator_pmk_len == 16 &&
348                    eapol_sm_get_key(e->wpa_s->eapol, pmk, 16) == 0) {
349                 wpa_hexdump(MSG_DEBUG, "LEAP PMK from EAPOL", pmk, 16);
350                 if (os_memcmp(pmk, e->authenticator_pmk, 16) != 0) {
351                         printf("WARNING: PMK mismatch\n");
352                         wpa_hexdump(MSG_DEBUG, "PMK from AS",
353                                     e->authenticator_pmk, 16);
354                 } else if (e->radius_access_accept_received)
355                         ret = 0;
356         } else if (e->radius_access_accept_received && e->no_mppe_keys) {
357                 /* No keying material expected */
358                 ret = 0;
359         }
360
361         if (ret && !e->no_mppe_keys)
362                 e->num_mppe_mismatch++;
363         else if (!e->no_mppe_keys)
364                 e->num_mppe_ok++;
365
366         return ret;
367 }
368
369
370 static void eapol_sm_cb(struct eapol_sm *eapol, int success, void *ctx)
371 {
372         struct eapol_test_data *e = ctx;
373         printf("eapol_sm_cb: success=%d\n", success);
374         e->eapol_test_num_reauths--;
375         if (e->eapol_test_num_reauths < 0)
376                 eloop_terminate();
377         else {
378                 eapol_test_compare_pmk(e);
379                 eloop_register_timeout(0, 100000, eapol_sm_reauth, e, NULL);
380         }
381 }
382
383
384 static int test_eapol(struct eapol_test_data *e, struct wpa_supplicant *wpa_s,
385                       struct wpa_ssid *ssid)
386 {
387         struct eapol_config eapol_conf;
388         struct eapol_ctx *ctx;
389
390         ctx = os_zalloc(sizeof(*ctx));
391         if (ctx == NULL) {
392                 printf("Failed to allocate EAPOL context.\n");
393                 return -1;
394         }
395         ctx->ctx = wpa_s;
396         ctx->msg_ctx = wpa_s;
397         ctx->scard_ctx = wpa_s->scard;
398         ctx->cb = eapol_sm_cb;
399         ctx->cb_ctx = e;
400         ctx->eapol_send_ctx = wpa_s;
401         ctx->preauth = 0;
402         ctx->eapol_done_cb = eapol_test_eapol_done_cb;
403         ctx->eapol_send = eapol_test_eapol_send;
404         ctx->set_config_blob = eapol_test_set_config_blob;
405         ctx->get_config_blob = eapol_test_get_config_blob;
406         ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
407         ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
408         ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
409
410         wpa_s->eapol = eapol_sm_init(ctx);
411         if (wpa_s->eapol == NULL) {
412                 os_free(ctx);
413                 printf("Failed to initialize EAPOL state machines.\n");
414                 return -1;
415         }
416
417         wpa_s->current_ssid = ssid;
418         os_memset(&eapol_conf, 0, sizeof(eapol_conf));
419         eapol_conf.accept_802_1x_keys = 1;
420         eapol_conf.required_keys = 0;
421         eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
422         eapol_conf.workaround = ssid->eap_workaround;
423         eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
424         eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
425
426
427         eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
428         /* 802.1X::portControl = Auto */
429         eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
430
431         return 0;
432 }
433
434
435 static void test_eapol_clean(struct eapol_test_data *e,
436                              struct wpa_supplicant *wpa_s)
437 {
438         struct extra_radius_attr *p, *prev;
439
440         radius_client_deinit(e->radius);
441         os_free(e->last_eap_radius);
442         radius_msg_free(e->last_recv_radius);
443         e->last_recv_radius = NULL;
444         os_free(e->eap_identity);
445         e->eap_identity = NULL;
446         eapol_sm_deinit(wpa_s->eapol);
447         wpa_s->eapol = NULL;
448         if (e->radius_conf && e->radius_conf->auth_server) {
449                 os_free(e->radius_conf->auth_server->shared_secret);
450                 os_free(e->radius_conf->auth_server);
451         }
452         os_free(e->radius_conf);
453         e->radius_conf = NULL;
454         scard_deinit(wpa_s->scard);
455         if (wpa_s->ctrl_iface) {
456                 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
457                 wpa_s->ctrl_iface = NULL;
458         }
459         wpa_config_free(wpa_s->conf);
460
461         p = e->extra_attrs;
462         while (p) {
463                 prev = p;
464                 p = p->next;
465                 os_free(prev);
466         }
467 }
468
469
470 static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx)
471 {
472         struct wpa_supplicant *wpa_s = eloop_ctx;
473         u8 buf[100], *pos;
474         struct ieee802_1x_hdr *hdr;
475         struct eap_hdr *eap;
476
477         hdr = (struct ieee802_1x_hdr *) buf;
478         hdr->version = EAPOL_VERSION;
479         hdr->type = IEEE802_1X_TYPE_EAP_PACKET;
480         hdr->length = htons(5);
481
482         eap = (struct eap_hdr *) (hdr + 1);
483         eap->code = EAP_CODE_REQUEST;
484         eap->identifier = 0;
485         eap->length = htons(5);
486         pos = (u8 *) (eap + 1);
487         *pos = EAP_TYPE_IDENTITY;
488
489         printf("Sending fake EAP-Request-Identity\n");
490         eapol_sm_rx_eapol(wpa_s->eapol, wpa_s->bssid, buf,
491                           sizeof(*hdr) + 5);
492 }
493
494
495 static void eapol_test_timeout(void *eloop_ctx, void *timeout_ctx)
496 {
497         struct eapol_test_data *e = eloop_ctx;
498         printf("EAPOL test timed out\n");
499         e->auth_timed_out = 1;
500         eloop_terminate();
501 }
502
503
504 static char *eap_type_text(u8 type)
505 {
506         switch (type) {
507         case EAP_TYPE_IDENTITY: return "Identity";
508         case EAP_TYPE_NOTIFICATION: return "Notification";
509         case EAP_TYPE_NAK: return "Nak";
510         case EAP_TYPE_TLS: return "TLS";
511         case EAP_TYPE_TTLS: return "TTLS";
512         case EAP_TYPE_PEAP: return "PEAP";
513         case EAP_TYPE_SIM: return "SIM";
514         case EAP_TYPE_GTC: return "GTC";
515         case EAP_TYPE_MD5: return "MD5";
516         case EAP_TYPE_OTP: return "OTP";
517         case EAP_TYPE_FAST: return "FAST";
518         case EAP_TYPE_SAKE: return "SAKE";
519         case EAP_TYPE_PSK: return "PSK";
520         default: return "Unknown";
521         }
522 }
523
524
525 static void ieee802_1x_decapsulate_radius(struct eapol_test_data *e)
526 {
527         u8 *eap;
528         size_t len;
529         struct eap_hdr *hdr;
530         int eap_type = -1;
531         char buf[64];
532         struct radius_msg *msg;
533
534         if (e->last_recv_radius == NULL)
535                 return;
536
537         msg = e->last_recv_radius;
538
539         eap = radius_msg_get_eap(msg, &len);
540         if (eap == NULL) {
541                 /* draft-aboba-radius-rfc2869bis-20.txt, Chap. 2.6.3:
542                  * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
543                  * attribute */
544                 wpa_printf(MSG_DEBUG, "could not extract "
545                                "EAP-Message from RADIUS message");
546                 os_free(e->last_eap_radius);
547                 e->last_eap_radius = NULL;
548                 e->last_eap_radius_len = 0;
549                 return;
550         }
551
552         if (len < sizeof(*hdr)) {
553                 wpa_printf(MSG_DEBUG, "too short EAP packet "
554                                "received from authentication server");
555                 os_free(eap);
556                 return;
557         }
558
559         if (len > sizeof(*hdr))
560                 eap_type = eap[sizeof(*hdr)];
561
562         hdr = (struct eap_hdr *) eap;
563         switch (hdr->code) {
564         case EAP_CODE_REQUEST:
565                 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
566                             eap_type >= 0 ? eap_type_text(eap_type) : "??",
567                             eap_type);
568                 break;
569         case EAP_CODE_RESPONSE:
570                 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
571                             eap_type >= 0 ? eap_type_text(eap_type) : "??",
572                             eap_type);
573                 break;
574         case EAP_CODE_SUCCESS:
575                 os_strlcpy(buf, "EAP Success", sizeof(buf));
576                 /* LEAP uses EAP Success within an authentication, so must not
577                  * stop here with eloop_terminate(); */
578                 break;
579         case EAP_CODE_FAILURE:
580                 os_strlcpy(buf, "EAP Failure", sizeof(buf));
581                 eloop_terminate();
582                 break;
583         default:
584                 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
585                 wpa_hexdump(MSG_DEBUG, "Decapsulated EAP packet", eap, len);
586                 break;
587         }
588         wpa_printf(MSG_DEBUG, "decapsulated EAP packet (code=%d "
589                        "id=%d len=%d) from RADIUS server: %s",
590                       hdr->code, hdr->identifier, ntohs(hdr->length), buf);
591
592         /* sta->eapol_sm->be_auth.idFromServer = hdr->identifier; */
593
594         os_free(e->last_eap_radius);
595         e->last_eap_radius = eap;
596         e->last_eap_radius_len = len;
597
598         {
599                 struct ieee802_1x_hdr *dot1x;
600                 dot1x = os_malloc(sizeof(*dot1x) + len);
601                 assert(dot1x != NULL);
602                 dot1x->version = EAPOL_VERSION;
603                 dot1x->type = IEEE802_1X_TYPE_EAP_PACKET;
604                 dot1x->length = htons(len);
605                 os_memcpy((u8 *) (dot1x + 1), eap, len);
606                 eapol_sm_rx_eapol(e->wpa_s->eapol, e->wpa_s->bssid,
607                                   (u8 *) dot1x, sizeof(*dot1x) + len);
608                 os_free(dot1x);
609         }
610 }
611
612
613 static void ieee802_1x_get_keys(struct eapol_test_data *e,
614                                 struct radius_msg *msg, struct radius_msg *req,
615                                 const u8 *shared_secret,
616                                 size_t shared_secret_len)
617 {
618         struct radius_ms_mppe_keys *keys;
619
620         keys = radius_msg_get_ms_keys(msg, req, shared_secret,
621                                       shared_secret_len);
622         if (keys && keys->send == NULL && keys->recv == NULL) {
623                 os_free(keys);
624                 keys = radius_msg_get_cisco_keys(msg, req, shared_secret,
625                                                  shared_secret_len);
626         }
627
628         if (keys) {
629                 if (keys->send) {
630                         wpa_hexdump(MSG_DEBUG, "MS-MPPE-Send-Key (sign)",
631                                     keys->send, keys->send_len);
632                 }
633                 if (keys->recv) {
634                         wpa_hexdump(MSG_DEBUG, "MS-MPPE-Recv-Key (crypt)",
635                                     keys->recv, keys->recv_len);
636                         e->authenticator_pmk_len =
637                                 keys->recv_len > PMK_LEN ? PMK_LEN :
638                                 keys->recv_len;
639                         os_memcpy(e->authenticator_pmk, keys->recv,
640                                   e->authenticator_pmk_len);
641                         if (e->authenticator_pmk_len == 16 && keys->send &&
642                             keys->send_len == 16) {
643                                 /* MS-CHAP-v2 derives 16 octet keys */
644                                 wpa_printf(MSG_DEBUG, "Use MS-MPPE-Send-Key "
645                                            "to extend PMK to 32 octets");
646                                 os_memcpy(e->authenticator_pmk +
647                                           e->authenticator_pmk_len,
648                                           keys->send, keys->send_len);
649                                 e->authenticator_pmk_len += keys->send_len;
650                         }
651                 }
652
653                 os_free(keys->send);
654                 os_free(keys->recv);
655                 os_free(keys);
656         }
657 }
658
659
660 /* Process the RADIUS frames from Authentication Server */
661 static RadiusRxResult
662 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
663                         const u8 *shared_secret, size_t shared_secret_len,
664                         void *data)
665 {
666         struct eapol_test_data *e = data;
667
668         /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
669          * present when packet contains an EAP-Message attribute */
670         if (msg->hdr->code == RADIUS_CODE_ACCESS_REJECT &&
671             radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
672                                 0) < 0 &&
673             radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
674                 wpa_printf(MSG_DEBUG, "Allowing RADIUS "
675                               "Access-Reject without Message-Authenticator "
676                               "since it does not include EAP-Message\n");
677         } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
678                                      req, 1)) {
679                 printf("Incoming RADIUS packet did not have correct "
680                        "Message-Authenticator - dropped\n");
681                 return RADIUS_RX_UNKNOWN;
682         }
683
684         if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
685             msg->hdr->code != RADIUS_CODE_ACCESS_REJECT &&
686             msg->hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
687                 printf("Unknown RADIUS message code\n");
688                 return RADIUS_RX_UNKNOWN;
689         }
690
691         e->radius_identifier = -1;
692         wpa_printf(MSG_DEBUG, "RADIUS packet matching with station");
693
694         radius_msg_free(e->last_recv_radius);
695         e->last_recv_radius = msg;
696
697         switch (msg->hdr->code) {
698         case RADIUS_CODE_ACCESS_ACCEPT:
699                 e->radius_access_accept_received = 1;
700                 ieee802_1x_get_keys(e, msg, req, shared_secret,
701                                     shared_secret_len);
702                 break;
703         case RADIUS_CODE_ACCESS_REJECT:
704                 e->radius_access_reject_received = 1;
705                 break;
706         }
707
708         ieee802_1x_decapsulate_radius(e);
709
710         if ((msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
711              e->eapol_test_num_reauths < 0) ||
712             msg->hdr->code == RADIUS_CODE_ACCESS_REJECT) {
713                 eloop_terminate();
714         }
715
716         return RADIUS_RX_QUEUED;
717 }
718
719
720 static void wpa_init_conf(struct eapol_test_data *e,
721                           struct wpa_supplicant *wpa_s, const char *authsrv,
722                           int port, const char *secret,
723                           const char *cli_addr)
724 {
725         struct hostapd_radius_server *as;
726         int res;
727
728         wpa_s->bssid[5] = 1;
729         os_memcpy(wpa_s->own_addr, e->own_addr, ETH_ALEN);
730         e->own_ip_addr.s_addr = htonl((127 << 24) | 1);
731         os_strlcpy(wpa_s->ifname, "test", sizeof(wpa_s->ifname));
732
733         e->radius_conf = os_zalloc(sizeof(struct hostapd_radius_servers));
734         assert(e->radius_conf != NULL);
735         e->radius_conf->num_auth_servers = 1;
736         as = os_zalloc(sizeof(struct hostapd_radius_server));
737         assert(as != NULL);
738 #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
739         {
740                 int a[4];
741                 u8 *pos;
742                 sscanf(authsrv, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
743                 pos = (u8 *) &as->addr.u.v4;
744                 *pos++ = a[0];
745                 *pos++ = a[1];
746                 *pos++ = a[2];
747                 *pos++ = a[3];
748         }
749 #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
750         inet_aton(authsrv, &as->addr.u.v4);
751 #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
752         as->addr.af = AF_INET;
753         as->port = port;
754         as->shared_secret = (u8 *) os_strdup(secret);
755         as->shared_secret_len = os_strlen(secret);
756         e->radius_conf->auth_server = as;
757         e->radius_conf->auth_servers = as;
758         e->radius_conf->msg_dumps = 1;
759         if (cli_addr) {
760                 if (hostapd_parse_ip_addr(cli_addr,
761                                           &e->radius_conf->client_addr) == 0)
762                         e->radius_conf->force_client_addr = 1;
763                 else {
764                         wpa_printf(MSG_ERROR, "Invalid IP address '%s'",
765                                    cli_addr);
766                         assert(0);
767                 }
768         }
769
770         e->radius = radius_client_init(wpa_s, e->radius_conf);
771         assert(e->radius != NULL);
772
773         res = radius_client_register(e->radius, RADIUS_AUTH,
774                                      ieee802_1x_receive_auth, e);
775         assert(res == 0);
776 }
777
778
779 static int scard_test(void)
780 {
781         struct scard_data *scard;
782         size_t len;
783         char imsi[20];
784         unsigned char _rand[16];
785 #ifdef PCSC_FUNCS
786         unsigned char sres[4];
787         unsigned char kc[8];
788 #endif /* PCSC_FUNCS */
789 #define num_triplets 5
790         unsigned char rand_[num_triplets][16];
791         unsigned char sres_[num_triplets][4];
792         unsigned char kc_[num_triplets][8];
793         int i, res;
794         size_t j;
795
796 #define AKA_RAND_LEN 16
797 #define AKA_AUTN_LEN 16
798 #define AKA_AUTS_LEN 14
799 #define RES_MAX_LEN 16
800 #define IK_LEN 16
801 #define CK_LEN 16
802         unsigned char aka_rand[AKA_RAND_LEN];
803         unsigned char aka_autn[AKA_AUTN_LEN];
804         unsigned char aka_auts[AKA_AUTS_LEN];
805         unsigned char aka_res[RES_MAX_LEN];
806         size_t aka_res_len;
807         unsigned char aka_ik[IK_LEN];
808         unsigned char aka_ck[CK_LEN];
809
810         scard = scard_init(SCARD_TRY_BOTH);
811         if (scard == NULL)
812                 return -1;
813         if (scard_set_pin(scard, "1234")) {
814                 wpa_printf(MSG_WARNING, "PIN validation failed");
815                 scard_deinit(scard);
816                 return -1;
817         }
818
819         len = sizeof(imsi);
820         if (scard_get_imsi(scard, imsi, &len))
821                 goto failed;
822         wpa_hexdump_ascii(MSG_DEBUG, "SCARD: IMSI", (u8 *) imsi, len);
823         /* NOTE: Permanent Username: 1 | IMSI */
824
825         os_memset(_rand, 0, sizeof(_rand));
826         if (scard_gsm_auth(scard, _rand, sres, kc))
827                 goto failed;
828
829         os_memset(_rand, 0xff, sizeof(_rand));
830         if (scard_gsm_auth(scard, _rand, sres, kc))
831                 goto failed;
832
833         for (i = 0; i < num_triplets; i++) {
834                 os_memset(rand_[i], i, sizeof(rand_[i]));
835                 if (scard_gsm_auth(scard, rand_[i], sres_[i], kc_[i]))
836                         goto failed;
837         }
838
839         for (i = 0; i < num_triplets; i++) {
840                 printf("1");
841                 for (j = 0; j < len; j++)
842                         printf("%c", imsi[j]);
843                 printf(",");
844                 for (j = 0; j < 16; j++)
845                         printf("%02X", rand_[i][j]);
846                 printf(",");
847                 for (j = 0; j < 4; j++)
848                         printf("%02X", sres_[i][j]);
849                 printf(",");
850                 for (j = 0; j < 8; j++)
851                         printf("%02X", kc_[i][j]);
852                 printf("\n");
853         }
854
855         wpa_printf(MSG_DEBUG, "Trying to use UMTS authentication");
856
857         /* seq 39 (0x28) */
858         os_memset(aka_rand, 0xaa, 16);
859         os_memcpy(aka_autn, "\x86\x71\x31\xcb\xa2\xfc\x61\xdf"
860                   "\xa3\xb3\x97\x9d\x07\x32\xa2\x12", 16);
861
862         res = scard_umts_auth(scard, aka_rand, aka_autn, aka_res, &aka_res_len,
863                               aka_ik, aka_ck, aka_auts);
864         if (res == 0) {
865                 wpa_printf(MSG_DEBUG, "UMTS auth completed successfully");
866                 wpa_hexdump(MSG_DEBUG, "RES", aka_res, aka_res_len);
867                 wpa_hexdump(MSG_DEBUG, "IK", aka_ik, IK_LEN);
868                 wpa_hexdump(MSG_DEBUG, "CK", aka_ck, CK_LEN);
869         } else if (res == -2) {
870                 wpa_printf(MSG_DEBUG, "UMTS auth resulted in synchronization "
871                            "failure");
872                 wpa_hexdump(MSG_DEBUG, "AUTS", aka_auts, AKA_AUTS_LEN);
873         } else {
874                 wpa_printf(MSG_DEBUG, "UMTS auth failed");
875         }
876
877 failed:
878         scard_deinit(scard);
879
880         return 0;
881 #undef num_triplets
882 }
883
884
885 static int scard_get_triplets(int argc, char *argv[])
886 {
887         struct scard_data *scard;
888         size_t len;
889         char imsi[20];
890         unsigned char _rand[16];
891         unsigned char sres[4];
892         unsigned char kc[8];
893         int num_triplets;
894         int i;
895         size_t j;
896
897         if (argc < 2 || ((num_triplets = atoi(argv[1])) <= 0)) {
898                 printf("invalid parameters for sim command\n");
899                 return -1;
900         }
901
902         if (argc <= 2 || os_strcmp(argv[2], "debug") != 0) {
903                 /* disable debug output */
904                 wpa_debug_level = 99;
905         }
906
907         scard = scard_init(SCARD_GSM_SIM_ONLY);
908         if (scard == NULL) {
909                 printf("Failed to open smartcard connection\n");
910                 return -1;
911         }
912         if (scard_set_pin(scard, argv[0])) {
913                 wpa_printf(MSG_WARNING, "PIN validation failed");
914                 scard_deinit(scard);
915                 return -1;
916         }
917
918         len = sizeof(imsi);
919         if (scard_get_imsi(scard, imsi, &len)) {
920                 scard_deinit(scard);
921                 return -1;
922         }
923
924         for (i = 0; i < num_triplets; i++) {
925                 os_memset(_rand, i, sizeof(_rand));
926                 if (scard_gsm_auth(scard, _rand, sres, kc))
927                         break;
928
929                 /* IMSI:Kc:SRES:RAND */
930                 for (j = 0; j < len; j++)
931                         printf("%c", imsi[j]);
932                 printf(":");
933                 for (j = 0; j < 8; j++)
934                         printf("%02X", kc[j]);
935                 printf(":");
936                 for (j = 0; j < 4; j++)
937                         printf("%02X", sres[j]);
938                 printf(":");
939                 for (j = 0; j < 16; j++)
940                         printf("%02X", _rand[j]);
941                 printf("\n");
942         }
943
944         scard_deinit(scard);
945
946         return 0;
947 }
948
949
950 static void eapol_test_terminate(int sig, void *eloop_ctx,
951                                  void *signal_ctx)
952 {
953         struct wpa_supplicant *wpa_s = eloop_ctx;
954         wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig);
955         eloop_terminate();
956 }
957
958
959 static void usage(void)
960 {
961         printf("usage:\n"
962                "eapol_test [-nWS] -c<conf> [-a<AS IP>] [-p<AS port>] "
963                "[-s<AS secret>]\\\n"
964                "           [-r<count>] [-t<timeout>] [-C<Connect-Info>] \\\n"
965                "           [-M<client MAC address>] \\\n"
966                "           [-N<attr spec>] \\\n"
967                "           [-A<client IP>]\n"
968                "eapol_test scard\n"
969                "eapol_test sim <PIN> <num triplets> [debug]\n"
970                "\n");
971         printf("options:\n"
972                "  -c<conf> = configuration file\n"
973                "  -a<AS IP> = IP address of the authentication server, "
974                "default 127.0.0.1\n"
975                "  -p<AS port> = UDP port of the authentication server, "
976                "default 1812\n"
977                "  -s<AS secret> = shared secret with the authentication "
978                "server, default 'radius'\n"
979                "  -A<client IP> = IP address of the client, default: select "
980                "automatically\n"
981                "  -r<count> = number of re-authentications\n"
982                "  -W = wait for a control interface monitor before starting\n"
983                "  -S = save configuration after authentication\n"
984                "  -n = no MPPE keys expected\n"
985                "  -t<timeout> = sets timeout in seconds (default: 30 s)\n"
986                "  -C<Connect-Info> = RADIUS Connect-Info (default: "
987                "CONNECT 11Mbps 802.11b)\n"
988                "  -M<client MAC address> = Set own MAC address "
989                "(Calling-Station-Id,\n"
990                "                           default: 02:00:00:00:00:01)\n"
991                "  -N<attr spec> = send arbitrary attribute specified by:\n"
992                "                  attr_id:syntax:value or attr_id\n"
993                "                  attr_id - number id of the attribute\n"
994                "                  syntax - one of: s, d, x\n"
995                "                     s = string\n"
996                "                     d = integer\n"
997                "                     x = octet string\n"
998                "                  value - attribute value.\n"
999                "       When only attr_id is specified, NULL will be used as "
1000                "value.\n"
1001                "       Multiple attributes can be specified by using the "
1002                "option several times.\n");
1003 }
1004
1005
1006 int main(int argc, char *argv[])
1007 {
1008         struct wpa_supplicant wpa_s;
1009         int c, ret = 1, wait_for_monitor = 0, save_config = 0;
1010         char *as_addr = "127.0.0.1";
1011         int as_port = 1812;
1012         char *as_secret = "radius";
1013         char *cli_addr = NULL;
1014         char *conf = NULL;
1015         int timeout = 30;
1016         char *pos;
1017         struct extra_radius_attr *p = NULL, *p1;
1018
1019         if (os_program_init())
1020                 return -1;
1021
1022         hostapd_logger_register_cb(hostapd_logger_cb);
1023
1024         os_memset(&eapol_test, 0, sizeof(eapol_test));
1025         eapol_test.connect_info = "CONNECT 11Mbps 802.11b";
1026         os_memcpy(eapol_test.own_addr, "\x02\x00\x00\x00\x00\x01", ETH_ALEN);
1027
1028         wpa_debug_level = 0;
1029         wpa_debug_show_keys = 1;
1030
1031         for (;;) {
1032                 c = getopt(argc, argv, "a:A:c:C:M:nN:p:r:s:St:W");
1033                 if (c < 0)
1034                         break;
1035                 switch (c) {
1036                 case 'a':
1037                         as_addr = optarg;
1038                         break;
1039                 case 'A':
1040                         cli_addr = optarg;
1041                         break;
1042                 case 'c':
1043                         conf = optarg;
1044                         break;
1045                 case 'C':
1046                         eapol_test.connect_info = optarg;
1047                         break;
1048                 case 'M':
1049                         if (hwaddr_aton(optarg, eapol_test.own_addr)) {
1050                                 usage();
1051                                 return -1;
1052                         }
1053                         break;
1054                 case 'n':
1055                         eapol_test.no_mppe_keys++;
1056                         break;
1057                 case 'p':
1058                         as_port = atoi(optarg);
1059                         break;
1060                 case 'r':
1061                         eapol_test.eapol_test_num_reauths = atoi(optarg);
1062                         break;
1063                 case 's':
1064                         as_secret = optarg;
1065                         break;
1066                 case 'S':
1067                         save_config++;
1068                         break;
1069                 case 't':
1070                         timeout = atoi(optarg);
1071                         break;
1072                 case 'W':
1073                         wait_for_monitor++;
1074                         break;
1075                 case 'N':
1076                         p1 = os_zalloc(sizeof(p1));
1077                         if (p1 == NULL)
1078                                 break;
1079                         if (!p)
1080                                 eapol_test.extra_attrs = p1;
1081                         else
1082                                 p->next = p1;
1083                         p = p1;
1084
1085                         p->type = atoi(optarg);
1086                         pos = os_strchr(optarg, ':');
1087                         if (pos == NULL) {
1088                                 p->syntax = 'n';
1089                                 p->data = NULL;
1090                                 break;
1091                         }
1092
1093                         pos++;
1094                         if (pos[0] == '\0' || pos[1] != ':') {
1095                                 printf("Incorrect format of attribute "
1096                                        "specification\n");
1097                                 break;
1098                         }
1099
1100                         p->syntax = pos[0];
1101                         p->data = pos + 2;
1102                         break;
1103                 default:
1104                         usage();
1105                         return -1;
1106                 }
1107         }
1108
1109         if (argc > optind && os_strcmp(argv[optind], "scard") == 0) {
1110                 return scard_test();
1111         }
1112
1113         if (argc > optind && os_strcmp(argv[optind], "sim") == 0) {
1114                 return scard_get_triplets(argc - optind - 1,
1115                                           &argv[optind + 1]);
1116         }
1117
1118         if (conf == NULL) {
1119                 usage();
1120                 printf("Configuration file is required.\n");
1121                 return -1;
1122         }
1123
1124         if (eap_register_methods()) {
1125                 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1126                 return -1;
1127         }
1128
1129         if (eloop_init(&wpa_s)) {
1130                 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1131                 return -1;
1132         }
1133
1134         os_memset(&wpa_s, 0, sizeof(wpa_s));
1135         eapol_test.wpa_s = &wpa_s;
1136         wpa_s.conf = wpa_config_read(conf);
1137         if (wpa_s.conf == NULL) {
1138                 printf("Failed to parse configuration file '%s'.\n", conf);
1139                 return -1;
1140         }
1141         if (wpa_s.conf->ssid == NULL) {
1142                 printf("No networks defined.\n");
1143                 return -1;
1144         }
1145
1146         wpa_init_conf(&eapol_test, &wpa_s, as_addr, as_port, as_secret,
1147                       cli_addr);
1148         wpa_s.ctrl_iface = wpa_supplicant_ctrl_iface_init(&wpa_s);
1149         if (wpa_s.ctrl_iface == NULL) {
1150                 printf("Failed to initialize control interface '%s'.\n"
1151                        "You may have another eapol_test process already "
1152                        "running or the file was\n"
1153                        "left by an unclean termination of eapol_test in "
1154                        "which case you will need\n"
1155                        "to manually remove this file before starting "
1156                        "eapol_test again.\n",
1157                        wpa_s.conf->ctrl_interface);
1158                 return -1;
1159         }
1160         if (wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid))
1161                 return -1;
1162
1163         if (test_eapol(&eapol_test, &wpa_s, wpa_s.conf->ssid))
1164                 return -1;
1165
1166         if (wait_for_monitor)
1167                 wpa_supplicant_ctrl_iface_wait(wpa_s.ctrl_iface);
1168
1169         eloop_register_timeout(timeout, 0, eapol_test_timeout, &eapol_test,
1170                                NULL);
1171         eloop_register_timeout(0, 0, send_eap_request_identity, &wpa_s, NULL);
1172         eloop_register_signal_terminate(eapol_test_terminate, NULL);
1173         eloop_register_signal_reconfig(eapol_test_terminate, NULL);
1174         eloop_run();
1175
1176         eloop_cancel_timeout(eapol_test_timeout, &eapol_test, NULL);
1177         eloop_cancel_timeout(eapol_sm_reauth, &eapol_test, NULL);
1178
1179         if (eapol_test_compare_pmk(&eapol_test) == 0 ||
1180             eapol_test.no_mppe_keys)
1181                 ret = 0;
1182         if (eapol_test.auth_timed_out)
1183                 ret = -2;
1184         if (eapol_test.radius_access_reject_received)
1185                 ret = -3;
1186
1187         if (save_config)
1188                 wpa_config_write(conf, wpa_s.conf);
1189
1190         test_eapol_clean(&eapol_test, &wpa_s);
1191
1192         eap_peer_unregister_methods();
1193
1194         eloop_destroy();
1195
1196         printf("MPPE keys OK: %d  mismatch: %d\n",
1197                eapol_test.num_mppe_ok, eapol_test.num_mppe_mismatch);
1198         if (eapol_test.num_mppe_mismatch)
1199                 ret = -4;
1200         if (ret)
1201                 printf("FAILURE\n");
1202         else
1203                 printf("SUCCESS\n");
1204
1205         os_program_deinit();
1206
1207         return ret;
1208 }