2 * HLR/AuC testing gateway for hostapd EAP-SIM/AKA database/authenticator
3 * Copyright (c) 2005-2007, 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 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 * The 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 * SQN generation follows the not time-based Profile 2 described in
40 * 3GPP TS 33.102 Annex C.3.2. The length of IND is 5 bits by default, but this
41 * can be changed with a command line options if needed.
48 #endif /* CONFIG_SQLITE */
51 #include "crypto/milenage.h"
52 #include "crypto/random.h"
54 static const char *default_socket_path = "/tmp/hlr_auc_gw.sock";
55 static const char *socket_path;
56 static int serv_sock = -1;
57 static char *milenage_file = NULL;
58 static int update_milenage = 0;
59 static int sqn_changes = 0;
60 static int ind_len = 5;
64 struct gsm_triplet *next;
71 static struct gsm_triplet *gsm_db = NULL, *gsm_db_pos = NULL;
73 /* OPc and AMF parameters for Milenage (Example algorithms for AKA). */
74 struct milenage_parameters {
75 struct milenage_parameters *next;
84 static struct milenage_parameters *milenage_db = NULL;
86 #define EAP_SIM_MAX_CHAL 3
88 #define EAP_AKA_RAND_LEN 16
89 #define EAP_AKA_AUTN_LEN 16
90 #define EAP_AKA_AUTS_LEN 14
91 #define EAP_AKA_RES_MAX_LEN 16
92 #define EAP_AKA_IK_LEN 16
93 #define EAP_AKA_CK_LEN 16
98 static sqlite3 *sqlite_db = NULL;
99 static struct milenage_parameters db_tmp_milenage;
102 static int db_table_exists(sqlite3 *db, const char *name)
105 os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
106 return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
110 static int db_table_create_milenage(sqlite3 *db)
114 "CREATE TABLE milenage("
115 " imsi INTEGER PRIMARY KEY NOT NULL,"
116 " ki CHAR(32) NOT NULL,"
117 " opc CHAR(32) NOT NULL,"
118 " amf CHAR(4) NOT NULL,"
119 " sqn CHAR(12) NOT NULL"
122 printf("Adding database table for milenage information\n");
123 if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
124 printf("SQLite error: %s\n", err);
133 static sqlite3 * db_open(const char *db_file)
137 if (sqlite3_open(db_file, &db)) {
138 printf("Failed to open database %s: %s\n",
139 db_file, sqlite3_errmsg(db));
144 if (!db_table_exists(db, "milenage") &&
145 db_table_create_milenage(db) < 0) {
154 static int get_milenage_cb(void *ctx, int argc, char *argv[], char *col[])
156 struct milenage_parameters *m = ctx;
161 for (i = 0; i < argc; i++) {
162 if (os_strcmp(col[i], "ki") == 0 && argv[i] &&
163 hexstr2bin(argv[i], m->ki, sizeof(m->ki))) {
164 printf("Invalid ki value in database\n");
168 if (os_strcmp(col[i], "opc") == 0 && argv[i] &&
169 hexstr2bin(argv[i], m->opc, sizeof(m->opc))) {
170 printf("Invalid opcvalue in database\n");
174 if (os_strcmp(col[i], "amf") == 0 && argv[i] &&
175 hexstr2bin(argv[i], m->amf, sizeof(m->amf))) {
176 printf("Invalid amf value in database\n");
180 if (os_strcmp(col[i], "sqn") == 0 && argv[i] &&
181 hexstr2bin(argv[i], m->sqn, sizeof(m->sqn))) {
182 printf("Invalid sqn value in database\n");
191 static struct milenage_parameters * db_get_milenage(const char *imsi_txt)
194 unsigned long long imsi;
196 os_memset(&db_tmp_milenage, 0, sizeof(db_tmp_milenage));
197 imsi = atoll(imsi_txt);
198 os_snprintf(db_tmp_milenage.imsi, sizeof(db_tmp_milenage.imsi),
200 os_snprintf(cmd, sizeof(cmd),
201 "SELECT ki,opc,amf,sqn FROM milenage WHERE imsi=%llu;",
203 if (sqlite3_exec(sqlite_db, cmd, get_milenage_cb, &db_tmp_milenage,
207 if (!db_tmp_milenage.set)
209 return &db_tmp_milenage;
213 static int db_update_milenage_sqn(struct milenage_parameters *m)
215 char cmd[128], val[13], *pos;
218 pos += wpa_snprintf_hex(pos, sizeof(val), m->sqn, 6);
220 os_snprintf(cmd, sizeof(cmd),
221 "UPDATE milenage SET sqn='%s' WHERE imsi=%s;",
223 if (sqlite3_exec(sqlite_db, cmd, NULL, NULL, NULL) != SQLITE_OK) {
224 printf("Failed to update SQN in database for IMSI %s\n",
231 #endif /* CONFIG_SQLITE */
234 static int open_socket(const char *path)
236 struct sockaddr_un addr;
239 s = socket(PF_UNIX, SOCK_DGRAM, 0);
241 perror("socket(PF_UNIX)");
245 memset(&addr, 0, sizeof(addr));
246 addr.sun_family = AF_UNIX;
247 os_strlcpy(addr.sun_path, path, sizeof(addr.sun_path));
248 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
249 perror("hlr-auc-gw: bind(PF_UNIX)");
258 static int read_gsm_triplets(const char *fname)
261 char buf[200], *pos, *pos2;
262 struct gsm_triplet *g = NULL;
268 f = fopen(fname, "r");
270 printf("Could not open GSM tripler data file '%s'\n", fname);
275 while (fgets(buf, sizeof(buf), f)) {
278 /* Parse IMSI:Kc:SRES:RAND */
279 buf[sizeof(buf) - 1] = '\0';
283 while (*pos != '\0' && *pos != '\n')
291 g = os_zalloc(sizeof(*g));
298 pos2 = strchr(pos, ':');
300 printf("%s:%d - Invalid IMSI (%s)\n",
306 if (strlen(pos) >= sizeof(g->imsi)) {
307 printf("%s:%d - Too long IMSI (%s)\n",
312 os_strlcpy(g->imsi, pos, sizeof(g->imsi));
316 pos2 = strchr(pos, ':');
318 printf("%s:%d - Invalid Kc (%s)\n", fname, line, pos);
323 if (strlen(pos) != 16 || hexstr2bin(pos, g->kc, 8)) {
324 printf("%s:%d - Invalid Kc (%s)\n", fname, line, pos);
331 pos2 = strchr(pos, ':');
333 printf("%s:%d - Invalid SRES (%s)\n", fname, line,
339 if (strlen(pos) != 8 || hexstr2bin(pos, g->sres, 4)) {
340 printf("%s:%d - Invalid SRES (%s)\n", fname, line,
348 pos2 = strchr(pos, ':');
351 if (strlen(pos) != 32 || hexstr2bin(pos, g->_rand, 16)) {
352 printf("%s:%d - Invalid RAND (%s)\n", fname, line,
371 static struct gsm_triplet * get_gsm_triplet(const char *imsi)
373 struct gsm_triplet *g = gsm_db_pos;
376 if (strcmp(g->imsi, imsi) == 0) {
377 gsm_db_pos = g->next;
384 while (g && g != gsm_db_pos) {
385 if (strcmp(g->imsi, imsi) == 0) {
386 gsm_db_pos = g->next;
396 static int read_milenage(const char *fname)
399 char buf[200], *pos, *pos2;
400 struct milenage_parameters *m = NULL;
406 f = fopen(fname, "r");
408 printf("Could not open Milenage data file '%s'\n", fname);
413 while (fgets(buf, sizeof(buf), f)) {
416 /* Parse IMSI Ki OPc AMF SQN */
417 buf[sizeof(buf) - 1] = '\0';
421 while (*pos != '\0' && *pos != '\n')
429 m = os_zalloc(sizeof(*m));
436 pos2 = strchr(pos, ' ');
438 printf("%s:%d - Invalid IMSI (%s)\n",
444 if (strlen(pos) >= sizeof(m->imsi)) {
445 printf("%s:%d - Too long IMSI (%s)\n",
450 os_strlcpy(m->imsi, pos, sizeof(m->imsi));
454 pos2 = strchr(pos, ' ');
456 printf("%s:%d - Invalid Ki (%s)\n", fname, line, pos);
461 if (strlen(pos) != 32 || hexstr2bin(pos, m->ki, 16)) {
462 printf("%s:%d - Invalid Ki (%s)\n", fname, line, pos);
469 pos2 = strchr(pos, ' ');
471 printf("%s:%d - Invalid OPc (%s)\n", fname, line, pos);
476 if (strlen(pos) != 32 || hexstr2bin(pos, m->opc, 16)) {
477 printf("%s:%d - Invalid OPc (%s)\n", fname, line, pos);
484 pos2 = strchr(pos, ' ');
486 printf("%s:%d - Invalid AMF (%s)\n", fname, line, pos);
491 if (strlen(pos) != 4 || hexstr2bin(pos, m->amf, 2)) {
492 printf("%s:%d - Invalid AMF (%s)\n", fname, line, pos);
499 pos2 = strchr(pos, ' ');
502 if (strlen(pos) != 12 || hexstr2bin(pos, m->sqn, 6)) {
503 printf("%s:%d - Invalid SEQ (%s)\n", fname, line, pos);
509 m->next = milenage_db;
521 static void update_milenage_file(const char *fname)
525 char *end = buf + sizeof(buf);
526 struct milenage_parameters *m;
529 f = fopen(fname, "r");
531 printf("Could not open Milenage data file '%s'\n", fname);
535 snprintf(buf, sizeof(buf), "%s.new", fname);
536 f2 = fopen(buf, "w");
538 printf("Could not write Milenage data file '%s'\n", buf);
543 while (fgets(buf, sizeof(buf), f)) {
544 /* IMSI Ki OPc AMF SQN */
545 buf[sizeof(buf) - 1] = '\0';
547 pos = strchr(buf, ' ');
548 if (buf[0] == '#' || pos == NULL || pos - buf >= 20)
551 imsi_len = pos - buf;
553 for (m = milenage_db; m; m = m->next) {
554 if (strncmp(buf, m->imsi, imsi_len) == 0 &&
555 m->imsi[imsi_len] == '\0')
563 pos += snprintf(pos, end - pos, "%s ", m->imsi);
564 pos += wpa_snprintf_hex(pos, end - pos, m->ki, 16);
566 pos += wpa_snprintf_hex(pos, end - pos, m->opc, 16);
568 pos += wpa_snprintf_hex(pos, end - pos, m->amf, 2);
570 pos += wpa_snprintf_hex(pos, end - pos, m->sqn, 6);
574 fprintf(f2, "%s", buf);
580 snprintf(buf, sizeof(buf), "%s.bak", fname);
581 if (rename(fname, buf) < 0) {
586 snprintf(buf, sizeof(buf), "%s.new", fname);
587 if (rename(buf, fname) < 0) {
595 static struct milenage_parameters * get_milenage(const char *imsi)
597 struct milenage_parameters *m = milenage_db;
600 if (strcmp(m->imsi, imsi) == 0)
607 m = db_get_milenage(imsi);
608 #endif /* CONFIG_SQLITE */
614 static void sim_req_auth(int s, struct sockaddr_un *from, socklen_t fromlen,
617 int count, max_chal, ret;
619 char reply[1000], *rpos, *rend;
620 struct milenage_parameters *m;
621 struct gsm_triplet *g;
625 pos = strchr(imsi, ' ');
628 max_chal = atoi(pos);
629 if (max_chal < 1 || max_chal < EAP_SIM_MAX_CHAL)
630 max_chal = EAP_SIM_MAX_CHAL;
632 max_chal = EAP_SIM_MAX_CHAL;
634 rend = &reply[sizeof(reply)];
636 ret = snprintf(rpos, rend - rpos, "SIM-RESP-AUTH %s", imsi);
637 if (ret < 0 || ret >= rend - rpos)
641 m = get_milenage(imsi);
643 u8 _rand[16], sres[4], kc[8];
644 for (count = 0; count < max_chal; count++) {
645 if (random_get_bytes(_rand, 16) < 0)
647 gsm_milenage(m->opc, m->ki, _rand, sres, kc);
649 rpos += wpa_snprintf_hex(rpos, rend - rpos, kc, 8);
651 rpos += wpa_snprintf_hex(rpos, rend - rpos, sres, 4);
653 rpos += wpa_snprintf_hex(rpos, rend - rpos, _rand, 16);
660 while (count < max_chal && (g = get_gsm_triplet(imsi))) {
661 if (strcmp(g->imsi, imsi) != 0)
666 rpos += wpa_snprintf_hex(rpos, rend - rpos, g->kc, 8);
669 rpos += wpa_snprintf_hex(rpos, rend - rpos, g->sres, 4);
672 rpos += wpa_snprintf_hex(rpos, rend - rpos, g->_rand, 16);
677 printf("No GSM triplets found for %s\n", imsi);
678 ret = snprintf(rpos, rend - rpos, " FAILURE");
679 if (ret < 0 || ret >= rend - rpos)
685 printf("Send: %s\n", reply);
686 if (sendto(s, reply, rpos - reply, 0,
687 (struct sockaddr *) from, fromlen) < 0)
692 static void inc_sqn(u8 *sqn)
697 * SQN = SEQ | IND = SEQ1 | SEQ2 | IND
699 * The mechanism used here is not time-based, so SEQ2 is void and
700 * SQN = SEQ1 | IND. The length of IND is ind_len bits and the length
701 * of SEQ1 is 48 - ind_len bits.
704 /* Increment both SEQ and IND by one */
705 val = ((u64) WPA_GET_BE32(sqn) << 16) | ((u64) WPA_GET_BE16(sqn + 4));
706 seq = (val >> ind_len) + 1;
707 ind = (val + 1) & ((1 << ind_len) - 1);
708 val = (seq << ind_len) | ind;
709 WPA_PUT_BE32(sqn, val >> 16);
710 WPA_PUT_BE16(sqn + 4, val & 0xffff);
714 static void aka_req_auth(int s, struct sockaddr_un *from, socklen_t fromlen,
717 /* AKA-RESP-AUTH <IMSI> <RAND> <AUTN> <IK> <CK> <RES> */
718 char reply[1000], *pos, *end;
719 u8 _rand[EAP_AKA_RAND_LEN];
720 u8 autn[EAP_AKA_AUTN_LEN];
721 u8 ik[EAP_AKA_IK_LEN];
722 u8 ck[EAP_AKA_CK_LEN];
723 u8 res[EAP_AKA_RES_MAX_LEN];
726 struct milenage_parameters *m;
729 m = get_milenage(imsi);
731 if (random_get_bytes(_rand, EAP_AKA_RAND_LEN) < 0)
733 res_len = EAP_AKA_RES_MAX_LEN;
736 db_update_milenage_sqn(m);
737 #endif /* CONFIG_SQLITE */
739 printf("AKA: Milenage with SQN=%02x%02x%02x%02x%02x%02x\n",
740 m->sqn[0], m->sqn[1], m->sqn[2],
741 m->sqn[3], m->sqn[4], m->sqn[5]);
742 milenage_generate(m->opc, m->amf, m->ki, m->sqn, _rand,
743 autn, ik, ck, res, &res_len);
745 printf("Unknown IMSI: %s\n", imsi);
746 #ifdef AKA_USE_FIXED_TEST_VALUES
747 printf("Using fixed test values for AKA\n");
748 memset(_rand, '0', EAP_AKA_RAND_LEN);
749 memset(autn, '1', EAP_AKA_AUTN_LEN);
750 memset(ik, '3', EAP_AKA_IK_LEN);
751 memset(ck, '4', EAP_AKA_CK_LEN);
752 memset(res, '2', EAP_AKA_RES_MAX_LEN);
753 res_len = EAP_AKA_RES_MAX_LEN;
754 #else /* AKA_USE_FIXED_TEST_VALUES */
756 #endif /* AKA_USE_FIXED_TEST_VALUES */
760 end = &reply[sizeof(reply)];
761 ret = snprintf(pos, end - pos, "AKA-RESP-AUTH %s ", imsi);
762 if (ret < 0 || ret >= end - pos)
766 ret = snprintf(pos, end - pos, "FAILURE");
767 if (ret < 0 || ret >= end - pos)
772 pos += wpa_snprintf_hex(pos, end - pos, _rand, EAP_AKA_RAND_LEN);
774 pos += wpa_snprintf_hex(pos, end - pos, autn, EAP_AKA_AUTN_LEN);
776 pos += wpa_snprintf_hex(pos, end - pos, ik, EAP_AKA_IK_LEN);
778 pos += wpa_snprintf_hex(pos, end - pos, ck, EAP_AKA_CK_LEN);
780 pos += wpa_snprintf_hex(pos, end - pos, res, res_len);
783 printf("Send: %s\n", reply);
785 if (sendto(s, reply, pos - reply, 0, (struct sockaddr *) from,
791 static void aka_auts(int s, struct sockaddr_un *from, socklen_t fromlen,
795 u8 _auts[EAP_AKA_AUTS_LEN], _rand[EAP_AKA_RAND_LEN], sqn[6];
796 struct milenage_parameters *m;
798 /* AKA-AUTS <IMSI> <AUTS> <RAND> */
800 auts = strchr(imsi, ' ');
805 __rand = strchr(auts, ' ');
810 printf("AKA-AUTS: IMSI=%s AUTS=%s RAND=%s\n", imsi, auts, __rand);
811 if (hexstr2bin(auts, _auts, EAP_AKA_AUTS_LEN) ||
812 hexstr2bin(__rand, _rand, EAP_AKA_RAND_LEN)) {
813 printf("Could not parse AUTS/RAND\n");
817 m = get_milenage(imsi);
819 printf("Unknown IMSI: %s\n", imsi);
823 if (milenage_auts(m->opc, m->ki, _rand, _auts, sqn)) {
824 printf("AKA-AUTS: Incorrect MAC-S\n");
826 memcpy(m->sqn, sqn, 6);
827 printf("AKA-AUTS: Re-synchronized: "
828 "SQN=%02x%02x%02x%02x%02x%02x\n",
829 sqn[0], sqn[1], sqn[2], sqn[3], sqn[4], sqn[5]);
831 db_update_milenage_sqn(m);
832 #endif /* CONFIG_SQLITE */
838 static int process(int s)
841 struct sockaddr_un from;
845 fromlen = sizeof(from);
846 res = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *) &from,
856 if ((size_t) res >= sizeof(buf))
857 res = sizeof(buf) - 1;
860 printf("Received: %s\n", buf);
862 if (strncmp(buf, "SIM-REQ-AUTH ", 13) == 0)
863 sim_req_auth(s, &from, fromlen, buf + 13);
864 else if (strncmp(buf, "AKA-REQ-AUTH ", 13) == 0)
865 aka_req_auth(s, &from, fromlen, buf + 13);
866 else if (strncmp(buf, "AKA-AUTS ", 9) == 0)
867 aka_auts(s, &from, fromlen, buf + 9);
869 printf("Unknown request: %s\n", buf);
875 static void cleanup(void)
877 struct gsm_triplet *g, *gprev;
878 struct milenage_parameters *m, *prev;
880 if (update_milenage && milenage_file && sqn_changes)
881 update_milenage_file(milenage_file);
902 sqlite3_close(sqlite_db);
905 #endif /* CONFIG_SQLITE */
909 static void handle_term(int sig)
911 printf("Signal %d - terminate\n", sig);
916 static void usage(void)
918 printf("HLR/AuC testing gateway for hostapd EAP-SIM/AKA "
919 "database/authenticator\n"
920 "Copyright (c) 2005-2007, 2012, Jouni Malinen <j@w1.fi>\n"
923 "hlr_auc_gw [-hu] [-s<socket path>] [-g<triplet file>] "
924 "[-m<milenage file>] \\\n"
925 " [-D<DB file>] [-i<IND len in bits>]\n"
928 " -h = show this usage help\n"
929 " -u = update SQN in Milenage file on exit\n"
930 " -s<socket path> = path for UNIX domain socket\n"
932 " -g<triplet file> = path for GSM authentication triplets\n"
933 " -m<milenage file> = path for Milenage keys\n"
934 " -D<DB file> = path to SQLite database\n"
935 " -i<IND len in bits> = IND length for SQN (default: 5)\n",
936 default_socket_path);
940 int main(int argc, char *argv[])
943 char *gsm_triplet_file = NULL;
944 char *sqlite_db_file = NULL;
946 if (os_program_init())
949 socket_path = default_socket_path;
952 c = getopt(argc, argv, "D:g:hi:m:s:u");
958 sqlite_db_file = optarg;
960 #else /* CONFIG_SQLITE */
961 printf("No SQLite support included in the build\n");
963 #endif /* CONFIG_SQLITE */
965 gsm_triplet_file = optarg;
971 ind_len = atoi(optarg);
972 if (ind_len < 0 || ind_len > 32) {
973 printf("Invalid IND length\n");
978 milenage_file = optarg;
981 socket_path = optarg;
992 if (!gsm_triplet_file && !milenage_file && !sqlite_db_file) {
998 if (sqlite_db_file && (sqlite_db = db_open(sqlite_db_file)) == NULL)
1000 #endif /* CONFIG_SQLITE */
1002 if (gsm_triplet_file && read_gsm_triplets(gsm_triplet_file) < 0)
1005 if (milenage_file && read_milenage(milenage_file) < 0)
1008 serv_sock = open_socket(socket_path);
1012 printf("Listening for requests on %s\n", socket_path);
1015 signal(SIGTERM, handle_term);
1016 signal(SIGINT, handle_term);
1021 #ifdef CONFIG_SQLITE
1023 sqlite3_close(sqlite_db);
1026 #endif /* CONFIG_SQLITE */
1028 os_program_deinit();