hlr_auc_gw.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. /*
  2. * HLR/AuC testing gateway for hostapd EAP-SIM/AKA database/authenticator
  3. * Copyright (c) 2005-2007, 2012-2013, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. *
  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
  21. * GSM-AUTH-REQ <IMSI> RAND1:RAND2[:RAND3]
  22. * GSM-AUTH-RESP <IMSI> Kc1:SRES1:Kc2:SRES2[:Kc3:SRES3]
  23. * GSM-AUTH-RESP <IMSI> FAILURE
  24. *
  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. *
  36. * An example implementation here reads GSM authentication triplets from a
  37. * 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
  40. * implementation and for EAP-SIM/AKA/AKA' testing.
  41. *
  42. * 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. *
  46. * 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.
  49. */
  50. #include "includes.h"
  51. #include <sys/un.h>
  52. #ifdef CONFIG_SQLITE
  53. #include <sqlite3.h>
  54. #endif /* CONFIG_SQLITE */
  55. #include "common.h"
  56. #include "crypto/milenage.h"
  57. #include "crypto/random.h"
  58. static const char *default_socket_path = "/tmp/hlr_auc_gw.sock";
  59. static const char *socket_path;
  60. static int serv_sock = -1;
  61. static char *milenage_file = NULL;
  62. static int update_milenage = 0;
  63. static int sqn_changes = 0;
  64. static int ind_len = 5;
  65. static int stdout_debug = 1;
  66. /* GSM triplets */
  67. struct gsm_triplet {
  68. struct gsm_triplet *next;
  69. char imsi[20];
  70. u8 kc[8];
  71. u8 sres[4];
  72. u8 _rand[16];
  73. };
  74. static struct gsm_triplet *gsm_db = NULL, *gsm_db_pos = NULL;
  75. /* OPc and AMF parameters for Milenage (Example algorithms for AKA). */
  76. struct milenage_parameters {
  77. struct milenage_parameters *next;
  78. char imsi[20];
  79. u8 ki[16];
  80. u8 opc[16];
  81. u8 amf[2];
  82. u8 sqn[6];
  83. int set;
  84. };
  85. static struct milenage_parameters *milenage_db = NULL;
  86. #define EAP_SIM_MAX_CHAL 3
  87. #define EAP_AKA_RAND_LEN 16
  88. #define EAP_AKA_AUTN_LEN 16
  89. #define EAP_AKA_AUTS_LEN 14
  90. #define EAP_AKA_RES_MAX_LEN 16
  91. #define EAP_AKA_IK_LEN 16
  92. #define EAP_AKA_CK_LEN 16
  93. #ifdef CONFIG_SQLITE
  94. static sqlite3 *sqlite_db = NULL;
  95. static struct milenage_parameters db_tmp_milenage;
  96. static int db_table_exists(sqlite3 *db, const char *name)
  97. {
  98. char cmd[128];
  99. os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
  100. return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
  101. }
  102. static int db_table_create_milenage(sqlite3 *db)
  103. {
  104. char *err = NULL;
  105. const char *sql =
  106. "CREATE TABLE milenage("
  107. " imsi INTEGER PRIMARY KEY NOT NULL,"
  108. " ki CHAR(32) NOT NULL,"
  109. " opc CHAR(32) NOT NULL,"
  110. " amf CHAR(4) NOT NULL,"
  111. " sqn CHAR(12) NOT NULL"
  112. ");";
  113. printf("Adding database table for milenage information\n");
  114. if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
  115. printf("SQLite error: %s\n", err);
  116. sqlite3_free(err);
  117. return -1;
  118. }
  119. return 0;
  120. }
  121. static sqlite3 * db_open(const char *db_file)
  122. {
  123. sqlite3 *db;
  124. if (sqlite3_open(db_file, &db)) {
  125. printf("Failed to open database %s: %s\n",
  126. db_file, sqlite3_errmsg(db));
  127. sqlite3_close(db);
  128. return NULL;
  129. }
  130. if (!db_table_exists(db, "milenage") &&
  131. db_table_create_milenage(db) < 0) {
  132. sqlite3_close(db);
  133. return NULL;
  134. }
  135. return db;
  136. }
  137. static int get_milenage_cb(void *ctx, int argc, char *argv[], char *col[])
  138. {
  139. struct milenage_parameters *m = ctx;
  140. int i;
  141. m->set = 1;
  142. for (i = 0; i < argc; i++) {
  143. if (os_strcmp(col[i], "ki") == 0 && argv[i] &&
  144. hexstr2bin(argv[i], m->ki, sizeof(m->ki))) {
  145. printf("Invalid ki value in database\n");
  146. return -1;
  147. }
  148. if (os_strcmp(col[i], "opc") == 0 && argv[i] &&
  149. hexstr2bin(argv[i], m->opc, sizeof(m->opc))) {
  150. printf("Invalid opcvalue in database\n");
  151. return -1;
  152. }
  153. if (os_strcmp(col[i], "amf") == 0 && argv[i] &&
  154. hexstr2bin(argv[i], m->amf, sizeof(m->amf))) {
  155. printf("Invalid amf value in database\n");
  156. return -1;
  157. }
  158. if (os_strcmp(col[i], "sqn") == 0 && argv[i] &&
  159. hexstr2bin(argv[i], m->sqn, sizeof(m->sqn))) {
  160. printf("Invalid sqn value in database\n");
  161. return -1;
  162. }
  163. }
  164. return 0;
  165. }
  166. static struct milenage_parameters * db_get_milenage(const char *imsi_txt)
  167. {
  168. char cmd[128];
  169. unsigned long long imsi;
  170. os_memset(&db_tmp_milenage, 0, sizeof(db_tmp_milenage));
  171. imsi = atoll(imsi_txt);
  172. os_snprintf(db_tmp_milenage.imsi, sizeof(db_tmp_milenage.imsi),
  173. "%llu", imsi);
  174. os_snprintf(cmd, sizeof(cmd),
  175. "SELECT ki,opc,amf,sqn FROM milenage WHERE imsi=%llu;",
  176. imsi);
  177. if (sqlite3_exec(sqlite_db, cmd, get_milenage_cb, &db_tmp_milenage,
  178. NULL) != SQLITE_OK)
  179. return NULL;
  180. if (!db_tmp_milenage.set)
  181. return NULL;
  182. return &db_tmp_milenage;
  183. }
  184. static int db_update_milenage_sqn(struct milenage_parameters *m)
  185. {
  186. char cmd[128], val[13], *pos;
  187. if (sqlite_db == NULL)
  188. return 0;
  189. pos = val;
  190. pos += wpa_snprintf_hex(pos, sizeof(val), m->sqn, 6);
  191. *pos = '\0';
  192. os_snprintf(cmd, sizeof(cmd),
  193. "UPDATE milenage SET sqn='%s' WHERE imsi=%s;",
  194. val, m->imsi);
  195. if (sqlite3_exec(sqlite_db, cmd, NULL, NULL, NULL) != SQLITE_OK) {
  196. printf("Failed to update SQN in database for IMSI %s\n",
  197. m->imsi);
  198. return -1;
  199. }
  200. return 0;
  201. }
  202. #endif /* CONFIG_SQLITE */
  203. static int open_socket(const char *path)
  204. {
  205. struct sockaddr_un addr;
  206. int s;
  207. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  208. if (s < 0) {
  209. perror("socket(PF_UNIX)");
  210. return -1;
  211. }
  212. memset(&addr, 0, sizeof(addr));
  213. addr.sun_family = AF_UNIX;
  214. os_strlcpy(addr.sun_path, path, sizeof(addr.sun_path));
  215. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  216. perror("hlr-auc-gw: bind(PF_UNIX)");
  217. close(s);
  218. return -1;
  219. }
  220. return s;
  221. }
  222. static int read_gsm_triplets(const char *fname)
  223. {
  224. FILE *f;
  225. char buf[200], *pos, *pos2;
  226. struct gsm_triplet *g = NULL;
  227. int line, ret = 0;
  228. if (fname == NULL)
  229. return -1;
  230. f = fopen(fname, "r");
  231. if (f == NULL) {
  232. printf("Could not open GSM tripler data file '%s'\n", fname);
  233. return -1;
  234. }
  235. line = 0;
  236. while (fgets(buf, sizeof(buf), f)) {
  237. line++;
  238. /* Parse IMSI:Kc:SRES:RAND */
  239. buf[sizeof(buf) - 1] = '\0';
  240. if (buf[0] == '#')
  241. continue;
  242. pos = buf;
  243. while (*pos != '\0' && *pos != '\n')
  244. pos++;
  245. if (*pos == '\n')
  246. *pos = '\0';
  247. pos = buf;
  248. if (*pos == '\0')
  249. continue;
  250. g = os_zalloc(sizeof(*g));
  251. if (g == NULL) {
  252. ret = -1;
  253. break;
  254. }
  255. /* IMSI */
  256. pos2 = strchr(pos, ':');
  257. if (pos2 == NULL) {
  258. printf("%s:%d - Invalid IMSI (%s)\n",
  259. fname, line, pos);
  260. ret = -1;
  261. break;
  262. }
  263. *pos2 = '\0';
  264. if (strlen(pos) >= sizeof(g->imsi)) {
  265. printf("%s:%d - Too long IMSI (%s)\n",
  266. fname, line, pos);
  267. ret = -1;
  268. break;
  269. }
  270. os_strlcpy(g->imsi, pos, sizeof(g->imsi));
  271. pos = pos2 + 1;
  272. /* Kc */
  273. pos2 = strchr(pos, ':');
  274. if (pos2 == NULL) {
  275. printf("%s:%d - Invalid Kc (%s)\n", fname, line, pos);
  276. ret = -1;
  277. break;
  278. }
  279. *pos2 = '\0';
  280. if (strlen(pos) != 16 || hexstr2bin(pos, g->kc, 8)) {
  281. printf("%s:%d - Invalid Kc (%s)\n", fname, line, pos);
  282. ret = -1;
  283. break;
  284. }
  285. pos = pos2 + 1;
  286. /* SRES */
  287. pos2 = strchr(pos, ':');
  288. if (pos2 == NULL) {
  289. printf("%s:%d - Invalid SRES (%s)\n", fname, line,
  290. pos);
  291. ret = -1;
  292. break;
  293. }
  294. *pos2 = '\0';
  295. if (strlen(pos) != 8 || hexstr2bin(pos, g->sres, 4)) {
  296. printf("%s:%d - Invalid SRES (%s)\n", fname, line,
  297. pos);
  298. ret = -1;
  299. break;
  300. }
  301. pos = pos2 + 1;
  302. /* RAND */
  303. pos2 = strchr(pos, ':');
  304. if (pos2)
  305. *pos2 = '\0';
  306. if (strlen(pos) != 32 || hexstr2bin(pos, g->_rand, 16)) {
  307. printf("%s:%d - Invalid RAND (%s)\n", fname, line,
  308. pos);
  309. ret = -1;
  310. break;
  311. }
  312. pos = pos2 + 1;
  313. g->next = gsm_db;
  314. gsm_db = g;
  315. g = NULL;
  316. }
  317. os_free(g);
  318. fclose(f);
  319. return ret;
  320. }
  321. static struct gsm_triplet * get_gsm_triplet(const char *imsi)
  322. {
  323. struct gsm_triplet *g = gsm_db_pos;
  324. while (g) {
  325. if (strcmp(g->imsi, imsi) == 0) {
  326. gsm_db_pos = g->next;
  327. return g;
  328. }
  329. g = g->next;
  330. }
  331. g = gsm_db;
  332. while (g && g != gsm_db_pos) {
  333. if (strcmp(g->imsi, imsi) == 0) {
  334. gsm_db_pos = g->next;
  335. return g;
  336. }
  337. g = g->next;
  338. }
  339. return NULL;
  340. }
  341. static int read_milenage(const char *fname)
  342. {
  343. FILE *f;
  344. char buf[200], *pos, *pos2;
  345. struct milenage_parameters *m = NULL;
  346. int line, ret = 0;
  347. if (fname == NULL)
  348. return -1;
  349. f = fopen(fname, "r");
  350. if (f == NULL) {
  351. printf("Could not open Milenage data file '%s'\n", fname);
  352. return -1;
  353. }
  354. line = 0;
  355. while (fgets(buf, sizeof(buf), f)) {
  356. line++;
  357. /* Parse IMSI Ki OPc AMF SQN */
  358. buf[sizeof(buf) - 1] = '\0';
  359. if (buf[0] == '#')
  360. continue;
  361. pos = buf;
  362. while (*pos != '\0' && *pos != '\n')
  363. pos++;
  364. if (*pos == '\n')
  365. *pos = '\0';
  366. pos = buf;
  367. if (*pos == '\0')
  368. continue;
  369. m = os_zalloc(sizeof(*m));
  370. if (m == NULL) {
  371. ret = -1;
  372. break;
  373. }
  374. /* IMSI */
  375. pos2 = strchr(pos, ' ');
  376. if (pos2 == NULL) {
  377. printf("%s:%d - Invalid IMSI (%s)\n",
  378. fname, line, pos);
  379. ret = -1;
  380. break;
  381. }
  382. *pos2 = '\0';
  383. if (strlen(pos) >= sizeof(m->imsi)) {
  384. printf("%s:%d - Too long IMSI (%s)\n",
  385. fname, line, pos);
  386. ret = -1;
  387. break;
  388. }
  389. os_strlcpy(m->imsi, pos, sizeof(m->imsi));
  390. pos = pos2 + 1;
  391. /* Ki */
  392. pos2 = strchr(pos, ' ');
  393. if (pos2 == NULL) {
  394. printf("%s:%d - Invalid Ki (%s)\n", fname, line, pos);
  395. ret = -1;
  396. break;
  397. }
  398. *pos2 = '\0';
  399. if (strlen(pos) != 32 || hexstr2bin(pos, m->ki, 16)) {
  400. printf("%s:%d - Invalid Ki (%s)\n", fname, line, pos);
  401. ret = -1;
  402. break;
  403. }
  404. pos = pos2 + 1;
  405. /* OPc */
  406. pos2 = strchr(pos, ' ');
  407. if (pos2 == NULL) {
  408. printf("%s:%d - Invalid OPc (%s)\n", fname, line, pos);
  409. ret = -1;
  410. break;
  411. }
  412. *pos2 = '\0';
  413. if (strlen(pos) != 32 || hexstr2bin(pos, m->opc, 16)) {
  414. printf("%s:%d - Invalid OPc (%s)\n", fname, line, pos);
  415. ret = -1;
  416. break;
  417. }
  418. pos = pos2 + 1;
  419. /* AMF */
  420. pos2 = strchr(pos, ' ');
  421. if (pos2 == NULL) {
  422. printf("%s:%d - Invalid AMF (%s)\n", fname, line, pos);
  423. ret = -1;
  424. break;
  425. }
  426. *pos2 = '\0';
  427. if (strlen(pos) != 4 || hexstr2bin(pos, m->amf, 2)) {
  428. printf("%s:%d - Invalid AMF (%s)\n", fname, line, pos);
  429. ret = -1;
  430. break;
  431. }
  432. pos = pos2 + 1;
  433. /* SQN */
  434. pos2 = strchr(pos, ' ');
  435. if (pos2)
  436. *pos2 = '\0';
  437. if (strlen(pos) != 12 || hexstr2bin(pos, m->sqn, 6)) {
  438. printf("%s:%d - Invalid SEQ (%s)\n", fname, line, pos);
  439. ret = -1;
  440. break;
  441. }
  442. pos = pos2 + 1;
  443. m->next = milenage_db;
  444. milenage_db = m;
  445. m = NULL;
  446. }
  447. os_free(m);
  448. fclose(f);
  449. return ret;
  450. }
  451. static void update_milenage_file(const char *fname)
  452. {
  453. FILE *f, *f2;
  454. char buf[500], *pos;
  455. char *end = buf + sizeof(buf);
  456. struct milenage_parameters *m;
  457. size_t imsi_len;
  458. f = fopen(fname, "r");
  459. if (f == NULL) {
  460. printf("Could not open Milenage data file '%s'\n", fname);
  461. return;
  462. }
  463. snprintf(buf, sizeof(buf), "%s.new", fname);
  464. f2 = fopen(buf, "w");
  465. if (f2 == NULL) {
  466. printf("Could not write Milenage data file '%s'\n", buf);
  467. fclose(f);
  468. return;
  469. }
  470. while (fgets(buf, sizeof(buf), f)) {
  471. /* IMSI Ki OPc AMF SQN */
  472. buf[sizeof(buf) - 1] = '\0';
  473. pos = strchr(buf, ' ');
  474. if (buf[0] == '#' || pos == NULL || pos - buf >= 20)
  475. goto no_update;
  476. imsi_len = pos - buf;
  477. for (m = milenage_db; m; m = m->next) {
  478. if (strncmp(buf, m->imsi, imsi_len) == 0 &&
  479. m->imsi[imsi_len] == '\0')
  480. break;
  481. }
  482. if (!m)
  483. goto no_update;
  484. pos = buf;
  485. pos += snprintf(pos, end - pos, "%s ", m->imsi);
  486. pos += wpa_snprintf_hex(pos, end - pos, m->ki, 16);
  487. *pos++ = ' ';
  488. pos += wpa_snprintf_hex(pos, end - pos, m->opc, 16);
  489. *pos++ = ' ';
  490. pos += wpa_snprintf_hex(pos, end - pos, m->amf, 2);
  491. *pos++ = ' ';
  492. pos += wpa_snprintf_hex(pos, end - pos, m->sqn, 6);
  493. *pos++ = '\n';
  494. no_update:
  495. fprintf(f2, "%s", buf);
  496. }
  497. fclose(f2);
  498. fclose(f);
  499. snprintf(buf, sizeof(buf), "%s.bak", fname);
  500. if (rename(fname, buf) < 0) {
  501. perror("rename");
  502. return;
  503. }
  504. snprintf(buf, sizeof(buf), "%s.new", fname);
  505. if (rename(buf, fname) < 0) {
  506. perror("rename");
  507. return;
  508. }
  509. }
  510. static struct milenage_parameters * get_milenage(const char *imsi)
  511. {
  512. struct milenage_parameters *m = milenage_db;
  513. while (m) {
  514. if (strcmp(m->imsi, imsi) == 0)
  515. break;
  516. m = m->next;
  517. }
  518. #ifdef CONFIG_SQLITE
  519. if (!m)
  520. m = db_get_milenage(imsi);
  521. #endif /* CONFIG_SQLITE */
  522. return m;
  523. }
  524. static int sim_req_auth(char *imsi, char *resp, size_t resp_len)
  525. {
  526. int count, max_chal, ret;
  527. char *pos;
  528. char *rpos, *rend;
  529. struct milenage_parameters *m;
  530. struct gsm_triplet *g;
  531. resp[0] = '\0';
  532. pos = strchr(imsi, ' ');
  533. if (pos) {
  534. *pos++ = '\0';
  535. max_chal = atoi(pos);
  536. if (max_chal < 1 || max_chal > EAP_SIM_MAX_CHAL)
  537. max_chal = EAP_SIM_MAX_CHAL;
  538. } else
  539. max_chal = EAP_SIM_MAX_CHAL;
  540. rend = resp + resp_len;
  541. rpos = resp;
  542. ret = snprintf(rpos, rend - rpos, "SIM-RESP-AUTH %s", imsi);
  543. if (ret < 0 || ret >= rend - rpos)
  544. return -1;
  545. rpos += ret;
  546. m = get_milenage(imsi);
  547. if (m) {
  548. u8 _rand[16], sres[4], kc[8];
  549. for (count = 0; count < max_chal; count++) {
  550. if (random_get_bytes(_rand, 16) < 0)
  551. return -1;
  552. gsm_milenage(m->opc, m->ki, _rand, sres, kc);
  553. *rpos++ = ' ';
  554. rpos += wpa_snprintf_hex(rpos, rend - rpos, kc, 8);
  555. *rpos++ = ':';
  556. rpos += wpa_snprintf_hex(rpos, rend - rpos, sres, 4);
  557. *rpos++ = ':';
  558. rpos += wpa_snprintf_hex(rpos, rend - rpos, _rand, 16);
  559. }
  560. *rpos = '\0';
  561. return 0;
  562. }
  563. count = 0;
  564. while (count < max_chal && (g = get_gsm_triplet(imsi))) {
  565. if (strcmp(g->imsi, imsi) != 0)
  566. continue;
  567. if (rpos < rend)
  568. *rpos++ = ' ';
  569. rpos += wpa_snprintf_hex(rpos, rend - rpos, g->kc, 8);
  570. if (rpos < rend)
  571. *rpos++ = ':';
  572. rpos += wpa_snprintf_hex(rpos, rend - rpos, g->sres, 4);
  573. if (rpos < rend)
  574. *rpos++ = ':';
  575. rpos += wpa_snprintf_hex(rpos, rend - rpos, g->_rand, 16);
  576. count++;
  577. }
  578. if (count == 0) {
  579. printf("No GSM triplets found for %s\n", imsi);
  580. ret = snprintf(rpos, rend - rpos, " FAILURE");
  581. if (ret < 0 || ret >= rend - rpos)
  582. return -1;
  583. rpos += ret;
  584. }
  585. return 0;
  586. }
  587. static int gsm_auth_req(char *imsi, char *resp, size_t resp_len)
  588. {
  589. int count, ret;
  590. char *pos, *rpos, *rend;
  591. struct milenage_parameters *m;
  592. resp[0] = '\0';
  593. pos = os_strchr(imsi, ' ');
  594. if (!pos)
  595. return -1;
  596. *pos++ = '\0';
  597. rend = resp + resp_len;
  598. rpos = resp;
  599. ret = os_snprintf(rpos, rend - rpos, "GSM-AUTH-RESP %s", imsi);
  600. if (os_snprintf_error(rend - rpos, ret))
  601. return -1;
  602. rpos += ret;
  603. m = get_milenage(imsi);
  604. if (m) {
  605. u8 _rand[16], sres[4], kc[8];
  606. for (count = 0; count < EAP_SIM_MAX_CHAL; count++) {
  607. if (hexstr2bin(pos, _rand, 16) != 0)
  608. return -1;
  609. gsm_milenage(m->opc, m->ki, _rand, sres, kc);
  610. *rpos++ = count == 0 ? ' ' : ':';
  611. rpos += wpa_snprintf_hex(rpos, rend - rpos, kc, 8);
  612. *rpos++ = ':';
  613. rpos += wpa_snprintf_hex(rpos, rend - rpos, sres, 4);
  614. pos += 16 * 2;
  615. if (*pos != ':')
  616. break;
  617. pos++;
  618. }
  619. *rpos = '\0';
  620. return 0;
  621. }
  622. printf("No GSM triplets found for %s\n", imsi);
  623. ret = os_snprintf(rpos, rend - rpos, " FAILURE");
  624. if (os_snprintf_error(rend - rpos, ret))
  625. return -1;
  626. rpos += ret;
  627. return 0;
  628. }
  629. static void inc_sqn(u8 *sqn)
  630. {
  631. u64 val, seq, ind;
  632. /*
  633. * SQN = SEQ | IND = SEQ1 | SEQ2 | IND
  634. *
  635. * The mechanism used here is not time-based, so SEQ2 is void and
  636. * SQN = SEQ1 | IND. The length of IND is ind_len bits and the length
  637. * of SEQ1 is 48 - ind_len bits.
  638. */
  639. /* Increment both SEQ and IND by one */
  640. val = ((u64) WPA_GET_BE32(sqn) << 16) | ((u64) WPA_GET_BE16(sqn + 4));
  641. seq = (val >> ind_len) + 1;
  642. ind = (val + 1) & ((1 << ind_len) - 1);
  643. val = (seq << ind_len) | ind;
  644. WPA_PUT_BE32(sqn, val >> 16);
  645. WPA_PUT_BE16(sqn + 4, val & 0xffff);
  646. }
  647. static int aka_req_auth(char *imsi, char *resp, size_t resp_len)
  648. {
  649. /* AKA-RESP-AUTH <IMSI> <RAND> <AUTN> <IK> <CK> <RES> */
  650. char *pos, *end;
  651. u8 _rand[EAP_AKA_RAND_LEN];
  652. u8 autn[EAP_AKA_AUTN_LEN];
  653. u8 ik[EAP_AKA_IK_LEN];
  654. u8 ck[EAP_AKA_CK_LEN];
  655. u8 res[EAP_AKA_RES_MAX_LEN];
  656. size_t res_len;
  657. int ret;
  658. struct milenage_parameters *m;
  659. int failed = 0;
  660. m = get_milenage(imsi);
  661. if (m) {
  662. if (random_get_bytes(_rand, EAP_AKA_RAND_LEN) < 0)
  663. return -1;
  664. res_len = EAP_AKA_RES_MAX_LEN;
  665. inc_sqn(m->sqn);
  666. #ifdef CONFIG_SQLITE
  667. db_update_milenage_sqn(m);
  668. #endif /* CONFIG_SQLITE */
  669. sqn_changes = 1;
  670. if (stdout_debug) {
  671. printf("AKA: Milenage with SQN=%02x%02x%02x%02x%02x%02x\n",
  672. m->sqn[0], m->sqn[1], m->sqn[2],
  673. m->sqn[3], m->sqn[4], m->sqn[5]);
  674. }
  675. milenage_generate(m->opc, m->amf, m->ki, m->sqn, _rand,
  676. autn, ik, ck, res, &res_len);
  677. } else {
  678. printf("Unknown IMSI: %s\n", imsi);
  679. #ifdef AKA_USE_FIXED_TEST_VALUES
  680. printf("Using fixed test values for AKA\n");
  681. memset(_rand, '0', EAP_AKA_RAND_LEN);
  682. memset(autn, '1', EAP_AKA_AUTN_LEN);
  683. memset(ik, '3', EAP_AKA_IK_LEN);
  684. memset(ck, '4', EAP_AKA_CK_LEN);
  685. memset(res, '2', EAP_AKA_RES_MAX_LEN);
  686. res_len = EAP_AKA_RES_MAX_LEN;
  687. #else /* AKA_USE_FIXED_TEST_VALUES */
  688. failed = 1;
  689. #endif /* AKA_USE_FIXED_TEST_VALUES */
  690. }
  691. pos = resp;
  692. end = resp + resp_len;
  693. ret = snprintf(pos, end - pos, "AKA-RESP-AUTH %s ", imsi);
  694. if (ret < 0 || ret >= end - pos)
  695. return -1;
  696. pos += ret;
  697. if (failed) {
  698. ret = snprintf(pos, end - pos, "FAILURE");
  699. if (ret < 0 || ret >= end - pos)
  700. return -1;
  701. pos += ret;
  702. return 0;
  703. }
  704. pos += wpa_snprintf_hex(pos, end - pos, _rand, EAP_AKA_RAND_LEN);
  705. *pos++ = ' ';
  706. pos += wpa_snprintf_hex(pos, end - pos, autn, EAP_AKA_AUTN_LEN);
  707. *pos++ = ' ';
  708. pos += wpa_snprintf_hex(pos, end - pos, ik, EAP_AKA_IK_LEN);
  709. *pos++ = ' ';
  710. pos += wpa_snprintf_hex(pos, end - pos, ck, EAP_AKA_CK_LEN);
  711. *pos++ = ' ';
  712. pos += wpa_snprintf_hex(pos, end - pos, res, res_len);
  713. return 0;
  714. }
  715. static int aka_auts(char *imsi, char *resp, size_t resp_len)
  716. {
  717. char *auts, *__rand;
  718. u8 _auts[EAP_AKA_AUTS_LEN], _rand[EAP_AKA_RAND_LEN], sqn[6];
  719. struct milenage_parameters *m;
  720. resp[0] = '\0';
  721. /* AKA-AUTS <IMSI> <AUTS> <RAND> */
  722. auts = strchr(imsi, ' ');
  723. if (auts == NULL)
  724. return -1;
  725. *auts++ = '\0';
  726. __rand = strchr(auts, ' ');
  727. if (__rand == NULL)
  728. return -1;
  729. *__rand++ = '\0';
  730. if (stdout_debug) {
  731. printf("AKA-AUTS: IMSI=%s AUTS=%s RAND=%s\n",
  732. imsi, auts, __rand);
  733. }
  734. if (hexstr2bin(auts, _auts, EAP_AKA_AUTS_LEN) ||
  735. hexstr2bin(__rand, _rand, EAP_AKA_RAND_LEN)) {
  736. printf("Could not parse AUTS/RAND\n");
  737. return -1;
  738. }
  739. m = get_milenage(imsi);
  740. if (m == NULL) {
  741. printf("Unknown IMSI: %s\n", imsi);
  742. return -1;
  743. }
  744. if (milenage_auts(m->opc, m->ki, _rand, _auts, sqn)) {
  745. printf("AKA-AUTS: Incorrect MAC-S\n");
  746. } else {
  747. memcpy(m->sqn, sqn, 6);
  748. if (stdout_debug) {
  749. printf("AKA-AUTS: Re-synchronized: "
  750. "SQN=%02x%02x%02x%02x%02x%02x\n",
  751. sqn[0], sqn[1], sqn[2], sqn[3], sqn[4], sqn[5]);
  752. }
  753. #ifdef CONFIG_SQLITE
  754. db_update_milenage_sqn(m);
  755. #endif /* CONFIG_SQLITE */
  756. sqn_changes = 1;
  757. }
  758. return 0;
  759. }
  760. static int process_cmd(char *cmd, char *resp, size_t resp_len)
  761. {
  762. if (os_strncmp(cmd, "SIM-REQ-AUTH ", 13) == 0)
  763. return sim_req_auth(cmd + 13, resp, resp_len);
  764. if (os_strncmp(cmd, "GSM-AUTH-REQ ", 13) == 0)
  765. return gsm_auth_req(cmd + 13, resp, resp_len);
  766. if (os_strncmp(cmd, "AKA-REQ-AUTH ", 13) == 0)
  767. return aka_req_auth(cmd + 13, resp, resp_len);
  768. if (os_strncmp(cmd, "AKA-AUTS ", 9) == 0)
  769. return aka_auts(cmd + 9, resp, resp_len);
  770. printf("Unknown request: %s\n", cmd);
  771. return -1;
  772. }
  773. static int process(int s)
  774. {
  775. char buf[1000], resp[1000];
  776. struct sockaddr_un from;
  777. socklen_t fromlen;
  778. ssize_t res;
  779. fromlen = sizeof(from);
  780. res = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *) &from,
  781. &fromlen);
  782. if (res < 0) {
  783. perror("recvfrom");
  784. return -1;
  785. }
  786. if (res == 0)
  787. return 0;
  788. if ((size_t) res >= sizeof(buf))
  789. res = sizeof(buf) - 1;
  790. buf[res] = '\0';
  791. printf("Received: %s\n", buf);
  792. if (process_cmd(buf, resp, sizeof(resp)) < 0) {
  793. printf("Failed to process request\n");
  794. return -1;
  795. }
  796. if (resp[0] == '\0') {
  797. printf("No response\n");
  798. return 0;
  799. }
  800. printf("Send: %s\n", resp);
  801. if (sendto(s, resp, os_strlen(resp), 0, (struct sockaddr *) &from,
  802. fromlen) < 0)
  803. perror("send");
  804. return 0;
  805. }
  806. static void cleanup(void)
  807. {
  808. struct gsm_triplet *g, *gprev;
  809. struct milenage_parameters *m, *prev;
  810. if (update_milenage && milenage_file && sqn_changes)
  811. update_milenage_file(milenage_file);
  812. g = gsm_db;
  813. while (g) {
  814. gprev = g;
  815. g = g->next;
  816. os_free(gprev);
  817. }
  818. m = milenage_db;
  819. while (m) {
  820. prev = m;
  821. m = m->next;
  822. os_free(prev);
  823. }
  824. if (serv_sock >= 0)
  825. close(serv_sock);
  826. if (socket_path)
  827. unlink(socket_path);
  828. #ifdef CONFIG_SQLITE
  829. if (sqlite_db) {
  830. sqlite3_close(sqlite_db);
  831. sqlite_db = NULL;
  832. }
  833. #endif /* CONFIG_SQLITE */
  834. }
  835. static void handle_term(int sig)
  836. {
  837. printf("Signal %d - terminate\n", sig);
  838. exit(0);
  839. }
  840. static void usage(void)
  841. {
  842. printf("HLR/AuC testing gateway for hostapd EAP-SIM/AKA "
  843. "database/authenticator\n"
  844. "Copyright (c) 2005-2007, 2012-2013, Jouni Malinen <j@w1.fi>\n"
  845. "\n"
  846. "usage:\n"
  847. "hlr_auc_gw [-hu] [-s<socket path>] [-g<triplet file>] "
  848. "[-m<milenage file>] \\\n"
  849. " [-D<DB file>] [-i<IND len in bits>] [command]\n"
  850. "\n"
  851. "options:\n"
  852. " -h = show this usage help\n"
  853. " -u = update SQN in Milenage file on exit\n"
  854. " -s<socket path> = path for UNIX domain socket\n"
  855. " (default: %s)\n"
  856. " -g<triplet file> = path for GSM authentication triplets\n"
  857. " -m<milenage file> = path for Milenage keys\n"
  858. " -D<DB file> = path to SQLite database\n"
  859. " -i<IND len in bits> = IND length for SQN (default: 5)\n"
  860. "\n"
  861. "If the optional command argument, like "
  862. "\"AKA-REQ-AUTH <IMSI>\" is used, a single\n"
  863. "command is processed with response sent to stdout. Otherwise, "
  864. "hlr_auc_gw opens\n"
  865. "a control interface and processes commands sent through it "
  866. "(e.g., by EAP server\n"
  867. "in hostapd).\n",
  868. default_socket_path);
  869. }
  870. int main(int argc, char *argv[])
  871. {
  872. int c;
  873. char *gsm_triplet_file = NULL;
  874. char *sqlite_db_file = NULL;
  875. int ret = 0;
  876. if (os_program_init())
  877. return -1;
  878. socket_path = default_socket_path;
  879. for (;;) {
  880. c = getopt(argc, argv, "D:g:hi:m:s:u");
  881. if (c < 0)
  882. break;
  883. switch (c) {
  884. case 'D':
  885. #ifdef CONFIG_SQLITE
  886. sqlite_db_file = optarg;
  887. break;
  888. #else /* CONFIG_SQLITE */
  889. printf("No SQLite support included in the build\n");
  890. return -1;
  891. #endif /* CONFIG_SQLITE */
  892. case 'g':
  893. gsm_triplet_file = optarg;
  894. break;
  895. case 'h':
  896. usage();
  897. return 0;
  898. case 'i':
  899. ind_len = atoi(optarg);
  900. if (ind_len < 0 || ind_len > 32) {
  901. printf("Invalid IND length\n");
  902. return -1;
  903. }
  904. break;
  905. case 'm':
  906. milenage_file = optarg;
  907. break;
  908. case 's':
  909. socket_path = optarg;
  910. break;
  911. case 'u':
  912. update_milenage = 1;
  913. break;
  914. default:
  915. usage();
  916. return -1;
  917. }
  918. }
  919. if (!gsm_triplet_file && !milenage_file && !sqlite_db_file) {
  920. usage();
  921. return -1;
  922. }
  923. #ifdef CONFIG_SQLITE
  924. if (sqlite_db_file && (sqlite_db = db_open(sqlite_db_file)) == NULL)
  925. return -1;
  926. #endif /* CONFIG_SQLITE */
  927. if (gsm_triplet_file && read_gsm_triplets(gsm_triplet_file) < 0)
  928. return -1;
  929. if (milenage_file && read_milenage(milenage_file) < 0)
  930. return -1;
  931. if (optind == argc) {
  932. serv_sock = open_socket(socket_path);
  933. if (serv_sock < 0)
  934. return -1;
  935. printf("Listening for requests on %s\n", socket_path);
  936. atexit(cleanup);
  937. signal(SIGTERM, handle_term);
  938. signal(SIGINT, handle_term);
  939. for (;;)
  940. process(serv_sock);
  941. } else {
  942. char buf[1000];
  943. socket_path = NULL;
  944. stdout_debug = 0;
  945. if (process_cmd(argv[optind], buf, sizeof(buf)) < 0) {
  946. printf("FAIL\n");
  947. ret = -1;
  948. } else {
  949. printf("%s\n", buf);
  950. }
  951. cleanup();
  952. }
  953. #ifdef CONFIG_SQLITE
  954. if (sqlite_db) {
  955. sqlite3_close(sqlite_db);
  956. sqlite_db = NULL;
  957. }
  958. #endif /* CONFIG_SQLITE */
  959. os_program_deinit();
  960. return ret;
  961. }