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

📄 ikev2_common.c

📁 IEEE802.11 a/b/g 客户端应用程序源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
				wpa_printf(MSG_INFO, "IKEV2:   Unsupported "					   "critical payload %u - reject the "					   "entire message", next_payload);				return -1;			} else {				wpa_printf(MSG_DEBUG, "IKEV2:   Skipped "					   "unsupported payload %u",					   next_payload);			}		}		if (next_payload == IKEV2_PAYLOAD_ENCRYPTED &&		    pos + plen == end) {			/*			 * Next Payload in the case of Encrypted Payload is			 * actually the payload type for the first embedded			 * payload.			 */			payloads->encr_next_payload = phdr->next_payload;			next_payload = IKEV2_PAYLOAD_NO_NEXT_PAYLOAD;		} else			next_payload = phdr->next_payload;		pos += plen;	}	if (pos != end) {		wpa_printf(MSG_INFO, "IKEV2: Unexpected extra data after "			   "payloads");		return -1;	}	return 0;}int ikev2_derive_auth_data(int prf_alg, const struct wpabuf *sign_msg,			   const u8 *ID, size_t ID_len, u8 ID_type,			   struct ikev2_keys *keys, int initiator,			   const u8 *shared_secret, size_t shared_secret_len,			   const u8 *nonce, size_t nonce_len,			   const u8 *key_pad, size_t key_pad_len,			   u8 *auth_data){	size_t sign_len, buf_len;	u8 *sign_data, *pos, *buf, hash[IKEV2_MAX_HASH_LEN];	const struct ikev2_prf_alg *prf;	const u8 *SK_p = initiator ? keys->SK_pi : keys->SK_pr;	prf = ikev2_get_prf(prf_alg);	if (sign_msg == NULL || ID == NULL || SK_p == NULL ||	    shared_secret == NULL || nonce == NULL || prf == NULL)		return -1;	/* prf(SK_pi/r,IDi/r') */	buf_len = 4 + ID_len;	buf = os_zalloc(buf_len);	if (buf == NULL)		return -1;	buf[0] = ID_type;	os_memcpy(buf + 4, ID, ID_len);	if (ikev2_prf_hash(prf->id, SK_p, keys->SK_prf_len,			   1, (const u8 **) &buf, &buf_len, hash) < 0) {		os_free(buf);		return -1;	}	os_free(buf);	/* sign_data = msg | Nr/i | prf(SK_pi/r,IDi/r') */	sign_len = wpabuf_len(sign_msg) + nonce_len + prf->hash_len;	sign_data = os_malloc(sign_len);	if (sign_data == NULL)		return -1;	pos = sign_data;	os_memcpy(pos, wpabuf_head(sign_msg), wpabuf_len(sign_msg));	pos += wpabuf_len(sign_msg);	os_memcpy(pos, nonce, nonce_len);	pos += nonce_len;	os_memcpy(pos, hash, prf->hash_len);	/* AUTH = prf(prf(Shared Secret, key pad, sign_data) */	if (ikev2_prf_hash(prf->id, shared_secret, shared_secret_len, 1,			   &key_pad, &key_pad_len, hash) < 0 ||	    ikev2_prf_hash(prf->id, hash, prf->hash_len, 1,			   (const u8 **) &sign_data, &sign_len, auth_data) < 0)	{		os_free(sign_data);		return -1;	}	os_free(sign_data);	return 0;}u8 * ikev2_decrypt_payload(int encr_id, int integ_id,			   struct ikev2_keys *keys, int initiator,			   const struct ikev2_hdr *hdr,			   const u8 *encrypted, size_t encrypted_len,			   size_t *res_len){	size_t iv_len;	const u8 *pos, *end, *iv, *integ;	u8 hash[IKEV2_MAX_HASH_LEN], *decrypted;	size_t decrypted_len, pad_len;	const struct ikev2_integ_alg *integ_alg;	const struct ikev2_encr_alg *encr_alg;	const u8 *SK_e = initiator ? keys->SK_ei : keys->SK_er;	const u8 *SK_a = initiator ? keys->SK_ai : keys->SK_ar;	if (encrypted == NULL) {		wpa_printf(MSG_INFO, "IKEV2: No Encrypted payload in SA_AUTH");		return NULL;	}	encr_alg = ikev2_get_encr(encr_id);	if (encr_alg == NULL) {		wpa_printf(MSG_INFO, "IKEV2: Unsupported encryption type");		return NULL;	}	iv_len = encr_alg->block_size;	integ_alg = ikev2_get_integ(integ_id);	if (integ_alg == NULL) {		wpa_printf(MSG_INFO, "IKEV2: Unsupported intergrity type");		return NULL;	}	if (encrypted_len < iv_len + 1 + integ_alg->hash_len) {		wpa_printf(MSG_INFO, "IKEV2: No room for IV or Integrity "			  "Checksum");		return NULL;	}	iv = encrypted;	pos = iv + iv_len;	end = encrypted + encrypted_len;	integ = end - integ_alg->hash_len;	if (SK_a == NULL) {		wpa_printf(MSG_INFO, "IKEV2: No SK_a available");		return NULL;	}	if (ikev2_integ_hash(integ_id, SK_a, keys->SK_integ_len,			     (const u8 *) hdr,			     integ - (const u8 *) hdr, hash) < 0) {		wpa_printf(MSG_INFO, "IKEV2: Failed to calculate integrity "			   "hash");		return NULL;	}	if (os_memcmp(integ, hash, integ_alg->hash_len) != 0) {		wpa_printf(MSG_INFO, "IKEV2: Incorrect Integrity Checksum "			   "Data");		return NULL;	}	if (SK_e == NULL) {		wpa_printf(MSG_INFO, "IKEV2: No SK_e available");		return NULL;	}	decrypted_len = integ - pos;	decrypted = os_malloc(decrypted_len);	if (decrypted == NULL)		return NULL;	if (ikev2_encr_decrypt(encr_alg->id, SK_e, keys->SK_encr_len, iv, pos,			       decrypted, decrypted_len) < 0) {		os_free(decrypted);		return NULL;	}	pad_len = decrypted[decrypted_len - 1];	if (decrypted_len < pad_len + 1) {		wpa_printf(MSG_INFO, "IKEV2: Invalid padding in encrypted "			   "payload");		os_free(decrypted);		return NULL;	}	decrypted_len -= pad_len + 1;	*res_len = decrypted_len;	return decrypted;}void ikev2_update_hdr(struct wpabuf *msg){	struct ikev2_hdr *hdr;	/* Update lenth field in HDR */	hdr = wpabuf_mhead(msg);	WPA_PUT_BE32(hdr->length, wpabuf_len(msg));}int ikev2_build_encrypted(int encr_id, int integ_id, struct ikev2_keys *keys,			  int initiator, struct wpabuf *msg,			  struct wpabuf *plain, u8 next_payload){	struct ikev2_payload_hdr *phdr;	size_t plen;	size_t iv_len, pad_len;	u8 *icv, *iv;	const struct ikev2_integ_alg *integ_alg;	const struct ikev2_encr_alg *encr_alg;	const u8 *SK_e = initiator ? keys->SK_ei : keys->SK_er;	const u8 *SK_a = initiator ? keys->SK_ai : keys->SK_ar;	wpa_printf(MSG_DEBUG, "IKEV2: Adding Encrypted payload");	/* Encr - RFC 4306, Sect. 3.14 */	encr_alg = ikev2_get_encr(encr_id);	if (encr_alg == NULL) {		wpa_printf(MSG_INFO, "IKEV2: Unsupported encryption type");		return -1;	}	iv_len = encr_alg->block_size;	integ_alg = ikev2_get_integ(integ_id);	if (integ_alg == NULL) {		wpa_printf(MSG_INFO, "IKEV2: Unsupported intergrity type");		return -1;	}	if (SK_e == NULL) {		wpa_printf(MSG_INFO, "IKEV2: No SK_e available");		return -1;	}	if (SK_a == NULL) {		wpa_printf(MSG_INFO, "IKEV2: No SK_a available");		return -1;	}	phdr = wpabuf_put(msg, sizeof(*phdr));	phdr->next_payload = next_payload;	phdr->flags = 0;	iv = wpabuf_put(msg, iv_len);	if (os_get_random(iv, iv_len)) {		wpa_printf(MSG_INFO, "IKEV2: Could not generate IV");		return -1;	}	pad_len = iv_len - (wpabuf_len(plain) + 1) % iv_len;	if (pad_len == iv_len)		pad_len = 0;	wpabuf_put(plain, pad_len);	wpabuf_put_u8(plain, pad_len);	if (ikev2_encr_encrypt(encr_alg->id, SK_e, keys->SK_encr_len, iv,			       wpabuf_head(plain), wpabuf_mhead(plain),			       wpabuf_len(plain)) < 0)		return -1;	wpabuf_put_buf(msg, plain);	/* Need to update all headers (Length fields) prior to hash func */	icv = wpabuf_put(msg, integ_alg->hash_len);	plen = (u8 *) wpabuf_put(msg, 0) - (u8 *) phdr;	WPA_PUT_BE16(phdr->payload_length, plen);	ikev2_update_hdr(msg);	return ikev2_integ_hash(integ_id, SK_a, keys->SK_integ_len,				wpabuf_head(msg),				wpabuf_len(msg) - integ_alg->hash_len, icv);	return 0;}int ikev2_keys_set(struct ikev2_keys *keys){	return keys->SK_d && keys->SK_ai && keys->SK_ar && keys->SK_ei &&		keys->SK_er && keys->SK_pi && keys->SK_pr;}void ikev2_free_keys(struct ikev2_keys *keys){	os_free(keys->SK_d);	os_free(keys->SK_ai);	os_free(keys->SK_ar);	os_free(keys->SK_ei);	os_free(keys->SK_er);	os_free(keys->SK_pi);	os_free(keys->SK_pr);	keys->SK_d = keys->SK_ai = keys->SK_ar = keys->SK_ei = keys->SK_er =		keys->SK_pi = keys->SK_pr = NULL;}int ikev2_derive_sk_keys(const struct ikev2_prf_alg *prf,			 const struct ikev2_integ_alg *integ,			 const struct ikev2_encr_alg *encr,			 const u8 *skeyseed, const u8 *data, size_t data_len,			 struct ikev2_keys *keys){	u8 *keybuf, *pos;	size_t keybuf_len;	/*	 * {SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr } =	 *	prf+(SKEYSEED, Ni | Nr | SPIi | SPIr )	 */	ikev2_free_keys(keys);	keys->SK_d_len = prf->key_len;	keys->SK_integ_len = integ->key_len;	keys->SK_encr_len = encr->key_len;	keys->SK_prf_len = prf->key_len;#ifdef CCNS_PL	/* Uses encryption key length for SK_d; should be PRF length */	keys->SK_d_len = keys->SK_encr_len;#endif /* CCNS_PL */	keybuf_len = keys->SK_d_len + 2 * keys->SK_integ_len +		2 * keys->SK_encr_len + 2 * keys->SK_prf_len;	keybuf = os_malloc(keybuf_len);	if (keybuf == NULL)		return -1;	if (ikev2_prf_plus(prf->id, skeyseed, prf->hash_len,			   data, data_len, keybuf, keybuf_len)) {		os_free(keybuf);		return -1;	}	pos = keybuf;	keys->SK_d = os_malloc(keys->SK_d_len);	if (keys->SK_d) {		os_memcpy(keys->SK_d, pos, keys->SK_d_len);		wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_d",				keys->SK_d, keys->SK_d_len);	}	pos += keys->SK_d_len;	keys->SK_ai = os_malloc(keys->SK_integ_len);	if (keys->SK_ai) {		os_memcpy(keys->SK_ai, pos, keys->SK_integ_len);		wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_ai",				keys->SK_ai, keys->SK_integ_len);	}	pos += keys->SK_integ_len;	keys->SK_ar = os_malloc(keys->SK_integ_len);	if (keys->SK_ar) {		os_memcpy(keys->SK_ar, pos, keys->SK_integ_len);		wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_ar",				keys->SK_ar, keys->SK_integ_len);	}	pos += keys->SK_integ_len;	keys->SK_ei = os_malloc(keys->SK_encr_len);	if (keys->SK_ei) {		os_memcpy(keys->SK_ei, pos, keys->SK_encr_len);		wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_ei",				keys->SK_ei, keys->SK_encr_len);	}	pos += keys->SK_encr_len;	keys->SK_er = os_malloc(keys->SK_encr_len);	if (keys->SK_er) {		os_memcpy(keys->SK_er, pos, keys->SK_encr_len);		wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_er",				keys->SK_er, keys->SK_encr_len);	}	pos += keys->SK_encr_len;	keys->SK_pi = os_malloc(keys->SK_prf_len);	if (keys->SK_pi) {		os_memcpy(keys->SK_pi, pos, keys->SK_prf_len);		wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_pi",				keys->SK_pi, keys->SK_prf_len);	}	pos += keys->SK_prf_len;	keys->SK_pr = os_malloc(keys->SK_prf_len);	if (keys->SK_pr) {		os_memcpy(keys->SK_pr, pos, keys->SK_prf_len);		wpa_hexdump_key(MSG_DEBUG, "IKEV2: SK_pr",				keys->SK_pr, keys->SK_prf_len);	}	os_free(keybuf);	if (!ikev2_keys_set(keys)) {		ikev2_free_keys(keys);		return -1;	}	return 0;}

⌨️ 快捷键说明

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