2 * hostapd / EAP-SIM database/authenticator gateway
3 * Copyright (c) 2005-2010, 2012, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
8 * This is an example implementation of the EAP-SIM/AKA database/authentication
9 * gateway interface that is using an external program as an SS7 gateway to
10 * GSM/UMTS authentication center (HLR/AuC). hlr_auc_gw is an example
11 * implementation of such a gateway program. This eap_sim_db.c takes care of
12 * EAP-SIM/AKA pseudonyms and re-auth identities. It can be used with different
13 * gateway implementations for HLR/AuC access. Alternatively, it can also be
14 * completely replaced if the in-memory database of pseudonyms/re-auth
15 * identities is not suitable for some cases.
22 #endif /* CONFIG_SQLITE */
25 #include "crypto/random.h"
26 #include "eap_common/eap_sim_common.h"
27 #include "eap_server/eap_sim_db.h"
30 struct eap_sim_pseudonym {
31 struct eap_sim_pseudonym *next;
32 char *permanent; /* permanent username */
33 char *pseudonym; /* pseudonym username */
36 struct eap_sim_db_pending {
37 struct eap_sim_db_pending *next;
39 enum { PENDING, SUCCESS, FAILURE } state;
44 u8 kc[EAP_SIM_MAX_CHAL][EAP_SIM_KC_LEN];
45 u8 sres[EAP_SIM_MAX_CHAL][EAP_SIM_SRES_LEN];
46 u8 rand[EAP_SIM_MAX_CHAL][GSM_RAND_LEN];
50 u8 rand[EAP_AKA_RAND_LEN];
51 u8 autn[EAP_AKA_AUTN_LEN];
52 u8 ik[EAP_AKA_IK_LEN];
53 u8 ck[EAP_AKA_CK_LEN];
54 u8 res[EAP_AKA_RES_MAX_LEN];
60 struct eap_sim_db_data {
64 void (*get_complete_cb)(void *ctx, void *session_ctx);
66 struct eap_sim_pseudonym *pseudonyms;
67 struct eap_sim_reauth *reauths;
68 struct eap_sim_db_pending *pending;
69 unsigned int eap_sim_db_timeout;
72 char db_tmp_identity[100];
73 char db_tmp_pseudonym_str[100];
74 struct eap_sim_pseudonym db_tmp_pseudonym;
75 struct eap_sim_reauth db_tmp_reauth;
76 #endif /* CONFIG_SQLITE */
80 static void eap_sim_db_del_timeout(void *eloop_ctx, void *user_ctx);
81 static void eap_sim_db_query_timeout(void *eloop_ctx, void *user_ctx);
86 static int db_table_exists(sqlite3 *db, const char *name)
89 os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
90 return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
94 static int db_table_create_pseudonym(sqlite3 *db)
98 "CREATE TABLE pseudonyms("
99 " permanent CHAR(21) PRIMARY KEY,"
100 " pseudonym CHAR(21) NOT NULL"
103 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Adding database table for "
104 "pseudonym information");
105 if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
106 wpa_printf(MSG_ERROR, "EAP-SIM DB: SQLite error: %s", err);
115 static int db_table_create_reauth(sqlite3 *db)
119 "CREATE TABLE reauth("
120 " permanent CHAR(21) PRIMARY KEY,"
121 " reauth_id CHAR(21) NOT NULL,"
129 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Adding database table for "
130 "reauth information");
131 if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
132 wpa_printf(MSG_ERROR, "EAP-SIM DB: SQLite error: %s", err);
141 static sqlite3 * db_open(const char *db_file)
145 if (sqlite3_open(db_file, &db)) {
146 wpa_printf(MSG_ERROR, "EAP-SIM DB: Failed to open database "
147 "%s: %s", db_file, sqlite3_errmsg(db));
152 if (!db_table_exists(db, "pseudonyms") &&
153 db_table_create_pseudonym(db) < 0) {
158 if (!db_table_exists(db, "reauth") &&
159 db_table_create_reauth(db) < 0) {
168 static int valid_db_string(const char *str)
170 const char *pos = str;
172 if ((*pos < '0' || *pos > '9') &&
173 (*pos < 'a' || *pos > 'f'))
181 static int db_add_pseudonym(struct eap_sim_db_data *data,
182 const char *permanent, char *pseudonym)
187 if (!valid_db_string(permanent) || !valid_db_string(pseudonym)) {
192 os_snprintf(cmd, sizeof(cmd), "INSERT OR REPLACE INTO pseudonyms "
193 "(permanent, pseudonym) VALUES ('%s', '%s');",
194 permanent, pseudonym);
196 if (sqlite3_exec(data->sqlite_db, cmd, NULL, NULL, &err) != SQLITE_OK)
198 wpa_printf(MSG_ERROR, "EAP-SIM DB: SQLite error: %s", err);
207 static int get_pseudonym_cb(void *ctx, int argc, char *argv[], char *col[])
209 struct eap_sim_db_data *data = ctx;
212 for (i = 0; i < argc; i++) {
213 if (os_strcmp(col[i], "permanent") == 0 && argv[i]) {
214 os_strlcpy(data->db_tmp_identity, argv[i],
215 sizeof(data->db_tmp_identity));
224 db_get_pseudonym(struct eap_sim_db_data *data, const char *pseudonym)
228 if (!valid_db_string(pseudonym))
230 os_memset(&data->db_tmp_identity, 0, sizeof(data->db_tmp_identity));
231 os_snprintf(cmd, sizeof(cmd),
232 "SELECT permanent FROM pseudonyms WHERE pseudonym='%s';",
234 if (sqlite3_exec(data->sqlite_db, cmd, get_pseudonym_cb, data, NULL) !=
237 if (data->db_tmp_identity[0] == '\0')
239 return data->db_tmp_identity;
243 static int db_add_reauth(struct eap_sim_db_data *data, const char *permanent,
244 char *reauth_id, u16 counter, const u8 *mk,
245 const u8 *k_encr, const u8 *k_aut, const u8 *k_re)
247 char cmd[2000], *pos, *end;
250 if (!valid_db_string(permanent) || !valid_db_string(reauth_id)) {
256 end = pos + sizeof(cmd);
257 pos += os_snprintf(pos, end - pos, "INSERT OR REPLACE INTO reauth "
258 "(permanent, reauth_id, counter%s%s%s%s) "
259 "VALUES ('%s', '%s', %u",
261 k_encr ? ", k_encr" : "",
262 k_aut ? ", k_aut" : "",
263 k_re ? ", k_re" : "",
264 permanent, reauth_id, counter);
268 pos += os_snprintf(pos, end - pos, ", '");
269 pos += wpa_snprintf_hex(pos, end - pos, mk, EAP_SIM_MK_LEN);
270 pos += os_snprintf(pos, end - pos, "'");
274 pos += os_snprintf(pos, end - pos, ", '");
275 pos += wpa_snprintf_hex(pos, end - pos, k_encr,
277 pos += os_snprintf(pos, end - pos, "'");
281 pos += os_snprintf(pos, end - pos, ", '");
282 pos += wpa_snprintf_hex(pos, end - pos, k_aut,
283 EAP_AKA_PRIME_K_AUT_LEN);
284 pos += os_snprintf(pos, end - pos, "'");
288 pos += os_snprintf(pos, end - pos, ", '");
289 pos += wpa_snprintf_hex(pos, end - pos, k_re,
290 EAP_AKA_PRIME_K_RE_LEN);
291 pos += os_snprintf(pos, end - pos, "'");
294 os_snprintf(pos, end - pos, ");");
296 if (sqlite3_exec(data->sqlite_db, cmd, NULL, NULL, &err) != SQLITE_OK)
298 wpa_printf(MSG_ERROR, "EAP-SIM DB: SQLite error: %s", err);
307 static int get_reauth_cb(void *ctx, int argc, char *argv[], char *col[])
309 struct eap_sim_db_data *data = ctx;
311 struct eap_sim_reauth *reauth = &data->db_tmp_reauth;
313 for (i = 0; i < argc; i++) {
314 if (os_strcmp(col[i], "permanent") == 0 && argv[i]) {
315 os_strlcpy(data->db_tmp_identity, argv[i],
316 sizeof(data->db_tmp_identity));
317 reauth->permanent = data->db_tmp_identity;
318 } else if (os_strcmp(col[i], "counter") == 0 && argv[i]) {
319 reauth->counter = atoi(argv[i]);
320 } else if (os_strcmp(col[i], "mk") == 0 && argv[i]) {
321 hexstr2bin(argv[i], reauth->mk, sizeof(reauth->mk));
322 } else if (os_strcmp(col[i], "k_encr") == 0 && argv[i]) {
323 hexstr2bin(argv[i], reauth->k_encr,
324 sizeof(reauth->k_encr));
325 } else if (os_strcmp(col[i], "k_aut") == 0 && argv[i]) {
326 hexstr2bin(argv[i], reauth->k_aut,
327 sizeof(reauth->k_aut));
328 } else if (os_strcmp(col[i], "k_re") == 0 && argv[i]) {
329 hexstr2bin(argv[i], reauth->k_re,
330 sizeof(reauth->k_re));
338 static struct eap_sim_reauth *
339 db_get_reauth(struct eap_sim_db_data *data, const char *reauth_id)
343 if (!valid_db_string(reauth_id))
345 os_memset(&data->db_tmp_reauth, 0, sizeof(data->db_tmp_reauth));
346 os_strlcpy(data->db_tmp_pseudonym_str, reauth_id,
347 sizeof(data->db_tmp_pseudonym_str));
348 data->db_tmp_reauth.reauth_id = data->db_tmp_pseudonym_str;
349 os_snprintf(cmd, sizeof(cmd),
350 "SELECT * FROM reauth WHERE reauth_id='%s';", reauth_id);
351 if (sqlite3_exec(data->sqlite_db, cmd, get_reauth_cb, data, NULL) !=
354 if (data->db_tmp_reauth.permanent == NULL)
356 return &data->db_tmp_reauth;
360 static void db_remove_reauth(struct eap_sim_db_data *data,
361 struct eap_sim_reauth *reauth)
365 if (!valid_db_string(reauth->permanent))
367 os_snprintf(cmd, sizeof(cmd),
368 "DELETE FROM reauth WHERE permanent='%s';",
370 sqlite3_exec(data->sqlite_db, cmd, NULL, NULL, NULL);
373 #endif /* CONFIG_SQLITE */
376 static struct eap_sim_db_pending *
377 eap_sim_db_get_pending(struct eap_sim_db_data *data, const char *imsi, int aka)
379 struct eap_sim_db_pending *entry, *prev = NULL;
381 entry = data->pending;
383 if (entry->aka == aka && os_strcmp(entry->imsi, imsi) == 0) {
385 prev->next = entry->next;
387 data->pending = entry->next;
397 static void eap_sim_db_add_pending(struct eap_sim_db_data *data,
398 struct eap_sim_db_pending *entry)
400 entry->next = data->pending;
401 data->pending = entry;
405 static void eap_sim_db_free_pending(struct eap_sim_db_data *data,
406 struct eap_sim_db_pending *entry)
408 eloop_cancel_timeout(eap_sim_db_query_timeout, data, entry);
409 eloop_cancel_timeout(eap_sim_db_del_timeout, data, entry);
414 static void eap_sim_db_del_pending(struct eap_sim_db_data *data,
415 struct eap_sim_db_pending *entry)
417 struct eap_sim_db_pending **pp = &data->pending;
419 while (*pp != NULL) {
422 eap_sim_db_free_pending(data, entry);
430 static void eap_sim_db_del_timeout(void *eloop_ctx, void *user_ctx)
432 struct eap_sim_db_data *data = eloop_ctx;
433 struct eap_sim_db_pending *entry = user_ctx;
435 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Delete query timeout for %p", entry);
436 eap_sim_db_del_pending(data, entry);
440 static void eap_sim_db_query_timeout(void *eloop_ctx, void *user_ctx)
442 struct eap_sim_db_data *data = eloop_ctx;
443 struct eap_sim_db_pending *entry = user_ctx;
446 * Report failure and allow some time for EAP server to process it
447 * before deleting the query.
449 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Query timeout for %p", entry);
450 entry->state = FAILURE;
451 data->get_complete_cb(data->ctx, entry->cb_session_ctx);
452 eloop_register_timeout(1, 0, eap_sim_db_del_timeout, data, entry);
456 static void eap_sim_db_sim_resp_auth(struct eap_sim_db_data *data,
457 const char *imsi, char *buf)
459 char *start, *end, *pos;
460 struct eap_sim_db_pending *entry;
464 * SIM-RESP-AUTH <IMSI> Kc(i):SRES(i):RAND(i) ...
465 * SIM-RESP-AUTH <IMSI> FAILURE
466 * (IMSI = ASCII string, Kc/SRES/RAND = hex string)
469 entry = eap_sim_db_get_pending(data, imsi, 0);
471 wpa_printf(MSG_DEBUG, "EAP-SIM DB: No pending entry for the "
472 "received message found");
477 if (os_strncmp(start, "FAILURE", 7) == 0) {
478 wpa_printf(MSG_DEBUG, "EAP-SIM DB: External server reported "
480 entry->state = FAILURE;
481 eap_sim_db_add_pending(data, entry);
482 data->get_complete_cb(data->ctx, entry->cb_session_ctx);
487 while (num_chal < EAP_SIM_MAX_CHAL) {
488 end = os_strchr(start, ' ');
492 pos = os_strchr(start, ':');
496 if (hexstr2bin(start, entry->u.sim.kc[num_chal],
501 pos = os_strchr(start, ':');
505 if (hexstr2bin(start, entry->u.sim.sres[num_chal],
510 if (hexstr2bin(start, entry->u.sim.rand[num_chal],
520 entry->u.sim.num_chal = num_chal;
522 entry->state = SUCCESS;
523 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Authentication data parsed "
524 "successfully - callback");
525 eap_sim_db_add_pending(data, entry);
526 data->get_complete_cb(data->ctx, entry->cb_session_ctx);
530 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Failed to parse response string");
531 eap_sim_db_free_pending(data, entry);
535 static void eap_sim_db_aka_resp_auth(struct eap_sim_db_data *data,
536 const char *imsi, char *buf)
539 struct eap_sim_db_pending *entry;
542 * AKA-RESP-AUTH <IMSI> <RAND> <AUTN> <IK> <CK> <RES>
543 * AKA-RESP-AUTH <IMSI> FAILURE
544 * (IMSI = ASCII string, RAND/AUTN/IK/CK/RES = hex string)
547 entry = eap_sim_db_get_pending(data, imsi, 1);
549 wpa_printf(MSG_DEBUG, "EAP-SIM DB: No pending entry for the "
550 "received message found");
555 if (os_strncmp(start, "FAILURE", 7) == 0) {
556 wpa_printf(MSG_DEBUG, "EAP-SIM DB: External server reported "
558 entry->state = FAILURE;
559 eap_sim_db_add_pending(data, entry);
560 data->get_complete_cb(data->ctx, entry->cb_session_ctx);
564 end = os_strchr(start, ' ');
568 if (hexstr2bin(start, entry->u.aka.rand, EAP_AKA_RAND_LEN))
572 end = os_strchr(start, ' ');
576 if (hexstr2bin(start, entry->u.aka.autn, EAP_AKA_AUTN_LEN))
580 end = os_strchr(start, ' ');
584 if (hexstr2bin(start, entry->u.aka.ik, EAP_AKA_IK_LEN))
588 end = os_strchr(start, ' ');
592 if (hexstr2bin(start, entry->u.aka.ck, EAP_AKA_CK_LEN))
596 end = os_strchr(start, ' ');
604 entry->u.aka.res_len = (end - start) / 2;
605 if (entry->u.aka.res_len > EAP_AKA_RES_MAX_LEN) {
606 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Too long RES");
607 entry->u.aka.res_len = 0;
610 if (hexstr2bin(start, entry->u.aka.res, entry->u.aka.res_len))
613 entry->state = SUCCESS;
614 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Authentication data parsed "
615 "successfully - callback");
616 eap_sim_db_add_pending(data, entry);
617 data->get_complete_cb(data->ctx, entry->cb_session_ctx);
621 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Failed to parse response string");
622 eap_sim_db_free_pending(data, entry);
626 static void eap_sim_db_receive(int sock, void *eloop_ctx, void *sock_ctx)
628 struct eap_sim_db_data *data = eloop_ctx;
629 char buf[1000], *pos, *cmd, *imsi;
632 res = recv(sock, buf, sizeof(buf) - 1, 0);
636 wpa_hexdump_ascii_key(MSG_MSGDUMP, "EAP-SIM DB: Received from an "
637 "external source", (u8 *) buf, res);
641 if (data->get_complete_cb == NULL) {
642 wpa_printf(MSG_DEBUG, "EAP-SIM DB: No get_complete_cb "
647 /* <cmd> <IMSI> ... */
650 pos = os_strchr(cmd, ' ');
655 pos = os_strchr(imsi, ' ');
659 wpa_printf(MSG_DEBUG, "EAP-SIM DB: External response=%s for IMSI %s",
662 if (os_strcmp(cmd, "SIM-RESP-AUTH") == 0)
663 eap_sim_db_sim_resp_auth(data, imsi, pos + 1);
664 else if (os_strcmp(cmd, "AKA-RESP-AUTH") == 0)
665 eap_sim_db_aka_resp_auth(data, imsi, pos + 1);
667 wpa_printf(MSG_INFO, "EAP-SIM DB: Unknown external response "
672 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Failed to parse response string");
676 static int eap_sim_db_open_socket(struct eap_sim_db_data *data)
678 struct sockaddr_un addr;
679 static int counter = 0;
681 if (os_strncmp(data->fname, "unix:", 5) != 0)
684 data->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
685 if (data->sock < 0) {
686 wpa_printf(MSG_INFO, "socket(eap_sim_db): %s", strerror(errno));
690 os_memset(&addr, 0, sizeof(addr));
691 addr.sun_family = AF_UNIX;
692 os_snprintf(addr.sun_path, sizeof(addr.sun_path),
693 "/tmp/eap_sim_db_%d-%d", getpid(), counter++);
694 os_free(data->local_sock);
695 data->local_sock = os_strdup(addr.sun_path);
696 if (data->local_sock == NULL) {
701 if (bind(data->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
702 wpa_printf(MSG_INFO, "bind(eap_sim_db): %s", strerror(errno));
708 os_memset(&addr, 0, sizeof(addr));
709 addr.sun_family = AF_UNIX;
710 os_strlcpy(addr.sun_path, data->fname + 5, sizeof(addr.sun_path));
711 if (connect(data->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
712 wpa_printf(MSG_INFO, "connect(eap_sim_db): %s",
714 wpa_hexdump_ascii(MSG_INFO, "HLR/AuC GW socket",
715 (u8 *) addr.sun_path,
716 os_strlen(addr.sun_path));
719 unlink(data->local_sock);
720 os_free(data->local_sock);
721 data->local_sock = NULL;
725 eloop_register_read_sock(data->sock, eap_sim_db_receive, data, NULL);
731 static void eap_sim_db_close_socket(struct eap_sim_db_data *data)
733 if (data->sock >= 0) {
734 eloop_unregister_read_sock(data->sock);
738 if (data->local_sock) {
739 unlink(data->local_sock);
740 os_free(data->local_sock);
741 data->local_sock = NULL;
747 * eap_sim_db_init - Initialize EAP-SIM DB / authentication gateway interface
748 * @config: Configuration data (e.g., file name)
749 * @db_timeout: Database lookup timeout
750 * @get_complete_cb: Callback function for reporting availability of triplets
751 * @ctx: Context pointer for get_complete_cb
752 * Returns: Pointer to a private data structure or %NULL on failure
754 struct eap_sim_db_data *
755 eap_sim_db_init(const char *config, unsigned int db_timeout,
756 void (*get_complete_cb)(void *ctx, void *session_ctx),
759 struct eap_sim_db_data *data;
762 data = os_zalloc(sizeof(*data));
767 data->get_complete_cb = get_complete_cb;
769 data->eap_sim_db_timeout = db_timeout;
770 data->fname = os_strdup(config);
771 if (data->fname == NULL)
773 pos = os_strstr(data->fname, " db=");
778 data->sqlite_db = db_open(pos);
779 if (data->sqlite_db == NULL)
781 #endif /* CONFIG_SQLITE */
784 if (os_strncmp(data->fname, "unix:", 5) == 0) {
785 if (eap_sim_db_open_socket(data)) {
786 wpa_printf(MSG_DEBUG, "EAP-SIM DB: External database "
787 "connection not available - will retry "
795 eap_sim_db_close_socket(data);
796 os_free(data->fname);
802 static void eap_sim_db_free_pseudonym(struct eap_sim_pseudonym *p)
804 os_free(p->permanent);
805 os_free(p->pseudonym);
810 static void eap_sim_db_free_reauth(struct eap_sim_reauth *r)
812 os_free(r->permanent);
813 os_free(r->reauth_id);
819 * eap_sim_db_deinit - Deinitialize EAP-SIM DB/authentication gw interface
820 * @priv: Private data pointer from eap_sim_db_init()
822 void eap_sim_db_deinit(void *priv)
824 struct eap_sim_db_data *data = priv;
825 struct eap_sim_pseudonym *p, *prev;
826 struct eap_sim_reauth *r, *prevr;
827 struct eap_sim_db_pending *pending, *prev_pending;
830 if (data->sqlite_db) {
831 sqlite3_close(data->sqlite_db);
832 data->sqlite_db = NULL;
834 #endif /* CONFIG_SQLITE */
836 eap_sim_db_close_socket(data);
837 os_free(data->fname);
839 p = data->pseudonyms;
843 eap_sim_db_free_pseudonym(prev);
850 eap_sim_db_free_reauth(prevr);
853 pending = data->pending;
855 prev_pending = pending;
856 pending = pending->next;
857 eap_sim_db_free_pending(data, prev_pending);
864 static int eap_sim_db_send(struct eap_sim_db_data *data, const char *msg,
869 if (send(data->sock, msg, len, 0) < 0) {
871 wpa_printf(MSG_INFO, "send[EAP-SIM DB UNIX]: %s",
875 if (_errno == ENOTCONN || _errno == EDESTADDRREQ || _errno == EINVAL ||
876 _errno == ECONNREFUSED) {
877 /* Try to reconnect */
878 eap_sim_db_close_socket(data);
879 if (eap_sim_db_open_socket(data) < 0)
881 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Reconnected to the "
883 if (send(data->sock, msg, len, 0) < 0) {
884 wpa_printf(MSG_INFO, "send[EAP-SIM DB UNIX]: %s",
894 static void eap_sim_db_expire_pending(struct eap_sim_db_data *data,
895 struct eap_sim_db_pending *entry)
897 eloop_register_timeout(data->eap_sim_db_timeout, 0,
898 eap_sim_db_query_timeout, data, entry);
903 * eap_sim_db_get_gsm_triplets - Get GSM triplets
904 * @data: Private data pointer from eap_sim_db_init()
905 * @username: Permanent username (prefix | IMSI)
906 * @max_chal: Maximum number of triplets
907 * @_rand: Buffer for RAND values
908 * @kc: Buffer for Kc values
909 * @sres: Buffer for SRES values
910 * @cb_session_ctx: Session callback context for get_complete_cb()
911 * Returns: Number of triplets received (has to be less than or equal to
912 * max_chal), -1 (EAP_SIM_DB_FAILURE) on error (e.g., user not found), or
913 * -2 (EAP_SIM_DB_PENDING) if results are not yet available. In this case, the
914 * callback function registered with eap_sim_db_init() will be called once the
915 * results become available.
917 * When using an external server for GSM triplets, this function can always
918 * start a request and return EAP_SIM_DB_PENDING immediately if authentication
919 * triplets are not available. Once the triplets are received, callback
920 * function registered with eap_sim_db_init() is called to notify EAP state
921 * machine to reprocess the message. This eap_sim_db_get_gsm_triplets()
922 * function will then be called again and the newly received triplets will then
923 * be given to the caller.
925 int eap_sim_db_get_gsm_triplets(struct eap_sim_db_data *data,
926 const char *username, int max_chal,
927 u8 *_rand, u8 *kc, u8 *sres,
928 void *cb_session_ctx)
930 struct eap_sim_db_pending *entry;
936 if (username == NULL || username[0] != EAP_SIM_PERMANENT_PREFIX ||
937 username[1] == '\0' || os_strlen(username) > sizeof(entry->imsi)) {
938 wpa_printf(MSG_DEBUG, "EAP-SIM DB: unexpected username '%s'",
940 return EAP_SIM_DB_FAILURE;
943 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Get GSM triplets for IMSI '%s'",
946 entry = eap_sim_db_get_pending(data, imsi, 0);
949 if (entry->state == FAILURE) {
950 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Pending entry -> "
952 eap_sim_db_free_pending(data, entry);
953 return EAP_SIM_DB_FAILURE;
956 if (entry->state == PENDING) {
957 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Pending entry -> "
959 eap_sim_db_add_pending(data, entry);
960 return EAP_SIM_DB_PENDING;
963 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Pending entry -> "
964 "%d challenges", entry->u.sim.num_chal);
965 num_chal = entry->u.sim.num_chal;
966 if (num_chal > max_chal)
968 os_memcpy(_rand, entry->u.sim.rand, num_chal * GSM_RAND_LEN);
969 os_memcpy(sres, entry->u.sim.sres,
970 num_chal * EAP_SIM_SRES_LEN);
971 os_memcpy(kc, entry->u.sim.kc, num_chal * EAP_SIM_KC_LEN);
972 eap_sim_db_free_pending(data, entry);
976 if (data->sock < 0) {
977 if (eap_sim_db_open_socket(data) < 0)
978 return EAP_SIM_DB_FAILURE;
981 imsi_len = os_strlen(imsi);
982 len = os_snprintf(msg, sizeof(msg), "SIM-REQ-AUTH ");
983 if (os_snprintf_error(sizeof(msg), len) ||
984 len + imsi_len >= sizeof(msg))
985 return EAP_SIM_DB_FAILURE;
986 os_memcpy(msg + len, imsi, imsi_len);
988 ret = os_snprintf(msg + len, sizeof(msg) - len, " %d", max_chal);
989 if (os_snprintf_error(sizeof(msg) - len, ret))
990 return EAP_SIM_DB_FAILURE;
993 wpa_printf(MSG_DEBUG, "EAP-SIM DB: requesting SIM authentication "
994 "data for IMSI '%s'", imsi);
995 if (eap_sim_db_send(data, msg, len) < 0)
996 return EAP_SIM_DB_FAILURE;
998 entry = os_zalloc(sizeof(*entry));
1000 return EAP_SIM_DB_FAILURE;
1002 os_strlcpy(entry->imsi, imsi, sizeof(entry->imsi));
1003 entry->cb_session_ctx = cb_session_ctx;
1004 entry->state = PENDING;
1005 eap_sim_db_add_pending(data, entry);
1006 eap_sim_db_expire_pending(data, entry);
1007 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Added query %p", entry);
1009 return EAP_SIM_DB_PENDING;
1013 static char * eap_sim_db_get_next(struct eap_sim_db_data *data, char prefix)
1015 char *id, *pos, *end;
1018 if (random_get_bytes(buf, sizeof(buf)))
1020 id = os_malloc(sizeof(buf) * 2 + 2);
1025 end = id + sizeof(buf) * 2 + 2;
1027 wpa_snprintf_hex(pos, end - pos, buf, sizeof(buf));
1034 * eap_sim_db_get_next_pseudonym - EAP-SIM DB: Get next pseudonym
1035 * @data: Private data pointer from eap_sim_db_init()
1036 * @method: EAP method (SIM/AKA/AKA')
1037 * Returns: Next pseudonym (allocated string) or %NULL on failure
1039 * This function is used to generate a pseudonym for EAP-SIM. The returned
1040 * pseudonym is not added to database at this point; it will need to be added
1041 * with eap_sim_db_add_pseudonym() once the authentication has been completed
1042 * successfully. Caller is responsible for freeing the returned buffer.
1044 char * eap_sim_db_get_next_pseudonym(struct eap_sim_db_data *data,
1045 enum eap_sim_db_method method)
1047 char prefix = EAP_SIM_REAUTH_ID_PREFIX;
1050 case EAP_SIM_DB_SIM:
1051 prefix = EAP_SIM_PSEUDONYM_PREFIX;
1053 case EAP_SIM_DB_AKA:
1054 prefix = EAP_AKA_PSEUDONYM_PREFIX;
1056 case EAP_SIM_DB_AKA_PRIME:
1057 prefix = EAP_AKA_PRIME_PSEUDONYM_PREFIX;
1061 return eap_sim_db_get_next(data, prefix);
1066 * eap_sim_db_get_next_reauth_id - EAP-SIM DB: Get next reauth_id
1067 * @data: Private data pointer from eap_sim_db_init()
1068 * @method: EAP method (SIM/AKA/AKA')
1069 * Returns: Next reauth_id (allocated string) or %NULL on failure
1071 * This function is used to generate a fast re-authentication identity for
1072 * EAP-SIM. The returned reauth_id is not added to database at this point; it
1073 * will need to be added with eap_sim_db_add_reauth() once the authentication
1074 * has been completed successfully. Caller is responsible for freeing the
1077 char * eap_sim_db_get_next_reauth_id(struct eap_sim_db_data *data,
1078 enum eap_sim_db_method method)
1080 char prefix = EAP_SIM_REAUTH_ID_PREFIX;
1083 case EAP_SIM_DB_SIM:
1084 prefix = EAP_SIM_REAUTH_ID_PREFIX;
1086 case EAP_SIM_DB_AKA:
1087 prefix = EAP_AKA_REAUTH_ID_PREFIX;
1089 case EAP_SIM_DB_AKA_PRIME:
1090 prefix = EAP_AKA_PRIME_REAUTH_ID_PREFIX;
1094 return eap_sim_db_get_next(data, prefix);
1099 * eap_sim_db_add_pseudonym - EAP-SIM DB: Add new pseudonym
1100 * @data: Private data pointer from eap_sim_db_init()
1101 * @permanent: Permanent username
1102 * @pseudonym: Pseudonym for this user. This needs to be an allocated buffer,
1103 * e.g., return value from eap_sim_db_get_next_pseudonym(). Caller must not
1105 * Returns: 0 on success, -1 on failure
1107 * This function adds a new pseudonym for EAP-SIM user. EAP-SIM DB is
1108 * responsible of freeing pseudonym buffer once it is not needed anymore.
1110 int eap_sim_db_add_pseudonym(struct eap_sim_db_data *data,
1111 const char *permanent, char *pseudonym)
1113 struct eap_sim_pseudonym *p;
1114 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Add pseudonym '%s' for permanent "
1115 "username '%s'", pseudonym, permanent);
1117 /* TODO: could store last two pseudonyms */
1118 #ifdef CONFIG_SQLITE
1119 if (data->sqlite_db)
1120 return db_add_pseudonym(data, permanent, pseudonym);
1121 #endif /* CONFIG_SQLITE */
1122 for (p = data->pseudonyms; p; p = p->next) {
1123 if (os_strcmp(permanent, p->permanent) == 0)
1127 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Replacing previous "
1128 "pseudonym: %s", p->pseudonym);
1129 os_free(p->pseudonym);
1130 p->pseudonym = pseudonym;
1134 p = os_zalloc(sizeof(*p));
1140 p->next = data->pseudonyms;
1141 p->permanent = os_strdup(permanent);
1142 if (p->permanent == NULL) {
1147 p->pseudonym = pseudonym;
1148 data->pseudonyms = p;
1150 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Added new pseudonym entry");
1155 static struct eap_sim_reauth *
1156 eap_sim_db_add_reauth_data(struct eap_sim_db_data *data,
1157 const char *permanent,
1158 char *reauth_id, u16 counter)
1160 struct eap_sim_reauth *r;
1162 for (r = data->reauths; r; r = r->next) {
1163 if (os_strcmp(r->permanent, permanent) == 0)
1168 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Replacing previous "
1169 "reauth_id: %s", r->reauth_id);
1170 os_free(r->reauth_id);
1171 r->reauth_id = reauth_id;
1173 r = os_zalloc(sizeof(*r));
1179 r->next = data->reauths;
1180 r->permanent = os_strdup(permanent);
1181 if (r->permanent == NULL) {
1186 r->reauth_id = reauth_id;
1188 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Added new reauth entry");
1191 r->counter = counter;
1198 * eap_sim_db_add_reauth - EAP-SIM DB: Add new re-authentication entry
1199 * @priv: Private data pointer from eap_sim_db_init()
1200 * @permanent: Permanent username
1201 * @identity_len: Length of identity
1202 * @reauth_id: reauth_id for this user. This needs to be an allocated buffer,
1203 * e.g., return value from eap_sim_db_get_next_reauth_id(). Caller must not
1205 * @counter: AT_COUNTER value for fast re-authentication
1206 * @mk: 16-byte MK from the previous full authentication or %NULL
1207 * Returns: 0 on success, -1 on failure
1209 * This function adds a new re-authentication entry for an EAP-SIM user.
1210 * EAP-SIM DB is responsible of freeing reauth_id buffer once it is not needed
1213 int eap_sim_db_add_reauth(struct eap_sim_db_data *data, const char *permanent,
1214 char *reauth_id, u16 counter, const u8 *mk)
1216 struct eap_sim_reauth *r;
1218 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Add reauth_id '%s' for permanent "
1219 "identity '%s'", reauth_id, permanent);
1221 #ifdef CONFIG_SQLITE
1222 if (data->sqlite_db)
1223 return db_add_reauth(data, permanent, reauth_id, counter, mk,
1225 #endif /* CONFIG_SQLITE */
1226 r = eap_sim_db_add_reauth_data(data, permanent, reauth_id, counter);
1230 os_memcpy(r->mk, mk, EAP_SIM_MK_LEN);
1236 #ifdef EAP_SERVER_AKA_PRIME
1238 * eap_sim_db_add_reauth_prime - EAP-AKA' DB: Add new re-authentication entry
1239 * @data: Private data pointer from eap_sim_db_init()
1240 * @permanent: Permanent username
1241 * @reauth_id: reauth_id for this user. This needs to be an allocated buffer,
1242 * e.g., return value from eap_sim_db_get_next_reauth_id(). Caller must not
1244 * @counter: AT_COUNTER value for fast re-authentication
1245 * @k_encr: K_encr from the previous full authentication
1246 * @k_aut: K_aut from the previous full authentication
1247 * @k_re: 32-byte K_re from the previous full authentication
1248 * Returns: 0 on success, -1 on failure
1250 * This function adds a new re-authentication entry for an EAP-AKA' user.
1251 * EAP-SIM DB is responsible of freeing reauth_id buffer once it is not needed
1254 int eap_sim_db_add_reauth_prime(struct eap_sim_db_data *data,
1255 const char *permanent, char *reauth_id,
1256 u16 counter, const u8 *k_encr,
1257 const u8 *k_aut, const u8 *k_re)
1259 struct eap_sim_reauth *r;
1261 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Add reauth_id '%s' for permanent "
1262 "identity '%s'", reauth_id, permanent);
1264 #ifdef CONFIG_SQLITE
1265 if (data->sqlite_db)
1266 return db_add_reauth(data, permanent, reauth_id, counter, NULL,
1267 k_encr, k_aut, k_re);
1268 #endif /* CONFIG_SQLITE */
1269 r = eap_sim_db_add_reauth_data(data, permanent, reauth_id, counter);
1273 os_memcpy(r->k_encr, k_encr, EAP_SIM_K_ENCR_LEN);
1274 os_memcpy(r->k_aut, k_aut, EAP_AKA_PRIME_K_AUT_LEN);
1275 os_memcpy(r->k_re, k_re, EAP_AKA_PRIME_K_RE_LEN);
1279 #endif /* EAP_SERVER_AKA_PRIME */
1283 * eap_sim_db_get_permanent - EAP-SIM DB: Get permanent identity
1284 * @data: Private data pointer from eap_sim_db_init()
1285 * @pseudonym: Pseudonym username
1286 * Returns: Pointer to permanent username or %NULL if not found
1289 eap_sim_db_get_permanent(struct eap_sim_db_data *data, const char *pseudonym)
1291 struct eap_sim_pseudonym *p;
1293 #ifdef CONFIG_SQLITE
1294 if (data->sqlite_db)
1295 return db_get_pseudonym(data, pseudonym);
1296 #endif /* CONFIG_SQLITE */
1298 p = data->pseudonyms;
1300 if (os_strcmp(p->pseudonym, pseudonym) == 0)
1301 return p->permanent;
1310 * eap_sim_db_get_reauth_entry - EAP-SIM DB: Get re-authentication entry
1311 * @data: Private data pointer from eap_sim_db_init()
1312 * @reauth_id: Fast re-authentication username
1313 * Returns: Pointer to the re-auth entry, or %NULL if not found
1315 struct eap_sim_reauth *
1316 eap_sim_db_get_reauth_entry(struct eap_sim_db_data *data,
1317 const char *reauth_id)
1319 struct eap_sim_reauth *r;
1321 #ifdef CONFIG_SQLITE
1322 if (data->sqlite_db)
1323 return db_get_reauth(data, reauth_id);
1324 #endif /* CONFIG_SQLITE */
1328 if (os_strcmp(r->reauth_id, reauth_id) == 0)
1338 * eap_sim_db_remove_reauth - EAP-SIM DB: Remove re-authentication entry
1339 * @data: Private data pointer from eap_sim_db_init()
1340 * @reauth: Pointer to re-authentication entry from
1341 * eap_sim_db_get_reauth_entry()
1343 void eap_sim_db_remove_reauth(struct eap_sim_db_data *data,
1344 struct eap_sim_reauth *reauth)
1346 struct eap_sim_reauth *r, *prev = NULL;
1347 #ifdef CONFIG_SQLITE
1348 if (data->sqlite_db) {
1349 db_remove_reauth(data, reauth);
1352 #endif /* CONFIG_SQLITE */
1357 prev->next = r->next;
1359 data->reauths = r->next;
1360 eap_sim_db_free_reauth(r);
1370 * eap_sim_db_get_aka_auth - Get AKA authentication values
1371 * @data: Private data pointer from eap_sim_db_init()
1372 * @username: Permanent username (prefix | IMSI)
1373 * @_rand: Buffer for RAND value
1374 * @autn: Buffer for AUTN value
1375 * @ik: Buffer for IK value
1376 * @ck: Buffer for CK value
1377 * @res: Buffer for RES value
1378 * @res_len: Buffer for RES length
1379 * @cb_session_ctx: Session callback context for get_complete_cb()
1380 * Returns: 0 on success, -1 (EAP_SIM_DB_FAILURE) on error (e.g., user not
1381 * found), or -2 (EAP_SIM_DB_PENDING) if results are not yet available. In this
1382 * case, the callback function registered with eap_sim_db_init() will be
1383 * called once the results become available.
1385 * When using an external server for AKA authentication, this function can
1386 * always start a request and return EAP_SIM_DB_PENDING immediately if
1387 * authentication triplets are not available. Once the authentication data are
1388 * received, callback function registered with eap_sim_db_init() is called to
1389 * notify EAP state machine to reprocess the message. This
1390 * eap_sim_db_get_aka_auth() function will then be called again and the newly
1391 * received triplets will then be given to the caller.
1393 int eap_sim_db_get_aka_auth(struct eap_sim_db_data *data, const char *username,
1394 u8 *_rand, u8 *autn, u8 *ik, u8 *ck,
1395 u8 *res, size_t *res_len, void *cb_session_ctx)
1397 struct eap_sim_db_pending *entry;
1403 if (username == NULL ||
1404 (username[0] != EAP_AKA_PERMANENT_PREFIX &&
1405 username[0] != EAP_AKA_PRIME_PERMANENT_PREFIX) ||
1406 username[1] == '\0' || os_strlen(username) > sizeof(entry->imsi)) {
1407 wpa_printf(MSG_DEBUG, "EAP-SIM DB: unexpected username '%s'",
1409 return EAP_SIM_DB_FAILURE;
1411 imsi = username + 1;
1412 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Get AKA auth for IMSI '%s'",
1415 entry = eap_sim_db_get_pending(data, imsi, 1);
1417 if (entry->state == FAILURE) {
1418 eap_sim_db_free_pending(data, entry);
1419 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Failure");
1420 return EAP_SIM_DB_FAILURE;
1423 if (entry->state == PENDING) {
1424 eap_sim_db_add_pending(data, entry);
1425 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Pending");
1426 return EAP_SIM_DB_PENDING;
1429 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Returning successfully "
1430 "received authentication data");
1431 os_memcpy(_rand, entry->u.aka.rand, EAP_AKA_RAND_LEN);
1432 os_memcpy(autn, entry->u.aka.autn, EAP_AKA_AUTN_LEN);
1433 os_memcpy(ik, entry->u.aka.ik, EAP_AKA_IK_LEN);
1434 os_memcpy(ck, entry->u.aka.ck, EAP_AKA_CK_LEN);
1435 os_memcpy(res, entry->u.aka.res, EAP_AKA_RES_MAX_LEN);
1436 *res_len = entry->u.aka.res_len;
1437 eap_sim_db_free_pending(data, entry);
1441 if (data->sock < 0) {
1442 if (eap_sim_db_open_socket(data) < 0)
1443 return EAP_SIM_DB_FAILURE;
1446 imsi_len = os_strlen(imsi);
1447 len = os_snprintf(msg, sizeof(msg), "AKA-REQ-AUTH ");
1448 if (os_snprintf_error(sizeof(msg), len) ||
1449 len + imsi_len >= sizeof(msg))
1450 return EAP_SIM_DB_FAILURE;
1451 os_memcpy(msg + len, imsi, imsi_len);
1454 wpa_printf(MSG_DEBUG, "EAP-SIM DB: requesting AKA authentication "
1455 "data for IMSI '%s'", imsi);
1456 if (eap_sim_db_send(data, msg, len) < 0)
1457 return EAP_SIM_DB_FAILURE;
1459 entry = os_zalloc(sizeof(*entry));
1461 return EAP_SIM_DB_FAILURE;
1464 os_strlcpy(entry->imsi, imsi, sizeof(entry->imsi));
1465 entry->cb_session_ctx = cb_session_ctx;
1466 entry->state = PENDING;
1467 eap_sim_db_add_pending(data, entry);
1468 eap_sim_db_expire_pending(data, entry);
1469 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Added query %p", entry);
1471 return EAP_SIM_DB_PENDING;
1476 * eap_sim_db_resynchronize - Resynchronize AKA AUTN
1477 * @data: Private data pointer from eap_sim_db_init()
1478 * @username: Permanent username
1479 * @auts: AUTS value from the peer
1480 * @_rand: RAND value used in the rejected message
1481 * Returns: 0 on success, -1 on failure
1483 * This function is called when the peer reports synchronization failure in the
1484 * AUTN value by sending AUTS. The AUTS and RAND values should be sent to
1485 * HLR/AuC to allow it to resynchronize with the peer. After this,
1486 * eap_sim_db_get_aka_auth() will be called again to to fetch updated
1487 * RAND/AUTN values for the next challenge.
1489 int eap_sim_db_resynchronize(struct eap_sim_db_data *data,
1490 const char *username,
1491 const u8 *auts, const u8 *_rand)
1496 if (username == NULL ||
1497 (username[0] != EAP_AKA_PERMANENT_PREFIX &&
1498 username[0] != EAP_AKA_PRIME_PERMANENT_PREFIX) ||
1499 username[1] == '\0' || os_strlen(username) > 20) {
1500 wpa_printf(MSG_DEBUG, "EAP-SIM DB: unexpected username '%s'",
1504 imsi = username + 1;
1505 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Get AKA auth for IMSI '%s'",
1508 if (data->sock >= 0) {
1512 imsi_len = os_strlen(imsi);
1513 len = os_snprintf(msg, sizeof(msg), "AKA-AUTS ");
1514 if (os_snprintf_error(sizeof(msg), len) ||
1515 len + imsi_len >= sizeof(msg))
1517 os_memcpy(msg + len, imsi, imsi_len);
1520 ret = os_snprintf(msg + len, sizeof(msg) - len, " ");
1521 if (os_snprintf_error(sizeof(msg) - len, ret))
1524 len += wpa_snprintf_hex(msg + len, sizeof(msg) - len,
1525 auts, EAP_AKA_AUTS_LEN);
1526 ret = os_snprintf(msg + len, sizeof(msg) - len, " ");
1527 if (os_snprintf_error(sizeof(msg) - len, ret))
1530 len += wpa_snprintf_hex(msg + len, sizeof(msg) - len,
1531 _rand, EAP_AKA_RAND_LEN);
1532 wpa_printf(MSG_DEBUG, "EAP-SIM DB: reporting AKA AUTS for "
1534 if (eap_sim_db_send(data, msg, len) < 0)
1543 * sim_get_username - Extract username from SIM identity
1544 * @identity: Identity
1545 * @identity_len: Identity length
1546 * Returns: Allocated buffer with the username part of the identity
1548 * Caller is responsible for freeing the returned buffer with os_free().
1550 char * sim_get_username(const u8 *identity, size_t identity_len)
1554 if (identity == NULL)
1557 for (pos = 0; pos < identity_len; pos++) {
1558 if (identity[pos] == '@' || identity[pos] == '\0')
1562 return dup_binstr(identity, pos);