hlr_auc_gw.c 22 KB

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