Home | History | Annotate | Line # | Download | only in hostapd
hlr_auc_gw.c revision 1.1.1.4
      1      1.1  christos /*
      2      1.1  christos  * HLR/AuC testing gateway for hostapd EAP-SIM/AKA database/authenticator
      3  1.1.1.4  christos  * Copyright (c) 2005-2007, 2012, Jouni Malinen <j (at) w1.fi>
      4      1.1  christos  *
      5  1.1.1.4  christos  * This software may be distributed under the terms of the BSD license.
      6  1.1.1.4  christos  * See README for more details.
      7      1.1  christos  *
      8      1.1  christos  * This is an example implementation of the EAP-SIM/AKA database/authentication
      9      1.1  christos  * gateway interface to HLR/AuC. It is expected to be replaced with an
     10      1.1  christos  * implementation of SS7 gateway to GSM/UMTS authentication center (HLR/AuC) or
     11      1.1  christos  * a local implementation of SIM triplet and AKA authentication data generator.
     12      1.1  christos  *
     13      1.1  christos  * hostapd will send SIM/AKA authentication queries over a UNIX domain socket
     14      1.1  christos  * to and external program, e.g., this hlr_auc_gw. This interface uses simple
     15      1.1  christos  * text-based format:
     16      1.1  christos  *
     17      1.1  christos  * EAP-SIM / GSM triplet query/response:
     18      1.1  christos  * SIM-REQ-AUTH <IMSI> <max_chal>
     19      1.1  christos  * SIM-RESP-AUTH <IMSI> Kc1:SRES1:RAND1 Kc2:SRES2:RAND2 [Kc3:SRES3:RAND3]
     20      1.1  christos  * SIM-RESP-AUTH <IMSI> FAILURE
     21      1.1  christos  *
     22      1.1  christos  * EAP-AKA / UMTS query/response:
     23      1.1  christos  * AKA-REQ-AUTH <IMSI>
     24      1.1  christos  * AKA-RESP-AUTH <IMSI> <RAND> <AUTN> <IK> <CK> <RES>
     25      1.1  christos  * AKA-RESP-AUTH <IMSI> FAILURE
     26      1.1  christos  *
     27      1.1  christos  * EAP-AKA / UMTS AUTS (re-synchronization):
     28      1.1  christos  * AKA-AUTS <IMSI> <AUTS> <RAND>
     29      1.1  christos  *
     30      1.1  christos  * IMSI and max_chal are sent as an ASCII string,
     31      1.1  christos  * Kc/SRES/RAND/AUTN/IK/CK/RES/AUTS as hex strings.
     32      1.1  christos  *
     33      1.1  christos  * The example implementation here reads GSM authentication triplets from a
     34      1.1  christos  * text file in IMSI:Kc:SRES:RAND format, IMSI in ASCII, other fields as hex
     35      1.1  christos  * strings. This is used to simulate an HLR/AuC. As such, it is not very useful
     36      1.1  christos  * for real life authentication, but it is useful both as an example
     37  1.1.1.3      adam  * implementation and for EAP-SIM/AKA/AKA' testing.
     38  1.1.1.3      adam  *
     39  1.1.1.3      adam  * SQN generation follows the not time-based Profile 2 described in
     40  1.1.1.3      adam  * 3GPP TS 33.102 Annex C.3.2. The length of IND is 5 bits by default, but this
     41  1.1.1.3      adam  * can be changed with a command line options if needed.
     42      1.1  christos  */
     43      1.1  christos 
     44      1.1  christos #include "includes.h"
     45      1.1  christos #include <sys/un.h>
     46  1.1.1.4  christos #ifdef CONFIG_SQLITE
     47  1.1.1.4  christos #include <sqlite3.h>
     48  1.1.1.4  christos #endif /* CONFIG_SQLITE */
     49      1.1  christos 
     50      1.1  christos #include "common.h"
     51      1.1  christos #include "crypto/milenage.h"
     52  1.1.1.2  christos #include "crypto/random.h"
     53      1.1  christos 
     54      1.1  christos static const char *default_socket_path = "/tmp/hlr_auc_gw.sock";
     55      1.1  christos static const char *socket_path;
     56      1.1  christos static int serv_sock = -1;
     57  1.1.1.4  christos static char *milenage_file = NULL;
     58  1.1.1.4  christos static int update_milenage = 0;
     59  1.1.1.4  christos static int sqn_changes = 0;
     60  1.1.1.3      adam static int ind_len = 5;
     61      1.1  christos 
     62      1.1  christos /* GSM triplets */
     63      1.1  christos struct gsm_triplet {
     64      1.1  christos 	struct gsm_triplet *next;
     65      1.1  christos 	char imsi[20];
     66      1.1  christos 	u8 kc[8];
     67      1.1  christos 	u8 sres[4];
     68      1.1  christos 	u8 _rand[16];
     69      1.1  christos };
     70      1.1  christos 
     71      1.1  christos static struct gsm_triplet *gsm_db = NULL, *gsm_db_pos = NULL;
     72      1.1  christos 
     73      1.1  christos /* OPc and AMF parameters for Milenage (Example algorithms for AKA). */
     74      1.1  christos struct milenage_parameters {
     75      1.1  christos 	struct milenage_parameters *next;
     76      1.1  christos 	char imsi[20];
     77      1.1  christos 	u8 ki[16];
     78      1.1  christos 	u8 opc[16];
     79      1.1  christos 	u8 amf[2];
     80      1.1  christos 	u8 sqn[6];
     81  1.1.1.4  christos 	int set;
     82      1.1  christos };
     83      1.1  christos 
     84      1.1  christos static struct milenage_parameters *milenage_db = NULL;
     85      1.1  christos 
     86      1.1  christos #define EAP_SIM_MAX_CHAL 3
     87      1.1  christos 
     88      1.1  christos #define EAP_AKA_RAND_LEN 16
     89      1.1  christos #define EAP_AKA_AUTN_LEN 16
     90      1.1  christos #define EAP_AKA_AUTS_LEN 14
     91      1.1  christos #define EAP_AKA_RES_MAX_LEN 16
     92      1.1  christos #define EAP_AKA_IK_LEN 16
     93      1.1  christos #define EAP_AKA_CK_LEN 16
     94      1.1  christos 
     95      1.1  christos 
     96  1.1.1.4  christos #ifdef CONFIG_SQLITE
     97  1.1.1.4  christos 
     98  1.1.1.4  christos static sqlite3 *sqlite_db = NULL;
     99  1.1.1.4  christos static struct milenage_parameters db_tmp_milenage;
    100  1.1.1.4  christos 
    101  1.1.1.4  christos 
    102  1.1.1.4  christos static int db_table_exists(sqlite3 *db, const char *name)
    103  1.1.1.4  christos {
    104  1.1.1.4  christos 	char cmd[128];
    105  1.1.1.4  christos 	os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
    106  1.1.1.4  christos 	return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
    107  1.1.1.4  christos }
    108  1.1.1.4  christos 
    109  1.1.1.4  christos 
    110  1.1.1.4  christos static int db_table_create_milenage(sqlite3 *db)
    111  1.1.1.4  christos {
    112  1.1.1.4  christos 	char *err = NULL;
    113  1.1.1.4  christos 	const char *sql =
    114  1.1.1.4  christos 		"CREATE TABLE milenage("
    115  1.1.1.4  christos 		"  imsi INTEGER PRIMARY KEY NOT NULL,"
    116  1.1.1.4  christos 		"  ki CHAR(32) NOT NULL,"
    117  1.1.1.4  christos 		"  opc CHAR(32) NOT NULL,"
    118  1.1.1.4  christos 		"  amf CHAR(4) NOT NULL,"
    119  1.1.1.4  christos 		"  sqn CHAR(12) NOT NULL"
    120  1.1.1.4  christos 		");";
    121  1.1.1.4  christos 
    122  1.1.1.4  christos 	printf("Adding database table for milenage information\n");
    123  1.1.1.4  christos 	if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
    124  1.1.1.4  christos 		printf("SQLite error: %s\n", err);
    125  1.1.1.4  christos 		sqlite3_free(err);
    126  1.1.1.4  christos 		return -1;
    127  1.1.1.4  christos 	}
    128  1.1.1.4  christos 
    129  1.1.1.4  christos 	return 0;
    130  1.1.1.4  christos }
    131  1.1.1.4  christos 
    132  1.1.1.4  christos 
    133  1.1.1.4  christos static sqlite3 * db_open(const char *db_file)
    134  1.1.1.4  christos {
    135  1.1.1.4  christos 	sqlite3 *db;
    136  1.1.1.4  christos 
    137  1.1.1.4  christos 	if (sqlite3_open(db_file, &db)) {
    138  1.1.1.4  christos 		printf("Failed to open database %s: %s\n",
    139  1.1.1.4  christos 		       db_file, sqlite3_errmsg(db));
    140  1.1.1.4  christos 		sqlite3_close(db);
    141  1.1.1.4  christos 		return NULL;
    142  1.1.1.4  christos 	}
    143  1.1.1.4  christos 
    144  1.1.1.4  christos 	if (!db_table_exists(db, "milenage") &&
    145  1.1.1.4  christos 	    db_table_create_milenage(db) < 0) {
    146  1.1.1.4  christos 		sqlite3_close(db);
    147  1.1.1.4  christos 		return NULL;
    148  1.1.1.4  christos 	}
    149  1.1.1.4  christos 
    150  1.1.1.4  christos 	return db;
    151  1.1.1.4  christos }
    152  1.1.1.4  christos 
    153  1.1.1.4  christos 
    154  1.1.1.4  christos static int get_milenage_cb(void *ctx, int argc, char *argv[], char *col[])
    155  1.1.1.4  christos {
    156  1.1.1.4  christos 	struct milenage_parameters *m = ctx;
    157  1.1.1.4  christos 	int i;
    158  1.1.1.4  christos 
    159  1.1.1.4  christos 	m->set = 1;
    160  1.1.1.4  christos 
    161  1.1.1.4  christos 	for (i = 0; i < argc; i++) {
    162  1.1.1.4  christos 		if (os_strcmp(col[i], "ki") == 0 && argv[i] &&
    163  1.1.1.4  christos 		    hexstr2bin(argv[i], m->ki, sizeof(m->ki))) {
    164  1.1.1.4  christos 			printf("Invalid ki value in database\n");
    165  1.1.1.4  christos 			return -1;
    166  1.1.1.4  christos 		}
    167  1.1.1.4  christos 
    168  1.1.1.4  christos 		if (os_strcmp(col[i], "opc") == 0 && argv[i] &&
    169  1.1.1.4  christos 		    hexstr2bin(argv[i], m->opc, sizeof(m->opc))) {
    170  1.1.1.4  christos 			printf("Invalid opcvalue in database\n");
    171  1.1.1.4  christos 			return -1;
    172  1.1.1.4  christos 		}
    173  1.1.1.4  christos 
    174  1.1.1.4  christos 		if (os_strcmp(col[i], "amf") == 0 && argv[i] &&
    175  1.1.1.4  christos 		    hexstr2bin(argv[i], m->amf, sizeof(m->amf))) {
    176  1.1.1.4  christos 			printf("Invalid amf value in database\n");
    177  1.1.1.4  christos 			return -1;
    178  1.1.1.4  christos 		}
    179  1.1.1.4  christos 
    180  1.1.1.4  christos 		if (os_strcmp(col[i], "sqn") == 0 && argv[i] &&
    181  1.1.1.4  christos 		    hexstr2bin(argv[i], m->sqn, sizeof(m->sqn))) {
    182  1.1.1.4  christos 			printf("Invalid sqn value in database\n");
    183  1.1.1.4  christos 			return -1;
    184  1.1.1.4  christos 		}
    185  1.1.1.4  christos 	}
    186  1.1.1.4  christos 
    187  1.1.1.4  christos 	return 0;
    188  1.1.1.4  christos }
    189  1.1.1.4  christos 
    190  1.1.1.4  christos 
    191  1.1.1.4  christos static struct milenage_parameters * db_get_milenage(const char *imsi_txt)
    192  1.1.1.4  christos {
    193  1.1.1.4  christos 	char cmd[128];
    194  1.1.1.4  christos 	unsigned long long imsi;
    195  1.1.1.4  christos 
    196  1.1.1.4  christos 	os_memset(&db_tmp_milenage, 0, sizeof(db_tmp_milenage));
    197  1.1.1.4  christos 	imsi = atoll(imsi_txt);
    198  1.1.1.4  christos 	os_snprintf(db_tmp_milenage.imsi, sizeof(db_tmp_milenage.imsi),
    199  1.1.1.4  christos 		    "%llu", imsi);
    200  1.1.1.4  christos 	os_snprintf(cmd, sizeof(cmd),
    201  1.1.1.4  christos 		    "SELECT ki,opc,amf,sqn FROM milenage WHERE imsi=%llu;",
    202  1.1.1.4  christos 		    imsi);
    203  1.1.1.4  christos 	if (sqlite3_exec(sqlite_db, cmd, get_milenage_cb, &db_tmp_milenage,
    204  1.1.1.4  christos 			 NULL) != SQLITE_OK)
    205  1.1.1.4  christos 		return NULL;
    206  1.1.1.4  christos 
    207  1.1.1.4  christos 	if (!db_tmp_milenage.set)
    208  1.1.1.4  christos 		return NULL;
    209  1.1.1.4  christos 	return &db_tmp_milenage;
    210  1.1.1.4  christos }
    211  1.1.1.4  christos 
    212  1.1.1.4  christos 
    213  1.1.1.4  christos static int db_update_milenage_sqn(struct milenage_parameters *m)
    214  1.1.1.4  christos {
    215  1.1.1.4  christos 	char cmd[128], val[13], *pos;
    216  1.1.1.4  christos 
    217  1.1.1.4  christos 	pos = val;
    218  1.1.1.4  christos 	pos += wpa_snprintf_hex(pos, sizeof(val), m->sqn, 6);
    219  1.1.1.4  christos 	*pos = '\0';
    220  1.1.1.4  christos 	os_snprintf(cmd, sizeof(cmd),
    221  1.1.1.4  christos 		    "UPDATE milenage SET sqn='%s' WHERE imsi=%s;",
    222  1.1.1.4  christos 		    val, m->imsi);
    223  1.1.1.4  christos 	if (sqlite3_exec(sqlite_db, cmd, NULL, NULL, NULL) != SQLITE_OK) {
    224  1.1.1.4  christos 		printf("Failed to update SQN in database for IMSI %s\n",
    225  1.1.1.4  christos 		       m->imsi);
    226  1.1.1.4  christos 		return -1;
    227  1.1.1.4  christos 	}
    228  1.1.1.4  christos 	return 0;
    229  1.1.1.4  christos }
    230  1.1.1.4  christos 
    231  1.1.1.4  christos #endif /* CONFIG_SQLITE */
    232  1.1.1.4  christos 
    233  1.1.1.4  christos 
    234      1.1  christos static int open_socket(const char *path)
    235      1.1  christos {
    236      1.1  christos 	struct sockaddr_un addr;
    237      1.1  christos 	int s;
    238      1.1  christos 
    239      1.1  christos 	s = socket(PF_UNIX, SOCK_DGRAM, 0);
    240      1.1  christos 	if (s < 0) {
    241      1.1  christos 		perror("socket(PF_UNIX)");
    242      1.1  christos 		return -1;
    243      1.1  christos 	}
    244      1.1  christos 
    245      1.1  christos 	memset(&addr, 0, sizeof(addr));
    246      1.1  christos 	addr.sun_family = AF_UNIX;
    247      1.1  christos 	os_strlcpy(addr.sun_path, path, sizeof(addr.sun_path));
    248      1.1  christos 	if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
    249  1.1.1.4  christos 		perror("hlr-auc-gw: bind(PF_UNIX)");
    250      1.1  christos 		close(s);
    251      1.1  christos 		return -1;
    252      1.1  christos 	}
    253      1.1  christos 
    254      1.1  christos 	return s;
    255      1.1  christos }
    256      1.1  christos 
    257      1.1  christos 
    258      1.1  christos static int read_gsm_triplets(const char *fname)
    259      1.1  christos {
    260      1.1  christos 	FILE *f;
    261      1.1  christos 	char buf[200], *pos, *pos2;
    262      1.1  christos 	struct gsm_triplet *g = NULL;
    263      1.1  christos 	int line, ret = 0;
    264      1.1  christos 
    265      1.1  christos 	if (fname == NULL)
    266      1.1  christos 		return -1;
    267      1.1  christos 
    268      1.1  christos 	f = fopen(fname, "r");
    269      1.1  christos 	if (f == NULL) {
    270      1.1  christos 		printf("Could not open GSM tripler data file '%s'\n", fname);
    271      1.1  christos 		return -1;
    272      1.1  christos 	}
    273      1.1  christos 
    274      1.1  christos 	line = 0;
    275      1.1  christos 	while (fgets(buf, sizeof(buf), f)) {
    276      1.1  christos 		line++;
    277      1.1  christos 
    278      1.1  christos 		/* Parse IMSI:Kc:SRES:RAND */
    279      1.1  christos 		buf[sizeof(buf) - 1] = '\0';
    280      1.1  christos 		if (buf[0] == '#')
    281      1.1  christos 			continue;
    282      1.1  christos 		pos = buf;
    283      1.1  christos 		while (*pos != '\0' && *pos != '\n')
    284      1.1  christos 			pos++;
    285      1.1  christos 		if (*pos == '\n')
    286      1.1  christos 			*pos = '\0';
    287      1.1  christos 		pos = buf;
    288      1.1  christos 		if (*pos == '\0')
    289      1.1  christos 			continue;
    290      1.1  christos 
    291      1.1  christos 		g = os_zalloc(sizeof(*g));
    292      1.1  christos 		if (g == NULL) {
    293      1.1  christos 			ret = -1;
    294      1.1  christos 			break;
    295      1.1  christos 		}
    296      1.1  christos 
    297      1.1  christos 		/* IMSI */
    298      1.1  christos 		pos2 = strchr(pos, ':');
    299      1.1  christos 		if (pos2 == NULL) {
    300      1.1  christos 			printf("%s:%d - Invalid IMSI (%s)\n",
    301      1.1  christos 			       fname, line, pos);
    302      1.1  christos 			ret = -1;
    303      1.1  christos 			break;
    304      1.1  christos 		}
    305      1.1  christos 		*pos2 = '\0';
    306      1.1  christos 		if (strlen(pos) >= sizeof(g->imsi)) {
    307      1.1  christos 			printf("%s:%d - Too long IMSI (%s)\n",
    308      1.1  christos 			       fname, line, pos);
    309      1.1  christos 			ret = -1;
    310      1.1  christos 			break;
    311      1.1  christos 		}
    312      1.1  christos 		os_strlcpy(g->imsi, pos, sizeof(g->imsi));
    313      1.1  christos 		pos = pos2 + 1;
    314      1.1  christos 
    315      1.1  christos 		/* Kc */
    316      1.1  christos 		pos2 = strchr(pos, ':');
    317      1.1  christos 		if (pos2 == NULL) {
    318      1.1  christos 			printf("%s:%d - Invalid Kc (%s)\n", fname, line, pos);
    319      1.1  christos 			ret = -1;
    320      1.1  christos 			break;
    321      1.1  christos 		}
    322      1.1  christos 		*pos2 = '\0';
    323      1.1  christos 		if (strlen(pos) != 16 || hexstr2bin(pos, g->kc, 8)) {
    324      1.1  christos 			printf("%s:%d - Invalid Kc (%s)\n", fname, line, pos);
    325      1.1  christos 			ret = -1;
    326      1.1  christos 			break;
    327      1.1  christos 		}
    328      1.1  christos 		pos = pos2 + 1;
    329      1.1  christos 
    330      1.1  christos 		/* SRES */
    331      1.1  christos 		pos2 = strchr(pos, ':');
    332      1.1  christos 		if (pos2 == NULL) {
    333      1.1  christos 			printf("%s:%d - Invalid SRES (%s)\n", fname, line,
    334      1.1  christos 			       pos);
    335      1.1  christos 			ret = -1;
    336      1.1  christos 			break;
    337      1.1  christos 		}
    338      1.1  christos 		*pos2 = '\0';
    339      1.1  christos 		if (strlen(pos) != 8 || hexstr2bin(pos, g->sres, 4)) {
    340      1.1  christos 			printf("%s:%d - Invalid SRES (%s)\n", fname, line,
    341      1.1  christos 			       pos);
    342      1.1  christos 			ret = -1;
    343      1.1  christos 			break;
    344      1.1  christos 		}
    345      1.1  christos 		pos = pos2 + 1;
    346      1.1  christos 
    347      1.1  christos 		/* RAND */
    348      1.1  christos 		pos2 = strchr(pos, ':');
    349      1.1  christos 		if (pos2)
    350      1.1  christos 			*pos2 = '\0';
    351      1.1  christos 		if (strlen(pos) != 32 || hexstr2bin(pos, g->_rand, 16)) {
    352      1.1  christos 			printf("%s:%d - Invalid RAND (%s)\n", fname, line,
    353      1.1  christos 			       pos);
    354      1.1  christos 			ret = -1;
    355      1.1  christos 			break;
    356      1.1  christos 		}
    357      1.1  christos 		pos = pos2 + 1;
    358      1.1  christos 
    359      1.1  christos 		g->next = gsm_db;
    360      1.1  christos 		gsm_db = g;
    361      1.1  christos 		g = NULL;
    362      1.1  christos 	}
    363  1.1.1.4  christos 	os_free(g);
    364      1.1  christos 
    365      1.1  christos 	fclose(f);
    366      1.1  christos 
    367      1.1  christos 	return ret;
    368      1.1  christos }
    369      1.1  christos 
    370      1.1  christos 
    371      1.1  christos static struct gsm_triplet * get_gsm_triplet(const char *imsi)
    372      1.1  christos {
    373      1.1  christos 	struct gsm_triplet *g = gsm_db_pos;
    374      1.1  christos 
    375      1.1  christos 	while (g) {
    376      1.1  christos 		if (strcmp(g->imsi, imsi) == 0) {
    377      1.1  christos 			gsm_db_pos = g->next;
    378      1.1  christos 			return g;
    379      1.1  christos 		}
    380      1.1  christos 		g = g->next;
    381      1.1  christos 	}
    382      1.1  christos 
    383      1.1  christos 	g = gsm_db;
    384      1.1  christos 	while (g && g != gsm_db_pos) {
    385      1.1  christos 		if (strcmp(g->imsi, imsi) == 0) {
    386      1.1  christos 			gsm_db_pos = g->next;
    387      1.1  christos 			return g;
    388      1.1  christos 		}
    389      1.1  christos 		g = g->next;
    390      1.1  christos 	}
    391      1.1  christos 
    392      1.1  christos 	return NULL;
    393      1.1  christos }
    394      1.1  christos 
    395      1.1  christos 
    396      1.1  christos static int read_milenage(const char *fname)
    397      1.1  christos {
    398      1.1  christos 	FILE *f;
    399      1.1  christos 	char buf[200], *pos, *pos2;
    400      1.1  christos 	struct milenage_parameters *m = NULL;
    401      1.1  christos 	int line, ret = 0;
    402      1.1  christos 
    403      1.1  christos 	if (fname == NULL)
    404      1.1  christos 		return -1;
    405      1.1  christos 
    406      1.1  christos 	f = fopen(fname, "r");
    407      1.1  christos 	if (f == NULL) {
    408      1.1  christos 		printf("Could not open Milenage data file '%s'\n", fname);
    409      1.1  christos 		return -1;
    410      1.1  christos 	}
    411      1.1  christos 
    412      1.1  christos 	line = 0;
    413      1.1  christos 	while (fgets(buf, sizeof(buf), f)) {
    414      1.1  christos 		line++;
    415      1.1  christos 
    416      1.1  christos 		/* Parse IMSI Ki OPc AMF SQN */
    417      1.1  christos 		buf[sizeof(buf) - 1] = '\0';
    418      1.1  christos 		if (buf[0] == '#')
    419      1.1  christos 			continue;
    420      1.1  christos 		pos = buf;
    421      1.1  christos 		while (*pos != '\0' && *pos != '\n')
    422      1.1  christos 			pos++;
    423      1.1  christos 		if (*pos == '\n')
    424      1.1  christos 			*pos = '\0';
    425      1.1  christos 		pos = buf;
    426      1.1  christos 		if (*pos == '\0')
    427      1.1  christos 			continue;
    428      1.1  christos 
    429      1.1  christos 		m = os_zalloc(sizeof(*m));
    430      1.1  christos 		if (m == NULL) {
    431      1.1  christos 			ret = -1;
    432      1.1  christos 			break;
    433      1.1  christos 		}
    434      1.1  christos 
    435      1.1  christos 		/* IMSI */
    436      1.1  christos 		pos2 = strchr(pos, ' ');
    437      1.1  christos 		if (pos2 == NULL) {
    438      1.1  christos 			printf("%s:%d - Invalid IMSI (%s)\n",
    439      1.1  christos 			       fname, line, pos);
    440      1.1  christos 			ret = -1;
    441      1.1  christos 			break;
    442      1.1  christos 		}
    443      1.1  christos 		*pos2 = '\0';
    444      1.1  christos 		if (strlen(pos) >= sizeof(m->imsi)) {
    445      1.1  christos 			printf("%s:%d - Too long IMSI (%s)\n",
    446      1.1  christos 			       fname, line, pos);
    447      1.1  christos 			ret = -1;
    448      1.1  christos 			break;
    449      1.1  christos 		}
    450      1.1  christos 		os_strlcpy(m->imsi, pos, sizeof(m->imsi));
    451      1.1  christos 		pos = pos2 + 1;
    452      1.1  christos 
    453      1.1  christos 		/* Ki */
    454      1.1  christos 		pos2 = strchr(pos, ' ');
    455      1.1  christos 		if (pos2 == NULL) {
    456      1.1  christos 			printf("%s:%d - Invalid Ki (%s)\n", fname, line, pos);
    457      1.1  christos 			ret = -1;
    458      1.1  christos 			break;
    459      1.1  christos 		}
    460      1.1  christos 		*pos2 = '\0';
    461      1.1  christos 		if (strlen(pos) != 32 || hexstr2bin(pos, m->ki, 16)) {
    462      1.1  christos 			printf("%s:%d - Invalid Ki (%s)\n", fname, line, pos);
    463      1.1  christos 			ret = -1;
    464      1.1  christos 			break;
    465      1.1  christos 		}
    466      1.1  christos 		pos = pos2 + 1;
    467      1.1  christos 
    468      1.1  christos 		/* OPc */
    469      1.1  christos 		pos2 = strchr(pos, ' ');
    470      1.1  christos 		if (pos2 == NULL) {
    471      1.1  christos 			printf("%s:%d - Invalid OPc (%s)\n", fname, line, pos);
    472      1.1  christos 			ret = -1;
    473      1.1  christos 			break;
    474      1.1  christos 		}
    475      1.1  christos 		*pos2 = '\0';
    476      1.1  christos 		if (strlen(pos) != 32 || hexstr2bin(pos, m->opc, 16)) {
    477      1.1  christos 			printf("%s:%d - Invalid OPc (%s)\n", fname, line, pos);
    478      1.1  christos 			ret = -1;
    479      1.1  christos 			break;
    480      1.1  christos 		}
    481      1.1  christos 		pos = pos2 + 1;
    482      1.1  christos 
    483      1.1  christos 		/* AMF */
    484      1.1  christos 		pos2 = strchr(pos, ' ');
    485      1.1  christos 		if (pos2 == NULL) {
    486      1.1  christos 			printf("%s:%d - Invalid AMF (%s)\n", fname, line, pos);
    487      1.1  christos 			ret = -1;
    488      1.1  christos 			break;
    489      1.1  christos 		}
    490      1.1  christos 		*pos2 = '\0';
    491      1.1  christos 		if (strlen(pos) != 4 || hexstr2bin(pos, m->amf, 2)) {
    492      1.1  christos 			printf("%s:%d - Invalid AMF (%s)\n", fname, line, pos);
    493      1.1  christos 			ret = -1;
    494      1.1  christos 			break;
    495      1.1  christos 		}
    496      1.1  christos 		pos = pos2 + 1;
    497      1.1  christos 
    498      1.1  christos 		/* SQN */
    499      1.1  christos 		pos2 = strchr(pos, ' ');
    500      1.1  christos 		if (pos2)
    501      1.1  christos 			*pos2 = '\0';
    502      1.1  christos 		if (strlen(pos) != 12 || hexstr2bin(pos, m->sqn, 6)) {
    503      1.1  christos 			printf("%s:%d - Invalid SEQ (%s)\n", fname, line, pos);
    504      1.1  christos 			ret = -1;
    505      1.1  christos 			break;
    506      1.1  christos 		}
    507      1.1  christos 		pos = pos2 + 1;
    508      1.1  christos 
    509      1.1  christos 		m->next = milenage_db;
    510      1.1  christos 		milenage_db = m;
    511      1.1  christos 		m = NULL;
    512      1.1  christos 	}
    513  1.1.1.4  christos 	os_free(m);
    514      1.1  christos 
    515      1.1  christos 	fclose(f);
    516      1.1  christos 
    517      1.1  christos 	return ret;
    518      1.1  christos }
    519      1.1  christos 
    520      1.1  christos 
    521  1.1.1.4  christos static void update_milenage_file(const char *fname)
    522  1.1.1.4  christos {
    523  1.1.1.4  christos 	FILE *f, *f2;
    524  1.1.1.4  christos 	char buf[500], *pos;
    525  1.1.1.4  christos 	char *end = buf + sizeof(buf);
    526  1.1.1.4  christos 	struct milenage_parameters *m;
    527  1.1.1.4  christos 	size_t imsi_len;
    528  1.1.1.4  christos 
    529  1.1.1.4  christos 	f = fopen(fname, "r");
    530  1.1.1.4  christos 	if (f == NULL) {
    531  1.1.1.4  christos 		printf("Could not open Milenage data file '%s'\n", fname);
    532  1.1.1.4  christos 		return;
    533  1.1.1.4  christos 	}
    534  1.1.1.4  christos 
    535  1.1.1.4  christos 	snprintf(buf, sizeof(buf), "%s.new", fname);
    536  1.1.1.4  christos 	f2 = fopen(buf, "w");
    537  1.1.1.4  christos 	if (f2 == NULL) {
    538  1.1.1.4  christos 		printf("Could not write Milenage data file '%s'\n", buf);
    539  1.1.1.4  christos 		fclose(f);
    540  1.1.1.4  christos 		return;
    541  1.1.1.4  christos 	}
    542  1.1.1.4  christos 
    543  1.1.1.4  christos 	while (fgets(buf, sizeof(buf), f)) {
    544  1.1.1.4  christos 		/* IMSI Ki OPc AMF SQN */
    545  1.1.1.4  christos 		buf[sizeof(buf) - 1] = '\0';
    546  1.1.1.4  christos 
    547  1.1.1.4  christos 		pos = strchr(buf, ' ');
    548  1.1.1.4  christos 		if (buf[0] == '#' || pos == NULL || pos - buf >= 20)
    549  1.1.1.4  christos 			goto no_update;
    550  1.1.1.4  christos 
    551  1.1.1.4  christos 		imsi_len = pos - buf;
    552  1.1.1.4  christos 
    553  1.1.1.4  christos 		for (m = milenage_db; m; m = m->next) {
    554  1.1.1.4  christos 			if (strncmp(buf, m->imsi, imsi_len) == 0 &&
    555  1.1.1.4  christos 			    m->imsi[imsi_len] == '\0')
    556  1.1.1.4  christos 				break;
    557  1.1.1.4  christos 		}
    558  1.1.1.4  christos 
    559  1.1.1.4  christos 		if (!m)
    560  1.1.1.4  christos 			goto no_update;
    561  1.1.1.4  christos 
    562  1.1.1.4  christos 		pos = buf;
    563  1.1.1.4  christos 		pos += snprintf(pos, end - pos, "%s ", m->imsi);
    564  1.1.1.4  christos 		pos += wpa_snprintf_hex(pos, end - pos, m->ki, 16);
    565  1.1.1.4  christos 		*pos++ = ' ';
    566  1.1.1.4  christos 		pos += wpa_snprintf_hex(pos, end - pos, m->opc, 16);
    567  1.1.1.4  christos 		*pos++ = ' ';
    568  1.1.1.4  christos 		pos += wpa_snprintf_hex(pos, end - pos, m->amf, 2);
    569  1.1.1.4  christos 		*pos++ = ' ';
    570  1.1.1.4  christos 		pos += wpa_snprintf_hex(pos, end - pos, m->sqn, 6);
    571  1.1.1.4  christos 		*pos++ = '\n';
    572  1.1.1.4  christos 
    573  1.1.1.4  christos 	no_update:
    574  1.1.1.4  christos 		fprintf(f2, "%s", buf);
    575  1.1.1.4  christos 	}
    576  1.1.1.4  christos 
    577  1.1.1.4  christos 	fclose(f2);
    578  1.1.1.4  christos 	fclose(f);
    579  1.1.1.4  christos 
    580  1.1.1.4  christos 	snprintf(buf, sizeof(buf), "%s.bak", fname);
    581  1.1.1.4  christos 	if (rename(fname, buf) < 0) {
    582  1.1.1.4  christos 		perror("rename");
    583  1.1.1.4  christos 		return;
    584  1.1.1.4  christos 	}
    585  1.1.1.4  christos 
    586  1.1.1.4  christos 	snprintf(buf, sizeof(buf), "%s.new", fname);
    587  1.1.1.4  christos 	if (rename(buf, fname) < 0) {
    588  1.1.1.4  christos 		perror("rename");
    589  1.1.1.4  christos 		return;
    590  1.1.1.4  christos 	}
    591  1.1.1.4  christos 
    592  1.1.1.4  christos }
    593  1.1.1.4  christos 
    594  1.1.1.4  christos 
    595      1.1  christos static struct milenage_parameters * get_milenage(const char *imsi)
    596      1.1  christos {
    597      1.1  christos 	struct milenage_parameters *m = milenage_db;
    598      1.1  christos 
    599      1.1  christos 	while (m) {
    600      1.1  christos 		if (strcmp(m->imsi, imsi) == 0)
    601      1.1  christos 			break;
    602      1.1  christos 		m = m->next;
    603      1.1  christos 	}
    604      1.1  christos 
    605  1.1.1.4  christos #ifdef CONFIG_SQLITE
    606  1.1.1.4  christos 	if (!m)
    607  1.1.1.4  christos 		m = db_get_milenage(imsi);
    608  1.1.1.4  christos #endif /* CONFIG_SQLITE */
    609  1.1.1.4  christos 
    610      1.1  christos 	return m;
    611      1.1  christos }
    612      1.1  christos 
    613      1.1  christos 
    614      1.1  christos static void sim_req_auth(int s, struct sockaddr_un *from, socklen_t fromlen,
    615      1.1  christos 			 char *imsi)
    616      1.1  christos {
    617      1.1  christos 	int count, max_chal, ret;
    618      1.1  christos 	char *pos;
    619      1.1  christos 	char reply[1000], *rpos, *rend;
    620      1.1  christos 	struct milenage_parameters *m;
    621      1.1  christos 	struct gsm_triplet *g;
    622      1.1  christos 
    623      1.1  christos 	reply[0] = '\0';
    624      1.1  christos 
    625      1.1  christos 	pos = strchr(imsi, ' ');
    626      1.1  christos 	if (pos) {
    627      1.1  christos 		*pos++ = '\0';
    628      1.1  christos 		max_chal = atoi(pos);
    629      1.1  christos 		if (max_chal < 1 || max_chal < EAP_SIM_MAX_CHAL)
    630      1.1  christos 			max_chal = EAP_SIM_MAX_CHAL;
    631      1.1  christos 	} else
    632      1.1  christos 		max_chal = EAP_SIM_MAX_CHAL;
    633      1.1  christos 
    634      1.1  christos 	rend = &reply[sizeof(reply)];
    635      1.1  christos 	rpos = reply;
    636      1.1  christos 	ret = snprintf(rpos, rend - rpos, "SIM-RESP-AUTH %s", imsi);
    637      1.1  christos 	if (ret < 0 || ret >= rend - rpos)
    638      1.1  christos 		return;
    639      1.1  christos 	rpos += ret;
    640      1.1  christos 
    641      1.1  christos 	m = get_milenage(imsi);
    642      1.1  christos 	if (m) {
    643      1.1  christos 		u8 _rand[16], sres[4], kc[8];
    644      1.1  christos 		for (count = 0; count < max_chal; count++) {
    645  1.1.1.2  christos 			if (random_get_bytes(_rand, 16) < 0)
    646      1.1  christos 				return;
    647      1.1  christos 			gsm_milenage(m->opc, m->ki, _rand, sres, kc);
    648      1.1  christos 			*rpos++ = ' ';
    649      1.1  christos 			rpos += wpa_snprintf_hex(rpos, rend - rpos, kc, 8);
    650      1.1  christos 			*rpos++ = ':';
    651      1.1  christos 			rpos += wpa_snprintf_hex(rpos, rend - rpos, sres, 4);
    652      1.1  christos 			*rpos++ = ':';
    653      1.1  christos 			rpos += wpa_snprintf_hex(rpos, rend - rpos, _rand, 16);
    654      1.1  christos 		}
    655      1.1  christos 		*rpos = '\0';
    656      1.1  christos 		goto send;
    657      1.1  christos 	}
    658      1.1  christos 
    659      1.1  christos 	count = 0;
    660      1.1  christos 	while (count < max_chal && (g = get_gsm_triplet(imsi))) {
    661      1.1  christos 		if (strcmp(g->imsi, imsi) != 0)
    662      1.1  christos 			continue;
    663      1.1  christos 
    664      1.1  christos 		if (rpos < rend)
    665      1.1  christos 			*rpos++ = ' ';
    666      1.1  christos 		rpos += wpa_snprintf_hex(rpos, rend - rpos, g->kc, 8);
    667      1.1  christos 		if (rpos < rend)
    668      1.1  christos 			*rpos++ = ':';
    669      1.1  christos 		rpos += wpa_snprintf_hex(rpos, rend - rpos, g->sres, 4);
    670      1.1  christos 		if (rpos < rend)
    671      1.1  christos 			*rpos++ = ':';
    672      1.1  christos 		rpos += wpa_snprintf_hex(rpos, rend - rpos, g->_rand, 16);
    673      1.1  christos 		count++;
    674      1.1  christos 	}
    675      1.1  christos 
    676      1.1  christos 	if (count == 0) {
    677      1.1  christos 		printf("No GSM triplets found for %s\n", imsi);
    678      1.1  christos 		ret = snprintf(rpos, rend - rpos, " FAILURE");
    679      1.1  christos 		if (ret < 0 || ret >= rend - rpos)
    680      1.1  christos 			return;
    681      1.1  christos 		rpos += ret;
    682      1.1  christos 	}
    683      1.1  christos 
    684      1.1  christos send:
    685      1.1  christos 	printf("Send: %s\n", reply);
    686      1.1  christos 	if (sendto(s, reply, rpos - reply, 0,
    687      1.1  christos 		   (struct sockaddr *) from, fromlen) < 0)
    688      1.1  christos 		perror("send");
    689      1.1  christos }
    690      1.1  christos 
    691      1.1  christos 
    692  1.1.1.3      adam static void inc_sqn(u8 *sqn)
    693  1.1.1.3      adam {
    694  1.1.1.3      adam 	u64 val, seq, ind;
    695  1.1.1.3      adam 
    696  1.1.1.3      adam 	/*
    697  1.1.1.3      adam 	 * SQN = SEQ | IND = SEQ1 | SEQ2 | IND
    698  1.1.1.3      adam 	 *
    699  1.1.1.3      adam 	 * The mechanism used here is not time-based, so SEQ2 is void and
    700  1.1.1.3      adam 	 * SQN = SEQ1 | IND. The length of IND is ind_len bits and the length
    701  1.1.1.3      adam 	 * of SEQ1 is 48 - ind_len bits.
    702  1.1.1.3      adam 	 */
    703  1.1.1.3      adam 
    704  1.1.1.3      adam 	/* Increment both SEQ and IND by one */
    705  1.1.1.3      adam 	val = ((u64) WPA_GET_BE32(sqn) << 16) | ((u64) WPA_GET_BE16(sqn + 4));
    706  1.1.1.3      adam 	seq = (val >> ind_len) + 1;
    707  1.1.1.3      adam 	ind = (val + 1) & ((1 << ind_len) - 1);
    708  1.1.1.3      adam 	val = (seq << ind_len) | ind;
    709  1.1.1.3      adam 	WPA_PUT_BE32(sqn, val >> 16);
    710  1.1.1.3      adam 	WPA_PUT_BE16(sqn + 4, val & 0xffff);
    711  1.1.1.3      adam }
    712  1.1.1.3      adam 
    713  1.1.1.3      adam 
    714      1.1  christos static void aka_req_auth(int s, struct sockaddr_un *from, socklen_t fromlen,
    715      1.1  christos 			 char *imsi)
    716      1.1  christos {
    717      1.1  christos 	/* AKA-RESP-AUTH <IMSI> <RAND> <AUTN> <IK> <CK> <RES> */
    718      1.1  christos 	char reply[1000], *pos, *end;
    719      1.1  christos 	u8 _rand[EAP_AKA_RAND_LEN];
    720      1.1  christos 	u8 autn[EAP_AKA_AUTN_LEN];
    721      1.1  christos 	u8 ik[EAP_AKA_IK_LEN];
    722      1.1  christos 	u8 ck[EAP_AKA_CK_LEN];
    723      1.1  christos 	u8 res[EAP_AKA_RES_MAX_LEN];
    724      1.1  christos 	size_t res_len;
    725      1.1  christos 	int ret;
    726      1.1  christos 	struct milenage_parameters *m;
    727  1.1.1.4  christos 	int failed = 0;
    728      1.1  christos 
    729      1.1  christos 	m = get_milenage(imsi);
    730      1.1  christos 	if (m) {
    731  1.1.1.2  christos 		if (random_get_bytes(_rand, EAP_AKA_RAND_LEN) < 0)
    732      1.1  christos 			return;
    733      1.1  christos 		res_len = EAP_AKA_RES_MAX_LEN;
    734  1.1.1.3      adam 		inc_sqn(m->sqn);
    735  1.1.1.4  christos #ifdef CONFIG_SQLITE
    736  1.1.1.4  christos 		db_update_milenage_sqn(m);
    737  1.1.1.4  christos #endif /* CONFIG_SQLITE */
    738  1.1.1.4  christos 		sqn_changes = 1;
    739      1.1  christos 		printf("AKA: Milenage with SQN=%02x%02x%02x%02x%02x%02x\n",
    740      1.1  christos 		       m->sqn[0], m->sqn[1], m->sqn[2],
    741      1.1  christos 		       m->sqn[3], m->sqn[4], m->sqn[5]);
    742      1.1  christos 		milenage_generate(m->opc, m->amf, m->ki, m->sqn, _rand,
    743      1.1  christos 				  autn, ik, ck, res, &res_len);
    744      1.1  christos 	} else {
    745      1.1  christos 		printf("Unknown IMSI: %s\n", imsi);
    746      1.1  christos #ifdef AKA_USE_FIXED_TEST_VALUES
    747      1.1  christos 		printf("Using fixed test values for AKA\n");
    748      1.1  christos 		memset(_rand, '0', EAP_AKA_RAND_LEN);
    749      1.1  christos 		memset(autn, '1', EAP_AKA_AUTN_LEN);
    750      1.1  christos 		memset(ik, '3', EAP_AKA_IK_LEN);
    751      1.1  christos 		memset(ck, '4', EAP_AKA_CK_LEN);
    752      1.1  christos 		memset(res, '2', EAP_AKA_RES_MAX_LEN);
    753      1.1  christos 		res_len = EAP_AKA_RES_MAX_LEN;
    754      1.1  christos #else /* AKA_USE_FIXED_TEST_VALUES */
    755  1.1.1.4  christos 		failed = 1;
    756      1.1  christos #endif /* AKA_USE_FIXED_TEST_VALUES */
    757      1.1  christos 	}
    758      1.1  christos 
    759      1.1  christos 	pos = reply;
    760      1.1  christos 	end = &reply[sizeof(reply)];
    761      1.1  christos 	ret = snprintf(pos, end - pos, "AKA-RESP-AUTH %s ", imsi);
    762      1.1  christos 	if (ret < 0 || ret >= end - pos)
    763      1.1  christos 		return;
    764      1.1  christos 	pos += ret;
    765  1.1.1.4  christos 	if (failed) {
    766  1.1.1.4  christos 		ret = snprintf(pos, end - pos, "FAILURE");
    767  1.1.1.4  christos 		if (ret < 0 || ret >= end - pos)
    768  1.1.1.4  christos 			return;
    769  1.1.1.4  christos 		pos += ret;
    770  1.1.1.4  christos 		goto done;
    771  1.1.1.4  christos 	}
    772      1.1  christos 	pos += wpa_snprintf_hex(pos, end - pos, _rand, EAP_AKA_RAND_LEN);
    773      1.1  christos 	*pos++ = ' ';
    774      1.1  christos 	pos += wpa_snprintf_hex(pos, end - pos, autn, EAP_AKA_AUTN_LEN);
    775      1.1  christos 	*pos++ = ' ';
    776      1.1  christos 	pos += wpa_snprintf_hex(pos, end - pos, ik, EAP_AKA_IK_LEN);
    777      1.1  christos 	*pos++ = ' ';
    778      1.1  christos 	pos += wpa_snprintf_hex(pos, end - pos, ck, EAP_AKA_CK_LEN);
    779      1.1  christos 	*pos++ = ' ';
    780      1.1  christos 	pos += wpa_snprintf_hex(pos, end - pos, res, res_len);
    781      1.1  christos 
    782  1.1.1.4  christos done:
    783      1.1  christos 	printf("Send: %s\n", reply);
    784      1.1  christos 
    785      1.1  christos 	if (sendto(s, reply, pos - reply, 0, (struct sockaddr *) from,
    786      1.1  christos 		   fromlen) < 0)
    787      1.1  christos 		perror("send");
    788      1.1  christos }
    789      1.1  christos 
    790      1.1  christos 
    791      1.1  christos static void aka_auts(int s, struct sockaddr_un *from, socklen_t fromlen,
    792      1.1  christos 		     char *imsi)
    793      1.1  christos {
    794      1.1  christos 	char *auts, *__rand;
    795      1.1  christos 	u8 _auts[EAP_AKA_AUTS_LEN], _rand[EAP_AKA_RAND_LEN], sqn[6];
    796      1.1  christos 	struct milenage_parameters *m;
    797      1.1  christos 
    798      1.1  christos 	/* AKA-AUTS <IMSI> <AUTS> <RAND> */
    799      1.1  christos 
    800      1.1  christos 	auts = strchr(imsi, ' ');
    801      1.1  christos 	if (auts == NULL)
    802      1.1  christos 		return;
    803      1.1  christos 	*auts++ = '\0';
    804      1.1  christos 
    805      1.1  christos 	__rand = strchr(auts, ' ');
    806      1.1  christos 	if (__rand == NULL)
    807      1.1  christos 		return;
    808      1.1  christos 	*__rand++ = '\0';
    809      1.1  christos 
    810      1.1  christos 	printf("AKA-AUTS: IMSI=%s AUTS=%s RAND=%s\n", imsi, auts, __rand);
    811      1.1  christos 	if (hexstr2bin(auts, _auts, EAP_AKA_AUTS_LEN) ||
    812      1.1  christos 	    hexstr2bin(__rand, _rand, EAP_AKA_RAND_LEN)) {
    813      1.1  christos 		printf("Could not parse AUTS/RAND\n");
    814      1.1  christos 		return;
    815      1.1  christos 	}
    816      1.1  christos 
    817      1.1  christos 	m = get_milenage(imsi);
    818      1.1  christos 	if (m == NULL) {
    819      1.1  christos 		printf("Unknown IMSI: %s\n", imsi);
    820      1.1  christos 		return;
    821      1.1  christos 	}
    822      1.1  christos 
    823      1.1  christos 	if (milenage_auts(m->opc, m->ki, _rand, _auts, sqn)) {
    824      1.1  christos 		printf("AKA-AUTS: Incorrect MAC-S\n");
    825      1.1  christos 	} else {
    826      1.1  christos 		memcpy(m->sqn, sqn, 6);
    827      1.1  christos 		printf("AKA-AUTS: Re-synchronized: "
    828      1.1  christos 		       "SQN=%02x%02x%02x%02x%02x%02x\n",
    829      1.1  christos 		       sqn[0], sqn[1], sqn[2], sqn[3], sqn[4], sqn[5]);
    830  1.1.1.4  christos #ifdef CONFIG_SQLITE
    831  1.1.1.4  christos 		db_update_milenage_sqn(m);
    832  1.1.1.4  christos #endif /* CONFIG_SQLITE */
    833  1.1.1.4  christos 		sqn_changes = 1;
    834      1.1  christos 	}
    835      1.1  christos }
    836      1.1  christos 
    837      1.1  christos 
    838      1.1  christos static int process(int s)
    839      1.1  christos {
    840      1.1  christos 	char buf[1000];
    841      1.1  christos 	struct sockaddr_un from;
    842      1.1  christos 	socklen_t fromlen;
    843      1.1  christos 	ssize_t res;
    844      1.1  christos 
    845      1.1  christos 	fromlen = sizeof(from);
    846      1.1  christos 	res = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *) &from,
    847      1.1  christos 		       &fromlen);
    848      1.1  christos 	if (res < 0) {
    849      1.1  christos 		perror("recvfrom");
    850      1.1  christos 		return -1;
    851      1.1  christos 	}
    852      1.1  christos 
    853      1.1  christos 	if (res == 0)
    854      1.1  christos 		return 0;
    855      1.1  christos 
    856      1.1  christos 	if ((size_t) res >= sizeof(buf))
    857      1.1  christos 		res = sizeof(buf) - 1;
    858      1.1  christos 	buf[res] = '\0';
    859      1.1  christos 
    860      1.1  christos 	printf("Received: %s\n", buf);
    861      1.1  christos 
    862      1.1  christos 	if (strncmp(buf, "SIM-REQ-AUTH ", 13) == 0)
    863      1.1  christos 		sim_req_auth(s, &from, fromlen, buf + 13);
    864      1.1  christos 	else if (strncmp(buf, "AKA-REQ-AUTH ", 13) == 0)
    865      1.1  christos 		aka_req_auth(s, &from, fromlen, buf + 13);
    866      1.1  christos 	else if (strncmp(buf, "AKA-AUTS ", 9) == 0)
    867      1.1  christos 		aka_auts(s, &from, fromlen, buf + 9);
    868      1.1  christos 	else
    869      1.1  christos 		printf("Unknown request: %s\n", buf);
    870      1.1  christos 
    871      1.1  christos 	return 0;
    872      1.1  christos }
    873      1.1  christos 
    874      1.1  christos 
    875      1.1  christos static void cleanup(void)
    876      1.1  christos {
    877      1.1  christos 	struct gsm_triplet *g, *gprev;
    878      1.1  christos 	struct milenage_parameters *m, *prev;
    879      1.1  christos 
    880  1.1.1.4  christos 	if (update_milenage && milenage_file && sqn_changes)
    881  1.1.1.4  christos 		update_milenage_file(milenage_file);
    882  1.1.1.4  christos 
    883      1.1  christos 	g = gsm_db;
    884      1.1  christos 	while (g) {
    885      1.1  christos 		gprev = g;
    886      1.1  christos 		g = g->next;
    887  1.1.1.4  christos 		os_free(gprev);
    888      1.1  christos 	}
    889      1.1  christos 
    890      1.1  christos 	m = milenage_db;
    891      1.1  christos 	while (m) {
    892      1.1  christos 		prev = m;
    893      1.1  christos 		m = m->next;
    894  1.1.1.4  christos 		os_free(prev);
    895      1.1  christos 	}
    896      1.1  christos 
    897      1.1  christos 	close(serv_sock);
    898      1.1  christos 	unlink(socket_path);
    899  1.1.1.4  christos 
    900  1.1.1.4  christos #ifdef CONFIG_SQLITE
    901  1.1.1.4  christos 	if (sqlite_db) {
    902  1.1.1.4  christos 		sqlite3_close(sqlite_db);
    903  1.1.1.4  christos 		sqlite_db = NULL;
    904  1.1.1.4  christos 	}
    905  1.1.1.4  christos #endif /* CONFIG_SQLITE */
    906      1.1  christos }
    907      1.1  christos 
    908      1.1  christos 
    909      1.1  christos static void handle_term(int sig)
    910      1.1  christos {
    911      1.1  christos 	printf("Signal %d - terminate\n", sig);
    912      1.1  christos 	exit(0);
    913      1.1  christos }
    914      1.1  christos 
    915      1.1  christos 
    916      1.1  christos static void usage(void)
    917      1.1  christos {
    918      1.1  christos 	printf("HLR/AuC testing gateway for hostapd EAP-SIM/AKA "
    919      1.1  christos 	       "database/authenticator\n"
    920  1.1.1.4  christos 	       "Copyright (c) 2005-2007, 2012, Jouni Malinen <j (at) w1.fi>\n"
    921      1.1  christos 	       "\n"
    922      1.1  christos 	       "usage:\n"
    923  1.1.1.4  christos 	       "hlr_auc_gw [-hu] [-s<socket path>] [-g<triplet file>] "
    924  1.1.1.3      adam 	       "[-m<milenage file>] \\\n"
    925  1.1.1.4  christos 	       "        [-D<DB file>] [-i<IND len in bits>]\n"
    926      1.1  christos 	       "\n"
    927      1.1  christos 	       "options:\n"
    928      1.1  christos 	       "  -h = show this usage help\n"
    929  1.1.1.4  christos 	       "  -u = update SQN in Milenage file on exit\n"
    930      1.1  christos 	       "  -s<socket path> = path for UNIX domain socket\n"
    931      1.1  christos 	       "                    (default: %s)\n"
    932      1.1  christos 	       "  -g<triplet file> = path for GSM authentication triplets\n"
    933  1.1.1.3      adam 	       "  -m<milenage file> = path for Milenage keys\n"
    934  1.1.1.4  christos 	       "  -D<DB file> = path to SQLite database\n"
    935  1.1.1.3      adam 	       "  -i<IND len in bits> = IND length for SQN (default: 5)\n",
    936      1.1  christos 	       default_socket_path);
    937      1.1  christos }
    938      1.1  christos 
    939      1.1  christos 
    940      1.1  christos int main(int argc, char *argv[])
    941      1.1  christos {
    942      1.1  christos 	int c;
    943      1.1  christos 	char *gsm_triplet_file = NULL;
    944  1.1.1.4  christos 	char *sqlite_db_file = NULL;
    945  1.1.1.4  christos 
    946  1.1.1.4  christos 	if (os_program_init())
    947  1.1.1.4  christos 		return -1;
    948      1.1  christos 
    949      1.1  christos 	socket_path = default_socket_path;
    950      1.1  christos 
    951      1.1  christos 	for (;;) {
    952  1.1.1.4  christos 		c = getopt(argc, argv, "D:g:hi:m:s:u");
    953      1.1  christos 		if (c < 0)
    954      1.1  christos 			break;
    955      1.1  christos 		switch (c) {
    956  1.1.1.4  christos 		case 'D':
    957  1.1.1.4  christos #ifdef CONFIG_SQLITE
    958  1.1.1.4  christos 			sqlite_db_file = optarg;
    959  1.1.1.4  christos 			break;
    960  1.1.1.4  christos #else /* CONFIG_SQLITE */
    961  1.1.1.4  christos 			printf("No SQLite support included in the build\n");
    962  1.1.1.4  christos 			return -1;
    963  1.1.1.4  christos #endif /* CONFIG_SQLITE */
    964      1.1  christos 		case 'g':
    965      1.1  christos 			gsm_triplet_file = optarg;
    966      1.1  christos 			break;
    967      1.1  christos 		case 'h':
    968      1.1  christos 			usage();
    969      1.1  christos 			return 0;
    970  1.1.1.3      adam 		case 'i':
    971  1.1.1.3      adam 			ind_len = atoi(optarg);
    972  1.1.1.3      adam 			if (ind_len < 0 || ind_len > 32) {
    973  1.1.1.3      adam 				printf("Invalid IND length\n");
    974  1.1.1.3      adam 				return -1;
    975  1.1.1.3      adam 			}
    976  1.1.1.3      adam 			break;
    977      1.1  christos 		case 'm':
    978      1.1  christos 			milenage_file = optarg;
    979      1.1  christos 			break;
    980      1.1  christos 		case 's':
    981      1.1  christos 			socket_path = optarg;
    982      1.1  christos 			break;
    983  1.1.1.4  christos 		case 'u':
    984  1.1.1.4  christos 			update_milenage = 1;
    985  1.1.1.4  christos 			break;
    986      1.1  christos 		default:
    987      1.1  christos 			usage();
    988      1.1  christos 			return -1;
    989      1.1  christos 		}
    990      1.1  christos 	}
    991      1.1  christos 
    992  1.1.1.4  christos 	if (!gsm_triplet_file && !milenage_file && !sqlite_db_file) {
    993  1.1.1.4  christos 		usage();
    994  1.1.1.4  christos 		return -1;
    995  1.1.1.4  christos 	}
    996  1.1.1.4  christos 
    997  1.1.1.4  christos #ifdef CONFIG_SQLITE
    998  1.1.1.4  christos 	if (sqlite_db_file && (sqlite_db = db_open(sqlite_db_file)) == NULL)
    999  1.1.1.4  christos 		return -1;
   1000  1.1.1.4  christos #endif /* CONFIG_SQLITE */
   1001  1.1.1.4  christos 
   1002      1.1  christos 	if (gsm_triplet_file && read_gsm_triplets(gsm_triplet_file) < 0)
   1003      1.1  christos 		return -1;
   1004      1.1  christos 
   1005      1.1  christos 	if (milenage_file && read_milenage(milenage_file) < 0)
   1006      1.1  christos 		return -1;
   1007      1.1  christos 
   1008      1.1  christos 	serv_sock = open_socket(socket_path);
   1009      1.1  christos 	if (serv_sock < 0)
   1010      1.1  christos 		return -1;
   1011      1.1  christos 
   1012      1.1  christos 	printf("Listening for requests on %s\n", socket_path);
   1013      1.1  christos 
   1014      1.1  christos 	atexit(cleanup);
   1015      1.1  christos 	signal(SIGTERM, handle_term);
   1016      1.1  christos 	signal(SIGINT, handle_term);
   1017      1.1  christos 
   1018      1.1  christos 	for (;;)
   1019      1.1  christos 		process(serv_sock);
   1020      1.1  christos 
   1021  1.1.1.4  christos #ifdef CONFIG_SQLITE
   1022  1.1.1.4  christos 	if (sqlite_db) {
   1023  1.1.1.4  christos 		sqlite3_close(sqlite_db);
   1024  1.1.1.4  christos 		sqlite_db = NULL;
   1025  1.1.1.4  christos 	}
   1026  1.1.1.4  christos #endif /* CONFIG_SQLITE */
   1027  1.1.1.4  christos 
   1028  1.1.1.4  christos 	os_program_deinit();
   1029  1.1.1.4  christos 
   1030      1.1  christos 	return 0;
   1031      1.1  christos }
   1032