hlr_auc_gw.c 22 KB

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