]>
git.ipfire.org Git - thirdparty/hostap.git/blob - hostapd/hlr_auc_gw.c
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
;
581 m
= get_milenage(imsi
);
583 if (random_get_bytes(_rand
, EAP_AKA_RAND_LEN
) < 0)
585 res_len
= EAP_AKA_RES_MAX_LEN
;
588 printf("AKA: Milenage with SQN=%02x%02x%02x%02x%02x%02x\n",
589 m
->sqn
[0], m
->sqn
[1], m
->sqn
[2],
590 m
->sqn
[3], m
->sqn
[4], m
->sqn
[5]);
591 milenage_generate(m
->opc
, m
->amf
, m
->ki
, m
->sqn
, _rand
,
592 autn
, ik
, ck
, res
, &res_len
);
594 printf("Unknown IMSI: %s\n", imsi
);
595 #ifdef AKA_USE_FIXED_TEST_VALUES
596 printf("Using fixed test values for AKA\n");
597 memset(_rand
, '0', EAP_AKA_RAND_LEN
);
598 memset(autn
, '1', EAP_AKA_AUTN_LEN
);
599 memset(ik
, '3', EAP_AKA_IK_LEN
);
600 memset(ck
, '4', EAP_AKA_CK_LEN
);
601 memset(res
, '2', EAP_AKA_RES_MAX_LEN
);
602 res_len
= EAP_AKA_RES_MAX_LEN
;
603 #else /* AKA_USE_FIXED_TEST_VALUES */
605 #endif /* AKA_USE_FIXED_TEST_VALUES */
609 end
= &reply
[sizeof(reply
)];
610 ret
= snprintf(pos
, end
- pos
, "AKA-RESP-AUTH %s ", imsi
);
611 if (ret
< 0 || ret
>= end
- pos
)
614 pos
+= wpa_snprintf_hex(pos
, end
- pos
, _rand
, EAP_AKA_RAND_LEN
);
616 pos
+= wpa_snprintf_hex(pos
, end
- pos
, autn
, EAP_AKA_AUTN_LEN
);
618 pos
+= wpa_snprintf_hex(pos
, end
- pos
, ik
, EAP_AKA_IK_LEN
);
620 pos
+= wpa_snprintf_hex(pos
, end
- pos
, ck
, EAP_AKA_CK_LEN
);
622 pos
+= wpa_snprintf_hex(pos
, end
- pos
, res
, res_len
);
624 printf("Send: %s\n", reply
);
626 if (sendto(s
, reply
, pos
- reply
, 0, (struct sockaddr
*) from
,
632 static void aka_auts(int s
, struct sockaddr_un
*from
, socklen_t fromlen
,
636 u8 _auts
[EAP_AKA_AUTS_LEN
], _rand
[EAP_AKA_RAND_LEN
], sqn
[6];
637 struct milenage_parameters
*m
;
639 /* AKA-AUTS <IMSI> <AUTS> <RAND> */
641 auts
= strchr(imsi
, ' ');
646 __rand
= strchr(auts
, ' ');
651 printf("AKA-AUTS: IMSI=%s AUTS=%s RAND=%s\n", imsi
, auts
, __rand
);
652 if (hexstr2bin(auts
, _auts
, EAP_AKA_AUTS_LEN
) ||
653 hexstr2bin(__rand
, _rand
, EAP_AKA_RAND_LEN
)) {
654 printf("Could not parse AUTS/RAND\n");
658 m
= get_milenage(imsi
);
660 printf("Unknown IMSI: %s\n", imsi
);
664 if (milenage_auts(m
->opc
, m
->ki
, _rand
, _auts
, sqn
)) {
665 printf("AKA-AUTS: Incorrect MAC-S\n");
667 memcpy(m
->sqn
, sqn
, 6);
668 printf("AKA-AUTS: Re-synchronized: "
669 "SQN=%02x%02x%02x%02x%02x%02x\n",
670 sqn
[0], sqn
[1], sqn
[2], sqn
[3], sqn
[4], sqn
[5]);
676 static int process(int s
)
679 struct sockaddr_un from
;
683 fromlen
= sizeof(from
);
684 res
= recvfrom(s
, buf
, sizeof(buf
), 0, (struct sockaddr
*) &from
,
694 if ((size_t) res
>= sizeof(buf
))
695 res
= sizeof(buf
) - 1;
698 printf("Received: %s\n", buf
);
700 if (strncmp(buf
, "SIM-REQ-AUTH ", 13) == 0)
701 sim_req_auth(s
, &from
, fromlen
, buf
+ 13);
702 else if (strncmp(buf
, "AKA-REQ-AUTH ", 13) == 0)
703 aka_req_auth(s
, &from
, fromlen
, buf
+ 13);
704 else if (strncmp(buf
, "AKA-AUTS ", 9) == 0)
705 aka_auts(s
, &from
, fromlen
, buf
+ 9);
707 printf("Unknown request: %s\n", buf
);
713 static void cleanup(void)
715 struct gsm_triplet
*g
, *gprev
;
716 struct milenage_parameters
*m
, *prev
;
718 if (update_milenage
&& milenage_file
&& sqn_changes
)
719 update_milenage_file(milenage_file
);
740 static void handle_term(int sig
)
742 printf("Signal %d - terminate\n", sig
);
747 static void usage(void)
749 printf("HLR/AuC testing gateway for hostapd EAP-SIM/AKA "
750 "database/authenticator\n"
751 "Copyright (c) 2005-2007, 2012, Jouni Malinen <j@w1.fi>\n"
754 "hlr_auc_gw [-hu] [-s<socket path>] [-g<triplet file>] "
755 "[-m<milenage file>] \\\n"
756 " [-i<IND len in bits>]\n"
759 " -h = show this usage help\n"
760 " -u = update SQN in Milenage file on exit\n"
761 " -s<socket path> = path for UNIX domain socket\n"
763 " -g<triplet file> = path for GSM authentication triplets\n"
764 " -m<milenage file> = path for Milenage keys\n"
765 " -i<IND len in bits> = IND length for SQN (default: 5)\n",
766 default_socket_path
);
770 int main(int argc
, char *argv
[])
773 char *gsm_triplet_file
= NULL
;
775 if (os_program_init())
778 socket_path
= default_socket_path
;
781 c
= getopt(argc
, argv
, "g:hi:m:s:u");
786 gsm_triplet_file
= optarg
;
792 ind_len
= atoi(optarg
);
793 if (ind_len
< 0 || ind_len
> 32) {
794 printf("Invalid IND length\n");
799 milenage_file
= optarg
;
802 socket_path
= optarg
;
813 if (gsm_triplet_file
&& read_gsm_triplets(gsm_triplet_file
) < 0)
816 if (milenage_file
&& read_milenage(milenage_file
) < 0)
819 serv_sock
= open_socket(socket_path
);
823 printf("Listening for requests on %s\n", socket_path
);
826 signal(SIGTERM
, handle_term
);
827 signal(SIGINT
, handle_term
);