📄 eap_sim_common.c
字号:
} wpa_printf(MSG_DEBUG, "EAP-SIM: (encr) " "AT_NONCE_S"); if (alen != 2 + EAP_SIM_NONCE_S_LEN) { wpa_printf(MSG_INFO, "EAP-SIM: (encr) Invalid " "AT_NONCE_S (alen=%lu)", (unsigned long) alen); return -1; } attr->nonce_s = apos + 2; break; case EAP_SIM_AT_CLIENT_ERROR_CODE: if (alen != 2) { wpa_printf(MSG_INFO, "EAP-SIM: Invalid " "AT_CLIENT_ERROR_CODE length %lu", (unsigned long) alen); return -1; } attr->client_error_code = apos[0] * 256 + apos[1]; wpa_printf(MSG_DEBUG, "EAP-SIM: AT_CLIENT_ERROR_CODE " "%d", attr->client_error_code); break; case EAP_SIM_AT_IV: wpa_printf(MSG_DEBUG, "EAP-SIM: AT_IV"); if (alen != 2 + EAP_SIM_MAC_LEN) { wpa_printf(MSG_INFO, "EAP-SIM: Invalid AT_IV " "length %lu", (unsigned long) alen); return -1; } attr->iv = apos + 2; break; 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; case EAP_SIM_AT_RES: wpa_printf(MSG_DEBUG, "EAP-SIM: AT_RES"); apos += 2; alen -= 2; if (!aka || alen < EAP_AKA_MIN_RES_LEN || alen > EAP_AKA_MAX_RES_LEN) { wpa_printf(MSG_INFO, "EAP-SIM: Invalid AT_RES " "(len %lu)", (unsigned long) alen); return -1; } attr->res = apos; attr->res_len = alen; break; case EAP_SIM_AT_AUTS: wpa_printf(MSG_DEBUG, "EAP-AKA: AT_AUTS"); if (!aka) { wpa_printf(MSG_DEBUG, "EAP-SIM: " "Unexpected AT_AUTS"); return -1; } if (alen != EAP_AKA_AUTS_LEN) { wpa_printf(MSG_INFO, "EAP-AKA: Invalid AT_AUTS" " (len %lu)", (unsigned long) alen); return -1; } attr->auts = apos; break; case EAP_SIM_AT_CHECKCODE: wpa_printf(MSG_DEBUG, "EAP-AKA: AT_CHECKCODE"); if (!aka) { wpa_printf(MSG_DEBUG, "EAP-SIM: " "Unexpected AT_CHECKCODE"); return -1; } apos += 2; alen -= 2; if (alen != 0 && alen != EAP_AKA_CHECKCODE_LEN) { wpa_printf(MSG_INFO, "EAP-AKA: Invalid " "AT_CHECKCODE (len %lu)", (unsigned long) alen); return -1; } attr->checkcode = apos; attr->checkcode_len = alen; break; case EAP_SIM_AT_RESULT_IND: if (encr) { wpa_printf(MSG_ERROR, "EAP-SIM: Encrypted " "AT_RESULT_IND"); return -1; } if (alen != 2) { wpa_printf(MSG_INFO, "EAP-SIM: Invalid " "AT_RESULT_IND (alen=%lu)", (unsigned long) alen); return -1; } wpa_printf(MSG_DEBUG, "EAP-SIM: AT_RESULT_IND"); attr->result_ind = 1; 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 = os_malloc(encr_data_len); if (decrypted == NULL) return NULL; os_memcpy(decrypted, encr_data, encr_data_len); if (aes_128_cbc_decrypt(k_encr, iv, decrypted, encr_data_len)) { os_free(decrypted); return NULL; } 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"); os_free(decrypted); return NULL; } return decrypted;}#define EAP_SIM_INIT_LEN 128struct eap_sim_msg { struct wpabuf *buf; 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 = os_zalloc(sizeof(*msg)); if (msg == NULL) return NULL; msg->buf = wpabuf_alloc(EAP_SIM_INIT_LEN); if (msg->buf == NULL) { os_free(msg); return NULL; } eap = wpabuf_put(msg->buf, sizeof(*eap)); eap->code = code; eap->identifier = id; pos = wpabuf_put(msg->buf, 4); *pos++ = type; *pos++ = subtype; *pos++ = 0; /* Reserved */ *pos++ = 0; /* Reserved */ return msg;}struct wpabuf * eap_sim_msg_finish(struct eap_sim_msg *msg, const u8 *k_aut, const u8 *extra, size_t extra_len){ struct eap_hdr *eap; struct wpabuf *buf; if (msg == NULL) return NULL; eap = wpabuf_mhead(msg->buf); eap->length = host_to_be16(wpabuf_len(msg->buf)); if (k_aut && msg->mac) { eap_sim_add_mac(k_aut, (u8 *) wpabuf_head(msg->buf), wpabuf_len(msg->buf), (u8 *) wpabuf_mhead(msg->buf) + msg->mac, extra, extra_len); } buf = msg->buf; os_free(msg); return buf;}void eap_sim_msg_free(struct eap_sim_msg *msg){ if (msg) { wpabuf_free(msg->buf); os_free(msg); }}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; if (msg == NULL) return NULL; pad_len = (4 - attr_len % 4) % 4; attr_len += pad_len; if (wpabuf_resize(&msg->buf, attr_len)) return NULL; start = wpabuf_put(msg->buf, 0); wpabuf_put_u8(msg->buf, attr); wpabuf_put_u8(msg->buf, attr_len / 4); wpabuf_put_data(msg->buf, data, len); if (pad_len) os_memset(wpabuf_put(msg->buf, pad_len), 0, pad_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; if (msg == NULL) return NULL; pad_len = (4 - attr_len % 4) % 4; attr_len += pad_len; if (wpabuf_resize(&msg->buf, attr_len)) return NULL; start = wpabuf_put(msg->buf, 0); wpabuf_put_u8(msg->buf, attr); wpabuf_put_u8(msg->buf, attr_len / 4); wpabuf_put_be16(msg->buf, value); if (data) wpabuf_put_data(msg->buf, data, len); else wpabuf_put(msg->buf, len); if (pad_len) os_memset(wpabuf_put(msg->buf, pad_len), 0, pad_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 - wpabuf_head_u8(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 - wpabuf_head_u8(msg->buf)) + 4; if (os_get_random(wpabuf_mhead_u8(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 - wpabuf_head_u8(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 = wpabuf_len(msg->buf) - 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; os_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); wpabuf_mhead_u8(msg->buf)[msg->encr + 1] = encr_len / 4 + 1; return aes_128_cbc_encrypt(k_encr, wpabuf_head_u8(msg->buf) + msg->iv, wpabuf_mhead_u8(msg->buf) + msg->encr + 4, encr_len);}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); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -