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 testing.
44 #include "crypto/milenage.h"
45 #include "crypto/random.h"
47 static const char *default_socket_path = "/tmp/hlr_auc_gw.sock";
48 static const char *socket_path;
49 static int serv_sock = -1;
50 static char *milenage_file = NULL;
51 static int update_milenage = 0;
52 static int sqn_changes = 0;
56 struct gsm_triplet *next;
63 static struct gsm_triplet *gsm_db = NULL, *gsm_db_pos = NULL;
65 /* OPc and AMF parameters for Milenage (Example algorithms for AKA). */
66 struct milenage_parameters {
67 struct milenage_parameters *next;
75 static struct milenage_parameters *milenage_db = NULL;
77 #define EAP_SIM_MAX_CHAL 3
79 #define EAP_AKA_RAND_LEN 16
80 #define EAP_AKA_AUTN_LEN 16
81 #define EAP_AKA_AUTS_LEN 14
82 #define EAP_AKA_RES_MAX_LEN 16
83 #define EAP_AKA_IK_LEN 16
84 #define EAP_AKA_CK_LEN 16
87 static int open_socket(const char *path)
89 struct sockaddr_un addr;
92 s = socket(PF_UNIX, SOCK_DGRAM, 0);
94 perror("socket(PF_UNIX)");
98 memset(&addr, 0, sizeof(addr));
99 addr.sun_family = AF_UNIX;
100 os_strlcpy(addr.sun_path, path, sizeof(addr.sun_path));
101 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
102 perror("hlr-auc-gw: bind(PF_UNIX)");
111 static int read_gsm_triplets(const char *fname)
114 char buf[200], *pos, *pos2;
115 struct gsm_triplet *g = NULL;
121 f = fopen(fname, "r");
123 printf("Could not open GSM tripler data file '%s'\n", fname);
128 while (fgets(buf, sizeof(buf), f)) {
131 /* Parse IMSI:Kc:SRES:RAND */
132 buf[sizeof(buf) - 1] = '\0';
136 while (*pos != '\0' && *pos != '\n')
144 g = os_zalloc(sizeof(*g));
151 pos2 = strchr(pos, ':');
153 printf("%s:%d - Invalid IMSI (%s)\n",
159 if (strlen(pos) >= sizeof(g->imsi)) {
160 printf("%s:%d - Too long IMSI (%s)\n",
165 os_strlcpy(g->imsi, pos, sizeof(g->imsi));
169 pos2 = strchr(pos, ':');
171 printf("%s:%d - Invalid Kc (%s)\n", fname, line, pos);
176 if (strlen(pos) != 16 || hexstr2bin(pos, g->kc, 8)) {
177 printf("%s:%d - Invalid Kc (%s)\n", fname, line, pos);
184 pos2 = strchr(pos, ':');
186 printf("%s:%d - Invalid SRES (%s)\n", fname, line,
192 if (strlen(pos) != 8 || hexstr2bin(pos, g->sres, 4)) {
193 printf("%s:%d - Invalid SRES (%s)\n", fname, line,
201 pos2 = strchr(pos, ':');
204 if (strlen(pos) != 32 || hexstr2bin(pos, g->_rand, 16)) {
205 printf("%s:%d - Invalid RAND (%s)\n", fname, line,
224 static struct gsm_triplet * get_gsm_triplet(const char *imsi)
226 struct gsm_triplet *g = gsm_db_pos;
229 if (strcmp(g->imsi, imsi) == 0) {
230 gsm_db_pos = g->next;
237 while (g && g != gsm_db_pos) {
238 if (strcmp(g->imsi, imsi) == 0) {
239 gsm_db_pos = g->next;
249 static int read_milenage(const char *fname)
252 char buf[200], *pos, *pos2;
253 struct milenage_parameters *m = NULL;
259 f = fopen(fname, "r");
261 printf("Could not open Milenage data file '%s'\n", fname);
266 while (fgets(buf, sizeof(buf), f)) {
269 /* Parse IMSI Ki OPc AMF SQN */
270 buf[sizeof(buf) - 1] = '\0';
274 while (*pos != '\0' && *pos != '\n')
282 m = os_zalloc(sizeof(*m));
289 pos2 = strchr(pos, ' ');
291 printf("%s:%d - Invalid IMSI (%s)\n",
297 if (strlen(pos) >= sizeof(m->imsi)) {
298 printf("%s:%d - Too long IMSI (%s)\n",
303 os_strlcpy(m->imsi, pos, sizeof(m->imsi));
307 pos2 = strchr(pos, ' ');
309 printf("%s:%d - Invalid Ki (%s)\n", fname, line, pos);
314 if (strlen(pos) != 32 || hexstr2bin(pos, m->ki, 16)) {
315 printf("%s:%d - Invalid Ki (%s)\n", fname, line, pos);
322 pos2 = strchr(pos, ' ');
324 printf("%s:%d - Invalid OPc (%s)\n", fname, line, pos);
329 if (strlen(pos) != 32 || hexstr2bin(pos, m->opc, 16)) {
330 printf("%s:%d - Invalid OPc (%s)\n", fname, line, pos);
337 pos2 = strchr(pos, ' ');
339 printf("%s:%d - Invalid AMF (%s)\n", fname, line, pos);
344 if (strlen(pos) != 4 || hexstr2bin(pos, m->amf, 2)) {
345 printf("%s:%d - Invalid AMF (%s)\n", fname, line, pos);
352 pos2 = strchr(pos, ' ');
355 if (strlen(pos) != 12 || hexstr2bin(pos, m->sqn, 6)) {
356 printf("%s:%d - Invalid SEQ (%s)\n", fname, line, pos);
362 m->next = milenage_db;
374 static void update_milenage_file(const char *fname)
378 char *end = buf + sizeof(buf);
379 struct milenage_parameters *m;
382 f = fopen(fname, "r");
384 printf("Could not open Milenage data file '%s'\n", fname);
388 snprintf(buf, sizeof(buf), "%s.new", fname);
389 f2 = fopen(buf, "w");
391 printf("Could not write Milenage data file '%s'\n", buf);
396 while (fgets(buf, sizeof(buf), f)) {
397 /* IMSI Ki OPc AMF SQN */
398 buf[sizeof(buf) - 1] = '\0';
400 pos = strchr(buf, ' ');
401 if (buf[0] == '#' || pos == NULL || pos - buf >= 20)
404 imsi_len = pos - buf;
406 for (m = milenage_db; m; m = m->next) {
407 if (strncmp(buf, m->imsi, imsi_len) == 0 &&
408 m->imsi[imsi_len] == '\0')
416 pos += snprintf(pos, end - pos, "%s ", m->imsi);
417 pos += wpa_snprintf_hex(pos, end - pos, m->ki, 16);
419 pos += wpa_snprintf_hex(pos, end - pos, m->opc, 16);
421 pos += wpa_snprintf_hex(pos, end - pos, m->amf, 2);
423 pos += wpa_snprintf_hex(pos, end - pos, m->sqn, 6);
427 fprintf(f2, "%s", buf);
433 snprintf(buf, sizeof(buf), "%s.bak", fname);
434 if (rename(fname, buf) < 0) {
439 snprintf(buf, sizeof(buf), "%s.new", fname);
440 if (rename(buf, fname) < 0) {
448 static struct milenage_parameters * get_milenage(const char *imsi)
450 struct milenage_parameters *m = milenage_db;
453 if (strcmp(m->imsi, imsi) == 0)
462 static void sim_req_auth(int s, struct sockaddr_un *from, socklen_t fromlen,
465 int count, max_chal, ret;
467 char reply[1000], *rpos, *rend;
468 struct milenage_parameters *m;
469 struct gsm_triplet *g;
473 pos = strchr(imsi, ' ');
476 max_chal = atoi(pos);
477 if (max_chal < 1 || max_chal < EAP_SIM_MAX_CHAL)
478 max_chal = EAP_SIM_MAX_CHAL;
480 max_chal = EAP_SIM_MAX_CHAL;
482 rend = &reply[sizeof(reply)];
484 ret = snprintf(rpos, rend - rpos, "SIM-RESP-AUTH %s", imsi);
485 if (ret < 0 || ret >= rend - rpos)
489 m = get_milenage(imsi);
491 u8 _rand[16], sres[4], kc[8];
492 for (count = 0; count < max_chal; count++) {
493 if (random_get_bytes(_rand, 16) < 0)
495 gsm_milenage(m->opc, m->ki, _rand, sres, kc);
497 rpos += wpa_snprintf_hex(rpos, rend - rpos, kc, 8);
499 rpos += wpa_snprintf_hex(rpos, rend - rpos, sres, 4);
501 rpos += wpa_snprintf_hex(rpos, rend - rpos, _rand, 16);
508 while (count < max_chal && (g = get_gsm_triplet(imsi))) {
509 if (strcmp(g->imsi, imsi) != 0)
514 rpos += wpa_snprintf_hex(rpos, rend - rpos, g->kc, 8);
517 rpos += wpa_snprintf_hex(rpos, rend - rpos, g->sres, 4);
520 rpos += wpa_snprintf_hex(rpos, rend - rpos, g->_rand, 16);
525 printf("No GSM triplets found for %s\n", imsi);
526 ret = snprintf(rpos, rend - rpos, " FAILURE");
527 if (ret < 0 || ret >= rend - rpos)
533 printf("Send: %s\n", reply);
534 if (sendto(s, reply, rpos - reply, 0,
535 (struct sockaddr *) from, fromlen) < 0)
540 static void aka_req_auth(int s, struct sockaddr_un *from, socklen_t fromlen,
543 /* AKA-RESP-AUTH <IMSI> <RAND> <AUTN> <IK> <CK> <RES> */
544 char reply[1000], *pos, *end;
545 u8 _rand[EAP_AKA_RAND_LEN];
546 u8 autn[EAP_AKA_AUTN_LEN];
547 u8 ik[EAP_AKA_IK_LEN];
548 u8 ck[EAP_AKA_CK_LEN];
549 u8 res[EAP_AKA_RES_MAX_LEN];
552 struct milenage_parameters *m;
554 m = get_milenage(imsi);
556 if (random_get_bytes(_rand, EAP_AKA_RAND_LEN) < 0)
558 res_len = EAP_AKA_RES_MAX_LEN;
559 inc_byte_array(m->sqn, 6);
561 printf("AKA: Milenage with SQN=%02x%02x%02x%02x%02x%02x\n",
562 m->sqn[0], m->sqn[1], m->sqn[2],
563 m->sqn[3], m->sqn[4], m->sqn[5]);
564 milenage_generate(m->opc, m->amf, m->ki, m->sqn, _rand,
565 autn, ik, ck, res, &res_len);
567 printf("Unknown IMSI: %s\n", imsi);
568 #ifdef AKA_USE_FIXED_TEST_VALUES
569 printf("Using fixed test values for AKA\n");
570 memset(_rand, '0', EAP_AKA_RAND_LEN);
571 memset(autn, '1', EAP_AKA_AUTN_LEN);
572 memset(ik, '3', EAP_AKA_IK_LEN);
573 memset(ck, '4', EAP_AKA_CK_LEN);
574 memset(res, '2', EAP_AKA_RES_MAX_LEN);
575 res_len = EAP_AKA_RES_MAX_LEN;
576 #else /* AKA_USE_FIXED_TEST_VALUES */
578 #endif /* AKA_USE_FIXED_TEST_VALUES */
582 end = &reply[sizeof(reply)];
583 ret = snprintf(pos, end - pos, "AKA-RESP-AUTH %s ", imsi);
584 if (ret < 0 || ret >= end - pos)
587 pos += wpa_snprintf_hex(pos, end - pos, _rand, EAP_AKA_RAND_LEN);
589 pos += wpa_snprintf_hex(pos, end - pos, autn, EAP_AKA_AUTN_LEN);
591 pos += wpa_snprintf_hex(pos, end - pos, ik, EAP_AKA_IK_LEN);
593 pos += wpa_snprintf_hex(pos, end - pos, ck, EAP_AKA_CK_LEN);
595 pos += wpa_snprintf_hex(pos, end - pos, res, res_len);
597 printf("Send: %s\n", reply);
599 if (sendto(s, reply, pos - reply, 0, (struct sockaddr *) from,
605 static void aka_auts(int s, struct sockaddr_un *from, socklen_t fromlen,
609 u8 _auts[EAP_AKA_AUTS_LEN], _rand[EAP_AKA_RAND_LEN], sqn[6];
610 struct milenage_parameters *m;
612 /* AKA-AUTS <IMSI> <AUTS> <RAND> */
614 auts = strchr(imsi, ' ');
619 __rand = strchr(auts, ' ');
624 printf("AKA-AUTS: IMSI=%s AUTS=%s RAND=%s\n", imsi, auts, __rand);
625 if (hexstr2bin(auts, _auts, EAP_AKA_AUTS_LEN) ||
626 hexstr2bin(__rand, _rand, EAP_AKA_RAND_LEN)) {
627 printf("Could not parse AUTS/RAND\n");
631 m = get_milenage(imsi);
633 printf("Unknown IMSI: %s\n", imsi);
637 if (milenage_auts(m->opc, m->ki, _rand, _auts, sqn)) {
638 printf("AKA-AUTS: Incorrect MAC-S\n");
640 memcpy(m->sqn, sqn, 6);
641 printf("AKA-AUTS: Re-synchronized: "
642 "SQN=%02x%02x%02x%02x%02x%02x\n",
643 sqn[0], sqn[1], sqn[2], sqn[3], sqn[4], sqn[5]);
649 static int process(int s)
652 struct sockaddr_un from;
656 fromlen = sizeof(from);
657 res = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *) &from,
667 if ((size_t) res >= sizeof(buf))
668 res = sizeof(buf) - 1;
671 printf("Received: %s\n", buf);
673 if (strncmp(buf, "SIM-REQ-AUTH ", 13) == 0)
674 sim_req_auth(s, &from, fromlen, buf + 13);
675 else if (strncmp(buf, "AKA-REQ-AUTH ", 13) == 0)
676 aka_req_auth(s, &from, fromlen, buf + 13);
677 else if (strncmp(buf, "AKA-AUTS ", 9) == 0)
678 aka_auts(s, &from, fromlen, buf + 9);
680 printf("Unknown request: %s\n", buf);
686 static void cleanup(void)
688 struct gsm_triplet *g, *gprev;
689 struct milenage_parameters *m, *prev;
691 if (update_milenage && milenage_file && sqn_changes)
692 update_milenage_file(milenage_file);
713 static void handle_term(int sig)
715 printf("Signal %d - terminate\n", sig);
720 static void usage(void)
722 printf("HLR/AuC testing gateway for hostapd EAP-SIM/AKA "
723 "database/authenticator\n"
724 "Copyright (c) 2005-2007, 2012, Jouni Malinen <j@w1.fi>\n"
727 "hlr_auc_gw [-hu] [-s<socket path>] [-g<triplet file>] "
728 "[-m<milenage file>]\n"
731 " -h = show this usage help\n"
732 " -u = update SQN in Milenage file on exit\n"
733 " -s<socket path> = path for UNIX domain socket\n"
735 " -g<triplet file> = path for GSM authentication triplets\n"
736 " -m<milenage file> = path for Milenage keys\n",
737 default_socket_path);
741 int main(int argc, char *argv[])
744 char *gsm_triplet_file = NULL;
746 if (os_program_init())
749 socket_path = default_socket_path;
752 c = getopt(argc, argv, "g:hm:s:u");
757 gsm_triplet_file = optarg;
763 milenage_file = optarg;
766 socket_path = optarg;
777 if (gsm_triplet_file && read_gsm_triplets(gsm_triplet_file) < 0)
780 if (milenage_file && read_milenage(milenage_file) < 0)
783 serv_sock = open_socket(socket_path);
787 printf("Listening for requests on %s\n", socket_path);
790 signal(SIGTERM, handle_term);
791 signal(SIGINT, handle_term);