test_wpa.c
Go to the documentation of this file.
00001 /*
00002  * Test program for combined WPA authenticator/supplicant
00003  * Copyright (c) 2006-2007, Jouni Malinen <j@w1.fi>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 2 as
00007  * published by the Free Software Foundation.
00008  *
00009  * Alternatively, this software may be distributed under the terms of BSD
00010  * license.
00011  *
00012  * See README and COPYING for more details.
00013  */
00014 
00015 #include "includes.h"
00016 
00017 #include "common.h"
00018 #include "eloop.h"
00019 #include "common/ieee802_11_defs.h"
00020 #include "../config.h"
00021 #include "rsn_supp/wpa.h"
00022 #include "rsn_supp/wpa_ie.h"
00023 #include "../hostapd/wpa.h"
00024 
00025 
00026 extern int wpa_debug_level;
00027 extern int wpa_debug_show_keys;
00028 
00029 
00030 struct wpa {
00031         u8 auth_addr[ETH_ALEN];
00032         u8 supp_addr[ETH_ALEN];
00033         u8 psk[PMK_LEN];
00034 
00035         /* from authenticator */
00036         u8 auth_eapol_dst[ETH_ALEN];
00037         u8 *auth_eapol;
00038         size_t auth_eapol_len;
00039 
00040         /* from supplicant */
00041         u8 *supp_eapol;
00042         size_t supp_eapol_len;
00043 
00044         struct wpa_sm *supp;
00045         struct wpa_authenticator *auth_group;
00046         struct wpa_state_machine *auth;
00047 
00048         struct wpa_ssid ssid;
00049         u8 supp_ie[80];
00050         size_t supp_ie_len;
00051 };
00052 
00053 
00054 static int supp_get_bssid(void *ctx, u8 *bssid)
00055 {
00056         struct wpa *wpa = ctx;
00057         wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
00058         os_memcpy(bssid, wpa->auth_addr, ETH_ALEN);
00059         return 0;
00060 }
00061 
00062 
00063 static void supp_set_state(void *ctx, enum wpa_states state)
00064 {
00065         wpa_printf(MSG_DEBUG, "SUPP: %s(state=%d)", __func__, state);
00066 }
00067 
00068 
00069 static void auth_eapol_rx(void *eloop_data, void *user_ctx)
00070 {
00071         struct wpa *wpa = eloop_data;
00072 
00073         wpa_printf(MSG_DEBUG, "AUTH: RX EAPOL frame");
00074         wpa_receive(wpa->auth_group, wpa->auth, wpa->supp_eapol,
00075                     wpa->supp_eapol_len);
00076 }
00077 
00078 
00079 static int supp_ether_send(void *ctx, const u8 *dest, u16 proto, const u8 *buf,
00080                            size_t len)
00081 {
00082         struct wpa *wpa = ctx;
00083 
00084         wpa_printf(MSG_DEBUG, "SUPP: %s(dest=" MACSTR " proto=0x%04x "
00085                    "len=%lu)",
00086                    __func__, MAC2STR(dest), proto, (unsigned long) len);
00087 
00088         os_free(wpa->supp_eapol);
00089         wpa->supp_eapol = os_malloc(len);
00090         if (wpa->supp_eapol == NULL)
00091                 return -1;
00092         os_memcpy(wpa->supp_eapol, buf, len);
00093         wpa->supp_eapol_len = len;
00094         eloop_register_timeout(0, 0, auth_eapol_rx, wpa, NULL);
00095 
00096         return 0;
00097 }
00098 
00099 
00100 static u8 * supp_alloc_eapol(void *ctx, u8 type, const void *data,
00101                              u16 data_len, size_t *msg_len, void **data_pos)
00102 {
00103         struct ieee802_1x_hdr *hdr;
00104 
00105         wpa_printf(MSG_DEBUG, "SUPP: %s(type=%d data_len=%d)",
00106                    __func__, type, data_len);
00107 
00108         *msg_len = sizeof(*hdr) + data_len;
00109         hdr = os_malloc(*msg_len);
00110         if (hdr == NULL)
00111                 return NULL;
00112 
00113         hdr->version = 2;
00114         hdr->type = type;
00115         hdr->length = host_to_be16(data_len);
00116 
00117         if (data)
00118                 os_memcpy(hdr + 1, data, data_len);
00119         else
00120                 os_memset(hdr + 1, 0, data_len);
00121 
00122         if (data_pos)
00123                 *data_pos = hdr + 1;
00124 
00125         return (u8 *) hdr;
00126 }
00127 
00128 
00129 static int supp_get_beacon_ie(void *ctx)
00130 {
00131         struct wpa *wpa = ctx;
00132         const u8 *ie;
00133         size_t ielen;
00134 
00135         wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
00136 
00137         ie = wpa_auth_get_wpa_ie(wpa->auth_group, &ielen);
00138         if (ie == NULL || ielen < 1)
00139                 return -1;
00140         if (ie[0] == WLAN_EID_RSN)
00141                 return wpa_sm_set_ap_rsn_ie(wpa->supp, ie, 2 + ie[1]);
00142         return wpa_sm_set_ap_wpa_ie(wpa->supp, ie, 2 + ie[1]);
00143 }
00144 
00145 
00146 static int supp_set_key(void *ctx, enum wpa_alg alg,
00147                         const u8 *addr, int key_idx, int set_tx,
00148                         const u8 *seq, size_t seq_len,
00149                         const u8 *key, size_t key_len)
00150 {
00151         wpa_printf(MSG_DEBUG, "SUPP: %s(alg=%d addr=" MACSTR " key_idx=%d "
00152                    "set_tx=%d)",
00153                    __func__, alg, MAC2STR(addr), key_idx, set_tx);
00154         wpa_hexdump(MSG_DEBUG, "SUPP: set_key - seq", seq, seq_len);
00155         wpa_hexdump(MSG_DEBUG, "SUPP: set_key - key", key, key_len);
00156         return 0;
00157 }
00158 
00159 
00160 static int supp_mlme_setprotection(void *ctx, const u8 *addr,
00161                                    int protection_type, int key_type)
00162 {
00163         wpa_printf(MSG_DEBUG, "SUPP: %s(addr=" MACSTR " protection_type=%d "
00164                    "key_type=%d)",
00165                    __func__, MAC2STR(addr), protection_type, key_type);
00166         return 0;
00167 }
00168 
00169 
00170 static void supp_cancel_auth_timeout(void *ctx)
00171 {
00172         wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
00173 }
00174 
00175 
00176 static int supp_init(struct wpa *wpa)
00177 {
00178         struct wpa_sm_ctx *ctx = os_zalloc(sizeof(*ctx));
00179         if (ctx == NULL)
00180                 return -1;
00181 
00182         ctx->ctx = wpa;
00183         ctx->msg_ctx = wpa;
00184         ctx->set_state = supp_set_state;
00185         ctx->get_bssid = supp_get_bssid;
00186         ctx->ether_send = supp_ether_send;
00187         ctx->get_beacon_ie = supp_get_beacon_ie;
00188         ctx->alloc_eapol = supp_alloc_eapol;
00189         ctx->set_key = supp_set_key;
00190         ctx->mlme_setprotection = supp_mlme_setprotection;
00191         ctx->cancel_auth_timeout = supp_cancel_auth_timeout;
00192         wpa->supp = wpa_sm_init(ctx);
00193         if (wpa->supp == NULL) {
00194                 wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_init() failed");
00195                 return -1;
00196         }
00197 
00198         wpa_sm_set_own_addr(wpa->supp, wpa->supp_addr);
00199         wpa_sm_set_param(wpa->supp, WPA_PARAM_RSN_ENABLED, 1);
00200         wpa_sm_set_param(wpa->supp, WPA_PARAM_PROTO, WPA_PROTO_RSN);
00201         wpa_sm_set_param(wpa->supp, WPA_PARAM_PAIRWISE, WPA_CIPHER_CCMP);
00202         wpa_sm_set_param(wpa->supp, WPA_PARAM_GROUP, WPA_CIPHER_CCMP);
00203         wpa_sm_set_param(wpa->supp, WPA_PARAM_KEY_MGMT, WPA_KEY_MGMT_PSK);
00204         wpa_sm_set_pmk(wpa->supp, wpa->psk, PMK_LEN);
00205 
00206         wpa->supp_ie_len = sizeof(wpa->supp_ie);
00207         if (wpa_sm_set_assoc_wpa_ie_default(wpa->supp, wpa->supp_ie,
00208                                             &wpa->supp_ie_len) < 0) {
00209                 wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_set_assoc_wpa_ie_default()"
00210                            " failed");
00211                 return -1;
00212         }
00213 
00214         wpa_sm_notify_assoc(wpa->supp, wpa->auth_addr);
00215 
00216         return 0;
00217 }
00218 
00219 
00220 static void auth_logger(void *ctx, const u8 *addr, logger_level level,
00221                         const char *txt)
00222 {
00223         if (addr)
00224                 wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " - %s",
00225                            MAC2STR(addr), txt);
00226         else
00227                 wpa_printf(MSG_DEBUG, "AUTH: %s", txt);
00228 }
00229 
00230 
00231 static void supp_eapol_rx(void *eloop_data, void *user_ctx)
00232 {
00233         struct wpa *wpa = eloop_data;
00234 
00235         wpa_printf(MSG_DEBUG, "SUPP: RX EAPOL frame");
00236         wpa_sm_rx_eapol(wpa->supp, wpa->auth_addr, wpa->auth_eapol,
00237                         wpa->auth_eapol_len);
00238 }
00239 
00240 
00241 static int auth_send_eapol(void *ctx, const u8 *addr, const u8 *data,
00242                            size_t data_len, int encrypt)
00243 {
00244         struct wpa *wpa = ctx;
00245 
00246         wpa_printf(MSG_DEBUG, "AUTH: %s(addr=" MACSTR " data_len=%lu "
00247                    "encrypt=%d)",
00248                    __func__, MAC2STR(addr), (unsigned long) data_len, encrypt);
00249 
00250         os_free(wpa->auth_eapol);
00251         wpa->auth_eapol = os_malloc(data_len);
00252         if (wpa->auth_eapol == NULL)
00253                 return -1;
00254         os_memcpy(wpa->auth_eapol_dst, addr, ETH_ALEN);
00255         os_memcpy(wpa->auth_eapol, data, data_len);
00256         wpa->auth_eapol_len = data_len;
00257         eloop_register_timeout(0, 0, supp_eapol_rx, wpa, NULL);
00258 
00259         return 0;
00260 }
00261 
00262 
00263 static const u8 * auth_get_psk(void *ctx, const u8 *addr, const u8 *prev_psk)
00264 {
00265         struct wpa *wpa = ctx;
00266         wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
00267                    __func__, MAC2STR(addr), prev_psk);
00268         if (prev_psk)
00269                 return NULL;
00270         return wpa->psk;
00271 }
00272 
00273 
00274 static int auth_init_group(struct wpa *wpa)
00275 {
00276         struct wpa_auth_config conf;
00277         struct wpa_auth_callbacks cb;
00278 
00279         wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
00280 
00281         os_memset(&conf, 0, sizeof(conf));
00282         conf.wpa = 2;
00283         conf.wpa_key_mgmt = WPA_KEY_MGMT_PSK;
00284         conf.wpa_pairwise = WPA_CIPHER_CCMP;
00285         conf.rsn_pairwise = WPA_CIPHER_CCMP;
00286         conf.wpa_group = WPA_CIPHER_CCMP;
00287         conf.eapol_version = 2;
00288 
00289         os_memset(&cb, 0, sizeof(cb));
00290         cb.ctx = wpa;
00291         cb.logger = auth_logger;
00292         cb.send_eapol = auth_send_eapol;
00293         cb.get_psk = auth_get_psk;
00294 
00295         wpa->auth_group = wpa_init(wpa->auth_addr, &conf, &cb);
00296         if (wpa->auth_group == NULL) {
00297                 wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
00298                 return -1;
00299         }
00300 
00301         return 0;
00302 }
00303 
00304 
00305 static int auth_init(struct wpa *wpa)
00306 {
00307         wpa->auth = wpa_auth_sta_init(wpa->auth_group, wpa->supp_addr);
00308         if (wpa->auth == NULL) {
00309                 wpa_printf(MSG_DEBUG, "AUTH: wpa_auth_sta_init() failed");
00310                 return -1;
00311         }
00312 
00313         if (wpa_validate_wpa_ie(wpa->auth_group, wpa->auth, wpa->supp_ie,
00314                                 wpa->supp_ie_len, NULL, 0) != WPA_IE_OK) {
00315                 wpa_printf(MSG_DEBUG, "AUTH: wpa_validate_wpa_ie() failed");
00316                 return -1;
00317         }
00318 
00319         wpa_auth_sm_event(wpa->auth, WPA_ASSOC);
00320 
00321         wpa_auth_sta_associated(wpa->auth_group, wpa->auth);
00322 
00323         return 0;
00324 }
00325 
00326 
00327 static void deinit(struct wpa *wpa)
00328 {
00329         wpa_auth_sta_deinit(wpa->auth);
00330         wpa_sm_deinit(wpa->supp);
00331         wpa_deinit(wpa->auth_group);
00332         os_free(wpa->auth_eapol);
00333         wpa->auth_eapol = NULL;
00334         os_free(wpa->supp_eapol);
00335         wpa->supp_eapol = NULL;
00336 }
00337 
00338 
00339 int main(int argc, char *argv[])
00340 {
00341         struct wpa wpa;
00342 
00343         if (os_program_init())
00344                 return -1;
00345 
00346         os_memset(&wpa, 0, sizeof(wpa));
00347         os_memset(wpa.auth_addr, 0x12, ETH_ALEN);
00348         os_memset(wpa.supp_addr, 0x32, ETH_ALEN);
00349         os_memset(wpa.psk, 0x44, PMK_LEN);
00350 
00351         wpa_debug_level = 0;
00352         wpa_debug_show_keys = 1;
00353 
00354         if (eloop_init()) {
00355                 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
00356                 return -1;
00357         }
00358 
00359         if (auth_init_group(&wpa) < 0)
00360                 return -1;
00361 
00362         if (supp_init(&wpa) < 0)
00363                 return -1;
00364 
00365         if (auth_init(&wpa) < 0)
00366                 return -1;
00367 
00368         wpa_printf(MSG_DEBUG, "Starting eloop");
00369         eloop_run();
00370         wpa_printf(MSG_DEBUG, "eloop done");
00371 
00372         deinit(&wpa);
00373 
00374         eloop_destroy();
00375 
00376         os_program_deinit();
00377 
00378         return 0;
00379 }


wpa_supplicant
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Apr 24 2014 15:34:36