hlr_auc_gw.c revision 1.1.1.3 1 1.1 christos /*
2 1.1 christos * HLR/AuC testing gateway for hostapd EAP-SIM/AKA database/authenticator
3 1.1 christos * Copyright (c) 2005-2007, Jouni Malinen <j (at) w1.fi>
4 1.1 christos *
5 1.1 christos * This program is free software; you can redistribute it and/or modify
6 1.1 christos * it under the terms of the GNU General Public License version 2 as
7 1.1 christos * published by the Free Software Foundation.
8 1.1 christos *
9 1.1 christos * Alternatively, this software may be distributed under the terms of BSD
10 1.1 christos * license.
11 1.1 christos *
12 1.1 christos * See README and COPYING for more details.
13 1.1 christos *
14 1.1 christos * This is an example implementation of the EAP-SIM/AKA database/authentication
15 1.1 christos * gateway interface to HLR/AuC. It is expected to be replaced with an
16 1.1 christos * implementation of SS7 gateway to GSM/UMTS authentication center (HLR/AuC) or
17 1.1 christos * a local implementation of SIM triplet and AKA authentication data generator.
18 1.1 christos *
19 1.1 christos * hostapd will send SIM/AKA authentication queries over a UNIX domain socket
20 1.1 christos * to and external program, e.g., this hlr_auc_gw. This interface uses simple
21 1.1 christos * text-based format:
22 1.1 christos *
23 1.1 christos * EAP-SIM / GSM triplet query/response:
24 1.1 christos * SIM-REQ-AUTH <IMSI> <max_chal>
25 1.1 christos * SIM-RESP-AUTH <IMSI> Kc1:SRES1:RAND1 Kc2:SRES2:RAND2 [Kc3:SRES3:RAND3]
26 1.1 christos * SIM-RESP-AUTH <IMSI> FAILURE
27 1.1 christos *
28 1.1 christos * EAP-AKA / UMTS query/response:
29 1.1 christos * AKA-REQ-AUTH <IMSI>
30 1.1 christos * AKA-RESP-AUTH <IMSI> <RAND> <AUTN> <IK> <CK> <RES>
31 1.1 christos * AKA-RESP-AUTH <IMSI> FAILURE
32 1.1 christos *
33 1.1 christos * EAP-AKA / UMTS AUTS (re-synchronization):
34 1.1 christos * AKA-AUTS <IMSI> <AUTS> <RAND>
35 1.1 christos *
36 1.1 christos * IMSI and max_chal are sent as an ASCII string,
37 1.1 christos * Kc/SRES/RAND/AUTN/IK/CK/RES/AUTS as hex strings.
38 1.1 christos *
39 1.1 christos * The example implementation here reads GSM authentication triplets from a
40 1.1 christos * text file in IMSI:Kc:SRES:RAND format, IMSI in ASCII, other fields as hex
41 1.1 christos * strings. This is used to simulate an HLR/AuC. As such, it is not very useful
42 1.1 christos * for real life authentication, but it is useful both as an example
43 1.1.1.3 adam * implementation and for EAP-SIM/AKA/AKA' testing.
44 1.1.1.3 adam *
45 1.1.1.3 adam * SQN generation follows the not time-based Profile 2 described in
46 1.1.1.3 adam * 3GPP TS 33.102 Annex C.3.2. The length of IND is 5 bits by default, but this
47 1.1.1.3 adam * can be changed with a command line options if needed.
48 1.1 christos */
49 1.1 christos
50 1.1 christos #include "includes.h"
51 1.1 christos #include <sys/un.h>
52 1.1 christos
53 1.1 christos #include "common.h"
54 1.1 christos #include "crypto/milenage.h"
55 1.1.1.2 christos #include "crypto/random.h"
56 1.1 christos
57 1.1 christos static const char *default_socket_path = "/tmp/hlr_auc_gw.sock";
58 1.1 christos static const char *socket_path;
59 1.1 christos static int serv_sock = -1;
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 christos };
82 1.1 christos
83 1.1 christos static struct milenage_parameters *milenage_db = NULL;
84 1.1 christos
85 1.1 christos #define EAP_SIM_MAX_CHAL 3
86 1.1 christos
87 1.1 christos #define EAP_AKA_RAND_LEN 16
88 1.1 christos #define EAP_AKA_AUTN_LEN 16
89 1.1 christos #define EAP_AKA_AUTS_LEN 14
90 1.1 christos #define EAP_AKA_RES_MAX_LEN 16
91 1.1 christos #define EAP_AKA_IK_LEN 16
92 1.1 christos #define EAP_AKA_CK_LEN 16
93 1.1 christos
94 1.1 christos
95 1.1 christos static int open_socket(const char *path)
96 1.1 christos {
97 1.1 christos struct sockaddr_un addr;
98 1.1 christos int s;
99 1.1 christos
100 1.1 christos s = socket(PF_UNIX, SOCK_DGRAM, 0);
101 1.1 christos if (s < 0) {
102 1.1 christos perror("socket(PF_UNIX)");
103 1.1 christos return -1;
104 1.1 christos }
105 1.1 christos
106 1.1 christos memset(&addr, 0, sizeof(addr));
107 1.1 christos addr.sun_family = AF_UNIX;
108 1.1 christos os_strlcpy(addr.sun_path, path, sizeof(addr.sun_path));
109 1.1 christos if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
110 1.1 christos perror("bind(PF_UNIX)");
111 1.1 christos close(s);
112 1.1 christos return -1;
113 1.1 christos }
114 1.1 christos
115 1.1 christos return s;
116 1.1 christos }
117 1.1 christos
118 1.1 christos
119 1.1 christos static int read_gsm_triplets(const char *fname)
120 1.1 christos {
121 1.1 christos FILE *f;
122 1.1 christos char buf[200], *pos, *pos2;
123 1.1 christos struct gsm_triplet *g = NULL;
124 1.1 christos int line, ret = 0;
125 1.1 christos
126 1.1 christos if (fname == NULL)
127 1.1 christos return -1;
128 1.1 christos
129 1.1 christos f = fopen(fname, "r");
130 1.1 christos if (f == NULL) {
131 1.1 christos printf("Could not open GSM tripler data file '%s'\n", fname);
132 1.1 christos return -1;
133 1.1 christos }
134 1.1 christos
135 1.1 christos line = 0;
136 1.1 christos while (fgets(buf, sizeof(buf), f)) {
137 1.1 christos line++;
138 1.1 christos
139 1.1 christos /* Parse IMSI:Kc:SRES:RAND */
140 1.1 christos buf[sizeof(buf) - 1] = '\0';
141 1.1 christos if (buf[0] == '#')
142 1.1 christos continue;
143 1.1 christos pos = buf;
144 1.1 christos while (*pos != '\0' && *pos != '\n')
145 1.1 christos pos++;
146 1.1 christos if (*pos == '\n')
147 1.1 christos *pos = '\0';
148 1.1 christos pos = buf;
149 1.1 christos if (*pos == '\0')
150 1.1 christos continue;
151 1.1 christos
152 1.1 christos g = os_zalloc(sizeof(*g));
153 1.1 christos if (g == NULL) {
154 1.1 christos ret = -1;
155 1.1 christos break;
156 1.1 christos }
157 1.1 christos
158 1.1 christos /* IMSI */
159 1.1 christos pos2 = strchr(pos, ':');
160 1.1 christos if (pos2 == NULL) {
161 1.1 christos printf("%s:%d - Invalid IMSI (%s)\n",
162 1.1 christos fname, line, pos);
163 1.1 christos ret = -1;
164 1.1 christos break;
165 1.1 christos }
166 1.1 christos *pos2 = '\0';
167 1.1 christos if (strlen(pos) >= sizeof(g->imsi)) {
168 1.1 christos printf("%s:%d - Too long IMSI (%s)\n",
169 1.1 christos fname, line, pos);
170 1.1 christos ret = -1;
171 1.1 christos break;
172 1.1 christos }
173 1.1 christos os_strlcpy(g->imsi, pos, sizeof(g->imsi));
174 1.1 christos pos = pos2 + 1;
175 1.1 christos
176 1.1 christos /* Kc */
177 1.1 christos pos2 = strchr(pos, ':');
178 1.1 christos if (pos2 == NULL) {
179 1.1 christos printf("%s:%d - Invalid Kc (%s)\n", fname, line, pos);
180 1.1 christos ret = -1;
181 1.1 christos break;
182 1.1 christos }
183 1.1 christos *pos2 = '\0';
184 1.1 christos if (strlen(pos) != 16 || hexstr2bin(pos, g->kc, 8)) {
185 1.1 christos printf("%s:%d - Invalid Kc (%s)\n", fname, line, pos);
186 1.1 christos ret = -1;
187 1.1 christos break;
188 1.1 christos }
189 1.1 christos pos = pos2 + 1;
190 1.1 christos
191 1.1 christos /* SRES */
192 1.1 christos pos2 = strchr(pos, ':');
193 1.1 christos if (pos2 == NULL) {
194 1.1 christos printf("%s:%d - Invalid SRES (%s)\n", fname, line,
195 1.1 christos pos);
196 1.1 christos ret = -1;
197 1.1 christos break;
198 1.1 christos }
199 1.1 christos *pos2 = '\0';
200 1.1 christos if (strlen(pos) != 8 || hexstr2bin(pos, g->sres, 4)) {
201 1.1 christos printf("%s:%d - Invalid SRES (%s)\n", fname, line,
202 1.1 christos pos);
203 1.1 christos ret = -1;
204 1.1 christos break;
205 1.1 christos }
206 1.1 christos pos = pos2 + 1;
207 1.1 christos
208 1.1 christos /* RAND */
209 1.1 christos pos2 = strchr(pos, ':');
210 1.1 christos if (pos2)
211 1.1 christos *pos2 = '\0';
212 1.1 christos if (strlen(pos) != 32 || hexstr2bin(pos, g->_rand, 16)) {
213 1.1 christos printf("%s:%d - Invalid RAND (%s)\n", fname, line,
214 1.1 christos pos);
215 1.1 christos ret = -1;
216 1.1 christos break;
217 1.1 christos }
218 1.1 christos pos = pos2 + 1;
219 1.1 christos
220 1.1 christos g->next = gsm_db;
221 1.1 christos gsm_db = g;
222 1.1 christos g = NULL;
223 1.1 christos }
224 1.1 christos free(g);
225 1.1 christos
226 1.1 christos fclose(f);
227 1.1 christos
228 1.1 christos return ret;
229 1.1 christos }
230 1.1 christos
231 1.1 christos
232 1.1 christos static struct gsm_triplet * get_gsm_triplet(const char *imsi)
233 1.1 christos {
234 1.1 christos struct gsm_triplet *g = gsm_db_pos;
235 1.1 christos
236 1.1 christos while (g) {
237 1.1 christos if (strcmp(g->imsi, imsi) == 0) {
238 1.1 christos gsm_db_pos = g->next;
239 1.1 christos return g;
240 1.1 christos }
241 1.1 christos g = g->next;
242 1.1 christos }
243 1.1 christos
244 1.1 christos g = gsm_db;
245 1.1 christos while (g && g != gsm_db_pos) {
246 1.1 christos if (strcmp(g->imsi, imsi) == 0) {
247 1.1 christos gsm_db_pos = g->next;
248 1.1 christos return g;
249 1.1 christos }
250 1.1 christos g = g->next;
251 1.1 christos }
252 1.1 christos
253 1.1 christos return NULL;
254 1.1 christos }
255 1.1 christos
256 1.1 christos
257 1.1 christos static int read_milenage(const char *fname)
258 1.1 christos {
259 1.1 christos FILE *f;
260 1.1 christos char buf[200], *pos, *pos2;
261 1.1 christos struct milenage_parameters *m = NULL;
262 1.1 christos int line, ret = 0;
263 1.1 christos
264 1.1 christos if (fname == NULL)
265 1.1 christos return -1;
266 1.1 christos
267 1.1 christos f = fopen(fname, "r");
268 1.1 christos if (f == NULL) {
269 1.1 christos printf("Could not open Milenage data file '%s'\n", fname);
270 1.1 christos return -1;
271 1.1 christos }
272 1.1 christos
273 1.1 christos line = 0;
274 1.1 christos while (fgets(buf, sizeof(buf), f)) {
275 1.1 christos line++;
276 1.1 christos
277 1.1 christos /* Parse IMSI Ki OPc AMF SQN */
278 1.1 christos buf[sizeof(buf) - 1] = '\0';
279 1.1 christos if (buf[0] == '#')
280 1.1 christos continue;
281 1.1 christos pos = buf;
282 1.1 christos while (*pos != '\0' && *pos != '\n')
283 1.1 christos pos++;
284 1.1 christos if (*pos == '\n')
285 1.1 christos *pos = '\0';
286 1.1 christos pos = buf;
287 1.1 christos if (*pos == '\0')
288 1.1 christos continue;
289 1.1 christos
290 1.1 christos m = os_zalloc(sizeof(*m));
291 1.1 christos if (m == NULL) {
292 1.1 christos ret = -1;
293 1.1 christos break;
294 1.1 christos }
295 1.1 christos
296 1.1 christos /* IMSI */
297 1.1 christos pos2 = strchr(pos, ' ');
298 1.1 christos if (pos2 == NULL) {
299 1.1 christos printf("%s:%d - Invalid IMSI (%s)\n",
300 1.1 christos fname, line, pos);
301 1.1 christos ret = -1;
302 1.1 christos break;
303 1.1 christos }
304 1.1 christos *pos2 = '\0';
305 1.1 christos if (strlen(pos) >= sizeof(m->imsi)) {
306 1.1 christos printf("%s:%d - Too long IMSI (%s)\n",
307 1.1 christos fname, line, pos);
308 1.1 christos ret = -1;
309 1.1 christos break;
310 1.1 christos }
311 1.1 christos os_strlcpy(m->imsi, pos, sizeof(m->imsi));
312 1.1 christos pos = pos2 + 1;
313 1.1 christos
314 1.1 christos /* Ki */
315 1.1 christos pos2 = strchr(pos, ' ');
316 1.1 christos if (pos2 == NULL) {
317 1.1 christos printf("%s:%d - Invalid Ki (%s)\n", fname, line, pos);
318 1.1 christos ret = -1;
319 1.1 christos break;
320 1.1 christos }
321 1.1 christos *pos2 = '\0';
322 1.1 christos if (strlen(pos) != 32 || hexstr2bin(pos, m->ki, 16)) {
323 1.1 christos printf("%s:%d - Invalid Ki (%s)\n", fname, line, pos);
324 1.1 christos ret = -1;
325 1.1 christos break;
326 1.1 christos }
327 1.1 christos pos = pos2 + 1;
328 1.1 christos
329 1.1 christos /* OPc */
330 1.1 christos pos2 = strchr(pos, ' ');
331 1.1 christos if (pos2 == NULL) {
332 1.1 christos printf("%s:%d - Invalid OPc (%s)\n", fname, line, pos);
333 1.1 christos ret = -1;
334 1.1 christos break;
335 1.1 christos }
336 1.1 christos *pos2 = '\0';
337 1.1 christos if (strlen(pos) != 32 || hexstr2bin(pos, m->opc, 16)) {
338 1.1 christos printf("%s:%d - Invalid OPc (%s)\n", fname, line, pos);
339 1.1 christos ret = -1;
340 1.1 christos break;
341 1.1 christos }
342 1.1 christos pos = pos2 + 1;
343 1.1 christos
344 1.1 christos /* AMF */
345 1.1 christos pos2 = strchr(pos, ' ');
346 1.1 christos if (pos2 == NULL) {
347 1.1 christos printf("%s:%d - Invalid AMF (%s)\n", fname, line, pos);
348 1.1 christos ret = -1;
349 1.1 christos break;
350 1.1 christos }
351 1.1 christos *pos2 = '\0';
352 1.1 christos if (strlen(pos) != 4 || hexstr2bin(pos, m->amf, 2)) {
353 1.1 christos printf("%s:%d - Invalid AMF (%s)\n", fname, line, 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 /* SQN */
360 1.1 christos pos2 = strchr(pos, ' ');
361 1.1 christos if (pos2)
362 1.1 christos *pos2 = '\0';
363 1.1 christos if (strlen(pos) != 12 || hexstr2bin(pos, m->sqn, 6)) {
364 1.1 christos printf("%s:%d - Invalid SEQ (%s)\n", fname, line, pos);
365 1.1 christos ret = -1;
366 1.1 christos break;
367 1.1 christos }
368 1.1 christos pos = pos2 + 1;
369 1.1 christos
370 1.1 christos m->next = milenage_db;
371 1.1 christos milenage_db = m;
372 1.1 christos m = NULL;
373 1.1 christos }
374 1.1 christos free(m);
375 1.1 christos
376 1.1 christos fclose(f);
377 1.1 christos
378 1.1 christos return ret;
379 1.1 christos }
380 1.1 christos
381 1.1 christos
382 1.1 christos static struct milenage_parameters * get_milenage(const char *imsi)
383 1.1 christos {
384 1.1 christos struct milenage_parameters *m = milenage_db;
385 1.1 christos
386 1.1 christos while (m) {
387 1.1 christos if (strcmp(m->imsi, imsi) == 0)
388 1.1 christos break;
389 1.1 christos m = m->next;
390 1.1 christos }
391 1.1 christos
392 1.1 christos return m;
393 1.1 christos }
394 1.1 christos
395 1.1 christos
396 1.1 christos static void sim_req_auth(int s, struct sockaddr_un *from, socklen_t fromlen,
397 1.1 christos char *imsi)
398 1.1 christos {
399 1.1 christos int count, max_chal, ret;
400 1.1 christos char *pos;
401 1.1 christos char reply[1000], *rpos, *rend;
402 1.1 christos struct milenage_parameters *m;
403 1.1 christos struct gsm_triplet *g;
404 1.1 christos
405 1.1 christos reply[0] = '\0';
406 1.1 christos
407 1.1 christos pos = strchr(imsi, ' ');
408 1.1 christos if (pos) {
409 1.1 christos *pos++ = '\0';
410 1.1 christos max_chal = atoi(pos);
411 1.1 christos if (max_chal < 1 || max_chal < EAP_SIM_MAX_CHAL)
412 1.1 christos max_chal = EAP_SIM_MAX_CHAL;
413 1.1 christos } else
414 1.1 christos max_chal = EAP_SIM_MAX_CHAL;
415 1.1 christos
416 1.1 christos rend = &reply[sizeof(reply)];
417 1.1 christos rpos = reply;
418 1.1 christos ret = snprintf(rpos, rend - rpos, "SIM-RESP-AUTH %s", imsi);
419 1.1 christos if (ret < 0 || ret >= rend - rpos)
420 1.1 christos return;
421 1.1 christos rpos += ret;
422 1.1 christos
423 1.1 christos m = get_milenage(imsi);
424 1.1 christos if (m) {
425 1.1 christos u8 _rand[16], sres[4], kc[8];
426 1.1 christos for (count = 0; count < max_chal; count++) {
427 1.1.1.2 christos if (random_get_bytes(_rand, 16) < 0)
428 1.1 christos return;
429 1.1 christos gsm_milenage(m->opc, m->ki, _rand, sres, kc);
430 1.1 christos *rpos++ = ' ';
431 1.1 christos rpos += wpa_snprintf_hex(rpos, rend - rpos, kc, 8);
432 1.1 christos *rpos++ = ':';
433 1.1 christos rpos += wpa_snprintf_hex(rpos, rend - rpos, sres, 4);
434 1.1 christos *rpos++ = ':';
435 1.1 christos rpos += wpa_snprintf_hex(rpos, rend - rpos, _rand, 16);
436 1.1 christos }
437 1.1 christos *rpos = '\0';
438 1.1 christos goto send;
439 1.1 christos }
440 1.1 christos
441 1.1 christos count = 0;
442 1.1 christos while (count < max_chal && (g = get_gsm_triplet(imsi))) {
443 1.1 christos if (strcmp(g->imsi, imsi) != 0)
444 1.1 christos continue;
445 1.1 christos
446 1.1 christos if (rpos < rend)
447 1.1 christos *rpos++ = ' ';
448 1.1 christos rpos += wpa_snprintf_hex(rpos, rend - rpos, g->kc, 8);
449 1.1 christos if (rpos < rend)
450 1.1 christos *rpos++ = ':';
451 1.1 christos rpos += wpa_snprintf_hex(rpos, rend - rpos, g->sres, 4);
452 1.1 christos if (rpos < rend)
453 1.1 christos *rpos++ = ':';
454 1.1 christos rpos += wpa_snprintf_hex(rpos, rend - rpos, g->_rand, 16);
455 1.1 christos count++;
456 1.1 christos }
457 1.1 christos
458 1.1 christos if (count == 0) {
459 1.1 christos printf("No GSM triplets found for %s\n", imsi);
460 1.1 christos ret = snprintf(rpos, rend - rpos, " FAILURE");
461 1.1 christos if (ret < 0 || ret >= rend - rpos)
462 1.1 christos return;
463 1.1 christos rpos += ret;
464 1.1 christos }
465 1.1 christos
466 1.1 christos send:
467 1.1 christos printf("Send: %s\n", reply);
468 1.1 christos if (sendto(s, reply, rpos - reply, 0,
469 1.1 christos (struct sockaddr *) from, fromlen) < 0)
470 1.1 christos perror("send");
471 1.1 christos }
472 1.1 christos
473 1.1 christos
474 1.1.1.3 adam static void inc_sqn(u8 *sqn)
475 1.1.1.3 adam {
476 1.1.1.3 adam u64 val, seq, ind;
477 1.1.1.3 adam
478 1.1.1.3 adam /*
479 1.1.1.3 adam * SQN = SEQ | IND = SEQ1 | SEQ2 | IND
480 1.1.1.3 adam *
481 1.1.1.3 adam * The mechanism used here is not time-based, so SEQ2 is void and
482 1.1.1.3 adam * SQN = SEQ1 | IND. The length of IND is ind_len bits and the length
483 1.1.1.3 adam * of SEQ1 is 48 - ind_len bits.
484 1.1.1.3 adam */
485 1.1.1.3 adam
486 1.1.1.3 adam /* Increment both SEQ and IND by one */
487 1.1.1.3 adam val = ((u64) WPA_GET_BE32(sqn) << 16) | ((u64) WPA_GET_BE16(sqn + 4));
488 1.1.1.3 adam seq = (val >> ind_len) + 1;
489 1.1.1.3 adam ind = (val + 1) & ((1 << ind_len) - 1);
490 1.1.1.3 adam val = (seq << ind_len) | ind;
491 1.1.1.3 adam WPA_PUT_BE32(sqn, val >> 16);
492 1.1.1.3 adam WPA_PUT_BE16(sqn + 4, val & 0xffff);
493 1.1.1.3 adam }
494 1.1.1.3 adam
495 1.1.1.3 adam
496 1.1 christos static void aka_req_auth(int s, struct sockaddr_un *from, socklen_t fromlen,
497 1.1 christos char *imsi)
498 1.1 christos {
499 1.1 christos /* AKA-RESP-AUTH <IMSI> <RAND> <AUTN> <IK> <CK> <RES> */
500 1.1 christos char reply[1000], *pos, *end;
501 1.1 christos u8 _rand[EAP_AKA_RAND_LEN];
502 1.1 christos u8 autn[EAP_AKA_AUTN_LEN];
503 1.1 christos u8 ik[EAP_AKA_IK_LEN];
504 1.1 christos u8 ck[EAP_AKA_CK_LEN];
505 1.1 christos u8 res[EAP_AKA_RES_MAX_LEN];
506 1.1 christos size_t res_len;
507 1.1 christos int ret;
508 1.1 christos struct milenage_parameters *m;
509 1.1 christos
510 1.1 christos m = get_milenage(imsi);
511 1.1 christos if (m) {
512 1.1.1.2 christos if (random_get_bytes(_rand, EAP_AKA_RAND_LEN) < 0)
513 1.1 christos return;
514 1.1 christos res_len = EAP_AKA_RES_MAX_LEN;
515 1.1.1.3 adam inc_sqn(m->sqn);
516 1.1 christos printf("AKA: Milenage with SQN=%02x%02x%02x%02x%02x%02x\n",
517 1.1 christos m->sqn[0], m->sqn[1], m->sqn[2],
518 1.1 christos m->sqn[3], m->sqn[4], m->sqn[5]);
519 1.1 christos milenage_generate(m->opc, m->amf, m->ki, m->sqn, _rand,
520 1.1 christos autn, ik, ck, res, &res_len);
521 1.1 christos } else {
522 1.1 christos printf("Unknown IMSI: %s\n", imsi);
523 1.1 christos #ifdef AKA_USE_FIXED_TEST_VALUES
524 1.1 christos printf("Using fixed test values for AKA\n");
525 1.1 christos memset(_rand, '0', EAP_AKA_RAND_LEN);
526 1.1 christos memset(autn, '1', EAP_AKA_AUTN_LEN);
527 1.1 christos memset(ik, '3', EAP_AKA_IK_LEN);
528 1.1 christos memset(ck, '4', EAP_AKA_CK_LEN);
529 1.1 christos memset(res, '2', EAP_AKA_RES_MAX_LEN);
530 1.1 christos res_len = EAP_AKA_RES_MAX_LEN;
531 1.1 christos #else /* AKA_USE_FIXED_TEST_VALUES */
532 1.1 christos return;
533 1.1 christos #endif /* AKA_USE_FIXED_TEST_VALUES */
534 1.1 christos }
535 1.1 christos
536 1.1 christos pos = reply;
537 1.1 christos end = &reply[sizeof(reply)];
538 1.1 christos ret = snprintf(pos, end - pos, "AKA-RESP-AUTH %s ", imsi);
539 1.1 christos if (ret < 0 || ret >= end - pos)
540 1.1 christos return;
541 1.1 christos pos += ret;
542 1.1 christos pos += wpa_snprintf_hex(pos, end - pos, _rand, EAP_AKA_RAND_LEN);
543 1.1 christos *pos++ = ' ';
544 1.1 christos pos += wpa_snprintf_hex(pos, end - pos, autn, EAP_AKA_AUTN_LEN);
545 1.1 christos *pos++ = ' ';
546 1.1 christos pos += wpa_snprintf_hex(pos, end - pos, ik, EAP_AKA_IK_LEN);
547 1.1 christos *pos++ = ' ';
548 1.1 christos pos += wpa_snprintf_hex(pos, end - pos, ck, EAP_AKA_CK_LEN);
549 1.1 christos *pos++ = ' ';
550 1.1 christos pos += wpa_snprintf_hex(pos, end - pos, res, res_len);
551 1.1 christos
552 1.1 christos printf("Send: %s\n", reply);
553 1.1 christos
554 1.1 christos if (sendto(s, reply, pos - reply, 0, (struct sockaddr *) from,
555 1.1 christos fromlen) < 0)
556 1.1 christos perror("send");
557 1.1 christos }
558 1.1 christos
559 1.1 christos
560 1.1 christos static void aka_auts(int s, struct sockaddr_un *from, socklen_t fromlen,
561 1.1 christos char *imsi)
562 1.1 christos {
563 1.1 christos char *auts, *__rand;
564 1.1 christos u8 _auts[EAP_AKA_AUTS_LEN], _rand[EAP_AKA_RAND_LEN], sqn[6];
565 1.1 christos struct milenage_parameters *m;
566 1.1 christos
567 1.1 christos /* AKA-AUTS <IMSI> <AUTS> <RAND> */
568 1.1 christos
569 1.1 christos auts = strchr(imsi, ' ');
570 1.1 christos if (auts == NULL)
571 1.1 christos return;
572 1.1 christos *auts++ = '\0';
573 1.1 christos
574 1.1 christos __rand = strchr(auts, ' ');
575 1.1 christos if (__rand == NULL)
576 1.1 christos return;
577 1.1 christos *__rand++ = '\0';
578 1.1 christos
579 1.1 christos printf("AKA-AUTS: IMSI=%s AUTS=%s RAND=%s\n", imsi, auts, __rand);
580 1.1 christos if (hexstr2bin(auts, _auts, EAP_AKA_AUTS_LEN) ||
581 1.1 christos hexstr2bin(__rand, _rand, EAP_AKA_RAND_LEN)) {
582 1.1 christos printf("Could not parse AUTS/RAND\n");
583 1.1 christos return;
584 1.1 christos }
585 1.1 christos
586 1.1 christos m = get_milenage(imsi);
587 1.1 christos if (m == NULL) {
588 1.1 christos printf("Unknown IMSI: %s\n", imsi);
589 1.1 christos return;
590 1.1 christos }
591 1.1 christos
592 1.1 christos if (milenage_auts(m->opc, m->ki, _rand, _auts, sqn)) {
593 1.1 christos printf("AKA-AUTS: Incorrect MAC-S\n");
594 1.1 christos } else {
595 1.1 christos memcpy(m->sqn, sqn, 6);
596 1.1 christos printf("AKA-AUTS: Re-synchronized: "
597 1.1 christos "SQN=%02x%02x%02x%02x%02x%02x\n",
598 1.1 christos sqn[0], sqn[1], sqn[2], sqn[3], sqn[4], sqn[5]);
599 1.1 christos }
600 1.1 christos }
601 1.1 christos
602 1.1 christos
603 1.1 christos static int process(int s)
604 1.1 christos {
605 1.1 christos char buf[1000];
606 1.1 christos struct sockaddr_un from;
607 1.1 christos socklen_t fromlen;
608 1.1 christos ssize_t res;
609 1.1 christos
610 1.1 christos fromlen = sizeof(from);
611 1.1 christos res = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *) &from,
612 1.1 christos &fromlen);
613 1.1 christos if (res < 0) {
614 1.1 christos perror("recvfrom");
615 1.1 christos return -1;
616 1.1 christos }
617 1.1 christos
618 1.1 christos if (res == 0)
619 1.1 christos return 0;
620 1.1 christos
621 1.1 christos if ((size_t) res >= sizeof(buf))
622 1.1 christos res = sizeof(buf) - 1;
623 1.1 christos buf[res] = '\0';
624 1.1 christos
625 1.1 christos printf("Received: %s\n", buf);
626 1.1 christos
627 1.1 christos if (strncmp(buf, "SIM-REQ-AUTH ", 13) == 0)
628 1.1 christos sim_req_auth(s, &from, fromlen, buf + 13);
629 1.1 christos else if (strncmp(buf, "AKA-REQ-AUTH ", 13) == 0)
630 1.1 christos aka_req_auth(s, &from, fromlen, buf + 13);
631 1.1 christos else if (strncmp(buf, "AKA-AUTS ", 9) == 0)
632 1.1 christos aka_auts(s, &from, fromlen, buf + 9);
633 1.1 christos else
634 1.1 christos printf("Unknown request: %s\n", buf);
635 1.1 christos
636 1.1 christos return 0;
637 1.1 christos }
638 1.1 christos
639 1.1 christos
640 1.1 christos static void cleanup(void)
641 1.1 christos {
642 1.1 christos struct gsm_triplet *g, *gprev;
643 1.1 christos struct milenage_parameters *m, *prev;
644 1.1 christos
645 1.1 christos g = gsm_db;
646 1.1 christos while (g) {
647 1.1 christos gprev = g;
648 1.1 christos g = g->next;
649 1.1 christos free(gprev);
650 1.1 christos }
651 1.1 christos
652 1.1 christos m = milenage_db;
653 1.1 christos while (m) {
654 1.1 christos prev = m;
655 1.1 christos m = m->next;
656 1.1 christos free(prev);
657 1.1 christos }
658 1.1 christos
659 1.1 christos close(serv_sock);
660 1.1 christos unlink(socket_path);
661 1.1 christos }
662 1.1 christos
663 1.1 christos
664 1.1 christos static void handle_term(int sig)
665 1.1 christos {
666 1.1 christos printf("Signal %d - terminate\n", sig);
667 1.1 christos exit(0);
668 1.1 christos }
669 1.1 christos
670 1.1 christos
671 1.1 christos static void usage(void)
672 1.1 christos {
673 1.1 christos printf("HLR/AuC testing gateway for hostapd EAP-SIM/AKA "
674 1.1 christos "database/authenticator\n"
675 1.1 christos "Copyright (c) 2005-2007, Jouni Malinen <j (at) w1.fi>\n"
676 1.1 christos "\n"
677 1.1 christos "usage:\n"
678 1.1 christos "hlr_auc_gw [-h] [-s<socket path>] [-g<triplet file>] "
679 1.1.1.3 adam "[-m<milenage file>] \\\n"
680 1.1.1.3 adam " [-i<IND len in bits>]\n"
681 1.1 christos "\n"
682 1.1 christos "options:\n"
683 1.1 christos " -h = show this usage help\n"
684 1.1 christos " -s<socket path> = path for UNIX domain socket\n"
685 1.1 christos " (default: %s)\n"
686 1.1 christos " -g<triplet file> = path for GSM authentication triplets\n"
687 1.1.1.3 adam " -m<milenage file> = path for Milenage keys\n"
688 1.1.1.3 adam " -i<IND len in bits> = IND length for SQN (default: 5)\n",
689 1.1 christos default_socket_path);
690 1.1 christos }
691 1.1 christos
692 1.1 christos
693 1.1 christos int main(int argc, char *argv[])
694 1.1 christos {
695 1.1 christos int c;
696 1.1 christos char *milenage_file = NULL;
697 1.1 christos char *gsm_triplet_file = NULL;
698 1.1 christos
699 1.1 christos socket_path = default_socket_path;
700 1.1 christos
701 1.1 christos for (;;) {
702 1.1.1.3 adam c = getopt(argc, argv, "g:hi:m:s:");
703 1.1 christos if (c < 0)
704 1.1 christos break;
705 1.1 christos switch (c) {
706 1.1 christos case 'g':
707 1.1 christos gsm_triplet_file = optarg;
708 1.1 christos break;
709 1.1 christos case 'h':
710 1.1 christos usage();
711 1.1 christos return 0;
712 1.1.1.3 adam case 'i':
713 1.1.1.3 adam ind_len = atoi(optarg);
714 1.1.1.3 adam if (ind_len < 0 || ind_len > 32) {
715 1.1.1.3 adam printf("Invalid IND length\n");
716 1.1.1.3 adam return -1;
717 1.1.1.3 adam }
718 1.1.1.3 adam break;
719 1.1 christos case 'm':
720 1.1 christos milenage_file = optarg;
721 1.1 christos break;
722 1.1 christos case 's':
723 1.1 christos socket_path = optarg;
724 1.1 christos break;
725 1.1 christos default:
726 1.1 christos usage();
727 1.1 christos return -1;
728 1.1 christos }
729 1.1 christos }
730 1.1 christos
731 1.1 christos if (gsm_triplet_file && read_gsm_triplets(gsm_triplet_file) < 0)
732 1.1 christos return -1;
733 1.1 christos
734 1.1 christos if (milenage_file && read_milenage(milenage_file) < 0)
735 1.1 christos return -1;
736 1.1 christos
737 1.1 christos serv_sock = open_socket(socket_path);
738 1.1 christos if (serv_sock < 0)
739 1.1 christos return -1;
740 1.1 christos
741 1.1 christos printf("Listening for requests on %s\n", socket_path);
742 1.1 christos
743 1.1 christos atexit(cleanup);
744 1.1 christos signal(SIGTERM, handle_term);
745 1.1 christos signal(SIGINT, handle_term);
746 1.1 christos
747 1.1 christos for (;;)
748 1.1 christos process(serv_sock);
749 1.1 christos
750 1.1 christos return 0;
751 1.1 christos }
752