blob: 42d59dba7edea7fca2740bc54259cfdd7b1e76aa [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * HLR/AuC testing gateway for hostapd EAP-SIM/AKA database/authenticator
Dmitry Shmidt56052862013-10-04 10:23:25 -07003 * Copyright (c) 2005-2007, 2012-2013, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 *
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.
12 *
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
15 * text-based format:
16 *
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
Dmitry Shmidt051af732013-10-22 13:52:46 -070021 * GSM-AUTH-REQ <IMSI> RAND1:RAND2[:RAND3]
22 * GSM-AUTH-RESP <IMSI> Kc1:SRES1:Kc2:SRES2[:Kc3:SRES3]
23 * GSM-AUTH-RESP <IMSI> FAILURE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024 *
25 * EAP-AKA / UMTS query/response:
26 * AKA-REQ-AUTH <IMSI>
27 * AKA-RESP-AUTH <IMSI> <RAND> <AUTN> <IK> <CK> <RES>
28 * AKA-RESP-AUTH <IMSI> FAILURE
29 *
30 * EAP-AKA / UMTS AUTS (re-synchronization):
31 * AKA-AUTS <IMSI> <AUTS> <RAND>
32 *
33 * IMSI and max_chal are sent as an ASCII string,
34 * Kc/SRES/RAND/AUTN/IK/CK/RES/AUTS as hex strings.
35 *
Dmitry Shmidt56052862013-10-04 10:23:25 -070036 * An example implementation here reads GSM authentication triplets from a
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037 * text file in IMSI:Kc:SRES:RAND format, IMSI in ASCII, other fields as hex
38 * strings. This is used to simulate an HLR/AuC. As such, it is not very useful
39 * for real life authentication, but it is useful both as an example
Dmitry Shmidt04949592012-07-19 12:16:46 -070040 * implementation and for EAP-SIM/AKA/AKA' testing.
41 *
Dmitry Shmidt56052862013-10-04 10:23:25 -070042 * For a stronger example design, Milenage and GSM-Milenage algorithms can be
43 * used to dynamically generate authenticatipn information for EAP-AKA/AKA' and
44 * EAP-SIM, respectively, if Ki is known.
45 *
Dmitry Shmidt04949592012-07-19 12:16:46 -070046 * SQN generation follows the not time-based Profile 2 described in
47 * 3GPP TS 33.102 Annex C.3.2. The length of IND is 5 bits by default, but this
48 * can be changed with a command line options if needed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070049 */
50
51#include "includes.h"
52#include <sys/un.h>
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070053#ifdef CONFIG_SQLITE
54#include <sqlite3.h>
55#endif /* CONFIG_SQLITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070056
57#include "common.h"
58#include "crypto/milenage.h"
59#include "crypto/random.h"
60
61static const char *default_socket_path = "/tmp/hlr_auc_gw.sock";
62static const char *socket_path;
63static int serv_sock = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -070064static char *milenage_file = NULL;
65static int update_milenage = 0;
66static int sqn_changes = 0;
67static int ind_len = 5;
Dmitry Shmidt56052862013-10-04 10:23:25 -070068static int stdout_debug = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070069
70/* GSM triplets */
71struct gsm_triplet {
72 struct gsm_triplet *next;
73 char imsi[20];
74 u8 kc[8];
75 u8 sres[4];
76 u8 _rand[16];
77};
78
79static struct gsm_triplet *gsm_db = NULL, *gsm_db_pos = NULL;
80
81/* OPc and AMF parameters for Milenage (Example algorithms for AKA). */
82struct milenage_parameters {
83 struct milenage_parameters *next;
84 char imsi[20];
85 u8 ki[16];
86 u8 opc[16];
87 u8 amf[2];
88 u8 sqn[6];
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070089 int set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070090};
91
92static struct milenage_parameters *milenage_db = NULL;
93
94#define EAP_SIM_MAX_CHAL 3
95
96#define EAP_AKA_RAND_LEN 16
97#define EAP_AKA_AUTN_LEN 16
98#define EAP_AKA_AUTS_LEN 14
99#define EAP_AKA_RES_MAX_LEN 16
100#define EAP_AKA_IK_LEN 16
101#define EAP_AKA_CK_LEN 16
102
103
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700104#ifdef CONFIG_SQLITE
105
106static sqlite3 *sqlite_db = NULL;
107static struct milenage_parameters db_tmp_milenage;
108
109
110static int db_table_exists(sqlite3 *db, const char *name)
111{
112 char cmd[128];
113 os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
114 return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
115}
116
117
118static int db_table_create_milenage(sqlite3 *db)
119{
120 char *err = NULL;
121 const char *sql =
122 "CREATE TABLE milenage("
123 " imsi INTEGER PRIMARY KEY NOT NULL,"
124 " ki CHAR(32) NOT NULL,"
125 " opc CHAR(32) NOT NULL,"
126 " amf CHAR(4) NOT NULL,"
127 " sqn CHAR(12) NOT NULL"
128 ");";
129
130 printf("Adding database table for milenage information\n");
131 if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
132 printf("SQLite error: %s\n", err);
133 sqlite3_free(err);
134 return -1;
135 }
136
137 return 0;
138}
139
140
141static sqlite3 * db_open(const char *db_file)
142{
143 sqlite3 *db;
144
145 if (sqlite3_open(db_file, &db)) {
146 printf("Failed to open database %s: %s\n",
147 db_file, sqlite3_errmsg(db));
148 sqlite3_close(db);
149 return NULL;
150 }
151
152 if (!db_table_exists(db, "milenage") &&
153 db_table_create_milenage(db) < 0) {
154 sqlite3_close(db);
155 return NULL;
156 }
157
158 return db;
159}
160
161
162static int get_milenage_cb(void *ctx, int argc, char *argv[], char *col[])
163{
164 struct milenage_parameters *m = ctx;
165 int i;
166
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700167 m->set = 1;
168
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700169 for (i = 0; i < argc; i++) {
170 if (os_strcmp(col[i], "ki") == 0 && argv[i] &&
171 hexstr2bin(argv[i], m->ki, sizeof(m->ki))) {
172 printf("Invalid ki value in database\n");
173 return -1;
174 }
175
176 if (os_strcmp(col[i], "opc") == 0 && argv[i] &&
177 hexstr2bin(argv[i], m->opc, sizeof(m->opc))) {
178 printf("Invalid opcvalue in database\n");
179 return -1;
180 }
181
182 if (os_strcmp(col[i], "amf") == 0 && argv[i] &&
183 hexstr2bin(argv[i], m->amf, sizeof(m->amf))) {
184 printf("Invalid amf value in database\n");
185 return -1;
186 }
187
188 if (os_strcmp(col[i], "sqn") == 0 && argv[i] &&
189 hexstr2bin(argv[i], m->sqn, sizeof(m->sqn))) {
190 printf("Invalid sqn value in database\n");
191 return -1;
192 }
193 }
194
195 return 0;
196}
197
198
199static struct milenage_parameters * db_get_milenage(const char *imsi_txt)
200{
201 char cmd[128];
202 unsigned long long imsi;
203
204 os_memset(&db_tmp_milenage, 0, sizeof(db_tmp_milenage));
205 imsi = atoll(imsi_txt);
206 os_snprintf(db_tmp_milenage.imsi, sizeof(db_tmp_milenage.imsi),
207 "%llu", imsi);
208 os_snprintf(cmd, sizeof(cmd),
209 "SELECT ki,opc,amf,sqn FROM milenage WHERE imsi=%llu;",
210 imsi);
211 if (sqlite3_exec(sqlite_db, cmd, get_milenage_cb, &db_tmp_milenage,
212 NULL) != SQLITE_OK)
213 return NULL;
214
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700215 if (!db_tmp_milenage.set)
216 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700217 return &db_tmp_milenage;
218}
219
220
221static int db_update_milenage_sqn(struct milenage_parameters *m)
222{
223 char cmd[128], val[13], *pos;
224
Dmitry Shmidt56052862013-10-04 10:23:25 -0700225 if (sqlite_db == NULL)
226 return 0;
227
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700228 pos = val;
229 pos += wpa_snprintf_hex(pos, sizeof(val), m->sqn, 6);
230 *pos = '\0';
231 os_snprintf(cmd, sizeof(cmd),
232 "UPDATE milenage SET sqn='%s' WHERE imsi=%s;",
233 val, m->imsi);
234 if (sqlite3_exec(sqlite_db, cmd, NULL, NULL, NULL) != SQLITE_OK) {
235 printf("Failed to update SQN in database for IMSI %s\n",
236 m->imsi);
237 return -1;
238 }
239 return 0;
240}
241
242#endif /* CONFIG_SQLITE */
243
244
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245static int open_socket(const char *path)
246{
247 struct sockaddr_un addr;
248 int s;
249
250 s = socket(PF_UNIX, SOCK_DGRAM, 0);
251 if (s < 0) {
252 perror("socket(PF_UNIX)");
253 return -1;
254 }
255
256 memset(&addr, 0, sizeof(addr));
257 addr.sun_family = AF_UNIX;
258 os_strlcpy(addr.sun_path, path, sizeof(addr.sun_path));
259 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700260 perror("hlr-auc-gw: bind(PF_UNIX)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700261 close(s);
262 return -1;
263 }
264
265 return s;
266}
267
268
269static int read_gsm_triplets(const char *fname)
270{
271 FILE *f;
272 char buf[200], *pos, *pos2;
273 struct gsm_triplet *g = NULL;
274 int line, ret = 0;
275
276 if (fname == NULL)
277 return -1;
278
279 f = fopen(fname, "r");
280 if (f == NULL) {
281 printf("Could not open GSM tripler data file '%s'\n", fname);
282 return -1;
283 }
284
285 line = 0;
286 while (fgets(buf, sizeof(buf), f)) {
287 line++;
288
289 /* Parse IMSI:Kc:SRES:RAND */
290 buf[sizeof(buf) - 1] = '\0';
291 if (buf[0] == '#')
292 continue;
293 pos = buf;
294 while (*pos != '\0' && *pos != '\n')
295 pos++;
296 if (*pos == '\n')
297 *pos = '\0';
298 pos = buf;
299 if (*pos == '\0')
300 continue;
301
302 g = os_zalloc(sizeof(*g));
303 if (g == NULL) {
304 ret = -1;
305 break;
306 }
307
308 /* IMSI */
309 pos2 = strchr(pos, ':');
310 if (pos2 == NULL) {
311 printf("%s:%d - Invalid IMSI (%s)\n",
312 fname, line, pos);
313 ret = -1;
314 break;
315 }
316 *pos2 = '\0';
317 if (strlen(pos) >= sizeof(g->imsi)) {
318 printf("%s:%d - Too long IMSI (%s)\n",
319 fname, line, pos);
320 ret = -1;
321 break;
322 }
323 os_strlcpy(g->imsi, pos, sizeof(g->imsi));
324 pos = pos2 + 1;
325
326 /* Kc */
327 pos2 = strchr(pos, ':');
328 if (pos2 == NULL) {
329 printf("%s:%d - Invalid Kc (%s)\n", fname, line, pos);
330 ret = -1;
331 break;
332 }
333 *pos2 = '\0';
334 if (strlen(pos) != 16 || hexstr2bin(pos, g->kc, 8)) {
335 printf("%s:%d - Invalid Kc (%s)\n", fname, line, pos);
336 ret = -1;
337 break;
338 }
339 pos = pos2 + 1;
340
341 /* SRES */
342 pos2 = strchr(pos, ':');
343 if (pos2 == NULL) {
344 printf("%s:%d - Invalid SRES (%s)\n", fname, line,
345 pos);
346 ret = -1;
347 break;
348 }
349 *pos2 = '\0';
350 if (strlen(pos) != 8 || hexstr2bin(pos, g->sres, 4)) {
351 printf("%s:%d - Invalid SRES (%s)\n", fname, line,
352 pos);
353 ret = -1;
354 break;
355 }
356 pos = pos2 + 1;
357
358 /* RAND */
359 pos2 = strchr(pos, ':');
360 if (pos2)
361 *pos2 = '\0';
362 if (strlen(pos) != 32 || hexstr2bin(pos, g->_rand, 16)) {
363 printf("%s:%d - Invalid RAND (%s)\n", fname, line,
364 pos);
365 ret = -1;
366 break;
367 }
368 pos = pos2 + 1;
369
370 g->next = gsm_db;
371 gsm_db = g;
372 g = NULL;
373 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700374 os_free(g);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375
376 fclose(f);
377
378 return ret;
379}
380
381
382static struct gsm_triplet * get_gsm_triplet(const char *imsi)
383{
384 struct gsm_triplet *g = gsm_db_pos;
385
386 while (g) {
387 if (strcmp(g->imsi, imsi) == 0) {
388 gsm_db_pos = g->next;
389 return g;
390 }
391 g = g->next;
392 }
393
394 g = gsm_db;
395 while (g && g != gsm_db_pos) {
396 if (strcmp(g->imsi, imsi) == 0) {
397 gsm_db_pos = g->next;
398 return g;
399 }
400 g = g->next;
401 }
402
403 return NULL;
404}
405
406
407static int read_milenage(const char *fname)
408{
409 FILE *f;
410 char buf[200], *pos, *pos2;
411 struct milenage_parameters *m = NULL;
412 int line, ret = 0;
413
414 if (fname == NULL)
415 return -1;
416
417 f = fopen(fname, "r");
418 if (f == NULL) {
419 printf("Could not open Milenage data file '%s'\n", fname);
420 return -1;
421 }
422
423 line = 0;
424 while (fgets(buf, sizeof(buf), f)) {
425 line++;
426
427 /* Parse IMSI Ki OPc AMF SQN */
428 buf[sizeof(buf) - 1] = '\0';
429 if (buf[0] == '#')
430 continue;
431 pos = buf;
432 while (*pos != '\0' && *pos != '\n')
433 pos++;
434 if (*pos == '\n')
435 *pos = '\0';
436 pos = buf;
437 if (*pos == '\0')
438 continue;
439
440 m = os_zalloc(sizeof(*m));
441 if (m == NULL) {
442 ret = -1;
443 break;
444 }
445
446 /* IMSI */
447 pos2 = strchr(pos, ' ');
448 if (pos2 == NULL) {
449 printf("%s:%d - Invalid IMSI (%s)\n",
450 fname, line, pos);
451 ret = -1;
452 break;
453 }
454 *pos2 = '\0';
455 if (strlen(pos) >= sizeof(m->imsi)) {
456 printf("%s:%d - Too long IMSI (%s)\n",
457 fname, line, pos);
458 ret = -1;
459 break;
460 }
461 os_strlcpy(m->imsi, pos, sizeof(m->imsi));
462 pos = pos2 + 1;
463
464 /* Ki */
465 pos2 = strchr(pos, ' ');
466 if (pos2 == NULL) {
467 printf("%s:%d - Invalid Ki (%s)\n", fname, line, pos);
468 ret = -1;
469 break;
470 }
471 *pos2 = '\0';
472 if (strlen(pos) != 32 || hexstr2bin(pos, m->ki, 16)) {
473 printf("%s:%d - Invalid Ki (%s)\n", fname, line, pos);
474 ret = -1;
475 break;
476 }
477 pos = pos2 + 1;
478
479 /* OPc */
480 pos2 = strchr(pos, ' ');
481 if (pos2 == NULL) {
482 printf("%s:%d - Invalid OPc (%s)\n", fname, line, pos);
483 ret = -1;
484 break;
485 }
486 *pos2 = '\0';
487 if (strlen(pos) != 32 || hexstr2bin(pos, m->opc, 16)) {
488 printf("%s:%d - Invalid OPc (%s)\n", fname, line, pos);
489 ret = -1;
490 break;
491 }
492 pos = pos2 + 1;
493
494 /* AMF */
495 pos2 = strchr(pos, ' ');
496 if (pos2 == NULL) {
497 printf("%s:%d - Invalid AMF (%s)\n", fname, line, pos);
498 ret = -1;
499 break;
500 }
501 *pos2 = '\0';
502 if (strlen(pos) != 4 || hexstr2bin(pos, m->amf, 2)) {
503 printf("%s:%d - Invalid AMF (%s)\n", fname, line, pos);
504 ret = -1;
505 break;
506 }
507 pos = pos2 + 1;
508
509 /* SQN */
510 pos2 = strchr(pos, ' ');
511 if (pos2)
512 *pos2 = '\0';
513 if (strlen(pos) != 12 || hexstr2bin(pos, m->sqn, 6)) {
514 printf("%s:%d - Invalid SEQ (%s)\n", fname, line, pos);
515 ret = -1;
516 break;
517 }
518 pos = pos2 + 1;
519
520 m->next = milenage_db;
521 milenage_db = m;
522 m = NULL;
523 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700524 os_free(m);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700525
526 fclose(f);
527
528 return ret;
529}
530
531
Dmitry Shmidt04949592012-07-19 12:16:46 -0700532static void update_milenage_file(const char *fname)
533{
534 FILE *f, *f2;
535 char buf[500], *pos;
536 char *end = buf + sizeof(buf);
537 struct milenage_parameters *m;
538 size_t imsi_len;
539
540 f = fopen(fname, "r");
541 if (f == NULL) {
542 printf("Could not open Milenage data file '%s'\n", fname);
543 return;
544 }
545
546 snprintf(buf, sizeof(buf), "%s.new", fname);
547 f2 = fopen(buf, "w");
548 if (f2 == NULL) {
549 printf("Could not write Milenage data file '%s'\n", buf);
550 fclose(f);
551 return;
552 }
553
554 while (fgets(buf, sizeof(buf), f)) {
555 /* IMSI Ki OPc AMF SQN */
556 buf[sizeof(buf) - 1] = '\0';
557
558 pos = strchr(buf, ' ');
559 if (buf[0] == '#' || pos == NULL || pos - buf >= 20)
560 goto no_update;
561
562 imsi_len = pos - buf;
563
564 for (m = milenage_db; m; m = m->next) {
565 if (strncmp(buf, m->imsi, imsi_len) == 0 &&
566 m->imsi[imsi_len] == '\0')
567 break;
568 }
569
570 if (!m)
571 goto no_update;
572
573 pos = buf;
574 pos += snprintf(pos, end - pos, "%s ", m->imsi);
575 pos += wpa_snprintf_hex(pos, end - pos, m->ki, 16);
576 *pos++ = ' ';
577 pos += wpa_snprintf_hex(pos, end - pos, m->opc, 16);
578 *pos++ = ' ';
579 pos += wpa_snprintf_hex(pos, end - pos, m->amf, 2);
580 *pos++ = ' ';
581 pos += wpa_snprintf_hex(pos, end - pos, m->sqn, 6);
582 *pos++ = '\n';
583
584 no_update:
585 fprintf(f2, "%s", buf);
586 }
587
588 fclose(f2);
589 fclose(f);
590
591 snprintf(buf, sizeof(buf), "%s.bak", fname);
592 if (rename(fname, buf) < 0) {
593 perror("rename");
594 return;
595 }
596
597 snprintf(buf, sizeof(buf), "%s.new", fname);
598 if (rename(buf, fname) < 0) {
599 perror("rename");
600 return;
601 }
602
603}
604
605
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700606static struct milenage_parameters * get_milenage(const char *imsi)
607{
608 struct milenage_parameters *m = milenage_db;
609
610 while (m) {
611 if (strcmp(m->imsi, imsi) == 0)
612 break;
613 m = m->next;
614 }
615
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700616#ifdef CONFIG_SQLITE
617 if (!m)
618 m = db_get_milenage(imsi);
619#endif /* CONFIG_SQLITE */
620
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700621 return m;
622}
623
624
Dmitry Shmidt56052862013-10-04 10:23:25 -0700625static int sim_req_auth(char *imsi, char *resp, size_t resp_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700626{
627 int count, max_chal, ret;
628 char *pos;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700629 char *rpos, *rend;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700630 struct milenage_parameters *m;
631 struct gsm_triplet *g;
632
Dmitry Shmidt56052862013-10-04 10:23:25 -0700633 resp[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700634
635 pos = strchr(imsi, ' ');
636 if (pos) {
637 *pos++ = '\0';
638 max_chal = atoi(pos);
Dmitry Shmidt56052862013-10-04 10:23:25 -0700639 if (max_chal < 1 || max_chal > EAP_SIM_MAX_CHAL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700640 max_chal = EAP_SIM_MAX_CHAL;
641 } else
642 max_chal = EAP_SIM_MAX_CHAL;
643
Dmitry Shmidt56052862013-10-04 10:23:25 -0700644 rend = resp + resp_len;
645 rpos = resp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700646 ret = snprintf(rpos, rend - rpos, "SIM-RESP-AUTH %s", imsi);
647 if (ret < 0 || ret >= rend - rpos)
Dmitry Shmidt56052862013-10-04 10:23:25 -0700648 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700649 rpos += ret;
650
651 m = get_milenage(imsi);
652 if (m) {
653 u8 _rand[16], sres[4], kc[8];
654 for (count = 0; count < max_chal; count++) {
655 if (random_get_bytes(_rand, 16) < 0)
Dmitry Shmidt56052862013-10-04 10:23:25 -0700656 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700657 gsm_milenage(m->opc, m->ki, _rand, sres, kc);
658 *rpos++ = ' ';
659 rpos += wpa_snprintf_hex(rpos, rend - rpos, kc, 8);
660 *rpos++ = ':';
661 rpos += wpa_snprintf_hex(rpos, rend - rpos, sres, 4);
662 *rpos++ = ':';
663 rpos += wpa_snprintf_hex(rpos, rend - rpos, _rand, 16);
664 }
665 *rpos = '\0';
Dmitry Shmidt56052862013-10-04 10:23:25 -0700666 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700667 }
668
669 count = 0;
670 while (count < max_chal && (g = get_gsm_triplet(imsi))) {
671 if (strcmp(g->imsi, imsi) != 0)
672 continue;
673
674 if (rpos < rend)
675 *rpos++ = ' ';
676 rpos += wpa_snprintf_hex(rpos, rend - rpos, g->kc, 8);
677 if (rpos < rend)
678 *rpos++ = ':';
679 rpos += wpa_snprintf_hex(rpos, rend - rpos, g->sres, 4);
680 if (rpos < rend)
681 *rpos++ = ':';
682 rpos += wpa_snprintf_hex(rpos, rend - rpos, g->_rand, 16);
683 count++;
684 }
685
686 if (count == 0) {
687 printf("No GSM triplets found for %s\n", imsi);
688 ret = snprintf(rpos, rend - rpos, " FAILURE");
689 if (ret < 0 || ret >= rend - rpos)
Dmitry Shmidt56052862013-10-04 10:23:25 -0700690 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700691 rpos += ret;
692 }
693
Dmitry Shmidt56052862013-10-04 10:23:25 -0700694 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700695}
696
697
Dmitry Shmidt051af732013-10-22 13:52:46 -0700698static int gsm_auth_req(char *imsi, char *resp, size_t resp_len)
699{
700 int count, ret;
701 char *pos, *rpos, *rend;
702 struct milenage_parameters *m;
703
704 resp[0] = '\0';
705
706 pos = os_strchr(imsi, ' ');
707 if (!pos)
708 return -1;
709 *pos++ = '\0';
710
711 rend = resp + resp_len;
712 rpos = resp;
713 ret = os_snprintf(rpos, rend - rpos, "GSM-AUTH-RESP %s", imsi);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800714 if (os_snprintf_error(rend - rpos, ret))
Dmitry Shmidt051af732013-10-22 13:52:46 -0700715 return -1;
716 rpos += ret;
717
718 m = get_milenage(imsi);
719 if (m) {
720 u8 _rand[16], sres[4], kc[8];
721 for (count = 0; count < EAP_SIM_MAX_CHAL; count++) {
722 if (hexstr2bin(pos, _rand, 16) != 0)
723 return -1;
724 gsm_milenage(m->opc, m->ki, _rand, sres, kc);
725 *rpos++ = count == 0 ? ' ' : ':';
726 rpos += wpa_snprintf_hex(rpos, rend - rpos, kc, 8);
727 *rpos++ = ':';
728 rpos += wpa_snprintf_hex(rpos, rend - rpos, sres, 4);
729 pos += 16 * 2;
730 if (*pos != ':')
731 break;
732 pos++;
733 }
734 *rpos = '\0';
735 return 0;
736 }
737
738 printf("No GSM triplets found for %s\n", imsi);
739 ret = os_snprintf(rpos, rend - rpos, " FAILURE");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800740 if (os_snprintf_error(rend - rpos, ret))
Dmitry Shmidt051af732013-10-22 13:52:46 -0700741 return -1;
742 rpos += ret;
743
744 return 0;
745}
746
747
Dmitry Shmidt04949592012-07-19 12:16:46 -0700748static void inc_sqn(u8 *sqn)
749{
750 u64 val, seq, ind;
751
752 /*
753 * SQN = SEQ | IND = SEQ1 | SEQ2 | IND
754 *
755 * The mechanism used here is not time-based, so SEQ2 is void and
756 * SQN = SEQ1 | IND. The length of IND is ind_len bits and the length
757 * of SEQ1 is 48 - ind_len bits.
758 */
759
760 /* Increment both SEQ and IND by one */
761 val = ((u64) WPA_GET_BE32(sqn) << 16) | ((u64) WPA_GET_BE16(sqn + 4));
762 seq = (val >> ind_len) + 1;
763 ind = (val + 1) & ((1 << ind_len) - 1);
764 val = (seq << ind_len) | ind;
765 WPA_PUT_BE32(sqn, val >> 16);
766 WPA_PUT_BE16(sqn + 4, val & 0xffff);
767}
768
769
Dmitry Shmidt56052862013-10-04 10:23:25 -0700770static int aka_req_auth(char *imsi, char *resp, size_t resp_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700771{
772 /* AKA-RESP-AUTH <IMSI> <RAND> <AUTN> <IK> <CK> <RES> */
Dmitry Shmidt56052862013-10-04 10:23:25 -0700773 char *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700774 u8 _rand[EAP_AKA_RAND_LEN];
775 u8 autn[EAP_AKA_AUTN_LEN];
776 u8 ik[EAP_AKA_IK_LEN];
777 u8 ck[EAP_AKA_CK_LEN];
778 u8 res[EAP_AKA_RES_MAX_LEN];
779 size_t res_len;
780 int ret;
781 struct milenage_parameters *m;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700782 int failed = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700783
784 m = get_milenage(imsi);
785 if (m) {
786 if (random_get_bytes(_rand, EAP_AKA_RAND_LEN) < 0)
Dmitry Shmidt56052862013-10-04 10:23:25 -0700787 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700788 res_len = EAP_AKA_RES_MAX_LEN;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700789 inc_sqn(m->sqn);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700790#ifdef CONFIG_SQLITE
791 db_update_milenage_sqn(m);
792#endif /* CONFIG_SQLITE */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700793 sqn_changes = 1;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700794 if (stdout_debug) {
795 printf("AKA: Milenage with SQN=%02x%02x%02x%02x%02x%02x\n",
796 m->sqn[0], m->sqn[1], m->sqn[2],
797 m->sqn[3], m->sqn[4], m->sqn[5]);
798 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700799 milenage_generate(m->opc, m->amf, m->ki, m->sqn, _rand,
800 autn, ik, ck, res, &res_len);
801 } else {
802 printf("Unknown IMSI: %s\n", imsi);
803#ifdef AKA_USE_FIXED_TEST_VALUES
804 printf("Using fixed test values for AKA\n");
805 memset(_rand, '0', EAP_AKA_RAND_LEN);
806 memset(autn, '1', EAP_AKA_AUTN_LEN);
807 memset(ik, '3', EAP_AKA_IK_LEN);
808 memset(ck, '4', EAP_AKA_CK_LEN);
809 memset(res, '2', EAP_AKA_RES_MAX_LEN);
810 res_len = EAP_AKA_RES_MAX_LEN;
811#else /* AKA_USE_FIXED_TEST_VALUES */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700812 failed = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700813#endif /* AKA_USE_FIXED_TEST_VALUES */
814 }
815
Dmitry Shmidt56052862013-10-04 10:23:25 -0700816 pos = resp;
817 end = resp + resp_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700818 ret = snprintf(pos, end - pos, "AKA-RESP-AUTH %s ", imsi);
819 if (ret < 0 || ret >= end - pos)
Dmitry Shmidt56052862013-10-04 10:23:25 -0700820 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700821 pos += ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700822 if (failed) {
823 ret = snprintf(pos, end - pos, "FAILURE");
824 if (ret < 0 || ret >= end - pos)
Dmitry Shmidt56052862013-10-04 10:23:25 -0700825 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700826 pos += ret;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700827 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700828 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700829 pos += wpa_snprintf_hex(pos, end - pos, _rand, EAP_AKA_RAND_LEN);
830 *pos++ = ' ';
831 pos += wpa_snprintf_hex(pos, end - pos, autn, EAP_AKA_AUTN_LEN);
832 *pos++ = ' ';
833 pos += wpa_snprintf_hex(pos, end - pos, ik, EAP_AKA_IK_LEN);
834 *pos++ = ' ';
835 pos += wpa_snprintf_hex(pos, end - pos, ck, EAP_AKA_CK_LEN);
836 *pos++ = ' ';
837 pos += wpa_snprintf_hex(pos, end - pos, res, res_len);
838
Dmitry Shmidt56052862013-10-04 10:23:25 -0700839 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700840}
841
842
Dmitry Shmidt56052862013-10-04 10:23:25 -0700843static int aka_auts(char *imsi, char *resp, size_t resp_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700844{
845 char *auts, *__rand;
846 u8 _auts[EAP_AKA_AUTS_LEN], _rand[EAP_AKA_RAND_LEN], sqn[6];
847 struct milenage_parameters *m;
848
Dmitry Shmidt56052862013-10-04 10:23:25 -0700849 resp[0] = '\0';
850
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700851 /* AKA-AUTS <IMSI> <AUTS> <RAND> */
852
853 auts = strchr(imsi, ' ');
854 if (auts == NULL)
Dmitry Shmidt56052862013-10-04 10:23:25 -0700855 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700856 *auts++ = '\0';
857
858 __rand = strchr(auts, ' ');
859 if (__rand == NULL)
Dmitry Shmidt56052862013-10-04 10:23:25 -0700860 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861 *__rand++ = '\0';
862
Dmitry Shmidt56052862013-10-04 10:23:25 -0700863 if (stdout_debug) {
864 printf("AKA-AUTS: IMSI=%s AUTS=%s RAND=%s\n",
865 imsi, auts, __rand);
866 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700867 if (hexstr2bin(auts, _auts, EAP_AKA_AUTS_LEN) ||
868 hexstr2bin(__rand, _rand, EAP_AKA_RAND_LEN)) {
869 printf("Could not parse AUTS/RAND\n");
Dmitry Shmidt56052862013-10-04 10:23:25 -0700870 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700871 }
872
873 m = get_milenage(imsi);
874 if (m == NULL) {
875 printf("Unknown IMSI: %s\n", imsi);
Dmitry Shmidt56052862013-10-04 10:23:25 -0700876 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700877 }
878
879 if (milenage_auts(m->opc, m->ki, _rand, _auts, sqn)) {
880 printf("AKA-AUTS: Incorrect MAC-S\n");
881 } else {
882 memcpy(m->sqn, sqn, 6);
Dmitry Shmidt56052862013-10-04 10:23:25 -0700883 if (stdout_debug) {
884 printf("AKA-AUTS: Re-synchronized: "
885 "SQN=%02x%02x%02x%02x%02x%02x\n",
886 sqn[0], sqn[1], sqn[2], sqn[3], sqn[4], sqn[5]);
887 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700888#ifdef CONFIG_SQLITE
889 db_update_milenage_sqn(m);
890#endif /* CONFIG_SQLITE */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700891 sqn_changes = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700892 }
Dmitry Shmidt56052862013-10-04 10:23:25 -0700893
894 return 0;
895}
896
897
898static int process_cmd(char *cmd, char *resp, size_t resp_len)
899{
900 if (os_strncmp(cmd, "SIM-REQ-AUTH ", 13) == 0)
901 return sim_req_auth(cmd + 13, resp, resp_len);
902
Dmitry Shmidt051af732013-10-22 13:52:46 -0700903 if (os_strncmp(cmd, "GSM-AUTH-REQ ", 13) == 0)
904 return gsm_auth_req(cmd + 13, resp, resp_len);
905
Dmitry Shmidt56052862013-10-04 10:23:25 -0700906 if (os_strncmp(cmd, "AKA-REQ-AUTH ", 13) == 0)
907 return aka_req_auth(cmd + 13, resp, resp_len);
908
909 if (os_strncmp(cmd, "AKA-AUTS ", 9) == 0)
910 return aka_auts(cmd + 9, resp, resp_len);
911
912 printf("Unknown request: %s\n", cmd);
913 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700914}
915
916
917static int process(int s)
918{
Dmitry Shmidt56052862013-10-04 10:23:25 -0700919 char buf[1000], resp[1000];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700920 struct sockaddr_un from;
921 socklen_t fromlen;
922 ssize_t res;
923
924 fromlen = sizeof(from);
925 res = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *) &from,
926 &fromlen);
927 if (res < 0) {
928 perror("recvfrom");
929 return -1;
930 }
931
932 if (res == 0)
933 return 0;
934
935 if ((size_t) res >= sizeof(buf))
936 res = sizeof(buf) - 1;
937 buf[res] = '\0';
938
939 printf("Received: %s\n", buf);
940
Dmitry Shmidt56052862013-10-04 10:23:25 -0700941 if (process_cmd(buf, resp, sizeof(resp)) < 0) {
942 printf("Failed to process request\n");
943 return -1;
944 }
945
946 if (resp[0] == '\0') {
947 printf("No response\n");
948 return 0;
949 }
950
951 printf("Send: %s\n", resp);
952
953 if (sendto(s, resp, os_strlen(resp), 0, (struct sockaddr *) &from,
954 fromlen) < 0)
955 perror("send");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700956
957 return 0;
958}
959
960
961static void cleanup(void)
962{
963 struct gsm_triplet *g, *gprev;
964 struct milenage_parameters *m, *prev;
965
Dmitry Shmidt04949592012-07-19 12:16:46 -0700966 if (update_milenage && milenage_file && sqn_changes)
967 update_milenage_file(milenage_file);
968
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700969 g = gsm_db;
970 while (g) {
971 gprev = g;
972 g = g->next;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700973 os_free(gprev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700974 }
975
976 m = milenage_db;
977 while (m) {
978 prev = m;
979 m = m->next;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700980 os_free(prev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700981 }
982
Dmitry Shmidt56052862013-10-04 10:23:25 -0700983 if (serv_sock >= 0)
984 close(serv_sock);
985 if (socket_path)
986 unlink(socket_path);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700987
988#ifdef CONFIG_SQLITE
989 if (sqlite_db) {
990 sqlite3_close(sqlite_db);
991 sqlite_db = NULL;
992 }
993#endif /* CONFIG_SQLITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700994}
995
996
997static void handle_term(int sig)
998{
999 printf("Signal %d - terminate\n", sig);
1000 exit(0);
1001}
1002
1003
1004static void usage(void)
1005{
1006 printf("HLR/AuC testing gateway for hostapd EAP-SIM/AKA "
1007 "database/authenticator\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07001008 "Copyright (c) 2005-2007, 2012-2013, Jouni Malinen <j@w1.fi>\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001009 "\n"
1010 "usage:\n"
Dmitry Shmidt04949592012-07-19 12:16:46 -07001011 "hlr_auc_gw [-hu] [-s<socket path>] [-g<triplet file>] "
1012 "[-m<milenage file>] \\\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07001013 " [-D<DB file>] [-i<IND len in bits>] [command]\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001014 "\n"
1015 "options:\n"
1016 " -h = show this usage help\n"
Dmitry Shmidt04949592012-07-19 12:16:46 -07001017 " -u = update SQN in Milenage file on exit\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001018 " -s<socket path> = path for UNIX domain socket\n"
1019 " (default: %s)\n"
1020 " -g<triplet file> = path for GSM authentication triplets\n"
Dmitry Shmidt04949592012-07-19 12:16:46 -07001021 " -m<milenage file> = path for Milenage keys\n"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001022 " -D<DB file> = path to SQLite database\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07001023 " -i<IND len in bits> = IND length for SQN (default: 5)\n"
1024 "\n"
1025 "If the optional command argument, like "
1026 "\"AKA-REQ-AUTH <IMSI>\" is used, a single\n"
1027 "command is processed with response sent to stdout. Otherwise, "
1028 "hlr_auc_gw opens\n"
1029 "a control interface and processes commands sent through it "
1030 "(e.g., by EAP server\n"
1031 "in hostapd).\n",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001032 default_socket_path);
1033}
1034
1035
1036int main(int argc, char *argv[])
1037{
1038 int c;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001039 char *gsm_triplet_file = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001040 char *sqlite_db_file = NULL;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001041 int ret = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042
Dmitry Shmidt04949592012-07-19 12:16:46 -07001043 if (os_program_init())
1044 return -1;
1045
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046 socket_path = default_socket_path;
1047
1048 for (;;) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001049 c = getopt(argc, argv, "D:g:hi:m:s:u");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001050 if (c < 0)
1051 break;
1052 switch (c) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001053 case 'D':
1054#ifdef CONFIG_SQLITE
1055 sqlite_db_file = optarg;
1056 break;
1057#else /* CONFIG_SQLITE */
1058 printf("No SQLite support included in the build\n");
1059 return -1;
1060#endif /* CONFIG_SQLITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001061 case 'g':
1062 gsm_triplet_file = optarg;
1063 break;
1064 case 'h':
1065 usage();
1066 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001067 case 'i':
1068 ind_len = atoi(optarg);
1069 if (ind_len < 0 || ind_len > 32) {
1070 printf("Invalid IND length\n");
1071 return -1;
1072 }
1073 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074 case 'm':
1075 milenage_file = optarg;
1076 break;
1077 case 's':
1078 socket_path = optarg;
1079 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001080 case 'u':
1081 update_milenage = 1;
1082 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001083 default:
1084 usage();
1085 return -1;
1086 }
1087 }
1088
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001089 if (!gsm_triplet_file && !milenage_file && !sqlite_db_file) {
1090 usage();
1091 return -1;
1092 }
1093
1094#ifdef CONFIG_SQLITE
1095 if (sqlite_db_file && (sqlite_db = db_open(sqlite_db_file)) == NULL)
1096 return -1;
1097#endif /* CONFIG_SQLITE */
1098
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001099 if (gsm_triplet_file && read_gsm_triplets(gsm_triplet_file) < 0)
1100 return -1;
1101
1102 if (milenage_file && read_milenage(milenage_file) < 0)
1103 return -1;
1104
Dmitry Shmidt56052862013-10-04 10:23:25 -07001105 if (optind == argc) {
1106 serv_sock = open_socket(socket_path);
1107 if (serv_sock < 0)
1108 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001109
Dmitry Shmidt56052862013-10-04 10:23:25 -07001110 printf("Listening for requests on %s\n", socket_path);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001111
Dmitry Shmidt56052862013-10-04 10:23:25 -07001112 atexit(cleanup);
1113 signal(SIGTERM, handle_term);
1114 signal(SIGINT, handle_term);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001115
Dmitry Shmidt56052862013-10-04 10:23:25 -07001116 for (;;)
1117 process(serv_sock);
1118 } else {
1119 char buf[1000];
1120 socket_path = NULL;
1121 stdout_debug = 0;
1122 if (process_cmd(argv[optind], buf, sizeof(buf)) < 0) {
1123 printf("FAIL\n");
1124 ret = -1;
1125 } else {
1126 printf("%s\n", buf);
1127 }
1128 cleanup();
1129 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001130
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001131#ifdef CONFIG_SQLITE
1132 if (sqlite_db) {
1133 sqlite3_close(sqlite_db);
1134 sqlite_db = NULL;
1135 }
1136#endif /* CONFIG_SQLITE */
1137
Dmitry Shmidt04949592012-07-19 12:16:46 -07001138 os_program_deinit();
1139
Dmitry Shmidt56052862013-10-04 10:23:25 -07001140 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001141}