hlr_auc_gw.c 25 KB

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