eapol_test.c

Go to the documentation of this file.
00001 
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 #include <stdarg.h>
00022 #include <unistd.h>
00023 #include <ctype.h>
00024 #include <string.h>
00025 #include <signal.h>
00026 #ifndef CONFIG_NATIVE_WINDOWS
00027 #include <netinet/in.h>
00028 #include <arpa/inet.h>
00029 #endif /* CONFIG_NATIVE_WINDOWS */
00030 #include <assert.h>
00031 
00032 #include "common.h"
00033 #include "config.h"
00034 #include "eapol_sm.h"
00035 #include "eloop.h"
00036 #include "wpa.h"
00037 #include "eap_i.h"
00038 #include "wpa_supplicant.h"
00039 #include "wpa_supplicant_i.h"
00040 #include "radius.h"
00041 #include "radius_client.h"
00042 #include "l2_packet.h"
00043 #include "ctrl_iface.h"
00044 #include "pcsc_funcs.h"
00045 
00046 
00047 extern int wpa_debug_level;
00048 extern int wpa_debug_show_keys;
00049 
00050 struct wpa_driver_ops *wpa_supplicant_drivers[] = { };
00051 
00052 
00053 struct eapol_test_data {
00054         struct wpa_supplicant *wpa_s;
00055 
00056         int eapol_test_num_reauths;
00057         int no_mppe_keys;
00058         int num_mppe_ok, num_mppe_mismatch;
00059 
00060         u8 radius_identifier;
00061         struct radius_msg *last_recv_radius;
00062         struct in_addr own_ip_addr;
00063         struct radius_client_data *radius;
00064         struct hostapd_radius_servers *radius_conf;
00065 
00066         u8 *last_eap_radius; /* last received EAP Response from Authentication
00067                               * Server */
00068         size_t last_eap_radius_len;
00069 
00070         u8 authenticator_pmk[PMK_LEN];
00071         size_t authenticator_pmk_len;
00072         int radius_access_accept_received;
00073         int radius_access_reject_received;
00074         int auth_timed_out;
00075 
00076         u8 *eap_identity;
00077         size_t eap_identity_len;
00078 };
00079 
00080 static struct eapol_test_data eapol_test;
00081 
00082 
00083 static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx);
00084 
00085 
00086 void hostapd_logger(void *ctx, const u8 *addr, unsigned int module, int level,
00087                     char *fmt, ...)
00088 {
00089         char *format;
00090         int maxlen;
00091         va_list ap;
00092 
00093         maxlen = strlen(fmt) + 100;
00094         format = malloc(maxlen);
00095         if (!format)
00096                 return;
00097 
00098         va_start(ap, fmt);
00099 
00100 
00101         if (addr)
00102                 snprintf(format, maxlen, "STA " MACSTR ": %s",
00103                          MAC2STR(addr), fmt);
00104         else
00105                 snprintf(format, maxlen, "%s", fmt);
00106 
00107         vprintf(format, ap);
00108         printf("\n");
00109 
00110         free(format);
00111 
00112         va_end(ap);
00113 }
00114 
00115 
00116 const char * hostapd_ip_txt(const struct hostapd_ip_addr *addr, char *buf,
00117                             size_t buflen)
00118 {
00119         if (buflen == 0 || addr == NULL)
00120                 return NULL;
00121 
00122         if (addr->af == AF_INET) {
00123                 snprintf(buf, buflen, "%s", inet_ntoa(addr->u.v4));
00124         } else {
00125                 buf[0] = '\0';
00126         }
00127 #ifdef CONFIG_IPV6
00128         if (addr->af == AF_INET6) {
00129                 if (inet_ntop(AF_INET6, &addr->u.v6, buf, buflen) == NULL)
00130                         buf[0] = '\0';
00131         }
00132 #endif /* CONFIG_IPV6 */
00133 
00134         return buf;
00135 }
00136 
00137 
00138 static void ieee802_1x_encapsulate_radius(struct eapol_test_data *e,
00139                                           const u8 *eap, size_t len)
00140 {
00141         struct radius_msg *msg;
00142         char buf[128];
00143         const struct eap_hdr *hdr;
00144         const u8 *pos;
00145 
00146         wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
00147                    "packet");
00148 
00149         e->radius_identifier = radius_client_get_id(e->radius);
00150         msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
00151                              e->radius_identifier);
00152         if (msg == NULL) {
00153                 printf("Could not create net RADIUS packet\n");
00154                 return;
00155         }
00156 
00157         radius_msg_make_authenticator(msg, (u8 *) e, sizeof(*e));
00158 
00159         hdr = (const struct eap_hdr *) eap;
00160         pos = (const u8 *) (hdr + 1);
00161         if (len > sizeof(*hdr) && hdr->code == EAP_CODE_RESPONSE &&
00162             pos[0] == EAP_TYPE_IDENTITY) {
00163                 pos++;
00164                 free(e->eap_identity);
00165                 e->eap_identity_len = len - sizeof(*hdr) - 1;
00166                 e->eap_identity = malloc(e->eap_identity_len);
00167                 if (e->eap_identity) {
00168                         memcpy(e->eap_identity, pos, e->eap_identity_len);
00169                         wpa_hexdump(MSG_DEBUG, "Learned identity from "
00170                                     "EAP-Response-Identity",
00171                                     e->eap_identity, e->eap_identity_len);
00172                 }
00173         }
00174 
00175         if (e->eap_identity &&
00176             !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
00177                                  e->eap_identity, e->eap_identity_len)) {
00178                 printf("Could not add User-Name\n");
00179                 goto fail;
00180         }
00181 
00182         if (!radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
00183                                  (u8 *) &e->own_ip_addr, 4)) {
00184                 printf("Could not add NAS-IP-Address\n");
00185                 goto fail;
00186         }
00187 
00188         snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
00189                  MAC2STR(e->wpa_s->own_addr));
00190         if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
00191                                  (u8 *) buf, strlen(buf))) {
00192                 printf("Could not add Calling-Station-Id\n");
00193                 goto fail;
00194         }
00195 
00196         /* TODO: should probably check MTU from driver config; 2304 is max for
00197          * IEEE 802.11, but use 1400 to avoid problems with too large packets
00198          */
00199         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
00200                 printf("Could not add Framed-MTU\n");
00201                 goto fail;
00202         }
00203 
00204         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
00205                                        RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
00206                 printf("Could not add NAS-Port-Type\n");
00207                 goto fail;
00208         }
00209 
00210         snprintf(buf, sizeof(buf), "CONNECT 11Mbps 802.11b");
00211         if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
00212                                  (u8 *) buf, strlen(buf))) {
00213                 printf("Could not add Connect-Info\n");
00214                 goto fail;
00215         }
00216 
00217         if (eap && !radius_msg_add_eap(msg, eap, len)) {
00218                 printf("Could not add EAP-Message\n");
00219                 goto fail;
00220         }
00221 
00222         /* State attribute must be copied if and only if this packet is
00223          * Access-Request reply to the previous Access-Challenge */
00224         if (e->last_recv_radius && e->last_recv_radius->hdr->code ==
00225             RADIUS_CODE_ACCESS_CHALLENGE) {
00226                 int res = radius_msg_copy_attr(msg, e->last_recv_radius,
00227                                                RADIUS_ATTR_STATE);
00228                 if (res < 0) {
00229                         printf("Could not copy State attribute from previous "
00230                                "Access-Challenge\n");
00231                         goto fail;
00232                 }
00233                 if (res > 0) {
00234                         wpa_printf(MSG_DEBUG, "  Copied RADIUS State "
00235                                    "Attribute");
00236                 }
00237         }
00238 
00239         radius_client_send(e->radius, msg, RADIUS_AUTH, e->wpa_s->own_addr);
00240         return;
00241 
00242  fail:
00243         radius_msg_free(msg);
00244         free(msg);
00245 }
00246 
00247 
00248 static int eapol_test_eapol_send(void *ctx, int type, const u8 *buf,
00249                                  size_t len)
00250 {
00251         /* struct wpa_supplicant *wpa_s = ctx; */
00252         printf("WPA: eapol_test_eapol_send(type=%d len=%d)\n", type, len);
00253         if (type == IEEE802_1X_TYPE_EAP_PACKET) {
00254                 wpa_hexdump(MSG_DEBUG, "TX EAP -> RADIUS", buf, len);
00255                 ieee802_1x_encapsulate_radius(&eapol_test, buf, len);
00256         }
00257         return 0;
00258 }
00259 
00260 
00261 static void eapol_test_set_config_blob(void *ctx,
00262                                        struct wpa_config_blob *blob)
00263 {
00264         struct wpa_supplicant *wpa_s = ctx;
00265         wpa_config_set_blob(wpa_s->conf, blob);
00266 }
00267 
00268 
00269 static const struct wpa_config_blob *
00270 eapol_test_get_config_blob(void *ctx, const char *name)
00271 {
00272         struct wpa_supplicant *wpa_s = ctx;
00273         return wpa_config_get_blob(wpa_s->conf, name);
00274 }
00275 
00276 
00277 static void eapol_test_eapol_done_cb(void *ctx)
00278 {
00279         printf("WPA: EAPOL processing complete\n");
00280 }
00281 
00282 
00283 static void eapol_sm_reauth(void *eloop_ctx, void *timeout_ctx)
00284 {
00285         struct eapol_test_data *e = eloop_ctx;
00286         printf("\n\n\n\n\neapol_test: Triggering EAP reauthentication\n\n");
00287         e->radius_access_accept_received = 0;
00288         send_eap_request_identity(e->wpa_s, NULL);
00289 }
00290 
00291 
00292 static int eapol_test_compare_pmk(struct eapol_test_data *e)
00293 {
00294         u8 pmk[PMK_LEN];
00295         int ret = 1;
00296 
00297         if (eapol_sm_get_key(e->wpa_s->eapol, pmk, PMK_LEN) == 0) {
00298                 wpa_hexdump(MSG_DEBUG, "PMK from EAPOL", pmk, PMK_LEN);
00299                 if (memcmp(pmk, e->authenticator_pmk, PMK_LEN) != 0)
00300                         printf("WARNING: PMK mismatch\n");
00301                 else if (e->radius_access_accept_received)
00302                         ret = 0;
00303         } else if (e->authenticator_pmk_len == 16 &&
00304                    eapol_sm_get_key(e->wpa_s->eapol, pmk, 16) == 0) {
00305                 wpa_hexdump(MSG_DEBUG, "LEAP PMK from EAPOL", pmk, 16);
00306                 if (memcmp(pmk, e->authenticator_pmk, 16) != 0)
00307                         printf("WARNING: PMK mismatch\n");
00308                 else if (e->radius_access_accept_received)
00309                         ret = 0;
00310         } else if (e->radius_access_accept_received && e->no_mppe_keys) {
00311                 /* No keying material expected */
00312                 ret = 0;
00313         }
00314 
00315         if (ret)
00316                 e->num_mppe_mismatch++;
00317         else if (!e->no_mppe_keys)
00318                 e->num_mppe_ok++;
00319 
00320         return ret;
00321 }
00322 
00323 
00324 static void eapol_sm_cb(struct eapol_sm *eapol, int success, void *ctx)
00325 {
00326         struct eapol_test_data *e = ctx;
00327         printf("eapol_sm_cb: success=%d\n", success);
00328         e->eapol_test_num_reauths--;
00329         if (e->eapol_test_num_reauths < 0)
00330                 eloop_terminate();
00331         else {
00332                 eapol_test_compare_pmk(e);
00333                 eloop_register_timeout(0, 100000, eapol_sm_reauth, e, NULL);
00334         }
00335 }
00336 
00337 
00338 static int test_eapol(struct eapol_test_data *e, struct wpa_supplicant *wpa_s,
00339                       struct wpa_ssid *ssid)
00340 {
00341         struct eapol_config eapol_conf;
00342         struct eapol_ctx *ctx;
00343 
00344         ctx = malloc(sizeof(*ctx));
00345         if (ctx == NULL) {
00346                 printf("Failed to allocate EAPOL context.\n");
00347                 return -1;
00348         }
00349         memset(ctx, 0, sizeof(*ctx));
00350         ctx->ctx = wpa_s;
00351         ctx->msg_ctx = wpa_s;
00352         ctx->scard_ctx = wpa_s->scard;
00353         ctx->cb = eapol_sm_cb;
00354         ctx->cb_ctx = e;
00355         ctx->eapol_send_ctx = wpa_s;
00356         ctx->preauth = 0;
00357         ctx->eapol_done_cb = eapol_test_eapol_done_cb;
00358         ctx->eapol_send = eapol_test_eapol_send;
00359         ctx->set_config_blob = eapol_test_set_config_blob;
00360         ctx->get_config_blob = eapol_test_get_config_blob;
00361         ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
00362         ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
00363         ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
00364 
00365         wpa_s->eapol = eapol_sm_init(ctx);
00366         if (wpa_s->eapol == NULL) {
00367                 free(ctx);
00368                 printf("Failed to initialize EAPOL state machines.\n");
00369                 return -1;
00370         }
00371 
00372         wpa_s->current_ssid = ssid;
00373         memset(&eapol_conf, 0, sizeof(eapol_conf));
00374         eapol_conf.accept_802_1x_keys = 1;
00375         eapol_conf.required_keys = 0;
00376         eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
00377         eapol_conf.workaround = ssid->eap_workaround;
00378         eapol_sm_notify_config(wpa_s->eapol, ssid, &eapol_conf);
00379         eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
00380 
00381 
00382         eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
00383         /* 802.1X::portControl = Auto */
00384         eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
00385 
00386         return 0;
00387 }
00388 
00389 
00390 static void test_eapol_clean(struct eapol_test_data *e,
00391                              struct wpa_supplicant *wpa_s)
00392 {
00393         radius_client_deinit(e->radius);
00394         free(e->last_eap_radius);
00395         if (e->last_recv_radius) {
00396                 radius_msg_free(e->last_recv_radius);
00397                 free(e->last_recv_radius);
00398         }
00399         free(e->eap_identity);
00400         e->eap_identity = NULL;
00401         eapol_sm_deinit(wpa_s->eapol);
00402         wpa_s->eapol = NULL;
00403         if (e->radius_conf && e->radius_conf->auth_server) {
00404                 free(e->radius_conf->auth_server->shared_secret);
00405                 free(e->radius_conf->auth_server);
00406         }
00407         free(e->radius_conf);
00408         e->radius_conf = NULL;
00409         scard_deinit(wpa_s->scard);
00410         wpa_supplicant_ctrl_iface_deinit(wpa_s);
00411         wpa_config_free(wpa_s->conf);
00412 }
00413 
00414 
00415 static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx)
00416 {
00417         struct wpa_supplicant *wpa_s = eloop_ctx;
00418         u8 buf[100], *pos;
00419         struct ieee802_1x_hdr *hdr;
00420         struct eap_hdr *eap;
00421 
00422         hdr = (struct ieee802_1x_hdr *) buf;
00423         hdr->version = EAPOL_VERSION;
00424         hdr->type = IEEE802_1X_TYPE_EAP_PACKET;
00425         hdr->length = htons(5);
00426 
00427         eap = (struct eap_hdr *) (hdr + 1);
00428         eap->code = EAP_CODE_REQUEST;
00429         eap->identifier = 0;
00430         eap->length = htons(5);
00431         pos = (u8 *) (eap + 1);
00432         *pos = EAP_TYPE_IDENTITY;
00433 
00434         printf("Sending fake EAP-Request-Identity\n");
00435         eapol_sm_rx_eapol(wpa_s->eapol, wpa_s->bssid, buf,
00436                           sizeof(*hdr) + 5);
00437 }
00438 
00439 
00440 static void eapol_test_timeout(void *eloop_ctx, void *timeout_ctx)
00441 {
00442         struct eapol_test_data *e = eloop_ctx;
00443         printf("EAPOL test timed out\n");
00444         e->auth_timed_out = 1;
00445         eloop_terminate();
00446 }
00447 
00448 
00449 static char *eap_type_text(u8 type)
00450 {
00451         switch (type) {
00452         case EAP_TYPE_IDENTITY: return "Identity";
00453         case EAP_TYPE_NOTIFICATION: return "Notification";
00454         case EAP_TYPE_NAK: return "Nak";
00455         case EAP_TYPE_TLS: return "TLS";
00456         case EAP_TYPE_TTLS: return "TTLS";
00457         case EAP_TYPE_PEAP: return "PEAP";
00458         case EAP_TYPE_SIM: return "SIM";
00459         case EAP_TYPE_GTC: return "GTC";
00460         case EAP_TYPE_MD5: return "MD5";
00461         case EAP_TYPE_OTP: return "OTP";
00462         default: return "Unknown";
00463         }
00464 }
00465 
00466 
00467 static void ieee802_1x_decapsulate_radius(struct eapol_test_data *e)
00468 {
00469         u8 *eap;
00470         size_t len;
00471         struct eap_hdr *hdr;
00472         int eap_type = -1;
00473         char buf[64];
00474         struct radius_msg *msg;
00475 
00476         if (e->last_recv_radius == NULL)
00477                 return;
00478 
00479         msg = e->last_recv_radius;
00480 
00481         eap = radius_msg_get_eap(msg, &len);
00482         if (eap == NULL) {
00483                 /* draft-aboba-radius-rfc2869bis-20.txt, Chap. 2.6.3:
00484                  * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
00485                  * attribute */
00486                 wpa_printf(MSG_DEBUG, "could not extract "
00487                                "EAP-Message from RADIUS message");
00488                 free(e->last_eap_radius);
00489                 e->last_eap_radius = NULL;
00490                 e->last_eap_radius_len = 0;
00491                 return;
00492         }
00493 
00494         if (len < sizeof(*hdr)) {
00495                 wpa_printf(MSG_DEBUG, "too short EAP packet "
00496                                "received from authentication server");
00497                 free(eap);
00498                 return;
00499         }
00500 
00501         if (len > sizeof(*hdr))
00502                 eap_type = eap[sizeof(*hdr)];
00503 
00504         hdr = (struct eap_hdr *) eap;
00505         switch (hdr->code) {
00506         case EAP_CODE_REQUEST:
00507                 snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
00508                          eap_type >= 0 ? eap_type_text(eap_type) : "??",
00509                          eap_type);
00510                 break;
00511         case EAP_CODE_RESPONSE:
00512                 snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
00513                          eap_type >= 0 ? eap_type_text(eap_type) : "??",
00514                          eap_type);
00515                 break;
00516         case EAP_CODE_SUCCESS:
00517                 snprintf(buf, sizeof(buf), "EAP Success");
00518                 /* LEAP uses EAP Success within an authentication, so must not
00519                  * stop here with eloop_terminate(); */
00520                 break;
00521         case EAP_CODE_FAILURE:
00522                 snprintf(buf, sizeof(buf), "EAP Failure");
00523                 eloop_terminate();
00524                 break;
00525         default:
00526                 snprintf(buf, sizeof(buf), "unknown EAP code");
00527                 wpa_hexdump(MSG_DEBUG, "Decapsulated EAP packet", eap, len);
00528                 break;
00529         }
00530         wpa_printf(MSG_DEBUG, "decapsulated EAP packet (code=%d "
00531                        "id=%d len=%d) from RADIUS server: %s",
00532                       hdr->code, hdr->identifier, ntohs(hdr->length), buf);
00533 
00534         /* sta->eapol_sm->be_auth.idFromServer = hdr->identifier; */
00535 
00536         free(e->last_eap_radius);
00537         e->last_eap_radius = eap;
00538         e->last_eap_radius_len = len;
00539 
00540         {
00541                 struct ieee802_1x_hdr *hdr;
00542                 hdr = malloc(sizeof(*hdr) + len);
00543                 assert(hdr != NULL);
00544                 hdr->version = EAPOL_VERSION;
00545                 hdr->type = IEEE802_1X_TYPE_EAP_PACKET;
00546                 hdr->length = htons(len);
00547                 memcpy((u8 *) (hdr + 1), eap, len);
00548                 eapol_sm_rx_eapol(e->wpa_s->eapol, e->wpa_s->bssid,
00549                                   (u8 *) hdr, sizeof(*hdr) + len);
00550                 free(hdr);
00551         }
00552 }
00553 
00554 
00555 static void ieee802_1x_get_keys(struct eapol_test_data *e,
00556                                 struct radius_msg *msg, struct radius_msg *req,
00557                                 u8 *shared_secret, size_t shared_secret_len)
00558 {
00559         struct radius_ms_mppe_keys *keys;
00560 
00561         keys = radius_msg_get_ms_keys(msg, req, shared_secret,
00562                                       shared_secret_len);
00563         if (keys && keys->send == NULL && keys->recv == NULL) {
00564                 free(keys);
00565                 keys = radius_msg_get_cisco_keys(msg, req, shared_secret,
00566                                                  shared_secret_len);
00567         }
00568 
00569         if (keys) {
00570                 if (keys->send) {
00571                         wpa_hexdump(MSG_DEBUG, "MS-MPPE-Send-Key (sign)",
00572                                     keys->send, keys->send_len);
00573                 }
00574                 if (keys->recv) {
00575                         wpa_hexdump(MSG_DEBUG, "MS-MPPE-Recv-Key (crypt)",
00576                                     keys->recv, keys->recv_len);
00577                         e->authenticator_pmk_len =
00578                                 keys->recv_len > PMK_LEN ? PMK_LEN :
00579                                 keys->recv_len;
00580                         memcpy(e->authenticator_pmk, keys->recv,
00581                                e->authenticator_pmk_len);
00582                 }
00583 
00584                 free(keys->send);
00585                 free(keys->recv);
00586                 free(keys);
00587         }
00588 }
00589 
00590 
00591 /* Process the RADIUS frames from Authentication Server */
00592 static RadiusRxResult
00593 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
00594                         u8 *shared_secret, size_t shared_secret_len,
00595                         void *data)
00596 {
00597         struct eapol_test_data *e = data;
00598 
00599         /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
00600          * present when packet contains an EAP-Message attribute */
00601         if (msg->hdr->code == RADIUS_CODE_ACCESS_REJECT &&
00602             radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
00603                                 0) < 0 &&
00604             radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
00605                 wpa_printf(MSG_DEBUG, "Allowing RADIUS "
00606                               "Access-Reject without Message-Authenticator "
00607                               "since it does not include EAP-Message\n");
00608         } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
00609                                      req, 1)) {
00610                 printf("Incoming RADIUS packet did not have correct "
00611                        "Message-Authenticator - dropped\n");
00612                 return RADIUS_RX_UNKNOWN;
00613         }
00614 
00615         if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
00616             msg->hdr->code != RADIUS_CODE_ACCESS_REJECT &&
00617             msg->hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
00618                 printf("Unknown RADIUS message code\n");
00619                 return RADIUS_RX_UNKNOWN;
00620         }
00621 
00622         e->radius_identifier = -1;
00623         wpa_printf(MSG_DEBUG, "RADIUS packet matching with station");
00624 
00625         if (e->last_recv_radius) {
00626                 radius_msg_free(e->last_recv_radius);
00627                 free(e->last_recv_radius);
00628         }
00629 
00630         e->last_recv_radius = msg;
00631 
00632         switch (msg->hdr->code) {
00633         case RADIUS_CODE_ACCESS_ACCEPT:
00634                 e->radius_access_accept_received = 1;
00635                 ieee802_1x_get_keys(e, msg, req, shared_secret,
00636                                     shared_secret_len);
00637                 break;
00638         case RADIUS_CODE_ACCESS_REJECT:
00639                 e->radius_access_reject_received = 1;
00640                 break;
00641         }
00642 
00643         ieee802_1x_decapsulate_radius(e);
00644 
00645         if ((msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
00646              e->eapol_test_num_reauths < 0) ||
00647             msg->hdr->code == RADIUS_CODE_ACCESS_REJECT) {
00648                 eloop_terminate();
00649         }
00650 
00651         return RADIUS_RX_QUEUED;
00652 }
00653 
00654 
00655 static void wpa_init_conf(struct eapol_test_data *e,
00656                           struct wpa_supplicant *wpa_s, const char *authsrv,
00657                           int port, const char *secret)
00658 {
00659         struct hostapd_radius_server *as;
00660         int res;
00661 
00662         wpa_s->bssid[5] = 1;
00663         wpa_s->own_addr[5] = 2;
00664         e->own_ip_addr.s_addr = htonl((127 << 24) | 1);
00665         strncpy(wpa_s->ifname, "test", sizeof(wpa_s->ifname));
00666 
00667         e->radius_conf = malloc(sizeof(struct hostapd_radius_servers));
00668         assert(e->radius_conf != NULL);
00669         memset(e->radius_conf, 0, sizeof(struct hostapd_radius_servers));
00670         e->radius_conf->num_auth_servers = 1;
00671         as = malloc(sizeof(struct hostapd_radius_server));
00672         assert(as != NULL);
00673         memset(as, 0, sizeof(*as));
00674 #ifdef CONFIG_NATIVE_WINDOWS
00675         {
00676                 int a[4];
00677                 u8 *pos;
00678                 sscanf(authsrv, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
00679                 pos = (u8 *) &as->addr.u.v4;
00680                 *pos++ = a[0];
00681                 *pos++ = a[1];
00682                 *pos++ = a[2];
00683                 *pos++ = a[3];
00684         }
00685 #else /* CONFIG_NATIVE_WINDOWS */
00686         inet_aton(authsrv, &as->addr.u.v4);
00687 #endif /* CONFIG_NATIVE_WINDOWS */
00688         as->addr.af = AF_INET;
00689         as->port = port;
00690         as->shared_secret = (u8 *) strdup(secret);
00691         as->shared_secret_len = strlen(secret);
00692         e->radius_conf->auth_server = as;
00693         e->radius_conf->auth_servers = as;
00694         e->radius_conf->msg_dumps = 1;
00695 
00696         e->radius = radius_client_init(wpa_s, e->radius_conf);
00697         assert(e->radius != NULL);
00698 
00699         res = radius_client_register(e->radius, RADIUS_AUTH,
00700                                      ieee802_1x_receive_auth, e);
00701         assert(res == 0);
00702 }
00703 
00704 
00705 static int scard_test(void)
00706 {
00707         struct scard_data *scard;
00708         size_t len;
00709         char imsi[20];
00710         unsigned char rand[16];
00711 #ifdef PCSC_FUNCS
00712         unsigned char sres[4];
00713         unsigned char kc[8];
00714 #endif /* PCSC_FUNCS */
00715 #define num_triplets 5
00716         unsigned char rand_[num_triplets][16];
00717         unsigned char sres_[num_triplets][4];
00718         unsigned char kc_[num_triplets][8];
00719         int i, j, res;
00720 
00721 #define AKA_RAND_LEN 16
00722 #define AKA_AUTN_LEN 16
00723 #define AKA_AUTS_LEN 14
00724 #define RES_MAX_LEN 16
00725 #define IK_LEN 16
00726 #define CK_LEN 16
00727         unsigned char aka_rand[AKA_RAND_LEN];
00728         unsigned char aka_autn[AKA_AUTN_LEN];
00729         unsigned char aka_auts[AKA_AUTS_LEN];
00730         unsigned char aka_res[RES_MAX_LEN];
00731         size_t aka_res_len;
00732         unsigned char aka_ik[IK_LEN];
00733         unsigned char aka_ck[CK_LEN];
00734 
00735         scard = scard_init(SCARD_TRY_BOTH);
00736         if (scard == NULL)
00737                 return -1;
00738         if (scard_set_pin(scard, "1234")) {
00739                 wpa_printf(MSG_WARNING, "PIN validation failed");
00740                 scard_deinit(scard);
00741                 return -1;
00742         }
00743 
00744         len = sizeof(imsi);
00745         if (scard_get_imsi(scard, imsi, &len))
00746                 goto failed;
00747         wpa_hexdump_ascii(MSG_DEBUG, "SCARD: IMSI", (u8 *) imsi, len);
00748         /* NOTE: Permanent Username: 1 | IMSI */
00749 
00750         memset(rand, 0, sizeof(rand));
00751         if (scard_gsm_auth(scard, rand, sres, kc))
00752                 goto failed;
00753 
00754         memset(rand, 0xff, sizeof(rand));
00755         if (scard_gsm_auth(scard, rand, sres, kc))
00756                 goto failed;
00757 
00758         for (i = 0; i < num_triplets; i++) {
00759                 memset(rand_[i], i, sizeof(rand_[i]));
00760                 if (scard_gsm_auth(scard, rand_[i], sres_[i], kc_[i]))
00761                         goto failed;
00762         }
00763 
00764         for (i = 0; i < num_triplets; i++) {
00765                 printf("1");
00766                 for (j = 0; j < len; j++)
00767                         printf("%c", imsi[j]);
00768                 printf(",");
00769                 for (j = 0; j < 16; j++)
00770                         printf("%02X", rand_[i][j]);
00771                 printf(",");
00772                 for (j = 0; j < 4; j++)
00773                         printf("%02X", sres_[i][j]);
00774                 printf(",");
00775                 for (j = 0; j < 8; j++)
00776                         printf("%02X", kc_[i][j]);
00777                 printf("\n");
00778         }
00779 
00780         wpa_printf(MSG_DEBUG, "Trying to use UMTS authentication");
00781 
00782         /* seq 39 (0x28) */
00783         memset(aka_rand, 0xaa, 16);
00784         memcpy(aka_autn, "\x86\x71\x31\xcb\xa2\xfc\x61\xdf"
00785                "\xa3\xb3\x97\x9d\x07\x32\xa2\x12", 16);
00786 
00787         res = scard_umts_auth(scard, aka_rand, aka_autn, aka_res, &aka_res_len,
00788                               aka_ik, aka_ck, aka_auts);
00789         if (res == 0) {
00790                 wpa_printf(MSG_DEBUG, "UMTS auth completed successfully");
00791                 wpa_hexdump(MSG_DEBUG, "RES", aka_res, aka_res_len);
00792                 wpa_hexdump(MSG_DEBUG, "IK", aka_ik, IK_LEN);
00793                 wpa_hexdump(MSG_DEBUG, "CK", aka_ck, CK_LEN);
00794         } else if (res == -2) {
00795                 wpa_printf(MSG_DEBUG, "UMTS auth resulted in synchronization "
00796                            "failure");
00797                 wpa_hexdump(MSG_DEBUG, "AUTS", aka_auts, AKA_AUTS_LEN);
00798         } else {
00799                 wpa_printf(MSG_DEBUG, "UMTS auth failed");
00800         }
00801 
00802 failed:
00803         scard_deinit(scard);
00804 
00805         return 0;
00806 #undef num_triplets
00807 }
00808 
00809 
00810 static int scard_get_triplets(int argc, char *argv[])
00811 {
00812         struct scard_data *scard;
00813         size_t len;
00814         char imsi[20];
00815         unsigned char rand[16];
00816         unsigned char sres[4];
00817         unsigned char kc[8];
00818         int num_triplets;
00819         int i, j;
00820 
00821         if (argc < 2 || ((num_triplets = atoi(argv[1])) <= 0)) {
00822                 printf("invalid parameters for sim command\n");
00823                 return -1;
00824         }
00825 
00826         if (argc <= 2 || strcmp(argv[2], "debug") != 0) {
00827                 /* disable debug output */
00828                 wpa_debug_level = 99;
00829         }
00830 
00831         scard = scard_init(SCARD_GSM_SIM_ONLY);
00832         if (scard == NULL) {
00833                 printf("Failed to open smartcard connection\n");
00834                 return -1;
00835         }
00836         if (scard_set_pin(scard, argv[0])) {
00837                 wpa_printf(MSG_WARNING, "PIN validation failed");
00838                 scard_deinit(scard);
00839                 return -1;
00840         }
00841 
00842         len = sizeof(imsi);
00843         if (scard_get_imsi(scard, imsi, &len)) {
00844                 scard_deinit(scard);
00845                 return -1;
00846         }
00847 
00848         for (i = 0; i < num_triplets; i++) {
00849                 memset(rand, i, sizeof(rand));
00850                 if (scard_gsm_auth(scard, rand, sres, kc))
00851                         break;
00852 
00853                 /* IMSI:Kc:SRES:RAND */
00854                 for (j = 0; j < len; j++)
00855                         printf("%c", imsi[j]);
00856                 printf(":");
00857                 for (j = 0; j < 8; j++)
00858                         printf("%02X", kc[j]);
00859                 printf(":");
00860                 for (j = 0; j < 4; j++)
00861                         printf("%02X", sres[j]);
00862                 printf(":");
00863                 for (j = 0; j < 16; j++)
00864                         printf("%02X", rand[j]);
00865                 printf("\n");
00866         }
00867 
00868         scard_deinit(scard);
00869 
00870         return 0;
00871 }
00872 
00873 
00874 static void eapol_test_terminate(int sig, void *eloop_ctx,
00875                                  void *signal_ctx)
00876 {
00877         struct wpa_supplicant *wpa_s = eloop_ctx;
00878         wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig);
00879         eloop_terminate();
00880 }
00881 
00882 
00883 static void usage(void)
00884 {
00885         printf("usage:\n"
00886                "eapol_test [-nWS] -c<conf> [-a<AS IP>] [-p<AS port>] "
00887                "[-s<AS secret>] [-r<count>]\n"
00888                "eapol_test scard\n"
00889                "eapol_test sim <PIN> <num triplets> [debug]\n"
00890                "\n"
00891                "options:\n"
00892                "  -c<conf> = configuration file\n"
00893                "  -a<AS IP> = IP address of the authentication server, "
00894                "default 127.0.0.1\n"
00895                "  -p<AS port> = UDP port of the authentication server, "
00896                "default 1812\n"
00897                "  -s<AS secret> = shared secret with the authentication "
00898                "server, default 'radius'\n"
00899                "  -r<count> = number of re-authentications\n"
00900                "  -W = wait for a control interface monitor before starting\n"
00901                "  -S = save configuration after authentiation\n"
00902                "  -n = no MPPE keys expected\n");
00903 }
00904 
00905 
00906 int main(int argc, char *argv[])
00907 {
00908         struct wpa_supplicant wpa_s;
00909         int c, ret = 1, wait_for_monitor = 0, save_config = 0;
00910         char *as_addr = "127.0.0.1";
00911         int as_port = 1812;
00912         char *as_secret = "radius";
00913         char *conf = NULL;
00914 
00915 #ifdef CONFIG_NATIVE_WINDOWS
00916         WSADATA wsaData;
00917         if (WSAStartup(MAKEWORD(2, 0), &wsaData)) {
00918                 printf("Could not find a usable WinSock.dll\n");
00919                 return -1;
00920         }
00921 #endif /* CONFIG_NATIVE_WINDOWS */
00922 
00923         memset(&eapol_test, 0, sizeof(eapol_test));
00924 
00925         wpa_debug_level = 0;
00926         wpa_debug_show_keys = 1;
00927 
00928         for (;;) {
00929                 c = getopt(argc, argv, "a:c:np:r:s:SW");
00930                 if (c < 0)
00931                         break;
00932                 switch (c) {
00933                 case 'a':
00934                         as_addr = optarg;
00935                         break;
00936                 case 'c':
00937                         conf = optarg;
00938                         break;
00939                 case 'n':
00940                         eapol_test.no_mppe_keys++;
00941                         break;
00942                 case 'p':
00943                         as_port = atoi(optarg);
00944                         break;
00945                 case 'r':
00946                         eapol_test.eapol_test_num_reauths = atoi(optarg);
00947                         break;
00948                 case 's':
00949                         as_secret = optarg;
00950                         break;
00951                 case 'S':
00952                         save_config++;
00953                         break;
00954                 case 'W':
00955                         wait_for_monitor++;
00956                         break;
00957                 default:
00958                         usage();
00959                         return -1;
00960                 }
00961         }
00962 
00963         if (argc > optind && strcmp(argv[optind], "scard") == 0) {
00964                 return scard_test();
00965         }
00966 
00967         if (argc > optind && strcmp(argv[optind], "sim") == 0) {
00968                 return scard_get_triplets(argc - optind - 1,
00969                                           &argv[optind + 1]);
00970         }
00971 
00972         if (conf == NULL) {
00973                 usage();
00974                 printf("Configuration file is required.\n");
00975                 return -1;
00976         }
00977 
00978         eloop_init(&wpa_s);
00979 
00980         memset(&wpa_s, 0, sizeof(wpa_s));
00981         eapol_test.wpa_s = &wpa_s;
00982         wpa_s.conf = wpa_config_read(conf);
00983         if (wpa_s.conf == NULL) {
00984                 printf("Failed to parse configuration file '%s'.\n", conf);
00985                 return -1;
00986         }
00987         if (wpa_s.conf->ssid == NULL) {
00988                 printf("No networks defined.\n");
00989                 return -1;
00990         }
00991 
00992         wpa_init_conf(&eapol_test, &wpa_s, as_addr, as_port, as_secret);
00993         if (wpa_supplicant_ctrl_iface_init(&wpa_s)) {
00994                 printf("Failed to initialize control interface '%s'.\n"
00995                        "You may have another eapol_test process already "
00996                        "running or the file was\n"
00997                        "left by an unclean termination of eapol_test in "
00998                        "which case you will need\n"
00999                        "to manually remove this file before starting "
01000                        "eapol_test again.\n",
01001                        wpa_s.conf->ctrl_interface);
01002                 return -1;
01003         }
01004         if (wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid))
01005                 return -1;
01006 
01007         if (test_eapol(&eapol_test, &wpa_s, wpa_s.conf->ssid))
01008                 return -1;
01009 
01010         if (wait_for_monitor)
01011                 wpa_supplicant_ctrl_iface_wait(&wpa_s);
01012 
01013         eloop_register_timeout(30, 0, eapol_test_timeout, &eapol_test, NULL);
01014         eloop_register_timeout(0, 0, send_eap_request_identity, &wpa_s, NULL);
01015         eloop_register_signal(SIGINT, eapol_test_terminate, NULL);
01016         eloop_register_signal(SIGTERM, eapol_test_terminate, NULL);
01017 #ifndef CONFIG_NATIVE_WINDOWS
01018         eloop_register_signal(SIGHUP, eapol_test_terminate, NULL);
01019 #endif /* CONFIG_NATIVE_WINDOWS */
01020         eloop_run();
01021 
01022         if (eapol_test_compare_pmk(&eapol_test) == 0)
01023                 ret = 0;
01024         if (eapol_test.auth_timed_out)
01025                 ret = -2;
01026         if (eapol_test.radius_access_reject_received)
01027                 ret = -3;
01028 
01029         if (save_config)
01030                 wpa_config_write(conf, wpa_s.conf);
01031 
01032         test_eapol_clean(&eapol_test, &wpa_s);
01033 
01034         eloop_destroy();
01035 
01036         printf("MPPE keys OK: %d  mismatch: %d\n",
01037                eapol_test.num_mppe_ok, eapol_test.num_mppe_mismatch);
01038         if (eapol_test.num_mppe_mismatch)
01039                 ret = -4;
01040         if (ret)
01041                 printf("FAILURE\n");
01042         else
01043                 printf("SUCCESS\n");
01044 
01045 #ifdef CONFIG_NATIVE_WINDOWS
01046         WSACleanup();
01047 #endif /* CONFIG_NATIVE_WINDOWS */
01048 
01049         return ret;
01050 }
01051 

Generated on Sat May 6 21:13:36 2006 for wpa_supplicant by  doxygen 1.4.2