📄 cardif_windows_wireless.c
字号:
debug_printf(DEBUG_NORMAL, "Request to set encryption mode failed on interface '%s'.\n",
ctx->desc);
return -1;
}
}
else
{
debug_printf(DEBUG_INT, "(WEP Associate) Set encryption mode.\n");
if (cardif_windows_set_enc_mode(ctx, Ndis802_11Encryption1Enabled, TRUE) != 0)
{
debug_printf(DEBUG_NORMAL, "Request to set encryption mode failed on interface '%s'.\n",
ctx->desc);
return -1;
}
wctx->pairwiseKeyType = CIPHER_WEP104;
debug_printf(DEBUG_INT, "(WEP Associate) Set any static keys that are configured.\n");
set_static_wep_keys(ctx, &ctx->conn->association);
}
wctx = (wireless_ctx *)ctx->intTypeData;
if (!xsup_assert((wctx != NULL), "wctx != NULL", FALSE)) return -1;
debug_printf(DEBUG_INT, "(WEP Associate) Set SSID.\n");
if (cardif_windows_wireless_set_ssid(ctx, wctx->cur_essid) != 0)
{
debug_printf(DEBUG_NORMAL, "Request to set the SSID failed on interface '%s'.\n", ctx->desc);
return -1;
}
return XENONE;
}
/**
* Send a disassociate request to the AP we are currently connected to.
**/
int cardif_windows_wireless_disassociate(context *ctx, int reason)
{
wireless_ctx *wctx = NULL;
int i = 0;
char randomssid[31];
#if 1
if (!xsup_assert((ctx != NULL), "ctx != NULL", FALSE)) return -1;
wctx = (wireless_ctx *)ctx->intTypeData;
if (!xsup_assert((wctx != NULL), "wctx != NULL", FALSE)) return -1;
// Probably need to fix this to something else.
// This will clear any keys in the key cache.
cardif_windows_set_infra_mode(ctx);
for (i = 0; i < 30; i++)
{
randomssid[i] = (char)(((float)(rand() % 87)) + 35);
}
randomssid[30] = 0x00;
cardif_windows_wireless_set_ssid(ctx, randomssid);
// Clear out any async events that we might have for this context.
cardif_windows_wmi_async_clear_by_ctx(ctx);
// Set our association mode back to unknown.
wctx->assoc_type = ASSOC_TYPE_UNKNOWN;
memset(wctx->cur_bssid, 0x00, 6);
// Sending a disassociate turns off the radio, which is probably not what we want!
#endif
#if 0
DWORD BytesReturned;
DWORD result;
UCHAR QueryBuffer[sizeof(NDISPROT_QUERY_OID)];
PNDISPROT_QUERY_OID pQueryOid;
struct win_sock_data *sockData;
LPVOID lpMsgBuf;
if (!xsup_assert((ctx != NULL), "ctx != NULL", FALSE))
return -1;
sockData = (struct win_sock_data *)ctx->sockData;
if (!xsup_assert((sockData != NULL), "sockData != NULL", FALSE))
return -1;
wctx = (wireless_ctx *)ctx->intTypeData;
if (!xsup_assert((wctx != NULL), "wctx != NULL", FALSE)) return -1;
pQueryOid = (PNDISPROT_QUERY_OID)&QueryBuffer[0];
pQueryOid->Oid = OID_802_11_DISASSOCIATE;
if (devioctl_blk(sockData->devHandle, IOCTL_NDISPROT_SET_OID_VALUE,
(LPVOID)&QueryBuffer[0], sizeof(QueryBuffer),
(LPVOID)&QueryBuffer[0], sizeof(QueryBuffer), &BytesReturned) == FALSE)
{
debug_printf(DEBUG_NORMAL, "Disassociate failed.\n");
lpMsgBuf = GetLastErrorStr(GetLastError());
debug_printf(DEBUG_NORMAL, "Reason was : %s\n", lpMsgBuf);
LocalFree(lpMsgBuf);
return -1;
}
#endif
return XENONE;
}
// Windows uses higher order bits in the key index to determine what type of key it is.
// These defines just make the code a little more readable.
#define TX_KEY BIT(31)
#define PAIRWISE_KEY BIT(30)
#define MANUAL_RSC BIT(29)
#define AUTHENTICATOR_KEY BIT(28)
/**
* Set a WEP key. Also, based on the index, we may change the transmit
* key.
**/
int cardif_windows_wireless_set_WEP_key(context *ctx, uint8_t *key,
int keylen, int keyidx)
{
struct win_sock_data *sockData = NULL;
DWORD BytesReturned = 0;
UCHAR Buffer[sizeof(NDIS_OID)+sizeof(NDIS_802_11_WEP)+13];
PNDISPROT_SET_OID pSetOid = NULL;
PNDIS_802_11_WEP pKey = NULL;
LPVOID lpMsgBuf = NULL;
if (!xsup_assert((ctx != NULL), "ctx != NULL", FALSE)) return -1;
if (!xsup_assert((key != NULL), "key != NULL", FALSE)) return -1;
sockData = ctx->sockData;
if (!xsup_assert((sockData != NULL), "sockData != NULL", FALSE)) return -1;
pSetOid = (PNDISPROT_SET_OID)&Buffer[0];
pSetOid->Oid = OID_802_11_ADD_WEP;
pKey = (PNDIS_802_11_WEP)&pSetOid->Data[0];
pKey->Length = FIELD_OFFSET(NDIS_802_11_WEP, KeyMaterial) + keylen;
pKey->KeyIndex = (keyidx & 0x7f);
if ((keyidx & 0x80) == 0x80) pKey->KeyIndex |= TX_KEY;
pKey->KeyLength = keylen;
memcpy(&pKey->KeyMaterial[0], key, keylen);
if (devioctl_blk(sockData->devHandle, IOCTL_NDISPROT_SET_OID_VALUE,
(LPVOID)&Buffer[0], sizeof(Buffer), NULL, 0, &BytesReturned) == FALSE)
{
ipc_events_error(ctx, IPC_EVENT_ERROR_FAILED_SETTING_WEP_KEY, ctx->desc);
lpMsgBuf = GetLastErrorStr(GetLastError());
debug_printf(DEBUG_NORMAL, "Set WEP Key IOCTL failed on interface '%s'. Reason was : %s\n",
ctx->desc, lpMsgBuf);
LocalFree(lpMsgBuf);
return -1;
}
return XENONE;
}
/**
* Push an encryption key down to the driver/card. Windows differentiates between WEP/TKIP/CCMP
* by the length of the key data. Because of this, the variable "alg" isn't used. It is only
* left here to keep this code similar to the other interface handlers. (And will probably be
* removed at some point in the future.)
**/
int cardif_windows_wireless_set_key_ext(context *ctx, int alg,
unsigned char *addr, int keyidx, int settx,
char *seq, int seqlen, char *key,
int keylen)
{
struct win_sock_data *sockData = NULL;
DWORD BytesReturned = 0;
UCHAR Buffer[sizeof(NDIS_OID)+FIELD_OFFSET(NDIS_802_11_KEY, KeyMaterial) + 32];
PNDISPROT_SET_OID pSetOid = NULL;
PNDIS_802_11_KEY pKey = NULL;
LPVOID lpMsgBuf = NULL;
if (!xsup_assert((ctx != NULL), "ctx != NULL", FALSE)) return -1;
if (!xsup_assert((key != NULL), "key != NULL", FALSE)) return -1;
if (alg == ALG_WEP)
{
// The caller should call cardif_windows_wireless_set_wep directly, but this is
// just in case they do something stupid. ;)
return cardif_windows_wireless_set_WEP_key(ctx, key, keylen, keyidx);
}
sockData = ctx->sockData;
if (!xsup_assert((sockData != NULL), "sockData != NULL", FALSE)) return -1;
memset(&Buffer[0], 0x00, sizeof(Buffer));
pSetOid = (PNDISPROT_SET_OID)&Buffer[0];
pSetOid->Oid = OID_802_11_ADD_KEY;
pKey = (PNDIS_802_11_KEY)&pSetOid->Data[0];
pKey->Length = FIELD_OFFSET(NDIS_802_11_KEY, KeyMaterial) + keylen;
pKey->KeyIndex = keyidx;
pKey->KeyLength = keylen;
debug_printf(DEBUG_INT, "Key Index = %d\n", pKey->KeyIndex);
debug_printf(DEBUG_INT, "Key Length = %d\n", pKey->KeyLength);
if (alg == ALG_TKIP)
{
// Need to swap the MICs (back) so Windows can handle it the way it wants.
wpa_common_swap_rx_tx_mic(key);
}
if (seq == NULL)
{
pKey->KeyIndex |= MANUAL_RSC;
memset((UCHAR *)&pKey->KeyRSC, 0x00, sizeof(NDIS_802_11_KEY_RSC));
}
else
{
pKey->KeyIndex |= MANUAL_RSC;
memcpy(&pKey->KeyRSC, seq, seqlen);
}
if (addr != NULL)
{
memcpy(pKey->BSSID, addr, 6);
}
else
{
memset(pKey->BSSID, 0xff, 6);
}
if (settx == TRUE)
{
debug_printf(DEBUG_INT, "TX key!\n");
pKey->KeyIndex |= (TX_KEY | PAIRWISE_KEY);
//pKey->KeyIndex |= TX_KEY;
}
memcpy(&pKey->KeyMaterial, key, keylen);
debug_printf(DEBUG_INT, "Key : ");
debug_hex_printf(DEBUG_INT, pKey->KeyMaterial, keylen);
if (devioctl_blk(sockData->devHandle, IOCTL_NDISPROT_SET_OID_VALUE,
(LPVOID)&Buffer[0], sizeof(Buffer), NULL, 0, &BytesReturned) == FALSE)
{
switch (alg)
{
case ALG_TKIP:
ipc_events_error(ctx, IPC_EVENT_ERROR_FAILED_SETTING_TKIP_KEY, ctx->desc);
break;
case ALG_CCMP:
ipc_events_error(ctx, IPC_EVENT_ERROR_FAILED_SETTING_CCMP_KEY, ctx->desc);
break;
default:
ipc_events_error(ctx, IPC_EVENT_ERROR_FAILED_SETTING_UNKNOWN_KEY, ctx->desc);
break;
}
lpMsgBuf = GetLastErrorStr(GetLastError());
debug_printf(DEBUG_NORMAL, "Set key IOCTL failed on interface '%s'. Reason was : %s\n",
ctx->desc, lpMsgBuf);
LocalFree(lpMsgBuf);
return -1;
}
return XENONE;
}
/**
* Push a TKIP key down to the wireless card.
**/
int cardif_windows_wireless_set_tkip_key(context *ctx,
unsigned char *addr, int keyidx, int settx,
char *key, int keylen)
{
char seq[6] = {0x00,0x00,0x00,0x00,0x00,0x00};
return cardif_windows_wireless_set_key_ext(ctx, ALG_TKIP, addr, keyidx, settx,
seq, 6, key, keylen);
}
/**
* Push a CCMP key down to the wireless card.
**/
int cardif_windows_wireless_set_ccmp_key(context *ctx,
unsigned char *addr, int keyidx, int settx,
char *key, int keylen)
{
char seq[6] = {0x00,0x00,0x00,0x00,0x00,0x00};
return cardif_windows_wireless_set_key_ext(ctx, ALG_CCMP, addr, keyidx, settx,
seq, 6, key, keylen);
}
/**
* Set the WPA IE that is in use for this interface.
* On Windows, we don't have the ability to send in an IE, so this function
* does nothing.
**/
int cardif_windows_wireless_set_wpa_ie(context *ctx,
unsigned char *wpaie, unsigned int wpalen)
{
return XENONE;
}
/**
* Convert our cipher designator to something that will be understood by
* Windows.
*/
DWORD cardif_windows_wireless_cipher(int cipher)
{
switch (cipher)
{
case CIPHER_NONE:
return 0xffffffff;
break;
case CIPHER_WEP40:
return Ndis802_11Encryption1Enabled;
break;
case CIPHER_TKIP:
return Ndis802_11Encryption2Enabled;
break;
case CIPHER_WRAP:
debug_printf(DEBUG_NORMAL, "WRAP is not supported!\n");
return 0xffffffff;
break;
case CIPHER_CCMP:
return Ndis802_11Encryption3Enabled;
break;
case CIPHER_WEP104:
return Ndis802_11Encryption1Enabled;
break;
default:
debug_printf(DEBUG_NORMAL, "Unknown cipher value of %d! (Turning on everything.)\n", cipher);
return Ndis802_11Encryption3Enabled;
break;
}
}
/**
* Set all of the card settings that are needed in order to complete an
* association, so that we can begin the authentication.
**/
void cardif_windows_wireless_associate(context *ctx)
{
struct config_globals *globals = NULL;
wireless_ctx *wctx = NULL;
DWORD enc_mode = 0;
if (!xsup_assert((ctx != NULL), "ctx != NULL", FALSE))
return;
wctx = (wireless_ctx *)ctx->intTypeData;
if (!xsup_assert((wctx != NULL), "wctx != NULL", FALSE)) return;
debug_printf(DEBUG_INT, "(Associate) Set infra mode.\n");
if (cardif_windows_set_infra_mode(ctx) != 0)
{
debug_printf(DEBUG_NORMAL, "Request to set infrastructure mode failed on interface '%s'.\n",
ctx->desc);
return;
}
if ((config_ssid_get_ssid_abilities(ctx->intTypeData) & RSN_IE) ||
((config_ssid_find_by_name(wctx, wctx->cur_essid) == NULL) &&
((ctx->conn != NULL) && (ctx->conn->association.association_type == ASSOC_TYPE_WPA2))))
{
wctx->assoc_type = ASSOC_TYPE_WPA2;
// We are doing WPA2.
if (ctx->conn->association.auth_type != AUTH_PSK)
{
debug_printf(DEBUG_INT, "(Associate) Set auth mode. (WPA2-802.1X)\n");
if (cardif_windows_set_auth_mode(ctx, Ndis802_11AuthModeWPA2, TRUE) != 0)
{
debug_printf(DEBUG_NORMAL, "Couldn't set the authentication mode to WPA2-Enterprise on interface '%s'.\n",
ctx->desc);
return;
}
}
else
{
debug_printf(DEBUG_INT, "(Associate) Set auth mode. (WPA2-PSK)\n");
if (cardif_windows_set_auth_mode(ctx, Ndis802_11AuthModeWPA2PSK, TRUE) != 0)
{
debug_printf(DEBUG_NORMAL, "Request to set the authentication mode to WPA2-PSK failed on interface '%s'.\n",
ctx->desc);
return;
}
}
if (ctx->conn->association.pairwise_keys == 0)
{
wctx->groupKeyType = wpa2_get_group_crypt(ctx);
wctx->pairwiseKeyType = wpa2_get_pairwise_crypt(ctx);
if (wctx->pairwiseKeyType == 0xff)
{
debug_printf(DEBUG_NORMAL, "Couldn't determine cipher type. Forcing to CCMP.\n");
wctx->pairwiseKeyType = CIPHER_CCMP;
}
}
else
{
debug_printf(DEBUG_INT, "Key type hard set to %d.\n", ctx->conn->association.pairwise_keys);
if (ctx->conn->association.pairwise_keys & CRYPT_FLAGS_CCMP)
{
wctx->pairwiseKeyType = CIPHER_CCMP;
}
else if (ctx->conn->association.pairwise_keys & CRYPT_FLAGS_TKIP)
{
wctx->pairwiseKeyType = CIPHER_TKIP;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -