hlr_auc_gw.c 18 KB

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