2 * HLR/AuC testing gateway for hostapd EAP-SIM/AKA database/authenticator
3 * Copyright (c) 2005-2007, 2012-2013, 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 to HLR/AuC. It is expected to be replaced with an
10 * implementation of SS7 gateway to GSM/UMTS authentication center (HLR/AuC) or
11 * a local implementation of SIM triplet and AKA authentication data generator.
13 * hostapd will send SIM/AKA authentication queries over a UNIX domain socket
14 * to and external program, e.g., this hlr_auc_gw. This interface uses simple
17 * EAP-SIM / GSM triplet query/response:
18 * SIM-REQ-AUTH <IMSI> <max_chal>
19 * SIM-RESP-AUTH <IMSI> Kc1:SRES1:RAND1 Kc2:SRES2:RAND2 [Kc3:SRES3:RAND3]
20 * SIM-RESP-AUTH <IMSI> FAILURE
22 * EAP-AKA / UMTS query/response:
24 * AKA-RESP-AUTH <IMSI> <RAND> <AUTN> <IK> <CK> <RES>
25 * AKA-RESP-AUTH <IMSI> FAILURE
27 * EAP-AKA / UMTS AUTS (re-synchronization):
28 * AKA-AUTS <IMSI> <AUTS> <RAND>
30 * IMSI and max_chal are sent as an ASCII string,
31 * Kc/SRES/RAND/AUTN/IK/CK/RES/AUTS as hex strings.
33 * An example implementation here reads GSM authentication triplets from a
34 * text file in IMSI:Kc:SRES:RAND format, IMSI in ASCII, other fields as hex
35 * strings. This is used to simulate an HLR/AuC. As such, it is not very useful
36 * for real life authentication, but it is useful both as an example
37 * implementation and for EAP-SIM/AKA/AKA' testing.
39 * For a stronger example design, Milenage and GSM-Milenage algorithms can be
40 * used to dynamically generate authenticatipn information for EAP-AKA/AKA' and
41 * EAP-SIM, respectively, if Ki is known.
43 * SQN generation follows the not time-based Profile 2 described in
44 * 3GPP TS 33.102 Annex C.3.2. The length of IND is 5 bits by default, but this
45 * can be changed with a command line options if needed.
52 #endif /* CONFIG_SQLITE */
55 #include "crypto/milenage.h"
56 #include "crypto/random.h"
58 static const char *default_socket_path = "/tmp/hlr_auc_gw.sock";
59 static const char *socket_path;
60 static int serv_sock = -1;
61 static char *milenage_file = NULL;
62 static int update_milenage = 0;
63 static int sqn_changes = 0;
64 static int ind_len = 5;
68 struct gsm_triplet *next;
75 static struct gsm_triplet *gsm_db = NULL, *gsm_db_pos = NULL;
77 /* OPc and AMF parameters for Milenage (Example algorithms for AKA). */
78 struct milenage_parameters {
79 struct milenage_parameters *next;
88 static struct milenage_parameters *milenage_db = NULL;
90 #define EAP_SIM_MAX_CHAL 3
92 #define EAP_AKA_RAND_LEN 16
93 #define EAP_AKA_AUTN_LEN 16
94 #define EAP_AKA_AUTS_LEN 14
95 #define EAP_AKA_RES_MAX_LEN 16
96 #define EAP_AKA_IK_LEN 16
97 #define EAP_AKA_CK_LEN 16
102 static sqlite3 *sqlite_db = NULL;
103 static struct milenage_parameters db_tmp_milenage;
106 static int db_table_exists(sqlite3 *db, const char *name)
109 os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
110 return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
114 static int db_table_create_milenage(sqlite3 *db)
118 "CREATE TABLE milenage("
119 " imsi INTEGER PRIMARY KEY NOT NULL,"
120 " ki CHAR(32) NOT NULL,"
121 " opc CHAR(32) NOT NULL,"
122 " amf CHAR(4) NOT NULL,"
123 " sqn CHAR(12) NOT NULL"
126 printf("Adding database table for milenage information\n");
127 if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
128 printf("SQLite error: %s\n", err);
137 static sqlite3 * db_open(const char *db_file)
141 if (sqlite3_open(db_file, &db)) {
142 printf("Failed to open database %s: %s\n",
143 db_file, sqlite3_errmsg(db));
148 if (!db_table_exists(db, "milenage") &&
149 db_table_create_milenage(db) < 0) {
158 static int get_milenage_cb(void *ctx, int argc, char *argv[], char *col[])
160 struct milenage_parameters *m = ctx;
165 for (i = 0; i < argc; i++) {
166 if (os_strcmp(col[i], "ki") == 0 && argv[i] &&
167 hexstr2bin(argv[i], m->ki, sizeof(m->ki))) {
168 printf("Invalid ki value in database\n");
172 if (os_strcmp(col[i], "opc") == 0 && argv[i] &&
173 hexstr2bin(argv[i], m->opc, sizeof(m->opc))) {
174 printf("Invalid opcvalue in database\n");
178 if (os_strcmp(col[i], "amf") == 0 && argv[i] &&
179 hexstr2bin(argv[i], m->amf, sizeof(m->amf))) {
180 printf("Invalid amf value in database\n");
184 if (os_strcmp(col[i], "sqn") == 0 && argv[i] &&
185 hexstr2bin(argv[i], m->sqn, sizeof(m->sqn))) {
186 printf("Invalid sqn value in database\n");
195 static struct milenage_parameters * db_get_milenage(const char *imsi_txt)
198 unsigned long long imsi;
200 os_memset(&db_tmp_milenage, 0, sizeof(db_tmp_milenage));
201 imsi = atoll(imsi_txt);
202 os_snprintf(db_tmp_milenage.imsi, sizeof(db_tmp_milenage.imsi),
204 os_snprintf(cmd, sizeof(cmd),
205 "SELECT ki,opc,amf,sqn FROM milenage WHERE imsi=%llu;",
207 if (sqlite3_exec(sqlite_db, cmd, get_milenage_cb, &db_tmp_milenage,
211 if (!db_tmp_milenage.set)
213 return &db_tmp_milenage;
217 static int db_update_milenage_sqn(struct milenage_parameters *m)
219 char cmd[128], val[13], *pos;
222 pos += wpa_snprintf_hex(pos, sizeof(val), m->sqn, 6);
224 os_snprintf(cmd, sizeof(cmd),
225 "UPDATE milenage SET sqn='%s' WHERE imsi=%s;",
227 if (sqlite3_exec(sqlite_db, cmd, NULL, NULL, NULL) != SQLITE_OK) {
228 printf("Failed to update SQN in database for IMSI %s\n",
235 #endif /* CONFIG_SQLITE */
238 static int open_socket(const char *path)
240 struct sockaddr_un addr;
243 s = socket(PF_UNIX, SOCK_DGRAM, 0);
245 perror("socket(PF_UNIX)");
249 memset(&addr, 0, sizeof(addr));
250 addr.sun_family = AF_UNIX;
251 os_strlcpy(addr.sun_path, path, sizeof(addr.sun_path));
252 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
253 perror("hlr-auc-gw: bind(PF_UNIX)");
262 static int read_gsm_triplets(const char *fname)
265 char buf[200], *pos, *pos2;
266 struct gsm_triplet *g = NULL;
272 f = fopen(fname, "r");
274 printf("Could not open GSM tripler data file '%s'\n", fname);
279 while (fgets(buf, sizeof(buf), f)) {
282 /* Parse IMSI:Kc:SRES:RAND */
283 buf[sizeof(buf) - 1] = '\0';
287 while (*pos != '\0' && *pos != '\n')
295 g = os_zalloc(sizeof(*g));
302 pos2 = strchr(pos, ':');
304 printf("%s:%d - Invalid IMSI (%s)\n",
310 if (strlen(pos) >= sizeof(g->imsi)) {
311 printf("%s:%d - Too long IMSI (%s)\n",
316 os_strlcpy(g->imsi, pos, sizeof(g->imsi));
320 pos2 = strchr(pos, ':');
322 printf("%s:%d - Invalid Kc (%s)\n", fname, line, pos);
327 if (strlen(pos) != 16 || hexstr2bin(pos, g->kc, 8)) {
328 printf("%s:%d - Invalid Kc (%s)\n", fname, line, pos);
335 pos2 = strchr(pos, ':');
337 printf("%s:%d - Invalid SRES (%s)\n", fname, line,
343 if (strlen(pos) != 8 || hexstr2bin(pos, g->sres, 4)) {
344 printf("%s:%d - Invalid SRES (%s)\n", fname, line,
352 pos2 = strchr(pos, ':');
355 if (strlen(pos) != 32 || hexstr2bin(pos, g->_rand, 16)) {
356 printf("%s:%d - Invalid RAND (%s)\n", fname, line,
375 static struct gsm_triplet * get_gsm_triplet(const char *imsi)
377 struct gsm_triplet *g = gsm_db_pos;
380 if (strcmp(g->imsi, imsi) == 0) {
381 gsm_db_pos = g->next;
388 while (g && g != gsm_db_pos) {
389 if (strcmp(g->imsi, imsi) == 0) {
390 gsm_db_pos = g->next;
400 static int read_milenage(const char *fname)
403 char buf[200], *pos, *pos2;
404 struct milenage_parameters *m = NULL;
410 f = fopen(fname, "r");
412 printf("Could not open Milenage data file '%s'\n", fname);
417 while (fgets(buf, sizeof(buf), f)) {
420 /* Parse IMSI Ki OPc AMF SQN */
421 buf[sizeof(buf) - 1] = '\0';
425 while (*pos != '\0' && *pos != '\n')
433 m = os_zalloc(sizeof(*m));
440 pos2 = strchr(pos, ' ');
442 printf("%s:%d - Invalid IMSI (%s)\n",
448 if (strlen(pos) >= sizeof(m->imsi)) {
449 printf("%s:%d - Too long IMSI (%s)\n",
454 os_strlcpy(m->imsi, pos, sizeof(m->imsi));
458 pos2 = strchr(pos, ' ');
460 printf("%s:%d - Invalid Ki (%s)\n", fname, line, pos);
465 if (strlen(pos) != 32 || hexstr2bin(pos, m->ki, 16)) {
466 printf("%s:%d - Invalid Ki (%s)\n", fname, line, pos);
473 pos2 = strchr(pos, ' ');
475 printf("%s:%d - Invalid OPc (%s)\n", fname, line, pos);
480 if (strlen(pos) != 32 || hexstr2bin(pos, m->opc, 16)) {
481 printf("%s:%d - Invalid OPc (%s)\n", fname, line, pos);
488 pos2 = strchr(pos, ' ');
490 printf("%s:%d - Invalid AMF (%s)\n", fname, line, pos);
495 if (strlen(pos) != 4 || hexstr2bin(pos, m->amf, 2)) {
496 printf("%s:%d - Invalid AMF (%s)\n", fname, line, pos);
503 pos2 = strchr(pos, ' ');
506 if (strlen(pos) != 12 || hexstr2bin(pos, m->sqn, 6)) {
507 printf("%s:%d - Invalid SEQ (%s)\n", fname, line, pos);
513 m->next = milenage_db;
525 static void update_milenage_file(const char *fname)
529 char *end = buf + sizeof(buf);
530 struct milenage_parameters *m;
533 f = fopen(fname, "r");
535 printf("Could not open Milenage data file '%s'\n", fname);
539 snprintf(buf, sizeof(buf), "%s.new", fname);
540 f2 = fopen(buf, "w");
542 printf("Could not write Milenage data file '%s'\n", buf);
547 while (fgets(buf, sizeof(buf), f)) {
548 /* IMSI Ki OPc AMF SQN */
549 buf[sizeof(buf) - 1] = '\0';
551 pos = strchr(buf, ' ');
552 if (buf[0] == '#' || pos == NULL || pos - buf >= 20)
555 imsi_len = pos - buf;
557 for (m = milenage_db; m; m = m->next) {
558 if (strncmp(buf, m->imsi, imsi_len) == 0 &&
559 m->imsi[imsi_len] == '\0')
567 pos += snprintf(pos, end - pos, "%s ", m->imsi);
568 pos += wpa_snprintf_hex(pos, end - pos, m->ki, 16);
570 pos += wpa_snprintf_hex(pos, end - pos, m->opc, 16);
572 pos += wpa_snprintf_hex(pos, end - pos, m->amf, 2);
574 pos += wpa_snprintf_hex(pos, end - pos, m->sqn, 6);
578 fprintf(f2, "%s", buf);
584 snprintf(buf, sizeof(buf), "%s.bak", fname);
585 if (rename(fname, buf) < 0) {
590 snprintf(buf, sizeof(buf), "%s.new", fname);
591 if (rename(buf, fname) < 0) {
599 static struct milenage_parameters * get_milenage(const char *imsi)
601 struct milenage_parameters *m = milenage_db;
604 if (strcmp(m->imsi, imsi) == 0)
611 m = db_get_milenage(imsi);
612 #endif /* CONFIG_SQLITE */
618 static void sim_req_auth(int s, struct sockaddr_un *from, socklen_t fromlen,
621 int count, max_chal, ret;
623 char reply[1000], *rpos, *rend;
624 struct milenage_parameters *m;
625 struct gsm_triplet *g;
629 pos = strchr(imsi, ' ');
632 max_chal = atoi(pos);
633 if (max_chal < 1 || max_chal < EAP_SIM_MAX_CHAL)
634 max_chal = EAP_SIM_MAX_CHAL;
636 max_chal = EAP_SIM_MAX_CHAL;
638 rend = &reply[sizeof(reply)];
640 ret = snprintf(rpos, rend - rpos, "SIM-RESP-AUTH %s", imsi);
641 if (ret < 0 || ret >= rend - rpos)
645 m = get_milenage(imsi);
647 u8 _rand[16], sres[4], kc[8];
648 for (count = 0; count < max_chal; count++) {
649 if (random_get_bytes(_rand, 16) < 0)
651 gsm_milenage(m->opc, m->ki, _rand, sres, kc);
653 rpos += wpa_snprintf_hex(rpos, rend - rpos, kc, 8);
655 rpos += wpa_snprintf_hex(rpos, rend - rpos, sres, 4);
657 rpos += wpa_snprintf_hex(rpos, rend - rpos, _rand, 16);
664 while (count < max_chal && (g = get_gsm_triplet(imsi))) {
665 if (strcmp(g->imsi, imsi) != 0)
670 rpos += wpa_snprintf_hex(rpos, rend - rpos, g->kc, 8);
673 rpos += wpa_snprintf_hex(rpos, rend - rpos, g->sres, 4);
676 rpos += wpa_snprintf_hex(rpos, rend - rpos, g->_rand, 16);
681 printf("No GSM triplets found for %s\n", imsi);
682 ret = snprintf(rpos, rend - rpos, " FAILURE");
683 if (ret < 0 || ret >= rend - rpos)
689 printf("Send: %s\n", reply);
690 if (sendto(s, reply, rpos - reply, 0,
691 (struct sockaddr *) from, fromlen) < 0)
696 static void inc_sqn(u8 *sqn)
701 * SQN = SEQ | IND = SEQ1 | SEQ2 | IND
703 * The mechanism used here is not time-based, so SEQ2 is void and
704 * SQN = SEQ1 | IND. The length of IND is ind_len bits and the length
705 * of SEQ1 is 48 - ind_len bits.
708 /* Increment both SEQ and IND by one */
709 val = ((u64) WPA_GET_BE32(sqn) << 16) | ((u64) WPA_GET_BE16(sqn + 4));
710 seq = (val >> ind_len) + 1;
711 ind = (val + 1) & ((1 << ind_len) - 1);
712 val = (seq << ind_len) | ind;
713 WPA_PUT_BE32(sqn, val >> 16);
714 WPA_PUT_BE16(sqn + 4, val & 0xffff);
718 static void aka_req_auth(int s, struct sockaddr_un *from, socklen_t fromlen,
721 /* AKA-RESP-AUTH <IMSI> <RAND> <AUTN> <IK> <CK> <RES> */
722 char reply[1000], *pos, *end;
723 u8 _rand[EAP_AKA_RAND_LEN];
724 u8 autn[EAP_AKA_AUTN_LEN];
725 u8 ik[EAP_AKA_IK_LEN];
726 u8 ck[EAP_AKA_CK_LEN];
727 u8 res[EAP_AKA_RES_MAX_LEN];
730 struct milenage_parameters *m;
733 m = get_milenage(imsi);
735 if (random_get_bytes(_rand, EAP_AKA_RAND_LEN) < 0)
737 res_len = EAP_AKA_RES_MAX_LEN;
740 db_update_milenage_sqn(m);
741 #endif /* CONFIG_SQLITE */
743 printf("AKA: Milenage with SQN=%02x%02x%02x%02x%02x%02x\n",
744 m->sqn[0], m->sqn[1], m->sqn[2],
745 m->sqn[3], m->sqn[4], m->sqn[5]);
746 milenage_generate(m->opc, m->amf, m->ki, m->sqn, _rand,
747 autn, ik, ck, res, &res_len);
749 printf("Unknown IMSI: %s\n", imsi);
750 #ifdef AKA_USE_FIXED_TEST_VALUES
751 printf("Using fixed test values for AKA\n");
752 memset(_rand, '0', EAP_AKA_RAND_LEN);
753 memset(autn, '1', EAP_AKA_AUTN_LEN);
754 memset(ik, '3', EAP_AKA_IK_LEN);
755 memset(ck, '4', EAP_AKA_CK_LEN);
756 memset(res, '2', EAP_AKA_RES_MAX_LEN);
757 res_len = EAP_AKA_RES_MAX_LEN;
758 #else /* AKA_USE_FIXED_TEST_VALUES */
760 #endif /* AKA_USE_FIXED_TEST_VALUES */
764 end = &reply[sizeof(reply)];
765 ret = snprintf(pos, end - pos, "AKA-RESP-AUTH %s ", imsi);
766 if (ret < 0 || ret >= end - pos)
770 ret = snprintf(pos, end - pos, "FAILURE");
771 if (ret < 0 || ret >= end - pos)
776 pos += wpa_snprintf_hex(pos, end - pos, _rand, EAP_AKA_RAND_LEN);
778 pos += wpa_snprintf_hex(pos, end - pos, autn, EAP_AKA_AUTN_LEN);
780 pos += wpa_snprintf_hex(pos, end - pos, ik, EAP_AKA_IK_LEN);
782 pos += wpa_snprintf_hex(pos, end - pos, ck, EAP_AKA_CK_LEN);
784 pos += wpa_snprintf_hex(pos, end - pos, res, res_len);
787 printf("Send: %s\n", reply);
789 if (sendto(s, reply, pos - reply, 0, (struct sockaddr *) from,
795 static void aka_auts(int s, struct sockaddr_un *from, socklen_t fromlen,
799 u8 _auts[EAP_AKA_AUTS_LEN], _rand[EAP_AKA_RAND_LEN], sqn[6];
800 struct milenage_parameters *m;
802 /* AKA-AUTS <IMSI> <AUTS> <RAND> */
804 auts = strchr(imsi, ' ');
809 __rand = strchr(auts, ' ');
814 printf("AKA-AUTS: IMSI=%s AUTS=%s RAND=%s\n", imsi, auts, __rand);
815 if (hexstr2bin(auts, _auts, EAP_AKA_AUTS_LEN) ||
816 hexstr2bin(__rand, _rand, EAP_AKA_RAND_LEN)) {
817 printf("Could not parse AUTS/RAND\n");
821 m = get_milenage(imsi);
823 printf("Unknown IMSI: %s\n", imsi);
827 if (milenage_auts(m->opc, m->ki, _rand, _auts, sqn)) {
828 printf("AKA-AUTS: Incorrect MAC-S\n");
830 memcpy(m->sqn, sqn, 6);
831 printf("AKA-AUTS: Re-synchronized: "
832 "SQN=%02x%02x%02x%02x%02x%02x\n",
833 sqn[0], sqn[1], sqn[2], sqn[3], sqn[4], sqn[5]);
835 db_update_milenage_sqn(m);
836 #endif /* CONFIG_SQLITE */
842 static int process(int s)
845 struct sockaddr_un from;
849 fromlen = sizeof(from);
850 res = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *) &from,
860 if ((size_t) res >= sizeof(buf))
861 res = sizeof(buf) - 1;
864 printf("Received: %s\n", buf);
866 if (strncmp(buf, "SIM-REQ-AUTH ", 13) == 0)
867 sim_req_auth(s, &from, fromlen, buf + 13);
868 else if (strncmp(buf, "AKA-REQ-AUTH ", 13) == 0)
869 aka_req_auth(s, &from, fromlen, buf + 13);
870 else if (strncmp(buf, "AKA-AUTS ", 9) == 0)
871 aka_auts(s, &from, fromlen, buf + 9);
873 printf("Unknown request: %s\n", buf);
879 static void cleanup(void)
881 struct gsm_triplet *g, *gprev;
882 struct milenage_parameters *m, *prev;
884 if (update_milenage && milenage_file && sqn_changes)
885 update_milenage_file(milenage_file);
906 sqlite3_close(sqlite_db);
909 #endif /* CONFIG_SQLITE */
913 static void handle_term(int sig)
915 printf("Signal %d - terminate\n", sig);
920 static void usage(void)
922 printf("HLR/AuC testing gateway for hostapd EAP-SIM/AKA "
923 "database/authenticator\n"
924 "Copyright (c) 2005-2007, 2012-2013, Jouni Malinen <j@w1.fi>\n"
927 "hlr_auc_gw [-hu] [-s<socket path>] [-g<triplet file>] "
928 "[-m<milenage file>] \\\n"
929 " [-D<DB file>] [-i<IND len in bits>]\n"
932 " -h = show this usage help\n"
933 " -u = update SQN in Milenage file on exit\n"
934 " -s<socket path> = path for UNIX domain socket\n"
936 " -g<triplet file> = path for GSM authentication triplets\n"
937 " -m<milenage file> = path for Milenage keys\n"
938 " -D<DB file> = path to SQLite database\n"
939 " -i<IND len in bits> = IND length for SQN (default: 5)\n",
940 default_socket_path);
944 int main(int argc, char *argv[])
947 char *gsm_triplet_file = NULL;
948 char *sqlite_db_file = NULL;
950 if (os_program_init())
953 socket_path = default_socket_path;
956 c = getopt(argc, argv, "D:g:hi:m:s:u");
962 sqlite_db_file = optarg;
964 #else /* CONFIG_SQLITE */
965 printf("No SQLite support included in the build\n");
967 #endif /* CONFIG_SQLITE */
969 gsm_triplet_file = optarg;
975 ind_len = atoi(optarg);
976 if (ind_len < 0 || ind_len > 32) {
977 printf("Invalid IND length\n");
982 milenage_file = optarg;
985 socket_path = optarg;
996 if (!gsm_triplet_file && !milenage_file && !sqlite_db_file) {
1001 #ifdef CONFIG_SQLITE
1002 if (sqlite_db_file && (sqlite_db = db_open(sqlite_db_file)) == NULL)
1004 #endif /* CONFIG_SQLITE */
1006 if (gsm_triplet_file && read_gsm_triplets(gsm_triplet_file) < 0)
1009 if (milenage_file && read_milenage(milenage_file) < 0)
1012 serv_sock = open_socket(socket_path);
1016 printf("Listening for requests on %s\n", socket_path);
1019 signal(SIGTERM, handle_term);
1020 signal(SIGINT, handle_term);
1025 #ifdef CONFIG_SQLITE
1027 sqlite3_close(sqlite_db);
1030 #endif /* CONFIG_SQLITE */
1032 os_program_deinit();