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 #include "crypto/milenage.h"
49 #include "crypto/random.h"
51 static const char *default_socket_path = "/tmp/hlr_auc_gw.sock";
52 static const char *socket_path;
53 static int serv_sock = -1;
54 static char *milenage_file = NULL;
55 static int update_milenage = 0;
56 static int sqn_changes = 0;
57 static int ind_len = 5;
61 struct gsm_triplet *next;
68 static struct gsm_triplet *gsm_db = NULL, *gsm_db_pos = NULL;
70 /* OPc and AMF parameters for Milenage (Example algorithms for AKA). */
71 struct milenage_parameters {
72 struct milenage_parameters *next;
80 static struct milenage_parameters *milenage_db = NULL;
82 #define EAP_SIM_MAX_CHAL 3
84 #define EAP_AKA_RAND_LEN 16
85 #define EAP_AKA_AUTN_LEN 16
86 #define EAP_AKA_AUTS_LEN 14
87 #define EAP_AKA_RES_MAX_LEN 16
88 #define EAP_AKA_IK_LEN 16
89 #define EAP_AKA_CK_LEN 16
92 static int open_socket(const char *path)
94 struct sockaddr_un addr;
97 s = socket(PF_UNIX, SOCK_DGRAM, 0);
99 perror("socket(PF_UNIX)");
103 memset(&addr, 0, sizeof(addr));
104 addr.sun_family = AF_UNIX;
105 os_strlcpy(addr.sun_path, path, sizeof(addr.sun_path));
106 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
107 perror("hlr-auc-gw: bind(PF_UNIX)");
116 static int read_gsm_triplets(const char *fname)
119 char buf[200], *pos, *pos2;
120 struct gsm_triplet *g = NULL;
126 f = fopen(fname, "r");
128 printf("Could not open GSM tripler data file '%s'\n", fname);
133 while (fgets(buf, sizeof(buf), f)) {
136 /* Parse IMSI:Kc:SRES:RAND */
137 buf[sizeof(buf) - 1] = '\0';
141 while (*pos != '\0' && *pos != '\n')
149 g = os_zalloc(sizeof(*g));
156 pos2 = strchr(pos, ':');
158 printf("%s:%d - Invalid IMSI (%s)\n",
164 if (strlen(pos) >= sizeof(g->imsi)) {
165 printf("%s:%d - Too long IMSI (%s)\n",
170 os_strlcpy(g->imsi, pos, sizeof(g->imsi));
174 pos2 = strchr(pos, ':');
176 printf("%s:%d - Invalid Kc (%s)\n", fname, line, pos);
181 if (strlen(pos) != 16 || hexstr2bin(pos, g->kc, 8)) {
182 printf("%s:%d - Invalid Kc (%s)\n", fname, line, pos);
189 pos2 = strchr(pos, ':');
191 printf("%s:%d - Invalid SRES (%s)\n", fname, line,
197 if (strlen(pos) != 8 || hexstr2bin(pos, g->sres, 4)) {
198 printf("%s:%d - Invalid SRES (%s)\n", fname, line,
206 pos2 = strchr(pos, ':');
209 if (strlen(pos) != 32 || hexstr2bin(pos, g->_rand, 16)) {
210 printf("%s:%d - Invalid RAND (%s)\n", fname, line,
229 static struct gsm_triplet * get_gsm_triplet(const char *imsi)
231 struct gsm_triplet *g = gsm_db_pos;
234 if (strcmp(g->imsi, imsi) == 0) {
235 gsm_db_pos = g->next;
242 while (g && g != gsm_db_pos) {
243 if (strcmp(g->imsi, imsi) == 0) {
244 gsm_db_pos = g->next;
254 static int read_milenage(const char *fname)
257 char buf[200], *pos, *pos2;
258 struct milenage_parameters *m = NULL;
264 f = fopen(fname, "r");
266 printf("Could not open Milenage data file '%s'\n", fname);
271 while (fgets(buf, sizeof(buf), f)) {
274 /* Parse IMSI Ki OPc AMF SQN */
275 buf[sizeof(buf) - 1] = '\0';
279 while (*pos != '\0' && *pos != '\n')
287 m = os_zalloc(sizeof(*m));
294 pos2 = strchr(pos, ' ');
296 printf("%s:%d - Invalid IMSI (%s)\n",
302 if (strlen(pos) >= sizeof(m->imsi)) {
303 printf("%s:%d - Too long IMSI (%s)\n",
308 os_strlcpy(m->imsi, pos, sizeof(m->imsi));
312 pos2 = strchr(pos, ' ');
314 printf("%s:%d - Invalid Ki (%s)\n", fname, line, pos);
319 if (strlen(pos) != 32 || hexstr2bin(pos, m->ki, 16)) {
320 printf("%s:%d - Invalid Ki (%s)\n", fname, line, pos);
327 pos2 = strchr(pos, ' ');
329 printf("%s:%d - Invalid OPc (%s)\n", fname, line, pos);
334 if (strlen(pos) != 32 || hexstr2bin(pos, m->opc, 16)) {
335 printf("%s:%d - Invalid OPc (%s)\n", fname, line, pos);
342 pos2 = strchr(pos, ' ');
344 printf("%s:%d - Invalid AMF (%s)\n", fname, line, pos);
349 if (strlen(pos) != 4 || hexstr2bin(pos, m->amf, 2)) {
350 printf("%s:%d - Invalid AMF (%s)\n", fname, line, pos);
357 pos2 = strchr(pos, ' ');
360 if (strlen(pos) != 12 || hexstr2bin(pos, m->sqn, 6)) {
361 printf("%s:%d - Invalid SEQ (%s)\n", fname, line, pos);
367 m->next = milenage_db;
379 static void update_milenage_file(const char *fname)
383 char *end = buf + sizeof(buf);
384 struct milenage_parameters *m;
387 f = fopen(fname, "r");
389 printf("Could not open Milenage data file '%s'\n", fname);
393 snprintf(buf, sizeof(buf), "%s.new", fname);
394 f2 = fopen(buf, "w");
396 printf("Could not write Milenage data file '%s'\n", buf);
401 while (fgets(buf, sizeof(buf), f)) {
402 /* IMSI Ki OPc AMF SQN */
403 buf[sizeof(buf) - 1] = '\0';
405 pos = strchr(buf, ' ');
406 if (buf[0] == '#' || pos == NULL || pos - buf >= 20)
409 imsi_len = pos - buf;
411 for (m = milenage_db; m; m = m->next) {
412 if (strncmp(buf, m->imsi, imsi_len) == 0 &&
413 m->imsi[imsi_len] == '\0')
421 pos += snprintf(pos, end - pos, "%s ", m->imsi);
422 pos += wpa_snprintf_hex(pos, end - pos, m->ki, 16);
424 pos += wpa_snprintf_hex(pos, end - pos, m->opc, 16);
426 pos += wpa_snprintf_hex(pos, end - pos, m->amf, 2);
428 pos += wpa_snprintf_hex(pos, end - pos, m->sqn, 6);
432 fprintf(f2, "%s", buf);
438 snprintf(buf, sizeof(buf), "%s.bak", fname);
439 if (rename(fname, buf) < 0) {
444 snprintf(buf, sizeof(buf), "%s.new", fname);
445 if (rename(buf, fname) < 0) {
453 static struct milenage_parameters * get_milenage(const char *imsi)
455 struct milenage_parameters *m = milenage_db;
458 if (strcmp(m->imsi, imsi) == 0)
467 static void sim_req_auth(int s, struct sockaddr_un *from, socklen_t fromlen,
470 int count, max_chal, ret;
472 char reply[1000], *rpos, *rend;
473 struct milenage_parameters *m;
474 struct gsm_triplet *g;
478 pos = strchr(imsi, ' ');
481 max_chal = atoi(pos);
482 if (max_chal < 1 || max_chal < EAP_SIM_MAX_CHAL)
483 max_chal = EAP_SIM_MAX_CHAL;
485 max_chal = EAP_SIM_MAX_CHAL;
487 rend = &reply[sizeof(reply)];
489 ret = snprintf(rpos, rend - rpos, "SIM-RESP-AUTH %s", imsi);
490 if (ret < 0 || ret >= rend - rpos)
494 m = get_milenage(imsi);
496 u8 _rand[16], sres[4], kc[8];
497 for (count = 0; count < max_chal; count++) {
498 if (random_get_bytes(_rand, 16) < 0)
500 gsm_milenage(m->opc, m->ki, _rand, sres, kc);
502 rpos += wpa_snprintf_hex(rpos, rend - rpos, kc, 8);
504 rpos += wpa_snprintf_hex(rpos, rend - rpos, sres, 4);
506 rpos += wpa_snprintf_hex(rpos, rend - rpos, _rand, 16);
513 while (count < max_chal && (g = get_gsm_triplet(imsi))) {
514 if (strcmp(g->imsi, imsi) != 0)
519 rpos += wpa_snprintf_hex(rpos, rend - rpos, g->kc, 8);
522 rpos += wpa_snprintf_hex(rpos, rend - rpos, g->sres, 4);
525 rpos += wpa_snprintf_hex(rpos, rend - rpos, g->_rand, 16);
530 printf("No GSM triplets found for %s\n", imsi);
531 ret = snprintf(rpos, rend - rpos, " FAILURE");
532 if (ret < 0 || ret >= rend - rpos)
538 printf("Send: %s\n", reply);
539 if (sendto(s, reply, rpos - reply, 0,
540 (struct sockaddr *) from, fromlen) < 0)
545 static void inc_sqn(u8 *sqn)
550 * SQN = SEQ | IND = SEQ1 | SEQ2 | IND
552 * The mechanism used here is not time-based, so SEQ2 is void and
553 * SQN = SEQ1 | IND. The length of IND is ind_len bits and the length
554 * of SEQ1 is 48 - ind_len bits.
557 /* Increment both SEQ and IND by one */
558 val = ((u64) WPA_GET_BE32(sqn) << 16) | ((u64) WPA_GET_BE16(sqn + 4));
559 seq = (val >> ind_len) + 1;
560 ind = (val + 1) & ((1 << ind_len) - 1);
561 val = (seq << ind_len) | ind;
562 WPA_PUT_BE32(sqn, val >> 16);
563 WPA_PUT_BE16(sqn + 4, val & 0xffff);
567 static void aka_req_auth(int s, struct sockaddr_un *from, socklen_t fromlen,
570 /* AKA-RESP-AUTH <IMSI> <RAND> <AUTN> <IK> <CK> <RES> */
571 char reply[1000], *pos, *end;
572 u8 _rand[EAP_AKA_RAND_LEN];
573 u8 autn[EAP_AKA_AUTN_LEN];
574 u8 ik[EAP_AKA_IK_LEN];
575 u8 ck[EAP_AKA_CK_LEN];
576 u8 res[EAP_AKA_RES_MAX_LEN];
579 struct milenage_parameters *m;
582 m = get_milenage(imsi);
584 if (random_get_bytes(_rand, EAP_AKA_RAND_LEN) < 0)
586 res_len = EAP_AKA_RES_MAX_LEN;
589 printf("AKA: Milenage with SQN=%02x%02x%02x%02x%02x%02x\n",
590 m->sqn[0], m->sqn[1], m->sqn[2],
591 m->sqn[3], m->sqn[4], m->sqn[5]);
592 milenage_generate(m->opc, m->amf, m->ki, m->sqn, _rand,
593 autn, ik, ck, res, &res_len);
595 printf("Unknown IMSI: %s\n", imsi);
596 #ifdef AKA_USE_FIXED_TEST_VALUES
597 printf("Using fixed test values for AKA\n");
598 memset(_rand, '0', EAP_AKA_RAND_LEN);
599 memset(autn, '1', EAP_AKA_AUTN_LEN);
600 memset(ik, '3', EAP_AKA_IK_LEN);
601 memset(ck, '4', EAP_AKA_CK_LEN);
602 memset(res, '2', EAP_AKA_RES_MAX_LEN);
603 res_len = EAP_AKA_RES_MAX_LEN;
604 #else /* AKA_USE_FIXED_TEST_VALUES */
606 #endif /* AKA_USE_FIXED_TEST_VALUES */
610 end = &reply[sizeof(reply)];
611 ret = snprintf(pos, end - pos, "AKA-RESP-AUTH %s ", imsi);
612 if (ret < 0 || ret >= end - pos)
616 ret = snprintf(pos, end - pos, "FAILURE");
617 if (ret < 0 || ret >= end - pos)
622 pos += wpa_snprintf_hex(pos, end - pos, _rand, EAP_AKA_RAND_LEN);
624 pos += wpa_snprintf_hex(pos, end - pos, autn, EAP_AKA_AUTN_LEN);
626 pos += wpa_snprintf_hex(pos, end - pos, ik, EAP_AKA_IK_LEN);
628 pos += wpa_snprintf_hex(pos, end - pos, ck, EAP_AKA_CK_LEN);
630 pos += wpa_snprintf_hex(pos, end - pos, res, res_len);
633 printf("Send: %s\n", reply);
635 if (sendto(s, reply, pos - reply, 0, (struct sockaddr *) from,
641 static void aka_auts(int s, struct sockaddr_un *from, socklen_t fromlen,
645 u8 _auts[EAP_AKA_AUTS_LEN], _rand[EAP_AKA_RAND_LEN], sqn[6];
646 struct milenage_parameters *m;
648 /* AKA-AUTS <IMSI> <AUTS> <RAND> */
650 auts = strchr(imsi, ' ');
655 __rand = strchr(auts, ' ');
660 printf("AKA-AUTS: IMSI=%s AUTS=%s RAND=%s\n", imsi, auts, __rand);
661 if (hexstr2bin(auts, _auts, EAP_AKA_AUTS_LEN) ||
662 hexstr2bin(__rand, _rand, EAP_AKA_RAND_LEN)) {
663 printf("Could not parse AUTS/RAND\n");
667 m = get_milenage(imsi);
669 printf("Unknown IMSI: %s\n", imsi);
673 if (milenage_auts(m->opc, m->ki, _rand, _auts, sqn)) {
674 printf("AKA-AUTS: Incorrect MAC-S\n");
676 memcpy(m->sqn, sqn, 6);
677 printf("AKA-AUTS: Re-synchronized: "
678 "SQN=%02x%02x%02x%02x%02x%02x\n",
679 sqn[0], sqn[1], sqn[2], sqn[3], sqn[4], sqn[5]);
685 static int process(int s)
688 struct sockaddr_un from;
692 fromlen = sizeof(from);
693 res = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *) &from,
703 if ((size_t) res >= sizeof(buf))
704 res = sizeof(buf) - 1;
707 printf("Received: %s\n", buf);
709 if (strncmp(buf, "SIM-REQ-AUTH ", 13) == 0)
710 sim_req_auth(s, &from, fromlen, buf + 13);
711 else if (strncmp(buf, "AKA-REQ-AUTH ", 13) == 0)
712 aka_req_auth(s, &from, fromlen, buf + 13);
713 else if (strncmp(buf, "AKA-AUTS ", 9) == 0)
714 aka_auts(s, &from, fromlen, buf + 9);
716 printf("Unknown request: %s\n", buf);
722 static void cleanup(void)
724 struct gsm_triplet *g, *gprev;
725 struct milenage_parameters *m, *prev;
727 if (update_milenage && milenage_file && sqn_changes)
728 update_milenage_file(milenage_file);
749 static void handle_term(int sig)
751 printf("Signal %d - terminate\n", sig);
756 static void usage(void)
758 printf("HLR/AuC testing gateway for hostapd EAP-SIM/AKA "
759 "database/authenticator\n"
760 "Copyright (c) 2005-2007, 2012, Jouni Malinen <j@w1.fi>\n"
763 "hlr_auc_gw [-hu] [-s<socket path>] [-g<triplet file>] "
764 "[-m<milenage file>] \\\n"
765 " [-i<IND len in bits>]\n"
768 " -h = show this usage help\n"
769 " -u = update SQN in Milenage file on exit\n"
770 " -s<socket path> = path for UNIX domain socket\n"
772 " -g<triplet file> = path for GSM authentication triplets\n"
773 " -m<milenage file> = path for Milenage keys\n"
774 " -i<IND len in bits> = IND length for SQN (default: 5)\n",
775 default_socket_path);
779 int main(int argc, char *argv[])
782 char *gsm_triplet_file = NULL;
784 if (os_program_init())
787 socket_path = default_socket_path;
790 c = getopt(argc, argv, "g:hi:m:s:u");
795 gsm_triplet_file = optarg;
801 ind_len = atoi(optarg);
802 if (ind_len < 0 || ind_len > 32) {
803 printf("Invalid IND length\n");
808 milenage_file = optarg;
811 socket_path = optarg;
822 if (gsm_triplet_file && read_gsm_triplets(gsm_triplet_file) < 0)
825 if (milenage_file && read_milenage(milenage_file) < 0)
828 serv_sock = open_socket(socket_path);
832 printf("Listening for requests on %s\n", socket_path);
835 signal(SIGTERM, handle_term);
836 signal(SIGINT, handle_term);