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;
41 struct os_time timestamp;
45 u8 kc[EAP_SIM_MAX_CHAL][EAP_SIM_KC_LEN];
46 u8 sres[EAP_SIM_MAX_CHAL][EAP_SIM_SRES_LEN];
47 u8 rand[EAP_SIM_MAX_CHAL][GSM_RAND_LEN];
51 u8 rand[EAP_AKA_RAND_LEN];
52 u8 autn[EAP_AKA_AUTN_LEN];
53 u8 ik[EAP_AKA_IK_LEN];
54 u8 ck[EAP_AKA_CK_LEN];
55 u8 res[EAP_AKA_RES_MAX_LEN];
61 struct eap_sim_db_data {
65 void (*get_complete_cb)(void *ctx, void *session_ctx);
67 struct eap_sim_pseudonym *pseudonyms;
68 struct eap_sim_reauth *reauths;
69 struct eap_sim_db_pending *pending;
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 */
82 static int db_table_exists(sqlite3 *db, const char *name)
85 os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
86 return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
90 static int db_table_create_pseudonym(sqlite3 *db)
94 "CREATE TABLE pseudonyms("
95 " permanent CHAR(21) PRIMARY KEY,"
96 " pseudonym CHAR(21) NOT NULL"
99 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Adding database table for "
100 "pseudonym information");
101 if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
102 wpa_printf(MSG_ERROR, "EAP-SIM DB: SQLite error: %s", err);
111 static int db_table_create_reauth(sqlite3 *db)
115 "CREATE TABLE reauth("
116 " permanent CHAR(21) PRIMARY KEY,"
117 " reauth_id CHAR(21) NOT NULL,"
125 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Adding database table for "
126 "reauth information");
127 if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
128 wpa_printf(MSG_ERROR, "EAP-SIM DB: SQLite error: %s", err);
137 static sqlite3 * db_open(const char *db_file)
141 if (sqlite3_open(db_file, &db)) {
142 wpa_printf(MSG_ERROR, "EAP-SIM DB: Failed to open database "
143 "%s: %s", db_file, sqlite3_errmsg(db));
148 if (!db_table_exists(db, "pseudonyms") &&
149 db_table_create_pseudonym(db) < 0) {
154 if (!db_table_exists(db, "reauth") &&
155 db_table_create_reauth(db) < 0) {
164 static int valid_db_string(const char *str)
166 const char *pos = str;
168 if ((*pos < '0' || *pos > '9') &&
169 (*pos < 'a' || *pos > 'f'))
177 static int db_add_pseudonym(struct eap_sim_db_data *data,
178 const char *permanent, char *pseudonym)
183 if (!valid_db_string(permanent) || !valid_db_string(pseudonym)) {
188 os_snprintf(cmd, sizeof(cmd), "INSERT OR REPLACE INTO pseudonyms "
189 "(permanent, pseudonym) VALUES ('%s', '%s');",
190 permanent, pseudonym);
192 if (sqlite3_exec(data->sqlite_db, cmd, NULL, NULL, &err) != SQLITE_OK)
194 wpa_printf(MSG_ERROR, "EAP-SIM DB: SQLite error: %s", err);
203 static int get_pseudonym_cb(void *ctx, int argc, char *argv[], char *col[])
205 struct eap_sim_db_data *data = ctx;
208 for (i = 0; i < argc; i++) {
209 if (os_strcmp(col[i], "permanent") == 0 && argv[i]) {
210 os_strlcpy(data->db_tmp_identity, argv[i],
211 sizeof(data->db_tmp_identity));
220 db_get_pseudonym(struct eap_sim_db_data *data, const char *pseudonym)
224 if (!valid_db_string(pseudonym))
226 os_memset(&data->db_tmp_identity, 0, sizeof(data->db_tmp_identity));
227 os_snprintf(cmd, sizeof(cmd),
228 "SELECT permanent FROM pseudonyms WHERE pseudonym='%s';",
230 if (sqlite3_exec(data->sqlite_db, cmd, get_pseudonym_cb, data, NULL) !=
233 if (data->db_tmp_identity[0] == '\0')
235 return data->db_tmp_identity;
239 static int db_add_reauth(struct eap_sim_db_data *data, const char *permanent,
240 char *reauth_id, u16 counter, const u8 *mk,
241 const u8 *k_encr, const u8 *k_aut, const u8 *k_re)
243 char cmd[2000], *pos, *end;
246 if (!valid_db_string(permanent) || !valid_db_string(reauth_id)) {
252 end = pos + sizeof(cmd);
253 pos += os_snprintf(pos, end - pos, "INSERT OR REPLACE INTO reauth "
254 "(permanent, reauth_id, counter%s%s%s%s) "
255 "VALUES ('%s', '%s', %u",
257 k_encr ? ", k_encr" : "",
258 k_aut ? ", k_aut" : "",
259 k_re ? ", k_re" : "",
260 permanent, reauth_id, counter);
264 pos += os_snprintf(pos, end - pos, ", '");
265 pos += wpa_snprintf_hex(pos, end - pos, mk, EAP_SIM_MK_LEN);
266 pos += os_snprintf(pos, end - pos, "'");
270 pos += os_snprintf(pos, end - pos, ", '");
271 pos += wpa_snprintf_hex(pos, end - pos, k_encr,
273 pos += os_snprintf(pos, end - pos, "'");
277 pos += os_snprintf(pos, end - pos, ", '");
278 pos += wpa_snprintf_hex(pos, end - pos, k_aut,
279 EAP_AKA_PRIME_K_AUT_LEN);
280 pos += os_snprintf(pos, end - pos, "'");
284 pos += os_snprintf(pos, end - pos, ", '");
285 pos += wpa_snprintf_hex(pos, end - pos, k_re,
286 EAP_AKA_PRIME_K_RE_LEN);
287 pos += os_snprintf(pos, end - pos, "'");
290 os_snprintf(pos, end - pos, ");");
292 if (sqlite3_exec(data->sqlite_db, cmd, NULL, NULL, &err) != SQLITE_OK)
294 wpa_printf(MSG_ERROR, "EAP-SIM DB: SQLite error: %s", err);
303 static int get_reauth_cb(void *ctx, int argc, char *argv[], char *col[])
305 struct eap_sim_db_data *data = ctx;
307 struct eap_sim_reauth *reauth = &data->db_tmp_reauth;
309 for (i = 0; i < argc; i++) {
310 if (os_strcmp(col[i], "permanent") == 0 && argv[i]) {
311 os_strlcpy(data->db_tmp_identity, argv[i],
312 sizeof(data->db_tmp_identity));
313 reauth->permanent = data->db_tmp_identity;
314 } else if (os_strcmp(col[i], "counter") == 0 && argv[i]) {
315 reauth->counter = atoi(argv[i]);
316 } else if (os_strcmp(col[i], "mk") == 0 && argv[i]) {
317 hexstr2bin(argv[i], reauth->mk, sizeof(reauth->mk));
318 } else if (os_strcmp(col[i], "k_encr") == 0 && argv[i]) {
319 hexstr2bin(argv[i], reauth->k_encr,
320 sizeof(reauth->k_encr));
321 } else if (os_strcmp(col[i], "k_aut") == 0 && argv[i]) {
322 hexstr2bin(argv[i], reauth->k_aut,
323 sizeof(reauth->k_aut));
324 } else if (os_strcmp(col[i], "k_re") == 0 && argv[i]) {
325 hexstr2bin(argv[i], reauth->k_re,
326 sizeof(reauth->k_re));
334 static struct eap_sim_reauth *
335 db_get_reauth(struct eap_sim_db_data *data, const char *reauth_id)
339 if (!valid_db_string(reauth_id))
341 os_memset(&data->db_tmp_reauth, 0, sizeof(data->db_tmp_reauth));
342 os_strlcpy(data->db_tmp_pseudonym_str, reauth_id,
343 sizeof(data->db_tmp_pseudonym_str));
344 data->db_tmp_reauth.reauth_id = data->db_tmp_pseudonym_str;
345 os_snprintf(cmd, sizeof(cmd),
346 "SELECT * FROM reauth WHERE reauth_id='%s';", reauth_id);
347 if (sqlite3_exec(data->sqlite_db, cmd, get_reauth_cb, data, NULL) !=
350 if (data->db_tmp_reauth.permanent == NULL)
352 return &data->db_tmp_reauth;
356 static void db_remove_reauth(struct eap_sim_db_data *data,
357 struct eap_sim_reauth *reauth)
361 if (!valid_db_string(reauth->permanent))
363 os_snprintf(cmd, sizeof(cmd),
364 "DELETE FROM reauth WHERE permanent='%s';",
366 sqlite3_exec(data->sqlite_db, cmd, NULL, NULL, NULL);
369 #endif /* CONFIG_SQLITE */
372 static struct eap_sim_db_pending *
373 eap_sim_db_get_pending(struct eap_sim_db_data *data, const char *imsi, int aka)
375 struct eap_sim_db_pending *entry, *prev = NULL;
377 entry = data->pending;
379 if (entry->aka == aka && os_strcmp(entry->imsi, imsi) == 0) {
381 prev->next = entry->next;
383 data->pending = entry->next;
393 static void eap_sim_db_add_pending(struct eap_sim_db_data *data,
394 struct eap_sim_db_pending *entry)
396 entry->next = data->pending;
397 data->pending = entry;
401 static void eap_sim_db_sim_resp_auth(struct eap_sim_db_data *data,
402 const char *imsi, char *buf)
404 char *start, *end, *pos;
405 struct eap_sim_db_pending *entry;
409 * SIM-RESP-AUTH <IMSI> Kc(i):SRES(i):RAND(i) ...
410 * SIM-RESP-AUTH <IMSI> FAILURE
411 * (IMSI = ASCII string, Kc/SRES/RAND = hex string)
414 entry = eap_sim_db_get_pending(data, imsi, 0);
416 wpa_printf(MSG_DEBUG, "EAP-SIM DB: No pending entry for the "
417 "received message found");
422 if (os_strncmp(start, "FAILURE", 7) == 0) {
423 wpa_printf(MSG_DEBUG, "EAP-SIM DB: External server reported "
425 entry->state = FAILURE;
426 eap_sim_db_add_pending(data, entry);
427 data->get_complete_cb(data->ctx, entry->cb_session_ctx);
432 while (num_chal < EAP_SIM_MAX_CHAL) {
433 end = os_strchr(start, ' ');
437 pos = os_strchr(start, ':');
441 if (hexstr2bin(start, entry->u.sim.kc[num_chal],
446 pos = os_strchr(start, ':');
450 if (hexstr2bin(start, entry->u.sim.sres[num_chal],
455 if (hexstr2bin(start, entry->u.sim.rand[num_chal],
465 entry->u.sim.num_chal = num_chal;
467 entry->state = SUCCESS;
468 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Authentication data parsed "
469 "successfully - callback");
470 eap_sim_db_add_pending(data, entry);
471 data->get_complete_cb(data->ctx, entry->cb_session_ctx);
475 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Failed to parse response string");
480 static void eap_sim_db_aka_resp_auth(struct eap_sim_db_data *data,
481 const char *imsi, char *buf)
484 struct eap_sim_db_pending *entry;
487 * AKA-RESP-AUTH <IMSI> <RAND> <AUTN> <IK> <CK> <RES>
488 * AKA-RESP-AUTH <IMSI> FAILURE
489 * (IMSI = ASCII string, RAND/AUTN/IK/CK/RES = hex string)
492 entry = eap_sim_db_get_pending(data, imsi, 1);
494 wpa_printf(MSG_DEBUG, "EAP-SIM DB: No pending entry for the "
495 "received message found");
500 if (os_strncmp(start, "FAILURE", 7) == 0) {
501 wpa_printf(MSG_DEBUG, "EAP-SIM DB: External server reported "
503 entry->state = FAILURE;
504 eap_sim_db_add_pending(data, entry);
505 data->get_complete_cb(data->ctx, entry->cb_session_ctx);
509 end = os_strchr(start, ' ');
513 if (hexstr2bin(start, entry->u.aka.rand, EAP_AKA_RAND_LEN))
517 end = os_strchr(start, ' ');
521 if (hexstr2bin(start, entry->u.aka.autn, EAP_AKA_AUTN_LEN))
525 end = os_strchr(start, ' ');
529 if (hexstr2bin(start, entry->u.aka.ik, EAP_AKA_IK_LEN))
533 end = os_strchr(start, ' ');
537 if (hexstr2bin(start, entry->u.aka.ck, EAP_AKA_CK_LEN))
541 end = os_strchr(start, ' ');
549 entry->u.aka.res_len = (end - start) / 2;
550 if (entry->u.aka.res_len > EAP_AKA_RES_MAX_LEN) {
551 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Too long RES");
552 entry->u.aka.res_len = 0;
555 if (hexstr2bin(start, entry->u.aka.res, entry->u.aka.res_len))
558 entry->state = SUCCESS;
559 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Authentication data parsed "
560 "successfully - callback");
561 eap_sim_db_add_pending(data, entry);
562 data->get_complete_cb(data->ctx, entry->cb_session_ctx);
566 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Failed to parse response string");
571 static void eap_sim_db_receive(int sock, void *eloop_ctx, void *sock_ctx)
573 struct eap_sim_db_data *data = eloop_ctx;
574 char buf[1000], *pos, *cmd, *imsi;
577 res = recv(sock, buf, sizeof(buf), 0);
580 wpa_hexdump_ascii_key(MSG_MSGDUMP, "EAP-SIM DB: Received from an "
581 "external source", (u8 *) buf, res);
584 if (res >= (int) sizeof(buf))
585 res = sizeof(buf) - 1;
588 if (data->get_complete_cb == NULL) {
589 wpa_printf(MSG_DEBUG, "EAP-SIM DB: No get_complete_cb "
594 /* <cmd> <IMSI> ... */
597 pos = os_strchr(cmd, ' ');
602 pos = os_strchr(imsi, ' ');
606 wpa_printf(MSG_DEBUG, "EAP-SIM DB: External response=%s for IMSI %s",
609 if (os_strcmp(cmd, "SIM-RESP-AUTH") == 0)
610 eap_sim_db_sim_resp_auth(data, imsi, pos + 1);
611 else if (os_strcmp(cmd, "AKA-RESP-AUTH") == 0)
612 eap_sim_db_aka_resp_auth(data, imsi, pos + 1);
614 wpa_printf(MSG_INFO, "EAP-SIM DB: Unknown external response "
619 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Failed to parse response string");
623 static int eap_sim_db_open_socket(struct eap_sim_db_data *data)
625 struct sockaddr_un addr;
626 static int counter = 0;
628 if (os_strncmp(data->fname, "unix:", 5) != 0)
631 data->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
632 if (data->sock < 0) {
633 perror("socket(eap_sim_db)");
637 os_memset(&addr, 0, sizeof(addr));
638 addr.sun_family = AF_UNIX;
639 os_snprintf(addr.sun_path, sizeof(addr.sun_path),
640 "/tmp/eap_sim_db_%d-%d", getpid(), counter++);
641 os_free(data->local_sock);
642 data->local_sock = os_strdup(addr.sun_path);
643 if (bind(data->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
644 perror("bind(eap_sim_db)");
650 os_memset(&addr, 0, sizeof(addr));
651 addr.sun_family = AF_UNIX;
652 os_strlcpy(addr.sun_path, data->fname + 5, sizeof(addr.sun_path));
653 if (connect(data->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
654 perror("connect(eap_sim_db)");
655 wpa_hexdump_ascii(MSG_INFO, "HLR/AuC GW socket",
656 (u8 *) addr.sun_path,
657 os_strlen(addr.sun_path));
663 eloop_register_read_sock(data->sock, eap_sim_db_receive, data, NULL);
669 static void eap_sim_db_close_socket(struct eap_sim_db_data *data)
671 if (data->sock >= 0) {
672 eloop_unregister_read_sock(data->sock);
676 if (data->local_sock) {
677 unlink(data->local_sock);
678 os_free(data->local_sock);
679 data->local_sock = NULL;
685 * eap_sim_db_init - Initialize EAP-SIM DB / authentication gateway interface
686 * @config: Configuration data (e.g., file name)
687 * @get_complete_cb: Callback function for reporting availability of triplets
688 * @ctx: Context pointer for get_complete_cb
689 * Returns: Pointer to a private data structure or %NULL on failure
691 struct eap_sim_db_data *
692 eap_sim_db_init(const char *config,
693 void (*get_complete_cb)(void *ctx, void *session_ctx),
696 struct eap_sim_db_data *data;
699 data = os_zalloc(sizeof(*data));
704 data->get_complete_cb = get_complete_cb;
706 data->fname = os_strdup(config);
707 if (data->fname == NULL)
709 pos = os_strstr(data->fname, " db=");
714 data->sqlite_db = db_open(pos);
715 if (data->sqlite_db == NULL)
717 #endif /* CONFIG_SQLITE */
720 if (os_strncmp(data->fname, "unix:", 5) == 0) {
721 if (eap_sim_db_open_socket(data)) {
722 wpa_printf(MSG_DEBUG, "EAP-SIM DB: External database "
723 "connection not available - will retry "
731 eap_sim_db_close_socket(data);
732 os_free(data->fname);
738 static void eap_sim_db_free_pseudonym(struct eap_sim_pseudonym *p)
740 os_free(p->permanent);
741 os_free(p->pseudonym);
746 static void eap_sim_db_free_reauth(struct eap_sim_reauth *r)
748 os_free(r->permanent);
749 os_free(r->reauth_id);
755 * eap_sim_db_deinit - Deinitialize EAP-SIM DB/authentication gw interface
756 * @priv: Private data pointer from eap_sim_db_init()
758 void eap_sim_db_deinit(void *priv)
760 struct eap_sim_db_data *data = priv;
761 struct eap_sim_pseudonym *p, *prev;
762 struct eap_sim_reauth *r, *prevr;
763 struct eap_sim_db_pending *pending, *prev_pending;
766 if (data->sqlite_db) {
767 sqlite3_close(data->sqlite_db);
768 data->sqlite_db = NULL;
770 #endif /* CONFIG_SQLITE */
772 eap_sim_db_close_socket(data);
773 os_free(data->fname);
775 p = data->pseudonyms;
779 eap_sim_db_free_pseudonym(prev);
786 eap_sim_db_free_reauth(prevr);
789 pending = data->pending;
791 prev_pending = pending;
792 pending = pending->next;
793 os_free(prev_pending);
800 static int eap_sim_db_send(struct eap_sim_db_data *data, const char *msg,
805 if (send(data->sock, msg, len, 0) < 0) {
807 perror("send[EAP-SIM DB UNIX]");
810 if (_errno == ENOTCONN || _errno == EDESTADDRREQ || _errno == EINVAL ||
811 _errno == ECONNREFUSED) {
812 /* Try to reconnect */
813 eap_sim_db_close_socket(data);
814 if (eap_sim_db_open_socket(data) < 0)
816 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Reconnected to the "
818 if (send(data->sock, msg, len, 0) < 0) {
819 perror("send[EAP-SIM DB UNIX]");
828 static void eap_sim_db_expire_pending(struct eap_sim_db_data *data)
830 /* TODO: add limit for maximum length for pending list; remove latest
831 * (i.e., last) entry from the list if the limit is reached; could also
832 * use timeout to expire pending entries */
837 * eap_sim_db_get_gsm_triplets - Get GSM triplets
838 * @data: Private data pointer from eap_sim_db_init()
839 * @username: Permanent username (prefix | IMSI)
840 * @max_chal: Maximum number of triplets
841 * @_rand: Buffer for RAND values
842 * @kc: Buffer for Kc values
843 * @sres: Buffer for SRES values
844 * @cb_session_ctx: Session callback context for get_complete_cb()
845 * Returns: Number of triplets received (has to be less than or equal to
846 * max_chal), -1 (EAP_SIM_DB_FAILURE) on error (e.g., user not found), or
847 * -2 (EAP_SIM_DB_PENDING) if results are not yet available. In this case, the
848 * callback function registered with eap_sim_db_init() will be called once the
849 * results become available.
851 * When using an external server for GSM triplets, this function can always
852 * start a request and return EAP_SIM_DB_PENDING immediately if authentication
853 * triplets are not available. Once the triplets are received, callback
854 * function registered with eap_sim_db_init() is called to notify EAP state
855 * machine to reprocess the message. This eap_sim_db_get_gsm_triplets()
856 * function will then be called again and the newly received triplets will then
857 * be given to the caller.
859 int eap_sim_db_get_gsm_triplets(struct eap_sim_db_data *data,
860 const char *username, int max_chal,
861 u8 *_rand, u8 *kc, u8 *sres,
862 void *cb_session_ctx)
864 struct eap_sim_db_pending *entry;
870 if (username == NULL || username[0] != EAP_SIM_PERMANENT_PREFIX ||
871 username[1] == '\0' || os_strlen(username) > sizeof(entry->imsi)) {
872 wpa_printf(MSG_DEBUG, "EAP-SIM DB: unexpected username '%s'",
874 return EAP_SIM_DB_FAILURE;
877 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Get GSM triplets for IMSI '%s'",
880 entry = eap_sim_db_get_pending(data, imsi, 0);
883 if (entry->state == FAILURE) {
884 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Pending entry -> "
887 return EAP_SIM_DB_FAILURE;
890 if (entry->state == PENDING) {
891 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Pending entry -> "
893 eap_sim_db_add_pending(data, entry);
894 return EAP_SIM_DB_PENDING;
897 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Pending entry -> "
898 "%d challenges", entry->u.sim.num_chal);
899 num_chal = entry->u.sim.num_chal;
900 if (num_chal > max_chal)
902 os_memcpy(_rand, entry->u.sim.rand, num_chal * GSM_RAND_LEN);
903 os_memcpy(sres, entry->u.sim.sres,
904 num_chal * EAP_SIM_SRES_LEN);
905 os_memcpy(kc, entry->u.sim.kc, num_chal * EAP_SIM_KC_LEN);
910 if (data->sock < 0) {
911 if (eap_sim_db_open_socket(data) < 0)
912 return EAP_SIM_DB_FAILURE;
915 imsi_len = os_strlen(imsi);
916 len = os_snprintf(msg, sizeof(msg), "SIM-REQ-AUTH ");
917 if (len < 0 || len + imsi_len >= sizeof(msg))
918 return EAP_SIM_DB_FAILURE;
919 os_memcpy(msg + len, imsi, imsi_len);
921 ret = os_snprintf(msg + len, sizeof(msg) - len, " %d", max_chal);
922 if (ret < 0 || (size_t) ret >= sizeof(msg) - len)
923 return EAP_SIM_DB_FAILURE;
926 wpa_printf(MSG_DEBUG, "EAP-SIM DB: requesting SIM authentication "
927 "data for IMSI '%s'", imsi);
928 if (eap_sim_db_send(data, msg, len) < 0)
929 return EAP_SIM_DB_FAILURE;
931 entry = os_zalloc(sizeof(*entry));
933 return EAP_SIM_DB_FAILURE;
935 os_get_time(&entry->timestamp);
936 os_strlcpy(entry->imsi, imsi, sizeof(entry->imsi));
937 entry->cb_session_ctx = cb_session_ctx;
938 entry->state = PENDING;
939 eap_sim_db_add_pending(data, entry);
940 eap_sim_db_expire_pending(data);
942 return EAP_SIM_DB_PENDING;
946 static char * eap_sim_db_get_next(struct eap_sim_db_data *data, char prefix)
948 char *id, *pos, *end;
951 if (random_get_bytes(buf, sizeof(buf)))
953 id = os_malloc(sizeof(buf) * 2 + 2);
958 end = id + sizeof(buf) * 2 + 2;
960 pos += wpa_snprintf_hex(pos, end - pos, buf, sizeof(buf));
967 * eap_sim_db_get_next_pseudonym - EAP-SIM DB: Get next pseudonym
968 * @data: Private data pointer from eap_sim_db_init()
969 * @method: EAP method (SIM/AKA/AKA')
970 * Returns: Next pseudonym (allocated string) or %NULL on failure
972 * This function is used to generate a pseudonym for EAP-SIM. The returned
973 * pseudonym is not added to database at this point; it will need to be added
974 * with eap_sim_db_add_pseudonym() once the authentication has been completed
975 * successfully. Caller is responsible for freeing the returned buffer.
977 char * eap_sim_db_get_next_pseudonym(struct eap_sim_db_data *data,
978 enum eap_sim_db_method method)
980 char prefix = EAP_SIM_REAUTH_ID_PREFIX;
984 prefix = EAP_SIM_PSEUDONYM_PREFIX;
987 prefix = EAP_AKA_PSEUDONYM_PREFIX;
989 case EAP_SIM_DB_AKA_PRIME:
990 prefix = EAP_AKA_PRIME_PSEUDONYM_PREFIX;
994 return eap_sim_db_get_next(data, prefix);
999 * eap_sim_db_get_next_reauth_id - EAP-SIM DB: Get next reauth_id
1000 * @data: Private data pointer from eap_sim_db_init()
1001 * @method: EAP method (SIM/AKA/AKA')
1002 * Returns: Next reauth_id (allocated string) or %NULL on failure
1004 * This function is used to generate a fast re-authentication identity for
1005 * EAP-SIM. The returned reauth_id is not added to database at this point; it
1006 * will need to be added with eap_sim_db_add_reauth() once the authentication
1007 * has been completed successfully. Caller is responsible for freeing the
1010 char * eap_sim_db_get_next_reauth_id(struct eap_sim_db_data *data,
1011 enum eap_sim_db_method method)
1013 char prefix = EAP_SIM_REAUTH_ID_PREFIX;
1016 case EAP_SIM_DB_SIM:
1017 prefix = EAP_SIM_REAUTH_ID_PREFIX;
1019 case EAP_SIM_DB_AKA:
1020 prefix = EAP_AKA_REAUTH_ID_PREFIX;
1022 case EAP_SIM_DB_AKA_PRIME:
1023 prefix = EAP_AKA_PRIME_REAUTH_ID_PREFIX;
1027 return eap_sim_db_get_next(data, prefix);
1032 * eap_sim_db_add_pseudonym - EAP-SIM DB: Add new pseudonym
1033 * @data: Private data pointer from eap_sim_db_init()
1034 * @permanent: Permanent username
1035 * @pseudonym: Pseudonym for this user. This needs to be an allocated buffer,
1036 * e.g., return value from eap_sim_db_get_next_pseudonym(). Caller must not
1038 * Returns: 0 on success, -1 on failure
1040 * This function adds a new pseudonym for EAP-SIM user. EAP-SIM DB is
1041 * responsible of freeing pseudonym buffer once it is not needed anymore.
1043 int eap_sim_db_add_pseudonym(struct eap_sim_db_data *data,
1044 const char *permanent, char *pseudonym)
1046 struct eap_sim_pseudonym *p;
1047 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Add pseudonym '%s' for permanent "
1048 "username '%s'", pseudonym, permanent);
1050 /* TODO: could store last two pseudonyms */
1051 #ifdef CONFIG_SQLITE
1052 if (data->sqlite_db)
1053 return db_add_pseudonym(data, permanent, pseudonym);
1054 #endif /* CONFIG_SQLITE */
1055 for (p = data->pseudonyms; p; p = p->next) {
1056 if (os_strcmp(permanent, p->permanent) == 0)
1060 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Replacing previous "
1061 "pseudonym: %s", p->pseudonym);
1062 os_free(p->pseudonym);
1063 p->pseudonym = pseudonym;
1067 p = os_zalloc(sizeof(*p));
1073 p->next = data->pseudonyms;
1074 p->permanent = os_strdup(permanent);
1075 if (p->permanent == NULL) {
1080 p->pseudonym = pseudonym;
1081 data->pseudonyms = p;
1083 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Added new pseudonym entry");
1088 static struct eap_sim_reauth *
1089 eap_sim_db_add_reauth_data(struct eap_sim_db_data *data,
1090 const char *permanent,
1091 char *reauth_id, u16 counter)
1093 struct eap_sim_reauth *r;
1095 for (r = data->reauths; r; r = r->next) {
1096 if (os_strcmp(r->permanent, permanent) == 0)
1101 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Replacing previous "
1102 "reauth_id: %s", r->reauth_id);
1103 os_free(r->reauth_id);
1104 r->reauth_id = reauth_id;
1106 r = os_zalloc(sizeof(*r));
1112 r->next = data->reauths;
1113 r->permanent = os_strdup(permanent);
1114 if (r->permanent == NULL) {
1119 r->reauth_id = reauth_id;
1121 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Added new reauth entry");
1124 r->counter = counter;
1131 * eap_sim_db_add_reauth - EAP-SIM DB: Add new re-authentication entry
1132 * @priv: Private data pointer from eap_sim_db_init()
1133 * @permanent: Permanent username
1134 * @identity_len: Length of identity
1135 * @reauth_id: reauth_id for this user. This needs to be an allocated buffer,
1136 * e.g., return value from eap_sim_db_get_next_reauth_id(). Caller must not
1138 * @counter: AT_COUNTER value for fast re-authentication
1139 * @mk: 16-byte MK from the previous full authentication or %NULL
1140 * Returns: 0 on success, -1 on failure
1142 * This function adds a new re-authentication entry for an EAP-SIM user.
1143 * EAP-SIM DB is responsible of freeing reauth_id buffer once it is not needed
1146 int eap_sim_db_add_reauth(struct eap_sim_db_data *data, const char *permanent,
1147 char *reauth_id, u16 counter, const u8 *mk)
1149 struct eap_sim_reauth *r;
1151 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Add reauth_id '%s' for permanent "
1152 "identity '%s'", reauth_id, permanent);
1154 #ifdef CONFIG_SQLITE
1155 if (data->sqlite_db)
1156 return db_add_reauth(data, permanent, reauth_id, counter, mk,
1158 #endif /* CONFIG_SQLITE */
1159 r = eap_sim_db_add_reauth_data(data, permanent, reauth_id, counter);
1163 os_memcpy(r->mk, mk, EAP_SIM_MK_LEN);
1169 #ifdef EAP_SERVER_AKA_PRIME
1171 * eap_sim_db_add_reauth_prime - EAP-AKA' DB: Add new re-authentication entry
1172 * @data: Private data pointer from eap_sim_db_init()
1173 * @permanent: Permanent username
1174 * @reauth_id: reauth_id for this user. This needs to be an allocated buffer,
1175 * e.g., return value from eap_sim_db_get_next_reauth_id(). Caller must not
1177 * @counter: AT_COUNTER value for fast re-authentication
1178 * @k_encr: K_encr from the previous full authentication
1179 * @k_aut: K_aut from the previous full authentication
1180 * @k_re: 32-byte K_re from the previous full authentication
1181 * Returns: 0 on success, -1 on failure
1183 * This function adds a new re-authentication entry for an EAP-AKA' user.
1184 * EAP-SIM DB is responsible of freeing reauth_id buffer once it is not needed
1187 int eap_sim_db_add_reauth_prime(struct eap_sim_db_data *data,
1188 const char *permanent, char *reauth_id,
1189 u16 counter, const u8 *k_encr,
1190 const u8 *k_aut, const u8 *k_re)
1192 struct eap_sim_reauth *r;
1194 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Add reauth_id '%s' for permanent "
1195 "identity '%s'", reauth_id, permanent);
1197 #ifdef CONFIG_SQLITE
1198 if (data->sqlite_db)
1199 return db_add_reauth(data, permanent, reauth_id, counter, NULL,
1200 k_encr, k_aut, k_re);
1201 #endif /* CONFIG_SQLITE */
1202 r = eap_sim_db_add_reauth_data(data, permanent, reauth_id, counter);
1206 os_memcpy(r->k_encr, k_encr, EAP_SIM_K_ENCR_LEN);
1207 os_memcpy(r->k_aut, k_aut, EAP_AKA_PRIME_K_AUT_LEN);
1208 os_memcpy(r->k_re, k_re, EAP_AKA_PRIME_K_RE_LEN);
1212 #endif /* EAP_SERVER_AKA_PRIME */
1216 * eap_sim_db_get_permanent - EAP-SIM DB: Get permanent identity
1217 * @data: Private data pointer from eap_sim_db_init()
1218 * @pseudonym: Pseudonym username
1219 * Returns: Pointer to permanent username or %NULL if not found
1222 eap_sim_db_get_permanent(struct eap_sim_db_data *data, const char *pseudonym)
1224 struct eap_sim_pseudonym *p;
1226 #ifdef CONFIG_SQLITE
1227 if (data->sqlite_db)
1228 return db_get_pseudonym(data, pseudonym);
1229 #endif /* CONFIG_SQLITE */
1231 p = data->pseudonyms;
1233 if (os_strcmp(p->pseudonym, pseudonym) == 0)
1234 return p->permanent;
1243 * eap_sim_db_get_reauth_entry - EAP-SIM DB: Get re-authentication entry
1244 * @data: Private data pointer from eap_sim_db_init()
1245 * @reauth_id: Fast re-authentication username
1246 * Returns: Pointer to the re-auth entry, or %NULL if not found
1248 struct eap_sim_reauth *
1249 eap_sim_db_get_reauth_entry(struct eap_sim_db_data *data,
1250 const char *reauth_id)
1252 struct eap_sim_reauth *r;
1254 #ifdef CONFIG_SQLITE
1255 if (data->sqlite_db)
1256 return db_get_reauth(data, reauth_id);
1257 #endif /* CONFIG_SQLITE */
1261 if (os_strcmp(r->reauth_id, reauth_id) == 0)
1271 * eap_sim_db_remove_reauth - EAP-SIM DB: Remove re-authentication entry
1272 * @data: Private data pointer from eap_sim_db_init()
1273 * @reauth: Pointer to re-authentication entry from
1274 * eap_sim_db_get_reauth_entry()
1276 void eap_sim_db_remove_reauth(struct eap_sim_db_data *data,
1277 struct eap_sim_reauth *reauth)
1279 struct eap_sim_reauth *r, *prev = NULL;
1280 #ifdef CONFIG_SQLITE
1281 if (data->sqlite_db) {
1282 db_remove_reauth(data, reauth);
1285 #endif /* CONFIG_SQLITE */
1290 prev->next = r->next;
1292 data->reauths = r->next;
1293 eap_sim_db_free_reauth(r);
1303 * eap_sim_db_get_aka_auth - Get AKA authentication values
1304 * @data: Private data pointer from eap_sim_db_init()
1305 * @username: Permanent username (prefix | IMSI)
1306 * @_rand: Buffer for RAND value
1307 * @autn: Buffer for AUTN value
1308 * @ik: Buffer for IK value
1309 * @ck: Buffer for CK value
1310 * @res: Buffer for RES value
1311 * @res_len: Buffer for RES length
1312 * @cb_session_ctx: Session callback context for get_complete_cb()
1313 * Returns: 0 on success, -1 (EAP_SIM_DB_FAILURE) on error (e.g., user not
1314 * found), or -2 (EAP_SIM_DB_PENDING) if results are not yet available. In this
1315 * case, the callback function registered with eap_sim_db_init() will be
1316 * called once the results become available.
1318 * When using an external server for AKA authentication, this function can
1319 * always start a request and return EAP_SIM_DB_PENDING immediately if
1320 * authentication triplets are not available. Once the authentication data are
1321 * received, callback function registered with eap_sim_db_init() is called to
1322 * notify EAP state machine to reprocess the message. This
1323 * eap_sim_db_get_aka_auth() function will then be called again and the newly
1324 * received triplets will then be given to the caller.
1326 int eap_sim_db_get_aka_auth(struct eap_sim_db_data *data, const char *username,
1327 u8 *_rand, u8 *autn, u8 *ik, u8 *ck,
1328 u8 *res, size_t *res_len, void *cb_session_ctx)
1330 struct eap_sim_db_pending *entry;
1336 if (username == NULL ||
1337 (username[0] != EAP_AKA_PERMANENT_PREFIX &&
1338 username[0] != EAP_AKA_PRIME_PERMANENT_PREFIX) ||
1339 username[1] == '\0' || os_strlen(username) > sizeof(entry->imsi)) {
1340 wpa_printf(MSG_DEBUG, "EAP-SIM DB: unexpected username '%s'",
1342 return EAP_SIM_DB_FAILURE;
1344 imsi = username + 1;
1345 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Get AKA auth for IMSI '%s'",
1348 entry = eap_sim_db_get_pending(data, imsi, 1);
1350 if (entry->state == FAILURE) {
1352 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Failure");
1353 return EAP_SIM_DB_FAILURE;
1356 if (entry->state == PENDING) {
1357 eap_sim_db_add_pending(data, entry);
1358 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Pending");
1359 return EAP_SIM_DB_PENDING;
1362 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Returning successfully "
1363 "received authentication data");
1364 os_memcpy(_rand, entry->u.aka.rand, EAP_AKA_RAND_LEN);
1365 os_memcpy(autn, entry->u.aka.autn, EAP_AKA_AUTN_LEN);
1366 os_memcpy(ik, entry->u.aka.ik, EAP_AKA_IK_LEN);
1367 os_memcpy(ck, entry->u.aka.ck, EAP_AKA_CK_LEN);
1368 os_memcpy(res, entry->u.aka.res, EAP_AKA_RES_MAX_LEN);
1369 *res_len = entry->u.aka.res_len;
1374 if (data->sock < 0) {
1375 if (eap_sim_db_open_socket(data) < 0)
1376 return EAP_SIM_DB_FAILURE;
1379 imsi_len = os_strlen(imsi);
1380 len = os_snprintf(msg, sizeof(msg), "AKA-REQ-AUTH ");
1381 if (len < 0 || len + imsi_len >= sizeof(msg))
1382 return EAP_SIM_DB_FAILURE;
1383 os_memcpy(msg + len, imsi, imsi_len);
1386 wpa_printf(MSG_DEBUG, "EAP-SIM DB: requesting AKA authentication "
1387 "data for IMSI '%s'", imsi);
1388 if (eap_sim_db_send(data, msg, len) < 0)
1389 return EAP_SIM_DB_FAILURE;
1391 entry = os_zalloc(sizeof(*entry));
1393 return EAP_SIM_DB_FAILURE;
1395 os_get_time(&entry->timestamp);
1397 os_strlcpy(entry->imsi, imsi, sizeof(entry->imsi));
1398 entry->cb_session_ctx = cb_session_ctx;
1399 entry->state = PENDING;
1400 eap_sim_db_add_pending(data, entry);
1401 eap_sim_db_expire_pending(data);
1403 return EAP_SIM_DB_PENDING;
1408 * eap_sim_db_resynchronize - Resynchronize AKA AUTN
1409 * @data: Private data pointer from eap_sim_db_init()
1410 * @username: Permanent username
1411 * @auts: AUTS value from the peer
1412 * @_rand: RAND value used in the rejected message
1413 * Returns: 0 on success, -1 on failure
1415 * This function is called when the peer reports synchronization failure in the
1416 * AUTN value by sending AUTS. The AUTS and RAND values should be sent to
1417 * HLR/AuC to allow it to resynchronize with the peer. After this,
1418 * eap_sim_db_get_aka_auth() will be called again to to fetch updated
1419 * RAND/AUTN values for the next challenge.
1421 int eap_sim_db_resynchronize(struct eap_sim_db_data *data,
1422 const char *username,
1423 const u8 *auts, const u8 *_rand)
1428 if (username == NULL ||
1429 (username[0] != EAP_AKA_PERMANENT_PREFIX &&
1430 username[0] != EAP_AKA_PRIME_PERMANENT_PREFIX) ||
1431 username[1] == '\0' || os_strlen(username) > 20) {
1432 wpa_printf(MSG_DEBUG, "EAP-SIM DB: unexpected username '%s'",
1436 imsi = username + 1;
1437 wpa_printf(MSG_DEBUG, "EAP-SIM DB: Get AKA auth for IMSI '%s'",
1440 if (data->sock >= 0) {
1444 imsi_len = os_strlen(imsi);
1445 len = os_snprintf(msg, sizeof(msg), "AKA-AUTS ");
1446 if (len < 0 || len + imsi_len >= sizeof(msg))
1448 os_memcpy(msg + len, imsi, imsi_len);
1451 ret = os_snprintf(msg + len, sizeof(msg) - len, " ");
1452 if (ret < 0 || (size_t) ret >= sizeof(msg) - len)
1455 len += wpa_snprintf_hex(msg + len, sizeof(msg) - len,
1456 auts, EAP_AKA_AUTS_LEN);
1457 ret = os_snprintf(msg + len, sizeof(msg) - len, " ");
1458 if (ret < 0 || (size_t) ret >= sizeof(msg) - len)
1461 len += wpa_snprintf_hex(msg + len, sizeof(msg) - len,
1462 _rand, EAP_AKA_RAND_LEN);
1463 wpa_printf(MSG_DEBUG, "EAP-SIM DB: reporting AKA AUTS for "
1465 if (eap_sim_db_send(data, msg, len) < 0)
1474 * sim_get_username - Extract username from SIM identity
1475 * @identity: Identity
1476 * @identity_len: Identity length
1477 * Returns: Allocated buffer with the username part of the identity
1479 * Caller is responsible for freeing the returned buffer with os_free().
1481 char * sim_get_username(const u8 *identity, size_t identity_len)
1486 if (identity == NULL)
1489 for (pos = 0; pos < identity_len; pos++) {
1490 if (identity[pos] == '@' || identity[pos] == '\0')
1494 username = os_malloc(pos + 1);
1495 if (username == NULL)
1497 os_memcpy(username, identity, pos);
1498 username[pos] = '\0';