📄 eap_sim_db.c
字号:
* * This function is used to generate a pseudonym for EAP-SIM. The returned * pseudonym is not added to database at this point; it will need to be added * with eap_sim_db_add_pseudonym() once the authentication has been completed * successfully. Caller is responsible for freeing the returned buffer. */char * eap_sim_db_get_next_pseudonym(void *priv, int aka){ struct eap_sim_db_data *data = priv; return eap_sim_db_get_next(data, aka ? EAP_AKA_PSEUDONYM_PREFIX : EAP_SIM_PSEUDONYM_PREFIX);}/** * eap_sim_db_get_next_reauth_id - EAP-SIM DB: Get next reauth_id * @priv: Private data pointer from eap_sim_db_init() * @aka: Using EAP-AKA instead of EAP-SIM * Returns: Next reauth_id (allocated string) or %NULL on failure * * This function is used to generate a fast re-authentication identity for * EAP-SIM. The returned reauth_id is not added to database at this point; it * will need to be added with eap_sim_db_add_reauth() once the authentication * has been completed successfully. Caller is responsible for freeing the * returned buffer. */char * eap_sim_db_get_next_reauth_id(void *priv, int aka){ struct eap_sim_db_data *data = priv; return eap_sim_db_get_next(data, aka ? EAP_AKA_REAUTH_ID_PREFIX : EAP_SIM_REAUTH_ID_PREFIX);}/** * eap_sim_db_add_pseudonym - EAP-SIM DB: Add new pseudonym * @priv: Private data pointer from eap_sim_db_init() * @identity: Identity of the user (may be permanent identity or pseudonym) * @identity_len: Length of identity * @pseudonym: Pseudonym for this user. This needs to be an allocated buffer, * e.g., return value from eap_sim_db_get_next_pseudonym(). Caller must not * free it. * Returns: 0 on success, -1 on failure * * This function adds a new pseudonym for EAP-SIM user. EAP-SIM DB is * responsible of freeing pseudonym buffer once it is not needed anymore. */int eap_sim_db_add_pseudonym(void *priv, const u8 *identity, size_t identity_len, char *pseudonym){ struct eap_sim_db_data *data = priv; struct eap_sim_pseudonym *p; wpa_hexdump_ascii(MSG_DEBUG, "EAP-SIM DB: Add pseudonym for identity", identity, identity_len); wpa_printf(MSG_DEBUG, "EAP-SIM DB: Pseudonym: %s", pseudonym); /* TODO: could store last two pseudonyms */ p = eap_sim_db_get_pseudonym(data, identity, identity_len); if (p == NULL) p = eap_sim_db_get_pseudonym_id(data, identity, identity_len); if (p) { wpa_printf(MSG_DEBUG, "EAP-SIM DB: Replacing previous " "pseudonym: %s", p->pseudonym); os_free(p->pseudonym); p->pseudonym = pseudonym; return 0; } p = os_zalloc(sizeof(*p)); if (p == NULL) { os_free(pseudonym); return -1; } p->next = data->pseudonyms; p->identity = os_malloc(identity_len); if (p->identity == NULL) { os_free(p); os_free(pseudonym); return -1; } os_memcpy(p->identity, identity, identity_len); p->identity_len = identity_len; p->pseudonym = pseudonym; data->pseudonyms = p; wpa_printf(MSG_DEBUG, "EAP-SIM DB: Added new pseudonym entry"); return 0;}/** * eap_sim_db_add_reauth - EAP-SIM DB: Add new re-authentication entry * @priv: Private data pointer from eap_sim_db_init() * @identity: Identity of the user (may be permanent identity or pseudonym) * @identity_len: Length of identity * @reauth_id: reauth_id for this user. This needs to be an allocated buffer, * e.g., return value from eap_sim_db_get_next_reauth_id(). Caller must not * free it. * @mk: 16-byte MK from the previous full authentication * Returns: 0 on success, -1 on failure * * This function adds a new re-authentication entry for an EAP-SIM user. * EAP-SIM DB is responsible of freeing reauth_id buffer once it is not needed * anymore. */int eap_sim_db_add_reauth(void *priv, const u8 *identity, size_t identity_len, char *reauth_id, u16 counter, const u8 *mk){ struct eap_sim_db_data *data = priv; struct eap_sim_reauth *r; wpa_hexdump_ascii(MSG_DEBUG, "EAP-SIM DB: Add reauth_id for identity", identity, identity_len); wpa_printf(MSG_DEBUG, "EAP-SIM DB: reauth_id: %s", reauth_id); r = eap_sim_db_get_reauth(data, identity, identity_len); if (r == NULL) r = eap_sim_db_get_reauth_id(data, identity, identity_len); if (r) { wpa_printf(MSG_DEBUG, "EAP-SIM DB: Replacing previous " "reauth_id: %s", r->reauth_id); os_free(r->reauth_id); r->reauth_id = reauth_id; } else { r = os_zalloc(sizeof(*r)); if (r == NULL) { os_free(reauth_id); return -1; } r->next = data->reauths; r->identity = os_malloc(identity_len); if (r->identity == NULL) { os_free(r); os_free(reauth_id); return -1; } os_memcpy(r->identity, identity, identity_len); r->identity_len = identity_len; r->reauth_id = reauth_id; data->reauths = r; wpa_printf(MSG_DEBUG, "EAP-SIM DB: Added new reauth entry"); } r->counter = counter; os_memcpy(r->mk, mk, EAP_SIM_MK_LEN); return 0;}/** * eap_sim_db_get_permanent - EAP-SIM DB: Get permanent identity * @priv: Private data pointer from eap_sim_db_init() * @identity: Identity of the user (may be permanent identity or pseudonym) * @identity_len: Length of identity * @len: Buffer for length of the returned permanent identity * Returns: Pointer to the permanent identity, or %NULL if not found */const u8 * eap_sim_db_get_permanent(void *priv, const u8 *identity, size_t identity_len, size_t *len){ struct eap_sim_db_data *data = priv; struct eap_sim_pseudonym *p; if (identity == NULL) return NULL; p = eap_sim_db_get_pseudonym(data, identity, identity_len); if (p == NULL) p = eap_sim_db_get_pseudonym_id(data, identity, identity_len); if (p == NULL) return NULL; *len = p->identity_len; return p->identity;}/** * eap_sim_db_get_reauth_entry - EAP-SIM DB: Get re-authentication entry * @priv: Private data pointer from eap_sim_db_init() * @identity: Identity of the user (may be permanent identity, pseudonym, or * reauth_id) * @identity_len: Length of identity * @len: Buffer for length of the returned permanent identity * Returns: Pointer to the re-auth entry, or %NULL if not found */struct eap_sim_reauth *eap_sim_db_get_reauth_entry(void *priv, const u8 *identity, size_t identity_len){ struct eap_sim_db_data *data = priv; struct eap_sim_reauth *r; if (identity == NULL) return NULL; r = eap_sim_db_get_reauth(data, identity, identity_len); if (r == NULL) r = eap_sim_db_get_reauth_id(data, identity, identity_len); return r;}/** * eap_sim_db_remove_reauth - EAP-SIM DB: Remove re-authentication entry * @priv: Private data pointer from eap_sim_db_init() * @reauth: Pointer to re-authentication entry from * eap_sim_db_get_reauth_entry() */void eap_sim_db_remove_reauth(void *priv, struct eap_sim_reauth *reauth){ struct eap_sim_db_data *data = priv; struct eap_sim_reauth *r, *prev = NULL; r = data->reauths; while (r) { if (r == reauth) { if (prev) prev->next = r->next; else data->reauths = r->next; eap_sim_db_free_reauth(r); return; } prev = r; r = r->next; }}/** * eap_sim_db_get_aka_auth - Get AKA authentication values * @priv: Private data pointer from eap_sim_db_init() * @identity: User name identity * @identity_len: Length of identity in bytes * @_rand: Buffer for RAND value * @autn: Buffer for AUTN value * @ik: Buffer for IK value * @ck: Buffer for CK value * @res: Buffer for RES value * @res_len: Buffer for RES length * @cb_session_ctx: Session callback context for get_complete_cb() * Returns: 0 on success, -1 (EAP_SIM_DB_FAILURE) on error (e.g., user not * found), or -2 (EAP_SIM_DB_PENDING) if results are not yet available. In this * case, the callback function registered with eap_sim_db_init() will be * called once the results become available. * * In most cases, the user name is '0' | IMSI, i.e., 0 followed by the IMSI in * ASCII format. * * When using an external server for AKA authentication, this function can * always start a request and return EAP_SIM_DB_PENDING immediately if * authentication triplets are not available. Once the authentication data are * received, callback function registered with eap_sim_db_init() is called to * notify EAP state machine to reprocess the message. This * eap_sim_db_get_aka_auth() function will then be called again and the newly * received triplets will then be given to the caller. */int eap_sim_db_get_aka_auth(void *priv, const u8 *identity, size_t identity_len, u8 *_rand, u8 *autn, u8 *ik, u8 *ck, u8 *res, size_t *res_len, void *cb_session_ctx){ struct eap_sim_db_data *data = priv; struct eap_sim_db_pending *entry; int len; size_t i; char msg[40]; if (identity_len < 2 || identity == NULL || identity[0] != EAP_AKA_PERMANENT_PREFIX) { wpa_hexdump_ascii(MSG_DEBUG, "EAP-SIM DB: unexpected identity", identity, identity_len); return EAP_SIM_DB_FAILURE; } identity++; identity_len--; for (i = 0; i < identity_len; i++) { if (identity[i] == '@') { identity_len = i; break; } } if (identity_len + 1 > sizeof(entry->imsi)) { wpa_hexdump_ascii(MSG_DEBUG, "EAP-SIM DB: unexpected identity", identity, identity_len); return EAP_SIM_DB_FAILURE; } wpa_hexdump_ascii(MSG_DEBUG, "EAP-SIM DB: Get AKA auth for IMSI", identity, identity_len); entry = eap_sim_db_get_pending(data, identity, identity_len, 1); if (entry) { if (entry->state == FAILURE) { os_free(entry); wpa_printf(MSG_DEBUG, "EAP-SIM DB: Failure"); return EAP_SIM_DB_FAILURE; } if (entry->state == PENDING) { eap_sim_db_add_pending(data, entry); wpa_printf(MSG_DEBUG, "EAP-SIM DB: Pending"); return EAP_SIM_DB_PENDING; } wpa_printf(MSG_DEBUG, "EAP-SIM DB: Returning successfully " "received authentication data"); os_memcpy(_rand, entry->u.aka.rand, EAP_AKA_RAND_LEN); os_memcpy(autn, entry->u.aka.autn, EAP_AKA_AUTN_LEN); os_memcpy(ik, entry->u.aka.ik, EAP_AKA_IK_LEN); os_memcpy(ck, entry->u.aka.ck, EAP_AKA_CK_LEN); os_memcpy(res, entry->u.aka.res, EAP_AKA_RES_MAX_LEN); *res_len = entry->u.aka.res_len; os_free(entry); return 0; } if (data->sock < 0) { if (eap_sim_db_open_socket(data) < 0) return EAP_SIM_DB_FAILURE; } len = os_snprintf(msg, sizeof(msg), "AKA-REQ-AUTH "); if (len < 0 || len + identity_len >= sizeof(msg)) return EAP_SIM_DB_FAILURE; os_memcpy(msg + len, identity, identity_len); len += identity_len; wpa_hexdump(MSG_DEBUG, "EAP-SIM DB: requesting AKA authentication " "data for IMSI", identity, identity_len); if (eap_sim_db_send(data, msg, len) < 0) return EAP_SIM_DB_FAILURE; entry = os_zalloc(sizeof(*entry)); if (entry == NULL) return EAP_SIM_DB_FAILURE; os_get_time(&entry->timestamp); entry->aka = 1; os_memcpy(entry->imsi, identity, identity_len); entry->imsi_len = identity_len; entry->cb_session_ctx = cb_session_ctx; entry->state = PENDING; eap_sim_db_add_pending(data, entry); eap_sim_db_expire_pending(data); return EAP_SIM_DB_PENDING;}/** * eap_sim_db_resynchronize - Resynchronize AKA AUTN * @priv: Private data pointer from eap_sim_db_init() * @identity: User name identity * @identity_len: Length of identity in bytes * @auts: AUTS value from the peer * @_rand: RAND value used in the rejected message * Returns: 0 on success, -1 on failure * * This function is called when the peer reports synchronization failure in the * AUTN value by sending AUTS. The AUTS and RAND values should be sent to * HLR/AuC to allow it to resynchronize with the peer. After this, * eap_sim_db_get_aka_auth() will be called again to to fetch updated * RAND/AUTN values for the next challenge. */int eap_sim_db_resynchronize(void *priv, const u8 *identity, size_t identity_len, const u8 *auts, const u8 *_rand){ struct eap_sim_db_data *data = priv; size_t i; if (identity_len < 2 || identity == NULL || identity[0] != EAP_AKA_PERMANENT_PREFIX) { wpa_hexdump_ascii(MSG_DEBUG, "EAP-SIM DB: unexpected identity", identity, identity_len); return -1; } identity++; identity_len--; for (i = 0; i < identity_len; i++) { if (identity[i] == '@') { identity_len = i; break; } } if (identity_len > 20) { wpa_hexdump_ascii(MSG_DEBUG, "EAP-SIM DB: unexpected identity", identity, identity_len); return -1; } if (data->sock >= 0) { char msg[100]; int len, ret; len = os_snprintf(msg, sizeof(msg), "AKA-AUTS "); if (len < 0 || len + identity_len >= sizeof(msg)) return -1; os_memcpy(msg + len, identity, identity_len); len += identity_len; ret = os_snprintf(msg + len, sizeof(msg) - len, " "); if (ret < 0 || (size_t) ret >= sizeof(msg) - len) return -1; len += ret; len += wpa_snprintf_hex(msg + len, sizeof(msg) - len, auts, EAP_AKA_AUTS_LEN); ret = os_snprintf(msg + len, sizeof(msg) - len, " "); if (ret < 0 || (size_t) ret >= sizeof(msg) - len) return -1; len += ret; len += wpa_snprintf_hex(msg + len, sizeof(msg) - len, _rand, EAP_AKA_RAND_LEN); wpa_hexdump(MSG_DEBUG, "EAP-SIM DB: reporting AKA AUTS for " "IMSI", identity, identity_len); if (eap_sim_db_send(data, msg, len) < 0) return -1; } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -