hlr_auc_gw.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. /*
  2. * HLR/AuC testing gateway for hostapd EAP-SIM/AKA database/authenticator
  3. * Copyright (c) 2005-2007, 2012-2017, 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 triplet 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 = NULL;
  262. pos = str_token(buf, ":", &pos2);
  263. if (!pos || os_strlen(pos) >= sizeof(g->imsi)) {
  264. printf("%s:%d - Invalid IMSI\n", fname, line);
  265. ret = -1;
  266. break;
  267. }
  268. os_strlcpy(g->imsi, pos, sizeof(g->imsi));
  269. /* Kc */
  270. pos = str_token(buf, ":", &pos2);
  271. if (!pos || os_strlen(pos) != 16 || hexstr2bin(pos, g->kc, 8)) {
  272. printf("%s:%d - Invalid Kc\n", fname, line);
  273. ret = -1;
  274. break;
  275. }
  276. /* SRES */
  277. pos = str_token(buf, ":", &pos2);
  278. if (!pos || os_strlen(pos) != 8 ||
  279. hexstr2bin(pos, g->sres, 4)) {
  280. printf("%s:%d - Invalid SRES\n", fname, line);
  281. ret = -1;
  282. break;
  283. }
  284. /* RAND */
  285. pos = str_token(buf, ":", &pos2);
  286. if (!pos || os_strlen(pos) != 32 ||
  287. hexstr2bin(pos, g->_rand, 16)) {
  288. printf("%s:%d - Invalid RAND\n", fname, line);
  289. ret = -1;
  290. break;
  291. }
  292. g->next = gsm_db;
  293. gsm_db = g;
  294. g = NULL;
  295. }
  296. os_free(g);
  297. fclose(f);
  298. return ret;
  299. }
  300. static struct gsm_triplet * get_gsm_triplet(const char *imsi)
  301. {
  302. struct gsm_triplet *g = gsm_db_pos;
  303. while (g) {
  304. if (strcmp(g->imsi, imsi) == 0) {
  305. gsm_db_pos = g->next;
  306. return g;
  307. }
  308. g = g->next;
  309. }
  310. g = gsm_db;
  311. while (g && g != gsm_db_pos) {
  312. if (strcmp(g->imsi, imsi) == 0) {
  313. gsm_db_pos = g->next;
  314. return g;
  315. }
  316. g = g->next;
  317. }
  318. return NULL;
  319. }
  320. static int read_milenage(const char *fname)
  321. {
  322. FILE *f;
  323. char buf[200], *pos, *pos2;
  324. struct milenage_parameters *m = NULL;
  325. int line, ret = 0;
  326. if (fname == NULL)
  327. return -1;
  328. f = fopen(fname, "r");
  329. if (f == NULL) {
  330. printf("Could not open Milenage data file '%s'\n", fname);
  331. return -1;
  332. }
  333. line = 0;
  334. while (fgets(buf, sizeof(buf), f)) {
  335. line++;
  336. /* Parse IMSI Ki OPc AMF SQN [RES_len] */
  337. buf[sizeof(buf) - 1] = '\0';
  338. if (buf[0] == '#')
  339. continue;
  340. pos = buf;
  341. while (*pos != '\0' && *pos != '\n')
  342. pos++;
  343. if (*pos == '\n')
  344. *pos = '\0';
  345. pos = buf;
  346. if (*pos == '\0')
  347. continue;
  348. m = os_zalloc(sizeof(*m));
  349. if (m == NULL) {
  350. ret = -1;
  351. break;
  352. }
  353. /* IMSI */
  354. pos2 = NULL;
  355. pos = str_token(buf, " ", &pos2);
  356. if (!pos || os_strlen(pos) >= sizeof(m->imsi)) {
  357. printf("%s:%d - Invalid IMSI\n", fname, line);
  358. ret = -1;
  359. break;
  360. }
  361. os_strlcpy(m->imsi, pos, sizeof(m->imsi));
  362. /* Ki */
  363. pos = str_token(buf, " ", &pos2);
  364. if (!pos || os_strlen(pos) != 32 ||
  365. hexstr2bin(pos, m->ki, 16)) {
  366. printf("%s:%d - Invalid Ki\n", fname, line);
  367. ret = -1;
  368. break;
  369. }
  370. /* OPc */
  371. pos = str_token(buf, " ", &pos2);
  372. if (!pos || os_strlen(pos) != 32 ||
  373. hexstr2bin(pos, m->opc, 16)) {
  374. printf("%s:%d - Invalid OPc\n", fname, line);
  375. ret = -1;
  376. break;
  377. }
  378. /* AMF */
  379. pos = str_token(buf, " ", &pos2);
  380. if (!pos || os_strlen(pos) != 4 || hexstr2bin(pos, m->amf, 2)) {
  381. printf("%s:%d - Invalid AMF\n", fname, line);
  382. ret = -1;
  383. break;
  384. }
  385. /* SQN */
  386. pos = str_token(buf, " ", &pos2);
  387. if (!pos || os_strlen(pos) != 12 ||
  388. hexstr2bin(pos, m->sqn, 6)) {
  389. printf("%s:%d - Invalid SEQ\n", fname, line);
  390. ret = -1;
  391. break;
  392. }
  393. pos = str_token(buf, " ", &pos2);
  394. if (pos) {
  395. m->res_len = atoi(pos);
  396. if (m->res_len &&
  397. (m->res_len < EAP_AKA_RES_MIN_LEN ||
  398. m->res_len > EAP_AKA_RES_MAX_LEN)) {
  399. printf("%s:%d - Invalid RES_len\n",
  400. fname, line);
  401. ret = -1;
  402. break;
  403. }
  404. }
  405. m->next = milenage_db;
  406. milenage_db = m;
  407. m = NULL;
  408. }
  409. os_free(m);
  410. fclose(f);
  411. return ret;
  412. }
  413. static void update_milenage_file(const char *fname)
  414. {
  415. FILE *f, *f2;
  416. char name[500], buf[500], *pos;
  417. char *end = buf + sizeof(buf);
  418. struct milenage_parameters *m;
  419. size_t imsi_len;
  420. f = fopen(fname, "r");
  421. if (f == NULL) {
  422. printf("Could not open Milenage data file '%s'\n", fname);
  423. return;
  424. }
  425. snprintf(name, sizeof(name), "%s.new", fname);
  426. f2 = fopen(name, "w");
  427. if (f2 == NULL) {
  428. printf("Could not write Milenage data file '%s'\n", name);
  429. fclose(f);
  430. return;
  431. }
  432. while (fgets(buf, sizeof(buf), f)) {
  433. /* IMSI Ki OPc AMF SQN */
  434. buf[sizeof(buf) - 1] = '\0';
  435. pos = strchr(buf, ' ');
  436. if (buf[0] == '#' || pos == NULL || pos - buf >= 20)
  437. goto no_update;
  438. imsi_len = pos - buf;
  439. for (m = milenage_db; m; m = m->next) {
  440. if (strncmp(buf, m->imsi, imsi_len) == 0 &&
  441. m->imsi[imsi_len] == '\0')
  442. break;
  443. }
  444. if (!m)
  445. goto no_update;
  446. pos = buf;
  447. pos += snprintf(pos, end - pos, "%s ", m->imsi);
  448. pos += wpa_snprintf_hex(pos, end - pos, m->ki, 16);
  449. *pos++ = ' ';
  450. pos += wpa_snprintf_hex(pos, end - pos, m->opc, 16);
  451. *pos++ = ' ';
  452. pos += wpa_snprintf_hex(pos, end - pos, m->amf, 2);
  453. *pos++ = ' ';
  454. pos += wpa_snprintf_hex(pos, end - pos, m->sqn, 6);
  455. *pos++ = '\n';
  456. no_update:
  457. fprintf(f2, "%s", buf);
  458. }
  459. fclose(f2);
  460. fclose(f);
  461. snprintf(name, sizeof(name), "%s.bak", fname);
  462. if (rename(fname, name) < 0) {
  463. perror("rename");
  464. return;
  465. }
  466. snprintf(name, sizeof(name), "%s.new", fname);
  467. if (rename(name, fname) < 0) {
  468. perror("rename");
  469. return;
  470. }
  471. }
  472. static struct milenage_parameters * get_milenage(const char *imsi)
  473. {
  474. struct milenage_parameters *m = milenage_db;
  475. while (m) {
  476. if (strcmp(m->imsi, imsi) == 0)
  477. break;
  478. m = m->next;
  479. }
  480. #ifdef CONFIG_SQLITE
  481. if (!m)
  482. m = db_get_milenage(imsi);
  483. #endif /* CONFIG_SQLITE */
  484. return m;
  485. }
  486. static int sim_req_auth(char *imsi, char *resp, size_t resp_len)
  487. {
  488. int count, max_chal, ret;
  489. char *pos;
  490. char *rpos, *rend;
  491. struct milenage_parameters *m;
  492. struct gsm_triplet *g;
  493. resp[0] = '\0';
  494. pos = strchr(imsi, ' ');
  495. if (pos) {
  496. *pos++ = '\0';
  497. max_chal = atoi(pos);
  498. if (max_chal < 1 || max_chal > EAP_SIM_MAX_CHAL)
  499. max_chal = EAP_SIM_MAX_CHAL;
  500. } else
  501. max_chal = EAP_SIM_MAX_CHAL;
  502. rend = resp + resp_len;
  503. rpos = resp;
  504. ret = snprintf(rpos, rend - rpos, "SIM-RESP-AUTH %s", imsi);
  505. if (ret < 0 || ret >= rend - rpos)
  506. return -1;
  507. rpos += ret;
  508. m = get_milenage(imsi);
  509. if (m) {
  510. u8 _rand[16], sres[4], kc[8];
  511. for (count = 0; count < max_chal; count++) {
  512. if (random_get_bytes(_rand, 16) < 0)
  513. return -1;
  514. gsm_milenage(m->opc, m->ki, _rand, sres, kc);
  515. *rpos++ = ' ';
  516. rpos += wpa_snprintf_hex(rpos, rend - rpos, kc, 8);
  517. *rpos++ = ':';
  518. rpos += wpa_snprintf_hex(rpos, rend - rpos, sres, 4);
  519. *rpos++ = ':';
  520. rpos += wpa_snprintf_hex(rpos, rend - rpos, _rand, 16);
  521. }
  522. *rpos = '\0';
  523. return 0;
  524. }
  525. count = 0;
  526. while (count < max_chal && (g = get_gsm_triplet(imsi))) {
  527. if (strcmp(g->imsi, imsi) != 0)
  528. continue;
  529. if (rpos < rend)
  530. *rpos++ = ' ';
  531. rpos += wpa_snprintf_hex(rpos, rend - rpos, g->kc, 8);
  532. if (rpos < rend)
  533. *rpos++ = ':';
  534. rpos += wpa_snprintf_hex(rpos, rend - rpos, g->sres, 4);
  535. if (rpos < rend)
  536. *rpos++ = ':';
  537. rpos += wpa_snprintf_hex(rpos, rend - rpos, g->_rand, 16);
  538. count++;
  539. }
  540. if (count == 0) {
  541. printf("No GSM triplets found for %s\n", imsi);
  542. ret = snprintf(rpos, rend - rpos, " FAILURE");
  543. if (ret < 0 || ret >= rend - rpos)
  544. return -1;
  545. rpos += ret;
  546. }
  547. return 0;
  548. }
  549. static int gsm_auth_req(char *imsi, char *resp, size_t resp_len)
  550. {
  551. int count, ret;
  552. char *pos, *rpos, *rend;
  553. struct milenage_parameters *m;
  554. resp[0] = '\0';
  555. pos = os_strchr(imsi, ' ');
  556. if (!pos)
  557. return -1;
  558. *pos++ = '\0';
  559. rend = resp + resp_len;
  560. rpos = resp;
  561. ret = os_snprintf(rpos, rend - rpos, "GSM-AUTH-RESP %s", imsi);
  562. if (os_snprintf_error(rend - rpos, ret))
  563. return -1;
  564. rpos += ret;
  565. m = get_milenage(imsi);
  566. if (m) {
  567. u8 _rand[16], sres[4], kc[8];
  568. for (count = 0; count < EAP_SIM_MAX_CHAL; count++) {
  569. if (hexstr2bin(pos, _rand, 16) != 0)
  570. return -1;
  571. gsm_milenage(m->opc, m->ki, _rand, sres, kc);
  572. *rpos++ = count == 0 ? ' ' : ':';
  573. rpos += wpa_snprintf_hex(rpos, rend - rpos, kc, 8);
  574. *rpos++ = ':';
  575. rpos += wpa_snprintf_hex(rpos, rend - rpos, sres, 4);
  576. pos += 16 * 2;
  577. if (*pos != ':')
  578. break;
  579. pos++;
  580. }
  581. *rpos = '\0';
  582. return 0;
  583. }
  584. printf("No GSM triplets found for %s\n", imsi);
  585. ret = os_snprintf(rpos, rend - rpos, " FAILURE");
  586. if (os_snprintf_error(rend - rpos, ret))
  587. return -1;
  588. rpos += ret;
  589. return 0;
  590. }
  591. static void inc_sqn(u8 *sqn)
  592. {
  593. u64 val, seq, ind;
  594. /*
  595. * SQN = SEQ | IND = SEQ1 | SEQ2 | IND
  596. *
  597. * The mechanism used here is not time-based, so SEQ2 is void and
  598. * SQN = SEQ1 | IND. The length of IND is ind_len bits and the length
  599. * of SEQ1 is 48 - ind_len bits.
  600. */
  601. /* Increment both SEQ and IND by one */
  602. val = ((u64) WPA_GET_BE32(sqn) << 16) | ((u64) WPA_GET_BE16(sqn + 4));
  603. seq = (val >> ind_len) + 1;
  604. ind = (val + 1) & ((1 << ind_len) - 1);
  605. val = (seq << ind_len) | ind;
  606. WPA_PUT_BE32(sqn, val >> 16);
  607. WPA_PUT_BE16(sqn + 4, val & 0xffff);
  608. }
  609. static int aka_req_auth(char *imsi, char *resp, size_t resp_len)
  610. {
  611. /* AKA-RESP-AUTH <IMSI> <RAND> <AUTN> <IK> <CK> <RES> */
  612. char *pos, *end;
  613. u8 _rand[EAP_AKA_RAND_LEN];
  614. u8 autn[EAP_AKA_AUTN_LEN];
  615. u8 ik[EAP_AKA_IK_LEN];
  616. u8 ck[EAP_AKA_CK_LEN];
  617. u8 res[EAP_AKA_RES_MAX_LEN];
  618. size_t res_len;
  619. int ret;
  620. struct milenage_parameters *m;
  621. int failed = 0;
  622. m = get_milenage(imsi);
  623. if (m) {
  624. if (random_get_bytes(_rand, EAP_AKA_RAND_LEN) < 0)
  625. return -1;
  626. res_len = EAP_AKA_RES_MAX_LEN;
  627. inc_sqn(m->sqn);
  628. #ifdef CONFIG_SQLITE
  629. db_update_milenage_sqn(m);
  630. #endif /* CONFIG_SQLITE */
  631. sqn_changes = 1;
  632. if (stdout_debug) {
  633. printf("AKA: Milenage with SQN=%02x%02x%02x%02x%02x%02x\n",
  634. m->sqn[0], m->sqn[1], m->sqn[2],
  635. m->sqn[3], m->sqn[4], m->sqn[5]);
  636. }
  637. milenage_generate(m->opc, m->amf, m->ki, m->sqn, _rand,
  638. autn, ik, ck, res, &res_len);
  639. if (m->res_len >= EAP_AKA_RES_MIN_LEN &&
  640. m->res_len <= EAP_AKA_RES_MAX_LEN &&
  641. m->res_len < res_len)
  642. res_len = m->res_len;
  643. } else {
  644. printf("Unknown IMSI: %s\n", imsi);
  645. #ifdef AKA_USE_FIXED_TEST_VALUES
  646. printf("Using fixed test values for AKA\n");
  647. memset(_rand, '0', EAP_AKA_RAND_LEN);
  648. memset(autn, '1', EAP_AKA_AUTN_LEN);
  649. memset(ik, '3', EAP_AKA_IK_LEN);
  650. memset(ck, '4', EAP_AKA_CK_LEN);
  651. memset(res, '2', EAP_AKA_RES_MAX_LEN);
  652. res_len = EAP_AKA_RES_MAX_LEN;
  653. #else /* AKA_USE_FIXED_TEST_VALUES */
  654. failed = 1;
  655. #endif /* AKA_USE_FIXED_TEST_VALUES */
  656. }
  657. pos = resp;
  658. end = resp + resp_len;
  659. ret = snprintf(pos, end - pos, "AKA-RESP-AUTH %s ", imsi);
  660. if (ret < 0 || ret >= end - pos)
  661. return -1;
  662. pos += ret;
  663. if (failed) {
  664. ret = snprintf(pos, end - pos, "FAILURE");
  665. if (ret < 0 || ret >= end - pos)
  666. return -1;
  667. pos += ret;
  668. return 0;
  669. }
  670. pos += wpa_snprintf_hex(pos, end - pos, _rand, EAP_AKA_RAND_LEN);
  671. *pos++ = ' ';
  672. pos += wpa_snprintf_hex(pos, end - pos, autn, EAP_AKA_AUTN_LEN);
  673. *pos++ = ' ';
  674. pos += wpa_snprintf_hex(pos, end - pos, ik, EAP_AKA_IK_LEN);
  675. *pos++ = ' ';
  676. pos += wpa_snprintf_hex(pos, end - pos, ck, EAP_AKA_CK_LEN);
  677. *pos++ = ' ';
  678. pos += wpa_snprintf_hex(pos, end - pos, res, res_len);
  679. return 0;
  680. }
  681. static int aka_auts(char *imsi, char *resp, size_t resp_len)
  682. {
  683. char *auts, *__rand;
  684. u8 _auts[EAP_AKA_AUTS_LEN], _rand[EAP_AKA_RAND_LEN], sqn[6];
  685. struct milenage_parameters *m;
  686. resp[0] = '\0';
  687. /* AKA-AUTS <IMSI> <AUTS> <RAND> */
  688. auts = strchr(imsi, ' ');
  689. if (auts == NULL)
  690. return -1;
  691. *auts++ = '\0';
  692. __rand = strchr(auts, ' ');
  693. if (__rand == NULL)
  694. return -1;
  695. *__rand++ = '\0';
  696. if (stdout_debug) {
  697. printf("AKA-AUTS: IMSI=%s AUTS=%s RAND=%s\n",
  698. imsi, auts, __rand);
  699. }
  700. if (hexstr2bin(auts, _auts, EAP_AKA_AUTS_LEN) ||
  701. hexstr2bin(__rand, _rand, EAP_AKA_RAND_LEN)) {
  702. printf("Could not parse AUTS/RAND\n");
  703. return -1;
  704. }
  705. m = get_milenage(imsi);
  706. if (m == NULL) {
  707. printf("Unknown IMSI: %s\n", imsi);
  708. return -1;
  709. }
  710. if (milenage_auts(m->opc, m->ki, _rand, _auts, sqn)) {
  711. printf("AKA-AUTS: Incorrect MAC-S\n");
  712. } else {
  713. memcpy(m->sqn, sqn, 6);
  714. if (stdout_debug) {
  715. printf("AKA-AUTS: Re-synchronized: "
  716. "SQN=%02x%02x%02x%02x%02x%02x\n",
  717. sqn[0], sqn[1], sqn[2], sqn[3], sqn[4], sqn[5]);
  718. }
  719. #ifdef CONFIG_SQLITE
  720. db_update_milenage_sqn(m);
  721. #endif /* CONFIG_SQLITE */
  722. sqn_changes = 1;
  723. }
  724. return 0;
  725. }
  726. static int process_cmd(char *cmd, char *resp, size_t resp_len)
  727. {
  728. if (os_strncmp(cmd, "SIM-REQ-AUTH ", 13) == 0)
  729. return sim_req_auth(cmd + 13, resp, resp_len);
  730. if (os_strncmp(cmd, "GSM-AUTH-REQ ", 13) == 0)
  731. return gsm_auth_req(cmd + 13, resp, resp_len);
  732. if (os_strncmp(cmd, "AKA-REQ-AUTH ", 13) == 0)
  733. return aka_req_auth(cmd + 13, resp, resp_len);
  734. if (os_strncmp(cmd, "AKA-AUTS ", 9) == 0)
  735. return aka_auts(cmd + 9, resp, resp_len);
  736. printf("Unknown request: %s\n", cmd);
  737. return -1;
  738. }
  739. static int process(int s)
  740. {
  741. char buf[1000], resp[1000];
  742. struct sockaddr_un from;
  743. socklen_t fromlen;
  744. ssize_t res;
  745. fromlen = sizeof(from);
  746. res = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *) &from,
  747. &fromlen);
  748. if (res < 0) {
  749. perror("recvfrom");
  750. return -1;
  751. }
  752. if (res == 0)
  753. return 0;
  754. if ((size_t) res >= sizeof(buf))
  755. res = sizeof(buf) - 1;
  756. buf[res] = '\0';
  757. printf("Received: %s\n", buf);
  758. if (process_cmd(buf, resp, sizeof(resp)) < 0) {
  759. printf("Failed to process request\n");
  760. return -1;
  761. }
  762. if (resp[0] == '\0') {
  763. printf("No response\n");
  764. return 0;
  765. }
  766. printf("Send: %s\n", resp);
  767. if (sendto(s, resp, os_strlen(resp), 0, (struct sockaddr *) &from,
  768. fromlen) < 0)
  769. perror("send");
  770. return 0;
  771. }
  772. static void cleanup(void)
  773. {
  774. struct gsm_triplet *g, *gprev;
  775. struct milenage_parameters *m, *prev;
  776. if (update_milenage && milenage_file && sqn_changes)
  777. update_milenage_file(milenage_file);
  778. g = gsm_db;
  779. while (g) {
  780. gprev = g;
  781. g = g->next;
  782. os_free(gprev);
  783. }
  784. m = milenage_db;
  785. while (m) {
  786. prev = m;
  787. m = m->next;
  788. os_free(prev);
  789. }
  790. if (serv_sock >= 0)
  791. close(serv_sock);
  792. if (socket_path)
  793. unlink(socket_path);
  794. #ifdef CONFIG_SQLITE
  795. if (sqlite_db) {
  796. sqlite3_close(sqlite_db);
  797. sqlite_db = NULL;
  798. }
  799. #endif /* CONFIG_SQLITE */
  800. }
  801. static void handle_term(int sig)
  802. {
  803. printf("Signal %d - terminate\n", sig);
  804. exit(0);
  805. }
  806. static void usage(void)
  807. {
  808. printf("HLR/AuC testing gateway for hostapd EAP-SIM/AKA "
  809. "database/authenticator\n"
  810. "Copyright (c) 2005-2017, Jouni Malinen <j@w1.fi>\n"
  811. "\n"
  812. "usage:\n"
  813. "hlr_auc_gw [-hu] [-s<socket path>] [-g<triplet file>] "
  814. "[-m<milenage file>] \\\n"
  815. " [-D<DB file>] [-i<IND len in bits>] [command]\n"
  816. "\n"
  817. "options:\n"
  818. " -h = show this usage help\n"
  819. " -u = update SQN in Milenage file on exit\n"
  820. " -s<socket path> = path for UNIX domain socket\n"
  821. " (default: %s)\n"
  822. " -g<triplet file> = path for GSM authentication triplets\n"
  823. " -m<milenage file> = path for Milenage keys\n"
  824. " -D<DB file> = path to SQLite database\n"
  825. " -i<IND len in bits> = IND length for SQN (default: 5)\n"
  826. "\n"
  827. "If the optional command argument, like "
  828. "\"AKA-REQ-AUTH <IMSI>\" is used, a single\n"
  829. "command is processed with response sent to stdout. Otherwise, "
  830. "hlr_auc_gw opens\n"
  831. "a control interface and processes commands sent through it "
  832. "(e.g., by EAP server\n"
  833. "in hostapd).\n",
  834. default_socket_path);
  835. }
  836. int main(int argc, char *argv[])
  837. {
  838. int c;
  839. char *gsm_triplet_file = NULL;
  840. char *sqlite_db_file = NULL;
  841. int ret = 0;
  842. if (os_program_init())
  843. return -1;
  844. socket_path = default_socket_path;
  845. for (;;) {
  846. c = getopt(argc, argv, "D:g:hi:m:s:u");
  847. if (c < 0)
  848. break;
  849. switch (c) {
  850. case 'D':
  851. #ifdef CONFIG_SQLITE
  852. sqlite_db_file = optarg;
  853. break;
  854. #else /* CONFIG_SQLITE */
  855. printf("No SQLite support included in the build\n");
  856. return -1;
  857. #endif /* CONFIG_SQLITE */
  858. case 'g':
  859. gsm_triplet_file = optarg;
  860. break;
  861. case 'h':
  862. usage();
  863. return 0;
  864. case 'i':
  865. ind_len = atoi(optarg);
  866. if (ind_len < 0 || ind_len > 32) {
  867. printf("Invalid IND length\n");
  868. return -1;
  869. }
  870. break;
  871. case 'm':
  872. milenage_file = optarg;
  873. break;
  874. case 's':
  875. socket_path = optarg;
  876. break;
  877. case 'u':
  878. update_milenage = 1;
  879. break;
  880. default:
  881. usage();
  882. return -1;
  883. }
  884. }
  885. if (!gsm_triplet_file && !milenage_file && !sqlite_db_file) {
  886. usage();
  887. return -1;
  888. }
  889. #ifdef CONFIG_SQLITE
  890. if (sqlite_db_file && (sqlite_db = db_open(sqlite_db_file)) == NULL)
  891. return -1;
  892. #endif /* CONFIG_SQLITE */
  893. if (gsm_triplet_file && read_gsm_triplets(gsm_triplet_file) < 0)
  894. return -1;
  895. if (milenage_file && read_milenage(milenage_file) < 0)
  896. return -1;
  897. if (optind == argc) {
  898. serv_sock = open_socket(socket_path);
  899. if (serv_sock < 0)
  900. return -1;
  901. printf("Listening for requests on %s\n", socket_path);
  902. atexit(cleanup);
  903. signal(SIGTERM, handle_term);
  904. signal(SIGINT, handle_term);
  905. for (;;)
  906. process(serv_sock);
  907. } else {
  908. char buf[1000];
  909. socket_path = NULL;
  910. stdout_debug = 0;
  911. if (process_cmd(argv[optind], buf, sizeof(buf)) < 0) {
  912. printf("FAIL\n");
  913. ret = -1;
  914. } else {
  915. printf("%s\n", buf);
  916. }
  917. cleanup();
  918. }
  919. #ifdef CONFIG_SQLITE
  920. if (sqlite_db) {
  921. sqlite3_close(sqlite_db);
  922. sqlite_db = NULL;
  923. }
  924. #endif /* CONFIG_SQLITE */
  925. os_program_deinit();
  926. return ret;
  927. }