⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eap_sim_common.c

📁 WPA在Linux下实现的原代码 WPA在Linux下实现的原代码
💻 C
📖 第 1 页 / 共 2 页
字号:
		case EAP_SIM_AT_ENCR_DATA:			wpa_printf(MSG_DEBUG, "EAP-SIM: AT_ENCR_DATA");			attr->encr_data = apos + 2;			attr->encr_data_len = alen - 2;			if (attr->encr_data_len % 16) {				wpa_printf(MSG_INFO, "EAP-SIM: Invalid "					   "AT_ENCR_DATA length %lu",					   (unsigned long)					   attr->encr_data_len);				return -1;			}			break;		case EAP_SIM_AT_NEXT_PSEUDONYM:			if (!encr) {				wpa_printf(MSG_ERROR, "EAP-SIM: Unencrypted "					   "AT_NEXT_PSEUDONYM");				return -1;			}			wpa_printf(MSG_DEBUG, "EAP-SIM: (encr) "				   "AT_NEXT_PSEUDONYM");			plen = apos[0] * 256 + apos[1];			if (plen > alen - 2) {				wpa_printf(MSG_INFO, "EAP-SIM: (encr) Invalid"					   " AT_NEXT_PSEUDONYM (actual"					   " len %lu, attr len %lu)",					   (unsigned long) plen,					   (unsigned long) alen);				return -1;			}			attr->next_pseudonym = pos + 4;			attr->next_pseudonym_len = plen;			break;		case EAP_SIM_AT_NEXT_REAUTH_ID:			if (!encr) {				wpa_printf(MSG_ERROR, "EAP-SIM: Unencrypted "					   "AT_NEXT_REAUTH_ID");				return -1;			}			wpa_printf(MSG_DEBUG, "EAP-SIM: (encr) "				   "AT_NEXT_REAUTH_ID");			plen = apos[0] * 256 + apos[1];			if (plen > alen - 2) {				wpa_printf(MSG_INFO, "EAP-SIM: (encr) Invalid"					   " AT_NEXT_REAUTH_ID (actual"					   " len %lu, attr len %lu)",					   (unsigned long) plen,					   (unsigned long) alen);				return -1;			}			attr->next_reauth_id = pos + 4;			attr->next_reauth_id_len = plen;			break;		default:			if (pos[0] < 128) {				wpa_printf(MSG_INFO, "EAP-SIM: Unrecognized "					   "non-skippable attribute %d",					   pos[0]);				return -1;			}			wpa_printf(MSG_DEBUG, "EAP-SIM: Unrecognized skippable"				   " attribute %d ignored", pos[0]);			break;		}		pos += pos[1] * 4;	}	wpa_printf(MSG_DEBUG, "EAP-SIM: Attributes parsed successfully "		   "(aka=%d encr=%d)", aka, encr);	return 0;}u8 * eap_sim_parse_encr(const u8 *k_encr, const u8 *encr_data,			size_t encr_data_len, const u8 *iv,			struct eap_sim_attrs *attr, int aka){	u8 *decrypted;	if (!iv) {		wpa_printf(MSG_INFO, "EAP-SIM: Encrypted data, but no IV");		return NULL;	}	decrypted = malloc(encr_data_len);	if (decrypted == NULL)		return NULL;	memcpy(decrypted, encr_data, encr_data_len);	aes_128_cbc_decrypt(k_encr, iv, decrypted, encr_data_len);	wpa_hexdump(MSG_MSGDUMP, "EAP-SIM: Decrypted AT_ENCR_DATA",		    decrypted, encr_data_len);	if (eap_sim_parse_attr(decrypted, decrypted + encr_data_len, attr,			       aka, 1)) {		wpa_printf(MSG_INFO, "EAP-SIM: (encr) Failed to parse "			   "decrypted AT_ENCR_DATA");		free(decrypted);		return NULL;	}	return decrypted;}#define EAP_SIM_INIT_LEN 128struct eap_sim_msg {	u8 *buf;	size_t buf_len, used;	size_t mac, iv, encr; /* index from buf */};struct eap_sim_msg * eap_sim_msg_init(int code, int id, int type, int subtype){	struct eap_sim_msg *msg;	struct eap_hdr *eap;	u8 *pos;	msg = malloc(sizeof(*msg));	if (msg == NULL)		return NULL;	memset(msg, 0, sizeof(*msg));	msg->buf = malloc(EAP_SIM_INIT_LEN);	if (msg->buf == NULL) {		free(msg);		return NULL;	}	memset(msg->buf, 0, EAP_SIM_INIT_LEN);	msg->buf_len = EAP_SIM_INIT_LEN;	eap = (struct eap_hdr *) msg->buf;	eap->code = code;	eap->identifier = id;	msg->used = sizeof(*eap);	pos = (u8 *) (eap + 1);	*pos++ = type;	*pos++ = subtype;	*pos++ = 0; /* Reserved */	*pos++ = 0; /* Reserved */	msg->used += 4;	return msg;}u8 * eap_sim_msg_finish(struct eap_sim_msg *msg, size_t *len, const u8 *k_aut,			const u8 *extra, size_t extra_len){	struct eap_hdr *eap;	u8 *buf;	if (msg == NULL)		return NULL;	eap = (struct eap_hdr *) msg->buf;	eap->length = host_to_be16(msg->used);	if (k_aut && msg->mac) {		eap_sim_add_mac(k_aut, msg->buf, msg->used,				msg->buf + msg->mac, extra, extra_len);	}	*len = msg->used;	buf = msg->buf;	free(msg);	return buf;}void eap_sim_msg_free(struct eap_sim_msg *msg){	if (msg) {		free(msg->buf);		free(msg);	}}static int eap_sim_msg_resize(struct eap_sim_msg *msg, size_t add_len){	if (msg->used + add_len > msg->buf_len) {		u8 *nbuf = realloc(msg->buf, msg->used + add_len);		if (nbuf == NULL)			return -1;		msg->buf = nbuf;		msg->buf_len = msg->used + add_len;	}	return 0;}u8 * eap_sim_msg_add_full(struct eap_sim_msg *msg, u8 attr,			  const u8 *data, size_t len){	int attr_len = 2 + len;	int pad_len;	u8 *start, *pos;	if (msg == NULL)		return NULL;	pad_len = (4 - attr_len % 4) % 4;	attr_len += pad_len;	if (eap_sim_msg_resize(msg, attr_len))		return NULL;	start = pos = msg->buf + msg->used;	*pos++ = attr;	*pos++ = attr_len / 4;	memcpy(pos, data, len);	if (pad_len) {		pos += len;		memset(pos, 0, pad_len);	}	msg->used += attr_len;	return start;}u8 * eap_sim_msg_add(struct eap_sim_msg *msg, u8 attr, u16 value,		     const u8 *data, size_t len){	int attr_len = 4 + len;	int pad_len;	u8 *start, *pos;	if (msg == NULL)		return NULL;	pad_len = (4 - attr_len % 4) % 4;	attr_len += pad_len;	if (eap_sim_msg_resize(msg, attr_len))		return NULL;	start = pos = msg->buf + msg->used;	*pos++ = attr;	*pos++ = attr_len / 4;	WPA_PUT_BE16(pos, value);	pos += 2;	if (data)		memcpy(pos, data, len);	if (pad_len) {		pos += len;		memset(pos, 0, pad_len);	}	msg->used += attr_len;	return start;}u8 * eap_sim_msg_add_mac(struct eap_sim_msg *msg, u8 attr){	u8 *pos = eap_sim_msg_add(msg, attr, 0, NULL, EAP_SIM_MAC_LEN);	if (pos)		msg->mac = (pos - msg->buf) + 4;	return pos;}int eap_sim_msg_add_encr_start(struct eap_sim_msg *msg, u8 attr_iv,			       u8 attr_encr){	u8 *pos = eap_sim_msg_add(msg, attr_iv, 0, NULL, EAP_SIM_IV_LEN);	if (pos == NULL)		return -1;	msg->iv = (pos - msg->buf) + 4;	if (hostapd_get_rand(msg->buf + msg->iv, EAP_SIM_IV_LEN)) {		msg->iv = 0;		return -1;	}	pos = eap_sim_msg_add(msg, attr_encr, 0, NULL, 0);	if (pos == NULL) {		msg->iv = 0;		return -1;	}	msg->encr = pos - msg->buf;	return 0;}int eap_sim_msg_add_encr_end(struct eap_sim_msg *msg, u8 *k_encr, int attr_pad){	size_t encr_len;	if (msg == NULL || k_encr == NULL || msg->iv == 0 || msg->encr == 0)		return -1;	encr_len = msg->used - msg->encr - 4;	if (encr_len % 16) {		u8 *pos;		int pad_len = 16 - (encr_len % 16);		if (pad_len < 4) {			wpa_printf(MSG_WARNING, "EAP-SIM: "				   "eap_sim_msg_add_encr_end - invalid pad_len"				   " %d", pad_len);			return -1;		}		wpa_printf(MSG_DEBUG, "   *AT_PADDING");		pos = eap_sim_msg_add(msg, attr_pad, 0, NULL, pad_len - 4);		if (pos == NULL)			return -1;		memset(pos + 4, 0, pad_len - 4);		encr_len += pad_len;	}	wpa_printf(MSG_DEBUG, "   (AT_ENCR_DATA data len %lu)",		   (unsigned long) encr_len);	msg->buf[msg->encr + 1] = encr_len / 4 + 1;	aes_128_cbc_encrypt(k_encr, msg->buf + msg->iv,			    msg->buf + msg->encr + 4, encr_len);	return 0;}void eap_sim_report_notification(void *msg_ctx, int notification, int aka){#ifndef CONFIG_NO_STDOUT_DEBUG	const char *type = aka ? "AKA" : "SIM";#endif /* CONFIG_NO_STDOUT_DEBUG */	switch (notification) {	case EAP_SIM_GENERAL_FAILURE_AFTER_AUTH:		wpa_printf(MSG_WARNING, "EAP-%s: General failure "			   "notification (after authentication)", type);		break;	case EAP_SIM_TEMPORARILY_DENIED:		wpa_printf(MSG_WARNING, "EAP-%s: Failure notification: "			   "User has been temporarily denied access to the "			   "requested service", type);		break;	case EAP_SIM_NOT_SUBSCRIBED:		wpa_printf(MSG_WARNING, "EAP-%s: Failure notification: "			   "User has not subscribed to the requested service",			   type);		break;	case EAP_SIM_GENERAL_FAILURE_BEFORE_AUTH:		wpa_printf(MSG_WARNING, "EAP-%s: General failure "			   "notification (before authentication)", type);		break;	case EAP_SIM_SUCCESS:		wpa_printf(MSG_INFO, "EAP-%s: Successful authentication "			   "notification", type);		break;	default:		if (notification >= 32768) {			wpa_printf(MSG_INFO, "EAP-%s: Unrecognized "				   "non-failure notification %d",				   type, notification);		} else {			wpa_printf(MSG_WARNING, "EAP-%s: Unrecognized "				   "failure notification %d",				   type, notification);		}	}}#ifdef TEST_MAIN_EAP_SIM_COMMONstatic int test_eap_sim_prf(void){	/* http://csrc.nist.gov/encryption/dss/Examples-1024bit.pdf */	u8 xkey[] = {		0xbd, 0x02, 0x9b, 0xbe, 0x7f, 0x51, 0x96, 0x0b,		0xcf, 0x9e, 0xdb, 0x2b, 0x61, 0xf0, 0x6f, 0x0f,		0xeb, 0x5a, 0x38, 0xb6	};	u8 w[] = {		0x20, 0x70, 0xb3, 0x22, 0x3d, 0xba, 0x37, 0x2f,		0xde, 0x1c, 0x0f, 0xfc, 0x7b, 0x2e, 0x3b, 0x49,		0x8b, 0x26, 0x06, 0x14, 0x3c, 0x6c, 0x18, 0xba,		0xcb, 0x0f, 0x6c, 0x55, 0xba, 0xbb, 0x13, 0x78,		0x8e, 0x20, 0xd7, 0x37, 0xa3, 0x27, 0x51, 0x16	};	u8 buf[40];	printf("Testing EAP-SIM PRF (FIPS 186-2 + change notice 1)\n");	eap_sim_prf(xkey, buf, sizeof(buf));	if (memcmp(w, buf, sizeof(w) != 0)) {		printf("eap_sim_prf failed\n");		return 1;	}	return 0;}int main(int argc, char *argv[]){	int errors = 0;	errors += test_eap_sim_prf();	return errors;}#endif /* TEST_MAIN_EAP_SIM_COMMON */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -