wpa_ft.c

来自「最新的Host AP 新添加了许多pcmcia 的驱动」· C语言 代码 · 共 1,500 行 · 第 1/3 页

C
1,500
字号
	if (os_memcmp(sta_addr, sm->addr, ETH_ALEN) != 0) {		wpa_printf(MSG_DEBUG, "FT: Mismatch in FT Action STA address: "			   "STA=" MACSTR " STA-Address=" MACSTR,			   MAC2STR(sm->addr), MAC2STR(sta_addr));		return -1;	}	/*	 * Do some sanity checking on the target AP address (not own and not	 * broadcast. This could be extended to filter based on a list of known	 * APs in the MD (if such a list were configured).	 */	if ((target_ap[0] & 0x01) ||	    os_memcmp(target_ap, sm->wpa_auth->addr, ETH_ALEN) == 0) {		wpa_printf(MSG_DEBUG, "FT: Invalid Target AP in FT Action "			   "frame");		return -1;	}	wpa_hexdump(MSG_MSGDUMP, "FT: Action frame body", ies, ies_len);	/* RRB - Forward action frame to the target AP */	frame = os_malloc(sizeof(*frame) + len);	frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;	frame->packet_type = FT_PACKET_REQUEST;	frame->action_length = host_to_le16(len);	os_memcpy(frame->ap_address, sm->wpa_auth->addr, ETH_ALEN);	os_memcpy(frame + 1, data, len);	wpa_ft_rrb_send(sm->wpa_auth, target_ap, (u8 *) frame,			sizeof(*frame) + len);	os_free(frame);	return 0;}static int wpa_ft_rrb_rx_request(struct wpa_authenticator *wpa_auth,				 const u8 *current_ap, const u8 *sta_addr,				 const u8 *body, size_t len){	struct wpa_state_machine *sm;	u16 status;	u8 *resp_ies, *pos;	size_t resp_ies_len, rlen;	struct ft_rrb_frame *frame;	sm = wpa_ft_add_sta(wpa_auth, sta_addr);	if (sm == NULL) {		wpa_printf(MSG_DEBUG, "FT: Failed to add new STA based on "			   "RRB Request");		return -1;	}	wpa_hexdump(MSG_MSGDUMP, "FT: RRB Request Frame body", body, len);	status = wpa_ft_process_auth_req(sm, body, len, &resp_ies,					 &resp_ies_len);	wpa_printf(MSG_DEBUG, "FT: RRB authentication response: STA=" MACSTR		   " CurrentAP=" MACSTR " status=%d",		   MAC2STR(sm->addr), MAC2STR(current_ap), status);	wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len);	/* RRB - Forward action frame response to the Current AP */	/*	 * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]	 * Status_Code[2] FT Request action frame body[variable]	 */	rlen = 2 + 2 * ETH_ALEN + 2 + resp_ies_len;	frame = os_malloc(sizeof(*frame) + rlen);	frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;	frame->packet_type = FT_PACKET_RESPONSE;	frame->action_length = host_to_le16(rlen);	os_memcpy(frame->ap_address, wpa_auth->addr, ETH_ALEN);	pos = (u8 *) (frame + 1);	*pos++ = WLAN_ACTION_FT;	*pos++ = 2; /* Action: Response */	os_memcpy(pos, sta_addr, ETH_ALEN);	pos += ETH_ALEN;	os_memcpy(pos, wpa_auth->addr, ETH_ALEN);	pos += ETH_ALEN;	WPA_PUT_LE16(pos, status);	pos += 2;	if (resp_ies) {		os_memcpy(pos, resp_ies, resp_ies_len);		os_free(resp_ies);	}	wpa_ft_rrb_send(wpa_auth, current_ap, (u8 *) frame,			sizeof(*frame) + rlen);	os_free(frame);	return 0;}static int wpa_ft_rrb_rx_pull(struct wpa_authenticator *wpa_auth,			      const u8 *src_addr,			      const u8 *data, size_t data_len){	struct ft_r0kh_r1kh_pull_frame *frame, f;	struct ft_remote_r1kh *r1kh;	struct ft_r0kh_r1kh_resp_frame resp, r;	u8 pmk_r0[PMK_LEN];	wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull");	if (data_len < sizeof(*frame))		return -1;	r1kh = wpa_auth->conf.r1kh_list;	while (r1kh) {		if (os_memcmp(r1kh->addr, src_addr, ETH_ALEN) == 0)			break;		r1kh = r1kh->next;	}	if (r1kh == NULL) {		wpa_printf(MSG_DEBUG, "FT: No matching R1KH address found for "			   "PMK-R1 pull source address " MACSTR,			   MAC2STR(src_addr));		return -1;	}	frame = (struct ft_r0kh_r1kh_pull_frame *) data;	/* aes_unwrap() does not support inplace decryption, so use a temporary	 * buffer for the data. */	if (aes_unwrap(r1kh->key, (FT_R0KH_R1KH_PULL_DATA_LEN + 7) / 8,		       frame->nonce, f.nonce) < 0) {		wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull "			   "request from " MACSTR, MAC2STR(src_addr));		return -1;	}	wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce",		    f.nonce, sizeof(f.nonce));	wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR0Name",		    f.pmk_r0_name, WPA_PMK_NAME_LEN);	wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - R1KH-ID=" MACSTR "S1KH-ID="		   MACSTR, MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id));	os_memset(&resp, 0, sizeof(resp));	resp.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;	resp.packet_type = FT_PACKET_R0KH_R1KH_RESP;	resp.data_length = host_to_le16(FT_R0KH_R1KH_RESP_DATA_LEN);	os_memcpy(resp.ap_address, wpa_auth->addr, ETH_ALEN);	/* aes_wrap() does not support inplace encryption, so use a temporary	 * buffer for the data. */	os_memcpy(r.nonce, f.nonce, sizeof(f.nonce));	os_memcpy(r.r1kh_id, f.r1kh_id, FT_R1KH_ID_LEN);	os_memcpy(r.s1kh_id, f.s1kh_id, ETH_ALEN);	if (wpa_ft_fetch_pmk_r0(wpa_auth, f.s1kh_id, f.pmk_r0_name, pmk_r0) <	    0) {		wpa_printf(MSG_DEBUG, "FT: No matching PMKR0Name found for "			   "PMK-R1 pull");		return -1;	}	wpa_derive_pmk_r1(pmk_r0, f.pmk_r0_name, f.r1kh_id, f.s1kh_id,			  r.pmk_r1, r.pmk_r1_name);	wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", r.pmk_r1, PMK_LEN);	wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", r.pmk_r1_name,		    WPA_PMK_NAME_LEN);	if (aes_wrap(r1kh->key, (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8,		     r.nonce, resp.nonce) < 0) {		os_memset(pmk_r0, 0, PMK_LEN);		return -1;	}	os_memset(pmk_r0, 0, PMK_LEN);	wpa_ft_rrb_send(wpa_auth, src_addr, (u8 *) &resp, sizeof(resp));	return 0;}static int wpa_ft_rrb_rx_resp(struct wpa_authenticator *wpa_auth,			      const u8 *src_addr,			      const u8 *data, size_t data_len){	struct ft_r0kh_r1kh_resp_frame *frame, f;	struct ft_remote_r0kh *r0kh;	wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull response");	if (data_len < sizeof(*frame))		return -1;	r0kh = wpa_auth->conf.r0kh_list;	while (r0kh) {		if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0)			break;		r0kh = r0kh->next;	}	if (r0kh == NULL) {		wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for "			   "PMK-R0 pull response source address " MACSTR,			   MAC2STR(src_addr));		return -1;	}	frame = (struct ft_r0kh_r1kh_resp_frame *) data;	/* aes_unwrap() does not support inplace decryption, so use a temporary	 * buffer for the data. */	if (aes_unwrap(r0kh->key, (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8,		       frame->nonce, f.nonce) < 0) {		wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull "			   "response from " MACSTR, MAC2STR(src_addr));		return -1;	}	if (os_memcmp(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN)	    != 0) {		wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull response did not use a "			   "matching R1KH-ID");		return -1;	}	/* TODO: verify that <nonce,s1kh_id> matches with a pending request	 * and call this requests callback function to finish request	 * processing */	wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce",		    f.nonce, sizeof(f.nonce));	wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - R1KH-ID=" MACSTR "S1KH-ID="		   MACSTR, MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id));	wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1 pull - PMK-R1",			f.pmk_r1, PMK_LEN);	wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR1Name",			f.pmk_r1_name, WPA_PMK_NAME_LEN);	wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name);	os_memset(f.pmk_r1, 0, PMK_LEN);	return 0;}static int wpa_ft_rrb_rx_push(struct wpa_authenticator *wpa_auth,			      const u8 *src_addr,			      const u8 *data, size_t data_len){	struct ft_r0kh_r1kh_push_frame *frame, f;	struct ft_remote_r0kh *r0kh;	struct os_time now;	os_time_t tsend;	wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 push");	if (data_len < sizeof(*frame))		return -1;	r0kh = wpa_auth->conf.r0kh_list;	while (r0kh) {		if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0)			break;		r0kh = r0kh->next;	}	if (r0kh == NULL) {		wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for "			   "PMK-R0 push source address " MACSTR,			   MAC2STR(src_addr));		return -1;	}	frame = (struct ft_r0kh_r1kh_push_frame *) data;	/* aes_unwrap() does not support inplace decryption, so use a temporary	 * buffer for the data. */	if (aes_unwrap(r0kh->key, (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8,		       frame->timestamp, f.timestamp) < 0) {		wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 push from "			   MACSTR, MAC2STR(src_addr));		return -1;	}	os_get_time(&now);	tsend = WPA_GET_LE32(f.timestamp);	if ((now.sec > tsend && now.sec - tsend > 60) ||	    (now.sec < tsend && tsend - now.sec > 60)) {		wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not have a valid "			   "timestamp: sender time %d own time %d\n",			   (int) tsend, (int) now.sec);		return -1;	}	if (os_memcmp(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN)	    != 0) {		wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not use a matching "			   "R1KH-ID (received " MACSTR " own " MACSTR ")",			   MAC2STR(f.r1kh_id),			   MAC2STR(wpa_auth->conf.r1_key_holder));		return -1;	}	wpa_printf(MSG_DEBUG, "FT: PMK-R1 push - R1KH-ID=" MACSTR " S1KH-ID="		   MACSTR, MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id));	wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1 push - PMK-R1",			f.pmk_r1, PMK_LEN);	wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 push - PMKR1Name",			f.pmk_r1_name, WPA_PMK_NAME_LEN);	wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name);	os_memset(f.pmk_r1, 0, PMK_LEN);	return 0;}int wpa_ft_rrb_rx(struct wpa_authenticator *wpa_auth, const u8 *src_addr,		  const u8 *data, size_t data_len){	struct ft_rrb_frame *frame;	u16 alen;	const u8 *pos, *end, *start;	u8 action;	const u8 *sta_addr, *target_ap_addr;	wpa_printf(MSG_DEBUG, "FT: RRB received frame from remote AP " MACSTR,		   MAC2STR(src_addr));	if (data_len < sizeof(*frame)) {		wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (data_len=%lu)",			   (unsigned long) data_len);		return -1;	}	pos = data;	frame = (struct ft_rrb_frame *) pos;	pos += sizeof(*frame);	alen = le_to_host16(frame->action_length);	wpa_printf(MSG_DEBUG, "FT: RRB frame - frame_type=%d packet_type=%d "		   "action_length=%d ap_address=" MACSTR,		   frame->frame_type, frame->packet_type, alen,		   MAC2STR(frame->ap_address));	if (frame->frame_type != RSN_REMOTE_FRAME_TYPE_FT_RRB) {		/* Discard frame per IEEE Std 802.11r-2008, 11A.10.3 */		wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with "			   "unrecognized type %d", frame->frame_type);		return -1;	}	if (alen > data_len - sizeof(*frame)) {		wpa_printf(MSG_DEBUG, "FT: RRB frame too short for action "			   "frame");		return -1;	}	if (frame->packet_type == FT_PACKET_R0KH_R1KH_PULL)		return wpa_ft_rrb_rx_pull(wpa_auth, src_addr, data, data_len);	if (frame->packet_type == FT_PACKET_R0KH_R1KH_RESP)		return wpa_ft_rrb_rx_resp(wpa_auth, src_addr, data, data_len);	if (frame->packet_type == FT_PACKET_R0KH_R1KH_PUSH)		return wpa_ft_rrb_rx_push(wpa_auth, src_addr, data, data_len);	wpa_hexdump(MSG_MSGDUMP, "FT: RRB - FT Action frame", pos, alen);	if (alen < 1 + 1 + 2 * ETH_ALEN) {		wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (not enough "			   "room for Action Frame body); alen=%lu",			   (unsigned long) alen);		return -1;	}	start = pos;	end = pos + alen;	if (*pos != WLAN_ACTION_FT) {		wpa_printf(MSG_DEBUG, "FT: Unexpected Action frame category "			   "%d", *pos);		return -1;	}	pos++;	action = *pos++;	sta_addr = pos;	pos += ETH_ALEN;	target_ap_addr = pos;	pos += ETH_ALEN;	wpa_printf(MSG_DEBUG, "FT: RRB Action Frame: action=%d sta_addr="		   MACSTR " target_ap_addr=" MACSTR,		   action, MAC2STR(sta_addr), MAC2STR(target_ap_addr));	if (frame->packet_type == FT_PACKET_REQUEST) {		wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Request");		if (action != 1) {			wpa_printf(MSG_DEBUG, "FT: Unexpected Action %d in "				   "RRB Request", action);			return -1;		}		if (os_memcmp(target_ap_addr, wpa_auth->addr, ETH_ALEN) != 0) {			wpa_printf(MSG_DEBUG, "FT: Target AP address in the "				   "RRB Request does not match with own "				   "address");			return -1;		}		if (wpa_ft_rrb_rx_request(wpa_auth, frame->ap_address,					  sta_addr, pos, end - pos) < 0)			return -1;	} else if (frame->packet_type == FT_PACKET_RESPONSE) {		u16 status_code;		if (end - pos < 2) {			wpa_printf(MSG_DEBUG, "FT: Not enough room for status "				   "code in RRB Response");			return -1;		}		status_code = WPA_GET_LE16(pos);		pos += 2;		wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Response "			   "(status_code=%d)", status_code);		if (wpa_ft_action_send(wpa_auth, sta_addr, start, alen) < 0)			return -1;	} else {		wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with unknown "			   "packet_type %d", frame->packet_type);		return -1;	}	return 0;}static void wpa_ft_generate_pmk_r1(struct wpa_authenticator *wpa_auth,				   struct wpa_ft_pmk_r0_sa *pmk_r0,				   struct ft_remote_r1kh *r1kh,				   const u8 *s1kh_id){	struct ft_r0kh_r1kh_push_frame frame, f;	struct os_time now;	os_memset(&frame, 0, sizeof(frame));	frame.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;	frame.packet_type = FT_PACKET_R0KH_R1KH_PUSH;	frame.data_length = host_to_le16(FT_R0KH_R1KH_PUSH_DATA_LEN);	os_memcpy(frame.ap_address, wpa_auth->addr, ETH_ALEN);	/* aes_wrap() does not support inplace encryption, so use a temporary	 * buffer for the data. */	os_memcpy(f.r1kh_id, r1kh->id, FT_R1KH_ID_LEN);	os_memcpy(f.s1kh_id, s1kh_id, ETH_ALEN);	os_memcpy(f.pmk_r0_name, pmk_r0->pmk_r0_name, WPA_PMK_NAME_LEN);	wpa_derive_pmk_r1(pmk_r0->pmk_r0, pmk_r0->pmk_r0_name, r1kh->id,			  s1kh_id, f.pmk_r1, f.pmk_r1_name);	wpa_printf(MSG_DEBUG, "FT: R1KH-ID " MACSTR, MAC2STR(r1kh->id));	wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", f.pmk_r1, PMK_LEN);	wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", f.pmk_r1_name,		    WPA_PMK_NAME_LEN);	os_get_time(&now);	WPA_PUT_LE32(f.timestamp, now.sec);	if (aes_wrap(r1kh->key, (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8,		     f.timestamp, frame.timestamp) < 0)		return;	wpa_ft_rrb_send(wpa_auth, r1kh->addr, (u8 *) &frame, sizeof(frame));}void wpa_ft_push_pmk_r1(struct wpa_authenticator *wpa_auth, const u8 *addr){	struct wpa_ft_pmk_r0_sa *r0;	struct ft_remote_r1kh *r1kh;	if (!wpa_auth->conf.pmk_r1_push)		return;	r0 = wpa_auth->ft_pmk_cache->pmk_r0;	while (r0) {		if (os_memcmp(r0->spa, addr, ETH_ALEN) == 0)			break;		r0 = r0->next;	}	if (r0 == NULL || r0->pmk_r1_pushed)		return;	r0->pmk_r1_pushed = 1;	wpa_printf(MSG_DEBUG, "FT: Deriving and pushing PMK-R1 keys to R1KHs "		   "for STA " MACSTR, MAC2STR(addr));	r1kh = wpa_auth->conf.r1kh_list;	while (r1kh) {		wpa_ft_generate_pmk_r1(wpa_auth, r0, r1kh, addr);		r1kh = r1kh->next;	}}#endif /* CONFIG_IEEE80211R */

⌨️ 快捷键说明

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